forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduces the Block Bindings API for WordPress. The API allows developers to connects block attributes to different sources. In this PR, two such sources are included: "post meta" and "pattern". Attributes connected to sources can have their HTML replaced by values coming from the source in a way defined by the binding. Props czapla, lgladdy, gziolo, sc0ttkclark, swissspidy, artemiosans, kevin940726, fabiankaegy, santosguillamot, talldanwp, wildworks. Fixes #60282. git-svn-id: https://develop.svn.wordpress.org/trunk@57514 602fd350-edb4-49c9-b593-d223f7449a82
- Loading branch information
1 parent
5b46851
commit 1e564ad
Showing
9 changed files
with
416 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* The "pattern" source for the Block Bindings API. This source is used by the | ||
* Pattern Overrides. | ||
* | ||
* @since 6.5.0 | ||
* @package WordPress | ||
*/ | ||
function pattern_source_callback( $source_attrs, $block_instance, $attribute_name ) { | ||
if ( ! _wp_array_get( $block_instance->attributes, array( 'metadata', 'id' ), false ) ) { | ||
return null; | ||
} | ||
$block_id = $block_instance->attributes['metadata']['id']; | ||
return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null ); | ||
} | ||
|
||
|
||
/** | ||
* Registers the "pattern" source for the Block Bindings API. | ||
* | ||
* @access private | ||
* @since 6.5.0 | ||
*/ | ||
function _register_block_bindings_pattern_overrides_source() { | ||
register_block_bindings_source( | ||
'core/pattern-overrides', | ||
array( | ||
'label' => _x( 'Pattern Overrides', 'block bindings source' ), | ||
'get_value_callback' => 'pattern_source_callback', | ||
) | ||
); | ||
} | ||
|
||
add_action( 'init', '_register_block_bindings_pattern_overrides_source' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Add the post_meta source to the Block Bindings API. | ||
* | ||
* @since 6.5.0 | ||
* @package WordPress | ||
*/ | ||
function post_meta_source_callback( $source_attrs ) { | ||
if ( ! isset( $source_attrs['key'] ) ) { | ||
return null; | ||
} | ||
|
||
// Use the postId attribute if available | ||
if ( isset( $source_attrs['postId'] ) ) { | ||
$post_id = $source_attrs['postId']; | ||
} else { | ||
// $block_instance->context['postId'] is not available in the Image block. | ||
$post_id = get_the_ID(); | ||
} | ||
|
||
// If a post isn't public, we need to prevent | ||
// unauthorized users from accessing the post meta. | ||
$post = get_post( $post_id ); | ||
if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) { | ||
return null; | ||
} | ||
|
||
return get_post_meta( $post_id, $source_attrs['key'], true ); | ||
} | ||
|
||
/** | ||
* Registers the "post_meta" source for the Block Bindings API. | ||
* | ||
* @access private | ||
* @since 6.5.0 | ||
*/ | ||
function _register_block_bindings_post_meta_source() { | ||
register_block_bindings_source( | ||
'core/post-meta', | ||
array( | ||
'label' => _x( 'Post Meta', 'block bindings source' ), | ||
'get_value_callback' => 'post_meta_source_callback', | ||
) | ||
); | ||
} | ||
|
||
add_action( 'init', '_register_block_bindings_post_meta_source' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.