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

Add REST_REQUEST check. #192

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 1.1.3

- The uri field was being overwritten during GraphQL requests, resulting in post uri's that included the content sync URL.
- Some logic attempting to choose the correct manifest ID instead of regenerating it was causing manifest id's to be outdated during previews.

## 1.1.2

- Fixed redirection to Gatsby Cloud Content Sync preview loader in Gutenberg
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: Gatsby, GatsbyJS, JavaScript, JAMStack, Static Site generator, GraphQL, He
Requires at least: 5.4.2
Tested up to: 5.6
Requires PHP: 7.3
Stable tag: 1.1.2
Stable tag: 1.1.3
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Expand Down
4 changes: 4 additions & 0 deletions src/ActionMonitor/Monitors/PostMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function callback_transition_post_status( $new_status, $old_status, WP_Po
return;
}

if(defined("REST_REQUEST") && REST_REQUEST) {
DerthVedder marked this conversation as resolved.
Show resolved Hide resolved
return;
}

// If the object is not a valid post, ignore it
if ( ! is_a( $post, 'WP_Post' ) ) {
return;
Expand Down
77 changes: 8 additions & 69 deletions src/Admin/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ function() {
);

$use_cloud_loader = self::get_setting( 'use_gatsby_content_sync' );

if ( $use_cloud_loader === 'on' ) {

if (
$use_cloud_loader === 'on' &&
// don't do this during graphql requests
// because that could override the post URI
// in Gatsby! meaning the content sync url could be added to the page path in Gatsby if pages are created from the uri.
! ( defined( 'GRAPHQL_REQUEST' ) && true === GRAPHQL_REQUEST )
) {
add_filter( 'preview_post_link', function( $link, $post ) {
return self::get_gatsby_content_sync_url_for_post( $post );
}, 10, 2 );
Expand All @@ -33,73 +39,6 @@ function() {
}

public static function get_preview_manifest_id_for_post( $post ) {
$graphql_single_name =
get_post_type_object( $post->post_type )
->graphql_single_name;

if ( !$graphql_single_name || $graphql_single_name === "" ) {
// if we don't have a graphql single name
// Gatsby can't use this post anyway.
// No need to return a manifest
return null;
}

$action_monitor_posts = new \WP_Query( [
'post_type' => 'action_monitor',
'post_status' => 'any',
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'gatsby_action_ref_node_dbid',
'field' => 'name',
'terms' => sanitize_text_field( $post->ID ),
],
[
'taxonomy' => 'gatsby_action_ref_node_type',
'field' => 'name',
'terms' => sanitize_text_field( $graphql_single_name ),
],
[
'taxonomy' => 'gatsby_action_stream_type',
'field' => 'name',
'terms' => 'PREVIEW',
]
],
] );

if (
isset( $action_monitor_posts->posts )
&& ! empty( $action_monitor_posts->posts )
) {
$action_monitor_post_id = $action_monitor_posts->posts[0];
$referenced_node_preview_data = get_post_meta(
$action_monitor_post_id,
'_gatsby_preview_data',
true
);

$preview_data = false;

if (
$referenced_node_preview_data
&& $referenced_node_preview_data !== ""
) {
$preview_data = json_decode( $referenced_node_preview_data );
}

if (
$preview_data
&& property_exists( $preview_data, 'manifestIds' )
&& count( $preview_data->manifestIds ) > 0
) {
return $preview_data->manifestIds[0];
}
}

// if the above doesn't return a value we generate a new manifest ID from the post_modified date and post db id
$revision = self::getPreviewablePostObjectByPostId( $post->ID );
$revision_modified = $revision->post_modified ?? null;

Expand Down
4 changes: 2 additions & 2 deletions wp-gatsby.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: WP Gatsby
* Description: Optimize your WordPress site to be a source for Gatsby sites.
* Version: 1.1.2
* Version: 1.1.3
* Author: GatsbyJS, Jason Bahl, Tyler Barnes
* Author URI: https://gatsbyjs.org
* Text Domain: wp-gatsby
Expand Down Expand Up @@ -103,7 +103,7 @@ private function setup_constants()
{
// Plugin version.
if (! defined('WPGATSBY_VERSION') ) {
define('WPGATSBY_VERSION', '1.1.2');
define('WPGATSBY_VERSION', '1.1.3');
}

// Plugin Folder Path.
Expand Down