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
DynamicProducts.php
122 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\BlockPostQuery; |
| 12 | use MailPoet\Newsletter\DynamicProducts as DP; |
| 13 | use MailPoet\Util\APIPermissionHelper; |
| 14 | use MailPoet\WP\Functions as WPFunctions; |
| 15 | |
| 16 | class DynamicProducts extends APIEndpoint { |
| 17 | /** @var DP */ |
| 18 | public $dynamicProducts; |
| 19 | |
| 20 | /** @var WPFunctions */ |
| 21 | private $wp; |
| 22 | |
| 23 | /** @var APIPermissionHelper */ |
| 24 | private $permissionHelper; |
| 25 | |
| 26 | public $permissions = [ |
| 27 | 'global' => AccessControl::PERMISSION_MANAGE_EMAILS, |
| 28 | ]; |
| 29 | |
| 30 | public function __construct( |
| 31 | DP $dynamicProducts, |
| 32 | APIPermissionHelper $permissionHelper, |
| 33 | WPFunctions $wp |
| 34 | ) { |
| 35 | $this->dynamicProducts = $dynamicProducts; |
| 36 | $this->wp = $wp; |
| 37 | $this->permissionHelper = $permissionHelper; |
| 38 | } |
| 39 | |
| 40 | public function getTaxonomies($data = []) { |
| 41 | $postType = 'product'; |
| 42 | $allTaxonomies = WPFunctions::get()->getObjectTaxonomies($postType, 'objects'); |
| 43 | $taxonomiesWithLabel = array_filter($allTaxonomies, function($taxonomy) { |
| 44 | return $taxonomy->label; |
| 45 | }); |
| 46 | return $this->successResponse($taxonomiesWithLabel); |
| 47 | } |
| 48 | |
| 49 | public function getTerms($data = []) { |
| 50 | $taxonomies = (isset($data['taxonomies'])) ? $data['taxonomies'] : []; |
| 51 | $search = (isset($data['search'])) ? $data['search'] : ''; |
| 52 | $limit = (isset($data['limit'])) ? (int)$data['limit'] : 100; |
| 53 | $page = (isset($data['page'])) ? (int)$data['page'] : 1; |
| 54 | $args = [ |
| 55 | 'taxonomy' => $taxonomies, |
| 56 | 'hide_empty' => false, |
| 57 | 'search' => $search, |
| 58 | 'number' => $limit, |
| 59 | 'offset' => $limit * ($page - 1), |
| 60 | 'orderby' => 'name', |
| 61 | 'order' => 'ASC', |
| 62 | ]; |
| 63 | |
| 64 | $args = (array)$this->wp->applyFilters('mailpoet_search_terms_args', $args); |
| 65 | $terms = WPFunctions::get()->getTerms($args); |
| 66 | |
| 67 | return $this->successResponse(array_values($terms)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Fetches products for Products static block |
| 72 | */ |
| 73 | public function getProducts(array $data = []): SuccessResponse { |
| 74 | return $this->successResponse( |
| 75 | $this->getPermittedProducts($this->dynamicProducts->getPosts(new BlockPostQuery(['args' => $data, 'dynamic' => false]))) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Fetches products for Dynamic Products block |
| 81 | */ |
| 82 | public function getTransformedProducts(array $data = []): SuccessResponse { |
| 83 | $products = $this->getPermittedProducts($this->dynamicProducts->getPosts(new BlockPostQuery([ |
| 84 | 'args' => $data, |
| 85 | 'dynamic' => true, |
| 86 | ]))); |
| 87 | return $this->successResponse( |
| 88 | $this->dynamicProducts->transformPosts($data, $products) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Fetches products for multiple Dynamic Products blocks |
| 94 | */ |
| 95 | public function getBulkTransformedProducts(array $data = []): SuccessResponse { |
| 96 | $usedProducts = []; |
| 97 | $renderedProducts = []; |
| 98 | |
| 99 | foreach ($data['blocks'] as $block) { |
| 100 | $query = new BlockPostQuery(['args' => $block, 'postsToExclude' => $usedProducts]); |
| 101 | $products = $this->getPermittedProducts($this->dynamicProducts->getPosts($query)); |
| 102 | $renderedProducts[] = $this->dynamicProducts->transformPosts($block, $products); |
| 103 | |
| 104 | foreach ($products as $product) { |
| 105 | $usedProducts[] = $product->get_id(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $this->successResponse($renderedProducts); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param \WC_Product[] $products |
| 114 | * @return \WC_Product[] |
| 115 | */ |
| 116 | private function getPermittedProducts($products) { |
| 117 | return array_filter($products, function ($product) { |
| 118 | return $this->permissionHelper->checkReadPermission($product); |
| 119 | }); |
| 120 | } |
| 121 | } |
| 122 |