<?php
/**
 * Block patterns
 *
 * @package MemberRead
 * @since 1.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Register block patterns.
 *
 * @since 1.0.0
 * @package MemberRead
 */
function member_read_register_block_patterns() {

	/**
	 * Block pattern categories.
	 *
	 * @since 1.0.0
	 * @package MemberRead
	 */
	$block_pattern_categories = apply_filters(
		'member_read_block_pattern_categories',
		array(
			'member-read'         => array( 'label' => __( 'Member Read', 'member-read' ) ),
		)
	);

	// Register pattern categories.
	if ( ! empty( $block_pattern_categories ) ) {
		foreach ( $block_pattern_categories as $category_name => $category_properties ) {
			register_block_pattern_category(
				$category_name,
				$category_properties
			);
		}
	}

	member_read_register_patterns();
}

add_action( 'init', 'member_read_register_block_patterns' );

/**
 * Register block patterns.
 *
 * Patterns live under `inc/patterns/<category>/<slug>.php` and each file
 * returns the pattern definition array. The array key below is the
 * registered pattern slug (kept unprefixed so it matches what templates
 * and template parts already reference as `member-read/<slug>`); the
 * value is the path to the pattern file, relative to `inc/patterns/`.
 *
 * @since 1.0.0
 * @package MemberRead
 */
function member_read_register_patterns() {
	$patterns = apply_filters(
		'member_read_block_patterns',
		array(
			'404'                    => 'templates/404',
			'archive'                => 'templates/archive',
			'single-article'         => 'templates/single-article',
			'single-article-premium' => 'templates/single-article-premium',
			'page-header'            => 'page_titles/page-header',
			'stories'                => 'loops/stories',
			'categories'             => 'layout/categories',
			'downloads'              => 'layout/downloads',
			'faq'                    => 'layout/faq',
			'footer'                 => 'layout/footer',
			'header'                 => 'layout/header',
			'hero'                   => 'layout/hero',
			'home'                   => 'layout/home',
			'members'                => 'layout/members',
			'membership-cta'         => 'layout/membership-cta',
			'newsletter-cta'         => 'layout/newsletter-cta',
			'premium-reads'          => 'layout/premium-reads',
			'pricing'                => 'layout/pricing',
		)
	);

	foreach ( $patterns as $slug => $path ) {
		$file = get_theme_file_path( 'inc/patterns/' . $path . '.php' );

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

		register_block_pattern( 'member-read/' . $slug, require $file );
	}
}
