Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent store_validation_errors from clobbering global post when empty invalid_url_post supplied #1433

Merged
merged 1 commit into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions includes/validation/class-amp-invalid-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,11 @@ public static function get_url_from_post( $post ) {
public static function store_validation_errors( $validation_errors, $url, $args = array() ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for now but may consider splitting this function? Kind of long.

$url = remove_query_arg( amp_get_slug(), $url ); // Only ever store the canonical version.
$slug = md5( $url );
if ( isset( $args['invalid_url_post'] ) ) {
$post = null;
if ( ! empty( $args['invalid_url_post'] ) ) {
$post = get_post( $args['invalid_url_post'] );
} else {
}
if ( ! $post ) {
$post = get_page_by_path( $slug, OBJECT, self::POST_TYPE_SLUG );
if ( ! $post ) {
$post = get_page_by_path( $slug . '__trashed', OBJECT, self::POST_TYPE_SLUG );
Expand Down
7 changes: 6 additions & 1 deletion tests/validation/test-class-amp-invalid-url-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public function test_get_url_from_post() {
* @covers \AMP_Invalid_URL_Post_Type::store_validation_errors()
*/
public function test_store_validation_errors() {
global $post;
add_theme_support( 'amp', array( 'paired' => true ) );
AMP_Validation_Manager::init();
$post = $this->factory()->post->create_and_get();
Expand Down Expand Up @@ -335,9 +336,13 @@ public function test_store_validation_errors() {

$invalid_url_post_id = AMP_Invalid_URL_Post_Type::store_validation_errors(
$errors,
get_permalink( $post )
get_permalink( $post ),
array(
'invalid_url_post' => 0,
)
);
$this->assertNotInstanceOf( 'WP_Error', $invalid_url_post_id );
$this->assertNotEquals( $invalid_url_post_id, $post->ID, 'Passing an empty invalid_url_post should not re-use the global $post ever.' );

// Test resurrection from trash.
wp_trash_post( $invalid_url_post_id );
Expand Down