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

Account for ambiguity in matching templates by incorporating template priority order from core #1938

Merged
merged 3 commits into from
Mar 8, 2019

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Mar 7, 2019

This fixes a bug identified in https://wordpress.org/support/topic/amp-notice-on-woocommerce-product-search-page/

It turns out that if you have a custom post type registered (say product), this custom post type has an archive, and you do a search among the custom post types with a URL such as:

?s=foo&post_type=product

The resulting WP_Query has both is_search and is_custom_post_type set. WordPress will end up serving the search.php template here because the is_search() condition comes before is_post_type_archive() check in template-loader.php. Nevertheless, we were not taking this logic into account but are rather just issuing a warning when more than one template is matched:

/*
* If there are more than one matching templates, then something is probably not right.
* Template conditions need to be set up properly to prevent this from happening.
*/
if ( count( $matching_templates ) > 1 ) {
_doing_it_wrong(
__METHOD__,
esc_html(
sprintf(
/* translators: %s: amp_supportable_templates */
__( 'Did not expect there to be more than one matching template. Did you filter %s to not honor the template hierarchy?', 'amp' ),
'amp_supportable_templates'
)
),
'1.0'
);
}

Now that we see there is a case where template conditionals can be ambiguous, we need to incorporate the priority order of template conditional checks in order to make sure we select the same matching template as WordPress core will do.

The one additional consideration that the AMP plugin has to make here is the case where a site registers a custom template type that is outside of the WordPress template hierarchy entirely. Since such a template type would not be accounted for in the template-loader.php conditional, in this case we just use the custom matching templates by assuming they are more specific than the ones coming from core.

This PR also fixes an issue with the build process where compiled files from other branches (e.g. amp-stories-redux) were not getting cleaned up and were unintentionally being included in the builds. So now those compiled files are all removed before invoking Webpack when doing a build.

Build for testing: amp.zip (v1.1-alpha-20190307T202052Z-bef18cf6)

@westonruter westonruter added the Bug Something isn't working label Mar 7, 2019
@westonruter westonruter added this to the v1.1 milestone Mar 7, 2019
@googlebot googlebot added the cla: yes Signed the Google CLA label Mar 7, 2019
@westonruter westonruter changed the title Account for ambiguity in matching templates by incorporating template priority order form core Account for ambiguity in matching templates by incorporating template priority order from core Mar 8, 2019
@westonruter westonruter merged commit be94de1 into develop Mar 8, 2019
@westonruter westonruter deleted the fix/ambigious-matching-templates branch March 8, 2019 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working cla: yes Signed the Google CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants