| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\API; |
| 4 |
|
| 5 |
use StoreEngine\API\Schema\AnalyticsSchema; |
| 6 |
use StoreEngine\Utils\Helper; |
| 7 |
use WP_REST_Controller; |
| 8 |
use WP_REST_Server; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* @deprecated |
| 16 |
*/ |
| 17 |
class Subscription extends WP_REST_Controller { |
| 18 |
use AnalyticsSchema; |
| 19 |
|
| 20 |
/** |
| 21 |
* @deprecated |
| 22 |
*/ |
| 23 |
public static function init() { |
| 24 |
$self = new self(); |
| 25 |
$self->namespace = STOREENGINE_PLUGIN_SLUG . '/v1'; |
| 26 |
$self->rest_base = 'subscription'; |
| 27 |
add_action( 'rest_api_init', array( $self, 'register_routes' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @deprecated |
| 32 |
*/ |
| 33 |
public function register_routes() { |
| 34 |
register_rest_route( $this->namespace, '/' . $this->rest_base, [ |
| 35 |
[ |
| 36 |
'methods' => \WP_REST_Server::READABLE, |
| 37 |
'callback' => [ $this, 'get_subscriptions' ], |
| 38 |
'permission_callback' => [ $this, 'get_permission_check' ], |
| 39 |
'args' => $this->get_collection_params(), |
| 40 |
], |
| 41 |
] ); |
| 42 |
|
| 43 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [ |
| 44 |
'args' => [ |
| 45 |
'id' => [ |
| 46 |
'description' => __( 'Unique identifier for the object.', 'storeengine' ), |
| 47 |
'type' => 'integer', |
| 48 |
], |
| 49 |
], |
| 50 |
[ |
| 51 |
'methods' => WP_REST_Server::READABLE, |
| 52 |
'callback' => [ $this, 'get_item' ], |
| 53 |
'permission_callback' => [ $this, 'get_permission_check' ], |
| 54 |
'args' => [ |
| 55 |
'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
| 56 |
], |
| 57 |
], |
| 58 |
] ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @deprecated |
| 63 |
*/ |
| 64 |
public function get_permission_check(): bool { |
| 65 |
return Helper::check_rest_user_cap( 'manage_options' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @deprecated |
| 70 |
*/ |
| 71 |
public function get_subscriptions( $request ): \WP_REST_Response { |
| 72 |
$order_model = new \StoreEngine\models\Order(); |
| 73 |
$subscriptions = $order_model->get_subscription_orders(); |
| 74 |
|
| 75 |
$response = []; |
| 76 |
$product_model = new \StoreEngine\models\Product(); |
| 77 |
foreach ( $subscriptions as $key => $subscription ) { |
| 78 |
$response[ $key ] = $this->prepare_item_for_response( $subscription, $request ); |
| 79 |
} |
| 80 |
|
| 81 |
return rest_ensure_response( $response ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @deprecated |
| 86 |
*/ |
| 87 |
public function prepare_item_for_response( $item, $request ): array { |
| 88 |
$product_model = new \StoreEngine\models\Product(); |
| 89 |
return array( |
| 90 |
'id' => $item['id'], |
| 91 |
'customer_email' => $item['billing_email'], |
| 92 |
'customer_name' => $item['order_billing_address']->first_name . ' ' . $item['order_billing_address']->last_name, |
| 93 |
'status' => $item['status'], |
| 94 |
'amount' => Helper::currency_format( $item['total_amount'] ), |
| 95 |
'product_name' => get_the_title( $item['purchase_items'][0]->product_id ), |
| 96 |
'price_name' => $product_model->get_price_name( $item['purchase_items'][0]->price_id ), |
| 97 |
'price_structure' => $product_model->get_price_duration( $item['purchase_items'][0]->price_id ), |
| 98 |
'remaining_payments' => 'inf', |
| 99 |
'next_payment_date' => gmdate( 'd F Y \a\t H:i', $item['meta']['next_payment_date'] ), |
| 100 |
'last_payment_date' => isset( $item['meta']['last_payment_date'] ) ? gmdate( 'd F Y \a\t H:i', |
| 101 |
$item['meta']['last_payment_date'] ) : gmdate( 'd F Y \a\t H:i', |
| 102 |
strtotime( $item['date_created_gmt'] ) ), |
| 103 |
'payment_method' => $item['payment_method'], |
| 104 |
'integration' => '', |
| 105 |
'created_at' => $item['date_created_gmt'], |
| 106 |
'billing_periods' => $item['billing_periods'], |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @deprecated |
| 112 |
*/ |
| 113 |
public function get_item( $request ) { |
| 114 |
$id = $request['id']; |
| 115 |
$order_model = new \StoreEngine\models\Order(); |
| 116 |
$subscription = $this->prepare_item_for_response( $order_model->get_subscription_by_order_id( $id ), $request ); |
| 117 |
if ( empty( $subscription ) ) { |
| 118 |
return []; |
| 119 |
} |
| 120 |
|
| 121 |
return rest_ensure_response( $subscription ); |
| 122 |
} |
| 123 |
} |
| 124 |
|