Make WordPress Core


Ignore:
Timestamp:
10/12/2023 01:55:27 PM (16 months ago)
Author:
hellofromTonya
Message:

Editor: Update npm packages ahead of 6.4 RC1.

Updates the npm packages and code for:

References:

Follow-up to [56818], [56816].

Props ellatrix, peterwilsoncc, jsnajdr, afercia, gziolo, isabel_brison, artemiosans, richtabor, bernhard-reiter, flixos90, mikachan, spacedmonkey, hellofromTonya.
See #59583, #59411.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks/image.php

    r56816 r56849  
    1717 */
    1818function render_block_core_image( $attributes, $content, $block ) {
     19    if ( false === stripos( $content, '<img' ) ) {
     20        return '';
     21    }
    1922
    2023    $processor = new WP_HTML_Tag_Processor( $content );
    21     $processor->next_tag( 'img' );
    22 
    23     if ( $processor->get_attribute( 'src' ) === null ) {
     24
     25    if ( ! $processor->next_tag( 'img' ) || null === $processor->get_attribute( 'src' ) ) {
    2426        return '';
    2527    }
     
    3335    }
    3436
    35     $lightbox_enabled  = false;
    3637    $link_destination  = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none';
    3738    $lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block );
    3839
    39     // If the lightbox is enabled and the image is not linked, flag the lightbox to be rendered.
    40     if ( isset( $lightbox_settings ) && 'none' === $link_destination ) {
    41 
    42         if ( isset( $lightbox_settings['enabled'] ) && true === $lightbox_settings['enabled'] ) {
    43             $lightbox_enabled = true;
    44         }
    45     }
    46 
    47     // If at least one block in the page has the lightbox, mark the block type as interactive.
    48     if ( $lightbox_enabled ) {
     40    $view_js_file_handle = 'wp-block-image-view';
     41    $script_handles      = $block->block_type->view_script_handles;
     42
     43    /*
     44     * If the lightbox is enabled and the image is not linked, add the filter
     45     * and the JavaScript view file.
     46     */
     47    if (
     48        isset( $lightbox_settings ) &&
     49        'none' === $link_destination &&
     50        isset( $lightbox_settings['enabled'] ) &&
     51        true === $lightbox_settings['enabled']
     52    ) {
    4953        $block->block_type->supports['interactivity'] = true;
    50     }
    51 
    52     // Determine whether the view script should be enqueued or not.
    53     $view_js_file = 'wp-block-image-view';
    54     if ( ! wp_script_is( $view_js_file ) ) {
    55         $script_handles = $block->block_type->view_script_handles;
    56 
     54
     55        if ( ! in_array( $view_js_file_handle, $script_handles, true ) ) {
     56            $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file_handle ) );
     57        }
     58
     59        /*
     60         * This render needs to happen in a filter with priority 15 to ensure
     61         * that it runs after the duotone filter and that duotone styles are
     62         * applied to the image in the lightbox. We also need to ensure that the
     63         * lightbox works with any plugins that might use filters as well. We
     64         * can consider removing this in the future if the way the blocks are
     65         * rendered changes, or if a new kind of filter is introduced.
     66         */
     67        add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 );
     68    } else {
     69        /*
     70         * Remove the filter and the JavaScript view file if previously added by
     71         * other Image blocks.
     72         */
     73        remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 );
    5774        // If the script is not needed, and it is still in the `view_script_handles`, remove it.
    58         if ( ! $lightbox_enabled && in_array( $view_js_file, $script_handles, true ) ) {
    59             $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) );
    60         }
    61         // If the script is needed, but it was previously removed, add it again.
    62         if ( $lightbox_enabled && ! in_array( $view_js_file, $script_handles, true ) ) {
    63             $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) );
    64         }
    65     }
    66 
    67     if ( $lightbox_enabled ) {
    68         // This render needs to happen in a filter with priority 15 to ensure that it
    69         // runs after the duotone filter and that duotone styles are applied to the image
    70         // in the lightbox. We also need to ensure that the lightbox works with any plugins
    71         // that might use filters as well. We can consider removing this in the future if the
    72         // way the blocks are rendered changes, or if a new kind of filter is introduced.
    73         add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 );
     75        if ( in_array( $view_js_file_handle, $script_handles, true ) ) {
     76            $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file_handle ) );
     77        }
    7478    }
    7579
     
    124128 */
    125129function block_core_image_render_lightbox( $block_content, $block ) {
     130    /*
     131     * If it's not possible that an IMG element exists then return the given
     132     * block content as-is. It may be that there's no actual image in the block
     133     * or it could be that another plugin already modified this HTML.
     134     */
     135    if ( false === stripos( $block_content, '<img' ) ) {
     136        return $block_content;
     137    }
     138
    126139    $processor = new WP_HTML_Tag_Processor( $block_content );
    127140
    128141    $aria_label = __( 'Enlarge image' );
    129142
    130     $processor->next_tag( 'img' );
     143    /*
     144     * If there's definitely no IMG element in the block then return the given
     145     * block content as-is. There's nothing that this code can knowingly modify
     146     * to add the lightbox behavior.
     147     */
     148    if ( ! $processor->next_tag( 'img' ) ) {
     149        return $block_content;
     150    }
     151
    131152    $alt_attribute = $processor->get_attribute( 'alt' );
    132153
     
    311332 *
    312333 * @global WP_Scripts $wp_scripts
    313  *
    314  * @return void
    315334 */
    316335function block_core_image_ensure_interactivity_dependency() {
     
    328347/**
    329348 * Registers the `core/image` block on server.
    330  *
    331  * @return void
    332349 */
    333350function register_block_core_image() {
Note: See TracChangeset for help on using the changeset viewer.