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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref