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: Add canUpdateBlockBindings editor setting #7258

2 changes: 2 additions & 0 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
}
}

$editor_settings['canUpdateBlockBindings'] = current_user_can( 'edit_block_binding', $block_editor_context );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a new setting and not something you access through coreData canUser or something?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason we started using a new setting was to allow users to disable this behavior with a PHP filter if wanted. And it seems there was a precedence for this with canLockBlocks: link.

It's true that user capabilities can also be modified through a filter, so if we are able to access edit_block_binding in the editor somehow, we wouldn't need an editor setting.

I couldn't find a way to do so, but I must say I'm not familiar with user capabilities.

I'm happy to explore other paths. Is it possible to access these user capabilities? From what I understood, canUser checks whether the user can perform the given action on the given REST resource. But that's not exactly what we need here.

Another possibility to replicate the capabilities mapping in JS, but I am not sure it makes sense and I believe users wouldn't be able to override this behavior easily.

Any thoughts are more than welcome.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of precedents for block editor settings indeed. But IMO, we should start moving away from them like I explained on my recent blog posts about Gutenberg practices.

In this case, it's a capability check and we should just have a unique way to fetch capabilities from the client. I think @Mamaduka might know better whether that's already possible or not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, I'm not saying that users shouldn't override the capability in php, I'm saying that the frontend should access the capability through the capability APIs and not block editor settings.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying that the frontend should access the capability through the capability APIs and not block editor settings.

If we can make it work this way, I agree it seems the best path forward. I will take another look, but I couldn't find a way to do that, at least with the existing APIs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the difference with the global settings is that this endpoint is user specific. So maybe a "user settings" endpoint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing that, I wonder if that's just the user endpoint though :P

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not wrong, that endpoint has never made it into Core.

Yeah looks like it's still GB only. We should probably merge that, I can't remember any specific reasons why we held off on it.

I'm personally convinced this is not user preferences though.

I'm not too fussed if it's a "preference" or a "setting". But I do think it's not a capability.

I think we need a consistent global settings endpoint in Core.

Along the lines of wp-block-editor/v1/settings?

I know we used the "root" endpoint for that sometimes. Maybe we should just continue using that

I think this is less of an index type of thing, since it is so spceific to Block Editor UI, and not the general capabiliites of the site.

I guess the difference with the global settings is that this endpoint is user specific. So maybe a "user settings" endpoint. Writing that, I wonder if that's just the user endpoint though :P

I don't think it would be odd, IMO, for wp-block-editor/v1/settings to take into account the user when building the settings. It's already something that I imagine extenders are using when filtering.

Copy link
Contributor

@youknowriad youknowriad Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would be odd, IMO, for wp-block-editor/v1/settings to take into account the user when building the settings. It's already something that I imagine extenders are using when filtering.

Just to be clear, I think a block editor settings endpoint would work here. But I wonder if it's a bit shortsighted in the sense that we'll have a need for similar settings (user settings) outside the block editor. (For instance, a setting to prevent users from creating custom WP-Admin views or a setting to prevent users from using "filters" in the different admin pages, or random things like that)...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should start exploring that in more detail.


/**
* Filters the settings to pass to the block editor for all editor type.
*
Expand Down
26 changes: 26 additions & 0 deletions src/wp-includes/capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,32 @@ function map_meta_cap( $cap, $user_id, ...$args ) {
case 'delete_app_password':
$caps = map_meta_cap( 'edit_user', $user_id, $args[0] );
break;
case 'edit_block_binding':
$block_editor_context = $args[0];
if ( isset( $block_editor_context ) && isset( $block_editor_context->post ) ) {
SantosGuillamot marked this conversation as resolved.
Show resolved Hide resolved
$object_id = $block_editor_context->post->ID;
}
/*
* If the post ID is null, check if the context is the site editor.
* Fall back to the edit_theme_options in that case.
*/
if ( ! isset( $object_id ) ) {
if ( ! isset( $block_editor_context->name ) || 'core/edit-site' !== $block_editor_context->name ) {
$caps[] = 'do_not_allow';
break;
}
$caps = map_meta_cap( 'edit_theme_options', $user_id );
break;
}

$object_subtype = get_object_subtype( 'post', (int) $object_id );
if ( empty( $object_subtype ) ) {
$caps[] = 'do_not_allow';
break;
}

$caps = map_meta_cap( "edit_{$object_subtype}", $user_id, $object_id );
break;
default:
// Handle meta capabilities for custom post types.
global $post_type_meta_caps;
Expand Down
51 changes: 50 additions & 1 deletion tests/phpunit/tests/user/capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@ public function testMetaCapsTestsAreCorrect() {
$expected['read_app_password'],
$expected['edit_app_password'],
$expected['delete_app_passwords'],
$expected['delete_app_password']
$expected['delete_app_password'],
$expected['edit_block_binding']
);

$expected = array_keys( $expected );
Expand Down Expand Up @@ -2376,4 +2377,52 @@ public function data_block_caps() {

return $data;
}

/**
* Test `edit_block_binding` meta capability is properly mapped.
*
* @ticket 61945
*/
public function test_edit_block_binding_caps_are_mapped_correctly() {
$author = self::$users['administrator'];
$post = self::factory()->post->create_and_get(
array(
'post_author' => $author->ID,
'post_type' => 'post',
)
);

foreach ( self::$users as $role => $user ) {
// It should map to `edit_{post_type}` if editing a post.
$this->assertSame(
user_can( $user->ID, 'edit_post', $post->ID ),
user_can(
$user->ID,
'edit_block_binding',
new WP_Block_Editor_Context(
array(
'post' => $post,
'name' => 'core/edit-post',
)
)
),
"Role: {$role} in post editing"
);
// It should map to `edit_theme_options` if editing a template.
$this->assertSame(
user_can( $user->ID, 'edit_theme_options' ),
user_can(
$user->ID,
'edit_block_binding',
new WP_Block_Editor_Context(
array(
'post' => null,
'name' => 'core/edit-site',
)
)
),
"Role: {$role} in template editing"
);
}
}
}
Loading