@@ -20,91 +20,91 @@ discard block |
||
20 | 20 | class WPInv_REST_Items_Controller extends WP_REST_Posts_Controller { |
21 | 21 | |
22 | 22 | /** |
23 | - * Post type. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $post_type = 'wpi_item'; |
|
23 | + * Post type. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $post_type = 'wpi_item'; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Cached results of get_item_schema. |
|
31 | - * |
|
32 | - * @since 1.0.13 |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - protected $schema; |
|
29 | + /** |
|
30 | + * Cached results of get_item_schema. |
|
31 | + * |
|
32 | + * @since 1.0.13 |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + protected $schema; |
|
36 | 36 | |
37 | 37 | /** |
38 | - * Constructor. |
|
39 | - * |
|
40 | - * @since 1.0.13 |
|
41 | - * |
|
42 | - * @param string $namespace Api Namespace |
|
43 | - */ |
|
44 | - public function __construct( $namespace ) { |
|
38 | + * Constructor. |
|
39 | + * |
|
40 | + * @since 1.0.13 |
|
41 | + * |
|
42 | + * @param string $namespace Api Namespace |
|
43 | + */ |
|
44 | + public function __construct( $namespace ) { |
|
45 | 45 | |
46 | 46 | // Set api namespace... |
47 | - $this->namespace = $namespace; |
|
47 | + $this->namespace = $namespace; |
|
48 | 48 | |
49 | 49 | // ... and the rest base |
50 | 50 | $this->rest_base = 'items'; |
51 | 51 | |
52 | 52 | } |
53 | 53 | |
54 | - /** |
|
55 | - * Registers the routes for the objects of the controller. |
|
56 | - * |
|
57 | - * @since 1.0.13 |
|
58 | - * |
|
59 | - * @see register_rest_route() |
|
60 | - */ |
|
61 | - public function register_routes() { |
|
62 | - |
|
63 | - parent::register_routes(); |
|
64 | - |
|
65 | - register_rest_route( |
|
66 | - $this->namespace, |
|
67 | - '/' . $this->rest_base . '/item-types', |
|
68 | - array( |
|
69 | - array( |
|
70 | - 'methods' => WP_REST_Server::READABLE, |
|
71 | - 'callback' => array( $this, 'get_item_types' ), |
|
72 | - ), |
|
73 | - ) |
|
74 | - ); |
|
75 | - |
|
76 | - } |
|
54 | + /** |
|
55 | + * Registers the routes for the objects of the controller. |
|
56 | + * |
|
57 | + * @since 1.0.13 |
|
58 | + * |
|
59 | + * @see register_rest_route() |
|
60 | + */ |
|
61 | + public function register_routes() { |
|
62 | + |
|
63 | + parent::register_routes(); |
|
64 | + |
|
65 | + register_rest_route( |
|
66 | + $this->namespace, |
|
67 | + '/' . $this->rest_base . '/item-types', |
|
68 | + array( |
|
69 | + array( |
|
70 | + 'methods' => WP_REST_Server::READABLE, |
|
71 | + 'callback' => array( $this, 'get_item_types' ), |
|
72 | + ), |
|
73 | + ) |
|
74 | + ); |
|
75 | + |
|
76 | + } |
|
77 | 77 | |
78 | 78 | /** |
79 | - * Checks if a given request has access to read items. |
|
79 | + * Checks if a given request has access to read items. |
|
80 | 80 | * |
81 | - * |
|
82 | - * @since 1.0.13 |
|
83 | - * |
|
84 | - * @param WP_REST_Request $request Full details about the request. |
|
85 | - * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
86 | - */ |
|
87 | - public function get_items_permissions_check( $request ) { |
|
81 | + * |
|
82 | + * @since 1.0.13 |
|
83 | + * |
|
84 | + * @param WP_REST_Request $request Full details about the request. |
|
85 | + * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
86 | + */ |
|
87 | + public function get_items_permissions_check( $request ) { |
|
88 | 88 | |
89 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
90 | - return true; |
|
91 | - } |
|
89 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
90 | + return true; |
|
91 | + } |
|
92 | 92 | |
93 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice items.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
93 | + return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice items.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
94 | 94 | |
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
98 | - * Retrieves a collection of invoice items. |
|
99 | - * |
|
100 | - * @since 1.0.13 |
|
101 | - * |
|
102 | - * @param WP_REST_Request $request Full details about the request. |
|
103 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
104 | - */ |
|
105 | - public function get_items( $request ) { |
|
98 | + * Retrieves a collection of invoice items. |
|
99 | + * |
|
100 | + * @since 1.0.13 |
|
101 | + * |
|
102 | + * @param WP_REST_Request $request Full details about the request. |
|
103 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
104 | + */ |
|
105 | + public function get_items( $request ) { |
|
106 | 106 | |
107 | - // Retrieve the list of registered item query parameters. |
|
107 | + // Retrieve the list of registered item query parameters. |
|
108 | 108 | $registered = $this->get_collection_params(); |
109 | 109 | |
110 | 110 | $args = array(); |
@@ -117,54 +117,54 @@ discard block |
||
117 | 117 | |
118 | 118 | } |
119 | 119 | |
120 | - /** |
|
121 | - * Filters the wpinv_get_items arguments for items rest requests. |
|
122 | - * |
|
123 | - * |
|
124 | - * @since 1.0.13 |
|
125 | - * |
|
126 | - * |
|
127 | - * @param array $args Key value array of query var to query value. |
|
128 | - * @param WP_REST_Request $request The request used. |
|
129 | - */ |
|
120 | + /** |
|
121 | + * Filters the wpinv_get_items arguments for items rest requests. |
|
122 | + * |
|
123 | + * |
|
124 | + * @since 1.0.13 |
|
125 | + * |
|
126 | + * |
|
127 | + * @param array $args Key value array of query var to query value. |
|
128 | + * @param WP_REST_Request $request The request used. |
|
129 | + */ |
|
130 | 130 | $args = apply_filters( "wpinv_rest_get_items_arguments", $args, $request, $this ); |
131 | 131 | |
132 | - // Special args |
|
133 | - $args[ 'return' ] = 'objects'; |
|
134 | - $args[ 'paginate' ] = true; |
|
132 | + // Special args |
|
133 | + $args[ 'return' ] = 'objects'; |
|
134 | + $args[ 'paginate' ] = true; |
|
135 | 135 | |
136 | 136 | // Run the query. |
137 | - $query = wpinv_get_all_items( $args ); |
|
137 | + $query = wpinv_get_all_items( $args ); |
|
138 | 138 | |
139 | - // Prepare the retrieved items |
|
140 | - $items = array(); |
|
141 | - foreach( $query->items as $item ) { |
|
142 | - |
|
143 | - if ( ! $this->check_read_permission( $item ) ) { |
|
144 | - continue; |
|
145 | - } |
|
146 | - |
|
147 | - $data = $this->prepare_item_for_response( $item, $request ); |
|
148 | - $items[] = $this->prepare_response_for_collection( $data ); |
|
149 | - |
|
150 | - } |
|
151 | - |
|
152 | - // Prepare the response. |
|
153 | - $response = rest_ensure_response( $items ); |
|
154 | - $response->header( 'X-WP-Total', (int) $query->total ); |
|
155 | - $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
156 | - |
|
157 | - /** |
|
158 | - * Filters the responses for item requests. |
|
159 | - * |
|
160 | - * |
|
161 | - * @since 1.0.13 |
|
162 | - * |
|
163 | - * |
|
164 | - * @param arrWP_REST_Response $response Response object. |
|
165 | - * @param WP_REST_Request $request The request used. |
|
139 | + // Prepare the retrieved items |
|
140 | + $items = array(); |
|
141 | + foreach( $query->items as $item ) { |
|
142 | + |
|
143 | + if ( ! $this->check_read_permission( $item ) ) { |
|
144 | + continue; |
|
145 | + } |
|
146 | + |
|
147 | + $data = $this->prepare_item_for_response( $item, $request ); |
|
148 | + $items[] = $this->prepare_response_for_collection( $data ); |
|
149 | + |
|
150 | + } |
|
151 | + |
|
152 | + // Prepare the response. |
|
153 | + $response = rest_ensure_response( $items ); |
|
154 | + $response->header( 'X-WP-Total', (int) $query->total ); |
|
155 | + $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
156 | + |
|
157 | + /** |
|
158 | + * Filters the responses for item requests. |
|
159 | + * |
|
160 | + * |
|
161 | + * @since 1.0.13 |
|
162 | + * |
|
163 | + * |
|
164 | + * @param arrWP_REST_Response $response Response object. |
|
165 | + * @param WP_REST_Request $request The request used. |
|
166 | 166 | * @param array $args Array of args used to retrieve the items |
167 | - */ |
|
167 | + */ |
|
168 | 168 | $response = apply_filters( "wpinv_rest_items_response", $response, $request, $args ); |
169 | 169 | |
170 | 170 | return rest_ensure_response( $response ); |
@@ -172,25 +172,25 @@ discard block |
||
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
175 | - * Get the post, if the ID is valid. |
|
176 | - * |
|
177 | - * @since 1.0.13 |
|
178 | - * |
|
179 | - * @param int $item_id Supplied ID. |
|
180 | - * @return WPInv_Item|WP_Error Item object if ID is valid, WP_Error otherwise. |
|
181 | - */ |
|
182 | - protected function get_post( $item_id ) { |
|
175 | + * Get the post, if the ID is valid. |
|
176 | + * |
|
177 | + * @since 1.0.13 |
|
178 | + * |
|
179 | + * @param int $item_id Supplied ID. |
|
180 | + * @return WPInv_Item|WP_Error Item object if ID is valid, WP_Error otherwise. |
|
181 | + */ |
|
182 | + protected function get_post( $item_id ) { |
|
183 | 183 | |
184 | - $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid item ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
184 | + $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid item ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
185 | 185 | |
186 | 186 | // Ids start from 1 |
187 | 187 | if ( (int) $item_id <= 0 ) { |
188 | - return $error; |
|
189 | - } |
|
188 | + return $error; |
|
189 | + } |
|
190 | 190 | |
191 | - $item = wpinv_get_item_by( 'id', (int) $item_id ); |
|
192 | - if ( empty( $item ) ) { |
|
193 | - return $error; |
|
191 | + $item = wpinv_get_item_by( 'id', (int) $item_id ); |
|
192 | + if ( empty( $item ) ) { |
|
193 | + return $error; |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | return $item; |
@@ -198,27 +198,27 @@ discard block |
||
198 | 198 | } |
199 | 199 | |
200 | 200 | /** |
201 | - * Checks if a given request has access to read an invoice item. |
|
202 | - * |
|
203 | - * @since 1.0.13 |
|
204 | - * |
|
205 | - * @param WP_REST_Request $request Full details about the request. |
|
206 | - * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
|
207 | - */ |
|
208 | - public function get_item_permissions_check( $request ) { |
|
201 | + * Checks if a given request has access to read an invoice item. |
|
202 | + * |
|
203 | + * @since 1.0.13 |
|
204 | + * |
|
205 | + * @param WP_REST_Request $request Full details about the request. |
|
206 | + * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
|
207 | + */ |
|
208 | + public function get_item_permissions_check( $request ) { |
|
209 | 209 | |
210 | 210 | // Retrieve the item object. |
211 | 211 | $item = $this->get_post( $request['id'] ); |
212 | 212 | |
213 | 213 | // Ensure it is valid. |
214 | - if ( is_wp_error( $item ) ) { |
|
215 | - return $item; |
|
216 | - } |
|
214 | + if ( is_wp_error( $item ) ) { |
|
215 | + return $item; |
|
216 | + } |
|
217 | 217 | |
218 | - $post_type = get_post_type_object( $this->post_type ); |
|
218 | + $post_type = get_post_type_object( $this->post_type ); |
|
219 | 219 | |
220 | - if ( ! current_user_can( $post_type->cap->read_post, $item->ID ) ) { |
|
221 | - return new WP_Error( |
|
220 | + if ( ! current_user_can( $post_type->cap->read_post, $item->ID ) ) { |
|
221 | + return new WP_Error( |
|
222 | 222 | 'rest_cannot_edit', |
223 | 223 | __( 'Sorry, you are not allowed to view this item.', 'invoicing' ), |
224 | 224 | array( |
@@ -227,61 +227,61 @@ discard block |
||
227 | 227 | ); |
228 | 228 | } |
229 | 229 | |
230 | - return $this->check_read_permission( $item ); |
|
230 | + return $this->check_read_permission( $item ); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | /** |
234 | - * Checks if an item can be read. |
|
235 | - * |
|
236 | - * An item can be read by site admins. |
|
237 | - * |
|
238 | - * |
|
239 | - * @since 1.0.13 |
|
240 | - * |
|
241 | - * @param WPInv_Item $item WPInv_Item object. |
|
242 | - * @return bool Whether the post can be read. |
|
243 | - */ |
|
244 | - public function check_read_permission( $item ) { |
|
245 | - |
|
246 | - // An item can be read by an admin... |
|
247 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
248 | - return true; |
|
249 | - } |
|
250 | - |
|
251 | - return false; |
|
234 | + * Checks if an item can be read. |
|
235 | + * |
|
236 | + * An item can be read by site admins. |
|
237 | + * |
|
238 | + * |
|
239 | + * @since 1.0.13 |
|
240 | + * |
|
241 | + * @param WPInv_Item $item WPInv_Item object. |
|
242 | + * @return bool Whether the post can be read. |
|
243 | + */ |
|
244 | + public function check_read_permission( $item ) { |
|
245 | + |
|
246 | + // An item can be read by an admin... |
|
247 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
248 | + return true; |
|
249 | + } |
|
250 | + |
|
251 | + return false; |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | /** |
255 | - * Retrieves a single invoice item. |
|
256 | - * |
|
257 | - * @since 1.0.13 |
|
258 | - * |
|
259 | - * @param WP_REST_Request $request Full details about the request. |
|
260 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
261 | - */ |
|
262 | - public function get_item( $request ) { |
|
255 | + * Retrieves a single invoice item. |
|
256 | + * |
|
257 | + * @since 1.0.13 |
|
258 | + * |
|
259 | + * @param WP_REST_Request $request Full details about the request. |
|
260 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
261 | + */ |
|
262 | + public function get_item( $request ) { |
|
263 | 263 | |
264 | 264 | // Fetch the item. |
265 | 265 | $item = $this->get_post( $request['id'] ); |
266 | 266 | |
267 | 267 | // Abort early if it does not exist |
268 | - if ( is_wp_error( $item ) ) { |
|
269 | - return $item; |
|
270 | - } |
|
271 | - |
|
272 | - // Prepare the response |
|
273 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
274 | - |
|
275 | - /** |
|
276 | - * Filters the responses for single invoice item requests. |
|
277 | - * |
|
278 | - * |
|
279 | - * @since 1.0.13 |
|
280 | - * @var WP_HTTP_Response |
|
281 | - * |
|
282 | - * @param WP_HTTP_Response $response Response. |
|
283 | - * @param WP_REST_Request $request The request used. |
|
284 | - */ |
|
268 | + if ( is_wp_error( $item ) ) { |
|
269 | + return $item; |
|
270 | + } |
|
271 | + |
|
272 | + // Prepare the response |
|
273 | + $response = $this->prepare_item_for_response( $item, $request ); |
|
274 | + |
|
275 | + /** |
|
276 | + * Filters the responses for single invoice item requests. |
|
277 | + * |
|
278 | + * |
|
279 | + * @since 1.0.13 |
|
280 | + * @var WP_HTTP_Response |
|
281 | + * |
|
282 | + * @param WP_HTTP_Response $response Response. |
|
283 | + * @param WP_REST_Request $request The request used. |
|
284 | + */ |
|
285 | 285 | $response = apply_filters( "wpinv_rest_get_item_response", $response, $request ); |
286 | 286 | |
287 | 287 | return rest_ensure_response( $response ); |
@@ -289,26 +289,26 @@ discard block |
||
289 | 289 | } |
290 | 290 | |
291 | 291 | /** |
292 | - * Checks if a given request has access to create an invoice item. |
|
293 | - * |
|
294 | - * @since 1.0.13 |
|
295 | - * |
|
296 | - * @param WP_REST_Request $request Full details about the request. |
|
297 | - * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
298 | - */ |
|
299 | - public function create_item_permissions_check( $request ) { |
|
292 | + * Checks if a given request has access to create an invoice item. |
|
293 | + * |
|
294 | + * @since 1.0.13 |
|
295 | + * |
|
296 | + * @param WP_REST_Request $request Full details about the request. |
|
297 | + * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
298 | + */ |
|
299 | + public function create_item_permissions_check( $request ) { |
|
300 | 300 | |
301 | - if ( ! empty( $request['id'] ) ) { |
|
302 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
303 | - } |
|
301 | + if ( ! empty( $request['id'] ) ) { |
|
302 | + return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
303 | + } |
|
304 | 304 | |
305 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
306 | - return true; |
|
307 | - } |
|
305 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
306 | + return true; |
|
307 | + } |
|
308 | 308 | |
309 | - $post_type = get_post_type_object( $this->post_type ); |
|
310 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
311 | - return new WP_Error( |
|
309 | + $post_type = get_post_type_object( $this->post_type ); |
|
310 | + if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
311 | + return new WP_Error( |
|
312 | 312 | 'rest_cannot_create', |
313 | 313 | __( 'Sorry, you are not allowed to create invoice items as this user.', 'invoicing' ), |
314 | 314 | array( |
@@ -317,253 +317,253 @@ discard block |
||
317 | 317 | ); |
318 | 318 | } |
319 | 319 | |
320 | - return true; |
|
320 | + return true; |
|
321 | 321 | } |
322 | 322 | |
323 | 323 | /** |
324 | - * Creates a single invoice item. |
|
325 | - * |
|
326 | - * @since 1.0.13 |
|
327 | - * |
|
328 | - * @param WP_REST_Request $request Full details about the request. |
|
329 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
330 | - */ |
|
331 | - public function create_item( $request ) { |
|
332 | - |
|
333 | - if ( ! empty( $request['id'] ) ) { |
|
334 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing invoice item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
335 | - } |
|
324 | + * Creates a single invoice item. |
|
325 | + * |
|
326 | + * @since 1.0.13 |
|
327 | + * |
|
328 | + * @param WP_REST_Request $request Full details about the request. |
|
329 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
330 | + */ |
|
331 | + public function create_item( $request ) { |
|
332 | + |
|
333 | + if ( ! empty( $request['id'] ) ) { |
|
334 | + return new WP_Error( 'rest_item_exists', __( 'Cannot create existing invoice item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
335 | + } |
|
336 | 336 | |
337 | - $request->set_param( 'context', 'edit' ); |
|
337 | + $request->set_param( 'context', 'edit' ); |
|
338 | 338 | |
339 | - // Prepare the updated data. |
|
340 | - $item_data = $this->prepare_item_for_database( $request ); |
|
339 | + // Prepare the updated data. |
|
340 | + $item_data = $this->prepare_item_for_database( $request ); |
|
341 | 341 | |
342 | - if ( is_wp_error( $item_data ) ) { |
|
343 | - return $item_data; |
|
344 | - } |
|
342 | + if ( is_wp_error( $item_data ) ) { |
|
343 | + return $item_data; |
|
344 | + } |
|
345 | 345 | |
346 | - // Try creating the item. |
|
346 | + // Try creating the item. |
|
347 | 347 | $item = wpinv_create_item( $item_data, true ); |
348 | 348 | |
349 | - if ( is_wp_error( $item ) ) { |
|
349 | + if ( is_wp_error( $item ) ) { |
|
350 | 350 | return $item; |
351 | - } |
|
352 | - |
|
353 | - // Prepare the response |
|
354 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
355 | - |
|
356 | - /** |
|
357 | - * Fires after a single invoice item is created or updated via the REST API. |
|
358 | - * |
|
359 | - * @since 1.0.13 |
|
360 | - * |
|
361 | - * @param WPinv_Item $item Inserted or updated item object. |
|
362 | - * @param WP_REST_Request $request Request object. |
|
363 | - * @param bool $creating True when creating a post, false when updating. |
|
364 | - */ |
|
365 | - do_action( "wpinv_rest_insert_item", $item, $request, true ); |
|
366 | - |
|
367 | - /** |
|
368 | - * Filters the responses for creating single item requests. |
|
369 | - * |
|
370 | - * |
|
371 | - * @since 1.0.13 |
|
372 | - * |
|
373 | - * |
|
374 | - * @param array $item_data Invoice properties. |
|
375 | - * @param WP_REST_Request $request The request used. |
|
376 | - */ |
|
351 | + } |
|
352 | + |
|
353 | + // Prepare the response |
|
354 | + $response = $this->prepare_item_for_response( $item, $request ); |
|
355 | + |
|
356 | + /** |
|
357 | + * Fires after a single invoice item is created or updated via the REST API. |
|
358 | + * |
|
359 | + * @since 1.0.13 |
|
360 | + * |
|
361 | + * @param WPinv_Item $item Inserted or updated item object. |
|
362 | + * @param WP_REST_Request $request Request object. |
|
363 | + * @param bool $creating True when creating a post, false when updating. |
|
364 | + */ |
|
365 | + do_action( "wpinv_rest_insert_item", $item, $request, true ); |
|
366 | + |
|
367 | + /** |
|
368 | + * Filters the responses for creating single item requests. |
|
369 | + * |
|
370 | + * |
|
371 | + * @since 1.0.13 |
|
372 | + * |
|
373 | + * |
|
374 | + * @param array $item_data Invoice properties. |
|
375 | + * @param WP_REST_Request $request The request used. |
|
376 | + */ |
|
377 | 377 | $response = apply_filters( "wpinv_rest_create_item_response", $response, $request ); |
378 | 378 | |
379 | 379 | return rest_ensure_response( $response ); |
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Checks if a given request has access to update an item. |
|
384 | - * |
|
385 | - * @since 1.0.13 |
|
386 | - * |
|
387 | - * @param WP_REST_Request $request Full details about the request. |
|
388 | - * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
389 | - */ |
|
390 | - public function update_item_permissions_check( $request ) { |
|
391 | - |
|
392 | - // Retrieve the item. |
|
393 | - $item = $this->get_post( $request['id'] ); |
|
394 | - if ( is_wp_error( $item ) ) { |
|
395 | - return $item; |
|
396 | - } |
|
397 | - |
|
398 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
399 | - return true; |
|
400 | - } |
|
401 | - |
|
402 | - return new WP_Error( |
|
403 | - 'rest_cannot_edit', |
|
404 | - __( 'Sorry, you are not allowed to update this item.', 'invoicing' ), |
|
405 | - array( |
|
406 | - 'status' => rest_authorization_required_code(), |
|
407 | - ) |
|
408 | - ); |
|
409 | - |
|
410 | - } |
|
411 | - |
|
412 | - /** |
|
413 | - * Updates a single item. |
|
414 | - * |
|
415 | - * @since 1.0.13 |
|
416 | - * |
|
417 | - * @param WP_REST_Request $request Full details about the request. |
|
418 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
419 | - */ |
|
420 | - public function update_item( $request ) { |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Checks if a given request has access to update an item. |
|
384 | + * |
|
385 | + * @since 1.0.13 |
|
386 | + * |
|
387 | + * @param WP_REST_Request $request Full details about the request. |
|
388 | + * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
389 | + */ |
|
390 | + public function update_item_permissions_check( $request ) { |
|
391 | + |
|
392 | + // Retrieve the item. |
|
393 | + $item = $this->get_post( $request['id'] ); |
|
394 | + if ( is_wp_error( $item ) ) { |
|
395 | + return $item; |
|
396 | + } |
|
397 | + |
|
398 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
399 | + return true; |
|
400 | + } |
|
401 | + |
|
402 | + return new WP_Error( |
|
403 | + 'rest_cannot_edit', |
|
404 | + __( 'Sorry, you are not allowed to update this item.', 'invoicing' ), |
|
405 | + array( |
|
406 | + 'status' => rest_authorization_required_code(), |
|
407 | + ) |
|
408 | + ); |
|
409 | + |
|
410 | + } |
|
411 | + |
|
412 | + /** |
|
413 | + * Updates a single item. |
|
414 | + * |
|
415 | + * @since 1.0.13 |
|
416 | + * |
|
417 | + * @param WP_REST_Request $request Full details about the request. |
|
418 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
419 | + */ |
|
420 | + public function update_item( $request ) { |
|
421 | 421 | |
422 | - // Ensure the item exists. |
|
422 | + // Ensure the item exists. |
|
423 | 423 | $valid_check = $this->get_post( $request['id'] ); |
424 | 424 | |
425 | 425 | // Abort early if it does not exist |
426 | - if ( is_wp_error( $valid_check ) ) { |
|
427 | - return $valid_check; |
|
428 | - } |
|
426 | + if ( is_wp_error( $valid_check ) ) { |
|
427 | + return $valid_check; |
|
428 | + } |
|
429 | 429 | |
430 | - $request->set_param( 'context', 'edit' ); |
|
430 | + $request->set_param( 'context', 'edit' ); |
|
431 | 431 | |
432 | - // Prepare the updated data. |
|
433 | - $data_to_update = $this->prepare_item_for_database( $request ); |
|
432 | + // Prepare the updated data. |
|
433 | + $data_to_update = $this->prepare_item_for_database( $request ); |
|
434 | 434 | |
435 | - if ( is_wp_error( $data_to_update ) ) { |
|
436 | - return $data_to_update; |
|
437 | - } |
|
435 | + if ( is_wp_error( $data_to_update ) ) { |
|
436 | + return $data_to_update; |
|
437 | + } |
|
438 | 438 | |
439 | - // Abort if no item data is provided |
|
439 | + // Abort if no item data is provided |
|
440 | 440 | if( empty( $data_to_update ) ) { |
441 | 441 | return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
442 | 442 | } |
443 | 443 | |
444 | - // Include the item ID |
|
445 | - $data_to_update['ID'] = $request['id']; |
|
446 | - |
|
447 | - // Update the item |
|
448 | - $updated_item = wpinv_update_item( $data_to_update, true ); |
|
449 | - |
|
450 | - // Incase the update operation failed... |
|
451 | - if ( is_wp_error( $updated_item ) ) { |
|
452 | - return $updated_item; |
|
453 | - } |
|
454 | - |
|
455 | - // Prepare the response |
|
456 | - $response = $this->prepare_item_for_response( $updated_item, $request ); |
|
457 | - |
|
458 | - /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
459 | - do_action( "wpinv_rest_insert_item", $updated_item, $request, false ); |
|
460 | - |
|
461 | - /** |
|
462 | - * Filters the responses for updating single item requests. |
|
463 | - * |
|
464 | - * |
|
465 | - * @since 1.0.13 |
|
466 | - * |
|
467 | - * |
|
468 | - * @param array $item_data Item properties. |
|
469 | - * @param WP_REST_Request $request The request used. |
|
470 | - */ |
|
444 | + // Include the item ID |
|
445 | + $data_to_update['ID'] = $request['id']; |
|
446 | + |
|
447 | + // Update the item |
|
448 | + $updated_item = wpinv_update_item( $data_to_update, true ); |
|
449 | + |
|
450 | + // Incase the update operation failed... |
|
451 | + if ( is_wp_error( $updated_item ) ) { |
|
452 | + return $updated_item; |
|
453 | + } |
|
454 | + |
|
455 | + // Prepare the response |
|
456 | + $response = $this->prepare_item_for_response( $updated_item, $request ); |
|
457 | + |
|
458 | + /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
459 | + do_action( "wpinv_rest_insert_item", $updated_item, $request, false ); |
|
460 | + |
|
461 | + /** |
|
462 | + * Filters the responses for updating single item requests. |
|
463 | + * |
|
464 | + * |
|
465 | + * @since 1.0.13 |
|
466 | + * |
|
467 | + * |
|
468 | + * @param array $item_data Item properties. |
|
469 | + * @param WP_REST_Request $request The request used. |
|
470 | + */ |
|
471 | 471 | $response = apply_filters( "wpinv_rest_update_item_response", $response, $data_to_update, $request ); |
472 | 472 | |
473 | 473 | return rest_ensure_response( $response ); |
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Checks if a given request has access to delete an item. |
|
478 | - * |
|
479 | - * @since 1.0.13 |
|
480 | - * |
|
481 | - * @param WP_REST_Request $request Full details about the request. |
|
482 | - * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. |
|
483 | - */ |
|
484 | - public function delete_item_permissions_check( $request ) { |
|
485 | - |
|
486 | - // Retrieve the item. |
|
487 | - $item = $this->get_post( $request['id'] ); |
|
488 | - if ( is_wp_error( $item ) ) { |
|
489 | - return $item; |
|
490 | - } |
|
491 | - |
|
492 | - // |
|
493 | - |
|
494 | - // Ensure the current user can delete the item |
|
495 | - if (! wpinv_can_delete_item( $request['id'] ) ) { |
|
496 | - return new WP_Error( |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Checks if a given request has access to delete an item. |
|
478 | + * |
|
479 | + * @since 1.0.13 |
|
480 | + * |
|
481 | + * @param WP_REST_Request $request Full details about the request. |
|
482 | + * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. |
|
483 | + */ |
|
484 | + public function delete_item_permissions_check( $request ) { |
|
485 | + |
|
486 | + // Retrieve the item. |
|
487 | + $item = $this->get_post( $request['id'] ); |
|
488 | + if ( is_wp_error( $item ) ) { |
|
489 | + return $item; |
|
490 | + } |
|
491 | + |
|
492 | + // |
|
493 | + |
|
494 | + // Ensure the current user can delete the item |
|
495 | + if (! wpinv_can_delete_item( $request['id'] ) ) { |
|
496 | + return new WP_Error( |
|
497 | 497 | 'rest_cannot_delete', |
498 | 498 | __( 'Sorry, you are not allowed to delete this item.', 'invoicing' ), |
499 | 499 | array( |
500 | 500 | 'status' => rest_authorization_required_code(), |
501 | 501 | ) |
502 | 502 | ); |
503 | - } |
|
504 | - |
|
505 | - return true; |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * Deletes a single item. |
|
510 | - * |
|
511 | - * @since 1.0.13 |
|
512 | - * |
|
513 | - * @param WP_REST_Request $request Full details about the request. |
|
514 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
515 | - */ |
|
516 | - public function delete_item( $request ) { |
|
503 | + } |
|
504 | + |
|
505 | + return true; |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * Deletes a single item. |
|
510 | + * |
|
511 | + * @since 1.0.13 |
|
512 | + * |
|
513 | + * @param WP_REST_Request $request Full details about the request. |
|
514 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
515 | + */ |
|
516 | + public function delete_item( $request ) { |
|
517 | 517 | |
518 | - // Retrieve the item. |
|
519 | - $item = $this->get_post( $request['id'] ); |
|
520 | - if ( is_wp_error( $item ) ) { |
|
521 | - return $item; |
|
522 | - } |
|
518 | + // Retrieve the item. |
|
519 | + $item = $this->get_post( $request['id'] ); |
|
520 | + if ( is_wp_error( $item ) ) { |
|
521 | + return $item; |
|
522 | + } |
|
523 | 523 | |
524 | - $request->set_param( 'context', 'edit' ); |
|
524 | + $request->set_param( 'context', 'edit' ); |
|
525 | 525 | |
526 | - // Prepare the item id |
|
527 | - $id = $item->ID; |
|
526 | + // Prepare the item id |
|
527 | + $id = $item->ID; |
|
528 | 528 | |
529 | - // Prepare the response |
|
530 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
529 | + // Prepare the response |
|
530 | + $response = $this->prepare_item_for_response( $item, $request ); |
|
531 | 531 | |
532 | - // Check if the user wants to bypass the trash... |
|
533 | - $force_delete = (bool) $request['force']; |
|
532 | + // Check if the user wants to bypass the trash... |
|
533 | + $force_delete = (bool) $request['force']; |
|
534 | 534 | |
535 | - // Try deleting the item. |
|
536 | - $deleted = wp_delete_post( $id, $force_delete ); |
|
535 | + // Try deleting the item. |
|
536 | + $deleted = wp_delete_post( $id, $force_delete ); |
|
537 | 537 | |
538 | - // Abort early if we can't delete the item. |
|
539 | - if ( ! $deleted ) { |
|
540 | - return new WP_Error( 'rest_cannot_delete', __( 'The item cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
541 | - } |
|
538 | + // Abort early if we can't delete the item. |
|
539 | + if ( ! $deleted ) { |
|
540 | + return new WP_Error( 'rest_cannot_delete', __( 'The item cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
541 | + } |
|
542 | 542 | |
543 | - /** |
|
544 | - * Fires immediately after a single item is deleted or trashed via the REST API. |
|
545 | - * |
|
546 | - * |
|
547 | - * @since 1.0.13 |
|
548 | - * |
|
549 | - * @param WPInv_Item $item The deleted or trashed item. |
|
550 | - * @param WP_REST_Request $request The request sent to the API. |
|
551 | - */ |
|
552 | - do_action( "wpinv_rest_delete_item", $item, $request ); |
|
543 | + /** |
|
544 | + * Fires immediately after a single item is deleted or trashed via the REST API. |
|
545 | + * |
|
546 | + * |
|
547 | + * @since 1.0.13 |
|
548 | + * |
|
549 | + * @param WPInv_Item $item The deleted or trashed item. |
|
550 | + * @param WP_REST_Request $request The request sent to the API. |
|
551 | + */ |
|
552 | + do_action( "wpinv_rest_delete_item", $item, $request ); |
|
553 | 553 | |
554 | - return $response; |
|
554 | + return $response; |
|
555 | 555 | |
556 | - } |
|
556 | + } |
|
557 | 557 | |
558 | 558 | |
559 | 559 | /** |
560 | - * Retrieves the query params for the items collection. |
|
561 | - * |
|
562 | - * @since 1.0.13 |
|
563 | - * |
|
564 | - * @return array Collection parameters. |
|
565 | - */ |
|
566 | - public function get_collection_params() { |
|
560 | + * Retrieves the query params for the items collection. |
|
561 | + * |
|
562 | + * @since 1.0.13 |
|
563 | + * |
|
564 | + * @return array Collection parameters. |
|
565 | + */ |
|
566 | + public function get_collection_params() { |
|
567 | 567 | |
568 | 568 | $query_params = array( |
569 | 569 | |
@@ -577,27 +577,27 @@ discard block |
||
577 | 577 | |
578 | 578 | // Item types |
579 | 579 | 'type' => array( |
580 | - 'description' => __( 'Type of items to fetch.', 'invoicing' ), |
|
581 | - 'type' => 'array', |
|
582 | - 'default' => wpinv_item_types(), |
|
583 | - 'items' => array( |
|
580 | + 'description' => __( 'Type of items to fetch.', 'invoicing' ), |
|
581 | + 'type' => 'array', |
|
582 | + 'default' => wpinv_item_types(), |
|
583 | + 'items' => array( |
|
584 | 584 | 'enum' => wpinv_item_types(), |
585 | 585 | 'type' => 'string', |
586 | 586 | ), |
587 | - ), |
|
587 | + ), |
|
588 | 588 | |
589 | - // Number of results per page |
|
589 | + // Number of results per page |
|
590 | 590 | 'limit' => array( |
591 | - 'description' => __( 'Number of items to fetch.', 'invoicing' ), |
|
592 | - 'type' => 'integer', |
|
593 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
591 | + 'description' => __( 'Number of items to fetch.', 'invoicing' ), |
|
592 | + 'type' => 'integer', |
|
593 | + 'default' => (int) get_option( 'posts_per_page' ), |
|
594 | 594 | ), |
595 | 595 | |
596 | 596 | // Pagination |
597 | 597 | 'page' => array( |
598 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
599 | - 'type' => 'integer', |
|
600 | - 'default' => 1, |
|
598 | + 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
599 | + 'type' => 'integer', |
|
600 | + 'default' => 1, |
|
601 | 601 | ), |
602 | 602 | |
603 | 603 | // Exclude certain items |
@@ -620,9 +620,9 @@ discard block |
||
620 | 620 | 'date', |
621 | 621 | 'ID', |
622 | 622 | 'modified', |
623 | - 'title', |
|
624 | - 'relevance', |
|
625 | - 'rand' |
|
623 | + 'title', |
|
624 | + 'relevance', |
|
625 | + 'rand' |
|
626 | 626 | ), |
627 | 627 | ), |
628 | 628 | |
@@ -632,617 +632,617 @@ discard block |
||
632 | 632 | 'type' => 'string', |
633 | 633 | 'default' => 'DESC', |
634 | 634 | 'enum' => array( 'ASC', 'DESC' ), |
635 | - ), |
|
635 | + ), |
|
636 | 636 | |
637 | - // Search term |
|
637 | + // Search term |
|
638 | 638 | 'search' => array( |
639 | - 'description' => __( 'Return items that match the search term.', 'invoicing' ), |
|
640 | - 'type' => 'string', |
|
639 | + 'description' => __( 'Return items that match the search term.', 'invoicing' ), |
|
640 | + 'type' => 'string', |
|
641 | 641 | ), |
642 | 642 | ); |
643 | 643 | |
644 | - /** |
|
645 | - * Filter collection parameters for the items controller. |
|
646 | - * |
|
647 | - * |
|
648 | - * @since 1.0.13 |
|
649 | - * |
|
650 | - * @param array $query_params JSON Schema-formatted collection parameters. |
|
651 | - */ |
|
652 | - return apply_filters( "wpinv_rest_items_collection_params", $query_params ); |
|
644 | + /** |
|
645 | + * Filter collection parameters for the items controller. |
|
646 | + * |
|
647 | + * |
|
648 | + * @since 1.0.13 |
|
649 | + * |
|
650 | + * @param array $query_params JSON Schema-formatted collection parameters. |
|
651 | + */ |
|
652 | + return apply_filters( "wpinv_rest_items_collection_params", $query_params ); |
|
653 | 653 | } |
654 | 654 | |
655 | 655 | /** |
656 | - * Checks if a given post type can be viewed or managed. |
|
657 | - * |
|
658 | - * @since 1.0.13 |
|
659 | - * |
|
660 | - * @param object|string $post_type Post type name or object. |
|
661 | - * @return bool Whether the post type is allowed in REST. |
|
662 | - */ |
|
663 | - protected function check_is_post_type_allowed( $post_type ) { |
|
664 | - return true; |
|
665 | - } |
|
666 | - |
|
667 | - /** |
|
668 | - * Prepares a single item for create or update. |
|
669 | - * |
|
670 | - * @since 1.0.13 |
|
671 | - * |
|
672 | - * @param WP_REST_Request $request Request object. |
|
673 | - * @return array|WP_Error Invoice Properties or WP_Error. |
|
674 | - */ |
|
675 | - protected function prepare_item_for_database( $request ) { |
|
676 | - $prepared_item = new stdClass(); |
|
677 | - |
|
678 | - // Post ID. |
|
679 | - if ( isset( $request['id'] ) ) { |
|
680 | - $existing_item = $this->get_post( $request['id'] ); |
|
681 | - if ( is_wp_error( $existing_item ) ) { |
|
682 | - return $existing_item; |
|
683 | - } |
|
684 | - |
|
685 | - $prepared_item->ID = $existing_item->ID; |
|
686 | - } |
|
687 | - |
|
688 | - $schema = $this->get_item_schema(); |
|
689 | - |
|
690 | - // item title. |
|
691 | - if ( ! empty( $schema['properties']['name'] ) && isset( $request['name'] ) ) { |
|
692 | - $prepared_item->title = sanitize_text_field( $request['name'] ); |
|
693 | - } |
|
694 | - |
|
695 | - // item summary. |
|
696 | - if ( ! empty( $schema['properties']['summary'] ) && isset( $request['summary'] ) ) { |
|
697 | - $prepared_item->excerpt = wp_kses_post( $request['summary'] ); |
|
698 | - } |
|
699 | - |
|
700 | - // item price. |
|
701 | - if ( ! empty( $schema['properties']['price'] ) && isset( $request['price'] ) ) { |
|
702 | - $prepared_item->price = floatval( $request['price'] ); |
|
703 | - } |
|
704 | - |
|
705 | - // minimum price (for dynamc items). |
|
706 | - if ( ! empty( $schema['properties']['minimum_price'] ) && isset( $request['minimum_price'] ) ) { |
|
707 | - $prepared_item->minimum_price = floatval( $request['minimum_price'] ); |
|
708 | - } |
|
709 | - |
|
710 | - // item status. |
|
711 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
712 | - $prepared_item->status = 'publish' === $request['status'] ? 'publish' : 'pending'; |
|
713 | - } |
|
714 | - |
|
715 | - // item type. |
|
716 | - if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) ) { |
|
717 | - $prepared_item->type = in_array( $request['type'], wpinv_item_types() ) ? trim( strtolower( $request['type'] ) ) : 'custom'; |
|
718 | - } |
|
719 | - |
|
720 | - // VAT rule. |
|
721 | - if ( ! empty( $schema['properties']['vat_rule'] ) && isset( $request['vat_rule'] ) ) { |
|
722 | - $prepared_item->vat_rule = 'digital' === $request['vat_rule'] ? 'digital' : 'physical'; |
|
723 | - } |
|
724 | - |
|
725 | - // Simple strings. |
|
726 | - foreach( array( 'custom_id', 'custom_name', 'custom_singular_name' ) as $property ) { |
|
727 | - |
|
728 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
729 | - $prepared_item->$property = sanitize_text_field( $request[$property] ); |
|
730 | - } |
|
731 | - |
|
732 | - } |
|
733 | - |
|
734 | - // Simple integers. |
|
735 | - foreach( array( 'is_recurring', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_interval', 'dynamic_pricing', 'editable' ) as $property ) { |
|
736 | - |
|
737 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
738 | - $prepared_item->$property = intval( $request[$property] ); |
|
739 | - } |
|
740 | - |
|
741 | - } |
|
742 | - |
|
743 | - // Time periods. |
|
744 | - foreach( array( 'recurring_period', 'trial_period' ) as $property ) { |
|
745 | - |
|
746 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
747 | - $prepared_item->$property = in_array( $request[$property], array( 'D', 'W', 'M', 'Y' ) ) ? trim( strtoupper( $request[$property] ) ) : 'D'; |
|
748 | - } |
|
749 | - |
|
750 | - } |
|
751 | - |
|
752 | - $item_data = (array) wp_unslash( $prepared_item ); |
|
753 | - |
|
754 | - /** |
|
755 | - * Filters an item before it is inserted via the REST API. |
|
756 | - * |
|
757 | - * @since 1.0.13 |
|
758 | - * |
|
759 | - * @param array $item_data An array of item data |
|
760 | - * @param WP_REST_Request $request Request object. |
|
761 | - */ |
|
762 | - return apply_filters( "wpinv_rest_pre_insert_item", $item_data, $request ); |
|
763 | - |
|
764 | - } |
|
765 | - |
|
766 | - /** |
|
767 | - * Prepares a single item output for response. |
|
768 | - * |
|
769 | - * @since 1.0.13 |
|
770 | - * |
|
771 | - * @param WPInv_Item $item item object. |
|
772 | - * @param WP_REST_Request $request Request object. |
|
773 | - * @return WP_REST_Response Response object. |
|
774 | - */ |
|
775 | - public function prepare_item_for_response( $item, $request ) { |
|
656 | + * Checks if a given post type can be viewed or managed. |
|
657 | + * |
|
658 | + * @since 1.0.13 |
|
659 | + * |
|
660 | + * @param object|string $post_type Post type name or object. |
|
661 | + * @return bool Whether the post type is allowed in REST. |
|
662 | + */ |
|
663 | + protected function check_is_post_type_allowed( $post_type ) { |
|
664 | + return true; |
|
665 | + } |
|
776 | 666 | |
777 | - $GLOBALS['post'] = get_post( $item->get_ID() ); |
|
667 | + /** |
|
668 | + * Prepares a single item for create or update. |
|
669 | + * |
|
670 | + * @since 1.0.13 |
|
671 | + * |
|
672 | + * @param WP_REST_Request $request Request object. |
|
673 | + * @return array|WP_Error Invoice Properties or WP_Error. |
|
674 | + */ |
|
675 | + protected function prepare_item_for_database( $request ) { |
|
676 | + $prepared_item = new stdClass(); |
|
677 | + |
|
678 | + // Post ID. |
|
679 | + if ( isset( $request['id'] ) ) { |
|
680 | + $existing_item = $this->get_post( $request['id'] ); |
|
681 | + if ( is_wp_error( $existing_item ) ) { |
|
682 | + return $existing_item; |
|
683 | + } |
|
778 | 684 | |
779 | - setup_postdata( $item->get_ID() ); |
|
685 | + $prepared_item->ID = $existing_item->ID; |
|
686 | + } |
|
780 | 687 | |
781 | - // Fetch the fields to include in this response. |
|
782 | - $fields = $this->get_fields_for_response( $request ); |
|
688 | + $schema = $this->get_item_schema(); |
|
783 | 689 | |
784 | - // Base fields for every item. |
|
785 | - $data = array(); |
|
690 | + // item title. |
|
691 | + if ( ! empty( $schema['properties']['name'] ) && isset( $request['name'] ) ) { |
|
692 | + $prepared_item->title = sanitize_text_field( $request['name'] ); |
|
693 | + } |
|
786 | 694 | |
787 | - // Set up ID |
|
788 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
789 | - $data['id'] = $item->get_ID(); |
|
790 | - } |
|
695 | + // item summary. |
|
696 | + if ( ! empty( $schema['properties']['summary'] ) && isset( $request['summary'] ) ) { |
|
697 | + $prepared_item->excerpt = wp_kses_post( $request['summary'] ); |
|
698 | + } |
|
791 | 699 | |
700 | + // item price. |
|
701 | + if ( ! empty( $schema['properties']['price'] ) && isset( $request['price'] ) ) { |
|
702 | + $prepared_item->price = floatval( $request['price'] ); |
|
703 | + } |
|
792 | 704 | |
793 | - // Item properties |
|
794 | - $item_properties = array( |
|
795 | - 'name', 'summary', 'price', 'status', 'type', |
|
796 | - 'vat_rule', 'vat_class', |
|
797 | - 'custom_id', 'custom_name', 'custom_singular_name', |
|
798 | - 'editable' |
|
799 | - ); |
|
705 | + // minimum price (for dynamc items). |
|
706 | + if ( ! empty( $schema['properties']['minimum_price'] ) && isset( $request['minimum_price'] ) ) { |
|
707 | + $prepared_item->minimum_price = floatval( $request['minimum_price'] ); |
|
708 | + } |
|
709 | + |
|
710 | + // item status. |
|
711 | + if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
712 | + $prepared_item->status = 'publish' === $request['status'] ? 'publish' : 'pending'; |
|
713 | + } |
|
800 | 714 | |
801 | - foreach( $item_properties as $property ) { |
|
715 | + // item type. |
|
716 | + if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) ) { |
|
717 | + $prepared_item->type = in_array( $request['type'], wpinv_item_types() ) ? trim( strtolower( $request['type'] ) ) : 'custom'; |
|
718 | + } |
|
802 | 719 | |
803 | - if ( rest_is_field_included( $property, $fields ) && method_exists( $item, 'get_' . $property ) ) { |
|
804 | - $data[$property] = call_user_func( array( $item, 'get_' . $property ) ); |
|
805 | - } |
|
720 | + // VAT rule. |
|
721 | + if ( ! empty( $schema['properties']['vat_rule'] ) && isset( $request['vat_rule'] ) ) { |
|
722 | + $prepared_item->vat_rule = 'digital' === $request['vat_rule'] ? 'digital' : 'physical'; |
|
723 | + } |
|
806 | 724 | |
807 | - } |
|
725 | + // Simple strings. |
|
726 | + foreach( array( 'custom_id', 'custom_name', 'custom_singular_name' ) as $property ) { |
|
808 | 727 | |
809 | - // Dynamic pricing. |
|
810 | - if( $item->supports_dynamic_pricing() ) { |
|
728 | + if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
729 | + $prepared_item->$property = sanitize_text_field( $request[$property] ); |
|
730 | + } |
|
811 | 731 | |
812 | - if( rest_is_field_included( 'dynamic_pricing', $fields ) ) { |
|
813 | - $data['dynamic_pricing'] = $item->get_is_dynamic_pricing(); |
|
814 | - } |
|
732 | + } |
|
815 | 733 | |
816 | - if( rest_is_field_included( 'minimum_price', $fields ) ) { |
|
817 | - $data['minimum_price'] = $item->get_minimum_price(); |
|
818 | - } |
|
819 | - } |
|
734 | + // Simple integers. |
|
735 | + foreach( array( 'is_recurring', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_interval', 'dynamic_pricing', 'editable' ) as $property ) { |
|
820 | 736 | |
821 | - // Subscriptions. |
|
822 | - if( rest_is_field_included( 'is_recurring', $fields ) ) { |
|
823 | - $data['is_recurring'] = $item->get_is_recurring(); |
|
824 | - } |
|
737 | + if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
738 | + $prepared_item->$property = intval( $request[$property] ); |
|
739 | + } |
|
825 | 740 | |
826 | - if( $item->is_recurring() ) { |
|
741 | + } |
|
827 | 742 | |
828 | - $recurring_fields = array( 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial' ); |
|
829 | - foreach( $recurring_fields as $field ) { |
|
743 | + // Time periods. |
|
744 | + foreach( array( 'recurring_period', 'trial_period' ) as $property ) { |
|
830 | 745 | |
831 | - if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
832 | - $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
833 | - } |
|
746 | + if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
747 | + $prepared_item->$property = in_array( $request[$property], array( 'D', 'W', 'M', 'Y' ) ) ? trim( strtoupper( $request[$property] ) ) : 'D'; |
|
748 | + } |
|
749 | + |
|
750 | + } |
|
751 | + |
|
752 | + $item_data = (array) wp_unslash( $prepared_item ); |
|
753 | + |
|
754 | + /** |
|
755 | + * Filters an item before it is inserted via the REST API. |
|
756 | + * |
|
757 | + * @since 1.0.13 |
|
758 | + * |
|
759 | + * @param array $item_data An array of item data |
|
760 | + * @param WP_REST_Request $request Request object. |
|
761 | + */ |
|
762 | + return apply_filters( "wpinv_rest_pre_insert_item", $item_data, $request ); |
|
763 | + |
|
764 | + } |
|
765 | + |
|
766 | + /** |
|
767 | + * Prepares a single item output for response. |
|
768 | + * |
|
769 | + * @since 1.0.13 |
|
770 | + * |
|
771 | + * @param WPInv_Item $item item object. |
|
772 | + * @param WP_REST_Request $request Request object. |
|
773 | + * @return WP_REST_Response Response object. |
|
774 | + */ |
|
775 | + public function prepare_item_for_response( $item, $request ) { |
|
776 | + |
|
777 | + $GLOBALS['post'] = get_post( $item->get_ID() ); |
|
778 | + |
|
779 | + setup_postdata( $item->get_ID() ); |
|
780 | + |
|
781 | + // Fetch the fields to include in this response. |
|
782 | + $fields = $this->get_fields_for_response( $request ); |
|
783 | + |
|
784 | + // Base fields for every item. |
|
785 | + $data = array(); |
|
786 | + |
|
787 | + // Set up ID |
|
788 | + if ( rest_is_field_included( 'id', $fields ) ) { |
|
789 | + $data['id'] = $item->get_ID(); |
|
790 | + } |
|
791 | + |
|
792 | + |
|
793 | + // Item properties |
|
794 | + $item_properties = array( |
|
795 | + 'name', 'summary', 'price', 'status', 'type', |
|
796 | + 'vat_rule', 'vat_class', |
|
797 | + 'custom_id', 'custom_name', 'custom_singular_name', |
|
798 | + 'editable' |
|
799 | + ); |
|
800 | + |
|
801 | + foreach( $item_properties as $property ) { |
|
802 | + |
|
803 | + if ( rest_is_field_included( $property, $fields ) && method_exists( $item, 'get_' . $property ) ) { |
|
804 | + $data[$property] = call_user_func( array( $item, 'get_' . $property ) ); |
|
805 | + } |
|
806 | + |
|
807 | + } |
|
808 | + |
|
809 | + // Dynamic pricing. |
|
810 | + if( $item->supports_dynamic_pricing() ) { |
|
811 | + |
|
812 | + if( rest_is_field_included( 'dynamic_pricing', $fields ) ) { |
|
813 | + $data['dynamic_pricing'] = $item->get_is_dynamic_pricing(); |
|
814 | + } |
|
815 | + |
|
816 | + if( rest_is_field_included( 'minimum_price', $fields ) ) { |
|
817 | + $data['minimum_price'] = $item->get_minimum_price(); |
|
818 | + } |
|
819 | + } |
|
820 | + |
|
821 | + // Subscriptions. |
|
822 | + if( rest_is_field_included( 'is_recurring', $fields ) ) { |
|
823 | + $data['is_recurring'] = $item->get_is_recurring(); |
|
824 | + } |
|
825 | + |
|
826 | + if( $item->is_recurring() ) { |
|
827 | + |
|
828 | + $recurring_fields = array( 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial' ); |
|
829 | + foreach( $recurring_fields as $field ) { |
|
830 | + |
|
831 | + if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
832 | + $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
833 | + } |
|
834 | 834 | |
835 | - } |
|
835 | + } |
|
836 | 836 | |
837 | - if( $item->has_free_trial() ) { |
|
837 | + if( $item->has_free_trial() ) { |
|
838 | 838 | |
839 | - $trial_fields = array( 'trial_period', 'trial_interval' ); |
|
840 | - foreach( $trial_fields as $field ) { |
|
839 | + $trial_fields = array( 'trial_period', 'trial_interval' ); |
|
840 | + foreach( $trial_fields as $field ) { |
|
841 | 841 | |
842 | - if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
843 | - $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
844 | - } |
|
842 | + if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
843 | + $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
844 | + } |
|
845 | 845 | |
846 | - } |
|
847 | - |
|
848 | - } |
|
849 | - |
|
850 | - } |
|
851 | - |
|
852 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
853 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
854 | - $data = $this->filter_response_by_context( $data, $context ); |
|
855 | - |
|
856 | - // Wrap the data in a response object. |
|
857 | - $response = rest_ensure_response( $data ); |
|
858 | - |
|
859 | - $links = $this->prepare_links( $item ); |
|
860 | - $response->add_links( $links ); |
|
861 | - |
|
862 | - if ( ! empty( $links['self']['href'] ) ) { |
|
863 | - $actions = $this->get_available_actions( $item, $request ); |
|
864 | - |
|
865 | - $self = $links['self']['href']; |
|
866 | - |
|
867 | - foreach ( $actions as $rel ) { |
|
868 | - $response->add_link( $rel, $self ); |
|
869 | - } |
|
870 | - } |
|
871 | - |
|
872 | - /** |
|
873 | - * Filters the item data for a response. |
|
874 | - * |
|
875 | - * @since 1.0.13 |
|
876 | - * |
|
877 | - * @param WP_REST_Response $response The response object. |
|
878 | - * @param WPInv_Item $item The item object. |
|
879 | - * @param WP_REST_Request $request Request object. |
|
880 | - */ |
|
881 | - return apply_filters( "wpinv_rest_prepare_item", $response, $item, $request ); |
|
882 | - } |
|
883 | - |
|
884 | - /** |
|
885 | - * Gets an array of fields to be included on the response. |
|
886 | - * |
|
887 | - * Included fields are based on item schema and `_fields=` request argument. |
|
888 | - * |
|
889 | - * @since 1.0.13 |
|
890 | - * |
|
891 | - * @param WP_REST_Request $request Full details about the request. |
|
892 | - * @return array Fields to be included in the response. |
|
893 | - */ |
|
894 | - public function get_fields_for_response( $request ) { |
|
895 | - $schema = $this->get_item_schema(); |
|
896 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
897 | - |
|
898 | - $additional_fields = $this->get_additional_fields(); |
|
899 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
900 | - // For back-compat, include any field with an empty schema |
|
901 | - // because it won't be present in $this->get_item_schema(). |
|
902 | - if ( is_null( $field_options['schema'] ) ) { |
|
903 | - $properties[ $field_name ] = $field_options; |
|
904 | - } |
|
905 | - } |
|
906 | - |
|
907 | - // Exclude fields that specify a different context than the request context. |
|
908 | - $context = $request['context']; |
|
909 | - if ( $context ) { |
|
910 | - foreach ( $properties as $name => $options ) { |
|
911 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
912 | - unset( $properties[ $name ] ); |
|
913 | - } |
|
914 | - } |
|
915 | - } |
|
916 | - |
|
917 | - $fields = array_keys( $properties ); |
|
918 | - |
|
919 | - if ( ! isset( $request['_fields'] ) ) { |
|
920 | - return $fields; |
|
921 | - } |
|
922 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
923 | - if ( 0 === count( $requested_fields ) ) { |
|
924 | - return $fields; |
|
925 | - } |
|
926 | - // Trim off outside whitespace from the comma delimited list. |
|
927 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
928 | - // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
929 | - if ( in_array( 'id', $fields, true ) ) { |
|
930 | - $requested_fields[] = 'id'; |
|
931 | - } |
|
932 | - // Return the list of all requested fields which appear in the schema. |
|
933 | - return array_reduce( |
|
934 | - $requested_fields, |
|
935 | - function( $response_fields, $field ) use ( $fields ) { |
|
936 | - if ( in_array( $field, $fields, true ) ) { |
|
937 | - $response_fields[] = $field; |
|
938 | - return $response_fields; |
|
939 | - } |
|
940 | - // Check for nested fields if $field is not a direct match. |
|
941 | - $nested_fields = explode( '.', $field ); |
|
942 | - // A nested field is included so long as its top-level property is |
|
943 | - // present in the schema. |
|
944 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
945 | - $response_fields[] = $field; |
|
946 | - } |
|
947 | - return $response_fields; |
|
948 | - }, |
|
949 | - array() |
|
950 | - ); |
|
951 | - } |
|
952 | - |
|
953 | - /** |
|
954 | - * Retrieves the item's schema, conforming to JSON Schema. |
|
955 | - * |
|
956 | - * @since 1.0.13 |
|
957 | - * |
|
958 | - * @return array Item schema data. |
|
959 | - */ |
|
960 | - public function get_item_schema() { |
|
961 | - |
|
962 | - // Maybe retrieve the schema from cache. |
|
963 | - if ( $this->schema ) { |
|
964 | - return $this->add_additional_fields_schema( $this->schema ); |
|
965 | - } |
|
966 | - |
|
967 | - $schema = array( |
|
968 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
969 | - 'title' => $this->post_type, |
|
970 | - 'type' => 'object', |
|
971 | - |
|
972 | - // Base properties for every Item. |
|
973 | - 'properties' => array( |
|
974 | - |
|
975 | - 'id' => array( |
|
976 | - 'description' => __( 'Unique identifier for the item.', 'invoicing' ), |
|
977 | - 'type' => 'integer', |
|
978 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
979 | - 'readonly' => true, |
|
980 | - ), |
|
981 | - |
|
982 | - 'name' => array( |
|
983 | - 'description' => __( 'The name for the item.', 'invoicing' ), |
|
984 | - 'type' => 'string', |
|
985 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
986 | - ), |
|
987 | - |
|
988 | - 'summary' => array( |
|
989 | - 'description' => __( 'A summary for the item.', 'invoicing' ), |
|
990 | - 'type' => 'string', |
|
991 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
992 | - ), |
|
993 | - |
|
994 | - 'price' => array( |
|
995 | - 'description' => __( 'The price for the item.', 'invoicing' ), |
|
996 | - 'type' => 'number', |
|
997 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
998 | - ), |
|
999 | - |
|
1000 | - 'status' => array( |
|
1001 | - 'description' => __( 'A named status for the item.', 'invoicing' ), |
|
1002 | - 'type' => 'string', |
|
1003 | - 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
1004 | - 'context' => array( 'view', 'edit' ), |
|
1005 | - ), |
|
1006 | - |
|
1007 | - 'type' => array( |
|
1008 | - 'description' => __( 'The item type.', 'invoicing' ), |
|
1009 | - 'type' => 'string', |
|
1010 | - 'enum' => wpinv_item_types(), |
|
1011 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1012 | - ), |
|
1013 | - |
|
1014 | - 'vat_rule' => array( |
|
1015 | - 'description' => __( 'VAT rule applied to the item.', 'invoicing' ), |
|
1016 | - 'type' => 'string', |
|
1017 | - 'enum' => array( 'digital', 'physical' ), |
|
1018 | - 'context' => array( 'view', 'edit' ), |
|
1019 | - ), |
|
1020 | - |
|
1021 | - 'vat_class' => array( |
|
1022 | - 'description' => __( 'VAT class for the item.', 'invoicing' ), |
|
1023 | - 'type' => 'string', |
|
1024 | - 'context' => array( 'view', 'edit' ), |
|
1025 | - 'readonly' => true, |
|
1026 | - ), |
|
1027 | - |
|
1028 | - 'custom_id' => array( |
|
1029 | - 'description' => __( 'Custom id for the item.', 'invoicing' ), |
|
1030 | - 'type' => 'string', |
|
1031 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1032 | - ), |
|
846 | + } |
|
847 | + |
|
848 | + } |
|
849 | + |
|
850 | + } |
|
851 | + |
|
852 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
853 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
854 | + $data = $this->filter_response_by_context( $data, $context ); |
|
855 | + |
|
856 | + // Wrap the data in a response object. |
|
857 | + $response = rest_ensure_response( $data ); |
|
858 | + |
|
859 | + $links = $this->prepare_links( $item ); |
|
860 | + $response->add_links( $links ); |
|
861 | + |
|
862 | + if ( ! empty( $links['self']['href'] ) ) { |
|
863 | + $actions = $this->get_available_actions( $item, $request ); |
|
864 | + |
|
865 | + $self = $links['self']['href']; |
|
866 | + |
|
867 | + foreach ( $actions as $rel ) { |
|
868 | + $response->add_link( $rel, $self ); |
|
869 | + } |
|
870 | + } |
|
871 | + |
|
872 | + /** |
|
873 | + * Filters the item data for a response. |
|
874 | + * |
|
875 | + * @since 1.0.13 |
|
876 | + * |
|
877 | + * @param WP_REST_Response $response The response object. |
|
878 | + * @param WPInv_Item $item The item object. |
|
879 | + * @param WP_REST_Request $request Request object. |
|
880 | + */ |
|
881 | + return apply_filters( "wpinv_rest_prepare_item", $response, $item, $request ); |
|
882 | + } |
|
883 | + |
|
884 | + /** |
|
885 | + * Gets an array of fields to be included on the response. |
|
886 | + * |
|
887 | + * Included fields are based on item schema and `_fields=` request argument. |
|
888 | + * |
|
889 | + * @since 1.0.13 |
|
890 | + * |
|
891 | + * @param WP_REST_Request $request Full details about the request. |
|
892 | + * @return array Fields to be included in the response. |
|
893 | + */ |
|
894 | + public function get_fields_for_response( $request ) { |
|
895 | + $schema = $this->get_item_schema(); |
|
896 | + $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
897 | + |
|
898 | + $additional_fields = $this->get_additional_fields(); |
|
899 | + foreach ( $additional_fields as $field_name => $field_options ) { |
|
900 | + // For back-compat, include any field with an empty schema |
|
901 | + // because it won't be present in $this->get_item_schema(). |
|
902 | + if ( is_null( $field_options['schema'] ) ) { |
|
903 | + $properties[ $field_name ] = $field_options; |
|
904 | + } |
|
905 | + } |
|
906 | + |
|
907 | + // Exclude fields that specify a different context than the request context. |
|
908 | + $context = $request['context']; |
|
909 | + if ( $context ) { |
|
910 | + foreach ( $properties as $name => $options ) { |
|
911 | + if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
912 | + unset( $properties[ $name ] ); |
|
913 | + } |
|
914 | + } |
|
915 | + } |
|
916 | + |
|
917 | + $fields = array_keys( $properties ); |
|
918 | + |
|
919 | + if ( ! isset( $request['_fields'] ) ) { |
|
920 | + return $fields; |
|
921 | + } |
|
922 | + $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
923 | + if ( 0 === count( $requested_fields ) ) { |
|
924 | + return $fields; |
|
925 | + } |
|
926 | + // Trim off outside whitespace from the comma delimited list. |
|
927 | + $requested_fields = array_map( 'trim', $requested_fields ); |
|
928 | + // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
929 | + if ( in_array( 'id', $fields, true ) ) { |
|
930 | + $requested_fields[] = 'id'; |
|
931 | + } |
|
932 | + // Return the list of all requested fields which appear in the schema. |
|
933 | + return array_reduce( |
|
934 | + $requested_fields, |
|
935 | + function( $response_fields, $field ) use ( $fields ) { |
|
936 | + if ( in_array( $field, $fields, true ) ) { |
|
937 | + $response_fields[] = $field; |
|
938 | + return $response_fields; |
|
939 | + } |
|
940 | + // Check for nested fields if $field is not a direct match. |
|
941 | + $nested_fields = explode( '.', $field ); |
|
942 | + // A nested field is included so long as its top-level property is |
|
943 | + // present in the schema. |
|
944 | + if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
945 | + $response_fields[] = $field; |
|
946 | + } |
|
947 | + return $response_fields; |
|
948 | + }, |
|
949 | + array() |
|
950 | + ); |
|
951 | + } |
|
952 | + |
|
953 | + /** |
|
954 | + * Retrieves the item's schema, conforming to JSON Schema. |
|
955 | + * |
|
956 | + * @since 1.0.13 |
|
957 | + * |
|
958 | + * @return array Item schema data. |
|
959 | + */ |
|
960 | + public function get_item_schema() { |
|
961 | + |
|
962 | + // Maybe retrieve the schema from cache. |
|
963 | + if ( $this->schema ) { |
|
964 | + return $this->add_additional_fields_schema( $this->schema ); |
|
965 | + } |
|
966 | + |
|
967 | + $schema = array( |
|
968 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
969 | + 'title' => $this->post_type, |
|
970 | + 'type' => 'object', |
|
971 | + |
|
972 | + // Base properties for every Item. |
|
973 | + 'properties' => array( |
|
974 | + |
|
975 | + 'id' => array( |
|
976 | + 'description' => __( 'Unique identifier for the item.', 'invoicing' ), |
|
977 | + 'type' => 'integer', |
|
978 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
979 | + 'readonly' => true, |
|
980 | + ), |
|
981 | + |
|
982 | + 'name' => array( |
|
983 | + 'description' => __( 'The name for the item.', 'invoicing' ), |
|
984 | + 'type' => 'string', |
|
985 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
986 | + ), |
|
987 | + |
|
988 | + 'summary' => array( |
|
989 | + 'description' => __( 'A summary for the item.', 'invoicing' ), |
|
990 | + 'type' => 'string', |
|
991 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
992 | + ), |
|
993 | + |
|
994 | + 'price' => array( |
|
995 | + 'description' => __( 'The price for the item.', 'invoicing' ), |
|
996 | + 'type' => 'number', |
|
997 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
998 | + ), |
|
999 | + |
|
1000 | + 'status' => array( |
|
1001 | + 'description' => __( 'A named status for the item.', 'invoicing' ), |
|
1002 | + 'type' => 'string', |
|
1003 | + 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
1004 | + 'context' => array( 'view', 'edit' ), |
|
1005 | + ), |
|
1006 | + |
|
1007 | + 'type' => array( |
|
1008 | + 'description' => __( 'The item type.', 'invoicing' ), |
|
1009 | + 'type' => 'string', |
|
1010 | + 'enum' => wpinv_item_types(), |
|
1011 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1012 | + ), |
|
1013 | + |
|
1014 | + 'vat_rule' => array( |
|
1015 | + 'description' => __( 'VAT rule applied to the item.', 'invoicing' ), |
|
1016 | + 'type' => 'string', |
|
1017 | + 'enum' => array( 'digital', 'physical' ), |
|
1018 | + 'context' => array( 'view', 'edit' ), |
|
1019 | + ), |
|
1020 | + |
|
1021 | + 'vat_class' => array( |
|
1022 | + 'description' => __( 'VAT class for the item.', 'invoicing' ), |
|
1023 | + 'type' => 'string', |
|
1024 | + 'context' => array( 'view', 'edit' ), |
|
1025 | + 'readonly' => true, |
|
1026 | + ), |
|
1027 | + |
|
1028 | + 'custom_id' => array( |
|
1029 | + 'description' => __( 'Custom id for the item.', 'invoicing' ), |
|
1030 | + 'type' => 'string', |
|
1031 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1032 | + ), |
|
1033 | 1033 | |
1034 | - 'custom_name' => array( |
|
1035 | - 'description' => __( 'Custom name for the item.', 'invoicing' ), |
|
1036 | - 'type' => 'string', |
|
1037 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1038 | - ), |
|
1039 | - |
|
1040 | - 'custom_singular_name' => array( |
|
1041 | - 'description' => __( 'Custom singular name for the item.', 'invoicing' ), |
|
1042 | - 'type' => 'string', |
|
1043 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1044 | - ), |
|
1045 | - |
|
1046 | - 'dynamic_pricing' => array( |
|
1047 | - 'description' => __( 'Whether the item allows a user to set their own price.', 'invoicing' ), |
|
1048 | - 'type' => 'integer', |
|
1049 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1050 | - ), |
|
1051 | - |
|
1052 | - 'minimum_price' => array( |
|
1053 | - 'description' => __( 'For dynamic prices, this is the minimum price that a user can set.', 'invoicing' ), |
|
1054 | - 'type' => 'number', |
|
1055 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1056 | - ), |
|
1057 | - |
|
1058 | - 'is_recurring' => array( |
|
1059 | - 'description' => __( 'Whether the item is a subscription item.', 'invoicing' ), |
|
1060 | - 'type' => 'integer', |
|
1061 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1062 | - ), |
|
1063 | - |
|
1064 | - 'recurring_period' => array( |
|
1065 | - 'description' => __( 'The recurring period for a recurring item.', 'invoicing' ), |
|
1066 | - 'type' => 'string', |
|
1067 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1068 | - 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1069 | - ), |
|
1070 | - |
|
1071 | - 'recurring_interval' => array( |
|
1072 | - 'description' => __( 'The recurring interval for a subscription item.', 'invoicing' ), |
|
1073 | - 'type' => 'integer', |
|
1074 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1075 | - ), |
|
1076 | - |
|
1077 | - 'recurring_limit' => array( |
|
1078 | - 'description' => __( 'The maximum number of renewals for a subscription item.', 'invoicing' ), |
|
1079 | - 'type' => 'integer', |
|
1080 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1081 | - ), |
|
1082 | - |
|
1083 | - 'free_trial' => array( |
|
1084 | - 'description' => __( 'Whether the item has a free trial period.', 'invoicing' ), |
|
1085 | - 'type' => 'integer', |
|
1086 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1087 | - ), |
|
1088 | - |
|
1089 | - 'trial_period' => array( |
|
1090 | - 'description' => __( 'The trial period of a recurring item.', 'invoicing' ), |
|
1091 | - 'type' => 'string', |
|
1092 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1093 | - 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1094 | - ), |
|
1095 | - |
|
1096 | - 'trial_interval' => array( |
|
1097 | - 'description' => __( 'The trial interval for a subscription item.', 'invoicing' ), |
|
1098 | - 'type' => 'integer', |
|
1099 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1100 | - ), |
|
1101 | - |
|
1102 | - 'editable' => array( |
|
1103 | - 'description' => __( 'Whether or not the item is editable.', 'invoicing' ), |
|
1104 | - 'type' => 'integer', |
|
1105 | - 'context' => array( 'view', 'edit' ), |
|
1106 | - ), |
|
1107 | - |
|
1108 | - ), |
|
1109 | - ); |
|
1110 | - |
|
1111 | - // Add helpful links to the item schem. |
|
1112 | - $schema['links'] = $this->get_schema_links(); |
|
1113 | - |
|
1114 | - /** |
|
1115 | - * Filters the item schema for the REST API. |
|
1116 | - * |
|
1117 | - * Enables adding extra properties to items. |
|
1118 | - * |
|
1119 | - * @since 1.0.13 |
|
1120 | - * |
|
1121 | - * @param array $schema The item schema. |
|
1122 | - */ |
|
1034 | + 'custom_name' => array( |
|
1035 | + 'description' => __( 'Custom name for the item.', 'invoicing' ), |
|
1036 | + 'type' => 'string', |
|
1037 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1038 | + ), |
|
1039 | + |
|
1040 | + 'custom_singular_name' => array( |
|
1041 | + 'description' => __( 'Custom singular name for the item.', 'invoicing' ), |
|
1042 | + 'type' => 'string', |
|
1043 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1044 | + ), |
|
1045 | + |
|
1046 | + 'dynamic_pricing' => array( |
|
1047 | + 'description' => __( 'Whether the item allows a user to set their own price.', 'invoicing' ), |
|
1048 | + 'type' => 'integer', |
|
1049 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1050 | + ), |
|
1051 | + |
|
1052 | + 'minimum_price' => array( |
|
1053 | + 'description' => __( 'For dynamic prices, this is the minimum price that a user can set.', 'invoicing' ), |
|
1054 | + 'type' => 'number', |
|
1055 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1056 | + ), |
|
1057 | + |
|
1058 | + 'is_recurring' => array( |
|
1059 | + 'description' => __( 'Whether the item is a subscription item.', 'invoicing' ), |
|
1060 | + 'type' => 'integer', |
|
1061 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1062 | + ), |
|
1063 | + |
|
1064 | + 'recurring_period' => array( |
|
1065 | + 'description' => __( 'The recurring period for a recurring item.', 'invoicing' ), |
|
1066 | + 'type' => 'string', |
|
1067 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1068 | + 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1069 | + ), |
|
1070 | + |
|
1071 | + 'recurring_interval' => array( |
|
1072 | + 'description' => __( 'The recurring interval for a subscription item.', 'invoicing' ), |
|
1073 | + 'type' => 'integer', |
|
1074 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1075 | + ), |
|
1076 | + |
|
1077 | + 'recurring_limit' => array( |
|
1078 | + 'description' => __( 'The maximum number of renewals for a subscription item.', 'invoicing' ), |
|
1079 | + 'type' => 'integer', |
|
1080 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1081 | + ), |
|
1082 | + |
|
1083 | + 'free_trial' => array( |
|
1084 | + 'description' => __( 'Whether the item has a free trial period.', 'invoicing' ), |
|
1085 | + 'type' => 'integer', |
|
1086 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1087 | + ), |
|
1088 | + |
|
1089 | + 'trial_period' => array( |
|
1090 | + 'description' => __( 'The trial period of a recurring item.', 'invoicing' ), |
|
1091 | + 'type' => 'string', |
|
1092 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1093 | + 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1094 | + ), |
|
1095 | + |
|
1096 | + 'trial_interval' => array( |
|
1097 | + 'description' => __( 'The trial interval for a subscription item.', 'invoicing' ), |
|
1098 | + 'type' => 'integer', |
|
1099 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1100 | + ), |
|
1101 | + |
|
1102 | + 'editable' => array( |
|
1103 | + 'description' => __( 'Whether or not the item is editable.', 'invoicing' ), |
|
1104 | + 'type' => 'integer', |
|
1105 | + 'context' => array( 'view', 'edit' ), |
|
1106 | + ), |
|
1107 | + |
|
1108 | + ), |
|
1109 | + ); |
|
1110 | + |
|
1111 | + // Add helpful links to the item schem. |
|
1112 | + $schema['links'] = $this->get_schema_links(); |
|
1113 | + |
|
1114 | + /** |
|
1115 | + * Filters the item schema for the REST API. |
|
1116 | + * |
|
1117 | + * Enables adding extra properties to items. |
|
1118 | + * |
|
1119 | + * @since 1.0.13 |
|
1120 | + * |
|
1121 | + * @param array $schema The item schema. |
|
1122 | + */ |
|
1123 | 1123 | $schema = apply_filters( "wpinv_rest_item_schema", $schema ); |
1124 | 1124 | |
1125 | - // Cache the item schema. |
|
1126 | - $this->schema = $schema; |
|
1125 | + // Cache the item schema. |
|
1126 | + $this->schema = $schema; |
|
1127 | 1127 | |
1128 | - return $this->add_additional_fields_schema( $this->schema ); |
|
1129 | - } |
|
1130 | - |
|
1131 | - /** |
|
1132 | - * Retrieve Link Description Objects that should be added to the Schema for the invoices collection. |
|
1133 | - * |
|
1134 | - * @since 1.0.13 |
|
1135 | - * |
|
1136 | - * @return array |
|
1137 | - */ |
|
1138 | - protected function get_schema_links() { |
|
1139 | - |
|
1140 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1141 | - |
|
1142 | - $links = array(); |
|
1143 | - |
|
1144 | - $links[] = array( |
|
1145 | - 'rel' => 'https://api.w.org/action-publish', |
|
1146 | - 'title' => __( 'The current user can publish this item.' ), |
|
1147 | - 'href' => $href, |
|
1148 | - 'targetSchema' => array( |
|
1149 | - 'type' => 'object', |
|
1150 | - 'properties' => array( |
|
1151 | - 'status' => array( |
|
1152 | - 'type' => 'string', |
|
1153 | - 'enum' => array( 'publish', 'future' ), |
|
1154 | - ), |
|
1155 | - ), |
|
1156 | - ), |
|
1157 | - ); |
|
1158 | - |
|
1159 | - return $links; |
|
1160 | - } |
|
1161 | - |
|
1162 | - /** |
|
1163 | - * Prepares links for the request. |
|
1164 | - * |
|
1165 | - * @since 1.0.13 |
|
1166 | - * |
|
1167 | - * @param WPInv_Item $item Item Object. |
|
1168 | - * @return array Links for the given item. |
|
1169 | - */ |
|
1170 | - protected function prepare_links( $item ) { |
|
1171 | - |
|
1172 | - // Prepare the base REST API endpoint for items. |
|
1173 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1174 | - |
|
1175 | - // Entity meta. |
|
1176 | - $links = array( |
|
1177 | - 'self' => array( |
|
1178 | - 'href' => rest_url( trailingslashit( $base ) . $item->ID ), |
|
1179 | - ), |
|
1180 | - 'collection' => array( |
|
1181 | - 'href' => rest_url( $base ), |
|
1182 | - ), |
|
1183 | - ); |
|
1184 | - |
|
1185 | - /** |
|
1186 | - * Filters the returned item links for the REST API. |
|
1187 | - * |
|
1188 | - * Enables adding extra links to item API responses. |
|
1189 | - * |
|
1190 | - * @since 1.0.13 |
|
1191 | - * |
|
1192 | - * @param array $links Rest links. |
|
1193 | - */ |
|
1194 | - return apply_filters( "wpinv_rest_item_links", $links ); |
|
1195 | - |
|
1196 | - } |
|
1197 | - |
|
1198 | - /** |
|
1199 | - * Get the link relations available for the post and current user. |
|
1200 | - * |
|
1201 | - * @since 1.0.13 |
|
1202 | - * |
|
1203 | - * @param WPInv_Item $item Item object. |
|
1204 | - * @param WP_REST_Request $request Request object. |
|
1205 | - * @return array List of link relations. |
|
1206 | - */ |
|
1207 | - protected function get_available_actions( $item, $request ) { |
|
1208 | - |
|
1209 | - if ( 'edit' !== $request['context'] ) { |
|
1210 | - return array(); |
|
1211 | - } |
|
1212 | - |
|
1213 | - $rels = array(); |
|
1214 | - |
|
1215 | - // Retrieve the post type object. |
|
1216 | - $post_type = get_post_type_object( $item->post_type ); |
|
1217 | - |
|
1218 | - // Mark item as published. |
|
1219 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1220 | - $rels[] = 'https://api.w.org/action-publish'; |
|
1221 | - } |
|
1222 | - |
|
1223 | - /** |
|
1224 | - * Filters the available item link relations for the REST API. |
|
1225 | - * |
|
1226 | - * Enables adding extra link relation for the current user and request to item responses. |
|
1227 | - * |
|
1228 | - * @since 1.0.13 |
|
1229 | - * |
|
1230 | - * @param array $rels Available link relations. |
|
1231 | - */ |
|
1232 | - return apply_filters( "wpinv_rest_item_link_relations", $rels ); |
|
1233 | - } |
|
1234 | - |
|
1235 | - /** |
|
1236 | - * Handles rest requests for item types. |
|
1237 | - * |
|
1238 | - * @since 1.0.13 |
|
1239 | - * |
|
1240 | - * |
|
1241 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
1242 | - */ |
|
1243 | - public function get_item_types() { |
|
1244 | - return rest_ensure_response( wpinv_get_item_types() ); |
|
1245 | - } |
|
1128 | + return $this->add_additional_fields_schema( $this->schema ); |
|
1129 | + } |
|
1130 | + |
|
1131 | + /** |
|
1132 | + * Retrieve Link Description Objects that should be added to the Schema for the invoices collection. |
|
1133 | + * |
|
1134 | + * @since 1.0.13 |
|
1135 | + * |
|
1136 | + * @return array |
|
1137 | + */ |
|
1138 | + protected function get_schema_links() { |
|
1139 | + |
|
1140 | + $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1141 | + |
|
1142 | + $links = array(); |
|
1143 | + |
|
1144 | + $links[] = array( |
|
1145 | + 'rel' => 'https://api.w.org/action-publish', |
|
1146 | + 'title' => __( 'The current user can publish this item.' ), |
|
1147 | + 'href' => $href, |
|
1148 | + 'targetSchema' => array( |
|
1149 | + 'type' => 'object', |
|
1150 | + 'properties' => array( |
|
1151 | + 'status' => array( |
|
1152 | + 'type' => 'string', |
|
1153 | + 'enum' => array( 'publish', 'future' ), |
|
1154 | + ), |
|
1155 | + ), |
|
1156 | + ), |
|
1157 | + ); |
|
1158 | + |
|
1159 | + return $links; |
|
1160 | + } |
|
1161 | + |
|
1162 | + /** |
|
1163 | + * Prepares links for the request. |
|
1164 | + * |
|
1165 | + * @since 1.0.13 |
|
1166 | + * |
|
1167 | + * @param WPInv_Item $item Item Object. |
|
1168 | + * @return array Links for the given item. |
|
1169 | + */ |
|
1170 | + protected function prepare_links( $item ) { |
|
1171 | + |
|
1172 | + // Prepare the base REST API endpoint for items. |
|
1173 | + $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1174 | + |
|
1175 | + // Entity meta. |
|
1176 | + $links = array( |
|
1177 | + 'self' => array( |
|
1178 | + 'href' => rest_url( trailingslashit( $base ) . $item->ID ), |
|
1179 | + ), |
|
1180 | + 'collection' => array( |
|
1181 | + 'href' => rest_url( $base ), |
|
1182 | + ), |
|
1183 | + ); |
|
1184 | + |
|
1185 | + /** |
|
1186 | + * Filters the returned item links for the REST API. |
|
1187 | + * |
|
1188 | + * Enables adding extra links to item API responses. |
|
1189 | + * |
|
1190 | + * @since 1.0.13 |
|
1191 | + * |
|
1192 | + * @param array $links Rest links. |
|
1193 | + */ |
|
1194 | + return apply_filters( "wpinv_rest_item_links", $links ); |
|
1195 | + |
|
1196 | + } |
|
1197 | + |
|
1198 | + /** |
|
1199 | + * Get the link relations available for the post and current user. |
|
1200 | + * |
|
1201 | + * @since 1.0.13 |
|
1202 | + * |
|
1203 | + * @param WPInv_Item $item Item object. |
|
1204 | + * @param WP_REST_Request $request Request object. |
|
1205 | + * @return array List of link relations. |
|
1206 | + */ |
|
1207 | + protected function get_available_actions( $item, $request ) { |
|
1208 | + |
|
1209 | + if ( 'edit' !== $request['context'] ) { |
|
1210 | + return array(); |
|
1211 | + } |
|
1212 | + |
|
1213 | + $rels = array(); |
|
1214 | + |
|
1215 | + // Retrieve the post type object. |
|
1216 | + $post_type = get_post_type_object( $item->post_type ); |
|
1217 | + |
|
1218 | + // Mark item as published. |
|
1219 | + if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1220 | + $rels[] = 'https://api.w.org/action-publish'; |
|
1221 | + } |
|
1222 | + |
|
1223 | + /** |
|
1224 | + * Filters the available item link relations for the REST API. |
|
1225 | + * |
|
1226 | + * Enables adding extra link relation for the current user and request to item responses. |
|
1227 | + * |
|
1228 | + * @since 1.0.13 |
|
1229 | + * |
|
1230 | + * @param array $rels Available link relations. |
|
1231 | + */ |
|
1232 | + return apply_filters( "wpinv_rest_item_link_relations", $rels ); |
|
1233 | + } |
|
1234 | + |
|
1235 | + /** |
|
1236 | + * Handles rest requests for item types. |
|
1237 | + * |
|
1238 | + * @since 1.0.13 |
|
1239 | + * |
|
1240 | + * |
|
1241 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
1242 | + */ |
|
1243 | + public function get_item_types() { |
|
1244 | + return rest_ensure_response( wpinv_get_item_types() ); |
|
1245 | + } |
|
1246 | 1246 | |
1247 | 1247 | |
1248 | 1248 | } |
1249 | 1249 | \ No newline at end of file |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @since 1.0.13 |
9 | 9 | */ |
10 | 10 | |
11 | -if ( !defined( 'WPINC' ) ) { |
|
11 | +if (!defined('WPINC')) { |
|
12 | 12 | exit; |
13 | 13 | } |
14 | 14 | |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @param string $namespace Api Namespace |
43 | 43 | */ |
44 | - public function __construct( $namespace ) { |
|
44 | + public function __construct($namespace) { |
|
45 | 45 | |
46 | 46 | // Set api namespace... |
47 | 47 | $this->namespace = $namespace; |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | array( |
69 | 69 | array( |
70 | 70 | 'methods' => WP_REST_Server::READABLE, |
71 | - 'callback' => array( $this, 'get_item_types' ), |
|
71 | + 'callback' => array($this, 'get_item_types'), |
|
72 | 72 | ), |
73 | 73 | ) |
74 | 74 | ); |
@@ -84,13 +84,13 @@ discard block |
||
84 | 84 | * @param WP_REST_Request $request Full details about the request. |
85 | 85 | * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
86 | 86 | */ |
87 | - public function get_items_permissions_check( $request ) { |
|
87 | + public function get_items_permissions_check($request) { |
|
88 | 88 | |
89 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
89 | + if (current_user_can('manage_options') || current_user_can('manage_invoicing')) { |
|
90 | 90 | return true; |
91 | 91 | } |
92 | 92 | |
93 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice items.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
93 | + return new WP_Error('rest_forbidden_context', __('Sorry, you are not allowed to view invoice items.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
94 | 94 | |
95 | 95 | } |
96 | 96 | |
@@ -102,17 +102,17 @@ discard block |
||
102 | 102 | * @param WP_REST_Request $request Full details about the request. |
103 | 103 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
104 | 104 | */ |
105 | - public function get_items( $request ) { |
|
105 | + public function get_items($request) { |
|
106 | 106 | |
107 | 107 | // Retrieve the list of registered item query parameters. |
108 | 108 | $registered = $this->get_collection_params(); |
109 | 109 | |
110 | 110 | $args = array(); |
111 | 111 | |
112 | - foreach( array_keys( $registered ) as $key ) { |
|
112 | + foreach (array_keys($registered) as $key) { |
|
113 | 113 | |
114 | - if( isset( $request[ $key] ) ) { |
|
115 | - $args[ $key ] = $request[ $key]; |
|
114 | + if (isset($request[$key])) { |
|
115 | + $args[$key] = $request[$key]; |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | } |
@@ -127,32 +127,32 @@ discard block |
||
127 | 127 | * @param array $args Key value array of query var to query value. |
128 | 128 | * @param WP_REST_Request $request The request used. |
129 | 129 | */ |
130 | - $args = apply_filters( "wpinv_rest_get_items_arguments", $args, $request, $this ); |
|
130 | + $args = apply_filters("wpinv_rest_get_items_arguments", $args, $request, $this); |
|
131 | 131 | |
132 | 132 | // Special args |
133 | - $args[ 'return' ] = 'objects'; |
|
134 | - $args[ 'paginate' ] = true; |
|
133 | + $args['return'] = 'objects'; |
|
134 | + $args['paginate'] = true; |
|
135 | 135 | |
136 | 136 | // Run the query. |
137 | - $query = wpinv_get_all_items( $args ); |
|
137 | + $query = wpinv_get_all_items($args); |
|
138 | 138 | |
139 | 139 | // Prepare the retrieved items |
140 | 140 | $items = array(); |
141 | - foreach( $query->items as $item ) { |
|
141 | + foreach ($query->items as $item) { |
|
142 | 142 | |
143 | - if ( ! $this->check_read_permission( $item ) ) { |
|
143 | + if (!$this->check_read_permission($item)) { |
|
144 | 144 | continue; |
145 | 145 | } |
146 | 146 | |
147 | - $data = $this->prepare_item_for_response( $item, $request ); |
|
148 | - $items[] = $this->prepare_response_for_collection( $data ); |
|
147 | + $data = $this->prepare_item_for_response($item, $request); |
|
148 | + $items[] = $this->prepare_response_for_collection($data); |
|
149 | 149 | |
150 | 150 | } |
151 | 151 | |
152 | 152 | // Prepare the response. |
153 | - $response = rest_ensure_response( $items ); |
|
154 | - $response->header( 'X-WP-Total', (int) $query->total ); |
|
155 | - $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
153 | + $response = rest_ensure_response($items); |
|
154 | + $response->header('X-WP-Total', (int) $query->total); |
|
155 | + $response->header('X-WP-TotalPages', (int) $query->max_num_pages); |
|
156 | 156 | |
157 | 157 | /** |
158 | 158 | * Filters the responses for item requests. |
@@ -165,9 +165,9 @@ discard block |
||
165 | 165 | * @param WP_REST_Request $request The request used. |
166 | 166 | * @param array $args Array of args used to retrieve the items |
167 | 167 | */ |
168 | - $response = apply_filters( "wpinv_rest_items_response", $response, $request, $args ); |
|
168 | + $response = apply_filters("wpinv_rest_items_response", $response, $request, $args); |
|
169 | 169 | |
170 | - return rest_ensure_response( $response ); |
|
170 | + return rest_ensure_response($response); |
|
171 | 171 | |
172 | 172 | } |
173 | 173 | |
@@ -179,17 +179,17 @@ discard block |
||
179 | 179 | * @param int $item_id Supplied ID. |
180 | 180 | * @return WPInv_Item|WP_Error Item object if ID is valid, WP_Error otherwise. |
181 | 181 | */ |
182 | - protected function get_post( $item_id ) { |
|
182 | + protected function get_post($item_id) { |
|
183 | 183 | |
184 | - $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid item ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
184 | + $error = new WP_Error('rest_item_invalid_id', __('Invalid item ID.', 'invoicing'), array('status' => 404)); |
|
185 | 185 | |
186 | 186 | // Ids start from 1 |
187 | - if ( (int) $item_id <= 0 ) { |
|
187 | + if ((int) $item_id <= 0) { |
|
188 | 188 | return $error; |
189 | 189 | } |
190 | 190 | |
191 | - $item = wpinv_get_item_by( 'id', (int) $item_id ); |
|
192 | - if ( empty( $item ) ) { |
|
191 | + $item = wpinv_get_item_by('id', (int) $item_id); |
|
192 | + if (empty($item)) { |
|
193 | 193 | return $error; |
194 | 194 | } |
195 | 195 | |
@@ -205,29 +205,29 @@ discard block |
||
205 | 205 | * @param WP_REST_Request $request Full details about the request. |
206 | 206 | * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
207 | 207 | */ |
208 | - public function get_item_permissions_check( $request ) { |
|
208 | + public function get_item_permissions_check($request) { |
|
209 | 209 | |
210 | 210 | // Retrieve the item object. |
211 | - $item = $this->get_post( $request['id'] ); |
|
211 | + $item = $this->get_post($request['id']); |
|
212 | 212 | |
213 | 213 | // Ensure it is valid. |
214 | - if ( is_wp_error( $item ) ) { |
|
214 | + if (is_wp_error($item)) { |
|
215 | 215 | return $item; |
216 | 216 | } |
217 | 217 | |
218 | - $post_type = get_post_type_object( $this->post_type ); |
|
218 | + $post_type = get_post_type_object($this->post_type); |
|
219 | 219 | |
220 | - if ( ! current_user_can( $post_type->cap->read_post, $item->ID ) ) { |
|
220 | + if (!current_user_can($post_type->cap->read_post, $item->ID)) { |
|
221 | 221 | return new WP_Error( |
222 | 222 | 'rest_cannot_edit', |
223 | - __( 'Sorry, you are not allowed to view this item.', 'invoicing' ), |
|
223 | + __('Sorry, you are not allowed to view this item.', 'invoicing'), |
|
224 | 224 | array( |
225 | 225 | 'status' => rest_authorization_required_code(), |
226 | 226 | ) |
227 | 227 | ); |
228 | 228 | } |
229 | 229 | |
230 | - return $this->check_read_permission( $item ); |
|
230 | + return $this->check_read_permission($item); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | /** |
@@ -241,10 +241,10 @@ discard block |
||
241 | 241 | * @param WPInv_Item $item WPInv_Item object. |
242 | 242 | * @return bool Whether the post can be read. |
243 | 243 | */ |
244 | - public function check_read_permission( $item ) { |
|
244 | + public function check_read_permission($item) { |
|
245 | 245 | |
246 | 246 | // An item can be read by an admin... |
247 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
247 | + if (current_user_can('manage_options') || current_user_can('manage_invoicing')) { |
|
248 | 248 | return true; |
249 | 249 | } |
250 | 250 | |
@@ -259,18 +259,18 @@ discard block |
||
259 | 259 | * @param WP_REST_Request $request Full details about the request. |
260 | 260 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
261 | 261 | */ |
262 | - public function get_item( $request ) { |
|
262 | + public function get_item($request) { |
|
263 | 263 | |
264 | 264 | // Fetch the item. |
265 | - $item = $this->get_post( $request['id'] ); |
|
265 | + $item = $this->get_post($request['id']); |
|
266 | 266 | |
267 | 267 | // Abort early if it does not exist |
268 | - if ( is_wp_error( $item ) ) { |
|
268 | + if (is_wp_error($item)) { |
|
269 | 269 | return $item; |
270 | 270 | } |
271 | 271 | |
272 | 272 | // Prepare the response |
273 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
273 | + $response = $this->prepare_item_for_response($item, $request); |
|
274 | 274 | |
275 | 275 | /** |
276 | 276 | * Filters the responses for single invoice item requests. |
@@ -282,9 +282,9 @@ discard block |
||
282 | 282 | * @param WP_HTTP_Response $response Response. |
283 | 283 | * @param WP_REST_Request $request The request used. |
284 | 284 | */ |
285 | - $response = apply_filters( "wpinv_rest_get_item_response", $response, $request ); |
|
285 | + $response = apply_filters("wpinv_rest_get_item_response", $response, $request); |
|
286 | 286 | |
287 | - return rest_ensure_response( $response ); |
|
287 | + return rest_ensure_response($response); |
|
288 | 288 | |
289 | 289 | } |
290 | 290 | |
@@ -296,21 +296,21 @@ discard block |
||
296 | 296 | * @param WP_REST_Request $request Full details about the request. |
297 | 297 | * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
298 | 298 | */ |
299 | - public function create_item_permissions_check( $request ) { |
|
299 | + public function create_item_permissions_check($request) { |
|
300 | 300 | |
301 | - if ( ! empty( $request['id'] ) ) { |
|
302 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
301 | + if (!empty($request['id'])) { |
|
302 | + return new WP_Error('rest_item_exists', __('Cannot create existing item.', 'invoicing'), array('status' => 400)); |
|
303 | 303 | } |
304 | 304 | |
305 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
305 | + if (current_user_can('manage_options') || current_user_can('manage_invoicing')) { |
|
306 | 306 | return true; |
307 | 307 | } |
308 | 308 | |
309 | - $post_type = get_post_type_object( $this->post_type ); |
|
310 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
309 | + $post_type = get_post_type_object($this->post_type); |
|
310 | + if (!current_user_can($post_type->cap->create_posts)) { |
|
311 | 311 | return new WP_Error( |
312 | 312 | 'rest_cannot_create', |
313 | - __( 'Sorry, you are not allowed to create invoice items as this user.', 'invoicing' ), |
|
313 | + __('Sorry, you are not allowed to create invoice items as this user.', 'invoicing'), |
|
314 | 314 | array( |
315 | 315 | 'status' => rest_authorization_required_code(), |
316 | 316 | ) |
@@ -328,30 +328,30 @@ discard block |
||
328 | 328 | * @param WP_REST_Request $request Full details about the request. |
329 | 329 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
330 | 330 | */ |
331 | - public function create_item( $request ) { |
|
331 | + public function create_item($request) { |
|
332 | 332 | |
333 | - if ( ! empty( $request['id'] ) ) { |
|
334 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing invoice item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
333 | + if (!empty($request['id'])) { |
|
334 | + return new WP_Error('rest_item_exists', __('Cannot create existing invoice item.', 'invoicing'), array('status' => 400)); |
|
335 | 335 | } |
336 | 336 | |
337 | - $request->set_param( 'context', 'edit' ); |
|
337 | + $request->set_param('context', 'edit'); |
|
338 | 338 | |
339 | 339 | // Prepare the updated data. |
340 | - $item_data = $this->prepare_item_for_database( $request ); |
|
340 | + $item_data = $this->prepare_item_for_database($request); |
|
341 | 341 | |
342 | - if ( is_wp_error( $item_data ) ) { |
|
342 | + if (is_wp_error($item_data)) { |
|
343 | 343 | return $item_data; |
344 | 344 | } |
345 | 345 | |
346 | 346 | // Try creating the item. |
347 | - $item = wpinv_create_item( $item_data, true ); |
|
347 | + $item = wpinv_create_item($item_data, true); |
|
348 | 348 | |
349 | - if ( is_wp_error( $item ) ) { |
|
349 | + if (is_wp_error($item)) { |
|
350 | 350 | return $item; |
351 | 351 | } |
352 | 352 | |
353 | 353 | // Prepare the response |
354 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
354 | + $response = $this->prepare_item_for_response($item, $request); |
|
355 | 355 | |
356 | 356 | /** |
357 | 357 | * Fires after a single invoice item is created or updated via the REST API. |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | * @param WP_REST_Request $request Request object. |
363 | 363 | * @param bool $creating True when creating a post, false when updating. |
364 | 364 | */ |
365 | - do_action( "wpinv_rest_insert_item", $item, $request, true ); |
|
365 | + do_action("wpinv_rest_insert_item", $item, $request, true); |
|
366 | 366 | |
367 | 367 | /** |
368 | 368 | * Filters the responses for creating single item requests. |
@@ -374,9 +374,9 @@ discard block |
||
374 | 374 | * @param array $item_data Invoice properties. |
375 | 375 | * @param WP_REST_Request $request The request used. |
376 | 376 | */ |
377 | - $response = apply_filters( "wpinv_rest_create_item_response", $response, $request ); |
|
377 | + $response = apply_filters("wpinv_rest_create_item_response", $response, $request); |
|
378 | 378 | |
379 | - return rest_ensure_response( $response ); |
|
379 | + return rest_ensure_response($response); |
|
380 | 380 | } |
381 | 381 | |
382 | 382 | /** |
@@ -387,21 +387,21 @@ discard block |
||
387 | 387 | * @param WP_REST_Request $request Full details about the request. |
388 | 388 | * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
389 | 389 | */ |
390 | - public function update_item_permissions_check( $request ) { |
|
390 | + public function update_item_permissions_check($request) { |
|
391 | 391 | |
392 | 392 | // Retrieve the item. |
393 | - $item = $this->get_post( $request['id'] ); |
|
394 | - if ( is_wp_error( $item ) ) { |
|
393 | + $item = $this->get_post($request['id']); |
|
394 | + if (is_wp_error($item)) { |
|
395 | 395 | return $item; |
396 | 396 | } |
397 | 397 | |
398 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
398 | + if (current_user_can('manage_options') || current_user_can('manage_invoicing')) { |
|
399 | 399 | return true; |
400 | 400 | } |
401 | 401 | |
402 | 402 | return new WP_Error( |
403 | 403 | 'rest_cannot_edit', |
404 | - __( 'Sorry, you are not allowed to update this item.', 'invoicing' ), |
|
404 | + __('Sorry, you are not allowed to update this item.', 'invoicing'), |
|
405 | 405 | array( |
406 | 406 | 'status' => rest_authorization_required_code(), |
407 | 407 | ) |
@@ -417,46 +417,46 @@ discard block |
||
417 | 417 | * @param WP_REST_Request $request Full details about the request. |
418 | 418 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
419 | 419 | */ |
420 | - public function update_item( $request ) { |
|
420 | + public function update_item($request) { |
|
421 | 421 | |
422 | 422 | // Ensure the item exists. |
423 | - $valid_check = $this->get_post( $request['id'] ); |
|
423 | + $valid_check = $this->get_post($request['id']); |
|
424 | 424 | |
425 | 425 | // Abort early if it does not exist |
426 | - if ( is_wp_error( $valid_check ) ) { |
|
426 | + if (is_wp_error($valid_check)) { |
|
427 | 427 | return $valid_check; |
428 | 428 | } |
429 | 429 | |
430 | - $request->set_param( 'context', 'edit' ); |
|
430 | + $request->set_param('context', 'edit'); |
|
431 | 431 | |
432 | 432 | // Prepare the updated data. |
433 | - $data_to_update = $this->prepare_item_for_database( $request ); |
|
433 | + $data_to_update = $this->prepare_item_for_database($request); |
|
434 | 434 | |
435 | - if ( is_wp_error( $data_to_update ) ) { |
|
435 | + if (is_wp_error($data_to_update)) { |
|
436 | 436 | return $data_to_update; |
437 | 437 | } |
438 | 438 | |
439 | 439 | // Abort if no item data is provided |
440 | - if( empty( $data_to_update ) ) { |
|
441 | - return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
|
440 | + if (empty($data_to_update)) { |
|
441 | + return new WP_Error('missing_data', __('An update request cannot be empty.', 'invoicing')); |
|
442 | 442 | } |
443 | 443 | |
444 | 444 | // Include the item ID |
445 | 445 | $data_to_update['ID'] = $request['id']; |
446 | 446 | |
447 | 447 | // Update the item |
448 | - $updated_item = wpinv_update_item( $data_to_update, true ); |
|
448 | + $updated_item = wpinv_update_item($data_to_update, true); |
|
449 | 449 | |
450 | 450 | // Incase the update operation failed... |
451 | - if ( is_wp_error( $updated_item ) ) { |
|
451 | + if (is_wp_error($updated_item)) { |
|
452 | 452 | return $updated_item; |
453 | 453 | } |
454 | 454 | |
455 | 455 | // Prepare the response |
456 | - $response = $this->prepare_item_for_response( $updated_item, $request ); |
|
456 | + $response = $this->prepare_item_for_response($updated_item, $request); |
|
457 | 457 | |
458 | 458 | /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
459 | - do_action( "wpinv_rest_insert_item", $updated_item, $request, false ); |
|
459 | + do_action("wpinv_rest_insert_item", $updated_item, $request, false); |
|
460 | 460 | |
461 | 461 | /** |
462 | 462 | * Filters the responses for updating single item requests. |
@@ -468,9 +468,9 @@ discard block |
||
468 | 468 | * @param array $item_data Item properties. |
469 | 469 | * @param WP_REST_Request $request The request used. |
470 | 470 | */ |
471 | - $response = apply_filters( "wpinv_rest_update_item_response", $response, $data_to_update, $request ); |
|
471 | + $response = apply_filters("wpinv_rest_update_item_response", $response, $data_to_update, $request); |
|
472 | 472 | |
473 | - return rest_ensure_response( $response ); |
|
473 | + return rest_ensure_response($response); |
|
474 | 474 | } |
475 | 475 | |
476 | 476 | /** |
@@ -481,21 +481,21 @@ discard block |
||
481 | 481 | * @param WP_REST_Request $request Full details about the request. |
482 | 482 | * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. |
483 | 483 | */ |
484 | - public function delete_item_permissions_check( $request ) { |
|
484 | + public function delete_item_permissions_check($request) { |
|
485 | 485 | |
486 | 486 | // Retrieve the item. |
487 | - $item = $this->get_post( $request['id'] ); |
|
488 | - if ( is_wp_error( $item ) ) { |
|
487 | + $item = $this->get_post($request['id']); |
|
488 | + if (is_wp_error($item)) { |
|
489 | 489 | return $item; |
490 | 490 | } |
491 | 491 | |
492 | 492 | // |
493 | 493 | |
494 | 494 | // Ensure the current user can delete the item |
495 | - if (! wpinv_can_delete_item( $request['id'] ) ) { |
|
495 | + if (!wpinv_can_delete_item($request['id'])) { |
|
496 | 496 | return new WP_Error( |
497 | 497 | 'rest_cannot_delete', |
498 | - __( 'Sorry, you are not allowed to delete this item.', 'invoicing' ), |
|
498 | + __('Sorry, you are not allowed to delete this item.', 'invoicing'), |
|
499 | 499 | array( |
500 | 500 | 'status' => rest_authorization_required_code(), |
501 | 501 | ) |
@@ -513,31 +513,31 @@ discard block |
||
513 | 513 | * @param WP_REST_Request $request Full details about the request. |
514 | 514 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
515 | 515 | */ |
516 | - public function delete_item( $request ) { |
|
516 | + public function delete_item($request) { |
|
517 | 517 | |
518 | 518 | // Retrieve the item. |
519 | - $item = $this->get_post( $request['id'] ); |
|
520 | - if ( is_wp_error( $item ) ) { |
|
519 | + $item = $this->get_post($request['id']); |
|
520 | + if (is_wp_error($item)) { |
|
521 | 521 | return $item; |
522 | 522 | } |
523 | 523 | |
524 | - $request->set_param( 'context', 'edit' ); |
|
524 | + $request->set_param('context', 'edit'); |
|
525 | 525 | |
526 | 526 | // Prepare the item id |
527 | - $id = $item->ID; |
|
527 | + $id = $item->ID; |
|
528 | 528 | |
529 | 529 | // Prepare the response |
530 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
530 | + $response = $this->prepare_item_for_response($item, $request); |
|
531 | 531 | |
532 | 532 | // Check if the user wants to bypass the trash... |
533 | 533 | $force_delete = (bool) $request['force']; |
534 | 534 | |
535 | 535 | // Try deleting the item. |
536 | - $deleted = wp_delete_post( $id, $force_delete ); |
|
536 | + $deleted = wp_delete_post($id, $force_delete); |
|
537 | 537 | |
538 | 538 | // Abort early if we can't delete the item. |
539 | - if ( ! $deleted ) { |
|
540 | - return new WP_Error( 'rest_cannot_delete', __( 'The item cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
539 | + if (!$deleted) { |
|
540 | + return new WP_Error('rest_cannot_delete', __('The item cannot be deleted.', 'invoicing'), array('status' => 500)); |
|
541 | 541 | } |
542 | 542 | |
543 | 543 | /** |
@@ -549,7 +549,7 @@ discard block |
||
549 | 549 | * @param WPInv_Item $item The deleted or trashed item. |
550 | 550 | * @param WP_REST_Request $request The request sent to the API. |
551 | 551 | */ |
552 | - do_action( "wpinv_rest_delete_item", $item, $request ); |
|
552 | + do_action("wpinv_rest_delete_item", $item, $request); |
|
553 | 553 | |
554 | 554 | return $response; |
555 | 555 | |
@@ -565,19 +565,19 @@ discard block |
||
565 | 565 | */ |
566 | 566 | public function get_collection_params() { |
567 | 567 | |
568 | - $query_params = array( |
|
568 | + $query_params = array( |
|
569 | 569 | |
570 | 570 | // Invoice status. |
571 | 571 | 'status' => array( |
572 | 572 | 'default' => 'publish', |
573 | - 'description' => __( 'Limit result set to items assigned one or more statuses.', 'invoicing' ), |
|
573 | + 'description' => __('Limit result set to items assigned one or more statuses.', 'invoicing'), |
|
574 | 574 | 'type' => 'array', |
575 | - 'sanitize_callback' => array( $this, 'sanitize_post_statuses' ), |
|
575 | + 'sanitize_callback' => array($this, 'sanitize_post_statuses'), |
|
576 | 576 | ), |
577 | 577 | |
578 | 578 | // Item types |
579 | 579 | 'type' => array( |
580 | - 'description' => __( 'Type of items to fetch.', 'invoicing' ), |
|
580 | + 'description' => __('Type of items to fetch.', 'invoicing'), |
|
581 | 581 | 'type' => 'array', |
582 | 582 | 'default' => wpinv_item_types(), |
583 | 583 | 'items' => array( |
@@ -588,21 +588,21 @@ discard block |
||
588 | 588 | |
589 | 589 | // Number of results per page |
590 | 590 | 'limit' => array( |
591 | - 'description' => __( 'Number of items to fetch.', 'invoicing' ), |
|
591 | + 'description' => __('Number of items to fetch.', 'invoicing'), |
|
592 | 592 | 'type' => 'integer', |
593 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
593 | + 'default' => (int) get_option('posts_per_page'), |
|
594 | 594 | ), |
595 | 595 | |
596 | 596 | // Pagination |
597 | 597 | 'page' => array( |
598 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
598 | + 'description' => __('Current page to fetch.', 'invoicing'), |
|
599 | 599 | 'type' => 'integer', |
600 | 600 | 'default' => 1, |
601 | 601 | ), |
602 | 602 | |
603 | 603 | // Exclude certain items |
604 | 604 | 'exclude' => array( |
605 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'invoicing' ), |
|
605 | + 'description' => __('Ensure result set excludes specific IDs.', 'invoicing'), |
|
606 | 606 | 'type' => 'array', |
607 | 607 | 'items' => array( |
608 | 608 | 'type' => 'integer', |
@@ -612,7 +612,7 @@ discard block |
||
612 | 612 | |
613 | 613 | // Order items by |
614 | 614 | 'orderby' => array( |
615 | - 'description' => __( 'Sort items by object attribute.', 'invoicing' ), |
|
615 | + 'description' => __('Sort items by object attribute.', 'invoicing'), |
|
616 | 616 | 'type' => 'string', |
617 | 617 | 'default' => 'date', |
618 | 618 | 'enum' => array( |
@@ -628,15 +628,15 @@ discard block |
||
628 | 628 | |
629 | 629 | // How to order |
630 | 630 | 'order' => array( |
631 | - 'description' => __( 'Order sort attribute ascending or descending.', 'invoicing' ), |
|
631 | + 'description' => __('Order sort attribute ascending or descending.', 'invoicing'), |
|
632 | 632 | 'type' => 'string', |
633 | 633 | 'default' => 'DESC', |
634 | - 'enum' => array( 'ASC', 'DESC' ), |
|
634 | + 'enum' => array('ASC', 'DESC'), |
|
635 | 635 | ), |
636 | 636 | |
637 | 637 | // Search term |
638 | 638 | 'search' => array( |
639 | - 'description' => __( 'Return items that match the search term.', 'invoicing' ), |
|
639 | + 'description' => __('Return items that match the search term.', 'invoicing'), |
|
640 | 640 | 'type' => 'string', |
641 | 641 | ), |
642 | 642 | ); |
@@ -649,7 +649,7 @@ discard block |
||
649 | 649 | * |
650 | 650 | * @param array $query_params JSON Schema-formatted collection parameters. |
651 | 651 | */ |
652 | - return apply_filters( "wpinv_rest_items_collection_params", $query_params ); |
|
652 | + return apply_filters("wpinv_rest_items_collection_params", $query_params); |
|
653 | 653 | } |
654 | 654 | |
655 | 655 | /** |
@@ -660,7 +660,7 @@ discard block |
||
660 | 660 | * @param object|string $post_type Post type name or object. |
661 | 661 | * @return bool Whether the post type is allowed in REST. |
662 | 662 | */ |
663 | - protected function check_is_post_type_allowed( $post_type ) { |
|
663 | + protected function check_is_post_type_allowed($post_type) { |
|
664 | 664 | return true; |
665 | 665 | } |
666 | 666 | |
@@ -672,84 +672,84 @@ discard block |
||
672 | 672 | * @param WP_REST_Request $request Request object. |
673 | 673 | * @return array|WP_Error Invoice Properties or WP_Error. |
674 | 674 | */ |
675 | - protected function prepare_item_for_database( $request ) { |
|
675 | + protected function prepare_item_for_database($request) { |
|
676 | 676 | $prepared_item = new stdClass(); |
677 | 677 | |
678 | 678 | // Post ID. |
679 | - if ( isset( $request['id'] ) ) { |
|
680 | - $existing_item = $this->get_post( $request['id'] ); |
|
681 | - if ( is_wp_error( $existing_item ) ) { |
|
679 | + if (isset($request['id'])) { |
|
680 | + $existing_item = $this->get_post($request['id']); |
|
681 | + if (is_wp_error($existing_item)) { |
|
682 | 682 | return $existing_item; |
683 | 683 | } |
684 | 684 | |
685 | - $prepared_item->ID = $existing_item->ID; |
|
685 | + $prepared_item->ID = $existing_item->ID; |
|
686 | 686 | } |
687 | 687 | |
688 | 688 | $schema = $this->get_item_schema(); |
689 | 689 | |
690 | 690 | // item title. |
691 | - if ( ! empty( $schema['properties']['name'] ) && isset( $request['name'] ) ) { |
|
692 | - $prepared_item->title = sanitize_text_field( $request['name'] ); |
|
691 | + if (!empty($schema['properties']['name']) && isset($request['name'])) { |
|
692 | + $prepared_item->title = sanitize_text_field($request['name']); |
|
693 | 693 | } |
694 | 694 | |
695 | 695 | // item summary. |
696 | - if ( ! empty( $schema['properties']['summary'] ) && isset( $request['summary'] ) ) { |
|
697 | - $prepared_item->excerpt = wp_kses_post( $request['summary'] ); |
|
696 | + if (!empty($schema['properties']['summary']) && isset($request['summary'])) { |
|
697 | + $prepared_item->excerpt = wp_kses_post($request['summary']); |
|
698 | 698 | } |
699 | 699 | |
700 | 700 | // item price. |
701 | - if ( ! empty( $schema['properties']['price'] ) && isset( $request['price'] ) ) { |
|
702 | - $prepared_item->price = floatval( $request['price'] ); |
|
701 | + if (!empty($schema['properties']['price']) && isset($request['price'])) { |
|
702 | + $prepared_item->price = floatval($request['price']); |
|
703 | 703 | } |
704 | 704 | |
705 | 705 | // minimum price (for dynamc items). |
706 | - if ( ! empty( $schema['properties']['minimum_price'] ) && isset( $request['minimum_price'] ) ) { |
|
707 | - $prepared_item->minimum_price = floatval( $request['minimum_price'] ); |
|
706 | + if (!empty($schema['properties']['minimum_price']) && isset($request['minimum_price'])) { |
|
707 | + $prepared_item->minimum_price = floatval($request['minimum_price']); |
|
708 | 708 | } |
709 | 709 | |
710 | 710 | // item status. |
711 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
711 | + if (!empty($schema['properties']['status']) && isset($request['status'])) { |
|
712 | 712 | $prepared_item->status = 'publish' === $request['status'] ? 'publish' : 'pending'; |
713 | 713 | } |
714 | 714 | |
715 | 715 | // item type. |
716 | - if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) ) { |
|
717 | - $prepared_item->type = in_array( $request['type'], wpinv_item_types() ) ? trim( strtolower( $request['type'] ) ) : 'custom'; |
|
716 | + if (!empty($schema['properties']['type']) && isset($request['type'])) { |
|
717 | + $prepared_item->type = in_array($request['type'], wpinv_item_types()) ? trim(strtolower($request['type'])) : 'custom'; |
|
718 | 718 | } |
719 | 719 | |
720 | 720 | // VAT rule. |
721 | - if ( ! empty( $schema['properties']['vat_rule'] ) && isset( $request['vat_rule'] ) ) { |
|
721 | + if (!empty($schema['properties']['vat_rule']) && isset($request['vat_rule'])) { |
|
722 | 722 | $prepared_item->vat_rule = 'digital' === $request['vat_rule'] ? 'digital' : 'physical'; |
723 | 723 | } |
724 | 724 | |
725 | 725 | // Simple strings. |
726 | - foreach( array( 'custom_id', 'custom_name', 'custom_singular_name' ) as $property ) { |
|
726 | + foreach (array('custom_id', 'custom_name', 'custom_singular_name') as $property) { |
|
727 | 727 | |
728 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
729 | - $prepared_item->$property = sanitize_text_field( $request[$property] ); |
|
728 | + if (!empty($schema['properties'][$property]) && isset($request[$property])) { |
|
729 | + $prepared_item->$property = sanitize_text_field($request[$property]); |
|
730 | 730 | } |
731 | 731 | |
732 | 732 | } |
733 | 733 | |
734 | 734 | // Simple integers. |
735 | - foreach( array( 'is_recurring', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_interval', 'dynamic_pricing', 'editable' ) as $property ) { |
|
735 | + foreach (array('is_recurring', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_interval', 'dynamic_pricing', 'editable') as $property) { |
|
736 | 736 | |
737 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
738 | - $prepared_item->$property = intval( $request[$property] ); |
|
737 | + if (!empty($schema['properties'][$property]) && isset($request[$property])) { |
|
738 | + $prepared_item->$property = intval($request[$property]); |
|
739 | 739 | } |
740 | 740 | |
741 | 741 | } |
742 | 742 | |
743 | 743 | // Time periods. |
744 | - foreach( array( 'recurring_period', 'trial_period' ) as $property ) { |
|
744 | + foreach (array('recurring_period', 'trial_period') as $property) { |
|
745 | 745 | |
746 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
747 | - $prepared_item->$property = in_array( $request[$property], array( 'D', 'W', 'M', 'Y' ) ) ? trim( strtoupper( $request[$property] ) ) : 'D'; |
|
746 | + if (!empty($schema['properties'][$property]) && isset($request[$property])) { |
|
747 | + $prepared_item->$property = in_array($request[$property], array('D', 'W', 'M', 'Y')) ? trim(strtoupper($request[$property])) : 'D'; |
|
748 | 748 | } |
749 | 749 | |
750 | 750 | } |
751 | 751 | |
752 | - $item_data = (array) wp_unslash( $prepared_item ); |
|
752 | + $item_data = (array) wp_unslash($prepared_item); |
|
753 | 753 | |
754 | 754 | /** |
755 | 755 | * Filters an item before it is inserted via the REST API. |
@@ -759,7 +759,7 @@ discard block |
||
759 | 759 | * @param array $item_data An array of item data |
760 | 760 | * @param WP_REST_Request $request Request object. |
761 | 761 | */ |
762 | - return apply_filters( "wpinv_rest_pre_insert_item", $item_data, $request ); |
|
762 | + return apply_filters("wpinv_rest_pre_insert_item", $item_data, $request); |
|
763 | 763 | |
764 | 764 | } |
765 | 765 | |
@@ -772,20 +772,20 @@ discard block |
||
772 | 772 | * @param WP_REST_Request $request Request object. |
773 | 773 | * @return WP_REST_Response Response object. |
774 | 774 | */ |
775 | - public function prepare_item_for_response( $item, $request ) { |
|
775 | + public function prepare_item_for_response($item, $request) { |
|
776 | 776 | |
777 | - $GLOBALS['post'] = get_post( $item->get_ID() ); |
|
777 | + $GLOBALS['post'] = get_post($item->get_ID()); |
|
778 | 778 | |
779 | - setup_postdata( $item->get_ID() ); |
|
779 | + setup_postdata($item->get_ID()); |
|
780 | 780 | |
781 | 781 | // Fetch the fields to include in this response. |
782 | - $fields = $this->get_fields_for_response( $request ); |
|
782 | + $fields = $this->get_fields_for_response($request); |
|
783 | 783 | |
784 | 784 | // Base fields for every item. |
785 | 785 | $data = array(); |
786 | 786 | |
787 | 787 | // Set up ID |
788 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
788 | + if (rest_is_field_included('id', $fields)) { |
|
789 | 789 | $data['id'] = $item->get_ID(); |
790 | 790 | } |
791 | 791 | |
@@ -798,49 +798,49 @@ discard block |
||
798 | 798 | 'editable' |
799 | 799 | ); |
800 | 800 | |
801 | - foreach( $item_properties as $property ) { |
|
801 | + foreach ($item_properties as $property) { |
|
802 | 802 | |
803 | - if ( rest_is_field_included( $property, $fields ) && method_exists( $item, 'get_' . $property ) ) { |
|
804 | - $data[$property] = call_user_func( array( $item, 'get_' . $property ) ); |
|
803 | + if (rest_is_field_included($property, $fields) && method_exists($item, 'get_' . $property)) { |
|
804 | + $data[$property] = call_user_func(array($item, 'get_' . $property)); |
|
805 | 805 | } |
806 | 806 | |
807 | 807 | } |
808 | 808 | |
809 | 809 | // Dynamic pricing. |
810 | - if( $item->supports_dynamic_pricing() ) { |
|
810 | + if ($item->supports_dynamic_pricing()) { |
|
811 | 811 | |
812 | - if( rest_is_field_included( 'dynamic_pricing', $fields ) ) { |
|
812 | + if (rest_is_field_included('dynamic_pricing', $fields)) { |
|
813 | 813 | $data['dynamic_pricing'] = $item->get_is_dynamic_pricing(); |
814 | 814 | } |
815 | 815 | |
816 | - if( rest_is_field_included( 'minimum_price', $fields ) ) { |
|
816 | + if (rest_is_field_included('minimum_price', $fields)) { |
|
817 | 817 | $data['minimum_price'] = $item->get_minimum_price(); |
818 | 818 | } |
819 | 819 | } |
820 | 820 | |
821 | 821 | // Subscriptions. |
822 | - if( rest_is_field_included( 'is_recurring', $fields ) ) { |
|
822 | + if (rest_is_field_included('is_recurring', $fields)) { |
|
823 | 823 | $data['is_recurring'] = $item->get_is_recurring(); |
824 | 824 | } |
825 | 825 | |
826 | - if( $item->is_recurring() ) { |
|
826 | + if ($item->is_recurring()) { |
|
827 | 827 | |
828 | - $recurring_fields = array( 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial' ); |
|
829 | - foreach( $recurring_fields as $field ) { |
|
828 | + $recurring_fields = array('is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial'); |
|
829 | + foreach ($recurring_fields as $field) { |
|
830 | 830 | |
831 | - if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
832 | - $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
831 | + if (rest_is_field_included($field, $fields) && method_exists($item, 'get_' . $field)) { |
|
832 | + $data[$field] = call_user_func(array($item, 'get_' . $field)); |
|
833 | 833 | } |
834 | 834 | |
835 | 835 | } |
836 | 836 | |
837 | - if( $item->has_free_trial() ) { |
|
837 | + if ($item->has_free_trial()) { |
|
838 | 838 | |
839 | - $trial_fields = array( 'trial_period', 'trial_interval' ); |
|
840 | - foreach( $trial_fields as $field ) { |
|
839 | + $trial_fields = array('trial_period', 'trial_interval'); |
|
840 | + foreach ($trial_fields as $field) { |
|
841 | 841 | |
842 | - if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
843 | - $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
842 | + if (rest_is_field_included($field, $fields) && method_exists($item, 'get_' . $field)) { |
|
843 | + $data[$field] = call_user_func(array($item, 'get_' . $field)); |
|
844 | 844 | } |
845 | 845 | |
846 | 846 | } |
@@ -849,23 +849,23 @@ discard block |
||
849 | 849 | |
850 | 850 | } |
851 | 851 | |
852 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
853 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
854 | - $data = $this->filter_response_by_context( $data, $context ); |
|
852 | + $context = !empty($request['context']) ? $request['context'] : 'view'; |
|
853 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
854 | + $data = $this->filter_response_by_context($data, $context); |
|
855 | 855 | |
856 | 856 | // Wrap the data in a response object. |
857 | - $response = rest_ensure_response( $data ); |
|
857 | + $response = rest_ensure_response($data); |
|
858 | 858 | |
859 | - $links = $this->prepare_links( $item ); |
|
860 | - $response->add_links( $links ); |
|
859 | + $links = $this->prepare_links($item); |
|
860 | + $response->add_links($links); |
|
861 | 861 | |
862 | - if ( ! empty( $links['self']['href'] ) ) { |
|
863 | - $actions = $this->get_available_actions( $item, $request ); |
|
862 | + if (!empty($links['self']['href'])) { |
|
863 | + $actions = $this->get_available_actions($item, $request); |
|
864 | 864 | |
865 | 865 | $self = $links['self']['href']; |
866 | 866 | |
867 | - foreach ( $actions as $rel ) { |
|
868 | - $response->add_link( $rel, $self ); |
|
867 | + foreach ($actions as $rel) { |
|
868 | + $response->add_link($rel, $self); |
|
869 | 869 | } |
870 | 870 | } |
871 | 871 | |
@@ -878,7 +878,7 @@ discard block |
||
878 | 878 | * @param WPInv_Item $item The item object. |
879 | 879 | * @param WP_REST_Request $request Request object. |
880 | 880 | */ |
881 | - return apply_filters( "wpinv_rest_prepare_item", $response, $item, $request ); |
|
881 | + return apply_filters("wpinv_rest_prepare_item", $response, $item, $request); |
|
882 | 882 | } |
883 | 883 | |
884 | 884 | /** |
@@ -891,57 +891,57 @@ discard block |
||
891 | 891 | * @param WP_REST_Request $request Full details about the request. |
892 | 892 | * @return array Fields to be included in the response. |
893 | 893 | */ |
894 | - public function get_fields_for_response( $request ) { |
|
894 | + public function get_fields_for_response($request) { |
|
895 | 895 | $schema = $this->get_item_schema(); |
896 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
896 | + $properties = isset($schema['properties']) ? $schema['properties'] : array(); |
|
897 | 897 | |
898 | 898 | $additional_fields = $this->get_additional_fields(); |
899 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
899 | + foreach ($additional_fields as $field_name => $field_options) { |
|
900 | 900 | // For back-compat, include any field with an empty schema |
901 | 901 | // because it won't be present in $this->get_item_schema(). |
902 | - if ( is_null( $field_options['schema'] ) ) { |
|
903 | - $properties[ $field_name ] = $field_options; |
|
902 | + if (is_null($field_options['schema'])) { |
|
903 | + $properties[$field_name] = $field_options; |
|
904 | 904 | } |
905 | 905 | } |
906 | 906 | |
907 | 907 | // Exclude fields that specify a different context than the request context. |
908 | 908 | $context = $request['context']; |
909 | - if ( $context ) { |
|
910 | - foreach ( $properties as $name => $options ) { |
|
911 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
912 | - unset( $properties[ $name ] ); |
|
909 | + if ($context) { |
|
910 | + foreach ($properties as $name => $options) { |
|
911 | + if (!empty($options['context']) && !in_array($context, $options['context'], true)) { |
|
912 | + unset($properties[$name]); |
|
913 | 913 | } |
914 | 914 | } |
915 | 915 | } |
916 | 916 | |
917 | - $fields = array_keys( $properties ); |
|
917 | + $fields = array_keys($properties); |
|
918 | 918 | |
919 | - if ( ! isset( $request['_fields'] ) ) { |
|
919 | + if (!isset($request['_fields'])) { |
|
920 | 920 | return $fields; |
921 | 921 | } |
922 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
923 | - if ( 0 === count( $requested_fields ) ) { |
|
922 | + $requested_fields = wpinv_parse_list($request['_fields']); |
|
923 | + if (0 === count($requested_fields)) { |
|
924 | 924 | return $fields; |
925 | 925 | } |
926 | 926 | // Trim off outside whitespace from the comma delimited list. |
927 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
927 | + $requested_fields = array_map('trim', $requested_fields); |
|
928 | 928 | // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
929 | - if ( in_array( 'id', $fields, true ) ) { |
|
929 | + if (in_array('id', $fields, true)) { |
|
930 | 930 | $requested_fields[] = 'id'; |
931 | 931 | } |
932 | 932 | // Return the list of all requested fields which appear in the schema. |
933 | 933 | return array_reduce( |
934 | 934 | $requested_fields, |
935 | - function( $response_fields, $field ) use ( $fields ) { |
|
936 | - if ( in_array( $field, $fields, true ) ) { |
|
935 | + function($response_fields, $field) use ($fields) { |
|
936 | + if (in_array($field, $fields, true)) { |
|
937 | 937 | $response_fields[] = $field; |
938 | 938 | return $response_fields; |
939 | 939 | } |
940 | 940 | // Check for nested fields if $field is not a direct match. |
941 | - $nested_fields = explode( '.', $field ); |
|
941 | + $nested_fields = explode('.', $field); |
|
942 | 942 | // A nested field is included so long as its top-level property is |
943 | 943 | // present in the schema. |
944 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
944 | + if (in_array($nested_fields[0], $fields, true)) { |
|
945 | 945 | $response_fields[] = $field; |
946 | 946 | } |
947 | 947 | return $response_fields; |
@@ -960,8 +960,8 @@ discard block |
||
960 | 960 | public function get_item_schema() { |
961 | 961 | |
962 | 962 | // Maybe retrieve the schema from cache. |
963 | - if ( $this->schema ) { |
|
964 | - return $this->add_additional_fields_schema( $this->schema ); |
|
963 | + if ($this->schema) { |
|
964 | + return $this->add_additional_fields_schema($this->schema); |
|
965 | 965 | } |
966 | 966 | |
967 | 967 | $schema = array( |
@@ -973,136 +973,136 @@ discard block |
||
973 | 973 | 'properties' => array( |
974 | 974 | |
975 | 975 | 'id' => array( |
976 | - 'description' => __( 'Unique identifier for the item.', 'invoicing' ), |
|
976 | + 'description' => __('Unique identifier for the item.', 'invoicing'), |
|
977 | 977 | 'type' => 'integer', |
978 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
978 | + 'context' => array('view', 'edit', 'embed'), |
|
979 | 979 | 'readonly' => true, |
980 | 980 | ), |
981 | 981 | |
982 | 982 | 'name' => array( |
983 | - 'description' => __( 'The name for the item.', 'invoicing' ), |
|
983 | + 'description' => __('The name for the item.', 'invoicing'), |
|
984 | 984 | 'type' => 'string', |
985 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
985 | + 'context' => array('view', 'edit', 'embed'), |
|
986 | 986 | ), |
987 | 987 | |
988 | 988 | 'summary' => array( |
989 | - 'description' => __( 'A summary for the item.', 'invoicing' ), |
|
989 | + 'description' => __('A summary for the item.', 'invoicing'), |
|
990 | 990 | 'type' => 'string', |
991 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
991 | + 'context' => array('view', 'edit', 'embed'), |
|
992 | 992 | ), |
993 | 993 | |
994 | 994 | 'price' => array( |
995 | - 'description' => __( 'The price for the item.', 'invoicing' ), |
|
995 | + 'description' => __('The price for the item.', 'invoicing'), |
|
996 | 996 | 'type' => 'number', |
997 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
997 | + 'context' => array('view', 'edit', 'embed'), |
|
998 | 998 | ), |
999 | 999 | |
1000 | 1000 | 'status' => array( |
1001 | - 'description' => __( 'A named status for the item.', 'invoicing' ), |
|
1001 | + 'description' => __('A named status for the item.', 'invoicing'), |
|
1002 | 1002 | 'type' => 'string', |
1003 | - 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
1004 | - 'context' => array( 'view', 'edit' ), |
|
1003 | + 'enum' => array_keys(get_post_stati(array('internal' => false))), |
|
1004 | + 'context' => array('view', 'edit'), |
|
1005 | 1005 | ), |
1006 | 1006 | |
1007 | 1007 | 'type' => array( |
1008 | - 'description' => __( 'The item type.', 'invoicing' ), |
|
1008 | + 'description' => __('The item type.', 'invoicing'), |
|
1009 | 1009 | 'type' => 'string', |
1010 | 1010 | 'enum' => wpinv_item_types(), |
1011 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1011 | + 'context' => array('view', 'edit', 'embed'), |
|
1012 | 1012 | ), |
1013 | 1013 | |
1014 | 1014 | 'vat_rule' => array( |
1015 | - 'description' => __( 'VAT rule applied to the item.', 'invoicing' ), |
|
1015 | + 'description' => __('VAT rule applied to the item.', 'invoicing'), |
|
1016 | 1016 | 'type' => 'string', |
1017 | - 'enum' => array( 'digital', 'physical' ), |
|
1018 | - 'context' => array( 'view', 'edit' ), |
|
1017 | + 'enum' => array('digital', 'physical'), |
|
1018 | + 'context' => array('view', 'edit'), |
|
1019 | 1019 | ), |
1020 | 1020 | |
1021 | 1021 | 'vat_class' => array( |
1022 | - 'description' => __( 'VAT class for the item.', 'invoicing' ), |
|
1022 | + 'description' => __('VAT class for the item.', 'invoicing'), |
|
1023 | 1023 | 'type' => 'string', |
1024 | - 'context' => array( 'view', 'edit' ), |
|
1024 | + 'context' => array('view', 'edit'), |
|
1025 | 1025 | 'readonly' => true, |
1026 | 1026 | ), |
1027 | 1027 | |
1028 | 1028 | 'custom_id' => array( |
1029 | - 'description' => __( 'Custom id for the item.', 'invoicing' ), |
|
1029 | + 'description' => __('Custom id for the item.', 'invoicing'), |
|
1030 | 1030 | 'type' => 'string', |
1031 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1031 | + 'context' => array('view', 'edit', 'embed'), |
|
1032 | 1032 | ), |
1033 | 1033 | |
1034 | 1034 | 'custom_name' => array( |
1035 | - 'description' => __( 'Custom name for the item.', 'invoicing' ), |
|
1035 | + 'description' => __('Custom name for the item.', 'invoicing'), |
|
1036 | 1036 | 'type' => 'string', |
1037 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1037 | + 'context' => array('view', 'edit', 'embed'), |
|
1038 | 1038 | ), |
1039 | 1039 | |
1040 | 1040 | 'custom_singular_name' => array( |
1041 | - 'description' => __( 'Custom singular name for the item.', 'invoicing' ), |
|
1041 | + 'description' => __('Custom singular name for the item.', 'invoicing'), |
|
1042 | 1042 | 'type' => 'string', |
1043 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1043 | + 'context' => array('view', 'edit', 'embed'), |
|
1044 | 1044 | ), |
1045 | 1045 | |
1046 | 1046 | 'dynamic_pricing' => array( |
1047 | - 'description' => __( 'Whether the item allows a user to set their own price.', 'invoicing' ), |
|
1047 | + 'description' => __('Whether the item allows a user to set their own price.', 'invoicing'), |
|
1048 | 1048 | 'type' => 'integer', |
1049 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1049 | + 'context' => array('view', 'edit', 'embed'), |
|
1050 | 1050 | ), |
1051 | 1051 | |
1052 | 1052 | 'minimum_price' => array( |
1053 | - 'description' => __( 'For dynamic prices, this is the minimum price that a user can set.', 'invoicing' ), |
|
1053 | + 'description' => __('For dynamic prices, this is the minimum price that a user can set.', 'invoicing'), |
|
1054 | 1054 | 'type' => 'number', |
1055 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1055 | + 'context' => array('view', 'edit', 'embed'), |
|
1056 | 1056 | ), |
1057 | 1057 | |
1058 | 1058 | 'is_recurring' => array( |
1059 | - 'description' => __( 'Whether the item is a subscription item.', 'invoicing' ), |
|
1059 | + 'description' => __('Whether the item is a subscription item.', 'invoicing'), |
|
1060 | 1060 | 'type' => 'integer', |
1061 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1061 | + 'context' => array('view', 'edit', 'embed'), |
|
1062 | 1062 | ), |
1063 | 1063 | |
1064 | 1064 | 'recurring_period' => array( |
1065 | - 'description' => __( 'The recurring period for a recurring item.', 'invoicing' ), |
|
1065 | + 'description' => __('The recurring period for a recurring item.', 'invoicing'), |
|
1066 | 1066 | 'type' => 'string', |
1067 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1068 | - 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1067 | + 'context' => array('view', 'edit', 'embed'), |
|
1068 | + 'enum' => array('D', 'W', 'M', 'Y'), |
|
1069 | 1069 | ), |
1070 | 1070 | |
1071 | 1071 | 'recurring_interval' => array( |
1072 | - 'description' => __( 'The recurring interval for a subscription item.', 'invoicing' ), |
|
1072 | + 'description' => __('The recurring interval for a subscription item.', 'invoicing'), |
|
1073 | 1073 | 'type' => 'integer', |
1074 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1074 | + 'context' => array('view', 'edit', 'embed'), |
|
1075 | 1075 | ), |
1076 | 1076 | |
1077 | 1077 | 'recurring_limit' => array( |
1078 | - 'description' => __( 'The maximum number of renewals for a subscription item.', 'invoicing' ), |
|
1078 | + 'description' => __('The maximum number of renewals for a subscription item.', 'invoicing'), |
|
1079 | 1079 | 'type' => 'integer', |
1080 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1080 | + 'context' => array('view', 'edit', 'embed'), |
|
1081 | 1081 | ), |
1082 | 1082 | |
1083 | 1083 | 'free_trial' => array( |
1084 | - 'description' => __( 'Whether the item has a free trial period.', 'invoicing' ), |
|
1084 | + 'description' => __('Whether the item has a free trial period.', 'invoicing'), |
|
1085 | 1085 | 'type' => 'integer', |
1086 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1086 | + 'context' => array('view', 'edit', 'embed'), |
|
1087 | 1087 | ), |
1088 | 1088 | |
1089 | 1089 | 'trial_period' => array( |
1090 | - 'description' => __( 'The trial period of a recurring item.', 'invoicing' ), |
|
1090 | + 'description' => __('The trial period of a recurring item.', 'invoicing'), |
|
1091 | 1091 | 'type' => 'string', |
1092 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1093 | - 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1092 | + 'context' => array('view', 'edit', 'embed'), |
|
1093 | + 'enum' => array('D', 'W', 'M', 'Y'), |
|
1094 | 1094 | ), |
1095 | 1095 | |
1096 | 1096 | 'trial_interval' => array( |
1097 | - 'description' => __( 'The trial interval for a subscription item.', 'invoicing' ), |
|
1097 | + 'description' => __('The trial interval for a subscription item.', 'invoicing'), |
|
1098 | 1098 | 'type' => 'integer', |
1099 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1099 | + 'context' => array('view', 'edit', 'embed'), |
|
1100 | 1100 | ), |
1101 | 1101 | |
1102 | 1102 | 'editable' => array( |
1103 | - 'description' => __( 'Whether or not the item is editable.', 'invoicing' ), |
|
1103 | + 'description' => __('Whether or not the item is editable.', 'invoicing'), |
|
1104 | 1104 | 'type' => 'integer', |
1105 | - 'context' => array( 'view', 'edit' ), |
|
1105 | + 'context' => array('view', 'edit'), |
|
1106 | 1106 | ), |
1107 | 1107 | |
1108 | 1108 | ), |
@@ -1120,12 +1120,12 @@ discard block |
||
1120 | 1120 | * |
1121 | 1121 | * @param array $schema The item schema. |
1122 | 1122 | */ |
1123 | - $schema = apply_filters( "wpinv_rest_item_schema", $schema ); |
|
1123 | + $schema = apply_filters("wpinv_rest_item_schema", $schema); |
|
1124 | 1124 | |
1125 | 1125 | // Cache the item schema. |
1126 | 1126 | $this->schema = $schema; |
1127 | 1127 | |
1128 | - return $this->add_additional_fields_schema( $this->schema ); |
|
1128 | + return $this->add_additional_fields_schema($this->schema); |
|
1129 | 1129 | } |
1130 | 1130 | |
1131 | 1131 | /** |
@@ -1137,20 +1137,20 @@ discard block |
||
1137 | 1137 | */ |
1138 | 1138 | protected function get_schema_links() { |
1139 | 1139 | |
1140 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1140 | + $href = rest_url("{$this->namespace}/{$this->rest_base}/{id}"); |
|
1141 | 1141 | |
1142 | 1142 | $links = array(); |
1143 | 1143 | |
1144 | 1144 | $links[] = array( |
1145 | 1145 | 'rel' => 'https://api.w.org/action-publish', |
1146 | - 'title' => __( 'The current user can publish this item.' ), |
|
1146 | + 'title' => __('The current user can publish this item.'), |
|
1147 | 1147 | 'href' => $href, |
1148 | 1148 | 'targetSchema' => array( |
1149 | 1149 | 'type' => 'object', |
1150 | 1150 | 'properties' => array( |
1151 | 1151 | 'status' => array( |
1152 | 1152 | 'type' => 'string', |
1153 | - 'enum' => array( 'publish', 'future' ), |
|
1153 | + 'enum' => array('publish', 'future'), |
|
1154 | 1154 | ), |
1155 | 1155 | ), |
1156 | 1156 | ), |
@@ -1167,18 +1167,18 @@ discard block |
||
1167 | 1167 | * @param WPInv_Item $item Item Object. |
1168 | 1168 | * @return array Links for the given item. |
1169 | 1169 | */ |
1170 | - protected function prepare_links( $item ) { |
|
1170 | + protected function prepare_links($item) { |
|
1171 | 1171 | |
1172 | 1172 | // Prepare the base REST API endpoint for items. |
1173 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1173 | + $base = sprintf('%s/%s', $this->namespace, $this->rest_base); |
|
1174 | 1174 | |
1175 | 1175 | // Entity meta. |
1176 | 1176 | $links = array( |
1177 | 1177 | 'self' => array( |
1178 | - 'href' => rest_url( trailingslashit( $base ) . $item->ID ), |
|
1178 | + 'href' => rest_url(trailingslashit($base) . $item->ID), |
|
1179 | 1179 | ), |
1180 | 1180 | 'collection' => array( |
1181 | - 'href' => rest_url( $base ), |
|
1181 | + 'href' => rest_url($base), |
|
1182 | 1182 | ), |
1183 | 1183 | ); |
1184 | 1184 | |
@@ -1191,7 +1191,7 @@ discard block |
||
1191 | 1191 | * |
1192 | 1192 | * @param array $links Rest links. |
1193 | 1193 | */ |
1194 | - return apply_filters( "wpinv_rest_item_links", $links ); |
|
1194 | + return apply_filters("wpinv_rest_item_links", $links); |
|
1195 | 1195 | |
1196 | 1196 | } |
1197 | 1197 | |
@@ -1204,19 +1204,19 @@ discard block |
||
1204 | 1204 | * @param WP_REST_Request $request Request object. |
1205 | 1205 | * @return array List of link relations. |
1206 | 1206 | */ |
1207 | - protected function get_available_actions( $item, $request ) { |
|
1207 | + protected function get_available_actions($item, $request) { |
|
1208 | 1208 | |
1209 | - if ( 'edit' !== $request['context'] ) { |
|
1209 | + if ('edit' !== $request['context']) { |
|
1210 | 1210 | return array(); |
1211 | 1211 | } |
1212 | 1212 | |
1213 | 1213 | $rels = array(); |
1214 | 1214 | |
1215 | 1215 | // Retrieve the post type object. |
1216 | - $post_type = get_post_type_object( $item->post_type ); |
|
1216 | + $post_type = get_post_type_object($item->post_type); |
|
1217 | 1217 | |
1218 | 1218 | // Mark item as published. |
1219 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1219 | + if (current_user_can($post_type->cap->publish_posts)) { |
|
1220 | 1220 | $rels[] = 'https://api.w.org/action-publish'; |
1221 | 1221 | } |
1222 | 1222 | |
@@ -1229,7 +1229,7 @@ discard block |
||
1229 | 1229 | * |
1230 | 1230 | * @param array $rels Available link relations. |
1231 | 1231 | */ |
1232 | - return apply_filters( "wpinv_rest_item_link_relations", $rels ); |
|
1232 | + return apply_filters("wpinv_rest_item_link_relations", $rels); |
|
1233 | 1233 | } |
1234 | 1234 | |
1235 | 1235 | /** |
@@ -1241,7 +1241,7 @@ discard block |
||
1241 | 1241 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
1242 | 1242 | */ |
1243 | 1243 | public function get_item_types() { |
1244 | - return rest_ensure_response( wpinv_get_item_types() ); |
|
1244 | + return rest_ensure_response(wpinv_get_item_types()); |
|
1245 | 1245 | } |
1246 | 1246 | |
1247 | 1247 |
@@ -52,14 +52,14 @@ |
||
52 | 52 | } |
53 | 53 | |
54 | 54 | |
55 | - /** |
|
56 | - * Registers routes |
|
57 | - * |
|
55 | + /** |
|
56 | + * Registers routes |
|
57 | + * |
|
58 | 58 | * @since 1.0.13 |
59 | - */ |
|
60 | - public function register_rest_routes() { |
|
59 | + */ |
|
60 | + public function register_rest_routes() { |
|
61 | 61 | |
62 | - // Invoices. |
|
62 | + // Invoices. |
|
63 | 63 | $this->invoices_controller->register_routes(); |
64 | 64 | |
65 | 65 | // Items. |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | // MUST have WordPress. |
10 | -if ( !defined( 'WPINC' ) ) { |
|
10 | +if (!defined('WPINC')) { |
|
11 | 11 | exit; |
12 | 12 | } |
13 | 13 | |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | /** |
20 | 20 | * @param string A prefix for our REST routes |
21 | 21 | */ |
22 | - public $api_namespace = ''; |
|
22 | + public $api_namespace = ''; |
|
23 | 23 | |
24 | 24 | /** |
25 | 25 | * @param WPInv_REST_Invoice_Controller Invoices controller |
@@ -37,18 +37,18 @@ discard block |
||
37 | 37 | * @since 1.0.13 |
38 | 38 | * Sets the API namespace and inits hooks |
39 | 39 | */ |
40 | - public function __construct( $api_namespace = 'invoicing/v1' ) { |
|
40 | + public function __construct($api_namespace = 'invoicing/v1') { |
|
41 | 41 | |
42 | 42 | // Include controllers and related files |
43 | 43 | $this->includes(); |
44 | 44 | |
45 | 45 | // Set up class variables |
46 | - $this->api_namespace = apply_filters( 'wpinv_rest_api_namespace', $api_namespace ); |
|
47 | - $this->invoices_controller = new WPInv_REST_Invoice_Controller( $this->api_namespace ); |
|
48 | - $this->items_controller = new WPInv_REST_Items_Controller( $this->api_namespace ); |
|
46 | + $this->api_namespace = apply_filters('wpinv_rest_api_namespace', $api_namespace); |
|
47 | + $this->invoices_controller = new WPInv_REST_Invoice_Controller($this->api_namespace); |
|
48 | + $this->items_controller = new WPInv_REST_Items_Controller($this->api_namespace); |
|
49 | 49 | |
50 | 50 | //Register REST routes |
51 | - add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); |
|
51 | + add_action('rest_api_init', array($this, 'register_rest_routes')); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | protected function includes() { |
77 | 77 | |
78 | 78 | // Invoices |
79 | - require_once( WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-invoice-controller.php' ); |
|
79 | + require_once(WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-invoice-controller.php'); |
|
80 | 80 | |
81 | 81 | // Items |
82 | - require_once( WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-items-controller.php' ); |
|
82 | + require_once(WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-items-controller.php'); |
|
83 | 83 | |
84 | 84 | } |
85 | 85 |
@@ -1,6 +1,8 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
3 | +if ( ! defined( 'ABSPATH' ) ) { |
|
4 | + exit; |
|
5 | +} |
|
4 | 6 | |
5 | 7 | function wpinv_get_item_by_id( $id ) { |
6 | 8 | return wpinv_get_item_by( 'id', $id ); |
@@ -80,8 +82,9 @@ discard block |
||
80 | 82 | function wpinv_get_item( $item = 0 ) { |
81 | 83 | if ( is_numeric( $item ) ) { |
82 | 84 | $item = get_post( $item ); |
83 | - if ( ! $item || 'wpi_item' !== $item->post_type ) |
|
84 | - return null; |
|
85 | + if ( ! $item || 'wpi_item' !== $item->post_type ) { |
|
86 | + return null; |
|
87 | + } |
|
85 | 88 | return $item; |
86 | 89 | } |
87 | 90 | |
@@ -331,8 +334,9 @@ discard block |
||
331 | 334 | } |
332 | 335 | |
333 | 336 | function wpinv_remove_item_logs_on_delete( $item_id = 0 ) { |
334 | - if ( 'wpi_item' !== get_post_type( $item_id ) ) |
|
335 | - return; |
|
337 | + if ( 'wpi_item' !== get_post_type( $item_id ) ) { |
|
338 | + return; |
|
339 | + } |
|
336 | 340 | |
337 | 341 | global $wpinv_logs; |
338 | 342 |
@@ -1,23 +1,23 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
3 | +if (!defined('ABSPATH')) exit; |
|
4 | 4 | |
5 | -function wpinv_get_item_by_id( $id ) { |
|
6 | - return wpinv_get_item_by( 'id', $id ); |
|
5 | +function wpinv_get_item_by_id($id) { |
|
6 | + return wpinv_get_item_by('id', $id); |
|
7 | 7 | } |
8 | 8 | |
9 | -function wpinv_get_item_by( $field = '', $value = '', $type = '' ) { |
|
10 | - if( empty( $field ) || empty( $value ) ) { |
|
9 | +function wpinv_get_item_by($field = '', $value = '', $type = '') { |
|
10 | + if (empty($field) || empty($value)) { |
|
11 | 11 | return false; |
12 | 12 | } |
13 | 13 | |
14 | 14 | $posts = array(); |
15 | 15 | |
16 | - switch( strtolower( $field ) ) { |
|
16 | + switch (strtolower($field)) { |
|
17 | 17 | case 'id': |
18 | - $item = new WPInv_Item( $value ); |
|
18 | + $item = new WPInv_Item($value); |
|
19 | 19 | |
20 | - if ( !empty( $item ) && $item->post_type == 'wpi_item' ) { |
|
20 | + if (!empty($item) && $item->post_type == 'wpi_item') { |
|
21 | 21 | return $item; |
22 | 22 | } |
23 | 23 | return false; |
@@ -26,16 +26,16 @@ discard block |
||
26 | 26 | |
27 | 27 | case 'slug': |
28 | 28 | case 'name': |
29 | - $posts = get_posts( array( |
|
29 | + $posts = get_posts(array( |
|
30 | 30 | 'post_type' => 'wpi_item', |
31 | 31 | 'name' => $value, |
32 | 32 | 'posts_per_page' => 1, |
33 | 33 | 'post_status' => 'any' |
34 | - ) ); |
|
34 | + )); |
|
35 | 35 | |
36 | 36 | break; |
37 | 37 | case 'custom_id': |
38 | - if ( empty( $value ) || empty( $type ) ) { |
|
38 | + if (empty($value) || empty($type)) { |
|
39 | 39 | return false; |
40 | 40 | } |
41 | 41 | |
@@ -55,10 +55,10 @@ discard block |
||
55 | 55 | 'post_status' => 'any', |
56 | 56 | 'orderby' => 'ID', |
57 | 57 | 'order' => 'ASC', |
58 | - 'meta_query' => array( $meta_query ) |
|
58 | + 'meta_query' => array($meta_query) |
|
59 | 59 | ); |
60 | 60 | |
61 | - $posts = get_posts( $args ); |
|
61 | + $posts = get_posts($args); |
|
62 | 62 | |
63 | 63 | break; |
64 | 64 | |
@@ -66,10 +66,10 @@ discard block |
||
66 | 66 | return false; |
67 | 67 | } |
68 | 68 | |
69 | - if ( !empty( $posts[0] ) ) { |
|
70 | - $item = new WPInv_Item( $posts[0]->ID ); |
|
69 | + if (!empty($posts[0])) { |
|
70 | + $item = new WPInv_Item($posts[0]->ID); |
|
71 | 71 | |
72 | - if ( !empty( $item ) && $item->post_type == 'wpi_item' ) { |
|
72 | + if (!empty($item) && $item->post_type == 'wpi_item') { |
|
73 | 73 | return $item; |
74 | 74 | } |
75 | 75 | } |
@@ -77,10 +77,10 @@ discard block |
||
77 | 77 | return false; |
78 | 78 | } |
79 | 79 | |
80 | -function wpinv_get_item( $item = 0 ) { |
|
81 | - if ( is_numeric( $item ) ) { |
|
82 | - $item = get_post( $item ); |
|
83 | - if ( ! $item || 'wpi_item' !== $item->post_type ) |
|
80 | +function wpinv_get_item($item = 0) { |
|
81 | + if (is_numeric($item)) { |
|
82 | + $item = get_post($item); |
|
83 | + if (!$item || 'wpi_item' !== $item->post_type) |
|
84 | 84 | return null; |
85 | 85 | return $item; |
86 | 86 | } |
@@ -93,18 +93,18 @@ discard block |
||
93 | 93 | |
94 | 94 | $item = get_posts($args); |
95 | 95 | |
96 | - if ( $item ) { |
|
96 | + if ($item) { |
|
97 | 97 | return $item[0]; |
98 | 98 | } |
99 | 99 | |
100 | 100 | return null; |
101 | 101 | } |
102 | 102 | |
103 | -function wpinv_get_all_items( $args = array() ) { |
|
103 | +function wpinv_get_all_items($args = array()) { |
|
104 | 104 | |
105 | - $args = wp_parse_args( $args, array( |
|
106 | - 'status' => array( 'publish' ), |
|
107 | - 'limit' => get_option( 'posts_per_page' ), |
|
105 | + $args = wp_parse_args($args, array( |
|
106 | + 'status' => array('publish'), |
|
107 | + 'limit' => get_option('posts_per_page'), |
|
108 | 108 | 'page' => 1, |
109 | 109 | 'exclude' => array(), |
110 | 110 | 'orderby' => 'date', |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | 'meta_query' => array(), |
114 | 114 | 'return' => 'objects', |
115 | 115 | 'paginate' => false, |
116 | - ) ); |
|
116 | + )); |
|
117 | 117 | |
118 | 118 | $wp_query_args = array( |
119 | 119 | 'post_type' => 'wpi_item', |
@@ -123,26 +123,26 @@ discard block |
||
123 | 123 | 'fields' => 'ids', |
124 | 124 | 'orderby' => $args['orderby'], |
125 | 125 | 'order' => $args['order'], |
126 | - 'paged' => absint( $args['page'] ), |
|
126 | + 'paged' => absint($args['page']), |
|
127 | 127 | ); |
128 | 128 | |
129 | - if ( ! empty( $args['exclude'] ) ) { |
|
130 | - $wp_query_args['post__not_in'] = array_map( 'absint', $args['exclude'] ); |
|
129 | + if (!empty($args['exclude'])) { |
|
130 | + $wp_query_args['post__not_in'] = array_map('absint', $args['exclude']); |
|
131 | 131 | } |
132 | 132 | |
133 | - if ( ! $args['paginate' ] ) { |
|
133 | + if (!$args['paginate']) { |
|
134 | 134 | $wp_query_args['no_found_rows'] = true; |
135 | 135 | } |
136 | 136 | |
137 | - if ( ! empty( $args['search'] ) ) { |
|
137 | + if (!empty($args['search'])) { |
|
138 | 138 | $wp_query_args['s'] = $args['search']; |
139 | 139 | } |
140 | 140 | |
141 | - if ( ! empty( $args['type'] ) && $args['type'] !== wpinv_item_types() ) { |
|
142 | - $types = wpinv_parse_list( $args['type'] ); |
|
141 | + if (!empty($args['type']) && $args['type'] !== wpinv_item_types()) { |
|
142 | + $types = wpinv_parse_list($args['type']); |
|
143 | 143 | $wp_query_args['meta_query'][] = array( |
144 | 144 | 'key' => '_wpinv_type', |
145 | - 'value' => implode( ',', $types ), |
|
145 | + 'value' => implode(',', $types), |
|
146 | 146 | 'compare' => 'IN', |
147 | 147 | ); |
148 | 148 | } |
@@ -150,17 +150,17 @@ discard block |
||
150 | 150 | $wp_query_args = apply_filters('wpinv_get_items_args', $wp_query_args, $args); |
151 | 151 | |
152 | 152 | // Get results. |
153 | - $items = new WP_Query( $wp_query_args ); |
|
153 | + $items = new WP_Query($wp_query_args); |
|
154 | 154 | |
155 | - if ( 'objects' === $args['return'] ) { |
|
156 | - $return = array_map( 'wpinv_get_item_by_id', $items->posts ); |
|
157 | - } elseif ( 'self' === $args['return'] ) { |
|
155 | + if ('objects' === $args['return']) { |
|
156 | + $return = array_map('wpinv_get_item_by_id', $items->posts); |
|
157 | + } elseif ('self' === $args['return']) { |
|
158 | 158 | return $items; |
159 | 159 | } else { |
160 | 160 | $return = $items->posts; |
161 | 161 | } |
162 | 162 | |
163 | - if ( $args['paginate' ] ) { |
|
163 | + if ($args['paginate']) { |
|
164 | 164 | return (object) array( |
165 | 165 | 'items' => $return, |
166 | 166 | 'total' => $items->found_posts, |
@@ -172,143 +172,143 @@ discard block |
||
172 | 172 | |
173 | 173 | } |
174 | 174 | |
175 | -function wpinv_is_free_item( $item_id = 0 ) { |
|
176 | - if( empty( $item_id ) ) { |
|
175 | +function wpinv_is_free_item($item_id = 0) { |
|
176 | + if (empty($item_id)) { |
|
177 | 177 | return false; |
178 | 178 | } |
179 | 179 | |
180 | - $item = new WPInv_Item( $item_id ); |
|
180 | + $item = new WPInv_Item($item_id); |
|
181 | 181 | |
182 | 182 | return $item->is_free(); |
183 | 183 | } |
184 | 184 | |
185 | -function wpinv_item_is_editable( $item = 0 ) { |
|
186 | - if ( !empty( $item ) && is_a( $item, 'WP_Post' ) ) { |
|
185 | +function wpinv_item_is_editable($item = 0) { |
|
186 | + if (!empty($item) && is_a($item, 'WP_Post')) { |
|
187 | 187 | $item = $item->ID; |
188 | 188 | } |
189 | 189 | |
190 | - if ( empty( $item ) ) { |
|
190 | + if (empty($item)) { |
|
191 | 191 | return true; |
192 | 192 | } |
193 | 193 | |
194 | - $item = new WPInv_Item( $item ); |
|
194 | + $item = new WPInv_Item($item); |
|
195 | 195 | |
196 | 196 | return (bool) $item->is_editable(); |
197 | 197 | } |
198 | 198 | |
199 | -function wpinv_get_item_price( $item_id = 0 ) { |
|
200 | - if( empty( $item_id ) ) { |
|
199 | +function wpinv_get_item_price($item_id = 0) { |
|
200 | + if (empty($item_id)) { |
|
201 | 201 | return false; |
202 | 202 | } |
203 | 203 | |
204 | - $item = new WPInv_Item( $item_id ); |
|
204 | + $item = new WPInv_Item($item_id); |
|
205 | 205 | |
206 | 206 | return $item->get_price(); |
207 | 207 | } |
208 | 208 | |
209 | -function wpinv_is_recurring_item( $item_id = 0 ) { |
|
210 | - if( empty( $item_id ) ) { |
|
209 | +function wpinv_is_recurring_item($item_id = 0) { |
|
210 | + if (empty($item_id)) { |
|
211 | 211 | return false; |
212 | 212 | } |
213 | 213 | |
214 | - $item = new WPInv_Item( $item_id ); |
|
214 | + $item = new WPInv_Item($item_id); |
|
215 | 215 | |
216 | 216 | return $item->is_recurring(); |
217 | 217 | } |
218 | 218 | |
219 | -function wpinv_item_price( $item_id = 0 ) { |
|
220 | - if( empty( $item_id ) ) { |
|
219 | +function wpinv_item_price($item_id = 0) { |
|
220 | + if (empty($item_id)) { |
|
221 | 221 | return false; |
222 | 222 | } |
223 | 223 | |
224 | - $price = wpinv_get_item_price( $item_id ); |
|
225 | - $price = wpinv_price( wpinv_format_amount( $price ) ); |
|
224 | + $price = wpinv_get_item_price($item_id); |
|
225 | + $price = wpinv_price(wpinv_format_amount($price)); |
|
226 | 226 | |
227 | - return apply_filters( 'wpinv_item_price', $price, $item_id ); |
|
227 | + return apply_filters('wpinv_item_price', $price, $item_id); |
|
228 | 228 | } |
229 | 229 | |
230 | -function wpinv_item_show_price( $item_id = 0, $echo = true ) { |
|
231 | - if ( empty( $item_id ) ) { |
|
230 | +function wpinv_item_show_price($item_id = 0, $echo = true) { |
|
231 | + if (empty($item_id)) { |
|
232 | 232 | $item_id = get_the_ID(); |
233 | 233 | } |
234 | 234 | |
235 | - $price = wpinv_item_price( $item_id ); |
|
235 | + $price = wpinv_item_price($item_id); |
|
236 | 236 | |
237 | - $price = apply_filters( 'wpinv_item_price', wpinv_sanitize_amount( $price ), $item_id ); |
|
237 | + $price = apply_filters('wpinv_item_price', wpinv_sanitize_amount($price), $item_id); |
|
238 | 238 | $formatted_price = '<span class="wpinv_price" id="wpinv_item_' . $item_id . '">' . $price . '</span>'; |
239 | - $formatted_price = apply_filters( 'wpinv_item_price_after_html', $formatted_price, $item_id, $price ); |
|
239 | + $formatted_price = apply_filters('wpinv_item_price_after_html', $formatted_price, $item_id, $price); |
|
240 | 240 | |
241 | - if ( $echo ) { |
|
241 | + if ($echo) { |
|
242 | 242 | echo $formatted_price; |
243 | 243 | } else { |
244 | 244 | return $formatted_price; |
245 | 245 | } |
246 | 246 | } |
247 | 247 | |
248 | -function wpinv_get_item_final_price( $item_id = 0, $amount_override = null ) { |
|
249 | - if ( is_null( $amount_override ) ) { |
|
250 | - $original_price = get_post_meta( $item_id, '_wpinv_price', true ); |
|
248 | +function wpinv_get_item_final_price($item_id = 0, $amount_override = null) { |
|
249 | + if (is_null($amount_override)) { |
|
250 | + $original_price = get_post_meta($item_id, '_wpinv_price', true); |
|
251 | 251 | } else { |
252 | 252 | $original_price = $amount_override; |
253 | 253 | } |
254 | 254 | |
255 | 255 | $price = $original_price; |
256 | 256 | |
257 | - return apply_filters( 'wpinv_get_item_final_price', $price, $item_id ); |
|
257 | + return apply_filters('wpinv_get_item_final_price', $price, $item_id); |
|
258 | 258 | } |
259 | 259 | |
260 | -function wpinv_item_custom_singular_name( $item_id ) { |
|
261 | - if( empty( $item_id ) ) { |
|
260 | +function wpinv_item_custom_singular_name($item_id) { |
|
261 | + if (empty($item_id)) { |
|
262 | 262 | return false; |
263 | 263 | } |
264 | 264 | |
265 | - $item = new WPInv_Item( $item_id ); |
|
265 | + $item = new WPInv_Item($item_id); |
|
266 | 266 | |
267 | 267 | return $item->get_custom_singular_name(); |
268 | 268 | } |
269 | 269 | |
270 | 270 | function wpinv_get_item_types() { |
271 | 271 | $item_types = array( |
272 | - 'custom' => __( 'Standard', 'invoicing' ), |
|
273 | - 'fee' => __( 'Fee', 'invoicing' ), |
|
272 | + 'custom' => __('Standard', 'invoicing'), |
|
273 | + 'fee' => __('Fee', 'invoicing'), |
|
274 | 274 | ); |
275 | - return apply_filters( 'wpinv_get_item_types', $item_types ); |
|
275 | + return apply_filters('wpinv_get_item_types', $item_types); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | function wpinv_item_types() { |
279 | 279 | $item_types = wpinv_get_item_types(); |
280 | 280 | |
281 | - return ( !empty( $item_types ) ? array_keys( $item_types ) : array() ); |
|
281 | + return (!empty($item_types) ? array_keys($item_types) : array()); |
|
282 | 282 | } |
283 | 283 | |
284 | -function wpinv_get_item_type( $item_id ) { |
|
285 | - if( empty( $item_id ) ) { |
|
284 | +function wpinv_get_item_type($item_id) { |
|
285 | + if (empty($item_id)) { |
|
286 | 286 | return false; |
287 | 287 | } |
288 | 288 | |
289 | - $item = new WPInv_Item( $item_id ); |
|
289 | + $item = new WPInv_Item($item_id); |
|
290 | 290 | |
291 | 291 | return $item->get_type(); |
292 | 292 | } |
293 | 293 | |
294 | -function wpinv_item_type( $item_id ) { |
|
294 | +function wpinv_item_type($item_id) { |
|
295 | 295 | $item_types = wpinv_get_item_types(); |
296 | 296 | |
297 | - $item_type = wpinv_get_item_type( $item_id ); |
|
297 | + $item_type = wpinv_get_item_type($item_id); |
|
298 | 298 | |
299 | - if ( empty( $item_type ) ) { |
|
299 | + if (empty($item_type)) { |
|
300 | 300 | $item_type = '-'; |
301 | 301 | } |
302 | 302 | |
303 | - $item_type = isset( $item_types[$item_type] ) ? $item_types[$item_type] : __( $item_type, 'invoicing' ); |
|
303 | + $item_type = isset($item_types[$item_type]) ? $item_types[$item_type] : __($item_type, 'invoicing'); |
|
304 | 304 | |
305 | - return apply_filters( 'wpinv_item_type', $item_type, $item_id ); |
|
305 | + return apply_filters('wpinv_item_type', $item_type, $item_id); |
|
306 | 306 | } |
307 | 307 | |
308 | -function wpinv_record_item_in_log( $item_id = 0, $file_id, $user_info, $ip, $invoice_id ) { |
|
308 | +function wpinv_record_item_in_log($item_id = 0, $file_id, $user_info, $ip, $invoice_id) { |
|
309 | 309 | global $wpinv_logs; |
310 | 310 | |
311 | - if ( empty( $wpinv_logs ) ) { |
|
311 | + if (empty($wpinv_logs)) { |
|
312 | 312 | return false; |
313 | 313 | } |
314 | 314 | |
@@ -317,264 +317,264 @@ discard block |
||
317 | 317 | 'log_type' => 'wpi_item' |
318 | 318 | ); |
319 | 319 | |
320 | - $user_id = isset( $user_info['user_id'] ) ? $user_info['user_id'] : (int) -1; |
|
320 | + $user_id = isset($user_info['user_id']) ? $user_info['user_id'] : (int) -1; |
|
321 | 321 | |
322 | 322 | $log_meta = array( |
323 | 323 | 'user_info' => $user_info, |
324 | 324 | 'user_id' => $user_id, |
325 | - 'file_id' => (int)$file_id, |
|
325 | + 'file_id' => (int) $file_id, |
|
326 | 326 | 'ip' => $ip, |
327 | 327 | 'invoice_id'=> $invoice_id, |
328 | 328 | ); |
329 | 329 | |
330 | - $wpinv_logs->insert_log( $log_data, $log_meta ); |
|
330 | + $wpinv_logs->insert_log($log_data, $log_meta); |
|
331 | 331 | } |
332 | 332 | |
333 | -function wpinv_remove_item_logs_on_delete( $item_id = 0 ) { |
|
334 | - if ( 'wpi_item' !== get_post_type( $item_id ) ) |
|
333 | +function wpinv_remove_item_logs_on_delete($item_id = 0) { |
|
334 | + if ('wpi_item' !== get_post_type($item_id)) |
|
335 | 335 | return; |
336 | 336 | |
337 | 337 | global $wpinv_logs; |
338 | 338 | |
339 | - if ( empty( $wpinv_logs ) ) { |
|
339 | + if (empty($wpinv_logs)) { |
|
340 | 340 | return false; |
341 | 341 | } |
342 | 342 | |
343 | 343 | // Remove all log entries related to this item |
344 | - $wpinv_logs->delete_logs( $item_id ); |
|
344 | + $wpinv_logs->delete_logs($item_id); |
|
345 | 345 | } |
346 | -add_action( 'delete_post', 'wpinv_remove_item_logs_on_delete' ); |
|
346 | +add_action('delete_post', 'wpinv_remove_item_logs_on_delete'); |
|
347 | 347 | |
348 | -function wpinv_get_random_item( $post_ids = true ) { |
|
349 | - wpinv_get_random_items( 1, $post_ids ); |
|
348 | +function wpinv_get_random_item($post_ids = true) { |
|
349 | + wpinv_get_random_items(1, $post_ids); |
|
350 | 350 | } |
351 | 351 | |
352 | -function wpinv_get_random_items( $num = 3, $post_ids = true ) { |
|
353 | - if ( $post_ids ) { |
|
354 | - $args = array( 'post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num, 'fields' => 'ids' ); |
|
352 | +function wpinv_get_random_items($num = 3, $post_ids = true) { |
|
353 | + if ($post_ids) { |
|
354 | + $args = array('post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num, 'fields' => 'ids'); |
|
355 | 355 | } else { |
356 | - $args = array( 'post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num ); |
|
356 | + $args = array('post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num); |
|
357 | 357 | } |
358 | 358 | |
359 | - $args = apply_filters( 'wpinv_get_random_items', $args ); |
|
359 | + $args = apply_filters('wpinv_get_random_items', $args); |
|
360 | 360 | |
361 | - return get_posts( $args ); |
|
361 | + return get_posts($args); |
|
362 | 362 | } |
363 | 363 | |
364 | -function wpinv_get_item_token( $url = '' ) { |
|
364 | +function wpinv_get_item_token($url = '') { |
|
365 | 365 | $args = array(); |
366 | - $hash = apply_filters( 'wpinv_get_url_token_algorithm', 'sha256' ); |
|
367 | - $secret = apply_filters( 'wpinv_get_url_token_secret', hash( $hash, wp_salt() ) ); |
|
366 | + $hash = apply_filters('wpinv_get_url_token_algorithm', 'sha256'); |
|
367 | + $secret = apply_filters('wpinv_get_url_token_secret', hash($hash, wp_salt())); |
|
368 | 368 | |
369 | - $parts = parse_url( $url ); |
|
369 | + $parts = parse_url($url); |
|
370 | 370 | $options = array(); |
371 | 371 | |
372 | - if ( isset( $parts['query'] ) ) { |
|
373 | - wp_parse_str( $parts['query'], $query_args ); |
|
372 | + if (isset($parts['query'])) { |
|
373 | + wp_parse_str($parts['query'], $query_args); |
|
374 | 374 | |
375 | - if ( ! empty( $query_args['o'] ) ) { |
|
376 | - $options = explode( ':', rawurldecode( $query_args['o'] ) ); |
|
375 | + if (!empty($query_args['o'])) { |
|
376 | + $options = explode(':', rawurldecode($query_args['o'])); |
|
377 | 377 | |
378 | - if ( in_array( 'ip', $options ) ) { |
|
378 | + if (in_array('ip', $options)) { |
|
379 | 379 | $args['ip'] = wpinv_get_ip(); |
380 | 380 | } |
381 | 381 | |
382 | - if ( in_array( 'ua', $options ) ) { |
|
382 | + if (in_array('ua', $options)) { |
|
383 | 383 | $ua = wpinv_get_user_agent(); |
384 | - $args['user_agent'] = rawurlencode( $ua ); |
|
384 | + $args['user_agent'] = rawurlencode($ua); |
|
385 | 385 | } |
386 | 386 | } |
387 | 387 | } |
388 | 388 | |
389 | - $args = apply_filters( 'wpinv_get_url_token_args', $args, $url, $options ); |
|
389 | + $args = apply_filters('wpinv_get_url_token_args', $args, $url, $options); |
|
390 | 390 | |
391 | 391 | $args['secret'] = $secret; |
392 | 392 | $args['token'] = false; |
393 | 393 | |
394 | - $url = add_query_arg( $args, $url ); |
|
395 | - $parts = parse_url( $url ); |
|
394 | + $url = add_query_arg($args, $url); |
|
395 | + $parts = parse_url($url); |
|
396 | 396 | |
397 | - if ( ! isset( $parts['path'] ) ) { |
|
397 | + if (!isset($parts['path'])) { |
|
398 | 398 | $parts['path'] = ''; |
399 | 399 | } |
400 | 400 | |
401 | - $token = md5( $parts['path'] . '?' . $parts['query'] ); |
|
401 | + $token = md5($parts['path'] . '?' . $parts['query']); |
|
402 | 402 | |
403 | 403 | return $token; |
404 | 404 | } |
405 | 405 | |
406 | -function wpinv_validate_url_token( $url = '' ) { |
|
406 | +function wpinv_validate_url_token($url = '') { |
|
407 | 407 | $ret = false; |
408 | - $parts = parse_url( $url ); |
|
408 | + $parts = parse_url($url); |
|
409 | 409 | |
410 | - if ( isset( $parts['query'] ) ) { |
|
411 | - wp_parse_str( $parts['query'], $query_args ); |
|
410 | + if (isset($parts['query'])) { |
|
411 | + wp_parse_str($parts['query'], $query_args); |
|
412 | 412 | |
413 | - $allowed = apply_filters( 'wpinv_url_token_allowed_params', array( |
|
413 | + $allowed = apply_filters('wpinv_url_token_allowed_params', array( |
|
414 | 414 | 'item', |
415 | 415 | 'ttl', |
416 | 416 | 'token' |
417 | - ) ); |
|
417 | + )); |
|
418 | 418 | |
419 | 419 | $remove = array(); |
420 | 420 | |
421 | - foreach( $query_args as $key => $value ) { |
|
422 | - if( false === in_array( $key, $allowed ) ) { |
|
421 | + foreach ($query_args as $key => $value) { |
|
422 | + if (false === in_array($key, $allowed)) { |
|
423 | 423 | $remove[] = $key; |
424 | 424 | } |
425 | 425 | } |
426 | 426 | |
427 | - if( ! empty( $remove ) ) { |
|
428 | - $url = remove_query_arg( $remove, $url ); |
|
427 | + if (!empty($remove)) { |
|
428 | + $url = remove_query_arg($remove, $url); |
|
429 | 429 | } |
430 | 430 | |
431 | - if ( isset( $query_args['ttl'] ) && current_time( 'timestamp' ) > $query_args['ttl'] ) { |
|
432 | - wp_die( apply_filters( 'wpinv_item_link_expired_text', __( 'Sorry but your item link has expired.', 'invoicing' ) ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
431 | + if (isset($query_args['ttl']) && current_time('timestamp') > $query_args['ttl']) { |
|
432 | + wp_die(apply_filters('wpinv_item_link_expired_text', __('Sorry but your item link has expired.', 'invoicing')), __('Error', 'invoicing'), array('response' => 403)); |
|
433 | 433 | } |
434 | 434 | |
435 | - if ( isset( $query_args['token'] ) && $query_args['token'] == wpinv_get_item_token( $url ) ) { |
|
435 | + if (isset($query_args['token']) && $query_args['token'] == wpinv_get_item_token($url)) { |
|
436 | 436 | $ret = true; |
437 | 437 | } |
438 | 438 | |
439 | 439 | } |
440 | 440 | |
441 | - return apply_filters( 'wpinv_validate_url_token', $ret, $url, $query_args ); |
|
441 | + return apply_filters('wpinv_validate_url_token', $ret, $url, $query_args); |
|
442 | 442 | } |
443 | 443 | |
444 | -function wpinv_item_in_cart( $item_id = 0, $options = array() ) { |
|
444 | +function wpinv_item_in_cart($item_id = 0, $options = array()) { |
|
445 | 445 | $cart_items = wpinv_get_cart_contents(); |
446 | 446 | |
447 | 447 | $ret = false; |
448 | 448 | |
449 | - if ( is_array( $cart_items ) ) { |
|
450 | - foreach ( $cart_items as $item ) { |
|
451 | - if ( $item['id'] == $item_id ) { |
|
449 | + if (is_array($cart_items)) { |
|
450 | + foreach ($cart_items as $item) { |
|
451 | + if ($item['id'] == $item_id) { |
|
452 | 452 | $ret = true; |
453 | 453 | break; |
454 | 454 | } |
455 | 455 | } |
456 | 456 | } |
457 | 457 | |
458 | - return (bool) apply_filters( 'wpinv_item_in_cart', $ret, $item_id, $options ); |
|
458 | + return (bool) apply_filters('wpinv_item_in_cart', $ret, $item_id, $options); |
|
459 | 459 | } |
460 | 460 | |
461 | -function wpinv_get_cart_item_tax( $item_id = 0, $subtotal = '', $options = array() ) { |
|
461 | +function wpinv_get_cart_item_tax($item_id = 0, $subtotal = '', $options = array()) { |
|
462 | 462 | $tax = 0; |
463 | - if ( ! wpinv_item_is_tax_exclusive( $item_id ) ) { |
|
464 | - $country = !empty( $_POST['country'] ) ? $_POST['country'] : false; |
|
465 | - $state = isset( $_POST['state'] ) ? $_POST['state'] : ''; |
|
463 | + if (!wpinv_item_is_tax_exclusive($item_id)) { |
|
464 | + $country = !empty($_POST['country']) ? $_POST['country'] : false; |
|
465 | + $state = isset($_POST['state']) ? $_POST['state'] : ''; |
|
466 | 466 | |
467 | - $tax = wpinv_calculate_tax( $subtotal, $country, $state, $item_id ); |
|
467 | + $tax = wpinv_calculate_tax($subtotal, $country, $state, $item_id); |
|
468 | 468 | } |
469 | 469 | |
470 | - return apply_filters( 'wpinv_get_cart_item_tax', $tax, $item_id, $subtotal, $options ); |
|
470 | + return apply_filters('wpinv_get_cart_item_tax', $tax, $item_id, $subtotal, $options); |
|
471 | 471 | } |
472 | 472 | |
473 | -function wpinv_cart_item_price( $item ) { |
|
474 | - $item_id = isset( $item['id'] ) ? $item['id'] : 0; |
|
475 | - $price = isset( $item['item_price'] ) ? wpinv_round_amount( $item['item_price'] ) : 0; |
|
476 | - $tax = wpinv_price( wpinv_format_amount( $item['tax'] ) ); |
|
473 | +function wpinv_cart_item_price($item) { |
|
474 | + $item_id = isset($item['id']) ? $item['id'] : 0; |
|
475 | + $price = isset($item['item_price']) ? wpinv_round_amount($item['item_price']) : 0; |
|
476 | + $tax = wpinv_price(wpinv_format_amount($item['tax'])); |
|
477 | 477 | |
478 | - if ( !wpinv_is_free_item( $item_id ) && !wpinv_item_is_tax_exclusive( $item_id ) ) { |
|
479 | - if ( wpinv_prices_show_tax_on_checkout() && !wpinv_prices_include_tax() ) { |
|
478 | + if (!wpinv_is_free_item($item_id) && !wpinv_item_is_tax_exclusive($item_id)) { |
|
479 | + if (wpinv_prices_show_tax_on_checkout() && !wpinv_prices_include_tax()) { |
|
480 | 480 | $price += $tax; |
481 | 481 | } |
482 | 482 | |
483 | - if( !wpinv_prices_show_tax_on_checkout() && wpinv_prices_include_tax() ) { |
|
483 | + if (!wpinv_prices_show_tax_on_checkout() && wpinv_prices_include_tax()) { |
|
484 | 484 | $price -= $tax; |
485 | 485 | } |
486 | 486 | } |
487 | 487 | |
488 | - $price = wpinv_price( wpinv_format_amount( $price ) ); |
|
488 | + $price = wpinv_price(wpinv_format_amount($price)); |
|
489 | 489 | |
490 | - return apply_filters( 'wpinv_cart_item_price_label', $price, $item ); |
|
490 | + return apply_filters('wpinv_cart_item_price_label', $price, $item); |
|
491 | 491 | } |
492 | 492 | |
493 | -function wpinv_cart_item_subtotal( $item ) { |
|
494 | - $subtotal = isset( $item['subtotal'] ) ? $item['subtotal'] : 0; |
|
495 | - $subtotal = wpinv_price( wpinv_format_amount( $subtotal ) ); |
|
493 | +function wpinv_cart_item_subtotal($item) { |
|
494 | + $subtotal = isset($item['subtotal']) ? $item['subtotal'] : 0; |
|
495 | + $subtotal = wpinv_price(wpinv_format_amount($subtotal)); |
|
496 | 496 | |
497 | - return apply_filters( 'wpinv_cart_item_subtotal_label', $subtotal, $item ); |
|
497 | + return apply_filters('wpinv_cart_item_subtotal_label', $subtotal, $item); |
|
498 | 498 | } |
499 | 499 | |
500 | -function wpinv_cart_item_tax( $item ) { |
|
500 | +function wpinv_cart_item_tax($item) { |
|
501 | 501 | $tax = ''; |
502 | 502 | $tax_rate = ''; |
503 | 503 | |
504 | - if ( isset( $item['tax'] ) && $item['tax'] > 0 && $item['subtotal'] > 0 ) { |
|
505 | - $tax = wpinv_price( wpinv_format_amount( $item['tax'] ) ); |
|
506 | - $tax_rate = !empty( $item['vat_rate'] ) ? $item['vat_rate'] : ( $item['tax'] / $item['subtotal'] ) * 100; |
|
507 | - $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : ''; |
|
504 | + if (isset($item['tax']) && $item['tax'] > 0 && $item['subtotal'] > 0) { |
|
505 | + $tax = wpinv_price(wpinv_format_amount($item['tax'])); |
|
506 | + $tax_rate = !empty($item['vat_rate']) ? $item['vat_rate'] : ($item['tax'] / $item['subtotal']) * 100; |
|
507 | + $tax_rate = $tax_rate > 0 ? (float) wpinv_round_amount($tax_rate, 4) : ''; |
|
508 | 508 | $tax_rate = $tax_rate != '' ? ' <small class="tax-rate normal small">(' . $tax_rate . '%)</small>' : ''; |
509 | 509 | } |
510 | 510 | |
511 | - $tax = $tax . $tax_rate; |
|
511 | + $tax = $tax . $tax_rate; |
|
512 | 512 | |
513 | - if ( $tax === '' ) { |
|
513 | + if ($tax === '') { |
|
514 | 514 | $tax = 0; // Zero tax |
515 | 515 | } |
516 | 516 | |
517 | - return apply_filters( 'wpinv_cart_item_tax_label', $tax, $item ); |
|
517 | + return apply_filters('wpinv_cart_item_tax_label', $tax, $item); |
|
518 | 518 | } |
519 | 519 | |
520 | -function wpinv_get_cart_item_price( $item_id = 0, $cart_item = array(), $options = array(), $remove_tax_from_inclusive = false ) { |
|
520 | +function wpinv_get_cart_item_price($item_id = 0, $cart_item = array(), $options = array(), $remove_tax_from_inclusive = false) { |
|
521 | 521 | $price = 0; |
522 | 522 | |
523 | 523 | // Set custom price |
524 | - if ( isset( $cart_item['custom_price'] ) && $cart_item['custom_price'] !== '' ) { |
|
524 | + if (isset($cart_item['custom_price']) && $cart_item['custom_price'] !== '') { |
|
525 | 525 | $price = $cart_item['custom_price']; |
526 | 526 | } else { |
527 | - $variable_prices = wpinv_has_variable_prices( $item_id ); |
|
527 | + $variable_prices = wpinv_has_variable_prices($item_id); |
|
528 | 528 | |
529 | - if ( $variable_prices ) { |
|
530 | - $prices = wpinv_get_variable_prices( $item_id ); |
|
529 | + if ($variable_prices) { |
|
530 | + $prices = wpinv_get_variable_prices($item_id); |
|
531 | 531 | |
532 | - if ( $prices ) { |
|
533 | - if( ! empty( $options ) ) { |
|
534 | - $price = isset( $prices[ $options['price_id'] ] ) ? $prices[ $options['price_id'] ]['amount'] : false; |
|
532 | + if ($prices) { |
|
533 | + if (!empty($options)) { |
|
534 | + $price = isset($prices[$options['price_id']]) ? $prices[$options['price_id']]['amount'] : false; |
|
535 | 535 | } else { |
536 | 536 | $price = false; |
537 | 537 | } |
538 | 538 | } |
539 | 539 | } |
540 | 540 | |
541 | - if( ! $variable_prices || false === $price ) { |
|
542 | - if($cart_item['item_price'] > 0){ |
|
541 | + if (!$variable_prices || false === $price) { |
|
542 | + if ($cart_item['item_price'] > 0) { |
|
543 | 543 | $price = $cart_item['item_price']; |
544 | 544 | } else { |
545 | 545 | // Get the standard Item price if not using variable prices |
546 | - $price = wpinv_get_item_price( $item_id ); |
|
546 | + $price = wpinv_get_item_price($item_id); |
|
547 | 547 | } |
548 | 548 | } |
549 | 549 | } |
550 | 550 | |
551 | - if ( $remove_tax_from_inclusive && wpinv_prices_include_tax() ) { |
|
552 | - $price -= wpinv_get_cart_item_tax( $item_id, $price, $options ); |
|
551 | + if ($remove_tax_from_inclusive && wpinv_prices_include_tax()) { |
|
552 | + $price -= wpinv_get_cart_item_tax($item_id, $price, $options); |
|
553 | 553 | } |
554 | 554 | |
555 | - return apply_filters( 'wpinv_cart_item_price', $price, $item_id, $cart_item, $options, $remove_tax_from_inclusive ); |
|
555 | + return apply_filters('wpinv_cart_item_price', $price, $item_id, $cart_item, $options, $remove_tax_from_inclusive); |
|
556 | 556 | } |
557 | 557 | |
558 | -function wpinv_get_cart_item_price_id( $item = array() ) { |
|
559 | - if( isset( $item['item_number'] ) ) { |
|
560 | - $price_id = isset( $item['item_number']['options']['price_id'] ) ? $item['item_number']['options']['price_id'] : null; |
|
558 | +function wpinv_get_cart_item_price_id($item = array()) { |
|
559 | + if (isset($item['item_number'])) { |
|
560 | + $price_id = isset($item['item_number']['options']['price_id']) ? $item['item_number']['options']['price_id'] : null; |
|
561 | 561 | } else { |
562 | - $price_id = isset( $item['options']['price_id'] ) ? $item['options']['price_id'] : null; |
|
562 | + $price_id = isset($item['options']['price_id']) ? $item['options']['price_id'] : null; |
|
563 | 563 | } |
564 | 564 | return $price_id; |
565 | 565 | } |
566 | 566 | |
567 | -function wpinv_get_cart_item_price_name( $item = array() ) { |
|
568 | - $price_id = (int)wpinv_get_cart_item_price_id( $item ); |
|
569 | - $prices = wpinv_get_variable_prices( $item['id'] ); |
|
570 | - $name = ! empty( $prices[ $price_id ] ) ? $prices[ $price_id ]['name'] : ''; |
|
571 | - return apply_filters( 'wpinv_get_cart_item_price_name', $name, $item['id'], $price_id, $item ); |
|
567 | +function wpinv_get_cart_item_price_name($item = array()) { |
|
568 | + $price_id = (int) wpinv_get_cart_item_price_id($item); |
|
569 | + $prices = wpinv_get_variable_prices($item['id']); |
|
570 | + $name = !empty($prices[$price_id]) ? $prices[$price_id]['name'] : ''; |
|
571 | + return apply_filters('wpinv_get_cart_item_price_name', $name, $item['id'], $price_id, $item); |
|
572 | 572 | } |
573 | 573 | |
574 | -function wpinv_get_cart_item_name( $item = array() ) { |
|
575 | - $item_title = !empty( $item['name'] ) ? $item['name'] : get_the_title( $item['id'] ); |
|
574 | +function wpinv_get_cart_item_name($item = array()) { |
|
575 | + $item_title = !empty($item['name']) ? $item['name'] : get_the_title($item['id']); |
|
576 | 576 | |
577 | - if ( empty( $item_title ) ) { |
|
577 | + if (empty($item_title)) { |
|
578 | 578 | $item_title = $item['id']; |
579 | 579 | } |
580 | 580 | |
@@ -584,23 +584,23 @@ discard block |
||
584 | 584 | } |
585 | 585 | */ |
586 | 586 | |
587 | - return apply_filters( 'wpinv_get_cart_item_name', $item_title, $item['id'], $item ); |
|
587 | + return apply_filters('wpinv_get_cart_item_name', $item_title, $item['id'], $item); |
|
588 | 588 | } |
589 | 589 | |
590 | -function wpinv_has_variable_prices( $item_id = 0 ) { |
|
590 | +function wpinv_has_variable_prices($item_id = 0) { |
|
591 | 591 | return false; |
592 | 592 | } |
593 | 593 | |
594 | -function wpinv_get_item_position_in_cart( $item_id = 0, $options = array() ) { |
|
594 | +function wpinv_get_item_position_in_cart($item_id = 0, $options = array()) { |
|
595 | 595 | $cart_items = wpinv_get_cart_contents(); |
596 | 596 | |
597 | - if ( !is_array( $cart_items ) ) { |
|
597 | + if (!is_array($cart_items)) { |
|
598 | 598 | return false; // Empty cart |
599 | 599 | } else { |
600 | - foreach ( $cart_items as $position => $item ) { |
|
601 | - if ( $item['id'] == $item_id ) { |
|
602 | - if ( isset( $options['price_id'] ) && isset( $item['options']['price_id'] ) ) { |
|
603 | - if ( (int) $options['price_id'] == (int) $item['options']['price_id'] ) { |
|
600 | + foreach ($cart_items as $position => $item) { |
|
601 | + if ($item['id'] == $item_id) { |
|
602 | + if (isset($options['price_id']) && isset($item['options']['price_id'])) { |
|
603 | + if ((int) $options['price_id'] == (int) $item['options']['price_id']) { |
|
604 | 604 | return $position; |
605 | 605 | } |
606 | 606 | } else { |
@@ -613,80 +613,80 @@ discard block |
||
613 | 613 | return false; // Not found |
614 | 614 | } |
615 | 615 | |
616 | -function wpinv_get_cart_item_quantity( $item ) { |
|
617 | - if ( wpinv_item_quantities_enabled() ) { |
|
618 | - $quantity = !empty( $item['quantity'] ) && (int)$item['quantity'] > 0 ? absint( $item['quantity'] ) : 1; |
|
616 | +function wpinv_get_cart_item_quantity($item) { |
|
617 | + if (wpinv_item_quantities_enabled()) { |
|
618 | + $quantity = !empty($item['quantity']) && (int) $item['quantity'] > 0 ? absint($item['quantity']) : 1; |
|
619 | 619 | } else { |
620 | 620 | $quantity = 1; |
621 | 621 | } |
622 | 622 | |
623 | - if ( $quantity < 1 ) { |
|
623 | + if ($quantity < 1) { |
|
624 | 624 | $quantity = 1; |
625 | 625 | } |
626 | 626 | |
627 | - return apply_filters( 'wpinv_get_cart_item_quantity', $quantity, $item ); |
|
627 | + return apply_filters('wpinv_get_cart_item_quantity', $quantity, $item); |
|
628 | 628 | } |
629 | 629 | |
630 | -function wpinv_get_item_suffix( $item, $html = true ) { |
|
631 | - if ( empty( $item ) ) { |
|
630 | +function wpinv_get_item_suffix($item, $html = true) { |
|
631 | + if (empty($item)) { |
|
632 | 632 | return NULL; |
633 | 633 | } |
634 | 634 | |
635 | - if ( is_int( $item ) ) { |
|
636 | - $item = new WPInv_Item( $item ); |
|
635 | + if (is_int($item)) { |
|
636 | + $item = new WPInv_Item($item); |
|
637 | 637 | } |
638 | 638 | |
639 | - if ( !( is_object( $item ) && is_a( $item, 'WPInv_Item' ) ) ) { |
|
639 | + if (!(is_object($item) && is_a($item, 'WPInv_Item'))) { |
|
640 | 640 | return NULL; |
641 | 641 | } |
642 | 642 | |
643 | - $suffix = $item->is_recurring() ? ' <span class="wpi-suffix">' . __( '(r)', 'invoicing' ) . '</span>' : ''; |
|
643 | + $suffix = $item->is_recurring() ? ' <span class="wpi-suffix">' . __('(r)', 'invoicing') . '</span>' : ''; |
|
644 | 644 | |
645 | - if ( !$html && $suffix ) { |
|
646 | - $suffix = strip_tags( $suffix ); |
|
645 | + if (!$html && $suffix) { |
|
646 | + $suffix = strip_tags($suffix); |
|
647 | 647 | } |
648 | 648 | |
649 | - return apply_filters( 'wpinv_get_item_suffix', $suffix, $item, $html ); |
|
649 | + return apply_filters('wpinv_get_item_suffix', $suffix, $item, $html); |
|
650 | 650 | } |
651 | 651 | |
652 | -function wpinv_remove_item( $item = 0, $force_delete = false ) { |
|
653 | - if ( empty( $item ) ) { |
|
652 | +function wpinv_remove_item($item = 0, $force_delete = false) { |
|
653 | + if (empty($item)) { |
|
654 | 654 | return NULL; |
655 | 655 | } |
656 | 656 | |
657 | - if ( is_int( $item ) ) { |
|
658 | - $item = new WPInv_Item( $item ); |
|
657 | + if (is_int($item)) { |
|
658 | + $item = new WPInv_Item($item); |
|
659 | 659 | } |
660 | 660 | |
661 | - if ( !( is_object( $item ) && is_a( $item, 'WPInv_Item' ) ) ) { |
|
661 | + if (!(is_object($item) && is_a($item, 'WPInv_Item'))) { |
|
662 | 662 | return NULL; |
663 | 663 | } |
664 | 664 | |
665 | - do_action( 'wpinv_pre_delete_item', $item ); |
|
665 | + do_action('wpinv_pre_delete_item', $item); |
|
666 | 666 | |
667 | - wp_delete_post( $item->ID, $force_delete ); |
|
667 | + wp_delete_post($item->ID, $force_delete); |
|
668 | 668 | |
669 | - do_action( 'wpinv_post_delete_item', $item ); |
|
669 | + do_action('wpinv_post_delete_item', $item); |
|
670 | 670 | } |
671 | 671 | |
672 | -function wpinv_can_delete_item( $post_id ) { |
|
672 | +function wpinv_can_delete_item($post_id) { |
|
673 | 673 | $return = wpinv_current_user_can_manage_invoicing() ? true : false; |
674 | 674 | |
675 | - if ( $return && wpinv_item_in_use( $post_id ) ) { |
|
675 | + if ($return && wpinv_item_in_use($post_id)) { |
|
676 | 676 | $return = false; // Don't delete item already use in invoices. |
677 | 677 | } |
678 | 678 | |
679 | - return apply_filters( 'wpinv_can_delete_item', $return, $post_id ); |
|
679 | + return apply_filters('wpinv_can_delete_item', $return, $post_id); |
|
680 | 680 | } |
681 | 681 | |
682 | 682 | function wpinv_admin_action_delete() { |
683 | 683 | $screen = get_current_screen(); |
684 | 684 | |
685 | - if ( !empty( $screen->post_type ) && $screen->post_type == 'wpi_item' && !empty( $_REQUEST['post'] ) && is_array( $_REQUEST['post'] ) ) { |
|
685 | + if (!empty($screen->post_type) && $screen->post_type == 'wpi_item' && !empty($_REQUEST['post']) && is_array($_REQUEST['post'])) { |
|
686 | 686 | $post_ids = array(); |
687 | 687 | |
688 | - foreach ( $_REQUEST['post'] as $post_id ) { |
|
689 | - if ( !wpinv_can_delete_item( $post_id ) ) { |
|
688 | + foreach ($_REQUEST['post'] as $post_id) { |
|
689 | + if (!wpinv_can_delete_item($post_id)) { |
|
690 | 690 | continue; |
691 | 691 | } |
692 | 692 | |
@@ -696,88 +696,88 @@ discard block |
||
696 | 696 | $_REQUEST['post'] = $post_ids; |
697 | 697 | } |
698 | 698 | } |
699 | -add_action( 'admin_action_trash', 'wpinv_admin_action_delete', -10 ); |
|
700 | -add_action( 'admin_action_delete', 'wpinv_admin_action_delete', -10 ); |
|
699 | +add_action('admin_action_trash', 'wpinv_admin_action_delete', -10); |
|
700 | +add_action('admin_action_delete', 'wpinv_admin_action_delete', -10); |
|
701 | 701 | |
702 | -function wpinv_check_delete_item( $check, $post, $force_delete ) { |
|
703 | - if ( $post->post_type == 'wpi_item' ) { |
|
704 | - if ( $force_delete && !wpinv_can_delete_item( $post->ID ) ) { |
|
702 | +function wpinv_check_delete_item($check, $post, $force_delete) { |
|
703 | + if ($post->post_type == 'wpi_item') { |
|
704 | + if ($force_delete && !wpinv_can_delete_item($post->ID)) { |
|
705 | 705 | return true; |
706 | 706 | } |
707 | 707 | } |
708 | 708 | |
709 | 709 | return $check; |
710 | 710 | } |
711 | -add_filter( 'pre_delete_post', 'wpinv_check_delete_item', 10, 3 ); |
|
711 | +add_filter('pre_delete_post', 'wpinv_check_delete_item', 10, 3); |
|
712 | 712 | |
713 | -function wpinv_item_in_use( $item_id ) { |
|
713 | +function wpinv_item_in_use($item_id) { |
|
714 | 714 | global $wpdb, $wpi_items_in_use; |
715 | 715 | |
716 | - if ( !$item_id > 0 ) { |
|
716 | + if (!$item_id > 0) { |
|
717 | 717 | return false; |
718 | 718 | } |
719 | 719 | |
720 | - if ( !empty( $wpi_items_in_use ) ) { |
|
721 | - if ( isset( $wpi_items_in_use[$item_id] ) ) { |
|
720 | + if (!empty($wpi_items_in_use)) { |
|
721 | + if (isset($wpi_items_in_use[$item_id])) { |
|
722 | 722 | return $wpi_items_in_use[$item_id]; |
723 | 723 | } |
724 | 724 | } else { |
725 | 725 | $wpi_items_in_use = array(); |
726 | 726 | } |
727 | 727 | |
728 | - $statuses = array_keys( wpinv_get_invoice_statuses( true, true ) ); |
|
728 | + $statuses = array_keys(wpinv_get_invoice_statuses(true, true)); |
|
729 | 729 | |
730 | - $query = "SELECT p.ID FROM " . $wpdb->posts . " AS p INNER JOIN " . $wpdb->postmeta . " AS pm ON p.ID = pm.post_id WHERE p.post_type = 'wpi_invoice' AND p.post_status IN( '" . implode( "','", $statuses ) . "' ) AND pm.meta_key = '_wpinv_item_ids' AND FIND_IN_SET( '" . (int)$item_id . "', pm.meta_value )"; |
|
731 | - $in_use = $wpdb->get_var( $query ) > 0 ? true : false; |
|
730 | + $query = "SELECT p.ID FROM " . $wpdb->posts . " AS p INNER JOIN " . $wpdb->postmeta . " AS pm ON p.ID = pm.post_id WHERE p.post_type = 'wpi_invoice' AND p.post_status IN( '" . implode("','", $statuses) . "' ) AND pm.meta_key = '_wpinv_item_ids' AND FIND_IN_SET( '" . (int) $item_id . "', pm.meta_value )"; |
|
731 | + $in_use = $wpdb->get_var($query) > 0 ? true : false; |
|
732 | 732 | |
733 | 733 | $wpi_items_in_use[$item_id] = $in_use; |
734 | 734 | |
735 | 735 | return $in_use; |
736 | 736 | } |
737 | 737 | |
738 | -function wpinv_create_item( $args = array(), $wp_error = false, $force_update = false ) { |
|
738 | +function wpinv_create_item($args = array(), $wp_error = false, $force_update = false) { |
|
739 | 739 | // Set some defaults |
740 | 740 | $defaults = array( |
741 | - 'type' => 'custom', // Optional. Item type. Default 'custom'. |
|
742 | - 'title' => '', // Required. Item title. |
|
743 | - 'custom_id' => 0, // Optional. Any integer or non numeric id. Must be unique within item type. |
|
744 | - 'price' => '0.00', // Optional. Item price. Default '0.00'. |
|
745 | - 'status' => 'pending', // Optional. pending, publish |
|
746 | - 'custom_name' => '', // Optional. Plural sub title for item. |
|
747 | - 'custom_singular_name' => '', // Optional. Singular sub title for item. |
|
748 | - 'vat_rule' => 'digital', // Optional. digital => Digital item, physical => Physical item |
|
749 | - 'editable' => true, // Optional. Item editable from Items list page? Default true. |
|
750 | - 'excerpt' => '', // Optional. Item short description |
|
741 | + 'type' => 'custom', // Optional. Item type. Default 'custom'. |
|
742 | + 'title' => '', // Required. Item title. |
|
743 | + 'custom_id' => 0, // Optional. Any integer or non numeric id. Must be unique within item type. |
|
744 | + 'price' => '0.00', // Optional. Item price. Default '0.00'. |
|
745 | + 'status' => 'pending', // Optional. pending, publish |
|
746 | + 'custom_name' => '', // Optional. Plural sub title for item. |
|
747 | + 'custom_singular_name' => '', // Optional. Singular sub title for item. |
|
748 | + 'vat_rule' => 'digital', // Optional. digital => Digital item, physical => Physical item |
|
749 | + 'editable' => true, // Optional. Item editable from Items list page? Default true. |
|
750 | + 'excerpt' => '', // Optional. Item short description |
|
751 | 751 | /* Recurring item fields */ |
752 | - 'is_recurring' => 0, // Optional. 1 => Allow recurring or 0 => Don't allow recurring |
|
753 | - 'recurring_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
754 | - 'recurring_interval' => 0, // Optional. Integer value between 1 - 90. |
|
755 | - 'recurring_limit' => 0, // Optional. Any integer number. 0 for recurring forever until cancelled. |
|
756 | - 'free_trial' => 0, // Optional. 1 => Allow free trial or 0 => Don't free trial |
|
757 | - 'trial_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
758 | - 'trial_interval' => 0, // Optional. Any integer number. |
|
759 | - 'minimum_price' => '0.00', // Optional. Minimum allowed prices for items with dynamic pricing. |
|
760 | - 'dynamic_pricing' => 0, // Optional. Whether or not the item supports dynamic prices. |
|
752 | + 'is_recurring' => 0, // Optional. 1 => Allow recurring or 0 => Don't allow recurring |
|
753 | + 'recurring_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
754 | + 'recurring_interval' => 0, // Optional. Integer value between 1 - 90. |
|
755 | + 'recurring_limit' => 0, // Optional. Any integer number. 0 for recurring forever until cancelled. |
|
756 | + 'free_trial' => 0, // Optional. 1 => Allow free trial or 0 => Don't free trial |
|
757 | + 'trial_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
758 | + 'trial_interval' => 0, // Optional. Any integer number. |
|
759 | + 'minimum_price' => '0.00', // Optional. Minimum allowed prices for items with dynamic pricing. |
|
760 | + 'dynamic_pricing' => 0, // Optional. Whether or not the item supports dynamic prices. |
|
761 | 761 | ); |
762 | 762 | |
763 | - $data = wp_parse_args( $args, $defaults ); |
|
763 | + $data = wp_parse_args($args, $defaults); |
|
764 | 764 | |
765 | - if ( empty( $data['type'] ) ) { |
|
765 | + if (empty($data['type'])) { |
|
766 | 766 | $data['type'] = 'custom'; |
767 | 767 | } |
768 | 768 | |
769 | - if ( !empty( $data['custom_id'] ) ) { |
|
770 | - $item = wpinv_get_item_by( 'custom_id', $data['custom_id'], $data['type'] ); |
|
769 | + if (!empty($data['custom_id'])) { |
|
770 | + $item = wpinv_get_item_by('custom_id', $data['custom_id'], $data['type']); |
|
771 | 771 | } else { |
772 | 772 | $item = NULL; |
773 | 773 | } |
774 | 774 | |
775 | - if ( !empty( $item ) ) { |
|
776 | - if ( $force_update ) { |
|
777 | - if ( empty( $args['ID'] ) ) { |
|
775 | + if (!empty($item)) { |
|
776 | + if ($force_update) { |
|
777 | + if (empty($args['ID'])) { |
|
778 | 778 | $args['ID'] = $item->ID; |
779 | 779 | } |
780 | - return wpinv_update_item( $args, $wp_error ); |
|
780 | + return wpinv_update_item($args, $wp_error); |
|
781 | 781 | } |
782 | 782 | |
783 | 783 | return $item; |
@@ -788,21 +788,21 @@ discard block |
||
788 | 788 | $meta['custom_id'] = $data['custom_id']; |
789 | 789 | $meta['custom_singular_name'] = $data['custom_singular_name']; |
790 | 790 | $meta['custom_name'] = $data['custom_name']; |
791 | - $meta['price'] = wpinv_round_amount( $data['price'] ); |
|
792 | - $meta['editable'] = (int)$data['editable']; |
|
791 | + $meta['price'] = wpinv_round_amount($data['price']); |
|
792 | + $meta['editable'] = (int) $data['editable']; |
|
793 | 793 | $meta['vat_rule'] = $data['vat_rule']; |
794 | 794 | $meta['vat_class'] = '_standard'; |
795 | 795 | $meta['dynamic_pricing'] = (int) $data['dynamic_pricing']; |
796 | - $meta['minimum_price'] = wpinv_round_amount( $data['minimum_price'] ); |
|
796 | + $meta['minimum_price'] = wpinv_round_amount($data['minimum_price']); |
|
797 | 797 | |
798 | - if ( !empty( $data['is_recurring'] ) ) { |
|
798 | + if (!empty($data['is_recurring'])) { |
|
799 | 799 | $meta['is_recurring'] = $data['is_recurring']; |
800 | 800 | $meta['recurring_period'] = $data['recurring_period']; |
801 | - $meta['recurring_interval'] = absint( $data['recurring_interval'] ); |
|
802 | - $meta['recurring_limit'] = absint( $data['recurring_limit'] ); |
|
801 | + $meta['recurring_interval'] = absint($data['recurring_interval']); |
|
802 | + $meta['recurring_limit'] = absint($data['recurring_limit']); |
|
803 | 803 | $meta['free_trial'] = $data['free_trial']; |
804 | 804 | $meta['trial_period'] = $data['trial_period']; |
805 | - $meta['trial_interval'] = absint( $data['trial_interval'] ); |
|
805 | + $meta['trial_interval'] = absint($data['trial_interval']); |
|
806 | 806 | } else { |
807 | 807 | $meta['is_recurring'] = 0; |
808 | 808 | $meta['recurring_period'] = ''; |
@@ -813,7 +813,7 @@ discard block |
||
813 | 813 | $meta['trial_interval'] = ''; |
814 | 814 | } |
815 | 815 | |
816 | - $post_data = array( |
|
816 | + $post_data = array( |
|
817 | 817 | 'post_title' => $data['title'], |
818 | 818 | 'post_excerpt' => $data['excerpt'], |
819 | 819 | 'post_status' => $data['status'], |
@@ -821,34 +821,34 @@ discard block |
||
821 | 821 | ); |
822 | 822 | |
823 | 823 | $item = new WPInv_Item(); |
824 | - $return = $item->create( $post_data, $wp_error ); |
|
824 | + $return = $item->create($post_data, $wp_error); |
|
825 | 825 | |
826 | - if ( $return && !empty( $item ) && !is_wp_error( $return ) ) { |
|
826 | + if ($return && !empty($item) && !is_wp_error($return)) { |
|
827 | 827 | return $item; |
828 | 828 | } |
829 | 829 | |
830 | - if ( $wp_error && is_wp_error( $return ) ) { |
|
830 | + if ($wp_error && is_wp_error($return)) { |
|
831 | 831 | return $return; |
832 | 832 | } |
833 | 833 | return 0; |
834 | 834 | } |
835 | 835 | |
836 | -function wpinv_update_item( $args = array(), $wp_error = false ) { |
|
837 | - $item = !empty( $args['ID'] ) ? new WPInv_Item( $args['ID'] ) : NULL; |
|
836 | +function wpinv_update_item($args = array(), $wp_error = false) { |
|
837 | + $item = !empty($args['ID']) ? new WPInv_Item($args['ID']) : NULL; |
|
838 | 838 | |
839 | - if ( empty( $item ) || !( !empty( $item->post_type ) && $item->post_type == 'wpi_item' ) ) { |
|
840 | - if ( $wp_error ) { |
|
841 | - return new WP_Error( 'wpinv_invalid_item', __( 'Invalid item.', 'invoicing' ) ); |
|
839 | + if (empty($item) || !(!empty($item->post_type) && $item->post_type == 'wpi_item')) { |
|
840 | + if ($wp_error) { |
|
841 | + return new WP_Error('wpinv_invalid_item', __('Invalid item.', 'invoicing')); |
|
842 | 842 | } |
843 | 843 | return 0; |
844 | 844 | } |
845 | 845 | |
846 | - if ( !empty( $args['custom_id'] ) ) { |
|
847 | - $item_exists = wpinv_get_item_by( 'custom_id', $args['custom_id'], ( !empty( $args['type'] ) ? $args['type'] : $item->type ) ); |
|
846 | + if (!empty($args['custom_id'])) { |
|
847 | + $item_exists = wpinv_get_item_by('custom_id', $args['custom_id'], (!empty($args['type']) ? $args['type'] : $item->type)); |
|
848 | 848 | |
849 | - if ( !empty( $item_exists ) && $item_exists->ID != $args['ID'] ) { |
|
850 | - if ( $wp_error ) { |
|
851 | - return new WP_Error( 'wpinv_invalid_custom_id', __( 'Item with custom id already exists.', 'invoicing' ) ); |
|
849 | + if (!empty($item_exists) && $item_exists->ID != $args['ID']) { |
|
850 | + if ($wp_error) { |
|
851 | + return new WP_Error('wpinv_invalid_custom_id', __('Item with custom id already exists.', 'invoicing')); |
|
852 | 852 | } |
853 | 853 | return 0; |
854 | 854 | } |
@@ -875,54 +875,54 @@ discard block |
||
875 | 875 | ); |
876 | 876 | |
877 | 877 | $post_data = array(); |
878 | - if ( isset( $args['title'] ) ) { |
|
878 | + if (isset($args['title'])) { |
|
879 | 879 | $post_data['post_title'] = $args['title']; |
880 | 880 | } |
881 | - if ( isset( $args['excerpt'] ) ) { |
|
881 | + if (isset($args['excerpt'])) { |
|
882 | 882 | $post_data['post_excerpt'] = $args['excerpt']; |
883 | 883 | } |
884 | - if ( isset( $args['status'] ) ) { |
|
884 | + if (isset($args['status'])) { |
|
885 | 885 | $post_data['post_status'] = $args['status']; |
886 | 886 | } |
887 | 887 | |
888 | - foreach ( $meta_fields as $meta_field ) { |
|
889 | - if ( isset( $args[ $meta_field ] ) ) { |
|
890 | - $value = $args[ $meta_field ]; |
|
888 | + foreach ($meta_fields as $meta_field) { |
|
889 | + if (isset($args[$meta_field])) { |
|
890 | + $value = $args[$meta_field]; |
|
891 | 891 | |
892 | - switch ( $meta_field ) { |
|
892 | + switch ($meta_field) { |
|
893 | 893 | case 'price': |
894 | 894 | case 'minimum_price': |
895 | - $value = wpinv_round_amount( $value ); |
|
895 | + $value = wpinv_round_amount($value); |
|
896 | 896 | break; |
897 | 897 | case 'recurring_interval': |
898 | 898 | case 'recurring_limit': |
899 | 899 | case 'trial_interval': |
900 | - $value = absint( $value ); |
|
900 | + $value = absint($value); |
|
901 | 901 | break; |
902 | 902 | case 'editable': |
903 | 903 | $value = (int) $value; |
904 | 904 | break; |
905 | 905 | } |
906 | 906 | |
907 | - $post_data['meta'][ $meta_field ] = $value; |
|
907 | + $post_data['meta'][$meta_field] = $value; |
|
908 | 908 | }; |
909 | 909 | } |
910 | 910 | |
911 | - if ( empty( $post_data ) ) { |
|
912 | - if ( $wp_error ) { |
|
913 | - return new WP_Error( 'wpinv_invalid_item_data', __( 'Invalid item data.', 'invoicing' ) ); |
|
911 | + if (empty($post_data)) { |
|
912 | + if ($wp_error) { |
|
913 | + return new WP_Error('wpinv_invalid_item_data', __('Invalid item data.', 'invoicing')); |
|
914 | 914 | } |
915 | 915 | return 0; |
916 | 916 | } |
917 | 917 | $post_data['ID'] = $args['ID']; |
918 | 918 | |
919 | - $return = $item->update( $post_data, $wp_error ); |
|
919 | + $return = $item->update($post_data, $wp_error); |
|
920 | 920 | |
921 | - if ( $return && !empty( $item ) && !is_wp_error( $return ) ) { |
|
921 | + if ($return && !empty($item) && !is_wp_error($return)) { |
|
922 | 922 | return $item; |
923 | 923 | } |
924 | 924 | |
925 | - if ( $wp_error && is_wp_error( $return ) ) { |
|
925 | + if ($wp_error && is_wp_error($return)) { |
|
926 | 926 | return $return; |
927 | 927 | } |
928 | 928 | return 0; |
@@ -20,31 +20,31 @@ discard block |
||
20 | 20 | class WPInv_REST_Invoice_Controller extends WP_REST_Posts_Controller { |
21 | 21 | |
22 | 22 | /** |
23 | - * Post type. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $post_type = 'wpi_invoice'; |
|
23 | + * Post type. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $post_type = 'wpi_invoice'; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Cached results of get_item_schema. |
|
31 | - * |
|
32 | - * @since 1.0.13 |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - protected $schema; |
|
29 | + /** |
|
30 | + * Cached results of get_item_schema. |
|
31 | + * |
|
32 | + * @since 1.0.13 |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + protected $schema; |
|
36 | 36 | |
37 | 37 | /** |
38 | - * Constructor. |
|
39 | - * |
|
40 | - * @since 1.0.13 |
|
41 | - * |
|
42 | - * @param string $namespace Api Namespace |
|
43 | - */ |
|
44 | - public function __construct( $namespace ) { |
|
38 | + * Constructor. |
|
39 | + * |
|
40 | + * @since 1.0.13 |
|
41 | + * |
|
42 | + * @param string $namespace Api Namespace |
|
43 | + */ |
|
44 | + public function __construct( $namespace ) { |
|
45 | 45 | |
46 | 46 | // Set api namespace... |
47 | - $this->namespace = $namespace; |
|
47 | + $this->namespace = $namespace; |
|
48 | 48 | |
49 | 49 | // ... and the rest base |
50 | 50 | $this->rest_base = 'invoices'; |
@@ -52,39 +52,39 @@ discard block |
||
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
55 | - * Checks if a given request has access to read invoices. |
|
55 | + * Checks if a given request has access to read invoices. |
|
56 | 56 | * |
57 | - * |
|
58 | - * @since 1.0.13 |
|
59 | - * |
|
60 | - * @param WP_REST_Request $request Full details about the request. |
|
61 | - * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
62 | - */ |
|
63 | - public function get_items_permissions_check( $request ) { |
|
57 | + * |
|
58 | + * @since 1.0.13 |
|
59 | + * |
|
60 | + * @param WP_REST_Request $request Full details about the request. |
|
61 | + * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
62 | + */ |
|
63 | + public function get_items_permissions_check( $request ) { |
|
64 | 64 | |
65 | 65 | $post_type = get_post_type_object( $this->post_type ); |
66 | 66 | |
67 | - if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) { |
|
68 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
69 | - } |
|
67 | + if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) { |
|
68 | + return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
69 | + } |
|
70 | 70 | |
71 | - // Read checks will be evaluated on a per invoice basis |
|
71 | + // Read checks will be evaluated on a per invoice basis |
|
72 | 72 | |
73 | - return true; |
|
73 | + return true; |
|
74 | 74 | |
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
78 | - * Retrieves a collection of invoices. |
|
79 | - * |
|
80 | - * @since 1.0.13 |
|
81 | - * |
|
82 | - * @param WP_REST_Request $request Full details about the request. |
|
83 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
84 | - */ |
|
85 | - public function get_items( $request ) { |
|
78 | + * Retrieves a collection of invoices. |
|
79 | + * |
|
80 | + * @since 1.0.13 |
|
81 | + * |
|
82 | + * @param WP_REST_Request $request Full details about the request. |
|
83 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
84 | + */ |
|
85 | + public function get_items( $request ) { |
|
86 | 86 | |
87 | - // Retrieve the list of registered invoice query parameters. |
|
87 | + // Retrieve the list of registered invoice query parameters. |
|
88 | 88 | $registered = $this->get_collection_params(); |
89 | 89 | |
90 | 90 | $args = array(); |
@@ -97,54 +97,54 @@ discard block |
||
97 | 97 | |
98 | 98 | } |
99 | 99 | |
100 | - /** |
|
101 | - * Filters the wpinv_get_invoices arguments for invoices requests. |
|
102 | - * |
|
103 | - * |
|
104 | - * @since 1.0.13 |
|
105 | - * |
|
106 | - * |
|
107 | - * @param array $args Key value array of query var to query value. |
|
108 | - * @param WP_REST_Request $request The request used. |
|
109 | - */ |
|
100 | + /** |
|
101 | + * Filters the wpinv_get_invoices arguments for invoices requests. |
|
102 | + * |
|
103 | + * |
|
104 | + * @since 1.0.13 |
|
105 | + * |
|
106 | + * |
|
107 | + * @param array $args Key value array of query var to query value. |
|
108 | + * @param WP_REST_Request $request The request used. |
|
109 | + */ |
|
110 | 110 | $args = apply_filters( "wpinv_rest_get_invoices_arguments", $args, $request, $this ); |
111 | 111 | |
112 | - // Special args |
|
113 | - $args[ 'return' ] = 'objects'; |
|
114 | - $args[ 'paginate' ] = true; |
|
112 | + // Special args |
|
113 | + $args[ 'return' ] = 'objects'; |
|
114 | + $args[ 'paginate' ] = true; |
|
115 | 115 | |
116 | 116 | // Run the query. |
117 | - $query = wpinv_get_invoices( $args ); |
|
117 | + $query = wpinv_get_invoices( $args ); |
|
118 | 118 | |
119 | - // Prepare the retrieved invoices |
|
120 | - $invoices = array(); |
|
121 | - foreach( $query->invoices as $invoice ) { |
|
122 | - |
|
123 | - if ( ! $this->check_read_permission( $invoice ) ) { |
|
124 | - continue; |
|
125 | - } |
|
126 | - |
|
127 | - $data = $this->prepare_item_for_response( $invoice, $request ); |
|
128 | - $invoices[] = $this->prepare_response_for_collection( $data ); |
|
129 | - |
|
130 | - } |
|
131 | - |
|
132 | - // Prepare the response. |
|
133 | - $response = rest_ensure_response( $invoices ); |
|
134 | - $response->header( 'X-WP-Total', (int) $query->total ); |
|
135 | - $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
136 | - |
|
137 | - /** |
|
138 | - * Filters the responses for invoices requests. |
|
139 | - * |
|
140 | - * |
|
141 | - * @since 1.0.13 |
|
142 | - * |
|
143 | - * |
|
144 | - * @param arrWP_REST_Response $response Response object. |
|
145 | - * @param WP_REST_Request $request The request used. |
|
119 | + // Prepare the retrieved invoices |
|
120 | + $invoices = array(); |
|
121 | + foreach( $query->invoices as $invoice ) { |
|
122 | + |
|
123 | + if ( ! $this->check_read_permission( $invoice ) ) { |
|
124 | + continue; |
|
125 | + } |
|
126 | + |
|
127 | + $data = $this->prepare_item_for_response( $invoice, $request ); |
|
128 | + $invoices[] = $this->prepare_response_for_collection( $data ); |
|
129 | + |
|
130 | + } |
|
131 | + |
|
132 | + // Prepare the response. |
|
133 | + $response = rest_ensure_response( $invoices ); |
|
134 | + $response->header( 'X-WP-Total', (int) $query->total ); |
|
135 | + $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
136 | + |
|
137 | + /** |
|
138 | + * Filters the responses for invoices requests. |
|
139 | + * |
|
140 | + * |
|
141 | + * @since 1.0.13 |
|
142 | + * |
|
143 | + * |
|
144 | + * @param arrWP_REST_Response $response Response object. |
|
145 | + * @param WP_REST_Request $request The request used. |
|
146 | 146 | * @param array $args Array of args used to retrieve the invoices |
147 | - */ |
|
147 | + */ |
|
148 | 148 | $response = apply_filters( "wpinv_rest_invoices_response", $response, $request, $args ); |
149 | 149 | |
150 | 150 | return rest_ensure_response( $response ); |
@@ -152,25 +152,25 @@ discard block |
||
152 | 152 | } |
153 | 153 | |
154 | 154 | /** |
155 | - * Get the post, if the ID is valid. |
|
156 | - * |
|
157 | - * @since 1.0.13 |
|
158 | - * |
|
159 | - * @param int $invoice_id Supplied ID. |
|
160 | - * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise. |
|
161 | - */ |
|
162 | - protected function get_post( $invoice_id ) { |
|
155 | + * Get the post, if the ID is valid. |
|
156 | + * |
|
157 | + * @since 1.0.13 |
|
158 | + * |
|
159 | + * @param int $invoice_id Supplied ID. |
|
160 | + * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise. |
|
161 | + */ |
|
162 | + protected function get_post( $invoice_id ) { |
|
163 | 163 | |
164 | - $error = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
164 | + $error = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
165 | 165 | |
166 | 166 | // Ids start from 1 |
167 | 167 | if ( (int) $invoice_id <= 0 ) { |
168 | - return $error; |
|
169 | - } |
|
168 | + return $error; |
|
169 | + } |
|
170 | 170 | |
171 | - $invoice = wpinv_get_invoice( (int) $invoice_id ); |
|
172 | - if ( empty( $invoice ) ) { |
|
173 | - return $error; |
|
171 | + $invoice = wpinv_get_invoice( (int) $invoice_id ); |
|
172 | + if ( empty( $invoice ) ) { |
|
173 | + return $error; |
|
174 | 174 | } |
175 | 175 | |
176 | 176 | return $invoice; |
@@ -178,77 +178,77 @@ discard block |
||
178 | 178 | } |
179 | 179 | |
180 | 180 | /** |
181 | - * Checks if a given request has access to read an invoice. |
|
182 | - * |
|
183 | - * @since 1.0.13 |
|
184 | - * |
|
185 | - * @param WP_REST_Request $request Full details about the request. |
|
186 | - * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise. |
|
187 | - */ |
|
188 | - public function get_item_permissions_check( $request ) { |
|
181 | + * Checks if a given request has access to read an invoice. |
|
182 | + * |
|
183 | + * @since 1.0.13 |
|
184 | + * |
|
185 | + * @param WP_REST_Request $request Full details about the request. |
|
186 | + * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise. |
|
187 | + */ |
|
188 | + public function get_item_permissions_check( $request ) { |
|
189 | 189 | |
190 | 190 | // Retrieve the invoice object. |
191 | 191 | $invoice = $this->get_post( $request['id'] ); |
192 | 192 | |
193 | 193 | // Ensure it is valid. |
194 | - if ( is_wp_error( $invoice ) ) { |
|
195 | - return $invoice; |
|
196 | - } |
|
194 | + if ( is_wp_error( $invoice ) ) { |
|
195 | + return $invoice; |
|
196 | + } |
|
197 | 197 | |
198 | - if ( $invoice ) { |
|
199 | - return $this->check_read_permission( $invoice ); |
|
200 | - } |
|
198 | + if ( $invoice ) { |
|
199 | + return $this->check_read_permission( $invoice ); |
|
200 | + } |
|
201 | 201 | |
202 | - return true; |
|
202 | + return true; |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | /** |
206 | - * Checks if an invoice can be read. |
|
207 | - * |
|
208 | - * An invoice can be read by site admins and owners of the invoice |
|
209 | - * |
|
210 | - * |
|
211 | - * @since 1.0.13 |
|
212 | - * |
|
213 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
214 | - * @return bool Whether the post can be read. |
|
215 | - */ |
|
216 | - public function check_read_permission( $invoice ) { |
|
217 | - return wpinv_user_can_view_invoice( $invoice->ID ); |
|
206 | + * Checks if an invoice can be read. |
|
207 | + * |
|
208 | + * An invoice can be read by site admins and owners of the invoice |
|
209 | + * |
|
210 | + * |
|
211 | + * @since 1.0.13 |
|
212 | + * |
|
213 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
214 | + * @return bool Whether the post can be read. |
|
215 | + */ |
|
216 | + public function check_read_permission( $invoice ) { |
|
217 | + return wpinv_user_can_view_invoice( $invoice->ID ); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
221 | - * Retrieves a single invoice. |
|
222 | - * |
|
223 | - * @since 1.0.13 |
|
224 | - * |
|
225 | - * @param WP_REST_Request $request Full details about the request. |
|
226 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
227 | - */ |
|
228 | - public function get_item( $request ) { |
|
221 | + * Retrieves a single invoice. |
|
222 | + * |
|
223 | + * @since 1.0.13 |
|
224 | + * |
|
225 | + * @param WP_REST_Request $request Full details about the request. |
|
226 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
227 | + */ |
|
228 | + public function get_item( $request ) { |
|
229 | 229 | |
230 | 230 | // Fetch the invoice. |
231 | 231 | $invoice = $this->get_post( $request['id'] ); |
232 | 232 | |
233 | 233 | // Abort early if it does not exist |
234 | - if ( is_wp_error( $invoice ) ) { |
|
235 | - return $invoice; |
|
236 | - } |
|
237 | - |
|
238 | - // Prepare the response |
|
239 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
240 | - $response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) ); |
|
241 | - |
|
242 | - /** |
|
243 | - * Filters the responses for single invoice requests. |
|
244 | - * |
|
245 | - * |
|
246 | - * @since 1.0.13 |
|
247 | - * @var WP_HTTP_Response |
|
248 | - * |
|
249 | - * @param WP_HTTP_Response $response Response. |
|
250 | - * @param WP_REST_Request $request The request used. |
|
251 | - */ |
|
234 | + if ( is_wp_error( $invoice ) ) { |
|
235 | + return $invoice; |
|
236 | + } |
|
237 | + |
|
238 | + // Prepare the response |
|
239 | + $response = $this->prepare_item_for_response( $invoice, $request ); |
|
240 | + $response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) ); |
|
241 | + |
|
242 | + /** |
|
243 | + * Filters the responses for single invoice requests. |
|
244 | + * |
|
245 | + * |
|
246 | + * @since 1.0.13 |
|
247 | + * @var WP_HTTP_Response |
|
248 | + * |
|
249 | + * @param WP_HTTP_Response $response Response. |
|
250 | + * @param WP_REST_Request $request The request used. |
|
251 | + */ |
|
252 | 252 | $response = apply_filters( "wpinv_rest_get_invoice_response", $response, $request ); |
253 | 253 | |
254 | 254 | return rest_ensure_response( $response ); |
@@ -256,23 +256,23 @@ discard block |
||
256 | 256 | } |
257 | 257 | |
258 | 258 | /** |
259 | - * Checks if a given request has access to create an invoice. |
|
260 | - * |
|
261 | - * @since 1.0.13 |
|
262 | - * |
|
263 | - * @param WP_REST_Request $request Full details about the request. |
|
264 | - * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
265 | - */ |
|
266 | - public function create_item_permissions_check( $request ) { |
|
259 | + * Checks if a given request has access to create an invoice. |
|
260 | + * |
|
261 | + * @since 1.0.13 |
|
262 | + * |
|
263 | + * @param WP_REST_Request $request Full details about the request. |
|
264 | + * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
265 | + */ |
|
266 | + public function create_item_permissions_check( $request ) { |
|
267 | 267 | |
268 | - if ( ! empty( $request['id'] ) ) { |
|
269 | - return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
270 | - } |
|
268 | + if ( ! empty( $request['id'] ) ) { |
|
269 | + return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
270 | + } |
|
271 | 271 | |
272 | - $post_type = get_post_type_object( $this->post_type ); |
|
272 | + $post_type = get_post_type_object( $this->post_type ); |
|
273 | 273 | |
274 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
275 | - return new WP_Error( |
|
274 | + if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
275 | + return new WP_Error( |
|
276 | 276 | 'rest_cannot_create', |
277 | 277 | __( 'Sorry, you are not allowed to create invoices as this user.', 'invoicing' ), |
278 | 278 | array( |
@@ -281,88 +281,88 @@ discard block |
||
281 | 281 | ); |
282 | 282 | } |
283 | 283 | |
284 | - return true; |
|
284 | + return true; |
|
285 | 285 | } |
286 | 286 | |
287 | 287 | /** |
288 | - * Creates a single invoice. |
|
289 | - * |
|
290 | - * @since 1.0.13 |
|
291 | - * |
|
292 | - * @param WP_REST_Request $request Full details about the request. |
|
293 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
294 | - */ |
|
295 | - public function create_item( $request ) { |
|
296 | - |
|
297 | - if ( ! empty( $request['id'] ) ) { |
|
298 | - return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
299 | - } |
|
288 | + * Creates a single invoice. |
|
289 | + * |
|
290 | + * @since 1.0.13 |
|
291 | + * |
|
292 | + * @param WP_REST_Request $request Full details about the request. |
|
293 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
294 | + */ |
|
295 | + public function create_item( $request ) { |
|
296 | + |
|
297 | + if ( ! empty( $request['id'] ) ) { |
|
298 | + return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
299 | + } |
|
300 | 300 | |
301 | - $request->set_param( 'context', 'edit' ); |
|
301 | + $request->set_param( 'context', 'edit' ); |
|
302 | 302 | |
303 | - // Prepare the updated data. |
|
304 | - $invoice_data = $this->prepare_item_for_database( $request ); |
|
303 | + // Prepare the updated data. |
|
304 | + $invoice_data = $this->prepare_item_for_database( $request ); |
|
305 | 305 | |
306 | - if ( is_wp_error( $invoice_data ) ) { |
|
307 | - return $invoice_data; |
|
308 | - } |
|
306 | + if ( is_wp_error( $invoice_data ) ) { |
|
307 | + return $invoice_data; |
|
308 | + } |
|
309 | 309 | |
310 | - // Try creating the invoice |
|
310 | + // Try creating the invoice |
|
311 | 311 | $invoice = wpinv_insert_invoice( $invoice_data, true ); |
312 | 312 | |
313 | - if ( is_wp_error( $invoice ) ) { |
|
313 | + if ( is_wp_error( $invoice ) ) { |
|
314 | 314 | return $invoice; |
315 | - } |
|
316 | - |
|
317 | - // Prepare the response |
|
318 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
319 | - |
|
320 | - /** |
|
321 | - * Fires after a single invoice is created or updated via the REST API. |
|
322 | - * |
|
323 | - * @since 1.0.13 |
|
324 | - * |
|
325 | - * @param WPinv_Invoice $invoice Inserted or updated invoice object. |
|
326 | - * @param WP_REST_Request $request Request object. |
|
327 | - * @param bool $creating True when creating a post, false when updating. |
|
328 | - */ |
|
329 | - do_action( "wpinv_rest_insert_invoice", $invoice, $request, true ); |
|
330 | - |
|
331 | - /** |
|
332 | - * Filters the responses for creating single invoice requests. |
|
333 | - * |
|
334 | - * |
|
335 | - * @since 1.0.13 |
|
336 | - * |
|
337 | - * |
|
338 | - * @param array $invoice_data Invoice properties. |
|
339 | - * @param WP_REST_Request $request The request used. |
|
340 | - */ |
|
315 | + } |
|
316 | + |
|
317 | + // Prepare the response |
|
318 | + $response = $this->prepare_item_for_response( $invoice, $request ); |
|
319 | + |
|
320 | + /** |
|
321 | + * Fires after a single invoice is created or updated via the REST API. |
|
322 | + * |
|
323 | + * @since 1.0.13 |
|
324 | + * |
|
325 | + * @param WPinv_Invoice $invoice Inserted or updated invoice object. |
|
326 | + * @param WP_REST_Request $request Request object. |
|
327 | + * @param bool $creating True when creating a post, false when updating. |
|
328 | + */ |
|
329 | + do_action( "wpinv_rest_insert_invoice", $invoice, $request, true ); |
|
330 | + |
|
331 | + /** |
|
332 | + * Filters the responses for creating single invoice requests. |
|
333 | + * |
|
334 | + * |
|
335 | + * @since 1.0.13 |
|
336 | + * |
|
337 | + * |
|
338 | + * @param array $invoice_data Invoice properties. |
|
339 | + * @param WP_REST_Request $request The request used. |
|
340 | + */ |
|
341 | 341 | $response = apply_filters( "wpinv_rest_create_invoice_response", $response, $request ); |
342 | 342 | |
343 | 343 | return rest_ensure_response( $response ); |
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Checks if a given request has access to update an invoice. |
|
348 | - * |
|
349 | - * @since 1.0.13 |
|
350 | - * |
|
351 | - * @param WP_REST_Request $request Full details about the request. |
|
352 | - * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
353 | - */ |
|
354 | - public function update_item_permissions_check( $request ) { |
|
355 | - |
|
356 | - // Retrieve the invoice. |
|
357 | - $invoice = $this->get_post( $request['id'] ); |
|
358 | - if ( is_wp_error( $invoice ) ) { |
|
359 | - return $invoice; |
|
360 | - } |
|
361 | - |
|
362 | - $post_type = get_post_type_object( $this->post_type ); |
|
363 | - |
|
364 | - if ( ! current_user_can( $post_type->cap->edit_post, $invoice->ID ) ) { |
|
365 | - return new WP_Error( |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Checks if a given request has access to update an invoice. |
|
348 | + * |
|
349 | + * @since 1.0.13 |
|
350 | + * |
|
351 | + * @param WP_REST_Request $request Full details about the request. |
|
352 | + * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
353 | + */ |
|
354 | + public function update_item_permissions_check( $request ) { |
|
355 | + |
|
356 | + // Retrieve the invoice. |
|
357 | + $invoice = $this->get_post( $request['id'] ); |
|
358 | + if ( is_wp_error( $invoice ) ) { |
|
359 | + return $invoice; |
|
360 | + } |
|
361 | + |
|
362 | + $post_type = get_post_type_object( $this->post_type ); |
|
363 | + |
|
364 | + if ( ! current_user_can( $post_type->cap->edit_post, $invoice->ID ) ) { |
|
365 | + return new WP_Error( |
|
366 | 366 | 'rest_cannot_edit', |
367 | 367 | __( 'Sorry, you are not allowed to update this invoice.', 'invoicing' ), |
368 | 368 | array( |
@@ -371,162 +371,162 @@ discard block |
||
371 | 371 | ); |
372 | 372 | } |
373 | 373 | |
374 | - return true; |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Updates a single invoice. |
|
379 | - * |
|
380 | - * @since 1.0.13 |
|
381 | - * |
|
382 | - * @param WP_REST_Request $request Full details about the request. |
|
383 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
384 | - */ |
|
385 | - public function update_item( $request ) { |
|
374 | + return true; |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Updates a single invoice. |
|
379 | + * |
|
380 | + * @since 1.0.13 |
|
381 | + * |
|
382 | + * @param WP_REST_Request $request Full details about the request. |
|
383 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
384 | + */ |
|
385 | + public function update_item( $request ) { |
|
386 | 386 | |
387 | - // Ensure the invoice exists. |
|
387 | + // Ensure the invoice exists. |
|
388 | 388 | $valid_check = $this->get_post( $request['id'] ); |
389 | 389 | |
390 | 390 | // Abort early if it does not exist |
391 | - if ( is_wp_error( $valid_check ) ) { |
|
392 | - return $valid_check; |
|
393 | - } |
|
391 | + if ( is_wp_error( $valid_check ) ) { |
|
392 | + return $valid_check; |
|
393 | + } |
|
394 | 394 | |
395 | - $request->set_param( 'context', 'edit' ); |
|
395 | + $request->set_param( 'context', 'edit' ); |
|
396 | 396 | |
397 | - // Prepare the updated data. |
|
398 | - $data_to_update = $this->prepare_item_for_database( $request ); |
|
397 | + // Prepare the updated data. |
|
398 | + $data_to_update = $this->prepare_item_for_database( $request ); |
|
399 | 399 | |
400 | - if ( is_wp_error( $data_to_update ) ) { |
|
401 | - return $data_to_update; |
|
402 | - } |
|
400 | + if ( is_wp_error( $data_to_update ) ) { |
|
401 | + return $data_to_update; |
|
402 | + } |
|
403 | 403 | |
404 | - // Abort if no invoice data is provided |
|
404 | + // Abort if no invoice data is provided |
|
405 | 405 | if( empty( $data_to_update ) ) { |
406 | 406 | return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
407 | 407 | } |
408 | 408 | |
409 | - // Include the invoice ID |
|
410 | - $data_to_update['ID'] = $request['id']; |
|
411 | - |
|
412 | - // Update the invoice |
|
413 | - $updated_invoice = wpinv_update_invoice( $data_to_update, true ); |
|
414 | - |
|
415 | - // Incase the update operation failed... |
|
416 | - if ( is_wp_error( $updated_invoice ) ) { |
|
417 | - return $updated_invoice; |
|
418 | - } |
|
419 | - |
|
420 | - // Prepare the response |
|
421 | - $response = $this->prepare_item_for_response( $updated_invoice, $request ); |
|
422 | - |
|
423 | - /** This action is documented in includes/class-wpinv-rest-invoice-controller.php */ |
|
424 | - do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false ); |
|
425 | - |
|
426 | - /** |
|
427 | - * Filters the responses for updating single invoice requests. |
|
428 | - * |
|
429 | - * |
|
430 | - * @since 1.0.13 |
|
431 | - * |
|
432 | - * |
|
433 | - * @param array $invoice_data Invoice properties. |
|
434 | - * @param WP_REST_Request $request The request used. |
|
435 | - */ |
|
409 | + // Include the invoice ID |
|
410 | + $data_to_update['ID'] = $request['id']; |
|
411 | + |
|
412 | + // Update the invoice |
|
413 | + $updated_invoice = wpinv_update_invoice( $data_to_update, true ); |
|
414 | + |
|
415 | + // Incase the update operation failed... |
|
416 | + if ( is_wp_error( $updated_invoice ) ) { |
|
417 | + return $updated_invoice; |
|
418 | + } |
|
419 | + |
|
420 | + // Prepare the response |
|
421 | + $response = $this->prepare_item_for_response( $updated_invoice, $request ); |
|
422 | + |
|
423 | + /** This action is documented in includes/class-wpinv-rest-invoice-controller.php */ |
|
424 | + do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false ); |
|
425 | + |
|
426 | + /** |
|
427 | + * Filters the responses for updating single invoice requests. |
|
428 | + * |
|
429 | + * |
|
430 | + * @since 1.0.13 |
|
431 | + * |
|
432 | + * |
|
433 | + * @param array $invoice_data Invoice properties. |
|
434 | + * @param WP_REST_Request $request The request used. |
|
435 | + */ |
|
436 | 436 | $response = apply_filters( "wpinv_rest_update_invoice_response", $response, $request ); |
437 | 437 | |
438 | 438 | return rest_ensure_response( $response ); |
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Checks if a given request has access to delete an invoice. |
|
443 | - * |
|
444 | - * @since 1.0.13 |
|
445 | - * |
|
446 | - * @param WP_REST_Request $request Full details about the request. |
|
447 | - * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise. |
|
448 | - */ |
|
449 | - public function delete_item_permissions_check( $request ) { |
|
450 | - |
|
451 | - // Retrieve the invoice. |
|
452 | - $invoice = $this->get_post( $request['id'] ); |
|
453 | - if ( is_wp_error( $invoice ) ) { |
|
454 | - return $invoice; |
|
455 | - } |
|
456 | - |
|
457 | - // Ensure the current user can delete invoices |
|
458 | - if ( wpinv_current_user_can_manage_invoicing() || current_user_can( 'delete_invoices', $request['id'] ) ) { |
|
459 | - return new WP_Error( |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Checks if a given request has access to delete an invoice. |
|
443 | + * |
|
444 | + * @since 1.0.13 |
|
445 | + * |
|
446 | + * @param WP_REST_Request $request Full details about the request. |
|
447 | + * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise. |
|
448 | + */ |
|
449 | + public function delete_item_permissions_check( $request ) { |
|
450 | + |
|
451 | + // Retrieve the invoice. |
|
452 | + $invoice = $this->get_post( $request['id'] ); |
|
453 | + if ( is_wp_error( $invoice ) ) { |
|
454 | + return $invoice; |
|
455 | + } |
|
456 | + |
|
457 | + // Ensure the current user can delete invoices |
|
458 | + if ( wpinv_current_user_can_manage_invoicing() || current_user_can( 'delete_invoices', $request['id'] ) ) { |
|
459 | + return new WP_Error( |
|
460 | 460 | 'rest_cannot_delete', |
461 | 461 | __( 'Sorry, you are not allowed to delete this invoice.', 'invoicing' ), |
462 | 462 | array( |
463 | 463 | 'status' => rest_authorization_required_code(), |
464 | 464 | ) |
465 | 465 | ); |
466 | - } |
|
467 | - |
|
468 | - return true; |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Deletes a single invoice. |
|
473 | - * |
|
474 | - * @since 1.0.13 |
|
475 | - * |
|
476 | - * @param WP_REST_Request $request Full details about the request. |
|
477 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
478 | - */ |
|
479 | - public function delete_item( $request ) { |
|
466 | + } |
|
467 | + |
|
468 | + return true; |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Deletes a single invoice. |
|
473 | + * |
|
474 | + * @since 1.0.13 |
|
475 | + * |
|
476 | + * @param WP_REST_Request $request Full details about the request. |
|
477 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
478 | + */ |
|
479 | + public function delete_item( $request ) { |
|
480 | 480 | |
481 | - // Retrieve the invoice. |
|
482 | - $invoice = $this->get_post( $request['id'] ); |
|
483 | - if ( is_wp_error( $invoice ) ) { |
|
484 | - return $invoice; |
|
485 | - } |
|
481 | + // Retrieve the invoice. |
|
482 | + $invoice = $this->get_post( $request['id'] ); |
|
483 | + if ( is_wp_error( $invoice ) ) { |
|
484 | + return $invoice; |
|
485 | + } |
|
486 | 486 | |
487 | - $request->set_param( 'context', 'edit' ); |
|
487 | + $request->set_param( 'context', 'edit' ); |
|
488 | 488 | |
489 | - // Prepare the invoice id |
|
490 | - $id = $invoice->ID; |
|
489 | + // Prepare the invoice id |
|
490 | + $id = $invoice->ID; |
|
491 | 491 | |
492 | - // Prepare the response |
|
493 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
492 | + // Prepare the response |
|
493 | + $response = $this->prepare_item_for_response( $invoice, $request ); |
|
494 | 494 | |
495 | - // Check if the user wants to bypass the trash... |
|
496 | - $force_delete = (bool) $request['force']; |
|
495 | + // Check if the user wants to bypass the trash... |
|
496 | + $force_delete = (bool) $request['force']; |
|
497 | 497 | |
498 | - // Try deleting the invoice. |
|
499 | - $deleted = wp_delete_post( $id, $force_delete ); |
|
498 | + // Try deleting the invoice. |
|
499 | + $deleted = wp_delete_post( $id, $force_delete ); |
|
500 | 500 | |
501 | - // Abort early if we can't delete the invoice. |
|
502 | - if ( ! $deleted ) { |
|
503 | - return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
504 | - } |
|
501 | + // Abort early if we can't delete the invoice. |
|
502 | + if ( ! $deleted ) { |
|
503 | + return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
504 | + } |
|
505 | 505 | |
506 | - /** |
|
507 | - * Fires immediately after a single invoice is deleted or trashed via the REST API. |
|
508 | - * |
|
509 | - * |
|
510 | - * @since 1.0.13 |
|
511 | - * |
|
512 | - * @param WPInv_Invoice $invoice The deleted or trashed invoice. |
|
513 | - * @param WP_REST_Request $request The request sent to the API. |
|
514 | - */ |
|
515 | - do_action( "wpinv_rest_delete_invoice", $invoice, $request ); |
|
506 | + /** |
|
507 | + * Fires immediately after a single invoice is deleted or trashed via the REST API. |
|
508 | + * |
|
509 | + * |
|
510 | + * @since 1.0.13 |
|
511 | + * |
|
512 | + * @param WPInv_Invoice $invoice The deleted or trashed invoice. |
|
513 | + * @param WP_REST_Request $request The request sent to the API. |
|
514 | + */ |
|
515 | + do_action( "wpinv_rest_delete_invoice", $invoice, $request ); |
|
516 | 516 | |
517 | - return $response; |
|
517 | + return $response; |
|
518 | 518 | |
519 | - } |
|
519 | + } |
|
520 | 520 | |
521 | 521 | |
522 | 522 | /** |
523 | - * Retrieves the query params for the invoices collection. |
|
524 | - * |
|
525 | - * @since 1.0.13 |
|
526 | - * |
|
527 | - * @return array Collection parameters. |
|
528 | - */ |
|
529 | - public function get_collection_params() { |
|
523 | + * Retrieves the query params for the invoices collection. |
|
524 | + * |
|
525 | + * @since 1.0.13 |
|
526 | + * |
|
527 | + * @return array Collection parameters. |
|
528 | + */ |
|
529 | + public function get_collection_params() { |
|
530 | 530 | |
531 | 531 | $query_params = array( |
532 | 532 | |
@@ -544,22 +544,22 @@ discard block |
||
544 | 544 | |
545 | 545 | // User. |
546 | 546 | 'user' => array( |
547 | - 'description' => __( 'Limit result set to invoices for a specif user.', 'invoicing' ), |
|
548 | - 'type' => 'integer', |
|
547 | + 'description' => __( 'Limit result set to invoices for a specif user.', 'invoicing' ), |
|
548 | + 'type' => 'integer', |
|
549 | 549 | ), |
550 | 550 | |
551 | 551 | // Number of results per page |
552 | 552 | 'limit' => array( |
553 | - 'description' => __( 'Number of invoices to fetch.', 'invoicing' ), |
|
554 | - 'type' => 'integer', |
|
555 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
553 | + 'description' => __( 'Number of invoices to fetch.', 'invoicing' ), |
|
554 | + 'type' => 'integer', |
|
555 | + 'default' => (int) get_option( 'posts_per_page' ), |
|
556 | 556 | ), |
557 | 557 | |
558 | 558 | // Pagination |
559 | 559 | 'page' => array( |
560 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
561 | - 'type' => 'integer', |
|
562 | - 'default' => 1, |
|
560 | + 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
561 | + 'type' => 'integer', |
|
562 | + 'default' => 1, |
|
563 | 563 | ), |
564 | 564 | |
565 | 565 | // Exclude certain invoices |
@@ -595,789 +595,789 @@ discard block |
||
595 | 595 | ), |
596 | 596 | ); |
597 | 597 | |
598 | - /** |
|
599 | - * Filter collection parameters for the invoices controller. |
|
600 | - * |
|
601 | - * |
|
602 | - * @since 1.0.13 |
|
603 | - * |
|
604 | - * @param array $query_params JSON Schema-formatted collection parameters. |
|
605 | - */ |
|
606 | - return apply_filters( "wpinv_rest_invoices_collection_params", $query_params ); |
|
598 | + /** |
|
599 | + * Filter collection parameters for the invoices controller. |
|
600 | + * |
|
601 | + * |
|
602 | + * @since 1.0.13 |
|
603 | + * |
|
604 | + * @param array $query_params JSON Schema-formatted collection parameters. |
|
605 | + */ |
|
606 | + return apply_filters( "wpinv_rest_invoices_collection_params", $query_params ); |
|
607 | 607 | } |
608 | 608 | |
609 | 609 | /** |
610 | - * Checks if a given post type can be viewed or managed. |
|
611 | - * |
|
612 | - * @since 1.0.13 |
|
613 | - * |
|
614 | - * @param object|string $post_type Post type name or object. |
|
615 | - * @return bool Whether the post type is allowed in REST. |
|
616 | - */ |
|
617 | - protected function check_is_post_type_allowed( $post_type ) { |
|
618 | - return true; |
|
619 | - } |
|
620 | - |
|
621 | - /** |
|
622 | - * Prepares a single invoice for create or update. |
|
623 | - * |
|
624 | - * @since 1.0.13 |
|
625 | - * |
|
626 | - * @param WP_REST_Request $request Request object. |
|
627 | - * @return array|WP_Error Invoice Properties or WP_Error. |
|
628 | - */ |
|
629 | - protected function prepare_item_for_database( $request ) { |
|
630 | - $prepared_invoice = new stdClass(); |
|
631 | - |
|
632 | - // Post ID. |
|
633 | - if ( isset( $request['id'] ) ) { |
|
634 | - $existing_invoice = $this->get_post( $request['id'] ); |
|
635 | - if ( is_wp_error( $existing_invoice ) ) { |
|
636 | - return $existing_invoice; |
|
637 | - } |
|
638 | - |
|
639 | - $prepared_invoice->ID = $existing_invoice->ID; |
|
640 | - $prepared_invoice->invoice_id = $existing_invoice->ID; |
|
641 | - } |
|
642 | - |
|
643 | - $schema = $this->get_item_schema(); |
|
644 | - |
|
645 | - // Invoice owner. |
|
646 | - if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) { |
|
647 | - $prepared_invoice->user_id = (int) $request['user_id']; |
|
648 | - } |
|
649 | - |
|
650 | - // Cart details. |
|
651 | - if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) { |
|
652 | - $prepared_invoice->cart_details = (array) $request['cart_details']; |
|
653 | - } |
|
654 | - |
|
655 | - // Invoice status. |
|
656 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
657 | - |
|
658 | - if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) { |
|
659 | - $prepared_invoice->status = $request['status']; |
|
660 | - } |
|
661 | - |
|
662 | - } |
|
663 | - |
|
664 | - // User info |
|
665 | - if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) { |
|
666 | - $prepared_invoice->user_info = array(); |
|
667 | - $user_info = (array) $request['user_info']; |
|
668 | - |
|
669 | - foreach( $user_info as $prop => $value ) { |
|
670 | - |
|
671 | - if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) { |
|
672 | - |
|
673 | - $prepared_invoice->user_info[$prop] = $value; |
|
610 | + * Checks if a given post type can be viewed or managed. |
|
611 | + * |
|
612 | + * @since 1.0.13 |
|
613 | + * |
|
614 | + * @param object|string $post_type Post type name or object. |
|
615 | + * @return bool Whether the post type is allowed in REST. |
|
616 | + */ |
|
617 | + protected function check_is_post_type_allowed( $post_type ) { |
|
618 | + return true; |
|
619 | + } |
|
620 | + |
|
621 | + /** |
|
622 | + * Prepares a single invoice for create or update. |
|
623 | + * |
|
624 | + * @since 1.0.13 |
|
625 | + * |
|
626 | + * @param WP_REST_Request $request Request object. |
|
627 | + * @return array|WP_Error Invoice Properties or WP_Error. |
|
628 | + */ |
|
629 | + protected function prepare_item_for_database( $request ) { |
|
630 | + $prepared_invoice = new stdClass(); |
|
631 | + |
|
632 | + // Post ID. |
|
633 | + if ( isset( $request['id'] ) ) { |
|
634 | + $existing_invoice = $this->get_post( $request['id'] ); |
|
635 | + if ( is_wp_error( $existing_invoice ) ) { |
|
636 | + return $existing_invoice; |
|
637 | + } |
|
638 | + |
|
639 | + $prepared_invoice->ID = $existing_invoice->ID; |
|
640 | + $prepared_invoice->invoice_id = $existing_invoice->ID; |
|
641 | + } |
|
642 | + |
|
643 | + $schema = $this->get_item_schema(); |
|
644 | + |
|
645 | + // Invoice owner. |
|
646 | + if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) { |
|
647 | + $prepared_invoice->user_id = (int) $request['user_id']; |
|
648 | + } |
|
649 | + |
|
650 | + // Cart details. |
|
651 | + if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) { |
|
652 | + $prepared_invoice->cart_details = (array) $request['cart_details']; |
|
653 | + } |
|
654 | + |
|
655 | + // Invoice status. |
|
656 | + if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
657 | + |
|
658 | + if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) { |
|
659 | + $prepared_invoice->status = $request['status']; |
|
660 | + } |
|
661 | + |
|
662 | + } |
|
663 | + |
|
664 | + // User info |
|
665 | + if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) { |
|
666 | + $prepared_invoice->user_info = array(); |
|
667 | + $user_info = (array) $request['user_info']; |
|
668 | + |
|
669 | + foreach( $user_info as $prop => $value ) { |
|
670 | + |
|
671 | + if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) { |
|
672 | + |
|
673 | + $prepared_invoice->user_info[$prop] = $value; |
|
674 | 674 | |
675 | - } |
|
675 | + } |
|
676 | 676 | |
677 | - } |
|
677 | + } |
|
678 | 678 | |
679 | - } |
|
679 | + } |
|
680 | 680 | |
681 | - // IP |
|
682 | - if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) { |
|
683 | - $prepared_invoice->ip = $request['ip']; |
|
684 | - } |
|
681 | + // IP |
|
682 | + if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) { |
|
683 | + $prepared_invoice->ip = $request['ip']; |
|
684 | + } |
|
685 | 685 | |
686 | - // Payment details |
|
687 | - $prepared_invoice->payment_details = array(); |
|
686 | + // Payment details |
|
687 | + $prepared_invoice->payment_details = array(); |
|
688 | 688 | |
689 | - if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) { |
|
690 | - $prepared_invoice->payment_details['gateway'] = $request['gateway']; |
|
691 | - } |
|
689 | + if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) { |
|
690 | + $prepared_invoice->payment_details['gateway'] = $request['gateway']; |
|
691 | + } |
|
692 | 692 | |
693 | - if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) { |
|
694 | - $prepared_invoice->payment_details['gateway_title'] = $request['gateway_title']; |
|
695 | - } |
|
693 | + if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) { |
|
694 | + $prepared_invoice->payment_details['gateway_title'] = $request['gateway_title']; |
|
695 | + } |
|
696 | 696 | |
697 | - if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) { |
|
698 | - $prepared_invoice->payment_details['currency'] = $request['currency']; |
|
699 | - } |
|
697 | + if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) { |
|
698 | + $prepared_invoice->payment_details['currency'] = $request['currency']; |
|
699 | + } |
|
700 | 700 | |
701 | - if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) { |
|
702 | - $prepared_invoice->payment_details['transaction_id'] = $request['transaction_id']; |
|
703 | - } |
|
701 | + if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) { |
|
702 | + $prepared_invoice->payment_details['transaction_id'] = $request['transaction_id']; |
|
703 | + } |
|
704 | 704 | |
705 | - // Dates |
|
706 | - if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) { |
|
707 | - $post_date = rest_get_date_with_gmt( $request['date'] ); |
|
705 | + // Dates |
|
706 | + if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) { |
|
707 | + $post_date = rest_get_date_with_gmt( $request['date'] ); |
|
708 | 708 | |
709 | - if ( ! empty( $post_date ) ) { |
|
710 | - $prepared_invoice->post_date = $post_date[0]; |
|
711 | - } |
|
709 | + if ( ! empty( $post_date ) ) { |
|
710 | + $prepared_invoice->post_date = $post_date[0]; |
|
711 | + } |
|
712 | 712 | |
713 | - } |
|
713 | + } |
|
714 | 714 | |
715 | - if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) { |
|
716 | - $due_date = rest_get_date_with_gmt( $request['due_date'] ); |
|
715 | + if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) { |
|
716 | + $due_date = rest_get_date_with_gmt( $request['due_date'] ); |
|
717 | 717 | |
718 | - if ( ! empty( $due_date ) ) { |
|
719 | - $prepared_invoice->due_date = $due_date[0]; |
|
720 | - } |
|
718 | + if ( ! empty( $due_date ) ) { |
|
719 | + $prepared_invoice->due_date = $due_date[0]; |
|
720 | + } |
|
721 | 721 | |
722 | - } |
|
722 | + } |
|
723 | 723 | |
724 | - $invoice_data = (array) wp_unslash( $prepared_invoice ); |
|
724 | + $invoice_data = (array) wp_unslash( $prepared_invoice ); |
|
725 | 725 | |
726 | - /** |
|
727 | - * Filters an invoice before it is inserted via the REST API. |
|
728 | - * |
|
729 | - * @since 1.0.13 |
|
730 | - * |
|
731 | - * @param array $invoice_data An array of invoice data |
|
732 | - * @param WP_REST_Request $request Request object. |
|
733 | - */ |
|
734 | - return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request ); |
|
726 | + /** |
|
727 | + * Filters an invoice before it is inserted via the REST API. |
|
728 | + * |
|
729 | + * @since 1.0.13 |
|
730 | + * |
|
731 | + * @param array $invoice_data An array of invoice data |
|
732 | + * @param WP_REST_Request $request Request object. |
|
733 | + */ |
|
734 | + return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request ); |
|
735 | 735 | |
736 | - } |
|
736 | + } |
|
737 | + |
|
738 | + /** |
|
739 | + * Prepares a single invoice output for response. |
|
740 | + * |
|
741 | + * @since 1.0.13 |
|
742 | + * |
|
743 | + * @param WPInv_Invoice $invoice Invoice object. |
|
744 | + * @param WP_REST_Request $request Request object. |
|
745 | + * @return WP_REST_Response Response object. |
|
746 | + */ |
|
747 | + public function prepare_item_for_response( $invoice, $request ) { |
|
737 | 748 | |
738 | - /** |
|
739 | - * Prepares a single invoice output for response. |
|
740 | - * |
|
741 | - * @since 1.0.13 |
|
742 | - * |
|
743 | - * @param WPInv_Invoice $invoice Invoice object. |
|
744 | - * @param WP_REST_Request $request Request object. |
|
745 | - * @return WP_REST_Response Response object. |
|
746 | - */ |
|
747 | - public function prepare_item_for_response( $invoice, $request ) { |
|
749 | + $GLOBALS['post'] = get_post( $invoice->ID ); |
|
748 | 750 | |
749 | - $GLOBALS['post'] = get_post( $invoice->ID ); |
|
751 | + setup_postdata( $invoice->ID ); |
|
750 | 752 | |
751 | - setup_postdata( $invoice->ID ); |
|
753 | + // Fetch the fields to include in this response. |
|
754 | + $fields = $this->get_fields_for_response( $request ); |
|
752 | 755 | |
753 | - // Fetch the fields to include in this response. |
|
754 | - $fields = $this->get_fields_for_response( $request ); |
|
756 | + // Base fields for every invoice. |
|
757 | + $data = array(); |
|
758 | + |
|
759 | + // Set up ID |
|
760 | + if ( rest_is_field_included( 'id', $fields ) ) { |
|
761 | + $data['id'] = $invoice->ID; |
|
762 | + } |
|
763 | + |
|
764 | + |
|
765 | + // Basic properties |
|
766 | + $invoice_properties = array( |
|
767 | + 'title', 'email', 'ip', |
|
768 | + 'key', 'number', 'transaction_id', 'mode', |
|
769 | + 'gateway', 'gateway_title', |
|
770 | + 'total', 'discount', 'discount_code', |
|
771 | + 'tax', 'fees_total', 'subtotal', 'currency', |
|
772 | + 'status', 'status_nicename', 'post_type' |
|
773 | + ); |
|
774 | + |
|
775 | + foreach( $invoice_properties as $property ) { |
|
776 | + |
|
777 | + if ( rest_is_field_included( $property, $fields ) ) { |
|
778 | + $data[$property] = $invoice->get( $property ); |
|
779 | + } |
|
780 | + |
|
781 | + } |
|
782 | + |
|
783 | + // Cart details |
|
784 | + if ( rest_is_field_included( 'cart_details', $fields ) ) { |
|
785 | + $data['cart_details'] = $invoice->get( 'cart_details' ); |
|
786 | + } |
|
787 | + |
|
788 | + //Dates |
|
789 | + $invoice_properties = array( 'date', 'due_date', 'completed_date' ); |
|
790 | + |
|
791 | + foreach( $invoice_properties as $property ) { |
|
792 | + |
|
793 | + if ( rest_is_field_included( $property, $fields ) ) { |
|
794 | + $data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) ); |
|
795 | + } |
|
796 | + |
|
797 | + } |
|
798 | + |
|
799 | + // User id |
|
800 | + if ( rest_is_field_included( 'user_id', $fields ) ) { |
|
801 | + $data['user_id'] = (int) $invoice->get( 'user_id' ); |
|
802 | + } |
|
803 | + |
|
804 | + // User info |
|
805 | + $user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' ); |
|
806 | + |
|
807 | + foreach( $user_info as $property ) { |
|
808 | + |
|
809 | + if ( rest_is_field_included( "user_info.$property", $fields ) ) { |
|
810 | + $data['user_info'][$property] = $invoice->get( $property ); |
|
811 | + } |
|
812 | + |
|
813 | + } |
|
814 | + |
|
815 | + // Slug |
|
816 | + if ( rest_is_field_included( 'slug', $fields ) ) { |
|
817 | + $data['slug'] = $invoice->get( 'post_name' ); |
|
818 | + } |
|
819 | + |
|
820 | + // View invoice link |
|
821 | + if ( rest_is_field_included( 'link', $fields ) ) { |
|
822 | + $data['link'] = esc_url( $invoice->get_view_url() ); |
|
823 | + } |
|
755 | 824 | |
756 | - // Base fields for every invoice. |
|
757 | - $data = array(); |
|
758 | 825 | |
759 | - // Set up ID |
|
760 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
761 | - $data['id'] = $invoice->ID; |
|
762 | - } |
|
826 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
827 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
828 | + $data = $this->filter_response_by_context( $data, $context ); |
|
763 | 829 | |
830 | + // Wrap the data in a response object. |
|
831 | + $response = rest_ensure_response( $data ); |
|
764 | 832 | |
765 | - // Basic properties |
|
766 | - $invoice_properties = array( |
|
767 | - 'title', 'email', 'ip', |
|
768 | - 'key', 'number', 'transaction_id', 'mode', |
|
769 | - 'gateway', 'gateway_title', |
|
770 | - 'total', 'discount', 'discount_code', |
|
771 | - 'tax', 'fees_total', 'subtotal', 'currency', |
|
772 | - 'status', 'status_nicename', 'post_type' |
|
773 | - ); |
|
774 | - |
|
775 | - foreach( $invoice_properties as $property ) { |
|
776 | - |
|
777 | - if ( rest_is_field_included( $property, $fields ) ) { |
|
778 | - $data[$property] = $invoice->get( $property ); |
|
779 | - } |
|
780 | - |
|
781 | - } |
|
782 | - |
|
783 | - // Cart details |
|
784 | - if ( rest_is_field_included( 'cart_details', $fields ) ) { |
|
785 | - $data['cart_details'] = $invoice->get( 'cart_details' ); |
|
786 | - } |
|
787 | - |
|
788 | - //Dates |
|
789 | - $invoice_properties = array( 'date', 'due_date', 'completed_date' ); |
|
790 | - |
|
791 | - foreach( $invoice_properties as $property ) { |
|
792 | - |
|
793 | - if ( rest_is_field_included( $property, $fields ) ) { |
|
794 | - $data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) ); |
|
795 | - } |
|
796 | - |
|
797 | - } |
|
798 | - |
|
799 | - // User id |
|
800 | - if ( rest_is_field_included( 'user_id', $fields ) ) { |
|
801 | - $data['user_id'] = (int) $invoice->get( 'user_id' ); |
|
802 | - } |
|
803 | - |
|
804 | - // User info |
|
805 | - $user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' ); |
|
806 | - |
|
807 | - foreach( $user_info as $property ) { |
|
808 | - |
|
809 | - if ( rest_is_field_included( "user_info.$property", $fields ) ) { |
|
810 | - $data['user_info'][$property] = $invoice->get( $property ); |
|
811 | - } |
|
812 | - |
|
813 | - } |
|
814 | - |
|
815 | - // Slug |
|
816 | - if ( rest_is_field_included( 'slug', $fields ) ) { |
|
817 | - $data['slug'] = $invoice->get( 'post_name' ); |
|
818 | - } |
|
819 | - |
|
820 | - // View invoice link |
|
821 | - if ( rest_is_field_included( 'link', $fields ) ) { |
|
822 | - $data['link'] = esc_url( $invoice->get_view_url() ); |
|
823 | - } |
|
824 | - |
|
825 | - |
|
826 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
827 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
828 | - $data = $this->filter_response_by_context( $data, $context ); |
|
829 | - |
|
830 | - // Wrap the data in a response object. |
|
831 | - $response = rest_ensure_response( $data ); |
|
832 | - |
|
833 | - $links = $this->prepare_links( $invoice ); |
|
834 | - $response->add_links( $links ); |
|
835 | - |
|
836 | - if ( ! empty( $links['self']['href'] ) ) { |
|
837 | - $actions = $this->get_available_actions( $invoice, $request ); |
|
838 | - |
|
839 | - $self = $links['self']['href']; |
|
840 | - |
|
841 | - foreach ( $actions as $rel ) { |
|
842 | - $response->add_link( $rel, $self ); |
|
843 | - } |
|
844 | - } |
|
845 | - |
|
846 | - /** |
|
847 | - * Filters the invoice data for a response. |
|
848 | - * |
|
849 | - * @since 1.0.13 |
|
850 | - * |
|
851 | - * @param WP_REST_Response $response The response object. |
|
852 | - * @param WPInv_Invoice $invoice The invoice object. |
|
853 | - * @param WP_REST_Request $request Request object. |
|
854 | - */ |
|
855 | - return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request ); |
|
856 | - } |
|
857 | - |
|
858 | - /** |
|
859 | - * Gets an array of fields to be included on the response. |
|
860 | - * |
|
861 | - * Included fields are based on item schema and `_fields=` request argument. |
|
862 | - * |
|
863 | - * @since 1.0.13 |
|
864 | - * |
|
865 | - * @param WP_REST_Request $request Full details about the request. |
|
866 | - * @return array Fields to be included in the response. |
|
867 | - */ |
|
868 | - public function get_fields_for_response( $request ) { |
|
869 | - $schema = $this->get_item_schema(); |
|
870 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
871 | - |
|
872 | - $additional_fields = $this->get_additional_fields(); |
|
873 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
874 | - // For back-compat, include any field with an empty schema |
|
875 | - // because it won't be present in $this->get_item_schema(). |
|
876 | - if ( is_null( $field_options['schema'] ) ) { |
|
877 | - $properties[ $field_name ] = $field_options; |
|
878 | - } |
|
879 | - } |
|
880 | - |
|
881 | - // Exclude fields that specify a different context than the request context. |
|
882 | - $context = $request['context']; |
|
883 | - if ( $context ) { |
|
884 | - foreach ( $properties as $name => $options ) { |
|
885 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
886 | - unset( $properties[ $name ] ); |
|
887 | - } |
|
888 | - } |
|
889 | - } |
|
890 | - |
|
891 | - $fields = array_keys( $properties ); |
|
892 | - |
|
893 | - if ( ! isset( $request['_fields'] ) ) { |
|
894 | - return $fields; |
|
895 | - } |
|
896 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
897 | - if ( 0 === count( $requested_fields ) ) { |
|
898 | - return $fields; |
|
899 | - } |
|
900 | - // Trim off outside whitespace from the comma delimited list. |
|
901 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
902 | - // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
903 | - if ( in_array( 'id', $fields, true ) ) { |
|
904 | - $requested_fields[] = 'id'; |
|
905 | - } |
|
906 | - // Return the list of all requested fields which appear in the schema. |
|
907 | - return array_reduce( |
|
908 | - $requested_fields, |
|
909 | - function( $response_fields, $field ) use ( $fields ) { |
|
910 | - if ( in_array( $field, $fields, true ) ) { |
|
911 | - $response_fields[] = $field; |
|
912 | - return $response_fields; |
|
913 | - } |
|
914 | - // Check for nested fields if $field is not a direct match. |
|
915 | - $nested_fields = explode( '.', $field ); |
|
916 | - // A nested field is included so long as its top-level property is |
|
917 | - // present in the schema. |
|
918 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
919 | - $response_fields[] = $field; |
|
920 | - } |
|
921 | - return $response_fields; |
|
922 | - }, |
|
923 | - array() |
|
924 | - ); |
|
925 | - } |
|
926 | - |
|
927 | - /** |
|
928 | - * Retrieves the invoice's schema, conforming to JSON Schema. |
|
929 | - * |
|
930 | - * @since 1.0.13 |
|
931 | - * |
|
932 | - * @return array Invoice schema data. |
|
933 | - */ |
|
934 | - public function get_item_schema() { |
|
935 | - |
|
936 | - // Maybe retrieve the schema from cache. |
|
937 | - if ( $this->schema ) { |
|
938 | - return $this->add_additional_fields_schema( $this->schema ); |
|
939 | - } |
|
940 | - |
|
941 | - $schema = array( |
|
942 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
943 | - 'title' => $this->post_type, |
|
944 | - 'type' => 'object', |
|
945 | - |
|
946 | - // Base properties for every Invoice. |
|
947 | - 'properties' => array( |
|
948 | - |
|
949 | - 'title' => array( |
|
950 | - 'description' => __( 'The title for the invoice.', 'invoicing' ), |
|
951 | - 'type' => 'string', |
|
952 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
953 | - 'readonly' => true, |
|
954 | - ), |
|
955 | - |
|
956 | - 'user_id' => array( |
|
957 | - 'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ), |
|
958 | - 'type' => 'integer', |
|
959 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
960 | - ), |
|
961 | - |
|
962 | - 'email' => array( |
|
963 | - 'description' => __( 'The email of the owner of the invoice.', 'invoicing' ), |
|
964 | - 'type' => 'string', |
|
965 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
966 | - 'readonly' => true, |
|
967 | - ), |
|
968 | - |
|
969 | - 'ip' => array( |
|
970 | - 'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ), |
|
971 | - 'type' => 'string', |
|
972 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
973 | - ), |
|
974 | - |
|
975 | - 'user_info' => array( |
|
976 | - 'description' => __( 'Information about the owner of the invoice.', 'invoicing' ), |
|
977 | - 'type' => 'object', |
|
978 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
979 | - 'properties' => array( |
|
980 | - |
|
981 | - 'first_name' => array( |
|
982 | - 'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ), |
|
983 | - 'type' => 'string', |
|
984 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
985 | - ), |
|
986 | - |
|
987 | - 'last_name' => array( |
|
988 | - 'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ), |
|
989 | - 'type' => 'string', |
|
990 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
991 | - ), |
|
992 | - |
|
993 | - 'company' => array( |
|
994 | - 'description' => __( 'The company of the owner of the invoice.', 'invoicing' ), |
|
995 | - 'type' => 'string', |
|
996 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
997 | - ), |
|
998 | - |
|
999 | - 'vat_number' => array( |
|
1000 | - 'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ), |
|
1001 | - 'type' => 'string', |
|
1002 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1003 | - ), |
|
1004 | - |
|
1005 | - 'vat_rate' => array( |
|
1006 | - 'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ), |
|
1007 | - 'type' => 'string', |
|
1008 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1009 | - ), |
|
1010 | - |
|
1011 | - 'address' => array( |
|
1012 | - 'description' => __( 'The address of the invoice owner.', 'invoicing' ), |
|
1013 | - 'type' => 'string', |
|
1014 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1015 | - ), |
|
1016 | - |
|
1017 | - 'city' => array( |
|
1018 | - 'description' => __( 'The city of the invoice owner.', 'invoicing' ), |
|
1019 | - 'type' => 'string', |
|
1020 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1021 | - ), |
|
1022 | - |
|
1023 | - 'country' => array( |
|
1024 | - 'description' => __( 'The country of the invoice owner.', 'invoicing' ), |
|
1025 | - 'type' => 'string', |
|
1026 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1027 | - ), |
|
1028 | - |
|
1029 | - 'state' => array( |
|
1030 | - 'description' => __( 'The state of the invoice owner.', 'invoicing' ), |
|
1031 | - 'type' => 'string', |
|
1032 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1033 | - ), |
|
1034 | - |
|
1035 | - 'zip' => array( |
|
1036 | - 'description' => __( 'The zip code of the invoice owner.', 'invoicing' ), |
|
1037 | - 'type' => 'string', |
|
1038 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1039 | - ), |
|
1040 | - |
|
1041 | - 'phone' => array( |
|
1042 | - 'description' => __( 'The phone number of the invoice owner.', 'invoicing' ), |
|
1043 | - 'type' => 'string', |
|
1044 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1045 | - ), |
|
1046 | - ), |
|
1047 | - ), |
|
1048 | - |
|
1049 | - 'id' => array( |
|
1050 | - 'description' => __( 'Unique identifier for the invoice.', 'invoicing' ), |
|
1051 | - 'type' => 'integer', |
|
1052 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1053 | - 'readonly' => true, |
|
1054 | - ), |
|
1055 | - |
|
1056 | - 'key' => array( |
|
1057 | - 'description' => __( 'A unique key for the invoice.', 'invoicing' ), |
|
1058 | - 'type' => 'string', |
|
1059 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1060 | - 'readonly' => true, |
|
1061 | - ), |
|
1062 | - |
|
1063 | - 'number' => array( |
|
1064 | - 'description' => __( 'The invoice number.', 'invoicing' ), |
|
1065 | - 'type' => 'string', |
|
1066 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1067 | - 'readonly' => true, |
|
1068 | - ), |
|
1069 | - |
|
1070 | - 'transaction_id' => array( |
|
1071 | - 'description' => __( 'The transaction id of the invoice.', 'invoicing' ), |
|
1072 | - 'type' => 'string', |
|
1073 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1074 | - ), |
|
1075 | - |
|
1076 | - 'gateway' => array( |
|
1077 | - 'description' => __( 'The gateway used to process the invoice.', 'invoicing' ), |
|
1078 | - 'type' => 'string', |
|
1079 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1080 | - ), |
|
1081 | - |
|
1082 | - 'gateway_title' => array( |
|
1083 | - 'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ), |
|
1084 | - 'type' => 'string', |
|
1085 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1086 | - ), |
|
1087 | - |
|
1088 | - 'total' => array( |
|
1089 | - 'description' => __( 'The total amount of the invoice.', 'invoicing' ), |
|
1090 | - 'type' => 'number', |
|
1091 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1092 | - 'readonly' => true, |
|
1093 | - ), |
|
1094 | - |
|
1095 | - 'discount' => array( |
|
1096 | - 'description' => __( 'The discount applied to the invoice.', 'invoicing' ), |
|
1097 | - 'type' => 'number', |
|
1098 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1099 | - 'readonly' => true, |
|
1100 | - ), |
|
1101 | - |
|
1102 | - 'discount_code' => array( |
|
1103 | - 'description' => __( 'The discount code applied to the invoice.', 'invoicing' ), |
|
1104 | - 'type' => 'string', |
|
1105 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1106 | - 'readonly' => true, |
|
1107 | - ), |
|
1108 | - |
|
1109 | - 'tax' => array( |
|
1110 | - 'description' => __( 'The tax applied to the invoice.', 'invoicing' ), |
|
1111 | - 'type' => 'number', |
|
1112 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1113 | - 'readonly' => true, |
|
1114 | - ), |
|
1115 | - |
|
1116 | - 'fees_total' => array( |
|
1117 | - 'description' => __( 'The total fees applied to the invoice.', 'invoicing' ), |
|
1118 | - 'type' => 'number', |
|
1119 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1120 | - 'readonly' => true, |
|
1121 | - ), |
|
1122 | - |
|
1123 | - 'subtotal' => array( |
|
1124 | - 'description' => __( 'The sub-total for the invoice.', 'invoicing' ), |
|
1125 | - 'type' => 'number', |
|
1126 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1127 | - 'readonly' => true, |
|
1128 | - ), |
|
1129 | - |
|
1130 | - 'currency' => array( |
|
1131 | - 'description' => __( 'The currency used to process the invoice.', 'invoicing' ), |
|
1132 | - 'type' => 'string', |
|
1133 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1134 | - ), |
|
1135 | - |
|
1136 | - 'cart_details' => array( |
|
1137 | - 'description' => __( 'The cart details for invoice.', 'invoicing' ), |
|
1138 | - 'type' => 'array', |
|
1139 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1140 | - 'required' => true, |
|
1141 | - ), |
|
1142 | - |
|
1143 | - 'date' => array( |
|
1144 | - 'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ), |
|
1145 | - 'type' => array( 'string', 'null' ), |
|
1146 | - 'format' => 'date-time', |
|
1147 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1148 | - ), |
|
1149 | - |
|
1150 | - 'due_date' => array( |
|
1151 | - 'description' => __( 'The due date for the invoice.', 'invoicing' ), |
|
1152 | - 'type' => array( 'string', 'null' ), |
|
1153 | - 'format' => 'date-time', |
|
1154 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1155 | - ), |
|
1156 | - |
|
1157 | - 'completed_date' => array( |
|
1158 | - 'description' => __( 'The completed date for the invoice.', 'invoicing' ), |
|
1159 | - 'type' => array( 'string', 'null' ), |
|
1160 | - 'format' => 'date-time', |
|
1161 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1162 | - 'readonly' => true, |
|
1163 | - ), |
|
833 | + $links = $this->prepare_links( $invoice ); |
|
834 | + $response->add_links( $links ); |
|
835 | + |
|
836 | + if ( ! empty( $links['self']['href'] ) ) { |
|
837 | + $actions = $this->get_available_actions( $invoice, $request ); |
|
838 | + |
|
839 | + $self = $links['self']['href']; |
|
840 | + |
|
841 | + foreach ( $actions as $rel ) { |
|
842 | + $response->add_link( $rel, $self ); |
|
843 | + } |
|
844 | + } |
|
845 | + |
|
846 | + /** |
|
847 | + * Filters the invoice data for a response. |
|
848 | + * |
|
849 | + * @since 1.0.13 |
|
850 | + * |
|
851 | + * @param WP_REST_Response $response The response object. |
|
852 | + * @param WPInv_Invoice $invoice The invoice object. |
|
853 | + * @param WP_REST_Request $request Request object. |
|
854 | + */ |
|
855 | + return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request ); |
|
856 | + } |
|
857 | + |
|
858 | + /** |
|
859 | + * Gets an array of fields to be included on the response. |
|
860 | + * |
|
861 | + * Included fields are based on item schema and `_fields=` request argument. |
|
862 | + * |
|
863 | + * @since 1.0.13 |
|
864 | + * |
|
865 | + * @param WP_REST_Request $request Full details about the request. |
|
866 | + * @return array Fields to be included in the response. |
|
867 | + */ |
|
868 | + public function get_fields_for_response( $request ) { |
|
869 | + $schema = $this->get_item_schema(); |
|
870 | + $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
871 | + |
|
872 | + $additional_fields = $this->get_additional_fields(); |
|
873 | + foreach ( $additional_fields as $field_name => $field_options ) { |
|
874 | + // For back-compat, include any field with an empty schema |
|
875 | + // because it won't be present in $this->get_item_schema(). |
|
876 | + if ( is_null( $field_options['schema'] ) ) { |
|
877 | + $properties[ $field_name ] = $field_options; |
|
878 | + } |
|
879 | + } |
|
880 | + |
|
881 | + // Exclude fields that specify a different context than the request context. |
|
882 | + $context = $request['context']; |
|
883 | + if ( $context ) { |
|
884 | + foreach ( $properties as $name => $options ) { |
|
885 | + if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
886 | + unset( $properties[ $name ] ); |
|
887 | + } |
|
888 | + } |
|
889 | + } |
|
890 | + |
|
891 | + $fields = array_keys( $properties ); |
|
892 | + |
|
893 | + if ( ! isset( $request['_fields'] ) ) { |
|
894 | + return $fields; |
|
895 | + } |
|
896 | + $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
897 | + if ( 0 === count( $requested_fields ) ) { |
|
898 | + return $fields; |
|
899 | + } |
|
900 | + // Trim off outside whitespace from the comma delimited list. |
|
901 | + $requested_fields = array_map( 'trim', $requested_fields ); |
|
902 | + // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
903 | + if ( in_array( 'id', $fields, true ) ) { |
|
904 | + $requested_fields[] = 'id'; |
|
905 | + } |
|
906 | + // Return the list of all requested fields which appear in the schema. |
|
907 | + return array_reduce( |
|
908 | + $requested_fields, |
|
909 | + function( $response_fields, $field ) use ( $fields ) { |
|
910 | + if ( in_array( $field, $fields, true ) ) { |
|
911 | + $response_fields[] = $field; |
|
912 | + return $response_fields; |
|
913 | + } |
|
914 | + // Check for nested fields if $field is not a direct match. |
|
915 | + $nested_fields = explode( '.', $field ); |
|
916 | + // A nested field is included so long as its top-level property is |
|
917 | + // present in the schema. |
|
918 | + if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
919 | + $response_fields[] = $field; |
|
920 | + } |
|
921 | + return $response_fields; |
|
922 | + }, |
|
923 | + array() |
|
924 | + ); |
|
925 | + } |
|
926 | + |
|
927 | + /** |
|
928 | + * Retrieves the invoice's schema, conforming to JSON Schema. |
|
929 | + * |
|
930 | + * @since 1.0.13 |
|
931 | + * |
|
932 | + * @return array Invoice schema data. |
|
933 | + */ |
|
934 | + public function get_item_schema() { |
|
935 | + |
|
936 | + // Maybe retrieve the schema from cache. |
|
937 | + if ( $this->schema ) { |
|
938 | + return $this->add_additional_fields_schema( $this->schema ); |
|
939 | + } |
|
940 | + |
|
941 | + $schema = array( |
|
942 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
943 | + 'title' => $this->post_type, |
|
944 | + 'type' => 'object', |
|
945 | + |
|
946 | + // Base properties for every Invoice. |
|
947 | + 'properties' => array( |
|
948 | + |
|
949 | + 'title' => array( |
|
950 | + 'description' => __( 'The title for the invoice.', 'invoicing' ), |
|
951 | + 'type' => 'string', |
|
952 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
953 | + 'readonly' => true, |
|
954 | + ), |
|
955 | + |
|
956 | + 'user_id' => array( |
|
957 | + 'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ), |
|
958 | + 'type' => 'integer', |
|
959 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
960 | + ), |
|
961 | + |
|
962 | + 'email' => array( |
|
963 | + 'description' => __( 'The email of the owner of the invoice.', 'invoicing' ), |
|
964 | + 'type' => 'string', |
|
965 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
966 | + 'readonly' => true, |
|
967 | + ), |
|
968 | + |
|
969 | + 'ip' => array( |
|
970 | + 'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ), |
|
971 | + 'type' => 'string', |
|
972 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
973 | + ), |
|
974 | + |
|
975 | + 'user_info' => array( |
|
976 | + 'description' => __( 'Information about the owner of the invoice.', 'invoicing' ), |
|
977 | + 'type' => 'object', |
|
978 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
979 | + 'properties' => array( |
|
980 | + |
|
981 | + 'first_name' => array( |
|
982 | + 'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ), |
|
983 | + 'type' => 'string', |
|
984 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
985 | + ), |
|
986 | + |
|
987 | + 'last_name' => array( |
|
988 | + 'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ), |
|
989 | + 'type' => 'string', |
|
990 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
991 | + ), |
|
992 | + |
|
993 | + 'company' => array( |
|
994 | + 'description' => __( 'The company of the owner of the invoice.', 'invoicing' ), |
|
995 | + 'type' => 'string', |
|
996 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
997 | + ), |
|
998 | + |
|
999 | + 'vat_number' => array( |
|
1000 | + 'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ), |
|
1001 | + 'type' => 'string', |
|
1002 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1003 | + ), |
|
1004 | + |
|
1005 | + 'vat_rate' => array( |
|
1006 | + 'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ), |
|
1007 | + 'type' => 'string', |
|
1008 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1009 | + ), |
|
1010 | + |
|
1011 | + 'address' => array( |
|
1012 | + 'description' => __( 'The address of the invoice owner.', 'invoicing' ), |
|
1013 | + 'type' => 'string', |
|
1014 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1015 | + ), |
|
1016 | + |
|
1017 | + 'city' => array( |
|
1018 | + 'description' => __( 'The city of the invoice owner.', 'invoicing' ), |
|
1019 | + 'type' => 'string', |
|
1020 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1021 | + ), |
|
1022 | + |
|
1023 | + 'country' => array( |
|
1024 | + 'description' => __( 'The country of the invoice owner.', 'invoicing' ), |
|
1025 | + 'type' => 'string', |
|
1026 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1027 | + ), |
|
1028 | + |
|
1029 | + 'state' => array( |
|
1030 | + 'description' => __( 'The state of the invoice owner.', 'invoicing' ), |
|
1031 | + 'type' => 'string', |
|
1032 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1033 | + ), |
|
1034 | + |
|
1035 | + 'zip' => array( |
|
1036 | + 'description' => __( 'The zip code of the invoice owner.', 'invoicing' ), |
|
1037 | + 'type' => 'string', |
|
1038 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1039 | + ), |
|
1040 | + |
|
1041 | + 'phone' => array( |
|
1042 | + 'description' => __( 'The phone number of the invoice owner.', 'invoicing' ), |
|
1043 | + 'type' => 'string', |
|
1044 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1045 | + ), |
|
1046 | + ), |
|
1047 | + ), |
|
1048 | + |
|
1049 | + 'id' => array( |
|
1050 | + 'description' => __( 'Unique identifier for the invoice.', 'invoicing' ), |
|
1051 | + 'type' => 'integer', |
|
1052 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1053 | + 'readonly' => true, |
|
1054 | + ), |
|
1055 | + |
|
1056 | + 'key' => array( |
|
1057 | + 'description' => __( 'A unique key for the invoice.', 'invoicing' ), |
|
1058 | + 'type' => 'string', |
|
1059 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1060 | + 'readonly' => true, |
|
1061 | + ), |
|
1062 | + |
|
1063 | + 'number' => array( |
|
1064 | + 'description' => __( 'The invoice number.', 'invoicing' ), |
|
1065 | + 'type' => 'string', |
|
1066 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1067 | + 'readonly' => true, |
|
1068 | + ), |
|
1069 | + |
|
1070 | + 'transaction_id' => array( |
|
1071 | + 'description' => __( 'The transaction id of the invoice.', 'invoicing' ), |
|
1072 | + 'type' => 'string', |
|
1073 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1074 | + ), |
|
1075 | + |
|
1076 | + 'gateway' => array( |
|
1077 | + 'description' => __( 'The gateway used to process the invoice.', 'invoicing' ), |
|
1078 | + 'type' => 'string', |
|
1079 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1080 | + ), |
|
1081 | + |
|
1082 | + 'gateway_title' => array( |
|
1083 | + 'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ), |
|
1084 | + 'type' => 'string', |
|
1085 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1086 | + ), |
|
1087 | + |
|
1088 | + 'total' => array( |
|
1089 | + 'description' => __( 'The total amount of the invoice.', 'invoicing' ), |
|
1090 | + 'type' => 'number', |
|
1091 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1092 | + 'readonly' => true, |
|
1093 | + ), |
|
1094 | + |
|
1095 | + 'discount' => array( |
|
1096 | + 'description' => __( 'The discount applied to the invoice.', 'invoicing' ), |
|
1097 | + 'type' => 'number', |
|
1098 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1099 | + 'readonly' => true, |
|
1100 | + ), |
|
1101 | + |
|
1102 | + 'discount_code' => array( |
|
1103 | + 'description' => __( 'The discount code applied to the invoice.', 'invoicing' ), |
|
1104 | + 'type' => 'string', |
|
1105 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1106 | + 'readonly' => true, |
|
1107 | + ), |
|
1108 | + |
|
1109 | + 'tax' => array( |
|
1110 | + 'description' => __( 'The tax applied to the invoice.', 'invoicing' ), |
|
1111 | + 'type' => 'number', |
|
1112 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1113 | + 'readonly' => true, |
|
1114 | + ), |
|
1115 | + |
|
1116 | + 'fees_total' => array( |
|
1117 | + 'description' => __( 'The total fees applied to the invoice.', 'invoicing' ), |
|
1118 | + 'type' => 'number', |
|
1119 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1120 | + 'readonly' => true, |
|
1121 | + ), |
|
1122 | + |
|
1123 | + 'subtotal' => array( |
|
1124 | + 'description' => __( 'The sub-total for the invoice.', 'invoicing' ), |
|
1125 | + 'type' => 'number', |
|
1126 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1127 | + 'readonly' => true, |
|
1128 | + ), |
|
1129 | + |
|
1130 | + 'currency' => array( |
|
1131 | + 'description' => __( 'The currency used to process the invoice.', 'invoicing' ), |
|
1132 | + 'type' => 'string', |
|
1133 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1134 | + ), |
|
1135 | + |
|
1136 | + 'cart_details' => array( |
|
1137 | + 'description' => __( 'The cart details for invoice.', 'invoicing' ), |
|
1138 | + 'type' => 'array', |
|
1139 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1140 | + 'required' => true, |
|
1141 | + ), |
|
1142 | + |
|
1143 | + 'date' => array( |
|
1144 | + 'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ), |
|
1145 | + 'type' => array( 'string', 'null' ), |
|
1146 | + 'format' => 'date-time', |
|
1147 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1148 | + ), |
|
1149 | + |
|
1150 | + 'due_date' => array( |
|
1151 | + 'description' => __( 'The due date for the invoice.', 'invoicing' ), |
|
1152 | + 'type' => array( 'string', 'null' ), |
|
1153 | + 'format' => 'date-time', |
|
1154 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1155 | + ), |
|
1156 | + |
|
1157 | + 'completed_date' => array( |
|
1158 | + 'description' => __( 'The completed date for the invoice.', 'invoicing' ), |
|
1159 | + 'type' => array( 'string', 'null' ), |
|
1160 | + 'format' => 'date-time', |
|
1161 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1162 | + 'readonly' => true, |
|
1163 | + ), |
|
1164 | 1164 | |
1165 | - 'link' => array( |
|
1166 | - 'description' => __( 'URL to the invoice.', 'invoicing' ), |
|
1167 | - 'type' => 'string', |
|
1168 | - 'format' => 'uri', |
|
1169 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1170 | - 'readonly' => true, |
|
1171 | - ), |
|
1172 | - |
|
1173 | - 'mode' => array( |
|
1174 | - 'description' => __( 'The mode used to process the invoice.', 'invoicing' ), |
|
1175 | - 'type' => 'string', |
|
1176 | - 'enum' => array( 'live', 'test' ), |
|
1177 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1178 | - 'readonly' => true, |
|
1179 | - ), |
|
1180 | - |
|
1181 | - 'slug' => array( |
|
1182 | - 'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ), |
|
1183 | - 'type' => 'string', |
|
1184 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1185 | - 'arg_options' => array( |
|
1186 | - 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
|
1187 | - ), |
|
1188 | - 'readonly' => true, |
|
1189 | - ), |
|
1190 | - |
|
1191 | - 'status' => array( |
|
1192 | - 'description' => __( 'A named status for the invoice.', 'invoicing' ), |
|
1193 | - 'type' => 'string', |
|
1194 | - 'enum' => array_keys( wpinv_get_invoice_statuses( true, true ) ), |
|
1195 | - 'context' => array( 'view', 'edit' ), |
|
1196 | - 'default' => 'wpi-pending', |
|
1197 | - ), |
|
1198 | - |
|
1199 | - 'status_nicename' => array( |
|
1200 | - 'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ), |
|
1201 | - 'type' => 'string', |
|
1202 | - 'context' => array( 'view', 'embed' ), |
|
1203 | - 'readonly' => true, |
|
1204 | - ), |
|
1205 | - |
|
1206 | - 'post_type' => array( |
|
1207 | - 'description' => __( 'The post type for the invoice.', 'invoicing' ), |
|
1208 | - 'type' => 'string', |
|
1209 | - 'context' => array( 'view' ), |
|
1210 | - 'readonly' => true, |
|
1211 | - ), |
|
1212 | - ), |
|
1213 | - ); |
|
1214 | - |
|
1215 | - // Add helpful links to the invoice schem. |
|
1216 | - $schema['links'] = $this->get_schema_links(); |
|
1217 | - |
|
1218 | - /** |
|
1219 | - * Filters the invoice schema for the REST API. |
|
1220 | - * |
|
1221 | - * Enables adding extra properties to invoices. |
|
1222 | - * |
|
1223 | - * @since 1.0.13 |
|
1224 | - * |
|
1225 | - * @param array $schema The invoice schema. |
|
1226 | - */ |
|
1165 | + 'link' => array( |
|
1166 | + 'description' => __( 'URL to the invoice.', 'invoicing' ), |
|
1167 | + 'type' => 'string', |
|
1168 | + 'format' => 'uri', |
|
1169 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1170 | + 'readonly' => true, |
|
1171 | + ), |
|
1172 | + |
|
1173 | + 'mode' => array( |
|
1174 | + 'description' => __( 'The mode used to process the invoice.', 'invoicing' ), |
|
1175 | + 'type' => 'string', |
|
1176 | + 'enum' => array( 'live', 'test' ), |
|
1177 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1178 | + 'readonly' => true, |
|
1179 | + ), |
|
1180 | + |
|
1181 | + 'slug' => array( |
|
1182 | + 'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ), |
|
1183 | + 'type' => 'string', |
|
1184 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1185 | + 'arg_options' => array( |
|
1186 | + 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
|
1187 | + ), |
|
1188 | + 'readonly' => true, |
|
1189 | + ), |
|
1190 | + |
|
1191 | + 'status' => array( |
|
1192 | + 'description' => __( 'A named status for the invoice.', 'invoicing' ), |
|
1193 | + 'type' => 'string', |
|
1194 | + 'enum' => array_keys( wpinv_get_invoice_statuses( true, true ) ), |
|
1195 | + 'context' => array( 'view', 'edit' ), |
|
1196 | + 'default' => 'wpi-pending', |
|
1197 | + ), |
|
1198 | + |
|
1199 | + 'status_nicename' => array( |
|
1200 | + 'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ), |
|
1201 | + 'type' => 'string', |
|
1202 | + 'context' => array( 'view', 'embed' ), |
|
1203 | + 'readonly' => true, |
|
1204 | + ), |
|
1205 | + |
|
1206 | + 'post_type' => array( |
|
1207 | + 'description' => __( 'The post type for the invoice.', 'invoicing' ), |
|
1208 | + 'type' => 'string', |
|
1209 | + 'context' => array( 'view' ), |
|
1210 | + 'readonly' => true, |
|
1211 | + ), |
|
1212 | + ), |
|
1213 | + ); |
|
1214 | + |
|
1215 | + // Add helpful links to the invoice schem. |
|
1216 | + $schema['links'] = $this->get_schema_links(); |
|
1217 | + |
|
1218 | + /** |
|
1219 | + * Filters the invoice schema for the REST API. |
|
1220 | + * |
|
1221 | + * Enables adding extra properties to invoices. |
|
1222 | + * |
|
1223 | + * @since 1.0.13 |
|
1224 | + * |
|
1225 | + * @param array $schema The invoice schema. |
|
1226 | + */ |
|
1227 | 1227 | $schema = apply_filters( "wpinv_rest_invoice_schema", $schema ); |
1228 | 1228 | |
1229 | - // Cache the invoice schema. |
|
1230 | - $this->schema = $schema; |
|
1229 | + // Cache the invoice schema. |
|
1230 | + $this->schema = $schema; |
|
1231 | 1231 | |
1232 | - return $this->add_additional_fields_schema( $this->schema ); |
|
1233 | - } |
|
1234 | - |
|
1235 | - /** |
|
1236 | - * Retrieve Link Description Objects that should be added to the Schema for the invoices collection. |
|
1237 | - * |
|
1238 | - * @since 1.0.13 |
|
1239 | - * |
|
1240 | - * @return array |
|
1241 | - */ |
|
1242 | - protected function get_schema_links() { |
|
1243 | - |
|
1244 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1245 | - |
|
1246 | - $links = array(); |
|
1247 | - |
|
1248 | - $links[] = array( |
|
1249 | - 'rel' => 'https://api.w.org/action-publish', |
|
1250 | - 'title' => __( 'The current user can mark this invoice as completed.', 'invoicing' ), |
|
1251 | - 'href' => $href, |
|
1252 | - 'targetSchema' => array( |
|
1253 | - 'type' => 'object', |
|
1254 | - 'properties' => array( |
|
1255 | - 'status' => array( |
|
1256 | - 'type' => 'string', |
|
1257 | - 'enum' => array( 'publish', 'wpi-renewal' ), |
|
1258 | - ), |
|
1259 | - ), |
|
1260 | - ), |
|
1261 | - ); |
|
1262 | - |
|
1263 | - $links[] = array( |
|
1264 | - 'rel' => 'https://api.w.org/action-assign-author', |
|
1265 | - 'title' => __( 'The current user can change the owner of this invoice.', 'invoicing' ), |
|
1266 | - 'href' => $href, |
|
1267 | - 'targetSchema' => array( |
|
1268 | - 'type' => 'object', |
|
1269 | - 'properties' => array( |
|
1270 | - 'user_id' => array( |
|
1271 | - 'type' => 'integer', |
|
1272 | - ), |
|
1273 | - ), |
|
1274 | - ), |
|
1275 | - ); |
|
1276 | - |
|
1277 | - return $links; |
|
1278 | - } |
|
1279 | - |
|
1280 | - /** |
|
1281 | - * Prepares links for the request. |
|
1282 | - * |
|
1283 | - * @since 1.0.13 |
|
1284 | - * |
|
1285 | - * @param WPInv_Invoice $invoice Invoice Object. |
|
1286 | - * @return array Links for the given invoice. |
|
1287 | - */ |
|
1288 | - protected function prepare_links( $invoice ) { |
|
1289 | - |
|
1290 | - // Prepare the base REST API endpoint for invoices. |
|
1291 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1292 | - |
|
1293 | - // Entity meta. |
|
1294 | - $links = array( |
|
1295 | - 'self' => array( |
|
1296 | - 'href' => rest_url( trailingslashit( $base ) . $invoice->ID ), |
|
1297 | - ), |
|
1298 | - 'collection' => array( |
|
1299 | - 'href' => rest_url( $base ), |
|
1300 | - ), |
|
1301 | - ); |
|
1302 | - |
|
1303 | - if ( ! empty( $invoice->user_id ) ) { |
|
1304 | - $links['user'] = array( |
|
1305 | - 'href' => rest_url( 'wp/v2/users/' . $invoice->user_id ), |
|
1306 | - 'embeddable' => true, |
|
1307 | - ); |
|
1308 | - } |
|
1309 | - |
|
1310 | - /** |
|
1311 | - * Filters the returned invoice links for the REST API. |
|
1312 | - * |
|
1313 | - * Enables adding extra links to invoice API responses. |
|
1314 | - * |
|
1315 | - * @since 1.0.13 |
|
1316 | - * |
|
1317 | - * @param array $links Rest links. |
|
1318 | - */ |
|
1319 | - return apply_filters( "wpinv_rest_invoice_links", $links ); |
|
1320 | - |
|
1321 | - } |
|
1322 | - |
|
1323 | - /** |
|
1324 | - * Get the link relations available for the post and current user. |
|
1325 | - * |
|
1326 | - * @since 1.0.13 |
|
1327 | - * |
|
1328 | - * @param WPInv_Invoice $invoice Invoice object. |
|
1329 | - * @param WP_REST_Request $request Request object. |
|
1330 | - * @return array List of link relations. |
|
1331 | - */ |
|
1332 | - protected function get_available_actions( $invoice, $request ) { |
|
1333 | - |
|
1334 | - if ( 'edit' !== $request['context'] ) { |
|
1335 | - return array(); |
|
1336 | - } |
|
1337 | - |
|
1338 | - $rels = array(); |
|
1339 | - |
|
1340 | - // Retrieve the post type object. |
|
1341 | - $post_type = get_post_type_object( $invoice->post_type ); |
|
1342 | - |
|
1343 | - // Mark invoice as completed. |
|
1344 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1345 | - $rels[] = 'https://api.w.org/action-publish'; |
|
1346 | - } |
|
1347 | - |
|
1348 | - // Change the owner of the invoice. |
|
1349 | - if ( current_user_can( $post_type->cap->edit_others_posts ) ) { |
|
1350 | - $rels[] = 'https://api.w.org/action-assign-author'; |
|
1351 | - } |
|
1352 | - |
|
1353 | - /** |
|
1354 | - * Filters the available invoice link relations for the REST API. |
|
1355 | - * |
|
1356 | - * Enables adding extra link relation for the current user and request to invoice responses. |
|
1357 | - * |
|
1358 | - * @since 1.0.13 |
|
1359 | - * |
|
1360 | - * @param array $rels Available link relations. |
|
1361 | - */ |
|
1362 | - return apply_filters( "wpinv_rest_invoice_link_relations", $rels ); |
|
1363 | - } |
|
1364 | - |
|
1365 | - /** |
|
1366 | - * Sanitizes and validates the list of post statuses. |
|
1367 | - * |
|
1368 | - * @since 1.0.13 |
|
1369 | - * |
|
1370 | - * @param string|array $statuses One or more post statuses. |
|
1371 | - * @param WP_REST_Request $request Full details about the request. |
|
1372 | - * @param string $parameter Additional parameter to pass to validation. |
|
1373 | - * @return array|WP_Error A list of valid statuses, otherwise WP_Error object. |
|
1374 | - */ |
|
1375 | - public function sanitize_post_statuses( $statuses, $request, $parameter ) { |
|
1376 | - |
|
1377 | - $statuses = wp_parse_slug_list( $statuses ); |
|
1378 | - $valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) ); |
|
1379 | - return array_intersect( $statuses, $valid_statuses ); |
|
1232 | + return $this->add_additional_fields_schema( $this->schema ); |
|
1233 | + } |
|
1234 | + |
|
1235 | + /** |
|
1236 | + * Retrieve Link Description Objects that should be added to the Schema for the invoices collection. |
|
1237 | + * |
|
1238 | + * @since 1.0.13 |
|
1239 | + * |
|
1240 | + * @return array |
|
1241 | + */ |
|
1242 | + protected function get_schema_links() { |
|
1243 | + |
|
1244 | + $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1245 | + |
|
1246 | + $links = array(); |
|
1247 | + |
|
1248 | + $links[] = array( |
|
1249 | + 'rel' => 'https://api.w.org/action-publish', |
|
1250 | + 'title' => __( 'The current user can mark this invoice as completed.', 'invoicing' ), |
|
1251 | + 'href' => $href, |
|
1252 | + 'targetSchema' => array( |
|
1253 | + 'type' => 'object', |
|
1254 | + 'properties' => array( |
|
1255 | + 'status' => array( |
|
1256 | + 'type' => 'string', |
|
1257 | + 'enum' => array( 'publish', 'wpi-renewal' ), |
|
1258 | + ), |
|
1259 | + ), |
|
1260 | + ), |
|
1261 | + ); |
|
1262 | + |
|
1263 | + $links[] = array( |
|
1264 | + 'rel' => 'https://api.w.org/action-assign-author', |
|
1265 | + 'title' => __( 'The current user can change the owner of this invoice.', 'invoicing' ), |
|
1266 | + 'href' => $href, |
|
1267 | + 'targetSchema' => array( |
|
1268 | + 'type' => 'object', |
|
1269 | + 'properties' => array( |
|
1270 | + 'user_id' => array( |
|
1271 | + 'type' => 'integer', |
|
1272 | + ), |
|
1273 | + ), |
|
1274 | + ), |
|
1275 | + ); |
|
1276 | + |
|
1277 | + return $links; |
|
1278 | + } |
|
1279 | + |
|
1280 | + /** |
|
1281 | + * Prepares links for the request. |
|
1282 | + * |
|
1283 | + * @since 1.0.13 |
|
1284 | + * |
|
1285 | + * @param WPInv_Invoice $invoice Invoice Object. |
|
1286 | + * @return array Links for the given invoice. |
|
1287 | + */ |
|
1288 | + protected function prepare_links( $invoice ) { |
|
1289 | + |
|
1290 | + // Prepare the base REST API endpoint for invoices. |
|
1291 | + $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1292 | + |
|
1293 | + // Entity meta. |
|
1294 | + $links = array( |
|
1295 | + 'self' => array( |
|
1296 | + 'href' => rest_url( trailingslashit( $base ) . $invoice->ID ), |
|
1297 | + ), |
|
1298 | + 'collection' => array( |
|
1299 | + 'href' => rest_url( $base ), |
|
1300 | + ), |
|
1301 | + ); |
|
1302 | + |
|
1303 | + if ( ! empty( $invoice->user_id ) ) { |
|
1304 | + $links['user'] = array( |
|
1305 | + 'href' => rest_url( 'wp/v2/users/' . $invoice->user_id ), |
|
1306 | + 'embeddable' => true, |
|
1307 | + ); |
|
1308 | + } |
|
1309 | + |
|
1310 | + /** |
|
1311 | + * Filters the returned invoice links for the REST API. |
|
1312 | + * |
|
1313 | + * Enables adding extra links to invoice API responses. |
|
1314 | + * |
|
1315 | + * @since 1.0.13 |
|
1316 | + * |
|
1317 | + * @param array $links Rest links. |
|
1318 | + */ |
|
1319 | + return apply_filters( "wpinv_rest_invoice_links", $links ); |
|
1320 | + |
|
1321 | + } |
|
1322 | + |
|
1323 | + /** |
|
1324 | + * Get the link relations available for the post and current user. |
|
1325 | + * |
|
1326 | + * @since 1.0.13 |
|
1327 | + * |
|
1328 | + * @param WPInv_Invoice $invoice Invoice object. |
|
1329 | + * @param WP_REST_Request $request Request object. |
|
1330 | + * @return array List of link relations. |
|
1331 | + */ |
|
1332 | + protected function get_available_actions( $invoice, $request ) { |
|
1333 | + |
|
1334 | + if ( 'edit' !== $request['context'] ) { |
|
1335 | + return array(); |
|
1336 | + } |
|
1337 | + |
|
1338 | + $rels = array(); |
|
1339 | + |
|
1340 | + // Retrieve the post type object. |
|
1341 | + $post_type = get_post_type_object( $invoice->post_type ); |
|
1342 | + |
|
1343 | + // Mark invoice as completed. |
|
1344 | + if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1345 | + $rels[] = 'https://api.w.org/action-publish'; |
|
1346 | + } |
|
1347 | + |
|
1348 | + // Change the owner of the invoice. |
|
1349 | + if ( current_user_can( $post_type->cap->edit_others_posts ) ) { |
|
1350 | + $rels[] = 'https://api.w.org/action-assign-author'; |
|
1351 | + } |
|
1352 | + |
|
1353 | + /** |
|
1354 | + * Filters the available invoice link relations for the REST API. |
|
1355 | + * |
|
1356 | + * Enables adding extra link relation for the current user and request to invoice responses. |
|
1357 | + * |
|
1358 | + * @since 1.0.13 |
|
1359 | + * |
|
1360 | + * @param array $rels Available link relations. |
|
1361 | + */ |
|
1362 | + return apply_filters( "wpinv_rest_invoice_link_relations", $rels ); |
|
1363 | + } |
|
1364 | + |
|
1365 | + /** |
|
1366 | + * Sanitizes and validates the list of post statuses. |
|
1367 | + * |
|
1368 | + * @since 1.0.13 |
|
1369 | + * |
|
1370 | + * @param string|array $statuses One or more post statuses. |
|
1371 | + * @param WP_REST_Request $request Full details about the request. |
|
1372 | + * @param string $parameter Additional parameter to pass to validation. |
|
1373 | + * @return array|WP_Error A list of valid statuses, otherwise WP_Error object. |
|
1374 | + */ |
|
1375 | + public function sanitize_post_statuses( $statuses, $request, $parameter ) { |
|
1376 | + |
|
1377 | + $statuses = wp_parse_slug_list( $statuses ); |
|
1378 | + $valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) ); |
|
1379 | + return array_intersect( $statuses, $valid_statuses ); |
|
1380 | 1380 | |
1381 | - } |
|
1381 | + } |
|
1382 | 1382 | |
1383 | 1383 | } |
1384 | 1384 | \ No newline at end of file |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @since 1.0.13 |
9 | 9 | */ |
10 | 10 | |
11 | -if ( !defined( 'WPINC' ) ) { |
|
11 | +if (!defined('WPINC')) { |
|
12 | 12 | exit; |
13 | 13 | } |
14 | 14 | |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @param string $namespace Api Namespace |
43 | 43 | */ |
44 | - public function __construct( $namespace ) { |
|
44 | + public function __construct($namespace) { |
|
45 | 45 | |
46 | 46 | // Set api namespace... |
47 | 47 | $this->namespace = $namespace; |
@@ -60,12 +60,12 @@ discard block |
||
60 | 60 | * @param WP_REST_Request $request Full details about the request. |
61 | 61 | * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
62 | 62 | */ |
63 | - public function get_items_permissions_check( $request ) { |
|
63 | + public function get_items_permissions_check($request) { |
|
64 | 64 | |
65 | - $post_type = get_post_type_object( $this->post_type ); |
|
65 | + $post_type = get_post_type_object($this->post_type); |
|
66 | 66 | |
67 | - if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) { |
|
68 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
67 | + if ('edit' === $request['context'] && !current_user_can($post_type->cap->edit_posts)) { |
|
68 | + return new WP_Error('rest_forbidden_context', __('Sorry, you are not allowed to edit invoices.', 'invoicing'), array('status' => rest_authorization_required_code())); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | // Read checks will be evaluated on a per invoice basis |
@@ -82,17 +82,17 @@ discard block |
||
82 | 82 | * @param WP_REST_Request $request Full details about the request. |
83 | 83 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
84 | 84 | */ |
85 | - public function get_items( $request ) { |
|
85 | + public function get_items($request) { |
|
86 | 86 | |
87 | 87 | // Retrieve the list of registered invoice query parameters. |
88 | 88 | $registered = $this->get_collection_params(); |
89 | 89 | |
90 | 90 | $args = array(); |
91 | 91 | |
92 | - foreach( array_keys( $registered ) as $key ) { |
|
92 | + foreach (array_keys($registered) as $key) { |
|
93 | 93 | |
94 | - if( isset( $request[ $key] ) ) { |
|
95 | - $args[ $key ] = $request[ $key]; |
|
94 | + if (isset($request[$key])) { |
|
95 | + $args[$key] = $request[$key]; |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | } |
@@ -107,32 +107,32 @@ discard block |
||
107 | 107 | * @param array $args Key value array of query var to query value. |
108 | 108 | * @param WP_REST_Request $request The request used. |
109 | 109 | */ |
110 | - $args = apply_filters( "wpinv_rest_get_invoices_arguments", $args, $request, $this ); |
|
110 | + $args = apply_filters("wpinv_rest_get_invoices_arguments", $args, $request, $this); |
|
111 | 111 | |
112 | 112 | // Special args |
113 | - $args[ 'return' ] = 'objects'; |
|
114 | - $args[ 'paginate' ] = true; |
|
113 | + $args['return'] = 'objects'; |
|
114 | + $args['paginate'] = true; |
|
115 | 115 | |
116 | 116 | // Run the query. |
117 | - $query = wpinv_get_invoices( $args ); |
|
117 | + $query = wpinv_get_invoices($args); |
|
118 | 118 | |
119 | 119 | // Prepare the retrieved invoices |
120 | 120 | $invoices = array(); |
121 | - foreach( $query->invoices as $invoice ) { |
|
121 | + foreach ($query->invoices as $invoice) { |
|
122 | 122 | |
123 | - if ( ! $this->check_read_permission( $invoice ) ) { |
|
123 | + if (!$this->check_read_permission($invoice)) { |
|
124 | 124 | continue; |
125 | 125 | } |
126 | 126 | |
127 | - $data = $this->prepare_item_for_response( $invoice, $request ); |
|
128 | - $invoices[] = $this->prepare_response_for_collection( $data ); |
|
127 | + $data = $this->prepare_item_for_response($invoice, $request); |
|
128 | + $invoices[] = $this->prepare_response_for_collection($data); |
|
129 | 129 | |
130 | 130 | } |
131 | 131 | |
132 | 132 | // Prepare the response. |
133 | - $response = rest_ensure_response( $invoices ); |
|
134 | - $response->header( 'X-WP-Total', (int) $query->total ); |
|
135 | - $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
133 | + $response = rest_ensure_response($invoices); |
|
134 | + $response->header('X-WP-Total', (int) $query->total); |
|
135 | + $response->header('X-WP-TotalPages', (int) $query->max_num_pages); |
|
136 | 136 | |
137 | 137 | /** |
138 | 138 | * Filters the responses for invoices requests. |
@@ -145,9 +145,9 @@ discard block |
||
145 | 145 | * @param WP_REST_Request $request The request used. |
146 | 146 | * @param array $args Array of args used to retrieve the invoices |
147 | 147 | */ |
148 | - $response = apply_filters( "wpinv_rest_invoices_response", $response, $request, $args ); |
|
148 | + $response = apply_filters("wpinv_rest_invoices_response", $response, $request, $args); |
|
149 | 149 | |
150 | - return rest_ensure_response( $response ); |
|
150 | + return rest_ensure_response($response); |
|
151 | 151 | |
152 | 152 | } |
153 | 153 | |
@@ -159,17 +159,17 @@ discard block |
||
159 | 159 | * @param int $invoice_id Supplied ID. |
160 | 160 | * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise. |
161 | 161 | */ |
162 | - protected function get_post( $invoice_id ) { |
|
162 | + protected function get_post($invoice_id) { |
|
163 | 163 | |
164 | - $error = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
164 | + $error = new WP_Error('rest_invoice_invalid_id', __('Invalid invoice ID.', 'invoicing'), array('status' => 404)); |
|
165 | 165 | |
166 | 166 | // Ids start from 1 |
167 | - if ( (int) $invoice_id <= 0 ) { |
|
167 | + if ((int) $invoice_id <= 0) { |
|
168 | 168 | return $error; |
169 | 169 | } |
170 | 170 | |
171 | - $invoice = wpinv_get_invoice( (int) $invoice_id ); |
|
172 | - if ( empty( $invoice ) ) { |
|
171 | + $invoice = wpinv_get_invoice((int) $invoice_id); |
|
172 | + if (empty($invoice)) { |
|
173 | 173 | return $error; |
174 | 174 | } |
175 | 175 | |
@@ -185,18 +185,18 @@ discard block |
||
185 | 185 | * @param WP_REST_Request $request Full details about the request. |
186 | 186 | * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise. |
187 | 187 | */ |
188 | - public function get_item_permissions_check( $request ) { |
|
188 | + public function get_item_permissions_check($request) { |
|
189 | 189 | |
190 | 190 | // Retrieve the invoice object. |
191 | - $invoice = $this->get_post( $request['id'] ); |
|
191 | + $invoice = $this->get_post($request['id']); |
|
192 | 192 | |
193 | 193 | // Ensure it is valid. |
194 | - if ( is_wp_error( $invoice ) ) { |
|
194 | + if (is_wp_error($invoice)) { |
|
195 | 195 | return $invoice; |
196 | 196 | } |
197 | 197 | |
198 | - if ( $invoice ) { |
|
199 | - return $this->check_read_permission( $invoice ); |
|
198 | + if ($invoice) { |
|
199 | + return $this->check_read_permission($invoice); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | return true; |
@@ -213,8 +213,8 @@ discard block |
||
213 | 213 | * @param WPInv_Invoice $invoice WPInv_Invoice object. |
214 | 214 | * @return bool Whether the post can be read. |
215 | 215 | */ |
216 | - public function check_read_permission( $invoice ) { |
|
217 | - return wpinv_user_can_view_invoice( $invoice->ID ); |
|
216 | + public function check_read_permission($invoice) { |
|
217 | + return wpinv_user_can_view_invoice($invoice->ID); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -225,19 +225,19 @@ discard block |
||
225 | 225 | * @param WP_REST_Request $request Full details about the request. |
226 | 226 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
227 | 227 | */ |
228 | - public function get_item( $request ) { |
|
228 | + public function get_item($request) { |
|
229 | 229 | |
230 | 230 | // Fetch the invoice. |
231 | - $invoice = $this->get_post( $request['id'] ); |
|
231 | + $invoice = $this->get_post($request['id']); |
|
232 | 232 | |
233 | 233 | // Abort early if it does not exist |
234 | - if ( is_wp_error( $invoice ) ) { |
|
234 | + if (is_wp_error($invoice)) { |
|
235 | 235 | return $invoice; |
236 | 236 | } |
237 | 237 | |
238 | 238 | // Prepare the response |
239 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
240 | - $response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) ); |
|
239 | + $response = $this->prepare_item_for_response($invoice, $request); |
|
240 | + $response->link_header('alternate', esc_url($invoice->get_view_url()), array('type' => 'text/html')); |
|
241 | 241 | |
242 | 242 | /** |
243 | 243 | * Filters the responses for single invoice requests. |
@@ -249,9 +249,9 @@ discard block |
||
249 | 249 | * @param WP_HTTP_Response $response Response. |
250 | 250 | * @param WP_REST_Request $request The request used. |
251 | 251 | */ |
252 | - $response = apply_filters( "wpinv_rest_get_invoice_response", $response, $request ); |
|
252 | + $response = apply_filters("wpinv_rest_get_invoice_response", $response, $request); |
|
253 | 253 | |
254 | - return rest_ensure_response( $response ); |
|
254 | + return rest_ensure_response($response); |
|
255 | 255 | |
256 | 256 | } |
257 | 257 | |
@@ -263,18 +263,18 @@ discard block |
||
263 | 263 | * @param WP_REST_Request $request Full details about the request. |
264 | 264 | * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
265 | 265 | */ |
266 | - public function create_item_permissions_check( $request ) { |
|
266 | + public function create_item_permissions_check($request) { |
|
267 | 267 | |
268 | - if ( ! empty( $request['id'] ) ) { |
|
269 | - return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
268 | + if (!empty($request['id'])) { |
|
269 | + return new WP_Error('rest_invoice_exists', __('Cannot create existing invoice.', 'invoicing'), array('status' => 400)); |
|
270 | 270 | } |
271 | 271 | |
272 | - $post_type = get_post_type_object( $this->post_type ); |
|
272 | + $post_type = get_post_type_object($this->post_type); |
|
273 | 273 | |
274 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
274 | + if (!current_user_can($post_type->cap->create_posts)) { |
|
275 | 275 | return new WP_Error( |
276 | 276 | 'rest_cannot_create', |
277 | - __( 'Sorry, you are not allowed to create invoices as this user.', 'invoicing' ), |
|
277 | + __('Sorry, you are not allowed to create invoices as this user.', 'invoicing'), |
|
278 | 278 | array( |
279 | 279 | 'status' => rest_authorization_required_code(), |
280 | 280 | ) |
@@ -292,30 +292,30 @@ discard block |
||
292 | 292 | * @param WP_REST_Request $request Full details about the request. |
293 | 293 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
294 | 294 | */ |
295 | - public function create_item( $request ) { |
|
295 | + public function create_item($request) { |
|
296 | 296 | |
297 | - if ( ! empty( $request['id'] ) ) { |
|
298 | - return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) ); |
|
297 | + if (!empty($request['id'])) { |
|
298 | + return new WP_Error('rest_invoice_exists', __('Cannot create existing invoice.', 'invoicing'), array('status' => 400)); |
|
299 | 299 | } |
300 | 300 | |
301 | - $request->set_param( 'context', 'edit' ); |
|
301 | + $request->set_param('context', 'edit'); |
|
302 | 302 | |
303 | 303 | // Prepare the updated data. |
304 | - $invoice_data = $this->prepare_item_for_database( $request ); |
|
304 | + $invoice_data = $this->prepare_item_for_database($request); |
|
305 | 305 | |
306 | - if ( is_wp_error( $invoice_data ) ) { |
|
306 | + if (is_wp_error($invoice_data)) { |
|
307 | 307 | return $invoice_data; |
308 | 308 | } |
309 | 309 | |
310 | 310 | // Try creating the invoice |
311 | - $invoice = wpinv_insert_invoice( $invoice_data, true ); |
|
311 | + $invoice = wpinv_insert_invoice($invoice_data, true); |
|
312 | 312 | |
313 | - if ( is_wp_error( $invoice ) ) { |
|
313 | + if (is_wp_error($invoice)) { |
|
314 | 314 | return $invoice; |
315 | 315 | } |
316 | 316 | |
317 | 317 | // Prepare the response |
318 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
318 | + $response = $this->prepare_item_for_response($invoice, $request); |
|
319 | 319 | |
320 | 320 | /** |
321 | 321 | * Fires after a single invoice is created or updated via the REST API. |
@@ -326,7 +326,7 @@ discard block |
||
326 | 326 | * @param WP_REST_Request $request Request object. |
327 | 327 | * @param bool $creating True when creating a post, false when updating. |
328 | 328 | */ |
329 | - do_action( "wpinv_rest_insert_invoice", $invoice, $request, true ); |
|
329 | + do_action("wpinv_rest_insert_invoice", $invoice, $request, true); |
|
330 | 330 | |
331 | 331 | /** |
332 | 332 | * Filters the responses for creating single invoice requests. |
@@ -338,9 +338,9 @@ discard block |
||
338 | 338 | * @param array $invoice_data Invoice properties. |
339 | 339 | * @param WP_REST_Request $request The request used. |
340 | 340 | */ |
341 | - $response = apply_filters( "wpinv_rest_create_invoice_response", $response, $request ); |
|
341 | + $response = apply_filters("wpinv_rest_create_invoice_response", $response, $request); |
|
342 | 342 | |
343 | - return rest_ensure_response( $response ); |
|
343 | + return rest_ensure_response($response); |
|
344 | 344 | } |
345 | 345 | |
346 | 346 | /** |
@@ -351,20 +351,20 @@ discard block |
||
351 | 351 | * @param WP_REST_Request $request Full details about the request. |
352 | 352 | * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
353 | 353 | */ |
354 | - public function update_item_permissions_check( $request ) { |
|
354 | + public function update_item_permissions_check($request) { |
|
355 | 355 | |
356 | 356 | // Retrieve the invoice. |
357 | - $invoice = $this->get_post( $request['id'] ); |
|
358 | - if ( is_wp_error( $invoice ) ) { |
|
357 | + $invoice = $this->get_post($request['id']); |
|
358 | + if (is_wp_error($invoice)) { |
|
359 | 359 | return $invoice; |
360 | 360 | } |
361 | 361 | |
362 | - $post_type = get_post_type_object( $this->post_type ); |
|
362 | + $post_type = get_post_type_object($this->post_type); |
|
363 | 363 | |
364 | - if ( ! current_user_can( $post_type->cap->edit_post, $invoice->ID ) ) { |
|
364 | + if (!current_user_can($post_type->cap->edit_post, $invoice->ID)) { |
|
365 | 365 | return new WP_Error( |
366 | 366 | 'rest_cannot_edit', |
367 | - __( 'Sorry, you are not allowed to update this invoice.', 'invoicing' ), |
|
367 | + __('Sorry, you are not allowed to update this invoice.', 'invoicing'), |
|
368 | 368 | array( |
369 | 369 | 'status' => rest_authorization_required_code(), |
370 | 370 | ) |
@@ -382,46 +382,46 @@ discard block |
||
382 | 382 | * @param WP_REST_Request $request Full details about the request. |
383 | 383 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
384 | 384 | */ |
385 | - public function update_item( $request ) { |
|
385 | + public function update_item($request) { |
|
386 | 386 | |
387 | 387 | // Ensure the invoice exists. |
388 | - $valid_check = $this->get_post( $request['id'] ); |
|
388 | + $valid_check = $this->get_post($request['id']); |
|
389 | 389 | |
390 | 390 | // Abort early if it does not exist |
391 | - if ( is_wp_error( $valid_check ) ) { |
|
391 | + if (is_wp_error($valid_check)) { |
|
392 | 392 | return $valid_check; |
393 | 393 | } |
394 | 394 | |
395 | - $request->set_param( 'context', 'edit' ); |
|
395 | + $request->set_param('context', 'edit'); |
|
396 | 396 | |
397 | 397 | // Prepare the updated data. |
398 | - $data_to_update = $this->prepare_item_for_database( $request ); |
|
398 | + $data_to_update = $this->prepare_item_for_database($request); |
|
399 | 399 | |
400 | - if ( is_wp_error( $data_to_update ) ) { |
|
400 | + if (is_wp_error($data_to_update)) { |
|
401 | 401 | return $data_to_update; |
402 | 402 | } |
403 | 403 | |
404 | 404 | // Abort if no invoice data is provided |
405 | - if( empty( $data_to_update ) ) { |
|
406 | - return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
|
405 | + if (empty($data_to_update)) { |
|
406 | + return new WP_Error('missing_data', __('An update request cannot be empty.', 'invoicing')); |
|
407 | 407 | } |
408 | 408 | |
409 | 409 | // Include the invoice ID |
410 | 410 | $data_to_update['ID'] = $request['id']; |
411 | 411 | |
412 | 412 | // Update the invoice |
413 | - $updated_invoice = wpinv_update_invoice( $data_to_update, true ); |
|
413 | + $updated_invoice = wpinv_update_invoice($data_to_update, true); |
|
414 | 414 | |
415 | 415 | // Incase the update operation failed... |
416 | - if ( is_wp_error( $updated_invoice ) ) { |
|
416 | + if (is_wp_error($updated_invoice)) { |
|
417 | 417 | return $updated_invoice; |
418 | 418 | } |
419 | 419 | |
420 | 420 | // Prepare the response |
421 | - $response = $this->prepare_item_for_response( $updated_invoice, $request ); |
|
421 | + $response = $this->prepare_item_for_response($updated_invoice, $request); |
|
422 | 422 | |
423 | 423 | /** This action is documented in includes/class-wpinv-rest-invoice-controller.php */ |
424 | - do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false ); |
|
424 | + do_action("wpinv_rest_insert_invoice", $updated_invoice, $request, false); |
|
425 | 425 | |
426 | 426 | /** |
427 | 427 | * Filters the responses for updating single invoice requests. |
@@ -433,9 +433,9 @@ discard block |
||
433 | 433 | * @param array $invoice_data Invoice properties. |
434 | 434 | * @param WP_REST_Request $request The request used. |
435 | 435 | */ |
436 | - $response = apply_filters( "wpinv_rest_update_invoice_response", $response, $request ); |
|
436 | + $response = apply_filters("wpinv_rest_update_invoice_response", $response, $request); |
|
437 | 437 | |
438 | - return rest_ensure_response( $response ); |
|
438 | + return rest_ensure_response($response); |
|
439 | 439 | } |
440 | 440 | |
441 | 441 | /** |
@@ -446,19 +446,19 @@ discard block |
||
446 | 446 | * @param WP_REST_Request $request Full details about the request. |
447 | 447 | * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise. |
448 | 448 | */ |
449 | - public function delete_item_permissions_check( $request ) { |
|
449 | + public function delete_item_permissions_check($request) { |
|
450 | 450 | |
451 | 451 | // Retrieve the invoice. |
452 | - $invoice = $this->get_post( $request['id'] ); |
|
453 | - if ( is_wp_error( $invoice ) ) { |
|
452 | + $invoice = $this->get_post($request['id']); |
|
453 | + if (is_wp_error($invoice)) { |
|
454 | 454 | return $invoice; |
455 | 455 | } |
456 | 456 | |
457 | 457 | // Ensure the current user can delete invoices |
458 | - if ( wpinv_current_user_can_manage_invoicing() || current_user_can( 'delete_invoices', $request['id'] ) ) { |
|
458 | + if (wpinv_current_user_can_manage_invoicing() || current_user_can('delete_invoices', $request['id'])) { |
|
459 | 459 | return new WP_Error( |
460 | 460 | 'rest_cannot_delete', |
461 | - __( 'Sorry, you are not allowed to delete this invoice.', 'invoicing' ), |
|
461 | + __('Sorry, you are not allowed to delete this invoice.', 'invoicing'), |
|
462 | 462 | array( |
463 | 463 | 'status' => rest_authorization_required_code(), |
464 | 464 | ) |
@@ -476,31 +476,31 @@ discard block |
||
476 | 476 | * @param WP_REST_Request $request Full details about the request. |
477 | 477 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
478 | 478 | */ |
479 | - public function delete_item( $request ) { |
|
479 | + public function delete_item($request) { |
|
480 | 480 | |
481 | 481 | // Retrieve the invoice. |
482 | - $invoice = $this->get_post( $request['id'] ); |
|
483 | - if ( is_wp_error( $invoice ) ) { |
|
482 | + $invoice = $this->get_post($request['id']); |
|
483 | + if (is_wp_error($invoice)) { |
|
484 | 484 | return $invoice; |
485 | 485 | } |
486 | 486 | |
487 | - $request->set_param( 'context', 'edit' ); |
|
487 | + $request->set_param('context', 'edit'); |
|
488 | 488 | |
489 | 489 | // Prepare the invoice id |
490 | - $id = $invoice->ID; |
|
490 | + $id = $invoice->ID; |
|
491 | 491 | |
492 | 492 | // Prepare the response |
493 | - $response = $this->prepare_item_for_response( $invoice, $request ); |
|
493 | + $response = $this->prepare_item_for_response($invoice, $request); |
|
494 | 494 | |
495 | 495 | // Check if the user wants to bypass the trash... |
496 | 496 | $force_delete = (bool) $request['force']; |
497 | 497 | |
498 | 498 | // Try deleting the invoice. |
499 | - $deleted = wp_delete_post( $id, $force_delete ); |
|
499 | + $deleted = wp_delete_post($id, $force_delete); |
|
500 | 500 | |
501 | 501 | // Abort early if we can't delete the invoice. |
502 | - if ( ! $deleted ) { |
|
503 | - return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
502 | + if (!$deleted) { |
|
503 | + return new WP_Error('rest_cannot_delete', __('The invoice cannot be deleted.', 'invoicing'), array('status' => 500)); |
|
504 | 504 | } |
505 | 505 | |
506 | 506 | /** |
@@ -512,7 +512,7 @@ discard block |
||
512 | 512 | * @param WPInv_Invoice $invoice The deleted or trashed invoice. |
513 | 513 | * @param WP_REST_Request $request The request sent to the API. |
514 | 514 | */ |
515 | - do_action( "wpinv_rest_delete_invoice", $invoice, $request ); |
|
515 | + do_action("wpinv_rest_delete_invoice", $invoice, $request); |
|
516 | 516 | |
517 | 517 | return $response; |
518 | 518 | |
@@ -528,43 +528,43 @@ discard block |
||
528 | 528 | */ |
529 | 529 | public function get_collection_params() { |
530 | 530 | |
531 | - $query_params = array( |
|
531 | + $query_params = array( |
|
532 | 532 | |
533 | 533 | // Invoice status. |
534 | 534 | 'status' => array( |
535 | 535 | 'default' => 'publish', |
536 | - 'description' => __( 'Limit result set to invoices assigned one or more statuses.', 'invoicing' ), |
|
536 | + 'description' => __('Limit result set to invoices assigned one or more statuses.', 'invoicing'), |
|
537 | 537 | 'type' => 'array', |
538 | 538 | 'items' => array( |
539 | - 'enum' => array_keys( wpinv_get_invoice_statuses( true, true ) ), |
|
539 | + 'enum' => array_keys(wpinv_get_invoice_statuses(true, true)), |
|
540 | 540 | 'type' => 'string', |
541 | 541 | ), |
542 | - 'sanitize_callback' => array( $this, 'sanitize_post_statuses' ), |
|
542 | + 'sanitize_callback' => array($this, 'sanitize_post_statuses'), |
|
543 | 543 | ), |
544 | 544 | |
545 | 545 | // User. |
546 | 546 | 'user' => array( |
547 | - 'description' => __( 'Limit result set to invoices for a specif user.', 'invoicing' ), |
|
547 | + 'description' => __('Limit result set to invoices for a specif user.', 'invoicing'), |
|
548 | 548 | 'type' => 'integer', |
549 | 549 | ), |
550 | 550 | |
551 | 551 | // Number of results per page |
552 | 552 | 'limit' => array( |
553 | - 'description' => __( 'Number of invoices to fetch.', 'invoicing' ), |
|
553 | + 'description' => __('Number of invoices to fetch.', 'invoicing'), |
|
554 | 554 | 'type' => 'integer', |
555 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
555 | + 'default' => (int) get_option('posts_per_page'), |
|
556 | 556 | ), |
557 | 557 | |
558 | 558 | // Pagination |
559 | 559 | 'page' => array( |
560 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
560 | + 'description' => __('Current page to fetch.', 'invoicing'), |
|
561 | 561 | 'type' => 'integer', |
562 | 562 | 'default' => 1, |
563 | 563 | ), |
564 | 564 | |
565 | 565 | // Exclude certain invoices |
566 | 566 | 'exclude' => array( |
567 | - 'description' => __( 'Ensure result set excludes specific IDs.', 'invoicing' ), |
|
567 | + 'description' => __('Ensure result set excludes specific IDs.', 'invoicing'), |
|
568 | 568 | 'type' => 'array', |
569 | 569 | 'items' => array( |
570 | 570 | 'type' => 'integer', |
@@ -574,7 +574,7 @@ discard block |
||
574 | 574 | |
575 | 575 | // Order invoices by |
576 | 576 | 'orderby' => array( |
577 | - 'description' => __( 'Sort invoices by object attribute.', 'invoicing' ), |
|
577 | + 'description' => __('Sort invoices by object attribute.', 'invoicing'), |
|
578 | 578 | 'type' => 'string', |
579 | 579 | 'default' => 'date', |
580 | 580 | 'enum' => array( |
@@ -588,10 +588,10 @@ discard block |
||
588 | 588 | |
589 | 589 | // How to order |
590 | 590 | 'order' => array( |
591 | - 'description' => __( 'Order sort attribute ascending or descending.', 'invoicing' ), |
|
591 | + 'description' => __('Order sort attribute ascending or descending.', 'invoicing'), |
|
592 | 592 | 'type' => 'string', |
593 | 593 | 'default' => 'DESC', |
594 | - 'enum' => array( 'ASC', 'DESC' ), |
|
594 | + 'enum' => array('ASC', 'DESC'), |
|
595 | 595 | ), |
596 | 596 | ); |
597 | 597 | |
@@ -603,7 +603,7 @@ discard block |
||
603 | 603 | * |
604 | 604 | * @param array $query_params JSON Schema-formatted collection parameters. |
605 | 605 | */ |
606 | - return apply_filters( "wpinv_rest_invoices_collection_params", $query_params ); |
|
606 | + return apply_filters("wpinv_rest_invoices_collection_params", $query_params); |
|
607 | 607 | } |
608 | 608 | |
609 | 609 | /** |
@@ -614,7 +614,7 @@ discard block |
||
614 | 614 | * @param object|string $post_type Post type name or object. |
615 | 615 | * @return bool Whether the post type is allowed in REST. |
616 | 616 | */ |
617 | - protected function check_is_post_type_allowed( $post_type ) { |
|
617 | + protected function check_is_post_type_allowed($post_type) { |
|
618 | 618 | return true; |
619 | 619 | } |
620 | 620 | |
@@ -626,49 +626,49 @@ discard block |
||
626 | 626 | * @param WP_REST_Request $request Request object. |
627 | 627 | * @return array|WP_Error Invoice Properties or WP_Error. |
628 | 628 | */ |
629 | - protected function prepare_item_for_database( $request ) { |
|
629 | + protected function prepare_item_for_database($request) { |
|
630 | 630 | $prepared_invoice = new stdClass(); |
631 | 631 | |
632 | 632 | // Post ID. |
633 | - if ( isset( $request['id'] ) ) { |
|
634 | - $existing_invoice = $this->get_post( $request['id'] ); |
|
635 | - if ( is_wp_error( $existing_invoice ) ) { |
|
633 | + if (isset($request['id'])) { |
|
634 | + $existing_invoice = $this->get_post($request['id']); |
|
635 | + if (is_wp_error($existing_invoice)) { |
|
636 | 636 | return $existing_invoice; |
637 | 637 | } |
638 | 638 | |
639 | - $prepared_invoice->ID = $existing_invoice->ID; |
|
639 | + $prepared_invoice->ID = $existing_invoice->ID; |
|
640 | 640 | $prepared_invoice->invoice_id = $existing_invoice->ID; |
641 | 641 | } |
642 | 642 | |
643 | 643 | $schema = $this->get_item_schema(); |
644 | 644 | |
645 | 645 | // Invoice owner. |
646 | - if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) { |
|
646 | + if (!empty($schema['properties']['user_id']) && isset($request['user_id'])) { |
|
647 | 647 | $prepared_invoice->user_id = (int) $request['user_id']; |
648 | 648 | } |
649 | 649 | |
650 | 650 | // Cart details. |
651 | - if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) { |
|
651 | + if (!empty($schema['properties']['cart_details']) && isset($request['cart_details'])) { |
|
652 | 652 | $prepared_invoice->cart_details = (array) $request['cart_details']; |
653 | 653 | } |
654 | 654 | |
655 | 655 | // Invoice status. |
656 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
656 | + if (!empty($schema['properties']['status']) && isset($request['status'])) { |
|
657 | 657 | |
658 | - if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) { |
|
658 | + if (in_array($request['status'], array_keys(wpinv_get_invoice_statuses(true, true)), true)) { |
|
659 | 659 | $prepared_invoice->status = $request['status']; |
660 | 660 | } |
661 | 661 | |
662 | 662 | } |
663 | 663 | |
664 | 664 | // User info |
665 | - if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) { |
|
665 | + if (!empty($schema['properties']['user_info']) && isset($request['user_info'])) { |
|
666 | 666 | $prepared_invoice->user_info = array(); |
667 | 667 | $user_info = (array) $request['user_info']; |
668 | 668 | |
669 | - foreach( $user_info as $prop => $value ) { |
|
669 | + foreach ($user_info as $prop => $value) { |
|
670 | 670 | |
671 | - if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) { |
|
671 | + if (!empty($schema['properties']['user_info']['properties'][$prop])) { |
|
672 | 672 | |
673 | 673 | $prepared_invoice->user_info[$prop] = $value; |
674 | 674 | |
@@ -679,49 +679,49 @@ discard block |
||
679 | 679 | } |
680 | 680 | |
681 | 681 | // IP |
682 | - if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) { |
|
682 | + if (!empty($schema['properties']['ip']) && isset($request['ip'])) { |
|
683 | 683 | $prepared_invoice->ip = $request['ip']; |
684 | 684 | } |
685 | 685 | |
686 | 686 | // Payment details |
687 | 687 | $prepared_invoice->payment_details = array(); |
688 | 688 | |
689 | - if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) { |
|
689 | + if (!empty($schema['properties']['gateway']) && isset($request['gateway'])) { |
|
690 | 690 | $prepared_invoice->payment_details['gateway'] = $request['gateway']; |
691 | 691 | } |
692 | 692 | |
693 | - if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) { |
|
693 | + if (!empty($schema['properties']['gateway_title']) && isset($request['gateway_title'])) { |
|
694 | 694 | $prepared_invoice->payment_details['gateway_title'] = $request['gateway_title']; |
695 | 695 | } |
696 | 696 | |
697 | - if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) { |
|
697 | + if (!empty($schema['properties']['currency']) && isset($request['currency'])) { |
|
698 | 698 | $prepared_invoice->payment_details['currency'] = $request['currency']; |
699 | 699 | } |
700 | 700 | |
701 | - if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) { |
|
701 | + if (!empty($schema['properties']['transaction_id']) && isset($request['transaction_id'])) { |
|
702 | 702 | $prepared_invoice->payment_details['transaction_id'] = $request['transaction_id']; |
703 | 703 | } |
704 | 704 | |
705 | 705 | // Dates |
706 | - if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) { |
|
707 | - $post_date = rest_get_date_with_gmt( $request['date'] ); |
|
706 | + if (!empty($schema['properties']['date']) && isset($request['date'])) { |
|
707 | + $post_date = rest_get_date_with_gmt($request['date']); |
|
708 | 708 | |
709 | - if ( ! empty( $post_date ) ) { |
|
709 | + if (!empty($post_date)) { |
|
710 | 710 | $prepared_invoice->post_date = $post_date[0]; |
711 | 711 | } |
712 | 712 | |
713 | 713 | } |
714 | 714 | |
715 | - if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) { |
|
716 | - $due_date = rest_get_date_with_gmt( $request['due_date'] ); |
|
715 | + if (!empty($schema['properties']['due_date']) && isset($request['due_date'])) { |
|
716 | + $due_date = rest_get_date_with_gmt($request['due_date']); |
|
717 | 717 | |
718 | - if ( ! empty( $due_date ) ) { |
|
718 | + if (!empty($due_date)) { |
|
719 | 719 | $prepared_invoice->due_date = $due_date[0]; |
720 | 720 | } |
721 | 721 | |
722 | 722 | } |
723 | 723 | |
724 | - $invoice_data = (array) wp_unslash( $prepared_invoice ); |
|
724 | + $invoice_data = (array) wp_unslash($prepared_invoice); |
|
725 | 725 | |
726 | 726 | /** |
727 | 727 | * Filters an invoice before it is inserted via the REST API. |
@@ -731,7 +731,7 @@ discard block |
||
731 | 731 | * @param array $invoice_data An array of invoice data |
732 | 732 | * @param WP_REST_Request $request Request object. |
733 | 733 | */ |
734 | - return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request ); |
|
734 | + return apply_filters("wpinv_rest_pre_insert_invoice", $invoice_data, $request); |
|
735 | 735 | |
736 | 736 | } |
737 | 737 | |
@@ -744,20 +744,20 @@ discard block |
||
744 | 744 | * @param WP_REST_Request $request Request object. |
745 | 745 | * @return WP_REST_Response Response object. |
746 | 746 | */ |
747 | - public function prepare_item_for_response( $invoice, $request ) { |
|
747 | + public function prepare_item_for_response($invoice, $request) { |
|
748 | 748 | |
749 | - $GLOBALS['post'] = get_post( $invoice->ID ); |
|
749 | + $GLOBALS['post'] = get_post($invoice->ID); |
|
750 | 750 | |
751 | - setup_postdata( $invoice->ID ); |
|
751 | + setup_postdata($invoice->ID); |
|
752 | 752 | |
753 | 753 | // Fetch the fields to include in this response. |
754 | - $fields = $this->get_fields_for_response( $request ); |
|
754 | + $fields = $this->get_fields_for_response($request); |
|
755 | 755 | |
756 | 756 | // Base fields for every invoice. |
757 | 757 | $data = array(); |
758 | 758 | |
759 | 759 | // Set up ID |
760 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
760 | + if (rest_is_field_included('id', $fields)) { |
|
761 | 761 | $data['id'] = $invoice->ID; |
762 | 762 | } |
763 | 763 | |
@@ -772,74 +772,74 @@ discard block |
||
772 | 772 | 'status', 'status_nicename', 'post_type' |
773 | 773 | ); |
774 | 774 | |
775 | - foreach( $invoice_properties as $property ) { |
|
775 | + foreach ($invoice_properties as $property) { |
|
776 | 776 | |
777 | - if ( rest_is_field_included( $property, $fields ) ) { |
|
778 | - $data[$property] = $invoice->get( $property ); |
|
777 | + if (rest_is_field_included($property, $fields)) { |
|
778 | + $data[$property] = $invoice->get($property); |
|
779 | 779 | } |
780 | 780 | |
781 | 781 | } |
782 | 782 | |
783 | 783 | // Cart details |
784 | - if ( rest_is_field_included( 'cart_details', $fields ) ) { |
|
785 | - $data['cart_details'] = $invoice->get( 'cart_details' ); |
|
784 | + if (rest_is_field_included('cart_details', $fields)) { |
|
785 | + $data['cart_details'] = $invoice->get('cart_details'); |
|
786 | 786 | } |
787 | 787 | |
788 | 788 | //Dates |
789 | - $invoice_properties = array( 'date', 'due_date', 'completed_date' ); |
|
789 | + $invoice_properties = array('date', 'due_date', 'completed_date'); |
|
790 | 790 | |
791 | - foreach( $invoice_properties as $property ) { |
|
791 | + foreach ($invoice_properties as $property) { |
|
792 | 792 | |
793 | - if ( rest_is_field_included( $property, $fields ) ) { |
|
794 | - $data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) ); |
|
793 | + if (rest_is_field_included($property, $fields)) { |
|
794 | + $data[$property] = $this->prepare_date_response('0000-00-00 00:00:00', $invoice->get($property)); |
|
795 | 795 | } |
796 | 796 | |
797 | 797 | } |
798 | 798 | |
799 | 799 | // User id |
800 | - if ( rest_is_field_included( 'user_id', $fields ) ) { |
|
801 | - $data['user_id'] = (int) $invoice->get( 'user_id' ); |
|
800 | + if (rest_is_field_included('user_id', $fields)) { |
|
801 | + $data['user_id'] = (int) $invoice->get('user_id'); |
|
802 | 802 | } |
803 | 803 | |
804 | 804 | // User info |
805 | - $user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' ); |
|
805 | + $user_info = array('first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone'); |
|
806 | 806 | |
807 | - foreach( $user_info as $property ) { |
|
807 | + foreach ($user_info as $property) { |
|
808 | 808 | |
809 | - if ( rest_is_field_included( "user_info.$property", $fields ) ) { |
|
810 | - $data['user_info'][$property] = $invoice->get( $property ); |
|
809 | + if (rest_is_field_included("user_info.$property", $fields)) { |
|
810 | + $data['user_info'][$property] = $invoice->get($property); |
|
811 | 811 | } |
812 | 812 | |
813 | 813 | } |
814 | 814 | |
815 | 815 | // Slug |
816 | - if ( rest_is_field_included( 'slug', $fields ) ) { |
|
817 | - $data['slug'] = $invoice->get( 'post_name' ); |
|
816 | + if (rest_is_field_included('slug', $fields)) { |
|
817 | + $data['slug'] = $invoice->get('post_name'); |
|
818 | 818 | } |
819 | 819 | |
820 | 820 | // View invoice link |
821 | - if ( rest_is_field_included( 'link', $fields ) ) { |
|
822 | - $data['link'] = esc_url( $invoice->get_view_url() ); |
|
821 | + if (rest_is_field_included('link', $fields)) { |
|
822 | + $data['link'] = esc_url($invoice->get_view_url()); |
|
823 | 823 | } |
824 | 824 | |
825 | 825 | |
826 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
827 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
828 | - $data = $this->filter_response_by_context( $data, $context ); |
|
826 | + $context = !empty($request['context']) ? $request['context'] : 'view'; |
|
827 | + $data = $this->add_additional_fields_to_object($data, $request); |
|
828 | + $data = $this->filter_response_by_context($data, $context); |
|
829 | 829 | |
830 | 830 | // Wrap the data in a response object. |
831 | - $response = rest_ensure_response( $data ); |
|
831 | + $response = rest_ensure_response($data); |
|
832 | 832 | |
833 | - $links = $this->prepare_links( $invoice ); |
|
834 | - $response->add_links( $links ); |
|
833 | + $links = $this->prepare_links($invoice); |
|
834 | + $response->add_links($links); |
|
835 | 835 | |
836 | - if ( ! empty( $links['self']['href'] ) ) { |
|
837 | - $actions = $this->get_available_actions( $invoice, $request ); |
|
836 | + if (!empty($links['self']['href'])) { |
|
837 | + $actions = $this->get_available_actions($invoice, $request); |
|
838 | 838 | |
839 | 839 | $self = $links['self']['href']; |
840 | 840 | |
841 | - foreach ( $actions as $rel ) { |
|
842 | - $response->add_link( $rel, $self ); |
|
841 | + foreach ($actions as $rel) { |
|
842 | + $response->add_link($rel, $self); |
|
843 | 843 | } |
844 | 844 | } |
845 | 845 | |
@@ -852,7 +852,7 @@ discard block |
||
852 | 852 | * @param WPInv_Invoice $invoice The invoice object. |
853 | 853 | * @param WP_REST_Request $request Request object. |
854 | 854 | */ |
855 | - return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request ); |
|
855 | + return apply_filters("wpinv_rest_prepare_invoice", $response, $invoice, $request); |
|
856 | 856 | } |
857 | 857 | |
858 | 858 | /** |
@@ -865,57 +865,57 @@ discard block |
||
865 | 865 | * @param WP_REST_Request $request Full details about the request. |
866 | 866 | * @return array Fields to be included in the response. |
867 | 867 | */ |
868 | - public function get_fields_for_response( $request ) { |
|
868 | + public function get_fields_for_response($request) { |
|
869 | 869 | $schema = $this->get_item_schema(); |
870 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
870 | + $properties = isset($schema['properties']) ? $schema['properties'] : array(); |
|
871 | 871 | |
872 | 872 | $additional_fields = $this->get_additional_fields(); |
873 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
873 | + foreach ($additional_fields as $field_name => $field_options) { |
|
874 | 874 | // For back-compat, include any field with an empty schema |
875 | 875 | // because it won't be present in $this->get_item_schema(). |
876 | - if ( is_null( $field_options['schema'] ) ) { |
|
877 | - $properties[ $field_name ] = $field_options; |
|
876 | + if (is_null($field_options['schema'])) { |
|
877 | + $properties[$field_name] = $field_options; |
|
878 | 878 | } |
879 | 879 | } |
880 | 880 | |
881 | 881 | // Exclude fields that specify a different context than the request context. |
882 | 882 | $context = $request['context']; |
883 | - if ( $context ) { |
|
884 | - foreach ( $properties as $name => $options ) { |
|
885 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
886 | - unset( $properties[ $name ] ); |
|
883 | + if ($context) { |
|
884 | + foreach ($properties as $name => $options) { |
|
885 | + if (!empty($options['context']) && !in_array($context, $options['context'], true)) { |
|
886 | + unset($properties[$name]); |
|
887 | 887 | } |
888 | 888 | } |
889 | 889 | } |
890 | 890 | |
891 | - $fields = array_keys( $properties ); |
|
891 | + $fields = array_keys($properties); |
|
892 | 892 | |
893 | - if ( ! isset( $request['_fields'] ) ) { |
|
893 | + if (!isset($request['_fields'])) { |
|
894 | 894 | return $fields; |
895 | 895 | } |
896 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
897 | - if ( 0 === count( $requested_fields ) ) { |
|
896 | + $requested_fields = wpinv_parse_list($request['_fields']); |
|
897 | + if (0 === count($requested_fields)) { |
|
898 | 898 | return $fields; |
899 | 899 | } |
900 | 900 | // Trim off outside whitespace from the comma delimited list. |
901 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
901 | + $requested_fields = array_map('trim', $requested_fields); |
|
902 | 902 | // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
903 | - if ( in_array( 'id', $fields, true ) ) { |
|
903 | + if (in_array('id', $fields, true)) { |
|
904 | 904 | $requested_fields[] = 'id'; |
905 | 905 | } |
906 | 906 | // Return the list of all requested fields which appear in the schema. |
907 | 907 | return array_reduce( |
908 | 908 | $requested_fields, |
909 | - function( $response_fields, $field ) use ( $fields ) { |
|
910 | - if ( in_array( $field, $fields, true ) ) { |
|
909 | + function($response_fields, $field) use ($fields) { |
|
910 | + if (in_array($field, $fields, true)) { |
|
911 | 911 | $response_fields[] = $field; |
912 | 912 | return $response_fields; |
913 | 913 | } |
914 | 914 | // Check for nested fields if $field is not a direct match. |
915 | - $nested_fields = explode( '.', $field ); |
|
915 | + $nested_fields = explode('.', $field); |
|
916 | 916 | // A nested field is included so long as its top-level property is |
917 | 917 | // present in the schema. |
918 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
918 | + if (in_array($nested_fields[0], $fields, true)) { |
|
919 | 919 | $response_fields[] = $field; |
920 | 920 | } |
921 | 921 | return $response_fields; |
@@ -934,8 +934,8 @@ discard block |
||
934 | 934 | public function get_item_schema() { |
935 | 935 | |
936 | 936 | // Maybe retrieve the schema from cache. |
937 | - if ( $this->schema ) { |
|
938 | - return $this->add_additional_fields_schema( $this->schema ); |
|
937 | + if ($this->schema) { |
|
938 | + return $this->add_additional_fields_schema($this->schema); |
|
939 | 939 | } |
940 | 940 | |
941 | 941 | $schema = array( |
@@ -947,266 +947,266 @@ discard block |
||
947 | 947 | 'properties' => array( |
948 | 948 | |
949 | 949 | 'title' => array( |
950 | - 'description' => __( 'The title for the invoice.', 'invoicing' ), |
|
950 | + 'description' => __('The title for the invoice.', 'invoicing'), |
|
951 | 951 | 'type' => 'string', |
952 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
952 | + 'context' => array('view', 'edit', 'embed'), |
|
953 | 953 | 'readonly' => true, |
954 | 954 | ), |
955 | 955 | |
956 | 956 | 'user_id' => array( |
957 | - 'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ), |
|
957 | + 'description' => __('The ID of the owner of the invoice.', 'invoicing'), |
|
958 | 958 | 'type' => 'integer', |
959 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
959 | + 'context' => array('view', 'edit', 'embed'), |
|
960 | 960 | ), |
961 | 961 | |
962 | 962 | 'email' => array( |
963 | - 'description' => __( 'The email of the owner of the invoice.', 'invoicing' ), |
|
963 | + 'description' => __('The email of the owner of the invoice.', 'invoicing'), |
|
964 | 964 | 'type' => 'string', |
965 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
965 | + 'context' => array('view', 'edit', 'embed'), |
|
966 | 966 | 'readonly' => true, |
967 | 967 | ), |
968 | 968 | |
969 | 969 | 'ip' => array( |
970 | - 'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ), |
|
970 | + 'description' => __('The IP of the owner of the invoice.', 'invoicing'), |
|
971 | 971 | 'type' => 'string', |
972 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
972 | + 'context' => array('view', 'edit', 'embed'), |
|
973 | 973 | ), |
974 | 974 | |
975 | 975 | 'user_info' => array( |
976 | - 'description' => __( 'Information about the owner of the invoice.', 'invoicing' ), |
|
976 | + 'description' => __('Information about the owner of the invoice.', 'invoicing'), |
|
977 | 977 | 'type' => 'object', |
978 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
978 | + 'context' => array('view', 'edit', 'embed'), |
|
979 | 979 | 'properties' => array( |
980 | 980 | |
981 | 981 | 'first_name' => array( |
982 | - 'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ), |
|
982 | + 'description' => __('The first name of the owner of the invoice.', 'invoicing'), |
|
983 | 983 | 'type' => 'string', |
984 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
984 | + 'context' => array('view', 'edit', 'embed'), |
|
985 | 985 | ), |
986 | 986 | |
987 | 987 | 'last_name' => array( |
988 | - 'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ), |
|
988 | + 'description' => __('The last name of the owner of the invoice.', 'invoicing'), |
|
989 | 989 | 'type' => 'string', |
990 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
990 | + 'context' => array('view', 'edit', 'embed'), |
|
991 | 991 | ), |
992 | 992 | |
993 | 993 | 'company' => array( |
994 | - 'description' => __( 'The company of the owner of the invoice.', 'invoicing' ), |
|
994 | + 'description' => __('The company of the owner of the invoice.', 'invoicing'), |
|
995 | 995 | 'type' => 'string', |
996 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
996 | + 'context' => array('view', 'edit', 'embed'), |
|
997 | 997 | ), |
998 | 998 | |
999 | 999 | 'vat_number' => array( |
1000 | - 'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ), |
|
1000 | + 'description' => __('The VAT number of the owner of the invoice.', 'invoicing'), |
|
1001 | 1001 | 'type' => 'string', |
1002 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1002 | + 'context' => array('view', 'edit', 'embed'), |
|
1003 | 1003 | ), |
1004 | 1004 | |
1005 | 1005 | 'vat_rate' => array( |
1006 | - 'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ), |
|
1006 | + 'description' => __('The VAT rate applied on the invoice.', 'invoicing'), |
|
1007 | 1007 | 'type' => 'string', |
1008 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1008 | + 'context' => array('view', 'edit', 'embed'), |
|
1009 | 1009 | ), |
1010 | 1010 | |
1011 | 1011 | 'address' => array( |
1012 | - 'description' => __( 'The address of the invoice owner.', 'invoicing' ), |
|
1012 | + 'description' => __('The address of the invoice owner.', 'invoicing'), |
|
1013 | 1013 | 'type' => 'string', |
1014 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1014 | + 'context' => array('view', 'edit', 'embed'), |
|
1015 | 1015 | ), |
1016 | 1016 | |
1017 | 1017 | 'city' => array( |
1018 | - 'description' => __( 'The city of the invoice owner.', 'invoicing' ), |
|
1018 | + 'description' => __('The city of the invoice owner.', 'invoicing'), |
|
1019 | 1019 | 'type' => 'string', |
1020 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1020 | + 'context' => array('view', 'edit', 'embed'), |
|
1021 | 1021 | ), |
1022 | 1022 | |
1023 | 1023 | 'country' => array( |
1024 | - 'description' => __( 'The country of the invoice owner.', 'invoicing' ), |
|
1024 | + 'description' => __('The country of the invoice owner.', 'invoicing'), |
|
1025 | 1025 | 'type' => 'string', |
1026 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1026 | + 'context' => array('view', 'edit', 'embed'), |
|
1027 | 1027 | ), |
1028 | 1028 | |
1029 | 1029 | 'state' => array( |
1030 | - 'description' => __( 'The state of the invoice owner.', 'invoicing' ), |
|
1030 | + 'description' => __('The state of the invoice owner.', 'invoicing'), |
|
1031 | 1031 | 'type' => 'string', |
1032 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1032 | + 'context' => array('view', 'edit', 'embed'), |
|
1033 | 1033 | ), |
1034 | 1034 | |
1035 | 1035 | 'zip' => array( |
1036 | - 'description' => __( 'The zip code of the invoice owner.', 'invoicing' ), |
|
1036 | + 'description' => __('The zip code of the invoice owner.', 'invoicing'), |
|
1037 | 1037 | 'type' => 'string', |
1038 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1038 | + 'context' => array('view', 'edit', 'embed'), |
|
1039 | 1039 | ), |
1040 | 1040 | |
1041 | 1041 | 'phone' => array( |
1042 | - 'description' => __( 'The phone number of the invoice owner.', 'invoicing' ), |
|
1042 | + 'description' => __('The phone number of the invoice owner.', 'invoicing'), |
|
1043 | 1043 | 'type' => 'string', |
1044 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1044 | + 'context' => array('view', 'edit', 'embed'), |
|
1045 | 1045 | ), |
1046 | 1046 | ), |
1047 | 1047 | ), |
1048 | 1048 | |
1049 | 1049 | 'id' => array( |
1050 | - 'description' => __( 'Unique identifier for the invoice.', 'invoicing' ), |
|
1050 | + 'description' => __('Unique identifier for the invoice.', 'invoicing'), |
|
1051 | 1051 | 'type' => 'integer', |
1052 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1052 | + 'context' => array('view', 'edit', 'embed'), |
|
1053 | 1053 | 'readonly' => true, |
1054 | 1054 | ), |
1055 | 1055 | |
1056 | 1056 | 'key' => array( |
1057 | - 'description' => __( 'A unique key for the invoice.', 'invoicing' ), |
|
1057 | + 'description' => __('A unique key for the invoice.', 'invoicing'), |
|
1058 | 1058 | 'type' => 'string', |
1059 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1059 | + 'context' => array('view', 'edit', 'embed'), |
|
1060 | 1060 | 'readonly' => true, |
1061 | 1061 | ), |
1062 | 1062 | |
1063 | 1063 | 'number' => array( |
1064 | - 'description' => __( 'The invoice number.', 'invoicing' ), |
|
1064 | + 'description' => __('The invoice number.', 'invoicing'), |
|
1065 | 1065 | 'type' => 'string', |
1066 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1066 | + 'context' => array('view', 'edit', 'embed'), |
|
1067 | 1067 | 'readonly' => true, |
1068 | 1068 | ), |
1069 | 1069 | |
1070 | 1070 | 'transaction_id' => array( |
1071 | - 'description' => __( 'The transaction id of the invoice.', 'invoicing' ), |
|
1071 | + 'description' => __('The transaction id of the invoice.', 'invoicing'), |
|
1072 | 1072 | 'type' => 'string', |
1073 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1073 | + 'context' => array('view', 'edit', 'embed'), |
|
1074 | 1074 | ), |
1075 | 1075 | |
1076 | 1076 | 'gateway' => array( |
1077 | - 'description' => __( 'The gateway used to process the invoice.', 'invoicing' ), |
|
1077 | + 'description' => __('The gateway used to process the invoice.', 'invoicing'), |
|
1078 | 1078 | 'type' => 'string', |
1079 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1079 | + 'context' => array('view', 'edit', 'embed'), |
|
1080 | 1080 | ), |
1081 | 1081 | |
1082 | 1082 | 'gateway_title' => array( |
1083 | - 'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ), |
|
1083 | + 'description' => __('The title of the gateway used to process the invoice.', 'invoicing'), |
|
1084 | 1084 | 'type' => 'string', |
1085 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1085 | + 'context' => array('view', 'edit', 'embed'), |
|
1086 | 1086 | ), |
1087 | 1087 | |
1088 | 1088 | 'total' => array( |
1089 | - 'description' => __( 'The total amount of the invoice.', 'invoicing' ), |
|
1089 | + 'description' => __('The total amount of the invoice.', 'invoicing'), |
|
1090 | 1090 | 'type' => 'number', |
1091 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1091 | + 'context' => array('view', 'edit', 'embed'), |
|
1092 | 1092 | 'readonly' => true, |
1093 | 1093 | ), |
1094 | 1094 | |
1095 | 1095 | 'discount' => array( |
1096 | - 'description' => __( 'The discount applied to the invoice.', 'invoicing' ), |
|
1096 | + 'description' => __('The discount applied to the invoice.', 'invoicing'), |
|
1097 | 1097 | 'type' => 'number', |
1098 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1098 | + 'context' => array('view', 'edit', 'embed'), |
|
1099 | 1099 | 'readonly' => true, |
1100 | 1100 | ), |
1101 | 1101 | |
1102 | 1102 | 'discount_code' => array( |
1103 | - 'description' => __( 'The discount code applied to the invoice.', 'invoicing' ), |
|
1103 | + 'description' => __('The discount code applied to the invoice.', 'invoicing'), |
|
1104 | 1104 | 'type' => 'string', |
1105 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1105 | + 'context' => array('view', 'edit', 'embed'), |
|
1106 | 1106 | 'readonly' => true, |
1107 | 1107 | ), |
1108 | 1108 | |
1109 | 1109 | 'tax' => array( |
1110 | - 'description' => __( 'The tax applied to the invoice.', 'invoicing' ), |
|
1110 | + 'description' => __('The tax applied to the invoice.', 'invoicing'), |
|
1111 | 1111 | 'type' => 'number', |
1112 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1112 | + 'context' => array('view', 'edit', 'embed'), |
|
1113 | 1113 | 'readonly' => true, |
1114 | 1114 | ), |
1115 | 1115 | |
1116 | 1116 | 'fees_total' => array( |
1117 | - 'description' => __( 'The total fees applied to the invoice.', 'invoicing' ), |
|
1117 | + 'description' => __('The total fees applied to the invoice.', 'invoicing'), |
|
1118 | 1118 | 'type' => 'number', |
1119 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1119 | + 'context' => array('view', 'edit', 'embed'), |
|
1120 | 1120 | 'readonly' => true, |
1121 | 1121 | ), |
1122 | 1122 | |
1123 | 1123 | 'subtotal' => array( |
1124 | - 'description' => __( 'The sub-total for the invoice.', 'invoicing' ), |
|
1124 | + 'description' => __('The sub-total for the invoice.', 'invoicing'), |
|
1125 | 1125 | 'type' => 'number', |
1126 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1126 | + 'context' => array('view', 'edit', 'embed'), |
|
1127 | 1127 | 'readonly' => true, |
1128 | 1128 | ), |
1129 | 1129 | |
1130 | 1130 | 'currency' => array( |
1131 | - 'description' => __( 'The currency used to process the invoice.', 'invoicing' ), |
|
1131 | + 'description' => __('The currency used to process the invoice.', 'invoicing'), |
|
1132 | 1132 | 'type' => 'string', |
1133 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1133 | + 'context' => array('view', 'edit', 'embed'), |
|
1134 | 1134 | ), |
1135 | 1135 | |
1136 | 1136 | 'cart_details' => array( |
1137 | - 'description' => __( 'The cart details for invoice.', 'invoicing' ), |
|
1137 | + 'description' => __('The cart details for invoice.', 'invoicing'), |
|
1138 | 1138 | 'type' => 'array', |
1139 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1139 | + 'context' => array('view', 'edit', 'embed'), |
|
1140 | 1140 | 'required' => true, |
1141 | 1141 | ), |
1142 | 1142 | |
1143 | 1143 | 'date' => array( |
1144 | - 'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ), |
|
1145 | - 'type' => array( 'string', 'null' ), |
|
1144 | + 'description' => __("The date the invoice was published, in the site's timezone.", 'invoicing'), |
|
1145 | + 'type' => array('string', 'null'), |
|
1146 | 1146 | 'format' => 'date-time', |
1147 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1147 | + 'context' => array('view', 'edit', 'embed'), |
|
1148 | 1148 | ), |
1149 | 1149 | |
1150 | 1150 | 'due_date' => array( |
1151 | - 'description' => __( 'The due date for the invoice.', 'invoicing' ), |
|
1152 | - 'type' => array( 'string', 'null' ), |
|
1151 | + 'description' => __('The due date for the invoice.', 'invoicing'), |
|
1152 | + 'type' => array('string', 'null'), |
|
1153 | 1153 | 'format' => 'date-time', |
1154 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1154 | + 'context' => array('view', 'edit', 'embed'), |
|
1155 | 1155 | ), |
1156 | 1156 | |
1157 | 1157 | 'completed_date' => array( |
1158 | - 'description' => __( 'The completed date for the invoice.', 'invoicing' ), |
|
1159 | - 'type' => array( 'string', 'null' ), |
|
1158 | + 'description' => __('The completed date for the invoice.', 'invoicing'), |
|
1159 | + 'type' => array('string', 'null'), |
|
1160 | 1160 | 'format' => 'date-time', |
1161 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1161 | + 'context' => array('view', 'edit', 'embed'), |
|
1162 | 1162 | 'readonly' => true, |
1163 | 1163 | ), |
1164 | 1164 | |
1165 | 1165 | 'link' => array( |
1166 | - 'description' => __( 'URL to the invoice.', 'invoicing' ), |
|
1166 | + 'description' => __('URL to the invoice.', 'invoicing'), |
|
1167 | 1167 | 'type' => 'string', |
1168 | 1168 | 'format' => 'uri', |
1169 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1169 | + 'context' => array('view', 'edit', 'embed'), |
|
1170 | 1170 | 'readonly' => true, |
1171 | 1171 | ), |
1172 | 1172 | |
1173 | 1173 | 'mode' => array( |
1174 | - 'description' => __( 'The mode used to process the invoice.', 'invoicing' ), |
|
1174 | + 'description' => __('The mode used to process the invoice.', 'invoicing'), |
|
1175 | 1175 | 'type' => 'string', |
1176 | - 'enum' => array( 'live', 'test' ), |
|
1177 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1176 | + 'enum' => array('live', 'test'), |
|
1177 | + 'context' => array('view', 'edit', 'embed'), |
|
1178 | 1178 | 'readonly' => true, |
1179 | 1179 | ), |
1180 | 1180 | |
1181 | 1181 | 'slug' => array( |
1182 | - 'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ), |
|
1182 | + 'description' => __('An alphanumeric identifier for the invoice.', 'invoicing'), |
|
1183 | 1183 | 'type' => 'string', |
1184 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1184 | + 'context' => array('view', 'edit', 'embed'), |
|
1185 | 1185 | 'arg_options' => array( |
1186 | - 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
|
1186 | + 'sanitize_callback' => array($this, 'sanitize_slug'), |
|
1187 | 1187 | ), |
1188 | 1188 | 'readonly' => true, |
1189 | 1189 | ), |
1190 | 1190 | |
1191 | 1191 | 'status' => array( |
1192 | - 'description' => __( 'A named status for the invoice.', 'invoicing' ), |
|
1192 | + 'description' => __('A named status for the invoice.', 'invoicing'), |
|
1193 | 1193 | 'type' => 'string', |
1194 | - 'enum' => array_keys( wpinv_get_invoice_statuses( true, true ) ), |
|
1195 | - 'context' => array( 'view', 'edit' ), |
|
1194 | + 'enum' => array_keys(wpinv_get_invoice_statuses(true, true)), |
|
1195 | + 'context' => array('view', 'edit'), |
|
1196 | 1196 | 'default' => 'wpi-pending', |
1197 | 1197 | ), |
1198 | 1198 | |
1199 | 1199 | 'status_nicename' => array( |
1200 | - 'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ), |
|
1200 | + 'description' => __('A human-readable status name for the invoice.', 'invoicing'), |
|
1201 | 1201 | 'type' => 'string', |
1202 | - 'context' => array( 'view', 'embed' ), |
|
1202 | + 'context' => array('view', 'embed'), |
|
1203 | 1203 | 'readonly' => true, |
1204 | 1204 | ), |
1205 | 1205 | |
1206 | 1206 | 'post_type' => array( |
1207 | - 'description' => __( 'The post type for the invoice.', 'invoicing' ), |
|
1207 | + 'description' => __('The post type for the invoice.', 'invoicing'), |
|
1208 | 1208 | 'type' => 'string', |
1209 | - 'context' => array( 'view' ), |
|
1209 | + 'context' => array('view'), |
|
1210 | 1210 | 'readonly' => true, |
1211 | 1211 | ), |
1212 | 1212 | ), |
@@ -1224,12 +1224,12 @@ discard block |
||
1224 | 1224 | * |
1225 | 1225 | * @param array $schema The invoice schema. |
1226 | 1226 | */ |
1227 | - $schema = apply_filters( "wpinv_rest_invoice_schema", $schema ); |
|
1227 | + $schema = apply_filters("wpinv_rest_invoice_schema", $schema); |
|
1228 | 1228 | |
1229 | 1229 | // Cache the invoice schema. |
1230 | 1230 | $this->schema = $schema; |
1231 | 1231 | |
1232 | - return $this->add_additional_fields_schema( $this->schema ); |
|
1232 | + return $this->add_additional_fields_schema($this->schema); |
|
1233 | 1233 | } |
1234 | 1234 | |
1235 | 1235 | /** |
@@ -1241,20 +1241,20 @@ discard block |
||
1241 | 1241 | */ |
1242 | 1242 | protected function get_schema_links() { |
1243 | 1243 | |
1244 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1244 | + $href = rest_url("{$this->namespace}/{$this->rest_base}/{id}"); |
|
1245 | 1245 | |
1246 | 1246 | $links = array(); |
1247 | 1247 | |
1248 | 1248 | $links[] = array( |
1249 | 1249 | 'rel' => 'https://api.w.org/action-publish', |
1250 | - 'title' => __( 'The current user can mark this invoice as completed.', 'invoicing' ), |
|
1250 | + 'title' => __('The current user can mark this invoice as completed.', 'invoicing'), |
|
1251 | 1251 | 'href' => $href, |
1252 | 1252 | 'targetSchema' => array( |
1253 | 1253 | 'type' => 'object', |
1254 | 1254 | 'properties' => array( |
1255 | 1255 | 'status' => array( |
1256 | 1256 | 'type' => 'string', |
1257 | - 'enum' => array( 'publish', 'wpi-renewal' ), |
|
1257 | + 'enum' => array('publish', 'wpi-renewal'), |
|
1258 | 1258 | ), |
1259 | 1259 | ), |
1260 | 1260 | ), |
@@ -1262,7 +1262,7 @@ discard block |
||
1262 | 1262 | |
1263 | 1263 | $links[] = array( |
1264 | 1264 | 'rel' => 'https://api.w.org/action-assign-author', |
1265 | - 'title' => __( 'The current user can change the owner of this invoice.', 'invoicing' ), |
|
1265 | + 'title' => __('The current user can change the owner of this invoice.', 'invoicing'), |
|
1266 | 1266 | 'href' => $href, |
1267 | 1267 | 'targetSchema' => array( |
1268 | 1268 | 'type' => 'object', |
@@ -1285,24 +1285,24 @@ discard block |
||
1285 | 1285 | * @param WPInv_Invoice $invoice Invoice Object. |
1286 | 1286 | * @return array Links for the given invoice. |
1287 | 1287 | */ |
1288 | - protected function prepare_links( $invoice ) { |
|
1288 | + protected function prepare_links($invoice) { |
|
1289 | 1289 | |
1290 | 1290 | // Prepare the base REST API endpoint for invoices. |
1291 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1291 | + $base = sprintf('%s/%s', $this->namespace, $this->rest_base); |
|
1292 | 1292 | |
1293 | 1293 | // Entity meta. |
1294 | 1294 | $links = array( |
1295 | 1295 | 'self' => array( |
1296 | - 'href' => rest_url( trailingslashit( $base ) . $invoice->ID ), |
|
1296 | + 'href' => rest_url(trailingslashit($base) . $invoice->ID), |
|
1297 | 1297 | ), |
1298 | 1298 | 'collection' => array( |
1299 | - 'href' => rest_url( $base ), |
|
1299 | + 'href' => rest_url($base), |
|
1300 | 1300 | ), |
1301 | 1301 | ); |
1302 | 1302 | |
1303 | - if ( ! empty( $invoice->user_id ) ) { |
|
1303 | + if (!empty($invoice->user_id)) { |
|
1304 | 1304 | $links['user'] = array( |
1305 | - 'href' => rest_url( 'wp/v2/users/' . $invoice->user_id ), |
|
1305 | + 'href' => rest_url('wp/v2/users/' . $invoice->user_id), |
|
1306 | 1306 | 'embeddable' => true, |
1307 | 1307 | ); |
1308 | 1308 | } |
@@ -1316,7 +1316,7 @@ discard block |
||
1316 | 1316 | * |
1317 | 1317 | * @param array $links Rest links. |
1318 | 1318 | */ |
1319 | - return apply_filters( "wpinv_rest_invoice_links", $links ); |
|
1319 | + return apply_filters("wpinv_rest_invoice_links", $links); |
|
1320 | 1320 | |
1321 | 1321 | } |
1322 | 1322 | |
@@ -1329,24 +1329,24 @@ discard block |
||
1329 | 1329 | * @param WP_REST_Request $request Request object. |
1330 | 1330 | * @return array List of link relations. |
1331 | 1331 | */ |
1332 | - protected function get_available_actions( $invoice, $request ) { |
|
1332 | + protected function get_available_actions($invoice, $request) { |
|
1333 | 1333 | |
1334 | - if ( 'edit' !== $request['context'] ) { |
|
1334 | + if ('edit' !== $request['context']) { |
|
1335 | 1335 | return array(); |
1336 | 1336 | } |
1337 | 1337 | |
1338 | 1338 | $rels = array(); |
1339 | 1339 | |
1340 | 1340 | // Retrieve the post type object. |
1341 | - $post_type = get_post_type_object( $invoice->post_type ); |
|
1341 | + $post_type = get_post_type_object($invoice->post_type); |
|
1342 | 1342 | |
1343 | 1343 | // Mark invoice as completed. |
1344 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1344 | + if (current_user_can($post_type->cap->publish_posts)) { |
|
1345 | 1345 | $rels[] = 'https://api.w.org/action-publish'; |
1346 | 1346 | } |
1347 | 1347 | |
1348 | 1348 | // Change the owner of the invoice. |
1349 | - if ( current_user_can( $post_type->cap->edit_others_posts ) ) { |
|
1349 | + if (current_user_can($post_type->cap->edit_others_posts)) { |
|
1350 | 1350 | $rels[] = 'https://api.w.org/action-assign-author'; |
1351 | 1351 | } |
1352 | 1352 | |
@@ -1359,7 +1359,7 @@ discard block |
||
1359 | 1359 | * |
1360 | 1360 | * @param array $rels Available link relations. |
1361 | 1361 | */ |
1362 | - return apply_filters( "wpinv_rest_invoice_link_relations", $rels ); |
|
1362 | + return apply_filters("wpinv_rest_invoice_link_relations", $rels); |
|
1363 | 1363 | } |
1364 | 1364 | |
1365 | 1365 | /** |
@@ -1372,11 +1372,11 @@ discard block |
||
1372 | 1372 | * @param string $parameter Additional parameter to pass to validation. |
1373 | 1373 | * @return array|WP_Error A list of valid statuses, otherwise WP_Error object. |
1374 | 1374 | */ |
1375 | - public function sanitize_post_statuses( $statuses, $request, $parameter ) { |
|
1375 | + public function sanitize_post_statuses($statuses, $request, $parameter) { |
|
1376 | 1376 | |
1377 | - $statuses = wp_parse_slug_list( $statuses ); |
|
1378 | - $valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) ); |
|
1379 | - return array_intersect( $statuses, $valid_statuses ); |
|
1377 | + $statuses = wp_parse_slug_list($statuses); |
|
1378 | + $valid_statuses = array_keys(wpinv_get_invoice_statuses(true, true)); |
|
1379 | + return array_intersect($statuses, $valid_statuses); |
|
1380 | 1380 | |
1381 | 1381 | } |
1382 | 1382 |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
3 | +if (!defined('ABSPATH')) exit; |
|
4 | 4 | |
5 | 5 | class WPInv_Item { |
6 | 6 | public $ID = 0; |
@@ -49,26 +49,26 @@ discard block |
||
49 | 49 | public $filter; |
50 | 50 | |
51 | 51 | |
52 | - public function __construct( $_id = false, $_args = array() ) { |
|
53 | - $item = WP_Post::get_instance( $_id ); |
|
54 | - return $this->setup_item( $item ); |
|
52 | + public function __construct($_id = false, $_args = array()) { |
|
53 | + $item = WP_Post::get_instance($_id); |
|
54 | + return $this->setup_item($item); |
|
55 | 55 | } |
56 | 56 | |
57 | - private function setup_item( $item ) { |
|
58 | - if( ! is_object( $item ) ) { |
|
57 | + private function setup_item($item) { |
|
58 | + if (!is_object($item)) { |
|
59 | 59 | return false; |
60 | 60 | } |
61 | 61 | |
62 | - if( ! is_a( $item, 'WP_Post' ) ) { |
|
62 | + if (!is_a($item, 'WP_Post')) { |
|
63 | 63 | return false; |
64 | 64 | } |
65 | 65 | |
66 | - if( 'wpi_item' !== $item->post_type ) { |
|
66 | + if ('wpi_item' !== $item->post_type) { |
|
67 | 67 | return false; |
68 | 68 | } |
69 | 69 | |
70 | - foreach ( $item as $key => $value ) { |
|
71 | - switch ( $key ) { |
|
70 | + foreach ($item as $key => $value) { |
|
71 | + switch ($key) { |
|
72 | 72 | default: |
73 | 73 | $this->$key = $value; |
74 | 74 | break; |
@@ -78,38 +78,38 @@ discard block |
||
78 | 78 | return true; |
79 | 79 | } |
80 | 80 | |
81 | - public function __get( $key ) { |
|
82 | - if ( method_exists( $this, 'get_' . $key ) ) { |
|
83 | - return call_user_func( array( $this, 'get_' . $key ) ); |
|
81 | + public function __get($key) { |
|
82 | + if (method_exists($this, 'get_' . $key)) { |
|
83 | + return call_user_func(array($this, 'get_' . $key)); |
|
84 | 84 | } else { |
85 | - return new WP_Error( 'wpinv-item-invalid-property', sprintf( __( 'Can\'t get property %s', 'invoicing' ), $key ) ); |
|
85 | + return new WP_Error('wpinv-item-invalid-property', sprintf(__('Can\'t get property %s', 'invoicing'), $key)); |
|
86 | 86 | } |
87 | 87 | } |
88 | 88 | |
89 | - public function create( $data = array(), $wp_error = false ) { |
|
90 | - if ( $this->ID != 0 ) { |
|
89 | + public function create($data = array(), $wp_error = false) { |
|
90 | + if ($this->ID != 0) { |
|
91 | 91 | return false; |
92 | 92 | } |
93 | 93 | |
94 | 94 | $defaults = array( |
95 | 95 | 'post_type' => 'wpi_item', |
96 | 96 | 'post_status' => 'draft', |
97 | - 'post_title' => __( 'New Invoice Item', 'invoicing' ) |
|
97 | + 'post_title' => __('New Invoice Item', 'invoicing') |
|
98 | 98 | ); |
99 | 99 | |
100 | - $args = wp_parse_args( $data, $defaults ); |
|
100 | + $args = wp_parse_args($data, $defaults); |
|
101 | 101 | |
102 | - do_action( 'wpinv_item_pre_create', $args ); |
|
102 | + do_action('wpinv_item_pre_create', $args); |
|
103 | 103 | |
104 | - $id = wp_insert_post( $args, $wp_error ); |
|
104 | + $id = wp_insert_post($args, $wp_error); |
|
105 | 105 | if ($wp_error && is_wp_error($id)) { |
106 | 106 | return $id; |
107 | 107 | } |
108 | - if ( !$id ) { |
|
108 | + if (!$id) { |
|
109 | 109 | return false; |
110 | 110 | } |
111 | 111 | |
112 | - $item = WP_Post::get_instance( $id ); |
|
112 | + $item = WP_Post::get_instance($id); |
|
113 | 113 | |
114 | 114 | if (!empty($item) && !empty($data['meta'])) { |
115 | 115 | $this->ID = $item->ID; |
@@ -117,47 +117,47 @@ discard block |
||
117 | 117 | } |
118 | 118 | |
119 | 119 | // Set custom id if not set. |
120 | - if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
|
121 | - $this->save_metas( array( 'custom_id' => $id ) ); |
|
120 | + if (empty($data['meta']['custom_id']) && !$this->get_custom_id()) { |
|
121 | + $this->save_metas(array('custom_id' => $id)); |
|
122 | 122 | } |
123 | 123 | |
124 | - do_action( 'wpinv_item_create', $id, $args ); |
|
124 | + do_action('wpinv_item_create', $id, $args); |
|
125 | 125 | |
126 | - return $this->setup_item( $item ); |
|
126 | + return $this->setup_item($item); |
|
127 | 127 | } |
128 | 128 | |
129 | - public function update( $data = array(), $wp_error = false ) { |
|
130 | - if ( !$this->ID > 0 ) { |
|
129 | + public function update($data = array(), $wp_error = false) { |
|
130 | + if (!$this->ID > 0) { |
|
131 | 131 | return false; |
132 | 132 | } |
133 | 133 | |
134 | 134 | $data['ID'] = $this->ID; |
135 | 135 | |
136 | - do_action( 'wpinv_item_pre_update', $data ); |
|
136 | + do_action('wpinv_item_pre_update', $data); |
|
137 | 137 | |
138 | - $id = wp_update_post( $data, $wp_error ); |
|
138 | + $id = wp_update_post($data, $wp_error); |
|
139 | 139 | if ($wp_error && is_wp_error($id)) { |
140 | 140 | return $id; |
141 | 141 | } |
142 | 142 | |
143 | - if ( !$id ) { |
|
143 | + if (!$id) { |
|
144 | 144 | return false; |
145 | 145 | } |
146 | 146 | |
147 | - $item = WP_Post::get_instance( $id ); |
|
147 | + $item = WP_Post::get_instance($id); |
|
148 | 148 | if (!empty($item) && !empty($data['meta'])) { |
149 | 149 | $this->ID = $item->ID; |
150 | 150 | $this->save_metas($data['meta']); |
151 | 151 | } |
152 | 152 | |
153 | 153 | // Set custom id if not set. |
154 | - if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
|
155 | - $this->save_metas( array( 'custom_id' => $id ) ); |
|
154 | + if (empty($data['meta']['custom_id']) && !$this->get_custom_id()) { |
|
155 | + $this->save_metas(array('custom_id' => $id)); |
|
156 | 156 | } |
157 | 157 | |
158 | - do_action( 'wpinv_item_update', $id, $data ); |
|
158 | + do_action('wpinv_item_update', $id, $data); |
|
159 | 159 | |
160 | - return $this->setup_item( $item ); |
|
160 | + return $this->setup_item($item); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | public function get_ID() { |
@@ -165,111 +165,111 @@ discard block |
||
165 | 165 | } |
166 | 166 | |
167 | 167 | public function get_name() { |
168 | - return get_the_title( $this->ID ); |
|
168 | + return get_the_title($this->ID); |
|
169 | 169 | } |
170 | 170 | |
171 | 171 | public function get_title() { |
172 | - return get_the_title( $this->ID ); |
|
172 | + return get_the_title($this->ID); |
|
173 | 173 | } |
174 | 174 | |
175 | 175 | public function get_status() { |
176 | - return get_post_status( $this->ID ); |
|
176 | + return get_post_status($this->ID); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | public function get_summary() { |
180 | - $post = get_post( $this->ID ); |
|
181 | - return !empty( $post->post_excerpt ) ? $post->post_excerpt : ''; |
|
180 | + $post = get_post($this->ID); |
|
181 | + return !empty($post->post_excerpt) ? $post->post_excerpt : ''; |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | public function get_price() { |
185 | - if ( ! isset( $this->price ) ) { |
|
186 | - $this->price = get_post_meta( $this->ID, '_wpinv_price', true ); |
|
185 | + if (!isset($this->price)) { |
|
186 | + $this->price = get_post_meta($this->ID, '_wpinv_price', true); |
|
187 | 187 | |
188 | - if ( $this->price ) { |
|
189 | - $this->price = wpinv_sanitize_amount( $this->price ); |
|
188 | + if ($this->price) { |
|
189 | + $this->price = wpinv_sanitize_amount($this->price); |
|
190 | 190 | } else { |
191 | 191 | $this->price = 0; |
192 | 192 | } |
193 | 193 | } |
194 | 194 | |
195 | - return apply_filters( 'wpinv_get_item_price', $this->price, $this->ID ); |
|
195 | + return apply_filters('wpinv_get_item_price', $this->price, $this->ID); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | public function get_vat_rule() { |
199 | 199 | global $wpinv_euvat; |
200 | 200 | |
201 | - if( !isset( $this->vat_rule ) ) { |
|
202 | - $this->vat_rule = get_post_meta( $this->ID, '_wpinv_vat_rule', true ); |
|
201 | + if (!isset($this->vat_rule)) { |
|
202 | + $this->vat_rule = get_post_meta($this->ID, '_wpinv_vat_rule', true); |
|
203 | 203 | |
204 | - if ( empty( $this->vat_rule ) ) { |
|
204 | + if (empty($this->vat_rule)) { |
|
205 | 205 | $this->vat_rule = $wpinv_euvat->allow_vat_rules() ? 'digital' : 'physical'; |
206 | 206 | } |
207 | 207 | } |
208 | 208 | |
209 | - return apply_filters( 'wpinv_get_item_vat_rule', $this->vat_rule, $this->ID ); |
|
209 | + return apply_filters('wpinv_get_item_vat_rule', $this->vat_rule, $this->ID); |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | public function get_vat_class() { |
213 | - if( !isset( $this->vat_class ) ) { |
|
214 | - $this->vat_class = get_post_meta( $this->ID, '_wpinv_vat_class', true ); |
|
213 | + if (!isset($this->vat_class)) { |
|
214 | + $this->vat_class = get_post_meta($this->ID, '_wpinv_vat_class', true); |
|
215 | 215 | |
216 | - if ( empty( $this->vat_class ) ) { |
|
216 | + if (empty($this->vat_class)) { |
|
217 | 217 | $this->vat_class = '_standard'; |
218 | 218 | } |
219 | 219 | } |
220 | 220 | |
221 | - return apply_filters( 'wpinv_get_item_vat_class', $this->vat_class, $this->ID ); |
|
221 | + return apply_filters('wpinv_get_item_vat_class', $this->vat_class, $this->ID); |
|
222 | 222 | } |
223 | 223 | |
224 | 224 | public function get_type() { |
225 | - if( ! isset( $this->type ) ) { |
|
226 | - $this->type = get_post_meta( $this->ID, '_wpinv_type', true ); |
|
225 | + if (!isset($this->type)) { |
|
226 | + $this->type = get_post_meta($this->ID, '_wpinv_type', true); |
|
227 | 227 | |
228 | - if ( empty( $this->type ) ) { |
|
228 | + if (empty($this->type)) { |
|
229 | 229 | $this->type = 'custom'; |
230 | 230 | } |
231 | 231 | } |
232 | 232 | |
233 | - return apply_filters( 'wpinv_get_item_type', $this->type, $this->ID ); |
|
233 | + return apply_filters('wpinv_get_item_type', $this->type, $this->ID); |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | public function get_custom_id() { |
237 | - $custom_id = get_post_meta( $this->ID, '_wpinv_custom_id', true ); |
|
237 | + $custom_id = get_post_meta($this->ID, '_wpinv_custom_id', true); |
|
238 | 238 | |
239 | - return apply_filters( 'wpinv_get_item_custom_id', $custom_id, $this->ID ); |
|
239 | + return apply_filters('wpinv_get_item_custom_id', $custom_id, $this->ID); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | public function get_custom_name() { |
243 | - $custom_name = get_post_meta( $this->ID, '_wpinv_custom_name', true ); |
|
243 | + $custom_name = get_post_meta($this->ID, '_wpinv_custom_name', true); |
|
244 | 244 | |
245 | - return apply_filters( 'wpinv_get_item_custom_name', $custom_name, $this->ID ); |
|
245 | + return apply_filters('wpinv_get_item_custom_name', $custom_name, $this->ID); |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | public function get_custom_singular_name() { |
249 | - $custom_singular_name = get_post_meta( $this->ID, '_wpinv_custom_singular_name', true ); |
|
249 | + $custom_singular_name = get_post_meta($this->ID, '_wpinv_custom_singular_name', true); |
|
250 | 250 | |
251 | - return apply_filters( 'wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID ); |
|
251 | + return apply_filters('wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID); |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | public function get_editable() { |
255 | - $editable = get_post_meta( $this->ID, '_wpinv_editable', true ); |
|
255 | + $editable = get_post_meta($this->ID, '_wpinv_editable', true); |
|
256 | 256 | |
257 | - return apply_filters( 'wpinv_item_get_editable', $editable, $this->ID ); |
|
257 | + return apply_filters('wpinv_item_get_editable', $editable, $this->ID); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | public function get_excerpt() { |
261 | - $excerpt = get_the_excerpt( $this->ID ); |
|
261 | + $excerpt = get_the_excerpt($this->ID); |
|
262 | 262 | |
263 | - return apply_filters( 'wpinv_item_get_excerpt', $excerpt, $this->ID ); |
|
263 | + return apply_filters('wpinv_item_get_excerpt', $excerpt, $this->ID); |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | /** |
267 | 267 | * Checks whether the item allows a user to set their own price |
268 | 268 | */ |
269 | 269 | public function get_is_dynamic_pricing() { |
270 | - $is_dynamic_pricing = get_post_meta( $this->ID, '_wpinv_dynamic_pricing', true ); |
|
270 | + $is_dynamic_pricing = get_post_meta($this->ID, '_wpinv_dynamic_pricing', true); |
|
271 | 271 | |
272 | - return (int) apply_filters( 'wpinv_item_get_is_dynamic_pricing', $is_dynamic_pricing, $this->ID ); |
|
272 | + return (int) apply_filters('wpinv_item_get_is_dynamic_pricing', $is_dynamic_pricing, $this->ID); |
|
273 | 273 | |
274 | 274 | } |
275 | 275 | |
@@ -279,32 +279,32 @@ discard block |
||
279 | 279 | public function get_minimum_price() { |
280 | 280 | |
281 | 281 | //Fetch the minimum price and cast it to a float |
282 | - $price = (float) get_post_meta( $this->ID, '_minimum_price', true ); |
|
282 | + $price = (float) get_post_meta($this->ID, '_minimum_price', true); |
|
283 | 283 | |
284 | 284 | //Sanitize it |
285 | - $price = wpinv_sanitize_amount( $price ); |
|
285 | + $price = wpinv_sanitize_amount($price); |
|
286 | 286 | |
287 | 287 | //Filter then return it |
288 | - return apply_filters( 'wpinv_item_get_minimum_price', $price, $this->ID ); |
|
288 | + return apply_filters('wpinv_item_get_minimum_price', $price, $this->ID); |
|
289 | 289 | |
290 | 290 | } |
291 | 291 | |
292 | 292 | public function get_is_recurring() { |
293 | - $is_recurring = get_post_meta( $this->ID, '_wpinv_is_recurring', true ); |
|
293 | + $is_recurring = get_post_meta($this->ID, '_wpinv_is_recurring', true); |
|
294 | 294 | |
295 | - return apply_filters( 'wpinv_item_get_is_recurring', $is_recurring, $this->ID ); |
|
295 | + return apply_filters('wpinv_item_get_is_recurring', $is_recurring, $this->ID); |
|
296 | 296 | |
297 | 297 | } |
298 | 298 | |
299 | - public function get_recurring_period( $full = false ) { |
|
300 | - $period = get_post_meta( $this->ID, '_wpinv_recurring_period', true ); |
|
299 | + public function get_recurring_period($full = false) { |
|
300 | + $period = get_post_meta($this->ID, '_wpinv_recurring_period', true); |
|
301 | 301 | |
302 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
302 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
303 | 303 | $period = 'D'; |
304 | 304 | } |
305 | 305 | |
306 | - if ( $full ) { |
|
307 | - switch( $period ) { |
|
306 | + if ($full) { |
|
307 | + switch ($period) { |
|
308 | 308 | case 'D': |
309 | 309 | $period = 'day'; |
310 | 310 | break; |
@@ -320,40 +320,40 @@ discard block |
||
320 | 320 | } |
321 | 321 | } |
322 | 322 | |
323 | - return apply_filters( 'wpinv_item_recurring_period', $period, $full, $this->ID ); |
|
323 | + return apply_filters('wpinv_item_recurring_period', $period, $full, $this->ID); |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | public function get_recurring_interval() { |
327 | - $interval = (int)get_post_meta( $this->ID, '_wpinv_recurring_interval', true ); |
|
327 | + $interval = (int) get_post_meta($this->ID, '_wpinv_recurring_interval', true); |
|
328 | 328 | |
329 | - if ( !$interval > 0 ) { |
|
329 | + if (!$interval > 0) { |
|
330 | 330 | $interval = 1; |
331 | 331 | } |
332 | 332 | |
333 | - return apply_filters( 'wpinv_item_recurring_interval', $interval, $this->ID ); |
|
333 | + return apply_filters('wpinv_item_recurring_interval', $interval, $this->ID); |
|
334 | 334 | } |
335 | 335 | |
336 | 336 | public function get_recurring_limit() { |
337 | - $limit = get_post_meta( $this->ID, '_wpinv_recurring_limit', true ); |
|
337 | + $limit = get_post_meta($this->ID, '_wpinv_recurring_limit', true); |
|
338 | 338 | |
339 | - return (int)apply_filters( 'wpinv_item_recurring_limit', $limit, $this->ID ); |
|
339 | + return (int) apply_filters('wpinv_item_recurring_limit', $limit, $this->ID); |
|
340 | 340 | } |
341 | 341 | |
342 | 342 | public function get_free_trial() { |
343 | - $free_trial = get_post_meta( $this->ID, '_wpinv_free_trial', true ); |
|
343 | + $free_trial = get_post_meta($this->ID, '_wpinv_free_trial', true); |
|
344 | 344 | |
345 | - return apply_filters( 'wpinv_item_get_free_trial', $free_trial, $this->ID ); |
|
345 | + return apply_filters('wpinv_item_get_free_trial', $free_trial, $this->ID); |
|
346 | 346 | } |
347 | 347 | |
348 | - public function get_trial_period( $full = false ) { |
|
349 | - $period = get_post_meta( $this->ID, '_wpinv_trial_period', true ); |
|
348 | + public function get_trial_period($full = false) { |
|
349 | + $period = get_post_meta($this->ID, '_wpinv_trial_period', true); |
|
350 | 350 | |
351 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
351 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
352 | 352 | $period = 'D'; |
353 | 353 | } |
354 | 354 | |
355 | - if ( $full ) { |
|
356 | - switch( $period ) { |
|
355 | + if ($full) { |
|
356 | + switch ($period) { |
|
357 | 357 | case 'D': |
358 | 358 | $period = 'day'; |
359 | 359 | break; |
@@ -369,47 +369,47 @@ discard block |
||
369 | 369 | } |
370 | 370 | } |
371 | 371 | |
372 | - return apply_filters( 'wpinv_item_trial_period', $period, $full, $this->ID ); |
|
372 | + return apply_filters('wpinv_item_trial_period', $period, $full, $this->ID); |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | public function get_trial_interval() { |
376 | - $interval = absint( get_post_meta( $this->ID, '_wpinv_trial_interval', true ) ); |
|
376 | + $interval = absint(get_post_meta($this->ID, '_wpinv_trial_interval', true)); |
|
377 | 377 | |
378 | - if ( !$interval > 0 ) { |
|
378 | + if (!$interval > 0) { |
|
379 | 379 | $interval = 1; |
380 | 380 | } |
381 | 381 | |
382 | - return apply_filters( 'wpinv_item_trial_interval', $interval, $this->ID ); |
|
382 | + return apply_filters('wpinv_item_trial_interval', $interval, $this->ID); |
|
383 | 383 | } |
384 | 384 | |
385 | 385 | public function get_the_price() { |
386 | - $item_price = wpinv_price( wpinv_format_amount( $this->get_price() ) ); |
|
386 | + $item_price = wpinv_price(wpinv_format_amount($this->get_price())); |
|
387 | 387 | |
388 | - return apply_filters( 'wpinv_get_the_item_price', $item_price, $this->ID ); |
|
388 | + return apply_filters('wpinv_get_the_item_price', $item_price, $this->ID); |
|
389 | 389 | } |
390 | 390 | |
391 | 391 | public function is_recurring() { |
392 | 392 | $is_recurring = $this->get_is_recurring(); |
393 | 393 | |
394 | - return (bool)apply_filters( 'wpinv_is_recurring_item', $is_recurring, $this->ID ); |
|
394 | + return (bool) apply_filters('wpinv_is_recurring_item', $is_recurring, $this->ID); |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | public function has_free_trial() { |
398 | 398 | $free_trial = $this->is_recurring() && $this->get_free_trial() ? true : false; |
399 | 399 | |
400 | - return (bool)apply_filters( 'wpinv_item_has_free_trial', $free_trial, $this->ID ); |
|
400 | + return (bool) apply_filters('wpinv_item_has_free_trial', $free_trial, $this->ID); |
|
401 | 401 | } |
402 | 402 | |
403 | 403 | public function is_free() { |
404 | 404 | $is_free = false; |
405 | 405 | |
406 | - $price = get_post_meta( $this->ID, '_wpinv_price', true ); |
|
406 | + $price = get_post_meta($this->ID, '_wpinv_price', true); |
|
407 | 407 | |
408 | - if ( (float)$price == 0 ) { |
|
408 | + if ((float) $price == 0) { |
|
409 | 409 | $is_free = true; |
410 | 410 | } |
411 | 411 | |
412 | - return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID ); |
|
412 | + return (bool) apply_filters('wpinv_is_free_item', $is_free, $this->ID); |
|
413 | 413 | |
414 | 414 | } |
415 | 415 | |
@@ -418,15 +418,15 @@ discard block |
||
418 | 418 | |
419 | 419 | $is_editable = $editable === 0 || $editable === '0' ? false : true; |
420 | 420 | |
421 | - return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID ); |
|
421 | + return (bool) apply_filters('wpinv_item_is_editable', $is_editable, $this->ID); |
|
422 | 422 | } |
423 | 423 | |
424 | - public function save_metas( $metas = array() ) { |
|
425 | - if ( empty( $metas ) ) { |
|
424 | + public function save_metas($metas = array()) { |
|
425 | + if (empty($metas)) { |
|
426 | 426 | return false; |
427 | 427 | } |
428 | 428 | |
429 | - foreach ( $metas as $meta_key => $meta_value ) { |
|
429 | + foreach ($metas as $meta_key => $meta_value) { |
|
430 | 430 | $meta_key = strpos($meta_key, '_wpinv_') !== 0 ? '_wpinv_' . $meta_key : $meta_key; |
431 | 431 | |
432 | 432 | $this->update_meta($meta_key, $meta_value); |
@@ -435,77 +435,77 @@ discard block |
||
435 | 435 | return true; |
436 | 436 | } |
437 | 437 | |
438 | - public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) { |
|
439 | - if ( empty( $meta_key ) ) { |
|
438 | + public function update_meta($meta_key = '', $meta_value = '', $prev_value = '') { |
|
439 | + if (empty($meta_key)) { |
|
440 | 440 | return false; |
441 | 441 | } |
442 | 442 | |
443 | - if( '_wpinv_minimum_price' === $meta_key ) { |
|
443 | + if ('_wpinv_minimum_price' === $meta_key) { |
|
444 | 444 | $meta_key = '_minimum_price'; |
445 | 445 | } |
446 | 446 | |
447 | - $meta_value = apply_filters( 'wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID ); |
|
447 | + $meta_value = apply_filters('wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID); |
|
448 | 448 | |
449 | - return update_post_meta( $this->ID, $meta_key, $meta_value, $prev_value ); |
|
449 | + return update_post_meta($this->ID, $meta_key, $meta_value, $prev_value); |
|
450 | 450 | } |
451 | 451 | |
452 | - public function get_fees( $type = 'fee', $item_id = 0 ) { |
|
452 | + public function get_fees($type = 'fee', $item_id = 0) { |
|
453 | 453 | global $wpi_session; |
454 | 454 | |
455 | - $fees = $wpi_session->get( 'wpi_cart_fees' ); |
|
455 | + $fees = $wpi_session->get('wpi_cart_fees'); |
|
456 | 456 | |
457 | - if ( ! wpinv_get_cart_contents() ) { |
|
457 | + if (!wpinv_get_cart_contents()) { |
|
458 | 458 | // We can only get item type fees when the cart is empty |
459 | 459 | $type = 'custom'; |
460 | 460 | } |
461 | 461 | |
462 | - if ( ! empty( $fees ) && ! empty( $type ) && 'all' !== $type ) { |
|
463 | - foreach( $fees as $key => $fee ) { |
|
464 | - if( ! empty( $fee['type'] ) && $type != $fee['type'] ) { |
|
465 | - unset( $fees[ $key ] ); |
|
462 | + if (!empty($fees) && !empty($type) && 'all' !== $type) { |
|
463 | + foreach ($fees as $key => $fee) { |
|
464 | + if (!empty($fee['type']) && $type != $fee['type']) { |
|
465 | + unset($fees[$key]); |
|
466 | 466 | } |
467 | 467 | } |
468 | 468 | } |
469 | 469 | |
470 | - if ( ! empty( $fees ) && ! empty( $item_id ) ) { |
|
470 | + if (!empty($fees) && !empty($item_id)) { |
|
471 | 471 | // Remove fees that don't belong to the specified Item |
472 | - foreach ( $fees as $key => $fee ) { |
|
473 | - if ( (int) $item_id !== (int)$fee['custom_id'] ) { |
|
474 | - unset( $fees[ $key ] ); |
|
472 | + foreach ($fees as $key => $fee) { |
|
473 | + if ((int) $item_id !== (int) $fee['custom_id']) { |
|
474 | + unset($fees[$key]); |
|
475 | 475 | } |
476 | 476 | } |
477 | 477 | } |
478 | 478 | |
479 | - if ( ! empty( $fees ) ) { |
|
479 | + if (!empty($fees)) { |
|
480 | 480 | // Remove fees that belong to a specific item but are not in the cart |
481 | - foreach( $fees as $key => $fee ) { |
|
482 | - if( empty( $fee['custom_id'] ) ) { |
|
481 | + foreach ($fees as $key => $fee) { |
|
482 | + if (empty($fee['custom_id'])) { |
|
483 | 483 | continue; |
484 | 484 | } |
485 | 485 | |
486 | - if ( !wpinv_item_in_cart( $fee['custom_id'] ) ) { |
|
487 | - unset( $fees[ $key ] ); |
|
486 | + if (!wpinv_item_in_cart($fee['custom_id'])) { |
|
487 | + unset($fees[$key]); |
|
488 | 488 | } |
489 | 489 | } |
490 | 490 | } |
491 | 491 | |
492 | - return ! empty( $fees ) ? $fees : array(); |
|
492 | + return !empty($fees) ? $fees : array(); |
|
493 | 493 | } |
494 | 494 | |
495 | 495 | public function can_purchase() { |
496 | 496 | $can_purchase = true; |
497 | 497 | |
498 | - if ( !current_user_can( 'edit_post', $this->ID ) && $this->post_status != 'publish' ) { |
|
498 | + if (!current_user_can('edit_post', $this->ID) && $this->post_status != 'publish') { |
|
499 | 499 | $can_purchase = false; |
500 | 500 | } |
501 | 501 | |
502 | - return (bool)apply_filters( 'wpinv_can_purchase_item', $can_purchase, $this ); |
|
502 | + return (bool) apply_filters('wpinv_can_purchase_item', $can_purchase, $this); |
|
503 | 503 | } |
504 | 504 | |
505 | 505 | /** |
506 | 506 | * Checks whether this item supports dynamic pricing or not |
507 | 507 | */ |
508 | 508 | public function supports_dynamic_pricing() { |
509 | - return (bool) apply_filters( 'wpinv_item_supports_dynamic_pricing', true, $this ); |
|
509 | + return (bool) apply_filters('wpinv_item_supports_dynamic_pricing', true, $this); |
|
510 | 510 | } |
511 | 511 | } |