Skip to content

Commit

Permalink
Merge pull request #3622 from ampproject/fix/1786-only-support-public…
Browse files Browse the repository at this point in the history
…-taxonomies-for-templates

Only support public post-types/taxonomies for template support
  • Loading branch information
westonruter authored Oct 25, 2019
2 parents 580feac + 1adf6c8 commit a116db4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
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

0 comments on commit a116db4

Please sign in to comment.