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
Coupons.php
68 lines
| 1 | <?php declare(strict_types = 1); |
| 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\WooCommerce\Helper; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class Coupons extends APIEndpoint { |
| 15 | public const DEFAULT_PAGE_SIZE = 100; |
| 16 | |
| 17 | /** @var Helper */ |
| 18 | public $helper; |
| 19 | |
| 20 | /*** @var WPFunctions */ |
| 21 | private $wp; |
| 22 | |
| 23 | public $permissions = [ |
| 24 | 'global' => AccessControl::PERMISSION_MANAGE_EMAILS, |
| 25 | ]; |
| 26 | |
| 27 | public function __construct( |
| 28 | WPFunctions $wp, |
| 29 | Helper $helper |
| 30 | ) { |
| 31 | $this->wp = $wp; |
| 32 | $this->helper = $helper; |
| 33 | } |
| 34 | |
| 35 | public function getCoupons(array $data = []): SuccessResponse { |
| 36 | $pageSize = $data['page_size'] ?? self::DEFAULT_PAGE_SIZE; |
| 37 | $pageNumber = $data['page_number'] ?? 1; |
| 38 | $discountType = $data['discount_type'] ?? null; |
| 39 | $search = $data['search'] ?? null; |
| 40 | $includeCouponIds = $data['include_coupon_ids'] ?? []; |
| 41 | return $this->successResponse( |
| 42 | $this->formatCoupons($this->helper->getCouponList( |
| 43 | (int)$pageSize, |
| 44 | (int)$pageNumber, |
| 45 | $discountType, |
| 46 | $search, |
| 47 | $includeCouponIds |
| 48 | )) |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param array $couponPosts |
| 54 | * @return array |
| 55 | */ |
| 56 | private function formatCoupons(array $couponPosts): array { |
| 57 | return array_map(function (\WP_Post $post): array { |
| 58 | $discountType = $this->wp->getPostMeta($post->ID, 'discount_type', true); |
| 59 | return [ |
| 60 | 'id' => $post->ID, |
| 61 | 'text' => $post->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 62 | 'excerpt' => $post->post_excerpt, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 63 | 'discountType' => $discountType, |
| 64 | ]; |
| 65 | }, $couponPosts); |
| 66 | } |
| 67 | } |
| 68 |