diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index ac35769410f..38dfd0e053f 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -715,7 +715,27 @@ public static function get_template_availability( $query = null ) { if ( ! $has_children ) { $supportable_template = $supportable_templates[ $id ]; while ( ! empty( $supportable_template['parent'] ) ) { - $parent = $supportable_template['parent']; + $parent = $supportable_template['parent']; + + /* + * If the parent is not amongst the supportable templates, then something is off in terms of hierarchy. + * Either the matching is off-track, or the template is badly configured. + */ + if ( ! array_key_exists( $parent, $supportable_templates ) ) { + _doing_it_wrong( + __METHOD__, + esc_html( + sprintf( + /* translators: %s: amp_supportable_templates */ + __( 'An expected parent was not found. Did you filter %s to not honor the template hierarchy?', 'amp' ), + 'amp_supportable_templates' + ) + ), + '1.4' + ); + break; + } + $supportable_template = $supportable_templates[ $parent ]; // Let the child supported status override the parent's supported status. @@ -922,8 +942,8 @@ public static function get_supportable_templates() { } $taxonomy_args = [ - '_builtin' => false, - 'publicly_queryable' => true, + '_builtin' => false, + 'public' => true, ]; foreach ( get_taxonomies( $taxonomy_args, 'objects' ) as $taxonomy ) { $templates[ sprintf( 'is_tax[%s]', $taxonomy->name ) ] = [ @@ -936,8 +956,8 @@ public static function get_supportable_templates() { } $post_type_args = [ - 'has_archive' => true, - 'publicly_queryable' => true, + 'has_archive' => true, + 'public' => true, ]; foreach ( get_post_types( $post_type_args, 'objects' ) as $post_type ) { $templates[ sprintf( 'is_post_type_archive[%s]', $post_type->name ) ] = [ diff --git a/tests/php/test-class-amp-theme-support.php b/tests/php/test-class-amp-theme-support.php index 16cf8d35f7c..49143d9b4ef 100644 --- a/tests/php/test-class-amp-theme-support.php +++ b/tests/php/test-class-amp-theme-support.php @@ -783,8 +783,8 @@ public function test_get_template_availability_with_ambiguity() { register_post_type( $custom_post_type, [ - 'has_archive' => true, - 'publicly_queryable' => true, + 'has_archive' => true, + 'public' => true, ] ); self::factory()->post->create( @@ -813,6 +813,43 @@ public function test_get_template_availability_with_ambiguity() { $this->assertEquals( 'is_search', $availability['template'] ); } + /** + * Test get_template_availability with broken parent relationship. + * + * @covers AMP_Theme_Support::get_template_availability() + */ + public function test_get_template_availability_with_missing_parent() { + AMP_Options_Manager::update_option( 'supported_templates', [ 'missing_parent' ] ); + add_theme_support( AMP_Theme_Support::SLUG ); + add_filter( + 'amp_supportable_templates', + static function ( $templates ) { + $templates['missing_parent'] = [ + 'label' => 'Missing parent', + 'parent' => 'is_unknown', + 'callback' => static function( WP_Query $query ) { + return false !== $query->get( 'missing_parent', false ); + }, + ]; + return $templates; + } + ); + add_filter( + 'query_vars', + static function ( $vars ) { + $vars[] = 'missing_parent'; + return $vars; + } + ); + + // Test missing_parent. + $this->go_to( '/?missing_parent=1' ); + $this->setExpectedIncorrectUsage( 'AMP_Theme_Support::get_template_availability' ); + $availability = AMP_Theme_Support::get_template_availability(); + $this->assertTrue( $availability['supported'] ); + $this->assertEquals( 'missing_parent', $availability['template'] ); + } + /** * Test get_supportable_templates. *