Passed
Pull Request — master (#392)
by Brian
05:22
created
includes/class-wpinv-item.php 1 patch
Indentation   +735 added lines, -735 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 if ( ! defined( 'ABSPATH' ) ) {
3
-	exit;
3
+    exit;
4 4
 }
5 5
 
6 6
 /**
@@ -10,30 +10,30 @@  discard block
 block discarded – undo
10 10
 class WPInv_Item  extends GetPaid_Data {
11 11
 
12 12
     /**
13
-	 * Which data store to load.
14
-	 *
15
-	 * @var string
16
-	 */
13
+     * Which data store to load.
14
+     *
15
+     * @var string
16
+     */
17 17
     protected $data_store_name = 'item';
18 18
 
19 19
     /**
20
-	 * This is the name of this object type.
21
-	 *
22
-	 * @var string
23
-	 */
24
-	protected $object_type = 'item';
20
+     * This is the name of this object type.
21
+     *
22
+     * @var string
23
+     */
24
+    protected $object_type = 'item';
25 25
 
26 26
     /**
27
-	 * Item Data array. This is the core item data exposed in APIs.
28
-	 *
29
-	 * @since 1.0.19
30
-	 * @var array
31
-	 */
32
-	protected $data = array(
33
-		'parent_id'            => 0,
34
-		'status'               => 'draft',
35
-		'version'              => '',
36
-		'date_created'         => null,
27
+     * Item Data array. This is the core item data exposed in APIs.
28
+     *
29
+     * @since 1.0.19
30
+     * @var array
31
+     */
32
+    protected $data = array(
33
+        'parent_id'            => 0,
34
+        'status'               => 'draft',
35
+        'version'              => '',
36
+        'date_created'         => null,
37 37
         'date_modified'        => null,
38 38
         'name'                 => '',
39 39
         'description'          => '',
@@ -58,13 +58,13 @@  discard block
 block discarded – undo
58 58
     );
59 59
 
60 60
     /**
61
-	 * Stores meta in cache for future reads.
62
-	 *
63
-	 * A group must be set to to enable caching.
64
-	 *
65
-	 * @var string
66
-	 */
67
-	protected $cache_group = 'getpaid_items';
61
+     * Stores meta in cache for future reads.
62
+     *
63
+     * A group must be set to to enable caching.
64
+     *
65
+     * @var string
66
+     */
67
+    protected $cache_group = 'getpaid_items';
68 68
 
69 69
     /**
70 70
      * Stores a reference to the original WP_Post object
@@ -74,37 +74,37 @@  discard block
 block discarded – undo
74 74
     protected $post = null;
75 75
 
76 76
     /**
77
-	 * Get the item if ID is passed, otherwise the item is new and empty.
78
-	 *
79
-	 * @param  int|object|WPInv_Item|WP_Post $item Item to read.
80
-	 */
81
-	public function __construct( $item = 0 ) {
82
-		parent::__construct( $item );
83
-
84
-		if ( ! empty( $item ) && is_numeric( $item ) && 'wpi_item' == get_post_type( $item ) ) {
85
-			$this->set_id( $item );
86
-		} elseif ( $item instanceof self ) {
87
-			$this->set_id( $item->get_id() );
88
-		} elseif ( ! empty( $item->ID ) ) {
89
-			$this->set_id( $item->ID );
90
-		} elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'custom_id' ) ) {
91
-			$this->set_id( $item_id );
92
-		} elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'name' ) ) {
93
-			$this->set_id( $item_id );
94
-		} else {
95
-			$this->set_object_read( true );
96
-		}
77
+     * Get the item if ID is passed, otherwise the item is new and empty.
78
+     *
79
+     * @param  int|object|WPInv_Item|WP_Post $item Item to read.
80
+     */
81
+    public function __construct( $item = 0 ) {
82
+        parent::__construct( $item );
83
+
84
+        if ( ! empty( $item ) && is_numeric( $item ) && 'wpi_item' == get_post_type( $item ) ) {
85
+            $this->set_id( $item );
86
+        } elseif ( $item instanceof self ) {
87
+            $this->set_id( $item->get_id() );
88
+        } elseif ( ! empty( $item->ID ) ) {
89
+            $this->set_id( $item->ID );
90
+        } elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'custom_id' ) ) {
91
+            $this->set_id( $item_id );
92
+        } elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'name' ) ) {
93
+            $this->set_id( $item_id );
94
+        } else {
95
+            $this->set_object_read( true );
96
+        }
97 97
 
98 98
         // Load the datastore.
99
-		$this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
99
+        $this->data_store = GetPaid_Data_Store::load( $this->data_store_name );
100 100
 
101
-		if ( $this->get_id() > 0 ) {
101
+        if ( $this->get_id() > 0 ) {
102 102
             $this->post = get_post( $this->get_id() );
103 103
             $this->ID   = $this->get_id();
104
-			$this->data_store->read( $this );
104
+            $this->data_store->read( $this );
105 105
         }
106 106
 
107
-	}
107
+    }
108 108
 
109 109
     /*
110 110
 	|--------------------------------------------------------------------------
@@ -122,401 +122,401 @@  discard block
 block discarded – undo
122 122
     */
123 123
 
124 124
     /**
125
-	 * Get parent item ID.
126
-	 *
127
-	 * @since 1.0.19
128
-	 * @param  string $context View or edit context.
129
-	 * @return int
130
-	 */
131
-	public function get_parent_id( $context = 'view' ) {
132
-		return (int) $this->get_prop( 'parent_id', $context );
125
+     * Get parent item ID.
126
+     *
127
+     * @since 1.0.19
128
+     * @param  string $context View or edit context.
129
+     * @return int
130
+     */
131
+    public function get_parent_id( $context = 'view' ) {
132
+        return (int) $this->get_prop( 'parent_id', $context );
133 133
     }
134 134
 
135 135
     /**
136
-	 * Get item status.
137
-	 *
138
-	 * @since 1.0.19
139
-	 * @param  string $context View or edit context.
140
-	 * @return string
141
-	 */
142
-	public function get_status( $context = 'view' ) {
143
-		return $this->get_prop( 'status', $context );
136
+     * Get item status.
137
+     *
138
+     * @since 1.0.19
139
+     * @param  string $context View or edit context.
140
+     * @return string
141
+     */
142
+    public function get_status( $context = 'view' ) {
143
+        return $this->get_prop( 'status', $context );
144 144
     }
145 145
 
146 146
     /**
147
-	 * Get plugin version when the item was created.
148
-	 *
149
-	 * @since 1.0.19
150
-	 * @param  string $context View or edit context.
151
-	 * @return string
152
-	 */
153
-	public function get_version( $context = 'view' ) {
154
-		return $this->get_prop( 'version', $context );
147
+     * Get plugin version when the item was created.
148
+     *
149
+     * @since 1.0.19
150
+     * @param  string $context View or edit context.
151
+     * @return string
152
+     */
153
+    public function get_version( $context = 'view' ) {
154
+        return $this->get_prop( 'version', $context );
155 155
     }
156 156
 
157 157
     /**
158
-	 * Get date when the item was created.
159
-	 *
160
-	 * @since 1.0.19
161
-	 * @param  string $context View or edit context.
162
-	 * @return string
163
-	 */
164
-	public function get_date_created( $context = 'view' ) {
165
-		return $this->get_prop( 'date_created', $context );
158
+     * Get date when the item was created.
159
+     *
160
+     * @since 1.0.19
161
+     * @param  string $context View or edit context.
162
+     * @return string
163
+     */
164
+    public function get_date_created( $context = 'view' ) {
165
+        return $this->get_prop( 'date_created', $context );
166 166
     }
167 167
 
168 168
     /**
169
-	 * Get GMT date when the item was created.
170
-	 *
171
-	 * @since 1.0.19
172
-	 * @param  string $context View or edit context.
173
-	 * @return string
174
-	 */
175
-	public function get_date_created_gmt( $context = 'view' ) {
169
+     * Get GMT date when the item was created.
170
+     *
171
+     * @since 1.0.19
172
+     * @param  string $context View or edit context.
173
+     * @return string
174
+     */
175
+    public function get_date_created_gmt( $context = 'view' ) {
176 176
         $date = $this->get_date_created( $context );
177 177
 
178 178
         if ( $date ) {
179 179
             $date = get_gmt_from_date( $date );
180 180
         }
181
-		return $date;
181
+        return $date;
182 182
     }
183 183
 
184 184
     /**
185
-	 * Get date when the item was last modified.
186
-	 *
187
-	 * @since 1.0.19
188
-	 * @param  string $context View or edit context.
189
-	 * @return string
190
-	 */
191
-	public function get_date_modified( $context = 'view' ) {
192
-		return $this->get_prop( 'date_modified', $context );
185
+     * Get date when the item was last modified.
186
+     *
187
+     * @since 1.0.19
188
+     * @param  string $context View or edit context.
189
+     * @return string
190
+     */
191
+    public function get_date_modified( $context = 'view' ) {
192
+        return $this->get_prop( 'date_modified', $context );
193 193
     }
194 194
 
195 195
     /**
196
-	 * Get GMT date when the item was last modified.
197
-	 *
198
-	 * @since 1.0.19
199
-	 * @param  string $context View or edit context.
200
-	 * @return string
201
-	 */
202
-	public function get_date_modified_gmt( $context = 'view' ) {
196
+     * Get GMT date when the item was last modified.
197
+     *
198
+     * @since 1.0.19
199
+     * @param  string $context View or edit context.
200
+     * @return string
201
+     */
202
+    public function get_date_modified_gmt( $context = 'view' ) {
203 203
         $date = $this->get_date_modified( $context );
204 204
 
205 205
         if ( $date ) {
206 206
             $date = get_gmt_from_date( $date );
207 207
         }
208
-		return $date;
208
+        return $date;
209 209
     }
210 210
 
211 211
     /**
212
-	 * Get the item name.
213
-	 *
214
-	 * @since 1.0.19
215
-	 * @param  string $context View or edit context.
216
-	 * @return string
217
-	 */
218
-	public function get_name( $context = 'view' ) {
219
-		return $this->get_prop( 'name', $context );
212
+     * Get the item name.
213
+     *
214
+     * @since 1.0.19
215
+     * @param  string $context View or edit context.
216
+     * @return string
217
+     */
218
+    public function get_name( $context = 'view' ) {
219
+        return $this->get_prop( 'name', $context );
220 220
     }
221 221
 
222 222
     /**
223
-	 * Alias of self::get_name().
224
-	 *
225
-	 * @since 1.0.19
226
-	 * @param  string $context View or edit context.
227
-	 * @return string
228
-	 */
229
-	public function get_title( $context = 'view' ) {
230
-		return $this->get_name( $context );
223
+     * Alias of self::get_name().
224
+     *
225
+     * @since 1.0.19
226
+     * @param  string $context View or edit context.
227
+     * @return string
228
+     */
229
+    public function get_title( $context = 'view' ) {
230
+        return $this->get_name( $context );
231 231
     }
232 232
 
233 233
     /**
234
-	 * Get the item description.
235
-	 *
236
-	 * @since 1.0.19
237
-	 * @param  string $context View or edit context.
238
-	 * @return string
239
-	 */
240
-	public function get_description( $context = 'view' ) {
241
-		return $this->get_prop( 'description', $context );
234
+     * Get the item description.
235
+     *
236
+     * @since 1.0.19
237
+     * @param  string $context View or edit context.
238
+     * @return string
239
+     */
240
+    public function get_description( $context = 'view' ) {
241
+        return $this->get_prop( 'description', $context );
242 242
     }
243 243
 
244 244
     /**
245
-	 * Alias of self::get_description().
246
-	 *
247
-	 * @since 1.0.19
248
-	 * @param  string $context View or edit context.
249
-	 * @return string
250
-	 */
251
-	public function get_excerpt( $context = 'view' ) {
252
-		return $this->get_description( $context );
245
+     * Alias of self::get_description().
246
+     *
247
+     * @since 1.0.19
248
+     * @param  string $context View or edit context.
249
+     * @return string
250
+     */
251
+    public function get_excerpt( $context = 'view' ) {
252
+        return $this->get_description( $context );
253 253
     }
254 254
 
255 255
     /**
256
-	 * Alias of self::get_description().
257
-	 *
258
-	 * @since 1.0.19
259
-	 * @param  string $context View or edit context.
260
-	 * @return string
261
-	 */
262
-	public function get_summary( $context = 'view' ) {
263
-		return $this->get_description( $context );
256
+     * Alias of self::get_description().
257
+     *
258
+     * @since 1.0.19
259
+     * @param  string $context View or edit context.
260
+     * @return string
261
+     */
262
+    public function get_summary( $context = 'view' ) {
263
+        return $this->get_description( $context );
264 264
     }
265 265
 
266 266
     /**
267
-	 * Get the owner of the item.
268
-	 *
269
-	 * @since 1.0.19
270
-	 * @param  string $context View or edit context.
271
-	 * @return int
272
-	 */
273
-	public function get_author( $context = 'view' ) {
274
-		return (int) $this->get_prop( 'author', $context );
275
-	}
267
+     * Get the owner of the item.
268
+     *
269
+     * @since 1.0.19
270
+     * @param  string $context View or edit context.
271
+     * @return int
272
+     */
273
+    public function get_author( $context = 'view' ) {
274
+        return (int) $this->get_prop( 'author', $context );
275
+    }
276 276
 	
277
-	/**
278
-	 * Alias of self::get_author().
279
-	 *
280
-	 * @since 1.0.19
281
-	 * @param  string $context View or edit context.
282
-	 * @return int
283
-	 */
284
-	public function get_owner( $context = 'view' ) {
285
-		return $this->get_author( $context );
286
-    }
287
-
288
-    /**
289
-	 * Get the price of the item.
290
-	 *
291
-	 * @since 1.0.19
292
-	 * @param  string $context View or edit context.
293
-	 * @return float
294
-	 */
295
-	public function get_price( $context = 'view' ) {
277
+    /**
278
+     * Alias of self::get_author().
279
+     *
280
+     * @since 1.0.19
281
+     * @param  string $context View or edit context.
282
+     * @return int
283
+     */
284
+    public function get_owner( $context = 'view' ) {
285
+        return $this->get_author( $context );
286
+    }
287
+
288
+    /**
289
+     * Get the price of the item.
290
+     *
291
+     * @since 1.0.19
292
+     * @param  string $context View or edit context.
293
+     * @return float
294
+     */
295
+    public function get_price( $context = 'view' ) {
296 296
         return wpinv_sanitize_amount( $this->get_prop( 'price', $context ) );
297
-	}
297
+    }
298 298
 	
299
-	/**
300
-	 * Get the inital price of the item.
301
-	 *
302
-	 * @since 1.0.19
303
-	 * @param  string $context View or edit context.
304
-	 * @return float
305
-	 */
306
-	public function get_initial_price( $context = 'view' ) {
299
+    /**
300
+     * Get the inital price of the item.
301
+     *
302
+     * @since 1.0.19
303
+     * @param  string $context View or edit context.
304
+     * @return float
305
+     */
306
+    public function get_initial_price( $context = 'view' ) {
307 307
 
308
-		$price = (float) $this->get_price( $context );
308
+        $price = (float) $this->get_price( $context );
309 309
 
310
-		if ( $this->has_free_trial() ) {
311
-			$price = 0;
312
-		}
310
+        if ( $this->has_free_trial() ) {
311
+            $price = 0;
312
+        }
313 313
 
314 314
         return wpinv_sanitize_amount( apply_filters( 'wpinv_get_initial_item_price', $price, $this ) );
315 315
     }
316 316
 
317 317
     /**
318
-	 * Returns a formated price.
319
-	 *
320
-	 * @since 1.0.19
321
-	 * @param  string $context View or edit context.
322
-	 * @return string
323
-	 */
318
+     * Returns a formated price.
319
+     *
320
+     * @since 1.0.19
321
+     * @param  string $context View or edit context.
322
+     * @return string
323
+     */
324 324
     public function get_the_price() {
325 325
         return wpinv_price( wpinv_format_amount( $this->get_price() ) );
326
-	}
327
-
328
-	/**
329
-	 * Returns the formated initial price.
330
-	 *
331
-	 * @since 1.0.19
332
-	 * @param  string $context View or edit context.
333
-	 * @return string
334
-	 */
326
+    }
327
+
328
+    /**
329
+     * Returns the formated initial price.
330
+     *
331
+     * @since 1.0.19
332
+     * @param  string $context View or edit context.
333
+     * @return string
334
+     */
335 335
     public function get_the_initial_price() {
336 336
         return wpinv_price( wpinv_format_amount( $this->get_initial_price() ) );
337 337
     }
338 338
 
339 339
     /**
340
-	 * Get the VAT rule of the item.
341
-	 *
342
-	 * @since 1.0.19
343
-	 * @param  string $context View or edit context.
344
-	 * @return string
345
-	 */
346
-	public function get_vat_rule( $context = 'view' ) {
340
+     * Get the VAT rule of the item.
341
+     *
342
+     * @since 1.0.19
343
+     * @param  string $context View or edit context.
344
+     * @return string
345
+     */
346
+    public function get_vat_rule( $context = 'view' ) {
347 347
         return $this->get_prop( 'vat_rule', $context );
348 348
     }
349 349
 
350 350
     /**
351
-	 * Get the VAT class of the item.
352
-	 *
353
-	 * @since 1.0.19
354
-	 * @param  string $context View or edit context.
355
-	 * @return string
356
-	 */
357
-	public function get_vat_class( $context = 'view' ) {
351
+     * Get the VAT class of the item.
352
+     *
353
+     * @since 1.0.19
354
+     * @param  string $context View or edit context.
355
+     * @return string
356
+     */
357
+    public function get_vat_class( $context = 'view' ) {
358 358
         return $this->get_prop( 'vat_class', $context );
359 359
     }
360 360
 
361 361
     /**
362
-	 * Get the type of the item.
363
-	 *
364
-	 * @since 1.0.19
365
-	 * @param  string $context View or edit context.
366
-	 * @return string
367
-	 */
368
-	public function get_type( $context = 'view' ) {
362
+     * Get the type of the item.
363
+     *
364
+     * @since 1.0.19
365
+     * @param  string $context View or edit context.
366
+     * @return string
367
+     */
368
+    public function get_type( $context = 'view' ) {
369 369
         return $this->get_prop( 'type', $context );
370 370
     }
371 371
 
372 372
     /**
373
-	 * Get the custom id of the item.
374
-	 *
375
-	 * @since 1.0.19
376
-	 * @param  string $context View or edit context.
377
-	 * @return string
378
-	 */
379
-	public function get_custom_id( $context = 'view' ) {
373
+     * Get the custom id of the item.
374
+     *
375
+     * @since 1.0.19
376
+     * @param  string $context View or edit context.
377
+     * @return string
378
+     */
379
+    public function get_custom_id( $context = 'view' ) {
380 380
         return $this->get_prop( 'custom_id', $context );
381 381
     }
382 382
 
383 383
     /**
384
-	 * Get the custom name of the item.
385
-	 *
386
-	 * @since 1.0.19
387
-	 * @param  string $context View or edit context.
388
-	 * @return string
389
-	 */
390
-	public function get_custom_name( $context = 'view' ) {
384
+     * Get the custom name of the item.
385
+     *
386
+     * @since 1.0.19
387
+     * @param  string $context View or edit context.
388
+     * @return string
389
+     */
390
+    public function get_custom_name( $context = 'view' ) {
391 391
         return $this->get_prop( 'custom_name', $context );
392 392
     }
393 393
 
394 394
     /**
395
-	 * Get the custom singular name of the item.
396
-	 *
397
-	 * @since 1.0.19
398
-	 * @param  string $context View or edit context.
399
-	 * @return string
400
-	 */
401
-	public function get_custom_singular_name( $context = 'view' ) {
395
+     * Get the custom singular name of the item.
396
+     *
397
+     * @since 1.0.19
398
+     * @param  string $context View or edit context.
399
+     * @return string
400
+     */
401
+    public function get_custom_singular_name( $context = 'view' ) {
402 402
         return $this->get_prop( 'custom_singular_name', $context );
403 403
     }
404 404
 
405 405
     /**
406
-	 * Checks if an item is editable..
407
-	 *
408
-	 * @since 1.0.19
409
-	 * @param  string $context View or edit context.
410
-	 * @return int
411
-	 */
412
-	public function get_is_editable( $context = 'view' ) {
406
+     * Checks if an item is editable..
407
+     *
408
+     * @since 1.0.19
409
+     * @param  string $context View or edit context.
410
+     * @return int
411
+     */
412
+    public function get_is_editable( $context = 'view' ) {
413 413
         return (int) $this->get_prop( 'is_editable', $context );
414 414
     }
415 415
 
416 416
     /**
417
-	 * Alias of self::get_is_editable().
418
-	 *
419
-	 * @since 1.0.19
420
-	 * @param  string $context View or edit context.
421
-	 * @return int
422
-	 */
423
-	public function get_editable( $context = 'view' ) {
424
-		return $this->get_is_editable( $context );
417
+     * Alias of self::get_is_editable().
418
+     *
419
+     * @since 1.0.19
420
+     * @param  string $context View or edit context.
421
+     * @return int
422
+     */
423
+    public function get_editable( $context = 'view' ) {
424
+        return $this->get_is_editable( $context );
425 425
     }
426 426
 
427 427
     /**
428
-	 * Checks if dynamic pricing is enabled.
429
-	 *
430
-	 * @since 1.0.19
431
-	 * @param  string $context View or edit context.
432
-	 * @return int
433
-	 */
434
-	public function get_is_dynamic_pricing( $context = 'view' ) {
428
+     * Checks if dynamic pricing is enabled.
429
+     *
430
+     * @since 1.0.19
431
+     * @param  string $context View or edit context.
432
+     * @return int
433
+     */
434
+    public function get_is_dynamic_pricing( $context = 'view' ) {
435 435
         return (int) $this->get_prop( 'is_dynamic_pricing', $context );
436 436
     }
437 437
 
438 438
     /**
439
-	 * Returns the minimum price if dynamic pricing is enabled.
440
-	 *
441
-	 * @since 1.0.19
442
-	 * @param  string $context View or edit context.
443
-	 * @return float
444
-	 */
445
-	public function get_minimum_price( $context = 'view' ) {
439
+     * Returns the minimum price if dynamic pricing is enabled.
440
+     *
441
+     * @since 1.0.19
442
+     * @param  string $context View or edit context.
443
+     * @return float
444
+     */
445
+    public function get_minimum_price( $context = 'view' ) {
446 446
         return wpinv_sanitize_amount( $this->get_prop( 'minimum_price', $context ) );
447 447
     }
448 448
 
449 449
     /**
450
-	 * Checks if this is a recurring item.
451
-	 *
452
-	 * @since 1.0.19
453
-	 * @param  string $context View or edit context.
454
-	 * @return int
455
-	 */
456
-	public function get_is_recurring( $context = 'view' ) {
450
+     * Checks if this is a recurring item.
451
+     *
452
+     * @since 1.0.19
453
+     * @param  string $context View or edit context.
454
+     * @return int
455
+     */
456
+    public function get_is_recurring( $context = 'view' ) {
457 457
         return (int) $this->get_prop( 'is_recurring', $context );
458
-	}
458
+    }
459 459
 	
460
-	/**
461
-	 * Get the recurring price of the item.
462
-	 *
463
-	 * @since 1.0.19
464
-	 * @param  string $context View or edit context.
465
-	 * @return float
466
-	 */
467
-	public function get_recurring_price( $context = 'view' ) {
468
-		$price = $this->get_price( $context );
460
+    /**
461
+     * Get the recurring price of the item.
462
+     *
463
+     * @since 1.0.19
464
+     * @param  string $context View or edit context.
465
+     * @return float
466
+     */
467
+    public function get_recurring_price( $context = 'view' ) {
468
+        $price = $this->get_price( $context );
469 469
         return wpinv_sanitize_amount( apply_filters( 'wpinv_get_recurring_item_price', $price, $this->ID ) );
470
-	}
471
-
472
-	/**
473
-	 * Get the formatted recurring price of the item.
474
-	 *
475
-	 * @since 1.0.19
476
-	 * @param  string $context View or edit context.
477
-	 * @return string
478
-	 */
470
+    }
471
+
472
+    /**
473
+     * Get the formatted recurring price of the item.
474
+     *
475
+     * @since 1.0.19
476
+     * @param  string $context View or edit context.
477
+     * @return string
478
+     */
479 479
     public function get_the_recurring_price() {
480 480
         return wpinv_price( wpinv_format_amount( $this->get_recurring_price() ) );
481
-	}
482
-
483
-	/**
484
-	 * Get the first renewal date (in timestamps) of the item.
485
-	 *
486
-	 * @since 1.0.19
487
-	 * @return int
488
-	 */
489
-	public function get_first_renewal_date() {
490
-
491
-		$periods = array(
492
-			'D' => 'days',
493
-			'W' => 'weeks',
494
-			'M' => 'months',
495
-			'Y' => 'years',
496
-		);
497
-
498
-		$period   = $this->get_recurring_period();
499
-		$interval = $this->get_recurring_interval();
500
-
501
-		if ( $this->has_free_trial() ) {
502
-			$period   = $this->get_trial_period();
503
-			$interval = $this->get_trial_interval();
504
-		}
505
-
506
-		$period       = $periods[ $period ];
507
-		$interval     = empty( $interval ) ? 1 : $interval;
508
-		$next_renewal = strtotime( "+$interval $period", current_time( 'timestamp' ) );
481
+    }
482
+
483
+    /**
484
+     * Get the first renewal date (in timestamps) of the item.
485
+     *
486
+     * @since 1.0.19
487
+     * @return int
488
+     */
489
+    public function get_first_renewal_date() {
490
+
491
+        $periods = array(
492
+            'D' => 'days',
493
+            'W' => 'weeks',
494
+            'M' => 'months',
495
+            'Y' => 'years',
496
+        );
497
+
498
+        $period   = $this->get_recurring_period();
499
+        $interval = $this->get_recurring_interval();
500
+
501
+        if ( $this->has_free_trial() ) {
502
+            $period   = $this->get_trial_period();
503
+            $interval = $this->get_trial_interval();
504
+        }
505
+
506
+        $period       = $periods[ $period ];
507
+        $interval     = empty( $interval ) ? 1 : $interval;
508
+        $next_renewal = strtotime( "+$interval $period", current_time( 'timestamp' ) );
509 509
         return apply_filters( 'wpinv_get_first_renewal_date', $next_renewal, $this );
510 510
     }
511 511
 
512 512
     /**
513
-	 * Get the recurring period.
514
-	 *
515
-	 * @since 1.0.19
516
-	 * @param  bool $full Return abbreviation or in full.
517
-	 * @return string
518
-	 */
519
-	public function get_recurring_period( $full = false ) {
513
+     * Get the recurring period.
514
+     *
515
+     * @since 1.0.19
516
+     * @param  bool $full Return abbreviation or in full.
517
+     * @return string
518
+     */
519
+    public function get_recurring_period( $full = false ) {
520 520
         $period = $this->get_prop( 'recurring_period', 'view' );
521 521
 
522 522
         if ( $full && ! is_bool( $full ) ) {
@@ -527,63 +527,63 @@  discard block
 block discarded – undo
527 527
     }
528 528
 
529 529
     /**
530
-	 * Get the recurring interval.
531
-	 *
532
-	 * @since 1.0.19
533
-	 * @param  string $context View or edit context.
534
-	 * @return int
535
-	 */
536
-	public function get_recurring_interval( $context = 'view' ) {
537
-		$interval = absint( $this->get_prop( 'recurring_interval', $context ) );
530
+     * Get the recurring interval.
531
+     *
532
+     * @since 1.0.19
533
+     * @param  string $context View or edit context.
534
+     * @return int
535
+     */
536
+    public function get_recurring_interval( $context = 'view' ) {
537
+        $interval = absint( $this->get_prop( 'recurring_interval', $context ) );
538 538
 
539
-		if ( $interval < 1 ) {
540
-			$interval = 1;
541
-		}
539
+        if ( $interval < 1 ) {
540
+            $interval = 1;
541
+        }
542 542
 
543 543
         return $interval;
544 544
     }
545 545
 
546 546
     /**
547
-	 * Get the recurring limit.
548
-	 *
549
-	 * @since 1.0.19
550
-	 * @param  string $context View or edit context.
551
-	 * @return int
552
-	 */
553
-	public function get_recurring_limit( $context = 'view' ) {
547
+     * Get the recurring limit.
548
+     *
549
+     * @since 1.0.19
550
+     * @param  string $context View or edit context.
551
+     * @return int
552
+     */
553
+    public function get_recurring_limit( $context = 'view' ) {
554 554
         return (int) $this->get_prop( 'recurring_limit', $context );
555 555
     }
556 556
 
557 557
     /**
558
-	 * Checks if we have a free trial.
559
-	 *
560
-	 * @since 1.0.19
561
-	 * @param  string $context View or edit context.
562
-	 * @return int
563
-	 */
564
-	public function get_is_free_trial( $context = 'view' ) {
558
+     * Checks if we have a free trial.
559
+     *
560
+     * @since 1.0.19
561
+     * @param  string $context View or edit context.
562
+     * @return int
563
+     */
564
+    public function get_is_free_trial( $context = 'view' ) {
565 565
         return (int) $this->get_prop( 'is_free_trial', $context );
566 566
     }
567 567
 
568 568
     /**
569
-	 * Alias for self::get_is_free_trial().
570
-	 *
571
-	 * @since 1.0.19
572
-	 * @param  string $context View or edit context.
573
-	 * @return int
574
-	 */
575
-	public function get_free_trial( $context = 'view' ) {
569
+     * Alias for self::get_is_free_trial().
570
+     *
571
+     * @since 1.0.19
572
+     * @param  string $context View or edit context.
573
+     * @return int
574
+     */
575
+    public function get_free_trial( $context = 'view' ) {
576 576
         return $this->get_is_free_trial( $context );
577 577
     }
578 578
 
579 579
     /**
580
-	 * Get the trial period.
581
-	 *
582
-	 * @since 1.0.19
583
-	 * @param  bool $full Return abbreviation or in full.
584
-	 * @return string
585
-	 */
586
-	public function get_trial_period( $full = false ) {
580
+     * Get the trial period.
581
+     *
582
+     * @since 1.0.19
583
+     * @param  bool $full Return abbreviation or in full.
584
+     * @return string
585
+     */
586
+    public function get_trial_period( $full = false ) {
587 587
         $period = $this->get_prop( 'trial_period', 'view' );
588 588
 
589 589
         if ( $full && ! is_bool( $full ) ) {
@@ -594,104 +594,104 @@  discard block
 block discarded – undo
594 594
     }
595 595
 
596 596
     /**
597
-	 * Get the trial interval.
598
-	 *
599
-	 * @since 1.0.19
600
-	 * @param  string $context View or edit context.
601
-	 * @return int
602
-	 */
603
-	public function get_trial_interval( $context = 'view' ) {
597
+     * Get the trial interval.
598
+     *
599
+     * @since 1.0.19
600
+     * @param  string $context View or edit context.
601
+     * @return int
602
+     */
603
+    public function get_trial_interval( $context = 'view' ) {
604 604
         return (int) $this->get_prop( 'trial_interval', $context );
605
-	}
605
+    }
606 606
 	
607
-	/**
608
-	 * Get the item's edit url.
609
-	 *
610
-	 * @since 1.0.19
611
-	 * @return string
612
-	 */
613
-	public function get_edit_url() {
607
+    /**
608
+     * Get the item's edit url.
609
+     *
610
+     * @since 1.0.19
611
+     * @return string
612
+     */
613
+    public function get_edit_url() {
614 614
         return get_edit_post_link( $this->get_id() );
615
-	}
616
-
617
-	/**
618
-	 * Given an item's name/custom id, it returns its id.
619
-	 *
620
-	 *
621
-	 * @static
622
-	 * @param string $value The item name or custom id.
623
-	 * @param string $field Either name or custom_id.
624
-	 * @param string $type in case you need to search for a given type.
625
-	 * @since 1.0.15
626
-	 * @return int
627
-	 */
628
-	public static function get_item_id_by_field( $value, $field = 'custom_id', $type = '' ) {
629
-
630
-		// Trim the value.
631
-		$value = trim( $value );
632
-
633
-		if ( empty( $value ) ) {
634
-			return 0;
635
-		}
615
+    }
616
+
617
+    /**
618
+     * Given an item's name/custom id, it returns its id.
619
+     *
620
+     *
621
+     * @static
622
+     * @param string $value The item name or custom id.
623
+     * @param string $field Either name or custom_id.
624
+     * @param string $type in case you need to search for a given type.
625
+     * @since 1.0.15
626
+     * @return int
627
+     */
628
+    public static function get_item_id_by_field( $value, $field = 'custom_id', $type = '' ) {
629
+
630
+        // Trim the value.
631
+        $value = trim( $value );
632
+
633
+        if ( empty( $value ) ) {
634
+            return 0;
635
+        }
636 636
 
637 637
         // Valid fields.
638 638
         $fields = array( 'custom_id', 'name', 'slug' );
639 639
 
640
-		// Ensure a field has been passed.
641
-		if ( empty( $field ) || ! in_array( $field, $fields ) ) {
642
-			return 0;
643
-		}
644
-
645
-		if ( $field == 'name' ) {
646
-			$field = 'slug';
647
-		} 
648
-
649
-		// Maybe retrieve from the cache.
650
-		$item_id = wp_cache_get( $value, "getpaid_{$type}_item_{$field}s_to_item_ids" );
651
-		if ( ! empty( $item_id ) ) {
652
-			return $item_id;
653
-		}
654
-
655
-		// Fetch from the db.
656
-		if ( $field =='slug' ) {
657
-			$items = get_posts(
658
-				array(
659
-					'post_type'      => 'wpi_item',
660
-					'name'           => $value,
661
-					'posts_per_page' => 1,
662
-					'post_status'    => 'any',
663
-				)
664
-			);
665
-		}
666
-
667
-		if ( $field =='custom_id' ) {
668
-			$items = get_posts(
669
-				array(
670
-					'post_type'      => 'wpi_item',
671
-					'posts_per_page' => 1,
672
-					'post_status'    => 'any',
673
-					'meta_query'     => array(
674
-						array(
675
-							'key'   => '_wpinv_type',
676
-                			'value' => $type,
677
-						),
678
-						array(
679
-							'key'   => '_wpinv_custom_id',
680
-                			'value' => $type,
681
-						)
682
-					)
683
-				)
684
-			);
685
-		}
686
-
687
-		if ( empty( $items ) ) {
688
-			return 0;
689
-		}
690
-
691
-		// Update the cache with our data
692
-		wp_cache_set( $value, $items[0]->ID, "getpaid_{$type}_item_{$field}s_to_item_ids" );
693
-
694
-		return $items[0]->ID;
640
+        // Ensure a field has been passed.
641
+        if ( empty( $field ) || ! in_array( $field, $fields ) ) {
642
+            return 0;
643
+        }
644
+
645
+        if ( $field == 'name' ) {
646
+            $field = 'slug';
647
+        } 
648
+
649
+        // Maybe retrieve from the cache.
650
+        $item_id = wp_cache_get( $value, "getpaid_{$type}_item_{$field}s_to_item_ids" );
651
+        if ( ! empty( $item_id ) ) {
652
+            return $item_id;
653
+        }
654
+
655
+        // Fetch from the db.
656
+        if ( $field =='slug' ) {
657
+            $items = get_posts(
658
+                array(
659
+                    'post_type'      => 'wpi_item',
660
+                    'name'           => $value,
661
+                    'posts_per_page' => 1,
662
+                    'post_status'    => 'any',
663
+                )
664
+            );
665
+        }
666
+
667
+        if ( $field =='custom_id' ) {
668
+            $items = get_posts(
669
+                array(
670
+                    'post_type'      => 'wpi_item',
671
+                    'posts_per_page' => 1,
672
+                    'post_status'    => 'any',
673
+                    'meta_query'     => array(
674
+                        array(
675
+                            'key'   => '_wpinv_type',
676
+                            'value' => $type,
677
+                        ),
678
+                        array(
679
+                            'key'   => '_wpinv_custom_id',
680
+                            'value' => $type,
681
+                        )
682
+                    )
683
+                )
684
+            );
685
+        }
686
+
687
+        if ( empty( $items ) ) {
688
+            return 0;
689
+        }
690
+
691
+        // Update the cache with our data
692
+        wp_cache_set( $value, $items[0]->ID, "getpaid_{$type}_item_{$field}s_to_item_ids" );
693
+
694
+        return $items[0]->ID;
695 695
     }
696 696
 
697 697
     /**
@@ -724,52 +724,52 @@  discard block
 block discarded – undo
724 724
     */
725 725
 
726 726
     /**
727
-	 * Set parent order ID.
728
-	 *
729
-	 * @since 1.0.19
730
-	 */
731
-	public function set_parent_id( $value ) {
732
-		if ( $value && ( $value === $this->get_id() || ! get_post( $value ) ) ) {
733
-			return;
734
-		}
735
-		$this->set_prop( 'parent_id', absint( $value ) );
736
-	}
737
-
738
-    /**
739
-	 * Sets item status.
740
-	 *
741
-	 * @since 1.0.19
742
-	 * @param  string $status New status.
743
-	 * @return array details of change.
744
-	 */
745
-	public function set_status( $status ) {
727
+     * Set parent order ID.
728
+     *
729
+     * @since 1.0.19
730
+     */
731
+    public function set_parent_id( $value ) {
732
+        if ( $value && ( $value === $this->get_id() || ! get_post( $value ) ) ) {
733
+            return;
734
+        }
735
+        $this->set_prop( 'parent_id', absint( $value ) );
736
+    }
737
+
738
+    /**
739
+     * Sets item status.
740
+     *
741
+     * @since 1.0.19
742
+     * @param  string $status New status.
743
+     * @return array details of change.
744
+     */
745
+    public function set_status( $status ) {
746 746
         $old_status = $this->get_status();
747 747
 
748 748
         $this->set_prop( 'status', $status );
749 749
 
750
-		return array(
751
-			'from' => $old_status,
752
-			'to'   => $status,
753
-		);
750
+        return array(
751
+            'from' => $old_status,
752
+            'to'   => $status,
753
+        );
754 754
     }
755 755
 
756 756
     /**
757
-	 * Set plugin version when the item was created.
758
-	 *
759
-	 * @since 1.0.19
760
-	 */
761
-	public function set_version( $value ) {
762
-		$this->set_prop( 'version', $value );
757
+     * Set plugin version when the item was created.
758
+     *
759
+     * @since 1.0.19
760
+     */
761
+    public function set_version( $value ) {
762
+        $this->set_prop( 'version', $value );
763 763
     }
764 764
 
765 765
     /**
766
-	 * Set date when the item was created.
767
-	 *
768
-	 * @since 1.0.19
769
-	 * @param string $value Value to set.
766
+     * Set date when the item was created.
767
+     *
768
+     * @since 1.0.19
769
+     * @param string $value Value to set.
770 770
      * @return bool Whether or not the date was set.
771
-	 */
772
-	public function set_date_created( $value ) {
771
+     */
772
+    public function set_date_created( $value ) {
773 773
         $date = strtotime( $value );
774 774
 
775 775
         if ( $date ) {
@@ -782,13 +782,13 @@  discard block
 block discarded – undo
782 782
     }
783 783
 
784 784
     /**
785
-	 * Set date when the item was last modified.
786
-	 *
787
-	 * @since 1.0.19
788
-	 * @param string $value Value to set.
785
+     * Set date when the item was last modified.
786
+     *
787
+     * @since 1.0.19
788
+     * @param string $value Value to set.
789 789
      * @return bool Whether or not the date was set.
790
-	 */
791
-	public function set_date_modified( $value ) {
790
+     */
791
+    public function set_date_modified( $value ) {
792 792
         $date = strtotime( $value );
793 793
 
794 794
         if ( $date ) {
@@ -801,115 +801,115 @@  discard block
 block discarded – undo
801 801
     }
802 802
 
803 803
     /**
804
-	 * Set the item name.
805
-	 *
806
-	 * @since 1.0.19
807
-	 * @param  string $value New name.
808
-	 */
809
-	public function set_name( $value ) {
804
+     * Set the item name.
805
+     *
806
+     * @since 1.0.19
807
+     * @param  string $value New name.
808
+     */
809
+    public function set_name( $value ) {
810 810
         $name = sanitize_text_field( $value );
811
-		$this->set_prop( 'name', $name );
811
+        $this->set_prop( 'name', $name );
812 812
     }
813 813
 
814 814
     /**
815
-	 * Alias of self::set_name().
816
-	 *
817
-	 * @since 1.0.19
818
-	 * @param  string $value New name.
819
-	 */
820
-	public function set_title( $value ) {
821
-		$this->set_name( $value );
815
+     * Alias of self::set_name().
816
+     *
817
+     * @since 1.0.19
818
+     * @param  string $value New name.
819
+     */
820
+    public function set_title( $value ) {
821
+        $this->set_name( $value );
822 822
     }
823 823
 
824 824
     /**
825
-	 * Set the item description.
826
-	 *
827
-	 * @since 1.0.19
828
-	 * @param  string $value New description.
829
-	 */
830
-	public function set_description( $value ) {
825
+     * Set the item description.
826
+     *
827
+     * @since 1.0.19
828
+     * @param  string $value New description.
829
+     */
830
+    public function set_description( $value ) {
831 831
         $description = wp_kses_post( $value );
832
-		return $this->set_prop( 'description', $description );
832
+        return $this->set_prop( 'description', $description );
833 833
     }
834 834
 
835 835
     /**
836
-	 * Alias of self::set_description().
837
-	 *
838
-	 * @since 1.0.19
839
-	 * @param  string $value New description.
840
-	 */
841
-	public function set_excerpt( $value ) {
842
-		$this->set_description( $value );
836
+     * Alias of self::set_description().
837
+     *
838
+     * @since 1.0.19
839
+     * @param  string $value New description.
840
+     */
841
+    public function set_excerpt( $value ) {
842
+        $this->set_description( $value );
843 843
     }
844 844
 
845 845
     /**
846
-	 * Alias of self::set_description().
847
-	 *
848
-	 * @since 1.0.19
849
-	 * @param  string $value New description.
850
-	 */
851
-	public function set_summary( $value ) {
852
-		$this->set_description( $value );
846
+     * Alias of self::set_description().
847
+     *
848
+     * @since 1.0.19
849
+     * @param  string $value New description.
850
+     */
851
+    public function set_summary( $value ) {
852
+        $this->set_description( $value );
853 853
     }
854 854
 
855 855
     /**
856
-	 * Set the owner of the item.
857
-	 *
858
-	 * @since 1.0.19
859
-	 * @param  int $value New author.
860
-	 */
861
-	public function set_author( $value ) {
862
-		$this->set_prop( 'author', (int) $value );
863
-	}
856
+     * Set the owner of the item.
857
+     *
858
+     * @since 1.0.19
859
+     * @param  int $value New author.
860
+     */
861
+    public function set_author( $value ) {
862
+        $this->set_prop( 'author', (int) $value );
863
+    }
864 864
 	
865
-	/**
866
-	 * Alias of self::set_author().
867
-	 *
868
-	 * @since 1.0.19
869
-	 * @param  int $value New author.
870
-	 */
871
-	public function set_owner( $value ) {
872
-		$this->set_author( $value );
873
-    }
874
-
875
-    /**
876
-	 * Set the price of the item.
877
-	 *
878
-	 * @since 1.0.19
879
-	 * @param  float $value New price.
880
-	 */
881
-	public function set_price( $value ) {
865
+    /**
866
+     * Alias of self::set_author().
867
+     *
868
+     * @since 1.0.19
869
+     * @param  int $value New author.
870
+     */
871
+    public function set_owner( $value ) {
872
+        $this->set_author( $value );
873
+    }
874
+
875
+    /**
876
+     * Set the price of the item.
877
+     *
878
+     * @since 1.0.19
879
+     * @param  float $value New price.
880
+     */
881
+    public function set_price( $value ) {
882 882
         $this->set_prop( 'price', (float) wpinv_sanitize_amount( $value ) );
883 883
     }
884 884
 
885 885
     /**
886
-	 * Set the VAT rule of the item.
887
-	 *
888
-	 * @since 1.0.19
889
-	 * @param  string $value new rule.
890
-	 */
891
-	public function set_vat_rule( $value ) {
886
+     * Set the VAT rule of the item.
887
+     *
888
+     * @since 1.0.19
889
+     * @param  string $value new rule.
890
+     */
891
+    public function set_vat_rule( $value ) {
892 892
         $this->set_prop( 'vat_rule', $value );
893 893
     }
894 894
 
895 895
     /**
896
-	 * Set the VAT class of the item.
897
-	 *
898
-	 * @since 1.0.19
899
-	 * @param  string $value new class.
900
-	 */
901
-	public function set_vat_class( $value ) {
896
+     * Set the VAT class of the item.
897
+     *
898
+     * @since 1.0.19
899
+     * @param  string $value new class.
900
+     */
901
+    public function set_vat_class( $value ) {
902 902
         $this->set_prop( 'vat_class', $value );
903 903
     }
904 904
 
905 905
     /**
906
-	 * Set the type of the item.
907
-	 *
908
-	 * @since 1.0.19
909
-	 * @param  string $value new item type.
910
-	 * @return string
911
-	 */
912
-	public function set_type( $value ) {
906
+     * Set the type of the item.
907
+     *
908
+     * @since 1.0.19
909
+     * @param  string $value new item type.
910
+     * @return string
911
+     */
912
+    public function set_type( $value ) {
913 913
 
914 914
         if ( empty( $value ) ) {
915 915
             $value = 'custom';
@@ -919,134 +919,134 @@  discard block
 block discarded – undo
919 919
     }
920 920
 
921 921
     /**
922
-	 * Set the custom id of the item.
923
-	 *
924
-	 * @since 1.0.19
925
-	 * @param  string $value new custom id.
926
-	 */
927
-	public function set_custom_id( $value ) {
922
+     * Set the custom id of the item.
923
+     *
924
+     * @since 1.0.19
925
+     * @param  string $value new custom id.
926
+     */
927
+    public function set_custom_id( $value ) {
928 928
         $this->set_prop( 'custom_id', $value );
929 929
     }
930 930
 
931 931
     /**
932
-	 * Set the custom name of the item.
933
-	 *
934
-	 * @since 1.0.19
935
-	 * @param  string $value new custom name.
936
-	 */
937
-	public function set_custom_name( $value ) {
932
+     * Set the custom name of the item.
933
+     *
934
+     * @since 1.0.19
935
+     * @param  string $value new custom name.
936
+     */
937
+    public function set_custom_name( $value ) {
938 938
         $this->set_prop( 'custom_name', $value );
939 939
     }
940 940
 
941 941
     /**
942
-	 * Set the custom singular name of the item.
943
-	 *
944
-	 * @since 1.0.19
945
-	 * @param  string $value new custom singular name.
946
-	 */
947
-	public function set_custom_singular_name( $value ) {
942
+     * Set the custom singular name of the item.
943
+     *
944
+     * @since 1.0.19
945
+     * @param  string $value new custom singular name.
946
+     */
947
+    public function set_custom_singular_name( $value ) {
948 948
         $this->set_prop( 'custom_singular_name', $value );
949 949
     }
950 950
 
951 951
     /**
952
-	 * Sets if an item is editable..
953
-	 *
954
-	 * @since 1.0.19
955
-	 * @param  int|bool $value whether or not the item is editable.
956
-	 */
957
-	public function set_is_editable( $value ) {
958
-		if ( is_numeric( $value ) ) {
959
-			$this->set_prop( 'is_editable', (int) $value );
960
-		}
952
+     * Sets if an item is editable..
953
+     *
954
+     * @since 1.0.19
955
+     * @param  int|bool $value whether or not the item is editable.
956
+     */
957
+    public function set_is_editable( $value ) {
958
+        if ( is_numeric( $value ) ) {
959
+            $this->set_prop( 'is_editable', (int) $value );
960
+        }
961 961
     }
962 962
 
963 963
     /**
964
-	 * Sets if dynamic pricing is enabled.
965
-	 *
966
-	 * @since 1.0.19
967
-	 * @param  int|bool $value whether or not dynamic pricing is allowed.
968
-	 */
969
-	public function set_is_dynamic_pricing( $value ) {
964
+     * Sets if dynamic pricing is enabled.
965
+     *
966
+     * @since 1.0.19
967
+     * @param  int|bool $value whether or not dynamic pricing is allowed.
968
+     */
969
+    public function set_is_dynamic_pricing( $value ) {
970 970
         $this->set_prop( 'is_dynamic_pricing', (int) $value );
971 971
     }
972 972
 
973 973
     /**
974
-	 * Sets the minimum price if dynamic pricing is enabled.
975
-	 *
976
-	 * @since 1.0.19
977
-	 * @param  float $value minimum price.
978
-	 */
979
-	public function set_minimum_price( $value ) {
974
+     * Sets the minimum price if dynamic pricing is enabled.
975
+     *
976
+     * @since 1.0.19
977
+     * @param  float $value minimum price.
978
+     */
979
+    public function set_minimum_price( $value ) {
980 980
         $this->set_prop( 'minimum_price',  (float) wpinv_sanitize_amount( $value ) );
981 981
     }
982 982
 
983 983
     /**
984
-	 * Sets if this is a recurring item.
985
-	 *
986
-	 * @since 1.0.19
987
-	 * @param  int|bool $value whether or not dynamic pricing is allowed.
988
-	 */
989
-	public function set_is_recurring( $value ) {
984
+     * Sets if this is a recurring item.
985
+     *
986
+     * @since 1.0.19
987
+     * @param  int|bool $value whether or not dynamic pricing is allowed.
988
+     */
989
+    public function set_is_recurring( $value ) {
990 990
         $this->set_prop( 'is_recurring', (int) $value );
991 991
     }
992 992
 
993 993
     /**
994
-	 * Set the recurring period.
995
-	 *
996
-	 * @since 1.0.19
997
-	 * @param  string $value new period.
998
-	 */
999
-	public function set_recurring_period( $value ) {
994
+     * Set the recurring period.
995
+     *
996
+     * @since 1.0.19
997
+     * @param  string $value new period.
998
+     */
999
+    public function set_recurring_period( $value ) {
1000 1000
         $this->set_prop( 'recurring_period', $value );
1001 1001
     }
1002 1002
 
1003 1003
     /**
1004
-	 * Set the recurring interval.
1005
-	 *
1006
-	 * @since 1.0.19
1007
-	 * @param  int $value recurring interval.
1008
-	 */
1009
-	public function set_recurring_interval( $value ) {
1004
+     * Set the recurring interval.
1005
+     *
1006
+     * @since 1.0.19
1007
+     * @param  int $value recurring interval.
1008
+     */
1009
+    public function set_recurring_interval( $value ) {
1010 1010
         return $this->set_prop( 'recurring_interval', (int) $value );
1011 1011
     }
1012 1012
 
1013 1013
     /**
1014
-	 * Get the recurring limit.
1015
-	 * @since 1.0.19
1016
-	 * @param  int $value The recurring limit.
1017
-	 * @return int
1018
-	 */
1019
-	public function set_recurring_limit( $value ) {
1014
+     * Get the recurring limit.
1015
+     * @since 1.0.19
1016
+     * @param  int $value The recurring limit.
1017
+     * @return int
1018
+     */
1019
+    public function set_recurring_limit( $value ) {
1020 1020
         $this->set_prop( 'recurring_limit', (int) $value );
1021 1021
     }
1022 1022
 
1023 1023
     /**
1024
-	 * Checks if we have a free trial.
1025
-	 *
1026
-	 * @since 1.0.19
1027
-	 * @param  int|bool $value whether or not it has a free trial.
1028
-	 */
1029
-	public function set_is_free_trial( $value ) {
1024
+     * Checks if we have a free trial.
1025
+     *
1026
+     * @since 1.0.19
1027
+     * @param  int|bool $value whether or not it has a free trial.
1028
+     */
1029
+    public function set_is_free_trial( $value ) {
1030 1030
         $this->set_prop( 'is_free_trial', (int) $value );
1031 1031
     }
1032 1032
 
1033 1033
     /**
1034
-	 * Set the trial period.
1035
-	 *
1036
-	 * @since 1.0.19
1037
-	 * @param  string $value trial period.
1038
-	 */
1039
-	public function set_trial_period( $value ) {
1034
+     * Set the trial period.
1035
+     *
1036
+     * @since 1.0.19
1037
+     * @param  string $value trial period.
1038
+     */
1039
+    public function set_trial_period( $value ) {
1040 1040
         $this->set_prop( 'trial_period', $value );
1041 1041
     }
1042 1042
 
1043 1043
     /**
1044
-	 * Set the trial interval.
1045
-	 *
1046
-	 * @since 1.0.19
1047
-	 * @param  int $value trial interval.
1048
-	 */
1049
-	public function set_trial_interval( $value ) {
1044
+     * Set the trial interval.
1045
+     *
1046
+     * @since 1.0.19
1047
+     * @param  int $value trial interval.
1048
+     */
1049
+    public function set_trial_interval( $value ) {
1050 1050
         $this->set_prop( 'trial_interval', $value );
1051 1051
     }
1052 1052
 
@@ -1054,17 +1054,17 @@  discard block
 block discarded – undo
1054 1054
      * Create an item. For backwards compatibilty.
1055 1055
      * 
1056 1056
      * @deprecated
1057
-	 * @return int item id
1057
+     * @return int item id
1058 1058
      */
1059 1059
     public function create( $data = array() ) {
1060 1060
 
1061
-		// Set the properties.
1062
-		if ( is_array( $data ) ) {
1063
-			$this->set_props( $data );
1064
-		}
1061
+        // Set the properties.
1062
+        if ( is_array( $data ) ) {
1063
+            $this->set_props( $data );
1064
+        }
1065 1065
 
1066
-		// Save the item.
1067
-		return $this->save();
1066
+        // Save the item.
1067
+        return $this->save();
1068 1068
 
1069 1069
     }
1070 1070
 
@@ -1072,7 +1072,7 @@  discard block
 block discarded – undo
1072 1072
      * Updates an item. For backwards compatibilty.
1073 1073
      * 
1074 1074
      * @deprecated
1075
-	 * @return int item id
1075
+     * @return int item id
1076 1076
      */
1077 1077
     public function update( $data = array() ) {
1078 1078
         return $this->create( $data );
@@ -1088,84 +1088,84 @@  discard block
 block discarded – undo
1088 1088
 	*/
1089 1089
 
1090 1090
     /**
1091
-	 * Checks whether the item has enabled dynamic pricing.
1092
-	 *
1093
-	 * @since 1.0.19
1094
-	 * @return bool
1095
-	 */
1096
-	public function user_can_set_their_price() {
1091
+     * Checks whether the item has enabled dynamic pricing.
1092
+     *
1093
+     * @since 1.0.19
1094
+     * @return bool
1095
+     */
1096
+    public function user_can_set_their_price() {
1097 1097
         return (bool) $this->get_is_dynamic_pricing();
1098
-	}
1098
+    }
1099 1099
 	
1100
-	/**
1101
-	 * Checks whether the item is recurring.
1102
-	 *
1103
-	 * @since 1.0.19
1104
-	 * @return bool
1105
-	 */
1106
-	public function is_recurring() {
1100
+    /**
1101
+     * Checks whether the item is recurring.
1102
+     *
1103
+     * @since 1.0.19
1104
+     * @return bool
1105
+     */
1106
+    public function is_recurring() {
1107 1107
         return (bool) $this->get_is_recurring();
1108 1108
     }
1109 1109
 
1110 1110
     /**
1111
-	 * Checks whether the item has a free trial.
1112
-	 *
1113
-	 * @since 1.0.19
1114
-	 * @return bool
1115
-	 */
1111
+     * Checks whether the item has a free trial.
1112
+     *
1113
+     * @since 1.0.19
1114
+     * @return bool
1115
+     */
1116 1116
     public function has_free_trial() {
1117 1117
         $has_trial = $this->is_recurring() && (bool) $this->get_free_trial() ? true : false;
1118 1118
         return (bool) apply_filters( 'wpinv_item_has_free_trial', $has_trial, $this->ID, $this );
1119 1119
     }
1120 1120
 
1121 1121
     /**
1122
-	 * Checks whether the item is free.
1123
-	 *
1124
-	 * @since 1.0.19
1125
-	 * @return bool
1126
-	 */
1122
+     * Checks whether the item is free.
1123
+     *
1124
+     * @since 1.0.19
1125
+     * @return bool
1126
+     */
1127 1127
     public function is_free() {
1128 1128
         $is_free   = $this->get_price() == 0;
1129 1129
         return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID, $this );
1130 1130
     }
1131 1131
 
1132 1132
     /**
1133
-	 * Checks the item status against a passed in status.
1134
-	 *
1135
-	 * @param array|string $status Status to check.
1136
-	 * @return bool
1137
-	 */
1138
-	public function has_status( $status ) {
1139
-		$has_status = ( is_array( $status ) && in_array( $this->get_status(), $status, true ) ) || $this->get_status() === $status;
1140
-		return (bool) apply_filters( 'getpaid_item_has_status', $has_status, $this, $status );
1133
+     * Checks the item status against a passed in status.
1134
+     *
1135
+     * @param array|string $status Status to check.
1136
+     * @return bool
1137
+     */
1138
+    public function has_status( $status ) {
1139
+        $has_status = ( is_array( $status ) && in_array( $this->get_status(), $status, true ) ) || $this->get_status() === $status;
1140
+        return (bool) apply_filters( 'getpaid_item_has_status', $has_status, $this, $status );
1141 1141
     }
1142 1142
 
1143 1143
     /**
1144
-	 * Checks the item type against a passed in types.
1145
-	 *
1146
-	 * @param array|string $type Type to check.
1147
-	 * @return bool
1148
-	 */
1149
-	public function is_type( $type ) {
1150
-		$is_type = ( is_array( $type ) && in_array( $this->get_type(), $type, true ) ) || $this->get_type() === $type;
1151
-		return (bool) apply_filters( 'getpaid_item_is_type', $is_type, $this, $type );
1152
-	}
1144
+     * Checks the item type against a passed in types.
1145
+     *
1146
+     * @param array|string $type Type to check.
1147
+     * @return bool
1148
+     */
1149
+    public function is_type( $type ) {
1150
+        $is_type = ( is_array( $type ) && in_array( $this->get_type(), $type, true ) ) || $this->get_type() === $type;
1151
+        return (bool) apply_filters( 'getpaid_item_is_type', $is_type, $this, $type );
1152
+    }
1153 1153
 
1154 1154
     /**
1155
-	 * Checks whether the item is editable.
1156
-	 *
1157
-	 * @since 1.0.19
1158
-	 * @return bool
1159
-	 */
1155
+     * Checks whether the item is editable.
1156
+     *
1157
+     * @since 1.0.19
1158
+     * @return bool
1159
+     */
1160 1160
     public function is_editable() {
1161 1161
         $is_editable = $this->get_is_editable();
1162 1162
         return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID, $this );
1163
-	}
1163
+    }
1164 1164
 
1165
-	/**
1166
-	 * Returns an array of cart fees.
1167
-	 */
1168
-	public function get_fees( $type = 'fee', $item_id = 0 ) {
1165
+    /**
1166
+     * Returns an array of cart fees.
1167
+     */
1168
+    public function get_fees( $type = 'fee', $item_id = 0 ) {
1169 1169
         global $wpi_session;
1170 1170
         
1171 1171
         $fees = $wpi_session->get( 'wpi_cart_fees' );
@@ -1209,11 +1209,11 @@  discard block
 block discarded – undo
1209 1209
     }
1210 1210
 
1211 1211
     /**
1212
-	 * Checks whether the item is purchasable.
1213
-	 *
1214
-	 * @since 1.0.19
1215
-	 * @return bool
1216
-	 */
1212
+     * Checks whether the item is purchasable.
1213
+     *
1214
+     * @since 1.0.19
1215
+     * @return bool
1216
+     */
1217 1217
     public function can_purchase() {
1218 1218
         $can_purchase = null !== $this->get_id();
1219 1219
 
@@ -1225,11 +1225,11 @@  discard block
 block discarded – undo
1225 1225
     }
1226 1226
 
1227 1227
     /**
1228
-	 * Checks whether the item supports dynamic pricing.
1229
-	 *
1230
-	 * @since 1.0.19
1231
-	 * @return bool
1232
-	 */
1228
+     * Checks whether the item supports dynamic pricing.
1229
+     *
1230
+     * @since 1.0.19
1231
+     * @return bool
1232
+     */
1233 1233
     public function supports_dynamic_pricing() {
1234 1234
         return (bool) apply_filters( 'wpinv_item_supports_dynamic_pricing', true, $this );
1235 1235
     }
Please login to merge, or discard this patch.
includes/api/class-getpaid-rest-posts-controller.php 1 patch
Indentation   +968 added lines, -968 removed lines patch added patch discarded remove patch
@@ -18,978 +18,978 @@
 block discarded – undo
18 18
 class GetPaid_REST_Posts_Controller extends GetPaid_REST_Controller {
19 19
 
20 20
     /**
21
-	 * Post type.
22
-	 *
23
-	 * @var string
24
-	 */
25
-	protected $post_type;
26
-
27
-	/**
28
-	 * Controls visibility on frontend.
29
-	 *
30
-	 * @var string
31
-	 */
32
-	public $public = false;
33
-
34
-	/**
35
-	 * Contains this controller's class name.
36
-	 *
37
-	 * @var string
38
-	 */
39
-	public $crud_class;
40
-
41
-	/**
42
-	 * Registers the routes for the objects of the controller.
43
-	 *
44
-	 * @since 1.0.19
45
-	 *
46
-	 * @see register_rest_route()
47
-	 */
48
-	public function register_namespace_routes( $namespace ) {
49
-
50
-		register_rest_route(
51
-			$namespace,
52
-			'/' . $this->rest_base,
53
-			array(
54
-				array(
55
-					'methods'             => WP_REST_Server::READABLE,
56
-					'callback'            => array( $this, 'get_items' ),
57
-					'permission_callback' => array( $this, 'get_items_permissions_check' ),
58
-					'args'                => $this->get_collection_params(),
59
-				),
60
-				array(
61
-					'methods'             => WP_REST_Server::CREATABLE,
62
-					'callback'            => array( $this, 'create_item' ),
63
-					'permission_callback' => array( $this, 'create_item_permissions_check' ),
64
-					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
65
-				),
66
-				'schema' => array( $this, 'get_public_item_schema' ),
67
-			)
68
-		);
69
-
70
-		$get_item_args = array(
71
-			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
72
-		);
73
-
74
-		register_rest_route(
75
-			$namespace,
76
-			'/' . $this->rest_base . '/(?P<id>[\d]+)',
77
-			array(
78
-				'args'   => array(
79
-					'id' => array(
80
-						'description' => __( 'Unique identifier for the object.', 'invoicing' ),
81
-						'type'        => 'integer',
82
-					),
83
-				),
84
-				array(
85
-					'methods'             => WP_REST_Server::READABLE,
86
-					'callback'            => array( $this, 'get_item' ),
87
-					'permission_callback' => array( $this, 'get_item_permissions_check' ),
88
-					'args'                => $get_item_args,
89
-				),
90
-				array(
91
-					'methods'             => WP_REST_Server::EDITABLE,
92
-					'callback'            => array( $this, 'update_item' ),
93
-					'permission_callback' => array( $this, 'update_item_permissions_check' ),
94
-					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
95
-				),
96
-				array(
97
-					'methods'             => WP_REST_Server::DELETABLE,
98
-					'callback'            => array( $this, 'delete_item' ),
99
-					'permission_callback' => array( $this, 'delete_item_permissions_check' ),
100
-					'args'                => array(
101
-						'force' => array(
102
-							'type'        => 'boolean',
103
-							'default'     => false,
104
-							'description' => __( 'Whether to bypass Trash and force deletion.', 'invoicing' ),
105
-						),
106
-					),
107
-				),
108
-				'schema' => array( $this, 'get_public_item_schema' ),
109
-			)
110
-		);
111
-
112
-		register_rest_route(
113
-			$namespace,
114
-			'/' . $this->rest_base . '/batch',
115
-			array(
116
-				array(
117
-					'methods'             => WP_REST_Server::EDITABLE,
118
-					'callback'            => array( $this, 'batch_items' ),
119
-					'permission_callback' => array( $this, 'batch_items_permissions_check' ),
120
-					'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
121
-				),
122
-				'schema' => array( $this, 'get_public_batch_schema' ),
123
-			)
124
-		);
125
-
126
-	}
127
-
128
-	/**
129
-	 * Check permissions of items on REST API.
130
-	 *
131
-	 * @since 1.0.19
132
-	 * @param string $context   Request context.
133
-	 * @param int    $object_id Post ID.
134
-	 * @return bool
135
-	 */
136
-	public function check_post_permissions( $context = 'read', $object_id = 0 ) {
137
-
138
-		$contexts = array(
139
-			'read'   => 'read_private_posts',
140
-			'create' => 'publish_posts',
141
-			'edit'   => 'edit_post',
142
-			'delete' => 'delete_post',
143
-			'batch'  => 'edit_others_posts',
144
-		);
145
-
146
-		if ( 'revision' === $this->post_type ) {
147
-			$permission = false;
148
-		} else {
149
-			$cap              = $contexts[ $context ];
150
-			$post_type_object = get_post_type_object( $this->post_type );
151
-			$permission       = current_user_can( $post_type_object->cap->$cap, $object_id );
152
-		}
153
-
154
-		return apply_filters( 'getpaid_rest_check_permissions', $permission, $context, $object_id, $this->post_type );
155
-	}
156
-
157
-	/**
158
-	 * Check if a given request has access to read items.
159
-	 *
160
-	 * @param  WP_REST_Request $request Full details about the request.
161
-	 * @return WP_Error|boolean
162
-	 */
163
-	public function get_items_permissions_check( $request ) {
164
-		return $this->check_post_permissions() ? true : new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
165
-	}
166
-
167
-	/**
168
-	 * Check if a given request has access to create an item.
169
-	 *
170
-	 * @param  WP_REST_Request $request Full details about the request.
171
-	 * @return WP_Error|boolean
172
-	 */
173
-	public function create_item_permissions_check( $request ) {
174
-		return $this->check_post_permissions( 'create' ) ? true : new WP_Error( 'rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
175
-	}
176
-
177
-	/**
178
-	 * Check if a given request has access to read an item.
179
-	 *
180
-	 * @param  WP_REST_Request $request Full details about the request.
181
-	 * @return WP_Error|boolean
182
-	 */
183
-	public function get_item_permissions_check( $request ) {
184
-		$post = get_post( (int) $request['id'] );
185
-
186
-		if ( $post && ! $this->check_post_permissions( 'read', $post->ID ) ) {
187
-			return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
188
-		}
189
-
190
-		return true;
191
-	}
192
-
193
-	/**
194
-	 * Check if a given request has access to update an item.
195
-	 *
196
-	 * @param  WP_REST_Request $request Full details about the request.
197
-	 * @return WP_Error|boolean
198
-	 */
199
-	public function update_item_permissions_check( $request ) {
200
-		$post = get_post( (int) $request['id'] );
201
-
202
-		if ( $post && ! $this->check_post_permissions( 'edit', $post->ID ) ) {
203
-			return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
204
-		}
205
-
206
-		return true;
207
-	}
208
-
209
-	/**
210
-	 * Check if a given request has access to delete an item.
211
-	 *
212
-	 * @param  WP_REST_Request $request Full details about the request.
213
-	 * @return bool|WP_Error
214
-	 */
215
-	public function delete_item_permissions_check( $request ) {
216
-		$post = get_post( (int) $request['id'] );
217
-
218
-		if ( $post && ! $this->check_post_permissions( 'delete', $post->ID ) ) {
219
-			return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
220
-		}
221
-
222
-		return true;
223
-	}
224
-
225
-	/**
226
-	 * Check if a given request has access batch create, update and delete items.
227
-	 *
228
-	 * @param  WP_REST_Request $request Full details about the request.
229
-	 *
230
-	 * @return boolean|WP_Error
231
-	 */
232
-	public function batch_items_permissions_check( $request ) {
233
-		return $this->check_post_permissions( 'batch' ) ? true : new WP_Error( 'rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
234
-	}
235
-
236
-	/**
237
-	 * Saves a single object.
238
-	 *
239
-	 * @param GetPaid_Data $object Object to save.
240
-	 * @return WP_Error|GetPaid_Data
241
-	 */
242
-	protected function save_object( $object ) {
243
-		$object->save();
244
-
245
-		if ( ! empty( $object->last_error ) ) {
246
-			return new WP_Error( 'rest_cannot_save', $object->last_error, array( 'status' => 400 ) );
247
-		}
248
-
249
-		return new $this->crud_class( $object->get_id() );
250
-	}
251
-
252
-	/**
253
-	 * Returns the item's object.
254
-	 *
255
-	 * Child classes must implement this method.
256
-	 * @since 1.0.13
257
-	 *
258
-	 * @param int|WP_Post $object_id Supplied ID.
259
-	 * @return GetPaid_Data|WP_Error GetPaid_Data object if ID is valid, WP_Error otherwise.
260
-	 */
261
-	protected function get_object( $object_id ) {
262
-
263
-		// Do we have an object?
264
-		if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
265
-			return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
266
-		}
267
-
268
-		// Fetch the object.
269
-		$object = new $this->crud_class( $object_id );
270
-		if ( ! empty( $object->last_error ) ) {
271
-			return new WP_Error( 'rest_object_invalid_id', $object->last_error, array( 'status' => 404 ) );
272
-		}
273
-
274
-		return $object->get_id() ? $object : new WP_Error( 'rest_object_invalid_id', __( 'Invalid ID.', 'invoicing' ), array( 'status' => 404 ) );
275
-
276
-	}
21
+     * Post type.
22
+     *
23
+     * @var string
24
+     */
25
+    protected $post_type;
26
+
27
+    /**
28
+     * Controls visibility on frontend.
29
+     *
30
+     * @var string
31
+     */
32
+    public $public = false;
33
+
34
+    /**
35
+     * Contains this controller's class name.
36
+     *
37
+     * @var string
38
+     */
39
+    public $crud_class;
40
+
41
+    /**
42
+     * Registers the routes for the objects of the controller.
43
+     *
44
+     * @since 1.0.19
45
+     *
46
+     * @see register_rest_route()
47
+     */
48
+    public function register_namespace_routes( $namespace ) {
49
+
50
+        register_rest_route(
51
+            $namespace,
52
+            '/' . $this->rest_base,
53
+            array(
54
+                array(
55
+                    'methods'             => WP_REST_Server::READABLE,
56
+                    'callback'            => array( $this, 'get_items' ),
57
+                    'permission_callback' => array( $this, 'get_items_permissions_check' ),
58
+                    'args'                => $this->get_collection_params(),
59
+                ),
60
+                array(
61
+                    'methods'             => WP_REST_Server::CREATABLE,
62
+                    'callback'            => array( $this, 'create_item' ),
63
+                    'permission_callback' => array( $this, 'create_item_permissions_check' ),
64
+                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
65
+                ),
66
+                'schema' => array( $this, 'get_public_item_schema' ),
67
+            )
68
+        );
69
+
70
+        $get_item_args = array(
71
+            'context' => $this->get_context_param( array( 'default' => 'view' ) ),
72
+        );
73
+
74
+        register_rest_route(
75
+            $namespace,
76
+            '/' . $this->rest_base . '/(?P<id>[\d]+)',
77
+            array(
78
+                'args'   => array(
79
+                    'id' => array(
80
+                        'description' => __( 'Unique identifier for the object.', 'invoicing' ),
81
+                        'type'        => 'integer',
82
+                    ),
83
+                ),
84
+                array(
85
+                    'methods'             => WP_REST_Server::READABLE,
86
+                    'callback'            => array( $this, 'get_item' ),
87
+                    'permission_callback' => array( $this, 'get_item_permissions_check' ),
88
+                    'args'                => $get_item_args,
89
+                ),
90
+                array(
91
+                    'methods'             => WP_REST_Server::EDITABLE,
92
+                    'callback'            => array( $this, 'update_item' ),
93
+                    'permission_callback' => array( $this, 'update_item_permissions_check' ),
94
+                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
95
+                ),
96
+                array(
97
+                    'methods'             => WP_REST_Server::DELETABLE,
98
+                    'callback'            => array( $this, 'delete_item' ),
99
+                    'permission_callback' => array( $this, 'delete_item_permissions_check' ),
100
+                    'args'                => array(
101
+                        'force' => array(
102
+                            'type'        => 'boolean',
103
+                            'default'     => false,
104
+                            'description' => __( 'Whether to bypass Trash and force deletion.', 'invoicing' ),
105
+                        ),
106
+                    ),
107
+                ),
108
+                'schema' => array( $this, 'get_public_item_schema' ),
109
+            )
110
+        );
111
+
112
+        register_rest_route(
113
+            $namespace,
114
+            '/' . $this->rest_base . '/batch',
115
+            array(
116
+                array(
117
+                    'methods'             => WP_REST_Server::EDITABLE,
118
+                    'callback'            => array( $this, 'batch_items' ),
119
+                    'permission_callback' => array( $this, 'batch_items_permissions_check' ),
120
+                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
121
+                ),
122
+                'schema' => array( $this, 'get_public_batch_schema' ),
123
+            )
124
+        );
125
+
126
+    }
127
+
128
+    /**
129
+     * Check permissions of items on REST API.
130
+     *
131
+     * @since 1.0.19
132
+     * @param string $context   Request context.
133
+     * @param int    $object_id Post ID.
134
+     * @return bool
135
+     */
136
+    public function check_post_permissions( $context = 'read', $object_id = 0 ) {
137
+
138
+        $contexts = array(
139
+            'read'   => 'read_private_posts',
140
+            'create' => 'publish_posts',
141
+            'edit'   => 'edit_post',
142
+            'delete' => 'delete_post',
143
+            'batch'  => 'edit_others_posts',
144
+        );
145
+
146
+        if ( 'revision' === $this->post_type ) {
147
+            $permission = false;
148
+        } else {
149
+            $cap              = $contexts[ $context ];
150
+            $post_type_object = get_post_type_object( $this->post_type );
151
+            $permission       = current_user_can( $post_type_object->cap->$cap, $object_id );
152
+        }
153
+
154
+        return apply_filters( 'getpaid_rest_check_permissions', $permission, $context, $object_id, $this->post_type );
155
+    }
156
+
157
+    /**
158
+     * Check if a given request has access to read items.
159
+     *
160
+     * @param  WP_REST_Request $request Full details about the request.
161
+     * @return WP_Error|boolean
162
+     */
163
+    public function get_items_permissions_check( $request ) {
164
+        return $this->check_post_permissions() ? true : new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot list resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
165
+    }
166
+
167
+    /**
168
+     * Check if a given request has access to create an item.
169
+     *
170
+     * @param  WP_REST_Request $request Full details about the request.
171
+     * @return WP_Error|boolean
172
+     */
173
+    public function create_item_permissions_check( $request ) {
174
+        return $this->check_post_permissions( 'create' ) ? true : new WP_Error( 'rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
175
+    }
176
+
177
+    /**
178
+     * Check if a given request has access to read an item.
179
+     *
180
+     * @param  WP_REST_Request $request Full details about the request.
181
+     * @return WP_Error|boolean
182
+     */
183
+    public function get_item_permissions_check( $request ) {
184
+        $post = get_post( (int) $request['id'] );
185
+
186
+        if ( $post && ! $this->check_post_permissions( 'read', $post->ID ) ) {
187
+            return new WP_Error( 'rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
188
+        }
189
+
190
+        return true;
191
+    }
192
+
193
+    /**
194
+     * Check if a given request has access to update an item.
195
+     *
196
+     * @param  WP_REST_Request $request Full details about the request.
197
+     * @return WP_Error|boolean
198
+     */
199
+    public function update_item_permissions_check( $request ) {
200
+        $post = get_post( (int) $request['id'] );
201
+
202
+        if ( $post && ! $this->check_post_permissions( 'edit', $post->ID ) ) {
203
+            return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
204
+        }
205
+
206
+        return true;
207
+    }
208
+
209
+    /**
210
+     * Check if a given request has access to delete an item.
211
+     *
212
+     * @param  WP_REST_Request $request Full details about the request.
213
+     * @return bool|WP_Error
214
+     */
215
+    public function delete_item_permissions_check( $request ) {
216
+        $post = get_post( (int) $request['id'] );
217
+
218
+        if ( $post && ! $this->check_post_permissions( 'delete', $post->ID ) ) {
219
+            return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
220
+        }
221
+
222
+        return true;
223
+    }
224
+
225
+    /**
226
+     * Check if a given request has access batch create, update and delete items.
227
+     *
228
+     * @param  WP_REST_Request $request Full details about the request.
229
+     *
230
+     * @return boolean|WP_Error
231
+     */
232
+    public function batch_items_permissions_check( $request ) {
233
+        return $this->check_post_permissions( 'batch' ) ? true : new WP_Error( 'rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
234
+    }
235
+
236
+    /**
237
+     * Saves a single object.
238
+     *
239
+     * @param GetPaid_Data $object Object to save.
240
+     * @return WP_Error|GetPaid_Data
241
+     */
242
+    protected function save_object( $object ) {
243
+        $object->save();
244
+
245
+        if ( ! empty( $object->last_error ) ) {
246
+            return new WP_Error( 'rest_cannot_save', $object->last_error, array( 'status' => 400 ) );
247
+        }
248
+
249
+        return new $this->crud_class( $object->get_id() );
250
+    }
251
+
252
+    /**
253
+     * Returns the item's object.
254
+     *
255
+     * Child classes must implement this method.
256
+     * @since 1.0.13
257
+     *
258
+     * @param int|WP_Post $object_id Supplied ID.
259
+     * @return GetPaid_Data|WP_Error GetPaid_Data object if ID is valid, WP_Error otherwise.
260
+     */
261
+    protected function get_object( $object_id ) {
262
+
263
+        // Do we have an object?
264
+        if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
265
+            return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
266
+        }
267
+
268
+        // Fetch the object.
269
+        $object = new $this->crud_class( $object_id );
270
+        if ( ! empty( $object->last_error ) ) {
271
+            return new WP_Error( 'rest_object_invalid_id', $object->last_error, array( 'status' => 404 ) );
272
+        }
273
+
274
+        return $object->get_id() ? $object : new WP_Error( 'rest_object_invalid_id', __( 'Invalid ID.', 'invoicing' ), array( 'status' => 404 ) );
275
+
276
+    }
277 277
 	
278
-	/**
279
-	 * @deprecated
280
-	 */
281
-	public function get_post( $object_id ) {
282
-		return $this->get_object( $object_id );
278
+    /**
279
+     * @deprecated
280
+     */
281
+    public function get_post( $object_id ) {
282
+        return $this->get_object( $object_id );
283
+    }
284
+
285
+    /**
286
+     * Get a single object.
287
+     *
288
+     * @param WP_REST_Request $request Full details about the request.
289
+     * @return WP_Error|WP_REST_Response
290
+     */
291
+    public function get_item( $request ) {
292
+
293
+        // Fetch the item.
294
+        $object = $this->get_object( $request['id'] );
295
+
296
+        if ( is_wp_error( $object ) ) {
297
+            return $object;
298
+        }
299
+
300
+        // Generate a response.
301
+        $data     = $this->prepare_item_for_response( $object, $request );
302
+        $response = rest_ensure_response( $data );
303
+
304
+        // (Maybe) add a link to the html pagee.
305
+        if ( $this->public && ! is_wp_error( $response ) ) {
306
+            $response->link_header( 'alternate', get_permalink( $object->get_id() ), array( 'type' => 'text/html' ) );
307
+        }
308
+
309
+        return $response;
310
+    }
311
+
312
+    /**
313
+     * Create a single object.
314
+     *
315
+     * @param WP_REST_Request $request Full details about the request.
316
+     * @return WP_Error|WP_REST_Response
317
+     */
318
+    public function create_item( $request ) {
319
+
320
+        // Can not create an existing item.
321
+        if ( ! empty( $request['id'] ) ) {
322
+            /* translators: %s: post type */
323
+            return new WP_Error( "getpaid_rest_{$this->post_type}_exists", __( 'Cannot create existing resource.', 'invoicing' ), array( 'status' => 400 ) );
324
+        }
325
+
326
+        // Generate a GetPaid_Data object from the request.
327
+        $object = $this->prepare_item_for_database( $request );
328
+        if ( is_wp_error( $object ) ) {
329
+            return $object;
330
+        }
331
+
332
+        // Save the object.
333
+        $object = $this->save_object( $object );
334
+        if ( is_wp_error( $object ) ) {
335
+            return $object;
336
+        }
337
+
338
+        // Save special fields.
339
+        $save_special = $this->update_additional_fields_for_object( $object, $request );
340
+        if ( is_wp_error( $save_special ) ) {
341
+            $object->delete( true );
342
+            return $save_special;
343
+        }
344
+
345
+        /**
346
+         * Fires after a single item is created or updated via the REST API.
347
+         *
348
+         * @param WP_Post         $post      Post object.
349
+         * @param WP_REST_Request $request   Request object.
350
+         * @param boolean         $creating  True when creating item, false when updating.
351
+         */
352
+        do_action( "getpaid_rest_insert_{$this->post_type}", $object, $request, true );
353
+
354
+        $request->set_param( 'context', 'edit' );
355
+        $response = $this->prepare_item_for_response( $object, $request );
356
+        $response = rest_ensure_response( $response );
357
+        $response->set_status( 201 );
358
+        $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) );
359
+
360
+        return $response;
361
+    }
362
+
363
+    /**
364
+     * Update a single object.
365
+     *
366
+     * @param WP_REST_Request $request Full details about the request.
367
+     * @return WP_Error|WP_REST_Response
368
+     */
369
+    public function update_item( $request ) {
370
+
371
+        // Fetch the item.
372
+        $object = $this->get_object( $request['id'] );
373
+        if ( is_wp_error( $object ) ) {
374
+            return $object;
375
+        }
376
+
377
+        // Prepare the item for saving.
378
+        $object = $this->prepare_item_for_database( $request );
379
+        if ( is_wp_error( $object ) ) {
380
+            return $object;
381
+        }
382
+
383
+        // Save the item.
384
+        $object = $this->save_object( $object );
385
+        if ( is_wp_error( $object ) ) {
386
+            return $object;
387
+        }
388
+
389
+        // Save special fields (those added via hooks).
390
+        $save_special = $this->update_additional_fields_for_object( $object, $request );
391
+        if ( is_wp_error( $save_special ) ) {
392
+            return $save_special;
393
+        }
394
+
395
+        /**
396
+         * Fires after a single item is created or updated via the REST API.
397
+         *
398
+         * @param GetPaid_Data    $object    GetPaid_Data object.
399
+         * @param WP_REST_Request $request   Request object.
400
+         * @param boolean         $creating  True when creating item, false when updating.
401
+         */
402
+        do_action( "getpaid_rest_insert_{$this->post_type}", $object, $request, false );
403
+
404
+        $request->set_param( 'context', 'edit' );
405
+        $response = $this->prepare_item_for_response( $object, $request );
406
+        return rest_ensure_response( $response );
283 407
     }
284 408
 
285
-	/**
286
-	 * Get a single object.
287
-	 *
288
-	 * @param WP_REST_Request $request Full details about the request.
289
-	 * @return WP_Error|WP_REST_Response
290
-	 */
291
-	public function get_item( $request ) {
292
-
293
-		// Fetch the item.
294
-		$object = $this->get_object( $request['id'] );
295
-
296
-		if ( is_wp_error( $object ) ) {
297
-			return $object;
298
-		}
299
-
300
-		// Generate a response.
301
-		$data     = $this->prepare_item_for_response( $object, $request );
302
-		$response = rest_ensure_response( $data );
303
-
304
-		// (Maybe) add a link to the html pagee.
305
-		if ( $this->public && ! is_wp_error( $response ) ) {
306
-			$response->link_header( 'alternate', get_permalink( $object->get_id() ), array( 'type' => 'text/html' ) );
307
-		}
308
-
309
-		return $response;
310
-	}
311
-
312
-	/**
313
-	 * Create a single object.
314
-	 *
315
-	 * @param WP_REST_Request $request Full details about the request.
316
-	 * @return WP_Error|WP_REST_Response
317
-	 */
318
-	public function create_item( $request ) {
319
-
320
-		// Can not create an existing item.
321
-		if ( ! empty( $request['id'] ) ) {
322
-			/* translators: %s: post type */
323
-			return new WP_Error( "getpaid_rest_{$this->post_type}_exists", __( 'Cannot create existing resource.', 'invoicing' ), array( 'status' => 400 ) );
324
-		}
325
-
326
-		// Generate a GetPaid_Data object from the request.
327
-		$object = $this->prepare_item_for_database( $request );
328
-		if ( is_wp_error( $object ) ) {
329
-			return $object;
330
-		}
331
-
332
-		// Save the object.
333
-		$object = $this->save_object( $object );
334
-		if ( is_wp_error( $object ) ) {
335
-			return $object;
336
-		}
337
-
338
-		// Save special fields.
339
-		$save_special = $this->update_additional_fields_for_object( $object, $request );
340
-		if ( is_wp_error( $save_special ) ) {
341
-			$object->delete( true );
342
-			return $save_special;
343
-		}
344
-
345
-		/**
346
-		 * Fires after a single item is created or updated via the REST API.
347
-		 *
348
-		 * @param WP_Post         $post      Post object.
349
-		 * @param WP_REST_Request $request   Request object.
350
-		 * @param boolean         $creating  True when creating item, false when updating.
351
-		 */
352
-		do_action( "getpaid_rest_insert_{$this->post_type}", $object, $request, true );
353
-
354
-		$request->set_param( 'context', 'edit' );
355
-		$response = $this->prepare_item_for_response( $object, $request );
356
-		$response = rest_ensure_response( $response );
357
-		$response->set_status( 201 );
358
-		$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ) );
359
-
360
-		return $response;
361
-	}
362
-
363
-	/**
364
-	 * Update a single object.
365
-	 *
366
-	 * @param WP_REST_Request $request Full details about the request.
367
-	 * @return WP_Error|WP_REST_Response
368
-	 */
369
-	public function update_item( $request ) {
370
-
371
-		// Fetch the item.
372
-		$object = $this->get_object( $request['id'] );
373
-		if ( is_wp_error( $object ) ) {
374
-			return $object;
375
-		}
376
-
377
-		// Prepare the item for saving.
378
-		$object = $this->prepare_item_for_database( $request );
379
-		if ( is_wp_error( $object ) ) {
380
-			return $object;
381
-		}
382
-
383
-		// Save the item.
384
-		$object = $this->save_object( $object );
385
-		if ( is_wp_error( $object ) ) {
386
-			return $object;
387
-		}
388
-
389
-		// Save special fields (those added via hooks).
390
-		$save_special = $this->update_additional_fields_for_object( $object, $request );
391
-		if ( is_wp_error( $save_special ) ) {
392
-			return $save_special;
393
-		}
394
-
395
-		/**
396
-		 * Fires after a single item is created or updated via the REST API.
397
-		 *
398
-		 * @param GetPaid_Data    $object    GetPaid_Data object.
399
-		 * @param WP_REST_Request $request   Request object.
400
-		 * @param boolean         $creating  True when creating item, false when updating.
401
-		 */
402
-		do_action( "getpaid_rest_insert_{$this->post_type}", $object, $request, false );
403
-
404
-		$request->set_param( 'context', 'edit' );
405
-		$response = $this->prepare_item_for_response( $object, $request );
406
-		return rest_ensure_response( $response );
407
-	}
408
-
409
-	/**
410
-	 * Get a collection of objects.
411
-	 *
412
-	 * @param WP_REST_Request $request Full details about the request.
413
-	 * @return WP_Error|WP_REST_Response
414
-	 */
415
-	public function get_items( $request ) {
416
-		$args                         = array();
417
-		$args['offset']               = $request['offset'];
418
-		$args['order']                = $request['order'];
419
-		$args['orderby']              = $request['orderby'];
420
-		$args['paged']                = $request['page'];
421
-		$args['post__in']             = $request['include'];
422
-		$args['post__not_in']         = $request['exclude'];
423
-		$args['posts_per_page']       = $request['per_page'];
424
-		$args['name']                 = $request['slug'];
425
-		$args['post_parent__in']      = $request['parent'];
426
-		$args['post_parent__not_in']  = $request['parent_exclude'];
427
-		$args['s']                    = $request['search'];
428
-		$args['post_status']          = $request['status'] == 'any' ? 'any' : wpinv_parse_list( $request['status'] );
429
-
430
-		$args['date_query'] = array();
431
-		// Set before into date query. Date query must be specified as an array of an array.
432
-		if ( isset( $request['before'] ) ) {
433
-			$args['date_query'][0]['before'] = $request['before'];
434
-		}
435
-
436
-		// Set after into date query. Date query must be specified as an array of an array.
437
-		if ( isset( $request['after'] ) ) {
438
-			$args['date_query'][0]['after'] = $request['after'];
439
-		}
440
-
441
-		// Force the post_type & fields arguments, since they're not a user input variable.
442
-		$args['post_type'] = $this->post_type;
443
-		$args['fields']    = 'ids';
444
-
445
-		// Filter the query arguments for a request.
446
-		$args       = apply_filters( "getpaid_rest_{$this->post_type}_query", $args, $request );
447
-		$query_args = $this->prepare_items_query( $args, $request );
448
-
449
-		$posts_query = new WP_Query();
450
-		$query_result = $posts_query->query( $query_args );
451
-
452
-		$posts = array();
453
-		foreach ( $query_result as $post_id ) {
454
-			if ( ! $this->check_post_permissions( 'read', $post_id ) ) {
455
-				continue;
456
-			}
457
-
458
-			$data    = $this->prepare_item_for_response( $this->get_object( $post_id ), $request );
459
-			$posts[] = $this->prepare_response_for_collection( $data );
460
-		}
461
-
462
-		$page        = (int) $query_args['paged'];
463
-		$total_posts = $posts_query->found_posts;
464
-
465
-		if ( $total_posts < 1 ) {
466
-			// Out-of-bounds, run the query again without LIMIT for total count.
467
-			unset( $query_args['paged'] );
468
-			$count_query = new WP_Query();
469
-			$count_query->query( $query_args );
470
-			$total_posts = $count_query->found_posts;
471
-		}
472
-
473
-		$max_pages = ceil( $total_posts / (int) $query_args['posts_per_page'] );
474
-
475
-		$response = rest_ensure_response( $posts );
476
-		$response->header( 'X-WP-Total', (int) $total_posts );
477
-		$response->header( 'X-WP-TotalPages', (int) $max_pages );
478
-
479
-		$request_params = $request->get_query_params();
480
-		$base = add_query_arg( $request_params, rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
481
-
482
-		if ( $page > 1 ) {
483
-			$prev_page = $page - 1;
484
-			if ( $prev_page > $max_pages ) {
485
-				$prev_page = $max_pages;
486
-			}
487
-			$prev_link = add_query_arg( 'page', $prev_page, $base );
488
-			$response->link_header( 'prev', $prev_link );
489
-		}
490
-		if ( $max_pages > $page ) {
491
-			$next_page = $page + 1;
492
-			$next_link = add_query_arg( 'page', $next_page, $base );
493
-			$response->link_header( 'next', $next_link );
494
-		}
495
-
496
-		return $response;
497
-	}
498
-
499
-	/**
500
-	 * Delete a single item.
501
-	 *
502
-	 * @param WP_REST_Request $request Full details about the request.
503
-	 * @return WP_REST_Response|WP_Error
504
-	 */
505
-	public function delete_item( $request ) {
506
-
507
-		// Fetch the item.
508
-		$item = $this->get_object( $request['id'] );
509
-		if ( is_wp_error( $item ) ) {
510
-			return $item;
511
-		}
512
-
513
-		$supports_trash = EMPTY_TRASH_DAYS > 0;
514
-		$force          = $supports_trash && (bool) $request['force'];
515
-
516
-		if ( ! $this->check_post_permissions( 'delete', $item->ID ) ) {
517
-			return new WP_Error( "cannot_delete", __( 'Sorry, you are not allowed to delete this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
518
-		}
519
-
520
-		$request->set_param( 'context', 'edit' );
521
-		$response = $this->prepare_item_for_response( $item, $request );
522
-
523
-		if ( ! wp_delete_post( $item->ID, $force ) ) {
524
-			return new WP_Error( 'rest_cannot_delete', sprintf( __( 'The resource cannot be deleted.', 'invoicing' ), $this->post_type ), array( 'status' => 500 ) );
525
-		}
526
-
527
-		return $response;
528
-	}
529
-
530
-	/**
531
-	 * Prepare links for the request.
532
-	 *
533
-	 * @param GetPaid_Data    $object GetPaid_Data object.
534
-	 * @return array Links for the given object.
535
-	 */
536
-	protected function prepare_links( $object ) {
537
-
538
-		$links = array(
539
-			'self'       => array(
540
-				'href'   => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
541
-			),
542
-			'collection' => array(
543
-				'href'   => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
544
-			),
545
-		);
546
-
547
-		if ( is_callable( array( $object, 'get_user_id' ) ) ) {
548
-			$links['user'] = array(
549
-				'href'       => rest_url( 'wp/v2/users/' . call_user_func(  array( $object, 'get_user_id' )  ) ),
550
-				'embeddable' => true,
551
-			);
552
-		}
553
-
554
-		return $links;
555
-	}
556
-
557
-	/**
558
-	 * Determine the allowed query_vars for a get_items() response and
559
-	 * prepare for WP_Query.
560
-	 *
561
-	 * @param array           $prepared_args Prepared arguments.
562
-	 * @param WP_REST_Request $request Request object.
563
-	 * @return array          $query_args
564
-	 */
565
-	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
566
-
567
-		$valid_vars = array_flip( $this->get_allowed_query_vars() );
568
-		$query_args = array();
569
-		foreach ( $valid_vars as $var => $index ) {
570
-			if ( isset( $prepared_args[ $var ] ) ) {
571
-				$query_args[ $var ] = apply_filters( "getpaid_rest_query_var-{$var}", $prepared_args[ $var ] );
572
-			}
573
-		}
574
-
575
-		$query_args['ignore_sticky_posts'] = true;
576
-
577
-		if ( 'include' === $query_args['orderby'] ) {
578
-			$query_args['orderby'] = 'post__in';
579
-		} elseif ( 'id' === $query_args['orderby'] ) {
580
-			$query_args['orderby'] = 'ID'; // ID must be capitalized.
581
-		} elseif ( 'slug' === $query_args['orderby'] ) {
582
-			$query_args['orderby'] = 'name';
583
-		}
584
-
585
-		return apply_filters( 'getpaid_rest_prepare_items_query', $query_args, $request, $this );
586
-
587
-	}
588
-
589
-	/**
590
-	 * Get all the WP Query vars that are allowed for the API request.
591
-	 *
592
-	 * @return array
593
-	 */
594
-	protected function get_allowed_query_vars() {
595
-		global $wp;
596
-
597
-		/**
598
-		 * Filter the publicly allowed query vars.
599
-		 *
600
-		 * Allows adjusting of the default query vars that are made public.
601
-		 *
602
-		 * @param array  Array of allowed WP_Query query vars.
603
-		 */
604
-		$valid_vars = apply_filters( 'query_vars', $wp->public_query_vars );
605
-
606
-		$post_type_obj = get_post_type_object( $this->post_type );
607
-		if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
608
-			$private = apply_filters( 'getpaid_rest_private_query_vars', $wp->private_query_vars );
609
-			$valid_vars = array_merge( $valid_vars, $private );
610
-		}
611
-
612
-		// Define our own in addition to WP's normal vars.
613
-		$rest_valid = array(
614
-			'post_status',
615
-			'date_query',
616
-			'ignore_sticky_posts',
617
-			'offset',
618
-			'post__in',
619
-			'post__not_in',
620
-			'post_parent',
621
-			'post_parent__in',
622
-			'post_parent__not_in',
623
-			'posts_per_page',
624
-			'meta_query',
625
-			'tax_query',
626
-			'meta_key',
627
-			'meta_value',
628
-			'meta_compare',
629
-			'meta_value_num',
630
-		);
631
-		$valid_vars = array_merge( $valid_vars, $rest_valid );
632
-
633
-		// Filter allowed query vars for the REST API.
634
-		$valid_vars = apply_filters( 'getpaid_rest_query_vars', $valid_vars, $this );
635
-
636
-		return $valid_vars;
637
-	}
638
-
639
-	/**
640
-	 * Get the query params for collections of attachments.
641
-	 *
642
-	 * @return array
643
-	 */
644
-	public function get_collection_params() {
645
-		$params = parent::get_collection_params();
646
-
647
-		$params['context']['default'] = 'view';
648
-
649
-		$params['status'] = array(
650
-			'default'           => 'any',
651
-			'description'       => __( 'Limit result set to resources assigned one or more statuses.', 'invoicing' ),
652
-			'type'              => array( 'array', 'string' ),
653
-			'items'             => array(
654
-				'enum'          => array_merge( array( 'any' ), $this->get_post_statuses() ),
655
-				'type'          => 'string',
656
-			),
657
-			'sanitize_callback' => 'rest_validate_request_arg',
658
-		);
659
-
660
-		$params['after'] = array(
661
-			'description'        => __( 'Limit response to resources created after a given ISO8601 compliant date.', 'invoicing' ),
662
-			'type'               => 'string',
663
-			'format'             => 'string',
664
-			'validate_callback'  => 'rest_validate_request_arg',
665
-		);
666
-		$params['before'] = array(
667
-			'description'        => __( 'Limit response to resources created before a given ISO8601 compliant date.', 'invoicing' ),
668
-			'type'               => 'string',
669
-			'format'             => 'string',
670
-			'validate_callback'  => 'rest_validate_request_arg',
671
-		);
672
-		$params['exclude'] = array(
673
-			'description'       => __( 'Ensure result set excludes specific IDs.', 'invoicing' ),
674
-			'type'              => 'array',
675
-			'items'             => array(
676
-				'type'          => 'integer',
677
-			),
678
-			'default'           => array(),
679
-			'sanitize_callback' => 'wp_parse_id_list',
680
-		);
681
-		$params['include'] = array(
682
-			'description'       => __( 'Limit result set to specific ids.', 'invoicing' ),
683
-			'type'              => 'array',
684
-			'items'             => array(
685
-				'type'          => 'integer',
686
-			),
687
-			'default'           => array(),
688
-			'sanitize_callback' => 'wp_parse_id_list',
689
-		);
690
-		$params['offset'] = array(
691
-			'description'        => __( 'Offset the result set by a specific number of items.', 'invoicing' ),
692
-			'type'               => 'integer',
693
-			'sanitize_callback'  => 'absint',
694
-			'validate_callback'  => 'rest_validate_request_arg',
695
-		);
696
-		$params['order'] = array(
697
-			'description'        => __( 'Order sort attribute ascending or descending.', 'invoicing' ),
698
-			'type'               => 'string',
699
-			'default'            => 'desc',
700
-			'enum'               => array( 'asc', 'desc' ),
701
-			'validate_callback'  => 'rest_validate_request_arg',
702
-		);
703
-		$params['orderby'] = array(
704
-			'description'        => __( 'Sort collection by object attribute.', 'invoicing' ),
705
-			'type'               => 'string',
706
-			'default'            => 'date',
707
-			'enum'               => array(
708
-				'date',
709
-				'id',
710
-				'include',
711
-				'title',
712
-				'slug',
713
-				'modified',
714
-			),
715
-			'validate_callback'  => 'rest_validate_request_arg',
716
-		);
717
-
718
-		$post_type_obj = get_post_type_object( $this->post_type );
719
-
720
-		if ( isset( $post_type_obj->hierarchical ) && $post_type_obj->hierarchical ) {
721
-			$params['parent'] = array(
722
-				'description'       => __( 'Limit result set to those of particular parent IDs.', 'invoicing' ),
723
-				'type'              => 'array',
724
-				'items'             => array(
725
-					'type'          => 'integer',
726
-				),
727
-				'sanitize_callback' => 'wp_parse_id_list',
728
-				'default'           => array(),
729
-			);
730
-			$params['parent_exclude'] = array(
731
-				'description'       => __( 'Limit result set to all items except those of a particular parent ID.', 'invoicing' ),
732
-				'type'              => 'array',
733
-				'items'             => array(
734
-					'type'          => 'integer',
735
-				),
736
-				'sanitize_callback' => 'wp_parse_id_list',
737
-				'default'           => array(),
738
-			);
739
-		}
740
-
741
-		return $params;
742
-	}
743
-
744
-	/**
745
-	 * Retrieves the items's schema, conforming to JSON Schema.
746
-	 *
747
-	 * @since 1.0.19
748
-	 *
749
-	 * @return array Item schema data.
750
-	 */
751
-	public function get_item_schema() {
752
-
753
-		// Maybe retrieve the schema from cache.
754
-		if ( $this->schema ) {
755
-			return $this->add_additional_fields_schema( $this->schema );
756
-		}
757
-
758
-		$type   = str_replace( 'wpi_', '', $this->post_type );
759
-		$schema = array(
760
-			'$schema'    => 'http://json-schema.org/draft-04/schema#',
761
-			'title'      => $this->post_type,
762
-			'type'       => 'object',
763
-			'properties' => wpinv_get_data( "$type-schema" ),
764
-		);
765
-
766
-		// Filters the invoice schema for the REST API.
409
+    /**
410
+     * Get a collection of objects.
411
+     *
412
+     * @param WP_REST_Request $request Full details about the request.
413
+     * @return WP_Error|WP_REST_Response
414
+     */
415
+    public function get_items( $request ) {
416
+        $args                         = array();
417
+        $args['offset']               = $request['offset'];
418
+        $args['order']                = $request['order'];
419
+        $args['orderby']              = $request['orderby'];
420
+        $args['paged']                = $request['page'];
421
+        $args['post__in']             = $request['include'];
422
+        $args['post__not_in']         = $request['exclude'];
423
+        $args['posts_per_page']       = $request['per_page'];
424
+        $args['name']                 = $request['slug'];
425
+        $args['post_parent__in']      = $request['parent'];
426
+        $args['post_parent__not_in']  = $request['parent_exclude'];
427
+        $args['s']                    = $request['search'];
428
+        $args['post_status']          = $request['status'] == 'any' ? 'any' : wpinv_parse_list( $request['status'] );
429
+
430
+        $args['date_query'] = array();
431
+        // Set before into date query. Date query must be specified as an array of an array.
432
+        if ( isset( $request['before'] ) ) {
433
+            $args['date_query'][0]['before'] = $request['before'];
434
+        }
435
+
436
+        // Set after into date query. Date query must be specified as an array of an array.
437
+        if ( isset( $request['after'] ) ) {
438
+            $args['date_query'][0]['after'] = $request['after'];
439
+        }
440
+
441
+        // Force the post_type & fields arguments, since they're not a user input variable.
442
+        $args['post_type'] = $this->post_type;
443
+        $args['fields']    = 'ids';
444
+
445
+        // Filter the query arguments for a request.
446
+        $args       = apply_filters( "getpaid_rest_{$this->post_type}_query", $args, $request );
447
+        $query_args = $this->prepare_items_query( $args, $request );
448
+
449
+        $posts_query = new WP_Query();
450
+        $query_result = $posts_query->query( $query_args );
451
+
452
+        $posts = array();
453
+        foreach ( $query_result as $post_id ) {
454
+            if ( ! $this->check_post_permissions( 'read', $post_id ) ) {
455
+                continue;
456
+            }
457
+
458
+            $data    = $this->prepare_item_for_response( $this->get_object( $post_id ), $request );
459
+            $posts[] = $this->prepare_response_for_collection( $data );
460
+        }
461
+
462
+        $page        = (int) $query_args['paged'];
463
+        $total_posts = $posts_query->found_posts;
464
+
465
+        if ( $total_posts < 1 ) {
466
+            // Out-of-bounds, run the query again without LIMIT for total count.
467
+            unset( $query_args['paged'] );
468
+            $count_query = new WP_Query();
469
+            $count_query->query( $query_args );
470
+            $total_posts = $count_query->found_posts;
471
+        }
472
+
473
+        $max_pages = ceil( $total_posts / (int) $query_args['posts_per_page'] );
474
+
475
+        $response = rest_ensure_response( $posts );
476
+        $response->header( 'X-WP-Total', (int) $total_posts );
477
+        $response->header( 'X-WP-TotalPages', (int) $max_pages );
478
+
479
+        $request_params = $request->get_query_params();
480
+        $base = add_query_arg( $request_params, rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
481
+
482
+        if ( $page > 1 ) {
483
+            $prev_page = $page - 1;
484
+            if ( $prev_page > $max_pages ) {
485
+                $prev_page = $max_pages;
486
+            }
487
+            $prev_link = add_query_arg( 'page', $prev_page, $base );
488
+            $response->link_header( 'prev', $prev_link );
489
+        }
490
+        if ( $max_pages > $page ) {
491
+            $next_page = $page + 1;
492
+            $next_link = add_query_arg( 'page', $next_page, $base );
493
+            $response->link_header( 'next', $next_link );
494
+        }
495
+
496
+        return $response;
497
+    }
498
+
499
+    /**
500
+     * Delete a single item.
501
+     *
502
+     * @param WP_REST_Request $request Full details about the request.
503
+     * @return WP_REST_Response|WP_Error
504
+     */
505
+    public function delete_item( $request ) {
506
+
507
+        // Fetch the item.
508
+        $item = $this->get_object( $request['id'] );
509
+        if ( is_wp_error( $item ) ) {
510
+            return $item;
511
+        }
512
+
513
+        $supports_trash = EMPTY_TRASH_DAYS > 0;
514
+        $force          = $supports_trash && (bool) $request['force'];
515
+
516
+        if ( ! $this->check_post_permissions( 'delete', $item->ID ) ) {
517
+            return new WP_Error( "cannot_delete", __( 'Sorry, you are not allowed to delete this resource.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
518
+        }
519
+
520
+        $request->set_param( 'context', 'edit' );
521
+        $response = $this->prepare_item_for_response( $item, $request );
522
+
523
+        if ( ! wp_delete_post( $item->ID, $force ) ) {
524
+            return new WP_Error( 'rest_cannot_delete', sprintf( __( 'The resource cannot be deleted.', 'invoicing' ), $this->post_type ), array( 'status' => 500 ) );
525
+        }
526
+
527
+        return $response;
528
+    }
529
+
530
+    /**
531
+     * Prepare links for the request.
532
+     *
533
+     * @param GetPaid_Data    $object GetPaid_Data object.
534
+     * @return array Links for the given object.
535
+     */
536
+    protected function prepare_links( $object ) {
537
+
538
+        $links = array(
539
+            'self'       => array(
540
+                'href'   => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),
541
+            ),
542
+            'collection' => array(
543
+                'href'   => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
544
+            ),
545
+        );
546
+
547
+        if ( is_callable( array( $object, 'get_user_id' ) ) ) {
548
+            $links['user'] = array(
549
+                'href'       => rest_url( 'wp/v2/users/' . call_user_func(  array( $object, 'get_user_id' )  ) ),
550
+                'embeddable' => true,
551
+            );
552
+        }
553
+
554
+        return $links;
555
+    }
556
+
557
+    /**
558
+     * Determine the allowed query_vars for a get_items() response and
559
+     * prepare for WP_Query.
560
+     *
561
+     * @param array           $prepared_args Prepared arguments.
562
+     * @param WP_REST_Request $request Request object.
563
+     * @return array          $query_args
564
+     */
565
+    protected function prepare_items_query( $prepared_args = array(), $request = null ) {
566
+
567
+        $valid_vars = array_flip( $this->get_allowed_query_vars() );
568
+        $query_args = array();
569
+        foreach ( $valid_vars as $var => $index ) {
570
+            if ( isset( $prepared_args[ $var ] ) ) {
571
+                $query_args[ $var ] = apply_filters( "getpaid_rest_query_var-{$var}", $prepared_args[ $var ] );
572
+            }
573
+        }
574
+
575
+        $query_args['ignore_sticky_posts'] = true;
576
+
577
+        if ( 'include' === $query_args['orderby'] ) {
578
+            $query_args['orderby'] = 'post__in';
579
+        } elseif ( 'id' === $query_args['orderby'] ) {
580
+            $query_args['orderby'] = 'ID'; // ID must be capitalized.
581
+        } elseif ( 'slug' === $query_args['orderby'] ) {
582
+            $query_args['orderby'] = 'name';
583
+        }
584
+
585
+        return apply_filters( 'getpaid_rest_prepare_items_query', $query_args, $request, $this );
586
+
587
+    }
588
+
589
+    /**
590
+     * Get all the WP Query vars that are allowed for the API request.
591
+     *
592
+     * @return array
593
+     */
594
+    protected function get_allowed_query_vars() {
595
+        global $wp;
596
+
597
+        /**
598
+         * Filter the publicly allowed query vars.
599
+         *
600
+         * Allows adjusting of the default query vars that are made public.
601
+         *
602
+         * @param array  Array of allowed WP_Query query vars.
603
+         */
604
+        $valid_vars = apply_filters( 'query_vars', $wp->public_query_vars );
605
+
606
+        $post_type_obj = get_post_type_object( $this->post_type );
607
+        if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
608
+            $private = apply_filters( 'getpaid_rest_private_query_vars', $wp->private_query_vars );
609
+            $valid_vars = array_merge( $valid_vars, $private );
610
+        }
611
+
612
+        // Define our own in addition to WP's normal vars.
613
+        $rest_valid = array(
614
+            'post_status',
615
+            'date_query',
616
+            'ignore_sticky_posts',
617
+            'offset',
618
+            'post__in',
619
+            'post__not_in',
620
+            'post_parent',
621
+            'post_parent__in',
622
+            'post_parent__not_in',
623
+            'posts_per_page',
624
+            'meta_query',
625
+            'tax_query',
626
+            'meta_key',
627
+            'meta_value',
628
+            'meta_compare',
629
+            'meta_value_num',
630
+        );
631
+        $valid_vars = array_merge( $valid_vars, $rest_valid );
632
+
633
+        // Filter allowed query vars for the REST API.
634
+        $valid_vars = apply_filters( 'getpaid_rest_query_vars', $valid_vars, $this );
635
+
636
+        return $valid_vars;
637
+    }
638
+
639
+    /**
640
+     * Get the query params for collections of attachments.
641
+     *
642
+     * @return array
643
+     */
644
+    public function get_collection_params() {
645
+        $params = parent::get_collection_params();
646
+
647
+        $params['context']['default'] = 'view';
648
+
649
+        $params['status'] = array(
650
+            'default'           => 'any',
651
+            'description'       => __( 'Limit result set to resources assigned one or more statuses.', 'invoicing' ),
652
+            'type'              => array( 'array', 'string' ),
653
+            'items'             => array(
654
+                'enum'          => array_merge( array( 'any' ), $this->get_post_statuses() ),
655
+                'type'          => 'string',
656
+            ),
657
+            'sanitize_callback' => 'rest_validate_request_arg',
658
+        );
659
+
660
+        $params['after'] = array(
661
+            'description'        => __( 'Limit response to resources created after a given ISO8601 compliant date.', 'invoicing' ),
662
+            'type'               => 'string',
663
+            'format'             => 'string',
664
+            'validate_callback'  => 'rest_validate_request_arg',
665
+        );
666
+        $params['before'] = array(
667
+            'description'        => __( 'Limit response to resources created before a given ISO8601 compliant date.', 'invoicing' ),
668
+            'type'               => 'string',
669
+            'format'             => 'string',
670
+            'validate_callback'  => 'rest_validate_request_arg',
671
+        );
672
+        $params['exclude'] = array(
673
+            'description'       => __( 'Ensure result set excludes specific IDs.', 'invoicing' ),
674
+            'type'              => 'array',
675
+            'items'             => array(
676
+                'type'          => 'integer',
677
+            ),
678
+            'default'           => array(),
679
+            'sanitize_callback' => 'wp_parse_id_list',
680
+        );
681
+        $params['include'] = array(
682
+            'description'       => __( 'Limit result set to specific ids.', 'invoicing' ),
683
+            'type'              => 'array',
684
+            'items'             => array(
685
+                'type'          => 'integer',
686
+            ),
687
+            'default'           => array(),
688
+            'sanitize_callback' => 'wp_parse_id_list',
689
+        );
690
+        $params['offset'] = array(
691
+            'description'        => __( 'Offset the result set by a specific number of items.', 'invoicing' ),
692
+            'type'               => 'integer',
693
+            'sanitize_callback'  => 'absint',
694
+            'validate_callback'  => 'rest_validate_request_arg',
695
+        );
696
+        $params['order'] = array(
697
+            'description'        => __( 'Order sort attribute ascending or descending.', 'invoicing' ),
698
+            'type'               => 'string',
699
+            'default'            => 'desc',
700
+            'enum'               => array( 'asc', 'desc' ),
701
+            'validate_callback'  => 'rest_validate_request_arg',
702
+        );
703
+        $params['orderby'] = array(
704
+            'description'        => __( 'Sort collection by object attribute.', 'invoicing' ),
705
+            'type'               => 'string',
706
+            'default'            => 'date',
707
+            'enum'               => array(
708
+                'date',
709
+                'id',
710
+                'include',
711
+                'title',
712
+                'slug',
713
+                'modified',
714
+            ),
715
+            'validate_callback'  => 'rest_validate_request_arg',
716
+        );
717
+
718
+        $post_type_obj = get_post_type_object( $this->post_type );
719
+
720
+        if ( isset( $post_type_obj->hierarchical ) && $post_type_obj->hierarchical ) {
721
+            $params['parent'] = array(
722
+                'description'       => __( 'Limit result set to those of particular parent IDs.', 'invoicing' ),
723
+                'type'              => 'array',
724
+                'items'             => array(
725
+                    'type'          => 'integer',
726
+                ),
727
+                'sanitize_callback' => 'wp_parse_id_list',
728
+                'default'           => array(),
729
+            );
730
+            $params['parent_exclude'] = array(
731
+                'description'       => __( 'Limit result set to all items except those of a particular parent ID.', 'invoicing' ),
732
+                'type'              => 'array',
733
+                'items'             => array(
734
+                    'type'          => 'integer',
735
+                ),
736
+                'sanitize_callback' => 'wp_parse_id_list',
737
+                'default'           => array(),
738
+            );
739
+        }
740
+
741
+        return $params;
742
+    }
743
+
744
+    /**
745
+     * Retrieves the items's schema, conforming to JSON Schema.
746
+     *
747
+     * @since 1.0.19
748
+     *
749
+     * @return array Item schema data.
750
+     */
751
+    public function get_item_schema() {
752
+
753
+        // Maybe retrieve the schema from cache.
754
+        if ( $this->schema ) {
755
+            return $this->add_additional_fields_schema( $this->schema );
756
+        }
757
+
758
+        $type   = str_replace( 'wpi_', '', $this->post_type );
759
+        $schema = array(
760
+            '$schema'    => 'http://json-schema.org/draft-04/schema#',
761
+            'title'      => $this->post_type,
762
+            'type'       => 'object',
763
+            'properties' => wpinv_get_data( "$type-schema" ),
764
+        );
765
+
766
+        // Filters the invoice schema for the REST API.
767 767
         $schema = apply_filters( "wpinv_rest_{$type}_schema", $schema );
768 768
 
769
-		// Cache the invoice schema.
770
-		$this->schema = $schema;
771
-
772
-		return $this->add_additional_fields_schema( $this->schema );
773
-	}
774
-
775
-	/**
776
-	 * Only return writable props from schema.
777
-	 *
778
-	 * @param  array $schema Schema.
779
-	 * @return bool
780
-	 */
781
-	public function filter_writable_props( $schema ) {
782
-		return empty( $schema['readonly'] );
783
-	}
784
-
785
-	/**
786
-	 * Sanitizes and validates the list of post statuses.
787
-	 *
788
-	 * @since 1.0.13
789
-	 *
790
-	 * @param string|array    $statuses  One or more post statuses.
791
-	 * @param WP_REST_Request $request   Full details about the request.
792
-	 * @param string          $parameter Additional parameter to pass to validation.
793
-	 * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
794
-	 */
795
-	public function sanitize_post_statuses( $statuses, $request, $parameter ) {
796
-		return array_intersect( wp_parse_slug_list( $statuses ), $this->get_post_statuses() );
797
-	}
798
-
799
-	/**
800
-	 * Retrieves a valid list of post statuses.
801
-	 *
802
-	 * @since 1.0.19
803
-	 *
804
-	 * @return array A list of registered item statuses.
805
-	 */
806
-	public function get_post_statuses() {
807
-		return get_post_stati();
808
-	}
809
-
810
-	/**
811
-	 * Prepare a single object for create or update.
812
-	 *
813
-	 * @since 1.0.19
814
-	 * @param  WP_REST_Request $request Request object.
815
-	 * @return GetPaid_Data|WP_Error Data object or WP_Error.
816
-	 */
817
-	protected function prepare_item_for_database( $request ) {
818
-
819
-		// Do we have an object?
820
-		if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
821
-			return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
822
-		}
823
-
824
-		// Prepare the object.
825
-		$id        = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
826
-		$object    = new $this->crud_class( $id );
827
-
828
-		// Abort if an error exists.
829
-		if ( ! empty( $object->last_error ) ) {
830
-			return new WP_Error( 'invalid_item', $object->last_error );
831
-		}
832
-
833
-		$schema    = $this->get_item_schema();
834
-		$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
835
-
836
-		// Handle all writable props.
837
-		foreach ( $data_keys as $key ) {
838
-			$value = $request[ $key ];
839
-
840
-			if ( ! is_null( $value ) ) {
841
-				switch ( $key ) {
842
-
843
-					case 'meta_data':
844
-						if ( is_array( $value ) ) {
845
-							foreach ( $value as $meta ) {
846
-								$object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
847
-							}
848
-						}
849
-						break;
850
-
851
-					default:
852
-						if ( is_callable( array( $object, "set_{$key}" ) ) ) {
853
-							$object->{"set_{$key}"}( $value );
854
-						}
855
-						break;
856
-				}
857
-			}
858
-
859
-		}
860
-
861
-		// Filters an object before it is inserted via the REST API..
862
-		return apply_filters( "getpaid_rest_pre_insert_{$this->post_type}_object", $object, $request );
863
-	}
864
-
865
-	/**
866
-	 * Retrieves data from a GetPaid class.
867
-	 *
868
-	 * @since  1.0.19
869
-	 * @param  GetPaid_Meta_Data[]    $meta_data  meta data objects.
870
-	 * @return array
871
-	 */
872
-	protected function prepare_object_meta_data( $meta_data ) {
873
-		$meta = array();
874
-
875
-		foreach( $meta_data as $object ) {
876
-			$meta[] = $object->get_data();
877
-		}
878
-
879
-		return $meta;
880
-	}
881
-
882
-	/**
883
-	 * Retrieves invoice items.
884
-	 *
885
-	 * @since  1.0.19
886
-	 * @param  WPInv_Invoice $invoice  Invoice items.
887
-	 * @return array
888
-	 */
889
-	protected function prepare_invoice_items( $invoice ) {
890
-		$items = array();
891
-
892
-		foreach( $invoice->get_items() as $item ) {
893
-			$item_data = $item->prepare_data_for_saving();
894
-
895
-			if ( 'amount' == $invoice->get_template() ) {
896
-				$item_data['quantity'] = 1;
897
-			}
898
-
899
-			$items[] = $item_data;
900
-		}
901
-
902
-		return $items;
903
-	}
904
-
905
-	/**
906
-	 * Retrieves data from a GetPaid class.
907
-	 *
908
-	 * @since  1.0.19
909
-	 * @param  GetPaid_Data    $object  Data object.
910
-	 * @param array            $fields Fields to include.
911
-	 * @param string           $context either view or edit.
912
-	 * @return array
913
-	 */
914
-	protected function prepare_object_data( $object, $fields, $context = 'view' ) {
915
-
916
-		$data      = array();
917
-		$schema    = $this->get_item_schema();
918
-		$data_keys = array_keys( $schema['properties'] );
919
-
920
-		// Handle all writable props.
921
-		foreach ( $data_keys as $key ) {
922
-
923
-			// Abort if it is not included.
924
-			if ( ! empty( $fields ) && ! $this->is_field_included( $key, $fields ) ) {
925
-				continue;
926
-			}
927
-
928
-			// Handle meta data.
929
-			if ( $key == 'meta_data' ) {
930
-				$data['meta_data'] = $this->prepare_object_meta_data( $object->get_meta_data() );
931
-				continue;
932
-			}
933
-
934
-			// Handle items.
935
-			if ( $key == 'items' && is_a( $object, 'WPInv_Invoice' )  ) {
936
-				$data['items'] = $this->prepare_invoice_items( $object );
937
-				continue;
938
-			}
939
-
940
-			// Booleans.
941
-			if ( is_callable( array( $object, $key ) ) ) {
942
-				$data[ $key ] = $object->$key( $context );
943
-				continue;
944
-			}
945
-
946
-			// Get object value.
947
-			if ( is_callable( array( $object, "get_{$key}" ) ) ) {
948
-				$value = $object->{"get_{$key}"}( $context );
949
-
950
-				// If the value is an instance of GetPaid_Data...
951
-				if ( is_a( $value, 'GetPaid_Data' ) ) {
952
-					$value = $value->get_data( $context );
953
-				}
954
-
955
-				// For objects, retrieves it's properties.
956
-				$data[ $key ] = is_object( $value ) ? get_object_vars( $value ) :  $value ;
957
-				continue;
958
-			}
959
-
960
-			// The value does not exist on an object.
961
-			$data[ $key ]     = apply_filters( "getpaid_{$this->post_type}_{$key}_object_data", null, $object );
962
-
963
-		}
964
-
965
-		return $data;
966
-	}
967
-
968
-	/**
969
-	 * Prepare a single object output for response.
970
-	 *
971
-	 * @since  1.0.19
972
-	 * @param  GetPaid_Data    $object  Data object.
973
-	 * @param  WP_REST_Request $request Request object.
974
-	 * @return WP_REST_Response
975
-	 */
976
-	public function prepare_item_for_response( $object, $request ) {
977
-
978
-		// Fetch the fields to include in this response.
979
-		$fields = $this->get_fields_for_response( $request );
980
-
981
-		// Prepare object data.
982
-		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
983
-		$data    = $this->prepare_object_data( $object, $fields, $context );
984
-		$data    = $this->add_additional_fields_to_object( $data, $request );
985
-		$data    = $this->filter_response_by_context( $data, $context );
986
-
987
-		// Prepare the response.
988
-		$response = rest_ensure_response( $data );
989
-		$response->add_links( $this->prepare_links( $object, $request ) );
990
-
991
-		// Filter item response.
992
-		return apply_filters( "getpaid_rest_prepare_{$this->post_type}_object", $response, $object, $request );
993
-	}
769
+        // Cache the invoice schema.
770
+        $this->schema = $schema;
771
+
772
+        return $this->add_additional_fields_schema( $this->schema );
773
+    }
774
+
775
+    /**
776
+     * Only return writable props from schema.
777
+     *
778
+     * @param  array $schema Schema.
779
+     * @return bool
780
+     */
781
+    public function filter_writable_props( $schema ) {
782
+        return empty( $schema['readonly'] );
783
+    }
784
+
785
+    /**
786
+     * Sanitizes and validates the list of post statuses.
787
+     *
788
+     * @since 1.0.13
789
+     *
790
+     * @param string|array    $statuses  One or more post statuses.
791
+     * @param WP_REST_Request $request   Full details about the request.
792
+     * @param string          $parameter Additional parameter to pass to validation.
793
+     * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
794
+     */
795
+    public function sanitize_post_statuses( $statuses, $request, $parameter ) {
796
+        return array_intersect( wp_parse_slug_list( $statuses ), $this->get_post_statuses() );
797
+    }
798
+
799
+    /**
800
+     * Retrieves a valid list of post statuses.
801
+     *
802
+     * @since 1.0.19
803
+     *
804
+     * @return array A list of registered item statuses.
805
+     */
806
+    public function get_post_statuses() {
807
+        return get_post_stati();
808
+    }
809
+
810
+    /**
811
+     * Prepare a single object for create or update.
812
+     *
813
+     * @since 1.0.19
814
+     * @param  WP_REST_Request $request Request object.
815
+     * @return GetPaid_Data|WP_Error Data object or WP_Error.
816
+     */
817
+    protected function prepare_item_for_database( $request ) {
818
+
819
+        // Do we have an object?
820
+        if ( empty( $this->crud_class ) || ! class_exists( $this->crud_class ) ) {
821
+            return new WP_Error( 'no_crud_class', __( 'You need to specify a CRUD class for this controller', 'invoicing' ) );
822
+        }
823
+
824
+        // Prepare the object.
825
+        $id        = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
826
+        $object    = new $this->crud_class( $id );
827
+
828
+        // Abort if an error exists.
829
+        if ( ! empty( $object->last_error ) ) {
830
+            return new WP_Error( 'invalid_item', $object->last_error );
831
+        }
832
+
833
+        $schema    = $this->get_item_schema();
834
+        $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
835
+
836
+        // Handle all writable props.
837
+        foreach ( $data_keys as $key ) {
838
+            $value = $request[ $key ];
839
+
840
+            if ( ! is_null( $value ) ) {
841
+                switch ( $key ) {
842
+
843
+                    case 'meta_data':
844
+                        if ( is_array( $value ) ) {
845
+                            foreach ( $value as $meta ) {
846
+                                $object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
847
+                            }
848
+                        }
849
+                        break;
850
+
851
+                    default:
852
+                        if ( is_callable( array( $object, "set_{$key}" ) ) ) {
853
+                            $object->{"set_{$key}"}( $value );
854
+                        }
855
+                        break;
856
+                }
857
+            }
858
+
859
+        }
860
+
861
+        // Filters an object before it is inserted via the REST API..
862
+        return apply_filters( "getpaid_rest_pre_insert_{$this->post_type}_object", $object, $request );
863
+    }
864
+
865
+    /**
866
+     * Retrieves data from a GetPaid class.
867
+     *
868
+     * @since  1.0.19
869
+     * @param  GetPaid_Meta_Data[]    $meta_data  meta data objects.
870
+     * @return array
871
+     */
872
+    protected function prepare_object_meta_data( $meta_data ) {
873
+        $meta = array();
874
+
875
+        foreach( $meta_data as $object ) {
876
+            $meta[] = $object->get_data();
877
+        }
878
+
879
+        return $meta;
880
+    }
881
+
882
+    /**
883
+     * Retrieves invoice items.
884
+     *
885
+     * @since  1.0.19
886
+     * @param  WPInv_Invoice $invoice  Invoice items.
887
+     * @return array
888
+     */
889
+    protected function prepare_invoice_items( $invoice ) {
890
+        $items = array();
891
+
892
+        foreach( $invoice->get_items() as $item ) {
893
+            $item_data = $item->prepare_data_for_saving();
894
+
895
+            if ( 'amount' == $invoice->get_template() ) {
896
+                $item_data['quantity'] = 1;
897
+            }
898
+
899
+            $items[] = $item_data;
900
+        }
901
+
902
+        return $items;
903
+    }
904
+
905
+    /**
906
+     * Retrieves data from a GetPaid class.
907
+     *
908
+     * @since  1.0.19
909
+     * @param  GetPaid_Data    $object  Data object.
910
+     * @param array            $fields Fields to include.
911
+     * @param string           $context either view or edit.
912
+     * @return array
913
+     */
914
+    protected function prepare_object_data( $object, $fields, $context = 'view' ) {
915
+
916
+        $data      = array();
917
+        $schema    = $this->get_item_schema();
918
+        $data_keys = array_keys( $schema['properties'] );
919
+
920
+        // Handle all writable props.
921
+        foreach ( $data_keys as $key ) {
922
+
923
+            // Abort if it is not included.
924
+            if ( ! empty( $fields ) && ! $this->is_field_included( $key, $fields ) ) {
925
+                continue;
926
+            }
927
+
928
+            // Handle meta data.
929
+            if ( $key == 'meta_data' ) {
930
+                $data['meta_data'] = $this->prepare_object_meta_data( $object->get_meta_data() );
931
+                continue;
932
+            }
933
+
934
+            // Handle items.
935
+            if ( $key == 'items' && is_a( $object, 'WPInv_Invoice' )  ) {
936
+                $data['items'] = $this->prepare_invoice_items( $object );
937
+                continue;
938
+            }
939
+
940
+            // Booleans.
941
+            if ( is_callable( array( $object, $key ) ) ) {
942
+                $data[ $key ] = $object->$key( $context );
943
+                continue;
944
+            }
945
+
946
+            // Get object value.
947
+            if ( is_callable( array( $object, "get_{$key}" ) ) ) {
948
+                $value = $object->{"get_{$key}"}( $context );
949
+
950
+                // If the value is an instance of GetPaid_Data...
951
+                if ( is_a( $value, 'GetPaid_Data' ) ) {
952
+                    $value = $value->get_data( $context );
953
+                }
954
+
955
+                // For objects, retrieves it's properties.
956
+                $data[ $key ] = is_object( $value ) ? get_object_vars( $value ) :  $value ;
957
+                continue;
958
+            }
959
+
960
+            // The value does not exist on an object.
961
+            $data[ $key ]     = apply_filters( "getpaid_{$this->post_type}_{$key}_object_data", null, $object );
962
+
963
+        }
964
+
965
+        return $data;
966
+    }
967
+
968
+    /**
969
+     * Prepare a single object output for response.
970
+     *
971
+     * @since  1.0.19
972
+     * @param  GetPaid_Data    $object  Data object.
973
+     * @param  WP_REST_Request $request Request object.
974
+     * @return WP_REST_Response
975
+     */
976
+    public function prepare_item_for_response( $object, $request ) {
977
+
978
+        // Fetch the fields to include in this response.
979
+        $fields = $this->get_fields_for_response( $request );
980
+
981
+        // Prepare object data.
982
+        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
983
+        $data    = $this->prepare_object_data( $object, $fields, $context );
984
+        $data    = $this->add_additional_fields_to_object( $data, $request );
985
+        $data    = $this->filter_response_by_context( $data, $context );
986
+
987
+        // Prepare the response.
988
+        $response = rest_ensure_response( $data );
989
+        $response->add_links( $this->prepare_links( $object, $request ) );
990
+
991
+        // Filter item response.
992
+        return apply_filters( "getpaid_rest_prepare_{$this->post_type}_object", $response, $object, $request );
993
+    }
994 994
 
995 995
 }
Please login to merge, or discard this patch.
includes/api/class-wpinv-rest-items-controller.php 1 patch
Indentation   +140 added lines, -140 removed lines patch added patch discarded remove patch
@@ -15,149 +15,149 @@
 block discarded – undo
15 15
 class WPInv_REST_Items_Controller extends GetPaid_REST_Posts_Controller {
16 16
 
17 17
     /**
18
-	 * Post type.
19
-	 *
20
-	 * @var string
21
-	 */
22
-	protected $post_type = 'wpi_item';
18
+     * Post type.
19
+     *
20
+     * @var string
21
+     */
22
+    protected $post_type = 'wpi_item';
23 23
 	
24
-	/**
25
-	 * The base of this controller's route.
26
-	 *
27
-	 * @since 1.0.13
28
-	 * @var string
29
-	 */
30
-	protected $rest_base = 'items';
31
-
32
-	/** Contains this controller's class name.
33
-	 *
34
-	 * @var string
35
-	 */
36
-	public $crud_class = 'WPInv_Item';
37
-
38
-	/**
39
-	 * Registers the routes for the objects of the controller.
40
-	 *
41
-	 * @since 1.0.19
42
-	 *
43
-	 * @see register_rest_route()
44
-	 */
45
-	public function register_namespace_routes( $namespace ) {
46
-
47
-		parent::register_namespace_routes( $namespace );
48
-
49
-		register_rest_route(
50
-			$this->namespace,
51
-			'/' . $this->rest_base . '/item-types',
52
-			array(
53
-				array(
54
-					'methods'             => WP_REST_Server::READABLE,
55
-					'callback'            => array( $this, 'get_item_types' ),
56
-					'permission_callback' => array( $this, 'get_items_permissions_check' ),
57
-				),
58
-			)
59
-		);
60
-
61
-	}
62
-
63
-	/**
64
-	 * Handles rest requests for item types.
65
-	 *
66
-	 * @since 1.0.13
67
-	 * 
68
-	 * 
69
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
70
-	 */
71
-	public function get_item_types() {
72
-		return rest_ensure_response( wpinv_get_item_types() );
73
-	}
24
+    /**
25
+     * The base of this controller's route.
26
+     *
27
+     * @since 1.0.13
28
+     * @var string
29
+     */
30
+    protected $rest_base = 'items';
31
+
32
+    /** Contains this controller's class name.
33
+     *
34
+     * @var string
35
+     */
36
+    public $crud_class = 'WPInv_Item';
37
+
38
+    /**
39
+     * Registers the routes for the objects of the controller.
40
+     *
41
+     * @since 1.0.19
42
+     *
43
+     * @see register_rest_route()
44
+     */
45
+    public function register_namespace_routes( $namespace ) {
46
+
47
+        parent::register_namespace_routes( $namespace );
48
+
49
+        register_rest_route(
50
+            $this->namespace,
51
+            '/' . $this->rest_base . '/item-types',
52
+            array(
53
+                array(
54
+                    'methods'             => WP_REST_Server::READABLE,
55
+                    'callback'            => array( $this, 'get_item_types' ),
56
+                    'permission_callback' => array( $this, 'get_items_permissions_check' ),
57
+                ),
58
+            )
59
+        );
60
+
61
+    }
62
+
63
+    /**
64
+     * Handles rest requests for item types.
65
+     *
66
+     * @since 1.0.13
67
+     * 
68
+     * 
69
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
70
+     */
71
+    public function get_item_types() {
72
+        return rest_ensure_response( wpinv_get_item_types() );
73
+    }
74 74
 
75 75
     /**
76
-	 * Retrieves the query params for the items collection.
77
-	 *
78
-	 * @since 1.0.13
79
-	 *
80
-	 * @return array Collection parameters.
81
-	 */
82
-	public function get_collection_params() {
83
-
84
-		$params = array_merge(
85
-
86
-			parent::get_collection_params(),
87
-
88
-        	array(
89
-
90
-				// Item types
91
-				'type'                  => array(
92
-					'description'       => __( 'Type of items to fetch.', 'invoicing' ),
93
-					'type'              => array( 'array', 'string' ),
94
-					'default'           => 'any',
95
-					'items'             => array(
96
-						'enum'          => array_merge( array( 'any' ), wpinv_item_types() ),
97
-						'type'          => 'string',
98
-					),
99
-				),
100
-
101
-			)
102
-		);
103
-
104
-		// Filter collection parameters for the items controller.
105
-		return apply_filters( 'getpaid_rest_items_collection_params', $params, $this );
106
-
107
-	}
76
+     * Retrieves the query params for the items collection.
77
+     *
78
+     * @since 1.0.13
79
+     *
80
+     * @return array Collection parameters.
81
+     */
82
+    public function get_collection_params() {
83
+
84
+        $params = array_merge(
85
+
86
+            parent::get_collection_params(),
87
+
88
+            array(
89
+
90
+                // Item types
91
+                'type'                  => array(
92
+                    'description'       => __( 'Type of items to fetch.', 'invoicing' ),
93
+                    'type'              => array( 'array', 'string' ),
94
+                    'default'           => 'any',
95
+                    'items'             => array(
96
+                        'enum'          => array_merge( array( 'any' ), wpinv_item_types() ),
97
+                        'type'          => 'string',
98
+                    ),
99
+                ),
100
+
101
+            )
102
+        );
103
+
104
+        // Filter collection parameters for the items controller.
105
+        return apply_filters( 'getpaid_rest_items_collection_params', $params, $this );
106
+
107
+    }
108 108
 	
109
-	/**
110
-	 * Get all the WP Query vars that are allowed for the API request.
111
-	 *
112
-	 * @return array
113
-	 */
114
-	protected function get_allowed_query_vars() {
115
-		$vars = array_merge( array( 'type' ), parent::get_allowed_query_vars() );
116
-		return apply_filters( 'getpaid_rest_items_allowed_query_vars', $vars, $this );
117
-	}
118
-
119
-	/**
120
-	 * Determine the allowed query_vars for a get_items() response and
121
-	 * prepare for WP_Query.
122
-	 *
123
-	 * @param array           $prepared_args Prepared arguments.
124
-	 * @param WP_REST_Request $request Request object.
125
-	 * @return array          $query_args
126
-	 */
127
-	protected function prepare_items_query( $prepared_args = array(), $request = null ) {
128
-
129
-		$query_args = parent::prepare_items_query( $prepared_args );
130
-
131
-		// Retrieve items by type.
132
-		if (  isset( $query_args['type'] ) && 'any' != $query_args['type'] ) {
133
-
134
-			if ( empty( $query_args['meta_query'] ) ) {
135
-				$query_args['meta_query'] = array();
136
-			}
137
-
138
-			$types = wpinv_parse_list( $query_args['type'] );
139
-			$query_args['meta_query'][] = array(
140
-				'key'     => '_wpinv_type',
141
-				'value'   => implode( ',', $types ),
142
-				'compare' => 'IN',
143
-			);
144
-			unset( $query_args['type'] );
145
-
146
-		}
147
-
148
-		return apply_filters( 'getpaid_rest_items_prepare_items_query', $query_args, $request, $this );
149
-
150
-	}
151
-
152
-	/**
153
-	 * Retrieves a valid list of post statuses.
154
-	 *
155
-	 * @since 1.0.15
156
-	 *
157
-	 * @return array A list of registered item statuses.
158
-	 */
159
-	public function get_post_statuses() {
160
-		return array( 'draft', 'pending', 'publish' );
161
-	}
109
+    /**
110
+     * Get all the WP Query vars that are allowed for the API request.
111
+     *
112
+     * @return array
113
+     */
114
+    protected function get_allowed_query_vars() {
115
+        $vars = array_merge( array( 'type' ), parent::get_allowed_query_vars() );
116
+        return apply_filters( 'getpaid_rest_items_allowed_query_vars', $vars, $this );
117
+    }
118
+
119
+    /**
120
+     * Determine the allowed query_vars for a get_items() response and
121
+     * prepare for WP_Query.
122
+     *
123
+     * @param array           $prepared_args Prepared arguments.
124
+     * @param WP_REST_Request $request Request object.
125
+     * @return array          $query_args
126
+     */
127
+    protected function prepare_items_query( $prepared_args = array(), $request = null ) {
128
+
129
+        $query_args = parent::prepare_items_query( $prepared_args );
130
+
131
+        // Retrieve items by type.
132
+        if (  isset( $query_args['type'] ) && 'any' != $query_args['type'] ) {
133
+
134
+            if ( empty( $query_args['meta_query'] ) ) {
135
+                $query_args['meta_query'] = array();
136
+            }
137
+
138
+            $types = wpinv_parse_list( $query_args['type'] );
139
+            $query_args['meta_query'][] = array(
140
+                'key'     => '_wpinv_type',
141
+                'value'   => implode( ',', $types ),
142
+                'compare' => 'IN',
143
+            );
144
+            unset( $query_args['type'] );
145
+
146
+        }
147
+
148
+        return apply_filters( 'getpaid_rest_items_prepare_items_query', $query_args, $request, $this );
149
+
150
+    }
151
+
152
+    /**
153
+     * Retrieves a valid list of post statuses.
154
+     *
155
+     * @since 1.0.15
156
+     *
157
+     * @return array A list of registered item statuses.
158
+     */
159
+    public function get_post_statuses() {
160
+        return array( 'draft', 'pending', 'publish' );
161
+    }
162 162
 
163 163
 }
Please login to merge, or discard this patch.
includes/data/item-schema.php 1 patch
Indentation   +235 added lines, -235 removed lines patch added patch discarded remove patch
@@ -13,240 +13,240 @@
 block discarded – undo
13 13
 
14 14
 return array(
15 15
 
16
-	'id'              => array(
17
-		'description' => __( 'Unique identifier for the item.', 'invoicing' ),
18
-		'type'        => 'integer',
19
-		'context'     => array( 'view', 'edit', 'embed' ),
20
-		'readonly'    => true,
21
-	),
22
-
23
-	'parent_id'       => array(
24
-		'description' => __( 'Parent item ID.', 'invoicing' ),
25
-		'type'        => 'integer',
26
-		'context'     => array( 'view', 'edit', 'embed' ),
27
-		'default'     => 0,
28
-	),
29
-
30
-	'status'          => array(
31
-		'description' => __( 'A named status for the item.', 'invoicing' ),
32
-		'type'        => 'string',
33
-		'enum'        => array( 'draft', 'pending', 'publish' ),
34
-		'context'     => array( 'view', 'edit', 'embed' ),
35
-		'default'     => 'draft',
36
-	),
37
-
38
-	'version'         => array(
39
-		'description' => __( 'Plugin version when the item was created.', 'invoicing' ),
40
-		'type'        => 'string',
41
-		'context'     => array( 'view', 'edit' ),
42
-		'readonly'    => true,
43
-	),
44
-
45
-	'date_created'    => array(
46
-		'description' => __( "The date the item was created, in the site's timezone.", 'invoicing' ),
47
-		'type'        => 'string',
48
-		'context'     => array( 'view', 'edit', 'embed' ),
49
-	),
50
-
51
-	'date_created_gmt'    => array(
52
-		'description' => __( 'The GMT date the item was created.', 'invoicing' ),
53
-		'type'        => 'string',
54
-		'context'     => array( 'view', 'edit', 'embed' ),
55
-		'readonly'    => true,
56
-	),
57
-
58
-	'date_modified'   => array(
59
-		'description' => __( "The date the item was last modified, in the site's timezone.", 'invoicing' ),
60
-		'type'        => 'string',
61
-		'context'     => array( 'view', 'edit', 'embed' ),
62
-		'readonly'    => true,
63
-	),
64
-
65
-	'date_modified_gmt'    => array(
66
-		'description' => __( 'The GMT date the item was last modified.', 'invoicing' ),
67
-		'type'        => 'string',
68
-		'context'     => array( 'view', 'edit', 'embed' ),
69
-		'readonly'    => true,
70
-	),
71
-
72
-	'name'			  => array(
73
-		'description' => __( "The item's name.", 'invoicing' ),
74
-		'type'        => 'string',
75
-		'context'     => array( 'view', 'edit', 'embed' ),
76
-		'required'    => true,
77
-	),
78
-
79
-	'description'     => array(
80
-		'description' => __( "The item's description.", 'invoicing' ),
81
-		'type'        => 'string',
82
-		'context'     => array( 'view', 'edit', 'embed' ),
83
-	),
84
-
85
-	'owner'           => array(
86
-		'description' => __( 'The owner of the item (user id).', 'invoicing' ),
87
-		'type'        => 'integer',
88
-		'context'     => array( 'view', 'edit', 'embed' ),
89
-	),
90
-
91
-	'price'           => array(
92
-		'description' => __( 'The price of the item.', 'invoicing' ),
93
-		'type'        => 'number',
94
-		'context'     => array( 'view', 'edit', 'embed' ),
95
-		'required'    => true,
96
-	),
97
-
98
-	'the_price'       => array(
99
-		'description' => __( 'The formatted price of the item.', 'invoicing' ),
100
-		'type'        => 'string',
101
-		'context'     => array( 'view', 'edit', 'embed' ),
102
-		'readonly'    => true,
103
-	),
104
-
105
-	'type'       => array(
106
-		'description' => __( 'The item type.', 'invoicing' ),
107
-		'type'        => 'string',
108
-		'enum'        => wpinv_item_types(),
109
-		'default'     => 'custom',
110
-		'context'     => array( 'view', 'edit', 'embed' ),
111
-	),
112
-
113
-	'vat_rule'       => array(
114
-		'description' => __( 'VAT rule applied to the item.', 'invoicing' ),
115
-		'type'        => 'string',
116
-		'enum'        => array_keys( getpaid_tax()->get_rules() ),
117
-		'context'     => array( 'view', 'edit', 'embed' ),
118
-	),
119
-
120
-	'vat_class'       => array(
121
-		'description' => __( 'VAT class for the item.', 'invoicing' ),
122
-		'type'        => 'string',
123
-		'context'     => array( 'view', 'edit', 'embed' ),
124
-		'enum'        => array_keys( getpaid_tax()->get_vat_groups() ),
125
-	),
126
-
127
-	'custom_id'       => array(
128
-		'description' => __( 'Custom id for the item.', 'invoicing' ),
129
-		'type'        => 'string',
130
-		'context'     => array( 'view', 'edit', 'embed' ),
131
-	),
16
+    'id'              => array(
17
+        'description' => __( 'Unique identifier for the item.', 'invoicing' ),
18
+        'type'        => 'integer',
19
+        'context'     => array( 'view', 'edit', 'embed' ),
20
+        'readonly'    => true,
21
+    ),
22
+
23
+    'parent_id'       => array(
24
+        'description' => __( 'Parent item ID.', 'invoicing' ),
25
+        'type'        => 'integer',
26
+        'context'     => array( 'view', 'edit', 'embed' ),
27
+        'default'     => 0,
28
+    ),
29
+
30
+    'status'          => array(
31
+        'description' => __( 'A named status for the item.', 'invoicing' ),
32
+        'type'        => 'string',
33
+        'enum'        => array( 'draft', 'pending', 'publish' ),
34
+        'context'     => array( 'view', 'edit', 'embed' ),
35
+        'default'     => 'draft',
36
+    ),
37
+
38
+    'version'         => array(
39
+        'description' => __( 'Plugin version when the item was created.', 'invoicing' ),
40
+        'type'        => 'string',
41
+        'context'     => array( 'view', 'edit' ),
42
+        'readonly'    => true,
43
+    ),
44
+
45
+    'date_created'    => array(
46
+        'description' => __( "The date the item was created, in the site's timezone.", 'invoicing' ),
47
+        'type'        => 'string',
48
+        'context'     => array( 'view', 'edit', 'embed' ),
49
+    ),
50
+
51
+    'date_created_gmt'    => array(
52
+        'description' => __( 'The GMT date the item was created.', 'invoicing' ),
53
+        'type'        => 'string',
54
+        'context'     => array( 'view', 'edit', 'embed' ),
55
+        'readonly'    => true,
56
+    ),
57
+
58
+    'date_modified'   => array(
59
+        'description' => __( "The date the item was last modified, in the site's timezone.", 'invoicing' ),
60
+        'type'        => 'string',
61
+        'context'     => array( 'view', 'edit', 'embed' ),
62
+        'readonly'    => true,
63
+    ),
64
+
65
+    'date_modified_gmt'    => array(
66
+        'description' => __( 'The GMT date the item was last modified.', 'invoicing' ),
67
+        'type'        => 'string',
68
+        'context'     => array( 'view', 'edit', 'embed' ),
69
+        'readonly'    => true,
70
+    ),
71
+
72
+    'name'			  => array(
73
+        'description' => __( "The item's name.", 'invoicing' ),
74
+        'type'        => 'string',
75
+        'context'     => array( 'view', 'edit', 'embed' ),
76
+        'required'    => true,
77
+    ),
78
+
79
+    'description'     => array(
80
+        'description' => __( "The item's description.", 'invoicing' ),
81
+        'type'        => 'string',
82
+        'context'     => array( 'view', 'edit', 'embed' ),
83
+    ),
84
+
85
+    'owner'           => array(
86
+        'description' => __( 'The owner of the item (user id).', 'invoicing' ),
87
+        'type'        => 'integer',
88
+        'context'     => array( 'view', 'edit', 'embed' ),
89
+    ),
90
+
91
+    'price'           => array(
92
+        'description' => __( 'The price of the item.', 'invoicing' ),
93
+        'type'        => 'number',
94
+        'context'     => array( 'view', 'edit', 'embed' ),
95
+        'required'    => true,
96
+    ),
97
+
98
+    'the_price'       => array(
99
+        'description' => __( 'The formatted price of the item.', 'invoicing' ),
100
+        'type'        => 'string',
101
+        'context'     => array( 'view', 'edit', 'embed' ),
102
+        'readonly'    => true,
103
+    ),
104
+
105
+    'type'       => array(
106
+        'description' => __( 'The item type.', 'invoicing' ),
107
+        'type'        => 'string',
108
+        'enum'        => wpinv_item_types(),
109
+        'default'     => 'custom',
110
+        'context'     => array( 'view', 'edit', 'embed' ),
111
+    ),
112
+
113
+    'vat_rule'       => array(
114
+        'description' => __( 'VAT rule applied to the item.', 'invoicing' ),
115
+        'type'        => 'string',
116
+        'enum'        => array_keys( getpaid_tax()->get_rules() ),
117
+        'context'     => array( 'view', 'edit', 'embed' ),
118
+    ),
119
+
120
+    'vat_class'       => array(
121
+        'description' => __( 'VAT class for the item.', 'invoicing' ),
122
+        'type'        => 'string',
123
+        'context'     => array( 'view', 'edit', 'embed' ),
124
+        'enum'        => array_keys( getpaid_tax()->get_vat_groups() ),
125
+    ),
126
+
127
+    'custom_id'       => array(
128
+        'description' => __( 'Custom id for the item.', 'invoicing' ),
129
+        'type'        => 'string',
130
+        'context'     => array( 'view', 'edit', 'embed' ),
131
+    ),
132 132
 	
133
-	'custom_name'       => array(
134
-		'description' => __( 'Custom name for the item.', 'invoicing' ),
135
-		'type'        => 'string',
136
-		'context'     => array( 'view', 'edit', 'embed' ),
137
-	),
138
-
139
-	'custom_singular_name'       => array(
140
-		'description' => __( 'Custom singular name for the item.', 'invoicing' ),
141
-		'type'        => 'string',
142
-		'context'     => array( 'view', 'edit', 'embed' ),
143
-	),
144
-
145
-	'is_editable'     => array(
146
-		'description' => __( 'Whether or not the item is editable.', 'invoicing' ),
147
-		'type'        => 'integer',
148
-		'enum'        => array( 0, 1 ),
149
-		'context'     => array( 'view', 'edit', 'embed' ),
150
-	),
151
-
152
-	'is_dynamic_pricing'     => array(
153
-		'description' => __( 'Whether or not customers can enter their own prices when checking out.', 'invoicing' ),
154
-		'type'        => 'integer',
155
-		'enum'        => array( 0, 1 ),
156
-		'context'     => array( 'view', 'edit', 'embed' ),
157
-	),
158
-
159
-	'minimum_price'   => array(
160
-		'description' => __( 'For dynamic prices, this is the minimum price that a user can set.', 'invoicing' ),
161
-		'type'        => 'number',
162
-		'context'     => array( 'view', 'edit', 'embed' ),
163
-	),
164
-
165
-	'is_recurring'        => array(
166
-		'description' => __( 'Whether or not this is a subscription item.', 'invoicing' ),
167
-		'type'        => 'integer',
168
-		'enum'        => array( 0, 1 ),
169
-		'context'     => array( 'view', 'edit', 'embed' ),
170
-	),
171
-
172
-	'initial_price'   => array(
173
-		'description' => __( 'The initial price of the item.', 'invoicing' ),
174
-		'type'        => 'number',
175
-		'context'     => array( 'view', 'edit', 'embed' ),
176
-		'readonly'    => true,
177
-	),
178
-
179
-	'the_initial_price'       => array(
180
-		'description' => __( 'The formatted initial price of the item.', 'invoicing' ),
181
-		'type'        => 'string',
182
-		'context'     => array( 'view', 'edit', 'embed' ),
183
-		'readonly'    => true,
184
-	),
185
-
186
-	'recurring_price' => array(
187
-		'description' => __( 'The recurring price of the item.', 'invoicing' ),
188
-		'type'        => 'number',
189
-		'context'     => array( 'view', 'edit', 'embed' ),
190
-		'readonly'    => true,
191
-	),
192
-
193
-	'the_recurring_price'       => array(
194
-		'description' => __( 'The formatted recurring price of the item.', 'invoicing' ),
195
-		'type'        => 'string',
196
-		'context'     => array( 'view', 'edit', 'embed' ),
197
-		'readonly'    => true,
198
-	),
199
-
200
-	'recurring_period'        => array(
201
-		'description' => __( 'The recurring period for a recurring item.', 'invoicing' ),
202
-		'type'        => 'string',
203
-		'context'     => array( 'view', 'edit', 'embed' ),
204
-		'enum'        => array( 'D', 'W', 'M', 'Y' ),
205
-	),
206
-
207
-	'recurring_interval'        => array(
208
-		'description' => __( 'The recurring interval for a subscription item.', 'invoicing' ),
209
-		'type'        => 'integer',
210
-		'context'     => array( 'view', 'edit', 'embed' ),
211
-	),
212
-
213
-	'recurring_limit' => array(
214
-		'description' => __( 'The maximum number of renewals for a subscription item.', 'invoicing' ),
215
-		'type'        => 'integer',
216
-		'context'     => array( 'view', 'edit', 'embed' ),
217
-	),
218
-
219
-	'is_free_trial'   => array(
220
-		'description' => __( 'Whether the item has a free trial period.', 'invoicing' ),
221
-		'type'        => 'integer',
222
-		'enum'        => array( 0, 1 ),
223
-		'context'     => array( 'view', 'edit', 'embed' ),
224
-	),
225
-
226
-	'trial_period'    => array(
227
-		'description' => __( 'The trial period.', 'invoicing' ),
228
-		'type'        => 'string',
229
-		'context'     => array( 'view', 'edit', 'embed' ),
230
-		'enum'        => array( 'D', 'W', 'M', 'Y' ),
231
-	),
232
-
233
-	'trial_interval'  => array(
234
-		'description' => __( 'The trial interval.', 'invoicing' ),
235
-		'type'        => 'integer',
236
-		'context'     => array( 'view', 'edit', 'embed' ),
237
-	),
238
-
239
-	'first_renewal_date'       => array(
240
-		'description' => __( 'The first renewal date in case the item was to be bought today.', 'invoicing' ),
241
-		'type'        => 'string',
242
-		'context'     => array( 'view', 'edit', 'embed' ),
243
-		'readonly'    => true,
244
-	),
245
-
246
-	'edit_url'        => array(
247
-		'description' => __( 'The URL to edit an item.', 'invoicing' ),
248
-		'type'        => 'string',
249
-		'context'     => array( 'view', 'edit', 'embed' ),
250
-		'readonly'    => true,
251
-	),
133
+    'custom_name'       => array(
134
+        'description' => __( 'Custom name for the item.', 'invoicing' ),
135
+        'type'        => 'string',
136
+        'context'     => array( 'view', 'edit', 'embed' ),
137
+    ),
138
+
139
+    'custom_singular_name'       => array(
140
+        'description' => __( 'Custom singular name for the item.', 'invoicing' ),
141
+        'type'        => 'string',
142
+        'context'     => array( 'view', 'edit', 'embed' ),
143
+    ),
144
+
145
+    'is_editable'     => array(
146
+        'description' => __( 'Whether or not the item is editable.', 'invoicing' ),
147
+        'type'        => 'integer',
148
+        'enum'        => array( 0, 1 ),
149
+        'context'     => array( 'view', 'edit', 'embed' ),
150
+    ),
151
+
152
+    'is_dynamic_pricing'     => array(
153
+        'description' => __( 'Whether or not customers can enter their own prices when checking out.', 'invoicing' ),
154
+        'type'        => 'integer',
155
+        'enum'        => array( 0, 1 ),
156
+        'context'     => array( 'view', 'edit', 'embed' ),
157
+    ),
158
+
159
+    'minimum_price'   => array(
160
+        'description' => __( 'For dynamic prices, this is the minimum price that a user can set.', 'invoicing' ),
161
+        'type'        => 'number',
162
+        'context'     => array( 'view', 'edit', 'embed' ),
163
+    ),
164
+
165
+    'is_recurring'        => array(
166
+        'description' => __( 'Whether or not this is a subscription item.', 'invoicing' ),
167
+        'type'        => 'integer',
168
+        'enum'        => array( 0, 1 ),
169
+        'context'     => array( 'view', 'edit', 'embed' ),
170
+    ),
171
+
172
+    'initial_price'   => array(
173
+        'description' => __( 'The initial price of the item.', 'invoicing' ),
174
+        'type'        => 'number',
175
+        'context'     => array( 'view', 'edit', 'embed' ),
176
+        'readonly'    => true,
177
+    ),
178
+
179
+    'the_initial_price'       => array(
180
+        'description' => __( 'The formatted initial price of the item.', 'invoicing' ),
181
+        'type'        => 'string',
182
+        'context'     => array( 'view', 'edit', 'embed' ),
183
+        'readonly'    => true,
184
+    ),
185
+
186
+    'recurring_price' => array(
187
+        'description' => __( 'The recurring price of the item.', 'invoicing' ),
188
+        'type'        => 'number',
189
+        'context'     => array( 'view', 'edit', 'embed' ),
190
+        'readonly'    => true,
191
+    ),
192
+
193
+    'the_recurring_price'       => array(
194
+        'description' => __( 'The formatted recurring price of the item.', 'invoicing' ),
195
+        'type'        => 'string',
196
+        'context'     => array( 'view', 'edit', 'embed' ),
197
+        'readonly'    => true,
198
+    ),
199
+
200
+    'recurring_period'        => array(
201
+        'description' => __( 'The recurring period for a recurring item.', 'invoicing' ),
202
+        'type'        => 'string',
203
+        'context'     => array( 'view', 'edit', 'embed' ),
204
+        'enum'        => array( 'D', 'W', 'M', 'Y' ),
205
+    ),
206
+
207
+    'recurring_interval'        => array(
208
+        'description' => __( 'The recurring interval for a subscription item.', 'invoicing' ),
209
+        'type'        => 'integer',
210
+        'context'     => array( 'view', 'edit', 'embed' ),
211
+    ),
212
+
213
+    'recurring_limit' => array(
214
+        'description' => __( 'The maximum number of renewals for a subscription item.', 'invoicing' ),
215
+        'type'        => 'integer',
216
+        'context'     => array( 'view', 'edit', 'embed' ),
217
+    ),
218
+
219
+    'is_free_trial'   => array(
220
+        'description' => __( 'Whether the item has a free trial period.', 'invoicing' ),
221
+        'type'        => 'integer',
222
+        'enum'        => array( 0, 1 ),
223
+        'context'     => array( 'view', 'edit', 'embed' ),
224
+    ),
225
+
226
+    'trial_period'    => array(
227
+        'description' => __( 'The trial period.', 'invoicing' ),
228
+        'type'        => 'string',
229
+        'context'     => array( 'view', 'edit', 'embed' ),
230
+        'enum'        => array( 'D', 'W', 'M', 'Y' ),
231
+    ),
232
+
233
+    'trial_interval'  => array(
234
+        'description' => __( 'The trial interval.', 'invoicing' ),
235
+        'type'        => 'integer',
236
+        'context'     => array( 'view', 'edit', 'embed' ),
237
+    ),
238
+
239
+    'first_renewal_date'       => array(
240
+        'description' => __( 'The first renewal date in case the item was to be bought today.', 'invoicing' ),
241
+        'type'        => 'string',
242
+        'context'     => array( 'view', 'edit', 'embed' ),
243
+        'readonly'    => true,
244
+    ),
245
+
246
+    'edit_url'        => array(
247
+        'description' => __( 'The URL to edit an item.', 'invoicing' ),
248
+        'type'        => 'string',
249
+        'context'     => array( 'view', 'edit', 'embed' ),
250
+        'readonly'    => true,
251
+    ),
252 252
 );
Please login to merge, or discard this patch.
includes/class-wpinv-api.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -57,26 +57,26 @@
 block discarded – undo
57 57
         add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
58 58
     }
59 59
 
60
-	/**
61
-	 * Registers routes
62
-	 *
60
+    /**
61
+     * Registers routes
62
+     *
63 63
      * @since 1.0.13
64
-	 */
65
-	public function register_rest_routes() {
64
+     */
65
+    public function register_rest_routes() {
66 66
 
67 67
         // Discounts.
68 68
         $this->discounts_controller->register_routes();
69 69
 
70 70
         /**
71
-		 * Fires when registering Invoicing REST routes.
72
-		 *
73
-		 *
74
-		 * @since 1.0.15
75
-		 *
76
-		 *
77
-		 * @param array           $invoice_data Invoice properties.
78
-		 * @param WP_REST_Request $request The request used.
79
-		 */
71
+         * Fires when registering Invoicing REST routes.
72
+         *
73
+         *
74
+         * @since 1.0.15
75
+         *
76
+         *
77
+         * @param array           $invoice_data Invoice properties.
78
+         * @param WP_REST_Request $request The request used.
79
+         */
80 80
         do_action( "wpinv_register_rest_routes", $this );
81 81
         
82 82
     }
Please login to merge, or discard this patch.