From aa7d06fc3b4777a6a632e6589c99e9dc53baff58 Mon Sep 17 00:00:00 2001 From: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Date: Tue, 30 Jan 2024 15:34:49 +0100 Subject: [PATCH] Block Bindings: Change `core/pattern-attributes` source for `core/pattern-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 --- .../wordpress-6.5/block-bindings/sources/pattern.php | 10 +++++----- .../wordpress-6.5/block-bindings/sources/post-meta.php | 9 ++++++++- packages/block-library/src/block/edit.js | 4 ++-- .../src/components/partial-syncing-controls.js | 8 ++++---- .../e2e/specs/editor/various/pattern-overrides.spec.js | 6 +++--- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/compat/wordpress-6.5/block-bindings/sources/pattern.php b/lib/compat/wordpress-6.5/block-bindings/sources/pattern.php index b43484660f1f8e..503723596bf9cc 100644 --- a/lib/compat/wordpress-6.5/block-bindings/sources/pattern.php +++ b/lib/compat/wordpress-6.5/block-bindings/sources/pattern.php @@ -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', ) ); diff --git a/lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php b/lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php index 4166ed3243ef0d..e9b84108e52a34 100644 --- a/lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php +++ b/lib/compat/wordpress-6.5/block-bindings/sources/post-meta.php @@ -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 ); } @@ -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', ) ); diff --git a/packages/block-library/src/block/edit.js b/packages/block-library/src/block/edit.js index b76b655ee0d943..c940f36e1a8b86 100644 --- a/packages/block-library/src/block/edit.js +++ b/packages/block-library/src/block/edit.js @@ -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 ); } diff --git a/packages/patterns/src/components/partial-syncing-controls.js b/packages/patterns/src/components/partial-syncing-controls.js index 0f6cde5ce7d689..f55ee7bdc26d36 100644 --- a/packages/patterns/src/components/partial-syncing-controls.js +++ b/packages/patterns/src/components/partial-syncing-controls.js @@ -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. @@ -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 ]; } @@ -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', }; } } @@ -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 ); diff --git a/test/e2e/specs/editor/various/pattern-overrides.spec.js b/test/e2e/specs/editor/various/pattern-overrides.spec.js index 541f2f1600ad29..417827a9071581 100644 --- a/test/e2e/specs/editor/various/pattern-overrides.spec.js +++ b/test/e2e/specs/editor/various/pattern-overrides.spec.js @@ -92,7 +92,7 @@ test.describe( 'Pattern Overrides', () => { id: expect.any( String ), bindings: { content: { - source: 'core/pattern-attributes', + source: 'core/pattern-overrides', }, }, }, @@ -222,7 +222,7 @@ test.describe( 'Pattern Overrides', () => { const paragraphId = 'paragraph-id'; const { id } = await requestUtils.createBlock( { title: 'Pattern', - content: ` + content: `

Editable

`, status: 'publish', @@ -270,7 +270,7 @@ test.describe( 'Pattern Overrides', () => { const { id } = await requestUtils.createBlock( { title: 'Pattern with overrides', content: ` -
+
`,