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