[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> template-part.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/template-part` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/template-part` block on the server.
  10   *
  11   * @since 5.9.0
  12   *
  13   * @global WP_Embed $wp_embed WordPress Embed object.
  14   *
  15   * @param array $attributes The block attributes.
  16   *
  17   * @return string The render.
  18   */
  19  function render_block_core_template_part( $attributes ) {
  20      static $seen_ids = array();
  21  
  22      $template_part_id = null;
  23      $content          = null;
  24      $area             = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
  25      $theme            = isset( $attributes['theme'] ) ? $attributes['theme'] : get_stylesheet();
  26  
  27      if ( isset( $attributes['slug'] ) && get_stylesheet() === $theme ) {
  28          $template_part_id    = $theme . '//' . $attributes['slug'];
  29          $template_part_query = new WP_Query(
  30              array(
  31                  'post_type'           => 'wp_template_part',
  32                  'post_status'         => 'publish',
  33                  'post_name__in'       => array( $attributes['slug'] ),
  34                  'tax_query'           => array(
  35                      array(
  36                          'taxonomy' => 'wp_theme',
  37                          'field'    => 'name',
  38                          'terms'    => $theme,
  39                      ),
  40                  ),
  41                  'posts_per_page'      => 1,
  42                  'no_found_rows'       => true,
  43                  'lazy_load_term_meta' => false, // Do not lazy load term meta, as template parts only have one term.
  44              )
  45          );
  46          $template_part_post  = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
  47          if ( $template_part_post ) {
  48              // A published post might already exist if this template part was customized elsewhere
  49              // or if it's part of a customized template.
  50              $block_template = _build_block_template_result_from_post( $template_part_post );
  51              $content        = $block_template->content;
  52              if ( isset( $block_template->area ) ) {
  53                  $area = $block_template->area;
  54              }
  55              /**
  56               * Fires when a block template part is loaded from a template post stored in the database.
  57               *
  58               * @since 5.9.0
  59               *
  60               * @param string  $template_part_id   The requested template part namespaced to the theme.
  61               * @param array   $attributes         The block attributes.
  62               * @param WP_Post $template_part_post The template part post object.
  63               * @param string  $content            The template part content.
  64               */
  65              do_action( 'render_block_core_template_part_post', $template_part_id, $attributes, $template_part_post, $content );
  66          } else {
  67              $template_part_file_path = '';
  68              // Else, if the template part was provided by the active theme,
  69              // render the corresponding file content.
  70              if ( 0 === validate_file( $attributes['slug'] ) ) {
  71                  $block_template = get_block_file_template( $template_part_id, 'wp_template_part' );
  72  
  73                  $content = $block_template->content;
  74                  if ( isset( $block_template->area ) ) {
  75                      $area = $block_template->area;
  76                  }
  77  
  78                  // Needed for the `render_block_core_template_part_file` and `render_block_core_template_part_none` actions below.
  79                  $block_template_file = _get_block_template_file( 'wp_template_part', $attributes['slug'] );
  80                  if ( $block_template_file ) {
  81                      $template_part_file_path = $block_template_file['path'];
  82                  }
  83              }
  84  
  85              if ( '' !== $content && null !== $content ) {
  86                  /**
  87                   * Fires when a block template part is loaded from a template part in the theme.
  88                   *
  89                   * @since 5.9.0
  90                   *
  91                   * @param string $template_part_id        The requested template part namespaced to the theme.
  92                   * @param array  $attributes              The block attributes.
  93                   * @param string $template_part_file_path Absolute path to the template path.
  94                   * @param string $content                 The template part content.
  95                   */
  96                  do_action( 'render_block_core_template_part_file', $template_part_id, $attributes, $template_part_file_path, $content );
  97              } else {
  98                  /**
  99                   * Fires when a requested block template part does not exist in the database nor in the theme.
 100                   *
 101                   * @since 5.9.0
 102                   *
 103                   * @param string $template_part_id        The requested template part namespaced to the theme.
 104                   * @param array  $attributes              The block attributes.
 105                   * @param string $template_part_file_path Absolute path to the not found template path.
 106                   */
 107                  do_action( 'render_block_core_template_part_none', $template_part_id, $attributes, $template_part_file_path );
 108              }
 109          }
 110      }
 111  
 112      // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
 113      // is set in `wp_debug_mode()`.
 114      $is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;
 115  
 116      if ( is_null( $content ) ) {
 117          if ( $is_debug && isset( $attributes['slug'] ) ) {
 118              return sprintf(
 119                  /* translators: %s: Template part slug. */
 120                  __( 'Template part has been deleted or is unavailable: %s' ),
 121                  $attributes['slug']
 122              );
 123          }
 124  
 125          return '';
 126      }
 127  
 128      if ( isset( $seen_ids[ $template_part_id ] ) ) {
 129          return $is_debug ?
 130              // translators: Visible only in the front end, this warning takes the place of a faulty block.
 131              __( '[block rendering halted]' ) :
 132              '';
 133      }
 134  
 135      // Look up area definition.
 136      $area_definition = null;
 137      $defined_areas   = get_allowed_block_template_part_areas();
 138      foreach ( $defined_areas as $defined_area ) {
 139          if ( $defined_area['area'] === $area ) {
 140              $area_definition = $defined_area;
 141              break;
 142          }
 143      }
 144  
 145      // If $area is not allowed, set it back to the uncategorized default.
 146      if ( ! $area_definition ) {
 147          $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
 148      }
 149  
 150      // Run through the actions that are typically taken on the_content.
 151      $content                       = shortcode_unautop( $content );
 152      $content                       = do_shortcode( $content );
 153      $seen_ids[ $template_part_id ] = true;
 154      $content                       = do_blocks( $content );
 155      unset( $seen_ids[ $template_part_id ] );
 156      $content = wptexturize( $content );
 157      $content = convert_smilies( $content );
 158      $content = wp_filter_content_tags( $content, "template_part_{$area}" );
 159  
 160      // Handle embeds for block template parts.
 161      global $wp_embed;
 162      $content = $wp_embed->autoembed( $content );
 163  
 164      if ( empty( $attributes['tagName'] ) || tag_escape( $attributes['tagName'] ) !== $attributes['tagName'] ) {
 165          $area_tag = 'div';
 166          if ( $area_definition && isset( $area_definition['area_tag'] ) ) {
 167              $area_tag = $area_definition['area_tag'];
 168          }
 169          $html_tag = $area_tag;
 170      } else {
 171          $html_tag = esc_attr( $attributes['tagName'] );
 172      }
 173      $wrapper_attributes = get_block_wrapper_attributes();
 174  
 175      return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]&gt;', $content ) . "</$html_tag>";
 176  }
 177  
 178  /**
 179   * Returns an array of area variation objects for the template part block.
 180   *
 181   * @since 6.1.0
 182   *
 183   * @param array $instance_variations The variations for instances.
 184   *
 185   * @return array Array containing the block variation objects.
 186   */
 187  function build_template_part_block_area_variations( $instance_variations ) {
 188      $variations    = array();
 189      $defined_areas = get_allowed_block_template_part_areas();
 190  
 191      foreach ( $defined_areas as $area ) {
 192          if ( 'uncategorized' !== $area['area'] ) {
 193              $has_instance_for_area = false;
 194              foreach ( $instance_variations as $variation ) {
 195                  if ( $variation['attributes']['area'] === $area['area'] ) {
 196                      $has_instance_for_area = true;
 197                      break;
 198                  }
 199              }
 200  
 201              $scope = $has_instance_for_area ? array() : array( 'inserter' );
 202  
 203              $variations[] = array(
 204                  'name'        => 'area_' . $area['area'],
 205                  'title'       => $area['label'],
 206                  'description' => $area['description'],
 207                  'attributes'  => array(
 208                      'area' => $area['area'],
 209                  ),
 210                  'scope'       => $scope,
 211                  'icon'        => $area['icon'],
 212              );
 213          }
 214      }
 215      return $variations;
 216  }
 217  
 218  /**
 219   * Returns an array of instance variation objects for the template part block
 220   *
 221   * @since 6.1.0
 222   *
 223   * @return array Array containing the block variation objects.
 224   */
 225  function build_template_part_block_instance_variations() {
 226      // Block themes are unavailable during installation.
 227      if ( wp_installing() ) {
 228          return array();
 229      }
 230  
 231      if ( ! current_theme_supports( 'block-templates' ) && ! current_theme_supports( 'block-template-parts' ) ) {
 232          return array();
 233      }
 234  
 235      $variations     = array();
 236      $template_parts = get_block_templates(
 237          array(
 238              'post_type' => 'wp_template_part',
 239          ),
 240          'wp_template_part'
 241      );
 242  
 243      $defined_areas = get_allowed_block_template_part_areas();
 244      $icon_by_area  = array_combine( array_column( $defined_areas, 'area' ), array_column( $defined_areas, 'icon' ) );
 245  
 246      foreach ( $template_parts as $template_part ) {
 247          $variations[] = array(
 248              'name'        => 'instance_' . sanitize_title( $template_part->slug ),
 249              'title'       => $template_part->title,
 250              // If there's no description for the template part don't show the
 251              // block description. This is a bit hacky, but prevent the fallback
 252              // by using a non-breaking space so that the value of description
 253              // isn't falsey.
 254              'description' => $template_part->description || '&nbsp;',
 255              'attributes'  => array(
 256                  'slug'  => $template_part->slug,
 257                  'theme' => $template_part->theme,
 258                  'area'  => $template_part->area,
 259              ),
 260              'scope'       => array( 'inserter' ),
 261              'icon'        => isset( $icon_by_area[ $template_part->area ] ) ? $icon_by_area[ $template_part->area ] : null,
 262              'example'     => array(
 263                  'attributes' => array(
 264                      'slug'  => $template_part->slug,
 265                      'theme' => $template_part->theme,
 266                      'area'  => $template_part->area,
 267                  ),
 268              ),
 269          );
 270      }
 271      return $variations;
 272  }
 273  
 274  /**
 275   * Returns an array of all template part block variations.
 276   *
 277   * @since 5.9.0
 278   *
 279   * @return array Array containing the block variation objects.
 280   */
 281  function build_template_part_block_variations() {
 282      $instance_variations = build_template_part_block_instance_variations();
 283      $area_variations     = build_template_part_block_area_variations( $instance_variations );
 284      return array_merge( $area_variations, $instance_variations );
 285  }
 286  
 287  /**
 288   * Registers the `core/template-part` block on the server.
 289   *
 290   * @since 5.9.0
 291   */
 292  function register_block_core_template_part() {
 293      register_block_type_from_metadata(
 294          __DIR__ . '/template-part',
 295          array(
 296              'render_callback'    => 'render_block_core_template_part',
 297              'variation_callback' => 'build_template_part_block_variations',
 298          )
 299      );
 300  }
 301  add_action( 'init', 'register_block_core_template_part' );


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