Create new API endpoint for WC coupons
Because I haven't found an easy way how to use WP or WC API, I decided to create a MailPoet endpoint for getting coupons that allows me loading them via Ajax. [MAILPOET-5123]
This commit is contained in:
65
mailpoet/lib/API/JSON/v1/Coupons.php
Normal file
65
mailpoet/lib/API/JSON/v1/Coupons.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\API\JSON\v1;
|
||||
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\API\JSON\SuccessResponse;
|
||||
use MailPoet\Config\AccessControl;
|
||||
use MailPoet\WooCommerce\Helper;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class Coupons extends APIEndpoint {
|
||||
public const DEFAULT_PAGE_SIZE = 10;
|
||||
|
||||
/** @var Helper */
|
||||
public $helper;
|
||||
|
||||
/*** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public $permissions = [
|
||||
'global' => 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user