Skip to content

Commit 235cb39

Browse files
committed
Editor: Refactor the way block bindings sources are handled
It fixes the coding style issues reported. It goes further and improves the code quality it other places where the logic for block bindings was added. Follow-up for [57514]. Props: gziolo, mukesh27, youknowriad, santosguillamot. See #60282. git-svn-id: https://develop.svn.wordpress.org/trunk@57526 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7e34c4b commit 235cb39

10 files changed

Lines changed: 188 additions & 167 deletions

File tree

src/wp-includes/block-bindings.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
*
2020
* @since 6.5.0
2121
*
22-
* @param string $source_name The name of the source. It must be a string containing a namespace prefix, i.e.
23-
* `my-plugin/my-custom-source`. It must only contain lowercase alphanumeric
24-
* characters, the forward slash `/` and dashes.
25-
* @param array $source_properties {
22+
* @param string $source_name The name of the source. It must be a string containing a namespace prefix, i.e.
23+
* `my-plugin/my-custom-source`. It must only contain lowercase alphanumeric
24+
* characters, the forward slash `/` and dashes.
25+
* @param array $source_properties {
2626
* The array of arguments that are used to register a source.
2727
*
2828
* @type string $label The label of the source.
@@ -40,7 +40,7 @@
4040
* }
4141
* @return array|false Source when the registration was successful, or `false` on failure.
4242
*/
43-
function register_block_bindings_source( $source_name, array $source_properties ) {
43+
function register_block_bindings_source( string $source_name, array $source_properties ) {
4444
return WP_Block_Bindings_Registry::get_instance()->register( $source_name, $source_properties );
4545
}
4646

@@ -52,7 +52,7 @@ function register_block_bindings_source( $source_name, array $source_properties
5252
* @param string $source_name Block bindings source name including namespace.
5353
* @return array|false The unregistered block bindings source on success and `false` otherwise.
5454
*/
55-
function unregister_block_bindings_source( $source_name ) {
55+
function unregister_block_bindings_source( string $source_name ) {
5656
return WP_Block_Bindings_Registry::get_instance()->unregister( $source_name );
5757
}
5858

@@ -66,3 +66,15 @@ function unregister_block_bindings_source( $source_name ) {
6666
function get_all_registered_block_bindings_sources() {
6767
return WP_Block_Bindings_Registry::get_instance()->get_all_registered();
6868
}
69+
70+
/**
71+
* Retrieves a registered block bindings source.
72+
*
73+
* @since 6.5.0
74+
*
75+
* @param string $source_name The name of the source.
76+
* @return array|null The registered block bindings source, or `null` if it is not registered.
77+
*/
78+
function get_block_bindings_source( string $source_name ) {
79+
return WP_Block_Bindings_Registry::get_instance()->get_registered( $source_name );
80+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Pattern Overrides source for the Block Bindings.
4+
*
5+
* @since 6.5.0
6+
* @package WordPress
7+
* @subpackage Block Bindings
8+
*/
9+
10+
/**
11+
* Gets value for the Pattern Overrides source.
12+
*
13+
* @since 6.5.0
14+
* @access private
15+
*
16+
* @param array $source_args Array containing source arguments used to look up the override value.
17+
* Example: array( "key" => "foo" ).
18+
* @param WP_Block $block_instance The block instance.
19+
* @param string $attribute_name The name of the target attribute.
20+
* @return mixed The value computed for the source.
21+
*/
22+
function _block_bindings_pattern_overrides_get_value( array $source_args, $block_instance, string $attribute_name ) {
23+
if ( empty( $block_instance->attributes['metadata']['id'] ) ) {
24+
return null;
25+
}
26+
$block_id = $block_instance->attributes['metadata']['id'];
27+
return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null );
28+
}
29+
30+
/**
31+
* Registers Pattern Overrides source in the Block Bindings registry.
32+
*
33+
* @since 6.5.0
34+
* @access private
35+
*/
36+
function _register_block_bindings_pattern_overrides_source() {
37+
register_block_bindings_source(
38+
'core/pattern-overrides',
39+
array(
40+
'label' => _x( 'Pattern Overrides', 'block bindings source' ),
41+
'get_value_callback' => '_block_bindings_pattern_overrides_get_value',
42+
)
43+
);
44+
}
45+
46+
add_action( 'init', '_register_block_bindings_pattern_overrides_source' );
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Post Meta source for the block bindings.
4+
*
5+
* @since 6.5.0
6+
* @package WordPress
7+
* @subpackage Block Bindings
8+
*/
9+
10+
/**
11+
* Gets value for Post Meta source.
12+
*
13+
* @since 6.5.0
14+
* @access private
15+
*
16+
* @param array $source_args Array containing source arguments used to look up the override value.
17+
* Example: array( "key" => "foo" ).
18+
* @return mixed The value computed for the source.
19+
*/
20+
function _block_bindings_post_meta_get_value( array $source_args ) {
21+
if ( ! isset( $source_args['key'] ) ) {
22+
return null;
23+
}
24+
25+
// Use the postId attribute if available.
26+
if ( isset( $source_args['postId'] ) ) {
27+
$post_id = $source_args['postId'];
28+
} else {
29+
// $block_instance->context['postId'] is not available in the Image block.
30+
$post_id = get_the_ID();
31+
}
32+
33+
// If a post isn't public, we need to prevent unauthorized users from accessing the post meta.
34+
$post = get_post( $post_id );
35+
if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) {
36+
return null;
37+
}
38+
39+
return get_post_meta( $post_id, $source_args['key'], true );
40+
}
41+
42+
/**
43+
* Registers Post Meta source in the block bindings registry.
44+
*
45+
* @since 6.5.0
46+
* @access private
47+
*/
48+
function _register_block_bindings_post_meta_source() {
49+
register_block_bindings_source(
50+
'core/post-meta',
51+
array(
52+
'label' => _x( 'Post Meta', 'block bindings source' ),
53+
'get_value_callback' => '_block_bindings_post_meta_get_value',
54+
)
55+
);
56+
}
57+
58+
add_action( 'init', '_register_block_bindings_post_meta_source' );

src/wp-includes/block-bindings/sources/pattern.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/wp-includes/block-bindings/sources/post-meta.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/wp-includes/class-wp-block-bindings-registry.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ final class WP_Block_Bindings_Registry {
5757
* used to look up the override value,
5858
* i.e. {"key": "foo"}.
5959
* - @param WP_Block $block_instance The block instance.
60-
* - @param string $attribute_name The name of an attribute .
60+
* - @param string $attribute_name The name of the target attribute.
6161
* The callback has a mixed return type; it may return a string to override
6262
* the block's original value, null, false to remove an attribute, etc.
6363
* }
6464
* @return array|false Source when the registration was successful, or `false` on failure.
6565
*/
66-
public function register( $source_name, array $source_properties ) {
66+
public function register( string $source_name, array $source_properties ) {
6767
if ( ! is_string( $source_name ) ) {
6868
_doing_it_wrong(
6969
__METHOD__,
@@ -120,7 +120,7 @@ public function register( $source_name, array $source_properties ) {
120120
* @param string $source_name Block bindings source name including namespace.
121121
* @return array|false The unregistered block bindings source on success and `false` otherwise.
122122
*/
123-
public function unregister( $source_name ) {
123+
public function unregister( string $source_name ) {
124124
if ( ! $this->is_registered( $source_name ) ) {
125125
_doing_it_wrong(
126126
__METHOD__,
@@ -156,7 +156,7 @@ public function get_all_registered() {
156156
* @param string $source_name The name of the source.
157157
* @return array|null The registered block bindings source, or `null` if it is not registered.
158158
*/
159-
public function get_registered( $source_name ) {
159+
public function get_registered( string $source_name ) {
160160
if ( ! $this->is_registered( $source_name ) ) {
161161
return null;
162162
}

0 commit comments

Comments
 (0)