Skip to content
Open
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
45 changes: 45 additions & 0 deletions src/ui/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/UI/Block_Editor_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.
*
Expand Down
72 changes: 72 additions & 0 deletions tests/WP/UI/Block_Editor_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Yoast\WP\Duplicate_Post\Tests\WP\UI;

use WP_REST_Request;
use WP_REST_Response;
use Yoast\WP\Duplicate_Post\Permissions_Helper;
use Yoast\WP\Duplicate_Post\UI\Asset_Manager;
use Yoast\WP\Duplicate_Post\UI\Block_Editor;
use Yoast\WP\Duplicate_Post\UI\Link_Builder;
use Yoast\WPTestUtils\WPIntegration\TestCase;

/**
* Integration tests for the Block_Editor class.
*/
final class Block_Editor_Test extends TestCase {

/**
* Tests removing the permalink fields from a Rewrite & Republish response.
*
* @covers \Yoast\WP\Duplicate_Post\UI\Block_Editor::remove_rewrite_republish_permalink
*
* @return void
*/
public function test_remove_rewrite_republish_permalink() {
$copy = self::factory()->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 );
}
}