diff --git a/src/ui/block-editor.php b/src/ui/block-editor.php index 749b6209b..ae5c7b8fe 100644 --- a/src/ui/block-editor.php +++ b/src/ui/block-editor.php @@ -3,6 +3,8 @@ namespace Yoast\WP\Duplicate_Post\UI; use WP_Post; +use WP_REST_Request; +use WP_REST_Response; use Yoast\WP\Duplicate_Post\Permissions_Helper; use Yoast\WP\Duplicate_Post\Utils; @@ -55,9 +57,52 @@ public function register_hooks() { \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_elementor_script' ], 9 ); \add_action( 'admin_enqueue_scripts', [ $this, 'should_previously_used_keyword_assessment_run' ], 9 ); \add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_scripts' ] ); + \add_action( 'rest_api_init', [ $this, 'register_rest_filters' ] ); \add_filter( 'wpseo_link_suggestions_indexables', [ $this, 'remove_original_from_wpseo_link_suggestions' ], 10, 3 ); } + /** + * Registers REST API filters for the post types exposed through the REST API. + * + * @return void + */ + public function register_rest_filters() { + $post_types = \get_post_types( [ 'show_in_rest' => true ] ); + + foreach ( $post_types as $post_type ) { + \add_filter( "rest_prepare_{$post_type}", [ $this, 'remove_rewrite_republish_permalink' ], 10, 3 ); + } + } + + /** + * Removes the unused permalink fields from a Rewrite & Republish copy. + * + * The copy's slug is never carried over to the original on republish, and + * the block editor hides the URL row when no permalink template is set. + * + * @param WP_REST_Response $response The REST response. + * @param WP_Post $post The post being prepared. + * @param WP_REST_Request $request The REST request. + * + * @return WP_REST_Response The filtered REST response. + */ + public function remove_rewrite_republish_permalink( $response, $post, $request ) { + if ( + ! $response instanceof WP_REST_Response + || ! $post instanceof WP_Post + || $request['context'] !== 'edit' + || ! $this->permissions_helper->is_rewrite_and_republish_copy( $post ) + ) { + return $response; + } + + $data = $response->get_data(); + unset( $data['permalink_template'], $data['generated_slug'] ); + $response->set_data( $data ); + + return $response; + } + /** * Enqueues the necessary Elementor script for the current post. * diff --git a/tests/Unit/UI/Block_Editor_Test.php b/tests/Unit/UI/Block_Editor_Test.php index b4620d06e..761f886f3 100644 --- a/tests/Unit/UI/Block_Editor_Test.php +++ b/tests/Unit/UI/Block_Editor_Test.php @@ -139,6 +139,17 @@ public function test_register_hooks() { 'Does not have expected enqueue_block_editor_assets action', ); + $this->assertNotFalse( + \has_action( + 'rest_api_init', + [ + $this->instance, + 'register_rest_filters', + ], + ), + 'Does not have expected rest_api_init action', + ); + $this->assertNotFalse( \has_filter( 'wpseo_link_suggestions_indexables', @@ -151,6 +162,28 @@ public function test_register_hooks() { ); } + /** + * Tests registration of REST response filters for REST-enabled post types. + * + * @covers \Yoast\WP\Duplicate_Post\UI\Block_Editor::register_rest_filters + * + * @return void + */ + public function test_register_rest_filters() { + Monkey\Functions\expect( '\\get_post_types' ) + ->with( [ 'show_in_rest' => true ] ) + ->andReturn( [ 'post', 'book' ] ); + + $this->instance->register_rest_filters(); + + $this->assertNotFalse( + \has_filter( 'rest_prepare_post', [ $this->instance, 'remove_rewrite_republish_permalink' ] ), + ); + $this->assertNotFalse( + \has_filter( 'rest_prepare_book', [ $this->instance, 'remove_rewrite_republish_permalink' ] ), + ); + } + /** * Tests the should_previously_used_keyword_assessment_run function. * diff --git a/tests/WP/UI/Block_Editor_Test.php b/tests/WP/UI/Block_Editor_Test.php new file mode 100644 index 000000000..5b9825166 --- /dev/null +++ b/tests/WP/UI/Block_Editor_Test.php @@ -0,0 +1,72 @@ +post->create_and_get(); + \update_post_meta( $copy->ID, '_dp_is_rewrite_republish_copy', 1 ); + + $block_editor = new Block_Editor( new Link_Builder(), new Permissions_Helper(), new Asset_Manager() ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $copy->ID ); + $request->set_param( 'context', 'edit' ); + $response = new WP_REST_Response( + [ + 'permalink_template' => 'https://example.com/%postname%/', + 'generated_slug' => $copy->post_name, + 'title' => [ 'raw' => $copy->post_title ], + ], + ); + + $filtered_response = $block_editor->remove_rewrite_republish_permalink( $response, $copy, $request ); + $data = $filtered_response->get_data(); + + $this->assertArrayNotHasKey( 'permalink_template', $data ); + $this->assertArrayNotHasKey( 'generated_slug', $data ); + $this->assertSame( [ 'raw' => $copy->post_title ], $data['title'] ); + } + + /** + * Tests that normal REST responses retain their permalink fields. + * + * @covers \Yoast\WP\Duplicate_Post\UI\Block_Editor::remove_rewrite_republish_permalink + * + * @return void + */ + public function test_remove_rewrite_republish_permalink_does_not_change_normal_post() { + $post = self::factory()->post->create_and_get(); + $block_editor = new Block_Editor( new Link_Builder(), new Permissions_Helper(), new Asset_Manager() ); + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post->ID ); + $request->set_param( 'context', 'edit' ); + $response = new WP_REST_Response( + [ + 'permalink_template' => 'https://example.com/%postname%/', + 'generated_slug' => $post->post_name, + ], + ); + + $data = $block_editor->remove_rewrite_republish_permalink( $response, $post, $request )->get_data(); + + $this->assertArrayHasKey( 'permalink_template', $data ); + $this->assertArrayHasKey( 'generated_slug', $data ); + } +}