diff --git a/mailpoet/lib/API/JSON/v1/Coupons.php b/mailpoet/lib/API/JSON/v1/Coupons.php new file mode 100644 index 0000000000..1d349bbda2 --- /dev/null +++ b/mailpoet/lib/API/JSON/v1/Coupons.php @@ -0,0 +1,65 @@ + AccessControl::PERMISSION_MANAGE_EMAILS, + ]; + + public function __construct( + WPFunctions $wp, + Helper $helper + ) { + $this->wp = $wp; + $this->helper = $helper; + } + + public function getCoupons(array $data = []): SuccessResponse { + $pageSize = $data['page_size'] ?? self::DEFAULT_PAGE_SIZE; + $pageNumber = $data['page_number'] ?? 0; + $discountType = $data['discount_type'] ?? null; + $search = $data['search'] ?? null; + $includeCouponId = $data['include_coupon_id'] ?? null; + + return $this->successResponse( + $this->formatCoupons($this->helper->getCouponList( + (int)$pageSize, + (int)$pageNumber, + $discountType, + $search, + (int)$includeCouponId + )) + ); + } + + /** + * @param array $couponPosts + * @return array + */ + private function formatCoupons(array $couponPosts): array { + return array_map(function (\WP_Post $post): array { + $discountType = $this->wp->getPostMeta($post->ID, 'discount_type', true); + return [ + 'id' => $post->ID, + 'text' => $post->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + 'excerpt' => $post->post_excerpt, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + 'discountType' => $discountType, + ]; + }, $couponPosts); + } +} diff --git a/mailpoet/lib/DI/ContainerConfigurator.php b/mailpoet/lib/DI/ContainerConfigurator.php index e233a0a01d..35e43bf6e7 100644 --- a/mailpoet/lib/DI/ContainerConfigurator.php +++ b/mailpoet/lib/DI/ContainerConfigurator.php @@ -70,6 +70,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoet\API\JSON\v1\Analytics::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\AutomatedLatestContent::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\AutomaticEmails::class)->setPublic(true); + $container->autowire(\MailPoet\API\JSON\v1\Coupons::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\CustomFields::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\DynamicSegments::class)->setPublic(true); $container->autowire(\MailPoet\API\JSON\v1\FeatureFlags::class)->setPublic(true); diff --git a/mailpoet/lib/WooCommerce/Helper.php b/mailpoet/lib/WooCommerce/Helper.php index e9fc513d16..21dc59fec1 100644 --- a/mailpoet/lib/WooCommerce/Helper.php +++ b/mailpoet/lib/WooCommerce/Helper.php @@ -211,24 +211,46 @@ class Helper { return wc_get_order_statuses(); } - public function getCouponList(): array { - $couponPosts = $this->wp->getPosts([ - 'posts_per_page' => -1, + /** + * @return array|\WP_Post[] + */ + public function getCouponList( + int $pageSize = 10, + int $pageNumber = 1, + ?string $discountType = null, + ?string $search = null, + ?int $includeCouponId = null + ): array { + $args = [ + 'posts_per_page' => $pageSize, 'orderby' => 'name', 'order' => 'asc', 'post_type' => 'shop_coupon', 'post_status' => 'publish', - ]); + 'paged' => $pageNumber, + ]; + // Filtering coupons by discount type + if ($discountType) { + $args['meta_key'] = 'discount_type'; + $args['meta_value'] = $discountType; + } + // Search coupon by a query string + if ($search) { + $args['s'] = $search; + } - return array_map(function(\WP_Post $post): array { - $discountType = $this->wp->getPostMeta($post->ID, 'discount_type', true); - return [ - 'id' => $post->ID, - 'text' => $post->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps - 'excerpt' => $post->post_excerpt, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps - 'discountType' => $discountType, - ]; - }, $couponPosts); + $includeCoupons = []; + // We need to include the coupon with the given ID in the first page + if ($includeCouponId && $pageNumber === 1) { + $includeArgs = $args; + $includeArgs['include'] = [$includeCouponId]; + $includeCoupons = $this->wp->getPosts($includeArgs); + } + + // We remove duplicates because one of the remaining pages might contain the coupon with the given ID + $result = array_merge($includeCoupons, $this->wp->getPosts($args)); + $result = array_unique($result, SORT_REGULAR); + return array_values($result); } public function wcGetPriceDecimalSeparator() {