Analytics.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmails.php
3 years ago
Captcha.php
1 year ago
Coupons.php
2 years ago
CustomFields.php
2 months ago
DynamicProducts.php
1 year ago
DynamicSegments.php
2 months ago
FeatureFlags.php
3 years ago
Forms.php
2 months ago
Help.php
1 year ago
ImportExport.php
2 months ago
Mailer.php
1 year ago
NewsletterLinks.php
2 months ago
NewsletterTemplates.php
2 months ago
Newsletters.php
2 months ago
Premium.php
10 months ago
RedirectResponse.php
1 year ago
Segments.php
2 months ago
SendingQueue.php
2 months ago
Services.php
6 months ago
Settings.php
2 months ago
Setup.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberStats.php
1 month ago
Subscribers.php
2 months ago
Tags.php
3 years ago
UserFlags.php
2 years ago
WoocommerceProductVariations.php
2 months ago
WoocommerceSettings.php
3 years ago
index.php
3 years ago
AutomatedLatestContent.php
161 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\Endpoint as APIEndpoint; |
| 9 | use MailPoet\API\JSON\SuccessResponse; |
| 10 | use MailPoet\Config\AccessControl; |
| 11 | use MailPoet\Newsletter\AutomatedLatestContent as ALC; |
| 12 | use MailPoet\Newsletter\BlockPostQuery; |
| 13 | use MailPoet\Util\APIPermissionHelper; |
| 14 | use MailPoet\WP\Functions as WPFunctions; |
| 15 | use MailPoet\WP\Posts as WPPosts; |
| 16 | |
| 17 | class AutomatedLatestContent extends APIEndpoint { |
| 18 | /** @var ALC */ |
| 19 | public $ALC; |
| 20 | |
| 21 | /*** @var WPFunctions */ |
| 22 | private $wp; |
| 23 | |
| 24 | /*** @var APIPermissionHelper */ |
| 25 | private $permissionHelper; |
| 26 | |
| 27 | public $permissions = [ |
| 28 | 'global' => AccessControl::PERMISSION_MANAGE_EMAILS, |
| 29 | ]; |
| 30 | |
| 31 | public function __construct( |
| 32 | ALC $alc, |
| 33 | APIPermissionHelper $permissionHelper, |
| 34 | WPFunctions $wp |
| 35 | ) { |
| 36 | $this->ALC = $alc; |
| 37 | $this->wp = $wp; |
| 38 | $this->permissionHelper = $permissionHelper; |
| 39 | } |
| 40 | |
| 41 | public function getPostTypes() { |
| 42 | $postTypes = array_map(function($postType) { |
| 43 | return [ |
| 44 | 'name' => $postType->name, |
| 45 | 'label' => $postType->label, |
| 46 | ]; |
| 47 | }, WPPosts::getTypes([], 'objects')); |
| 48 | return $this->successResponse( |
| 49 | array_filter($postTypes) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | public function getTaxonomies($data = []) { |
| 54 | $postType = (isset($data['postType'])) ? $data['postType'] : 'post'; |
| 55 | $allTaxonomies = WPFunctions::get()->getObjectTaxonomies($postType, 'objects'); |
| 56 | $taxonomiesWithLabel = array_filter($allTaxonomies, function($taxonomy) { |
| 57 | return $taxonomy->label; |
| 58 | }); |
| 59 | return $this->successResponse($taxonomiesWithLabel); |
| 60 | } |
| 61 | |
| 62 | public function getTerms($data = []) { |
| 63 | $taxonomies = (isset($data['taxonomies'])) ? $data['taxonomies'] : []; |
| 64 | $search = (isset($data['search'])) ? $data['search'] : ''; |
| 65 | $limit = (isset($data['limit'])) ? (int)$data['limit'] : 100; |
| 66 | $page = (isset($data['page'])) ? (int)$data['page'] : 1; |
| 67 | $args = [ |
| 68 | 'taxonomy' => $taxonomies, |
| 69 | 'hide_empty' => false, |
| 70 | 'search' => $search, |
| 71 | 'number' => $limit, |
| 72 | 'offset' => $limit * ($page - 1), |
| 73 | 'orderby' => 'name', |
| 74 | 'order' => 'ASC', |
| 75 | ]; |
| 76 | |
| 77 | $args = (array)$this->wp->applyFilters('mailpoet_search_terms_args', $args); |
| 78 | $terms = WPFunctions::get()->getTerms($args); |
| 79 | |
| 80 | $parentIds = array_unique(array_filter(array_column((array)$terms, 'parent'))); |
| 81 | if ($parentIds) { |
| 82 | $parentArgs = (array)$this->wp->applyFilters('mailpoet_search_terms_args', [ |
| 83 | 'taxonomy' => $taxonomies, |
| 84 | 'include' => $parentIds, |
| 85 | 'hide_empty' => false, |
| 86 | 'number' => 0, |
| 87 | ]); |
| 88 | $parents = WPFunctions::get()->getTerms($parentArgs); |
| 89 | $parentMap = []; |
| 90 | foreach ($parents as $parent) { |
| 91 | $parentMap[$parent->term_id] = $parent->name; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 92 | } |
| 93 | foreach ($terms as $key => $term) { |
| 94 | if (!$term instanceof \WP_Term) { |
| 95 | continue; |
| 96 | } |
| 97 | if ($term->parent && isset($parentMap[$term->parent])) { |
| 98 | $cloned = clone $term; |
| 99 | $cloned->name = $parentMap[$term->parent] . ' > ' . $term->name; |
| 100 | $terms[$key] = $cloned; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return $this->successResponse(array_values($terms)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Fetches posts for Posts static block |
| 110 | */ |
| 111 | public function getPosts(array $data = []): SuccessResponse { |
| 112 | return $this->successResponse( |
| 113 | $this->getPermittedPosts($this->ALC->getPosts(new BlockPostQuery(['args' => $data, 'dynamic' => false]))) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Fetches products for Abandoned Cart Content dynamic block |
| 119 | */ |
| 120 | public function getTransformedPosts(array $data = []): SuccessResponse { |
| 121 | $posts = $this->getPermittedPosts($this->ALC->getPosts(new BlockPostQuery([ |
| 122 | 'args' => $data, |
| 123 | // If the request is for Posts or Products block then we are fetching data for a static block |
| 124 | 'dynamic' => !(isset($data['type']) && in_array($data['type'], ["posts", "products"])), |
| 125 | ]))); |
| 126 | return $this->successResponse( |
| 127 | $this->ALC->transformPosts($data, $posts) |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Fetches different post types for ALC dynamic block |
| 133 | */ |
| 134 | public function getBulkTransformedPosts(array $data = []): SuccessResponse { |
| 135 | $usedPosts = []; |
| 136 | $renderedPosts = []; |
| 137 | |
| 138 | foreach ($data['blocks'] as $block) { |
| 139 | $query = new BlockPostQuery(['args' => $block, 'postsToExclude' => $usedPosts]); |
| 140 | $posts = $this->getPermittedPosts($this->ALC->getPosts($query)); |
| 141 | $renderedPosts[] = $this->ALC->transformPosts($block, $posts); |
| 142 | |
| 143 | foreach ($posts as $post) { |
| 144 | $usedPosts[] = $post->ID; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return $this->successResponse($renderedPosts); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @param \WP_Post[] $posts |
| 153 | * @return \WP_Post[] |
| 154 | */ |
| 155 | private function getPermittedPosts($posts) { |
| 156 | return array_filter($posts, function ($post) { |
| 157 | return $this->permissionHelper->checkReadPermission($post); |
| 158 | }); |
| 159 | } |
| 160 | } |
| 161 |