[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/video` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/video` block on the server to supply the width and height attributes from the attachment metadata.
  10   *
  11   * @since 6.9.0
  12   *
  13   * @phpstan-param  array{ "id"?: positive-int } $attributes
  14   *
  15   * @param array   $attributes The block attributes.
  16   * @param string  $content    The block content.
  17   * @return string The block content with the dimensions added.
  18   */
  19  function render_block_core_video( array $attributes, string $content ): string {
  20      // If the content lacks any video tag (either <video> or <VIDEO>), abort. Note that str_contains() is case-sensitive.
  21      if ( stripos( $content, '<video' ) === false ) {
  22          return $content;
  23      }
  24  
  25      /*
  26       * Inspect the video to determine whether autoplay is enabled and, when it
  27       * is, set the interactivity directives that stop playback for visitors who
  28       * prefer reduced motion.
  29       *
  30       * The directives are added here rather than in the saved markup so existing
  31       * content does not need a deprecation.
  32       *
  33       * `autoplay` is read from the markup rather than from `$attributes`, because
  34       * it is sourced from the `video` element and so is never stored in the block
  35       * comment delimiter the server parses.
  36       */
  37      $autoplay_processor = new WP_HTML_Tag_Processor( $content );
  38      if (
  39          $autoplay_processor->next_tag( array( 'tag_name' => 'VIDEO' ) ) &&
  40          null !== $autoplay_processor->get_attribute( 'autoplay' )
  41      ) {
  42          wp_enqueue_script_module( '@wordpress/block-library/video/view' );
  43  
  44          $autoplay_processor->set_attribute( 'data-wp-interactive', 'core/video' );
  45          $autoplay_processor->set_attribute( 'data-wp-init', 'callbacks.stopAutoplay' );
  46  
  47          $content = $autoplay_processor->get_updated_html();
  48      }
  49  
  50      // If the 'id' attribute is not populated for a video attachment, abort.
  51      if (
  52          ! isset( $attributes['id'] ) ||
  53          ! is_int( $attributes['id'] ) ||
  54          $attributes['id'] <= 0
  55      ) {
  56          return $content;
  57      }
  58  
  59      // If the 'id' attribute wasn't for an attachment, abort.
  60      if ( get_post_type( $attributes['id'] ) !== 'attachment' ) {
  61          return $content;
  62      }
  63  
  64      // Get the width and height metadata for the video, and abort if absent or invalid.
  65      $metadata = wp_get_attachment_metadata( $attributes['id'] );
  66      if (
  67          ! isset( $metadata['width'], $metadata['height'] ) ||
  68          ! ( is_int( $metadata['width'] ) && is_int( $metadata['height'] ) ) ||
  69          ! ( $metadata['width'] > 0 && $metadata['height'] > 0 )
  70      ) {
  71          return $content;
  72      }
  73  
  74      // Locate the VIDEO tag to add the dimensions.
  75      $p = new WP_HTML_Tag_Processor( $content );
  76      if ( ! $p->next_tag( array( 'tag_name' => 'VIDEO' ) ) ) {
  77          return $content;
  78      }
  79  
  80      $p->set_attribute( 'width', (string) $metadata['width'] );
  81      $p->set_attribute( 'height', (string) $metadata['height'] );
  82  
  83      /*
  84       * The aspect-ratio style is needed due to an issue with the CSS spec: <https://github.com/w3c/csswg-drafts/issues/7524>.
  85       * Note that a style rule using attr() like the following cannot currently be used:
  86       *
  87       *     .wp-block-video video[width][height] {
  88       *         aspect-ratio: attr(width type(<number>)) / attr(height type(<number>));
  89       *     }
  90       *
  91       * This is because this attr() is yet only implemented in Chromium: <https://caniuse.com/css3-attr>.
  92       */
  93      $style = $p->get_attribute( 'style' );
  94      if ( ! is_string( $style ) ) {
  95          $style = '';
  96      }
  97      $aspect_ratio_style = sprintf( 'aspect-ratio: %d / %d;', $metadata['width'], $metadata['height'] );
  98      $p->set_attribute( 'style', $aspect_ratio_style . $style );
  99  
 100      return $p->get_updated_html();
 101  }
 102  
 103  /**
 104   * Registers the `core/video` block on server.
 105   *
 106   * @since 6.9.0
 107   */
 108  function register_block_core_video(): void {
 109      register_block_type_from_metadata(
 110          __DIR__ . '/video',
 111          array(
 112              'render_callback' => 'render_block_core_video',
 113          )
 114      );
 115  }
 116  add_action( 'init', 'register_block_core_video' );


Generated : Thu Nov 5 08:20:37 2026 Cross-referenced by PHPXref