Changeset 57377 for trunk/src/wp-includes/blocks/file.php
- Timestamp:
- 01/29/2024 09:04:18 PM (17 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/file.php
r57034 r57377 15 15 * @return string Returns the block content. 16 16 */ 17 function render_block_core_file( $attributes, $content, $block ) { 18 $should_load_view_script = ! empty( $attributes['displayPreview'] ); 19 $view_js_file = 'wp-block-file-view'; 20 // If the script already exists, there is no point in removing it from viewScript. 21 if ( ! wp_script_is( $view_js_file ) ) { 22 $script_handles = $block->block_type->view_script_handles; 23 24 // If the script is not needed, and it is still in the `view_script_handles`, remove it. 25 if ( ! $should_load_view_script && in_array( $view_js_file, $script_handles, true ) ) { 26 $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) ); 27 } 28 // If the script is needed, but it was previously removed, add it again. 29 if ( $should_load_view_script && ! in_array( $view_js_file, $script_handles, true ) ) { 30 $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) ); 31 } 32 } 33 17 function render_block_core_file( $attributes, $content ) { 34 18 // Update object's aria-label attribute if present in block HTML. 35 36 19 // Match an aria-label attribute from an object tag. 37 20 $pattern = '@<object.+(?<attribute>aria-label="(?<filename>[^"]+)?")@i'; … … 54 37 ); 55 38 56 // If it uses the Interactivity API, add the directives. 57 if ( $should_load_view_script ) { 39 // If it's interactive, enqueue the script module and add the directives. 40 if ( ! empty( $attributes['displayPreview'] ) ) { 41 wp_enqueue_script_module( '@wordpress/block-library/file' ); 42 58 43 $processor = new WP_HTML_Tag_Processor( $content ); 59 44 $processor->next_tag(); 60 $processor->set_attribute( 'data-wp-interactive', ' ' );45 $processor->set_attribute( 'data-wp-interactive', '{"namespace":"core/file"}' ); 61 46 $processor->next_tag( 'object' ); 62 $processor->set_attribute( 'data-wp-bind--hidden', '!s electors.core.file.hasPdfPreview' );47 $processor->set_attribute( 'data-wp-bind--hidden', '!state.hasPdfPreview' ); 63 48 $processor->set_attribute( 'hidden', true ); 64 49 return $processor->get_updated_html(); … … 67 52 return $content; 68 53 } 69 70 /**71 * Ensure that the view script has the `wp-interactivity` dependency.72 *73 * @since 6.4.074 *75 * @global WP_Scripts $wp_scripts76 */77 function block_core_file_ensure_interactivity_dependency() {78 global $wp_scripts;79 if (80 isset( $wp_scripts->registered['wp-block-file-view'] ) &&81 ! in_array( 'wp-interactivity', $wp_scripts->registered['wp-block-file-view']->deps, true )82 ) {83 $wp_scripts->registered['wp-block-file-view']->deps[] = 'wp-interactivity';84 }85 }86 87 add_action( 'wp_print_scripts', 'block_core_file_ensure_interactivity_dependency' );88 54 89 55 /** … … 97 63 ) 98 64 ); 65 66 wp_register_script_module( 67 '@wordpress/block-library/file', 68 defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/interactivity/file.min.js' ) : includes_url( 'blocks/file/view.min.js' ), 69 array( '@wordpress/interactivity' ), 70 defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ) 71 ); 99 72 } 100 73 add_action( 'init', 'register_block_core_file' );
Note: See TracChangeset
for help on using the changeset viewer.