diff --git a/.changeset/smooth-cooks-warn.md b/.changeset/smooth-cooks-warn.md new file mode 100644 index 000000000..907c5fe71 --- /dev/null +++ b/.changeset/smooth-cooks-warn.md @@ -0,0 +1,5 @@ +--- +'@faustwp/wordpress-plugin': patch +--- + +Bug Fix: Fixed missing call to autosave when using Post/Page previews. diff --git a/plugins/faustwp/includes/graphql/callbacks.php b/plugins/faustwp/includes/graphql/callbacks.php index 0cc956d8e..7c0cf1fdc 100644 --- a/plugins/faustwp/includes/graphql/callbacks.php +++ b/plugins/faustwp/includes/graphql/callbacks.php @@ -7,10 +7,10 @@ namespace WPE\FaustWP\GraphQL; -use function WPE\FaustWP\Auth\generate_authorization_code; -use function WPE\FaustWP\Settings\get_secret_key; use GraphQL\Type\Definition\ResolveInfo; use WPGraphQL\AppContext; +use function WPE\FaustWP\Auth\generate_authorization_code; +use function WPE\FaustWP\Settings\get_secret_key; if ( ! defined( 'ABSPATH' ) ) { exit; @@ -64,7 +64,7 @@ function filter_introspection( $value, $default_value, $option_name, $section_fi // check header for faust secret key. if ( ! isset( $_SERVER['HTTP_X_FAUST_SECRET'] ) ) { return $value; - }; + } $secret_key = get_secret_key(); if ( $secret_key !== $_SERVER['HTTP_X_FAUST_SECRET'] ) { @@ -92,7 +92,7 @@ function register_faust_toolbar_field() { 'shouldShowFaustToolbar', array( 'type' => 'Boolean', - 'resolve' => function() { + 'resolve' => function () { $user = wp_get_current_user(); $toolbar_preference_meta = get_user_meta( $user->ID, 'show_admin_bar_front', true ); @@ -149,7 +149,7 @@ function register_global_stylesheet_field() { ), ), 'description' => __( 'Returns the stylesheet resulting of merging core, theme, and user data.', 'faustwp' ), - 'resolve' => function( $root, $args, $context, $info ) { + 'resolve' => function ( $root, $args, $context, $info ) { $types = $args['types'] ?? null; return wp_get_global_stylesheet( $types ); @@ -339,7 +339,7 @@ function register_generate_ac_mutation() { 'description' => __( 'Error encountered during user authentication, if any', 'faustwp' ), ), ), - 'mutateAndGetPayload' => function( $input, $context, $info ) { + 'mutateAndGetPayload' => function ( $input, $context, $info ) { $is_email = isset( $input['email'] ) ? true : false; $username = isset( $input['username'] ) ? $input['username'] : null; $email = isset( $input['email'] ) ? $input['email'] : null; diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php index efbf33315..6f72e8857 100644 --- a/plugins/faustwp/includes/replacement/callbacks.php +++ b/plugins/faustwp/includes/replacement/callbacks.php @@ -304,6 +304,24 @@ function enqueue_preview_scripts() { ); } +add_filter( 'rest_prepare_post', __NAMESPACE__ . '\\preview_link_in_rest_response', 10, 2 ); +add_filter( 'rest_prepare_page', __NAMESPACE__ . '\\preview_link_in_rest_response', 10, 2 ); +/** + * Adds the preview link to rest responses. + * + * @param WP_REST_Response $response The rest response object. + * @param WP_Post $post Post object. + * + * @return string URL used for the post preview. + */ +function preview_link_in_rest_response( $response, $post ) { + if ( 'draft' === $post->post_status ) { + $response->data['link'] = get_preview_post_link( $post->ID ); + } + + return $response; +} + add_filter( 'wp_sitemaps_posts_entry', __NAMESPACE__ . '\\sitemaps_posts_entry' ); /** * Filters the sitemap entry for an individual post. diff --git a/plugins/faustwp/includes/replacement/previewlinks.js b/plugins/faustwp/includes/replacement/previewlinks.js index ac48faf44..13af970b4 100644 --- a/plugins/faustwp/includes/replacement/previewlinks.js +++ b/plugins/faustwp/includes/replacement/previewlinks.js @@ -2,7 +2,7 @@ * XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998 */ -document.addEventListener('DOMContentLoaded', function() { +document.addEventListener('DOMContentLoaded', function () { // Get the preview data via wp_localize_script const faustPreviewData = window._faustwp_preview_data; @@ -19,12 +19,10 @@ document.addEventListener('DOMContentLoaded', function() { function debounce(func, wait) { let timeout; - return function() { - const context = this; - const args = arguments; + return function (...args) { clearTimeout(timeout); - timeout = setTimeout(function() { - func.apply(context, args); + timeout = setTimeout(function () { + func(args); }, wait); }; } @@ -34,8 +32,12 @@ document.addEventListener('DOMContentLoaded', function() { switch (version) { default: return { - headerLink: document.querySelector('.edit-post-header-preview__grouping-external a'), - snackbarLink: document.querySelector('.components-snackbar__content a'), + headerLink: document.querySelector( + '.edit-post-header-preview__grouping-external a', + ), + snackbarLink: document.querySelector( + '.components-snackbar__content a', + ), }; } } @@ -43,14 +45,14 @@ document.addEventListener('DOMContentLoaded', function() { function updateUIElements() { const { headerLink, snackbarLink } = getPreviewLinksByVersion(wpVersion); - // Clone & replace the original link in order to clear pre-existing events. if (headerLink && headerLink.getAttribute('href') !== faustPreviewLink) { - const clonedHeaderLink = headerLink.cloneNode(true); - headerLink.parentNode.replaceChild(clonedHeaderLink, headerLink); - if (clonedHeaderLink) clonedHeaderLink.setAttribute('href', faustPreviewLink); + headerLink.setAttribute('href', faustPreviewLink); } - if (snackbarLink && snackbarLink.getAttribute('href') !== faustPreviewLink) { + if ( + snackbarLink && + snackbarLink.getAttribute('href') !== faustPreviewLink + ) { snackbarLink.setAttribute('href', faustPreviewLink); } } diff --git a/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php b/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php index d078cbdcd..3ccbfb7a9 100644 --- a/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php +++ b/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php @@ -10,14 +10,17 @@ use function WPE\FaustWP\Replacement\{ content_replacement, post_preview_link, + preview_link_in_rest_response, image_source_replacement, image_source_srcset_replacement, - post_link + post_link, }; use function WPE\FaustWP\Settings\faustwp_update_setting; +use WP_REST_Response; class ReplacementCallbacksTests extends \WP_UnitTestCase { protected $post_id; + protected $draft_post_id; public function setUp(): void { parent::setUp(); @@ -27,6 +30,11 @@ public function setUp(): void { 'post_content' => 'Hi', 'post_status' => 'publish', ] ); + $this->draft_post_id = wp_insert_post( [ + 'title' => 'Hello', + 'post_content' => 'Hi', + 'post_status' => 'draft', + ] ); } public function test_the_content_filter() { @@ -37,6 +45,14 @@ public function test_preview_post_link_filter() { $this->assertSame( 1000, has_action( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link' ) ); } + public function test_preview_rest_prepare_post_filter() { + $this->assertSame( 10, has_action( 'rest_prepare_post', 'WPE\FaustWP\Replacement\preview_link_in_rest_response' ) ); + } + + public function test_preview_rest_prepare_page_filter() { + $this->assertSame( 10, has_action( 'rest_prepare_page', 'WPE\FaustWP\Replacement\preview_link_in_rest_response' ) ); + } + public function test_post_link_filter() { $this->assertSame( 1000, has_action( 'post_link', 'WPE\FaustWP\Replacement\post_link' ) ); }