Skip to content
This repository has been archived by the owner on Dec 9, 2023. It is now read-only.

Commit

Permalink
Un-trash validation post status to re-use instead of adding new post …
Browse files Browse the repository at this point in the history
…with duplicated name
  • Loading branch information
westonruter committed Mar 8, 2018
1 parent da5f4ad commit bf82b8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions includes/utils/class-amp-validation-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
31 changes: 30 additions & 1 deletion tests/test-class-amp-validation-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<!--amp-source-stack:plugin:foo-->' . $this->disallowed_tag . '<!--/amp-source-stack:plugin:foo-->' );
Expand Down Expand Up @@ -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().
*
Expand Down

0 comments on commit bf82b8d

Please sign in to comment.