<?php
/**
 * Journal Stories: Block Patterns
 *
 * @since Journal Stories 1.0
 */

 /**
  * Get patterns content.
  *
  * @param string $file_name Filename.
  * @return string
  */
function journal_stories_get_pattern_content( $file_name ) {
	ob_start();
	include get_theme_file_path( '/patterns/' . $file_name . '.php' );
	$output = ob_get_contents();
	ob_end_clean();
	return $output;
}

/**
 * Registers block patterns and categories.
 *
 * @since Journal Stories 1.0
 *
 * @return void
 */
function journal_stories_register_block_patterns() {

	$patterns = array(
		'header-default' => array(
			'title'      => __( 'Default header', 'journal-stories' ),
			'categories' => array( 'journal-stories-headers' ),
			'blockTypes' => array( 'parts/header' ),
		),
		'footer-default' => array(
			'title'      => __( 'Default footer', 'journal-stories' ),
			'categories' => array( 'journal-stories-footers' ),
			'blockTypes' => array( 'parts/footer' ),
		),
		'home-banner' => array(
			'title'      => __( 'Home Banner', 'journal-stories' ),
			'categories' => array( 'journal-stories-banner' ),
		),
		'trending-post-section' => array(
			'title'      => __( 'Trending Post Sections', 'journal-stories' ),
			'categories' => array( 'journal-stories-trending-post-section' ),
		),
		'latest-blog-section' => array(
			'title'      => __( 'Latest Blog Section', 'journal-stories' ),
			'categories' => array( 'journal-stories-latest-blog-section' ),
		),
		'primary-sidebar' => array(
			'title'    => __( 'Primary Sidebar', 'journal-stories' ),
			'categories' => array( 'journal-stories-sidebars' ),
		),
		'hidden-404' => array(
			'title'    => __( '404 content', 'journal-stories' ),
			'categories' => array( 'journal-stories-pages' ),
		),
		'post-listing-single-column' => array(
			'title'    => __( 'Post Single Column', 'journal-stories' ),
			//'inserter' => false,
			'categories' => array( 'journal-stories-query' ),
		),
		'post-listing-two-column' => array(
			'title'    => __( 'Post Two Column', 'journal-stories' ),
			//'inserter' => false,
			'categories' => array( 'journal-stories-query' ),
		),
		'post-listing-three-column' => array(
			'title'    => __( 'Post Three Column', 'journal-stories' ),
			//'inserter' => false,
			'categories' => array( 'journal-stories-query' ),
		),
		'post-listing-four-column' => array(
			'title'    => __( 'Post Four Column', 'journal-stories' ),
			//'inserter' => false,
			'categories' => array( 'journal-stories-query' ),
		),
		'feature-post-column' => array(
			'title'    => __( 'Feature Post Column', 'journal-stories' ),
			//'inserter' => false,
			'categories' => array( 'journal-stories-query' ),
		),
		'comment-section-1' => array(
			'title'    => __( 'Comment Section 1', 'journal-stories' ),
			'categories' => array( 'journal-stories-comment-sections' ),
		),
		'cover-with-post-title' => array(
			'title'    => __( 'Cover With Post Title', 'journal-stories' ),
			'categories' => array( 'journal-stories-banner-sections' ),
		),
		'theme-button' => array(
			'title'    => __( 'Theme Button', 'journal-stories' ),
			'categories' => array( 'journal-stories-theme-button' ),
		),
	);

	$block_pattern_categories = array(
		'journal-stories-footers' => array( 'label' => __( 'Footers', 'journal-stories' ) ),
		'journal-stories-headers' => array( 'label' => __( 'Headers', 'journal-stories' ) ),
		'journal-stories-pages'   => array( 'label' => __( 'Pages', 'journal-stories' ) ),
		'journal-stories-query'   => array( 'label' => __( 'Query', 'journal-stories' ) ),
		'journal-stories-sidebars'   => array( 'label' => __( 'Sidebars', 'journal-stories' ) ),
		'journal-stories-banner'   => array( 'label' => __( 'Banner Sections', 'journal-stories' ) ),
		'journal-stories-trending-post-section'   => array( 'label' => __( 'Trending Post Section', 'journal-stories' ) ),
		'journal-stories-latest-blog-section'   => array( 'label' => __( 'Latest Blog Section', 'journal-stories' ) ),
		'journal-stories-comment-section'   => array( 'label' => __( 'Comment Sections', 'journal-stories' ) ),
		'journal-stories-theme-button'   => array( 'label' => __( 'Theme Button Sections', 'journal-stories' ) ),
	);

	/**
	 * Filters the theme block pattern categories.
	 *
	 * @since Journal Stories 1.0
	 *
	 * @param array[] $block_pattern_categories {
	 *     An associative array of block pattern categories, keyed by category name.
	 *
	 *     @type array[] $properties {
	 *         An array of block category properties.
	 *
	 *         @type string $label A human-readable label for the pattern category.
	 *     }
	 * }
	 */
	$block_pattern_categories = apply_filters( 'journal_stories_block_pattern_categories', $block_pattern_categories );

	foreach ( $block_pattern_categories as $name => $properties ) {
		if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) {
			register_block_pattern_category( $name, $properties );
		}
	}

	/**
	 * Filters the theme block patterns.
	 *
	 * @since Journal Stories 1.0
	 *
	 * @param array $block_patterns List of block patterns by name.
	 */
	$patterns = apply_filters( 'journal_stories_block_patterns', $patterns );

	foreach ( $patterns as $block_pattern => $pattern ) {
		$pattern['content'] = journal_stories_get_pattern_content( $block_pattern );
		register_block_pattern(
			'journal-stories/' . $block_pattern,
			$pattern
		);
	}
}
add_action( 'init', 'journal_stories_register_block_patterns', 9 );
