Skip to content

Commit

Permalink
[Merl 1256] Bug fix. Generate Autosave is missing from Post/Page Prev…
Browse files Browse the repository at this point in the history
…iews (#1644)

* Fix: (faustwp) Fix autosave in preview links.

* Chore: Changeset

* Chore: Update deps

* Security: Fix Axios Advisory

* WIP

* Add rest_prepare_post and rest_prepare_page callbacks to fix autoupdate.

* Lint: PHPCS update

* Lint: Revert phpcs update

* Testing: Attempt to update codeception.

* WIP: Revert last commit

* Build: Revert package-lock and composer.lock

* Tests: Add unit tests for filters.

* Update ReplacementCallbacksTests.php

* Chore: Revert composer.json

* WIP: Debug testing CI/CD

* WIP: Debugging CI/CD

* WIP: CI/CD debug

* WIP: Debug CI/CD

* WIP: Cleanup risky test
  • Loading branch information
theodesp authored Nov 27, 2023
1 parent 42cbe14 commit 2559958
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-cooks-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/wordpress-plugin': patch
---

Bug Fix: Fixed missing call to autosave when using Post/Page previews.
12 changes: 6 additions & 6 deletions plugins/faustwp/includes/graphql/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'] ) {
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 15 additions & 13 deletions plugins/faustwp/includes/replacement/previewlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
};
}
Expand All @@ -34,23 +32,27 @@ 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',
),
};
}
}

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);
}
}
Expand Down
18 changes: 17 additions & 1 deletion plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand All @@ -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' ) );
}
Expand Down

0 comments on commit 2559958

Please sign in to comment.