[wp-trac] [WordPress Trac] #45451: Filter to allow plugins override block attributes

WordPress Trac noreply at wordpress.org
Fri Nov 30 13:37:33 UTC 2018


#45451: Filter to allow plugins override block attributes
-------------------------+-----------------------------
 Reporter:  kkarpieszuk  |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  5.0
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 **## Description**

 Add a filter so plugins and themes can override block attributes.

 Originally I submitted it as MR to Gutenberg here
 https://github.com/WordPress/gutenberg/pull/11730 but I have been asked to
 move it directly to WordPress repo.

 **## Why is this needed?**

 Dynamic blocks, rendered server side keep their values in block
 attributes. Some of those attributes might be texts printed in the front-
 end to be read by people. This kind of strings should be available for
 translation.

 We are preparing in WPML code to make it translatable, but we need to hook
 somewhere in Gutenberg to filter values. This place seems to be most
 appropriate for this.

 **## How has this been tested?**

 This has been tested in WPML with following code example (to translate
 dynamic strings created with ACF Blocks beta).


 {{{
 add_filter( 'block_prepared_attributes', 'block_attributes_filter', 1, 10
 );
 function block_attributes_filter( $attributes, $block_type ) {
     if ( $block_type->name === 'my-plugin/my-block' &&  isset(
 $attributes['data'] ) ) {
             $original_post_id = apply_filters( 'wpml_object_id',
 get_the_ID(), get_post_type(), true, apply_filters(
 'wpml_default_language', null) );
             foreach ( $attributes['data'] as $name => $value ) {
                 $string_name =
 WPML_Gutenberg_Strings_In_Block::get_string_id($attributes['name'],
 $value);
                 $translated = apply_filters( 'wpml_translate_string',
                     $value,
                     $string_name,
                     array('kind' => 'Gutenberg', 'name' =>
 $original_post_id) );
                 $attributes['data'][$name] = $translated;
             }
         }

         return $attributes;
 }
 }}}

 **## Types of changes**

 Add a new filter

 **## Checklist:**

 - [x] My code is tested.
 - [x] My code follows the WordPress code style. <!-- Check code: `npm run
 lint`, Guidelines: https://make.wordpress.org/core/handbook/best-practices
 /coding-standards/javascript/ -->
 - [x] My code follows the accessibility standards. <!-- Guidelines:
 https://make.wordpress.org/core/handbook/best-practices/coding-standards
 /accessibility-coding-standards/ -->
 - [x] My code has proper inline documentation. <!-- Guidelines:
 https://make.wordpress.org/core/handbook/best-practices/inline-
 documentation-standards/javascript/ -->

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/45451>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list