| 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 |
* @since 5.8.0 |
| 12 |
* |
| 13 |
* @param array $attributes The block attributes. |
| 14 |
* @param string $content The block content. |
| 15 |
* |
| 16 |
* @return string Returns the block content. |
| 17 |
*/ |
| 18 |
function gutenberg_render_block_core_file( $attributes, $content ) { |
| 19 |
if ( empty( $attributes['displayPreview'] ) ) { |
| 20 |
return $content; |
| 21 |
} |
| 22 |
|
| 23 |
// If it's interactive, enqueue the script module and add the directives. |
| 24 |
wp_enqueue_script_module( '@wordpress/block-library/file/view' ); |
| 25 |
|
| 26 |
$processor = new WP_HTML_Tag_Processor( $content ); |
| 27 |
if ( $processor->next_tag() ) { |
| 28 |
$processor->set_attribute( 'data-wp-interactive', 'core/file' ); |
| 29 |
} |
| 30 |
|
| 31 |
// If there are no OBJECT elements, something might have already modified the block. |
| 32 |
if ( ! $processor->next_tag( 'OBJECT' ) ) { |
| 33 |
return $content; |
| 34 |
} |
| 35 |
|
| 36 |
$processor->set_attribute( 'data-wp-bind--hidden', '!state.hasPdfPreview' ); |
| 37 |
$processor->set_attribute( 'hidden', true ); |
| 38 |
|
| 39 |
$filename = $processor->get_attribute( 'aria-label' ); |
| 40 |
$has_filename = is_string( $filename ) && ! empty( $filename ) && 'PDF embed' !== $filename; |
| 41 |
$label = $has_filename ? sprintf( |
| 42 |
/* translators: %s: filename. */ |
| 43 |
__( 'Embed of %s.' ), |
| 44 |
$filename |
| 45 |
) : __( 'PDF embed' ); |
| 46 |
|
| 47 |
// Update object's aria-label attribute if present in block HTML. |
| 48 |
// Match an aria-label attribute from an object tag. |
| 49 |
$processor->set_attribute( 'aria-label', $label ); |
| 50 |
|
| 51 |
return $processor->get_updated_html(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Registers the `core/file` block on server. |
| 56 |
* |
| 57 |
* @since 5.8.0 |
| 58 |
*/ |
| 59 |
function gutenberg_register_block_core_file() { |
| 60 |
register_block_type_from_metadata( |
| 61 |
__DIR__ . '/file', |
| 62 |
array( |
| 63 |
'render_callback' => 'gutenberg_render_block_core_file', |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
add_action( 'init', 'gutenberg_register_block_core_file', 20 ); |
| 68 |
|