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

Cherry-picked commits for WordPress 6.4 RC1 #55298

Merged
merged 10 commits into from
Oct 12, 2023
Merged
2 changes: 1 addition & 1 deletion docs/contributors/code/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ do so by opting-in to `@wordpress/private-apis`:
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/block-editor' // Name of the package calling __dangerousOptInToUnstableAPIsOnlyForCoreModules,
// (not the name of the package whose APIs you want to access)
);
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ export function useBlockSettings( name, parentLayout ) {
isBackgroundEnabled,
isLinkEnabled,
isTextEnabled,
isHeadingEnabled,
isButtonEnabled,
] );

return useSettingsForBlockElement( rawSettings, name );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/lock-unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/block-editor'
);
10 changes: 10 additions & 0 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ export default function Image( {
const lightboxChecked =
!! lightbox?.enabled || ( ! lightbox && !! lightboxSetting?.enabled );

const lightboxToggleDisabled = linkDestination !== 'none';

const dimensionsControl = (
<DimensionsTool
value={ { width, height, scale, aspectRatio } }
Expand Down Expand Up @@ -555,6 +557,14 @@ export default function Image( {
lightbox: { enabled: newValue },
} );
} }
disabled={ lightboxToggleDisabled }
help={
lightboxToggleDisabled
? __(
'“Expand on click” scales the image up, and can’t be combined with a link.'
)
: ''
}
/>
</ToolsPanelItem>
) }
Expand Down
91 changes: 54 additions & 37 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
* @return string The block content with the data-id attribute added.
*/
function render_block_core_image( $attributes, $content, $block ) {
if ( false === stripos( $content, '<img' ) ) {
return '';
}

$processor = new WP_HTML_Tag_Processor( $content );
$processor->next_tag( 'img' );

if ( $processor->get_attribute( 'src' ) === null ) {
if ( ! $processor->next_tag( 'img' ) || null === $processor->get_attribute( 'src' ) ) {
return '';
}

Expand All @@ -32,45 +34,47 @@ function render_block_core_image( $attributes, $content, $block ) {
$processor->set_attribute( 'data-id', $attributes['data-id'] );
}

$lightbox_enabled = false;
$link_destination = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none';
$lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block );

// If the lightbox is enabled and the image is not linked, flag the lightbox to be rendered.
if ( isset( $lightbox_settings ) && 'none' === $link_destination ) {

if ( isset( $lightbox_settings['enabled'] ) && true === $lightbox_settings['enabled'] ) {
$lightbox_enabled = true;
}
}
$view_js_file_handle = 'wp-block-image-view';
$script_handles = $block->block_type->view_script_handles;

// If at least one block in the page has the lightbox, mark the block type as interactive.
if ( $lightbox_enabled ) {
/*
* If the lightbox is enabled and the image is not linked, add the filter
* and the JavaScript view file.
*/
if (
isset( $lightbox_settings ) &&
'none' === $link_destination &&
isset( $lightbox_settings['enabled'] ) &&
true === $lightbox_settings['enabled']
) {
$block->block_type->supports['interactivity'] = true;
}

// Determine whether the view script should be enqueued or not.
$view_js_file = 'wp-block-image-view';
if ( ! wp_script_is( $view_js_file ) ) {
$script_handles = $block->block_type->view_script_handles;

// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if ( ! $lightbox_enabled && in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) );
}
// If the script is needed, but it was previously removed, add it again.
if ( $lightbox_enabled && ! in_array( $view_js_file, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) );
if ( ! in_array( $view_js_file_handle, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file_handle ) );
}
}

if ( $lightbox_enabled ) {
// This render needs to happen in a filter with priority 15 to ensure that it
// runs after the duotone filter and that duotone styles are applied to the image
// in the lightbox. We also need to ensure that the lightbox works with any plugins
// that might use filters as well. We can consider removing this in the future if the
// way the blocks are rendered changes, or if a new kind of filter is introduced.
/*
* This render needs to happen in a filter with priority 15 to ensure
* that it runs after the duotone filter and that duotone styles are
* applied to the image in the lightbox. We also need to ensure that the
* lightbox works with any plugins that might use filters as well. We
* can consider removing this in the future if the way the blocks are
* rendered changes, or if a new kind of filter is introduced.
*/
add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 );
} else {
/*
* Remove the filter and the JavaScript view file if previously added by
* other Image blocks.
*/
remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 );
// If the script is not needed, and it is still in the `view_script_handles`, remove it.
if ( in_array( $view_js_file_handle, $script_handles, true ) ) {
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file_handle ) );
}
}

return $processor->get_updated_html();
Expand Down Expand Up @@ -123,11 +127,28 @@ function block_core_image_get_lightbox_settings( $block ) {
* @return string Filtered block content.
*/
function block_core_image_render_lightbox( $block_content, $block ) {
/*
* If it's not possible that an IMG element exists then return the given
* block content as-is. It may be that there's no actual image in the block
* or it could be that another plugin already modified this HTML.
*/
if ( false === stripos( $block_content, '<img' ) ) {
return $block_content;
}

$processor = new WP_HTML_Tag_Processor( $block_content );

$aria_label = __( 'Enlarge image' );

$processor->next_tag( 'img' );
/*
* If there's definitely no IMG element in the block then return the given
* block content as-is. There's nothing that this code can knowingly modify
* to add the lightbox behavior.
*/
if ( ! $processor->next_tag( 'img' ) ) {
return $block_content;
}

$alt_attribute = $processor->get_attribute( 'alt' );

// An empty alt attribute `alt=""` is valid for decorative images.
Expand Down Expand Up @@ -310,8 +331,6 @@ function block_core_image_render_lightbox( $block_content, $block ) {
* @since 6.4.0
*
* @global WP_Scripts $wp_scripts
*
* @return void
*/
function block_core_image_ensure_interactivity_dependency() {
global $wp_scripts;
Expand All @@ -327,8 +346,6 @@ function block_core_image_ensure_interactivity_dependency() {

/**
* Registers the `core/image` block on server.
*
* @return void
*/
function register_block_core_image() {
register_block_type_from_metadata(
Expand Down
43 changes: 20 additions & 23 deletions packages/block-library/src/list-item/hooks/use-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ export default function useMerge( clientId, onMerge ) {
}

return ( forward ) => {
function mergeWithNested( clientIdA, clientIdB ) {
registry.batch( () => {
// When merging a sub list item with a higher next list item, we
// also need to move any nested list items. Check if there's a
// listed list, and append its nested list items to the current
// list.
const [ nestedListClientId ] = getBlockOrder( clientIdB );
if ( nestedListClientId ) {
moveBlocksToPosition(
getBlockOrder( nestedListClientId ),
nestedListClientId,
getBlockRootClientId( clientIdA )
);
}
mergeBlocks( clientIdA, clientIdB );
} );
}

if ( forward ) {
const nextBlockClientId = getNextId( clientId );

Expand All @@ -87,14 +105,7 @@ export default function useMerge( clientId, onMerge ) {
if ( getParentListItemId( nextBlockClientId ) ) {
outdentListItem( nextBlockClientId );
} else {
registry.batch( () => {
moveBlocksToPosition(
getBlockOrder( nextBlockClientId ),
nextBlockClientId,
getPreviousBlockClientId( nextBlockClientId )
);
mergeBlocks( clientId, nextBlockClientId );
} );
mergeWithNested( clientId, nextBlockClientId );
}
} else {
// Merging is only done from the top level. For lowel levels, the
Expand All @@ -104,21 +115,7 @@ export default function useMerge( clientId, onMerge ) {
outdentListItem( clientId );
} else if ( previousBlockClientId ) {
const trailingId = getTrailingId( previousBlockClientId );
registry.batch( () => {
// When merging a list item with a previous trailing list
// item, we also need to move any nested list items. First,
// check if there's a listed list. If there's a nested list,
// append its nested list items to the trailing list.
const [ nestedListClientId ] = getBlockOrder( clientId );
if ( nestedListClientId ) {
moveBlocksToPosition(
getBlockOrder( nestedListClientId ),
nestedListClientId,
getBlockRootClientId( trailingId )
);
}
mergeBlocks( trailingId, clientId );
} );
mergeWithNested( trailingId, clientId );
} else {
onMerge( forward );
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/lock-unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/block-library'
);
3 changes: 0 additions & 3 deletions packages/block-library/src/pattern/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

/**
* Registers the `core/pattern` block on the server.
*
* @return void
*/
function register_block_core_pattern() {
register_block_type_from_metadata(
Expand Down Expand Up @@ -46,7 +44,6 @@ function render_block_core_pattern( $attributes ) {
// Backward compatibility for handling Block Hooks and injecting the theme attribute in the Gutenberg plugin.
// This can be removed when the minimum supported WordPress is >= 6.4.
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && ! function_exists( 'traverse_and_serialize_blocks' ) ) {
$content = _inject_theme_attribute_in_block_template_content( $content );
$blocks = parse_blocks( $content );
$content = gutenberg_serialize_blocks( $blocks );
}
Expand Down
4 changes: 0 additions & 4 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ function classnames_for_block_core_search( $attributes ) {
* @param array $wrapper_styles Current collection of wrapper styles.
* @param array $button_styles Current collection of button styles.
* @param array $input_styles Current collection of input styles.
*
* @return void
*/
function apply_block_core_search_border_style( $attributes, $property, $side, &$wrapper_styles, &$button_styles, &$input_styles ) {
$is_button_inside = isset( $attributes['buttonPosition'] ) && 'button-inside' === $attributes['buttonPosition'];
Expand Down Expand Up @@ -327,8 +325,6 @@ function apply_block_core_search_border_style( $attributes, $property, $side, &$
* @param array $wrapper_styles Current collection of wrapper styles.
* @param array $button_styles Current collection of button styles.
* @param array $input_styles Current collection of input styles.
*
* @return void
*/
function apply_block_core_search_border_styles( $attributes, $property, &$wrapper_styles, &$button_styles, &$input_styles ) {
apply_block_core_search_border_style( $attributes, $property, null, $wrapper_styles, $button_styles, $input_styles );
Expand Down
11 changes: 4 additions & 7 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ function render_block_core_template_part( $attributes ) {
$template_part_id = null;
$content = null;
$area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
$theme = isset( $attributes['theme'] ) ? $attributes['theme'] : get_stylesheet();

if (
isset( $attributes['slug'] ) &&
isset( $attributes['theme'] ) &&
get_stylesheet() === $attributes['theme']
) {
$template_part_id = $attributes['theme'] . '//' . $attributes['slug'];
if ( isset( $attributes['slug'] ) && get_stylesheet() === $theme ) {
$template_part_id = $theme . '//' . $attributes['slug'];
$template_part_query = new WP_Query(
array(
'post_type' => 'wp_template_part',
Expand All @@ -34,7 +31,7 @@ function render_block_core_template_part( $attributes ) {
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $attributes['theme'],
'terms' => $theme,
),
),
'posts_per_page' => 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/lock-unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/blocks'
);
2 changes: 1 addition & 1 deletion packages/commands/src/lock-unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/commands'
);
2 changes: 1 addition & 1 deletion packages/components/src/private-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Theme from './theme';

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/components'
);

Expand Down
2 changes: 1 addition & 1 deletion packages/core-commands/src/lock-unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/core-commands'
);
2 changes: 1 addition & 1 deletion packages/core-data/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/core-data'
);
2 changes: 1 addition & 1 deletion packages/customize-widgets/src/lock-unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/customize-widgets'
);
2 changes: 1 addition & 1 deletion packages/data/src/lock-unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/data'
);
2 changes: 1 addition & 1 deletion packages/edit-post/src/lock-unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/pri

export const { lock, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'I know using unstable features means my theme or plugin will inevitably break in the next version of WordPress.',
'@wordpress/edit-post'
);
Loading
Loading