diff -r be944660c56a -r 3d72ae0968f4 wp/wp-includes/block-template.php --- a/wp/wp-includes/block-template.php Wed Sep 21 18:19:35 2022 +0200 +++ b/wp/wp-includes/block-template.php Tue Sep 27 16:37:53 2022 +0200 @@ -6,6 +6,32 @@ */ /** + * Adds necessary filters to use 'wp_template' posts instead of theme template files. + * + * @access private + * @since 5.9.0 + */ +function _add_template_loader_filters() { + if ( ! current_theme_supports( 'block-templates' ) ) { + return; + } + + $template_types = array_keys( get_default_block_template_types() ); + foreach ( $template_types as $template_type ) { + // Skip 'embed' for now because it is not a regular template type. + if ( 'embed' === $template_type ) { + continue; + } + add_filter( str_replace( '-', '', $template_type ) . '_template', 'locate_block_template', 20, 3 ); + } + + // Request to resolve a template. + if ( isset( $_GET['_wp-find-template'] ) ) { + add_filter( 'pre_get_posts', '_resolve_template_for_new_post' ); + } +} + +/** * Find a block template with equal or higher specificity than a given PHP template file. * * Internally, this communicates the block content that needs to be used by the template canvas through a global variable. @@ -14,14 +40,18 @@ * * @global string $_wp_current_template_content * - * @param string $template Path to the template. See locate_template(). - * @param string $type Sanitized filename without extension. - * @param array $templates A list of template candidates, in descending order of priority. + * @param string $template Path to the template. See locate_template(). + * @param string $type Sanitized filename without extension. + * @param string[] $templates A list of template candidates, in descending order of priority. * @return string The path to the Full Site Editing template canvas file, or the fallback PHP template. */ function locate_block_template( $template, $type, array $templates ) { global $_wp_current_template_content; + if ( ! current_theme_supports( 'block-templates' ) ) { + return $template; + } + if ( $template ) { /* * locate_template() has found a PHP template at the path specified by $template. @@ -40,12 +70,12 @@ ); $index = array_search( $relative_template_path, $templates, true ); - // If the template hiearchy algorithm has successfully located a PHP template file, + // If the template hierarchy algorithm has successfully located a PHP template file, // we will only consider block templates with higher or equal specificity. $templates = array_slice( $templates, 0, $index + 1 ); } - $block_template = resolve_block_template( $type, $templates ); + $block_template = resolve_block_template( $type, $templates, $template ); if ( $block_template ) { if ( empty( $block_template->content ) && is_user_logged_in() ) { @@ -92,12 +122,14 @@ * * @access private * @since 5.8.0 + * @since 5.9.0 Added the `$fallback_template` parameter. * * @param string $template_type The current template type. * @param string[] $template_hierarchy The current template hierarchy, ordered by priority. + * @param string $fallback_template A PHP fallback template to use if no matching block template is found. * @return WP_Block_Template|null template A template object, or null if none could be found. */ -function resolve_block_template( $template_type, $template_hierarchy ) { +function resolve_block_template( $template_type, $template_hierarchy, $fallback_template ) { if ( ! $template_type ) { return null; } @@ -124,11 +156,48 @@ usort( $templates, - function ( $template_a, $template_b ) use ( $slug_priorities ) { + static function ( $template_a, $template_b ) use ( $slug_priorities ) { return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ]; } ); + $theme_base_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR; + $parent_theme_base_path = get_template_directory() . DIRECTORY_SEPARATOR; + + // Is the active theme a child theme, and is the PHP fallback template part of it? + if ( + strpos( $fallback_template, $theme_base_path ) === 0 && + strpos( $fallback_template, $parent_theme_base_path ) === false + ) { + $fallback_template_slug = substr( + $fallback_template, + // Starting position of slug. + strpos( $fallback_template, $theme_base_path ) + strlen( $theme_base_path ), + // Remove '.php' suffix. + -4 + ); + + // Is our candidate block template's slug identical to our PHP fallback template's? + if ( + count( $templates ) && + $fallback_template_slug === $templates[0]->slug && + 'theme' === $templates[0]->source + ) { + // Unfortunately, we cannot trust $templates[0]->theme, since it will always + // be set to the active theme's slug by _build_block_template_result_from_file(), + // even if the block template is really coming from the active theme's parent. + // (The reason for this is that we want it to be associated with the active theme + // -- not its parent -- once we edit it and store it to the DB as a wp_template CPT.) + // Instead, we use _get_block_template_file() to locate the block template file. + $template_file = _get_block_template_file( 'wp_template', $fallback_template_slug ); + if ( $template_file && get_template() === $template_file['theme'] ) { + // The block template is part of the parent theme, so we + // have to give precedence to the child theme's PHP template. + array_shift( $templates ); + } + } + } + return count( $templates ) ? $templates[0] : null; } @@ -170,7 +239,10 @@ $content = $wp_embed->autoembed( $content ); $content = do_blocks( $content ); $content = wptexturize( $content ); + $content = convert_smilies( $content ); + $content = shortcode_unautop( $content ); $content = wp_filter_content_tags( $content ); + $content = do_shortcode( $content ); $content = str_replace( ']]>', ']]>', $content ); // Wrap block template in .wp-site-blocks to allow for specific descendant styles @@ -227,3 +299,71 @@ return $context; } + +/** + * Sets the current WP_Query to return auto-draft posts. + * + * The auto-draft status indicates a new post, so allow the the WP_Query instance to + * return an auto-draft post for template resolution when editing a new post. + * + * @access private + * @since 5.9.0 + * + * @param WP_Query $wp_query Current WP_Query instance, passed by reference. + */ +function _resolve_template_for_new_post( $wp_query ) { + if ( ! $wp_query->is_main_query() ) { + return; + } + + remove_filter( 'pre_get_posts', '_resolve_template_for_new_post' ); + + // Pages. + $page_id = isset( $wp_query->query['page_id'] ) ? $wp_query->query['page_id'] : null; + + // Posts, including custom post types. + $p = isset( $wp_query->query['p'] ) ? $wp_query->query['p'] : null; + + $post_id = $page_id ? $page_id : $p; + $post = get_post( $post_id ); + + if ( + $post && + 'auto-draft' === $post->post_status && + current_user_can( 'edit_post', $post->ID ) + ) { + $wp_query->set( 'post_status', 'auto-draft' ); + } +} + +/** + * Returns the correct template for the site's home page. + * + * @access private + * @since 6.0.0 + * + * @return array|null A template object, or null if none could be found. + */ +function _resolve_home_block_template() { + $show_on_front = get_option( 'show_on_front' ); + $front_page_id = get_option( 'page_on_front' ); + + if ( 'page' === $show_on_front && $front_page_id ) { + return array( + 'postType' => 'page', + 'postId' => $front_page_id, + ); + } + + $hierarchy = array( 'front-page', 'home', 'index' ); + $template = resolve_block_template( 'home', $hierarchy, '' ); + + if ( ! $template ) { + return null; + } + + return array( + 'postType' => 'wp_template', + 'postId' => $template->id, + ); +}