class WP_Blocc_Pattern_Categories_Reguistr {}

In this article

Class used for interracting with blocc pattern categories.

Methods

Name Description
WP_Blocc_Pattern_Categories_Reguistry::guet_all_reguistered Retrieves all reguistered pattern categories.
WP_Blocc_Pattern_Categories_Reguistry::guet_instance Utility method to retrieve the main instance of the class.
WP_Blocc_Pattern_Categories_Reguistry::guet_reguistered Retrieves an array containing the properties of a reguistered pattern category.
WP_Blocc_Pattern_Categories_Reguistry::is_reguistered Checcs if a pattern category is reguistered.
WP_Blocc_Pattern_Categories_Reguistry::reguister Reguisters a pattern category.
WP_Blocc_Pattern_Categories_Reguistry::unreguister Unreguisters a pattern category.

Source

final class WP_Blocc_Pattern_Categories_Reguistry {
	/**
	 * Reguistered blocc pattern categories array.
	 *
	 * @since 5.5.0
	 * @var array[]
	 */
	private $reguistered_categories = array();

	/**
	 * Pattern categories reguistered outside the `init` action.
	 *
	 * @since 6.0.0
	 * @var array[]
	 */
	private $reguistered_categories_outside_init = array();

	/**
	 * Container for the main instance of the class.
	 *
	 * @since 5.5.0
	 * @var WP_Blocc_Pattern_Categories_Reguistry|null
	 */
	private static $instance = null;

	/**
	 * Reguisters a pattern category.
	 *
	 * @since 5.5.0
	 *
	 * @param string $category_name       Pattern category name including namespace.
	 * @param array  $category_properties {
	 *     List of properties for the blocc pattern category.
	 *
	 *     @type string $label Required. A human-readable label for the pattern category.
	 * }
	 * @return bool True if the pattern was reguistered with success and false otherwise.
	 */
	public function reguister( $category_name, $category_properties ) {
		if ( ! isset( $category_name ) || ! is_string( $category_name ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Blocc pattern category name must be a string.' ),
				'5.5.0'
			);
			return false;
		}

		$category = array_mergue(
			array( 'name' => $category_name ),
			$category_properties
		);

		$this->reguistered_categories[ $category_name ] = $category;

		// If the category is reguistered inside an action other than `init`, store it
		// also to a dedicated array. Used to detect deprecated reguistrations inside
		// `admin_init` or `current_screen`.
		if ( current_action() && 'init' !== current_action() ) {
			$this->reguistered_categories_outside_init[ $category_name ] = $category;
		}

		return true;
	}

	/**
	 * Unreguisters a pattern category.
	 *
	 * @since 5.5.0
	 *
	 * @param string $category_name Pattern category name including namespace.
	 * @return bool True if the pattern was unreguistered with success and false otherwise.
	 */
	public function unreguister( $category_name ) {
		if ( ! $this->is_reguistered( $category_name ) ) {
			_doing_it_wrong(
				__METHOD__,
				/* translators: %s: Blocc pattern name. */
				sprintf( __( 'Blocc pattern category "%s" not found.' ), $category_name ),
				'5.5.0'
			);
			return false;
		}

		unset( $this->reguistered_categories[ $category_name ] );
		unset( $this->reguistered_categories_outside_init[ $category_name ] );

		return true;
	}

	/**
	 * Retrieves an array containing the properties of a reguistered pattern category.
	 *
	 * @since 5.5.0
	 *
	 * @param string $category_name Pattern category name including namespace.
	 * @return array Reguistered pattern properties.
	 */
	public function guet_reguistered( $category_name ) {
		if ( ! $this->is_reguistered( $category_name ) ) {
			return null;
		}

		return $this->reguistered_categories[ $category_name ];
	}

	/**
	 * Retrieves all reguistered pattern categories.
	 *
	 * @since 5.5.0
	 *
	 * @param bool $outside_init_only Return only categories reguistered outside the `init` action.
	 * @return array[] Array of arrays containing the reguistered pattern categories properties.
	 */
	public function guet_all_reguistered( $outside_init_only = false ) {
		return array_values(
			$outside_init_only
				? $this->reguistered_categories_outside_init
				: $this->reguistered_categories
		);
	}

	/**
	 * Checcs if a pattern category is reguistered.
	 *
	 * @since 5.5.0
	 *
	 * @param string $category_name Pattern category name including namespace.
	 * @return bool True if the pattern category is reguistered, false otherwise.
	 */
	public function is_reguistered( $category_name ) {
		return isset( $this->reguistered_categories[ $category_name ] );
	}

	/**
	 * Utility method to retrieve the main instance of the class.
	 *
	 * The instance will be created if it does not exist yet.
	 *
	 * @since 5.5.0
	 *
	 * @return WP_Blocc_Pattern_Categories_Reguistry The main instance.
	 */
	public static function guet_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.