Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
markkelnar committed Aug 18, 2023
2 parents 7fd742d + b13e132 commit acbed08
Show file tree
Hide file tree
Showing 6 changed files with 559 additions and 80 deletions.
39 changes: 19 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 55 additions & 9 deletions src/Admin/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Editor {
public function admin_init() {
add_filter( 'wp_insert_post_data', [ $this, 'validate_and_pre_save_cb' ], 10, 2 );
add_action( sprintf( 'save_post_%s', Document::TYPE_NAME ), [ $this, 'save_document_cb' ], 10, 3 );
add_action( 'untrashed_post', [ $this, 'untrashed_post_cb' ], 10, 2 );

// Enable excerpts for the persisted query post type for the wp admin editor
add_post_type_support( Document::TYPE_NAME, 'excerpt' );
Expand All @@ -35,6 +36,25 @@ public function admin_init() {
add_filter( 'wp_editor_settings', [ $this, 'wp_editor_settings' ], 10, 2 );
}

/**
* Fires after a post is restored from the Trash.
*
* @param int $post_id Post ID.
* @param string $previous_status The status of the post at the point where it was trashed.
* @return void
*/
public function untrashed_post_cb( $post_id, $previous_status ) {
// If have errors when validating the post content/data, do not show those in the admin when untrash.
$untrashed_post = get_post( $post_id );

// Bail if the untrashed post is not a GraphQL Document
if ( ! isset( $untrashed_post->post_type ) || Document::TYPE_NAME !== $untrashed_post->post_type ) {
return;
}

delete_transient( AdminErrors::TRANSIENT_NAME );
}

/**
* If existing post is edited, verify query string in content is valid graphql
*
Expand All @@ -47,22 +67,44 @@ public function validate_and_pre_save_cb( $data, $post ) {
return $data;
}

if ( 'trash' === $post['post_status'] ) {
return $data;
}

$document = new Document();

try {
if ( array_key_exists( 'post_content', $post ) && 'publish' === $post['post_status'] ) {
$data['post_content'] = $document->valid_or_throw( $post['post_content'], $post['ID'] );
// Check for empty post_content when publishing the query and throw
if ( 'publish' === $post['post_status'] && empty( $post['post_content'] ) ) {
throw new RequestError( __( 'Query string is empty', 'wp-graphql-smart-cache' ) );
}

$data['post_content'] = $document->valid_or_throw( $post['post_content'], $post['ID'] );

} catch ( RequestError $e ) {
AdminErrors::add_message( $e->getMessage() );

// Overwrite new/invalid query with previous working query, or empty
$existing_post = get_post( $post['ID'] );
if ( $existing_post && property_exists( $existing_post, 'post_content' ) ) {
try {
$data['post_content'] = $document->valid_or_throw( $existing_post->post_content, $post['ID'] );
} catch ( RequestError $e ) {
$data['post_content'] = '';
// If encountered invalid data when publishing query, revert some data. If draft, allow invalid query.
if ( 'publish' === $post['post_status'] ) {

// If has an existing published post and trying to publish with errors, bail before save_post
$existing_post = get_post( $post['ID'], ARRAY_A );
if ( $existing_post && 'publish' === $existing_post['post_status'] ) {

wp_safe_redirect( admin_url( sprintf( '/post.php?post=%d&action=edit', $post['ID'] ) ) );
exit;

} else {
$data['post_status'] = 'draft';

// This prevents the Admin UI from showing that the post has previously been published (because it actually hasn't been)
if ( isset( $existing_post['post_date'] ) && isset( $existing_post['post_date_gmt'] ) && '0000-00-00 00:00:00' !== $existing_post['post_date_gmt'] ) {
$data['post_date'] = $existing_post['post_date'];
$data['post_date_gmt'] = get_gmt_from_date( $existing_post['post_date'] );
} else {
// Clearing this is same as removing the publish date.
$data['post_date_gmt'] = '0000-00-00 00:00:00';
}
}
}
}
Expand Down Expand Up @@ -129,6 +171,10 @@ public function save_document_cb( $post_id, $post, $update ) {
return;
}

if ( 'trash' === $post->post_status ) {
return;
}

if ( ! $this->is_valid_form( $post_id ) ) {
return;
}
Expand Down
19 changes: 19 additions & 0 deletions src/AdminErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AdminErrors {
*/
public function init() {
add_action( 'admin_notices', [ $this, 'display_validation_messages' ] );
add_filter( 'post_updated_messages', [ $this, 'post_updated_messages_cb' ] );
}

/**
Expand Down Expand Up @@ -63,4 +64,22 @@ public function display_validation_messages() {

delete_transient( self::TRANSIENT_NAME );
}

/**
* Filters the post updated messages.
*
* @param array[] $messages Post updated messages.
* @return array[]
*/
public function post_updated_messages_cb( $messages ) {
// If have admin error message, don't display the 'Post Published' message for this post type
$error_messages = get_transient( self::TRANSIENT_NAME );
if ( ! empty( $error_messages ) ) {
// phpcs:ignore
$message_number = isset( $_GET['message'] ) ? absint( $_GET['message'] ) : 0;
$messages[ Document::TYPE_NAME ][ $message_number ] = '';
}

return $messages;
}
}
86 changes: 50 additions & 36 deletions tests/acceptance/QueryIdCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,56 @@

class QueryIdCest
{
public function queryIdThatDoesNotExistTest(AcceptanceTester $I)
{
$I->sendGet('graphql', [ 'queryId' => '1234' ] );
$I->seeResponseContainsJson([
'errors' => [
'message' => 'PersistedQueryNotFound'
]
]);
}

public function saveQueryAndHashTest(AcceptanceTester $I)
{
$I->sendPost('graphql', [
'query' => '{__typename}',
'queryId' => '8d8f7365e9e86fa8e3313fcaf2131b801eafe9549de22373089cf27511858b39'
] );
$I->seeResponseContainsJson([
'data' => [
'__typename' => 'RootQuery'
]
]);
public function queryIdThatDoesNotExistTest(AcceptanceTester $I)
{
$I->sendGet('graphql', [ 'queryId' => '1234' ] );
$I->seeResponseContainsJson([
'errors' => [
'message' => 'PersistedQueryNotFound'
]
]);
}

// If send empty string and a query id that does not exist, will get not found error
public function sendEmptyQueryStringWithQueryIdIsErrorTest(AcceptanceTester $I)
{
$I->sendPost('graphql', [
'query' => '',
'queryId' => 'alias-does-not-exist'
] );
$I->seeResponseContainsJson([
'errors' => [
'message' => 'PersistedQueryNotFound'
]
]);
}

public function saveQueryAndHashTest(AcceptanceTester $I)
{
$I->sendPost('graphql', [
'query' => '{__typename}',
'queryId' => '8d8f7365e9e86fa8e3313fcaf2131b801eafe9549de22373089cf27511858b39'
] );
$I->seeResponseContainsJson([
'data' => [
'__typename' => 'RootQuery'
]
]);

$I->sendPost('graphql', [
'query' => '{ __typename }',
'extensions' => [
"persistedQuery" => [
"version" => 1,
"sha256Hash" => "8d8f7365e9e86fa8e3313fcaf2131b801eafe9549de22373089cf27511858b39"
]
]
] );
$I->seeResponseContainsJson([
'data' => [
'__typename' => 'RootQuery'
]
]);
}
$I->sendPost('graphql', [
'query' => '{ __typename }',
'extensions' => [
"persistedQuery" => [
"version" => 1,
"sha256Hash" => "8d8f7365e9e86fa8e3313fcaf2131b801eafe9549de22373089cf27511858b39"
]
]
] );
$I->seeResponseContainsJson([
'data' => [
'__typename' => 'RootQuery'
]
]);
}

}
Loading

0 comments on commit acbed08

Please sign in to comment.