wp/wp-includes/theme-templates.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
     1 <?php
     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, wp_get_theme()->get_stylesheet(), 'wp_theme' );
       
    31 	}
       
    32 }
     2 
    33 
     3 /**
    34 /**
     4  * Generates a unique slug for templates.
    35  * Generates a unique slug for templates.
     5  *
    36  *
     6  * @access private
    37  * @access private
    12  * @param string $post_status   No uniqueness checks are made if the post is still draft or pending.
    43  * @param string $post_status   No uniqueness checks are made if the post is still draft or pending.
    13  * @param string $post_type     Post type.
    44  * @param string $post_type     Post type.
    14  * @return string The original, desired slug.
    45  * @return string The original, desired slug.
    15  */
    46  */
    16 function wp_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) {
    47 function wp_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) {
    17 	if ( 'wp_template' !== $post_type ) {
    48 	if ( 'wp_template' !== $post_type && 'wp_template_part' !== $post_type ) {
    18 		return $override_slug;
    49 		return $override_slug;
    19 	}
    50 	}
    20 
    51 
    21 	if ( ! $override_slug ) {
    52 	if ( ! $override_slug ) {
    22 		$override_slug = $slug;
    53 		$override_slug = $slug;
    66 
    97 
    67 	return $override_slug;
    98 	return $override_slug;
    68 }
    99 }
    69 
   100 
    70 /**
   101 /**
    71  * Print the skip-link script & styles.
   102  * Prints the skip-link script & styles.
    72  *
   103  *
    73  * @access private
   104  * @access private
    74  * @since 5.8.0
   105  * @since 5.8.0
    75  *
   106  *
    76  * @global string $_wp_current_template_content
   107  * @global string $_wp_current_template_content
    77  *
       
    78  * @return void
       
    79  */
   108  */
    80 function the_block_template_skip_link() {
   109 function the_block_template_skip_link() {
    81 	global $_wp_current_template_content;
   110 	global $_wp_current_template_content;
    82 
   111 
    83 	// Early exit if not a block theme.
   112 	// Early exit if not a block theme.
   133 	 */
   162 	 */
   134 	?>
   163 	?>
   135 	<script>
   164 	<script>
   136 	( function() {
   165 	( function() {
   137 		var skipLinkTarget = document.querySelector( 'main' ),
   166 		var skipLinkTarget = document.querySelector( 'main' ),
   138 			parentEl,
   167 			sibling,
   139 			skipLinkTargetID,
   168 			skipLinkTargetID,
   140 			skipLink;
   169 			skipLink;
   141 
   170 
   142 		// Early exit if a skip-link target can't be located.
   171 		// Early exit if a skip-link target can't be located.
   143 		if ( ! skipLinkTarget ) {
   172 		if ( ! skipLinkTarget ) {
   144 			return;
   173 			return;
   145 		}
   174 		}
   146 
   175 
   147 		// Get the site wrapper.
   176 		// Get the site wrapper.
   148 		// The skip-link will be injected in the beginning of it.
   177 		// The skip-link will be injected in the beginning of it.
   149 		parentEl = document.querySelector( '.wp-site-blocks' );
   178 		sibling = document.querySelector( '.wp-site-blocks' );
   150 
   179 
   151 		// Early exit if the root element was not found.
   180 		// Early exit if the root element was not found.
   152 		if ( ! parentEl ) {
   181 		if ( ! sibling ) {
   153 			return;
   182 			return;
   154 		}
   183 		}
   155 
   184 
   156 		// Get the skip-link target's ID, and generate one if it doesn't exist.
   185 		// Get the skip-link target's ID, and generate one if it doesn't exist.
   157 		skipLinkTargetID = skipLinkTarget.id;
   186 		skipLinkTargetID = skipLinkTarget.id;
   165 		skipLink.classList.add( 'skip-link', 'screen-reader-text' );
   194 		skipLink.classList.add( 'skip-link', 'screen-reader-text' );
   166 		skipLink.href = '#' + skipLinkTargetID;
   195 		skipLink.href = '#' + skipLinkTargetID;
   167 		skipLink.innerHTML = '<?php esc_html_e( 'Skip to content' ); ?>';
   196 		skipLink.innerHTML = '<?php esc_html_e( 'Skip to content' ); ?>';
   168 
   197 
   169 		// Inject the skip link.
   198 		// Inject the skip link.
   170 		parentEl.insertAdjacentElement( 'afterbegin', skipLink );
   199 		sibling.parentElement.insertBefore( skipLink, sibling );
   171 	}() );
   200 	}() );
   172 	</script>
   201 	</script>
   173 	<?php
   202 	<?php
   174 }
   203 }
   175 
   204 
   176 /**
   205 /**
   177  * Enable the block templates (editor mode) for themes with theme.json by default.
   206  * Enables the block templates (editor mode) for themes with theme.json by default.
   178  *
   207  *
   179  * @access private
   208  * @access private
   180  * @since 5.8.0
   209  * @since 5.8.0
   181  */
   210  */
   182 function wp_enable_block_templates() {
   211 function wp_enable_block_templates() {
   183 	if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
   212 	if ( wp_is_block_theme() || WP_Theme_JSON_Resolver::theme_has_support() ) {
   184 		add_theme_support( 'block-templates' );
   213 		add_theme_support( 'block-templates' );
   185 	}
   214 	}
   186 }
   215 }