From 1ea9eecfb013cedc56e4db26409d22aefaae21bd Mon Sep 17 00:00:00 2001 From: Moustafa Gouda Date: Mon, 29 Jul 2024 16:47:22 +0300 Subject: [PATCH] Put save exist to avoid any fatal error while fetching posts --- app/Settings.php | 90 +++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/app/Settings.php b/app/Settings.php index 612f10d..feabedb 100644 --- a/app/Settings.php +++ b/app/Settings.php @@ -137,52 +137,56 @@ public function publishDocuments(): void return; } - $PCCManager = new PccSyncManager(); - // Publish document - // phpcs:ignore WordPress.Security.NonceVerification.Recommended - if ( - isset($_GET['publishingLevel']) && - PublishingLevel::PRODUCTION->value === $_GET['publishingLevel'] && - $PCCManager->isPCCConfigured() - ) { - $parts = explode('/', $wp->request); - $documentId = end($parts); - $pcc = new PccSyncManager(); - $postId = $pcc->fetchAndStoreDocument($documentId, PublishingLevel::PRODUCTION); - - wp_redirect(get_permalink($postId)); - exit; - } - - // Preview document - if ( + try { + $PCCManager = new PccSyncManager(); + // Publish document // phpcs:ignore WordPress.Security.NonceVerification.Recommended - isset($_GET['pccGrant']) && isset($_GET['publishingLevel']) && - // phpcs:ignore WordPress.Security.NonceVerification.Recommended - PublishingLevel::REALTIME->value === $_GET['publishingLevel'] && - $PCCManager->isPCCConfigured() - ) { - $parts = explode('/', $wp->request); - $documentId = end($parts); - $pcc = new PccSyncManager(); - - if (!$pcc->findExistingConnectedPost($documentId)) { - $pcc->fetchAndStoreDocument($documentId, PublishingLevel::REALTIME, true); + if ( + isset($_GET['publishingLevel']) && + PublishingLevel::PRODUCTION->value === $_GET['publishingLevel'] && + $PCCManager->isPCCConfigured() + ) { + $parts = explode('/', $wp->request); + $documentId = end($parts); + $pcc = new PccSyncManager(); + $postId = $pcc->fetchAndStoreDocument($documentId, PublishingLevel::PRODUCTION); + + wp_redirect(get_permalink($postId)); + exit; } - $query = get_posts([ - 'post_type' => get_option(PCC_INTEGRATION_POST_TYPE_OPTION_KEY, 'post'), - 'post_status' => 'publish', - 'posts_per_page' => 1, - 'orderby' => 'date', - 'order' => 'ASC', - 'fields' => 'ids' - ]); - - $url = $pcc->preparePreviewingURL($documentId, $query[0] ?? 0); - - wp_redirect($url); - exit; + // Preview document + if ( + // phpcs:ignore WordPress.Security.NonceVerification.Recommended + isset($_GET['pccGrant']) && isset($_GET['publishingLevel']) && + // phpcs:ignore WordPress.Security.NonceVerification.Recommended + PublishingLevel::REALTIME->value === $_GET['publishingLevel'] && + $PCCManager->isPCCConfigured() + ) { + $parts = explode('/', $wp->request); + $documentId = end($parts); + $pcc = new PccSyncManager(); + + if (!$pcc->findExistingConnectedPost($documentId)) { + $pcc->fetchAndStoreDocument($documentId, PublishingLevel::REALTIME, true); + } + + $query = get_posts([ + 'post_type' => get_option(PCC_INTEGRATION_POST_TYPE_OPTION_KEY, 'post'), + 'post_status' => 'publish', + 'posts_per_page' => 1, + 'orderby' => 'date', + 'order' => 'ASC', + 'fields' => 'ids' + ]); + + $url = $pcc->preparePreviewingURL($documentId, $query[0] ?? 0); + + wp_redirect($url); + exit; + } + } catch (\Exception $ex) { + // No Action needed for safe exit } }