| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Block Bindings API: WP_Block_Bindings_Registry class. 4 * 5 * Supports overriding content in blocks by connecting them to different sources. 6 * 7 * @package WordPress 8 * @subpackage Block Bindings 9 * @since 6.5.0 10 */ 11 12 /** 13 * Core class used for interacting with block bindings sources. 14 * 15 * @since 6.5.0 16 */ 17 final class WP_Block_Bindings_Registry { 18 19 /** 20 * Holds the registered block bindings sources, keyed by source identifier. 21 * 22 * @since 6.5.0 23 * @var WP_Block_Bindings_Source[] 24 */ 25 private $sources = array(); 26 27 /** 28 * Container for the main instance of the class. 29 * 30 * @since 6.5.0 31 * @var WP_Block_Bindings_Registry|null 32 */ 33 private static $instance = null; 34 35 /** 36 * Supported source properties that can be passed to the registered source. 37 * 38 * @since 6.5.0 39 * @var string[] 40 */ 41 private $allowed_source_properties = array( 42 'label', 43 'get_value_callback', 44 'uses_context', 45 ); 46 47 /** 48 * Registers a new block bindings source. 49 * 50 * This is a low-level method. For most use cases, it is recommended to use 51 * the `register_block_bindings_source()` function instead. 52 * 53 * @see register_block_bindings_source() 54 * 55 * Sources are used to override block's original attributes with a value 56 * coming from the source. Once a source is registered, it can be used by a 57 * block by setting its `metadata.bindings` attribute to a value that refers 58 * to the source. 59 * 60 * @since 6.5.0 61 * 62 * @param string $source_name The name of the source. It must be a string containing a namespace prefix, i.e. 63 * `my-plugin/my-custom-source`. It must only contain lowercase alphanumeric 64 * characters, the forward slash `/` and dashes. 65 * @param array $source_properties { 66 * The array of arguments that are used to register a source. 67 * 68 * @type string $label The label of the source. 69 * @type callable $get_value_callback A callback executed when the source is processed during block rendering. 70 * The callback should have the following signature: 71 * 72 * `function( $source_args, $block_instance, $attribute_name ): mixed` 73 * - @param array $source_args Array containing source arguments 74 * used to look up the override value, 75 * i.e. {"key": "foo"}. 76 * - @param WP_Block $block_instance The block instance. 77 * - @param string $attribute_name The name of the target attribute. 78 * The callback has a mixed return type; it may return a string to override 79 * the block's original value, null, false to remove an attribute, etc. 80 * @type string[] $uses_context Optional. Array of values to add to block `uses_context` needed by the source. 81 * } 82 * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure. 83 * 84 * @phpstan-param lowercase-string&non-falsy-string $source_name 85 */ 86 public function register( string $source_name, array $source_properties ) { 87 if ( ! is_string( $source_name ) ) { 88 _doing_it_wrong( 89 __METHOD__, 90 __( 'Block bindings source name must be a string.' ), 91 '6.5.0' 92 ); 93 return false; 94 } 95 96 if ( preg_match( '/[A-Z]+/', $source_name ) ) { 97 _doing_it_wrong( 98 __METHOD__, 99 __( 'Block bindings source names must not contain uppercase characters.' ), 100 '6.5.0' 101 ); 102 return false; 103 } 104 105 $name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/'; 106 if ( ! preg_match( $name_matcher, $source_name ) ) { 107 _doing_it_wrong( 108 __METHOD__, 109 __( 'Block bindings source names must contain a namespace prefix. Example: my-plugin/my-custom-source' ), 110 '6.5.0' 111 ); 112 return false; 113 } 114 115 if ( $this->is_registered( $source_name ) ) { 116 _doing_it_wrong( 117 __METHOD__, 118 /* translators: %s: Block bindings source name. */ 119 sprintf( __( 'Block bindings source "%s" already registered.' ), $source_name ), 120 '6.5.0' 121 ); 122 return false; 123 } 124 125 // Validates that the source properties contain the label. 126 if ( ! isset( $source_properties['label'] ) ) { 127 _doing_it_wrong( 128 __METHOD__, 129 __( 'The $source_properties must contain a "label".' ), 130 '6.5.0' 131 ); 132 return false; 133 } 134 135 // Validates that the source properties contain the get_value_callback. 136 if ( ! isset( $source_properties['get_value_callback'] ) ) { 137 _doing_it_wrong( 138 __METHOD__, 139 __( 'The $source_properties must contain a "get_value_callback".' ), 140 '6.5.0' 141 ); 142 return false; 143 } 144 145 // Validates that the get_value_callback is a valid callback. 146 if ( ! is_callable( $source_properties['get_value_callback'] ) ) { 147 _doing_it_wrong( 148 __METHOD__, 149 __( 'The "get_value_callback" parameter must be a valid callback.' ), 150 '6.5.0' 151 ); 152 return false; 153 } 154 155 // Validates that the uses_context parameter is an array. 156 if ( isset( $source_properties['uses_context'] ) && ! is_array( $source_properties['uses_context'] ) ) { 157 _doing_it_wrong( 158 __METHOD__, 159 __( 'The "uses_context" parameter must be an array.' ), 160 '6.5.0' 161 ); 162 return false; 163 } 164 165 if ( ! empty( array_diff( array_keys( $source_properties ), $this->allowed_source_properties ) ) ) { 166 _doing_it_wrong( 167 __METHOD__, 168 __( 'The $source_properties array contains invalid properties.' ), 169 '6.5.0' 170 ); 171 return false; 172 } 173 174 $source = new WP_Block_Bindings_Source( 175 $source_name, 176 $source_properties 177 ); 178 179 $this->sources[ $source_name ] = $source; 180 181 return $source; 182 } 183 184 /** 185 * Unregisters a block bindings source. 186 * 187 * @since 6.5.0 188 * 189 * @param string $source_name Block bindings source name including namespace. 190 * @return WP_Block_Bindings_Source|false The unregistered block bindings source on success and `false` otherwise. 191 */ 192 public function unregister( string $source_name ) { 193 if ( ! $this->is_registered( $source_name ) ) { 194 _doing_it_wrong( 195 __METHOD__, 196 /* translators: %s: Block bindings source name. */ 197 sprintf( __( 'Block binding "%s" not found.' ), $source_name ), 198 '6.5.0' 199 ); 200 return false; 201 } 202 203 $unregistered_source = $this->sources[ $source_name ]; 204 unset( $this->sources[ $source_name ] ); 205 206 return $unregistered_source; 207 } 208 209 /** 210 * Retrieves the list of all registered block bindings sources. 211 * 212 * @since 6.5.0 213 * 214 * @return WP_Block_Bindings_Source[] The array of registered sources. 215 */ 216 public function get_all_registered() { 217 return $this->sources; 218 } 219 220 /** 221 * Retrieves a registered block bindings source. 222 * 223 * @since 6.5.0 224 * 225 * @param string $source_name The name of the source. 226 * @return WP_Block_Bindings_Source|null The registered block bindings source, or `null` if it is not registered. 227 */ 228 public function get_registered( string $source_name ) { 229 if ( ! $this->is_registered( $source_name ) ) { 230 return null; 231 } 232 233 return $this->sources[ $source_name ]; 234 } 235 236 /** 237 * Checks if a block bindings source is registered. 238 * 239 * @since 6.5.0 240 * 241 * @param string|null $source_name The name of the source. 242 * @return bool `true` if the block bindings source is registered, `false` otherwise. 243 */ 244 public function is_registered( $source_name ) { 245 return isset( $source_name, $this->sources[ $source_name ] ); 246 } 247 248 /** 249 * Wakeup magic method. 250 * 251 * @since 6.5.0 252 */ 253 public function __wakeup() { 254 if ( ! $this->sources ) { 255 return; 256 } 257 if ( ! is_array( $this->sources ) ) { 258 throw new UnexpectedValueException(); 259 } 260 foreach ( $this->sources as $value ) { 261 if ( ! $value instanceof WP_Block_Bindings_Source ) { 262 throw new UnexpectedValueException(); 263 } 264 } 265 } 266 267 /** 268 * Utility method to retrieve the main instance of the class. 269 * 270 * The instance will be created if it does not exist yet. 271 * 272 * @since 6.5.0 273 * 274 * @return WP_Block_Bindings_Registry The main instance. 275 */ 276 public static function get_instance() { 277 self::$instance ??= new self(); 278 279 return self::$instance; 280 } 281 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Thu Oct 8 08:20:33 2026 | Cross-referenced by PHPXref |