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

WP 6.3: remove IS_GUTENBERG_PLUGIN from PHP files #52274

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 0 additions & 28 deletions packages/block-library/src/file/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@
* @package WordPress
*/

if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
/**
* Replaces view script for the File block with version using Interactivity API.
*
* @param array $metadata Block metadata as read in via block.json.
*
* @return array Filtered block type metadata.
*/
function gutenberg_block_core_file_update_interactive_view_script( $metadata ) {
if ( 'core/file' === $metadata['name'] ) {
$metadata['viewScript'] = array( 'file:./interactivity.min.js' );
}
return $metadata;
}
add_filter( 'block_type_metadata', 'gutenberg_block_core_file_update_interactive_view_script', 10, 1 );
}

/**
* When the `core/file` block is rendering, check if we need to enqueue the `'wp-block-file-view` script.
*
Expand Down Expand Up @@ -70,17 +53,6 @@ static function ( $matches ) {
$content
);

// If it uses the Interactivity API, add the directives.
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && $should_load_view_script ) {
$processor = new WP_HTML_Tag_Processor( $content );
$processor->next_tag();
$processor->set_attribute( 'data-wp-interactive', '' );
$processor->next_tag( 'object' );
$processor->set_attribute( 'data-wp-bind--hidden', '!selectors.core.file.hasPdfPreview' );
$processor->set_attribute( 'hidden', true );
return $processor->get_updated_html();
}

return $content;
}

Expand Down
227 changes: 9 additions & 218 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,148 +5,6 @@
* @package WordPress
*/

// These functions are used for the __unstableLocation feature and only active
// when the gutenberg plugin is active.
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
/**
* Returns the menu items for a WordPress menu location.
*
* @param string $location The menu location.
* @return array Menu items for the location.
*/
function block_core_navigation_get_menu_items_at_location( $location ) {
if ( empty( $location ) ) {
return;
}

// Build menu data. The following approximates the code in
// `wp_nav_menu()` and `gutenberg_output_block_nav_menu`.

// Find the location in the list of locations, returning early if the
// location can't be found.
$locations = get_nav_menu_locations();
if ( ! isset( $locations[ $location ] ) ) {
return;
}

// Get the menu from the location, returning early if there is no
// menu or there was an error.
$menu = wp_get_nav_menu_object( $locations[ $location ] );
if ( ! $menu || is_wp_error( $menu ) ) {
return;
}

$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
_wp_menu_item_classes_by_context( $menu_items );

return $menu_items;
}


/**
* Sorts a standard array of menu items into a nested structure keyed by the
* id of the parent menu.
*
* @param array $menu_items Menu items to sort.
* @return array An array keyed by the id of the parent menu where each element
* is an array of menu items that belong to that parent.
*/
function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) {
$sorted_menu_items = array();
foreach ( (array) $menu_items as $menu_item ) {
$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
}
unset( $menu_items, $menu_item );

$menu_items_by_parent_id = array();
foreach ( $sorted_menu_items as $menu_item ) {
$menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
}

return $menu_items_by_parent_id;
}

/**
* Add Interactivity API directives to the navigation-submenu and page-list blocks markup using the Tag Processor
* The final HTML of the navigation-submenu and the page-list blocks will look similar to this:
*
* <li
* class="has-child"
* data-wp-context='{ "core": { "navigation": { "isMenuOpen": false, "overlay": false } } }'
* data-wp-effect="effects.core.navigation.initMenu"
* data-wp-on.keydown="actions.core.navigation.handleMenuKeydown"
* data-wp-on.focusout="actions.core.navigation.handleMenuFocusout"
* >
* <button
* class="wp-block-navigation-submenu__toggle"
* data-wp-on.click="actions.core.navigation.openMenu"
* data-wp-bind.aria-expanded="context.core.navigation.isMenuOpen"
* >
* </button>
* <span>Title</span>
* <ul class="wp-block-navigation__submenu-container">
* SUBMENU ITEMS
* </ul>
* </li>
*
* @param string $w Markup of the navigation block.
* @param array $block_attributes Block attributes.
*
* @return string Submenu markup with the directives injected.
*/
function block_core_navigation_add_directives_to_submenu( $w, $block_attributes ) {
while ( $w->next_tag(
array(
'tag_name' => 'LI',
'class_name' => 'has-child',
)
) ) {
// Add directives to the parent `<li>`.
$w->set_attribute( 'data-wp-interactive', true );
$w->set_attribute( 'data-wp-context', '{ "core": { "navigation": { "isMenuOpen": { "click": false, "hover": false }, "overlay": false } } }' );
$w->set_attribute( 'data-wp-effect', 'effects.core.navigation.initMenu' );
$w->set_attribute( 'data-wp-on--focusout', 'actions.core.navigation.handleMenuFocusout' );
$w->set_attribute( 'data-wp-on--keydown', 'actions.core.navigation.handleMenuKeydown' );
if ( ! isset( $block_attributes['openSubmenusOnClick'] ) || false === $block_attributes['openSubmenusOnClick'] ) {
$w->set_attribute( 'data-wp-on--mouseenter', 'actions.core.navigation.openMenuOnHover' );
$w->set_attribute( 'data-wp-on--mouseleave', 'actions.core.navigation.closeMenuOnHover' );
}

// Add directives to the toggle submenu button.
if ( $w->next_tag(
array(
'tag_name' => 'BUTTON',
'class_name' => 'wp-block-navigation-submenu__toggle',
)
) ) {
$w->set_attribute( 'data-wp-on--click', 'actions.core.navigation.toggleMenuOnClick' );
$w->set_attribute( 'data-wp-bind--aria-expanded', 'selectors.core.navigation.isMenuOpen' );
};

// Iterate through subitems if exist.
block_core_navigation_add_directives_to_submenu( $w, $block_attributes );
}
return $w->get_updated_html();
};

/**
* Replaces view script for the Navigation block with version using Interactivity API.
*
* @param array $metadata Block metadata as read in via block.json.
*
* @return array Filtered block type metadata.
*/
function gutenberg_block_core_navigation_update_interactive_view_script( $metadata ) {
Copy link
Contributor

@ntsekouras ntsekouras Jul 4, 2023

Choose a reason for hiding this comment

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

I think we can rename to loose the gutenberg prefix, but I don't think we should remove the above navigation block code. --cc @scruffian @draganescu

Since they are wrapped in IS_GUTENBERG_PLUGIN they are not part of the build in WP core, right? They are handled by Webpack.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure they are removed in PHP: https://github.com/WordPress/wordpress-develop/blob/1815f4c76c9f51665f046d79284552d09849597b/src/wp-includes/blocks/file.php#L8

In JS, yes.

I just threw this PR up as a "just in case". We could easily rename the functions too, but keep them in a wrapper.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure they are removed in PHP

Sorry, that link was to an unbuilt file. I just checked my build folder locally and can still see the IS_GUTENBERG_PLUGIN blocks.

Copy link
Contributor

@ntsekouras ntsekouras Jul 4, 2023

Choose a reason for hiding this comment

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

Yeah, the code is there, but not used in core. My wording above was ambiguous, sorry. Check my comment in the issue.

The blocks code is built automatically in WP core folder structure. There are some features/code that needs to be part of the php blocks' code, but we don't want it to be part of WP core.

So we should remove the code only if the code is not used/needed anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

So we should remove the code only if the code is not used/needed anymore.

Ah, okay. No worries. Just to confirm, the code is needed in the plugin but not in Core. The interactivity API will not be backported to WP 6.3.

So better to rename the functions? It's not quite the same as in #51989, which only renamed to an existing core function that had been around for a while. Here we are potentially introducing two new, currently experimental, functions, albeit guarded by the IS_GUTENBERG_PLUGIN checks.

As long as we are sure this is a good approach, then I can rejig this PR.

Copy link
Contributor

@ntsekouras ntsekouras Jul 4, 2023

Choose a reason for hiding this comment

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

It's not quite the same as in #51989,

That's a different use case. Since the code in blocks.php files are copied for core, we can't have functions that only exist in GB. Additionally, if blocks call some core functions(like the above PR) and we want to override in GB, we have the Webpack mechanism to add the gutenberg prefix to these specific functions.

I'll -cc @Mamaduka too

Copy link
Contributor

Choose a reason for hiding this comment

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

I think renaming the functions in trunk is the more future-proof approach. If all goes well, at some point the logic will make it into core and they'll have to be renamed all the same, so we might as well do it straightaway. In any case, given the block PHP files are auto-generated in core, it's good practice to never use gutenberg prefixes in them. If we need the functions to be namespaced for development reasons, we can use the webpack task to add the prefix at build time.

I also don't love the idea of introducing changes directly to the release branch, as it can cause conflicts if we later need to make changes to the files. This branch will still be used for minor releases after 6.3.0 is out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the feedback folks. I'll update this PR and change the base branch to trunk.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I'll create a new branch. This one has a name that might confuse: remove/is-gutenberg-plugin-from-6-3

if ( 'core/navigation' === $metadata['name'] ) {
$metadata['viewScript'] = array( 'file:./interactivity.min.js' );
}
return $metadata;
}
add_filter( 'block_type_metadata', 'gutenberg_block_core_navigation_update_interactive_view_script', 10, 1 );
}



/**
* Build an array with CSS classes and inline styles defining the colors
* which will be applied to the navigation markup in the front-end.
Expand Down Expand Up @@ -454,28 +312,6 @@ function render_block_core_navigation( $attributes, $content, $block ) {
$attributes['ref'] = $attributes['navigationMenuId'];
}

// If:
// - the gutenberg plugin is active
// - `__unstableLocation` is defined
// - we have menu items at the defined location
// - we don't have a relationship to a `wp_navigation` Post (via `ref`).
// ...then create inner blocks from the classic menu assigned to that location.
if (
defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN &&
array_key_exists( '__unstableLocation', $attributes ) &&
! array_key_exists( 'ref', $attributes ) &&
! empty( block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ) )
) {
$menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] );
if ( empty( $menu_items ) ) {
return '';
}

$menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items );
$parsed_blocks = block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id );
$inner_blocks = new WP_Block_List( $parsed_blocks, $attributes );
}

// Load inner blocks from the navigation post.
if ( array_key_exists( 'ref', $attributes ) ) {
$navigation_post = get_post( $attributes['ref'] );
Expand Down Expand Up @@ -674,15 +510,9 @@ function render_block_core_navigation( $attributes, $content, $block ) {
}
}

// Add directives to the submenu if needed.
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && $has_submenus && $should_load_view_script ) {
$w = new WP_HTML_Tag_Processor( $inner_blocks_html );
$inner_blocks_html = block_core_navigation_add_directives_to_submenu( $w, $attributes );
}

$modal_unique_id = wp_unique_id( 'modal-' );

// Determine whether or not navigation elements should be wrapped in the markup required to make it responsive,
// Determine whether navigation elements should be wrapped in the markup required to make it responsive,
// return early if they don't.
if ( ! $is_responsive_menu ) {
return sprintf(
Expand Down Expand Up @@ -716,46 +546,12 @@ function render_block_core_navigation( $attributes, $content, $block ) {
$toggle_close_button_content = $should_display_icon_label ? $toggle_close_button_icon : __( 'Close' );
$toggle_aria_label_open = $should_display_icon_label ? 'aria-label="' . __( 'Open menu' ) . '"' : ''; // Open button label.
$toggle_aria_label_close = $should_display_icon_label ? 'aria-label="' . __( 'Close menu' ) . '"' : ''; // Close button label.

// Add Interactivity API directives to the markup if needed.
$nav_element_directives = '';
$open_button_directives = '';
$responsive_container_directives = '';
$responsive_dialog_directives = '';
$close_button_directives = '';
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && $should_load_view_script ) {
$nav_element_directives = '
data-wp-interactive
data-wp-context=\'{ "core": { "navigation": { "isMenuOpen": { "click": false, "hover": false }, "overlay": true, "roleAttribute": "" } } }\'
';
$open_button_directives = '
data-wp-on--click="actions.core.navigation.openMenuOnClick"
data-wp-on--keydown="actions.core.navigation.handleMenuKeydown"
';
$responsive_container_directives = '
data-wp-class--has-modal-open="selectors.core.navigation.isMenuOpen"
data-wp-class--is-menu-open="selectors.core.navigation.isMenuOpen"
data-wp-effect="effects.core.navigation.initMenu"
data-wp-on--keydown="actions.core.navigation.handleMenuKeydown"
data-wp-on--focusout="actions.core.navigation.handleMenuFocusout"
tabindex="-1"
';
$responsive_dialog_directives = '
data-wp-bind--aria-modal="selectors.core.navigation.isMenuOpen"
data-wp-bind--role="selectors.core.navigation.roleAttribute"
data-wp-effect="effects.core.navigation.focusFirstElement"
';
$close_button_directives = '
data-wp-on--click="actions.core.navigation.closeMenuOnClick"
';
}

$responsive_container_markup = sprintf(
'<button aria-haspopup="true" %3$s class="%6$s" data-micromodal-trigger="%1$s" %11$s>%9$s</button>
<div class="%5$s" style="%7$s" id="%1$s" %12$s>
<div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close>
<div class="wp-block-navigation__responsive-dialog" aria-label="%8$s" %13$s>
<button %4$s data-micromodal-close class="wp-block-navigation__responsive-container-close" %14$s>%10$s</button>
'<button aria-haspopup="true" %3$s class="%6$s">%9$s</button>
<div class="%5$s" style="%7$s" id="%1$s">
<div class="wp-block-navigation__responsive-close" tabindex="-1">
<div class="wp-block-navigation__responsive-dialog" aria-label="%8$s">
<button %4$s class="wp-block-navigation__responsive-container-close">%10$s</button>
<div class="wp-block-navigation__responsive-container-content" id="%1$s-content">
%2$s
</div>
Expand All @@ -771,18 +567,13 @@ function render_block_core_navigation( $attributes, $content, $block ) {
esc_attr( safecss_filter_attr( $colors['overlay_inline_styles'] ) ),
__( 'Menu' ),
$toggle_button_content,
$toggle_close_button_content,
$open_button_directives,
$responsive_container_directives,
$responsive_dialog_directives,
$close_button_directives
$toggle_close_button_content
);

return sprintf(
'<nav %1$s %3$s>%2$s</nav>',
'<nav %1$s>%2$s</nav>',
$wrapper_attributes,
$responsive_container_markup,
$nav_element_directives
$responsive_container_markup
);
}

Expand Down
Loading