<?php
/**
 * Rest House: Block Patterns
 *
 * @since Rest House 1.0
 */

 /**
  * Get patterns content.
  *
  * @param string $file_name Filename.
  * @return string
  */
function rest_house_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 Rest House 1.0
 *
 * @return void
 */
function rest_house_register_block_patterns() {

	$patterns = array(
		'header-default' => array(
			'title'      => __( 'Default header', 'rest-house' ),
			'categories' => array( 'rest-house-headers' ),
			'blockTypes' => array( 'parts/header' ),
		),
		'footer-default' => array(
			'title'      => __( 'Default footer', 'rest-house' ),
			'categories' => array( 'rest-house-footers' ),
			'blockTypes' => array( 'parts/footer' ),
		),
		'home-banner' => array(
			'title'      => __( 'Home Banner', 'rest-house' ),
			'categories' => array( 'rest-house-banner' ),
		),
		'home-banner' => array(
			'title'      => __( 'Dometry Section', 'rest-house' ),
			'categories' => array( 'rest-house-dometry-section' ),
		),
		'about-section' => array(
			'title'      => __( 'About Section', 'rest-house' ),
			'categories' => array( 'rest-house-about-section' ),
		),
		'news-section' => array(
			'title'      => __( 'News Section', 'rest-house' ),
			'categories' => array( 'rest-house-news-section' ),
		),
		'faq-section' => array(
			'title'      => __( 'FAQ Section', 'rest-house' ),
			'categories' => array( 'rest-house-faq-section' ),
		),
		'primary-sidebar' => array(
			'title'    => __( 'Primary Sidebar', 'rest-house' ),
			'categories' => array( 'rest-house-sidebars' ),
		),
		'hidden-404' => array(
			'title'    => __( '404 content', 'rest-house' ),
			'categories' => array( 'rest-house-pages' ),
		),
		'post-listing-single-column' => array(
			'title'    => __( 'Post Single Column', 'rest-house' ),
			//'inserter' => false,
			'categories' => array( 'rest-house-query' ),
		),
		'post-listing-two-column' => array(
			'title'    => __( 'Post Two Column', 'rest-house' ),
			//'inserter' => false,
			'categories' => array( 'rest-house-query' ),
		),
		'post-listing-three-column' => array(
			'title'    => __( 'Post Three Column', 'rest-house' ),
			//'inserter' => false,
			'categories' => array( 'rest-house-query' ),
		),
		'post-listing-four-column' => array(
			'title'    => __( 'Post Four Column', 'rest-house' ),
			//'inserter' => false,
			'categories' => array( 'rest-house-query' ),
		),
		'feature-post-column' => array(
			'title'    => __( 'Feature Post Column', 'rest-house' ),
			//'inserter' => false,
			'categories' => array( 'rest-house-query' ),
		),
		'comment-section-1' => array(
			'title'    => __( 'Comment Section 1', 'rest-house' ),
			'categories' => array( 'rest-house-comment-sections' ),
		),
		'cover-with-post-title' => array(
			'title'    => __( 'Cover With Post Title', 'rest-house' ),
			'categories' => array( 'rest-house-banner-sections' ),
		),
		'cover-with-search-title' => array(
			'title'    => __( 'Cover With Search Title', 'rest-house' ),
			'categories' => array( 'rest-house-banner-sections' ),
		),
		'cover-with-archive-title' => array(
			'title'    => __( 'Cover With Archive Title', 'rest-house' ),
			'categories' => array( 'rest-house-banner-sections' ),
		),
		'cover-with-index-title' => array(
			'title'    => __( 'Cover With Index Title', 'rest-house' ),
			'categories' => array( 'rest-house-banner-sections' ),
		),
		'theme-button' => array(
			'title'    => __( 'Theme Button', 'rest-house' ),
			'categories' => array( 'rest-house-theme-button' ),
		),
	);

	$block_pattern_categories = array(
		'rest-house-footers' => array( 'label' => __( 'Footers', 'rest-house' ) ),
		'rest-house-headers' => array( 'label' => __( 'Headers', 'rest-house' ) ),
		'rest-house-pages'   => array( 'label' => __( 'Pages', 'rest-house' ) ),
		'rest-house-query'   => array( 'label' => __( 'Query', 'rest-house' ) ),
		'rest-house-sidebars'   => array( 'label' => __( 'Sidebars', 'rest-house' ) ),
		'rest-house-banner'   => array( 'label' => __( 'Banner Sections', 'rest-house' ) ),
		'rest-house-about-section'   => array( 'label' => __( 'About Section', 'rest-house' ) ),
		'rest-house-news-section'   => array( 'label' => __( 'News Section', 'rest-house' ) ),
		'rest-house-faq-section'   => array( 'label' => __( 'FAQ Section', 'rest-house' ) ),
		'rest-house-comment-section'   => array( 'label' => __( 'Comment Sections', 'rest-house' ) ),
		'rest-house-theme-button'   => array( 'label' => __( 'Theme Button Sections', 'rest-house' ) ),
	);

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

	foreach ( $patterns as $block_pattern => $pattern ) {
		$pattern['content'] = rest_house_get_pattern_content( $block_pattern );
		register_block_pattern(
			'rest-house/' . $block_pattern,
			$pattern
		);
	}
}
add_action( 'init', 'rest_house_register_block_patterns', 9 );
