<?php
/**
 * Style Accessories: Block Patterns
 *
 * @since Style Accessories 1.0
 */

 /**
  * Get patterns content.
  *
  * @param string $file_name Filename.
  * @return string
  */
function style_accessories_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 Style Accessories 1.0
 *
 * @return void
 */
function style_accessories_register_block_patterns() {

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

	$block_pattern_categories = array(
		'style-accessories-footers' => array( 'label' => __( 'Footers Section', 'style-accessories' ) ),
		'style-accessories-headers' => array( 'label' => __( 'Headers Section', 'style-accessories' ) ),
		'style-accessories-pages'   => array( 'label' => __( 'Pages', 'style-accessories' ) ),
		'style-accessories-query'   => array( 'label' => __( 'Query', 'style-accessories' ) ),
		'style-accessories-sidebars'   => array( 'label' => __( 'Sidebars', 'style-accessories' ) ),
		'style-accessories-banner'   => array( 'label' => __( 'Slider Section', 'style-accessories' ) ),
		'style-accessories-product-section'   => array( 'label' => __( 'Product Section', 'style-accessories' ) ),
		'style-accessories-latest-blog-section'   => array( 'label' => __( 'Latest Blog Section', 'style-accessories' ) ),
		'style-accessories-comment-section'   => array( 'label' => __( 'Comment Section', 'style-accessories' ) ),
		'style-accessories-theme-button'   => array( 'label' => __( 'Theme Button Section', 'style-accessories' ) ),
	);

	/**
	 * Filters the theme block pattern categories.
	 *
	 * @since Style Accessories 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( 'style_accessories_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 Style Accessories 1.0
	 *
	 * @param array $block_patterns List of block patterns by name.
	 */
	$patterns = apply_filters( 'style_accessories_block_patterns', $patterns );

	foreach ( $patterns as $block_pattern => $pattern ) {
		$pattern['content'] = style_accessories_get_pattern_content( $block_pattern );
		register_block_pattern(
			'style-accessories/' . $block_pattern,
			$pattern
		);
	}
}
add_action( 'init', 'style_accessories_register_block_patterns', 9 );
