<?php
/**
 * Patterns Handler.
 *
 * @author Themeisle
 * @package eduacademy
 * @since 1.0.0
 */

namespace Eduacademy;

use WP_Block_Pattern_Categories_Registry;

/**
 * Class Block_Patterns
 *
 * @package eduacademy
 */
class Block_Patterns {

	/**
	 * Patterns categories.
	 *
	 * @var array
	 */
	private $categories = array();
	/**
	 * The patterns array.
	 *
	 * These use the file names without termination inside the `inc/patterns` directory.
	 *
	 * @var array
	 */
	private $patterns = array();

	/**
	 * Block_Patterns constructor.
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'run' ) );
	}

	/**
	 * Run the class functionality.
	 *
	 * @return void
	 */
	public function run() {
		$this->setup_properties();
		$this->register_categories();
		$this->register_patterns();
	}

	/**
	 * Setup class properties.
	 *
	 * @return void
	 */
	private function setup_properties() {
		$categories = array(
			'eduacademy'         => array(
				'label'       => __( 'Eduacademy Patterns', 'eduacademy' ),
				'description' => __( 'Patterns for several sections and components', 'eduacademy' ),
			),
			'eduacademy-layouts' => array(
				'label'       => __( 'Eduacademy Layouts', 'eduacademy' ),
				'description' => __( 'Full-page layouts that can be used as templates', 'eduacademy' ),
			),
		);

		$patterns = array(
			// layout templates patterns.
			'templates/single-post-cover-boxed',
			'templates/single-post-cover',
			'templates/archive-cover',
			'templates/archive-list',

			// layout patterns.
			'layout/content-1',
			'layout/content-2',
			'layout/content-3',
			'layout/content-4',
			'layout/hero-1',
			'layout/hero-2',
			'layout/courses-1',
			'layout/courses-2',
			'layout/courses-3',
			'layout/features-1',
			'layout/features-2',
			'layout/instructors',
			'layout/testimonials-1',
			'layout/testimonials-2',
			'layout/course-cta',
			'layout/course-cta-2',
			'layout/curriculum',
			'layout/faq',
			'layout/pricing-1',
			'layout/pricing-2',

			// Post patterns.
			'loops/posts-1',
			'loops/posts-2',
			'loops/posts-3',
			'loops/posts-4',

			// Page title patterns.
			'page_titles/page-title-1',
			'page_titles/page-title-2',
		);

		$this->categories = apply_filters( 'eduacademy_block_patterns_categories', $categories );
		$this->patterns   = apply_filters( 'eduacademy_block_patterns', $patterns );
	}

	/**
	 * Register block patterns categories.
	 *
	 * @return void
	 */
	private function register_categories() {
		foreach ( $this->categories as $slug => $args ) {
			if ( WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $slug ) ) {
				continue;
			}

			register_block_pattern_category( $slug, $args );
		}
	}

	/**
	 * Register Patterns.
	 *
	 * @return void
	 */
	private function register_patterns() {
		foreach ( $this->patterns as $pattern ) {
			$file = EDUACADEMY_DIR . 'inc/patterns/' . $pattern . '.php';

			if ( ! is_file( $file ) ) {
				continue;
			}

			register_block_pattern( 'eduacademy/' . $pattern, require $file );
		}
	}
}
