Skip to content

Commit 4c7c3c8

Browse files
authored
Merge pull request #1794 from ampproject/fix/is-endpoint-timing-check
Add _doing_it_wrong() when calling is_amp_endpoint() before queried object is available
2 parents c96e3cd + 744202d commit 4c7c3c8

7 files changed

+28
-6
lines changed

includes/amp-helper-functions.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,15 @@ function is_amp_endpoint() {
283283
return true;
284284
}
285285

286-
$availability = AMP_Theme_Support::get_template_availability();
287-
return amp_is_canonical() ? $availability['supported'] : ( $has_amp_query_var && $availability['supported'] );
286+
if ( ! did_action( 'wp' ) ) {
287+
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( "is_amp_endpoint() was called before the 'wp' action which means it will not have access to the queried object to determine if it is an AMP response, thus neither the amp_skip_post filter nor the AMP enabled publish metabox toggle will not be considered.", 'amp' ) ), '1.0.2' );
288+
$supported = true;
289+
} else {
290+
$availability = AMP_Theme_Support::get_template_availability();
291+
$supported = $availability['supported'];
292+
}
293+
294+
return amp_is_canonical() ? $supported : ( $has_amp_query_var && $supported );
288295
}
289296

290297
/**

includes/class-amp-theme-support.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,12 @@ public static function get_template_availability( $query = null ) {
586586
* @var WP_Post $queried_object
587587
*/
588588
$queried_object = $query->get_queried_object();
589-
$support_errors = AMP_Post_Type_Support::get_support_errors( $queried_object );
590-
if ( ! empty( $support_errors ) ) {
591-
$matching_template['errors'] = array_merge( $matching_template['errors'], $support_errors );
592-
$matching_template['supported'] = false;
589+
if ( $queried_object instanceof WP_Post ) {
590+
$support_errors = AMP_Post_Type_Support::get_support_errors( $queried_object );
591+
if ( ! empty( $support_errors ) ) {
592+
$matching_template['errors'] = array_merge( $matching_template['errors'], $support_errors );
593+
$matching_template['supported'] = false;
594+
}
593595
}
594596
}
595597

tests/test-amp-style-sanitizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,7 @@ public function test_css_import() {
13881388
* @covers \AMP_DOM_Utils::get_content_from_dom_node()
13891389
*/
13901390
public function test_unicode_stylesheet() {
1391+
wp();
13911392
add_theme_support( AMP_Theme_Support::SLUG );
13921393
AMP_Theme_Support::init();
13931394
AMP_Theme_Support::finish_init();

tests/test-class-amp-theme-support.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ public function test_add_amp_template_filters() {
383383
* @covers AMP_Theme_Support::prepare_response()
384384
*/
385385
public function test_validate_non_amp_theme() {
386+
wp();
386387
add_filter( 'amp_validation_error_sanitized', '__return_true' );
387388
add_theme_support( AMP_Theme_Support::SLUG );
388389
AMP_Theme_Support::init();
@@ -1052,6 +1053,7 @@ public function test_dequeue_customize_preview_scripts() {
10521053
* @covers AMP_Theme_Support::finish_output_buffering()
10531054
*/
10541055
public function test_start_output_buffering() {
1056+
wp();
10551057
if ( ! function_exists( 'newrelic_disable_autorum ' ) ) {
10561058
/**
10571059
* Define newrelic_disable_autorum to allow passing line.
@@ -1079,6 +1081,7 @@ function newrelic_disable_autorum() {
10791081
* @covers AMP_Theme_Support::is_output_buffering()
10801082
*/
10811083
public function test_finish_output_buffering() {
1084+
wp();
10821085
add_filter( 'amp_validation_error_sanitized', '__return_true' );
10831086
add_theme_support( AMP_Theme_Support::SLUG );
10841087
AMP_Theme_Support::init();
@@ -1127,6 +1130,7 @@ public function test_finish_output_buffering() {
11271130
* @covers AMP_Theme_Support::filter_customize_partial_render()
11281131
*/
11291132
public function test_filter_customize_partial_render() {
1133+
wp();
11301134
add_filter( 'amp_validation_error_sanitized', '__return_true' );
11311135
add_theme_support( AMP_Theme_Support::SLUG );
11321136
AMP_Theme_Support::init();
@@ -1175,6 +1179,7 @@ protected function get_etag_header_value( $headers ) {
11751179
* @covers \amp_render_scripts()
11761180
*/
11771181
public function test_prepare_response() {
1182+
wp();
11781183
$prepare_response_args = array(
11791184
'enable_response_caching' => false,
11801185
);
@@ -1337,6 +1342,7 @@ public function test_prepare_response() {
13371342
* @covers AMP_Theme_Support::prepare_response()
13381343
*/
13391344
public function test_prepare_response_native_mode_non_amp() {
1345+
wp();
13401346
$original_html = $this->get_original_html();
13411347
add_filter( 'amp_validation_error_sanitized', '__return_false' ); // For testing purpose only. This should not normally be done.
13421348

@@ -1351,6 +1357,7 @@ public function test_prepare_response_native_mode_non_amp() {
13511357
* Test post-processor cache effectiveness in AMP_Theme_Support::prepare_response().
13521358
*/
13531359
public function test_post_processor_cache_effectiveness() {
1360+
wp();
13541361
$original_html = $this->get_original_html();
13551362
$args = array( 'enable_response_caching' => true );
13561363
wp_using_ext_object_cache( true ); // turn on external object cache flag.
@@ -1508,6 +1515,7 @@ private function reset_post_processor_cache_effectiveness() {
15081515
* @covers AMP_Theme_Support::prepare_response()
15091516
*/
15101517
public function test_prepare_response_bad_html() {
1518+
wp();
15111519
add_filter( 'amp_validation_error_sanitized', '__return_true' );
15121520
add_theme_support( AMP_Theme_Support::SLUG );
15131521
AMP_Theme_Support::init();
@@ -1533,6 +1541,7 @@ public function test_prepare_response_bad_html() {
15331541
* @covers AMP_Theme_Support::prepare_response()
15341542
*/
15351543
public function test_prepare_response_to_add_html5_doctype_and_amp_attribute() {
1544+
wp();
15361545
add_filter( 'amp_validation_error_sanitized', '__return_true' );
15371546
add_theme_support( AMP_Theme_Support::SLUG );
15381547
AMP_Theme_Support::init();

tests/test-class-amp-widget-archives.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function test_construct() {
6262
* @see AMP_Widget_Archives::widget().
6363
*/
6464
public function test_widget() {
65+
wp();
6566
$this->assertTrue( is_amp_endpoint() );
6667
$arguments = array(
6768
'before_widget' => '<div>',

tests/test-class-amp-widget-categories.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function test_construct() {
6262
* @covers AMP_Widget_Categories::widget()
6363
*/
6464
public function test_widget() {
65+
wp();
6566
$this->assertTrue( is_amp_endpoint() );
6667
$arguments = array(
6768
'before_widget' => '<div>',

tests/test-class-amp-widget-text.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function tearDown() {
5353
* @covers AMP_Widget_Text::inject_video_max_width_style()
5454
*/
5555
public function test_inject_video_max_width_style() {
56+
wp();
5657
$this->assertTrue( is_amp_endpoint() );
5758
$video = '<video src="http://example.com" height="100" width="200"></video>';
5859
$video_only_width = '<video src="http://example.com/this-video" width="500">';

0 commit comments

Comments
 (0)