wp/wp-includes/theme-templates.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    25 		);
    25 		);
    26 	}
    26 	}
    27 
    27 
    28 	$terms = get_the_terms( $post_id, 'wp_theme' );
    28 	$terms = get_the_terms( $post_id, 'wp_theme' );
    29 	if ( ! is_array( $terms ) || ! count( $terms ) ) {
    29 	if ( ! is_array( $terms ) || ! count( $terms ) ) {
    30 		wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
    30 		wp_set_post_terms( $post_id, get_stylesheet(), 'wp_theme' );
    31 	}
    31 	}
    32 }
    32 }
    33 
    33 
    34 /**
    34 /**
    35  * Generates a unique slug for templates.
    35  * Generates a unique slug for templates.
    37  * @access private
    37  * @access private
    38  * @since 5.8.0
    38  * @since 5.8.0
    39  *
    39  *
    40  * @param string $override_slug The filtered value of the slug (starts as `null` from apply_filter).
    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).
    41  * @param string $slug          The original/un-filtered slug (post_name).
    42  * @param int    $post_ID       Post ID.
    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.
    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.
    44  * @param string $post_type     Post type.
    45  * @return string The original, desired slug.
    45  * @return string The original, desired slug.
    46  */
    46  */
    47 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 ) {
    48 	if ( 'wp_template' !== $post_type && 'wp_template_part' !== $post_type ) {
    48 	if ( 'wp_template' !== $post_type && 'wp_template_part' !== $post_type ) {
    49 		return $override_slug;
    49 		return $override_slug;
    50 	}
    50 	}
    51 
    51 
    52 	if ( ! $override_slug ) {
    52 	if ( ! $override_slug ) {
    58 	 * TODO - Figure out how to update this to work for a multi-theme environment.
    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
    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
    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.
    61 	 * to the entity. So for now we use the currently activated theme for creation.
    62 	 */
    62 	 */
    63 	$theme = wp_get_theme()->get_stylesheet();
    63 	$theme = get_stylesheet();
    64 	$terms = get_the_terms( $post_ID, 'wp_theme' );
    64 	$terms = get_the_terms( $post_id, 'wp_theme' );
    65 	if ( $terms && ! is_wp_error( $terms ) ) {
    65 	if ( $terms && ! is_wp_error( $terms ) ) {
    66 		$theme = $terms[0]->name;
    66 		$theme = $terms[0]->name;
    67 	}
    67 	}
    68 
    68 
    69 	$check_query_args = array(
    69 	$check_query_args = array(
    70 		'post_name__in'  => array( $override_slug ),
    70 		'post_name__in'  => array( $override_slug ),
    71 		'post_type'      => $post_type,
    71 		'post_type'      => $post_type,
    72 		'posts_per_page' => 1,
    72 		'posts_per_page' => 1,
    73 		'no_found_rows'  => true,
    73 		'no_found_rows'  => true,
    74 		'post__not_in'   => array( $post_ID ),
    74 		'post__not_in'   => array( $post_id ),
    75 		'tax_query'      => array(
    75 		'tax_query'      => array(
    76 			array(
    76 			array(
    77 				'taxonomy' => 'wp_theme',
    77 				'taxonomy' => 'wp_theme',
    78 				'field'    => 'name',
    78 				'field'    => 'name',
    79 				'terms'    => $theme,
    79 				'terms'    => $theme,
    88 		do {
    88 		do {
    89 			$query_args                  = $check_query_args;
    89 			$query_args                  = $check_query_args;
    90 			$alt_post_name               = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    90 			$alt_post_name               = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    91 			$query_args['post_name__in'] = array( $alt_post_name );
    91 			$query_args['post_name__in'] = array( $alt_post_name );
    92 			$query                       = new WP_Query( $query_args );
    92 			$query                       = new WP_Query( $query_args );
    93 			$suffix++;
    93 			++$suffix;
    94 		} while ( count( $query->posts ) > 0 );
    94 		} while ( count( $query->posts ) > 0 );
    95 		$override_slug = $alt_post_name;
    95 		$override_slug = $alt_post_name;
    96 	}
    96 	}
    97 
    97 
    98 	return $override_slug;
    98 	return $override_slug;
    99 }
    99 }
   100 
   100 
   101 /**
   101 /**
   102  * Prints the skip-link script & styles.
   102  * Enqueues the skip-link script & styles.
   103  *
   103  *
   104  * @access private
   104  * @access private
   105  * @since 5.8.0
   105  * @since 6.4.0
   106  *
   106  *
   107  * @global string $_wp_current_template_content
   107  * @global string $_wp_current_template_content
   108  */
   108  */
   109 function the_block_template_skip_link() {
   109 function wp_enqueue_block_template_skip_link() {
   110 	global $_wp_current_template_content;
   110 	global $_wp_current_template_content;
       
   111 
       
   112 	// Back-compat for plugins that disable functionality by unhooking this action.
       
   113 	if ( ! has_action( 'wp_footer', 'the_block_template_skip_link' ) ) {
       
   114 		return;
       
   115 	}
       
   116 	remove_action( 'wp_footer', 'the_block_template_skip_link' );
   111 
   117 
   112 	// Early exit if not a block theme.
   118 	// Early exit if not a block theme.
   113 	if ( ! current_theme_supports( 'block-templates' ) ) {
   119 	if ( ! current_theme_supports( 'block-templates' ) ) {
   114 		return;
   120 		return;
   115 	}
   121 	}
   116 
   122 
   117 	// Early exit if not a block template.
   123 	// Early exit if not a block template.
   118 	if ( ! $_wp_current_template_content ) {
   124 	if ( ! $_wp_current_template_content ) {
   119 		return;
   125 		return;
   120 	}
   126 	}
   121 	?>
   127 
   122 
   128 	$skip_link_styles = '
   123 	<?php
       
   124 	/**
       
   125 	 * Print the skip-link styles.
       
   126 	 */
       
   127 	?>
       
   128 	<style id="skip-link-styles">
       
   129 		.skip-link.screen-reader-text {
   129 		.skip-link.screen-reader-text {
   130 			border: 0;
   130 			border: 0;
   131 			clip: rect(1px,1px,1px,1px);
   131 			clip: rect(1px,1px,1px,1px);
   132 			clip-path: inset(50%);
   132 			clip-path: inset(50%);
   133 			height: 1px;
   133 			height: 1px;
   152 			padding: 15px 23px 14px;
   152 			padding: 15px 23px 14px;
   153 			text-decoration: none;
   153 			text-decoration: none;
   154 			top: 5px;
   154 			top: 5px;
   155 			width: auto;
   155 			width: auto;
   156 			z-index: 100000;
   156 			z-index: 100000;
   157 		}
   157 		}';
   158 	</style>
   158 
   159 	<?php
   159 	$handle = 'wp-block-template-skip-link';
       
   160 
   160 	/**
   161 	/**
   161 	 * Print the skip-link script.
   162 	 * Print the skip-link styles.
   162 	 */
   163 	 */
       
   164 	wp_register_style( $handle, false );
       
   165 	wp_add_inline_style( $handle, $skip_link_styles );
       
   166 	wp_enqueue_style( $handle );
       
   167 
       
   168 	/**
       
   169 	 * Enqueue the skip-link script.
       
   170 	 */
       
   171 	ob_start();
   163 	?>
   172 	?>
   164 	<script>
   173 	<script>
   165 	( function() {
   174 	( function() {
   166 		var skipLinkTarget = document.querySelector( 'main' ),
   175 		var skipLinkTarget = document.querySelector( 'main' ),
   167 			sibling,
   176 			sibling,
   171 		// Early exit if a skip-link target can't be located.
   180 		// Early exit if a skip-link target can't be located.
   172 		if ( ! skipLinkTarget ) {
   181 		if ( ! skipLinkTarget ) {
   173 			return;
   182 			return;
   174 		}
   183 		}
   175 
   184 
   176 		// Get the site wrapper.
   185 		/*
   177 		// The skip-link will be injected in the beginning of it.
   186 		 * Get the site wrapper.
       
   187 		 * The skip-link will be injected in the beginning of it.
       
   188 		 */
   178 		sibling = document.querySelector( '.wp-site-blocks' );
   189 		sibling = document.querySelector( '.wp-site-blocks' );
   179 
   190 
   180 		// Early exit if the root element was not found.
   191 		// Early exit if the root element was not found.
   181 		if ( ! sibling ) {
   192 		if ( ! sibling ) {
   182 			return;
   193 			return;
   191 
   202 
   192 		// Create the skip link.
   203 		// Create the skip link.
   193 		skipLink = document.createElement( 'a' );
   204 		skipLink = document.createElement( 'a' );
   194 		skipLink.classList.add( 'skip-link', 'screen-reader-text' );
   205 		skipLink.classList.add( 'skip-link', 'screen-reader-text' );
   195 		skipLink.href = '#' + skipLinkTargetID;
   206 		skipLink.href = '#' + skipLinkTargetID;
   196 		skipLink.innerHTML = '<?php esc_html_e( 'Skip to content' ); ?>';
   207 		skipLink.innerHTML = '<?php /* translators: Hidden accessibility text. */ esc_html_e( 'Skip to content' ); ?>';
   197 
   208 
   198 		// Inject the skip link.
   209 		// Inject the skip link.
   199 		sibling.parentElement.insertBefore( skipLink, sibling );
   210 		sibling.parentElement.insertBefore( skipLink, sibling );
   200 	}() );
   211 	}() );
   201 	</script>
   212 	</script>
   202 	<?php
   213 	<?php
       
   214 	$skip_link_script = wp_remove_surrounding_empty_script_tags( ob_get_clean() );
       
   215 	$script_handle    = 'wp-block-template-skip-link';
       
   216 	wp_register_script( $script_handle, false, array(), false, array( 'in_footer' => true ) );
       
   217 	wp_add_inline_script( $script_handle, $skip_link_script );
       
   218 	wp_enqueue_script( $script_handle );
   203 }
   219 }
   204 
   220 
   205 /**
   221 /**
   206  * Enables the block templates (editor mode) for themes with theme.json by default.
   222  * Enables the block templates (editor mode) for themes with theme.json by default.
   207  *
   223  *
   208  * @access private
   224  * @access private
   209  * @since 5.8.0
   225  * @since 5.8.0
   210  */
   226  */
   211 function wp_enable_block_templates() {
   227 function wp_enable_block_templates() {
   212 	if ( wp_is_block_theme() || WP_Theme_JSON_Resolver::theme_has_support() ) {
   228 	if ( wp_is_block_theme() || wp_theme_has_theme_json() ) {
   213 		add_theme_support( 'block-templates' );
   229 		add_theme_support( 'block-templates' );
   214 	}
   230 	}
   215 }
   231 }