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
AutomatedLatestContent.php
136 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\WP\Functions as WPFunctions; |
| 11 | |
| 12 | class AutomatedLatestContent { |
| 13 | const FILTER_POST = 'mailpoet_automated_latest_content_post'; |
| 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 | public function __construct( |
| 28 | LoggerFactory $loggerFactory, |
| 29 | NewsletterPostsRepository $newsletterPostsRepository, |
| 30 | WPFunctions $wp |
| 31 | ) { |
| 32 | $this->loggerFactory = $loggerFactory; |
| 33 | $this->newsletterPostsRepository = $newsletterPostsRepository; |
| 34 | $this->wp = $wp; |
| 35 | } |
| 36 | |
| 37 | public function filterOutSentPosts(string $where): string { |
| 38 | $newsletterPostsTableName = $this->newsletterPostsRepository->getTableName(); |
| 39 | $sentPostsQuery = 'SELECT ' . $newsletterPostsTableName . '.post_id FROM ' |
| 40 | . $newsletterPostsTableName . ' WHERE ' |
| 41 | . $newsletterPostsTableName . ".newsletter_id='" . $this->newsletterId . "'"; |
| 42 | |
| 43 | $wherePostUnsent = 'ID NOT IN (' . $sentPostsQuery . ')'; |
| 44 | |
| 45 | if (!empty($where)) $wherePostUnsent = ' AND ' . $wherePostUnsent; |
| 46 | |
| 47 | return $where . $wherePostUnsent; |
| 48 | } |
| 49 | |
| 50 | public function ensureConsistentQueryType(\WP_Query $query) { |
| 51 | // Queries with taxonomies are autodetected as 'is_archive=true' and 'is_home=false' |
| 52 | // while queries without them end up being 'is_archive=false' and 'is_home=true'. |
| 53 | // This is to fix that by always enforcing constistent behavior. |
| 54 | $query->is_archive = true; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 55 | $query->is_home = false; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 56 | } |
| 57 | |
| 58 | public function getPosts(BlockPostQuery $query) { |
| 59 | $this->newsletterId = $query->newsletterId; |
| 60 | // Get posts as logged out user, so private posts hidden by other plugins (e.g. UAM) are also excluded |
| 61 | $currentUserId = $this->wp->getCurrentUserId(); |
| 62 | // phpcs:ignore Generic.PHP.ForbiddenFunctions.Discouraged |
| 63 | wp_set_current_user(0); |
| 64 | |
| 65 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_POST_NOTIFICATIONS)->info( |
| 66 | 'loading automated latest content', |
| 67 | [ |
| 68 | 'args' => $query->args, |
| 69 | 'posts_to_exclude' => $query->postsToExclude, |
| 70 | 'newsletter_id' => $query->newsletterId, |
| 71 | 'newer_than_timestamp' => $query->newerThanTimestamp, |
| 72 | ] |
| 73 | ); |
| 74 | |
| 75 | // set low priority to execute 'ensureConstistentQueryType' before any other filter |
| 76 | $filterPriority = defined('PHP_INT_MIN') ? constant('PHP_INT_MIN') : ~PHP_INT_MAX; |
| 77 | $this->wp->addAction('pre_get_posts', [$this, 'ensureConsistentQueryType'], $filterPriority); |
| 78 | $this->_attachSentPostsFilter($query->newsletterId); |
| 79 | $parameters = $query->getQueryParams(); |
| 80 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_POST_NOTIFICATIONS)->info( |
| 81 | 'getting automated latest content', |
| 82 | ['parameters' => $parameters] |
| 83 | ); |
| 84 | $posts = $this->wp->getPosts($parameters); |
| 85 | $this->logPosts($posts); |
| 86 | |
| 87 | $this->wp->removeAction('pre_get_posts', [$this, 'ensureConsistentQueryType'], $filterPriority); |
| 88 | $this->_detachSentPostsFilter($query->newsletterId); |
| 89 | // phpcs:ignore Generic.PHP.ForbiddenFunctions.Discouraged |
| 90 | wp_set_current_user($currentUserId); |
| 91 | return $posts; |
| 92 | } |
| 93 | |
| 94 | public function transformPosts($args, $posts) { |
| 95 | if ($this->wp->hasFilter(self::FILTER_POST) !== false) { |
| 96 | $posts = array_map(function($post) use ($args) { |
| 97 | return $this->filterPost($post, $args); |
| 98 | }, $posts); |
| 99 | } |
| 100 | $transformer = new Transformer($args); |
| 101 | return $transformer->transform($posts); |
| 102 | } |
| 103 | |
| 104 | private function filterPost($post, array $args) { |
| 105 | $filteredPost = is_object($post) ? clone $post : $post; |
| 106 | $filteredPost = $this->wp->applyFilters(self::FILTER_POST, $filteredPost, $post, $args); |
| 107 | return is_object($filteredPost) ? $filteredPost : $post; |
| 108 | } |
| 109 | |
| 110 | private function _attachSentPostsFilter($newsletterId) { |
| 111 | if ($newsletterId > 0) { |
| 112 | $this->wp->addAction('posts_where', [$this, 'filterOutSentPosts']); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private function _detachSentPostsFilter($newsletterId) { |
| 117 | if ($newsletterId > 0) { |
| 118 | $this->wp->removeAction('posts_where', [$this, 'filterOutSentPosts']); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | private function logPosts(array $posts) { |
| 123 | $postsToLog = []; |
| 124 | foreach ($posts as $post) { |
| 125 | $postsToLog[] = [ |
| 126 | 'id' => $post->ID, |
| 127 | 'post_date' => $post->post_date, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 128 | ]; |
| 129 | } |
| 130 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_POST_NOTIFICATIONS)->info( |
| 131 | 'automated latest content loaded posts', |
| 132 | ['posts' => $postsToLog] |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 |