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

Handle errors when editing queries in admin editor #244

Merged
115 changes: 57 additions & 58 deletions composer.lock

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

27 changes: 18 additions & 9 deletions src/Admin/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,28 @@ public function validate_and_pre_save_cb( $data, $post ) {
$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';
markkelnar marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
Loading
Loading