<?php
/**
 * Secretary Services: Block Patterns
 *
 * @since Secretary Services 1.0
 */

 /**
  * Get patterns content.
  *
  * @param string $file_name Filename.
  * @return string
  */
function secretary_services_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 Secretary Services 1.0
 *
 * @return void
 */
function secretary_services_register_block_patterns() {

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

	$block_pattern_categories = array(
		'secretary-services-footers' => array( 'label' => __( 'Footers', 'secretary-services' ) ),
		'secretary-services-headers' => array( 'label' => __( 'Headers', 'secretary-services' ) ),
		'secretary-services-pages'   => array( 'label' => __( 'Pages', 'secretary-services' ) ),
		'secretary-services-query'   => array( 'label' => __( 'Query', 'secretary-services' ) ),
		'secretary-services-sidebars'   => array( 'label' => __( 'Sidebars', 'secretary-services' ) ),
		'secretary-services-banner'   => array( 'label' => __( 'Banner Sections', 'secretary-services' ) ),
		'secretary-services-team-section'   => array( 'label' => __( 'Team Section', 'secretary-services' ) ),
		'secretary-services-comment-section'   => array( 'label' => __( 'Comment Sections', 'secretary-services' ) ),
		'secretary-services-theme-button'   => array( 'label' => __( 'Theme Button Sections', 'secretary-services' ) ),
	);

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

	foreach ( $patterns as $block_pattern => $pattern ) {
		$pattern['content'] = secretary_services_get_pattern_content( $block_pattern );
		register_block_pattern(
			'secretary-services/' . $block_pattern,
			$pattern
		);
	}
}
add_action( 'init', 'secretary_services_register_block_patterns', 9 );
