Editor
2 months ago
Embed
1 month ago
Links
2 months ago
Listing
9 months ago
Options
2 years ago
Preview
1 month ago
Renderer
5 days ago
RestApi
1 week ago
Scheduler
5 days ago
Segment
1 year ago
Sending
1 week ago
Sharing
1 month ago
Shortcodes
4 days ago
Statistics
1 week ago
ViewInBrowser
1 month ago
ApiDataSanitizer.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmailsRepository.php
3 years ago
BlockPostQuery.php
2 months ago
BulkActionController.php
2 months ago
BulkActionException.php
2 months ago
DynamicProducts.php
11 months ago
NewsletterCoupon.php
2 months ago
NewsletterDeleteController.php
2 years ago
NewsletterHtmlSanitizer.php
2 years ago
NewsletterPostsRepository.php
2 years ago
NewsletterResendController.php
5 days ago
NewsletterSaveController.php
1 month ago
NewsletterValidator.php
1 year ago
NewslettersRepository.php
2 weeks ago
StatusController.php
2 months ago
Url.php
2 months ago
index.php
3 years ago
BlockPostQuery.php
200 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use DateTimeInterface; |
| 9 | |
| 10 | class BlockPostQuery { |
| 11 | const DEFAULT_POSTS_PER_PAGE = 10; |
| 12 | |
| 13 | /** |
| 14 | * @var array{ |
| 15 | * amount?: int, |
| 16 | * offset?: int, |
| 17 | * posts?: int[], |
| 18 | * contentType?: string, |
| 19 | * postStatus?: string, |
| 20 | * search?: string, |
| 21 | * sortBy?: 'newest' | 'DESC' | 'ASC', |
| 22 | * terms?: array{'taxonomy': string, 'id': int}[], |
| 23 | * inclusionType?: 'include'|'exclude', |
| 24 | * excludeOutOfStock?: bool, |
| 25 | * } $args |
| 26 | */ |
| 27 | public $args = []; |
| 28 | |
| 29 | /*** @var null|int[] \WP_Query::post__not_in */ |
| 30 | public $postsToExclude = []; |
| 31 | |
| 32 | /** @var int|false */ |
| 33 | public $newsletterId = false; |
| 34 | |
| 35 | /*** |
| 36 | * Translates to \WP_Query::date_query => array{'column': 'post_date_gmt', 'after': date string} |
| 37 | * |
| 38 | * @var bool|DateTimeInterface|null |
| 39 | */ |
| 40 | public $newerThanTimestamp = false; |
| 41 | |
| 42 | /** |
| 43 | * If it's a dynamic block |
| 44 | * Dynamic blocks are not allowed to query none-public posts |
| 45 | * |
| 46 | * @var bool |
| 47 | */ |
| 48 | public $dynamic = true; |
| 49 | |
| 50 | /** @var int[] product IDs to include */ |
| 51 | public $includeProductIds = []; |
| 52 | |
| 53 | /** |
| 54 | * @param array{ |
| 55 | * args?: array{ |
| 56 | * amount?: int, |
| 57 | * offset?: int, |
| 58 | * posts?: int[], |
| 59 | * contentType?: string, |
| 60 | * postStatus?: string, |
| 61 | * search?: string, |
| 62 | * sortBy?: 'newest' | 'DESC' | 'ASC', |
| 63 | * terms?: array{'taxonomy': string, 'id': int}[], |
| 64 | * inclusionType?: 'include'|'exclude', |
| 65 | * excludeOutOfStock?: bool, |
| 66 | * }, |
| 67 | * postsToExclude?: int[], |
| 68 | * newsletterId?: int|false|null, |
| 69 | * newerThanTimestamp?: bool|DateTimeInterface|null, |
| 70 | * dynamic?: bool, |
| 71 | * includeProductIds?: int[], |
| 72 | * } $query |
| 73 | * @return void |
| 74 | */ |
| 75 | public function __construct( |
| 76 | array $query = [] |
| 77 | ) { |
| 78 | $this->args = $query['args'] ?? []; |
| 79 | $this->postsToExclude = $query['postsToExclude'] ?? []; |
| 80 | $this->newsletterId = $query['newsletterId'] ?? false; |
| 81 | $this->newerThanTimestamp = $query['newerThanTimestamp'] ?? false; |
| 82 | $this->dynamic = $query['dynamic'] ?? true; |
| 83 | $this->includeProductIds = $query['includeProductIds'] ?? []; |
| 84 | } |
| 85 | |
| 86 | public function getPostType(): string { |
| 87 | return $this->args['contentType'] ?? 'post'; |
| 88 | } |
| 89 | |
| 90 | public function getPostStatus(): string { |
| 91 | if ($this->dynamic) { |
| 92 | return 'publish'; |
| 93 | } |
| 94 | return $this->args['postStatus'] ?? 'publish'; |
| 95 | } |
| 96 | |
| 97 | public function getOrder(): string { |
| 98 | return isset($this->args['sortBy']) && in_array($this->args['sortBy'], ['newest', 'DESC']) ? 'DESC' : 'ASC'; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @see https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters |
| 103 | * @return array[] array{relation: string, taxonomy: string, field: string, terms: int/string/array, operator: string} |
| 104 | */ |
| 105 | private function constructTaxonomiesQuery(): array { |
| 106 | $taxonomiesQuery = []; |
| 107 | if (isset($this->args['terms']) && is_array($this->args['terms'])) { |
| 108 | $taxonomies = []; |
| 109 | // Categorize terms based on their taxonomies |
| 110 | foreach ($this->args['terms'] as $term) { |
| 111 | $taxonomy = $term['taxonomy']; |
| 112 | if (!isset($taxonomies[$taxonomy])) { |
| 113 | $taxonomies[$taxonomy] = []; |
| 114 | } |
| 115 | $taxonomies[$taxonomy][] = $term['id']; |
| 116 | } |
| 117 | |
| 118 | foreach ($taxonomies as $taxonomy => $terms) { |
| 119 | $tax = [ |
| 120 | 'taxonomy' => $taxonomy, |
| 121 | 'field' => 'id', |
| 122 | 'terms' => $terms, |
| 123 | ]; |
| 124 | if (isset($this->args['inclusionType']) && $this->args['inclusionType'] === 'exclude') $tax['operator'] = 'NOT IN'; |
| 125 | $taxonomiesQuery[] = $tax; |
| 126 | } |
| 127 | if (!empty($taxonomiesQuery)) { |
| 128 | // With exclusion we want to use 'AND', because we want posts that |
| 129 | // don't have excluded tags/categories. But with inclusion we want to |
| 130 | // use 'OR', because we want posts that have any of the included |
| 131 | // tags/categories |
| 132 | $taxonomiesQuery['relation'] = (isset($this->args['inclusionType']) && $this->args['inclusionType'] === 'exclude') |
| 133 | ? 'AND' |
| 134 | : 'OR'; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // make $taxonomies_query nested to avoid conflicts with plugins that use taxonomies |
| 139 | return empty($taxonomiesQuery) ? [] : [$taxonomiesQuery]; |
| 140 | } |
| 141 | |
| 142 | public function getQueryParams(): array { |
| 143 | $postsPerPage = (!empty($this->args['amount']) && (int)$this->args['amount'] > 0) |
| 144 | ? (int)$this->args['amount'] |
| 145 | : self::DEFAULT_POSTS_PER_PAGE; |
| 146 | $parameters = [ |
| 147 | 'posts_per_page' => $postsPerPage, |
| 148 | 'post_type' => $this->getPostType(), |
| 149 | 'post_status' => $this->getPostStatus(), |
| 150 | 'orderby' => 'date', |
| 151 | 'order' => $this->getOrder(), |
| 152 | ]; |
| 153 | if (!empty($this->args['offset']) && (int)$this->args['offset'] > 0) { |
| 154 | $parameters['offset'] = (int)$this->args['offset']; |
| 155 | } |
| 156 | if (isset($this->args['search'])) { |
| 157 | $parameters['s'] = $this->args['search']; |
| 158 | } |
| 159 | if (isset($this->args['posts']) && is_array($this->args['posts'])) { |
| 160 | $parameters['post__in'] = $this->args['posts']; |
| 161 | $parameters['posts_per_page'] = -1; // Get all posts with matching IDs |
| 162 | } |
| 163 | if (!empty($this->postsToExclude)) { |
| 164 | $parameters['post__not_in'] = $this->postsToExclude; |
| 165 | } |
| 166 | |
| 167 | // If specific product IDs are provided, override post__in |
| 168 | if (!empty($this->includeProductIds)) { |
| 169 | $parameters['post__in'] = $this->includeProductIds; |
| 170 | // Keep the posts_per_page limit to ensure we don't return too many products |
| 171 | } |
| 172 | |
| 173 | // WP posts with the type attachment have always post_status `inherit` |
| 174 | if ($parameters['post_type'] === 'attachment' && $parameters['post_status'] === 'publish') { |
| 175 | $parameters['post_status'] = 'inherit'; |
| 176 | } |
| 177 | |
| 178 | // This enables using posts query filters for get_posts, where by default |
| 179 | // it is disabled. |
| 180 | // However, it also enables other plugins and themes to hook in and alter |
| 181 | // the query. |
| 182 | $parameters['suppress_filters'] = false; |
| 183 | |
| 184 | if ($this->newerThanTimestamp instanceof DateTimeInterface) { |
| 185 | //Ensure UTC timezone |
| 186 | $after = new \DateTime('now', new \DateTimeZone('UTC')); |
| 187 | $after->setTimestamp($this->newerThanTimestamp->getTimestamp()); |
| 188 | $parameters['date_query'] = [ |
| 189 | [ |
| 190 | 'column' => 'post_date_gmt', |
| 191 | 'after' => $after->format('Y-m-d H:i:s'), |
| 192 | ], |
| 193 | ]; |
| 194 | } |
| 195 | |
| 196 | $parameters['tax_query'] = $this->constructTaxonomiesQuery(); |
| 197 | return $parameters; |
| 198 | } |
| 199 | } |
| 200 |