From 503f4bc4e05f43e993ad0cf8d1c0a811c519c1f4 Mon Sep 17 00:00:00 2001 From: Pierre Gordon Date: Thu, 7 Nov 2019 01:59:37 -0500 Subject: [PATCH 1/4] Prevent password protection from disabling AMP --- includes/class-amp-post-type-support.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/includes/class-amp-post-type-support.php b/includes/class-amp-post-type-support.php index 5c77b0b2acb..7e9e0cbe5de 100644 --- a/includes/class-amp-post-type-support.php +++ b/includes/class-amp-post-type-support.php @@ -88,8 +88,9 @@ public static function get_support_errors( $post ) { $errors[] = 'post-type-support'; } - if ( post_password_required( $post ) ) { - $errors[] = 'password-protected'; + // Show password form instead of the post content. + if ( ! current_theme_supports( 'amp' ) && post_password_required( $post ) ) { + add_filter( 'the_content', [ __CLASS__, 'show_password_form' ] ); } /** @@ -153,4 +154,15 @@ public static function get_support_errors( $post ) { } return $errors; } + + /** + * Show the password form if the post password is required. + * + * @return string + */ + public static function show_password_form() { + $post = get_post(); + + return get_the_password_form( $post ); + } } From f7b1dad3971c01ddcad3f0e083c674b899fe342b Mon Sep 17 00:00:00 2001 From: Pierre Gordon Date: Thu, 7 Nov 2019 02:08:40 -0500 Subject: [PATCH 2/4] Add tests --- tests/php/test-class-amp-post-type-support.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/php/test-class-amp-post-type-support.php b/tests/php/test-class-amp-post-type-support.php index de9d58d03d9..cc2c48eea5e 100644 --- a/tests/php/test-class-amp-post-type-support.php +++ b/tests/php/test-class-amp-post-type-support.php @@ -106,7 +106,8 @@ public function test_get_support_error() { // Password-protected. add_filter( 'post_password_required', '__return_true' ); - $this->assertEquals( [ 'password-protected' ], AMP_Post_Type_Support::get_support_errors( $book_id ) ); + $this->assertEmpty( AMP_Post_Type_Support::get_support_errors( $book_id ) ); + $this->assertEquals( 10, has_filter( 'the_content', [ 'AMP_Post_Type_Support', 'show_password_form' ] ) ); remove_filter( 'post_password_required', '__return_true' ); $this->assertEmpty( AMP_Post_Type_Support::get_support_errors( $book_id ) ); @@ -116,4 +117,17 @@ public function test_get_support_error() { remove_filter( 'amp_skip_post', '__return_true' ); $this->assertEmpty( AMP_Post_Type_Support::get_support_errors( $book_id ) ); } + + /** + * Test AMP_Post_Type_Support::show_password_form. + * + * @covers AMP_Post_Type_Support::show_password_form() + */ + public function test_show_password_form() { + $post = self::factory()->post->create_and_get(); + + add_filter( 'post_password_required', '__return_true' ); + AMP_Post_Type_Support::get_support_errors( $post ); + $this->assertEquals( get_the_password_form( $post ), get_the_content( null, false, $post ) ); + } } From 4d7eafa1befce11298e89eb998332611c8aca490 Mon Sep 17 00:00:00 2001 From: Pierre Gordon Date: Thu, 7 Nov 2019 02:54:54 -0500 Subject: [PATCH 3/4] Add post to global scope --- tests/php/test-class-amp-post-type-support.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/php/test-class-amp-post-type-support.php b/tests/php/test-class-amp-post-type-support.php index cc2c48eea5e..f19a9391701 100644 --- a/tests/php/test-class-amp-post-type-support.php +++ b/tests/php/test-class-amp-post-type-support.php @@ -124,10 +124,10 @@ public function test_get_support_error() { * @covers AMP_Post_Type_Support::show_password_form() */ public function test_show_password_form() { - $post = self::factory()->post->create_and_get(); + $GLOBALS['post'] = self::factory()->post->create_and_get(); add_filter( 'post_password_required', '__return_true' ); - AMP_Post_Type_Support::get_support_errors( $post ); - $this->assertEquals( get_the_password_form( $post ), get_the_content( null, false, $post ) ); + AMP_Post_Type_Support::get_support_errors( $GLOBALS['post'] ); + $this->assertEquals( get_the_password_form( $GLOBALS['post'] ), get_the_content( null, false, $GLOBALS['post'] ) ); } } From 8519e4ba46a79b1795f58ac3b5ac89864b74aede Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 7 Nov 2019 22:42:08 -0800 Subject: [PATCH 4/4] Simplify presentation of password form in Reader mode --- includes/class-amp-post-type-support.php | 16 --------------- .../templates/class-amp-post-template.php | 2 +- .../php/test-class-amp-post-type-support.php | 20 ------------------- 3 files changed, 1 insertion(+), 37 deletions(-) diff --git a/includes/class-amp-post-type-support.php b/includes/class-amp-post-type-support.php index 7e9e0cbe5de..36036771792 100644 --- a/includes/class-amp-post-type-support.php +++ b/includes/class-amp-post-type-support.php @@ -88,11 +88,6 @@ public static function get_support_errors( $post ) { $errors[] = 'post-type-support'; } - // Show password form instead of the post content. - if ( ! current_theme_supports( 'amp' ) && post_password_required( $post ) ) { - add_filter( 'the_content', [ __CLASS__, 'show_password_form' ] ); - } - /** * Filters whether to skip the post from AMP. * @@ -154,15 +149,4 @@ public static function get_support_errors( $post ) { } return $errors; } - - /** - * Show the password form if the post password is required. - * - * @return string - */ - public static function show_password_form() { - $post = get_post(); - - return get_the_password_form( $post ); - } } diff --git a/includes/templates/class-amp-post-template.php b/includes/templates/class-amp-post-template.php index a366bc98405..28e33c5e655 100644 --- a/includes/templates/class-amp-post-template.php +++ b/includes/templates/class-amp-post-template.php @@ -322,7 +322,7 @@ private function build_post_comments_data() { */ private function build_post_content() { $amp_content = new AMP_Content( - $this->post->post_content, + post_password_required( $this->post ) ? get_the_password_form( $this->post ) : $this->post->post_content, amp_get_content_embed_handlers( $this->post ), amp_get_content_sanitizers( $this->post ), [ diff --git a/tests/php/test-class-amp-post-type-support.php b/tests/php/test-class-amp-post-type-support.php index f19a9391701..c1609bd3573 100644 --- a/tests/php/test-class-amp-post-type-support.php +++ b/tests/php/test-class-amp-post-type-support.php @@ -104,30 +104,10 @@ public function test_get_support_error() { add_post_type_support( 'book', AMP_Post_Type_Support::SLUG ); $this->assertEmpty( AMP_Post_Type_Support::get_support_errors( $book_id ) ); - // Password-protected. - add_filter( 'post_password_required', '__return_true' ); - $this->assertEmpty( AMP_Post_Type_Support::get_support_errors( $book_id ) ); - $this->assertEquals( 10, has_filter( 'the_content', [ 'AMP_Post_Type_Support', 'show_password_form' ] ) ); - remove_filter( 'post_password_required', '__return_true' ); - $this->assertEmpty( AMP_Post_Type_Support::get_support_errors( $book_id ) ); - // Skip-post. add_filter( 'amp_skip_post', '__return_true' ); $this->assertEquals( [ 'skip-post' ], AMP_Post_Type_Support::get_support_errors( $book_id ) ); remove_filter( 'amp_skip_post', '__return_true' ); $this->assertEmpty( AMP_Post_Type_Support::get_support_errors( $book_id ) ); } - - /** - * Test AMP_Post_Type_Support::show_password_form. - * - * @covers AMP_Post_Type_Support::show_password_form() - */ - public function test_show_password_form() { - $GLOBALS['post'] = self::factory()->post->create_and_get(); - - add_filter( 'post_password_required', '__return_true' ); - AMP_Post_Type_Support::get_support_errors( $GLOBALS['post'] ); - $this->assertEquals( get_the_password_form( $GLOBALS['post'] ), get_the_content( null, false, $GLOBALS['post'] ) ); - } }