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: Map edit_block_binding capability #65116

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backport-changelog/6.7/7258.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/7258

* https://github.com/WordPress/gutenberg/pull/64570
* https://github.com/WordPress/gutenberg/pull/64570
* https://github.com/WordPress/gutenberg/pull/65116
53 changes: 48 additions & 5 deletions lib/compat/wordpress-6.7/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,63 @@ function gutenberg_bootstrap_server_block_bindings_sources() {

add_action( 'enqueue_block_editor_assets', 'gutenberg_bootstrap_server_block_bindings_sources', 5 );

/**
* Map the `edit_block_binding` capability to `edit_{object_subtype}` or `edit_theme_options``
* depending on the context.
* @param array $caps Primitive capabilities required of the user.
* @param string $cap Capability being checked.
* @param int $user_id The user ID.
* @param array $args Argumenst that add the context to the cap.
* @return array The updated capabilities.
*/
function gutenberg_add_edit_block_binding_capability( $caps, $cap, $user_id, $args ) {
if ( 'edit_block_binding' === $cap ) {
$block_editor_context = $args[0];
if ( isset( $block_editor_context->post ) ) {
$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 ) {
return array( 'do_not_allow' );
}
$caps = map_meta_cap( 'edit_theme_options', $user_id );
} else {
$object_subtype = get_object_subtype( 'post', (int) $object_id );
if ( empty( $object_subtype ) ) {
return array( 'do_not_allow' );
}
$post_type_object = get_post_type_object( $object_subtype );
// Initialize empty array if it doesn't exist.
if ( ! isset( $post_type_object->capabilities ) ) {
$post_type_object->capabilities = array();
}
$post_type_capabilities = get_post_type_capabilities( $post_type_object );
$caps = map_meta_cap( $post_type_capabilities->edit_post, $user_id, $object_id );
}
}
return $caps;
}
add_filter( 'map_meta_cap', 'gutenberg_add_edit_block_binding_capability', 5, 4 );

/**
* Initialize `canUpdateBlockBindings` editor setting if it doesn't exist. By default, it is `true` only for admin users.
*
* @param array $settings The block editor settings from the `block_editor_settings_all` filter.
* @param array $editor_settings The block editor settings from the `block_editor_settings_all` filter.
* @param array $block_editor_context The block editor context.
* @return array The editor settings including `canUpdateBlockBindings`.
*/
function gutenberg_add_can_update_block_bindings_editor_setting( $editor_settings ) {
if ( empty( $editor_settings['canUpdateBlockBindings'] ) ) {
$editor_settings['canUpdateBlockBindings'] = current_user_can( 'manage_options' );
function gutenberg_add_can_update_block_bindings_editor_setting( $editor_settings, $block_editor_context ) {
if ( ! isset( $editor_settings['canUpdateBlockBindings'] ) ) {
Copy link
Contributor Author

@SantosGuillamot SantosGuillamot Oct 15, 2024

Choose a reason for hiding this comment

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

Using empty was wrong because it could be false and that is a valid value. This means we were overriding the setting from core even if it was defined there.

$editor_settings['canUpdateBlockBindings'] = current_user_can( 'edit_block_binding', $block_editor_context );
}
return $editor_settings;
}

add_filter( 'block_editor_settings_all', 'gutenberg_add_can_update_block_bindings_editor_setting', 10 );
add_filter( 'block_editor_settings_all', 'gutenberg_add_can_update_block_bindings_editor_setting', 10, 2 );

/**
* Add `label` to `register_meta`.
Expand Down
Loading