Editor
9 months ago
Links
2 years ago
Listing
7 months ago
Options
2 years ago
Preview
3 months ago
Renderer
3 months ago
Scheduler
1 year ago
Segment
11 months ago
Sending
1 year ago
Shortcodes
6 months ago
Statistics
1 year ago
ViewInBrowser
9 months ago
ApiDataSanitizer.php
3 years ago
AutomatedLatestContent.php
1 year ago
AutomaticEmailsRepository.php
3 years ago
BlockPostQuery.php
9 months ago
DynamicProducts.php
9 months ago
NewsletterCoupon.php
3 years ago
NewsletterDeleteController.php
2 years ago
NewsletterHtmlSanitizer.php
2 years ago
NewsletterPostsRepository.php
2 years ago
NewsletterSaveController.php
3 months ago
NewsletterValidator.php
1 year ago
NewslettersRepository.php
6 months ago
Url.php
3 years ago
index.php
3 years ago
DynamicProducts.php
191 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Logging\LoggerFactory; |
| 9 | use MailPoet\Newsletter\Editor\Transformer; |
| 10 | use MailPoet\WooCommerce\Helper as WCHelper; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class DynamicProducts { |
| 14 | |
| 15 | /** @var LoggerFactory */ |
| 16 | private $loggerFactory; |
| 17 | |
| 18 | /** @var int|false */ |
| 19 | private $newsletterId; |
| 20 | |
| 21 | /** @var NewsletterPostsRepository */ |
| 22 | private $newsletterPostsRepository; |
| 23 | |
| 24 | /** @var WPFunctions */ |
| 25 | private $wp; |
| 26 | |
| 27 | /** @var WCHelper */ |
| 28 | private $wcHelper; |
| 29 | |
| 30 | public function __construct( |
| 31 | LoggerFactory $loggerFactory, |
| 32 | NewsletterPostsRepository $newsletterPostsRepository, |
| 33 | WPFunctions $wp, |
| 34 | WCHelper $wcHelper |
| 35 | ) { |
| 36 | $this->loggerFactory = $loggerFactory; |
| 37 | $this->newsletterPostsRepository = $newsletterPostsRepository; |
| 38 | $this->wp = $wp; |
| 39 | $this->wcHelper = $wcHelper; |
| 40 | } |
| 41 | |
| 42 | public function filterOutSentPosts(string $where): string { |
| 43 | $newsletterPostsTableName = $this->newsletterPostsRepository->getTableName(); |
| 44 | $sentPostsQuery = 'SELECT ' . $newsletterPostsTableName . '.post_id FROM ' |
| 45 | . $newsletterPostsTableName . ' WHERE ' |
| 46 | . $newsletterPostsTableName . ".newsletter_id='" . $this->newsletterId . "'"; |
| 47 | |
| 48 | $wherePostUnsent = 'ID NOT IN (' . $sentPostsQuery . ')'; |
| 49 | |
| 50 | if (!empty($where)) $wherePostUnsent = ' AND ' . $wherePostUnsent; |
| 51 | |
| 52 | return $where . $wherePostUnsent; |
| 53 | } |
| 54 | |
| 55 | public function ensureConsistentQueryType(\WP_Query $query) { |
| 56 | // Queries with taxonomies are autodetected as 'is_archive=true' and 'is_home=false' |
| 57 | // while queries without them end up being 'is_archive=false' and 'is_home=true'. |
| 58 | // This is to fix that by always enforcing constistent behavior. |
| 59 | $query->is_archive = true; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 60 | $query->is_home = false; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 61 | } |
| 62 | |
| 63 | public function getPosts(BlockPostQuery $query) { |
| 64 | $this->newsletterId = $query->newsletterId; |
| 65 | // Get posts as logged out user, so private posts hidden by other plugins (e.g. UAM) are also excluded |
| 66 | $currentUserId = $this->wp->getCurrentUserId(); |
| 67 | // phpcs:ignore Generic.PHP.ForbiddenFunctions.Discouraged |
| 68 | wp_set_current_user(0); |
| 69 | |
| 70 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_POST_NOTIFICATIONS)->info( |
| 71 | 'loading dynamic products', |
| 72 | [ |
| 73 | 'args' => $query->args, |
| 74 | 'posts_to_exclude' => $query->postsToExclude, |
| 75 | 'newsletter_id' => $query->newsletterId, |
| 76 | 'newer_than_timestamp' => $query->newerThanTimestamp, |
| 77 | 'include_product_ids' => $query->includeProductIds, |
| 78 | ] |
| 79 | ); |
| 80 | |
| 81 | // set low priority to execute 'ensureConstistentQueryType' before any other filter |
| 82 | $filterPriority = defined('PHP_INT_MIN') ? constant('PHP_INT_MIN') : ~PHP_INT_MAX; |
| 83 | $this->wp->addAction('pre_get_posts', [$this, 'ensureConsistentQueryType'], $filterPriority); |
| 84 | $this->_attachSentPostsFilter($query->newsletterId); |
| 85 | $parameters = $query->getQueryParams(); |
| 86 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_POST_NOTIFICATIONS)->info( |
| 87 | 'getting dynamic products', |
| 88 | ['parameters' => $parameters] |
| 89 | ); |
| 90 | |
| 91 | // Convert WP_Query parameters to WC_Product_Query parameters |
| 92 | $wcArgs = [ |
| 93 | 'limit' => $parameters['posts_per_page'] ?? -1, |
| 94 | 'orderby' => $parameters['orderby'] ?? 'date', |
| 95 | 'order' => $parameters['order'] ?? 'DESC', |
| 96 | 'exclude' => $query->postsToExclude, // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude |
| 97 | ]; |
| 98 | |
| 99 | // Exclude out-of-stock and products that are not on backorder |
| 100 | $excludeOutOfStock = $query->args['excludeOutOfStock'] ?? false; |
| 101 | if ($excludeOutOfStock === true || $excludeOutOfStock === 'true') { |
| 102 | $wcArgs['stock_status'] = $this->wp->applyFilters('mailpoet_products_exclude_out_of_stock_stock_status', ['instock', 'lowstock', 'onbackorder']); |
| 103 | } |
| 104 | |
| 105 | // If we have specific product IDs to include, use them |
| 106 | if (!empty($parameters['post__in'])) { |
| 107 | $wcArgs['include'] = $parameters['post__in']; |
| 108 | } |
| 109 | |
| 110 | // WooCommerce Product Query does not support 'any' status, |
| 111 | // so we need to handle it manually |
| 112 | $postStatus = $parameters['post_status'] ?? 'publish'; |
| 113 | // Default to published products only |
| 114 | $wcArgs['status'] = 'publish'; |
| 115 | |
| 116 | // Store original user capabilities for later permission filtering |
| 117 | $canEditPrivate = $this->wp->userCan($currentUserId, 'edit_private_posts'); |
| 118 | $canEditOthers = $this->wp->userCan($currentUserId, 'edit_others_posts'); |
| 119 | $isAdmin = $this->wp->userCan($currentUserId, 'manage_options'); |
| 120 | |
| 121 | if (!empty($parameters['tax_query'])) { |
| 122 | $wcArgs['tax_query'] = $parameters['tax_query']; |
| 123 | } |
| 124 | |
| 125 | if (!empty($parameters['date_query'])) { |
| 126 | $wcArgs['date_query'] = $parameters['date_query']; |
| 127 | } |
| 128 | |
| 129 | // Fetch published products (safe for everyone) |
| 130 | $products = $this->wcHelper->wcGetProducts($wcArgs); |
| 131 | |
| 132 | // For privileged users, fetch additional product statuses separately and merge |
| 133 | if ($postStatus === 'any' && ($canEditPrivate || $isAdmin)) { |
| 134 | // Editors get private products |
| 135 | if ($canEditPrivate) { |
| 136 | $privateArgs = $wcArgs; |
| 137 | $privateArgs['status'] = 'private'; |
| 138 | $privateProducts = $this->wcHelper->wcGetProducts($privateArgs); |
| 139 | $products = array_merge($products, $privateProducts); |
| 140 | } |
| 141 | |
| 142 | // Admins get draft and pending products too |
| 143 | if ($isAdmin) { |
| 144 | $draftArgs = $wcArgs; |
| 145 | $draftArgs['status'] = ['draft', 'pending']; |
| 146 | $draftProducts = $this->wcHelper->wcGetProducts($draftArgs); |
| 147 | $products = array_merge($products, $draftProducts); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | $this->logPosts($products); |
| 152 | |
| 153 | $this->wp->removeAction('pre_get_posts', [$this, 'ensureConsistentQueryType'], $filterPriority); |
| 154 | $this->_detachSentPostsFilter($query->newsletterId); |
| 155 | // phpcs:ignore Generic.PHP.ForbiddenFunctions.Discouraged |
| 156 | wp_set_current_user($currentUserId); |
| 157 | return $products; |
| 158 | } |
| 159 | |
| 160 | public function transformPosts($args, $posts) { |
| 161 | $transformer = new Transformer($args); |
| 162 | return $transformer->transform($posts); |
| 163 | } |
| 164 | |
| 165 | private function _attachSentPostsFilter($newsletterId) { |
| 166 | if ($newsletterId > 0) { |
| 167 | $this->wp->addAction('posts_where', [$this, 'filterOutSentPosts']); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private function _detachSentPostsFilter($newsletterId) { |
| 172 | if ($newsletterId > 0) { |
| 173 | $this->wp->removeAction('posts_where', [$this, 'filterOutSentPosts']); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private function logPosts(array $posts) { |
| 178 | $postsToLog = []; |
| 179 | foreach ($posts as $post) { |
| 180 | $postsToLog[] = [ |
| 181 | 'id' => $post->get_id(), |
| 182 | 'post_date' => $post->get_date_created()->format('Y-m-d H:i:s'), // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 183 | ]; |
| 184 | } |
| 185 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_POST_NOTIFICATIONS)->info( |
| 186 | 'dynamic products loaded posts', |
| 187 | ['posts' => $postsToLog] |
| 188 | ); |
| 189 | } |
| 190 | } |
| 191 |