diff --git a/includes/validation/class-amp-validation-manager.php b/includes/validation/class-amp-validation-manager.php index ac0da4705c3..9900ee389ce 100644 --- a/includes/validation/class-amp-validation-manager.php +++ b/includes/validation/class-amp-validation-manager.php @@ -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 ) @@ -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 ) { diff --git a/tests/validation/test-class-amp-validation-manager.php b/tests/validation/test-class-amp-validation-manager.php index 1b7457035ff..b978eb43707 100644 --- a/tests/validation/test-class-amp-validation-manager.php +++ b/tests/validation/test-class-amp-validation-manager.php @@ -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() { @@ -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'] ); } /**