[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> theme-templates.php (source)

   1  <?php
   2  
   3  /**
   4   * Sets a custom slug when creating auto-draft template parts.
   5   *
   6   * This is only needed for auto-drafts created by the regular WP editor.
   7   * If this page is to be removed, this will not be necessary.
   8   *
   9   * @since 5.9.0
  10   *
  11   * @param int $post_id Post ID.
  12   */
  13  function wp_set_unique_slug_on_create_template_part( $post_id ) {
  14      $post = get_post( $post_id );
  15      if ( 'auto-draft' !== $post->post_status ) {
  16          return;
  17      }
  18  
  19      if ( ! $post->post_name ) {
  20          wp_update_post(
  21              array(
  22                  'ID'        => $post_id,
  23                  'post_name' => 'custom_slug_' . uniqid(),
  24              )
  25          );
  26      }
  27  
  28      $terms = get_the_terms( $post_id, 'wp_theme' );
  29      if ( ! is_array( $terms ) || ! count( $terms ) ) {
  30          wp_set_post_terms( $post_id, get_stylesheet(), 'wp_theme' );
  31      }
  32  }
  33  
  34  /**
  35   * Generates a unique slug for templates.
  36   *
  37   * @access private
  38   * @since 5.8.0
  39   *
  40   * @param string $override_slug The filtered value of the slug (starts as `null` from apply_filter).
  41   * @param string $slug          The original/un-filtered slug (post_name).
  42   * @param int    $post_id       Post ID.
  43   * @param string $post_status   No uniqueness checks are made if the post is still draft or pending.
  44   * @param string $post_type     Post type.
  45   * @return string The original, desired slug.
  46   */
  47  function wp_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_id, $post_status, $post_type ) {
  48      if ( 'wp_template' !== $post_type && 'wp_template_part' !== $post_type ) {
  49          return $override_slug;
  50      }
  51  
  52      if ( ! $override_slug ) {
  53          $override_slug = $slug;
  54      }
  55  
  56      /*
  57       * Template slugs must be unique within the same theme.
  58       * TODO - Figure out how to update this to work for a multi-theme environment.
  59       * Unfortunately using `get_the_terms()` for the 'wp-theme' term does not work
  60       * in the case of new entities since is too early in the process to have been saved
  61       * to the entity. So for now we use the currently activated theme for creation.
  62       */
  63      $theme = get_stylesheet();
  64      $terms = get_the_terms( $post_id, 'wp_theme' );
  65      if ( $terms && ! is_wp_error( $terms ) ) {
  66          $theme = $terms[0]->name;
  67      }
  68  
  69      $check_query_args = array(
  70          'post_name__in'  => array( $override_slug ),
  71          'post_type'      => $post_type,
  72          'posts_per_page' => 1,
  73          'no_found_rows'  => true,
  74          'post__not_in'   => array( $post_id ),
  75          'tax_query'      => array(
  76              array(
  77                  'taxonomy' => 'wp_theme',
  78                  'field'    => 'name',
  79                  'terms'    => $theme,
  80              ),
  81          ),
  82      );
  83      $check_query      = new WP_Query( $check_query_args );
  84      $posts            = $check_query->posts;
  85  
  86      if ( count( $posts ) > 0 ) {
  87          $suffix = 2;
  88          do {
  89              $query_args                  = $check_query_args;
  90              $alt_post_name               = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
  91              $query_args['post_name__in'] = array( $alt_post_name );
  92              $query                       = new WP_Query( $query_args );
  93              ++$suffix;
  94          } while ( count( $query->posts ) > 0 );
  95          $override_slug = $alt_post_name;
  96      }
  97  
  98      return $override_slug;
  99  }
 100  
 101  /**
 102   * Enqueues the skip-link styles.
 103   *
 104   * @access private
 105   * @since 6.4.0
 106   * @since 7.0.0 A script is no longer printed in favor of being added via {@see _block_template_add_skip_link()}.
 107   *
 108   * @global string $_wp_current_template_content
 109   */
 110  function wp_enqueue_block_template_skip_link() {
 111      global $_wp_current_template_content;
 112  
 113      // Back-compat for plugins that disable functionality by unhooking this action.
 114      if ( ! has_action( 'wp_footer', 'the_block_template_skip_link' ) ) {
 115          return;
 116      }
 117      remove_action( 'wp_footer', 'the_block_template_skip_link' );
 118  
 119      // Early exit if not a block theme.
 120      if ( ! current_theme_supports( 'block-templates' ) ) {
 121          return;
 122      }
 123  
 124      // Early exit if not a block template.
 125      if ( ! $_wp_current_template_content ) {
 126          return;
 127      }
 128  
 129      wp_enqueue_style( 'wp-block-template-skip-link' );
 130  }
 131  
 132  /**
 133   * Enables the block templates (editor mode) for themes with theme.json by default.
 134   *
 135   * @access private
 136   * @since 5.8.0
 137   */
 138  function wp_enable_block_templates() {
 139      if ( wp_is_block_theme() || wp_theme_has_theme_json() ) {
 140          add_theme_support( 'block-templates' );
 141      }
 142  }


Generated : Wed May 6 08:20:15 2026 Cross-referenced by PHPXref