From bcae8ade3f85a5b5c788d56f4ea6f08017676c50 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Apr 2022 18:22:50 +0200 Subject: [PATCH] Revert "Revert "Restrict dynamic blocks to `public` post status only"" This reverts commit 79991ae46274b07cb77971f96c41c7ddd551e00d. --- .../API/JSON/v1/AutomatedLatestContent.php | 34 ++++++++++++------- mailpoet/lib/Newsletter/BlockPostQuery.php | 20 +++++++---- .../Blocks/AutomatedLatestContentBlock.php | 1 + 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/mailpoet/lib/API/JSON/v1/AutomatedLatestContent.php b/mailpoet/lib/API/JSON/v1/AutomatedLatestContent.php index f2332c11a5..e4aebdcdb9 100644 --- a/mailpoet/lib/API/JSON/v1/AutomatedLatestContent.php +++ b/mailpoet/lib/API/JSON/v1/AutomatedLatestContent.php @@ -3,6 +3,7 @@ namespace MailPoet\API\JSON\v1; use MailPoet\API\JSON\Endpoint as APIEndpoint; +use MailPoet\API\JSON\SuccessResponse; use MailPoet\Config\AccessControl; use MailPoet\Newsletter\AutomatedLatestContent as ALC; use MailPoet\Newsletter\BlockPostQuery; @@ -77,29 +78,28 @@ class AutomatedLatestContent extends APIEndpoint { } /** - * @param \WP_Post[] $posts - * @return \WP_Post[] + * Fetches posts for Posts static block */ - private function getPermittedPosts($posts) { - return array_filter($posts, function ($post) { - return $this->permissionHelper->checkReadPermission($post); - }); - } - - public function getPosts($data = []) { + public function getPosts(array $data = []): SuccessResponse { return $this->successResponse( - $this->getPermittedPosts($this->ALC->getPosts(new BlockPostQuery(['args' => $data]))) + $this->getPermittedPosts($this->ALC->getPosts(new BlockPostQuery(['args' => $data, 'dynamic' => false]))) ); } - public function getTransformedPosts($data = []) { + /** + * Fetches products for Abandoned Cart Content dynamic block + */ + public function getTransformedPosts(array $data = []): SuccessResponse { $posts = $this->getPermittedPosts($this->ALC->getPosts(new BlockPostQuery(['args' => $data]))); return $this->successResponse( $this->ALC->transformPosts($data, $posts) ); } - public function getBulkTransformedPosts($data = []) { + /** + * Fetches different post types for ALC dynamic block + */ + public function getBulkTransformedPosts(array $data = []): SuccessResponse { $usedPosts = []; $renderedPosts = []; @@ -115,4 +115,14 @@ class AutomatedLatestContent extends APIEndpoint { return $this->successResponse($renderedPosts); } + + /** + * @param \WP_Post[] $posts + * @return \WP_Post[] + */ + private function getPermittedPosts($posts) { + return array_filter($posts, function ($post) { + return $this->permissionHelper->checkReadPermission($post); + }); + } } diff --git a/mailpoet/lib/Newsletter/BlockPostQuery.php b/mailpoet/lib/Newsletter/BlockPostQuery.php index 032fa8d43e..9083bf5069 100644 --- a/mailpoet/lib/Newsletter/BlockPostQuery.php +++ b/mailpoet/lib/Newsletter/BlockPostQuery.php @@ -29,12 +29,20 @@ class BlockPostQuery { public $newsletterId = false; /*** - * Translates to \WP_Query::date_query => ['column' => 'post_date', 'after' => {date string}] + * Translates to \WP_Query::date_query => array{'column': 'post_date', 'after': date string} * * @var bool|DateTimeInterface|null */ public $newerThanTimestamp = false; + /** + * If it's a dynamic block + * Dynamic blocks are not allowed to query none-public posts + * + * @var bool + */ + public $dynamic = true; + /** * @param array{ * args?: array{ @@ -51,6 +59,7 @@ class BlockPostQuery { * postsToExclude?: int[], * newsletterId?: int|false|null, * newerThanTimestamp?: bool|DateTimeInterface|null, + * dynamic?: bool, * } $query * @return void */ @@ -61,18 +70,17 @@ class BlockPostQuery { $this->postsToExclude = $query['postsToExclude'] ?? []; $this->newsletterId = $query['newsletterId'] ?? false; $this->newerThanTimestamp = $query['newerThanTimestamp'] ?? false; + $this->dynamic = $query['dynamic'] ?? true; } public function getPostType(): string { return $this->args['contentType'] ?? 'post'; } - /** - * Returns post status - * - * @return string - */ public function getPostStatus(): string { + if ($this->dynamic) { + return 'publish'; + } return $this->args['postStatus'] ?? 'publish'; } diff --git a/mailpoet/lib/Newsletter/Renderer/Blocks/AutomatedLatestContentBlock.php b/mailpoet/lib/Newsletter/Renderer/Blocks/AutomatedLatestContentBlock.php index faaaf5286c..601a95027f 100644 --- a/mailpoet/lib/Newsletter/Renderer/Blocks/AutomatedLatestContentBlock.php +++ b/mailpoet/lib/Newsletter/Renderer/Blocks/AutomatedLatestContentBlock.php @@ -53,6 +53,7 @@ class AutomatedLatestContentBlock { 'postsToExclude' => $postsToExclude, 'newsletterId' => $newsletterId, 'newerThanTimestamp' => $newerThanTimestamp, + 'dynamic' => true, ]); $aLCPosts = $this->ALC->getPosts($query); foreach ($aLCPosts as $post) {