Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Bindings: Don't show protected fields that are bound to blocks #59326

Merged
merged 9 commits into from
Feb 28, 2024
13 changes: 13 additions & 0 deletions lib/compat/wordpress-6.5/block-bindings/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ function gutenberg_block_bindings_post_meta_callback( $source_attrs, $block_inst
return null;
}

// Check if the meta field is protected.
if ( is_protected_meta( $source_attrs['key'], 'post' ) ) {
return null;
}

// Check if the meta field is registered to be shown in REST.
$meta_keys = get_registered_meta_keys( 'post', $block_instance->context['postType'] );
// Add fields registered for all subtypes.
$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( 'post', '' ) );
if ( empty( $meta_keys[ $source_attrs['key'] ]['show_in_rest'] ) || false === $meta_keys[ $source_attrs['key'] ]['show_in_rest'] ) {
SantosGuillamot marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

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

Expand Down
20 changes: 19 additions & 1 deletion packages/e2e-tests/plugins/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function gutenberg_test_block_bindings_register_custom_fields() {
'default' => 'Value of the text_custom_field',
)
);
// TODO: Change url.
register_meta(
'post',
'url_custom_field',
Expand All @@ -32,5 +31,24 @@ function gutenberg_test_block_bindings_register_custom_fields() {
'default' => '#url-custom-field',
)
);
register_meta(
'post',
'_protected_field',
array(
'type' => 'string',
'single' => true,
'default' => 'protected field value',
)
);
register_meta(
'post',
'show_in_rest_false_field',
array(
'show_in_rest' => false,
'type' => 'string',
'single' => true,
'default' => 'show_in_rest false field value',
)
);
}
add_action( 'init', 'gutenberg_test_block_bindings_register_custom_fields' );
72 changes: 68 additions & 4 deletions test/e2e/specs/editor/various/block-bindings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@
name: 'core/paragraph',
attributes: {
anchor: 'paragraph-binding',
content: 'p',
content: 'fallback value',
metadata: {
bindings: {
content: {
Expand All @@ -1244,9 +1244,73 @@
// Check the frontend doesn't show the content.
const postId = await editor.publishPost();
await page.goto( `/?p=${ postId }` );
await expect(
page.locator( '#paragraph-binding' )
).toBeHidden();
await expect( page.locator( '#paragraph-binding' ) ).toHaveText(

Check failure on line 1247 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1213:4 › Block bindings › Post/page context › Paragraph › Should show the value of the key when custom field doesn't exists

1) [chromium] › editor/various/block-bindings.spec.js:1213:4 › Block bindings › Post/page context › Paragraph › Should show the value of the key when custom field doesn't exists Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "non_existing_custom_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" 1245 | const postId = await editor.publishPost(); 1246 | await page.goto( `/?p=${ postId }` ); > 1247 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1248 | 'fallback value' 1249 | ); 1250 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1247:58

Check failure on line 1247 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1213:4 › Block bindings › Post/page context › Paragraph › Should show the value of the key when custom field doesn't exists

1) [chromium] › editor/various/block-bindings.spec.js:1213:4 › Block bindings › Post/page context › Paragraph › Should show the value of the key when custom field doesn't exists Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "non_existing_custom_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" 1245 | const postId = await editor.publishPost(); 1246 | await page.goto( `/?p=${ postId }` ); > 1247 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1248 | 'fallback value' 1249 | ); 1250 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1247:58

Check failure on line 1247 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1213:4 › Block bindings › Post/page context › Paragraph › Should show the value of the key when custom field doesn't exists

1) [chromium] › editor/various/block-bindings.spec.js:1213:4 › Block bindings › Post/page context › Paragraph › Should show the value of the key when custom field doesn't exists Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "non_existing_custom_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" - locator resolved to <p id="paragraph-binding">non_existing_custom_field</p> - unexpected value "non_existing_custom_field" 1245 | const postId = await editor.publishPost(); 1246 | await page.goto( `/?p=${ postId }` ); > 1247 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1248 | 'fallback value' 1249 | ); 1250 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1247:58
'fallback value'
);
} );

test( 'should not show the value of a protected meta field', async ( {
editor,
page,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
anchor: 'paragraph-binding',
content: 'fallback value',
metadata: {
bindings: {
content: {
source: 'core/post-meta',
args: { key: '_protected_field' },
},
},
},
},
} );
const paragraphBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Paragraph',
} );
await expect( paragraphBlock ).toHaveText( '_protected_field' );
// Check the frontend doesn't show the content.
const postId = await editor.publishPost();
await page.goto( `/?p=${ postId }` );
await expect( page.locator( '#paragraph-binding' ) ).toHaveText(

Check failure on line 1278 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1252:4 › Block bindings › Post/page context › Paragraph › should not show the value of a protected meta field

2) [chromium] › editor/various/block-bindings.spec.js:1252:4 › Block bindings › Post/page context › Paragraph › should not show the value of a protected meta field Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "_protected_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" 1276 | const postId = await editor.publishPost(); 1277 | await page.goto( `/?p=${ postId }` ); > 1278 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1279 | 'fallback value' 1280 | ); 1281 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1278:58

Check failure on line 1278 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1252:4 › Block bindings › Post/page context › Paragraph › should not show the value of a protected meta field

2) [chromium] › editor/various/block-bindings.spec.js:1252:4 › Block bindings › Post/page context › Paragraph › should not show the value of a protected meta field Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "_protected_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" 1276 | const postId = await editor.publishPost(); 1277 | await page.goto( `/?p=${ postId }` ); > 1278 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1279 | 'fallback value' 1280 | ); 1281 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1278:58

Check failure on line 1278 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1252:4 › Block bindings › Post/page context › Paragraph › should not show the value of a protected meta field

2) [chromium] › editor/various/block-bindings.spec.js:1252:4 › Block bindings › Post/page context › Paragraph › should not show the value of a protected meta field Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "_protected_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" - locator resolved to <p id="paragraph-binding">_protected_field</p> - unexpected value "_protected_field" 1276 | const postId = await editor.publishPost(); 1277 | await page.goto( `/?p=${ postId }` ); > 1278 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1279 | 'fallback value' 1280 | ); 1281 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1278:58
'fallback value'
);
} );

test( 'should not show the value of a meta field with `show_in_rest` false', async ( {
editor,
page,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
anchor: 'paragraph-binding',
content: 'fallback value',
metadata: {
bindings: {
content: {
source: 'core/post-meta',
args: { key: 'show_in_rest_false_field' },
},
},
},
},
} );
const paragraphBlock = editor.canvas.getByRole( 'document', {
name: 'Block: Paragraph',
} );
await expect( paragraphBlock ).toHaveText(
'show_in_rest_false_field'
);
// Check the frontend doesn't show the content.
const postId = await editor.publishPost();
await page.goto( `/?p=${ postId }` );
await expect( page.locator( '#paragraph-binding' ) ).toHaveText(

Check failure on line 1311 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1283:4 › Block bindings › Post/page context › Paragraph › should not show the value of a meta field with `show_in_rest` false

3) [chromium] › editor/various/block-bindings.spec.js:1283:4 › Block bindings › Post/page context › Paragraph › should not show the value of a meta field with `show_in_rest` false Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "show_in_rest_false_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" 1309 | const postId = await editor.publishPost(); 1310 | await page.goto( `/?p=${ postId }` ); > 1311 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1312 | 'fallback value' 1313 | ); 1314 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1311:58

Check failure on line 1311 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1283:4 › Block bindings › Post/page context › Paragraph › should not show the value of a meta field with `show_in_rest` false

3) [chromium] › editor/various/block-bindings.spec.js:1283:4 › Block bindings › Post/page context › Paragraph › should not show the value of a meta field with `show_in_rest` false Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "show_in_rest_false_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" 1309 | const postId = await editor.publishPost(); 1310 | await page.goto( `/?p=${ postId }` ); > 1311 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1312 | 'fallback value' 1313 | ); 1314 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1311:58

Check failure on line 1311 in test/e2e/specs/editor/various/block-bindings.spec.js

View workflow job for this annotation

GitHub Actions / Playwright - 3

[chromium] › editor/various/block-bindings.spec.js:1283:4 › Block bindings › Post/page context › Paragraph › should not show the value of a meta field with `show_in_rest` false

3) [chromium] › editor/various/block-bindings.spec.js:1283:4 › Block bindings › Post/page context › Paragraph › should not show the value of a meta field with `show_in_rest` false Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: locator('#paragraph-binding') Expected string: "fallback value" Received string: "show_in_rest_false_field" Call log: - expect.toHaveText with timeout 5000ms - waiting for locator('#paragraph-binding') - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" - locator resolved to <p id="paragraph-binding">show_in_rest_false_field</p> - unexpected value "show_in_rest_false_field" 1309 | const postId = await editor.publishPost(); 1310 | await page.goto( `/?p=${ postId }` ); > 1311 | await expect( page.locator( '#paragraph-binding' ) ).toHaveText( | ^ 1312 | 'fallback value' 1313 | ); 1314 | } ); at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/block-bindings.spec.js:1311:58
'fallback value'
);
} );
} );

Expand Down
Loading