<?php
/**
 * Tree Plantation: Block Patterns
 *
 * @since Tree Plantation 1.0
 */

 /**
  * Get patterns content.
  *
  * @param string $file_name Filename.
  * @return string
  */
function tree_plantation_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 Tree Plantation 1.0
 *
 * @return void
 */
function tree_plantation_register_block_patterns() {

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

	$block_pattern_categories = array(
		'tree-plantation-footers' => array( 'label' => __( 'Footers', 'tree-plantation' ) ),
		'tree-plantation-headers' => array( 'label' => __( 'Headers', 'tree-plantation' ) ),
		'tree-plantation-pages'   => array( 'label' => __( 'Pages', 'tree-plantation' ) ),
		'tree-plantation-query'   => array( 'label' => __( 'Query', 'tree-plantation' ) ),
		'tree-plantation-sidebars'   => array( 'label' => __( 'Sidebars', 'tree-plantation' ) ),
		'tree-plantation-banner'   => array( 'label' => __( 'Banner Sections', 'tree-plantation' ) ),
		'tree-plantation-about'   => array( 'label' => __( 'About Sections', 'tree-plantation' ) ),
		'tree-plantation-service-section'   => array( 'label' => __( 'Service Section', 'tree-plantation' ) ),
		'tree-plantation-comment-section'   => array( 'label' => __( 'Comment Sections', 'tree-plantation' ) ),
		'tree-plantation-theme-button'   => array( 'label' => __( 'Theme Button Sections', 'tree-plantation' ) ),
	);

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

	foreach ( $patterns as $block_pattern => $pattern ) {
		$pattern['content'] = tree_plantation_get_pattern_content( $block_pattern );
		register_block_pattern(
			'tree-plantation/' . $block_pattern,
			$pattern
		);
	}
}
add_action( 'init', 'tree_plantation_register_block_patterns', 9 );
