Açıklama Yok

theme-templates.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Generates a unique slug for templates.
  4. *
  5. * @access private
  6. * @since 5.8.0
  7. *
  8. * @param string $override_slug The filtered value of the slug (starts as `null` from apply_filter).
  9. * @param string $slug The original/un-filtered slug (post_name).
  10. * @param int $post_ID Post ID.
  11. * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
  12. * @param string $post_type Post type.
  13. * @return string The original, desired slug.
  14. */
  15. function wp_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) {
  16. if ( 'wp_template' !== $post_type ) {
  17. return $override_slug;
  18. }
  19. if ( ! $override_slug ) {
  20. $override_slug = $slug;
  21. }
  22. /*
  23. * Template slugs must be unique within the same theme.
  24. * TODO - Figure out how to update this to work for a multi-theme environment.
  25. * Unfortunately using `get_the_terms()` for the 'wp-theme' term does not work
  26. * in the case of new entities since is too early in the process to have been saved
  27. * to the entity. So for now we use the currently activated theme for creation.
  28. */
  29. $theme = wp_get_theme()->get_stylesheet();
  30. $terms = get_the_terms( $post_ID, 'wp_theme' );
  31. if ( $terms && ! is_wp_error( $terms ) ) {
  32. $theme = $terms[0]->name;
  33. }
  34. $check_query_args = array(
  35. 'post_name__in' => array( $override_slug ),
  36. 'post_type' => $post_type,
  37. 'posts_per_page' => 1,
  38. 'no_found_rows' => true,
  39. 'post__not_in' => array( $post_ID ),
  40. 'tax_query' => array(
  41. array(
  42. 'taxonomy' => 'wp_theme',
  43. 'field' => 'name',
  44. 'terms' => $theme,
  45. ),
  46. ),
  47. );
  48. $check_query = new WP_Query( $check_query_args );
  49. $posts = $check_query->posts;
  50. if ( count( $posts ) > 0 ) {
  51. $suffix = 2;
  52. do {
  53. $query_args = $check_query_args;
  54. $alt_post_name = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
  55. $query_args['post_name__in'] = array( $alt_post_name );
  56. $query = new WP_Query( $query_args );
  57. $suffix++;
  58. } while ( count( $query->posts ) > 0 );
  59. $override_slug = $alt_post_name;
  60. }
  61. return $override_slug;
  62. }
  63. /**
  64. * Print the skip-link script & styles.
  65. *
  66. * @access private
  67. * @since 5.8.0
  68. *
  69. * @global string $_wp_current_template_content
  70. *
  71. * @return void
  72. */
  73. function the_block_template_skip_link() {
  74. global $_wp_current_template_content;
  75. // Early exit if not a block theme.
  76. if ( ! current_theme_supports( 'block-templates' ) ) {
  77. return;
  78. }
  79. // Early exit if not a block template.
  80. if ( ! $_wp_current_template_content ) {
  81. return;
  82. }
  83. ?>
  84. <?php
  85. /**
  86. * Print the skip-link styles.
  87. */
  88. ?>
  89. <style id="skip-link-styles">
  90. .skip-link.screen-reader-text {
  91. border: 0;
  92. clip: rect(1px,1px,1px,1px);
  93. clip-path: inset(50%);
  94. height: 1px;
  95. margin: -1px;
  96. overflow: hidden;
  97. padding: 0;
  98. position: absolute !important;
  99. width: 1px;
  100. word-wrap: normal !important;
  101. }
  102. .skip-link.screen-reader-text:focus {
  103. background-color: #eee;
  104. clip: auto !important;
  105. clip-path: none;
  106. color: #444;
  107. display: block;
  108. font-size: 1em;
  109. height: auto;
  110. left: 5px;
  111. line-height: normal;
  112. padding: 15px 23px 14px;
  113. text-decoration: none;
  114. top: 5px;
  115. width: auto;
  116. z-index: 100000;
  117. }
  118. </style>
  119. <?php
  120. /**
  121. * Print the skip-link script.
  122. */
  123. ?>
  124. <script>
  125. ( function() {
  126. var skipLinkTarget = document.querySelector( 'main' ),
  127. parentEl,
  128. skipLinkTargetID,
  129. skipLink;
  130. // Early exit if a skip-link target can't be located.
  131. if ( ! skipLinkTarget ) {
  132. return;
  133. }
  134. // Get the site wrapper.
  135. // The skip-link will be injected in the beginning of it.
  136. parentEl = document.querySelector( '.wp-site-blocks' );
  137. // Early exit if the root element was not found.
  138. if ( ! parentEl ) {
  139. return;
  140. }
  141. // Get the skip-link target's ID, and generate one if it doesn't exist.
  142. skipLinkTargetID = skipLinkTarget.id;
  143. if ( ! skipLinkTargetID ) {
  144. skipLinkTargetID = 'wp--skip-link--target';
  145. skipLinkTarget.id = skipLinkTargetID;
  146. }
  147. // Create the skip link.
  148. skipLink = document.createElement( 'a' );
  149. skipLink.classList.add( 'skip-link', 'screen-reader-text' );
  150. skipLink.href = '#' + skipLinkTargetID;
  151. skipLink.innerHTML = '<?php esc_html_e( 'Skip to content' ); ?>';
  152. // Inject the skip link.
  153. parentEl.insertAdjacentElement( 'afterbegin', skipLink );
  154. }() );
  155. </script>
  156. <?php
  157. }
  158. /**
  159. * Enable the block templates (editor mode) for themes with theme.json by default.
  160. *
  161. * @access private
  162. * @since 5.8.0
  163. */
  164. function wp_enable_block_templates() {
  165. if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
  166. add_theme_support( 'block-templates' );
  167. }
  168. }