Changeset 52308
- Timestamp:
- 12/02/2021 11:35:20 PM (3 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-template.php
r51657 r52308 46 46 } 47 47 48 $block_template = resolve_block_template( $type, $templates );48 $block_template = resolve_block_template( $type, $templates, $template ); 49 49 50 50 if ( $block_template ) { … … 93 93 * @access private 94 94 * @since 5.8.0 95 * @since 5.9.0 Added the `$fallback_template` parameter. 95 96 * 96 97 * @param string $template_type The current template type. 97 98 * @param string[] $template_hierarchy The current template hierarchy, ordered by priority. 99 * @param string $fallback_template A PHP fallback template to use if no matching block template is found. 98 100 * @return WP_Block_Template|null template A template object, or null if none could be found. 99 101 */ 100 function resolve_block_template( $template_type, $template_hierarchy ) {102 function resolve_block_template( $template_type, $template_hierarchy, $fallback_template ) { 101 103 if ( ! $template_type ) { 102 104 return null; … … 129 131 } 130 132 ); 133 134 $theme_base_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR; 135 $parent_theme_base_path = get_template_directory() . DIRECTORY_SEPARATOR; 136 137 // Is the current theme a child theme, and is the PHP fallback template part of it? 138 if ( 139 strpos( $fallback_template, $theme_base_path ) === 0 && 140 strpos( $fallback_template, $parent_theme_base_path ) === false 141 ) { 142 $fallback_template_slug = substr( 143 $fallback_template, 144 // Starting position of slug. 145 strpos( $fallback_template, $theme_base_path ) + strlen( $theme_base_path ), 146 // Remove '.php' suffix. 147 -4 148 ); 149 150 // Is our candidate block template's slug identical to our PHP fallback template's? 151 if ( 152 count( $templates ) && 153 $fallback_template_slug === $templates[0]->slug && 154 'theme' === $templates[0]->source 155 ) { 156 // Unfortunately, we cannot trust $templates[0]->theme, since it will always 157 // be set to the current theme's slug by _build_block_template_result_from_file(), 158 // even if the block template is really coming from the current theme's parent. 159 // (The reason for this is that we want it to be associated with the current theme 160 // -- not its parent -- once we edit it and store it to the DB as a wp_template CPT.) 161 // Instead, we use _get_block_template_file() to locate the block template file. 162 $template_file = _get_block_template_file( 'wp_template', $fallback_template_slug ); 163 if ( $template_file && get_template() === $template_file['theme'] ) { 164 // The block template is part of the parent theme, so we 165 // have to give precedence to the child theme's PHP template. 166 array_shift( $templates ); 167 } 168 } 169 } 131 170 132 171 return count( $templates ) ? $templates[0] : null; -
trunk/tests/phpunit/tests/block-template.php
r52266 r52308 90 90 * 91 91 * Covers https://github.com/WordPress/gutenberg/pull/31123. 92 * Covers https://core.trac.wordpress.org/ticket/54515. 92 93 * 93 94 */ 94 95 function test_child_theme_php_template_takes_precedence_over_equally_specific_parent_theme_block_template() { 95 /**96 * @todo This test is currently marked as skipped, since it wouldn't pass. Turns out that in Gutenberg,97 * it only passed due to a erroneous test setup.98 * For details, see https://github.com/WordPress/wordpress-develop/pull/1920#issuecomment-975929818.99 */100 $this->markTestSkipped( 'The block template resolution algorithm needs fixing in order for this test to pass.' );101 102 96 switch_theme( 'block-theme-child' ); 103 97
Note: See TracChangeset
for help on using the changeset viewer.