Skip to content

Commit

Permalink
Block Bindings: Change core/pattern-attributes source for `core/pat…
Browse files Browse the repository at this point in the history
…tern-overrides` (#58434)

* Change `pattern-attributes` for `pattern-overrides`

* Use `_x()` function for translations

* Add check in post meta source

* Update pattern description

* Update `_x( )` context to be more concise
  • Loading branch information
SantosGuillamot committed Jan 30, 2024
1 parent 3eb68c2 commit aa7d06f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
10 changes: 5 additions & 5 deletions lib/compat/wordpress-6.5/block-bindings/sources/pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ function gutenberg_block_bindings_pattern_overrides_callback( $source_attrs, $bl
}

function gutenberg_register_block_bindings_pattern_overrides_source() {
// Override the "core/pattern-attributes" source from core.
if ( array_key_exists( 'core/pattern-attributes', get_all_registered_block_bindings_sources() ) ) {
unregister_block_bindings_source( 'core/pattern-attributes' );
// Override the "core/pattern-overrides" source from core.
if ( array_key_exists( 'core/pattern-overrides', get_all_registered_block_bindings_sources() ) ) {
unregister_block_bindings_source( 'core/pattern-overrides' );
}
register_block_bindings_source(
'core/pattern-attributes',
'core/pattern-overrides',
array(
'label' => __( 'Pattern Attributes' ),
'label' => _x( 'Pattern Overrides', 'block bindings source' ),
'get_value_callback' => 'gutenberg_block_bindings_pattern_overrides_callback',
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ function gutenberg_block_bindings_post_meta_callback( $source_attrs ) {
$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 ( ( $post && 'publish' !== $post->post_status && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post_id ) ) {
return null;
}

return get_post_meta( $post_id, $source_attrs['key'], true );
}

Expand All @@ -28,7 +35,7 @@ function gutenberg_register_block_bindings_post_meta_source() {
register_block_bindings_source(
'core/post-meta',
array(
'label' => __( 'Post Meta' ),
'label' => _x( 'Post Meta', 'block bindings source' ),
'get_value_callback' => 'gutenberg_block_bindings_post_meta_callback',
)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ function isPartiallySynced( block ) {
) &&
!! block.attributes.metadata?.bindings &&
Object.values( block.attributes.metadata.bindings ).some(
( binding ) => binding.source === 'core/pattern-attributes'
( binding ) => binding.source === 'core/pattern-overrides'
)
);
}
function getPartiallySyncedAttributes( block ) {
return Object.entries( block.attributes.metadata.bindings )
.filter(
( [ , binding ] ) => binding.source === 'core/pattern-attributes'
( [ , binding ] ) => binding.source === 'core/pattern-overrides'
)
.map( ( [ attributeKey ] ) => attributeKey );
}
Expand Down
8 changes: 4 additions & 4 deletions packages/patterns/src/components/partial-syncing-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
attributes.metadata?.bindings?.[ attributeName ]?.source
);
const isConnectedToOtherSources = attributeSources.every(
( source ) => source && source !== 'core/pattern-attributes'
( source ) => source && source !== 'core/pattern-overrides'
);

// Render nothing if all supported attributes are connected to other sources.
Expand All @@ -39,7 +39,7 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
for ( const attributeName of Object.keys( syncedAttributes ) ) {
if (
updatedBindings[ attributeName ]?.source ===
'core/pattern-attributes'
'core/pattern-overrides'
) {
delete updatedBindings[ attributeName ];
}
Expand All @@ -59,7 +59,7 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
for ( const attributeName of Object.keys( syncedAttributes ) ) {
if ( ! updatedBindings[ attributeName ] ) {
updatedBindings[ attributeName ] = {
source: 'core/pattern-attributes',
source: 'core/pattern-overrides',
};
}
}
Expand Down Expand Up @@ -94,7 +94,7 @@ function PartialSyncingControls( { name, attributes, setAttributes } ) {
__nextHasNoMarginBottom
label={ __( 'Allow instance overrides' ) }
checked={ attributeSources.some(
( source ) => source === 'core/pattern-attributes'
( source ) => source === 'core/pattern-overrides'
) }
onChange={ ( isChecked ) => {
updateBindings( isChecked );
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test.describe( 'Pattern Overrides', () => {
id: expect.any( String ),
bindings: {
content: {
source: 'core/pattern-attributes',
source: 'core/pattern-overrides',
},
},
},
Expand Down Expand Up @@ -222,7 +222,7 @@ test.describe( 'Pattern Overrides', () => {
const paragraphId = 'paragraph-id';
const { id } = await requestUtils.createBlock( {
title: 'Pattern',
content: `<!-- wp:paragraph {"metadata":{"id":"${ paragraphId }","bindings":{"content":{"source":"core/pattern-attributes"}}}} -->
content: `<!-- wp:paragraph {"metadata":{"id":"${ paragraphId }","bindings":{"content":{"source":"core/pattern-overrides"}}}} -->
<p>Editable</p>
<!-- /wp:paragraph -->`,
status: 'publish',
Expand Down Expand Up @@ -270,7 +270,7 @@ test.describe( 'Pattern Overrides', () => {
const { id } = await requestUtils.createBlock( {
title: 'Pattern with overrides',
content: `<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"metadata":{"id":"${ buttonId }","bindings":{"text":{"source":"core/pattern-attributes"},"url":{"source":"core/pattern-attributes"},"linkTarget":{"source":"core/pattern-attributes"}}}} -->
<div class="wp-block-buttons"><!-- wp:button {"metadata":{"id":"${ buttonId }","bindings":{"text":{"source":"core/pattern-overrides"},"url":{"source":"core/pattern-overrides"},"linkTarget":{"source":"core/pattern-overrides"}}}} -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="http://wp.org" target="_blank" rel="noreferrer noopener">wp.org</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->`,
Expand Down

0 comments on commit aa7d06f

Please sign in to comment.