Passed
Pull Request — master (#250)
by
unknown
05:04
created
includes/wpinv-discount-functions.php 1 patch
Spacing   +480 added lines, -480 removed lines patch added patch discarded remove patch
@@ -7,110 +7,110 @@  discard block
 block discarded – undo
7 7
  */
8 8
  
9 9
 // MUST have WordPress.
10
-if ( !defined( 'WPINC' ) ) {
11
-    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
10
+if (!defined('WPINC')) {
11
+    exit('Do NOT access this file directly: ' . basename(__FILE__));
12 12
 }
13 13
 
14 14
 function wpinv_get_discount_types() {
15 15
     $discount_types = array(
16
-                        'percent'   => __( 'Percentage', 'invoicing' ),
17
-                        'flat'     => __( 'Flat Amount', 'invoicing' ),
16
+                        'percent'   => __('Percentage', 'invoicing'),
17
+                        'flat'     => __('Flat Amount', 'invoicing'),
18 18
                     );
19
-    return (array)apply_filters( 'wpinv_discount_types', $discount_types );
19
+    return (array) apply_filters('wpinv_discount_types', $discount_types);
20 20
 }
21 21
 
22
-function wpinv_get_discount_type_name( $type = '' ) {
22
+function wpinv_get_discount_type_name($type = '') {
23 23
     $types = wpinv_get_discount_types();
24
-    return isset( $types[ $type ] ) ? $types[ $type ] : '';
24
+    return isset($types[$type]) ? $types[$type] : '';
25 25
 }
26 26
 
27
-function wpinv_delete_discount( $data ) {
28
-    if ( ! isset( $data['_wpnonce'] ) || ! wp_verify_nonce( $data['_wpnonce'], 'wpinv_discount_nonce' ) ) {
29
-        wp_die( __( 'Trying to cheat or something?', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
27
+function wpinv_delete_discount($data) {
28
+    if (!isset($data['_wpnonce']) || !wp_verify_nonce($data['_wpnonce'], 'wpinv_discount_nonce')) {
29
+        wp_die(__('Trying to cheat or something?', 'invoicing'), __('Error', 'invoicing'), array('response' => 403));
30 30
     }
31 31
 
32
-    if( ! wpinv_current_user_can_manage_invoicing() ) {
33
-        wp_die( __( 'You do not have permission to delete discount codes', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
32
+    if (!wpinv_current_user_can_manage_invoicing()) {
33
+        wp_die(__('You do not have permission to delete discount codes', 'invoicing'), __('Error', 'invoicing'), array('response' => 403));
34 34
     }
35 35
 
36 36
     $discount_id = $data['discount'];
37
-    wpinv_remove_discount( $discount_id );
37
+    wpinv_remove_discount($discount_id);
38 38
 }
39
-add_action( 'wpinv_delete_discount', 'wpinv_delete_discount' );
39
+add_action('wpinv_delete_discount', 'wpinv_delete_discount');
40 40
 
41
-function wpinv_activate_discount( $data ) {
42
-    if ( ! isset( $data['_wpnonce'] ) || ! wp_verify_nonce( $data['_wpnonce'], 'wpinv_discount_nonce' ) ) {
43
-        wp_die( __( 'Trying to cheat or something?', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
41
+function wpinv_activate_discount($data) {
42
+    if (!isset($data['_wpnonce']) || !wp_verify_nonce($data['_wpnonce'], 'wpinv_discount_nonce')) {
43
+        wp_die(__('Trying to cheat or something?', 'invoicing'), __('Error', 'invoicing'), array('response' => 403));
44 44
     }
45 45
 
46
-    if( ! wpinv_current_user_can_manage_invoicing() ) {
47
-        wp_die( __( 'You do not have permission to edit discount codes', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
46
+    if (!wpinv_current_user_can_manage_invoicing()) {
47
+        wp_die(__('You do not have permission to edit discount codes', 'invoicing'), __('Error', 'invoicing'), array('response' => 403));
48 48
     }
49 49
 
50
-    $id = absint( $data['discount'] );
51
-    wpinv_update_discount_status( $id, 'publish' );
50
+    $id = absint($data['discount']);
51
+    wpinv_update_discount_status($id, 'publish');
52 52
 }
53
-add_action( 'wpinv_activate_discount', 'wpinv_activate_discount' );
53
+add_action('wpinv_activate_discount', 'wpinv_activate_discount');
54 54
 
55
-function wpinv_deactivate_discount( $data ) {
56
-    if ( ! isset( $data['_wpnonce'] ) || ! wp_verify_nonce( $data['_wpnonce'], 'wpinv_discount_nonce' ) ) {
57
-        wp_die( __( 'Trying to cheat or something?', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
55
+function wpinv_deactivate_discount($data) {
56
+    if (!isset($data['_wpnonce']) || !wp_verify_nonce($data['_wpnonce'], 'wpinv_discount_nonce')) {
57
+        wp_die(__('Trying to cheat or something?', 'invoicing'), __('Error', 'invoicing'), array('response' => 403));
58 58
     }
59 59
 
60
-    if( ! wpinv_current_user_can_manage_invoicing() ) {
61
-        wp_die( __( 'You do not have permission to create discount codes', 'invoicing' ), array( 'response' => 403 ) );
60
+    if (!wpinv_current_user_can_manage_invoicing()) {
61
+        wp_die(__('You do not have permission to create discount codes', 'invoicing'), array('response' => 403));
62 62
     }
63 63
 
64
-    $id = absint( $data['discount'] );
65
-    wpinv_update_discount_status( $id, 'pending' );
64
+    $id = absint($data['discount']);
65
+    wpinv_update_discount_status($id, 'pending');
66 66
 }
67
-add_action( 'wpinv_deactivate_discount', 'wpinv_deactivate_discount' );
67
+add_action('wpinv_deactivate_discount', 'wpinv_deactivate_discount');
68 68
 
69
-function wpinv_get_discounts( $args = array() ) {
69
+function wpinv_get_discounts($args = array()) {
70 70
     $defaults = array(
71 71
         'post_type'      => 'wpi_discount',
72 72
         'posts_per_page' => 20,
73 73
         'paged'          => null,
74
-        'post_status'    => array( 'publish', 'pending', 'draft', 'expired' )
74
+        'post_status'    => array('publish', 'pending', 'draft', 'expired')
75 75
     );
76 76
 
77
-    $args = wp_parse_args( $args, $defaults );
77
+    $args = wp_parse_args($args, $defaults);
78 78
 
79
-    $discounts = get_posts( $args );
79
+    $discounts = get_posts($args);
80 80
 
81
-    if ( $discounts ) {
81
+    if ($discounts) {
82 82
         return $discounts;
83 83
     }
84 84
 
85
-    if( ! $discounts && ! empty( $args['s'] ) ) {
85
+    if (!$discounts && !empty($args['s'])) {
86 86
         $args['meta_key']     = '_wpi_discount_code';
87 87
         $args['meta_value']   = $args['s'];
88 88
         $args['meta_compare'] = 'LIKE';
89
-        unset( $args['s'] );
90
-        $discounts = get_posts( $args );
89
+        unset($args['s']);
90
+        $discounts = get_posts($args);
91 91
     }
92 92
 
93
-    if( $discounts ) {
93
+    if ($discounts) {
94 94
         return $discounts;
95 95
     }
96 96
 
97 97
     return false;
98 98
 }
99 99
 
100
-function wpinv_get_all_discounts( $args = array() ) {
100
+function wpinv_get_all_discounts($args = array()) {
101 101
 
102
-    $args = wp_parse_args( $args, array(
103
-        'status'         => array( 'publish' ),
104
-        'limit'          => get_option( 'posts_per_page' ),
102
+    $args = wp_parse_args($args, array(
103
+        'status'         => array('publish'),
104
+        'limit'          => get_option('posts_per_page'),
105 105
         'page'           => 1,
106 106
         'exclude'        => array(),
107 107
         'orderby'        => 'date',
108 108
         'order'          => 'DESC',
109
-        'type'           => array_keys( wpinv_get_discount_types() ),
109
+        'type'           => array_keys(wpinv_get_discount_types()),
110 110
         'meta_query'     => array(),
111 111
         'return'         => 'objects',
112 112
         'paginate'       => false,
113
-    ) );
113
+    ));
114 114
 
115 115
     $wp_query_args = array(
116 116
         'post_type'      => 'wpi_discount',
@@ -120,18 +120,18 @@  discard block
 block discarded – undo
120 120
         'fields'         => 'ids',
121 121
         'orderby'        => $args['orderby'],
122 122
         'order'          => $args['order'],
123
-        'paged'          => absint( $args['page'] ),
123
+        'paged'          => absint($args['page']),
124 124
     );
125 125
 
126
-    if ( ! empty( $args['exclude'] ) ) {
127
-        $wp_query_args['post__not_in'] = array_map( 'absint', $args['exclude'] );
126
+    if (!empty($args['exclude'])) {
127
+        $wp_query_args['post__not_in'] = array_map('absint', $args['exclude']);
128 128
     }
129 129
 
130
-    if ( ! $args['paginate' ] ) {
130
+    if (!$args['paginate']) {
131 131
         $wp_query_args['no_found_rows'] = true;
132 132
     }
133 133
 
134
-    if ( ! empty( $args['search'] ) ) {
134
+    if (!empty($args['search'])) {
135 135
 
136 136
         $wp_query_args['meta_query'][] = array(
137 137
             'key'     => '_wpi_discount_code',
@@ -141,11 +141,11 @@  discard block
 block discarded – undo
141 141
 
142 142
     }
143 143
     
144
-    if ( ! empty( $args['type'] ) ) {
145
-        $types = wpinv_parse_list( $args['type'] );
144
+    if (!empty($args['type'])) {
145
+        $types = wpinv_parse_list($args['type']);
146 146
         $wp_query_args['meta_query'][] = array(
147 147
             'key'     => '_wpi_discount_type',
148
-            'value'   => implode( ',', $types ),
148
+            'value'   => implode(',', $types),
149 149
             'compare' => 'IN',
150 150
         );
151 151
     }
@@ -153,17 +153,17 @@  discard block
 block discarded – undo
153 153
     $wp_query_args = apply_filters('wpinv_get_discount_args', $wp_query_args, $args);
154 154
 
155 155
     // Get results.
156
-    $discounts = new WP_Query( $wp_query_args );
156
+    $discounts = new WP_Query($wp_query_args);
157 157
 
158
-    if ( 'objects' === $args['return'] ) {
159
-        $return = array_map( 'get_post', $discounts->posts );
160
-    } elseif ( 'self' === $args['return'] ) {
158
+    if ('objects' === $args['return']) {
159
+        $return = array_map('get_post', $discounts->posts);
160
+    } elseif ('self' === $args['return']) {
161 161
         return $discounts;
162 162
     } else {
163 163
         $return = $discounts->posts;
164 164
     }
165 165
 
166
-    if ( $args['paginate' ] ) {
166
+    if ($args['paginate']) {
167 167
         return (object) array(
168 168
             'discounts'      => $return,
169 169
             'total'         => $discounts->found_posts,
@@ -180,9 +180,9 @@  discard block
 block discarded – undo
180 180
 
181 181
     $discounts  = wpinv_get_discounts();
182 182
 
183
-    if ( $discounts) {
184
-        foreach ( $discounts as $discount ) {
185
-            if ( wpinv_is_discount_active( $discount->ID ) ) {
183
+    if ($discounts) {
184
+        foreach ($discounts as $discount) {
185
+            if (wpinv_is_discount_active($discount->ID)) {
186 186
                 $has_active = true;
187 187
                 break;
188 188
             }
@@ -191,38 +191,38 @@  discard block
 block discarded – undo
191 191
     return $has_active;
192 192
 }
193 193
 
194
-function wpinv_get_discount( $discount_id = 0 ) {
195
-    if( empty( $discount_id ) ) {
194
+function wpinv_get_discount($discount_id = 0) {
195
+    if (empty($discount_id)) {
196 196
         return false;
197 197
     }
198 198
     
199
-    if ( get_post_type( $discount_id ) != 'wpi_discount' ) {
199
+    if (get_post_type($discount_id) != 'wpi_discount') {
200 200
         return false;
201 201
     }
202 202
 
203
-    $discount = get_post( $discount_id );
203
+    $discount = get_post($discount_id);
204 204
 
205 205
     return $discount;
206 206
 }
207 207
 
208
-function wpinv_get_discount_by_code( $code = '' ) {
209
-    if( empty( $code ) || ! is_string( $code ) ) {
208
+function wpinv_get_discount_by_code($code = '') {
209
+    if (empty($code) || !is_string($code)) {
210 210
         return false;
211 211
     }
212 212
 
213
-    return wpinv_get_discount_by( 'code', $code );
213
+    return wpinv_get_discount_by('code', $code);
214 214
 }
215 215
 
216
-function wpinv_get_discount_by( $field = '', $value = '' ) {
217
-    if( empty( $field ) || empty( $value ) ) {
216
+function wpinv_get_discount_by($field = '', $value = '') {
217
+    if (empty($field) || empty($value)) {
218 218
         return false;
219 219
     }
220 220
 
221
-    if( ! is_string( $field ) ) {
221
+    if (!is_string($field)) {
222 222
         return false;
223 223
     }
224 224
 
225
-    switch( strtolower( $field ) ) {
225
+    switch (strtolower($field)) {
226 226
 
227 227
         case 'code':
228 228
             $meta_query     = array();
@@ -232,32 +232,32 @@  discard block
 block discarded – undo
232 232
                 'compare' => '='
233 233
             );
234 234
             
235
-            $discount = wpinv_get_discounts( array(
235
+            $discount = wpinv_get_discounts(array(
236 236
                 'posts_per_page' => 1,
237 237
                 'post_status'    => 'any',
238 238
                 'meta_query'     => $meta_query,
239
-            ) );
239
+            ));
240 240
             
241
-            if( $discount ) {
241
+            if ($discount) {
242 242
                 $discount = $discount[0];
243 243
             }
244 244
 
245 245
             break;
246 246
 
247 247
         case 'id':
248
-            $discount = wpinv_get_discount( $value );
248
+            $discount = wpinv_get_discount($value);
249 249
 
250 250
             break;
251 251
 
252 252
         case 'name':
253
-            $discount = get_posts( array(
253
+            $discount = get_posts(array(
254 254
                 'post_type'      => 'wpi_discount',
255 255
                 'name'           => $value,
256 256
                 'posts_per_page' => 1,
257 257
                 'post_status'    => 'any'
258
-            ) );
258
+            ));
259 259
 
260
-            if( $discount ) {
260
+            if ($discount) {
261 261
                 $discount = $discount[0];
262 262
             }
263 263
 
@@ -267,96 +267,96 @@  discard block
 block discarded – undo
267 267
             return false;
268 268
     }
269 269
 
270
-    if( ! empty( $discount ) ) {
270
+    if (!empty($discount)) {
271 271
         return $discount;
272 272
     }
273 273
 
274 274
     return false;
275 275
 }
276 276
 
277
-function wpinv_store_discount( $post_id, $data, $post, $update = false ) {
277
+function wpinv_store_discount($post_id, $data, $post, $update = false) {
278 278
     $meta = array(
279
-        'code'              => isset( $data['code'] )             ? sanitize_text_field( $data['code'] )              : '',
280
-        'type'              => isset( $data['type'] )             ? sanitize_text_field( $data['type'] )              : 'percent',
281
-        'amount'            => isset( $data['amount'] )           ? wpinv_sanitize_amount( $data['amount'] )          : '',
282
-        'start'             => isset( $data['start'] )            ? sanitize_text_field( $data['start'] )             : '',
283
-        'expiration'        => isset( $data['expiration'] )       ? sanitize_text_field( $data['expiration'] )        : '',
284
-        'min_total'         => isset( $data['min_total'] )        ? wpinv_sanitize_amount( $data['min_total'] )       : '',
285
-        'max_total'         => isset( $data['max_total'] )        ? wpinv_sanitize_amount( $data['max_total'] )       : '',
286
-        'max_uses'          => isset( $data['max_uses'] )         ? absint( $data['max_uses'] )                       : '',
287
-        'items'             => isset( $data['items'] )            ? $data['items']                                    : array(),
288
-        'excluded_items'    => isset( $data['excluded_items'] )   ? $data['excluded_items']                           : array(),
289
-        'is_recurring'      => isset( $data['recurring'] )        ? (bool)$data['recurring']                          : false,
290
-        'is_single_use'     => isset( $data['single_use'] )       ? (bool)$data['single_use']                         : false,
291
-        'uses'              => isset( $data['uses'] )             ? (int)$data['uses']                                : false,
279
+        'code'              => isset($data['code']) ? sanitize_text_field($data['code']) : '',
280
+        'type'              => isset($data['type']) ? sanitize_text_field($data['type']) : 'percent',
281
+        'amount'            => isset($data['amount']) ? wpinv_sanitize_amount($data['amount']) : '',
282
+        'start'             => isset($data['start']) ? sanitize_text_field($data['start']) : '',
283
+        'expiration'        => isset($data['expiration']) ? sanitize_text_field($data['expiration']) : '',
284
+        'min_total'         => isset($data['min_total']) ? wpinv_sanitize_amount($data['min_total']) : '',
285
+        'max_total'         => isset($data['max_total']) ? wpinv_sanitize_amount($data['max_total']) : '',
286
+        'max_uses'          => isset($data['max_uses']) ? absint($data['max_uses']) : '',
287
+        'items'             => isset($data['items']) ? $data['items'] : array(),
288
+        'excluded_items'    => isset($data['excluded_items']) ? $data['excluded_items'] : array(),
289
+        'is_recurring'      => isset($data['recurring']) ? (bool) $data['recurring'] : false,
290
+        'is_single_use'     => isset($data['single_use']) ? (bool) $data['single_use'] : false,
291
+        'uses'              => isset($data['uses']) ? (int) $data['uses'] : false,
292 292
     );
293 293
 
294
-    if ( $meta['type'] == 'percent' && (float)$meta['amount'] > 100 ) {
294
+    if ($meta['type'] == 'percent' && (float) $meta['amount'] > 100) {
295 295
         $meta['amount'] = 100;
296 296
     }
297 297
 
298
-    if ( !empty( $meta['start'] ) ) {
299
-        $meta['start']      = date_i18n( 'Y-m-d H:i:s', strtotime( $meta['start'] ) );
298
+    if (!empty($meta['start'])) {
299
+        $meta['start'] = date_i18n('Y-m-d H:i:s', strtotime($meta['start']));
300 300
     }
301 301
 
302
-    if ( !empty( $meta['expiration'] ) ) {
303
-        $meta['expiration'] = date_i18n( 'Y-m-d H:i:s', strtotime( $meta['expiration'] ) );
302
+    if (!empty($meta['expiration'])) {
303
+        $meta['expiration'] = date_i18n('Y-m-d H:i:s', strtotime($meta['expiration']));
304 304
 
305
-        if ( !empty( $meta['start'] ) && strtotime( $meta['start'] ) > strtotime( $meta['expiration'] ) ) {
305
+        if (!empty($meta['start']) && strtotime($meta['start']) > strtotime($meta['expiration'])) {
306 306
             $meta['expiration'] = $meta['start'];
307 307
         }
308 308
     }
309 309
     
310
-    if ( $meta['uses'] === false ) {
311
-        unset( $meta['uses'] );
310
+    if ($meta['uses'] === false) {
311
+        unset($meta['uses']);
312 312
     }
313 313
     
314
-    if ( ! empty( $meta['items'] ) ) {
315
-        foreach ( $meta['items'] as $key => $item ) {
316
-            if ( 0 === intval( $item ) ) {
317
-                unset( $meta['items'][ $key ] );
314
+    if (!empty($meta['items'])) {
315
+        foreach ($meta['items'] as $key => $item) {
316
+            if (0 === intval($item)) {
317
+                unset($meta['items'][$key]);
318 318
             }
319 319
         }
320 320
     }
321 321
     
322
-    if ( ! empty( $meta['excluded_items'] ) ) {
323
-        foreach ( $meta['excluded_items'] as $key => $item ) {
324
-            if ( 0 === intval( $item ) ) {
325
-                unset( $meta['excluded_items'][ $key ] );
322
+    if (!empty($meta['excluded_items'])) {
323
+        foreach ($meta['excluded_items'] as $key => $item) {
324
+            if (0 === intval($item)) {
325
+                unset($meta['excluded_items'][$key]);
326 326
             }
327 327
         }
328 328
     }
329 329
     
330
-    $meta = apply_filters( 'wpinv_update_discount', $meta, $post_id, $post );
330
+    $meta = apply_filters('wpinv_update_discount', $meta, $post_id, $post);
331 331
     
332
-    do_action( 'wpinv_pre_update_discount', $meta, $post_id, $post );
332
+    do_action('wpinv_pre_update_discount', $meta, $post_id, $post);
333 333
     
334
-    foreach( $meta as $key => $value ) {
335
-        update_post_meta( $post_id, '_wpi_discount_' . $key, $value );
334
+    foreach ($meta as $key => $value) {
335
+        update_post_meta($post_id, '_wpi_discount_' . $key, $value);
336 336
     }
337 337
     
338
-    do_action( 'wpinv_post_update_discount', $meta, $post_id, $post );
338
+    do_action('wpinv_post_update_discount', $meta, $post_id, $post);
339 339
     
340 340
     return $post_id;
341 341
 }
342 342
 
343
-function wpinv_remove_discount( $discount_id = 0 ) {
344
-    do_action( 'wpinv_pre_delete_discount', $discount_id );
343
+function wpinv_remove_discount($discount_id = 0) {
344
+    do_action('wpinv_pre_delete_discount', $discount_id);
345 345
 
346
-    wp_delete_post( $discount_id, true );
346
+    wp_delete_post($discount_id, true);
347 347
 
348
-    do_action( 'wpinv_post_delete_discount', $discount_id );
348
+    do_action('wpinv_post_delete_discount', $discount_id);
349 349
 }
350 350
 
351
-function wpinv_update_discount_status( $code_id = 0, $new_status = 'publish' ) {
352
-    $discount = wpinv_get_discount(  $code_id );
351
+function wpinv_update_discount_status($code_id = 0, $new_status = 'publish') {
352
+    $discount = wpinv_get_discount($code_id);
353 353
 
354
-    if ( $discount ) {
355
-        do_action( 'wpinv_pre_update_discount_status', $code_id, $new_status, $discount->post_status );
354
+    if ($discount) {
355
+        do_action('wpinv_pre_update_discount_status', $code_id, $new_status, $discount->post_status);
356 356
 
357
-        wp_update_post( array( 'ID' => $code_id, 'post_status' => $new_status ) );
357
+        wp_update_post(array('ID' => $code_id, 'post_status' => $new_status));
358 358
 
359
-        do_action( 'wpinv_post_update_discount_status', $code_id, $new_status, $discount->post_status );
359
+        do_action('wpinv_post_update_discount_status', $code_id, $new_status, $discount->post_status);
360 360
 
361 361
         return true;
362 362
     }
@@ -364,173 +364,173 @@  discard block
 block discarded – undo
364 364
     return false;
365 365
 }
366 366
 
367
-function wpinv_discount_exists( $code_id ) {
368
-    if ( wpinv_get_discount(  $code_id ) ) {
367
+function wpinv_discount_exists($code_id) {
368
+    if (wpinv_get_discount($code_id)) {
369 369
         return true;
370 370
     }
371 371
 
372 372
     return false;
373 373
 }
374 374
 
375
-function wpinv_is_discount_active( $code_id = null ) {
376
-    $discount = wpinv_get_discount(  $code_id );
375
+function wpinv_is_discount_active($code_id = null) {
376
+    $discount = wpinv_get_discount($code_id);
377 377
     $return   = false;
378 378
 
379
-    if ( $discount ) {
380
-        if ( wpinv_is_discount_expired( $code_id ) ) {
381
-            if( defined( 'DOING_AJAX' ) ) {
382
-                wpinv_set_error( 'wpinv-discount-error', __( 'This discount is expired.', 'invoicing' ) );
379
+    if ($discount) {
380
+        if (wpinv_is_discount_expired($code_id)) {
381
+            if (defined('DOING_AJAX')) {
382
+                wpinv_set_error('wpinv-discount-error', __('This discount is expired.', 'invoicing'));
383 383
             }
384
-        } elseif ( $discount->post_status == 'publish' ) {
384
+        } elseif ($discount->post_status == 'publish') {
385 385
             $return = true;
386 386
         } else {
387
-            if( defined( 'DOING_AJAX' ) ) {
388
-                wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active.', 'invoicing' ) );
387
+            if (defined('DOING_AJAX')) {
388
+                wpinv_set_error('wpinv-discount-error', __('This discount is not active.', 'invoicing'));
389 389
             }
390 390
         }
391 391
     }
392 392
 
393
-    return apply_filters( 'wpinv_is_discount_active', $return, $code_id );
393
+    return apply_filters('wpinv_is_discount_active', $return, $code_id);
394 394
 }
395 395
 
396
-function wpinv_get_discount_code( $code_id = null ) {
397
-    $code = get_post_meta( $code_id, '_wpi_discount_code', true );
396
+function wpinv_get_discount_code($code_id = null) {
397
+    $code = get_post_meta($code_id, '_wpi_discount_code', true);
398 398
 
399
-    return apply_filters( 'wpinv_get_discount_code', $code, $code_id );
399
+    return apply_filters('wpinv_get_discount_code', $code, $code_id);
400 400
 }
401 401
 
402
-function wpinv_get_discount_start_date( $code_id = null ) {
403
-    $start_date = get_post_meta( $code_id, '_wpi_discount_start', true );
402
+function wpinv_get_discount_start_date($code_id = null) {
403
+    $start_date = get_post_meta($code_id, '_wpi_discount_start', true);
404 404
 
405
-    return apply_filters( 'wpinv_get_discount_start_date', $start_date, $code_id );
405
+    return apply_filters('wpinv_get_discount_start_date', $start_date, $code_id);
406 406
 }
407 407
 
408
-function wpinv_get_discount_expiration( $code_id = null ) {
409
-    $expiration = get_post_meta( $code_id, '_wpi_discount_expiration', true );
408
+function wpinv_get_discount_expiration($code_id = null) {
409
+    $expiration = get_post_meta($code_id, '_wpi_discount_expiration', true);
410 410
 
411
-    return apply_filters( 'wpinv_get_discount_expiration', $expiration, $code_id );
411
+    return apply_filters('wpinv_get_discount_expiration', $expiration, $code_id);
412 412
 }
413 413
 
414
-function wpinv_get_discount_max_uses( $code_id = null ) {
415
-    $max_uses = get_post_meta( $code_id, '_wpi_discount_max_uses', true );
414
+function wpinv_get_discount_max_uses($code_id = null) {
415
+    $max_uses = get_post_meta($code_id, '_wpi_discount_max_uses', true);
416 416
 
417
-    return (int) apply_filters( 'wpinv_get_discount_max_uses', $max_uses, $code_id );
417
+    return (int) apply_filters('wpinv_get_discount_max_uses', $max_uses, $code_id);
418 418
 }
419 419
 
420
-function wpinv_get_discount_uses( $code_id = null ) {
421
-    $uses = get_post_meta( $code_id, '_wpi_discount_uses', true );
420
+function wpinv_get_discount_uses($code_id = null) {
421
+    $uses = get_post_meta($code_id, '_wpi_discount_uses', true);
422 422
 
423
-    return (int) apply_filters( 'wpinv_get_discount_uses', $uses, $code_id );
423
+    return (int) apply_filters('wpinv_get_discount_uses', $uses, $code_id);
424 424
 }
425 425
 
426
-function wpinv_get_discount_min_total( $code_id = null ) {
427
-    $min_total = get_post_meta( $code_id, '_wpi_discount_min_total', true );
426
+function wpinv_get_discount_min_total($code_id = null) {
427
+    $min_total = get_post_meta($code_id, '_wpi_discount_min_total', true);
428 428
 
429
-    return (float) apply_filters( 'wpinv_get_discount_min_total', $min_total, $code_id );
429
+    return (float) apply_filters('wpinv_get_discount_min_total', $min_total, $code_id);
430 430
 }
431 431
 
432
-function wpinv_get_discount_max_total( $code_id = null ) {
433
-    $max_total = get_post_meta( $code_id, '_wpi_discount_max_total', true );
432
+function wpinv_get_discount_max_total($code_id = null) {
433
+    $max_total = get_post_meta($code_id, '_wpi_discount_max_total', true);
434 434
 
435
-    return (float) apply_filters( 'wpinv_get_discount_max_total', $max_total, $code_id );
435
+    return (float) apply_filters('wpinv_get_discount_max_total', $max_total, $code_id);
436 436
 }
437 437
 
438
-function wpinv_get_discount_amount( $code_id = null ) {
439
-    $amount = get_post_meta( $code_id, '_wpi_discount_amount', true );
438
+function wpinv_get_discount_amount($code_id = null) {
439
+    $amount = get_post_meta($code_id, '_wpi_discount_amount', true);
440 440
 
441
-    return (float) apply_filters( 'wpinv_get_discount_amount', $amount, $code_id );
441
+    return (float) apply_filters('wpinv_get_discount_amount', $amount, $code_id);
442 442
 }
443 443
 
444
-function wpinv_get_discount_type( $code_id = null, $name = false ) {
445
-    $type = strtolower( get_post_meta( $code_id, '_wpi_discount_type', true ) );
444
+function wpinv_get_discount_type($code_id = null, $name = false) {
445
+    $type = strtolower(get_post_meta($code_id, '_wpi_discount_type', true));
446 446
     
447
-    if ( $name ) {
448
-        $name = wpinv_get_discount_type_name( $type );
447
+    if ($name) {
448
+        $name = wpinv_get_discount_type_name($type);
449 449
         
450
-        return apply_filters( 'wpinv_get_discount_type_name', $name, $code_id );
450
+        return apply_filters('wpinv_get_discount_type_name', $name, $code_id);
451 451
     }
452 452
 
453
-    return apply_filters( 'wpinv_get_discount_type', $type, $code_id );
453
+    return apply_filters('wpinv_get_discount_type', $type, $code_id);
454 454
 }
455 455
 
456
-function wpinv_discount_status( $status ) {
457
-    switch( $status ){
456
+function wpinv_discount_status($status) {
457
+    switch ($status) {
458 458
         case 'expired' :
459
-            $name = __( 'Expired', 'invoicing' );
459
+            $name = __('Expired', 'invoicing');
460 460
             break;
461 461
         case 'publish' :
462 462
         case 'active' :
463
-            $name = __( 'Active', 'invoicing' );
463
+            $name = __('Active', 'invoicing');
464 464
             break;
465 465
         default :
466
-            $name = __( 'Inactive', 'invoicing' );
466
+            $name = __('Inactive', 'invoicing');
467 467
             break;
468 468
     }
469 469
     return $name;
470 470
 }
471 471
 
472
-function wpinv_get_discount_excluded_items( $code_id = null ) {
473
-    $excluded_items = get_post_meta( $code_id, '_wpi_discount_excluded_items', true );
472
+function wpinv_get_discount_excluded_items($code_id = null) {
473
+    $excluded_items = get_post_meta($code_id, '_wpi_discount_excluded_items', true);
474 474
 
475
-    if ( empty( $excluded_items ) || ! is_array( $excluded_items ) ) {
475
+    if (empty($excluded_items) || !is_array($excluded_items)) {
476 476
         $excluded_items = array();
477 477
     }
478 478
 
479
-    return (array) apply_filters( 'wpinv_get_discount_excluded_items', $excluded_items, $code_id );
479
+    return (array) apply_filters('wpinv_get_discount_excluded_items', $excluded_items, $code_id);
480 480
 }
481 481
 
482
-function wpinv_get_discount_item_reqs( $code_id = null ) {
483
-    $item_reqs = get_post_meta( $code_id, '_wpi_discount_items', true );
482
+function wpinv_get_discount_item_reqs($code_id = null) {
483
+    $item_reqs = get_post_meta($code_id, '_wpi_discount_items', true);
484 484
 
485
-    if ( empty( $item_reqs ) || ! is_array( $item_reqs ) ) {
485
+    if (empty($item_reqs) || !is_array($item_reqs)) {
486 486
         $item_reqs = array();
487 487
     }
488 488
 
489
-    return (array) apply_filters( 'wpinv_get_discount_item_reqs', $item_reqs, $code_id );
489
+    return (array) apply_filters('wpinv_get_discount_item_reqs', $item_reqs, $code_id);
490 490
 }
491 491
 
492
-function wpinv_get_discount_item_condition( $code_id = 0 ) {
493
-    return get_post_meta( $code_id, '_wpi_discount_item_condition', true );
492
+function wpinv_get_discount_item_condition($code_id = 0) {
493
+    return get_post_meta($code_id, '_wpi_discount_item_condition', true);
494 494
 }
495 495
 
496
-function wpinv_is_discount_not_global( $code_id = 0 ) {
497
-    return (bool) get_post_meta( $code_id, '_wpi_discount_is_not_global', true );
496
+function wpinv_is_discount_not_global($code_id = 0) {
497
+    return (bool) get_post_meta($code_id, '_wpi_discount_is_not_global', true);
498 498
 }
499 499
 
500
-function wpinv_is_discount_expired( $code_id = null ) {
501
-    $discount = wpinv_get_discount(  $code_id );
500
+function wpinv_is_discount_expired($code_id = null) {
501
+    $discount = wpinv_get_discount($code_id);
502 502
     $return   = false;
503 503
 
504
-    if ( $discount ) {
505
-        $expiration = wpinv_get_discount_expiration( $code_id );
506
-        if ( $expiration ) {
507
-            $expiration = strtotime( $expiration );
508
-            if ( $expiration < current_time( 'timestamp' ) ) {
504
+    if ($discount) {
505
+        $expiration = wpinv_get_discount_expiration($code_id);
506
+        if ($expiration) {
507
+            $expiration = strtotime($expiration);
508
+            if ($expiration < current_time('timestamp')) {
509 509
                 // Discount is expired
510
-                wpinv_update_discount_status( $code_id, 'pending' );
510
+                wpinv_update_discount_status($code_id, 'pending');
511 511
                 $return = true;
512 512
             }
513 513
         }
514 514
     }
515 515
 
516
-    return apply_filters( 'wpinv_is_discount_expired', $return, $code_id );
516
+    return apply_filters('wpinv_is_discount_expired', $return, $code_id);
517 517
 }
518 518
 
519
-function wpinv_is_discount_started( $code_id = null ) {
520
-    $discount = wpinv_get_discount(  $code_id );
519
+function wpinv_is_discount_started($code_id = null) {
520
+    $discount = wpinv_get_discount($code_id);
521 521
     $return   = false;
522 522
 
523
-    if ( $discount ) {
524
-        $start_date = wpinv_get_discount_start_date( $code_id );
523
+    if ($discount) {
524
+        $start_date = wpinv_get_discount_start_date($code_id);
525 525
 
526
-        if ( $start_date ) {
527
-            $start_date = strtotime( $start_date );
526
+        if ($start_date) {
527
+            $start_date = strtotime($start_date);
528 528
 
529
-            if ( $start_date < current_time( 'timestamp' ) ) {
529
+            if ($start_date < current_time('timestamp')) {
530 530
                 // Discount has past the start date
531 531
                 $return = true;
532 532
             } else {
533
-                wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active yet.', 'invoicing' ) );
533
+                wpinv_set_error('wpinv-discount-error', __('This discount is not active yet.', 'invoicing'));
534 534
             }
535 535
         } else {
536 536
             // No start date for this discount, so has to be true
@@ -538,159 +538,159 @@  discard block
 block discarded – undo
538 538
         }
539 539
     }
540 540
 
541
-    return apply_filters( 'wpinv_is_discount_started', $return, $code_id );
541
+    return apply_filters('wpinv_is_discount_started', $return, $code_id);
542 542
 }
543 543
 
544
-function wpinv_check_discount_dates( $code_id = null ) {
545
-    $discount = wpinv_get_discount(  $code_id );
544
+function wpinv_check_discount_dates($code_id = null) {
545
+    $discount = wpinv_get_discount($code_id);
546 546
     $return   = false;
547 547
 
548
-    if ( $discount ) {
549
-        $start_date = wpinv_get_discount_start_date( $code_id );
548
+    if ($discount) {
549
+        $start_date = wpinv_get_discount_start_date($code_id);
550 550
 
551
-        if ( $start_date ) {
552
-            $start_date = strtotime( $start_date );
551
+        if ($start_date) {
552
+            $start_date = strtotime($start_date);
553 553
 
554
-            if ( $start_date < current_time( 'timestamp' ) ) {
554
+            if ($start_date < current_time('timestamp')) {
555 555
                 // Discount has past the start date
556 556
                 $return = true;
557 557
             } else {
558
-                wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active yet.', 'invoicing' ) );
558
+                wpinv_set_error('wpinv-discount-error', __('This discount is not active yet.', 'invoicing'));
559 559
             }
560 560
         } else {
561 561
             // No start date for this discount, so has to be true
562 562
             $return = true;
563 563
         }
564 564
         
565
-        if ( $return ) {
566
-            $expiration = wpinv_get_discount_expiration( $code_id );
565
+        if ($return) {
566
+            $expiration = wpinv_get_discount_expiration($code_id);
567 567
             
568
-            if ( $expiration ) {
569
-                $expiration = strtotime( $expiration );
570
-                if ( $expiration < current_time( 'timestamp' ) ) {
568
+            if ($expiration) {
569
+                $expiration = strtotime($expiration);
570
+                if ($expiration < current_time('timestamp')) {
571 571
                     // Discount is expired
572
-                    wpinv_update_discount_status( $code_id, 'pending' );
572
+                    wpinv_update_discount_status($code_id, 'pending');
573 573
                     $return = true;
574 574
                 }
575 575
             }
576 576
         }
577 577
     }
578 578
     
579
-    return apply_filters( 'wpinv_check_discount_dates', $return, $code_id );
579
+    return apply_filters('wpinv_check_discount_dates', $return, $code_id);
580 580
 }
581 581
 
582
-function wpinv_is_discount_maxed_out( $code_id = null ) {
583
-    $discount = wpinv_get_discount(  $code_id );
582
+function wpinv_is_discount_maxed_out($code_id = null) {
583
+    $discount = wpinv_get_discount($code_id);
584 584
     $return   = false;
585 585
 
586
-    if ( $discount ) {
587
-        $uses = wpinv_get_discount_uses( $code_id );
586
+    if ($discount) {
587
+        $uses = wpinv_get_discount_uses($code_id);
588 588
         // Large number that will never be reached
589
-        $max_uses = wpinv_get_discount_max_uses( $code_id );
589
+        $max_uses = wpinv_get_discount_max_uses($code_id);
590 590
         // Should never be greater than, but just in case
591
-        if ( $uses >= $max_uses && ! empty( $max_uses ) ) {
591
+        if ($uses >= $max_uses && !empty($max_uses)) {
592 592
             // Discount is maxed out
593
-            wpinv_set_error( 'wpinv-discount-error', __( 'This discount has reached its maximum usage.', 'invoicing' ) );
593
+            wpinv_set_error('wpinv-discount-error', __('This discount has reached its maximum usage.', 'invoicing'));
594 594
             $return = true;
595 595
         }
596 596
     }
597 597
 
598
-    return apply_filters( 'wpinv_is_discount_maxed_out', $return, $code_id );
598
+    return apply_filters('wpinv_is_discount_maxed_out', $return, $code_id);
599 599
 }
600 600
 
601
-function wpinv_discount_is_min_met( $code_id = null ) {
602
-    $discount = wpinv_get_discount( $code_id );
601
+function wpinv_discount_is_min_met($code_id = null) {
602
+    $discount = wpinv_get_discount($code_id);
603 603
     $return   = false;
604 604
 
605
-    if ( $discount ) {
606
-        $min         = (float)wpinv_get_discount_min_total( $code_id );
607
-        $cart_amount = (float)wpinv_get_cart_discountable_subtotal( $code_id );
605
+    if ($discount) {
606
+        $min         = (float) wpinv_get_discount_min_total($code_id);
607
+        $cart_amount = (float) wpinv_get_cart_discountable_subtotal($code_id);
608 608
 
609
-        if ( !$min > 0 || $cart_amount >= $min ) {
609
+        if (!$min > 0 || $cart_amount >= $min) {
610 610
             // Minimum has been met
611 611
             $return = true;
612 612
         } else {
613
-            wpinv_set_error( 'wpinv-discount-error', sprintf( __( 'Minimum invoice amount should be %s', 'invoicing' ), wpinv_price( wpinv_format_amount( $min ) ) ) );
613
+            wpinv_set_error('wpinv-discount-error', sprintf(__('Minimum invoice amount should be %s', 'invoicing'), wpinv_price(wpinv_format_amount($min))));
614 614
         }
615 615
     }
616 616
 
617
-    return apply_filters( 'wpinv_is_discount_min_met', $return, $code_id );
617
+    return apply_filters('wpinv_is_discount_min_met', $return, $code_id);
618 618
 }
619 619
 
620
-function wpinv_discount_is_max_met( $code_id = null ) {
621
-    $discount = wpinv_get_discount( $code_id );
620
+function wpinv_discount_is_max_met($code_id = null) {
621
+    $discount = wpinv_get_discount($code_id);
622 622
     $return   = false;
623 623
 
624
-    if ( $discount ) {
625
-        $max         = (float)wpinv_get_discount_max_total( $code_id );
626
-        $cart_amount = (float)wpinv_get_cart_discountable_subtotal( $code_id );
624
+    if ($discount) {
625
+        $max         = (float) wpinv_get_discount_max_total($code_id);
626
+        $cart_amount = (float) wpinv_get_cart_discountable_subtotal($code_id);
627 627
 
628
-        if ( !$max > 0 || $cart_amount <= $max ) {
628
+        if (!$max > 0 || $cart_amount <= $max) {
629 629
             // Minimum has been met
630 630
             $return = true;
631 631
         } else {
632
-            wpinv_set_error( 'wpinv-discount-error', sprintf( __( 'Maximum invoice amount should be %s', 'invoicing' ), wpinv_price( wpinv_format_amount( $max ) ) ) );
632
+            wpinv_set_error('wpinv-discount-error', sprintf(__('Maximum invoice amount should be %s', 'invoicing'), wpinv_price(wpinv_format_amount($max))));
633 633
         }
634 634
     }
635 635
 
636
-    return apply_filters( 'wpinv_is_discount_max_met', $return, $code_id );
636
+    return apply_filters('wpinv_is_discount_max_met', $return, $code_id);
637 637
 }
638 638
 
639
-function wpinv_discount_is_single_use( $code_id = 0 ) {
640
-    $single_use = get_post_meta( $code_id, '_wpi_discount_is_single_use', true );
641
-    return (bool) apply_filters( 'wpinv_is_discount_single_use', $single_use, $code_id );
639
+function wpinv_discount_is_single_use($code_id = 0) {
640
+    $single_use = get_post_meta($code_id, '_wpi_discount_is_single_use', true);
641
+    return (bool) apply_filters('wpinv_is_discount_single_use', $single_use, $code_id);
642 642
 }
643 643
 
644
-function wpinv_discount_is_recurring( $code_id = 0, $code = false ) {
645
-    if ( $code ) {
646
-        $discount = wpinv_get_discount_by_code( $code_id );
644
+function wpinv_discount_is_recurring($code_id = 0, $code = false) {
645
+    if ($code) {
646
+        $discount = wpinv_get_discount_by_code($code_id);
647 647
         
648
-        if ( !empty( $discount ) ) {
648
+        if (!empty($discount)) {
649 649
             $code_id = $discount->ID;
650 650
         }
651 651
     }
652 652
     
653
-    $recurring = get_post_meta( $code_id, '_wpi_discount_is_recurring', true );
653
+    $recurring = get_post_meta($code_id, '_wpi_discount_is_recurring', true);
654 654
     
655
-    return (bool) apply_filters( 'wpinv_is_discount_recurring', $recurring, $code_id, $code );
655
+    return (bool) apply_filters('wpinv_is_discount_recurring', $recurring, $code_id, $code);
656 656
 }
657 657
 
658
-function wpinv_discount_item_reqs_met( $code_id = null ) {
659
-    $item_reqs    = wpinv_get_discount_item_reqs( $code_id );
660
-    $condition    = wpinv_get_discount_item_condition( $code_id );
661
-    $excluded_ps  = wpinv_get_discount_excluded_items( $code_id );
658
+function wpinv_discount_item_reqs_met($code_id = null) {
659
+    $item_reqs    = wpinv_get_discount_item_reqs($code_id);
660
+    $condition    = wpinv_get_discount_item_condition($code_id);
661
+    $excluded_ps  = wpinv_get_discount_excluded_items($code_id);
662 662
     $cart_items   = wpinv_get_cart_contents();
663
-    $cart_ids     = $cart_items ? wp_list_pluck( $cart_items, 'id' ) : null;
663
+    $cart_ids     = $cart_items ? wp_list_pluck($cart_items, 'id') : null;
664 664
     $ret          = false;
665 665
 
666
-    if ( empty( $item_reqs ) && empty( $excluded_ps ) ) {
666
+    if (empty($item_reqs) && empty($excluded_ps)) {
667 667
         $ret = true;
668 668
     }
669 669
 
670 670
     // Normalize our data for item requirements, exclusions and cart data
671 671
     // First absint the items, then sort, and reset the array keys
672
-    $item_reqs = array_map( 'absint', $item_reqs );
673
-    asort( $item_reqs );
674
-    $item_reqs = array_values( $item_reqs );
672
+    $item_reqs = array_map('absint', $item_reqs);
673
+    asort($item_reqs);
674
+    $item_reqs = array_values($item_reqs);
675 675
 
676
-    $excluded_ps  = array_map( 'absint', $excluded_ps );
677
-    asort( $excluded_ps );
678
-    $excluded_ps  = array_values( $excluded_ps );
676
+    $excluded_ps  = array_map('absint', $excluded_ps);
677
+    asort($excluded_ps);
678
+    $excluded_ps  = array_values($excluded_ps);
679 679
 
680
-    $cart_ids     = array_map( 'absint', $cart_ids );
681
-    asort( $cart_ids );
682
-    $cart_ids     = array_values( $cart_ids );
680
+    $cart_ids     = array_map('absint', $cart_ids);
681
+    asort($cart_ids);
682
+    $cart_ids     = array_values($cart_ids);
683 683
 
684 684
     // Ensure we have requirements before proceeding
685
-    if ( !$ret && ! empty( $item_reqs ) ) {
686
-        switch( $condition ) {
685
+    if (!$ret && !empty($item_reqs)) {
686
+        switch ($condition) {
687 687
             case 'all' :
688 688
                 // Default back to true
689 689
                 $ret = true;
690 690
 
691
-                foreach ( $item_reqs as $item_id ) {
692
-                    if ( !wpinv_item_in_cart( $item_id ) ) {
693
-                        wpinv_set_error( 'wpinv-discount-error', __( 'The item requirements for this discount are not met.', 'invoicing' ) );
691
+                foreach ($item_reqs as $item_id) {
692
+                    if (!wpinv_item_in_cart($item_id)) {
693
+                        wpinv_set_error('wpinv-discount-error', __('The item requirements for this discount are not met.', 'invoicing'));
694 694
                         $ret = false;
695 695
                         break;
696 696
                     }
@@ -699,15 +699,15 @@  discard block
 block discarded – undo
699 699
                 break;
700 700
 
701 701
             default : // Any
702
-                foreach ( $item_reqs as $item_id ) {
703
-                    if ( wpinv_item_in_cart( $item_id ) ) {
702
+                foreach ($item_reqs as $item_id) {
703
+                    if (wpinv_item_in_cart($item_id)) {
704 704
                         $ret = true;
705 705
                         break;
706 706
                     }
707 707
                 }
708 708
 
709
-                if( ! $ret ) {
710
-                    wpinv_set_error( 'wpinv-discount-error', __( 'The item requirements for this discount are not met.', 'invoicing' ) );
709
+                if (!$ret) {
710
+                    wpinv_set_error('wpinv-discount-error', __('The item requirements for this discount are not met.', 'invoicing'));
711 711
                 }
712 712
 
713 713
                 break;
@@ -716,70 +716,70 @@  discard block
 block discarded – undo
716 716
         $ret = true;
717 717
     }
718 718
 
719
-    if( ! empty( $excluded_ps ) ) {
719
+    if (!empty($excluded_ps)) {
720 720
         // Check that there are items other than excluded ones in the cart
721
-        if( $cart_ids == $excluded_ps ) {
722
-            wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not valid for the cart contents.', 'invoicing' ) );
721
+        if ($cart_ids == $excluded_ps) {
722
+            wpinv_set_error('wpinv-discount-error', __('This discount is not valid for the cart contents.', 'invoicing'));
723 723
             $ret = false;
724 724
         }
725 725
     }
726 726
 
727
-    return (bool) apply_filters( 'wpinv_is_discount_item_req_met', $ret, $code_id, $condition );
727
+    return (bool) apply_filters('wpinv_is_discount_item_req_met', $ret, $code_id, $condition);
728 728
 }
729 729
 
730
-function wpinv_is_discount_used( $code = null, $user = '', $code_id = 0 ) {
730
+function wpinv_is_discount_used($code = null, $user = '', $code_id = 0) {
731 731
     global $wpi_checkout_id;
732 732
     
733 733
     $return = false;
734 734
 
735
-    if ( empty( $code_id ) ) {
736
-        $code_id = wpinv_get_discount_id_by_code( $code );
735
+    if (empty($code_id)) {
736
+        $code_id = wpinv_get_discount_id_by_code($code);
737 737
         
738
-        if( empty( $code_id ) ) {
738
+        if (empty($code_id)) {
739 739
             return false; // No discount was found
740 740
         }
741 741
     }
742 742
 
743
-    if ( wpinv_discount_is_single_use( $code_id ) ) {
743
+    if (wpinv_discount_is_single_use($code_id)) {
744 744
         $payments = array();
745 745
 
746 746
         $user_id = 0;
747
-        if ( is_int( $user ) ) {
748
-            $user_id = absint( $user );
749
-        } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) {
747
+        if (is_int($user)) {
748
+            $user_id = absint($user);
749
+        } else if (is_email($user) && $user_data = get_user_by('email', $user)) {
750 750
             $user_id = $user_data->ID;
751
-        } else if ( $user_data = get_user_by( 'login', $user ) ) {
751
+        } else if ($user_data = get_user_by('login', $user)) {
752 752
             $user_id = $user_data->ID;
753
-        } else if ( absint( $user ) > 0 ) {
754
-            $user_id = absint( $user );
753
+        } else if (absint($user) > 0) {
754
+            $user_id = absint($user);
755 755
         }
756 756
 
757
-        if ( !empty( $user_id ) ) {
758
-            $query    = array( 'user' => $user_id, 'limit' => false );
759
-            $payments = wpinv_get_invoices( $query ); // Get all payments with matching user id
757
+        if (!empty($user_id)) {
758
+            $query    = array('user' => $user_id, 'limit' => false);
759
+            $payments = wpinv_get_invoices($query); // Get all payments with matching user id
760 760
         }
761 761
 
762
-        if ( $payments ) {
763
-            foreach ( $payments as $payment ) {
762
+        if ($payments) {
763
+            foreach ($payments as $payment) {
764 764
                 // Don't count discount used for current invoice chekcout.
765
-                if ( !empty( $wpi_checkout_id ) && $wpi_checkout_id == $payment->ID ) {
765
+                if (!empty($wpi_checkout_id) && $wpi_checkout_id == $payment->ID) {
766 766
                     continue;
767 767
                 }
768 768
                 
769
-                if ( $payment->has_status( array( 'wpi-cancelled', 'wpi-failed' ) ) ) {
769
+                if ($payment->has_status(array('wpi-cancelled', 'wpi-failed'))) {
770 770
                     continue;
771 771
                 }
772 772
 
773
-                $discounts = $payment->get_discounts( true );
774
-                if ( empty( $discounts ) ) {
773
+                $discounts = $payment->get_discounts(true);
774
+                if (empty($discounts)) {
775 775
                     continue;
776 776
                 }
777 777
 
778
-                $discounts = $discounts && !is_array( $discounts ) ? explode( ',', $discounts ) : $discounts;
778
+                $discounts = $discounts && !is_array($discounts) ? explode(',', $discounts) : $discounts;
779 779
 
780
-                if ( !empty( $discounts ) && is_array( $discounts ) ) {
781
-                    if ( in_array( strtolower( $code ), array_map( 'strtolower', $discounts ) ) ) {
782
-                        wpinv_set_error( 'wpinv-discount-error', __( 'This discount has already been redeemed.', 'invoicing' ) );
780
+                if (!empty($discounts) && is_array($discounts)) {
781
+                    if (in_array(strtolower($code), array_map('strtolower', $discounts))) {
782
+                        wpinv_set_error('wpinv-discount-error', __('This discount has already been redeemed.', 'invoicing'));
783 783
                         $return = true;
784 784
                         break;
785 785
                     }
@@ -788,61 +788,61 @@  discard block
 block discarded – undo
788 788
         }
789 789
     }
790 790
 
791
-    return apply_filters( 'wpinv_is_discount_used', $return, $code, $user );
791
+    return apply_filters('wpinv_is_discount_used', $return, $code, $user);
792 792
 }
793 793
 
794
-function wpinv_is_discount_valid( $code = '', $user = '', $set_error = true ) {
794
+function wpinv_is_discount_valid($code = '', $user = '', $set_error = true) {
795 795
     $return      = false;
796
-    $discount_id = wpinv_get_discount_id_by_code( $code );
797
-    $user        = trim( $user );
796
+    $discount_id = wpinv_get_discount_id_by_code($code);
797
+    $user        = trim($user);
798 798
 
799
-    if ( wpinv_get_cart_contents() ) {
800
-        if ( $discount_id ) {
799
+    if (wpinv_get_cart_contents()) {
800
+        if ($discount_id) {
801 801
             if (
802
-                wpinv_is_discount_active( $discount_id ) &&
803
-                wpinv_check_discount_dates( $discount_id ) &&
804
-                !wpinv_is_discount_maxed_out( $discount_id ) &&
805
-                !wpinv_is_discount_used( $code, $user, $discount_id ) &&
806
-                wpinv_discount_is_min_met( $discount_id ) &&
807
-                wpinv_discount_is_max_met( $discount_id ) &&
808
-                wpinv_discount_item_reqs_met( $discount_id )
802
+                wpinv_is_discount_active($discount_id) &&
803
+                wpinv_check_discount_dates($discount_id) &&
804
+                !wpinv_is_discount_maxed_out($discount_id) &&
805
+                !wpinv_is_discount_used($code, $user, $discount_id) &&
806
+                wpinv_discount_is_min_met($discount_id) &&
807
+                wpinv_discount_is_max_met($discount_id) &&
808
+                wpinv_discount_item_reqs_met($discount_id)
809 809
             ) {
810 810
                 $return = true;
811 811
             }
812
-        } elseif( $set_error ) {
813
-            wpinv_set_error( 'wpinv-discount-error', __( 'This discount is invalid.', 'invoicing' ) );
812
+        } elseif ($set_error) {
813
+            wpinv_set_error('wpinv-discount-error', __('This discount is invalid.', 'invoicing'));
814 814
         }
815 815
     }
816 816
 
817
-    return apply_filters( 'wpinv_is_discount_valid', $return, $discount_id, $code, $user );
817
+    return apply_filters('wpinv_is_discount_valid', $return, $discount_id, $code, $user);
818 818
 }
819 819
 
820
-function wpinv_get_discount_id_by_code( $code ) {
821
-    $discount = wpinv_get_discount_by_code( $code );
822
-    if( $discount ) {
820
+function wpinv_get_discount_id_by_code($code) {
821
+    $discount = wpinv_get_discount_by_code($code);
822
+    if ($discount) {
823 823
         return $discount->ID;
824 824
     }
825 825
     return false;
826 826
 }
827 827
 
828
-function wpinv_get_discounted_amount( $code, $base_price ) {
828
+function wpinv_get_discounted_amount($code, $base_price) {
829 829
     $amount      = $base_price;
830
-    $discount_id = wpinv_get_discount_id_by_code( $code );
830
+    $discount_id = wpinv_get_discount_id_by_code($code);
831 831
 
832
-    if( $discount_id ) {
833
-        $type        = wpinv_get_discount_type( $discount_id );
834
-        $rate        = wpinv_get_discount_amount( $discount_id );
832
+    if ($discount_id) {
833
+        $type        = wpinv_get_discount_type($discount_id);
834
+        $rate        = wpinv_get_discount_amount($discount_id);
835 835
 
836
-        if ( $type == 'flat' ) {
836
+        if ($type == 'flat') {
837 837
             // Set amount
838 838
             $amount = $base_price - $rate;
839
-            if ( $amount < 0 ) {
839
+            if ($amount < 0) {
840 840
                 $amount = 0;
841 841
             }
842 842
 
843 843
         } else {
844 844
             // Percentage discount
845
-            $amount = $base_price - ( $base_price * ( $rate / 100 ) );
845
+            $amount = $base_price - ($base_price * ($rate / 100));
846 846
         }
847 847
 
848 848
     } else {
@@ -851,108 +851,108 @@  discard block
 block discarded – undo
851 851
 
852 852
     }
853 853
 
854
-    return apply_filters( 'wpinv_discounted_amount', $amount );
854
+    return apply_filters('wpinv_discounted_amount', $amount);
855 855
 }
856 856
 
857
-function wpinv_increase_discount_usage( $code ) {
857
+function wpinv_increase_discount_usage($code) {
858 858
 
859
-    $id   = wpinv_get_discount_id_by_code( $code );
860
-    $uses = wpinv_get_discount_uses( $id );
859
+    $id   = wpinv_get_discount_id_by_code($code);
860
+    $uses = wpinv_get_discount_uses($id);
861 861
 
862
-    if ( $uses ) {
862
+    if ($uses) {
863 863
         $uses++;
864 864
     } else {
865 865
         $uses = 1;
866 866
     }
867 867
 
868
-    update_post_meta( $id, '_wpi_discount_uses', $uses );
868
+    update_post_meta($id, '_wpi_discount_uses', $uses);
869 869
 
870
-    do_action( 'wpinv_discount_increase_use_count', $uses, $id, $code );
870
+    do_action('wpinv_discount_increase_use_count', $uses, $id, $code);
871 871
 
872 872
     return $uses;
873 873
 
874 874
 }
875 875
 
876
-function wpinv_decrease_discount_usage( $code ) {
876
+function wpinv_decrease_discount_usage($code) {
877 877
 
878
-    $id   = wpinv_get_discount_id_by_code( $code );
879
-    $uses = wpinv_get_discount_uses( $id );
878
+    $id   = wpinv_get_discount_id_by_code($code);
879
+    $uses = wpinv_get_discount_uses($id);
880 880
 
881
-    if ( $uses ) {
881
+    if ($uses) {
882 882
         $uses--;
883 883
     }
884 884
 
885
-    if ( $uses < 0 ) {
885
+    if ($uses < 0) {
886 886
         $uses = 0;
887 887
     }
888 888
 
889
-    update_post_meta( $id, '_wpi_discount_uses', $uses );
889
+    update_post_meta($id, '_wpi_discount_uses', $uses);
890 890
 
891
-    do_action( 'wpinv_discount_decrease_use_count', $uses, $id, $code );
891
+    do_action('wpinv_discount_decrease_use_count', $uses, $id, $code);
892 892
 
893 893
     return $uses;
894 894
 
895 895
 }
896 896
 
897
-function wpinv_format_discount_rate( $type, $amount ) {
898
-    if ( $type == 'flat' ) {
899
-        return wpinv_price( wpinv_format_amount( $amount ) );
897
+function wpinv_format_discount_rate($type, $amount) {
898
+    if ($type == 'flat') {
899
+        return wpinv_price(wpinv_format_amount($amount));
900 900
     } else {
901 901
         return $amount . '%';
902 902
     }
903 903
 }
904 904
 
905
-function wpinv_set_cart_discount( $code = '' ) {    
906
-    if ( wpinv_multiple_discounts_allowed() ) {
905
+function wpinv_set_cart_discount($code = '') {    
906
+    if (wpinv_multiple_discounts_allowed()) {
907 907
         // Get all active cart discounts
908 908
         $discounts = wpinv_get_cart_discounts();
909 909
     } else {
910 910
         $discounts = false; // Only one discount allowed per purchase, so override any existing
911 911
     }
912 912
 
913
-    if ( $discounts ) {
914
-        $key = array_search( strtolower( $code ), array_map( 'strtolower', $discounts ) );
915
-        if( false !== $key ) {
916
-            unset( $discounts[ $key ] ); // Can't set the same discount more than once
913
+    if ($discounts) {
914
+        $key = array_search(strtolower($code), array_map('strtolower', $discounts));
915
+        if (false !== $key) {
916
+            unset($discounts[$key]); // Can't set the same discount more than once
917 917
         }
918 918
         $discounts[] = $code;
919 919
     } else {
920 920
         $discounts = array();
921 921
         $discounts[] = $code;
922 922
     }
923
-    $discounts = array_values( $discounts );
923
+    $discounts = array_values($discounts);
924 924
     
925 925
     $data = wpinv_get_checkout_session();
926
-    if ( empty( $data ) ) {
926
+    if (empty($data)) {
927 927
         $data = array();
928 928
     } else {
929
-        if ( !empty( $data['invoice_id'] ) && $payment_meta = wpinv_get_invoice_meta( $data['invoice_id'] ) ) {
930
-            $payment_meta['user_info']['discount']  = implode( ',', $discounts );
931
-            update_post_meta( $data['invoice_id'], '_wpinv_payment_meta', $payment_meta );
929
+        if (!empty($data['invoice_id']) && $payment_meta = wpinv_get_invoice_meta($data['invoice_id'])) {
930
+            $payment_meta['user_info']['discount'] = implode(',', $discounts);
931
+            update_post_meta($data['invoice_id'], '_wpinv_payment_meta', $payment_meta);
932 932
         }
933 933
     }
934 934
     $data['cart_discounts'] = $discounts;
935 935
     
936
-    wpinv_set_checkout_session( $data );
936
+    wpinv_set_checkout_session($data);
937 937
     
938 938
     return $discounts;
939 939
 }
940 940
 
941
-function wpinv_unset_cart_discount( $code = '' ) {    
941
+function wpinv_unset_cart_discount($code = '') {    
942 942
     $discounts = wpinv_get_cart_discounts();
943 943
 
944
-    if ( $code && !empty( $discounts ) && in_array( $code, $discounts ) ) {
945
-        $key = array_search( $code, $discounts );
946
-        unset( $discounts[ $key ] );
944
+    if ($code && !empty($discounts) && in_array($code, $discounts)) {
945
+        $key = array_search($code, $discounts);
946
+        unset($discounts[$key]);
947 947
             
948 948
         $data = wpinv_get_checkout_session();
949 949
         $data['cart_discounts'] = $discounts;
950
-        if ( !empty( $data['invoice_id'] ) && $payment_meta = wpinv_get_invoice_meta( $data['invoice_id'] ) ) {
951
-            $payment_meta['user_info']['discount']  = !empty( $discounts ) ? implode( ',', $discounts ) : '';
952
-            update_post_meta( $data['invoice_id'], '_wpinv_payment_meta', $payment_meta );
950
+        if (!empty($data['invoice_id']) && $payment_meta = wpinv_get_invoice_meta($data['invoice_id'])) {
951
+            $payment_meta['user_info']['discount'] = !empty($discounts) ? implode(',', $discounts) : '';
952
+            update_post_meta($data['invoice_id'], '_wpinv_payment_meta', $payment_meta);
953 953
         }
954 954
         
955
-        wpinv_set_checkout_session( $data );
955
+        wpinv_set_checkout_session($data);
956 956
     }
957 957
 
958 958
     return $discounts;
@@ -961,27 +961,27 @@  discard block
 block discarded – undo
961 961
 function wpinv_unset_all_cart_discounts() {
962 962
     $data = wpinv_get_checkout_session();
963 963
     
964
-    if ( !empty( $data ) && isset( $data['cart_discounts'] ) ) {
965
-        unset( $data['cart_discounts'] );
964
+    if (!empty($data) && isset($data['cart_discounts'])) {
965
+        unset($data['cart_discounts']);
966 966
         
967
-         wpinv_set_checkout_session( $data );
967
+         wpinv_set_checkout_session($data);
968 968
          return true;
969 969
     }
970 970
     
971 971
     return false;
972 972
 }
973 973
 
974
-function wpinv_get_cart_discounts( $items = array() ) {
974
+function wpinv_get_cart_discounts($items = array()) {
975 975
     $session = wpinv_get_checkout_session();
976 976
     
977
-    $discounts = !empty( $session['cart_discounts'] ) ? $session['cart_discounts'] : false;
977
+    $discounts = !empty($session['cart_discounts']) ? $session['cart_discounts'] : false;
978 978
     return $discounts;
979 979
 }
980 980
 
981
-function wpinv_cart_has_discounts( $items = array() ) {
981
+function wpinv_cart_has_discounts($items = array()) {
982 982
     $ret = false;
983 983
 
984
-    if ( wpinv_get_cart_discounts( $items ) ) {
984
+    if (wpinv_get_cart_discounts($items)) {
985 985
         $ret = true;
986 986
     }
987 987
     
@@ -992,131 +992,131 @@  discard block
 block discarded – undo
992 992
     }
993 993
     */
994 994
 
995
-    return apply_filters( 'wpinv_cart_has_discounts', $ret );
995
+    return apply_filters('wpinv_cart_has_discounts', $ret);
996 996
 }
997 997
 
998
-function wpinv_get_cart_discounted_amount( $items = array(), $discounts = false ) {
998
+function wpinv_get_cart_discounted_amount($items = array(), $discounts = false) {
999 999
     $amount = 0.00;
1000
-    $items  = !empty( $items ) ? $items : wpinv_get_cart_content_details();
1000
+    $items  = !empty($items) ? $items : wpinv_get_cart_content_details();
1001 1001
 
1002
-    if ( $items ) {
1003
-        $discounts = wp_list_pluck( $items, 'discount' );
1002
+    if ($items) {
1003
+        $discounts = wp_list_pluck($items, 'discount');
1004 1004
 
1005
-        if ( is_array( $discounts ) ) {
1006
-            $discounts = array_map( 'floatval', $discounts );
1007
-            $amount    = array_sum( $discounts );
1005
+        if (is_array($discounts)) {
1006
+            $discounts = array_map('floatval', $discounts);
1007
+            $amount    = array_sum($discounts);
1008 1008
         }
1009 1009
     }
1010 1010
 
1011
-    return apply_filters( 'wpinv_get_cart_discounted_amount', $amount );
1011
+    return apply_filters('wpinv_get_cart_discounted_amount', $amount);
1012 1012
 }
1013 1013
 
1014
-function wpinv_get_cart_items_discount_amount( $items = array(), $discount = false ) {
1015
-    $items  = !empty( $items ) ? $items : wpinv_get_cart_content_details();
1014
+function wpinv_get_cart_items_discount_amount($items = array(), $discount = false) {
1015
+    $items = !empty($items) ? $items : wpinv_get_cart_content_details();
1016 1016
     
1017
-    if ( empty( $discount ) || empty( $items ) ) {
1017
+    if (empty($discount) || empty($items)) {
1018 1018
         return 0;
1019 1019
     }
1020 1020
 
1021 1021
     $amount = 0;
1022 1022
     
1023
-    foreach ( $items as $item ) {
1024
-        $amount += wpinv_get_cart_item_discount_amount( $item, $discount );
1023
+    foreach ($items as $item) {
1024
+        $amount += wpinv_get_cart_item_discount_amount($item, $discount);
1025 1025
     }
1026 1026
     
1027
-    $amount = wpinv_round_amount( $amount );
1027
+    $amount = wpinv_round_amount($amount);
1028 1028
 
1029 1029
     return $amount;
1030 1030
 }
1031 1031
 
1032
-function wpinv_get_cart_item_discount_amount( $item = array(), $discount = false ) {
1032
+function wpinv_get_cart_item_discount_amount($item = array(), $discount = false) {
1033 1033
     global $wpinv_is_last_cart_item, $wpinv_flat_discount_total;
1034 1034
     
1035 1035
     $amount = 0;
1036 1036
 
1037
-    if ( empty( $item ) || empty( $item['id'] ) ) {
1037
+    if (empty($item) || empty($item['id'])) {
1038 1038
         return $amount;
1039 1039
     }
1040 1040
 
1041
-    if ( empty( $item['quantity'] ) ) {
1041
+    if (empty($item['quantity'])) {
1042 1042
         return $amount;
1043 1043
     }
1044 1044
 
1045
-    if ( empty( $item['options'] ) ) {
1045
+    if (empty($item['options'])) {
1046 1046
         $item['options'] = array();
1047 1047
     }
1048 1048
 
1049
-    $price            = wpinv_get_cart_item_price( $item['id'], $item, $item['options'] );
1049
+    $price            = wpinv_get_cart_item_price($item['id'], $item, $item['options']);
1050 1050
     $discounted_price = $price;
1051 1051
 
1052 1052
     $discounts = false === $discount ? wpinv_get_cart_discounts() : $discount;
1053
-    if ( empty( $discounts ) ) {
1053
+    if (empty($discounts)) {
1054 1054
         return $amount;
1055 1055
     }
1056 1056
 
1057
-    if ( $discounts ) {
1058
-        if ( is_array( $discounts ) ) {
1059
-            $discounts = array_values( $discounts );
1057
+    if ($discounts) {
1058
+        if (is_array($discounts)) {
1059
+            $discounts = array_values($discounts);
1060 1060
         } else {
1061
-            $discounts = explode( ',', $discounts );
1061
+            $discounts = explode(',', $discounts);
1062 1062
         }
1063 1063
     }
1064 1064
 
1065
-    if( $discounts ) {
1066
-        foreach ( $discounts as $discount ) {
1067
-            $code_id = wpinv_get_discount_id_by_code( $discount );
1065
+    if ($discounts) {
1066
+        foreach ($discounts as $discount) {
1067
+            $code_id = wpinv_get_discount_id_by_code($discount);
1068 1068
 
1069 1069
             // Check discount exists
1070
-            if( ! $code_id ) {
1070
+            if (!$code_id) {
1071 1071
                 continue;
1072 1072
             }
1073 1073
 
1074
-            $reqs           = wpinv_get_discount_item_reqs( $code_id );
1075
-            $excluded_items = wpinv_get_discount_excluded_items( $code_id );
1074
+            $reqs           = wpinv_get_discount_item_reqs($code_id);
1075
+            $excluded_items = wpinv_get_discount_excluded_items($code_id);
1076 1076
 
1077 1077
             // Make sure requirements are set and that this discount shouldn't apply to the whole cart
1078
-            if ( !empty( $reqs ) && wpinv_is_discount_not_global( $code_id ) ) {
1079
-                foreach ( $reqs as $item_id ) {
1080
-                    if ( $item_id == $item['id'] && ! in_array( $item['id'], $excluded_items ) ) {
1081
-                        $discounted_price -= $price - wpinv_get_discounted_amount( $discount, $price );
1078
+            if (!empty($reqs) && wpinv_is_discount_not_global($code_id)) {
1079
+                foreach ($reqs as $item_id) {
1080
+                    if ($item_id == $item['id'] && !in_array($item['id'], $excluded_items)) {
1081
+                        $discounted_price -= $price - wpinv_get_discounted_amount($discount, $price);
1082 1082
                     }
1083 1083
                 }
1084 1084
             } else {
1085 1085
                 // This is a global cart discount
1086
-                if ( !in_array( $item['id'], $excluded_items ) ) {
1087
-                    if ( 'flat' === wpinv_get_discount_type( $code_id ) ) {
1086
+                if (!in_array($item['id'], $excluded_items)) {
1087
+                    if ('flat' === wpinv_get_discount_type($code_id)) {
1088 1088
                         $items_subtotal    = 0.00;
1089 1089
                         $cart_items        = wpinv_get_cart_contents();
1090 1090
                         
1091
-                        foreach ( $cart_items as $cart_item ) {
1092
-                            if ( ! in_array( $cart_item['id'], $excluded_items ) ) {
1093
-                                $options = !empty( $cart_item['options'] ) ? $cart_item['options'] : array();
1094
-                                $item_price      = wpinv_get_cart_item_price( $cart_item['id'], $cart_item, $options );
1091
+                        foreach ($cart_items as $cart_item) {
1092
+                            if (!in_array($cart_item['id'], $excluded_items)) {
1093
+                                $options = !empty($cart_item['options']) ? $cart_item['options'] : array();
1094
+                                $item_price      = wpinv_get_cart_item_price($cart_item['id'], $cart_item, $options);
1095 1095
                                 $items_subtotal += $item_price * $cart_item['quantity'];
1096 1096
                             }
1097 1097
                         }
1098 1098
 
1099
-                        $subtotal_percent  = ( ( $price * $item['quantity'] ) / $items_subtotal );
1100
-                        $code_amount       = wpinv_get_discount_amount( $code_id );
1099
+                        $subtotal_percent  = (($price * $item['quantity']) / $items_subtotal);
1100
+                        $code_amount       = wpinv_get_discount_amount($code_id);
1101 1101
                         $discounted_amount = $code_amount * $subtotal_percent;
1102 1102
                         $discounted_price -= $discounted_amount;
1103 1103
 
1104
-                        $wpinv_flat_discount_total += round( $discounted_amount, wpinv_currency_decimal_filter() );
1104
+                        $wpinv_flat_discount_total += round($discounted_amount, wpinv_currency_decimal_filter());
1105 1105
 
1106
-                        if ( $wpinv_is_last_cart_item && $wpinv_flat_discount_total < $code_amount ) {
1106
+                        if ($wpinv_is_last_cart_item && $wpinv_flat_discount_total < $code_amount) {
1107 1107
                             $adjustment = $code_amount - $wpinv_flat_discount_total;
1108 1108
                             $discounted_price -= $adjustment;
1109 1109
                         }
1110 1110
                     } else {
1111
-                        $discounted_price -= $price - wpinv_get_discounted_amount( $discount, $price );
1111
+                        $discounted_price -= $price - wpinv_get_discounted_amount($discount, $price);
1112 1112
                     }
1113 1113
                 }
1114 1114
             }
1115 1115
         }
1116 1116
 
1117
-        $amount = ( $price - apply_filters( 'wpinv_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price ) );
1117
+        $amount = ($price - apply_filters('wpinv_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price));
1118 1118
 
1119
-        if ( 'flat' !== wpinv_get_discount_type( $code_id ) ) {
1119
+        if ('flat' !== wpinv_get_discount_type($code_id)) {
1120 1120
             $amount = $amount * $item['quantity'];
1121 1121
         }
1122 1122
     }
@@ -1124,59 +1124,59 @@  discard block
 block discarded – undo
1124 1124
     return $amount;
1125 1125
 }
1126 1126
 
1127
-function wpinv_cart_discounts_html( $items = array() ) {
1128
-    echo wpinv_get_cart_discounts_html( $items );
1127
+function wpinv_cart_discounts_html($items = array()) {
1128
+    echo wpinv_get_cart_discounts_html($items);
1129 1129
 }
1130 1130
 
1131
-function wpinv_get_cart_discounts_html( $items = array(), $discounts = false ) {
1131
+function wpinv_get_cart_discounts_html($items = array(), $discounts = false) {
1132 1132
     global $wpi_cart_columns;
1133 1133
     
1134
-    $items  = !empty( $items ) ? $items : wpinv_get_cart_content_details();
1134
+    $items = !empty($items) ? $items : wpinv_get_cart_content_details();
1135 1135
     
1136
-    if ( !$discounts ) {
1137
-        $discounts = wpinv_get_cart_discounts( $items );
1136
+    if (!$discounts) {
1137
+        $discounts = wpinv_get_cart_discounts($items);
1138 1138
     }
1139 1139
 
1140
-    if ( !$discounts ) {
1140
+    if (!$discounts) {
1141 1141
         return;
1142 1142
     }
1143 1143
     
1144
-    $discounts = is_array( $discounts ) ? $discounts : array( $discounts );
1144
+    $discounts = is_array($discounts) ? $discounts : array($discounts);
1145 1145
     
1146 1146
     $html = '';
1147 1147
 
1148
-    foreach ( $discounts as $discount ) {
1149
-        $discount_id    = wpinv_get_discount_id_by_code( $discount );
1150
-        $discount_value = wpinv_get_discount_amount( $discount_id );
1151
-        $rate           = wpinv_format_discount_rate( wpinv_get_discount_type( $discount_id ), $discount_value );
1152
-        $amount         = wpinv_get_cart_items_discount_amount( $items, $discount );
1153
-        $remove_btn     = '<a title="' . esc_attr__( 'Remove discount', 'invoicing' ) . '" data-code="' . $discount . '" data-value="' . $discount_value . '" class="wpi-discount-remove" href="javascript:void(0);">[<i class="fa fa-times" aria-hidden="true"></i>]</a> ';
1148
+    foreach ($discounts as $discount) {
1149
+        $discount_id    = wpinv_get_discount_id_by_code($discount);
1150
+        $discount_value = wpinv_get_discount_amount($discount_id);
1151
+        $rate           = wpinv_format_discount_rate(wpinv_get_discount_type($discount_id), $discount_value);
1152
+        $amount         = wpinv_get_cart_items_discount_amount($items, $discount);
1153
+        $remove_btn     = '<a title="' . esc_attr__('Remove discount', 'invoicing') . '" data-code="' . $discount . '" data-value="' . $discount_value . '" class="wpi-discount-remove" href="javascript:void(0);">[<i class="fa fa-times" aria-hidden="true"></i>]</a> ';
1154 1154
         
1155 1155
         $html .= '<tr class="wpinv_cart_footer_row wpinv_cart_discount_row">';
1156 1156
         ob_start();
1157
-        do_action( 'wpinv_checkout_table_discount_first', $items );
1157
+        do_action('wpinv_checkout_table_discount_first', $items);
1158 1158
         $html .= ob_get_clean();
1159
-        $html .= '<td class="wpinv_cart_discount_label text-right" colspan="' . $wpi_cart_columns . '">' . $remove_btn . '<strong>' . wpinv_cart_discount_label( $discount, $rate, false ) . '</strong></td><td class="wpinv_cart_discount text-right"><span data-discount="' . $amount . '" class="wpinv_cart_discount_amount">&ndash;' . wpinv_price( wpinv_format_amount( $amount ) ) . '</span></td>';
1159
+        $html .= '<td class="wpinv_cart_discount_label text-right" colspan="' . $wpi_cart_columns . '">' . $remove_btn . '<strong>' . wpinv_cart_discount_label($discount, $rate, false) . '</strong></td><td class="wpinv_cart_discount text-right"><span data-discount="' . $amount . '" class="wpinv_cart_discount_amount">&ndash;' . wpinv_price(wpinv_format_amount($amount)) . '</span></td>';
1160 1160
         ob_start();
1161
-        do_action( 'wpinv_checkout_table_discount_last', $items );
1161
+        do_action('wpinv_checkout_table_discount_last', $items);
1162 1162
         $html .= ob_get_clean();
1163 1163
         $html .= '</tr>';
1164 1164
     }
1165 1165
 
1166
-    return apply_filters( 'wpinv_get_cart_discounts_html', $html, $discounts, $rate );
1166
+    return apply_filters('wpinv_get_cart_discounts_html', $html, $discounts, $rate);
1167 1167
 }
1168 1168
 
1169
-function wpinv_display_cart_discount( $formatted = false, $echo = false ) {
1169
+function wpinv_display_cart_discount($formatted = false, $echo = false) {
1170 1170
     $discounts = wpinv_get_cart_discounts();
1171 1171
 
1172
-    if ( empty( $discounts ) ) {
1172
+    if (empty($discounts)) {
1173 1173
         return false;
1174 1174
     }
1175 1175
 
1176
-    $discount_id  = wpinv_get_discount_id_by_code( $discounts[0] );
1177
-    $amount       = wpinv_format_discount_rate( wpinv_get_discount_type( $discount_id ), wpinv_get_discount_amount( $discount_id ) );
1176
+    $discount_id  = wpinv_get_discount_id_by_code($discounts[0]);
1177
+    $amount       = wpinv_format_discount_rate(wpinv_get_discount_type($discount_id), wpinv_get_discount_amount($discount_id));
1178 1178
 
1179
-    if ( $echo ) {
1179
+    if ($echo) {
1180 1180
         echo $amount;
1181 1181
     }
1182 1182
 
@@ -1184,101 +1184,101 @@  discard block
 block discarded – undo
1184 1184
 }
1185 1185
 
1186 1186
 function wpinv_remove_cart_discount() {
1187
-    if ( !isset( $_GET['discount_id'] ) || ! isset( $_GET['discount_code'] ) ) {
1187
+    if (!isset($_GET['discount_id']) || !isset($_GET['discount_code'])) {
1188 1188
         return;
1189 1189
     }
1190 1190
 
1191
-    do_action( 'wpinv_pre_remove_cart_discount', absint( $_GET['discount_id'] ) );
1191
+    do_action('wpinv_pre_remove_cart_discount', absint($_GET['discount_id']));
1192 1192
 
1193
-    wpinv_unset_cart_discount( urldecode( $_GET['discount_code'] ) );
1193
+    wpinv_unset_cart_discount(urldecode($_GET['discount_code']));
1194 1194
 
1195
-    do_action( 'wpinv_post_remove_cart_discount', absint( $_GET['discount_id'] ) );
1195
+    do_action('wpinv_post_remove_cart_discount', absint($_GET['discount_id']));
1196 1196
 
1197
-    wp_redirect( wpinv_get_checkout_uri() ); wpinv_die();
1197
+    wp_redirect(wpinv_get_checkout_uri()); wpinv_die();
1198 1198
 }
1199
-add_action( 'wpinv_remove_cart_discount', 'wpinv_remove_cart_discount' );
1199
+add_action('wpinv_remove_cart_discount', 'wpinv_remove_cart_discount');
1200 1200
 
1201
-function wpinv_maybe_remove_cart_discount( $cart_key = 0 ) {
1201
+function wpinv_maybe_remove_cart_discount($cart_key = 0) {
1202 1202
     $discounts = wpinv_get_cart_discounts();
1203 1203
 
1204
-    if ( !$discounts ) {
1204
+    if (!$discounts) {
1205 1205
         return;
1206 1206
     }
1207 1207
 
1208
-    foreach ( $discounts as $discount ) {
1209
-        if ( !wpinv_is_discount_valid( $discount ) ) {
1210
-            wpinv_unset_cart_discount( $discount );
1208
+    foreach ($discounts as $discount) {
1209
+        if (!wpinv_is_discount_valid($discount)) {
1210
+            wpinv_unset_cart_discount($discount);
1211 1211
         }
1212 1212
     }
1213 1213
 }
1214
-add_action( 'wpinv_post_remove_from_cart', 'wpinv_maybe_remove_cart_discount' );
1214
+add_action('wpinv_post_remove_from_cart', 'wpinv_maybe_remove_cart_discount');
1215 1215
 
1216 1216
 function wpinv_multiple_discounts_allowed() {
1217
-    $ret = wpinv_get_option( 'allow_multiple_discounts', false );
1218
-    return (bool) apply_filters( 'wpinv_multiple_discounts_allowed', $ret );
1217
+    $ret = wpinv_get_option('allow_multiple_discounts', false);
1218
+    return (bool) apply_filters('wpinv_multiple_discounts_allowed', $ret);
1219 1219
 }
1220 1220
 
1221
-function wpinv_get_discount_label( $code, $echo = true ) {
1222
-    $label = wp_sprintf( __( 'Discount%1$s', 'invoicing' ), ( $code != '' && $code != 'none' ? ' (<code>' . $code . '</code>)': '' ) );
1223
-    $label = apply_filters( 'wpinv_get_discount_label', $label, $code );
1221
+function wpinv_get_discount_label($code, $echo = true) {
1222
+    $label = wp_sprintf(__('Discount%1$s', 'invoicing'), ($code != '' && $code != 'none' ? ' (<code>' . $code . '</code>)' : ''));
1223
+    $label = apply_filters('wpinv_get_discount_label', $label, $code);
1224 1224
 
1225
-    if ( $echo ) {
1225
+    if ($echo) {
1226 1226
         echo $label;
1227 1227
     } else {
1228 1228
         return $label;
1229 1229
     }
1230 1230
 }
1231 1231
 
1232
-function wpinv_cart_discount_label( $code, $rate, $echo = true ) {
1233
-    $label = wp_sprintf( __( 'Discount: %s', 'invoicing' ), $code );
1234
-    $label = apply_filters( 'wpinv_cart_discount_label', $label, $code, $rate );
1232
+function wpinv_cart_discount_label($code, $rate, $echo = true) {
1233
+    $label = wp_sprintf(__('Discount: %s', 'invoicing'), $code);
1234
+    $label = apply_filters('wpinv_cart_discount_label', $label, $code, $rate);
1235 1235
 
1236
-    if ( $echo ) {
1236
+    if ($echo) {
1237 1237
         echo $label;
1238 1238
     } else {
1239 1239
         return $label;
1240 1240
     }
1241 1241
 }
1242 1242
 
1243
-function wpinv_check_delete_discount( $check, $post, $force_delete ) {
1244
-    if ( $post->post_type == 'wpi_discount' && wpinv_get_discount_uses( $post->ID ) > 0 ) {
1243
+function wpinv_check_delete_discount($check, $post, $force_delete) {
1244
+    if ($post->post_type == 'wpi_discount' && wpinv_get_discount_uses($post->ID) > 0) {
1245 1245
         return true;
1246 1246
     }
1247 1247
     
1248 1248
     return $check;
1249 1249
 }
1250
-add_filter( 'pre_delete_post', 'wpinv_check_delete_discount', 10, 3 );
1250
+add_filter('pre_delete_post', 'wpinv_check_delete_discount', 10, 3);
1251 1251
 
1252 1252
 function wpinv_checkout_form_validate_discounts() {
1253 1253
     global $wpi_checkout_id;
1254 1254
     
1255 1255
     $discounts = wpinv_get_cart_discounts();
1256 1256
     
1257
-    if ( !empty( $discounts ) ) {
1257
+    if (!empty($discounts)) {
1258 1258
         $invalid = false;
1259 1259
         
1260
-        foreach ( $discounts as $key => $code ) {
1261
-            if ( !wpinv_is_discount_valid( $code, (int)wpinv_get_user_id( $wpi_checkout_id ) ) ) {
1260
+        foreach ($discounts as $key => $code) {
1261
+            if (!wpinv_is_discount_valid($code, (int) wpinv_get_user_id($wpi_checkout_id))) {
1262 1262
                 $invalid = true;
1263 1263
                 
1264
-                wpinv_unset_cart_discount( $code );
1264
+                wpinv_unset_cart_discount($code);
1265 1265
             }
1266 1266
         }
1267 1267
         
1268
-        if ( $invalid ) {
1268
+        if ($invalid) {
1269 1269
             $errors = wpinv_get_errors();
1270
-            $error  = !empty( $errors['wpinv-discount-error'] ) ? $errors['wpinv-discount-error'] . ' ' : '';
1271
-            $error  .= __( 'The discount has been removed from cart.', 'invoicing' );
1272
-            wpinv_set_error( 'wpinv-discount-error', $error );
1270
+            $error  = !empty($errors['wpinv-discount-error']) ? $errors['wpinv-discount-error'] . ' ' : '';
1271
+            $error .= __('The discount has been removed from cart.', 'invoicing');
1272
+            wpinv_set_error('wpinv-discount-error', $error);
1273 1273
             
1274
-            wpinv_recalculate_tax( true );
1274
+            wpinv_recalculate_tax(true);
1275 1275
         }
1276 1276
     }
1277 1277
 }
1278
-add_action( 'wpinv_before_checkout_form', 'wpinv_checkout_form_validate_discounts', -10 );
1278
+add_action('wpinv_before_checkout_form', 'wpinv_checkout_form_validate_discounts', -10);
1279 1279
 
1280 1280
 function wpinv_discount_amount() {
1281 1281
     $output = 0.00;
1282 1282
     
1283
-    return apply_filters( 'wpinv_discount_amount', $output );
1283
+    return apply_filters('wpinv_discount_amount', $output);
1284 1284
 }
1285 1285
\ No newline at end of file
Please login to merge, or discard this patch.
includes/class-wpinv-api.php 1 patch
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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,19 +37,19 @@  discard block
 block discarded – undo
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 );
49
-        $this->discounts_controller= new WPInv_REST_Discounts_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
+        $this->discounts_controller = new WPInv_REST_Discounts_Controller($this->api_namespace);
50 50
 
51 51
         //Register REST routes
52
-        add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
52
+        add_action('rest_api_init', array($this, 'register_rest_routes'));
53 53
     }
54 54
 
55 55
 
@@ -80,13 +80,13 @@  discard block
 block discarded – undo
80 80
     protected function includes() {
81 81
         
82 82
         // Invoices
83
-        require_once( WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-invoice-controller.php' );
83
+        require_once(WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-invoice-controller.php');
84 84
 
85 85
         // Items
86
-        require_once( WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-items-controller.php' );
86
+        require_once(WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-items-controller.php');
87 87
 
88 88
         // Discounts
89
-        require_once( WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-discounts-controller.php' );
89
+        require_once(WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-discounts-controller.php');
90 90
 
91 91
     }
92 92
     
Please login to merge, or discard this patch.
includes/api/class-wpinv-rest-items-controller.php 2 patches
Indentation   +987 added lines, -987 removed lines patch added patch discarded remove patch
@@ -20,91 +20,91 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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 ( wpinv_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 ( wpinv_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           $data_to_update 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           $data_to_update 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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
Please login to merge, or discard this patch.
Spacing   +254 added lines, -254 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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 ( wpinv_current_user_can_manage_invoicing() ) {
398
+		if (wpinv_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
 block discarded – undo
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
 block discarded – undo
468 468
 		 * @param array           $data_to_update 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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
565 565
 	 */
566 566
 	public function get_collection_params() {
567 567
         
568
-        $query_params               = array(
568
+        $query_params = array(
569 569
 
570 570
             // Item 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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
     
Please login to merge, or discard this patch.
includes/api/class-wpinv-rest-discounts-controller.php 2 patches
Indentation   +970 added lines, -970 removed lines patch added patch discarded remove patch
@@ -20,91 +20,91 @@  discard block
 block discarded – undo
20 20
 class WPInv_REST_Discounts_Controller extends WP_REST_Posts_Controller {
21 21
 
22 22
     /**
23
-	 * Post type.
24
-	 *
25
-	 * @var string
26
-	 */
27
-	protected $post_type = 'wpi_discount';
23
+     * Post type.
24
+     *
25
+     * @var string
26
+     */
27
+    protected $post_type = 'wpi_discount';
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 = 'discounts';
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 . '/discount-types',
68
-			array(
69
-				array(
70
-					'methods'             => WP_REST_Server::READABLE,
71
-					'callback'            => array( $this, 'get_discount_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 . '/discount-types',
68
+            array(
69
+                array(
70
+                    'methods'             => WP_REST_Server::READABLE,
71
+                    'callback'            => array( $this, 'get_discount_types' ),
72
+                ),
73
+            )
74
+        );
75
+
76
+    }
77 77
 
78 78
     /**
79
-	 * Checks if a given request has access to read discounts.
79
+     * Checks if a given request has access to read discounts.
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 ( wpinv_current_user_can_manage_invoicing() ) {
90
-			return true;
91
-		}
89
+        if ( wpinv_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 discounts.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
93
+        return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice discounts.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
94 94
 
95 95
     }
96 96
     
97 97
     /**
98
-	 * Retrieves a collection of discounts.
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 discounts.
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();
@@ -115,52 +115,52 @@  discard block
 block discarded – undo
115 115
                 $args[ $key ] = $request[ $key];
116 116
             }
117 117
 
118
-		} 
119
-
120
-		/**
121
-		 * Filters the wpinv_get_all_discounts arguments for discounts 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
-		 */
118
+        } 
119
+
120
+        /**
121
+         * Filters the wpinv_get_all_discounts arguments for discounts 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_discounts_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_discounts( $args );
137
+        $query = wpinv_get_all_discounts( $args );
138 138
 		
139
-		// Prepare the retrieved discounts
140
-		$discounts = array();
141
-		foreach( $query->discounts as $discount ) {
142
-
143
-			$data       = $this->prepare_item_for_response( $discount, $request );
144
-			$discounts[]    = $this->prepare_response_for_collection( $data );
145
-
146
-		}
147
-
148
-		// Prepare the response.
149
-		$response = rest_ensure_response( $discounts );
150
-		$response->header( 'X-WP-Total', (int) $query->total );
151
-		$response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
152
-
153
-		/**
154
-		 * Filters the responses for discount requests.
155
-		 *
156
-		 *
157
-		 * @since 1.0.13
158
-		 *
159
-		 *
160
-		 * @param arrWP_REST_Response $response    Response object.
161
-		 * @param WP_REST_Request     $request The request used.
139
+        // Prepare the retrieved discounts
140
+        $discounts = array();
141
+        foreach( $query->discounts as $discount ) {
142
+
143
+            $data       = $this->prepare_item_for_response( $discount, $request );
144
+            $discounts[]    = $this->prepare_response_for_collection( $data );
145
+
146
+        }
147
+
148
+        // Prepare the response.
149
+        $response = rest_ensure_response( $discounts );
150
+        $response->header( 'X-WP-Total', (int) $query->total );
151
+        $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
152
+
153
+        /**
154
+         * Filters the responses for discount requests.
155
+         *
156
+         *
157
+         * @since 1.0.13
158
+         *
159
+         *
160
+         * @param arrWP_REST_Response $response    Response object.
161
+         * @param WP_REST_Request     $request The request used.
162 162
          * @param array               $args Array of args used to retrieve the discounts
163
-		 */
163
+         */
164 164
         $response       = apply_filters( "wpinv_rest_discounts_response", $response, $request, $args );
165 165
 
166 166
         return rest_ensure_response( $response );
@@ -168,25 +168,25 @@  discard block
 block discarded – undo
168 168
     }
169 169
 
170 170
     /**
171
-	 * Get the post, if the ID is valid.
172
-	 *
173
-	 * @since 1.0.13
174
-	 *
175
-	 * @param int $discount_id Supplied ID.
176
-	 * @return WPInv_Item|WP_Error Item object if ID is valid, WP_Error otherwise.
177
-	 */
178
-	protected function get_post( $discount_id ) {
171
+     * Get the post, if the ID is valid.
172
+     *
173
+     * @since 1.0.13
174
+     *
175
+     * @param int $discount_id Supplied ID.
176
+     * @return WPInv_Item|WP_Error Item object if ID is valid, WP_Error otherwise.
177
+     */
178
+    protected function get_post( $discount_id ) {
179 179
 		
180
-		$error     = new WP_Error( 'rest_item_invalid_id', __( 'Invalid discount ID.', 'invoicing' ), array( 'status' => 404 ) );
180
+        $error     = new WP_Error( 'rest_item_invalid_id', __( 'Invalid discount ID.', 'invoicing' ), array( 'status' => 404 ) );
181 181
 
182 182
         // Ids start from 1
183 183
         if ( (int) $discount_id <= 0 ) {
184
-			return $error;
185
-		}
184
+            return $error;
185
+        }
186 186
 
187
-		$discount = wpinv_get_discount( (int) $discount_id );
188
-		if ( empty( $discount ) ) {
189
-			return $error;
187
+        $discount = wpinv_get_discount( (int) $discount_id );
188
+        if ( empty( $discount ) ) {
189
+            return $error;
190 190
         }
191 191
 
192 192
         return $discount;
@@ -194,25 +194,25 @@  discard block
 block discarded – undo
194 194
     }
195 195
 
196 196
     /**
197
-	 * Checks if a given request has access to read a discount.
198
-	 *
199
-	 * @since 1.0.13
200
-	 *
201
-	 * @param WP_REST_Request $request Full details about the request.
202
-	 * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise.
203
-	 */
204
-	public function get_item_permissions_check( $request ) {
197
+     * Checks if a given request has access to read a discount.
198
+     *
199
+     * @since 1.0.13
200
+     *
201
+     * @param WP_REST_Request $request Full details about the request.
202
+     * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise.
203
+     */
204
+    public function get_item_permissions_check( $request ) {
205 205
 
206 206
         // Retrieve the discount object.
207 207
         $discount = $this->get_post( $request['id'] );
208 208
         
209 209
         // Ensure it is valid.
210
-		if ( is_wp_error( $discount ) ) {
211
-			return $discount;
212
-		}
210
+        if ( is_wp_error( $discount ) ) {
211
+            return $discount;
212
+        }
213 213
 
214
-		if ( ! wpinv_current_user_can_manage_invoicing() ) {
215
-			return new WP_Error(
214
+        if ( ! wpinv_current_user_can_manage_invoicing() ) {
215
+            return new WP_Error(
216 216
                 'rest_cannot_view', 
217 217
                 __( 'Sorry, you are not allowed to view this discount.', 'invoicing' ), 
218 218
                 array( 
@@ -221,40 +221,40 @@  discard block
 block discarded – undo
221 221
             );
222 222
         }
223 223
 
224
-		return true;
224
+        return true;
225 225
     }
226 226
     
227 227
     /**
228
-	 * Retrieves a single invoice item.
229
-	 *
230
-	 * @since 1.0.13
231
-	 *
232
-	 * @param WP_REST_Request $request Full details about the request.
233
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
234
-	 */
235
-	public function get_item( $request ) {
228
+     * Retrieves a single invoice item.
229
+     *
230
+     * @since 1.0.13
231
+     *
232
+     * @param WP_REST_Request $request Full details about the request.
233
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
234
+     */
235
+    public function get_item( $request ) {
236 236
 
237 237
         // Fetch the discount.
238 238
         $discount = $this->get_post( $request['id'] );
239 239
         
240 240
         // Abort early if it does not exist
241
-		if ( is_wp_error( $discount ) ) {
242
-			return $discount;
243
-		}
244
-
245
-		// Prepare the response
246
-		$response = $this->prepare_item_for_response( $discount, $request );
247
-
248
-		/**
249
-		 * Filters the responses for single discount requests.
250
-		 *
251
-		 *
252
-		 * @since 1.0.13
253
-		 * @var WP_HTTP_Response
254
-		 *
255
-		 * @param WP_HTTP_Response $response Response.
256
-		 * @param WP_REST_Request  $request The request used.
257
-		 */
241
+        if ( is_wp_error( $discount ) ) {
242
+            return $discount;
243
+        }
244
+
245
+        // Prepare the response
246
+        $response = $this->prepare_item_for_response( $discount, $request );
247
+
248
+        /**
249
+         * Filters the responses for single discount requests.
250
+         *
251
+         *
252
+         * @since 1.0.13
253
+         * @var WP_HTTP_Response
254
+         *
255
+         * @param WP_HTTP_Response $response Response.
256
+         * @param WP_REST_Request  $request The request used.
257
+         */
258 258
         $response       = apply_filters( "wpinv_rest_get_discount_response", $response, $request );
259 259
 
260 260
         return rest_ensure_response( $response );
@@ -262,26 +262,26 @@  discard block
 block discarded – undo
262 262
     }
263 263
     
264 264
     /**
265
-	 * Checks if a given request has access to create an invoice item.
266
-	 *
267
-	 * @since 1.0.13
268
-	 *
269
-	 * @param WP_REST_Request $request Full details about the request.
270
-	 * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
271
-	 */
272
-	public function create_item_permissions_check( $request ) {
265
+     * Checks if a given request has access to create an invoice item.
266
+     *
267
+     * @since 1.0.13
268
+     *
269
+     * @param WP_REST_Request $request Full details about the request.
270
+     * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
271
+     */
272
+    public function create_item_permissions_check( $request ) {
273 273
 	
274
-		if ( ! empty( $request['id'] ) ) {
275
-			return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) );
276
-		}
274
+        if ( ! empty( $request['id'] ) ) {
275
+            return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) );
276
+        }
277 277
 
278
-		if ( wpinv_current_user_can_manage_invoicing() ) {
279
-			return true;
280
-		}
278
+        if ( wpinv_current_user_can_manage_invoicing() ) {
279
+            return true;
280
+        }
281 281
 
282
-		$post_type = get_post_type_object( $this->post_type );
283
-		if ( ! current_user_can( $post_type->cap->create_posts ) ) {
284
-			return new WP_Error( 
282
+        $post_type = get_post_type_object( $this->post_type );
283
+        if ( ! current_user_can( $post_type->cap->create_posts ) ) {
284
+            return new WP_Error( 
285 285
                 'rest_cannot_create', 
286 286
                 __( 'Sorry, you are not allowed to create discounts as this user.', 'invoicing' ), 
287 287
                 array( 
@@ -290,261 +290,261 @@  discard block
 block discarded – undo
290 290
             );
291 291
         }
292 292
 
293
-		return true;
293
+        return true;
294 294
     }
295 295
     
296 296
     /**
297
-	 * Creates a single discount.
298
-	 *
299
-	 * @since 1.0.13
300
-	 *
301
-	 * @param WP_REST_Request $request Full details about the request.
302
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
303
-	 */
304
-	public function create_item( $request ) {
305
-
306
-		if ( ! empty( $request['id'] ) ) {
307
-			return new WP_Error( 'rest_item_exists', __( 'Cannot create existing discount.', 'invoicing' ), array( 'status' => 400 ) );
308
-		}
297
+     * Creates a single discount.
298
+     *
299
+     * @since 1.0.13
300
+     *
301
+     * @param WP_REST_Request $request Full details about the request.
302
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
303
+     */
304
+    public function create_item( $request ) {
305
+
306
+        if ( ! empty( $request['id'] ) ) {
307
+            return new WP_Error( 'rest_item_exists', __( 'Cannot create existing discount.', 'invoicing' ), array( 'status' => 400 ) );
308
+        }
309 309
 
310
-		$request->set_param( 'context', 'edit' );
310
+        $request->set_param( 'context', 'edit' );
311 311
 
312
-		// Prepare the updated data.
313
-		$discount_data = $this->prepare_item_for_database( $request );
312
+        // Prepare the updated data.
313
+        $discount_data = $this->prepare_item_for_database( $request );
314 314
 
315
-		if ( is_wp_error( $discount_data ) ) {
316
-			return $discount_data;
317
-		}
315
+        if ( is_wp_error( $discount_data ) ) {
316
+            return $discount_data;
317
+        }
318 318
 
319
-		$discount_data['post_type'] = $this->post_type;
319
+        $discount_data['post_type'] = $this->post_type;
320 320
 
321
-		// Try creating the discount.
321
+        // Try creating the discount.
322 322
         $discount = wp_insert_post( $discount_data, true );
323 323
 
324
-		if ( is_wp_error( $discount ) ) {
324
+        if ( is_wp_error( $discount ) ) {
325 325
             return $discount;
326
-		}
327
-
328
-		// Prepare the response
329
-		$response = $this->prepare_item_for_response( $discount, $request );
330
-
331
-		/**
332
-		 * Fires after a single discount is created or updated via the REST API.
333
-		 *
334
-		 * @since 1.0.13
335
-		 *
336
-		 * @param WP_Post   $discount  Inserted or updated discount object.
337
-		 * @param WP_REST_Request $request  Request object.
338
-		 * @param bool            $creating True when creating a post, false when updating.
339
-		 */
340
-		do_action( "wpinv_rest_insert_discount", $discount, $request, true );
341
-
342
-		/**
343
-		 * Filters the responses for creating single item requests.
344
-		 *
345
-		 *
346
-		 * @since 1.0.13
347
-		 *
348
-		 *
349
-		 * @param array           $response Invoice properties.
350
-		 * @param WP_REST_Request $request The request used.
351
-		 */
326
+        }
327
+
328
+        // Prepare the response
329
+        $response = $this->prepare_item_for_response( $discount, $request );
330
+
331
+        /**
332
+         * Fires after a single discount is created or updated via the REST API.
333
+         *
334
+         * @since 1.0.13
335
+         *
336
+         * @param WP_Post   $discount  Inserted or updated discount object.
337
+         * @param WP_REST_Request $request  Request object.
338
+         * @param bool            $creating True when creating a post, false when updating.
339
+         */
340
+        do_action( "wpinv_rest_insert_discount", $discount, $request, true );
341
+
342
+        /**
343
+         * Filters the responses for creating single item requests.
344
+         *
345
+         *
346
+         * @since 1.0.13
347
+         *
348
+         *
349
+         * @param array           $response Invoice properties.
350
+         * @param WP_REST_Request $request The request used.
351
+         */
352 352
         $response       = apply_filters( "wpinv_rest_create_discount_response", $response, $request );
353 353
 
354 354
         return rest_ensure_response( $response );
355
-	}
356
-
357
-	/**
358
-	 * Checks if a given request has access to update a discount.
359
-	 *
360
-	 * @since 1.0.13
361
-	 *
362
-	 * @param WP_REST_Request $request Full details about the request.
363
-	 * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
364
-	 */
365
-	public function update_item_permissions_check( $request ) {
366
-
367
-		// Retrieve the item.
368
-		$item = $this->get_post( $request['id'] );
369
-		if ( is_wp_error( $item ) ) {
370
-			return $item;
371
-		}
372
-
373
-		if ( wpinv_current_user_can_manage_invoicing() ) {
374
-			return true;
375
-		}
376
-
377
-		return new WP_Error( 
378
-			'rest_cannot_edit', 
379
-			__( 'Sorry, you are not allowed to update this discount.', 'invoicing' ), 
380
-			array( 
381
-				'status' => rest_authorization_required_code(),
382
-			)
383
-		);
384
-
385
-	}
386
-
387
-	/**
388
-	 * Updates a single discount.
389
-	 *
390
-	 * @since 1.0.13
391
-	 *
392
-	 * @param WP_REST_Request $request Full details about the request.
393
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
394
-	 */
395
-	public function update_item( $request ) {
355
+    }
356
+
357
+    /**
358
+     * Checks if a given request has access to update a discount.
359
+     *
360
+     * @since 1.0.13
361
+     *
362
+     * @param WP_REST_Request $request Full details about the request.
363
+     * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
364
+     */
365
+    public function update_item_permissions_check( $request ) {
366
+
367
+        // Retrieve the item.
368
+        $item = $this->get_post( $request['id'] );
369
+        if ( is_wp_error( $item ) ) {
370
+            return $item;
371
+        }
372
+
373
+        if ( wpinv_current_user_can_manage_invoicing() ) {
374
+            return true;
375
+        }
376
+
377
+        return new WP_Error( 
378
+            'rest_cannot_edit', 
379
+            __( 'Sorry, you are not allowed to update this discount.', 'invoicing' ), 
380
+            array( 
381
+                'status' => rest_authorization_required_code(),
382
+            )
383
+        );
384
+
385
+    }
386
+
387
+    /**
388
+     * Updates a single discount.
389
+     *
390
+     * @since 1.0.13
391
+     *
392
+     * @param WP_REST_Request $request Full details about the request.
393
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
394
+     */
395
+    public function update_item( $request ) {
396 396
 		
397
-		// Ensure the item exists.
397
+        // Ensure the item exists.
398 398
         $valid_check = $this->get_post( $request['id'] );
399 399
         
400 400
         // Abort early if it does not exist
401
-		if ( is_wp_error( $valid_check ) ) {
402
-			return $valid_check;
403
-		}
401
+        if ( is_wp_error( $valid_check ) ) {
402
+            return $valid_check;
403
+        }
404 404
 
405
-		$request->set_param( 'context', 'edit' );
405
+        $request->set_param( 'context', 'edit' );
406 406
 
407
-		// Prepare the updated data.
408
-		$data_to_update = $this->prepare_item_for_database( $request );
407
+        // Prepare the updated data.
408
+        $data_to_update = $this->prepare_item_for_database( $request );
409 409
 
410
-		if ( is_wp_error( $data_to_update ) ) {
411
-			return $data_to_update;
412
-		}
410
+        if ( is_wp_error( $data_to_update ) ) {
411
+            return $data_to_update;
412
+        }
413 413
 
414
-		if( empty( $data_to_update['meta_input'] ) ) {
415
-			unset( $data_to_update['meta_input'] );
416
-		}
414
+        if( empty( $data_to_update['meta_input'] ) ) {
415
+            unset( $data_to_update['meta_input'] );
416
+        }
417 417
 
418
-		// Abort if no item data is provided
418
+        // Abort if no item data is provided
419 419
         if( empty( $data_to_update ) ) {
420 420
             return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) );
421
-		}
421
+        }
422 422
 		
423
-		// post_status
424
-		if( ! empty( $data_to_update['post_status'] ) ) {
425
-			wpinv_update_discount_status( $request['id'], $data_to_update['post_status'] );
426
-			unset( $data_to_update['post_status'] );
427
-		}
423
+        // post_status
424
+        if( ! empty( $data_to_update['post_status'] ) ) {
425
+            wpinv_update_discount_status( $request['id'], $data_to_update['post_status'] );
426
+            unset( $data_to_update['post_status'] );
427
+        }
428 428
 
429
-		// Update the item
430
-		if( ! empty( $data_to_update ) ) {
429
+        // Update the item
430
+        if( ! empty( $data_to_update ) ) {
431 431
 
432
-			// Include the item ID
433
-			$data_to_update['ID'] = $request['id'];
432
+            // Include the item ID
433
+            $data_to_update['ID'] = $request['id'];
434 434
 
435
-			$updated = wp_update_post( $data_to_update, true );
435
+            $updated = wp_update_post( $data_to_update, true );
436 436
 
437
-			// Incase the update operation failed...
438
-			if ( is_wp_error( $updated ) ) {
439
-				return $updated;
440
-			}
437
+            // Incase the update operation failed...
438
+            if ( is_wp_error( $updated ) ) {
439
+                return $updated;
440
+            }
441 441
 
442
-		}
442
+        }
443 443
 
444
-		$updated_discount = get_post( $request['id'] );
444
+        $updated_discount = get_post( $request['id'] );
445 445
 
446
-		// Prepare the response
447
-		$response = $this->prepare_item_for_response( $updated_discount, $request );
446
+        // Prepare the response
447
+        $response = $this->prepare_item_for_response( $updated_discount, $request );
448 448
 
449
-		/** This action is documented in includes/class-wpinv-rest-item-controller.php */
450
-		do_action( "wpinv_rest_insert_discount", $updated_discount, $request, false );
449
+        /** This action is documented in includes/class-wpinv-rest-item-controller.php */
450
+        do_action( "wpinv_rest_insert_discount", $updated_discount, $request, false );
451 451
 
452
-		/**
453
-		 * Filters the responses for updating single discount requests.
454
-		 *
455
-		 *
456
-		 * @since 1.0.13
457
-		 *
458
-		 *
459
-		 * @param array           $data_to_update Discount properties.
460
-		 * @param WP_REST_Request $request The request used.
461
-		 */
452
+        /**
453
+         * Filters the responses for updating single discount requests.
454
+         *
455
+         *
456
+         * @since 1.0.13
457
+         *
458
+         *
459
+         * @param array           $data_to_update Discount properties.
460
+         * @param WP_REST_Request $request The request used.
461
+         */
462 462
         $response       = apply_filters( "wpinv_rest_update_discount_response", $response,  $data_to_update, $request );
463 463
 
464 464
         return rest_ensure_response( $response );
465
-	}
466
-
467
-	/**
468
-	 * Checks if a given request has access to delete a discount.
469
-	 *
470
-	 * @since 1.0.13
471
-	 *
472
-	 * @param WP_REST_Request $request Full details about the request.
473
-	 * @return true|WP_Error True if the request has access to delete the discount, WP_Error object otherwise.
474
-	 */
475
-	public function delete_item_permissions_check( $request ) {
476
-
477
-		// Retrieve the discount.
478
-		$discount = $this->get_post( $request['id'] );
479
-		if ( is_wp_error( $discount ) ) {
480
-			return $discount;
481
-		} 
482
-
483
-		// Ensure the current user can delete the discount
484
-		if (! wpinv_current_user_can_manage_invoicing() ) {
485
-			return new WP_Error( 
465
+    }
466
+
467
+    /**
468
+     * Checks if a given request has access to delete a discount.
469
+     *
470
+     * @since 1.0.13
471
+     *
472
+     * @param WP_REST_Request $request Full details about the request.
473
+     * @return true|WP_Error True if the request has access to delete the discount, WP_Error object otherwise.
474
+     */
475
+    public function delete_item_permissions_check( $request ) {
476
+
477
+        // Retrieve the discount.
478
+        $discount = $this->get_post( $request['id'] );
479
+        if ( is_wp_error( $discount ) ) {
480
+            return $discount;
481
+        } 
482
+
483
+        // Ensure the current user can delete the discount
484
+        if (! wpinv_current_user_can_manage_invoicing() ) {
485
+            return new WP_Error( 
486 486
                 'rest_cannot_delete', 
487 487
                 __( 'Sorry, you are not allowed to delete this discount.', 'invoicing' ), 
488 488
                 array( 
489 489
                     'status' => rest_authorization_required_code(),
490 490
                 )
491 491
             );
492
-		}
493
-
494
-		return true;
495
-	}
496
-
497
-	/**
498
-	 * Deletes a single discount.
499
-	 *
500
-	 * @since 1.0.13
501
-	 *
502
-	 * @param WP_REST_Request $request Full details about the request.
503
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
504
-	 */
505
-	public function delete_item( $request ) {
492
+        }
493
+
494
+        return true;
495
+    }
496
+
497
+    /**
498
+     * Deletes a single discount.
499
+     *
500
+     * @since 1.0.13
501
+     *
502
+     * @param WP_REST_Request $request Full details about the request.
503
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
504
+     */
505
+    public function delete_item( $request ) {
506 506
 		
507
-		// Retrieve the discount.
508
-		$discount = $this->get_post( $request['id'] );
509
-		if ( is_wp_error( $discount ) ) {
510
-			return $discount;
511
-		}
507
+        // Retrieve the discount.
508
+        $discount = $this->get_post( $request['id'] );
509
+        if ( is_wp_error( $discount ) ) {
510
+            return $discount;
511
+        }
512 512
 
513
-		$request->set_param( 'context', 'edit' );
513
+        $request->set_param( 'context', 'edit' );
514 514
 
515
-		// Prepare the discount id
516
-		$id    = $discount->ID;
515
+        // Prepare the discount id
516
+        $id    = $discount->ID;
517 517
 
518
-		// Prepare the response
519
-		$response = $this->prepare_item_for_response( $discount, $request );
518
+        // Prepare the response
519
+        $response = $this->prepare_item_for_response( $discount, $request );
520 520
 
521
-		// Delete the discount...
522
-		wpinv_remove_discount( $id );
521
+        // Delete the discount...
522
+        wpinv_remove_discount( $id );
523 523
 
524
-		/**
525
-		 * Fires immediately after a single discount is deleted via the REST API.
526
-		 *
527
-		 *
528
-		 * @since 1.0.13
529
-		 *
530
-		 * @param WP_POST    $discount  The deleted discount.
531
-		 * @param WP_REST_Request  $request  The request sent to the API.
532
-		 */
533
-		do_action( "wpinv_rest_delete_discount", $discount, $request );
524
+        /**
525
+         * Fires immediately after a single discount is deleted via the REST API.
526
+         *
527
+         *
528
+         * @since 1.0.13
529
+         *
530
+         * @param WP_POST    $discount  The deleted discount.
531
+         * @param WP_REST_Request  $request  The request sent to the API.
532
+         */
533
+        do_action( "wpinv_rest_delete_discount", $discount, $request );
534 534
 
535
-		return $response;
535
+        return $response;
536 536
 
537
-	}
537
+    }
538 538
     
539 539
     
540 540
     /**
541
-	 * Retrieves the query params for the discount collection.
542
-	 *
543
-	 * @since 1.0.13
544
-	 *
545
-	 * @return array Collection parameters.
546
-	 */
547
-	public function get_collection_params() {
541
+     * Retrieves the query params for the discount collection.
542
+     *
543
+     * @since 1.0.13
544
+     *
545
+     * @return array Collection parameters.
546
+     */
547
+    public function get_collection_params() {
548 548
         
549 549
         $query_params               = array(
550 550
 
@@ -558,27 +558,27 @@  discard block
 block discarded – undo
558 558
             
559 559
             // Discount types
560 560
             'type'                  => array(
561
-				'description'       => __( 'Type of discounts to fetch.', 'invoicing' ),
562
-				'type'              => 'array',
563
-				'default'           => array_keys( wpinv_get_discount_types() ),
564
-				'items'             => array(
561
+                'description'       => __( 'Type of discounts to fetch.', 'invoicing' ),
562
+                'type'              => 'array',
563
+                'default'           => array_keys( wpinv_get_discount_types() ),
564
+                'items'             => array(
565 565
                     'enum'          => array_keys( wpinv_get_discount_types() ),
566 566
                     'type'          => 'string',
567 567
                 ),
568
-			),
568
+            ),
569 569
 			
570
-			// Number of results per page
570
+            // Number of results per page
571 571
             'limit'                 => array(
572
-				'description'       => __( 'Number of discounts to fetch.', 'invoicing' ),
573
-				'type'              => 'integer',
574
-				'default'           => (int) get_option( 'posts_per_page' ),
572
+                'description'       => __( 'Number of discounts to fetch.', 'invoicing' ),
573
+                'type'              => 'integer',
574
+                'default'           => (int) get_option( 'posts_per_page' ),
575 575
             ),
576 576
 
577 577
             // Pagination
578 578
             'page'     => array(
579
-				'description'       => __( 'Current page to fetch.', 'invoicing' ),
580
-				'type'              => 'integer',
581
-				'default'           => 1,
579
+                'description'       => __( 'Current page to fetch.', 'invoicing' ),
580
+                'type'              => 'integer',
581
+                'default'           => 1,
582 582
             ),
583 583
 
584 584
             // Exclude certain items
@@ -601,9 +601,9 @@  discard block
 block discarded – undo
601 601
                     'date',
602 602
                     'ID',
603 603
                     'modified',
604
-					'title',
605
-					'relevance',
606
-					'rand'
604
+                    'title',
605
+                    'relevance',
606
+                    'rand'
607 607
                 ),
608 608
             ),
609 609
 
@@ -613,599 +613,599 @@  discard block
 block discarded – undo
613 613
                 'type'        => 'string',
614 614
                 'default'     => 'DESC',
615 615
                 'enum'        => array( 'ASC', 'DESC' ),
616
-			),
616
+            ),
617 617
 			
618
-			// Search term
618
+            // Search term
619 619
             'search'                => array(
620
-				'description'       => __( 'Return discounts that match the search term.', 'invoicing' ),
621
-				'type'              => 'string',
620
+                'description'       => __( 'Return discounts that match the search term.', 'invoicing' ),
621
+                'type'              => 'string',
622 622
             ),
623 623
         );
624 624
 
625
-		/**
626
-		 * Filter collection parameters for the discounts controller.
627
-		 *
628
-		 *
629
-		 * @since 1.0.13
630
-		 *
631
-		 * @param array        $query_params JSON Schema-formatted collection parameters.
632
-		 */
633
-		return apply_filters( "wpinv_rest_discounts_collection_params", $query_params );
625
+        /**
626
+         * Filter collection parameters for the discounts controller.
627
+         *
628
+         *
629
+         * @since 1.0.13
630
+         *
631
+         * @param array        $query_params JSON Schema-formatted collection parameters.
632
+         */
633
+        return apply_filters( "wpinv_rest_discounts_collection_params", $query_params );
634 634
     }
635 635
     
636 636
     /**
637
-	 * Checks if a given post type can be viewed or managed.
638
-	 *
639
-	 * @since 1.0.13
640
-	 *
641
-	 * @param object|string $post_type Post type name or object.
642
-	 * @return bool Whether the post type is allowed in REST.
643
-	 */
644
-	protected function check_is_post_type_allowed( $post_type ) {
645
-		return true;
646
-	}
647
-
648
-	/**
649
-	 * Prepares a single item for create or update.
650
-	 *
651
-	 * @since 1.0.13
652
-	 *
653
-	 * @param WP_REST_Request $request Request object.
654
-	 * @return array|WP_Error Discount Properties or WP_Error.
655
-	 */
656
-	protected function prepare_item_for_database( $request ) {
657
-		$prepared_item 		 = new stdClass();
658
-		$prepared_item->meta_input = array();
659
-
660
-		// Post ID.
661
-		if ( isset( $request['id'] ) ) {
662
-			$existing_item = $this->get_post( $request['id'] );
663
-			if ( is_wp_error( $existing_item ) ) {
664
-				return $existing_item;
665
-			}
666
-
667
-			$prepared_item->ID 		  = $existing_item->ID;
668
-		}
669
-
670
-		$schema = $this->get_item_schema();
671
-
672
-		// item title.
673
-		if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
674
-			$prepared_item->post_title = sanitize_text_field( $request['title'] );
675
-		}
676
-
677
-		// item status.
678
-		if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && in_array( $request['status'], array_keys( get_post_stati( array( 'internal' => false ) ) ) ) ) {
679
-			$prepared_item->post_status = sanitize_text_field( $request['status'] );
680
-		}
681
-
682
-		// Code.
683
-		if ( ! empty( $schema['properties']['code'] ) && isset( $request['code'] ) ) {
684
-			$prepared_item->meta_input['_wpi_discount_code'] = trim( $request['code'] );
685
-		}
686
-
687
-		// Type.
688
-		if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] )  && in_array( $request['type'], array_keys( wpinv_get_discount_types() ) ) ) {
689
-			$prepared_item->meta_input['_wpi_discount_type'] = trim( $request['type'] );
690
-		}
691
-
692
-		// Amount.
693
-		if ( ! empty( $schema['properties']['amount'] ) && isset( $request['amount'] ) ) {
694
-			$prepared_item->meta_input['_wpi_discount_amount'] = floatval( $request['amount'] );
695
-		}
696
-
697
-		// Items.
698
-		if ( ! empty( $schema['properties']['items'] ) && isset( $request['items'] ) ) {
699
-			$prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list( $request['items'] );
700
-		}
701
-
702
-		// Excluded Items.
703
-		if ( ! empty( $schema['properties']['exclude_items'] ) && isset( $request['exclude_items'] ) ) {
704
-			$prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list( $request['exclude_items'] );
705
-		}
706
-
707
-		// Start date.
708
-		if ( ! empty( $schema['properties']['start_date'] ) && isset( $request['start_date'] ) ) {
709
-			$prepared_item->meta_input['_wpi_discount_start'] = trim( $request['start_date'] );
710
-		}
711
-
712
-		// End date.
713
-		if ( ! empty( $schema['properties']['end_date'] ) && isset( $request['end_date'] ) ) {
714
-			$prepared_item->meta_input['_wpi_discount_expiration'] = trim( $request['end_date'] );
715
-		}
716
-
717
-		// Minimum amount.
718
-		if ( ! empty( $schema['properties']['minimum_amount'] ) && isset( $request['minimum_amount'] ) ) {
719
-			$prepared_item->meta_input['_wpi_discount_min_total'] = floatval( $request['minimum_amount'] );
720
-		}
721
-
722
-		// Maximum amount.
723
-		if ( ! empty( $schema['properties']['maximum_amount'] ) && isset( $request['maximum_amount'] ) ) {
724
-			$prepared_item->meta_input['_wpi_discount_max_total'] = floatval( $request['maximum_amount'] );
725
-		}
726
-
727
-		// Recurring.
728
-		if ( ! empty( $schema['properties']['recurring'] ) && isset( $request['recurring'] ) ) {
729
-			$prepared_item->meta_input['_wpi_discount_is_recurring'] = empty( (int) $request['recurring'] ) ? 0 : 1;
730
-		}
731
-
732
-		// Maximum uses.
733
-		if ( ! empty( $schema['properties']['max_uses'] ) && isset( $request['max_uses'] ) ) {
734
-			$prepared_item->meta_input['_wpi_discount_max_uses'] = intval( $request['max_uses'] );
735
-		}
736
-
737
-		// Single use.
738
-		if ( ! empty( $schema['properties']['single_use'] ) && isset( $request['single_use'] ) ) {
739
-			$prepared_item->meta_input['_wpi_discount_is_single_use'] = empty( (int) $request['single_use'] ) ? 0 : 1;
740
-		}
741
-
742
-		$discount_data = (array) wp_unslash( $prepared_item );
743
-
744
-		/**
745
-		 * Filters an item before it is inserted via the REST API.
746
-		 *
747
-		 * @since 1.0.13
748
-		 *
749
-		 * @param array        $discount_data An array of discount data
750
-		 * @param WP_REST_Request $request       Request object.
751
-		 */
752
-		return apply_filters( "wpinv_rest_pre_insert_discount", $discount_data, $request );
753
-
754
-	}
755
-
756
-	/**
757
-	 * Prepares a single discount output for response.
758
-	 *
759
-	 * @since 1.0.13
760
-	 *
761
-	 * @param WP_Post   $discount    WP_Post object.
762
-	 * @param WP_REST_Request $request Request object.
763
-	 * @return WP_REST_Response Response object.
764
-	 */
765
-	public function prepare_item_for_response( $discount, $request ) {
766
-
767
-		$GLOBALS['post'] = get_post( $discount->ID );
768
-
769
-		setup_postdata( $discount->ID );
770
-
771
-		// Fetch the fields to include in this response.
772
-		$fields = $this->get_fields_for_response( $request );
773
-
774
-		// Base fields for every discount.
775
-		$data = array();
776
-
777
-		// Set up ID.
778
-		if ( rest_is_field_included( 'id', $fields ) ) {
779
-			$data['id'] = $discount->ID;
780
-		}
781
-
782
-		// Title.
783
-		if ( rest_is_field_included( 'title', $fields ) ) {
784
-			$data['title'] = get_the_title( $discount->ID );
785
-		}
786
-
787
-		// Code.
788
-		if ( rest_is_field_included( 'code', $fields ) ) {
789
-			$data['code'] = wpinv_get_discount_code( $discount->ID );
790
-		}
791
-
792
-		// Type.
793
-		if ( rest_is_field_included( 'type', $fields ) ) {
794
-			$data['type'] = wpinv_get_discount_type( $discount->ID );
795
-		}
796
-
797
-		// Amount.
798
-		if ( rest_is_field_included( 'amount', $fields ) ) {
799
-			$data['amount'] = wpinv_get_discount_amount( $discount->ID );
800
-		}
801
-
802
-		// Status.
803
-		if ( rest_is_field_included( 'status', $fields ) ) {
804
-			$data['status'] = get_post_status( $discount->ID );
805
-		}
806
-
807
-		// Items.
808
-		if ( rest_is_field_included( 'items', $fields ) ) {
809
-			$data['items'] = wpinv_get_discount_item_reqs( $discount->ID );
810
-		}
811
-
812
-		// Excluded Items.
813
-		if ( rest_is_field_included( 'exclude_items', $fields ) ) {
814
-			$data['exclude_items'] = wpinv_get_discount_excluded_items( $discount->ID );
815
-		}
816
-
817
-		// Start date.
818
-		if ( rest_is_field_included( 'start_date', $fields ) ) {
819
-			$data['start_date'] = wpinv_get_discount_start_date( $discount->ID );
820
-		}
821
-
822
-		// End date.
823
-		if ( rest_is_field_included( 'end_date', $fields ) ) {
824
-			$data['end_date'] = wpinv_get_discount_expiration( $discount->ID );
825
-		}
826
-
827
-		// Minimum amount.
828
-		if ( rest_is_field_included( 'minimum_amount', $fields ) ) {
829
-			$data['minimum_amount'] = wpinv_get_discount_min_total( $discount->ID );
830
-		}
831
-
832
-		// Maximum amount.
833
-		if ( rest_is_field_included( 'maximum_amount', $fields ) ) {
834
-			$data['maximum_amount'] = wpinv_get_discount_max_total( $discount->ID );
835
-		}
836
-
837
-		// Recurring.
838
-		if ( rest_is_field_included( 'recurring', $fields ) ) {
839
-			$data['recurring'] = wpinv_discount_is_recurring( $discount->ID );
840
-		}
841
-
842
-		// Maximum uses.
843
-		if ( rest_is_field_included( 'max_uses', $fields ) ) {
844
-			$data['max_uses'] = wpinv_get_discount_max_uses( $discount->ID );
845
-		}
846
-
847
-		// Single use.
848
-		if ( rest_is_field_included( 'single_use', $fields ) ) {
849
-			$data['single_use'] = wpinv_discount_is_single_use( $discount->ID );
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( $discount );
860
-		$response->add_links( $links );
861
-
862
-		if ( ! empty( $links['self']['href'] ) ) {
863
-			$actions = $this->get_available_actions( $discount, $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 discount data for a response.
874
-		 *
875
-		 * @since 1.0.13
876
-		 *
877
-		 * @param WP_REST_Response $response The response object.
878
-		 * @param WP_Post    $discount  The discount post object.
879
-		 * @param WP_REST_Request  $request  Request object.
880
-		 */
881
-		return apply_filters( "wpinv_rest_prepare_discount", $response, $discount, $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 discount's schema, conforming to JSON Schema.
955
-	 *
956
-	 * @since 1.0.13
957
-	 *
958
-	 * @return array Discount 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 discount.', 'invoicing' ),
977
-					'type'        => 'integer',
978
-					'context'     => array( 'view', 'edit', 'embed' ),
979
-					'readonly'    => true,
980
-				),
981
-
982
-				'title'			  => array(
983
-					'description' => __( 'The title for the discount.', 'invoicing' ),
984
-					'type'        => 'string',
985
-					'context'     => array( 'view', 'edit' ),
986
-				),
987
-
988
-				'code'        => array(
989
-					'description' => __( 'The discount code.', 'invoicing' ),
990
-					'type'        => 'string',
991
-					'context'     => array( 'view', 'edit', 'embed' ),
992
-					'required'	  => true,
993
-				),
994
-
995
-				'type'        => array(
996
-					'description' => __( 'The type of discount.', 'invoicing' ),
997
-					'type'        => 'string',
998
-					'enum'        => array_keys( wpinv_get_discount_types() ),
999
-					'context'     => array( 'view', 'edit', 'embed' ),
1000
-					'default'	  => 'percentage',
1001
-				),
1002
-
1003
-				'amount'        => array(
1004
-					'description' => __( 'The discount value.', 'invoicing' ),
1005
-					'type'        => 'number',
1006
-					'context'     => array( 'view', 'edit', 'embed' ),
1007
-					'required'	  => true,
1008
-				),
1009
-
1010
-				'status'       => array(
1011
-					'description' => __( 'A named status for the discount.', 'invoicing' ),
1012
-					'type'        => 'string',
1013
-					'enum'        => array_keys( get_post_stati( array( 'internal' => false ) ) ),
1014
-					'context'     => array( 'view', 'edit' ),
1015
-				),
1016
-
1017
-				'items'       => array(
1018
-					'description' => __( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing' ),
1019
-					'type'        => 'array',
1020
-					'context'     => array( 'view', 'edit' ),
1021
-				),
1022
-
1023
-				'exclude_items'   => array(
1024
-					'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ),
1025
-					'type'        => 'array',
1026
-					'context'     => array( 'view', 'edit' ),
1027
-				),
1028
-
1029
-				'start_date'       => array(
1030
-					'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss  . If provided, the discount can only be used after or on this date.', 'invoicing' ),
1031
-					'type'        => 'string',
1032
-					'context'     => array( 'view', 'edit' ),
1033
-				),
1034
-
1035
-				'end_date'        => array(
1036
-					'description' => __( 'The expiration date for the discount.', 'invoicing' ),
1037
-					'type'        => 'string',
1038
-					'context'     => array( 'view', 'edit', 'embed' ),
1039
-				),
637
+     * Checks if a given post type can be viewed or managed.
638
+     *
639
+     * @since 1.0.13
640
+     *
641
+     * @param object|string $post_type Post type name or object.
642
+     * @return bool Whether the post type is allowed in REST.
643
+     */
644
+    protected function check_is_post_type_allowed( $post_type ) {
645
+        return true;
646
+    }
647
+
648
+    /**
649
+     * Prepares a single item for create or update.
650
+     *
651
+     * @since 1.0.13
652
+     *
653
+     * @param WP_REST_Request $request Request object.
654
+     * @return array|WP_Error Discount Properties or WP_Error.
655
+     */
656
+    protected function prepare_item_for_database( $request ) {
657
+        $prepared_item 		 = new stdClass();
658
+        $prepared_item->meta_input = array();
659
+
660
+        // Post ID.
661
+        if ( isset( $request['id'] ) ) {
662
+            $existing_item = $this->get_post( $request['id'] );
663
+            if ( is_wp_error( $existing_item ) ) {
664
+                return $existing_item;
665
+            }
666
+
667
+            $prepared_item->ID 		  = $existing_item->ID;
668
+        }
669
+
670
+        $schema = $this->get_item_schema();
671
+
672
+        // item title.
673
+        if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
674
+            $prepared_item->post_title = sanitize_text_field( $request['title'] );
675
+        }
676
+
677
+        // item status.
678
+        if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && in_array( $request['status'], array_keys( get_post_stati( array( 'internal' => false ) ) ) ) ) {
679
+            $prepared_item->post_status = sanitize_text_field( $request['status'] );
680
+        }
681
+
682
+        // Code.
683
+        if ( ! empty( $schema['properties']['code'] ) && isset( $request['code'] ) ) {
684
+            $prepared_item->meta_input['_wpi_discount_code'] = trim( $request['code'] );
685
+        }
686
+
687
+        // Type.
688
+        if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] )  && in_array( $request['type'], array_keys( wpinv_get_discount_types() ) ) ) {
689
+            $prepared_item->meta_input['_wpi_discount_type'] = trim( $request['type'] );
690
+        }
691
+
692
+        // Amount.
693
+        if ( ! empty( $schema['properties']['amount'] ) && isset( $request['amount'] ) ) {
694
+            $prepared_item->meta_input['_wpi_discount_amount'] = floatval( $request['amount'] );
695
+        }
696
+
697
+        // Items.
698
+        if ( ! empty( $schema['properties']['items'] ) && isset( $request['items'] ) ) {
699
+            $prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list( $request['items'] );
700
+        }
701
+
702
+        // Excluded Items.
703
+        if ( ! empty( $schema['properties']['exclude_items'] ) && isset( $request['exclude_items'] ) ) {
704
+            $prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list( $request['exclude_items'] );
705
+        }
706
+
707
+        // Start date.
708
+        if ( ! empty( $schema['properties']['start_date'] ) && isset( $request['start_date'] ) ) {
709
+            $prepared_item->meta_input['_wpi_discount_start'] = trim( $request['start_date'] );
710
+        }
711
+
712
+        // End date.
713
+        if ( ! empty( $schema['properties']['end_date'] ) && isset( $request['end_date'] ) ) {
714
+            $prepared_item->meta_input['_wpi_discount_expiration'] = trim( $request['end_date'] );
715
+        }
716
+
717
+        // Minimum amount.
718
+        if ( ! empty( $schema['properties']['minimum_amount'] ) && isset( $request['minimum_amount'] ) ) {
719
+            $prepared_item->meta_input['_wpi_discount_min_total'] = floatval( $request['minimum_amount'] );
720
+        }
721
+
722
+        // Maximum amount.
723
+        if ( ! empty( $schema['properties']['maximum_amount'] ) && isset( $request['maximum_amount'] ) ) {
724
+            $prepared_item->meta_input['_wpi_discount_max_total'] = floatval( $request['maximum_amount'] );
725
+        }
726
+
727
+        // Recurring.
728
+        if ( ! empty( $schema['properties']['recurring'] ) && isset( $request['recurring'] ) ) {
729
+            $prepared_item->meta_input['_wpi_discount_is_recurring'] = empty( (int) $request['recurring'] ) ? 0 : 1;
730
+        }
731
+
732
+        // Maximum uses.
733
+        if ( ! empty( $schema['properties']['max_uses'] ) && isset( $request['max_uses'] ) ) {
734
+            $prepared_item->meta_input['_wpi_discount_max_uses'] = intval( $request['max_uses'] );
735
+        }
736
+
737
+        // Single use.
738
+        if ( ! empty( $schema['properties']['single_use'] ) && isset( $request['single_use'] ) ) {
739
+            $prepared_item->meta_input['_wpi_discount_is_single_use'] = empty( (int) $request['single_use'] ) ? 0 : 1;
740
+        }
741
+
742
+        $discount_data = (array) wp_unslash( $prepared_item );
743
+
744
+        /**
745
+         * Filters an item before it is inserted via the REST API.
746
+         *
747
+         * @since 1.0.13
748
+         *
749
+         * @param array        $discount_data An array of discount data
750
+         * @param WP_REST_Request $request       Request object.
751
+         */
752
+        return apply_filters( "wpinv_rest_pre_insert_discount", $discount_data, $request );
753
+
754
+    }
755
+
756
+    /**
757
+     * Prepares a single discount output for response.
758
+     *
759
+     * @since 1.0.13
760
+     *
761
+     * @param WP_Post   $discount    WP_Post object.
762
+     * @param WP_REST_Request $request Request object.
763
+     * @return WP_REST_Response Response object.
764
+     */
765
+    public function prepare_item_for_response( $discount, $request ) {
766
+
767
+        $GLOBALS['post'] = get_post( $discount->ID );
768
+
769
+        setup_postdata( $discount->ID );
770
+
771
+        // Fetch the fields to include in this response.
772
+        $fields = $this->get_fields_for_response( $request );
773
+
774
+        // Base fields for every discount.
775
+        $data = array();
776
+
777
+        // Set up ID.
778
+        if ( rest_is_field_included( 'id', $fields ) ) {
779
+            $data['id'] = $discount->ID;
780
+        }
781
+
782
+        // Title.
783
+        if ( rest_is_field_included( 'title', $fields ) ) {
784
+            $data['title'] = get_the_title( $discount->ID );
785
+        }
786
+
787
+        // Code.
788
+        if ( rest_is_field_included( 'code', $fields ) ) {
789
+            $data['code'] = wpinv_get_discount_code( $discount->ID );
790
+        }
791
+
792
+        // Type.
793
+        if ( rest_is_field_included( 'type', $fields ) ) {
794
+            $data['type'] = wpinv_get_discount_type( $discount->ID );
795
+        }
796
+
797
+        // Amount.
798
+        if ( rest_is_field_included( 'amount', $fields ) ) {
799
+            $data['amount'] = wpinv_get_discount_amount( $discount->ID );
800
+        }
801
+
802
+        // Status.
803
+        if ( rest_is_field_included( 'status', $fields ) ) {
804
+            $data['status'] = get_post_status( $discount->ID );
805
+        }
806
+
807
+        // Items.
808
+        if ( rest_is_field_included( 'items', $fields ) ) {
809
+            $data['items'] = wpinv_get_discount_item_reqs( $discount->ID );
810
+        }
811
+
812
+        // Excluded Items.
813
+        if ( rest_is_field_included( 'exclude_items', $fields ) ) {
814
+            $data['exclude_items'] = wpinv_get_discount_excluded_items( $discount->ID );
815
+        }
816
+
817
+        // Start date.
818
+        if ( rest_is_field_included( 'start_date', $fields ) ) {
819
+            $data['start_date'] = wpinv_get_discount_start_date( $discount->ID );
820
+        }
821
+
822
+        // End date.
823
+        if ( rest_is_field_included( 'end_date', $fields ) ) {
824
+            $data['end_date'] = wpinv_get_discount_expiration( $discount->ID );
825
+        }
826
+
827
+        // Minimum amount.
828
+        if ( rest_is_field_included( 'minimum_amount', $fields ) ) {
829
+            $data['minimum_amount'] = wpinv_get_discount_min_total( $discount->ID );
830
+        }
831
+
832
+        // Maximum amount.
833
+        if ( rest_is_field_included( 'maximum_amount', $fields ) ) {
834
+            $data['maximum_amount'] = wpinv_get_discount_max_total( $discount->ID );
835
+        }
836
+
837
+        // Recurring.
838
+        if ( rest_is_field_included( 'recurring', $fields ) ) {
839
+            $data['recurring'] = wpinv_discount_is_recurring( $discount->ID );
840
+        }
841
+
842
+        // Maximum uses.
843
+        if ( rest_is_field_included( 'max_uses', $fields ) ) {
844
+            $data['max_uses'] = wpinv_get_discount_max_uses( $discount->ID );
845
+        }
846
+
847
+        // Single use.
848
+        if ( rest_is_field_included( 'single_use', $fields ) ) {
849
+            $data['single_use'] = wpinv_discount_is_single_use( $discount->ID );
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( $discount );
860
+        $response->add_links( $links );
861
+
862
+        if ( ! empty( $links['self']['href'] ) ) {
863
+            $actions = $this->get_available_actions( $discount, $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 discount data for a response.
874
+         *
875
+         * @since 1.0.13
876
+         *
877
+         * @param WP_REST_Response $response The response object.
878
+         * @param WP_Post    $discount  The discount post object.
879
+         * @param WP_REST_Request  $request  Request object.
880
+         */
881
+        return apply_filters( "wpinv_rest_prepare_discount", $response, $discount, $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 discount's schema, conforming to JSON Schema.
955
+     *
956
+     * @since 1.0.13
957
+     *
958
+     * @return array Discount 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 discount.', 'invoicing' ),
977
+                    'type'        => 'integer',
978
+                    'context'     => array( 'view', 'edit', 'embed' ),
979
+                    'readonly'    => true,
980
+                ),
981
+
982
+                'title'			  => array(
983
+                    'description' => __( 'The title for the discount.', 'invoicing' ),
984
+                    'type'        => 'string',
985
+                    'context'     => array( 'view', 'edit' ),
986
+                ),
987
+
988
+                'code'        => array(
989
+                    'description' => __( 'The discount code.', 'invoicing' ),
990
+                    'type'        => 'string',
991
+                    'context'     => array( 'view', 'edit', 'embed' ),
992
+                    'required'	  => true,
993
+                ),
994
+
995
+                'type'        => array(
996
+                    'description' => __( 'The type of discount.', 'invoicing' ),
997
+                    'type'        => 'string',
998
+                    'enum'        => array_keys( wpinv_get_discount_types() ),
999
+                    'context'     => array( 'view', 'edit', 'embed' ),
1000
+                    'default'	  => 'percentage',
1001
+                ),
1002
+
1003
+                'amount'        => array(
1004
+                    'description' => __( 'The discount value.', 'invoicing' ),
1005
+                    'type'        => 'number',
1006
+                    'context'     => array( 'view', 'edit', 'embed' ),
1007
+                    'required'	  => true,
1008
+                ),
1009
+
1010
+                'status'       => array(
1011
+                    'description' => __( 'A named status for the discount.', 'invoicing' ),
1012
+                    'type'        => 'string',
1013
+                    'enum'        => array_keys( get_post_stati( array( 'internal' => false ) ) ),
1014
+                    'context'     => array( 'view', 'edit' ),
1015
+                ),
1016
+
1017
+                'items'       => array(
1018
+                    'description' => __( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing' ),
1019
+                    'type'        => 'array',
1020
+                    'context'     => array( 'view', 'edit' ),
1021
+                ),
1022
+
1023
+                'exclude_items'   => array(
1024
+                    'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ),
1025
+                    'type'        => 'array',
1026
+                    'context'     => array( 'view', 'edit' ),
1027
+                ),
1028
+
1029
+                'start_date'       => array(
1030
+                    'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss  . If provided, the discount can only be used after or on this date.', 'invoicing' ),
1031
+                    'type'        => 'string',
1032
+                    'context'     => array( 'view', 'edit' ),
1033
+                ),
1034
+
1035
+                'end_date'        => array(
1036
+                    'description' => __( 'The expiration date for the discount.', 'invoicing' ),
1037
+                    'type'        => 'string',
1038
+                    'context'     => array( 'view', 'edit', 'embed' ),
1039
+                ),
1040 1040
 				
1041
-				'minimum_amount'       => array(
1042
-					'description' => __( 'Minimum amount needed to use this invoice.', 'invoicing' ),
1043
-					'type'        => 'number',
1044
-					'context'     => array( 'view', 'edit', 'embed' ),
1045
-				),
1046
-
1047
-				'maximum_amount'       => array(
1048
-					'description' => __( 'Maximum amount needed to use this invoice.', 'invoicing' ),
1049
-					'type'        => 'number',
1050
-					'context'     => array( 'view', 'edit', 'embed' ),
1051
-				),
1052
-
1053
-				'recurring'       => array(
1054
-					'description' => __( 'Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing' ),
1055
-					'type'        => 'integer',
1056
-					'context'     => array( 'view', 'edit', 'embed' ),
1057
-				),
1058
-
1059
-				'max_uses'        => array(
1060
-					'description' => __( 'The maximum number of times this discount code can be used.', 'invoicing' ),
1061
-					'type'        => 'number',
1062
-					'context'     => array( 'view', 'edit', 'embed' ),
1063
-				),
1064
-
1065
-				'single_use'       => array(
1066
-					'description' => __( 'Whether or not this discount can only be used once per user.', 'invoicing' ),
1067
-					'type'        => 'integer',
1068
-					'context'     => array( 'view', 'edit', 'embed' ),
1069
-				)
1070
-
1071
-			),
1072
-		);
1073
-
1074
-		// Add helpful links to the discount schem.
1075
-		$schema['links'] = $this->get_schema_links();
1076
-
1077
-		/**
1078
-		 * Filters the discount schema for the REST API.
1079
-		 *
1080
-		 * Enables adding extra properties to discounts.
1081
-		 *
1082
-		 * @since 1.0.13
1083
-		 *
1084
-		 * @param array   $schema    The discount schema.
1085
-		 */
1041
+                'minimum_amount'       => array(
1042
+                    'description' => __( 'Minimum amount needed to use this invoice.', 'invoicing' ),
1043
+                    'type'        => 'number',
1044
+                    'context'     => array( 'view', 'edit', 'embed' ),
1045
+                ),
1046
+
1047
+                'maximum_amount'       => array(
1048
+                    'description' => __( 'Maximum amount needed to use this invoice.', 'invoicing' ),
1049
+                    'type'        => 'number',
1050
+                    'context'     => array( 'view', 'edit', 'embed' ),
1051
+                ),
1052
+
1053
+                'recurring'       => array(
1054
+                    'description' => __( 'Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing' ),
1055
+                    'type'        => 'integer',
1056
+                    'context'     => array( 'view', 'edit', 'embed' ),
1057
+                ),
1058
+
1059
+                'max_uses'        => array(
1060
+                    'description' => __( 'The maximum number of times this discount code can be used.', 'invoicing' ),
1061
+                    'type'        => 'number',
1062
+                    'context'     => array( 'view', 'edit', 'embed' ),
1063
+                ),
1064
+
1065
+                'single_use'       => array(
1066
+                    'description' => __( 'Whether or not this discount can only be used once per user.', 'invoicing' ),
1067
+                    'type'        => 'integer',
1068
+                    'context'     => array( 'view', 'edit', 'embed' ),
1069
+                )
1070
+
1071
+            ),
1072
+        );
1073
+
1074
+        // Add helpful links to the discount schem.
1075
+        $schema['links'] = $this->get_schema_links();
1076
+
1077
+        /**
1078
+         * Filters the discount schema for the REST API.
1079
+         *
1080
+         * Enables adding extra properties to discounts.
1081
+         *
1082
+         * @since 1.0.13
1083
+         *
1084
+         * @param array   $schema    The discount schema.
1085
+         */
1086 1086
         $schema = apply_filters( "wpinv_rest_discount_schema", $schema );
1087 1087
 
1088
-		//  Cache the discount schema.
1089
-		$this->schema = $schema;
1088
+        //  Cache the discount schema.
1089
+        $this->schema = $schema;
1090 1090
 		
1091
-		return $this->add_additional_fields_schema( $this->schema );
1092
-	}
1093
-
1094
-	/**
1095
-	 * Retrieve Link Description Objects that should be added to the Schema for the discounts collection.
1096
-	 *
1097
-	 * @since 1.0.13
1098
-	 *
1099
-	 * @return array
1100
-	 */
1101
-	protected function get_schema_links() {
1102
-
1103
-		$href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
1104
-
1105
-		$links = array();
1106
-
1107
-		$links[] = array(
1108
-			'rel'          => 'https://api.w.org/action-publish',
1109
-			'title'        => __( 'The current user can publish this discount.' ),
1110
-			'href'         => $href,
1111
-			'targetSchema' => array(
1112
-				'type'       => 'object',
1113
-				'properties' => array(
1114
-					'status' => array(
1115
-						'type' => 'string',
1116
-						'enum' => array( 'publish', 'future' ),
1117
-					),
1118
-				),
1119
-			),
1120
-		);
1121
-
1122
-		return $links;
1123
-	}
1124
-
1125
-	/**
1126
-	 * Prepares links for the request.
1127
-	 *
1128
-	 * @since 1.0.13
1129
-	 *
1130
-	 * @param WP_Post $discount Post Object.
1131
-	 * @return array Links for the given discount.
1132
-	 */
1133
-	protected function prepare_links( $discount ) {
1134
-
1135
-		// Prepare the base REST API endpoint for discounts.
1136
-		$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
1137
-
1138
-		// Entity meta.
1139
-		$links = array(
1140
-			'self'       => array(
1141
-				'href' => rest_url( trailingslashit( $base ) . $discount->ID ),
1142
-			),
1143
-			'collection' => array(
1144
-				'href' => rest_url( $base ),
1145
-			),
1146
-		);
1147
-
1148
-		/**
1149
-		 * Filters the returned discount links for the REST API.
1150
-		 *
1151
-		 * Enables adding extra links to discount API responses.
1152
-		 *
1153
-		 * @since 1.0.13
1154
-		 *
1155
-		 * @param array   $links    Rest links.
1156
-		 */
1157
-		return apply_filters( "wpinv_rest_discount_links", $links );
1158
-
1159
-	}
1160
-
1161
-	/**
1162
-	 * Get the link relations available for the post and current user.
1163
-	 *
1164
-	 * @since 1.0.13
1165
-	 *
1166
-	 * @param WP_Post   $discount    WP_Post object.
1167
-	 * @param WP_REST_Request $request Request object.
1168
-	 * @return array List of link relations.
1169
-	 */
1170
-	protected function get_available_actions( $discount, $request ) {
1171
-
1172
-		if ( 'edit' !== $request['context'] ) {
1173
-			return array();
1174
-		}
1175
-
1176
-		$rels = array();
1177
-
1178
-		// Retrieve the post type object.
1179
-		$post_type = get_post_type_object( $discount->post_type );
1180
-
1181
-		// Mark discount as published.
1182
-		if ( current_user_can( $post_type->cap->publish_posts ) ) {
1183
-			$rels[] = 'https://api.w.org/action-publish';
1184
-		}
1185
-
1186
-		/**
1187
-		 * Filters the available discount link relations for the REST API.
1188
-		 *
1189
-		 * Enables adding extra link relation for the current user and request to discount responses.
1190
-		 *
1191
-		 * @since 1.0.13
1192
-		 *
1193
-		 * @param array   $rels    Available link relations.
1194
-		 */
1195
-		return apply_filters( "wpinv_rest_discount_link_relations", $rels );
1196
-	}
1197
-
1198
-	/**
1199
-	 * Handles rest requests for discount types.
1200
-	 *
1201
-	 * @since 1.0.13
1202
-	 * 
1203
-	 * 
1204
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
1205
-	 */
1206
-	public function get_discount_types() {
1207
-		return rest_ensure_response( wpinv_get_discount_types() );
1208
-	}
1091
+        return $this->add_additional_fields_schema( $this->schema );
1092
+    }
1093
+
1094
+    /**
1095
+     * Retrieve Link Description Objects that should be added to the Schema for the discounts collection.
1096
+     *
1097
+     * @since 1.0.13
1098
+     *
1099
+     * @return array
1100
+     */
1101
+    protected function get_schema_links() {
1102
+
1103
+        $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
1104
+
1105
+        $links = array();
1106
+
1107
+        $links[] = array(
1108
+            'rel'          => 'https://api.w.org/action-publish',
1109
+            'title'        => __( 'The current user can publish this discount.' ),
1110
+            'href'         => $href,
1111
+            'targetSchema' => array(
1112
+                'type'       => 'object',
1113
+                'properties' => array(
1114
+                    'status' => array(
1115
+                        'type' => 'string',
1116
+                        'enum' => array( 'publish', 'future' ),
1117
+                    ),
1118
+                ),
1119
+            ),
1120
+        );
1121
+
1122
+        return $links;
1123
+    }
1124
+
1125
+    /**
1126
+     * Prepares links for the request.
1127
+     *
1128
+     * @since 1.0.13
1129
+     *
1130
+     * @param WP_Post $discount Post Object.
1131
+     * @return array Links for the given discount.
1132
+     */
1133
+    protected function prepare_links( $discount ) {
1134
+
1135
+        // Prepare the base REST API endpoint for discounts.
1136
+        $base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
1137
+
1138
+        // Entity meta.
1139
+        $links = array(
1140
+            'self'       => array(
1141
+                'href' => rest_url( trailingslashit( $base ) . $discount->ID ),
1142
+            ),
1143
+            'collection' => array(
1144
+                'href' => rest_url( $base ),
1145
+            ),
1146
+        );
1147
+
1148
+        /**
1149
+         * Filters the returned discount links for the REST API.
1150
+         *
1151
+         * Enables adding extra links to discount API responses.
1152
+         *
1153
+         * @since 1.0.13
1154
+         *
1155
+         * @param array   $links    Rest links.
1156
+         */
1157
+        return apply_filters( "wpinv_rest_discount_links", $links );
1158
+
1159
+    }
1160
+
1161
+    /**
1162
+     * Get the link relations available for the post and current user.
1163
+     *
1164
+     * @since 1.0.13
1165
+     *
1166
+     * @param WP_Post   $discount    WP_Post object.
1167
+     * @param WP_REST_Request $request Request object.
1168
+     * @return array List of link relations.
1169
+     */
1170
+    protected function get_available_actions( $discount, $request ) {
1171
+
1172
+        if ( 'edit' !== $request['context'] ) {
1173
+            return array();
1174
+        }
1175
+
1176
+        $rels = array();
1177
+
1178
+        // Retrieve the post type object.
1179
+        $post_type = get_post_type_object( $discount->post_type );
1180
+
1181
+        // Mark discount as published.
1182
+        if ( current_user_can( $post_type->cap->publish_posts ) ) {
1183
+            $rels[] = 'https://api.w.org/action-publish';
1184
+        }
1185
+
1186
+        /**
1187
+         * Filters the available discount link relations for the REST API.
1188
+         *
1189
+         * Enables adding extra link relation for the current user and request to discount responses.
1190
+         *
1191
+         * @since 1.0.13
1192
+         *
1193
+         * @param array   $rels    Available link relations.
1194
+         */
1195
+        return apply_filters( "wpinv_rest_discount_link_relations", $rels );
1196
+    }
1197
+
1198
+    /**
1199
+     * Handles rest requests for discount types.
1200
+     *
1201
+     * @since 1.0.13
1202
+     * 
1203
+     * 
1204
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
1205
+     */
1206
+    public function get_discount_types() {
1207
+        return rest_ensure_response( wpinv_get_discount_types() );
1208
+    }
1209 1209
 
1210 1210
     
1211 1211
 }
1212 1212
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +261 added lines, -261 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
68 68
 			array(
69 69
 				array(
70 70
 					'methods'             => WP_REST_Server::READABLE,
71
-					'callback'            => array( $this, 'get_discount_types' ),
71
+					'callback'            => array($this, 'get_discount_types'),
72 72
 				),
73 73
 			)
74 74
 		);
@@ -84,13 +84,13 @@  discard block
 block discarded – undo
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 ( wpinv_current_user_can_manage_invoicing() ) {
89
+		if (wpinv_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 discounts.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
93
+		return new WP_Error('rest_forbidden_context', __('Sorry, you are not allowed to view invoice discounts.', 'invoicing'), array('status' => rest_authorization_required_code()));
94 94
 
95 95
     }
96 96
     
@@ -102,17 +102,17 @@  discard block
 block discarded – undo
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,28 +127,28 @@  discard block
 block discarded – undo
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_discounts_arguments", $args, $request, $this );
130
+        $args = apply_filters("wpinv_rest_get_discounts_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_discounts( $args );
137
+		$query = wpinv_get_all_discounts($args);
138 138
 		
139 139
 		// Prepare the retrieved discounts
140 140
 		$discounts = array();
141
-		foreach( $query->discounts as $discount ) {
141
+		foreach ($query->discounts as $discount) {
142 142
 
143
-			$data       = $this->prepare_item_for_response( $discount, $request );
144
-			$discounts[]    = $this->prepare_response_for_collection( $data );
143
+			$data = $this->prepare_item_for_response($discount, $request);
144
+			$discounts[] = $this->prepare_response_for_collection($data);
145 145
 
146 146
 		}
147 147
 
148 148
 		// Prepare the response.
149
-		$response = rest_ensure_response( $discounts );
150
-		$response->header( 'X-WP-Total', (int) $query->total );
151
-		$response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
149
+		$response = rest_ensure_response($discounts);
150
+		$response->header('X-WP-Total', (int) $query->total);
151
+		$response->header('X-WP-TotalPages', (int) $query->max_num_pages);
152 152
 
153 153
 		/**
154 154
 		 * Filters the responses for discount requests.
@@ -161,9 +161,9 @@  discard block
 block discarded – undo
161 161
 		 * @param WP_REST_Request     $request The request used.
162 162
          * @param array               $args Array of args used to retrieve the discounts
163 163
 		 */
164
-        $response       = apply_filters( "wpinv_rest_discounts_response", $response, $request, $args );
164
+        $response = apply_filters("wpinv_rest_discounts_response", $response, $request, $args);
165 165
 
166
-        return rest_ensure_response( $response );
166
+        return rest_ensure_response($response);
167 167
         
168 168
     }
169 169
 
@@ -175,17 +175,17 @@  discard block
 block discarded – undo
175 175
 	 * @param int $discount_id Supplied ID.
176 176
 	 * @return WPInv_Item|WP_Error Item object if ID is valid, WP_Error otherwise.
177 177
 	 */
178
-	protected function get_post( $discount_id ) {
178
+	protected function get_post($discount_id) {
179 179
 		
180
-		$error     = new WP_Error( 'rest_item_invalid_id', __( 'Invalid discount ID.', 'invoicing' ), array( 'status' => 404 ) );
180
+		$error = new WP_Error('rest_item_invalid_id', __('Invalid discount ID.', 'invoicing'), array('status' => 404));
181 181
 
182 182
         // Ids start from 1
183
-        if ( (int) $discount_id <= 0 ) {
183
+        if ((int) $discount_id <= 0) {
184 184
 			return $error;
185 185
 		}
186 186
 
187
-		$discount = wpinv_get_discount( (int) $discount_id );
188
-		if ( empty( $discount ) ) {
187
+		$discount = wpinv_get_discount((int) $discount_id);
188
+		if (empty($discount)) {
189 189
 			return $error;
190 190
         }
191 191
 
@@ -201,20 +201,20 @@  discard block
 block discarded – undo
201 201
 	 * @param WP_REST_Request $request Full details about the request.
202 202
 	 * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise.
203 203
 	 */
204
-	public function get_item_permissions_check( $request ) {
204
+	public function get_item_permissions_check($request) {
205 205
 
206 206
         // Retrieve the discount object.
207
-        $discount = $this->get_post( $request['id'] );
207
+        $discount = $this->get_post($request['id']);
208 208
         
209 209
         // Ensure it is valid.
210
-		if ( is_wp_error( $discount ) ) {
210
+		if (is_wp_error($discount)) {
211 211
 			return $discount;
212 212
 		}
213 213
 
214
-		if ( ! wpinv_current_user_can_manage_invoicing() ) {
214
+		if (!wpinv_current_user_can_manage_invoicing()) {
215 215
 			return new WP_Error(
216 216
                 'rest_cannot_view', 
217
-                __( 'Sorry, you are not allowed to view this discount.', 'invoicing' ), 
217
+                __('Sorry, you are not allowed to view this discount.', 'invoicing'), 
218 218
                 array( 
219 219
                     'status' => rest_authorization_required_code(),
220 220
                 )
@@ -232,18 +232,18 @@  discard block
 block discarded – undo
232 232
 	 * @param WP_REST_Request $request Full details about the request.
233 233
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
234 234
 	 */
235
-	public function get_item( $request ) {
235
+	public function get_item($request) {
236 236
 
237 237
         // Fetch the discount.
238
-        $discount = $this->get_post( $request['id'] );
238
+        $discount = $this->get_post($request['id']);
239 239
         
240 240
         // Abort early if it does not exist
241
-		if ( is_wp_error( $discount ) ) {
241
+		if (is_wp_error($discount)) {
242 242
 			return $discount;
243 243
 		}
244 244
 
245 245
 		// Prepare the response
246
-		$response = $this->prepare_item_for_response( $discount, $request );
246
+		$response = $this->prepare_item_for_response($discount, $request);
247 247
 
248 248
 		/**
249 249
 		 * Filters the responses for single discount requests.
@@ -255,9 +255,9 @@  discard block
 block discarded – undo
255 255
 		 * @param WP_HTTP_Response $response Response.
256 256
 		 * @param WP_REST_Request  $request The request used.
257 257
 		 */
258
-        $response       = apply_filters( "wpinv_rest_get_discount_response", $response, $request );
258
+        $response = apply_filters("wpinv_rest_get_discount_response", $response, $request);
259 259
 
260
-        return rest_ensure_response( $response );
260
+        return rest_ensure_response($response);
261 261
 
262 262
     }
263 263
     
@@ -269,21 +269,21 @@  discard block
 block discarded – undo
269 269
 	 * @param WP_REST_Request $request Full details about the request.
270 270
 	 * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
271 271
 	 */
272
-	public function create_item_permissions_check( $request ) {
272
+	public function create_item_permissions_check($request) {
273 273
 	
274
-		if ( ! empty( $request['id'] ) ) {
275
-			return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) );
274
+		if (!empty($request['id'])) {
275
+			return new WP_Error('rest_item_exists', __('Cannot create existing item.', 'invoicing'), array('status' => 400));
276 276
 		}
277 277
 
278
-		if ( wpinv_current_user_can_manage_invoicing() ) {
278
+		if (wpinv_current_user_can_manage_invoicing()) {
279 279
 			return true;
280 280
 		}
281 281
 
282
-		$post_type = get_post_type_object( $this->post_type );
283
-		if ( ! current_user_can( $post_type->cap->create_posts ) ) {
282
+		$post_type = get_post_type_object($this->post_type);
283
+		if (!current_user_can($post_type->cap->create_posts)) {
284 284
 			return new WP_Error( 
285 285
                 'rest_cannot_create', 
286
-                __( 'Sorry, you are not allowed to create discounts as this user.', 'invoicing' ), 
286
+                __('Sorry, you are not allowed to create discounts as this user.', 'invoicing'), 
287 287
                 array( 
288 288
                     'status' => rest_authorization_required_code(),
289 289
                 )
@@ -301,32 +301,32 @@  discard block
 block discarded – undo
301 301
 	 * @param WP_REST_Request $request Full details about the request.
302 302
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
303 303
 	 */
304
-	public function create_item( $request ) {
304
+	public function create_item($request) {
305 305
 
306
-		if ( ! empty( $request['id'] ) ) {
307
-			return new WP_Error( 'rest_item_exists', __( 'Cannot create existing discount.', 'invoicing' ), array( 'status' => 400 ) );
306
+		if (!empty($request['id'])) {
307
+			return new WP_Error('rest_item_exists', __('Cannot create existing discount.', 'invoicing'), array('status' => 400));
308 308
 		}
309 309
 
310
-		$request->set_param( 'context', 'edit' );
310
+		$request->set_param('context', 'edit');
311 311
 
312 312
 		// Prepare the updated data.
313
-		$discount_data = $this->prepare_item_for_database( $request );
313
+		$discount_data = $this->prepare_item_for_database($request);
314 314
 
315
-		if ( is_wp_error( $discount_data ) ) {
315
+		if (is_wp_error($discount_data)) {
316 316
 			return $discount_data;
317 317
 		}
318 318
 
319 319
 		$discount_data['post_type'] = $this->post_type;
320 320
 
321 321
 		// Try creating the discount.
322
-        $discount = wp_insert_post( $discount_data, true );
322
+        $discount = wp_insert_post($discount_data, true);
323 323
 
324
-		if ( is_wp_error( $discount ) ) {
324
+		if (is_wp_error($discount)) {
325 325
             return $discount;
326 326
 		}
327 327
 
328 328
 		// Prepare the response
329
-		$response = $this->prepare_item_for_response( $discount, $request );
329
+		$response = $this->prepare_item_for_response($discount, $request);
330 330
 
331 331
 		/**
332 332
 		 * Fires after a single discount is created or updated via the REST API.
@@ -337,7 +337,7 @@  discard block
 block discarded – undo
337 337
 		 * @param WP_REST_Request $request  Request object.
338 338
 		 * @param bool            $creating True when creating a post, false when updating.
339 339
 		 */
340
-		do_action( "wpinv_rest_insert_discount", $discount, $request, true );
340
+		do_action("wpinv_rest_insert_discount", $discount, $request, true);
341 341
 
342 342
 		/**
343 343
 		 * Filters the responses for creating single item requests.
@@ -349,9 +349,9 @@  discard block
 block discarded – undo
349 349
 		 * @param array           $response Invoice properties.
350 350
 		 * @param WP_REST_Request $request The request used.
351 351
 		 */
352
-        $response       = apply_filters( "wpinv_rest_create_discount_response", $response, $request );
352
+        $response = apply_filters("wpinv_rest_create_discount_response", $response, $request);
353 353
 
354
-        return rest_ensure_response( $response );
354
+        return rest_ensure_response($response);
355 355
 	}
356 356
 
357 357
 	/**
@@ -362,21 +362,21 @@  discard block
 block discarded – undo
362 362
 	 * @param WP_REST_Request $request Full details about the request.
363 363
 	 * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
364 364
 	 */
365
-	public function update_item_permissions_check( $request ) {
365
+	public function update_item_permissions_check($request) {
366 366
 
367 367
 		// Retrieve the item.
368
-		$item = $this->get_post( $request['id'] );
369
-		if ( is_wp_error( $item ) ) {
368
+		$item = $this->get_post($request['id']);
369
+		if (is_wp_error($item)) {
370 370
 			return $item;
371 371
 		}
372 372
 
373
-		if ( wpinv_current_user_can_manage_invoicing() ) {
373
+		if (wpinv_current_user_can_manage_invoicing()) {
374 374
 			return true;
375 375
 		}
376 376
 
377 377
 		return new WP_Error( 
378 378
 			'rest_cannot_edit', 
379
-			__( 'Sorry, you are not allowed to update this discount.', 'invoicing' ), 
379
+			__('Sorry, you are not allowed to update this discount.', 'invoicing'), 
380 380
 			array( 
381 381
 				'status' => rest_authorization_required_code(),
382 382
 			)
@@ -392,62 +392,62 @@  discard block
 block discarded – undo
392 392
 	 * @param WP_REST_Request $request Full details about the request.
393 393
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
394 394
 	 */
395
-	public function update_item( $request ) {
395
+	public function update_item($request) {
396 396
 		
397 397
 		// Ensure the item exists.
398
-        $valid_check = $this->get_post( $request['id'] );
398
+        $valid_check = $this->get_post($request['id']);
399 399
         
400 400
         // Abort early if it does not exist
401
-		if ( is_wp_error( $valid_check ) ) {
401
+		if (is_wp_error($valid_check)) {
402 402
 			return $valid_check;
403 403
 		}
404 404
 
405
-		$request->set_param( 'context', 'edit' );
405
+		$request->set_param('context', 'edit');
406 406
 
407 407
 		// Prepare the updated data.
408
-		$data_to_update = $this->prepare_item_for_database( $request );
408
+		$data_to_update = $this->prepare_item_for_database($request);
409 409
 
410
-		if ( is_wp_error( $data_to_update ) ) {
410
+		if (is_wp_error($data_to_update)) {
411 411
 			return $data_to_update;
412 412
 		}
413 413
 
414
-		if( empty( $data_to_update['meta_input'] ) ) {
415
-			unset( $data_to_update['meta_input'] );
414
+		if (empty($data_to_update['meta_input'])) {
415
+			unset($data_to_update['meta_input']);
416 416
 		}
417 417
 
418 418
 		// Abort if no item data is provided
419
-        if( empty( $data_to_update ) ) {
420
-            return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) );
419
+        if (empty($data_to_update)) {
420
+            return new WP_Error('missing_data', __('An update request cannot be empty.', 'invoicing'));
421 421
 		}
422 422
 		
423 423
 		// post_status
424
-		if( ! empty( $data_to_update['post_status'] ) ) {
425
-			wpinv_update_discount_status( $request['id'], $data_to_update['post_status'] );
426
-			unset( $data_to_update['post_status'] );
424
+		if (!empty($data_to_update['post_status'])) {
425
+			wpinv_update_discount_status($request['id'], $data_to_update['post_status']);
426
+			unset($data_to_update['post_status']);
427 427
 		}
428 428
 
429 429
 		// Update the item
430
-		if( ! empty( $data_to_update ) ) {
430
+		if (!empty($data_to_update)) {
431 431
 
432 432
 			// Include the item ID
433 433
 			$data_to_update['ID'] = $request['id'];
434 434
 
435
-			$updated = wp_update_post( $data_to_update, true );
435
+			$updated = wp_update_post($data_to_update, true);
436 436
 
437 437
 			// Incase the update operation failed...
438
-			if ( is_wp_error( $updated ) ) {
438
+			if (is_wp_error($updated)) {
439 439
 				return $updated;
440 440
 			}
441 441
 
442 442
 		}
443 443
 
444
-		$updated_discount = get_post( $request['id'] );
444
+		$updated_discount = get_post($request['id']);
445 445
 
446 446
 		// Prepare the response
447
-		$response = $this->prepare_item_for_response( $updated_discount, $request );
447
+		$response = $this->prepare_item_for_response($updated_discount, $request);
448 448
 
449 449
 		/** This action is documented in includes/class-wpinv-rest-item-controller.php */
450
-		do_action( "wpinv_rest_insert_discount", $updated_discount, $request, false );
450
+		do_action("wpinv_rest_insert_discount", $updated_discount, $request, false);
451 451
 
452 452
 		/**
453 453
 		 * Filters the responses for updating single discount requests.
@@ -459,9 +459,9 @@  discard block
 block discarded – undo
459 459
 		 * @param array           $data_to_update Discount properties.
460 460
 		 * @param WP_REST_Request $request The request used.
461 461
 		 */
462
-        $response       = apply_filters( "wpinv_rest_update_discount_response", $response,  $data_to_update, $request );
462
+        $response = apply_filters("wpinv_rest_update_discount_response", $response, $data_to_update, $request);
463 463
 
464
-        return rest_ensure_response( $response );
464
+        return rest_ensure_response($response);
465 465
 	}
466 466
 
467 467
 	/**
@@ -472,19 +472,19 @@  discard block
 block discarded – undo
472 472
 	 * @param WP_REST_Request $request Full details about the request.
473 473
 	 * @return true|WP_Error True if the request has access to delete the discount, WP_Error object otherwise.
474 474
 	 */
475
-	public function delete_item_permissions_check( $request ) {
475
+	public function delete_item_permissions_check($request) {
476 476
 
477 477
 		// Retrieve the discount.
478
-		$discount = $this->get_post( $request['id'] );
479
-		if ( is_wp_error( $discount ) ) {
478
+		$discount = $this->get_post($request['id']);
479
+		if (is_wp_error($discount)) {
480 480
 			return $discount;
481 481
 		} 
482 482
 
483 483
 		// Ensure the current user can delete the discount
484
-		if (! wpinv_current_user_can_manage_invoicing() ) {
484
+		if (!wpinv_current_user_can_manage_invoicing()) {
485 485
 			return new WP_Error( 
486 486
                 'rest_cannot_delete', 
487
-                __( 'Sorry, you are not allowed to delete this discount.', 'invoicing' ), 
487
+                __('Sorry, you are not allowed to delete this discount.', 'invoicing'), 
488 488
                 array( 
489 489
                     'status' => rest_authorization_required_code(),
490 490
                 )
@@ -502,24 +502,24 @@  discard block
 block discarded – undo
502 502
 	 * @param WP_REST_Request $request Full details about the request.
503 503
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
504 504
 	 */
505
-	public function delete_item( $request ) {
505
+	public function delete_item($request) {
506 506
 		
507 507
 		// Retrieve the discount.
508
-		$discount = $this->get_post( $request['id'] );
509
-		if ( is_wp_error( $discount ) ) {
508
+		$discount = $this->get_post($request['id']);
509
+		if (is_wp_error($discount)) {
510 510
 			return $discount;
511 511
 		}
512 512
 
513
-		$request->set_param( 'context', 'edit' );
513
+		$request->set_param('context', 'edit');
514 514
 
515 515
 		// Prepare the discount id
516
-		$id    = $discount->ID;
516
+		$id = $discount->ID;
517 517
 
518 518
 		// Prepare the response
519
-		$response = $this->prepare_item_for_response( $discount, $request );
519
+		$response = $this->prepare_item_for_response($discount, $request);
520 520
 
521 521
 		// Delete the discount...
522
-		wpinv_remove_discount( $id );
522
+		wpinv_remove_discount($id);
523 523
 
524 524
 		/**
525 525
 		 * Fires immediately after a single discount is deleted via the REST API.
@@ -530,7 +530,7 @@  discard block
 block discarded – undo
530 530
 		 * @param WP_POST    $discount  The deleted discount.
531 531
 		 * @param WP_REST_Request  $request  The request sent to the API.
532 532
 		 */
533
-		do_action( "wpinv_rest_delete_discount", $discount, $request );
533
+		do_action("wpinv_rest_delete_discount", $discount, $request);
534 534
 
535 535
 		return $response;
536 536
 
@@ -546,44 +546,44 @@  discard block
 block discarded – undo
546 546
 	 */
547 547
 	public function get_collection_params() {
548 548
         
549
-        $query_params               = array(
549
+        $query_params = array(
550 550
 
551 551
             // Discount status.
552 552
             'status'                => array(
553 553
                 'default'           => 'publish',
554
-                'description'       => __( 'Limit result set to discounts assigned one or more statuses.', 'invoicing' ),
554
+                'description'       => __('Limit result set to discounts assigned one or more statuses.', 'invoicing'),
555 555
                 'type'              => 'array',
556
-                'sanitize_callback' => array( $this, 'sanitize_post_statuses' ),
556
+                'sanitize_callback' => array($this, 'sanitize_post_statuses'),
557 557
             ),
558 558
             
559 559
             // Discount types
560 560
             'type'                  => array(
561
-				'description'       => __( 'Type of discounts to fetch.', 'invoicing' ),
561
+				'description'       => __('Type of discounts to fetch.', 'invoicing'),
562 562
 				'type'              => 'array',
563
-				'default'           => array_keys( wpinv_get_discount_types() ),
563
+				'default'           => array_keys(wpinv_get_discount_types()),
564 564
 				'items'             => array(
565
-                    'enum'          => array_keys( wpinv_get_discount_types() ),
565
+                    'enum'          => array_keys(wpinv_get_discount_types()),
566 566
                     'type'          => 'string',
567 567
                 ),
568 568
 			),
569 569
 			
570 570
 			// Number of results per page
571 571
             'limit'                 => array(
572
-				'description'       => __( 'Number of discounts to fetch.', 'invoicing' ),
572
+				'description'       => __('Number of discounts to fetch.', 'invoicing'),
573 573
 				'type'              => 'integer',
574
-				'default'           => (int) get_option( 'posts_per_page' ),
574
+				'default'           => (int) get_option('posts_per_page'),
575 575
             ),
576 576
 
577 577
             // Pagination
578 578
             'page'     => array(
579
-				'description'       => __( 'Current page to fetch.', 'invoicing' ),
579
+				'description'       => __('Current page to fetch.', 'invoicing'),
580 580
 				'type'              => 'integer',
581 581
 				'default'           => 1,
582 582
             ),
583 583
 
584 584
             // Exclude certain items
585 585
             'exclude'  => array(
586
-                'description' => __( 'Ensure result set excludes specific IDs.', 'invoicing' ),
586
+                'description' => __('Ensure result set excludes specific IDs.', 'invoicing'),
587 587
                 'type'        => 'array',
588 588
                 'items'       => array(
589 589
                     'type' => 'integer',
@@ -593,7 +593,7 @@  discard block
 block discarded – undo
593 593
 
594 594
             // Order discounts by
595 595
             'orderby'  => array(
596
-                'description' => __( 'Sort discounts by object attribute.', 'invoicing' ),
596
+                'description' => __('Sort discounts by object attribute.', 'invoicing'),
597 597
                 'type'        => 'string',
598 598
                 'default'     => 'date',
599 599
                 'enum'        => array(
@@ -609,15 +609,15 @@  discard block
 block discarded – undo
609 609
 
610 610
             // How to order
611 611
             'order'    => array(
612
-                'description' => __( 'Order sort attribute ascending or descending.', 'invoicing' ),
612
+                'description' => __('Order sort attribute ascending or descending.', 'invoicing'),
613 613
                 'type'        => 'string',
614 614
                 'default'     => 'DESC',
615
-                'enum'        => array( 'ASC', 'DESC' ),
615
+                'enum'        => array('ASC', 'DESC'),
616 616
 			),
617 617
 			
618 618
 			// Search term
619 619
             'search'                => array(
620
-				'description'       => __( 'Return discounts that match the search term.', 'invoicing' ),
620
+				'description'       => __('Return discounts that match the search term.', 'invoicing'),
621 621
 				'type'              => 'string',
622 622
             ),
623 623
         );
@@ -630,7 +630,7 @@  discard block
 block discarded – undo
630 630
 		 *
631 631
 		 * @param array        $query_params JSON Schema-formatted collection parameters.
632 632
 		 */
633
-		return apply_filters( "wpinv_rest_discounts_collection_params", $query_params );
633
+		return apply_filters("wpinv_rest_discounts_collection_params", $query_params);
634 634
     }
635 635
     
636 636
     /**
@@ -641,7 +641,7 @@  discard block
 block discarded – undo
641 641
 	 * @param object|string $post_type Post type name or object.
642 642
 	 * @return bool Whether the post type is allowed in REST.
643 643
 	 */
644
-	protected function check_is_post_type_allowed( $post_type ) {
644
+	protected function check_is_post_type_allowed($post_type) {
645 645
 		return true;
646 646
 	}
647 647
 
@@ -653,93 +653,93 @@  discard block
 block discarded – undo
653 653
 	 * @param WP_REST_Request $request Request object.
654 654
 	 * @return array|WP_Error Discount Properties or WP_Error.
655 655
 	 */
656
-	protected function prepare_item_for_database( $request ) {
657
-		$prepared_item 		 = new stdClass();
656
+	protected function prepare_item_for_database($request) {
657
+		$prepared_item = new stdClass();
658 658
 		$prepared_item->meta_input = array();
659 659
 
660 660
 		// Post ID.
661
-		if ( isset( $request['id'] ) ) {
662
-			$existing_item = $this->get_post( $request['id'] );
663
-			if ( is_wp_error( $existing_item ) ) {
661
+		if (isset($request['id'])) {
662
+			$existing_item = $this->get_post($request['id']);
663
+			if (is_wp_error($existing_item)) {
664 664
 				return $existing_item;
665 665
 			}
666 666
 
667
-			$prepared_item->ID 		  = $existing_item->ID;
667
+			$prepared_item->ID = $existing_item->ID;
668 668
 		}
669 669
 
670 670
 		$schema = $this->get_item_schema();
671 671
 
672 672
 		// item title.
673
-		if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
674
-			$prepared_item->post_title = sanitize_text_field( $request['title'] );
673
+		if (!empty($schema['properties']['title']) && isset($request['title'])) {
674
+			$prepared_item->post_title = sanitize_text_field($request['title']);
675 675
 		}
676 676
 
677 677
 		// item status.
678
-		if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && in_array( $request['status'], array_keys( get_post_stati( array( 'internal' => false ) ) ) ) ) {
679
-			$prepared_item->post_status = sanitize_text_field( $request['status'] );
678
+		if (!empty($schema['properties']['status']) && isset($request['status']) && in_array($request['status'], array_keys(get_post_stati(array('internal' => false))))) {
679
+			$prepared_item->post_status = sanitize_text_field($request['status']);
680 680
 		}
681 681
 
682 682
 		// Code.
683
-		if ( ! empty( $schema['properties']['code'] ) && isset( $request['code'] ) ) {
684
-			$prepared_item->meta_input['_wpi_discount_code'] = trim( $request['code'] );
683
+		if (!empty($schema['properties']['code']) && isset($request['code'])) {
684
+			$prepared_item->meta_input['_wpi_discount_code'] = trim($request['code']);
685 685
 		}
686 686
 
687 687
 		// Type.
688
-		if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] )  && in_array( $request['type'], array_keys( wpinv_get_discount_types() ) ) ) {
689
-			$prepared_item->meta_input['_wpi_discount_type'] = trim( $request['type'] );
688
+		if (!empty($schema['properties']['type']) && isset($request['type']) && in_array($request['type'], array_keys(wpinv_get_discount_types()))) {
689
+			$prepared_item->meta_input['_wpi_discount_type'] = trim($request['type']);
690 690
 		}
691 691
 
692 692
 		// Amount.
693
-		if ( ! empty( $schema['properties']['amount'] ) && isset( $request['amount'] ) ) {
694
-			$prepared_item->meta_input['_wpi_discount_amount'] = floatval( $request['amount'] );
693
+		if (!empty($schema['properties']['amount']) && isset($request['amount'])) {
694
+			$prepared_item->meta_input['_wpi_discount_amount'] = floatval($request['amount']);
695 695
 		}
696 696
 
697 697
 		// Items.
698
-		if ( ! empty( $schema['properties']['items'] ) && isset( $request['items'] ) ) {
699
-			$prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list( $request['items'] );
698
+		if (!empty($schema['properties']['items']) && isset($request['items'])) {
699
+			$prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list($request['items']);
700 700
 		}
701 701
 
702 702
 		// Excluded Items.
703
-		if ( ! empty( $schema['properties']['exclude_items'] ) && isset( $request['exclude_items'] ) ) {
704
-			$prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list( $request['exclude_items'] );
703
+		if (!empty($schema['properties']['exclude_items']) && isset($request['exclude_items'])) {
704
+			$prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list($request['exclude_items']);
705 705
 		}
706 706
 
707 707
 		// Start date.
708
-		if ( ! empty( $schema['properties']['start_date'] ) && isset( $request['start_date'] ) ) {
709
-			$prepared_item->meta_input['_wpi_discount_start'] = trim( $request['start_date'] );
708
+		if (!empty($schema['properties']['start_date']) && isset($request['start_date'])) {
709
+			$prepared_item->meta_input['_wpi_discount_start'] = trim($request['start_date']);
710 710
 		}
711 711
 
712 712
 		// End date.
713
-		if ( ! empty( $schema['properties']['end_date'] ) && isset( $request['end_date'] ) ) {
714
-			$prepared_item->meta_input['_wpi_discount_expiration'] = trim( $request['end_date'] );
713
+		if (!empty($schema['properties']['end_date']) && isset($request['end_date'])) {
714
+			$prepared_item->meta_input['_wpi_discount_expiration'] = trim($request['end_date']);
715 715
 		}
716 716
 
717 717
 		// Minimum amount.
718
-		if ( ! empty( $schema['properties']['minimum_amount'] ) && isset( $request['minimum_amount'] ) ) {
719
-			$prepared_item->meta_input['_wpi_discount_min_total'] = floatval( $request['minimum_amount'] );
718
+		if (!empty($schema['properties']['minimum_amount']) && isset($request['minimum_amount'])) {
719
+			$prepared_item->meta_input['_wpi_discount_min_total'] = floatval($request['minimum_amount']);
720 720
 		}
721 721
 
722 722
 		// Maximum amount.
723
-		if ( ! empty( $schema['properties']['maximum_amount'] ) && isset( $request['maximum_amount'] ) ) {
724
-			$prepared_item->meta_input['_wpi_discount_max_total'] = floatval( $request['maximum_amount'] );
723
+		if (!empty($schema['properties']['maximum_amount']) && isset($request['maximum_amount'])) {
724
+			$prepared_item->meta_input['_wpi_discount_max_total'] = floatval($request['maximum_amount']);
725 725
 		}
726 726
 
727 727
 		// Recurring.
728
-		if ( ! empty( $schema['properties']['recurring'] ) && isset( $request['recurring'] ) ) {
729
-			$prepared_item->meta_input['_wpi_discount_is_recurring'] = empty( (int) $request['recurring'] ) ? 0 : 1;
728
+		if (!empty($schema['properties']['recurring']) && isset($request['recurring'])) {
729
+			$prepared_item->meta_input['_wpi_discount_is_recurring'] = empty((int) $request['recurring']) ? 0 : 1;
730 730
 		}
731 731
 
732 732
 		// Maximum uses.
733
-		if ( ! empty( $schema['properties']['max_uses'] ) && isset( $request['max_uses'] ) ) {
734
-			$prepared_item->meta_input['_wpi_discount_max_uses'] = intval( $request['max_uses'] );
733
+		if (!empty($schema['properties']['max_uses']) && isset($request['max_uses'])) {
734
+			$prepared_item->meta_input['_wpi_discount_max_uses'] = intval($request['max_uses']);
735 735
 		}
736 736
 
737 737
 		// Single use.
738
-		if ( ! empty( $schema['properties']['single_use'] ) && isset( $request['single_use'] ) ) {
739
-			$prepared_item->meta_input['_wpi_discount_is_single_use'] = empty( (int) $request['single_use'] ) ? 0 : 1;
738
+		if (!empty($schema['properties']['single_use']) && isset($request['single_use'])) {
739
+			$prepared_item->meta_input['_wpi_discount_is_single_use'] = empty((int) $request['single_use']) ? 0 : 1;
740 740
 		}
741 741
 
742
-		$discount_data = (array) wp_unslash( $prepared_item );
742
+		$discount_data = (array) wp_unslash($prepared_item);
743 743
 
744 744
 		/**
745 745
 		 * Filters an item before it is inserted via the REST API.
@@ -749,7 +749,7 @@  discard block
 block discarded – undo
749 749
 		 * @param array        $discount_data An array of discount data
750 750
 		 * @param WP_REST_Request $request       Request object.
751 751
 		 */
752
-		return apply_filters( "wpinv_rest_pre_insert_discount", $discount_data, $request );
752
+		return apply_filters("wpinv_rest_pre_insert_discount", $discount_data, $request);
753 753
 
754 754
 	}
755 755
 
@@ -762,110 +762,110 @@  discard block
 block discarded – undo
762 762
 	 * @param WP_REST_Request $request Request object.
763 763
 	 * @return WP_REST_Response Response object.
764 764
 	 */
765
-	public function prepare_item_for_response( $discount, $request ) {
765
+	public function prepare_item_for_response($discount, $request) {
766 766
 
767
-		$GLOBALS['post'] = get_post( $discount->ID );
767
+		$GLOBALS['post'] = get_post($discount->ID);
768 768
 
769
-		setup_postdata( $discount->ID );
769
+		setup_postdata($discount->ID);
770 770
 
771 771
 		// Fetch the fields to include in this response.
772
-		$fields = $this->get_fields_for_response( $request );
772
+		$fields = $this->get_fields_for_response($request);
773 773
 
774 774
 		// Base fields for every discount.
775 775
 		$data = array();
776 776
 
777 777
 		// Set up ID.
778
-		if ( rest_is_field_included( 'id', $fields ) ) {
778
+		if (rest_is_field_included('id', $fields)) {
779 779
 			$data['id'] = $discount->ID;
780 780
 		}
781 781
 
782 782
 		// Title.
783
-		if ( rest_is_field_included( 'title', $fields ) ) {
784
-			$data['title'] = get_the_title( $discount->ID );
783
+		if (rest_is_field_included('title', $fields)) {
784
+			$data['title'] = get_the_title($discount->ID);
785 785
 		}
786 786
 
787 787
 		// Code.
788
-		if ( rest_is_field_included( 'code', $fields ) ) {
789
-			$data['code'] = wpinv_get_discount_code( $discount->ID );
788
+		if (rest_is_field_included('code', $fields)) {
789
+			$data['code'] = wpinv_get_discount_code($discount->ID);
790 790
 		}
791 791
 
792 792
 		// Type.
793
-		if ( rest_is_field_included( 'type', $fields ) ) {
794
-			$data['type'] = wpinv_get_discount_type( $discount->ID );
793
+		if (rest_is_field_included('type', $fields)) {
794
+			$data['type'] = wpinv_get_discount_type($discount->ID);
795 795
 		}
796 796
 
797 797
 		// Amount.
798
-		if ( rest_is_field_included( 'amount', $fields ) ) {
799
-			$data['amount'] = wpinv_get_discount_amount( $discount->ID );
798
+		if (rest_is_field_included('amount', $fields)) {
799
+			$data['amount'] = wpinv_get_discount_amount($discount->ID);
800 800
 		}
801 801
 
802 802
 		// Status.
803
-		if ( rest_is_field_included( 'status', $fields ) ) {
804
-			$data['status'] = get_post_status( $discount->ID );
803
+		if (rest_is_field_included('status', $fields)) {
804
+			$data['status'] = get_post_status($discount->ID);
805 805
 		}
806 806
 
807 807
 		// Items.
808
-		if ( rest_is_field_included( 'items', $fields ) ) {
809
-			$data['items'] = wpinv_get_discount_item_reqs( $discount->ID );
808
+		if (rest_is_field_included('items', $fields)) {
809
+			$data['items'] = wpinv_get_discount_item_reqs($discount->ID);
810 810
 		}
811 811
 
812 812
 		// Excluded Items.
813
-		if ( rest_is_field_included( 'exclude_items', $fields ) ) {
814
-			$data['exclude_items'] = wpinv_get_discount_excluded_items( $discount->ID );
813
+		if (rest_is_field_included('exclude_items', $fields)) {
814
+			$data['exclude_items'] = wpinv_get_discount_excluded_items($discount->ID);
815 815
 		}
816 816
 
817 817
 		// Start date.
818
-		if ( rest_is_field_included( 'start_date', $fields ) ) {
819
-			$data['start_date'] = wpinv_get_discount_start_date( $discount->ID );
818
+		if (rest_is_field_included('start_date', $fields)) {
819
+			$data['start_date'] = wpinv_get_discount_start_date($discount->ID);
820 820
 		}
821 821
 
822 822
 		// End date.
823
-		if ( rest_is_field_included( 'end_date', $fields ) ) {
824
-			$data['end_date'] = wpinv_get_discount_expiration( $discount->ID );
823
+		if (rest_is_field_included('end_date', $fields)) {
824
+			$data['end_date'] = wpinv_get_discount_expiration($discount->ID);
825 825
 		}
826 826
 
827 827
 		// Minimum amount.
828
-		if ( rest_is_field_included( 'minimum_amount', $fields ) ) {
829
-			$data['minimum_amount'] = wpinv_get_discount_min_total( $discount->ID );
828
+		if (rest_is_field_included('minimum_amount', $fields)) {
829
+			$data['minimum_amount'] = wpinv_get_discount_min_total($discount->ID);
830 830
 		}
831 831
 
832 832
 		// Maximum amount.
833
-		if ( rest_is_field_included( 'maximum_amount', $fields ) ) {
834
-			$data['maximum_amount'] = wpinv_get_discount_max_total( $discount->ID );
833
+		if (rest_is_field_included('maximum_amount', $fields)) {
834
+			$data['maximum_amount'] = wpinv_get_discount_max_total($discount->ID);
835 835
 		}
836 836
 
837 837
 		// Recurring.
838
-		if ( rest_is_field_included( 'recurring', $fields ) ) {
839
-			$data['recurring'] = wpinv_discount_is_recurring( $discount->ID );
838
+		if (rest_is_field_included('recurring', $fields)) {
839
+			$data['recurring'] = wpinv_discount_is_recurring($discount->ID);
840 840
 		}
841 841
 
842 842
 		// Maximum uses.
843
-		if ( rest_is_field_included( 'max_uses', $fields ) ) {
844
-			$data['max_uses'] = wpinv_get_discount_max_uses( $discount->ID );
843
+		if (rest_is_field_included('max_uses', $fields)) {
844
+			$data['max_uses'] = wpinv_get_discount_max_uses($discount->ID);
845 845
 		}
846 846
 
847 847
 		// Single use.
848
-		if ( rest_is_field_included( 'single_use', $fields ) ) {
849
-			$data['single_use'] = wpinv_discount_is_single_use( $discount->ID );
848
+		if (rest_is_field_included('single_use', $fields)) {
849
+			$data['single_use'] = wpinv_discount_is_single_use($discount->ID);
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( $discount );
860
-		$response->add_links( $links );
859
+		$links = $this->prepare_links($discount);
860
+		$response->add_links($links);
861 861
 
862
-		if ( ! empty( $links['self']['href'] ) ) {
863
-			$actions = $this->get_available_actions( $discount, $request );
862
+		if (!empty($links['self']['href'])) {
863
+			$actions = $this->get_available_actions($discount, $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
 block discarded – undo
878 878
 		 * @param WP_Post    $discount  The discount post object.
879 879
 		 * @param WP_REST_Request  $request  Request object.
880 880
 		 */
881
-		return apply_filters( "wpinv_rest_prepare_discount", $response, $discount, $request );
881
+		return apply_filters("wpinv_rest_prepare_discount", $response, $discount, $request);
882 882
 	}
883 883
 
884 884
 	/**
@@ -891,57 +891,57 @@  discard block
 block discarded – undo
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
 block discarded – undo
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,99 +973,99 @@  discard block
 block discarded – undo
973 973
 			'properties' 		  => array(
974 974
 
975 975
 				'id'           => array(
976
-					'description' => __( 'Unique identifier for the discount.', 'invoicing' ),
976
+					'description' => __('Unique identifier for the discount.', '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
 				'title'			  => array(
983
-					'description' => __( 'The title for the discount.', 'invoicing' ),
983
+					'description' => __('The title for the discount.', 'invoicing'),
984 984
 					'type'        => 'string',
985
-					'context'     => array( 'view', 'edit' ),
985
+					'context'     => array('view', 'edit'),
986 986
 				),
987 987
 
988 988
 				'code'        => array(
989
-					'description' => __( 'The discount code.', 'invoicing' ),
989
+					'description' => __('The discount code.', 'invoicing'),
990 990
 					'type'        => 'string',
991
-					'context'     => array( 'view', 'edit', 'embed' ),
991
+					'context'     => array('view', 'edit', 'embed'),
992 992
 					'required'	  => true,
993 993
 				),
994 994
 
995 995
 				'type'        => array(
996
-					'description' => __( 'The type of discount.', 'invoicing' ),
996
+					'description' => __('The type of discount.', 'invoicing'),
997 997
 					'type'        => 'string',
998
-					'enum'        => array_keys( wpinv_get_discount_types() ),
999
-					'context'     => array( 'view', 'edit', 'embed' ),
998
+					'enum'        => array_keys(wpinv_get_discount_types()),
999
+					'context'     => array('view', 'edit', 'embed'),
1000 1000
 					'default'	  => 'percentage',
1001 1001
 				),
1002 1002
 
1003 1003
 				'amount'        => array(
1004
-					'description' => __( 'The discount value.', 'invoicing' ),
1004
+					'description' => __('The discount value.', 'invoicing'),
1005 1005
 					'type'        => 'number',
1006
-					'context'     => array( 'view', 'edit', 'embed' ),
1006
+					'context'     => array('view', 'edit', 'embed'),
1007 1007
 					'required'	  => true,
1008 1008
 				),
1009 1009
 
1010 1010
 				'status'       => array(
1011
-					'description' => __( 'A named status for the discount.', 'invoicing' ),
1011
+					'description' => __('A named status for the discount.', 'invoicing'),
1012 1012
 					'type'        => 'string',
1013
-					'enum'        => array_keys( get_post_stati( array( 'internal' => false ) ) ),
1014
-					'context'     => array( 'view', 'edit' ),
1013
+					'enum'        => array_keys(get_post_stati(array('internal' => false))),
1014
+					'context'     => array('view', 'edit'),
1015 1015
 				),
1016 1016
 
1017 1017
 				'items'       => array(
1018
-					'description' => __( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing' ),
1018
+					'description' => __('Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing'),
1019 1019
 					'type'        => 'array',
1020
-					'context'     => array( 'view', 'edit' ),
1020
+					'context'     => array('view', 'edit'),
1021 1021
 				),
1022 1022
 
1023 1023
 				'exclude_items'   => array(
1024
-					'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ),
1024
+					'description' => __('Items which are NOT allowed to use this discount.', 'invoicing'),
1025 1025
 					'type'        => 'array',
1026
-					'context'     => array( 'view', 'edit' ),
1026
+					'context'     => array('view', 'edit'),
1027 1027
 				),
1028 1028
 
1029 1029
 				'start_date'       => array(
1030
-					'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss  . If provided, the discount can only be used after or on this date.', 'invoicing' ),
1030
+					'description' => __('The start date for the discount in the format of yyyy-mm-dd hh:mm:ss  . If provided, the discount can only be used after or on this date.', 'invoicing'),
1031 1031
 					'type'        => 'string',
1032
-					'context'     => array( 'view', 'edit' ),
1032
+					'context'     => array('view', 'edit'),
1033 1033
 				),
1034 1034
 
1035 1035
 				'end_date'        => array(
1036
-					'description' => __( 'The expiration date for the discount.', 'invoicing' ),
1036
+					'description' => __('The expiration date for the discount.', 'invoicing'),
1037 1037
 					'type'        => 'string',
1038
-					'context'     => array( 'view', 'edit', 'embed' ),
1038
+					'context'     => array('view', 'edit', 'embed'),
1039 1039
 				),
1040 1040
 				
1041 1041
 				'minimum_amount'       => array(
1042
-					'description' => __( 'Minimum amount needed to use this invoice.', 'invoicing' ),
1042
+					'description' => __('Minimum amount needed to use this invoice.', 'invoicing'),
1043 1043
 					'type'        => 'number',
1044
-					'context'     => array( 'view', 'edit', 'embed' ),
1044
+					'context'     => array('view', 'edit', 'embed'),
1045 1045
 				),
1046 1046
 
1047 1047
 				'maximum_amount'       => array(
1048
-					'description' => __( 'Maximum amount needed to use this invoice.', 'invoicing' ),
1048
+					'description' => __('Maximum amount needed to use this invoice.', 'invoicing'),
1049 1049
 					'type'        => 'number',
1050
-					'context'     => array( 'view', 'edit', 'embed' ),
1050
+					'context'     => array('view', 'edit', 'embed'),
1051 1051
 				),
1052 1052
 
1053 1053
 				'recurring'       => array(
1054
-					'description' => __( 'Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing' ),
1054
+					'description' => __('Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing'),
1055 1055
 					'type'        => 'integer',
1056
-					'context'     => array( 'view', 'edit', 'embed' ),
1056
+					'context'     => array('view', 'edit', 'embed'),
1057 1057
 				),
1058 1058
 
1059 1059
 				'max_uses'        => array(
1060
-					'description' => __( 'The maximum number of times this discount code can be used.', 'invoicing' ),
1060
+					'description' => __('The maximum number of times this discount code can be used.', 'invoicing'),
1061 1061
 					'type'        => 'number',
1062
-					'context'     => array( 'view', 'edit', 'embed' ),
1062
+					'context'     => array('view', 'edit', 'embed'),
1063 1063
 				),
1064 1064
 
1065 1065
 				'single_use'       => array(
1066
-					'description' => __( 'Whether or not this discount can only be used once per user.', 'invoicing' ),
1066
+					'description' => __('Whether or not this discount can only be used once per user.', 'invoicing'),
1067 1067
 					'type'        => 'integer',
1068
-					'context'     => array( 'view', 'edit', 'embed' ),
1068
+					'context'     => array('view', 'edit', 'embed'),
1069 1069
 				)
1070 1070
 
1071 1071
 			),
@@ -1083,12 +1083,12 @@  discard block
 block discarded – undo
1083 1083
 		 *
1084 1084
 		 * @param array   $schema    The discount schema.
1085 1085
 		 */
1086
-        $schema = apply_filters( "wpinv_rest_discount_schema", $schema );
1086
+        $schema = apply_filters("wpinv_rest_discount_schema", $schema);
1087 1087
 
1088 1088
 		//  Cache the discount schema.
1089 1089
 		$this->schema = $schema;
1090 1090
 		
1091
-		return $this->add_additional_fields_schema( $this->schema );
1091
+		return $this->add_additional_fields_schema($this->schema);
1092 1092
 	}
1093 1093
 
1094 1094
 	/**
@@ -1100,20 +1100,20 @@  discard block
 block discarded – undo
1100 1100
 	 */
1101 1101
 	protected function get_schema_links() {
1102 1102
 
1103
-		$href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
1103
+		$href = rest_url("{$this->namespace}/{$this->rest_base}/{id}");
1104 1104
 
1105 1105
 		$links = array();
1106 1106
 
1107 1107
 		$links[] = array(
1108 1108
 			'rel'          => 'https://api.w.org/action-publish',
1109
-			'title'        => __( 'The current user can publish this discount.' ),
1109
+			'title'        => __('The current user can publish this discount.'),
1110 1110
 			'href'         => $href,
1111 1111
 			'targetSchema' => array(
1112 1112
 				'type'       => 'object',
1113 1113
 				'properties' => array(
1114 1114
 					'status' => array(
1115 1115
 						'type' => 'string',
1116
-						'enum' => array( 'publish', 'future' ),
1116
+						'enum' => array('publish', 'future'),
1117 1117
 					),
1118 1118
 				),
1119 1119
 			),
@@ -1130,18 +1130,18 @@  discard block
 block discarded – undo
1130 1130
 	 * @param WP_Post $discount Post Object.
1131 1131
 	 * @return array Links for the given discount.
1132 1132
 	 */
1133
-	protected function prepare_links( $discount ) {
1133
+	protected function prepare_links($discount) {
1134 1134
 
1135 1135
 		// Prepare the base REST API endpoint for discounts.
1136
-		$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
1136
+		$base = sprintf('%s/%s', $this->namespace, $this->rest_base);
1137 1137
 
1138 1138
 		// Entity meta.
1139 1139
 		$links = array(
1140 1140
 			'self'       => array(
1141
-				'href' => rest_url( trailingslashit( $base ) . $discount->ID ),
1141
+				'href' => rest_url(trailingslashit($base) . $discount->ID),
1142 1142
 			),
1143 1143
 			'collection' => array(
1144
-				'href' => rest_url( $base ),
1144
+				'href' => rest_url($base),
1145 1145
 			),
1146 1146
 		);
1147 1147
 
@@ -1154,7 +1154,7 @@  discard block
 block discarded – undo
1154 1154
 		 *
1155 1155
 		 * @param array   $links    Rest links.
1156 1156
 		 */
1157
-		return apply_filters( "wpinv_rest_discount_links", $links );
1157
+		return apply_filters("wpinv_rest_discount_links", $links);
1158 1158
 
1159 1159
 	}
1160 1160
 
@@ -1167,19 +1167,19 @@  discard block
 block discarded – undo
1167 1167
 	 * @param WP_REST_Request $request Request object.
1168 1168
 	 * @return array List of link relations.
1169 1169
 	 */
1170
-	protected function get_available_actions( $discount, $request ) {
1170
+	protected function get_available_actions($discount, $request) {
1171 1171
 
1172
-		if ( 'edit' !== $request['context'] ) {
1172
+		if ('edit' !== $request['context']) {
1173 1173
 			return array();
1174 1174
 		}
1175 1175
 
1176 1176
 		$rels = array();
1177 1177
 
1178 1178
 		// Retrieve the post type object.
1179
-		$post_type = get_post_type_object( $discount->post_type );
1179
+		$post_type = get_post_type_object($discount->post_type);
1180 1180
 
1181 1181
 		// Mark discount as published.
1182
-		if ( current_user_can( $post_type->cap->publish_posts ) ) {
1182
+		if (current_user_can($post_type->cap->publish_posts)) {
1183 1183
 			$rels[] = 'https://api.w.org/action-publish';
1184 1184
 		}
1185 1185
 
@@ -1192,7 +1192,7 @@  discard block
 block discarded – undo
1192 1192
 		 *
1193 1193
 		 * @param array   $rels    Available link relations.
1194 1194
 		 */
1195
-		return apply_filters( "wpinv_rest_discount_link_relations", $rels );
1195
+		return apply_filters("wpinv_rest_discount_link_relations", $rels);
1196 1196
 	}
1197 1197
 
1198 1198
 	/**
@@ -1204,7 +1204,7 @@  discard block
 block discarded – undo
1204 1204
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
1205 1205
 	 */
1206 1206
 	public function get_discount_types() {
1207
-		return rest_ensure_response( wpinv_get_discount_types() );
1207
+		return rest_ensure_response(wpinv_get_discount_types());
1208 1208
 	}
1209 1209
 
1210 1210
     
Please login to merge, or discard this patch.