Skip to content

Commit

Permalink
Merge pull request #1424 from Automattic/fix/classic-editor-post-save…
Browse files Browse the repository at this point in the history
…-validation

Ensure only the post being edited in the classic editor is queued for validation
  • Loading branch information
westonruter authored Sep 13, 2018
2 parents 1992dbd + db04fe7 commit f7b8112
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
23 changes: 22 additions & 1 deletion includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,32 @@ class_exists( 'WP_Block_Type_Registry' )
/**
* Handle save_post action to queue re-validation of the post on the frontend.
*
* This is intended to only apply to post edits made in the classic editor.
*
* @see AMP_Validation_Manager::get_amp_validity_rest_field() The method responsible for validation post changes via Gutenberg.
* @see AMP_Validation_Manager::validate_queued_posts_on_frontend()
*
* @param int $post_id Post ID.
*/
public static function handle_save_post_prompting_validation( $post_id ) {
global $pagenow;
$post = get_post( $post_id );

$is_classic_editor_post_save = (
isset( $_SERVER['REQUEST_METHOD'] )
&&
'POST' === $_SERVER['REQUEST_METHOD']
&&
'post.php' === $pagenow
&&
isset( $_POST['post_ID'] ) // WPCS: csrf ok.
&&
intval( $_POST['post_ID'] ) === (int) $post_id // WPCS: csrf ok.
);

$should_validate_post = (
$is_classic_editor_post_save
&&
is_post_type_viewable( $post->post_type )
&&
! wp_is_post_autosave( $post )
Expand Down Expand Up @@ -531,7 +549,10 @@ function( $post ) {

$validation_posts = array();

// @todo Only validate the first and then queue the rest in WP Cron?
/*
* It is unlikely that there will be more than one post in the array.
* For the bulk recheck action, see AMP_Invalid_URL_Post_Type::handle_bulk_action().
*/
foreach ( $posts as $post ) {
$url = amp_get_permalink( $post->ID );
if ( ! $url ) {
Expand Down
18 changes: 16 additions & 2 deletions tests/validation/test-class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,31 @@ public function test_add_validation_error_sourcing_gutenberg() {
* @covers AMP_Validation_Manager::validate_queued_posts_on_frontend()
*/
public function test_handle_save_post_prompting_validation_and_validate_queued_posts_on_frontend() {
$_SERVER['REQUEST_METHOD'] = 'POST';
$GLOBALS['pagenow'] = 'post.php'; // WPCS: override ok.

register_post_type( 'secret', array( 'public' => false ) );
$secret = $this->factory()->post->create_and_get( array( 'post_type' => 'secret' ) );
$secret = $this->factory()->post->create_and_get( array( 'post_type' => 'secret' ) );
$_POST['post_ID'] = $secret->ID;
AMP_Validation_Manager::handle_save_post_prompting_validation( $secret->ID );
$this->assertFalse( has_action( 'shutdown', array( 'AMP_Validation_Manager', 'validate_queued_posts_on_frontend' ) ) );
$this->assertEmpty( AMP_Validation_Manager::validate_queued_posts_on_frontend() );

$auto_draft = $this->factory()->post->create_and_get( array( 'post_status' => 'auto-draft' ) );
$auto_draft = $this->factory()->post->create_and_get( array( 'post_status' => 'auto-draft' ) );
$_POST['post_ID'] = $auto_draft->ID;
AMP_Validation_Manager::handle_save_post_prompting_validation( $auto_draft->ID );
$this->assertFalse( has_action( 'shutdown', array( 'AMP_Validation_Manager', 'validate_queued_posts_on_frontend' ) ) );
$this->assertEmpty( AMP_Validation_Manager::validate_queued_posts_on_frontend() );

// Testing without $_POST context.
$post = $this->factory()->post->create_and_get( array( 'post_type' => 'post' ) );
AMP_Validation_Manager::handle_save_post_prompting_validation( $post->ID );
$this->assertFalse( has_action( 'shutdown', array( 'AMP_Validation_Manager', 'validate_queued_posts_on_frontend' ) ) );

// Test success.
$post = $this->factory()->post->create_and_get( array( 'post_type' => 'post' ) );
$_POST['post_ID'] = $post->ID;
AMP_Validation_Manager::handle_save_post_prompting_validation( $post->ID );
$this->assertEquals( 10, has_action( 'shutdown', array( 'AMP_Validation_Manager', 'validate_queued_posts_on_frontend' ) ) );

add_filter( 'pre_http_request', function() {
Expand All @@ -314,6 +326,8 @@ public function test_handle_save_post_prompting_validation_and_validate_queued_p
$results = AMP_Validation_Manager::validate_queued_posts_on_frontend();
$this->assertArrayHasKey( $post->ID, $results );
$this->assertInstanceOf( 'WP_Error', $results[ $post->ID ] );

unset( $GLOBALS['pagenow'] );
}

/**
Expand Down

0 comments on commit f7b8112

Please sign in to comment.