PluginProbe
Gutenberg / 16.2.0
Gutenberg v16.2.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / file.php

file.php in Gutenberg 16.2.0, at build/block-library/blocks/file.php

82 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/file` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * When the `core/file` block is rendering, check if we need to enqueue the `'wp-block-file-view` script.
10 *
11 * @param array $attributes The block attributes.
12 * @param string $content The block content.
13 * @param WP_Block $block The parsed block.
14 *
15 * @return string Returns the block content.
16 */
17 function gutenberg_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
34 // Update object's aria-label attribute if present in block HTML.
35
36 // Match an aria-label attribute from an object tag.
37 $pattern = '@<object.+(?<attribute>aria-label="(?<filename>[^"]+)?")@i';
38 $content = preg_replace_callback(
39 $pattern,
40 static function ( $matches ) {
41 $filename = ! empty( $matches['filename'] ) ? $matches['filename'] : '';
42 $has_filename = ! empty( $filename ) && 'PDF embed' !== $filename;
43 $label = $has_filename ?
44 sprintf(
45 /* translators: %s: filename. */
46 __( 'Embed of %s.' ),
47 $filename
48 )
49 : __( 'PDF embed' );
50
51 return str_replace( $matches['attribute'], sprintf( 'aria-label="%s"', $label ), $matches[0] );
52 },
53 $content
54 );
55
56 // If it uses the Interactivity API, add the directives.
57 if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && $should_load_view_script ) {
58 $processor = new WP_HTML_Tag_Processor( $content );
59 $processor->next_tag();
60 $processor->set_attribute( 'data-wp-interactive', '' );
61 $processor->next_tag( 'object' );
62 $processor->set_attribute( 'data-wp-bind--hidden', '!selectors.core.file.hasPdfPreview' );
63 $processor->set_attribute( 'hidden', true );
64 return $processor->get_updated_html();
65 }
66
67 return $content;
68 }
69
70 /**
71 * Registers the `core/file` block on server.
72 */
73 function gutenberg_register_block_core_file() {
74 register_block_type_from_metadata(
75 __DIR__ . '/file',
76 array(
77 'render_callback' => 'gutenberg_render_block_core_file',
78 )
79 );
80 }
81 add_action( 'init', 'gutenberg_register_block_core_file', 20 );
82