From bf82b8d027929d61870195831cdc12b7474ae41c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 7 Mar 2018 22:55:21 -0800 Subject: [PATCH] Un-trash validation post status to re-use instead of adding new post with duplicated name --- includes/utils/class-amp-validation-utils.php | 6 ++++ tests/test-class-amp-validation-utils.php | 31 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/includes/utils/class-amp-validation-utils.php b/includes/utils/class-amp-validation-utils.php index f90b5200f2f..548c78e08c4 100644 --- a/includes/utils/class-amp-validation-utils.php +++ b/includes/utils/class-amp-validation-utils.php @@ -1066,7 +1066,13 @@ public static function store_validation_errors( $validation_errors, $url ) { // If there already exists a post for the given validation errors, just amend the $url to the existing post. $post_for_other_url = get_page_by_path( $post_name, OBJECT, self::POST_TYPE_SLUG ); + if ( ! $post_for_other_url ) { + $post_for_other_url = get_page_by_path( $post_name . '__trashed', OBJECT, self::POST_TYPE_SLUG ); + } if ( $post_for_other_url ) { + if ( 'trash' === $post_for_other_url->post_status ) { + wp_untrash_post( $post_for_other_url->ID ); + } if ( ! in_array( $url, get_post_meta( $post_for_other_url->ID, self::AMP_URL_META, false ), true ) ) { add_post_meta( $post_for_other_url->ID, self::AMP_URL_META, wp_slash( $url ), false ); } diff --git a/tests/test-class-amp-validation-utils.php b/tests/test-class-amp-validation-utils.php index ad08ccc2c65..942a0144a65 100644 --- a/tests/test-class-amp-validation-utils.php +++ b/tests/test-class-amp-validation-utils.php @@ -624,7 +624,7 @@ public function test_register_post_type() { * @covers AMP_Validation_Utils::store_validation_errors() */ public function test_store_validation_errors() { - global $post, $wp; + global $post; $post = $this->factory()->post->create_and_get(); // WPCS: global override ok. add_theme_support( 'amp' ); AMP_Validation_Utils::process_markup( '' . $this->disallowed_tag . '' ); @@ -703,6 +703,35 @@ public function test_store_validation_errors() { remove_theme_support( 'amp' ); } + /** + * Test for store_validation_errors() when existing post is trashed. + * + * @covers AMP_Validation_Utils::store_validation_errors() + */ + public function test_store_validation_errors_untrashing() { + $validation_errors = $this->get_mock_errors(); + + $first_post_id = AMP_Validation_Utils::store_validation_errors( $validation_errors, home_url( '/foo/' ) ); + $this->assertInternalType( 'int', $first_post_id ); + + $post_name = get_post( $first_post_id )->post_name; + wp_trash_post( $first_post_id ); + $this->assertEquals( $post_name . '__trashed', get_post( $first_post_id )->post_name ); + + $next_post_id = AMP_Validation_Utils::store_validation_errors( $validation_errors, home_url( '/bar/' ) ); + $this->assertInternalType( 'int', $next_post_id ); + $this->assertEquals( $post_name, get_post( $next_post_id )->post_name ); + $this->assertEquals( $next_post_id, $first_post_id ); + + $this->assertEqualSets( + array( + home_url( '/foo/' ), + home_url( '/bar/' ), + ), + get_post_meta( $next_post_id, AMP_Validation_Utils::AMP_URL_META, false ) + ); + } + /** * Test for get_validation_status_post(). *