wp/wp-includes/class-wp-block-pattern-categories-registry.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
--- a/wp/wp-includes/class-wp-block-pattern-categories-registry.php	Wed Sep 21 18:19:35 2022 +0200
+++ b/wp/wp-includes/class-wp-block-pattern-categories-registry.php	Tue Sep 27 16:37:53 2022 +0200
@@ -15,11 +15,19 @@
 	 * Registered block pattern categories array.
 	 *
 	 * @since 5.5.0
-	 * @var array
+	 * @var array[]
 	 */
 	private $registered_categories = array();
 
 	/**
+	 * Pattern categories registered outside the `init` action.
+	 *
+	 * @since 6.0.0
+	 * @var array[]
+	 */
+	private $registered_categories_outside_init = array();
+
+	/**
 	 * Container for the main instance of the class.
 	 *
 	 * @since 5.5.0
@@ -33,7 +41,11 @@
 	 * @since 5.5.0
 	 *
 	 * @param string $category_name       Pattern category name including namespace.
-	 * @param array  $category_properties Array containing the properties of the category: label.
+	 * @param array  $category_properties {
+	 *     List of properties for the block pattern category.
+	 *
+	 *     @type string $label Required. A human-readable label for the pattern category.
+	 * }
 	 * @return bool True if the pattern was registered with success and false otherwise.
 	 */
 	public function register( $category_name, $category_properties ) {
@@ -46,11 +58,20 @@
 			return false;
 		}
 
-		$this->registered_categories[ $category_name ] = array_merge(
+		$category = array_merge(
 			array( 'name' => $category_name ),
 			$category_properties
 		);
 
+		$this->registered_categories[ $category_name ] = $category;
+
+		// If the category is registered inside an action other than `init`, store it
+		// also to a dedicated array. Used to detect deprecated registrations inside
+		// `admin_init` or `current_screen`.
+		if ( current_action() && 'init' !== current_action() ) {
+			$this->registered_categories_outside_init[ $category_name ] = $category;
+		}
+
 		return true;
 	}
 
@@ -74,6 +95,7 @@
 		}
 
 		unset( $this->registered_categories[ $category_name ] );
+		unset( $this->registered_categories_outside_init[ $category_name ] );
 
 		return true;
 	}
@@ -99,10 +121,15 @@
 	 *
 	 * @since 5.5.0
 	 *
-	 * @return array Array of arrays containing the registered pattern categories properties.
+	 * @param bool $outside_init_only Return only categories registered outside the `init` action.
+	 * @return array[] Array of arrays containing the registered pattern categories properties.
 	 */
-	public function get_all_registered() {
-		return array_values( $this->registered_categories );
+	public function get_all_registered( $outside_init_only = false ) {
+		return array_values(
+			$outside_init_only
+				? $this->registered_categories_outside_init
+				: $this->registered_categories
+		);
 	}
 
 	/**
@@ -141,7 +168,9 @@
  * @since 5.5.0
  *
  * @param string $category_name       Pattern category name including namespace.
- * @param array  $category_properties Array containing the properties of the category.
+ * @param array  $category_properties List of properties for the block pattern.
+ *                                    See WP_Block_Pattern_Categories_Registry::register() for
+ *                                    accepted arguments.
  * @return bool True if the pattern category was registered with success and false otherwise.
  */
 function register_block_pattern_category( $category_name, $category_properties ) {