PluginProbe
Gutenberg / 8.5.1
Gutenberg v8.5.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / class-wp-block-pattern-categories-registry.php

class-wp-block-pattern-categories-registry.php in Gutenberg 8.5.1, at lib/class-wp-block-pattern-categories-registry.php

145 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Blocks API: WP_Block_Pattern_Categories_Registry class
4 *
5 * @package Gutenberg
6 */
7
8 /**
9 * Class used for interacting with block pattern categories.
10 *
11 * This class can be removed when plugin support requires WordPress 5.5.0+.
12 *
13 * @see https://core.trac.wordpress.org/ticket/50445
14 * @see https://core.trac.wordpress.org/changeset/48156
15 */
16 final class WP_Block_Pattern_Categories_Registry {
17 /**
18 * Registered block pattern categories array.
19 *
20 * @var array
21 */
22 private $registered_categories = array();
23
24 /**
25 * Container for the main instance of the class.
26 *
27 * @var WP_Block_Pattern_Categories_Registry|null
28 */
29 private static $instance = null;
30
31 /**
32 * Registers a pattern category.
33 *
34 * @param string $category_name Pattern category name.
35 * @param array $category_properties Array containing the properties of the category: label.
36 * @return boolean True if the pattern was registered with success and false otherwise.
37 */
38 public function register( $category_name, $category_properties ) {
39 if ( ! isset( $category_name ) || ! is_string( $category_name ) ) {
40 $message = __( 'Block pattern category name must be a string.', 'gutenberg' );
41 _doing_it_wrong( __METHOD__, $message, '8.1.0' );
42 return false;
43 }
44
45 $this->registered_categories[ $category_name ] = array_merge(
46 array( 'name' => $category_name ),
47 $category_properties
48 );
49
50 return true;
51 }
52
53 /**
54 * Unregisters a pattern category.
55 *
56 * @param string $category_name Pattern name including namespace.
57 * @return boolean True if the pattern was unregistered with success and false otherwise.
58 */
59 public function unregister( $category_name ) {
60 if ( ! $this->is_registered( $category_name ) ) {
61 /* translators: 1: Block pattern name. */
62 $message = sprintf( __( 'Block pattern category "%1$s" not found.', 'gutenberg' ), $category_name );
63 _doing_it_wrong( __METHOD__, $message, '8.1.0' );
64 return false;
65 }
66
67 unset( $this->registered_categories[ $category_name ] );
68
69 return true;
70 }
71
72 /**
73 * Retrieves an array containing the properties of a registered pattern category.
74 *
75 * @param string $category_name Pattern category name.
76 * @return array Registered pattern properties.
77 */
78 public function get_registered( $category_name ) {
79 if ( ! $this->is_registered( $category_name ) ) {
80 return null;
81 }
82
83 return $this->registered_categories[ $category_name ];
84 }
85
86 /**
87 * Retrieves all registered pattern categories.
88 *
89 * @return array Array of arrays containing the registered pattern categories properties.
90 */
91 public function get_all_registered() {
92 return array_values( $this->registered_categories );
93 }
94
95 /**
96 * Checks if a pattern category is registered.
97 *
98 * @param string $category_name Pattern category name.
99 * @return bool True if the pattern category is registered, false otherwise.
100 */
101 public function is_registered( $category_name ) {
102 return isset( $this->registered_categories[ $category_name ] );
103 }
104
105 /**
106 * Utility method to retrieve the main instance of the class.
107 *
108 * The instance will be created if it does not exist yet.
109 *
110 * @since 5.3.0
111 *
112 * @return WP_Block_Pattern_Categories_Registry The main instance.
113 */
114 public static function get_instance() {
115 if ( null === self::$instance ) {
116 self::$instance = new self();
117 }
118
119 return self::$instance;
120 }
121 }
122
123 /**
124 * Registers a new pattern category.
125 *
126 * @param string $category_name Pattern category name.
127 * @param array $category_properties Array containing the properties of the category.
128 *
129 * @return boolean True if the pattern category was registered with success and false otherwise.
130 */
131 function register_block_pattern_category( $category_name, $category_properties ) {
132 return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
133 }
134
135 /**
136 * Unregisters a pattern category.
137 *
138 * @param string $category_name Pattern category name including namespace.
139 *
140 * @return boolean True if the pattern category was unregistered with success and false otherwise.
141 */
142 function unregister_block_pattern_category( $category_name ) {
143 return WP_Block_Pattern_Categories_Registry::get_instance()->unregister( $category_name );
144 }
145