Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only support public post-types/taxonomies for template support #3622

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) ] = [
Expand All @@ -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 ) ] = [
Expand Down
41 changes: 39 additions & 2 deletions tests/php/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
*
Expand Down