<?php
/**
 * Water Filter: Block Patterns
 *
 * @since Water Filter 1.0
 */

 /**
  * Get patterns content.
  *
  * @param string $file_name Filename.
  * @return string
  */
function water_filter_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 Water Filter 1.0
 *
 * @return void
 */
function water_filter_register_block_patterns() {

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

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

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

	foreach ( $patterns as $block_pattern => $pattern ) {
		$pattern['content'] = water_filter_get_pattern_content( $block_pattern );
		register_block_pattern(
			'water-filter/' . $block_pattern,
			$pattern
		);
	}
}
add_action( 'init', 'water_filter_register_block_patterns', 9 );
