<?php
/**
 * Sales Landing Page: Block Patterns
 *
 * @since Sales Landing Page 1.0
 */

 /**
  * Get patterns content.
  *
  * @param string $file_name Filename.
  * @return string
  */
function sales_landing_page_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 Sales Landing Page 1.0
 *
 * @return void
 */
function sales_landing_page_register_block_patterns() {

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

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

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

	foreach ( $patterns as $block_pattern => $pattern ) {
		$pattern['content'] = sales_landing_page_get_pattern_content( $block_pattern );
		register_block_pattern(
			'sales-landing-page/' . $block_pattern,
			$pattern
		);
	}
}
add_action( 'init', 'sales_landing_page_register_block_patterns', 9 );
