[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> file.php (source)

   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 render_block_core_file( $attributes, $content ) {
  18      // Update object's aria-label attribute if present in block HTML.
  19      // Match an aria-label attribute from an object tag.
  20      $pattern = '@<object.+(?<attribute>aria-label="(?<filename>[^"]+)?")@i';
  21      $content = preg_replace_callback(
  22          $pattern,
  23          static function ( $matches ) {
  24              $filename     = ! empty( $matches['filename'] ) ? $matches['filename'] : '';
  25              $has_filename = ! empty( $filename ) && 'PDF embed' !== $filename;
  26              $label        = $has_filename ?
  27                  sprintf(
  28                      /* translators: %s: filename. */
  29                      __( 'Embed of %s.' ),
  30                      $filename
  31                  )
  32                  : __( 'PDF embed' );
  33  
  34              return str_replace( $matches['attribute'], sprintf( 'aria-label="%s"', $label ), $matches[0] );
  35          },
  36          $content
  37      );
  38  
  39      // If it's interactive, enqueue the script module and add the directives.
  40      if ( ! empty( $attributes['displayPreview'] ) ) {
  41          $suffix = wp_scripts_get_suffix();
  42          if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
  43              $module_url = gutenberg_url( '/build/interactivity/file.min.js' );
  44          }
  45  
  46          wp_register_script_module(
  47              '@wordpress/block-library/file',
  48              isset( $module_url ) ? $module_url : includes_url( "blocks/file/view{$suffix}.js" ),
  49              array( '@wordpress/interactivity' ),
  50              defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
  51          );
  52          wp_enqueue_script_module( '@wordpress/block-library/file' );
  53  
  54          $processor = new WP_HTML_Tag_Processor( $content );
  55          $processor->next_tag();
  56          $processor->set_attribute( 'data-wp-interactive', 'core/file' );
  57          $processor->next_tag( 'object' );
  58          $processor->set_attribute( 'data-wp-bind--hidden', '!state.hasPdfPreview' );
  59          $processor->set_attribute( 'hidden', true );
  60          return $processor->get_updated_html();
  61      }
  62  
  63      return $content;
  64  }
  65  
  66  /**
  67   * Registers the `core/file` block on server.
  68   */
  69  function register_block_core_file() {
  70      register_block_type_from_metadata(
  71          __DIR__ . '/file',
  72          array(
  73              'render_callback' => 'render_block_core_file',
  74          )
  75      );
  76  }
  77  add_action( 'init', 'register_block_core_file' );


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref