|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Payment form class |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class GetPaid_Payment_Form extends GetPaid_Data { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Which data store to load. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $data_store_name = 'payment_form'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* This is the name of this object type. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $object_type = 'payment_form'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Form Data array. This is the core form data exposed in APIs. |
|
28
|
|
|
* |
|
29
|
|
|
* @since 1.0.19 |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $data = array( |
|
33
|
|
|
'status' => 'draft', |
|
34
|
|
|
'version' => '', |
|
35
|
|
|
'date_created' => null, |
|
36
|
|
|
'date_modified' => null, |
|
37
|
|
|
'name' => '', |
|
38
|
|
|
'author' => 1, |
|
39
|
|
|
'elements' => null, |
|
40
|
|
|
'items' => null, |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Stores meta in cache for future reads. |
|
45
|
|
|
* |
|
46
|
|
|
* A group must be set to to enable caching. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $cache_group = 'getpaid_forms'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Stores a reference to the original WP_Post object |
|
54
|
|
|
* |
|
55
|
|
|
* @var WP_Post |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $post = null; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the item if ID is passed, otherwise the item is new and empty. |
|
61
|
|
|
* |
|
62
|
|
|
* @param int|object|WPInv_Item|WP_Post $item Item to read. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct( $item = 0 ) { |
|
65
|
|
|
parent::__construct( $item ); |
|
66
|
|
|
|
|
67
|
|
|
if ( is_numeric( $item ) && $item > 0 ) { |
|
68
|
|
|
$this->set_id( $item ); |
|
69
|
|
|
} elseif ( $item instanceof self ) { |
|
70
|
|
|
$this->set_id( $item->get_id() ); |
|
71
|
|
|
} elseif ( ! empty( $item->ID ) ) { |
|
72
|
|
|
$this->set_id( $item->ID ); |
|
73
|
|
|
} else { |
|
74
|
|
|
$this->set_object_read( true ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Load the datastore. |
|
78
|
|
|
$this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
79
|
|
|
|
|
80
|
|
|
if ( $this->get_id() > 0 ) { |
|
81
|
|
|
$this->post = get_post( $this->get_id() ); |
|
82
|
|
|
$this->ID = $this->get_id(); |
|
|
|
|
|
|
83
|
|
|
$this->data_store->read( $this ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/* |
|
89
|
|
|
|-------------------------------------------------------------------------- |
|
90
|
|
|
| CRUD methods |
|
91
|
|
|
|-------------------------------------------------------------------------- |
|
92
|
|
|
| |
|
93
|
|
|
| Methods which create, read, update and delete items from the database. |
|
94
|
|
|
| |
|
95
|
|
|
*/ |
|
96
|
|
|
|
|
97
|
|
|
/* |
|
98
|
|
|
|-------------------------------------------------------------------------- |
|
99
|
|
|
| Getters |
|
100
|
|
|
|-------------------------------------------------------------------------- |
|
101
|
|
|
*/ |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get form status. |
|
105
|
|
|
* |
|
106
|
|
|
* @since 1.0.19 |
|
107
|
|
|
* @param string $context View or edit context. |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function get_status( $context = 'view' ) { |
|
111
|
|
|
return $this->get_prop( 'status', $context ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get plugin version when the form was created. |
|
116
|
|
|
* |
|
117
|
|
|
* @since 1.0.19 |
|
118
|
|
|
* @param string $context View or edit context. |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function get_version( $context = 'view' ) { |
|
122
|
|
|
return $this->get_prop( 'version', $context ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get date when the form was created. |
|
127
|
|
|
* |
|
128
|
|
|
* @since 1.0.19 |
|
129
|
|
|
* @param string $context View or edit context. |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function get_date_created( $context = 'view' ) { |
|
133
|
|
|
return $this->get_prop( 'date_created', $context ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get GMT date when the form was created. |
|
138
|
|
|
* |
|
139
|
|
|
* @since 1.0.19 |
|
140
|
|
|
* @param string $context View or edit context. |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function get_date_created_gmt( $context = 'view' ) { |
|
144
|
|
|
$date = $this->get_date_created( $context ); |
|
145
|
|
|
|
|
146
|
|
|
if ( $date ) { |
|
147
|
|
|
$date = get_gmt_from_date( $date ); |
|
148
|
|
|
} |
|
149
|
|
|
return $date; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get date when the form was last modified. |
|
154
|
|
|
* |
|
155
|
|
|
* @since 1.0.19 |
|
156
|
|
|
* @param string $context View or edit context. |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
public function get_date_modified( $context = 'view' ) { |
|
160
|
|
|
return $this->get_prop( 'date_modified', $context ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get GMT date when the form was last modified. |
|
165
|
|
|
* |
|
166
|
|
|
* @since 1.0.19 |
|
167
|
|
|
* @param string $context View or edit context. |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function get_date_modified_gmt( $context = 'view' ) { |
|
171
|
|
|
$date = $this->get_date_modified( $context ); |
|
172
|
|
|
|
|
173
|
|
|
if ( $date ) { |
|
174
|
|
|
$date = get_gmt_from_date( $date ); |
|
175
|
|
|
} |
|
176
|
|
|
return $date; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get the form name. |
|
181
|
|
|
* |
|
182
|
|
|
* @since 1.0.19 |
|
183
|
|
|
* @param string $context View or edit context. |
|
184
|
|
|
* @return string |
|
185
|
|
|
*/ |
|
186
|
|
|
public function get_name( $context = 'view' ) { |
|
187
|
|
|
return $this->get_prop( 'name', $context ); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Alias of self::get_name(). |
|
192
|
|
|
* |
|
193
|
|
|
* @since 1.0.19 |
|
194
|
|
|
* @param string $context View or edit context. |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
public function get_title( $context = 'view' ) { |
|
198
|
|
|
return $this->get_name( $context ); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get the owner of the form. |
|
203
|
|
|
* |
|
204
|
|
|
* @since 1.0.19 |
|
205
|
|
|
* @param string $context View or edit context. |
|
206
|
|
|
* @return int |
|
207
|
|
|
*/ |
|
208
|
|
|
public function get_author( $context = 'view' ) { |
|
209
|
|
|
return (int) $this->get_prop( 'author', $context ); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Get the elements that make up the form. |
|
214
|
|
|
* |
|
215
|
|
|
* @since 1.0.19 |
|
216
|
|
|
* @param string $context View or edit context. |
|
217
|
|
|
* @return array |
|
218
|
|
|
*/ |
|
219
|
|
|
public function get_elements( $context = 'view' ) { |
|
220
|
|
|
$elements = $this->get_prop( 'elements', $context ); |
|
221
|
|
|
|
|
222
|
|
|
if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
223
|
|
|
return wpinv_get_data( 'sample-payment-form' ); |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
return $elements; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get the items sold via the form. |
|
230
|
|
|
* |
|
231
|
|
|
* @since 1.0.19 |
|
232
|
|
|
* @param string $context View or edit context. |
|
233
|
|
|
* @return GetPaid_Form_Item[] |
|
234
|
|
|
*/ |
|
235
|
|
|
public function get_items( $context = 'view' ) { |
|
236
|
|
|
$items = $this->get_prop( 'items', $context ); |
|
237
|
|
|
|
|
238
|
|
|
if ( empty( $items ) || ! is_array( $items ) ) { |
|
239
|
|
|
$items = wpinv_get_data( 'sample-payment-form-items' ); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
// Convert the items. |
|
243
|
|
|
$prepared = array(); |
|
244
|
|
|
|
|
245
|
|
|
foreach ( $items as $key => $value ) { |
|
246
|
|
|
|
|
247
|
|
|
// $item_id => $quantity |
|
248
|
|
|
if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
249
|
|
|
$item = new GetPaid_Form_Item( $key ); |
|
250
|
|
|
|
|
251
|
|
|
if ( $item->can_purchase() ) { |
|
252
|
|
|
$item->set_quantity( $value ); |
|
253
|
|
|
$prepared[] = $item; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
continue; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
260
|
|
|
|
|
261
|
|
|
$item = new GetPaid_Form_Item( $value['id'] ); |
|
262
|
|
|
|
|
263
|
|
|
if ( ! $item->can_purchase() ) { |
|
264
|
|
|
continue; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
// Cart items. |
|
268
|
|
|
if ( isset( $value['subtotal'] ) ) { |
|
269
|
|
|
$item->set_price( $value['subtotal'] ); |
|
270
|
|
|
$item->set_quantity( $value['quantity'] ); |
|
271
|
|
|
$prepared[] = $item; |
|
272
|
|
|
continue; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
// Payment form item. |
|
276
|
|
|
$item->set_quantity( $value['quantity'] ); |
|
277
|
|
|
$item->set_allow_quantities( $value['allow_quantities'] ); |
|
278
|
|
|
$item->set_is_required( $value['required'] ); |
|
279
|
|
|
$item->set_custom_description( $value['required'] ); |
|
280
|
|
|
$prepared[] = $item; |
|
281
|
|
|
continue; |
|
282
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $prepared; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/* |
|
290
|
|
|
|-------------------------------------------------------------------------- |
|
291
|
|
|
| Setters |
|
292
|
|
|
|-------------------------------------------------------------------------- |
|
293
|
|
|
| |
|
294
|
|
|
| Functions for setting order data. These should not update anything in the |
|
295
|
|
|
| database itself and should only change what is stored in the class |
|
296
|
|
|
| object. |
|
297
|
|
|
*/ |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Sets item status. |
|
301
|
|
|
* |
|
302
|
|
|
* @since 1.0.19 |
|
303
|
|
|
* @param string $status New status. |
|
304
|
|
|
* @return array details of change. |
|
305
|
|
|
*/ |
|
306
|
|
|
public function set_status( $status ) { |
|
307
|
|
|
$old_status = $this->get_status(); |
|
308
|
|
|
|
|
309
|
|
|
$this->set_prop( 'status', $status ); |
|
310
|
|
|
|
|
311
|
|
|
return array( |
|
312
|
|
|
'from' => $old_status, |
|
313
|
|
|
'to' => $status, |
|
314
|
|
|
); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Set plugin version when the item was created. |
|
319
|
|
|
* |
|
320
|
|
|
* @since 1.0.19 |
|
321
|
|
|
*/ |
|
322
|
|
|
public function set_version( $value ) { |
|
323
|
|
|
$this->set_prop( 'version', $value ); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Set date when the item was created. |
|
328
|
|
|
* |
|
329
|
|
|
* @since 1.0.19 |
|
330
|
|
|
* @param string $value Value to set. |
|
331
|
|
|
* @return bool Whether or not the date was set. |
|
332
|
|
|
*/ |
|
333
|
|
|
public function set_date_created( $value ) { |
|
334
|
|
|
$date = strtotime( $value ); |
|
335
|
|
|
|
|
336
|
|
|
if ( $date ) { |
|
337
|
|
|
$this->set_prop( 'date_created', date( 'Y-m-d H:i:s', $date ) ); |
|
338
|
|
|
return true; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
return false; |
|
342
|
|
|
|
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Set date when the item was last modified. |
|
347
|
|
|
* |
|
348
|
|
|
* @since 1.0.19 |
|
349
|
|
|
* @param string $value Value to set. |
|
350
|
|
|
* @return bool Whether or not the date was set. |
|
351
|
|
|
*/ |
|
352
|
|
|
public function set_date_modified( $value ) { |
|
353
|
|
|
$date = strtotime( $value ); |
|
354
|
|
|
|
|
355
|
|
|
if ( $date ) { |
|
356
|
|
|
$this->set_prop( 'date_modified', date( 'Y-m-d H:i:s', $date ) ); |
|
357
|
|
|
return true; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
return false; |
|
361
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Set the item name. |
|
366
|
|
|
* |
|
367
|
|
|
* @since 1.0.19 |
|
368
|
|
|
* @param string $value New name. |
|
369
|
|
|
*/ |
|
370
|
|
|
public function set_name( $value ) { |
|
371
|
|
|
$name = sanitize_text_field( $value ); |
|
372
|
|
|
$this->set_prop( 'name', $name ); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Alias of self::set_name(). |
|
377
|
|
|
* |
|
378
|
|
|
* @since 1.0.19 |
|
379
|
|
|
* @param string $value New name. |
|
380
|
|
|
*/ |
|
381
|
|
|
public function set_title( $value ) { |
|
382
|
|
|
$this->set_name( $value ); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Set the owner of the item. |
|
387
|
|
|
* |
|
388
|
|
|
* @since 1.0.19 |
|
389
|
|
|
* @param int $value New author. |
|
390
|
|
|
*/ |
|
391
|
|
|
public function set_author( $value ) { |
|
392
|
|
|
$this->set_prop( 'author', (int) $value ); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Set the form elements. |
|
397
|
|
|
* |
|
398
|
|
|
* @since 1.0.19 |
|
399
|
|
|
* @param array $elements Form elements. |
|
400
|
|
|
*/ |
|
401
|
|
|
public function set_elements( $value ) { |
|
402
|
|
|
$this->set_prop( 'elements', $value ); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* Set the form items. |
|
407
|
|
|
* |
|
408
|
|
|
* @since 1.0.19 |
|
409
|
|
|
* @param array $elements Form elements. |
|
410
|
|
|
*/ |
|
411
|
|
|
public function set_items( $value ) { |
|
412
|
|
|
$this->set_prop( 'items', $value ); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* Create an item. For backwards compatibilty. |
|
417
|
|
|
* |
|
418
|
|
|
* @deprecated |
|
419
|
|
|
* @return int item id |
|
420
|
|
|
*/ |
|
421
|
|
|
public function create( $data = array() ) { |
|
422
|
|
|
|
|
423
|
|
|
// Set the properties. |
|
424
|
|
|
if ( is_array( $data ) ) { |
|
425
|
|
|
$this->set_props( $data ); |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
// Save the item. |
|
429
|
|
|
return $this->save(); |
|
430
|
|
|
|
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
/** |
|
434
|
|
|
* Updates an item. For backwards compatibilty. |
|
435
|
|
|
* |
|
436
|
|
|
* @deprecated |
|
437
|
|
|
* @return int item id |
|
438
|
|
|
*/ |
|
439
|
|
|
public function update( $data = array() ) { |
|
440
|
|
|
return $this->create( $data ); |
|
|
|
|
|
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/* |
|
444
|
|
|
|-------------------------------------------------------------------------- |
|
445
|
|
|
| Conditionals |
|
446
|
|
|
|-------------------------------------------------------------------------- |
|
447
|
|
|
| |
|
448
|
|
|
| Checks if a condition is true or false. |
|
449
|
|
|
| |
|
450
|
|
|
*/ |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* Checks whether this is the default payment form. |
|
454
|
|
|
* |
|
455
|
|
|
* @since 1.0.19 |
|
456
|
|
|
* @return bool |
|
457
|
|
|
*/ |
|
458
|
|
|
public function is_default() { |
|
459
|
|
|
$is_default = $this->get_id() == wpinv_get_default_payment_form(); |
|
460
|
|
|
return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->ID, $this ); |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* Checks whether the form is active. |
|
465
|
|
|
* |
|
466
|
|
|
* @since 1.0.19 |
|
467
|
|
|
* @return bool |
|
468
|
|
|
*/ |
|
469
|
|
|
public function is_active() { |
|
470
|
|
|
$is_active = null !== $this->get_id(); |
|
471
|
|
|
|
|
472
|
|
|
if ( ! current_user_can( 'edit_post', $this->ID ) && $this->post_status != 'publish' ) { |
|
|
|
|
|
|
473
|
|
|
$is_active = false; |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this ); |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
} |
|
480
|
|
|
|