Changeset 57526 for trunk/src/wp-includes/class-wp-block.php
- Timestamp:
- 02/02/2024 08:22:11 PM (15 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-block.php
r57514 r57526 212 212 * "bindings": { 213 213 * "title": { 214 * "source": " post_meta",214 * "source": "core/post-meta", 215 215 * "args": { "key": "text_custom_field" } 216 216 * }, 217 217 * "url": { 218 * "source": " post_meta",218 * "source": "core/post-meta", 219 219 * "args": { "key": "url_custom_field" } 220 220 * } … … 227 227 * block with the values of the `text_custom_field` and `url_custom_field` post meta. 228 228 * 229 * @access private230 229 * @since 6.5.0 231 230 * 232 * @param string $block_content Block content. 233 * @param array $block The full block, including name and attributes. 231 * @param string $block_content Block content. 232 * @param array $block The full block, including name and attributes. 233 * @return string The modified block content. 234 234 */ 235 235 private function process_block_bindings( $block_content ) { 236 $ block = $this->parsed_block;236 $parsed_block = $this->parsed_block; 237 237 238 238 // Allowed blocks that support block bindings. … … 247 247 // If the block doesn't have the bindings property, isn't one of the allowed 248 248 // block types, or the bindings property is not an array, return the block content. 249 if ( ! isset( $block['attrs']['metadata']['bindings'] ) || 250 ! is_array( $block['attrs']['metadata']['bindings'] ) || 251 ! isset( $allowed_blocks[ $this->name ] ) 252 ) { 249 if ( 250 ! isset( $allowed_blocks[ $this->name ] ) || 251 empty( $parsed_block['attrs']['metadata']['bindings'] ) || 252 ! is_array( $parsed_block['attrs']['metadata']['bindings'] ) 253 ) { 253 254 return $block_content; 254 255 } 255 256 256 $block_bindings_sources = get_all_registered_block_bindings_sources();257 257 $modified_block_content = $block_content; 258 foreach ( $ block['attrs']['metadata']['bindings'] as $binding_attribute => $binding_source) {259 // If the attribute is not in the list, process next attribute.260 if ( ! in_array( $ binding_attribute, $allowed_blocks[ $this->name ], true ) ) {258 foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) { 259 // If the attribute is not in the allowed list, process next attribute. 260 if ( ! in_array( $attribute_name, $allowed_blocks[ $this->name ], true ) ) { 261 261 continue; 262 262 } 263 263 // If no source is provided, or that source is not registered, process next attribute. 264 if ( ! isset( $b inding_source['source'] ) || ! is_string( $binding_source['source'] ) || ! isset( $block_bindings_sources[ $binding_source['source']] ) ) {264 if ( ! isset( $block_binding['source'] ) || ! is_string( $block_binding['source'] ) ) { 265 265 continue; 266 266 } 267 267 268 $source_callback = $block_bindings_sources[ $binding_source['source'] ]['get_value_callback']; 269 // Get the value based on the source. 270 if ( ! isset( $binding_source['args'] ) ) { 271 $source_args = array(); 272 } else { 273 $source_args = $binding_source['args']; 274 } 275 $source_value = call_user_func_array( $source_callback, array( $source_args, $this, $binding_attribute ) ); 276 // If the value is null, process next attribute. 277 if ( is_null( $source_value ) ) { 268 $block_binding_source = get_block_bindings_source( $block_binding['source'] ); 269 if ( null === $block_binding_source ) { 278 270 continue; 279 271 } 280 272 281 // Process the HTML based on the block and the attribute. 282 $modified_block_content = $this->replace_html( $modified_block_content, $this->name, $binding_attribute, $source_value ); 283 } 273 $source_callback = $block_binding_source['get_value_callback']; 274 $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); 275 $source_value = call_user_func_array( $source_callback, array( $source_args, $this, $attribute_name ) ); 276 277 // If the value is not null, process the HTML based on the block and the attribute. 278 if ( ! is_null( $source_value ) ) { 279 $modified_block_content = $this->replace_html( $modified_block_content, $attribute_name, $source_value ); 280 } 281 } 282 284 283 return $modified_block_content; 285 284 } 286 285 287 286 /** 288 * Depending on the block attribute s, replace the HTML based on the value returned by the source.287 * Depending on the block attribute name, replace its value in the HTML based on the value provided. 289 288 * 290 289 * @since 6.5.0 291 290 * 292 * @param string $block_content Block content.293 * @param string $ block_name The name of the block to process.294 * @param string $block_attr The attribute of the block we want to process.295 * @ param string $source_value The value used to replace the HTML.296 */ 297 private function replace_html( string $block_content, string $ block_name, string $block_attr, string$source_value ) {291 * @param string $block_content Block content. 292 * @param string $attribute_name The attribute name to replace. 293 * @param mixed $source_value The value used to replace in the HTML. 294 * @return string The modified block content. 295 */ 296 private function replace_html( string $block_content, string $attribute_name, $source_value ) { 298 297 $block_type = $this->block_type; 299 if ( null === $block_type || ! isset( $block_type->attributes[ $block_attr] ) ) {298 if ( ! isset( $block_type->attributes[ $attribute_name ] ) ) { 300 299 return $block_content; 301 300 } 302 301 303 302 // Depending on the attribute source, the processing will be different. 304 switch ( $block_type->attributes[ $ block_attr]['source'] ) {303 switch ( $block_type->attributes[ $attribute_name ]['source'] ) { 305 304 case 'html': 306 305 case 'rich-text': … … 309 308 // TODO: Support for CSS selectors whenever they are ready in the HTML API. 310 309 // In the meantime, support comma-separated selectors by exploding them into an array. 311 $selectors = explode( ',', $block_type->attributes[ $ block_attr]['selector'] );310 $selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] ); 312 311 // Add a bookmark to the first tag to be able to iterate over the selectors. 313 312 $block_reader->next_tag(); … … 317 316 // Store the parent tag and its attributes to be able to restore them later in the button. 318 317 // The button block has a wrapper while the paragraph and heading blocks don't. 319 if ( 'core/button' === $ block_name ) {318 if ( 'core/button' === $this->name ) { 320 319 $button_wrapper = $block_reader->get_tag(); 321 320 $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); … … 349 348 $amended_content->set_attribute( $attribute_key, $attribute_value ); 350 349 } 351 if ( 'core/paragraph' === $ block_name || 'core/heading' === $block_name ) {350 if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) { 352 351 return $amended_content->get_updated_html(); 353 352 } 354 if ( 'core/button' === $ block_name ) {353 if ( 'core/button' === $this->name ) { 355 354 $button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>"; 356 355 $amended_button = new WP_HTML_Tag_Processor( $button_markup ); … … 373 372 array( 374 373 // TODO: build the query from CSS selector. 375 'tag_name' => $block_type->attributes[ $ block_attr]['selector'],374 'tag_name' => $block_type->attributes[ $attribute_name ]['selector'], 376 375 ) 377 376 ) ) { 378 377 return $block_content; 379 378 } 380 $amended_content->set_attribute( $block_type->attributes[ $ block_attr]['attribute'], esc_attr( $source_value ) );379 $amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], esc_attr( $source_value ) ); 381 380 return $amended_content->get_updated_html(); 382 381 break;
Note: See TracChangeset
for help on using the changeset viewer.