From 31e6797012d0c2e788a8a352f091c54730679230 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 Jan 2018 14:00:58 -0800 Subject: [PATCH] Account for post_supports_amp() in is_paired_available() --- includes/class-amp-theme-support.php | 4 ++ tests/test-class-amp-theme-support.php | 70 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/test-class-amp-theme-support.php diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 7f5c1a3c631..c5100e90128 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -127,6 +127,10 @@ public static function is_paired_available() { return false; } + if ( is_singular() && ! post_supports_amp( get_queried_object() ) ) { + return false; + } + $args = array_shift( $support ); if ( isset( $args['available_callback'] ) && is_callable( $args['available_callback'] ) ) { diff --git a/tests/test-class-amp-theme-support.php b/tests/test-class-amp-theme-support.php new file mode 100644 index 00000000000..5a10663bfb7 --- /dev/null +++ b/tests/test-class-amp-theme-support.php @@ -0,0 +1,70 @@ +factory()->post->create( array( 'post_title' => 'Test' ) ); + remove_theme_support( 'amp' ); + query_posts( array( 'p' => $post_id ) ); // phpcs:ignore + $this->assertTrue( is_singular() ); + + // Paired support is not available if theme support is not present or canonical. + $this->assertFalse( AMP_Theme_Support::is_paired_available() ); + add_theme_support( 'amp' ); + $this->assertFalse( AMP_Theme_Support::is_paired_available() ); + + // Paired mode is available once template_dir is supplied. + add_theme_support( 'amp', array( + 'template_dir' => 'amp-templates', + ) ); + $this->assertTrue( AMP_Theme_Support::is_paired_available() ); + + // Paired mode not available when post does not support AMP. + add_filter( 'amp_skip_post', '__return_true' ); + $this->assertFalse( AMP_Theme_Support::is_paired_available() ); + $this->assertTrue( is_singular() ); + query_posts( array( 's' => 'test' ) ); // phpcs:ignore + $this->assertTrue( is_search() ); + $this->assertTrue( AMP_Theme_Support::is_paired_available() ); + remove_filter( 'amp_skip_post', '__return_true' ); + + // Check that available_callback works. + add_theme_support( 'amp', array( + 'template_dir' => 'amp-templates', + 'available_callback' => 'is_singular', + ) ); + query_posts( array( 'p' => $post_id ) ); // phpcs:ignore + $this->assertTrue( is_singular() ); + $this->assertTrue( AMP_Theme_Support::is_paired_available() ); + + query_posts( array( 's' => $post_id ) ); // phpcs:ignore + $this->assertTrue( is_search() ); + $this->assertFalse( AMP_Theme_Support::is_paired_available() ); + } +}