Skip to content

Commit aedd100

Browse files
authored
chore: Updated FaustWP preview link action for all draft post types (#2059)
* Updated FaustWP to create a preview link for all post types. Removed two filters and added a new one to catch all post types. Removed filters for rest_prepare_post and rest_prepare_page * Revert typo in composer file. * Removed test code. Updated tests. Removed test code. Updated test to do the action rest_api_init and then check if post and page are registered for preview links actions. * Added Changeset
1 parent f90cab9 commit aedd100

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

.changeset/honest-shirts-exist.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@faustwp/wordpress-plugin": major
3+
---
4+
5+
chore: Updated FaustWP to create a preview link for all draft post types.
6+
7+
Removed actions `rest_prepare_post` and `rest_prepare_page` from the callback functions.
8+
Added a new action for `rest_api_init` to add `rest_prepare_{$post_type}` action for all publicably queryable post types including custom post types.

plugins/faustwp/includes/replacement/callbacks.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,23 @@ function enqueue_preview_scripts() {
321321
);
322322
}
323323

324-
add_filter( 'rest_prepare_post', __NAMESPACE__ . '\\preview_link_in_rest_response', 10, 2 );
325-
add_filter( 'rest_prepare_page', __NAMESPACE__ . '\\preview_link_in_rest_response', 10, 2 );
324+
add_filter( 'rest_api_init', __NAMESPACE__ . '\\register_preview_link_hooks_for_all_draft_post_types' );
325+
326+
/**
327+
* Registers the preview link hooks for all post types.
328+
*/
329+
function register_preview_link_hooks_for_all_draft_post_types() {
330+
$post_types = get_post_types(
331+
array(
332+
'public' => true,
333+
)
334+
);
335+
336+
foreach ( $post_types as $post_type ) {
337+
add_filter( 'rest_prepare_' . $post_type, __NAMESPACE__ . '\\preview_link_in_rest_response', 10, 2 );
338+
}
339+
}
340+
326341
/**
327342
* Adds the preview link to rest responses.
328343
*

plugins/faustwp/tests/integration/ReplacementCallbacksTests.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ public function test_preview_post_link_filter() {
4848
$this->assertSame( 1000, has_action( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link' ) );
4949
}
5050

51-
public function test_preview_rest_prepare_post_filter() {
51+
public function test_rest_api_init_filter() {
52+
$this->assertSame( 10, has_action( 'rest_api_init', 'WPE\FaustWP\Replacement\register_preview_link_hooks_for_all_draft_post_types' ) );
53+
do_action( 'rest_api_init' );
5254
$this->assertSame( 10, has_action( 'rest_prepare_post', 'WPE\FaustWP\Replacement\preview_link_in_rest_response' ) );
53-
}
54-
55-
public function test_preview_rest_prepare_page_filter() {
5655
$this->assertSame( 10, has_action( 'rest_prepare_page', 'WPE\FaustWP\Replacement\preview_link_in_rest_response' ) );
5756
}
5857

0 commit comments

Comments
 (0)