Skip to content

Commit 2421625

Browse files
committed
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
1 parent f90cab9 commit 2421625

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

plugins/faustwp/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"lint": "parallel-lint -e php --no-colors --exclude vendor .",
3535
"phpcs": "phpcs",
3636
"phpcs:fix": "phpcbf",
37-
"phpstan": "phpstan analyze --ansi --memory-limit=1G",
37+
"phpstan": "phpstan --ansi --memory-limit=1G",
3838
"suite": [
3939
"@lint",
4040
"@phpcs",

plugins/faustwp/includes/replacement/callbacks.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,43 @@ 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+
341+
add_filter( 'rest_post_dispatch', __NAMESPACE__ . '\\rest_post_dispatch', 10, 3 );
342+
343+
/**
344+
* Adds the preview link to rest responses.
345+
*
346+
* @param WP_REST_Response $response The rest response object.
347+
* @param WP_Post $post Post object.
348+
*
349+
* @return WP_REST_Response The rest response object.
350+
*/
351+
function rest_post_dispatch( $response, $post ) {
352+
353+
if ( isset( $post->post_status ) && 'draft' === $post->post_status ) {
354+
$response->data['link'] = get_preview_post_link( $post->ID );
355+
}
356+
357+
return $response;
358+
}
359+
360+
326361
/**
327362
* Adds the preview link to rest responses.
328363
*

0 commit comments

Comments
 (0)