<commit_before><?php
/**
 * Creates a post to test all Gutenberg blocks.
 *
 * @codeCoverageIgnore
 * @package AMP
 */

/**
 * Gets many of the Gutenberg fixture blocks in blocks/tests/fixtures/.
 *
 * @throws Exception If this is script is not run inside the plugin directory.
 * @return string $content Post content with all Gutenberg blocks.
 */
function amp_get_blocks() {
	$fixtures_dir = dirname( dirname( __DIR__ ) ) . '/gutenberg/blocks/test/fixtures';
	$content      = amp_get_block_permutations();
	if ( ! is_dir( $fixtures_dir ) ) {
		$fixtures_dir = dirname( $fixtures_dir );
		if ( ! is_dir( $fixtures_dir ) ) {
			throw new Exception( 'Please run this script from the AMP plugin root.' );
		}
	}

	foreach ( glob( $fixtures_dir . '/*.html' ) as $file ) {
		if ( ! preg_match( '/(serialized|embed|shortcode|custom-text-teaser)/', $file ) ) {
			// Add the block's title.
			if ( preg_match( ':core__(?P<block>.+)\.html:s', basename( $file ), $matches ) ) {
				$content .= sprintf( '<h1>%s</h1>', $matches['block'] );
			}
			$content .= file_get_contents( $file ); // @codingStandardsIgnoreLine: file_get_contents_file_get_contents, file_system_read_file_get_contents.
		}
	}

	// Replace broken URLs in fixture files.
	$content = str_replace( 'http://google.com/hi.png', 'https://cldup.com/-3VMmmrPm9.jpg', $content );
	$content = str_replace( 'https://awesome-fake.video/file.mp4', 'https://videos.files.wordpress.com/DK5mLrbr/video-ca6dc0ab4a_hd.mp4', $content );
	return $content;
}

/**
 * Gets the Gutenberg block permutations.
 *
 * These are mostly copied from gutenberg/blocks/test/fixtures, and slightly modified.
 * Embeds and shortcodes are tested in a separate script, so this does not have have many.
 *
 * @return string $content The blocks as HTML.
 */
function amp_get_block_permutations() {
	$blocks = array(
		array(
			'title'   => 'Categories With Dropdown',
			'content' => '<!-- wp:core/categories {"showPostCounts":false,"displayAsDropdown":true,"showHierarchy":false} /-->',
		),
		array(
			'title'   => 'Columns, With 2 Columns',
			'content' => '<!-- wp:columns {"columns":2} --><div class="wp-block-columns has-2-columns"><!-- wp:paragraph {"layout":"column-1"} -->	<p class="layout-column-1">Column One, Paragraph One</p><!-- /wp:paragraph --><!-- wp:paragraph {"layout":"column-1"} --><p class="layout-column-1">Column One, Paragraph Two</p><!-- /wp:paragraph --><!-- wp:paragraph {"layout":"column-2"} --><p class="layout-column-2">Column Two, Paragraph One</p><!-- /wp:paragraph --></div><!-- /wp:columns -->',
		),
		array(
			'title'   => 'Cover Image With Fixed Background',
			'content' => '<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","dimRatio":40} --><section class="wp-block-cover-image has-background-dim-40 has-background-dim has-parallax" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)"><h2>Guten Berg!</h2></section><!-- /wp:core/cover-image -->',
		),
		array(
			'title'   => 'WordPress Embed',
			'content' => '<!-- wp:core-embed/wordpress {"url":"https://make.wordpress.org/core/2017/12/11/whats-new-in-gutenberg-11th-december/"} --><figure class="wp-block-embed-wordpress wp-block-embed">https://make.wordpress.org/core/2017/12/11/whats-new-in-gutenberg-11th-december/<figcaption>Embedded content from WordPress</figcaption></figure><!-- /wp:core-embed/wordpress -->',
		),
		array(
			'title'   => 'YouTube Embed',
			'content' => '<!-- wp:core-embed/youtube {"url":"https://www.youtube.com/watch?v=GGS-tKTXw4Y"} --><figure class="wp-block-embed-youtube wp-block-embed">https://www.youtube.com/watch?v=GGS-tKTXw4Y<figcaption>Embedded content from youtube</figcaption></figure><!-- /wp:core-embed/youtube -->',
		),
		array(
			'title'   => 'Twitter Embed',
			'content' => '<!-- wp:core-embed/twitter {"url":"https://twitter.com/AMPhtml/status/963443140005957632"} --><figure class="wp-block-embed-twitter wp-block-embed">https://twitter.com/AMPhtml/status/963443140005957632<figcaption>We are Automattic</figcaption></figure><!-- /wp:core-embed/twitter -->',
		),
		array(
			'title'   => 'Gallery With 3 Columns',
			'content' => '<!-- wp:core/gallery --><ul class="wp-block-gallery alignnone columns-3 is-cropped"><li class="blocks-gallery-item"><figure><img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title" /></figure></li><li class="blocks-gallery-item"><figure><img src="https://cldup.com/-3VMmmrPm9.jpg" alt="title" /></figure></li><li class="blocks-gallery-item"><figure><img src="https://cldup.com/aMbxBM0zAi.jpg" alt="title" /></figure></li></ul><!-- /wp:core/gallery -->',
		),
		array(
			'title'   => 'Audio Shortcode',
			'content' => '<!-- wp:core/shortcode -->[audio src=https://wptavern.com/wp-content/uploads/2017/11/EPISODE-296-Gutenberg-Telemetry-Calypso-and-More-With-Matt-Mullenweg.mp3]<!-- /wp:core/shortcode -->',
		),
		array(
			'title'   => 'Caption Shortcode',
			'content' => '<!-- wp:core/shortcode -->[caption width=150]This is a caption[/caption]<!-- /wp:core/shortcode -->',
		),
	);

	$content = '';
	foreach ( $blocks as $block ) {
		$content .= sprintf( '<h1>%s</h1>', $block['title'] );
		$content .= $block['content'];
	}

	return $content;
}

/**
 * Creates a Gutenberg test post (page).
 *
 * @throws Exception If there is an error in creating the test page.
 * @param string $content The content to add to the post.
 * @return int Page ID.
 */
function amp_create_gutenberg_test_post( $content ) {
	$slug            = 'amp-test-gutenberg-blocks';
	$title           = 'AMP Test Gutenberg Blocks';
	$page            = get_page_by_path( "/{$slug}/" );
	$failure_message = 'The test page could not be added, please try again.';
	if ( $page ) {<commit_msg>Create a test reusable block, reduce heading size.<commit_after><?php
/**
 * Creates a post to test all Gutenberg blocks.
 *
 * @codeCoverageIgnore
 * @package AMP
 */

/**
 * Gets many of the Gutenberg fixture blocks in blocks/tests/fixtures/.
 *
 * @throws Exception If this is script is not run inside the plugin directory.
 * @return string $content Post content with all Gutenberg blocks.
 */
function amp_get_blocks() {
	$fixtures_dir = dirname( dirname( __DIR__ ) ) . '/gutenberg/blocks/test/fixtures';
	$content      = amp_get_block_permutations();
	if ( ! is_dir( $fixtures_dir ) ) {
		$fixtures_dir = dirname( $fixtures_dir );
		if ( ! is_dir( $fixtures_dir ) ) {
			throw new Exception( 'Please run this script from the AMP plugin root.' );
		}
	}

	foreach ( glob( $fixtures_dir . '/*.html' ) as $file ) {
		if ( ! preg_match( '/(serialized|embed|shortcode|custom-text-teaser|core__block)/', $file ) ) {
			// Add the block's title.
			preg_match( ':core__(?P<block>.+)\.html:s', basename( $file ), $matches );
			if ( isset( $matches['block'] ) ) {
				$content .= sprintf( '<h1>%s</h1>', $matches['block'] );
			}
			$content .= file_get_contents( $file ); // @codingStandardsIgnoreLine: file_get_contents_file_get_contents, file_system_read_file_get_contents.
		}
	}

	// Replace broken URLs in fixture files.
	$content = str_replace( 'http://google.com/hi.png', 'https://cldup.com/-3VMmmrPm9.jpg', $content );
	$content = str_replace( 'https://awesome-fake.video/file.mp4', 'https://videos.files.wordpress.com/DK5mLrbr/video-ca6dc0ab4a_hd.mp4', $content );
	return $content;
}

/**
 * Gets the Gutenberg block permutations.
 *
 * These are mostly copied from gutenberg/blocks/test/fixtures/, and slightly modified.
 * Embeds and shortcodes are tested in a separate script, so this does not have have many.
 *
 * @return string $content The blocks as HTML.
 */
function amp_get_block_permutations() {
	$blocks = array(
		array(
			'title'   => '(Reusable) Block With Video',
			'content' => sprintf( '<!-- wp:block {"ref":%d} /-->', amp_create_reusable_block() ),
		),
		array(
			'title'   => 'Categories With Dropdown',
			'content' => '<!-- wp:core/categories {"showPostCounts":false,"displayAsDropdown":true,"showHierarchy":false} /-->',
		),
		array(
			'title'   => 'Columns, With 2 Columns',
			'content' => '<!-- wp:columns {"columns":2} --><div class="wp-block-columns has-2-columns"><!-- wp:paragraph {"layout":"column-1"} -->	<p class="layout-column-1">Column One, Paragraph One</p><!-- /wp:paragraph --><!-- wp:paragraph {"layout":"column-1"} --><p class="layout-column-1">Column One, Paragraph Two</p><!-- /wp:paragraph --><!-- wp:paragraph {"layout":"column-2"} --><p class="layout-column-2">Column Two, Paragraph One</p><!-- /wp:paragraph --></div><!-- /wp:columns -->',
		),
		array(
			'title'   => 'Cover Image With Fixed Background',
			'content' => '<!-- wp:core/cover-image {"url":"https://cldup.com/uuUqE_dXzy.jpg","dimRatio":40} --><section class="wp-block-cover-image has-background-dim-40 has-background-dim has-parallax" style="background-image:url(https://cldup.com/uuUqE_dXzy.jpg)"><h2>Guten Berg!</h2></section><!-- /wp:core/cover-image -->',
		),
		array(
			'title'   => 'WordPress Embed',
			'content' => '<!-- wp:core-embed/wordpress {"url":"https://make.wordpress.org/core/2017/12/11/whats-new-in-gutenberg-11th-december/"} --><figure class="wp-block-embed-wordpress wp-block-embed">https://make.wordpress.org/core/2017/12/11/whats-new-in-gutenberg-11th-december/<figcaption>Embedded content from WordPress</figcaption></figure><!-- /wp:core-embed/wordpress -->',
		),
		array(
			'title'   => 'YouTube Embed',
			'content' => '<!-- wp:core-embed/youtube {"url":"https://www.youtube.com/watch?v=GGS-tKTXw4Y"} --><figure class="wp-block-embed-youtube wp-block-embed">https://www.youtube.com/watch?v=GGS-tKTXw4Y<figcaption>Embedded content from youtube</figcaption></figure><!-- /wp:core-embed/youtube -->',
		),
		array(
			'title'   => 'Twitter Embed',
			'content' => '<!-- wp:core-embed/twitter {"url":"https://twitter.com/AMPhtml/status/963443140005957632"} --><figure class="wp-block-embed-twitter wp-block-embed">https://twitter.com/AMPhtml/status/963443140005957632<figcaption>We are Automattic</figcaption></figure><!-- /wp:core-embed/twitter -->',
		),
		array(
			'title'   => 'Gallery With 3 Columns',
			'content' => '<!-- wp:core/gallery --><ul class="wp-block-gallery alignnone columns-3 is-cropped"><li class="blocks-gallery-item"><figure><img src="https://cldup.com/uuUqE_dXzy.jpg" alt="title" /></figure></li><li class="blocks-gallery-item"><figure><img src="https://cldup.com/-3VMmmrPm9.jpg" alt="title" /></figure></li><li class="blocks-gallery-item"><figure><img src="https://cldup.com/aMbxBM0zAi.jpg" alt="title" /></figure></li></ul><!-- /wp:core/gallery -->',
		),
		array(
			'title'   => 'Audio Shortcode',
			'content' => '<!-- wp:core/shortcode -->[audio src=https://wptavern.com/wp-content/uploads/2017/11/EPISODE-296-Gutenberg-Telemetry-Calypso-and-More-With-Matt-Mullenweg.mp3]<!-- /wp:core/shortcode -->',
		),
		array(
			'title'   => 'Caption Shortcode',
			'content' => '<!-- wp:core/shortcode -->[caption width=150]This is a caption[/caption]<!-- /wp:core/shortcode -->',
		),
	);

	$content = '';
	foreach ( $blocks as $block ) {
		$content .= sprintf( '<h2>%s</h2>', $block['title'] );
		$content .= $block['content'];
	}

	return $content;
}

/**
 * Creates a reusable block with a video.
 *
 * Reusable blocks are stored in custom post types.
 * This creates one, and returns the ID on success.
 *
 * @return int|WP_Error $post_id The post ID where the reusable block is stored, and 0 or WP_Error in case of failure.
 */
function amp_create_reusable_block() {
	return wp_insert_post( array(
		'post_type'    => 'wp_block',
		'post_title'   => 'Test Reusable Block',
		'post_content' => '<!-- wp:core/video --><figure class="wp-block-video"><video src="https://videos.files.wordpress.com/DK5mLrbr/video-ca6dc0ab4a_hd.mp4" controls=""></video></figure><!-- /wp:core/video -->',
		'post_status'  => 'publish',
	) );
}

/**
 * Creates a Gutenberg test post (page).
 *
 * @throws Exception If there is an error in creating the test page.
 * @param string $content The content to add to the post.
 * @return int Page ID.
 */
function amp_create_gutenberg_test_post( $content ) {
	$slug            = 'amp-test-gutenberg-blocks';
	$title           = 'AMP Test Gutenberg Blocks';
	$page            = get_page_by_path( "/{$slug}/" );
	$failure_message = 'The test page could not be added, please try again.';
	if ( $page ) {
{'fraction_non_alphanumeric': 0.20118728145076034, 'fraction_numerical': 0.017239977230218754, 'mean_word_length': 3.9548751007252214, 'pattern_counts': {'":': 25, '<': 150, '<?xml version=': 0, '>': 190, 'https://': 31, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '503450', 'n_tokens_mistral': 4602, 'n_tokens_neox': 4246, 'n_words': 854}