[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/gallery` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Handles backwards compatibility for Gallery Blocks,
  10   * whose images feature a `data-id` attribute.
  11   *
  12   * Now that the Gallery Block contains inner Image Blocks,
  13   * we add a custom `data-id` attribute before rendering the gallery
  14   * so that the Image Block can pick it up in its render_callback.
  15   *
  16   * @param array $parsed_block The block being rendered.
  17   * @return array The migrated block object.
  18   */
  19  function block_core_gallery_data_id_backcompatibility( $parsed_block ) {
  20      if ( 'core/gallery' === $parsed_block['blockName'] ) {
  21          foreach ( $parsed_block['innerBlocks'] as $key => $inner_block ) {
  22              if ( 'core/image' === $inner_block['blockName'] ) {
  23                  if ( ! isset( $parsed_block['innerBlocks'][ $key ]['attrs']['data-id'] ) && isset( $inner_block['attrs']['id'] ) ) {
  24                      $parsed_block['innerBlocks'][ $key ]['attrs']['data-id'] = esc_attr( $inner_block['attrs']['id'] );
  25                  }
  26              }
  27          }
  28      }
  29  
  30      return $parsed_block;
  31  }
  32  
  33  add_filter( 'render_block_data', 'block_core_gallery_data_id_backcompatibility' );
  34  
  35  /**
  36   * Renders the `core/gallery` block on the server.
  37   *
  38   * @param array  $attributes Attributes of the block being rendered.
  39   * @param string $content Content of the block being rendered.
  40   * @return string The content of the block being rendered.
  41   */
  42  function block_core_gallery_render( $attributes, $content ) {
  43      // Adds a style tag for the --wp--style--unstable-gallery-gap var.
  44      // The Gallery block needs to recalculate Image block width based on
  45      // the current gap setting in order to maintain the number of flex columns
  46      // so a css var is added to allow this.
  47  
  48      $gap = $attributes['style']['spacing']['blockGap'] ?? null;
  49      // Skip if gap value contains unsupported characters.
  50      // Regex for CSS value borrowed from `safecss_filter_attr`, and used here
  51      // because we only want to match against the value, not the CSS attribute.
  52      if ( is_array( $gap ) ) {
  53          foreach ( $gap as $key => $value ) {
  54              // Make sure $value is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
  55              $value = is_string( $value ) ? $value : '';
  56              $value = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
  57  
  58              // Get spacing CSS variable from preset value if provided.
  59              if ( is_string( $value ) && str_contains( $value, 'var:preset|spacing|' ) ) {
  60                  $index_to_splice = strrpos( $value, '|' ) + 1;
  61                  $slug            = _wp_to_kebab_case( substr( $value, $index_to_splice ) );
  62                  $value           = "var(--wp--preset--spacing--$slug)";
  63              }
  64  
  65              $gap[ $key ] = $value;
  66          }
  67      } else {
  68          // Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.
  69          $gap = is_string( $gap ) ? $gap : '';
  70          $gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap;
  71  
  72          // Get spacing CSS variable from preset value if provided.
  73          if ( is_string( $gap ) && str_contains( $gap, 'var:preset|spacing|' ) ) {
  74              $index_to_splice = strrpos( $gap, '|' ) + 1;
  75              $slug            = _wp_to_kebab_case( substr( $gap, $index_to_splice ) );
  76              $gap             = "var(--wp--preset--spacing--$slug)";
  77          }
  78      }
  79  
  80      $unique_gallery_classname = wp_unique_id( 'wp-block-gallery-' );
  81      $processed_content        = new WP_HTML_Tag_Processor( $content );
  82      $processed_content->next_tag();
  83      $processed_content->add_class( $unique_gallery_classname );
  84  
  85      // --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default
  86      // gap on the gallery.
  87      $fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )';
  88      $gap_value    = $gap ? $gap : $fallback_gap;
  89      $gap_column   = $gap_value;
  90  
  91      if ( is_array( $gap_value ) ) {
  92          $gap_row    = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap;
  93          $gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap;
  94          $gap_value  = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;
  95      }
  96  
  97      // The unstable gallery gap calculation requires a real value (such as `0px`) and not `0`.
  98      if ( '0' === $gap_column ) {
  99          $gap_column = '0px';
 100      }
 101  
 102      // Set the CSS variable to the column value, and the `gap` property to the combined gap value.
 103      $gallery_styles = array(
 104          array(
 105              'selector'     => ".wp-block-gallery.{$unique_gallery_classname}",
 106              'declarations' => array(
 107                  '--wp--style--unstable-gallery-gap' => $gap_column,
 108                  'gap'                               => $gap_value,
 109              ),
 110          ),
 111      );
 112  
 113      wp_style_engine_get_stylesheet_from_css_rules(
 114          $gallery_styles,
 115          array(
 116              'context' => 'block-supports',
 117          )
 118      );
 119  
 120      // The WP_HTML_Tag_Processor class calls get_updated_html() internally
 121      // when the instance is treated as a string, but here we explicitly
 122      // convert it to a string.
 123      $updated_content = $processed_content->get_updated_html();
 124  
 125      /*
 126       * Randomize the order of image blocks. Ideally we should shuffle
 127       * the `$parsed_block['innerBlocks']` via the `render_block_data` hook.
 128       * However, this hook doesn't apply inner block updates when blocks are
 129       * nested.
 130       * @todo: In the future, if this hook supports updating innerBlocks in
 131       * nested blocks, it should be refactored.
 132       *
 133       * @see: https://github.com/WordPress/gutenberg/pull/58733
 134       */
 135      if ( empty( $attributes['randomOrder'] ) ) {
 136          return $updated_content;
 137      }
 138  
 139      // This pattern matches figure elements with the `wp-block-image` class to
 140      // avoid the gallery's wrapping `figure` element and extract images only.
 141      $pattern = '/<figure[^>]*\bwp-block-image\b[^>]*>.*?<\/figure>/';
 142  
 143      // Find all Image blocks.
 144      preg_match_all( $pattern, $updated_content, $matches );
 145      if ( ! $matches ) {
 146          return $updated_content;
 147      }
 148      $image_blocks = $matches[0];
 149  
 150      // Randomize the order of Image blocks.
 151      shuffle( $image_blocks );
 152      $i       = 0;
 153      $content = preg_replace_callback(
 154          $pattern,
 155          static function () use ( $image_blocks, &$i ) {
 156              $new_image_block = $image_blocks[ $i ];
 157              ++$i;
 158              return $new_image_block;
 159          },
 160          $updated_content
 161      );
 162  
 163      return $content;
 164  }
 165  /**
 166   * Registers the `core/gallery` block on server.
 167   */
 168  function register_block_core_gallery() {
 169      register_block_type_from_metadata(
 170          __DIR__ . '/gallery',
 171          array(
 172              'render_callback' => 'block_core_gallery_render',
 173          )
 174      );
 175  }
 176  
 177  add_action( 'init', 'register_block_core_gallery' );


Generated : Fri Mar 29 08:20:02 2024 Cross-referenced by PHPXref