<?php
/**
 * Translation Service: Block Patterns
 *
 * @since Translation Service 1.0
 */

 /**
  * Get patterns content.
  *
  * @param string $file_name Filename.
  * @return string
  */
function translation_service_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 Translation Service 1.0
 *
 * @return void
 */
function translation_service_register_block_patterns() {

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

	$block_pattern_categories = array(
		'translation-service-footers' => array( 'label' => __( 'Footers', 'translation-service' ) ),
		'translation-service-headers' => array( 'label' => __( 'Headers', 'translation-service' ) ),
		'translation-service-pages'   => array( 'label' => __( 'Pages', 'translation-service' ) ),
		'translation-service-query'   => array( 'label' => __( 'Query', 'translation-service' ) ),
		'translation-service-sidebars'   => array( 'label' => __( 'Sidebars', 'translation-service' ) ),
		'translation-service-banner'   => array( 'label' => __( 'Banner Sections', 'translation-service' ) ),
		'translation-service-our-services-section'   => array( 'label' => __( 'Our Services Section', 'translation-service' ) ),
		'translation-service-experience-section'   => array( 'label' => __( 'Experience Section', 'translation-service' ) ),
		'translation-service-testimonial-section'   => array( 'label' => __( 'Testimonial Section', 'translation-service' ) ),
		'translation-service-news-section'   => array( 'label' => __( 'News Section', 'translation-service' ) ),
		'translation-service-faq-section'   => array( 'label' => __( 'FAQ Section', 'translation-service' ) ),
		'translation-service-comment-section'   => array( 'label' => __( 'Comment Sections', 'translation-service' ) ),
		'translation-service-theme-button'   => array( 'label' => __( 'Theme Button Sections', 'translation-service' ) ),
	);

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

	foreach ( $patterns as $block_pattern => $pattern ) {
		$pattern['content'] = translation_service_get_pattern_content( $block_pattern );
		register_block_pattern(
			'translation-service/' . $block_pattern,
			$pattern
		);
	}
}
add_action( 'init', 'translation_service_register_block_patterns', 9 );
