wp/wp-includes/blocks/file.php
author ymh <ymh.work@gmail.com>
Fri, 05 Sep 2025 18:40:08 +0200
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
permissions -rw-r--r--
Add CLAUDE.md documentation and sync WordPress core files - Add comprehensive CLAUDE.md documentation file with project architecture, development setup, database operations, WordPress CLI usage, file sync procedures, and Mercurial commands - Update WordPress core files from wordpress/ to wp/ directory - Sync latest WordPress admin interface, includes, and core functionality - Update plugins: portfolio plugin with latest BWS framework and fancybox integration - Maintain custom configuration and theme files 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Server-side rendering of the `core/file` block.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
/**
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
     9
 * When the `core/file` block is rendering, check if we need to enqueue the `wp-block-file-view` script.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    10
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    11
 * @since 5.8.0
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 *
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    13
 * @param array    $attributes The block attributes.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    14
 * @param string   $content    The block content.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    15
 * @param WP_Block $block      The parsed block.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * @return string Returns the block content.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
function render_block_core_file( $attributes, $content ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    20
	// If it's interactive, enqueue the script module and add the directives.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    21
	if ( ! empty( $attributes['displayPreview'] ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    22
		$suffix = wp_scripts_get_suffix();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    23
		if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    24
			$module_url = gutenberg_url( '/build/interactivity/file.min.js' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    25
		}
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    26
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    27
		wp_register_script_module(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    28
			'@wordpress/block-library/file',
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    29
			isset( $module_url ) ? $module_url : includes_url( "blocks/file/view{$suffix}.js" ),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    30
			array( '@wordpress/interactivity' ),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    31
			defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    32
		);
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    33
		wp_enqueue_script_module( '@wordpress/block-library/file' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    34
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    35
		$processor = new WP_HTML_Tag_Processor( $content );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    36
		$processor->next_tag();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    37
		$processor->set_attribute( 'data-wp-interactive', 'core/file' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    38
		$processor->next_tag( 'object' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    39
		$processor->set_attribute( 'data-wp-bind--hidden', '!state.hasPdfPreview' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    40
		$processor->set_attribute( 'hidden', true );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    41
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    42
		$filename     = $processor->get_attribute( 'aria-label' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    43
		$has_filename = ! empty( $filename ) && 'PDF embed' !== $filename;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    44
		$label        = $has_filename ? sprintf(
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    45
			/* translators: %s: filename. */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    46
			__( 'Embed of %s.' ),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    47
			$filename
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    48
		) : __( 'PDF embed' );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    49
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    50
		// Update object's aria-label attribute if present in block HTML.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    51
		// Match an aria-label attribute from an object tag.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    52
		$processor->set_attribute( 'aria-label', $label );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    53
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    54
		return $processor->get_updated_html();
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
	return $content;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
 * Registers the `core/file` block on server.
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    62
 *
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
    63
 * @since 5.8.0
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
function register_block_core_file() {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
	register_block_type_from_metadata(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		__DIR__ . '/file',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
		array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
			'render_callback' => 'render_block_core_file',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
add_action( 'init', 'register_block_core_file' );