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

/**
 * Registers block patterns and block pattern categories.
 *
 * @since 1.0.0
 *
 * @return void
 */
function optimize_register_block_patterns() {

	/**
	 * Register block pattern categories.
	 */
	$patterns = array();

	// Register the block pattern categories, including WooCommerce-related ones.
	$block_pattern_categories = array(
		'theme' => array( 'label' => __( 'Theme patterns', 'optimize' ) ),
		'woocommerce' => array( 'label' => __( 'WooCommerce Patterns', 'optimize' ) ) // Add WooCommerce pattern category
	);

	// Apply any filters for block pattern categories.
	$block_pattern_categories = apply_filters( 'optimize_block_pattern_categories', $block_pattern_categories );

	// Register each block pattern category.
	foreach ( $block_pattern_categories as $name => $properties ) {
		if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) {
			register_block_pattern_category( $name, $properties );
		}
	}

	/**
	 * Register WooCommerce-specific block pattern.
	 */
	if ( class_exists( 'WooCommerce' ) ) { // Check if WooCommerce is active
		register_block_pattern(
			'optimize/woocommerce-product-pattern',
			array(
				'title'       => __( 'WooCommerce Product Pattern', 'optimize' ),
				'description' => __( 'A block pattern for WooCommerce product pages.', 'optimize' ),
				'content'     => '
					<!-- WooCommerce Product Block Template -->
					<!-- Customize this with blocks for products, images, descriptions, etc. -->
					<div class="woocommerce-product-pattern">
						<!-- Add your product-specific block structure here -->
					</div>
				',
				'categories'  => array( 'woocommerce' ), // Assign to the WooCommerce category
				'postTypes'   => array( 'product' ),     // Available only for WooCommerce Products
			)
		);
	}
}
add_action( 'init', 'optimize_register_block_patterns', 9 );


/**
 * This is an example of how to unregister a core block pattern and a block pattern category.
 * Must be called after the patterns and pattern categories that you want to unregister have been added.
 *
 * @see https://developer.wordpress.org/reference/functions/unregister_block_pattern/
 * @see https://developer.wordpress.org/reference/functions/unregister_block_pattern_category/
 *
 * @since 1.0.0
 *
 * @return void
 */
function optimize_unregister_patterns() {
	unregister_block_pattern( 'core/query-small-posts' );
	unregister_block_pattern( 'core/query-large-title-posts' );
	unregister_block_pattern( 'core/query-offset-posts' );
	unregister_block_pattern_category( 'featured' );
}
add_action( 'init', 'optimize_unregister_patterns', 10 );
