Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merl 1256] Bug fix. Generate Autosave is missing from Post/Page Previews #1644

Merged
merged 21 commits into from
Nov 27, 2023
Merged
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
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 @@ -300,6 +300,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 );
theodesp marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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);
Copy link
Member Author

@theodesp theodesp Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOM modifications are not allowed under a React managed DOM

Screenshot 2023-11-13 at 13 32 12

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
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
Loading