Skip to content

Commit

Permalink
Merge pull request #41 from crowdfavorite/bug/unauthenticated-preview
Browse files Browse the repository at this point in the history
enable previews for unauthenticated users
  • Loading branch information
al-esc authored Jul 26, 2024
2 parents aa50c81 + b02e65c commit 036f5ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
23 changes: 8 additions & 15 deletions app/PccSyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function fetchAndStoreDocument(
/**
* Get PccClient instance.
*
* @param string|null $pccGrant
* @return PccClient
*/
public function pccClient(string $pccGrant = null): PccClient
Expand Down Expand Up @@ -168,7 +169,13 @@ public function unPublishPostByDocumentId($documentId)
]);
}

public function preaprePreviewingURL(string $documentId, $postId = null)
/**
* Get preview link.
* @param string $documentId
* @param $postId
* @return string
*/
public function preparePreviewingURL(string $documentId, $postId = null): string
{
$postId = $postId ?: $this->findExistingConnectedPost($documentId);
return add_query_arg(
Expand All @@ -180,18 +187,4 @@ public function preaprePreviewingURL(string $documentId, $postId = null)
get_permalink($postId)
);
}

/**
* Get preview content from PCC.
*
* @param string $documentId
* @param string $pccGrant
* @return Article
*/
public function getPreviewContent(string $documentId, string $pccGrant)
{
$articleApi = new ArticlesApi($this->pccClient($pccGrant));

return $articleApi->getArticleById($documentId, [], PublishingLevel::REALTIME);
}
}
30 changes: 20 additions & 10 deletions app/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function wp_enqueue_script;

use const PCC_HANDLE;
use const PCC_INTEGRATION_POST_TYPE_OPTION_KEY;
use const PCC_PLUGIN_DIR;
use const PCC_PLUGIN_DIR_URL;

Expand Down Expand Up @@ -101,8 +102,10 @@ public function allowStyleTags($allowedTags)

/**
* Publish documents from Google Docs.
*
* @return void
*/
public function publishDocuments()
public function publishDocuments(): void
{
global $wp;
if (!str_starts_with($wp->request, static::PCC_PUBLISH_DOCUMENT_ENDPOINT)) {
Expand All @@ -127,17 +130,27 @@ public function publishDocuments()
if (
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
isset($_GET['pccGrant']) && isset($_GET['publishingLevel']) &&
PublishingLevel::REALTIME->value === $_GET['publishingLevel'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
PublishingLevel::REALTIME->value === $_GET['publishingLevel']
) {
$parts = explode('/', $wp->request);
$documentId = end($parts);
$pcc = new PccSyncManager();
$postId = $pcc->findExistingConnectedPost($documentId);
if (!$postId) {
$postId = $pcc->fetchAndStoreDocument($documentId, PublishingLevel::REALTIME, true);

if (!$pcc->findExistingConnectedPost($documentId)) {
$pcc->fetchAndStoreDocument($documentId, PublishingLevel::REALTIME, true);
}

$url = $pcc->preaprePreviewingURL($documentId, $postId);
$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;
Expand Down Expand Up @@ -205,17 +218,14 @@ private function buildEditDocumentURL(string $documentId): string
*/
public function addPreviewContainer(string $content): string
{
global $post;
$documentId = get_post_meta($post->ID, PCC_CONTENT_META_KEY, true);
// phpcs:disable
if (
isset($_GET['preview'], $_GET['document_id'], $_GET['publishing_level']) &&
'google_document' === $_GET['preview'] &&
$_GET['document_id'] === $documentId &&
$_GET['publishing_level'] === PublishingLevel::REALTIME->value
) {
// phpcs:enable
$content = '<div id="pcc-content-preview"></div>';
return '<div id="pcc-content-preview"></div>';
}

return $content;
Expand Down

0 comments on commit 036f5ed

Please sign in to comment.