1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* REST discounts controller. |
4
|
|
|
* |
5
|
|
|
* @version 1.0.19 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
defined( 'ABSPATH' ) || exit; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* REST API discounts controller class. |
12
|
|
|
* |
13
|
|
|
* @package Invoicing |
14
|
|
|
*/ |
15
|
|
|
class WPInv_REST_Discounts_Controller extends GetPaid_REST_Posts_Controller { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Post type. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $post_type = 'wpi_discount'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The base of this controller's route. |
26
|
|
|
* |
27
|
|
|
* @since 1.0.13 |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $rest_base = 'discounts'; |
31
|
|
|
|
32
|
|
|
/** Contains this controller's class name. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $crud_class = 'WPInv_Discount'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Registers the routes for the objects of the controller. |
40
|
|
|
* |
41
|
|
|
* @since 1.0.19 |
42
|
|
|
* |
43
|
|
|
* @see register_rest_route() |
44
|
|
|
*/ |
45
|
|
|
public function register_namespace_routes( $namespace ) { |
46
|
|
|
|
47
|
|
|
parent::register_namespace_routes( $namespace ); |
48
|
|
|
|
49
|
|
|
register_rest_route( |
50
|
|
|
$this->namespace, |
51
|
|
|
'/' . $this->rest_base . '/discount-types', |
52
|
|
|
array( |
53
|
|
|
array( |
54
|
|
|
'methods' => WP_REST_Server::READABLE, |
55
|
|
|
'callback' => array( $this, 'get_discount_types' ), |
56
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
57
|
|
|
), |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Handles rest requests for discount types. |
65
|
|
|
* |
66
|
|
|
* @since 1.0.13 |
67
|
|
|
* |
68
|
|
|
* |
69
|
|
|
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
70
|
|
|
*/ |
71
|
|
|
public function get_discount_types() { |
72
|
|
|
return rest_ensure_response( wpinv_get_discount_types() ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retrieves the query params for the discount collection. |
77
|
|
|
* |
78
|
|
|
* @since 1.0.13 |
79
|
|
|
* |
80
|
|
|
* @return array Collection parameters. |
81
|
|
|
*/ |
82
|
|
|
public function get_collection_params() { |
83
|
|
|
|
84
|
|
|
$params = array_merge( |
85
|
|
|
parent::get_collection_params(), |
86
|
|
|
array( |
87
|
|
|
|
88
|
|
|
// Discount types |
89
|
|
|
'type' => array( |
90
|
|
|
'description' => __( 'Type of discounts to fetch.', 'invoicing' ), |
91
|
|
|
'type' => array( 'array', 'string' ), |
92
|
|
|
'default' => 'any', |
93
|
|
|
'validate_callback' => 'rest_validate_request_arg', |
94
|
|
|
'sanitize_callback' => 'wpinv_parse_list', |
95
|
|
|
'items' => array( |
96
|
|
|
'enum' => array_merge( array( 'any' ), array_keys( wpinv_get_discount_types() ) ), |
97
|
|
|
'type' => 'string', |
98
|
|
|
), |
99
|
|
|
), |
100
|
|
|
|
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
// Filter collection parameters for the discounts controller. |
105
|
|
|
return apply_filters( 'getpaid_rest_discounts_collection_params', $params, $this ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Determine the allowed query_vars for a get_items() response and |
110
|
|
|
* prepare for WP_Query. |
111
|
|
|
* |
112
|
|
|
* @param array $prepared_args Prepared arguments. |
113
|
|
|
* @param WP_REST_Request $request Request object. |
114
|
|
|
* @return array $query_args |
115
|
|
|
*/ |
116
|
|
|
protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
117
|
|
|
|
118
|
|
|
$query_args = parent::prepare_items_query( $prepared_args ); |
119
|
|
|
|
120
|
|
|
// Retrieve items by type. |
121
|
|
|
if ( ! in_array( 'any', $request['type'] ) ) { |
|
|
|
|
122
|
|
|
|
123
|
|
|
if ( empty( $query_args['meta_query'] ) ) { |
124
|
|
|
$query_args['meta_query'] = array(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$query_args['meta_query'][] = array( |
128
|
|
|
'key' => '_wpi_discount_type', |
129
|
|
|
'value' => implode( ',', $request['type'] ), |
|
|
|
|
130
|
|
|
'compare' => 'IN', |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return apply_filters( 'getpaid_rest_discounts_prepare_items_query', $query_args, $request, $this ); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Retrieves a valid list of post statuses. |
141
|
|
|
* |
142
|
|
|
* @since 1.0.15 |
143
|
|
|
* |
144
|
|
|
* @return array A list of registered item statuses. |
145
|
|
|
*/ |
146
|
|
|
public function get_post_statuses() { |
147
|
|
|
return array( 'publish', 'pending', 'draft', 'expired' ); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|