diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 0370f824035..e03466ac7f5 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -318,7 +318,8 @@ public static function read_theme_support() { ); } - $is_paired = ! empty( $args[ self::PAIRED_FLAG ] ); + // See amp_is_canonical(). + $is_paired = isset( $args[ self::PAIRED_FLAG ] ) ? $args[ self::PAIRED_FLAG ] : ! empty( $args['template_dir'] ); self::$support_added_via_theme = $is_paired ? self::TRANSITIONAL_MODE_SLUG : self::STANDARD_MODE_SLUG; self::$support_added_via_option = $theme_support_option; diff --git a/includes/options/class-amp-options-manager.php b/includes/options/class-amp-options-manager.php index 5943f580a63..3c23da302a6 100644 --- a/includes/options/class-amp-options-manager.php +++ b/includes/options/class-amp-options-manager.php @@ -126,9 +126,8 @@ public static function get_options() { $defaults['enable_response_caching'] = wp_using_ext_object_cache(); - $args = AMP_Theme_Support::get_theme_support_args(); - if ( false !== $args ) { - $defaults['theme_support'] = empty( $args[ AMP_Theme_Support::PAIRED_FLAG ] ) ? AMP_Theme_Support::STANDARD_MODE_SLUG : AMP_Theme_Support::TRANSITIONAL_MODE_SLUG; + if ( current_theme_supports( 'amp' ) ) { + $defaults['theme_support'] = amp_is_canonical() ? AMP_Theme_Support::STANDARD_MODE_SLUG : AMP_Theme_Support::TRANSITIONAL_MODE_SLUG; } $options = array_merge( $defaults, $options ); diff --git a/tests/php/test-amp-helper-functions.php b/tests/php/test-amp-helper-functions.php index 873af10b55e..bca1566cba1 100644 --- a/tests/php/test-amp-helper-functions.php +++ b/tests/php/test-amp-helper-functions.php @@ -269,15 +269,15 @@ public function test_amp_add_frontend_actions() { public function get_amphtml_urls() { $post_id = self::factory()->post->create(); return [ - 'home' => [ + 'is_home' => [ home_url( '/' ), add_query_arg( amp_get_slug(), '', home_url( '/' ) ), ], - '404' => [ + 'is_404' => [ home_url( '/no-existe/' ), add_query_arg( amp_get_slug(), '', home_url( '/no-existe/' ) ), ], - 'post' => [ + 'is_post' => [ get_permalink( $post_id ), amp_get_permalink( $post_id ), ], @@ -292,8 +292,45 @@ public function get_amphtml_urls() { * @param string $canonical_url Canonical URL. * @param string $amphtml_url The amphtml URL. */ - public function test_amp_add_amphtml_link( $canonical_url, $amphtml_url ) { + public function test_amp_add_amphtml_link_reader_mode( $canonical_url, $amphtml_url ) { + $this->assertFalse( current_theme_supports( AMP_Theme_Support::SLUG ) ); + $this->assertFalse( amp_is_canonical() ); + $get_amp_html_link = static function() { + return get_echo( 'amp_add_amphtml_link' ); + }; + + $assert_amphtml_link_present = function() use ( $amphtml_url, $get_amp_html_link ) { + $this->assertEquals( + sprintf( '', esc_url( $amphtml_url ) ), + $get_amp_html_link() + ); + }; + + $this->go_to( $canonical_url ); + $assert_amphtml_link_present(); + + // Make sure adding the filter hides the amphtml link. + add_filter( 'amp_frontend_show_canonical', '__return_false' ); + $this->assertEmpty( $get_amp_html_link() ); + remove_filter( 'amp_frontend_show_canonical', '__return_false' ); + $assert_amphtml_link_present(); + } + + /** + * Adding link when theme support in transitional mode. + * + * @dataProvider get_amphtml_urls + * @covers ::amp_add_amphtml_link() + * @param string $canonical_url Canonical URL. + * @param string $amphtml_url The amphtml URL. + */ + public function test_amp_add_amphtml_link_transitional_mode( $canonical_url, $amphtml_url ) { + AMP_Options_Manager::update_option( 'theme_support', AMP_Theme_Support::TRANSITIONAL_MODE_SLUG ); AMP_Options_Manager::update_option( 'auto_accept_sanitization', false ); + AMP_Theme_Support::read_theme_support(); + AMP_Theme_Support::init(); + $this->assertTrue( current_theme_supports( AMP_Theme_Support::SLUG ) ); + $this->assertFalse( amp_is_canonical() ); $get_amp_html_link = static function() { return get_echo( 'amp_add_amphtml_link' ); @@ -316,15 +353,6 @@ public function test_amp_add_amphtml_link( $canonical_url, $amphtml_url ) { $assert_amphtml_link_present(); // Make sure that the link is not provided when there are validation errors associated with the URL. - add_theme_support( - AMP_Theme_Support::SLUG, - [ - 'template_dir' => './', - ] - ); - AMP_Options_Manager::update_option( 'theme_support', AMP_Theme_Support::STANDARD_MODE_SLUG ); - AMP_Theme_Support::read_theme_support(); - AMP_Theme_Support::init(); $invalid_url_post_id = AMP_Validated_URL_Post_Type::store_validation_errors( [ [ 'code' => 'foo' ], @@ -332,10 +360,12 @@ public function test_amp_add_amphtml_link( $canonical_url, $amphtml_url ) { $canonical_url ); $this->assertNotInstanceOf( 'WP_Error', $invalid_url_post_id ); + $this->assertContains( '