Pods – Custom Content Types and Fields - Version 2.7.24

Version Description

  • November 5th 2020 =

Bug Fixes

  • Fixed: Fix media [each] loop if no media Pod exists. #5882 (@JoryHogeveen)
  • Fixed: Fallback to default display field if custom option is invalid for relationship fields. #5839 & #5859 (@JoryHogeveen)
  • Fixed: Use pathinfo to properly validate file extension. #5876 (@JoryHogeveen)
Download this release

Release Info

Developer sc0ttkclark
Plugin Icon 128x128 Pods – Custom Content Types and Fields
Version 2.7.24
Comparing to
See all releases

Code changes from version 2.4.4.1 to 2.7.24

Files changed (4) hide show
  1. classes/Pods.php +2442 -1645
  2. classes/PodsAPI.php +8975 -7414
  3. classes/PodsAdmin.php +3740 -2250
  4. classes/PodsArray.php +102 -206
classes/Pods.php CHANGED
@@ -1,285 +1,393 @@
1
  <?php
 
2
  /**
 
 
3
  * @package Pods
4
  */
5
  class Pods implements Iterator {
6
 
7
  /**
 
 
8
  * @var bool
9
  */
10
  private $iterator = false;
11
 
12
  /**
 
 
13
  * @var PodsAPI
14
  */
15
  public $api;
16
 
17
  /**
 
 
18
  * @var PodsData
19
  */
20
  public $data;
21
 
22
  /**
 
 
23
  * @var PodsData
24
  */
25
  public $alt_data;
26
 
27
  /**
28
- * @var array Array of pod item arrays
 
 
29
  */
30
- public $rows = array();
31
 
32
  /**
33
- * @var array Current pod item array
 
 
34
  */
35
- public $row = array();
36
 
37
  /**
 
 
38
  * @var int
39
  */
40
- private $row_number = -1;
41
 
42
  /**
43
- * @var array Override pod item array
 
 
44
  */
45
  public $row_override = array();
46
 
47
  /**
 
 
48
  * @var bool
49
  */
50
- public $display_errors = false;
51
 
52
  /**
 
 
53
  * @var array|bool|mixed|null|void
54
  */
55
  public $pod_data;
56
 
57
  /**
 
 
58
  * @var array
59
  */
60
  public $params = array();
61
 
62
  /**
 
 
63
  * @var string
64
  */
65
- public $pod = '';
66
 
67
  /**
 
 
68
  * @var int
69
  */
70
- public $pod_id = 0;
71
 
72
  /**
 
 
73
  * @var array
74
  */
75
- public $fields = array();
76
 
77
  /**
 
 
78
  * @var array
79
  */
80
  public $filters = array();
81
 
82
  /**
 
 
83
  * @var string
84
  */
85
  public $detail_page;
86
 
87
  /**
 
 
88
  * @var int
89
  */
90
- public $id = 0;
91
 
92
  /**
 
 
93
  * @var int
94
  */
95
  public $limit = 15;
96
 
97
  /**
 
 
98
  * @var int
99
  */
100
  public $offset = 0;
101
 
102
  /**
 
 
103
  * @var string
104
  */
105
  public $page_var = 'pg';
106
 
107
  /**
 
 
108
  * @var int|mixed
109
  */
110
  public $page = 1;
111
 
112
  /**
 
 
113
  * @var bool
114
  */
115
  public $pagination = true;
116
 
117
  /**
 
 
118
  * @var bool
119
  */
120
  public $search = true;
121
 
122
  /**
 
 
123
  * @var string
124
  */
125
  public $search_var = 'search';
126
 
127
  /**
 
 
128
  * @var string
129
  */
130
- public $search_mode = 'int'; // int | text | text_like
131
 
132
  /**
 
 
133
  * @var int
134
  */
135
  public $total = 0;
136
 
137
  /**
 
 
138
  * @var int
139
  */
140
  public $total_found = 0;
141
 
142
  /**
 
 
143
  * @var array
144
  */
145
  public $ui = array();
146
 
147
  /**
148
- * @var mixed SEO related vars for Pod Pages
 
 
149
  */
150
  public $page_template;
 
 
 
 
 
 
151
  public $body_classes;
 
 
 
 
 
 
152
  public $meta = array();
 
 
 
 
 
 
153
  public $meta_properties = array();
 
 
 
 
 
 
154
  public $meta_extra = '';
155
 
156
  /**
157
- * @var string Last SQL query used by a find()
 
 
158
  */
159
  public $sql;
160
 
161
  /**
162
- * @var
 
 
163
  */
164
  public $deprecated;
165
 
 
 
 
 
 
 
 
166
  public $datatype;
167
 
 
 
 
 
 
 
 
168
  public $datatype_id;
169
 
170
  /**
171
- * Constructor - Pods Framework core
172
  *
173
- * @param string $pod The pod name
174
- * @param mixed $id (optional) The ID or slug, to load a single record; Provide array of $params to run 'find'
175
- *
176
- * @return \Pods
177
  *
178
  * @license http://www.gnu.org/licenses/gpl-2.0.html
179
- * @since 1.0.0
180
- * @link http://pods.io/docs/pods/
181
  */
182
- public function __construct ( $pod = null, $id = null ) {
 
183
  if ( null === $pod ) {
184
- $queried_object = get_queried_object();
 
185
 
186
- if ( $queried_object ) {
 
 
 
187
  $id_lookup = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
- // Post Type Singular
190
- if ( isset( $queried_object->post_type ) ) {
191
- $pod = $queried_object->post_type;
192
- }
193
- // Term Archive
194
- elseif ( isset( $queried_object->taxonomy ) ) {
195
- $pod = $queried_object->taxonomy;
196
- }
197
- // Author Archive
198
- elseif ( isset( $queried_object->user_login ) ) {
199
- $pod = 'user';
200
- }
201
- // Post Type Archive
202
- elseif ( isset( $queried_object->public ) && isset( $queried_object->name ) ) {
203
- $pod = $queried_object->name;
204
-
205
- $id_lookup = false;
206
- }
207
-
208
- if ( null === $id && $id_lookup ) {
209
- $id = get_queried_object_id();
210
- }
211
  }
212
- }
213
 
214
- $this->api = pods_api( $pod );
215
  $this->api->display_errors =& $this->display_errors;
216
 
217
- $this->data = pods_data( $this->api, $id, false );
218
  PodsData::$display_errors =& $this->display_errors;
219
 
220
- // Set up page variable
221
  if ( pods_strict( false ) ) {
222
- $this->page = 1;
223
  $this->pagination = false;
224
- $this->search = false;
225
- }
226
- else {
227
- // Get the page variable
228
- $this->page = pods_var( $this->page_var, 'get' );
229
- $this->page = ( empty( $this->page ) ? 1 : max( pods_absint( $this->page ), 1 ) );
 
 
230
  }
231
 
232
- // Set default pagination handling to on/off
233
  if ( defined( 'PODS_GLOBAL_POD_PAGINATION' ) ) {
234
- if ( !PODS_GLOBAL_POD_PAGINATION ) {
235
- $this->page = 1;
236
  $this->pagination = false;
237
- }
238
- else
239
  $this->pagination = true;
 
240
  }
241
 
242
- // Set default search to on/off
243
  if ( defined( 'PODS_GLOBAL_POD_SEARCH' ) ) {
244
- if ( PODS_GLOBAL_POD_SEARCH )
245
  $this->search = true;
246
- else
247
  $this->search = false;
 
248
  }
249
 
250
- // Set default search mode
251
  $allowed_search_modes = array( 'int', 'text', 'text_like' );
252
 
253
- if ( defined( 'PODS_GLOBAL_POD_SEARCH_MODE' ) && in_array( PODS_GLOBAL_POD_SEARCH_MODE, $allowed_search_modes ) )
254
  $this->search_mode = PODS_GLOBAL_POD_SEARCH_MODE;
 
255
 
256
- // Sync Settings
257
- $this->data->page =& $this->page;
258
- $this->data->limit =& $this->limit;
259
- $this->data->pagination =& $this->pagination;
260
- $this->data->search =& $this->search;
261
  $this->data->search_mode =& $this->search_mode;
262
 
263
- // Sync Pod Data
264
  $this->api->pod_data =& $this->data->pod_data;
265
- $this->pod_data =& $this->api->pod_data;
266
- $this->api->pod_id =& $this->data->pod_id;
267
- $this->pod_id =& $this->api->pod_id;
268
- $this->datatype_id =& $this->pod_id;
269
- $this->api->pod =& $this->data->pod;
270
- $this->pod =& $this->api->pod;
271
- $this->datatype =& $this->pod;
272
- $this->api->fields =& $this->data->fields;
273
- $this->fields =& $this->api->fields;
274
- $this->detail_page =& $this->data->detail_page;
275
- $this->id =& $this->data->id;
276
- $this->row =& $this->data->row;
277
- $this->rows =& $this->data->data;
278
- $this->row_number =& $this->data->row_number;
279
- $this->sql =& $this->data->sql;
280
-
281
- if ( is_array( $id ) || is_object( $id ) )
282
  $this->find( $id );
 
283
  }
284
 
285
  /**
@@ -287,14 +395,17 @@ class Pods implements Iterator {
287
  *
288
  * @return bool
289
  *
290
- * @since 2.0
291
  */
292
- public function valid () {
293
- if ( empty( $this->pod_id ) )
 
294
  return false;
 
295
 
296
- if ( $this->iterator )
297
  return isset( $this->rows[ $this->row_number ] );
 
298
 
299
  return true;
300
  }
@@ -306,9 +417,10 @@ class Pods implements Iterator {
306
  *
307
  * @since 2.3.4
308
  *
309
- * @link http://www.php.net/manual/en/class.iterator.php
310
  */
311
- public function is_iterator () {
 
312
  return $this->iterator;
313
  }
314
 
@@ -319,31 +431,28 @@ class Pods implements Iterator {
319
  *
320
  * @since 2.3.4
321
  *
322
- * @link http://www.php.net/manual/en/class.iterator.php
323
  */
324
- public function stop_iterator () {
 
325
  $this->iterator = false;
326
 
327
- return;
328
  }
329
 
330
  /**
331
  * Rewind Iterator
332
  *
333
- * @return void|boolean
334
- *
335
  * @since 2.3.4
336
  *
337
- * @link http://www.php.net/manual/en/class.iterator.php
338
  */
339
- public function rewind () {
340
- if ( $this->iterator ) {
341
- $this->row_number = 0;
342
 
343
- return;
344
- }
345
 
346
- return false;
 
347
  }
348
 
349
  /**
@@ -353,11 +462,13 @@ class Pods implements Iterator {
353
  *
354
  * @since 2.3.4
355
  *
356
- * @link http://www.php.net/manual/en/class.iterator.php
357
  */
358
- public function current () {
359
- if ( $this->iterator && $this->fetch() )
 
360
  return $this;
 
361
 
362
  return false;
363
  }
@@ -365,36 +476,29 @@ class Pods implements Iterator {
365
  /**
366
  * Get current Iterator key
367
  *
368
- * @return int|boolean
369
  *
370
  * @since 2.3.4
371
  *
372
- * @link http://www.php.net/manual/en/class.iterator.php
373
  */
374
- public function key () {
375
- if ( $this->iterator )
376
- return $this->row_number;
377
 
378
- return false;
379
  }
380
 
381
  /**
382
  * Move onto the next Iterator row
383
  *
384
- * @return void|boolean
385
  *
386
  * @since 2.3.4
387
  *
388
- * @link http://www.php.net/manual/en/class.iterator.php
389
  */
390
- public function next () {
391
- if ( $this->iterator ) {
392
- $this->row_number++;
393
-
394
- return;
395
- }
396
 
397
- return false;
398
  }
399
 
400
  /**
@@ -402,11 +506,13 @@ class Pods implements Iterator {
402
  *
403
  * @return bool
404
  *
405
- * @since 2.0
406
  */
407
- public function exists () {
408
- if ( empty( $this->row ) )
 
409
  return false;
 
410
 
411
  return true;
412
  }
@@ -419,14 +525,16 @@ class Pods implements Iterator {
419
  *
420
  * @return array|bool An array of all rows returned from a find() call, or false if no items returned
421
  *
422
- * @since 2.0
423
- * @link http://pods.io/docs/data/
424
  */
425
- public function data () {
426
- $this->do_hook( 'data' );
 
427
 
428
- if ( empty( $this->rows ) )
429
  return false;
 
430
 
431
  return (array) $this->rows;
432
  }
@@ -434,9 +542,9 @@ class Pods implements Iterator {
434
  /**
435
  * Return a field input for a specific field
436
  *
437
- * @param string|array $field Field name or Field data array
438
- * @param string $field Input field name to use (overrides default name)
439
- * @param mixed $value Current value to use
440
  *
441
  * @return string Field Input HTML
442
  *
@@ -444,25 +552,28 @@ class Pods implements Iterator {
444
  */
445
  public function input( $field, $input_name = null, $value = '__null' ) {
446
 
447
- // Field data override
448
  if ( is_array( $field ) ) {
 
449
  $field_data = $field;
450
- $field = pods_var_raw( 'name', $field );
451
- }
452
- // Get field data from field name
453
- else {
454
  $field_data = $this->fields( $field );
455
  }
456
 
457
- if ( !empty( $field_data ) ) {
458
- $field_type = pods_var_raw( 'type', $field_data );
459
 
460
  if ( empty( $input_name ) ) {
461
  $input_name = $field;
462
  }
463
 
464
- if ( '__null' == $value ) {
465
- $value = $this->field( array( 'name' => $field, 'in_form' => true ) );
 
 
 
466
  }
467
 
468
  return PodsForm::field( $input_name, $value, $field_type, $field_data, $this, $this->id() );
@@ -475,57 +586,80 @@ class Pods implements Iterator {
475
  /**
476
  * Return field array from a Pod, a field's data, or a field option
477
  *
478
- * @param null $field
479
- * @param null $option
480
  *
481
  * @return bool|mixed
482
  *
483
- * @since 2.0
484
  */
485
- public function fields ( $field = null, $option = null ) {
486
- // No fields found
487
- if ( empty( $this->fields ) )
 
 
 
488
  $field_data = array();
489
- // Return all fields
490
- elseif ( empty( $field ) )
491
  $field_data = (array) $this->fields;
492
- // Field not found
493
- elseif ( !isset( $this->fields[ $field ] ) )
494
  $field_data = array();
495
- // Return all field data
496
- elseif ( empty( $option ) )
497
- $field_data = $this->fields[ $field ];
498
- else {
499
- // Merge options
500
- $options = array_merge( $this->fields[ $field ], $this->fields[ $field ][ 'options' ] );
 
 
 
501
 
502
- $field_data = null;
 
 
 
 
 
503
 
504
- // Get a list of available items from a relationship field
505
- if ( 'data' == $option && in_array( pods_var_raw( 'type', $options ), PodsForm::tableless_field_types() ) ) {
506
  $field_data = PodsForm::field_method( 'pick', 'get_field_data', $options );
507
- }
508
- // Return option
509
- elseif ( isset( $options[ $option ] ) ) {
510
  $field_data = $options[ $option ];
511
  }
512
- }
 
 
 
 
 
 
 
 
 
 
 
 
513
 
514
- return $this->do_hook( 'fields', $field_data, $field, $option );
515
  }
516
 
517
  /**
518
  * Return row array for an item
519
  *
520
- * @return array
521
  *
522
- * @since 2.0
523
  */
524
- public function row () {
525
- $this->do_hook( 'row' );
 
526
 
527
- if ( !is_array( $this->row ) )
528
  return false;
 
529
 
530
  return (array) $this->row;
531
  }
@@ -535,44 +669,54 @@ class Pods implements Iterator {
535
  * you will want to use field() instead. This function will automatically convert arrays into a
536
  * list of text such as "Rick, John, and Gary"
537
  *
538
- * @param string|array $name The field name, or an associative array of parameters
539
- * @param boolean $single (optional) For tableless fields, to return an array or the first
540
  *
541
- * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned for tableless fields
542
- * @since 2.0
543
- * @link http://pods.io/docs/display/
 
544
  */
545
- public function display ( $name, $single = null ) {
 
546
  $defaults = array(
547
- 'name' => $name,
548
- 'single' => $single,
549
- 'display' => true,
550
- 'serial_params' => null
551
  );
552
 
553
  if ( is_array( $name ) || is_object( $name ) ) {
554
- $defaults[ 'name' ] = null;
 
555
  $params = (object) array_merge( $defaults, (array) $name );
556
- }
557
- elseif ( is_array( $single ) || is_object( $single ) ) {
558
- $defaults[ 'single' ] = null;
559
  $params = (object) array_merge( $defaults, (array) $single );
560
- }
561
- else
562
  $params = $defaults;
 
563
 
564
  $params = (object) $params;
565
 
566
  $value = $this->field( $params );
567
 
568
  if ( is_array( $value ) ) {
 
 
 
 
 
 
569
  $serial_params = array(
570
- 'field' => $params->name,
571
- 'fields' => $this->fields
572
  );
573
 
574
- if ( !empty( $params->serial_params ) && is_array( $params->serial_params ) )
575
  $serial_params = array_merge( $serial_params, $params->serial_params );
 
576
 
577
  $value = pods_serial_comma( $value, $serial_params );
578
  }
@@ -585,34 +729,35 @@ class Pods implements Iterator {
585
  * you will want to use field() instead. This function will automatically convert arrays into a
586
  * list of text such as "Rick, John, and Gary"
587
  *
588
- * @param string|array $name The field name, or an associative array of parameters
589
- * @param boolean $single (optional) For tableless fields, to return an array or the first
590
  *
591
- * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned for tableless fields
592
- * @since 2.0
593
- * @link http://pods.io/docs/display/
 
594
  */
595
- public function raw ( $name, $single = null ) {
 
596
  $defaults = array(
597
- 'name' => $name,
598
  'single' => $single,
599
- 'raw' => true
600
  );
601
 
602
  if ( is_array( $name ) || is_object( $name ) ) {
603
- $defaults[ 'name' ] = null;
 
604
  $params = (object) array_merge( $defaults, (array) $name );
605
- }
606
- elseif ( is_array( $single ) || is_object( $single ) ) {
607
- $defaults[ 'single' ] = null;
608
  $params = (object) array_merge( $defaults, (array) $single );
609
- }
610
- else
611
  $params = (object) $defaults;
 
612
 
613
- $value = $this->field( $params );
614
-
615
- return $value;
616
  }
617
 
618
  /**
@@ -622,957 +767,1206 @@ class Pods implements Iterator {
622
  *
623
  * This function will return arrays for relationship and file fields.
624
  *
625
- * @param string|array $name The field name, or an associative array of parameters
626
- * @param boolean $single (optional) For tableless fields, to return the whole array or the just the first item, or an associative array of parameters
627
- * @param boolean $raw (optional) Whether to return the raw value, or to run through the field type's display method, or an associative array of parameters
 
 
628
  *
629
- * @return mixed|null Value returned depends on the field type, null if the field doesn't exist, false if no value returned for tableless fields
630
- * @since 2.0
631
- * @link http://pods.io/docs/field/
 
632
  */
633
- public function field ( $name, $single = null, $raw = false ) {
634
- global $sitepress;
635
 
636
  $defaults = array(
637
- 'name' => $name,
638
- 'orderby' => null,
639
- 'single' => $single,
640
- 'params' => null,
641
- 'in_form' => false,
642
- 'raw' => $raw,
643
  'raw_display' => false,
644
- 'display' => false,
645
- 'get_meta' => false,
646
- 'output' => null,
647
- 'deprecated' => false,
648
- 'args' => array() // extra data to send to field handlers
 
 
649
  );
650
 
651
- if ( is_array( $name ) || is_object( $name ) ) {
652
- $defaults[ 'name' ] = null;
653
- $params = (object) array_merge( $defaults, (array) $name );
654
  }
655
- elseif ( is_array( $single ) || is_object( $single ) ) {
656
- $defaults[ 'single' ] = null;
657
- $params = (object) array_merge( $defaults, (array) $single );
658
  }
659
- elseif ( is_array( $raw ) || is_object( $raw ) ) {
660
- $defaults[ 'raw' ] = false;
661
- $params = (object) array_merge( $defaults, (array) $raw );
662
  }
663
- else
 
 
 
 
 
 
 
 
 
 
 
 
 
664
  $params = (object) $defaults;
 
665
 
666
  if ( $params->in_form ) {
667
  $params->output = 'ids';
668
- }
669
- elseif ( null === $params->output ) {
670
- $params->output = $this->do_hook( 'field_related_output_type', 'arrays', $this->row, $params );
 
 
 
 
 
 
 
671
  }
672
 
673
- if ( in_array( $params->output, array( 'id', 'name', 'object', 'array', 'pod' ) ) )
674
  $params->output .= 's';
 
675
 
676
- // Support old $orderby variable
677
  if ( null !== $params->single && is_string( $params->single ) && empty( $params->orderby ) ) {
678
- pods_deprecated( 'Pods::field', '2.0', 'Use $params[ \'orderby\' ] instead' );
 
 
679
 
680
  $params->orderby = $params->single;
681
- $params->single = false;
682
  }
683
 
684
- if ( null !== $params->single )
685
  $params->single = (boolean) $params->single;
 
686
 
687
- if ( is_array( $params->name ) || strlen( trim( $params->name ) ) < 1 )
 
688
  return null;
 
689
 
690
- $params->name = trim( $params->name );
691
  $params->full_name = $params->name;
692
 
693
  $value = null;
694
 
695
- if ( isset( $this->row_override[ $params->name ] ) )
696
  $value = $this->row_override[ $params->name ];
 
697
 
698
  if ( false === $this->row() ) {
699
- if ( false !== $this->data() )
700
  $this->fetch();
701
- else
702
  return $value;
 
703
  }
704
 
705
- if ( $this->data->field_id == $params->name ) {
706
- if ( isset( $this->row[ $params->name ] ) )
707
  return $this->row[ $params->name ];
708
- elseif ( null !== $value )
 
709
  return $value;
 
710
 
711
  return 0;
712
  }
713
 
714
- $tableless_field_types = PodsForm::tableless_field_types();
715
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
716
 
717
  $params->traverse = array();
718
 
719
- if ( in_array( $params->name, array( '_link', 'detail_url' ) ) || ( in_array( $params->name, array( 'permalink', 'the_permalink' ) ) && in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) ) {
720
- if ( 0 < strlen( $this->detail_page ) )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
721
  $value = get_home_url() . '/' . $this->do_magic_tags( $this->detail_page );
722
- elseif ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) )
723
- $value = get_permalink( $this->id() );
724
- elseif ( 'taxonomy' == $this->pod_data[ 'type' ] )
725
- $value = get_term_link( $this->id(), $this->pod_data[ 'name' ] );
726
- elseif ( 'user' == $this->pod_data[ 'type' ] )
727
- $value = get_author_posts_url( $this->id() );
728
- elseif ( 'comment' == $this->pod_data[ 'type' ] )
729
- $value = get_comment_link( $this->id() );
730
- }
731
-
732
- $field_data = false;
733
- $field_type = false;
734
-
735
- $first_field = explode( '.', $params->name );
736
- $first_field = $first_field[ 0 ];
737
-
738
- if ( isset( $this->fields[ $first_field ] ) ) {
739
- $field_data = $this->fields[ $first_field ];
740
- $field_type = 'field';
741
- }
742
- elseif ( isset( $this->pod_data[ 'object_fields' ] ) && !empty( $this->pod_data[ 'object_fields' ] ) ) {
743
- if ( isset( $this->pod_data[ 'object_fields' ][ $first_field ] ) ) {
744
- $field_data = $this->pod_data[ 'object_fields' ][ $first_field ];
745
- $field_type = 'object_field';
746
- }
747
- else {
748
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
749
- if ( in_array( $first_field, $object_field_opt[ 'alias' ] ) ) {
750
- if ( $first_field == $params->name )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
751
  $params->name = $object_field;
 
752
 
753
- $first_field = $object_field;
754
- $field_data = $object_field_opt;
755
- $field_type = 'object_field';
 
756
 
757
  break;
758
  }
759
  }
760
  }
761
- }
762
 
763
- // Simple fields have no other output options
764
- if ( 'pick' == $field_data[ 'type' ] && in_array( $field_data[ 'pick_object' ], $simple_tableless_objects ) ) {
 
 
 
 
 
765
  $params->output = 'arrays';
766
  }
767
 
768
- if ( empty( $value ) && in_array( $field_data[ 'type' ], $tableless_field_types ) ) {
 
769
  $params->raw = true;
770
 
771
  $value = false;
772
 
773
- if ( 'arrays' != $params->output && isset( $this->row[ '_' . $params->output . '_' . $params->name ] ) ) {
774
- $value = $this->row[ '_' . $params->output . '_' . $params->name ];
775
- }
776
- elseif ( 'arrays' == $params->output && isset( $this->row[ $params->name ] ) ) {
 
777
  $value = $this->row[ $params->name ];
778
  }
779
 
780
- if ( false !== $value && !is_array( $value ) && 'pick' == $field_data[ 'type' ] && in_array( $field_data[ 'pick_object' ], $simple_tableless_objects ) )
 
 
 
 
 
781
  $value = PodsForm::field_method( 'pick', 'simple_value', $params->name, $value, $field_data, $this->pod_data, $this->id(), true );
 
782
  }
783
 
784
- if ( empty( $value ) && isset( $this->row[ $params->name ] ) && ( !in_array( $field_data[ 'type' ], $tableless_field_types ) || 'arrays' == $params->output ) ) {
785
- if ( empty( $field_data ) || in_array( $field_data[ 'type' ], array( 'boolean', 'number', 'currency' ) ) )
 
 
 
 
786
  $params->raw = true;
 
 
 
 
 
 
 
 
 
787
 
788
  $value = $this->row[ $params->name ];
789
- }
790
- elseif ( empty( $value ) ) {
791
  $object_field_found = false;
792
 
793
- if ( 'object_field' == $field_type ) {
794
  $object_field_found = true;
795
 
796
- if ( isset( $this->row[ $first_field ] ) )
797
  $value = $this->row[ $first_field ];
798
- elseif ( in_array( $field_data[ 'type' ], $tableless_field_types ) ) {
 
799
  $this->fields[ $first_field ] = $field_data;
800
 
801
  $object_field_found = false;
802
- }
803
- else
804
  return null;
805
- }
806
-
807
- if ( 'post_type' == $this->pod_data[ 'type' ] && !isset( $this->fields[ $params->name ] ) ) {
808
- if ( !isset( $this->fields[ 'post_thumbnail' ] ) && ( 'post_thumbnail' == $params->name || 0 === strpos( $params->name, 'post_thumbnail.' ) ) ) {
809
- $size = 'thumbnail';
810
-
811
- if ( 0 === strpos( $params->name, 'post_thumbnail.' ) ) {
812
- $field_names = explode( '.', $params->name );
813
-
814
- if ( isset( $field_names[ 1 ] ) )
815
- $size = $field_names[ 1 ];
816
- }
817
-
818
- // Pods will auto-get the thumbnail ID if this isn't an attachment
819
- $value = pods_image( $this->id(), $size, 0, null, true );
820
-
821
- $object_field_found = true;
822
  }
823
- elseif ( !isset( $this->fields[ 'post_thumbnail_url' ] ) && ( 'post_thumbnail_url' == $params->name || 0 === strpos( $params->name, 'post_thumbnail_url.' ) ) ) {
824
- $size = 'thumbnail';
825
-
826
- if ( 0 === strpos( $params->name, 'post_thumbnail_url.' ) ) {
827
- $field_names = explode( '.', $params->name );
828
 
829
- if ( isset( $field_names[ 1 ] ) )
830
- $size = $field_names[ 1 ];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
831
  }
832
-
833
- // Pods will auto-get the thumbnail ID if this isn't an attachment
834
- $value = pods_image_url( $this->id(), $size, 0, true );
835
-
836
- $object_field_found = true;
837
  }
838
- elseif ( 0 === strpos( $params->name, 'image_attachment.' ) ) {
839
- $size = 'thumbnail';
840
 
841
- $image_id = 0;
842
-
843
- $field_names = explode( '.', $params->name );
844
-
845
- if ( isset( $field_names[ 1 ] ) )
846
- $image_id = $field_names[ 1 ];
847
-
848
- if ( isset( $field_names[ 2 ] ) )
849
- $size = $field_names[ 2 ];
850
-
851
- if ( !empty( $image_id ) ) {
852
- $value = pods_image( $image_id, $size, 0, null, true );
853
-
854
- if ( !empty( $value ) )
855
- $object_field_found = true;
856
  }
857
  }
858
- elseif ( 0 === strpos( $params->name, 'image_attachment_url.' ) ) {
859
- $size = 'thumbnail';
860
 
861
- $image_id = 0;
862
 
863
- $field_names = explode( '.', $params->name );
864
-
865
- if ( isset( $field_names[ 1 ] ) )
866
- $image_id = $field_names[ 1 ];
867
-
868
- if ( isset( $field_names[ 2 ] ) )
869
- $size = $field_names[ 2 ];
870
-
871
- if ( !empty( $image_id ) ) {
872
- $value = pods_image_url( $image_id, $size, 0, true );
873
 
874
- if ( !empty( $value ) )
875
- $object_field_found = true;
876
- }
877
  }
878
- }
879
- elseif ( 'user' == $this->pod_data[ 'type' ] && !isset( $this->fields[ $params->name ] ) ) {
880
- if ( !isset( $this->fields[ 'avatar' ] ) && ( 'avatar' == $params->name || 0 === strpos( $params->name, 'avatar.' ) ) ) {
881
- $size = null;
882
-
883
- if ( 0 === strpos( $params->name, 'avatar.' ) ) {
884
- $field_names = explode( '.', $params->name );
885
-
886
- if ( isset( $field_names[ 1 ] ) )
887
- $size = (int) $field_names[ 1 ];
888
- }
889
-
890
- if ( !empty( $size ) )
891
- $value = get_avatar( $this->id(), $size );
892
- else
893
- $value = get_avatar( $this->id() );
894
 
 
 
 
895
  $object_field_found = true;
896
- }
897
- }
898
- elseif ( 0 === strpos( $params->name, 'image_attachment.' ) ) {
899
- $size = 'thumbnail';
900
-
901
- $image_id = 0;
902
 
903
- $field_names = explode( '.', $params->name );
904
-
905
- if ( isset( $field_names[ 1 ] ) )
906
- $image_id = $field_names[ 1 ];
907
-
908
- if ( isset( $field_names[ 2 ] ) )
909
- $size = $field_names[ 2 ];
910
 
911
- if ( !empty( $image_id ) ) {
912
- $value = pods_image( $image_id, $size, 0, null, true );
 
 
913
 
914
- if ( !empty( $value ) )
915
- $object_field_found = true;
916
- }
917
- }
918
- elseif ( 0 === strpos( $params->name, 'image_attachment_url.' ) ) {
919
- $size = 'thumbnail';
920
-
921
- $image_id = 0;
 
 
 
 
922
 
923
- $field_names = explode( '.', $params->name );
 
924
 
925
- if ( isset( $field_names[ 1 ] ) )
926
- $image_id = $field_names[ 1 ];
 
927
 
928
- if ( isset( $field_names[ 2 ] ) )
929
- $size = $field_names[ 2 ];
 
 
 
 
 
 
930
 
931
- if ( !empty( $image_id ) ) {
932
- $value = pods_image_url( $image_id, $size, 0, true );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
933
 
934
- if ( !empty( $value ) )
935
- $object_field_found = true;
 
 
 
936
  }
937
  }
938
 
 
939
  if ( false === $object_field_found ) {
940
  $params->traverse = array( $params->name );
941
 
942
  if ( false !== strpos( $params->name, '.' ) ) {
943
  $params->traverse = explode( '.', $params->name );
944
 
945
- $params->name = $params->traverse[ 0 ];
946
  }
947
 
948
- if ( isset( $this->fields[ $params->name ] ) && isset( $this->fields[ $params->name ][ 'type' ] ) ) {
949
- $v = $this->do_hook( 'field_' . $this->fields[ $params->name ][ 'type' ], null, $this->fields[ $params->name ], $this->row, $params );
950
-
951
- if ( null !== $v )
 
 
 
 
 
 
 
 
 
 
 
 
 
952
  return $v;
 
953
  }
954
 
955
  $simple = false;
956
- $simple_data = array();
957
 
958
- if ( isset( $this->fields[ $params->name ] ) ) {
959
- if ( 'meta' == $this->pod_data[ 'storage' ] ) {
960
- if ( !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) )
961
- $simple = true;
962
  }
963
 
964
- if ( in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) ) {
965
  $params->raw = true;
966
 
967
- if ( 'pick' == $this->fields[ $params->name ][ 'type' ] && in_array( $this->fields[ $params->name ][ 'pick_object' ], $simple_tableless_objects ) ) {
968
- $simple = true;
969
  $params->single = true;
970
  }
971
- }
972
- elseif ( in_array( $this->fields[ $params->name ][ 'type' ], array( 'boolean', 'number', 'currency' ) ) )
973
  $params->raw = true;
 
974
  }
975
 
976
- if ( !isset( $this->fields[ $params->name ] ) || !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) || $simple ) {
977
  if ( null === $params->single ) {
978
- if ( isset( $this->fields[ $params->name ] ) && !in_array( $this->fields[ $params->name ][ 'type' ], $tableless_field_types ) )
979
  $params->single = true;
980
- else
981
  $params->single = false;
 
982
  }
983
 
984
- $no_conflict = pods_no_conflict_check( $this->pod_data[ 'type' ] );
985
 
986
- if ( !$no_conflict )
987
- pods_no_conflict_on( $this->pod_data[ 'type' ] );
 
 
988
 
989
- if ( in_array( $this->pod_data[ 'type' ], array( 'post_type', 'media' ) ) ) {
990
  $id = $this->id();
991
 
992
- // Support for WPML 'duplicated' translation handling
993
- if ( is_object( $sitepress ) && $sitepress->is_translated_post_type( $this->pod_data[ 'name' ] ) ) {
994
- $master_post_id = (int) get_post_meta( $id, '_icl_lang_duplicate_of', true );
 
 
 
 
 
995
 
996
- if ( 0 < $master_post_id )
997
- $id = $master_post_id;
 
 
 
 
998
  }
999
 
1000
- $value = get_post_meta( $id, $params->name, $params->single );
1001
 
1002
- $single_multi = pods_var( $this->fields[ $params->name ][ 'type' ] . '_format_type', $this->fields[ $params->name ][ 'options' ], 'single' );
1003
 
1004
- if ( $simple && !is_array( $value ) && 'single' != $single_multi ) {
1005
- $value = get_post_meta( $id, $params->name );
1006
  }
1007
- }
1008
- elseif ( in_array( $this->pod_data[ 'type' ], array( 'user', 'comment' ) ) ) {
1009
- $value = get_metadata( $this->pod_data[ 'type' ], $this->id(), $params->name, $params->single );
1010
-
1011
- $single_multi = pods_var( $this->fields[ $params->name ][ 'type' ] . '_format_type', $this->fields[ $params->name ][ 'options' ], 'single' );
1012
 
1013
- if ( $simple && !is_array( $value ) && 'single' != $single_multi ) {
1014
- $value = get_metadata( $this->pod_data[ 'type' ], $this->id(), $params->name );
1015
  }
1016
- }
1017
- elseif ( 'settings' == $this->pod_data[ 'type' ] )
1018
- $value = get_option( $this->pod_data[ 'name' ] . '_' . $params->name, null );
1019
 
1020
- // Handle Simple Relationships
1021
  if ( $simple ) {
1022
- if ( null === $params->single )
1023
  $params->single = false;
 
1024
 
1025
  $value = PodsForm::field_method( 'pick', 'simple_value', $params->name, $value, $this->fields[ $params->name ], $this->pod_data, $this->id(), true );
1026
  }
1027
 
1028
- if ( !$no_conflict )
1029
- pods_no_conflict_off( $this->pod_data[ 'type' ] );
1030
- }
1031
- else {
1032
- // Dot-traversal
1033
- $pod = $this->pod;
1034
- $ids = array( $this->id() );
 
1035
  $all_fields = array();
1036
 
1037
  $lookup = $params->traverse;
1038
 
1039
- if ( !empty( $lookup ) ) {
1040
- unset( $lookup[ 0 ] );
1041
-
1042
- foreach ( $this->fields as $field ) {
1043
- if ( !in_array( $field[ 'type' ], $tableless_field_types ) || in_array( $field[ 'name' ], $lookup ) )
1044
- continue;
1045
-
1046
- $lookup[] = $field[ 'name' ];
1047
- }
1048
- }
1049
-
1050
- // Get fields matching traversal names
1051
- if ( !empty( $lookup ) ) {
1052
  $fields = $this->api->load_fields( array(
1053
- 'name' => $lookup,
1054
- 'type' => $tableless_field_types,
1055
- 'object_fields' => true // @todo support object fields too
 
1056
  ) );
1057
 
1058
- if ( !empty( $fields ) ) {
1059
  foreach ( $fields as $field ) {
1060
- if ( !empty( $field ) ) {
1061
- if ( !isset( $all_fields[ $field[ 'pod' ] ] ) )
1062
- $all_fields[ $field[ 'pod' ] ] = array();
 
1063
 
1064
- $all_fields[ $field[ 'pod' ] ][ $field[ 'name' ] ] = $field;
1065
  }
1066
  }
1067
  }
1068
 
1069
- if ( isset( $this->pod_data[ 'object_fields' ] ) && !empty( $this->pod_data[ 'object_fields' ] ) ) {
1070
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
1071
- if ( in_array( $object_field_opt[ 'type' ], $tableless_field_types ) ) {
 
 
1072
  $all_fields[ $this->pod ][ $object_field ] = $object_field_opt;
1073
  }
1074
  }
1075
  }
1076
- }
1077
 
1078
- $last_type = $last_object = $last_pick_val = '';
1079
- $last_options = array();
 
1080
 
1081
- $single_multi = pods_var( $this->fields[ $params->name ][ 'type' ] . '_format_type', $this->fields[ $params->name ][ 'options' ], 'single' );
1082
 
1083
- if ( 'multi' == $single_multi )
1084
- $limit = (int) pods_var( $this->fields[ $params->name ][ 'type' ] . '_limit', $this->fields[ $params->name ][ 'options' ], 0 );
1085
- else
1086
  $limit = 1;
 
1087
 
1088
- $last_limit = 0;
1089
-
1090
- // Loop through each traversal level
1091
  foreach ( $params->traverse as $key => $field ) {
1092
  $last_loop = false;
1093
 
1094
- if ( count( $params->traverse ) <= ( $key + 1 ) )
1095
  $last_loop = true;
 
1096
 
1097
  $field_exists = isset( $all_fields[ $pod ][ $field ] );
1098
 
1099
- $simple = false;
1100
  $last_options = array();
1101
 
1102
- if ( $field_exists && 'pick' == $all_fields[ $pod ][ $field ][ 'type' ] && in_array( $all_fields[ $pod ][ $field ][ 'pick_object' ], $simple_tableless_objects ) ) {
1103
- $simple = true;
 
 
 
 
1104
  $last_options = $all_fields[ $pod ][ $field ];
1105
  }
1106
 
1107
- // Tableless handler
1108
- if ( $field_exists && ( !in_array( $all_fields[ $pod ][ $field ][ 'type' ], array( 'pick', 'taxonomy' ) ) || !$simple ) ) {
1109
-
1110
- $type = $all_fields[ $pod ][ $field ][ 'type' ];
1111
- $pick_object = $all_fields[ $pod ][ $field ][ 'pick_object' ];
1112
- $pick_val = $all_fields[ $pod ][ $field ][ 'pick_val' ];
1113
-
1114
- if ( 'table' == $pick_object ) {
1115
-
1116
- $pick_val = pods_var( 'pick_table', $all_fields[ $pod ][ $field ][ 'options' ], $pick_val, null, true );
1117
- }
1118
- elseif ( '__current__' == $pick_val ) {
1119
-
1120
  $pick_val = $pod;
1121
  }
1122
 
1123
  $last_limit = 0;
1124
 
1125
- if ( in_array( $type, $tableless_field_types ) ) {
1126
-
1127
- $single_multi = pods_var( "{$type}_format_type", $all_fields[ $pod ][ $field ][ 'options' ], 'single' );
1128
-
1129
- if ( 'multi' == $single_multi ) {
1130
-
1131
- $last_limit = (int) pods_var( "{$type}_limit", $all_fields[ $pod ][ $field ][ 'options' ], 0 );
1132
- }
1133
- else {
1134
 
 
 
 
1135
  $last_limit = 1;
1136
  }
1137
  }
1138
 
1139
- $last_type = $type;
1140
- $last_object = $pick_object;
1141
  $last_pick_val = $pick_val;
1142
- $last_options = $all_fields[ $pod ][ $field ];
1143
-
1144
- // Temporary hack until there's some better handling here
1145
- $last_limit = $last_limit * count( $ids );
1146
 
1147
- // Get related IDs
1148
- if ( !isset( $all_fields[ $pod ][ $field ][ 'pod_id' ] ) ) {
1149
 
1150
- $all_fields[ $pod ][ $field ][ 'pod_id' ] = 0;
 
 
1151
  }
1152
- if ( isset( $all_fields[ $pod ][ $field ][ 'id' ] ) ) {
1153
 
1154
- $ids = $this->api->lookup_related_items(
1155
- $all_fields[ $pod ][ $field ][ 'id' ],
1156
- $all_fields[ $pod ][ $field ][ 'pod_id' ],
1157
- $ids,
1158
- $all_fields[ $pod ][ $field ]
1159
- );
1160
  }
1161
 
1162
- // No items found
1163
  if ( empty( $ids ) ) {
1164
-
1165
  return false;
1166
- } // @todo This should return array() if not $params->single
1167
- elseif ( 0 < $last_limit ) {
1168
-
1169
  $ids = array_slice( $ids, 0, $last_limit );
1170
  }
1171
 
1172
- // Get $pod if related to a Pod
1173
- if ( !empty( $pick_object ) && !empty( $pick_val ) ) {
1174
-
1175
- if ( 'pod' == $pick_object ) {
1176
-
 
 
1177
  $pod = $pick_val;
1178
- }
1179
- else {
1180
-
1181
  $check = $this->api->get_table_info( $pick_object, $pick_val );
1182
 
1183
- if ( !empty( $check ) && !empty( $check[ 'pod' ] ) ) {
1184
-
1185
- $pod = $check[ 'pod' ][ 'name' ];
1186
  }
1187
  }
1188
  }
1189
- }
1190
- // Assume last iteration
1191
- else {
1192
- // Invalid field
1193
- if ( 0 == $key ) {
1194
- return false;
1195
- }
1196
-
1197
  $last_loop = true;
1198
- }
 
 
 
 
 
 
1199
 
1200
  if ( $last_loop ) {
1201
  $object_type = $last_object;
1202
- $object = $last_pick_val;
1203
 
1204
- if ( in_array( $last_type, PodsForm::file_field_types() ) ) {
1205
  $object_type = 'media';
1206
- $object = 'attachment';
1207
  }
1208
 
1209
  $data = array();
1210
 
1211
  $table = $this->api->get_table_info( $object_type, $object, null, null, $last_options );
1212
 
1213
- $join = $where = array();
 
1214
 
1215
- if ( !empty( $table[ 'join' ] ) )
1216
- $join = (array) $table[ 'join' ];
 
1217
 
1218
- if ( !empty( $table[ 'where' ] ) || !empty( $ids ) ) {
1219
  foreach ( $ids as $id ) {
1220
- $where[ $id ] = '`t`.`' . $table[ 'field_id' ] . '` = ' . (int) $id;
1221
  }
1222
 
1223
- if ( !empty( $where ) ) {
1224
  $where = array( implode( ' OR ', $where ) );
1225
  }
1226
 
1227
- if ( !empty( $table[ 'where' ] ) ) {
1228
- $where = array_merge( $where, array_values( (array) $table[ 'where' ] ) );
 
1229
  }
1230
  }
1231
 
1232
  /**
1233
- * @var $related_obj Pods
 
 
1234
  */
1235
  $related_obj = false;
1236
 
1237
- if ( 'pod' == $object_type )
 
 
 
 
 
 
 
1238
  $related_obj = pods( $object, null, false );
1239
- elseif ( isset( $table[ 'pod' ] ) && !empty( $table[ 'pod' ] ) )
1240
- $related_obj = pods( $table[ 'pod' ][ 'name' ], null, false );
 
1241
 
1242
- if ( !empty( $table[ 'table' ] ) || !empty( $related_obj ) ) {
1243
  $sql = array(
1244
- 'select' => '*, `t`.`' . $table[ 'field_id' ] . '` AS `pod_item_id`',
1245
- 'table' => $table[ 'table' ],
1246
- 'join' => $join,
1247
- 'where' => $where,
1248
- 'orderby' => $params->orderby,
1249
  'pagination' => false,
1250
- 'search' => false,
1251
- 'limit' => -1
 
 
1252
  );
1253
 
1254
- // Output types
1255
- if ( in_array( $params->output, array( 'ids', 'objects', 'pods' ) ) )
1256
- $sql[ 'select' ] = '`t`.`' . $table[ 'field_id' ] . '` AS `pod_item_id`';
1257
- elseif ( 'names' == $params->output && !empty( $table[ 'field_index' ] ) )
1258
- $sql[ 'select' ] = '`t`.`' . $table[ 'field_index' ] . '` AS `pod_item_index`, `t`.`' . $table[ 'field_id' ] . '` AS `pod_item_id`';
 
 
 
 
 
1259
 
1260
- if ( is_array( $params->params ) && !empty( $params->params ) ) {
1261
- $where = $sql[ 'where' ];
1262
 
 
1263
  $sql = array_merge( $sql, $params->params );
1264
 
1265
- if ( isset( $params->params[ 'where' ] ) )
1266
- $sql[ 'where' ] = array_merge( (array) $where, (array) $params->params['where' ] );
 
 
1267
  }
1268
 
1269
- if ( empty( $related_obj ) ) {
1270
- if ( !is_object( $this->alt_data ) )
 
 
1271
  $this->alt_data = pods_data( null, 0, true, true );
 
1272
 
1273
  $item_data = $this->alt_data->select( $sql );
1274
- }
1275
- else
1276
- $item_data = $related_obj->find( $sql )->data();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1277
 
1278
  $items = array();
1279
 
1280
- if ( !empty( $item_data ) ) {
1281
  foreach ( $item_data as $item ) {
1282
- if ( is_array( $item ) )
1283
  $item = (object) $item;
 
1284
 
1285
- if ( empty( $item->pod_item_id ) )
1286
  continue;
 
1287
 
1288
- // Bypass pass field
1289
- if ( isset( $item->user_pass ) )
1290
  unset( $item->user_pass );
 
1291
 
1292
- // Get Item ID
1293
  $item_id = $item->pod_item_id;
1294
 
1295
- // Cleanup
1296
- unset( $item->pod_item_id );
1297
-
1298
- // Output types
1299
- if ( 'ids' == $params->output )
1300
  $item = (int) $item_id;
1301
- elseif ( 'names' == $params->output && !empty( $table[ 'field_index' ] ) )
1302
  $item = $item->pod_item_index;
1303
- elseif ( 'objects' == $params->output ) {
1304
- if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
1305
  $item = get_post( $item_id );
1306
- elseif ( 'taxonomy' == $object_type )
1307
  $item = get_term( $item_id, $object );
1308
- elseif ( 'user' == $object_type ) {
1309
  $item = get_userdata( $item_id );
1310
 
1311
- if ( !empty( $item ) ) {
1312
- // Get other vars
1313
- $roles = $item->roles;
1314
- $caps = $item->caps;
1315
  $allcaps = $item->allcaps;
1316
 
1317
  $item = $item->data;
1318
 
1319
- // Set other vars
1320
- $item->roles = $roles;
1321
- $item->caps = $caps;
1322
  $item->allcaps = $allcaps;
1323
 
1324
  unset( $item->user_pass );
1325
  }
1326
- }
1327
- elseif ( 'comment' == $object_type )
1328
  $item = get_comment( $item_id );
1329
- else
1330
  $item = (object) $item;
1331
- }
1332
- elseif ( 'pods' == $params->output ) {
1333
- $item = pods( $object, (int) $item_id );
1334
- }
1335
- else // arrays
 
 
 
 
1336
  $item = get_object_vars( (object) $item );
 
1337
 
1338
- // Pass item data into $data
1339
  $items[ $item_id ] = $item;
1340
- }
1341
 
1342
- // Cleanup
1343
  unset( $item_data );
1344
 
1345
- // Return all of the data in the order expected
1346
  if ( empty( $params->orderby ) ) {
1347
  foreach ( $ids as $id ) {
1348
- if ( isset( $items[ $id ] ) )
1349
  $data[ $id ] = $items[ $id ];
 
 
 
 
 
 
 
 
 
1350
  }
1351
  }
1352
- }
1353
- }
1354
-
1355
- if ( in_array( $last_type, $tableless_field_types ) || in_array( $last_type, array( 'boolean', 'number', 'currency' ) ) )
 
 
 
 
1356
  $params->raw = true;
 
1357
 
1358
- if ( empty( $data ) )
1359
  $value = false;
1360
- else {
1361
- $object_type = $table[ 'type' ];
1362
 
1363
- if ( in_array( $table[ 'type' ], array( 'post_type', 'attachment', 'media' ) ) )
1364
  $object_type = 'post';
 
 
1365
 
1366
- $no_conflict = true;
1367
 
1368
- if ( in_array( $object_type, array( 'post', 'user', 'comment', 'settings' ) ) ) {
1369
- $no_conflict = pods_no_conflict_check( $object_type );
1370
 
1371
- if ( !$no_conflict )
1372
- pods_no_conflict_on( $object_type );
 
1373
  }
1374
 
1375
- // Return entire array
1376
- if ( false !== $field_exists && ( in_array( $last_type, $tableless_field_types ) && !$simple ) )
1377
  $value = $data;
1378
- // Return an array of single column values
1379
- else {
1380
  $value = array();
1381
 
1382
  foreach ( $data as $item_id => $item ) {
1383
  // $field is 123x123, needs to be _src.123x123
1384
- $full_field = implode( '.', array_splice( $params->traverse, $key ) );
 
 
1385
 
1386
- if ( ( ( false !== strpos( $full_field, '_src' ) || 'guid' == $field ) && ( in_array( $table[ 'type' ], array( 'attachment', 'media' ) ) || in_array( $last_type, PodsForm::file_field_types() ) ) ) || ( in_array( $field, array( '_link', 'detail_url' ) ) || in_array( $field, array( 'permalink', 'the_permalink' ) ) && in_array( $last_type, PodsForm::file_field_types() ) ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1387
  $size = 'full';
1388
 
1389
- if ( false !== strpos( $full_field, '_src.' ) && 5 < strlen( $full_field ) )
 
 
 
 
 
 
1390
  $size = substr( $full_field, 5 );
1391
- elseif ( false !== strpos( $full_field, '_src_relative.' ) && 14 < strlen( $full_field ) )
1392
  $size = substr( $full_field, 14 );
1393
- elseif ( false !== strpos( $full_field, '_src_schemeless.' ) && 16 < strlen( $full_field ) )
1394
  $size = substr( $full_field, 16 );
 
1395
 
1396
- $value_url = pods_image_url( $item_id, $size );
 
 
 
 
1397
 
1398
- if ( false !== strpos( $full_field, '_src_relative' ) && !empty( $value_url ) ) {
1399
- $value_url_parsed = parse_url( $value_url );
1400
- $value_url = $value_url_parsed[ 'path' ];
 
 
 
 
 
 
 
1401
  }
1402
- elseif ( false !== strpos( $full_field, '_src_schemeless' ) && !empty( $value_url ) )
1403
- $value_url = str_replace( array( 'http://', 'https://' ), '//', $value_url );
1404
 
1405
- if ( !empty( $value_url ) )
1406
  $value[] = $value_url;
 
1407
 
1408
  $params->raw_display = true;
1409
- }
1410
- elseif ( false !== strpos( $full_field, '_img' ) && ( in_array( $table[ 'type' ], array( 'attachment', 'media' ) ) || in_array( $last_type, PodsForm::file_field_types() ) ) ) {
 
 
1411
  $size = 'full';
1412
 
1413
- if ( false !== strpos( $full_field, '_img.' ) && 5 < strlen( $full_field ) )
1414
  $size = substr( $full_field, 5 );
 
1415
 
1416
- $value[] = pods_image( $item_id, $size );
1417
 
1418
  $params->raw_display = true;
1419
- }
1420
- elseif ( in_array( $field, array( '_link', 'detail_url' ) ) || ( in_array( $field, array( 'permalink', 'the_permalink' ) ) && 'post' == $object_type ) ) {
1421
- if ( 'pod' == $object_type ) {
 
 
1422
  if ( is_object( $related_obj ) ) {
1423
  $related_obj->fetch( $item_id );
1424
 
1425
  $value[] = $related_obj->field( 'detail_url' );
1426
- }
1427
- else
1428
  $value[] = '';
1429
- }
1430
- elseif ( 'post' == $object_type )
1431
  $value[] = get_permalink( $item_id );
1432
- elseif ( 'taxonomy' == $object_type )
1433
  $value[] = get_term_link( $item_id, $object );
1434
- elseif ( 'user' == $object_type )
1435
  $value[] = get_author_posts_url( $item_id );
1436
- elseif ( 'comment' == $object_type )
1437
  $value[] = get_comment_link( $item_id );
1438
- else
1439
  $value[] = '';
 
1440
 
1441
  $params->raw_display = true;
1442
- }
1443
- elseif ( is_array( $item ) && isset( $item[ $field ] ) ) {
1444
- if ( $table[ 'field_id' ] == $field )
1445
- $value[] = (int) $item[ $field ];
1446
- else
1447
- $value[] = $item[ $field ];
1448
- }
1449
- elseif ( is_object( $item ) && isset( $item->{$field} ) ) {
1450
- if ( $table[ 'field_id' ] == $field )
1451
- $value[] = (int) $item->{$field};
1452
- else
1453
- $value[] = $item->{$field};
1454
- }
1455
- elseif ( 'post' == $object_type ) {
1456
- // Support for WPML 'duplicated' translation handling
1457
- if ( is_object( $sitepress ) && $sitepress->is_translated_post_type( $object ) ) {
1458
- $master_post_id = (int) get_post_meta( $item_id, '_icl_lang_duplicate_of', true );
1459
-
1460
- if ( 0 < $master_post_id )
1461
- $item_id = $master_post_id;
 
1462
  }
1463
 
1464
- $value[] = get_post_meta( $item_id, $field, true );
1465
- }
1466
- elseif ( in_array( $object_type, array( 'post', 'user', 'comment' ) ) )
1467
- $value[] = get_metadata( $object_type, $item_id, $field, true );
1468
- elseif ( 'settings' == $object_type )
1469
- $value[] = get_option( $object . '_' . $field );
1470
- }
1471
- }
1472
-
1473
- if ( in_array( $object_type, array( 'post', 'user', 'comment', 'settings' ) ) && !$no_conflict )
 
1474
  pods_no_conflict_off( $object_type );
 
1475
 
1476
- // Handle Simple Relationships
1477
  if ( $simple ) {
1478
- if ( null === $params->single )
1479
  $params->single = false;
 
1480
 
1481
  $value = PodsForm::field_method( 'pick', 'simple_value', $field, $value, $last_options, $all_fields[ $pod ], 0, true );
1482
- }
1483
- elseif ( false === $params->in_form && !empty( $value ) )
1484
  $value = array_values( $value );
 
1485
 
1486
- // Return a single column value
1487
- if ( false === $params->in_form && 1 == $limit && !empty( $value ) && is_array( $value ) && 1 == count( $value ) )
1488
  $value = current( $value );
 
 
 
 
 
 
 
 
1489
  }
1490
 
1491
  break;
1492
- }
1493
- }
1494
- }
1495
- }
1496
- }
1497
 
1498
- if ( !empty( $params->traverse ) && 1 < count( $params->traverse ) ) {
1499
  $field_names = implode( '.', $params->traverse );
1500
 
1501
  $this->row[ $field_names ] = $value;
1502
- }
1503
- elseif ( 'arrays' != $params->output && in_array( $field_data[ 'type' ], $tableless_field_types ) ) {
1504
  $this->row[ '_' . $params->output . '_' . $params->full_name ] = $value;
1505
- }
1506
- elseif ( 'arrays' == $params->output || !in_array( $field_data[ 'type' ], $tableless_field_types ) ) {
1507
  $this->row[ $params->full_name ] = $value;
1508
  }
1509
 
1510
- if ( $params->single && is_array( $value ) && 1 == count( $value ) )
1511
  $value = current( $value );
 
 
 
 
 
1512
 
1513
- // @todo Expand this into traversed fields too
1514
- if ( !empty( $field_data ) && ( $params->display || !$params->raw ) && !$params->in_form && !$params->raw_display ) {
1515
- if ( $params->display || ( ( $params->get_meta || $params->deprecated ) && !in_array( $field_data[ 'type' ], $tableless_field_types ) ) ) {
1516
- $field_data[ 'options' ] = pods_var_raw( 'options', $field_data, array(), null, true );
1517
 
1518
- $post_temp = false;
 
 
 
1519
 
1520
- if ( 'post_type' == pods_var( 'type', $this->pod_data ) && 0 < $this->id() && ( !isset( $GLOBALS[ 'post' ] ) || empty( $GLOBALS[ 'post' ] ) ) ) {
1521
  global $post_ID, $post;
1522
 
1523
  $post_temp = true;
1524
 
1525
- $old_post = $GLOBALS[ 'post' ];
1526
- $old_ID = $GLOBALS[ 'post_ID' ];
1527
 
1528
- $post = get_post( $this->id() );
 
 
1529
  $post_ID = $this->id();
1530
  }
1531
 
1532
- $filter = pods_var_raw( 'display_filter', $field_data[ 'options' ] );
1533
 
1534
  if ( 0 < strlen( $filter ) ) {
1535
  $args = array(
1536
  $filter,
1537
- $value
1538
  );
1539
 
1540
- $filter_args = pods_var_raw( 'display_filter_args', $field_data[ 'options' ] );
1541
 
1542
- if ( !empty( $filter_args ) )
1543
  $args = array_merge( $args, compact( $filter_args ) );
 
1544
 
1545
  $value = call_user_func_array( 'apply_filters', $args );
1546
- }
1547
- elseif ( 1 == pods_var( 'display_process', $field_data[ 'options' ], 1 ) ) {
1548
- $value = PodsForm::display(
1549
- $field_data[ 'type' ],
1550
- $value,
1551
- $params->name,
1552
- array_merge( $field_data, $field_data[ 'options' ] ),
1553
- $this->pod_data,
1554
- $this->id()
1555
- );
1556
  }
1557
 
1558
  if ( $post_temp ) {
1559
- $post = $old_post;
1560
- $post_ID = $old_ID;
 
 
1561
  }
1562
- }
1563
- else {
1564
- $value = PodsForm::value(
1565
- $field_data[ 'type' ],
1566
- $value,
1567
- $params->name,
1568
- array_merge( $field_data, $field_data[ 'options' ] ),
1569
- $this->pod_data,
1570
- $this->id()
1571
- );
1572
- }
1573
- }
1574
-
1575
- $value = $this->do_hook( 'field', $value, $this->row, $params );
 
 
 
 
1576
 
1577
  return $value;
1578
  }
@@ -1580,71 +1974,79 @@ class Pods implements Iterator {
1580
  /**
1581
  * Check if an item field has a specific value in it
1582
  *
1583
- * @param string $field Field name
1584
- * @param mixed $value Value to check
1585
- * @param int $id (optional) ID of the pod item to check
1586
  *
1587
  * @return bool Whether the value was found
1588
  *
1589
  * @since 2.3.3
1590
  */
1591
- public function has ( $field, $value, $id = null ) {
 
1592
  $pod =& $this;
1593
 
1594
- if ( null === $id )
1595
  $id = $this->id();
1596
- elseif ( $id != $this->id() )
 
1597
  $pod = pods( $this->pod, $id );
 
1598
 
1599
  $this->do_hook( 'has', $field, $value, $id );
1600
 
1601
- if ( !isset( $this->fields[ $field ] ) )
1602
  return false;
 
1603
 
1604
- // Tableless fields
1605
- if ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::tableless_field_types() ) ) {
1606
- if ( !is_array( $value ) )
1607
  $value = explode( ',', $value );
 
1608
 
1609
- if ( 'pick' == $this->fields[ $field ][ 'type' ] && in_array( $this->fields[ $field ][ 'pick_object' ], PodsForm::field_method( 'pick', 'simple_objects' ) ) ) {
1610
  $current_value = $pod->raw( $field );
1611
 
1612
- if ( !empty( $current_value ) )
1613
  $current_value = (array) $current_value;
 
1614
 
1615
  foreach ( $current_value as $v ) {
1616
- if ( in_array( $v, $value ) )
 
1617
  return true;
 
1618
  }
1619
- }
1620
- else {
1621
- $related_ids = $this->api->lookup_related_items( $this->fields[ $field ][ 'id' ], $this->pod_data[ 'id' ], $id, $this->fields[ $field ], $this->pod_data );
1622
 
1623
  foreach ( $value as $k => $v ) {
1624
- if ( !preg_match( '/[^0-9]/', $v ) )
1625
  $value[ $k ] = (int) $v;
1626
- // @todo Convert slugs into IDs
1627
- else {
1628
-
1629
  }
 
 
1630
  }
1631
 
1632
  foreach ( $related_ids as $v ) {
1633
- if ( in_array( $v, $value ) )
 
1634
  return true;
 
1635
  }
1636
- }
1637
- }
1638
- // Text fields
1639
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::text_field_types() ) ) {
1640
  $current_value = $pod->raw( $field );
1641
 
1642
- if ( 0 < strlen( $current_value ) )
1643
  return stripos( $current_value, $value );
1644
- }
1645
- // All other fields
1646
- else
1647
  return $this->is( $field, $value, $id );
 
1648
 
1649
  return false;
1650
  }
@@ -1652,111 +2054,122 @@ class Pods implements Iterator {
1652
  /**
1653
  * Check if an item field is a specific value
1654
  *
1655
- * @param string $field Field name
1656
- * @param mixed $value Value to check
1657
- * @param int $id (optional) ID of the pod item to check
1658
  *
1659
  * @return bool Whether the value was found
1660
  *
1661
  * @since 2.3.3
1662
  */
1663
- public function is ( $field, $value, $id = null ) {
 
1664
  $pod =& $this;
1665
 
1666
- if ( null === $id )
1667
  $id = $this->id();
1668
- elseif ( $id != $this->id() )
 
1669
  $pod = pods( $this->pod, $id );
 
1670
 
1671
  $this->do_hook( 'is', $field, $value, $id );
1672
 
1673
- if ( !isset( $this->fields[ $field ] ) )
1674
  return false;
 
1675
 
1676
- // Tableless fields
1677
- if ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::tableless_field_types() ) ) {
1678
- if ( !is_array( $value ) )
1679
  $value = explode( ',', $value );
 
1680
 
1681
  $current_value = array();
1682
 
1683
- if ( 'pick' == $this->fields[ $field ][ 'type' ] && in_array( $this->fields[ $field ][ 'pick_object' ], PodsForm::field_method( 'pick', 'simple_objects' ) ) ) {
1684
  $current_value = $pod->raw( $field );
1685
 
1686
- if ( !empty( $current_value ) )
1687
  $current_value = (array) $current_value;
 
1688
 
1689
  foreach ( $current_value as $v ) {
1690
- if ( in_array( $v, $value ) )
 
1691
  return true;
 
1692
  }
1693
- }
1694
- else {
1695
- $related_ids = $this->api->lookup_related_items( $this->fields[ $field ][ 'id' ], $this->pod_data[ 'id' ], $id, $this->fields[ $field ], $this->pod_data );
1696
 
1697
  foreach ( $value as $k => $v ) {
1698
- if ( !preg_match( '/[^0-9]/', $v ) )
1699
  $value[ $k ] = (int) $v;
1700
- // @todo Convert slugs into IDs
1701
- else {
1702
-
1703
  }
 
 
1704
  }
1705
 
1706
  foreach ( $related_ids as $v ) {
1707
- if ( in_array( $v, $value ) )
 
1708
  return true;
 
1709
  }
1710
- }
1711
 
1712
- if ( !empty( $current_value ) )
1713
  $current_value = array_filter( array_unique( $current_value ) );
1714
- else
1715
  $current_value = array();
 
1716
 
1717
- if ( !empty( $value ) )
1718
  $value = array_filter( array_unique( $value ) );
1719
- else
1720
  $value = array();
 
1721
 
1722
  sort( $current_value );
1723
  sort( $value );
1724
 
1725
- if ( $value === $current_value )
1726
  return true;
1727
- }
1728
- // Number fields
1729
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::number_field_types() ) ) {
1730
  $current_value = $pod->raw( $field );
1731
 
1732
- if ( (float) $current_value === (float) $value )
1733
  return true;
1734
- }
1735
- // Date fields
1736
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::date_field_types() ) ) {
1737
  $current_value = $pod->raw( $field );
1738
 
1739
  if ( 0 < strlen( $current_value ) ) {
1740
- if ( strtotime( $current_value ) == strtotime( $value ) )
1741
  return true;
1742
- }
1743
- elseif ( empty( $value ) )
1744
  return true;
1745
- }
1746
- // Text fields
1747
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::text_field_types() ) ) {
1748
  $current_value = $pod->raw( $field );
1749
 
1750
- if ( (string) $current_value === (string) $value )
1751
  return true;
1752
- }
1753
- // All other fields
1754
- else {
1755
  $current_value = $pod->raw( $field );
1756
 
1757
- if ( $current_value === $value )
1758
  return true;
1759
- }
 
1760
 
1761
  return false;
1762
  }
@@ -1765,94 +2178,101 @@ class Pods implements Iterator {
1765
  * Return the item ID
1766
  *
1767
  * @return int
1768
- * @since 2.0
1769
  */
1770
- public function id () {
 
 
 
 
 
 
1771
  return $this->field( $this->data->field_id );
1772
  }
1773
 
1774
  /**
1775
  * Return the previous item ID, loops at the last id to return the first
1776
  *
1777
- * @param int $id
1778
- * @param array $params_override
1779
  *
1780
  * @return int
1781
- * @since 2.0
1782
  */
1783
- public function prev_id ( $id = null, $params_override = null ) {
1784
- if ( null === $id )
 
1785
  $id = $this->id();
 
1786
 
1787
  $id = (int) $id;
1788
 
1789
  $params = array(
1790
- 'select' => "`t`.`{$this->data->field_id}`",
1791
- 'where' => "`t`.`{$this->data->field_id}` < {$id}",
1792
  'orderby' => "`t`.`{$this->data->field_id}` DESC",
1793
- 'limit' => 1
1794
  );
1795
 
1796
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
1797
- if ( !empty( $params_override ) )
1798
  $params = $params_override;
1799
- elseif ( !empty( $this->params ) )
1800
  $params = $this->params;
 
1801
 
1802
  if ( is_object( $params ) ) {
1803
  $params = get_object_vars( $params );
1804
  }
1805
 
1806
  if ( 0 < $id ) {
1807
- if ( isset( $params[ 'where' ] ) && !empty( $params[ 'where' ] ) ) {
1808
- $params[ 'where' ] = (array) $params[ 'where' ];
1809
- $params[ 'where' ][] = "`t`.`{$this->data->field_id}` < {$id}";
1810
- }
1811
- else {
1812
- $params[ 'where' ] = "`t`.`{$this->data->field_id}` < {$id}";
1813
  }
1814
- }
1815
- elseif ( isset( $params[ 'offset' ] ) && 0 < $params[ 'offset' ] )
1816
- $params[ 'offset' ] -= 1;
1817
- elseif ( !isset( $params[ 'offset' ] ) && !empty( $this->params ) && 0 < $this->row_number )
1818
- $params[ 'offset' ] = $this->row_number - 1;
1819
- else
1820
  return 0;
 
1821
 
1822
- if ( isset( $params[ 'orderby' ] ) && !empty( $params[ 'orderby' ] ) ) {
1823
- if ( is_array( $params[ 'orderby' ] ) ) {
1824
- foreach ( $params[ 'orderby' ] as $orderby => $dir ) {
1825
  $dir = strtoupper( $dir );
1826
 
1827
- if ( !in_array( $dir, array( 'ASC', 'DESC' ) ) ) {
1828
  continue;
1829
  }
1830
 
1831
- if ( 'ASC' == $dir ) {
1832
- $params[ 'orderby' ][ $orderby ] = 'DESC';
1833
- }
1834
- else {
1835
- $params[ 'orderby' ][ $orderby ] = 'ASC';
1836
  }
1837
  }
1838
 
1839
- $params[ 'orderby' ][ $this->data->field_id ] = 'DESC';
 
 
1840
  }
1841
- elseif ( "`t`.`{$this->data->field_id}` DESC" != $params[ 'orderby' ] ) {
1842
- $params[ 'orderby' ] .= ", `t`.`{$this->data->field_id}` DESC";
1843
- }
1844
- }
1845
 
1846
- $params[ 'select' ] = "`t`.`{$this->data->field_id}`";
1847
- $params[ 'limit' ] = 1;
1848
- }
1849
 
1850
  $pod = pods( $this->pod, $params );
1851
 
1852
  $new_id = 0;
1853
 
1854
- if ( $pod->fetch() )
1855
  $new_id = $pod->id();
 
1856
 
1857
  $new_id = $this->do_hook( 'prev_id', $new_id, $id, $pod, $params_override );
1858
 
@@ -1862,63 +2282,66 @@ class Pods implements Iterator {
1862
  /**
1863
  * Return the next item ID, loops at the first id to return the last
1864
  *
1865
- * @param int $id
1866
- * @param array $find_params
1867
  *
1868
  * @return int
1869
- * @since 2.0
1870
  */
1871
- public function next_id ( $id = null, $params_override = null ) {
1872
- if ( null === $id )
 
1873
  $id = $this->id();
 
1874
 
1875
  $id = (int) $id;
1876
 
1877
  $params = array(
1878
- 'select' => "`t`.`{$this->data->field_id}`",
1879
- 'where' => "{$id} < `t`.`{$this->data->field_id}`",
1880
  'orderby' => "`t`.`{$this->data->field_id}` ASC",
1881
- 'limit' => 1
1882
  );
1883
 
1884
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
1885
- if ( !empty( $params_override ) )
1886
  $params = $params_override;
1887
- elseif ( !empty( $this->params ) )
1888
  $params = $this->params;
 
1889
 
1890
  if ( is_object( $params ) ) {
1891
  $params = get_object_vars( $params );
1892
  }
1893
 
1894
  if ( 0 < $id ) {
1895
- if ( isset( $params[ 'where' ] ) && !empty( $params[ 'where' ] ) ) {
1896
- $params[ 'where' ] = (array) $params[ 'where' ];
1897
- $params[ 'where' ][] = "{$id} < `t`.`{$this->data->field_id}`";
 
 
1898
  }
1899
- else {
1900
- $params[ 'where' ] = "{$id} < `t`.`{$this->data->field_id}`";
 
 
 
1901
  }
 
 
1902
  }
1903
- elseif ( !isset( $params[ 'offset' ] ) ) {
1904
- if ( !empty( $this->params ) && -1 < $this->row_number )
1905
- $params[ 'offset' ] = $this->row_number + 1;
1906
- else
1907
- $params[ 'offset' ] = 1;
1908
- }
1909
- else
1910
- $params[ 'offset' ] += 1;
1911
 
1912
- $params[ 'select' ] = "`t`.`{$this->data->field_id}`";
1913
- $params[ 'limit' ] = 1;
1914
- }
1915
 
1916
  $pod = pods( $this->pod, $params );
1917
 
1918
  $new_id = 0;
1919
 
1920
- if ( $pod->fetch() )
1921
  $new_id = $pod->id();
 
1922
 
1923
  $new_id = $this->do_hook( 'next_id', $new_id, $id, $pod, $params_override );
1924
 
@@ -1928,39 +2351,42 @@ class Pods implements Iterator {
1928
  /**
1929
  * Return the first item ID
1930
  *
1931
- * @param array $params_override
1932
  *
1933
  * @return int
1934
- * @since 2.3
1935
  */
1936
- public function first_id ( $params_override = null ) {
 
1937
  $params = array(
1938
- 'select' => "`t`.`{$this->data->field_id}`",
1939
  'orderby' => "`t`.`{$this->data->field_id}` ASC",
1940
- 'limit' => 1
1941
  );
1942
 
1943
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
1944
- if ( !empty( $params_override ) )
1945
  $params = $params_override;
1946
- elseif ( !empty( $this->params ) )
1947
  $params = $this->params;
 
1948
 
1949
  if ( is_object( $params ) ) {
1950
  $params = get_object_vars( $params );
1951
  }
1952
 
1953
- $params[ 'select' ] = "`t`.`{$this->data->field_id}`";
1954
- $params[ 'offset' ] = 0;
1955
- $params[ 'limit' ] = 1;
1956
  }
1957
 
1958
  $pod = pods( $this->pod, $params );
1959
 
1960
  $new_id = 0;
1961
 
1962
- if ( $pod->fetch() )
1963
  $new_id = $pod->id();
 
1964
 
1965
  $new_id = $this->do_hook( 'first_id', $new_id, $pod, $params_override );
1966
 
@@ -1970,286 +2396,321 @@ class Pods implements Iterator {
1970
  /**
1971
  * Return the last item ID
1972
  *
1973
- * @param array $params_override
1974
  *
1975
  * @return int
1976
- * @since 2.3
1977
  */
1978
- public function last_id ( $params_override = null ) {
 
1979
  $params = array(
1980
- 'select' => "`t`.`{$this->data->field_id}`",
1981
  'orderby' => "`t`.`{$this->data->field_id}` DESC",
1982
- 'limit' => 1
1983
  );
1984
 
1985
- if ( !empty( $params_override ) || !empty( $this->params ) ) {
1986
- if ( !empty( $params_override ) )
1987
  $params = $params_override;
1988
- elseif ( !empty( $this->params ) )
1989
  $params = $this->params;
 
1990
 
1991
  if ( is_object( $params ) ) {
1992
  $params = get_object_vars( $params );
1993
  }
1994
 
1995
- if ( isset( $params[ 'total_found' ] ) )
1996
- $params[ 'offset' ] = $params[ 'total_found' ] - 1;
1997
- else
1998
- $params[ 'offset' ] = $this->total_found() - 1;
 
1999
 
2000
- if ( isset( $params[ 'orderby' ] ) && !empty( $params[ 'orderby' ] ) ) {
2001
- if ( is_array( $params[ 'orderby' ] ) ) {
2002
- foreach ( $params[ 'orderby' ] as $orderby => $dir ) {
2003
  $dir = strtoupper( $dir );
2004
 
2005
- if ( !in_array( $dir, array( 'ASC', 'DESC' ) ) ) {
2006
  continue;
2007
  }
2008
 
2009
- if ( 'ASC' == $dir ) {
2010
- $params[ 'orderby' ][ $orderby ] = 'DESC';
2011
- }
2012
- else {
2013
- $params[ 'orderby' ][ $orderby ] = 'ASC';
2014
  }
2015
  }
2016
 
2017
- $params[ 'orderby' ][ $this->data->field_id ] = 'DESC';
2018
- }
2019
- elseif ( "`t`.`{$this->data->field_id}` DESC" != $params[ 'orderby' ] ) {
2020
- $params[ 'orderby' ] .= ", `t`.`{$this->data->field_id}` DESC";
2021
  }
2022
- }
2023
 
2024
- $params[ 'select' ] = "`t`.`{$this->data->field_id}`";
2025
- $params[ 'limit' ] = 1;
2026
- }
2027
 
2028
  $pod = pods( $this->pod, $params );
2029
 
2030
  $new_id = 0;
2031
 
2032
- if ( $pod->fetch() )
2033
  $new_id = $pod->id();
 
2034
 
2035
  $new_id = $this->do_hook( 'last_id', $new_id, $pod, $params_override );
 
 
2036
  }
2037
 
2038
  /**
2039
  * Return the item name
2040
  *
2041
  * @return string
2042
- * @since 2.0
2043
  */
2044
- public function index () {
 
2045
  return $this->field( $this->data->field_index );
2046
  }
2047
 
2048
  /**
2049
  * Find items of a pod, much like WP_Query, but with advanced table handling.
2050
  *
2051
- * @param array $params An associative array of parameters
2052
- * @param int $limit (optional) (deprecated) Limit the number of items to find, use -1 to return all items with no limit
2053
- * @param string $where (optional) (deprecated) SQL WHERE declaration to use
2054
- * @param string $sql (optional) (deprecated) For advanced use, a custom SQL query to run
2055
  *
2056
  * @return \Pods The pod object
2057
- * @since 2.0
2058
- * @link http://pods.io/docs/find/
2059
  */
2060
- public function find ( $params = null, $limit = 15, $where = null, $sql = null ) {
2061
- $tableless_field_types = PodsForm::tableless_field_types();
2062
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
 
2063
 
2064
  $this->params = $params;
2065
 
2066
  $select = '`t`.*';
2067
 
2068
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) && 'table' == $this->pod_data[ 'storage' ] )
 
 
 
2069
  $select .= ', `d`.*';
 
2070
 
2071
- if ( empty( $this->data->table ) )
2072
  return $this;
 
2073
 
2074
  $defaults = array(
2075
- 'table' => $this->data->table,
2076
- 'select' => $select,
2077
- 'join' => null,
2078
-
2079
- 'where' => $where,
2080
- 'groupby' => null,
2081
- 'having' => null,
2082
- 'orderby' => null,
2083
-
2084
- 'limit' => (int) $limit,
2085
- 'offset' => null,
2086
- 'page' => (int) $this->page,
2087
- 'page_var' => $this->page_var,
2088
- 'pagination' => (boolean) $this->pagination,
2089
-
2090
- 'search' => (boolean) $this->search,
2091
- 'search_var' => $this->search_var,
2092
- 'search_query' => null,
2093
- 'search_mode' => $this->search_mode,
2094
- 'search_across' => false,
 
2095
  'search_across_picks' => false,
2096
  'search_across_files' => false,
2097
-
2098
- 'filters' => $this->filters,
2099
- 'sql' => $sql,
2100
-
2101
- 'expires' => null,
2102
- 'cache_mode' => 'cache'
2103
  );
2104
 
2105
- if ( is_array( $params ) )
2106
  $params = (object) array_merge( $defaults, $params );
2107
- if ( is_object( $params ) )
2108
  $params = (object) array_merge( $defaults, get_object_vars( $params ) );
2109
- else {
2110
- $defaults[ 'orderby' ] = $params;
 
2111
  $params = (object) $defaults;
2112
  }
2113
 
2114
- $params = $this->do_hook( 'find', $params );
 
 
 
 
 
2115
 
2116
  $params->limit = (int) $params->limit;
2117
 
2118
- if ( 0 == $params->limit )
2119
- $params->limit = -1;
 
2120
 
2121
- $this->limit = (int) $params->limit;
2122
- $this->offset = (int) $params->offset;
2123
- $this->page = (int) $params->page;
2124
- $this->page_var = $params->page_var;
2125
  $this->pagination = (boolean) $params->pagination;
2126
- $this->search = (boolean) $params->search;
2127
  $this->search_var = $params->search_var;
2128
- $params->join = (array) $params->join;
2129
 
2130
- if ( empty( $params->search_query ) )
2131
- $params->search_query = pods_var( $this->search_var, 'get', '' );
 
2132
 
2133
- // Allow orderby array ( 'field' => 'asc|desc' )
2134
- if ( !empty( $params->orderby ) && is_array( $params->orderby ) ) {
2135
- foreach ( $params->orderby as $k => &$orderby ) {
2136
- if ( !is_numeric( $k ) ) {
2137
  $key = '';
2138
 
2139
  $order = 'ASC';
2140
 
2141
- if ( 'DESC' == strtoupper( $orderby ) )
2142
  $order = 'DESC';
 
2143
 
2144
- if ( isset( $this->fields[ $k ] ) && in_array( $this->fields[ $k ][ 'type' ], $tableless_field_types ) ) {
2145
- if ( in_array( $this->fields[ $k ][ 'pick_object' ], $simple_tableless_objects ) ) {
2146
- if ( 'table' == $this->pod_data[ 'storage' ] ) {
2147
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) )
2148
  $key = "`d`.`{$k}`";
2149
- else
2150
  $key = "`t`.`{$k}`";
2151
- }
2152
- else
2153
  $key = "`{$k}`.`meta_value`";
2154
- }
2155
- else {
2156
- $pick_val = $this->fields[ $k ][ 'pick_val' ];
2157
 
2158
- if ( '__current__' == $pick_val )
2159
  $pick_val = $this->pod;
 
2160
 
2161
- $table = $this->api->get_table_info( $this->fields[ $k ][ 'pick_object' ], $pick_val );
2162
 
2163
- if ( !empty( $table ) )
2164
- $key = "`{$k}`.`" . $table[ 'field_index' ] . '`';
2165
- }
2166
- }
 
2167
 
2168
  if ( empty( $key ) ) {
2169
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) ) {
2170
- if ( isset( $this->pod_data[ 'object_fields' ][ $k ] ) )
2171
  $key = "`t`.`{$k}`";
2172
- elseif ( isset( $this->fields[ $k ] ) ) {
2173
- if ( 'table' == $this->pod_data[ 'storage' ] )
2174
  $key = "`d`.`{$k}`";
2175
- else
2176
  $key = "`{$k}`.`meta_value`";
2177
- }
2178
- else {
2179
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
2180
- if ( $object_field == $k || in_array( $k, $object_field_opt[ 'alias' ] ) )
 
 
2181
  $key = "`t`.`{$object_field}`";
 
2182
  }
2183
  }
2184
- }
2185
- elseif ( isset( $this->fields[ $k ] ) ) {
2186
- if ( 'table' == $this->pod_data[ 'storage' ] )
2187
  $key = "`t`.`{$k}`";
2188
- else
2189
  $key = "`{$k}`.`meta_value`";
2190
- }
 
2191
 
2192
  if ( empty( $key ) ) {
2193
  $key = $k;
2194
 
2195
- if ( false === strpos( $key, ' ' ) && false === strpos( $key, '`' ) )
2196
  $key = '`' . str_replace( '.', '`.`', $key ) . '`';
 
2197
  }
2198
- }
2199
 
2200
  $orderby = $key;
2201
 
2202
- if ( false === strpos( $orderby, ' ' ) )
2203
  $orderby .= ' ' . $order;
2204
- }
2205
- }
2206
- }
 
 
 
2207
 
2208
- // Add prefix to $params->orderby if needed
2209
- if ( !empty( $params->orderby ) ) {
2210
- if ( !is_array( $params->orderby ) )
2211
  $params->orderby = array( $params->orderby );
 
2212
 
2213
- foreach ( $params->orderby as &$prefix_orderby ) {
2214
  if ( false === strpos( $prefix_orderby, ',' ) && false === strpos( $prefix_orderby, '(' ) && false === stripos( $prefix_orderby, ' AS ' ) && false === strpos( $prefix_orderby, '`' ) && false === strpos( $prefix_orderby, '.' ) ) {
2215
  if ( false !== stripos( $prefix_orderby, ' DESC' ) ) {
2216
- $k = trim( str_ireplace( array( '`', ' DESC' ), '', $prefix_orderby ) );
2217
  $dir = 'DESC';
2218
- }
2219
- else {
2220
- $k = trim( str_ireplace( array( '`', ' ASC' ), '', $prefix_orderby ) );
2221
  $dir = 'ASC';
2222
  }
2223
 
2224
  $key = $k;
2225
 
2226
- if ( !in_array( $this->pod_data[ 'type' ], array( 'pod', 'table' ) ) ) {
2227
- if ( isset( $this->pod_data[ 'object_fields' ][ $k ] ) )
2228
  $key = "`t`.`{$k}`";
2229
- elseif ( isset( $this->fields[ $k ] ) ) {
2230
- if ( 'table' == $this->pod_data[ 'storage' ] )
2231
  $key = "`d`.`{$k}`";
2232
- else
2233
  $key = "`{$k}`.`meta_value`";
2234
- }
2235
- else {
2236
- foreach ( $this->pod_data[ 'object_fields' ] as $object_field => $object_field_opt ) {
2237
- if ( $object_field == $k || in_array( $k, $object_field_opt[ 'alias' ] ) )
 
 
2238
  $key = "`t`.`{$object_field}`";
 
2239
  }
2240
  }
2241
- }
2242
- elseif ( isset( $this->fields[ $k ] ) ) {
2243
- if ( 'table' == $this->pod_data[ 'storage' ] )
2244
  $key = "`t`.`{$k}`";
2245
- else
2246
  $key = "`{$k}`.`meta_value`";
2247
- }
 
2248
 
2249
  $prefix_orderby = "{$key} {$dir}";
2250
- }
2251
- }
2252
- }
 
 
2253
 
2254
  $this->data->select( $params );
2255
 
@@ -2260,23 +2721,34 @@ class Pods implements Iterator {
2260
  * Fetch an item from a Pod. If $id is null, it will return the next item in the list after running find().
2261
  * You can rewind the list back to the start by using reset().
2262
  *
2263
- * Providing an $id will fetch a specific item from a Pod, much like a call to pods(), and can handle either an id or slug.
 
2264
  *
2265
- * @see PodsData::fetch
2266
  *
2267
- * @param int $id ID or slug of the item to fetch
2268
- * @param bool $explicit_set Whether to set explicitly (use false when in loop)
2269
  *
2270
  * @return array An array of fields from the row
2271
  *
2272
- * @since 2.0
2273
- * @link http://pods.io/docs/fetch/
2274
  */
2275
- public function fetch ( $id = null, $explicit_set = true ) {
2276
- $this->do_hook( 'fetch', $id );
2277
-
2278
- if ( !empty( $id ) )
 
 
 
 
 
 
 
 
 
2279
  $this->params = array();
 
2280
 
2281
  $this->data->fetch( $id, $explicit_set );
2282
 
@@ -2286,17 +2758,26 @@ class Pods implements Iterator {
2286
  /**
2287
  * (Re)set the MySQL result pointer
2288
  *
2289
- * @see PodsData::reset
2290
  *
2291
- * @param int $row ID of the row to reset to
2292
  *
2293
  * @return \Pods The pod object
2294
  *
2295
- * @since 2.0
2296
- * @link http://pods.io/docs/reset/
2297
  */
2298
- public function reset ( $row = null ) {
2299
- $this->do_hook( 'reset', $row );
 
 
 
 
 
 
 
 
 
2300
 
2301
  $this->data->reset( $row );
2302
 
@@ -2308,14 +2789,15 @@ class Pods implements Iterator {
2308
  *
2309
  * This is different than the total number of rows found in the database, which you can get with total_found().
2310
  *
2311
- * @see PodsData::total
2312
  *
2313
  * @return int Number of rows returned by find(), based on the 'limit' parameter set
2314
- * @since 2.0
2315
- * @link http://pods.io/docs/total/
2316
  */
2317
- public function total () {
2318
- $this->do_hook( 'total' );
 
2319
 
2320
  $this->data->total();
2321
 
@@ -2329,14 +2811,22 @@ class Pods implements Iterator {
2329
  *
2330
  * This is different than the total number of rows limited by the current call, which you can get with total().
2331
  *
2332
- * @see PodsData::total_found
2333
  *
2334
  * @return int Number of rows returned by find(), regardless of the 'limit' parameter
2335
- * @since 2.0
2336
- * @link http://pods.io/docs/total-found/
2337
  */
2338
- public function total_found () {
2339
- $this->do_hook( 'total_found' );
 
 
 
 
 
 
 
 
2340
 
2341
  $this->data->total_found();
2342
 
@@ -2348,9 +2838,9 @@ class Pods implements Iterator {
2348
  /**
2349
  * Fetch the total number of pages, based on total rows found and the last find() limit
2350
  *
2351
- * @param null|int $limit Rows per page
2352
- * @param null|int $offset Offset of rows
2353
- * @param null|int $total Total rows
2354
  *
2355
  * @return int Number of pages
2356
  * @since 2.3.10
@@ -2371,21 +2861,20 @@ class Pods implements Iterator {
2371
  $total = $this->total_found();
2372
  }
2373
 
2374
- $total_pages = ceil( ( $total - $offset ) / $limit );
2375
-
2376
- return $total_pages;
2377
 
2378
  }
2379
 
2380
  /**
2381
  * Fetch the zebra switch
2382
  *
2383
- * @see PodsData::zebra
2384
  *
2385
  * @return bool Zebra state
2386
  * @since 1.12
2387
  */
2388
- public function zebra () {
 
2389
  $this->do_hook( 'zebra' );
2390
 
2391
  return $this->data->zebra();
@@ -2394,14 +2883,15 @@ class Pods implements Iterator {
2394
  /**
2395
  * Fetch the nth state
2396
  *
2397
- * @see PodsData::nth
2398
  *
2399
- * @param int|string $nth The $nth to match on the PodsData::row_number
2400
  *
2401
  * @return bool Whether $nth matches
2402
- * @since 2.3
2403
  */
2404
- public function nth ( $nth = null ) {
 
2405
  $this->do_hook( 'nth', $nth );
2406
 
2407
  return $this->data->nth( $nth );
@@ -2410,12 +2900,13 @@ class Pods implements Iterator {
2410
  /**
2411
  * Fetch the current position in the loop (starting at 1)
2412
  *
2413
- * @see PodsData::position
2414
  *
2415
  * @return int Current row number (+1)
2416
- * @since 2.3
2417
  */
2418
- public function position () {
 
2419
  $this->do_hook( 'position' );
2420
 
2421
  return $this->data->position();
@@ -2427,48 +2918,53 @@ class Pods implements Iterator {
2427
  *
2428
  * You may be looking for save() in most cases where you're setting a specific field.
2429
  *
2430
- * @see PodsAPI::save_pod_item
2431
  *
2432
- * @param array|string $data Either an associative array of field information or a field name
2433
- * @param mixed $value (optional) Value of the field, if $data is a field name
2434
  *
2435
  * @return int The item ID
2436
  *
2437
- * @since 2.0
2438
- * @link http://pods.io/docs/add/
2439
  */
2440
- public function add ( $data = null, $value = null ) {
2441
- if ( null !== $value )
 
2442
  $data = array( $data => $value );
 
2443
 
2444
  $data = (array) $this->do_hook( 'add', $data );
2445
 
2446
- if ( empty( $data ) )
2447
  return 0;
 
2448
 
2449
  $params = array(
2450
- 'pod' => $this->pod,
2451
- 'data' => $data,
2452
- 'allow_custom_fields' => true
2453
  );
2454
 
2455
  return $this->api->save_pod_item( $params );
2456
  }
2457
 
2458
  /**
2459
- * Add an item to the values of a relationship field, add a value to a number field (field+1), add time to a date field, or add text to a text field
 
2460
  *
2461
- * @see PodsAPI::save_pod_item
2462
  *
2463
- * @param string $field Field name
2464
- * @param mixed $value ID(s) to add, int|float to add to number field, string for dates (+1 week), or string for text
2465
- * @param int $id (optional) ID of the pod item to update
2466
  *
2467
  * @return int The item ID
2468
  *
2469
- * @since 2.3
2470
  */
2471
- public function add_to ( $field, $value, $id = null ) {
 
2472
  $pod =& $this;
2473
 
2474
  $fetch = false;
@@ -2476,99 +2972,108 @@ class Pods implements Iterator {
2476
  if ( null === $id ) {
2477
  $fetch = true;
2478
 
2479
- $id = $this->id();
2480
- }
2481
- elseif ( $id != $this->id() )
2482
  $pod = pods( $this->pod, $id );
 
2483
 
2484
  $this->do_hook( 'add_to', $field, $value, $id );
2485
 
2486
- if ( !isset( $this->fields[ $field ] ) )
2487
  return $id;
 
2488
 
2489
- // Tableless fields
2490
- if ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::tableless_field_types() ) ) {
2491
- if ( !is_array( $value ) )
2492
  $value = explode( ',', $value );
 
2493
 
2494
- if ( 'pick' == $this->fields[ $field ][ 'type' ] && in_array( $this->fields[ $field ][ 'pick_object' ], PodsForm::field_method( 'pick', 'simple_objects' ) ) ) {
2495
  $current_value = $pod->raw( $field );
2496
 
2497
- if ( !empty( $current_value ) || ( !is_array( $current_value ) && 0 < strlen( $current_value ) ) )
2498
  $current_value = (array) $current_value;
2499
- else
2500
  $current_value = array();
 
2501
 
2502
  $value = array_merge( $current_value, $value );
2503
- }
2504
- else {
2505
- $related_ids = $this->api->lookup_related_items( $this->fields[ $field ][ 'id' ], $this->pod_data[ 'id' ], $id, $this->fields[ $field ], $this->pod_data );
2506
 
2507
  foreach ( $value as $k => $v ) {
2508
- if ( !preg_match( '/[^0-9]/', $v ) )
2509
  $value[ $k ] = (int) $v;
 
2510
  }
2511
 
2512
  $value = array_merge( $related_ids, $value );
2513
- }
2514
 
2515
- if ( !empty( $value ) )
2516
  $value = array_filter( array_unique( $value ) );
2517
- else
2518
  $value = array();
 
2519
 
2520
- if ( empty( $value ) )
2521
  return $id;
2522
- }
2523
- // Number fields
2524
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::number_field_types() ) ) {
2525
  $current_value = (float) $pod->raw( $field );
2526
 
2527
  $value = ( $current_value + (float) $value );
2528
- }
2529
- // Date fields
2530
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::date_field_types() ) ) {
2531
  $current_value = $pod->raw( $field );
2532
 
2533
- if ( 0 < strlen( $current_value ) )
2534
  $value = strtotime( $value, strtotime( $current_value ) );
2535
- else
2536
  $value = strtotime( $value );
2537
- }
2538
- // Text fields
2539
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::text_field_types() ) ) {
2540
  $current_value = $pod->raw( $field );
2541
 
2542
- if ( 0 < strlen( $current_value ) )
2543
  $value = $current_value . $value;
2544
- }
2545
-
2546
- // @todo handle object fields and taxonomies
2547
 
 
2548
  $params = array(
2549
- 'pod' => $this->pod,
2550
- 'id' => $id,
2551
  'data' => array(
2552
- $field => $value
2553
- )
2554
  );
2555
 
2556
  $id = $this->api->save_pod_item( $params );
2557
 
2558
- if ( 0 < $id && $fetch )
 
 
 
2559
  $pod->fetch( $id, false );
 
2560
 
2561
  return $id;
2562
  }
2563
 
2564
  /**
2565
- * Remove an item from the values of a relationship field, remove a value from a number field (field-1), remove time to a date field
 
2566
  *
2567
- * @see PodsAPI::save_pod_item
2568
  *
2569
- * @param string $field Field name
2570
- * @param mixed $value ID(s) to add, int|float to add to number field, string for dates (-1 week), or string for text
2571
- * @param int $id (optional) ID of the pod item to update
2572
  *
2573
  * @return int The item ID
2574
  *
@@ -2584,76 +3089,73 @@ class Pods implements Iterator {
2584
  $fetch = true;
2585
 
2586
  $id = $this->id();
2587
- }
2588
- elseif ( $id != $this->id() ) {
2589
  $pod = pods( $this->pod, $id );
2590
  }
2591
 
2592
  $this->do_hook( 'remove_from', $field, $value, $id );
2593
 
2594
- if ( !isset( $this->fields[ $field ] ) ) {
2595
  return $id;
2596
  }
2597
 
2598
- // Tableless fields
2599
- if ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::tableless_field_types() ) ) {
2600
  if ( empty( $value ) ) {
2601
  $value = array();
2602
  }
2603
 
2604
- if ( !empty( $value ) ) {
2605
- if ( !is_array( $value ) ) {
2606
  $value = explode( ',', $value );
2607
  }
2608
 
2609
- if ( 'pick' == $this->fields[ $field ][ 'type' ] && in_array( $this->fields[ $field ][ 'pick_object' ], PodsForm::field_method( 'pick', 'simple_objects' ) ) ) {
2610
  $current_value = $pod->raw( $field );
2611
 
2612
- if ( !empty( $current_value ) ) {
2613
  $current_value = (array) $current_value;
2614
  }
2615
 
2616
  foreach ( $current_value as $k => $v ) {
 
2617
  if ( in_array( $v, $value ) ) {
2618
  unset( $current_value[ $k ] );
2619
  }
2620
  }
2621
 
2622
  $value = $current_value;
2623
- }
2624
- else {
2625
- $related_ids = $this->api->lookup_related_items( $this->fields[ $field ][ 'id' ], $this->pod_data[ 'id' ], $id, $this->fields[ $field ], $this->pod_data );
2626
 
2627
  foreach ( $value as $k => $v ) {
2628
- if ( !preg_match( '/[^0-9]/', $v ) ) {
2629
  $value[ $k ] = (int) $v;
2630
  }
2631
- // @todo Convert slugs into IDs
2632
- else {
2633
 
2634
- }
2635
  }
2636
 
2637
  foreach ( $related_ids as $k => $v ) {
 
2638
  if ( in_array( $v, $value ) ) {
2639
  unset( $related_ids[ $k ] );
2640
  }
2641
  }
2642
 
2643
  $value = $related_ids;
2644
- }
2645
 
2646
- if ( !empty( $value ) ) {
2647
  $value = array_filter( array_unique( $value ) );
2648
- }
2649
- else {
2650
  $value = array();
2651
  }
2652
- }
2653
- }
2654
- // Number fields
2655
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::number_field_types() ) ) {
2656
- // Date fields don't support empty for removing
2657
  if ( empty( $value ) ) {
2658
  return $id;
2659
  }
@@ -2661,10 +3163,9 @@ class Pods implements Iterator {
2661
  $current_value = (float) $pod->raw( $field );
2662
 
2663
  $value = ( $current_value - (float) $value );
2664
- }
2665
- // Date fields
2666
- elseif ( in_array( $this->fields[ $field ][ 'type' ], PodsForm::date_field_types() ) ) {
2667
- // Date fields don't support empty for removing
2668
  if ( empty( $value ) ) {
2669
  return $id;
2670
  }
@@ -2673,27 +3174,28 @@ class Pods implements Iterator {
2673
 
2674
  if ( 0 < strlen( $current_value ) ) {
2675
  $value = strtotime( $value, strtotime( $current_value ) );
2676
- }
2677
- else {
2678
  $value = strtotime( $value );
2679
  }
2680
 
2681
  $value = date_i18n( 'Y-m-d h:i:s', $value );
2682
- }
2683
-
2684
- // @todo handle object fields and taxonomies
2685
 
 
2686
  $params = array(
2687
- 'pod' => $this->pod,
2688
- 'id' => $id,
2689
  'data' => array(
2690
- $field => $value
2691
- )
2692
  );
2693
 
2694
  $id = $this->api->save_pod_item( $params );
2695
 
2696
  if ( 0 < $id && $fetch ) {
 
 
 
2697
  $pod->fetch( $id, false );
2698
  }
2699
 
@@ -2707,62 +3209,73 @@ class Pods implements Iterator {
2707
  * Though this function has the capacity to add new items, best practice should direct you
2708
  * to use add() for that instead.
2709
  *
2710
- * @see PodsAPI::save_pod_item
2711
  *
2712
- * @param array|string $data Either an associative array of field information or a field name
2713
- * @param mixed $value (optional) Value of the field, if $data is a field name
2714
- * @param int $id (optional) ID of the pod item to update
2715
- * @param array $params (optional) Additional params to send to save_pod_item
2716
  *
2717
  * @return int The item ID
2718
  *
2719
- * @since 2.0
2720
- * @link http://pods.io/docs/save/
2721
  */
2722
- public function save ( $data = null, $value = null, $id = null, $params = null ) {
2723
- if ( null !== $value )
 
2724
  $data = array( $data => $value );
 
2725
 
2726
  $fetch = false;
2727
 
2728
- if ( null === $id ) {
 
2729
  $fetch = true;
2730
 
2731
- $id = $this->id();
 
 
2732
  }
2733
 
2734
  $data = (array) $this->do_hook( 'save', $data, $id );
2735
 
2736
- if ( empty( $data ) )
2737
  return $id;
 
2738
 
2739
  $default = array();
2740
 
2741
- if ( !empty( $params ) && is_array( $params ) )
2742
  $default = $params;
 
2743
 
2744
  $params = array(
2745
- 'pod' => $this->pod,
2746
- 'id' => $id,
2747
- 'data' => $data,
2748
  'allow_custom_fields' => true,
2749
- 'clear_slug_cache' => false
2750
  );
2751
 
2752
- if ( !empty( $default ) )
2753
  $params = array_merge( $params, $default );
 
2754
 
2755
  $id = $this->api->save_pod_item( $params );
2756
 
2757
- if ( 0 < $id && $fetch )
 
 
 
2758
  $this->fetch( $id, false );
 
2759
 
2760
- if ( !empty( $this->pod_data[ 'field_slug' ] ) ) {
2761
  if ( 0 < $id && $fetch ) {
2762
- $slug = $this->field( $this->pod_data[ 'field_slug' ] );
2763
- }
2764
- else {
2765
- $slug = pods( $this->pod, $id )->field( $this->pod_data[ 'field_slug' ] );
2766
  }
2767
 
2768
  if ( 0 < strlen( $slug ) ) {
@@ -2776,27 +3289,30 @@ class Pods implements Iterator {
2776
  /**
2777
  * Delete an item
2778
  *
2779
- * @see PodsAPI::delete_pod_item
2780
  *
2781
- * @param int $id ID of the Pod item to delete
2782
  *
2783
  * @return bool Whether the item was successfully deleted
2784
  *
2785
- * @since 2.0
2786
- * @link http://pods.io/docs/delete/
2787
  */
2788
- public function delete ( $id = null ) {
2789
- if ( null === $id )
 
2790
  $id = $this->id();
 
2791
 
2792
  $id = (int) $this->do_hook( 'delete', $id );
2793
 
2794
- if ( empty( $id ) )
2795
  return false;
 
2796
 
2797
  $params = array(
2798
  'pod' => $this->pod,
2799
- 'id' => $id
2800
  );
2801
 
2802
  return $this->api->delete_pod_item( $params );
@@ -2805,20 +3321,21 @@ class Pods implements Iterator {
2805
  /**
2806
  * Reset Pod
2807
  *
2808
- * @see PodsAPI::reset_pod
2809
  *
2810
  * @return bool Whether the Pod was successfully reset
2811
  *
2812
  * @since 2.1.1
2813
  */
2814
- public function reset_pod () {
 
2815
  $params = array( 'id' => $this->pod_id );
2816
 
2817
- $this->data->id = null;
2818
- $this->data->row = array();
2819
  $this->data->data = array();
2820
 
2821
- $this->data->total = 0;
2822
  $this->data->total_found = 0;
2823
 
2824
  return $this->api->reset_pod( $params );
@@ -2827,27 +3344,30 @@ class Pods implements Iterator {
2827
  /**
2828
  * Duplicate an item
2829
  *
2830
- * @see PodsAPI::duplicate_pod_item
2831
  *
2832
- * @param int $id ID of the pod item to duplicate
2833
  *
2834
  * @return int|bool ID of the new pod item
2835
  *
2836
- * @since 2.0
2837
- * @link http://pods.io/docs/duplicate/
2838
  */
2839
- public function duplicate ( $id = null ) {
2840
- if ( null === $id )
 
2841
  $id = $this->id();
 
2842
 
2843
  $id = (int) $this->do_hook( 'duplicate', $id );
2844
 
2845
- if ( empty( $id ) )
2846
  return false;
 
2847
 
2848
  $params = array(
2849
  'pod' => $this->pod,
2850
- 'id' => $id
2851
  );
2852
 
2853
  return $this->api->duplicate_pod_item( $params );
@@ -2856,64 +3376,71 @@ class Pods implements Iterator {
2856
  /**
2857
  * Import data / Save multiple rows of data at once
2858
  *
2859
- * @see PodsAPI::import
2860
  *
2861
- * @param mixed $import_data PHP associative array or CSV input
2862
- * @param bool $numeric_mode Use IDs instead of the name field when matching
2863
- * @param string $format Format of import data, options are php or csv
2864
  *
2865
  * @return array IDs of imported items
2866
  *
2867
- * @since 2.3
2868
  */
2869
- public function import ( $import_data, $numeric_mode = false, $format = null ) {
 
2870
  return $this->api->import( $import_data, $numeric_mode, $format );
2871
  }
2872
 
2873
  /**
2874
  * Export an item's data
2875
  *
2876
- * @see PodsAPI::export_pod_item
2877
  *
2878
- * @param array $fields (optional) Fields to export
2879
- * @param int $id (optional) ID of the pod item to export
 
2880
  *
2881
  * @return array|bool Data array of the exported pod item
2882
  *
2883
- * @since 2.0
2884
- * @link http://pods.io/docs/export/
2885
  */
2886
- public function export ( $fields = null, $id = null, $format = null ) {
 
2887
  $params = array(
2888
- 'pod' => $this->pod,
2889
- 'id' => $id,
2890
- 'fields' => null,
2891
- 'depth' => 2,
2892
- 'flatten' => false
 
 
2893
  );
2894
 
2895
- if ( is_array( $fields ) && ( isset( $fields[ 'fields' ] ) || isset( $fields[ 'depth' ] ) ) )
2896
  $params = array_merge( $params, $fields );
2897
- else
2898
- $params[ 'fields' ] = $fields;
 
2899
 
2900
- if ( isset( $params[ 'fields' ] ) && is_array( $params[ 'fields' ] ) && !in_array( $this->pod_data[ 'field_id' ], $params[ 'fields' ] ) )
2901
- $params[ 'fields' ] = array_merge( array( $this->pod_data[ 'field_id' ] ), $params[ 'fields' ] );
 
2902
 
2903
- if ( null === $params[ 'id' ] )
2904
- $params[ 'id' ] = $this->id();
 
2905
 
2906
  $params = (array) $this->do_hook( 'export', $params );
2907
 
2908
- if ( empty( $params[ 'id' ] ) )
2909
  return false;
 
2910
 
2911
  $data = $this->api->export_pod_item( $params );
2912
 
2913
- if ( !empty( $format ) ) {
2914
- if ( 'json' == $format )
2915
- $data = json_encode( (array) $data );
2916
- // @todo more formats
2917
  }
2918
 
2919
  return $data;
@@ -2922,25 +3449,27 @@ class Pods implements Iterator {
2922
  /**
2923
  * Export data from all items
2924
  *
2925
- * @see PodsAPI::export
2926
  *
2927
- * @param array $params An associative array of parameters
2928
  *
2929
  * @return array Data arrays of all exported pod items
2930
  *
2931
- * @since 2.3
2932
  */
2933
- public function export_data ( $params = null ) {
 
2934
  $defaults = array(
2935
  'fields' => null,
2936
- 'depth' => 2,
2937
- 'params' => null
2938
  );
2939
 
2940
- if ( empty( $params ) )
2941
  $params = $defaults;
2942
- else
2943
  $params = array_merge( $defaults, (array) $params );
 
2944
 
2945
  return $this->api->export( $this, $params );
2946
  }
@@ -2950,24 +3479,23 @@ class Pods implements Iterator {
2950
  * are simple, paginate and advanced. The base and format parameters
2951
  * are used only for the paginate view.
2952
  *
2953
- * @var array $params Associative array of parameters
2954
  *
2955
  * @return string Pagination HTML
2956
- * @since 2.0
2957
- * @link http://pods.io/docs/pagination/
2958
  */
2959
  public function pagination( $params = null ) {
2960
 
2961
  if ( empty( $params ) ) {
2962
  $params = array();
2963
- }
2964
- elseif ( !is_array( $params ) ) {
2965
  $params = array( 'label' => $params );
2966
  }
2967
 
2968
- $this->page_var = pods_var_raw( 'page_var', $params, $this->page_var );
2969
 
2970
- $url = pods_var_update( null, null, $this->page_var );
2971
 
2972
  $append = '?';
2973
 
@@ -2976,39 +3504,43 @@ class Pods implements Iterator {
2976
  }
2977
 
2978
  $defaults = array(
2979
- 'type' => 'advanced',
2980
- 'label' => __( 'Go to page:', 'pods' ),
2981
- 'show_label' => true,
2982
- 'first_text' => __( '&laquo; First', 'pods' ),
2983
- 'prev_text' => __( '&lsaquo; Previous', 'pods' ),
2984
- 'next_text' => __( 'Next &rsaquo;', 'pods' ),
2985
- 'last_text' => __( 'Last &raquo;', 'pods' ),
2986
- 'prev_next' => true,
2987
- 'first_last' => true,
2988
- 'limit' => (int) $this->limit,
2989
- 'offset' => (int) $this->offset,
2990
- 'page' => max( 1, (int) $this->page ),
2991
- 'mid_size' => 2,
2992
- 'end_size' => 1,
2993
  'total_found' => $this->total_found(),
2994
- 'page_var' => $this->page_var,
2995
- 'base' => "{$url}{$append}%_%",
2996
- 'format' => "{$this->page_var}=%#%",
2997
- 'class' => '',
2998
- 'link_class' => ''
2999
  );
3000
 
3001
- $params = (object) array_merge( $defaults, $params );
 
 
 
 
3002
 
3003
  $params->total = $this->total_pages( $params->limit, $params->offset, $params->total_found );
3004
 
3005
- if ( $params->limit < 1 || $params->total_found < 1 || 1 == $params->total || $params->total_found <= $params->offset ) {
3006
  return $this->do_hook( 'pagination', $this->do_hook( 'pagination_' . $params->type, '', $params ), $params );
3007
  }
3008
 
3009
  $pagination = $params->type;
3010
 
3011
- if ( !in_array( $params->type, array( 'simple', 'advanced', 'paginate', 'list' ) ) ) {
3012
  $pagination = 'advanced';
3013
  }
3014
 
@@ -3025,42 +3557,52 @@ class Pods implements Iterator {
3025
  /**
3026
  * Return a filter form for searching a Pod
3027
  *
3028
- * @var array|string $params Comma-separated list of fields or array of parameters
3029
  *
3030
  * @return string Filters HTML
3031
  *
3032
- * @since 2.0
3033
- * @link http://pods.io/docs/filters/
3034
  */
3035
- public function filters ( $params = null ) {
 
3036
  $defaults = array(
3037
  'fields' => $params,
3038
- 'label' => '',
3039
  'action' => '',
3040
- 'search' => ''
3041
  );
3042
 
3043
- if ( is_array( $params ) )
3044
  $params = array_merge( $defaults, $params );
3045
- else
3046
  $params = $defaults;
 
3047
 
3048
  $pod =& $this;
3049
 
 
 
 
 
 
 
3050
  $params = apply_filters( 'pods_filters_params', $params, $pod );
3051
 
3052
- $fields = $params[ 'fields' ];
3053
 
3054
- if ( null !== $fields && !is_array( $fields ) && 0 < strlen( $fields ) )
3055
  $fields = explode( ',', $fields );
 
3056
 
3057
- $object_fields = (array) pods_var_raw( 'object_fields', $this->pod_data, array(), null, true );
3058
 
3059
- // Force array
3060
- if ( empty( $fields ) )
3061
  $fields = array();
3062
- else {
3063
- $filter_fields = $fields; // Temporary
 
3064
 
3065
  $fields = array();
3066
 
@@ -3068,46 +3610,54 @@ class Pods implements Iterator {
3068
  $name = $k;
3069
 
3070
  $defaults = array(
3071
- 'name' => $name
3072
  );
3073
 
3074
- if ( !is_array( $field ) ) {
3075
  $name = $field;
3076
 
3077
  $field = array(
3078
- 'name' => $name
3079
  );
3080
  }
3081
 
 
3082
  $field = array_merge( $defaults, $field );
3083
 
3084
- $field[ 'name' ] = trim( $field[ 'name' ] );
3085
 
3086
- if ( pods_var_raw( 'hidden', $field, false, null, true ) )
3087
- $field[ 'type' ] = 'hidden';
 
3088
 
3089
- if ( isset( $object_fields[ $field[ 'name' ] ] ) )
3090
- $fields[ $field[ 'name' ] ] = array_merge( $object_fields[ $field[ 'name' ] ], $field );
3091
- elseif ( isset( $this->fields[ $field[ 'name' ] ] ) )
3092
- $fields[ $field[ 'name' ] ] = array_merge( $this->fields[ $field[ 'name' ] ], $field );
3093
- }
 
 
 
3094
 
3095
- unset( $filter_fields ); // Cleanup
3096
- }
 
3097
 
3098
  $this->filters = array_keys( $fields );
3099
 
3100
- $label = $params[ 'label' ];
3101
 
3102
- if ( strlen( $label ) < 1 )
3103
  $label = __( 'Search', 'pods' );
 
3104
 
3105
- $action = $params[ 'action' ];
3106
 
3107
- $search = trim( $params[ 'search' ] );
3108
 
3109
- if ( strlen( $search ) < 1 )
3110
- $search = pods_var_raw( $pod->search_var, 'get', '' );
 
3111
 
3112
  ob_start();
3113
 
@@ -3115,42 +3665,106 @@ class Pods implements Iterator {
3115
 
3116
  $output = ob_get_clean();
3117
 
3118
- return $this->do_hook( 'filters', $output, $params );
 
 
 
 
 
 
 
 
 
3119
  }
3120
 
3121
  /**
3122
  * Run a helper within a Pod Page or WP Template
3123
  *
3124
- * @see Pods_Helpers::helper
3125
  *
3126
- * @param string $helper Helper name
3127
- * @param string $value Value to run the helper on
3128
- * @param string $name Field name
3129
- * @internal param array $params An associative array of parameters
3130
  *
3131
  * @return mixed Anything returned by the helper
3132
- * @since 2.0
3133
  */
3134
- public function helper ( $helper, $value = null, $name = null ) {
 
3135
  $params = array(
3136
- 'helper' => $helper,
3137
- 'value' => $value,
3138
- 'name' => $name,
3139
- 'deprecated' => false
3140
  );
3141
 
3142
- if ( class_exists( 'Pods_Templates' ) )
3143
- $params[ 'deprecated' ] = Pods_Templates::$deprecated;
 
3144
 
3145
- if ( is_array( $helper ) )
3146
  $params = array_merge( $params, $helper );
 
3147
 
3148
- if ( class_exists( 'Pods_Helpers' ) )
3149
  $value = Pods_Helpers::helper( $params, $this );
3150
- elseif ( function_exists( $params[ 'helper' ] ) )
3151
- $value = call_user_func( $params[ 'helper' ], $value );
3152
- else
3153
- $value = apply_filters( $params[ 'helper' ], $value );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3154
 
3155
  return $value;
3156
  }
@@ -3158,73 +3772,130 @@ class Pods implements Iterator {
3158
  /**
3159
  * Display the page template
3160
  *
3161
- * @see Pods_Templates::template
3162
  *
3163
- * @param string $template The template name
3164
- * @param string $code Custom template code to use instead
3165
- * @param bool $deprecated Whether to use deprecated functionality based on old function usage
3166
  *
3167
  * @return mixed Template output
3168
  *
3169
- * @since 2.0
3170
- * @link http://pods.io/docs/template/
3171
  */
3172
- public function template ( $template_name, $code = null, $deprecated = false ) {
 
3173
  $out = null;
3174
 
3175
  $obj =& $this;
3176
 
3177
- if ( !empty( $code ) ) {
3178
- $code = str_replace( '$this->', '$obj->', $code ); // backwards compatibility
3179
-
 
 
 
 
 
 
 
 
3180
  $code = apply_filters( 'pods_templates_pre_template', $code, $template_name, $this );
 
 
 
 
 
 
 
 
3181
  $code = apply_filters( "pods_templates_pre_template_{$template_name}", $code, $template_name, $this );
3182
 
3183
  ob_start();
3184
 
3185
- if ( !empty( $code ) ) {
3186
- // Only detail templates need $this->id
3187
  if ( empty( $this->id ) ) {
3188
  while ( $this->fetch() ) {
 
3189
  echo $this->do_magic_tags( $code );
3190
  }
3191
- }
3192
- else
3193
  echo $this->do_magic_tags( $code );
 
3194
  }
3195
 
3196
  $out = ob_get_clean();
3197
 
 
 
 
 
 
 
 
 
3198
  $out = apply_filters( 'pods_templates_post_template', $out, $code, $template_name, $this );
 
 
 
 
 
 
 
 
 
3199
  $out = apply_filters( "pods_templates_post_template_{$template_name}", $out, $code, $template_name, $this );
3200
- }
3201
- elseif ( class_exists( 'Pods_Templates' ) )
3202
  $out = Pods_Templates::template( $template_name, $code, $this, $deprecated );
3203
- elseif ( $template_name == trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template_name ), ' /-' ) ) {
3204
  ob_start();
3205
 
3206
  $default_templates = array(
3207
  'pods/' . $template_name,
3208
  'pods-' . $template_name,
3209
- $template_name
3210
  );
3211
 
 
 
 
 
 
3212
  $default_templates = apply_filters( 'pods_template_default_templates', $default_templates );
3213
 
3214
- // Only detail templates need $this->id
3215
  if ( empty( $this->id ) ) {
3216
  while ( $this->fetch() ) {
3217
  pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
3218
  }
3219
- }
3220
- else
3221
  pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
 
3222
 
3223
  $out = ob_get_clean();
3224
 
 
 
 
 
 
 
 
 
3225
  $out = apply_filters( 'pods_templates_post_template', $out, $code, $template_name, $this );
 
 
 
 
 
 
 
 
 
3226
  $out = apply_filters( "pods_templates_post_template_{$template_name}", $out, $code, $template_name, $this );
3227
- }
3228
 
3229
  return $out;
3230
  }
@@ -3234,44 +3905,48 @@ class Pods implements Iterator {
3234
  * and override options where needed. For WP object based Pods, you can pass through the WP object
3235
  * field names too, such as "post_title" or "post_content" for example.
3236
  *
3237
- * @param array $params (optional) Fields to show on the form, defaults to all fields
3238
- * @param string $label (optional) Save button label, defaults to "Save Changes"
3239
- * @param string $thank_you (optional) Thank you URL to send to upon success
3240
  *
3241
  * @return bool|mixed
3242
- * @since 2.0
3243
- * @link http://pods.io/docs/form/
3244
  */
3245
- public function form ( $params = null, $label = null, $thank_you = null ) {
 
3246
  $defaults = array(
3247
- 'fields' => $params,
3248
- 'label' => $label,
3249
- 'thank_you' => $thank_you,
3250
- 'fields_only' => false
3251
  );
3252
 
3253
- if ( is_array( $params ) )
3254
  $params = array_merge( $defaults, $params );
3255
- else
3256
  $params = $defaults;
 
3257
 
3258
  $pod =& $this;
3259
 
3260
  $params = $this->do_hook( 'form_params', $params );
3261
 
3262
- $fields = $params[ 'fields' ];
3263
 
3264
- if ( null !== $fields && !is_array( $fields ) && 0 < strlen( $fields ) )
3265
  $fields = explode( ',', $fields );
 
3266
 
3267
- $object_fields = (array) pods_var_raw( 'object_fields', $this->pod_data, array(), null, true );
3268
 
3269
  if ( empty( $fields ) ) {
3270
- // Add core object fields if $fields is empty
3271
  $fields = array_merge( $object_fields, $this->fields );
3272
  }
3273
 
3274
- $form_fields = $fields; // Temporary
 
3275
 
3276
  $fields = array();
3277
 
@@ -3279,100 +3954,111 @@ class Pods implements Iterator {
3279
  $name = $k;
3280
 
3281
  $defaults = array(
3282
- 'name' => $name
3283
  );
3284
 
3285
- if ( !is_array( $field ) ) {
3286
  $name = $field;
3287
 
3288
  $field = array(
3289
- 'name' => $name
3290
  );
3291
  }
3292
 
 
3293
  $field = array_merge( $defaults, $field );
3294
 
3295
- $field[ 'name' ] = trim( $field[ 'name' ] );
3296
 
3297
- $default_value = pods_var_raw( 'default', $field );
3298
- $value = pods_var_raw( 'value', $field );
3299
 
3300
- if ( empty( $field[ 'name' ] ) )
3301
- $field[ 'name' ] = trim( $name );
3302
-
3303
- if ( isset( $object_fields[ $field[ 'name' ] ] ) ) {
3304
- $field = array_merge( $object_fields[ $field[ 'name' ] ], $field );
3305
  }
3306
- elseif ( isset( $this->fields[ $field[ 'name' ] ] ) ) {
3307
- $field = array_merge( $this->fields[ $field[ 'name' ] ], $field );
 
 
 
 
 
3308
  }
3309
 
3310
- if ( pods_var_raw( 'hidden', $field, false, null, true ) )
3311
- $field[ 'type' ] = 'hidden';
 
3312
 
3313
- $fields[ $field[ 'name' ] ] = $field;
3314
 
3315
  if ( empty( $this->id ) && null !== $default_value ) {
3316
- $this->row_override[ $field[ 'name' ] ] = $default_value;
3317
- }
3318
- elseif ( !empty( $this->id ) && null !== $value ) {
3319
- $this->row[ $field[ 'name' ] ] = $value;
3320
  }
3321
- }
3322
 
3323
- unset( $form_fields ); // Cleanup
 
3324
 
3325
  $fields = $this->do_hook( 'form_fields', $fields, $params );
3326
 
3327
- $label = $params[ 'label' ];
3328
 
3329
- if ( empty( $label ) )
3330
  $label = __( 'Save Changes', 'pods' );
 
3331
 
3332
- $thank_you = $params[ 'thank_you' ];
3333
- $fields_only = $params[ 'fields_only' ];
3334
 
3335
- PodsForm::$form_counter++;
3336
 
3337
  ob_start();
3338
 
3339
  if ( empty( $thank_you ) ) {
3340
  $success = 'success';
3341
 
3342
- if ( 1 < PodsForm::$form_counter )
3343
  $success .= PodsForm::$form_counter;
 
3344
 
3345
- $thank_you = pods_var_update( array( 'success*' => null, $success => 1 ) );
 
 
 
3346
 
3347
- if ( 1 == pods_var( $success, 'get', 0 ) ) {
3348
  $message = __( 'Form submitted successfully', 'pods' );
 
3349
  /**
3350
- * Change the text of the message that appears on succesful form submission.
3351
  *
3352
- * @param string $message
3353
  *
3354
- * @returns string the message
3355
- *
3356
- * @since 3.0.0
3357
  */
3358
  $message = apply_filters( 'pods_pod_form_success_message', $message );
3359
 
3360
- echo '<div id="message" class="pods-form-front-success">' . $message . '</div>';
3361
  }
3362
- }
3363
 
3364
  pods_view( PODS_DIR . 'ui/front/form.php', compact( array_keys( get_defined_vars() ) ) );
3365
 
3366
  $output = ob_get_clean();
3367
 
3368
- if ( empty( $this->id ) )
3369
  $this->row_override = array();
 
3370
 
3371
  return $this->do_hook( 'form', $output, $fields, $label, $thank_you, $this, $this->id() );
3372
  }
3373
 
3374
  /**
3375
- * @param array $fields (optional) Fields to show in the view, defaults to all fields
 
 
3376
  *
3377
  * @return mixed
3378
  * @since 2.3.10
@@ -3381,60 +4067,64 @@ class Pods implements Iterator {
3381
 
3382
  $pod =& $this;
3383
 
3384
- // Convert comma separated list of fields to an array
3385
- if ( null !== $fields && !is_array( $fields ) && 0 < strlen( $fields ) ) {
3386
  $fields = explode( ',', $fields );
3387
  }
3388
 
3389
  $object_fields = (array) pods_v( 'object_fields', $this->pod_data, array(), true );
3390
 
3391
  if ( empty( $fields ) ) {
3392
- // Add core object fields if $fields is empty
3393
  $fields = array_merge( $object_fields, $this->fields );
3394
  }
3395
 
3396
- $view_fields = $fields; // Temporary
 
3397
 
3398
  $fields = array();
3399
 
3400
  foreach ( $view_fields as $name => $field ) {
3401
-
3402
  $defaults = array(
3403
- 'name' => $name
3404
  );
3405
 
3406
- if ( !is_array( $field ) ) {
3407
  $name = $field;
3408
 
3409
  $field = array(
3410
- 'name' => $name
3411
  );
3412
  }
3413
 
 
3414
  $field = array_merge( $defaults, $field );
3415
 
3416
- $field[ 'name' ] = trim( $field[ 'name' ] );
3417
 
3418
- if ( empty( $field[ 'name' ] ) ) {
3419
- $field[ 'name' ] = trim( $name );
3420
  }
3421
 
3422
- if ( isset( $object_fields[ $field[ 'name' ] ] ) )
3423
- $field = array_merge( $field, $object_fields[ $field[ 'name' ] ] );
3424
- elseif ( isset( $this->fields[ $field[ 'name' ] ] ) )
3425
- $field = array_merge( $this->fields[ $field[ 'name' ] ], $field );
 
 
 
3426
 
3427
- if ( pods_v( 'hidden', $field, false, null, true ) || 'hidden' == $field[ 'type' ] ) {
3428
  continue;
3429
- }
3430
- elseif ( !PodsForm::permission( $field[ 'type' ], $field[ 'name' ], $field[ 'options' ], $fields, $pod, $pod->id() ) ) {
3431
  continue;
3432
  }
3433
 
3434
- $fields[ $field[ 'name' ] ] = $field;
3435
- }
3436
 
3437
- unset( $view_fields ); // Cleanup
 
3438
 
3439
  $output = pods_view( PODS_DIR . 'ui/front/view.php', compact( array_keys( get_defined_vars() ) ), false, 'cache', true );
3440
 
@@ -3445,73 +4135,115 @@ class Pods implements Iterator {
3445
  /**
3446
  * Replace magic tags with their values
3447
  *
3448
- * @param string $code The content to evaluate
3449
- * @param object $obj The Pods object
3450
  *
3451
  * @return string Code with Magic Tags evaluated
3452
  *
3453
- * @since 2.0
3454
  */
3455
- public function do_magic_tags ( $code ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3456
  return preg_replace_callback( '/({@(.*?)})/m', array( $this, 'process_magic_tags' ), $code );
3457
  }
3458
 
3459
  /**
3460
  * Replace magic tags with their values
3461
  *
3462
- * @param string $tag The magic tag to process
3463
- * @param object $obj The Pods object
3464
  *
3465
  * @return string Code with Magic Tags evaluated
3466
  *
3467
  * @since 2.0.2
3468
  */
3469
- private function process_magic_tags ( $tag ) {
3470
 
3471
  if ( is_array( $tag ) ) {
3472
- if ( !isset( $tag[ 2 ] ) && strlen( trim( $tag[ 2 ] ) ) < 1 )
3473
  return '';
 
3474
 
3475
- $tag = $tag[ 2 ];
3476
  }
3477
 
3478
  $tag = trim( $tag, ' {@}' );
3479
  $tag = explode( ',', $tag );
3480
 
3481
- if ( empty( $tag ) || !isset( $tag[ 0 ] ) || strlen( trim( $tag[ 0 ] ) ) < 1 )
3482
  return '';
3483
-
3484
- foreach ( $tag as $k => $v ) {
3485
- $tag[ $k ] = trim( $v );
3486
  }
3487
 
3488
- $field_name = $tag[ 0 ];
3489
 
3490
- $helper_name = $before = $after = '';
3491
 
3492
- if ( isset( $tag[ 1 ] ) && !empty( $tag[ 1 ] ) ) {
 
 
 
 
3493
  $value = $this->field( $field_name );
3494
 
3495
- $helper_name = $tag[ 1 ];
3496
 
3497
  $value = $this->helper( $helper_name, $value, $field_name );
3498
- }
3499
- else
3500
  $value = $this->display( $field_name );
 
 
 
 
 
 
 
 
 
 
 
 
3501
 
3502
- if ( isset( $tag[ 2 ] ) && !empty( $tag[ 2 ] ) )
3503
- $before = $tag[ 2 ];
 
3504
 
3505
- if ( isset( $tag[ 3 ] ) && !empty( $tag[ 3 ] ) )
3506
- $after = $tag[ 3 ];
 
3507
 
 
 
 
 
 
 
 
 
3508
  $value = apply_filters( 'pods_do_magic_tags', $value, $field_name, $helper_name, $before, $after );
3509
 
3510
- if ( is_array( $value ) )
3511
- $value = pods_serial_comma( $value, array( 'field' => $field_name, 'fields' => $this->fields ) );
 
 
 
 
3512
 
3513
- if ( null !== $value && false !== $value )
3514
  return $before . $value . $after;
 
3515
 
3516
  return '';
3517
  }
@@ -3520,41 +4252,46 @@ class Pods implements Iterator {
3520
  *
3521
  * Generate UI for Data Management
3522
  *
3523
- * @param mixed $options Array or String containing Pod or Options to be used
3524
- * @param bool $amend Whether to amend the default UI options or replace entirely
3525
  *
3526
- * @return PodsUI|void UI object or void if custom UI used
3527
  *
3528
  * @since 2.3.10
3529
  */
3530
- public function ui ( $options = null, $amend = false ) {
 
3531
  $num = '';
3532
 
3533
- if ( empty( $options ) )
3534
  $options = array();
3535
- else {
3536
- $num = pods_var( 'num', $options, '' );
3537
 
3538
  if ( empty( $num ) ) {
3539
  $num = '';
3540
  }
3541
  }
3542
 
3543
- if ( $this->id() != pods_var( 'id' . $num, 'get', null, null, true ) )
3544
- $this->fetch( pods_var( 'id' . $num, 'get', null, null, true ) );
 
 
 
 
3545
 
3546
- if ( !empty( $options ) && !$amend ) {
3547
  $this->ui = $options;
3548
 
3549
  return pods_ui( $this );
3550
- }
3551
- elseif ( !empty( $options ) || 'custom' != pods_var( 'ui_style', $this->pod_data[ 'options' ], 'post_type', null, true ) ) {
3552
- $actions_enabled = pods_var_raw( 'ui_actions_enabled', $this->pod_data[ 'options' ] );
3553
 
3554
- if ( !empty( $actions_enabled ) )
3555
  $actions_enabled = (array) $actions_enabled;
3556
- else
3557
  $actions_enabled = array();
 
3558
 
3559
  $available_actions = array(
3560
  'add',
@@ -3562,176 +4299,197 @@ class Pods implements Iterator {
3562
  'duplicate',
3563
  'delete',
3564
  'reorder',
3565
- 'export'
3566
  );
3567
 
3568
- if ( !empty( $actions_enabled ) ) {
3569
  $actions_disabled = array(
3570
- 'view' => 'view'
3571
  );
3572
 
3573
  foreach ( $available_actions as $action ) {
3574
- if ( !in_array( $action, $actions_enabled ) )
3575
  $actions_disabled[ $action ] = $action;
 
3576
  }
3577
- }
3578
- else {
3579
  $actions_disabled = array(
3580
  'duplicate' => 'duplicate',
3581
- 'view' => 'view',
3582
- 'export' => 'export'
3583
  );
3584
 
3585
- if ( 1 == pods_var( 'ui_export', $this->pod_data[ 'options' ], 0 ) )
3586
- unset( $actions_disabled[ 'export' ] );
3587
- }
 
3588
 
3589
  if ( empty( $options ) ) {
3590
  $author_restrict = false;
3591
 
3592
- if ( isset( $this->fields[ 'author' ] ) && 'pick' == $this->fields[ 'author' ][ 'type' ] && 'user' == $this->fields[ 'author' ][ 'pick_object' ] )
3593
  $author_restrict = 'author.ID';
 
3594
 
3595
- if ( !pods_is_admin( array( 'pods', 'pods_content' ) ) ) {
3596
- if ( !current_user_can( 'pods_add_' . $this->pod ) ) {
3597
- $actions_disabled[ 'add' ] = 'add';
3598
 
3599
- if ( 'add' == pods_var( 'action' . $num, 'get' ) )
3600
  $_GET[ 'action' . $num ] = 'manage';
 
3601
  }
3602
 
3603
- if ( !$author_restrict && !current_user_can( 'pods_edit_' . $this->pod ) && !current_user_can( 'pods_edit_others_' . $this->pod ) )
3604
- $actions_disabled[ 'edit' ] = 'edit';
 
3605
 
3606
- if ( !$author_restrict && !current_user_can( 'pods_delete_' . $this->pod ) && !current_user_can( 'pods_delete_others_' . $this->pod ) )
3607
- $actions_disabled[ 'delete' ] = 'delete';
 
3608
 
3609
- if ( !current_user_can( 'pods_reorder_' . $this->pod ) )
3610
- $actions_disabled[ 'reorder' ] = 'reorder';
 
3611
 
3612
- if ( !current_user_can( 'pods_export_' . $this->pod ) )
3613
- $actions_disabled[ 'export' ] = 'export';
3614
- }
3615
- }
 
3616
 
3617
- $_GET[ 'action' . $num ] = pods_var( 'action' . $num, 'get', pods_var( 'action', $options, 'manage' ) );
3618
 
3619
- $index = $this->pod_data[ 'field_id' ];
3620
  $label = __( 'ID', 'pods' );
3621
 
3622
- if ( isset( $this->pod_data[ 'fields' ][ $this->pod_data[ 'field_index' ] ] ) ) {
3623
- $index = $this->pod_data[ 'field_index' ];
3624
- $label = $this->pod_data[ 'fields' ][ $this->pod_data[ 'field_index' ] ];
3625
  }
3626
 
3627
  $manage = array(
3628
- $index => $label
3629
  );
3630
 
3631
- if ( isset( $this->pod_data[ 'fields' ][ 'modified' ] ) )
3632
- $manage[ 'modified' ] = $this->pod_data[ 'fields' ][ 'modified' ][ 'label' ];
 
3633
 
3634
- $manage_fields = pods_var_raw( 'ui_fields_manage', $this->pod_data[ 'options' ] );
3635
 
3636
- if ( !empty( $manage_fields ) ) {
3637
  $manage_new = array();
3638
 
3639
  foreach ( $manage_fields as $manage_field ) {
3640
- if ( isset( $this->pod_data[ 'fields' ][ $manage_field ] ) )
3641
- $manage_new[ $manage_field ] = $this->pod_data[ 'fields' ][ $manage_field ];
3642
- elseif ( isset( $this->pod_data[ 'object_fields' ][ $manage_field ] ) )
3643
- $manage_new[ $manage_field ] = $this->pod_data[ 'object_fields' ][ $manage_field ];
3644
- elseif ( $manage_field == $this->pod_data[ 'field_id' ] ) {
3645
  $field = array(
3646
- 'name' => $manage_field,
3647
  'label' => 'ID',
3648
- 'type' => 'number',
3649
- 'width' => '8%'
3650
  );
3651
 
3652
- $manage_new[ $manage_field ] = PodsForm::field_setup( $field, null, $field[ 'type' ] );
3653
  }
3654
  }
3655
 
3656
- if ( !empty( $manage_new ) )
3657
  $manage = $manage_new;
3658
- }
 
3659
 
3660
- $manage = apply_filters( 'pods_admin_ui_fields_' . $this->pod, apply_filters( 'pods_admin_ui_fields', $manage, $this->pod, $this ), $this->pod, $this );
 
3661
 
3662
- $icon = pods_var_raw( 'ui_icon', $this->pod_data[ 'options' ] );
3663
 
3664
- if ( !empty( $icon ) )
3665
  $icon = pods_image_url( $icon, '32x32' );
 
3666
 
3667
- $filters = pods_var_raw( 'ui_filters', $this->pod_data[ 'options' ] );
3668
 
3669
- if ( !empty( $filters ) ) {
3670
  $filters_new = array();
3671
 
 
 
3672
  foreach ( $filters as $filter_field ) {
3673
- if ( isset( $this->pod_data[ 'fields' ][ $filter_field ] ) )
3674
- $filters_new[ $filter_field ] = $this->pod_data[ 'fields' ][ $filter_field ];
3675
- elseif ( isset( $this->pod_data[ 'object_fields' ][ $filter_field ] ) )
3676
- $filters_new[ $filter_field ] = $this->pod_data[ 'object_fields' ][ $filter_field ];
 
3677
  }
3678
 
3679
  $filters = $filters_new;
3680
  }
3681
 
3682
  $ui = array(
3683
- 'fields' => array(
3684
- 'manage' => $manage,
3685
- 'add' => $this->pod_data[ 'fields' ],
3686
- 'edit' => $this->pod_data[ 'fields' ],
3687
- 'duplicate' => $this->pod_data[ 'fields' ]
3688
  ),
3689
- 'icon' => $icon,
3690
- 'actions_disabled' => $actions_disabled
 
3691
  );
3692
 
3693
- if ( !empty( $filters ) ) {
3694
- $ui[ 'fields' ][ 'search' ] = $filters;
3695
- $ui[ 'filters' ] = array_keys( $filters );
3696
- $ui[ 'filters_enhanced' ] = true;
3697
  }
3698
 
3699
- $reorder_field = pods_var_raw( 'ui_reorder_field', $this->pod_data[ 'options' ] );
 
 
 
 
 
 
3700
 
3701
- if ( in_array( 'reorder', $actions_enabled ) && !in_array( 'reorder', $actions_disabled ) && !empty( $reorder_field ) && ( ( !empty( $this->pod_data[ 'object_fields' ] ) && isset( $this->pod_data[ 'object_fields' ][ $reorder_field ] ) ) || isset( $this->pod_data[ 'fields' ][ $reorder_field ] ) ) ) {
3702
- $ui[ 'reorder' ] = array( 'on' => $reorder_field );
3703
- $ui[ 'orderby' ] = $reorder_field;
3704
- $ui[ 'orderby_dir' ] = 'ASC';
3705
  }
3706
 
3707
- if ( !empty( $author_restrict ) )
3708
- $ui[ 'restrict' ] = array( 'author_restrict' => $author_restrict );
 
 
 
 
3709
 
3710
- if ( !in_array( 'delete', $ui[ 'actions_disabled' ] ) ) {
3711
- $ui[ 'actions_bulk' ] = array(
3712
- 'delete' => array(
3713
- 'label' => __( 'Delete', 'pods' )
3714
- // callback not needed, Pods has this built-in for delete
3715
- )
3716
  );
3717
  }
3718
 
3719
- $detail_url = pods_var( 'detail_url', $this->pod_data[ 'options' ] );
3720
 
3721
  if ( 0 < strlen( $detail_url ) ) {
3722
- $ui[ 'actions_custom' ] = array(
3723
  'view_url' => array(
3724
  'label' => 'View',
3725
- 'link' => get_site_url() . '/' . $detail_url
3726
- )
3727
  );
3728
  }
3729
 
3730
- // @todo Customize the Add New / Manage links to point to their correct menu items
3731
-
3732
- $ui = apply_filters( 'pods_admin_ui_' . $this->pod, apply_filters( 'pods_admin_ui', $ui, $this->pod, $this ), $this->pod, $this );
3733
 
3734
- // Override UI options
3735
  foreach ( $options as $option => $value ) {
3736
  $ui[ $option ] = $value;
3737
  }
@@ -3739,28 +4497,36 @@ class Pods implements Iterator {
3739
  $this->ui = $ui;
3740
 
3741
  return pods_ui( $this );
3742
- }
3743
 
 
3744
  do_action( 'pods_admin_ui_custom', $this );
3745
- do_action( 'pods_admin_ui_custom_' . $this->pod, $this );
 
 
3746
  }
3747
 
3748
  /**
3749
  * Handle filters / actions for the class
3750
  *
3751
- * @see pods_do_hook
 
 
3752
  *
3753
  * @return mixed Value filtered
3754
  *
3755
- * @since 2.0
3756
  */
3757
- private function do_hook () {
 
3758
  $args = func_get_args();
3759
 
3760
- if ( empty( $args ) )
3761
  return false;
 
3762
 
3763
- $name = array_shift( $args );
 
3764
 
3765
  return pods_do_hook( 'pods', $name, $args, $this );
3766
  }
@@ -3768,64 +4534,95 @@ class Pods implements Iterator {
3768
  /**
3769
  * Handle variables that have been deprecated and PodsData vars
3770
  *
3771
- * @var $name
3772
  *
3773
  * @return mixed
3774
  *
3775
- * @since 2.0
3776
  */
3777
- public function __get ( $name ) {
 
3778
  $name = (string) $name;
3779
 
3780
- // PodsData vars
3781
- if ( 0 === strpos( $name, 'field_' ) && isset( $this->data->{$name} ) ) {
3782
  return $this->data->{$name};
3783
  }
3784
 
3785
- if ( !isset( $this->deprecated ) ) {
3786
- require_once( PODS_DIR . 'deprecated/classes/Pods.php' );
 
3787
  $this->deprecated = new Pods_Deprecated( $this );
3788
  }
3789
 
3790
  $var = null;
3791
 
 
 
3792
  if ( isset( $this->deprecated->{$name} ) ) {
3793
- pods_deprecated( "Pods->{$name}", '2.0' );
 
 
3794
 
3795
  $var = $this->deprecated->{$name};
3796
- }
3797
- else
3798
  pods_deprecated( "Pods->{$name}", '2.0' );
 
3799
 
3800
  return $var;
3801
  }
3802
 
3803
  /**
3804
- * Handle methods that have been deprecated and any aliasing
3805
  *
3806
- * @var $name
3807
- * @var $args
3808
  *
3809
- * @return mixed
3810
  *
3811
- * @since 2.0
3812
  */
3813
- public function __call ( $name, $args ) {
 
3814
  $name = (string) $name;
3815
 
3816
- // select > find alias
3817
- if ( 'select' == $name ) {
3818
  return call_user_func_array( array( $this, 'find' ), $args );
3819
  }
3820
 
3821
- if ( !isset( $this->deprecated ) ) {
3822
- require_once( PODS_DIR . 'deprecated/classes/Pods.php' );
 
3823
  $this->deprecated = new Pods_Deprecated( $this );
3824
  }
3825
 
3826
- if ( method_exists( $this->deprecated, $name ) )
 
 
3827
  return call_user_func_array( array( $this->deprecated, $name ), $args );
3828
- else
 
3829
  pods_deprecated( "Pods::{$name}", '2.0' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3830
  }
3831
  }
1
  <?php
2
+
3
  /**
4
+ * Pods class.
5
+ *
6
  * @package Pods
7
  */
8
  class Pods implements Iterator {
9
 
10
  /**
11
+ * Whether the Pods object is in a PHP Iterator call.
12
+ *
13
  * @var bool
14
  */
15
  private $iterator = false;
16
 
17
  /**
18
+ * PodsAPI object.
19
+ *
20
  * @var PodsAPI
21
  */
22
  public $api;
23
 
24
  /**
25
+ * PodsData object.
26
+ *
27
  * @var PodsData
28
  */
29
  public $data;
30
 
31
  /**
32
+ * PodsData object for additional calls.
33
+ *
34
  * @var PodsData
35
  */
36
  public $alt_data;
37
 
38
  /**
39
+ * Array of pod item arrays.
40
+ *
41
+ * @var array
42
  */
43
+ public $rows;
44
 
45
  /**
46
+ * Current pod item array.
47
+ *
48
+ * @var array
49
  */
50
+ public $row;
51
 
52
  /**
53
+ * Row number.
54
+ *
55
  * @var int
56
  */
57
+ private $row_number;
58
 
59
  /**
60
+ * Override pod item array.
61
+ *
62
+ * @var array
63
  */
64
  public $row_override = array();
65
 
66
  /**
67
+ * Whether to display errors on the screen.
68
+ *
69
  * @var bool
70
  */
71
+ public $display_errors = true;
72
 
73
  /**
74
+ * Current pod information.
75
+ *
76
  * @var array|bool|mixed|null|void
77
  */
78
  public $pod_data;
79
 
80
  /**
81
+ * Last used Pods::find() parameters.
82
+ *
83
  * @var array
84
  */
85
  public $params = array();
86
 
87
  /**
88
+ * Current Pod name.
89
+ *
90
  * @var string
91
  */
92
+ public $pod;
93
 
94
  /**
95
+ * Current Pod ID.
96
+ *
97
  * @var int
98
  */
99
+ public $pod_id;
100
 
101
  /**
102
+ * Pod fields information.
103
+ *
104
  * @var array
105
  */
106
+ public $fields;
107
 
108
  /**
109
+ * Last used filters() parameters.
110
+ *
111
  * @var array
112
  */
113
  public $filters = array();
114
 
115
  /**
116
+ * Detail page URL used for Advanced Content Types.
117
+ *
118
  * @var string
119
  */
120
  public $detail_page;
121
 
122
  /**
123
+ * Current Item ID.
124
+ *
125
  * @var int
126
  */
127
+ public $id;
128
 
129
  /**
130
+ * Last used limit from find() lookup.
131
+ *
132
  * @var int
133
  */
134
  public $limit = 15;
135
 
136
  /**
137
+ * Last used offset from find() lookup.
138
+ *
139
  * @var int
140
  */
141
  public $offset = 0;
142
 
143
  /**
144
+ * Query variable used for pagination number.
145
+ *
146
  * @var string
147
  */
148
  public $page_var = 'pg';
149
 
150
  /**
151
+ * Last used page from find() lookup.
152
+ *
153
  * @var int|mixed
154
  */
155
  public $page = 1;
156
 
157
  /**
158
+ * Last used state of whether pagination was enabled from find() lookup.
159
+ *
160
  * @var bool
161
  */
162
  public $pagination = true;
163
 
164
  /**
165
+ * Last used state of whether search was enabled from find() lookup.
166
+ *
167
  * @var bool
168
  */
169
  public $search = true;
170
 
171
  /**
172
+ * Query variable used for search string.
173
+ *
174
  * @var string
175
  */
176
  public $search_var = 'search';
177
 
178
  /**
179
+ * Search mode (int | text | text_like).
180
+ *
181
  * @var string
182
  */
183
+ public $search_mode = 'int';
184
 
185
  /**
186
+ * Total number of records returned from find() lookup.
187
+ *
188
  * @var int
189
  */
190
  public $total = 0;
191
 
192
  /**
193
+ * Total number of records found from find() lookup.
194
+ *
195
  * @var int
196
  */
197
  public $total_found = 0;
198
 
199
  /**
200
+ * Last used ui options for ui() call.
201
+ *
202
  * @var array
203
  */
204
  public $ui = array();
205
 
206
  /**
207
+ * Page template to use for SEO feature in Pods Pages.
208
+ *
209
+ * @var string
210
  */
211
  public $page_template;
212
+
213
+ /**
214
+ * Body classes to use for SEO feature in Pods Pages.
215
+ *
216
+ * @var array
217
+ */
218
  public $body_classes;
219
+
220
+ /**
221
+ * Meta tags to use for SEO feature in Pods Pages.
222
+ *
223
+ * @var array
224
+ */
225
  public $meta = array();
226
+
227
+ /**
228
+ * Meta properties to use for SEO feature in Pods Pages.
229
+ *
230
+ * @var array
231
+ */
232
  public $meta_properties = array();
233
+
234
+ /**
235
+ * Meta custom HTML to use for SEO feature in Pods Pages.
236
+ *
237
+ * @var string
238
+ */
239
  public $meta_extra = '';
240
 
241
  /**
242
+ * Last SQL query used by a find() lookup.
243
+ *
244
+ * @var string
245
  */
246
  public $sql;
247
 
248
  /**
249
+ * Pods_Deprecated object.
250
+ *
251
+ * @var Pods_Deprecated
252
  */
253
  public $deprecated;
254
 
255
+ /**
256
+ * Old Pod name variable.
257
+ *
258
+ * @var string
259
+ *
260
+ * @deprecated 2.0.0
261
+ */
262
  public $datatype;
263
 
264
+ /**
265
+ * Old Pod ID variable.
266
+ *
267
+ * @var int
268
+ *
269
+ * @deprecated 2.0.0
270
+ */
271
  public $datatype_id;
272
 
273
  /**
274
+ * Constructor - Pods Framework core.
275
  *
276
+ * @param string $pod The pod name.
277
+ * @param mixed $id (optional) The ID or slug, to load a single record; Provide array of $params to run 'find'.
 
 
278
  *
279
  * @license http://www.gnu.org/licenses/gpl-2.0.html
280
+ * @since 1.0.0
281
+ * @link https://pods.io/docs/pods/
282
  */
283
+ public function __construct( $pod = null, $id = null ) {
284
+
285
  if ( null === $pod ) {
286
+ $pod = get_queried_object();
287
+ }
288
 
289
+ if ( $pod && ! is_string( $pod ) ) {
290
+ if ( $pod instanceof WP_Post ) {
291
+ // Post Type Singular.
292
+ $pod = $pod->post_type;
293
  $id_lookup = true;
294
+ } elseif ( $pod instanceof WP_Term ) {
295
+ // Term Archive.
296
+ $pod = $pod->taxonomy;
297
+ $id_lookup = true;
298
+ } elseif ( $pod instanceof WP_User ) {
299
+ // Author Archive.
300
+ $pod = 'user';
301
+ $id_lookup = true;
302
+ } elseif ( $pod instanceof WP_Post_Type ) {
303
+ // Post Type Archive.
304
+ $pod = $pod->name;
305
+ $id_lookup = false;
306
+ } else {
307
+ // Unsupported pod object.
308
+ $pod = null;
309
+ $id_lookup = false;
310
+ }
311
 
312
+ if ( null === $id && $id_lookup ) {
313
+ $id = get_queried_object_id();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  }
315
+ }//end if
316
 
317
+ $this->api = pods_api( $pod );
318
  $this->api->display_errors =& $this->display_errors;
319
 
320
+ $this->data = pods_data( $this->api, $id, false );
321
  PodsData::$display_errors =& $this->display_errors;
322
 
323
+ // Set up page variable.
324
  if ( pods_strict( false ) ) {
325
+ $this->page = 1;
326
  $this->pagination = false;
327
+ $this->search = false;
328
+ } else {
329
+ // Get the page variable.
330
+ $this->page = pods_v( $this->page_var, 'get', 1, true );
331
+
332
+ if ( ! empty( $this->page ) ) {
333
+ $this->page = max( 1, pods_absint( $this->page ) );
334
+ }
335
  }
336
 
337
+ // Set default pagination handling to on/off.
338
  if ( defined( 'PODS_GLOBAL_POD_PAGINATION' ) ) {
339
+ if ( ! PODS_GLOBAL_POD_PAGINATION ) {
340
+ $this->page = 1;
341
  $this->pagination = false;
342
+ } else {
 
343
  $this->pagination = true;
344
+ }
345
  }
346
 
347
+ // Set default search to on/off.
348
  if ( defined( 'PODS_GLOBAL_POD_SEARCH' ) ) {
349
+ if ( PODS_GLOBAL_POD_SEARCH ) {
350
  $this->search = true;
351
+ } else {
352
  $this->search = false;
353
+ }
354
  }
355
 
356
+ // Set default search mode.
357
  $allowed_search_modes = array( 'int', 'text', 'text_like' );
358
 
359
+ if ( defined( 'PODS_GLOBAL_POD_SEARCH_MODE' ) && in_array( PODS_GLOBAL_POD_SEARCH_MODE, $allowed_search_modes, true ) ) {
360
  $this->search_mode = PODS_GLOBAL_POD_SEARCH_MODE;
361
+ }
362
 
363
+ // Sync Settings.
364
+ $this->data->page =& $this->page;
365
+ $this->data->limit =& $this->limit;
366
+ $this->data->pagination =& $this->pagination;
367
+ $this->data->search =& $this->search;
368
  $this->data->search_mode =& $this->search_mode;
369
 
370
+ // Sync Pod Data.
371
  $this->api->pod_data =& $this->data->pod_data;
372
+ $this->pod_data =& $this->api->pod_data;
373
+ $this->api->pod_id =& $this->data->pod_id;
374
+ $this->pod_id =& $this->api->pod_id;
375
+ $this->datatype_id =& $this->pod_id;
376
+ $this->api->pod =& $this->data->pod;
377
+ $this->pod =& $this->api->pod;
378
+ $this->datatype =& $this->pod;
379
+ $this->api->fields =& $this->data->fields;
380
+ $this->fields =& $this->api->fields;
381
+ $this->detail_page =& $this->data->detail_page;
382
+ $this->id =& $this->data->id;
383
+ $this->row =& $this->data->row;
384
+ $this->rows =& $this->data->data;
385
+ $this->row_number =& $this->data->row_number;
386
+ $this->sql =& $this->data->sql;
387
+
388
+ if ( is_array( $id ) || is_object( $id ) ) {
389
  $this->find( $id );
390
+ }
391
  }
392
 
393
  /**
395
  *
396
  * @return bool
397
  *
398
+ * @since 2.0.0
399
  */
400
+ public function valid() {
401
+
402
+ if ( empty( $this->pod_id ) ) {
403
  return false;
404
+ }
405
 
406
+ if ( $this->iterator ) {
407
  return isset( $this->rows[ $this->row_number ] );
408
+ }
409
 
410
  return true;
411
  }
417
  *
418
  * @since 2.3.4
419
  *
420
+ * @link http://www.php.net/manual/en/class.iterator.php
421
  */
422
+ public function is_iterator() {
423
+
424
  return $this->iterator;
425
  }
426
 
431
  *
432
  * @since 2.3.4
433
  *
434
+ * @link http://www.php.net/manual/en/class.iterator.php
435
  */
436
+ public function stop_iterator() {
437
+
438
  $this->iterator = false;
439
 
 
440
  }
441
 
442
  /**
443
  * Rewind Iterator
444
  *
 
 
445
  * @since 2.3.4
446
  *
447
+ * @link http://www.php.net/manual/en/class.iterator.php
448
  */
449
+ public function rewind() {
 
 
450
 
451
+ if ( ! $this->iterator ) {
452
+ $this->iterator = true;
453
 
454
+ $this->row_number = 0;
455
+ }
456
  }
457
 
458
  /**
462
  *
463
  * @since 2.3.4
464
  *
465
+ * @link http://www.php.net/manual/en/class.iterator.php
466
  */
467
+ public function current() {
468
+
469
+ if ( $this->iterator && $this->fetch() ) {
470
  return $this;
471
+ }
472
 
473
  return false;
474
  }
476
  /**
477
  * Get current Iterator key
478
  *
479
+ * @return int
480
  *
481
  * @since 2.3.4
482
  *
483
+ * @link http://www.php.net/manual/en/class.iterator.php
484
  */
485
+ public function key() {
 
 
486
 
487
+ return $this->row_number;
488
  }
489
 
490
  /**
491
  * Move onto the next Iterator row
492
  *
493
+ * @return void
494
  *
495
  * @since 2.3.4
496
  *
497
+ * @link http://www.php.net/manual/en/class.iterator.php
498
  */
499
+ public function next() {
 
 
 
 
 
500
 
501
+ $this->row_number ++;
502
  }
503
 
504
  /**
506
  *
507
  * @return bool
508
  *
509
+ * @since 2.0.0
510
  */
511
+ public function exists() {
512
+
513
+ if ( empty( $this->row ) ) {
514
  return false;
515
+ }
516
 
517
  return true;
518
  }
525
  *
526
  * @return array|bool An array of all rows returned from a find() call, or false if no items returned
527
  *
528
+ * @since 2.0.0
529
+ * @link https://pods.io/docs/data/
530
  */
531
+ public function data() {
532
+
533
+ do_action( 'pods_pods_data', $this );
534
 
535
+ if ( empty( $this->rows ) ) {
536
  return false;
537
+ }
538
 
539
  return (array) $this->rows;
540
  }
542
  /**
543
  * Return a field input for a specific field
544
  *
545
+ * @param string|array $field Field name or Field data array.
546
+ * @param string|array|null $input_name Input field name to use (overrides default name).
547
+ * @param mixed $value Current value to use.
548
  *
549
  * @return string Field Input HTML
550
  *
552
  */
553
  public function input( $field, $input_name = null, $value = '__null' ) {
554
 
 
555
  if ( is_array( $field ) ) {
556
+ // Field data override.
557
  $field_data = $field;
558
+
559
+ $field = pods_v( 'name', $field );
560
+ } else {
561
+ // Get field data from field name.
562
  $field_data = $this->fields( $field );
563
  }
564
 
565
+ if ( ! empty( $field_data ) ) {
566
+ $field_type = pods_v( 'type', $field_data );
567
 
568
  if ( empty( $input_name ) ) {
569
  $input_name = $field;
570
  }
571
 
572
+ if ( '__null' === $value ) {
573
+ $value = $this->field( array(
574
+ 'name' => $field,
575
+ 'in_form' => true,
576
+ ) );
577
  }
578
 
579
  return PodsForm::field( $input_name, $value, $field_type, $field_data, $this, $this->id() );
586
  /**
587
  * Return field array from a Pod, a field's data, or a field option
588
  *
589
+ * @param null $field Field name.
590
+ * @param null $option Option name.
591
  *
592
  * @return bool|mixed
593
  *
594
+ * @since 2.0.0
595
  */
596
+ public function fields( $field = null, $option = null ) {
597
+
598
+ $field_data = null;
599
+
600
+ if ( empty( $this->fields ) ) {
601
+ // No fields found.
602
  $field_data = array();
603
+ } elseif ( empty( $field ) ) {
604
+ // Return all fields.
605
  $field_data = (array) $this->fields;
606
+ } elseif ( ! isset( $this->fields[ $field ] ) && ! isset( $this->pod_data['object_fields'][ $field ] ) ) {
607
+ // Field not found.
608
  $field_data = array();
609
+ } elseif ( empty( $option ) ) {
610
+ // Return all field data.
611
+ if ( isset( $this->fields[ $field ] ) ) {
612
+ $field_data = $this->fields[ $field ];
613
+ } elseif ( isset( $this->pod_data['object_fields'] ) ) {
614
+ $field_data = $this->pod_data['object_fields'][ $field ];
615
+ }
616
+ } else {
617
+ $options = array();
618
 
619
+ // Merge options.
620
+ if ( isset( $this->fields[ $field ] ) ) {
621
+ $options = array_merge( $this->fields[ $field ], $this->fields[ $field ]['options'] );
622
+ } elseif ( isset( $this->pod_data['object_fields'] ) ) {
623
+ $options = array_merge( $this->pod_data['object_fields'][ $field ], $this->pod_data['object_fields'][ $field ]['options'] );
624
+ }
625
 
626
+ if ( 'data' === $option && in_array( pods_v( 'type', $options ), PodsForm::tableless_field_types(), true ) ) {
627
+ // Get a list of available items from a relationship field.
628
  $field_data = PodsForm::field_method( 'pick', 'get_field_data', $options );
629
+ } elseif ( isset( $options[ $option ] ) ) {
630
+ // Return option.
 
631
  $field_data = $options[ $option ];
632
  }
633
+ }//end if
634
+
635
+ /**
636
+ * Modify the field data before returning
637
+ *
638
+ * @since unknown
639
+ *
640
+ * @param array $field_data The data for the field.
641
+ * @param string|null $field The specific field that data is being return for, if set when method is called or null.
642
+ * @param string|null $option Value of option param when method was called. Can be used to get a list of available items from a relationship field.
643
+ * @param Pods|object $this The current Pods class instance.
644
+ */
645
+ return apply_filters( 'pods_pods_fields', $field_data, $field, $option, $this );
646
 
 
647
  }
648
 
649
  /**
650
  * Return row array for an item
651
  *
652
+ * @return array|false
653
  *
654
+ * @since 2.0.0
655
  */
656
+ public function row() {
657
+
658
+ do_action( 'pods_pods_row', $this );
659
 
660
+ if ( ! is_array( $this->row ) ) {
661
  return false;
662
+ }
663
 
664
  return (array) $this->row;
665
  }
669
  * you will want to use field() instead. This function will automatically convert arrays into a
670
  * list of text such as "Rick, John, and Gary"
671
  *
672
+ * @param string|array|object $name The field name, or an associative array of parameters.
673
+ * @param boolean|array|object $single (optional) For tableless fields, to return an array or the first.
674
  *
675
+ * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned
676
+ * for tableless fields
677
+ * @since 2.0.0
678
+ * @link https://pods.io/docs/display/
679
  */
680
+ public function display( $name, $single = null ) {
681
+
682
  $defaults = array(
683
+ 'name' => $name,
684
+ 'single' => $single,
685
+ 'display' => true,
686
+ 'serial_params' => null,
687
  );
688
 
689
  if ( is_array( $name ) || is_object( $name ) ) {
690
+ $defaults['name'] = null;
691
+
692
  $params = (object) array_merge( $defaults, (array) $name );
693
+ } elseif ( is_array( $single ) || is_object( $single ) ) {
694
+ $defaults['single'] = null;
695
+
696
  $params = (object) array_merge( $defaults, (array) $single );
697
+ } else {
 
698
  $params = $defaults;
699
+ }
700
 
701
  $params = (object) $params;
702
 
703
  $value = $this->field( $params );
704
 
705
  if ( is_array( $value ) ) {
706
+ $fields = $this->fields;
707
+
708
+ if ( isset( $this->pod_data['object_fields'] ) ) {
709
+ $fields = array_merge( $fields, $this->pod_data['object_fields'] );
710
+ }
711
+
712
  $serial_params = array(
713
+ 'field' => $params->name,
714
+ 'fields' => $fields,
715
  );
716
 
717
+ if ( ! empty( $params->serial_params ) && is_array( $params->serial_params ) ) {
718
  $serial_params = array_merge( $serial_params, $params->serial_params );
719
+ }
720
 
721
  $value = pods_serial_comma( $value, $serial_params );
722
  }
729
  * you will want to use field() instead. This function will automatically convert arrays into a
730
  * list of text such as "Rick, John, and Gary"
731
  *
732
+ * @param string|array|object $name The field name, or an associative array of parameters.
733
+ * @param boolean|array|object $single (optional) For tableless fields, to return an array or the first.
734
  *
735
+ * @return string|null|false The output from the field, null if the field doesn't exist, false if no value returned
736
+ * for tableless fields
737
+ * @since 2.0.0
738
+ * @link https://pods.io/docs/display/
739
  */
740
+ public function raw( $name, $single = null ) {
741
+
742
  $defaults = array(
743
+ 'name' => $name,
744
  'single' => $single,
745
+ 'raw' => true,
746
  );
747
 
748
  if ( is_array( $name ) || is_object( $name ) ) {
749
+ $defaults['name'] = null;
750
+
751
  $params = (object) array_merge( $defaults, (array) $name );
752
+ } elseif ( is_array( $single ) || is_object( $single ) ) {
753
+ $defaults['single'] = null;
754
+
755
  $params = (object) array_merge( $defaults, (array) $single );
756
+ } else {
 
757
  $params = (object) $defaults;
758
+ }
759
 
760
+ return $this->field( $params );
 
 
761
  }
762
 
763
  /**
767
  *
768
  * This function will return arrays for relationship and file fields.
769
  *
770
+ * @param string|array|object $name The field name, or an associative array of parameters.
771
+ * @param boolean|array|object $single For tableless fields, to return the whole array or the just the first item,
772
+ * or an associative array of parameters.
773
+ * @param boolean|array|object $raw Whether to return the raw value, or to run through the field type's display
774
+ * method, or an associative array of parameters.
775
  *
776
+ * @return mixed|null Value returned depends on the field type, null if the field doesn't exist, false if no value
777
+ * returned for tableless fields.
778
+ * @since 2.0.0
779
+ * @link https://pods.io/docs/field/
780
  */
781
+ public function field( $name, $single = null, $raw = false ) {
 
782
 
783
  $defaults = array(
784
+ 'name' => $name,
785
+ 'orderby' => null,
786
+ 'single' => $single,
787
+ 'params' => null,
788
+ 'in_form' => false,
789
+ 'raw' => $raw,
790
  'raw_display' => false,
791
+ 'display' => false,
792
+ 'get_meta' => false,
793
+ 'output' => null,
794
+ 'deprecated' => false,
795
+ 'keyed' => false,
796
+ // extra data to send to field handlers.
797
+ 'args' => array(),
798
  );
799
 
800
+ if ( is_object( $name ) ) {
801
+ $name = get_object_vars( $name );
 
802
  }
803
+
804
+ if ( is_object( $single ) ) {
805
+ $single = get_object_vars( $single );
806
  }
807
+
808
+ if ( is_object( $raw ) ) {
809
+ $raw = get_object_vars( $raw );
810
  }
811
+
812
+ if ( is_array( $name ) ) {
813
+ $defaults['name'] = null;
814
+
815
+ $params = (object) array_merge( $defaults, (array) $name );
816
+ } elseif ( is_array( $single ) ) {
817
+ $defaults['single'] = null;
818
+
819
+ $params = (object) array_merge( $defaults, (array) $single );
820
+ } elseif ( is_array( $raw ) ) {
821
+ $defaults['raw'] = false;
822
+
823
+ $params = (object) array_merge( $defaults, (array) $raw );
824
+ } else {
825
  $params = (object) $defaults;
826
+ }//end if
827
 
828
  if ( $params->in_form ) {
829
  $params->output = 'ids';
830
+ } elseif ( null === $params->output ) {
831
+ /**
832
+ * Override the way related fields are output
833
+ *
834
+ * @param string $output How to output related fields. Default is 'arrays'. Options: ids|names|objects|arrays|pods|find
835
+ * @param array|object $row Current row being outputted.
836
+ * @param array $params Params array passed to field().
837
+ * @param Pods $this Current Pods object.
838
+ */
839
+ $params->output = apply_filters( 'pods_pods_field_related_output_type', 'arrays', $this->row, $params, $this );
840
  }
841
 
842
+ if ( in_array( $params->output, array( 'id', 'name', 'object', 'array', 'pod' ), true ) ) {
843
  $params->output .= 's';
844
+ }
845
 
846
+ // Support old $orderby variable.
847
  if ( null !== $params->single && is_string( $params->single ) && empty( $params->orderby ) ) {
848
+ if ( ! class_exists( 'Pod' ) || Pod::$deprecated_notice ) {
849
+ pods_deprecated( 'Pods::field', '2.0', 'Use $params[ \'orderby\' ] instead' );
850
+ }
851
 
852
  $params->orderby = $params->single;
853
+ $params->single = false;
854
  }
855
 
856
+ if ( null !== $params->single ) {
857
  $params->single = (boolean) $params->single;
858
+ }
859
 
860
+ $params->name = trim( $params->name );
861
+ if ( is_array( $params->name ) || '' === $params->name ) {
862
  return null;
863
+ }
864
 
 
865
  $params->full_name = $params->name;
866
 
867
  $value = null;
868
 
869
+ if ( isset( $this->row_override[ $params->name ] ) ) {
870
  $value = $this->row_override[ $params->name ];
871
+ }
872
 
873
  if ( false === $this->row() ) {
874
+ if ( false !== $this->data() ) {
875
  $this->fetch();
876
+ } else {
877
  return $value;
878
+ }
879
  }
880
 
881
+ if ( $this->data->field_id === $params->name ) {
882
+ if ( isset( $this->row[ $params->name ] ) ) {
883
  return $this->row[ $params->name ];
884
+ // @codingStandardsIgnoreLine.
885
+ } elseif ( null !== $value ) {
886
  return $value;
887
+ }
888
 
889
  return 0;
890
  }
891
 
892
+ $tableless_field_types = PodsForm::tableless_field_types();
893
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
894
 
895
  $params->traverse = array();
896
 
897
+ $permalink_fields = array(
898
+ '_link',
899
+ 'detail_url',
900
+ 'permalink',
901
+ 'the_permalink',
902
+ );
903
+
904
+ $wp_object_types = array(
905
+ 'post_type',
906
+ 'taxonomy',
907
+ 'media',
908
+ 'user',
909
+ 'comment',
910
+ );
911
+
912
+ /** @var string $pod_type The pod object type. */
913
+ $pod_type = pods_v( 'type', $this->pod_data, '' );
914
+
915
+ $is_wp_object = in_array( $pod_type, $wp_object_types, true );
916
+
917
+ if ( in_array( $params->name, $permalink_fields, true ) ) {
918
+ if ( 0 < strlen( $this->detail_page ) && false === strpos( $params->name, 'permalink' ) ) {
919
+ // ACT Pods. Prevent tag loop by not parsing `permalink`.
920
  $value = get_home_url() . '/' . $this->do_magic_tags( $this->detail_page );
921
+ } else {
922
+ switch ( $pod_type ) {
923
+ case 'post_type':
924
+ case 'media':
925
+ $value = get_permalink( $this->id() );
926
+ break;
927
+ case 'taxonomy':
928
+ $value = get_term_link( $this->id(), $this->pod_data['name'] );
929
+ break;
930
+ case 'user':
931
+ $value = get_author_posts_url( $this->id() );
932
+ break;
933
+ case 'comment':
934
+ $value = get_comment_link( $this->id() );
935
+ break;
936
+ }
937
+ }
938
+ }
939
+
940
+ /**
941
+ * @var bool $is_field_set Is the field found.
942
+ * @var bool $is_tableless_field Is it a tableless field.
943
+ * @var string $field_source Regular field or object field.
944
+ * @var array $field_data The field data.
945
+ * @var string $field_type The field type.
946
+ * @var array $field_options The field options.
947
+ * @var array $traverse_fields All the traversal field names.
948
+ * @var bool $is_traversal Is it a traversal field request.
949
+ * @var string $first_field The name of the fieds without the traversal names from $params->name.
950
+ * @var array $last_field_data The field data used in traversal loop.
951
+ */
952
+
953
+ $is_field_set = false;
954
+ $is_tableless_field = false;
955
+ $field_source = '';
956
+ $field_data = array();
957
+ $field_type = '';
958
+ $field_options = array();
959
+ $traverse_fields = explode( '.', $params->name );
960
+ $is_traversal = 1 < count( $traverse_fields );
961
+ $first_field = $traverse_fields[0];
962
+ $last_field_data = null;
963
+
964
+ if ( isset( $this->fields[ $params->name ] ) ) {
965
+ // Get the full field name data.
966
+ $field_data = $this->fields[ $params->name ];
967
+ $field_source = 'field';
968
+ $is_field_set = true;
969
+ } elseif ( isset( $this->fields[ $first_field ] ) ) {
970
+ // Get the first field name data.
971
+ $field_data = $this->fields[ $first_field ];
972
+ $field_source = 'field';
973
+ $is_field_set = true;
974
+ } elseif ( ! empty( $this->pod_data['object_fields'] ) ) {
975
+ // Get the object field data.
976
+ if ( isset( $this->pod_data['object_fields'][ $first_field ] ) ) {
977
+ $field_data = $this->pod_data['object_fields'][ $first_field ];
978
+ $field_source = 'object_field';
979
+ $is_field_set = true;
980
+ } else {
981
+ $object_fields = (array) $this->pod_data['object_fields'];
982
+
983
+ // Search through field aliases.
984
+ foreach ( $object_fields as $object_field => $object_field_opt ) {
985
+ if ( in_array( $first_field, $object_field_opt['alias'], true ) ) {
986
+ if ( $first_field === $params->name ) {
987
  $params->name = $object_field;
988
+ }
989
 
990
+ $first_field = $object_field;
991
+ $field_data = $object_field_opt;
992
+ $field_source = 'object_field';
993
+ $is_field_set = true;
994
 
995
  break;
996
  }
997
  }
998
  }
999
+ }//end if
1000
 
1001
+ // Store field info.
1002
+ $field_type = pods_v( 'type', $field_data, '' );
1003
+ $field_options = pods_v( 'options', $field_data, array() );
1004
+ $is_tableless_field = in_array( $field_type, $tableless_field_types, true );
1005
+
1006
+ // Simple fields have no other output options.
1007
+ if ( 'pick' === $field_type && in_array( $field_data['pick_object'], $simple_tableless_objects, true ) ) {
1008
  $params->output = 'arrays';
1009
  }
1010
 
1011
+ // Enforce output type for tableless fields in forms.
1012
+ if ( empty( $value ) && $is_tableless_field ) {
1013
  $params->raw = true;
1014
 
1015
  $value = false;
1016
 
1017
+ $row_key = '_' . $params->output . '_' . $params->name;
1018
+
1019
+ if ( 'arrays' !== $params->output && isset( $this->row[ $row_key ] ) ) {
1020
+ $value = $this->row[ $row_key ];
1021
+ } elseif ( 'arrays' === $params->output && isset( $this->row[ $params->name ] ) ) {
1022
  $value = $this->row[ $params->name ];
1023
  }
1024
 
1025
+ if (
1026
+ false !== $value &&
1027
+ ! is_array( $value ) &&
1028
+ 'pick' === $field_type &&
1029
+ in_array( $field_data['pick_object'], $simple_tableless_objects, true )
1030
+ ) {
1031
  $value = PodsForm::field_method( 'pick', 'simple_value', $params->name, $value, $field_data, $this->pod_data, $this->id(), true );
1032
+ }
1033
  }
1034
 
1035
+ if (
1036
+ empty( $value ) &&
1037
+ isset( $this->row[ $params->name ] ) &&
1038
+ ( ! $is_tableless_field || 'arrays' === $params->output )
1039
+ ) {
1040
+ if ( empty( $field_data ) || in_array( $field_type, array( 'boolean', 'number', 'currency' ), true ) ) {
1041
  $params->raw = true;
1042
+ }
1043
+
1044
+ if ( null === $params->single ) {
1045
+ if ( ! $is_tableless_field ) {
1046
+ $params->single = true;
1047
+ } else {
1048
+ $params->single = false;
1049
+ }
1050
+ }
1051
 
1052
  $value = $this->row[ $params->name ];
1053
+ } elseif ( empty( $value ) ) {
 
1054
  $object_field_found = false;
1055
 
1056
+ if ( 'object_field' === $field_source ) {
1057
  $object_field_found = true;
1058
 
1059
+ if ( isset( $this->row[ $first_field ] ) ) {
1060
  $value = $this->row[ $first_field ];
1061
+ } elseif ( $is_tableless_field ) {
1062
+ // Overwrite existing field data.
1063
  $this->fields[ $first_field ] = $field_data;
1064
 
1065
  $object_field_found = false;
1066
+ } else {
 
1067
  return null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1068
  }
 
 
 
 
 
1069
 
1070
+ } elseif ( 'avatar' === $first_field && 'user' === $pod_type ) {
1071
+ // User avatar.
1072
+ $size = null;
1073
+ $get_avatar = true;
1074
+
1075
+ if ( $is_traversal ) {
1076
+ if ( $is_field_set ) {
1077
+ // This is a registered field.
1078
+ if ( isset( $traverse_fields[1] ) && is_numeric( $traverse_fields[1] ) ) {
1079
+ $size = (int) $traverse_fields[1];
1080
+ } else {
1081
+ // Traverse through attachment post.
1082
+ $get_avatar = false;
1083
+ }
1084
+ } else {
1085
+ if ( isset( $traverse_fields[1] ) ) {
1086
+ $size = (int) $traverse_fields[1];
1087
+ }
1088
  }
 
 
 
 
 
1089
  }
 
 
1090
 
1091
+ if ( $get_avatar ) {
1092
+ $object_field_found = true;
1093
+ if ( 0 < $size ) {
1094
+ $value = get_avatar( $this->id(), $size );
1095
+ } else {
1096
+ $value = get_avatar( $this->id() );
 
 
 
 
 
 
 
 
 
1097
  }
1098
  }
 
 
1099
 
1100
+ } elseif ( ! $is_field_set ) {
1101
 
1102
+ $image_fields = array(
1103
+ 'image_attachment',
1104
+ 'image_attachment_url',
1105
+ );
 
 
 
 
 
 
1106
 
1107
+ if ( 'post_type' === $pod_type ) {
1108
+ $image_fields[] = 'post_thumbnail';
1109
+ $image_fields[] = 'post_thumbnail_url';
1110
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1111
 
1112
+ // Handle special field tags.
1113
+ if ( in_array( $first_field, $image_fields, true ) ) {
1114
+ // Default image field handlers.
1115
  $object_field_found = true;
 
 
 
 
 
 
1116
 
1117
+ $image_field = $first_field;
1118
+ // Is it a URL request?
1119
+ $url = '_url' === substr( $image_field, - 4 );
1120
+ if ( $url ) {
1121
+ $image_field = substr( $image_field, 0, - 4 );
1122
+ }
 
1123
 
1124
+ // Copy traversal parameters.
1125
+ $traverse_params = $traverse_fields;
1126
+ // Results in an empty array if no traversal params are passed.
1127
+ array_shift( $traverse_params );
1128
 
1129
+ $attachment_id = 0;
1130
+ switch ( $image_field ) {
1131
+ case 'post_thumbnail':
1132
+ $attachment_id = get_post_thumbnail_id( $this->id() );
1133
+ break;
1134
+ case 'image_attachment':
1135
+ if ( isset( $traverse_params[0] ) ) {
1136
+ $attachment_id = $traverse_params[0];
1137
+ array_shift( $traverse_params );
1138
+ }
1139
+ break;
1140
+ }
1141
 
1142
+ if ( $attachment_id ) {
1143
+ $is_image = wp_attachment_is_image( $attachment_id );
1144
 
1145
+ $size = 'thumbnail';
1146
+ if ( isset( $traverse_params[0] ) ) {
1147
+ $size = $traverse_params[0];
1148
 
1149
+ if ( pods_is_image_size( $size ) ) {
1150
+ // Force image request since a valid size parameter is passed.
1151
+ $is_image = true;
1152
+ } else {
1153
+ // No valid image size found.
1154
+ $size = false;
1155
+ }
1156
+ }
1157
 
1158
+ if ( $url ) {
1159
+ if ( $is_image ) {
1160
+ $value = pods_image_url( $attachment_id, $size, 0, true );
1161
+ } else {
1162
+ $value = wp_get_attachment_url( $attachment_id );
1163
+ }
1164
+ } elseif ( $size ) {
1165
+ // Pods will auto-get the thumbnail ID if this isn't an attachment.
1166
+ $value = pods_image( $attachment_id, $size, 0, null, true );
1167
+ } else {
1168
+ // Fallback to attachment Post object to look for other image properties.
1169
+ $media = pods( 'media', $attachment_id );
1170
+
1171
+ if ( $media && $media->valid() && $media->exists() ) {
1172
+ $value = $media->field( implode( '.', $traverse_params ) );
1173
+ } else {
1174
+ // Fallback to default attachment object.
1175
+ $attachment = get_post( $attachment_id );
1176
+ $value = pods_v( implode( '.', $traverse_params ), $attachment, null );
1177
+
1178
+ if ( null === $value ) {
1179
+ // Start traversal though object property or metadata.
1180
+ $name_key = array_shift( $traverse_params );
1181
+ $value = pods_v( $name_key, $attachment, null );
1182
+
1183
+ if ( null === $value ) {
1184
+ $value = get_post_meta( $attachment_id, $name_key, true );
1185
+ }
1186
 
1187
+ $value = pods_traverse( $traverse_params, $value );
1188
+ }
1189
+ }
1190
+ }
1191
+ }
1192
  }
1193
  }
1194
 
1195
+ // Continue regular field parsing.
1196
  if ( false === $object_field_found ) {
1197
  $params->traverse = array( $params->name );
1198
 
1199
  if ( false !== strpos( $params->name, '.' ) ) {
1200
  $params->traverse = explode( '.', $params->name );
1201
 
1202
+ $params->name = $params->traverse[0];
1203
  }
1204
 
1205
+ if ( $field_type ) {
1206
+
1207
+ /**
1208
+ * Modify value returned by field() after its retrieved, but before its validated or formatted
1209
+ *
1210
+ * Filter name is set dynamically with name of field: "pods_pods_field_{field_type}"
1211
+ *
1212
+ * @since unknown
1213
+ *
1214
+ * @param array|string|null $value Value retrieved.
1215
+ * @param array|object $row Current row being outputted.
1216
+ * @param array $params Params array passed to field().
1217
+ * @param object|Pods $this Current Pods object.
1218
+ */
1219
+ $v = apply_filters( "pods_pods_field_{$field_type}", null, $this->fields[ $params->name ], $this->row, $params, $this );
1220
+
1221
+ if ( null !== $v ) {
1222
  return $v;
1223
+ }
1224
  }
1225
 
1226
  $simple = false;
 
1227
 
1228
+ if ( $is_field_set ) {
1229
+
1230
+ if ( 'meta' === $this->pod_data['storage'] && ! $is_tableless_field ) {
1231
+ $simple = true;
1232
  }
1233
 
1234
+ if ( $is_tableless_field ) {
1235
  $params->raw = true;
1236
 
1237
+ if ( 'pick' === $field_type && in_array( $field_data['pick_object'], $simple_tableless_objects, true ) ) {
1238
+ $simple = true;
1239
  $params->single = true;
1240
  }
1241
+ } elseif ( in_array( $field_type, array( 'boolean', 'number', 'currency' ), true ) ) {
 
1242
  $params->raw = true;
1243
+ }
1244
  }
1245
 
1246
+ if ( ! $is_traversal && ( $simple || ! $is_field_set || ! $is_tableless_field ) ) {
1247
  if ( null === $params->single ) {
1248
+ if ( $is_field_set && ! $is_tableless_field ) {
1249
  $params->single = true;
1250
+ } else {
1251
  $params->single = false;
1252
+ }
1253
  }
1254
 
1255
+ $no_conflict = pods_no_conflict_check( $pod_type );
1256
 
1257
+ if ( ! $no_conflict ) {
1258
+ // Temporarily enable no conflict.
1259
+ pods_no_conflict_on( $pod_type );
1260
+ }
1261
 
1262
+ if ( $is_wp_object ) {
1263
  $id = $this->id();
1264
 
1265
+ $metadata_type = $pod_type;
1266
+
1267
+ if ( in_array( $metadata_type, array( 'post_type', 'media' ), true ) ) {
1268
+ $metadata_type = 'post';
1269
+
1270
+ // Support for WPML 'duplicated' translation handling.
1271
+ if ( did_action( 'wpml_loaded' ) && apply_filters( 'wpml_is_translated_post_type', false, $this->pod_data['name'] ) ) {
1272
+ $master_post_id = (int) apply_filters( 'wpml_master_post_from_duplicate', $id );
1273
 
1274
+ if ( $master_post_id ) {
1275
+ $id = $master_post_id;
1276
+ }
1277
+ }
1278
+ } elseif ( 'taxonomy' === $metadata_type ) {
1279
+ $metadata_type = 'term';
1280
  }
1281
 
1282
+ $value = get_metadata( $metadata_type, $id, $params->name, $params->single );
1283
 
1284
+ $single_multi = 'single';
1285
 
1286
+ if ( $is_field_set && $field_type ) {
1287
+ $single_multi = pods_v( $field_type . '_format_type', $field_options, $single_multi );
1288
  }
 
 
 
 
 
1289
 
1290
+ if ( $simple && ! is_array( $value ) && 'single' !== $single_multi ) {
1291
+ $value = get_metadata( $metadata_type, $id, $params->name );
1292
  }
1293
+ } elseif ( 'settings' === $pod_type ) {
1294
+ $value = get_option( $this->pod_data['name'] . '_' . $params->name, null );
1295
+ }//end if
1296
 
1297
+ // Handle Simple Relationships.
1298
  if ( $simple ) {
1299
+ if ( null === $params->single ) {
1300
  $params->single = false;
1301
+ }
1302
 
1303
  $value = PodsForm::field_method( 'pick', 'simple_value', $params->name, $value, $this->fields[ $params->name ], $this->pod_data, $this->id(), true );
1304
  }
1305
 
1306
+ if ( ! $no_conflict ) {
1307
+ // Revert temporarily no conflict mode.
1308
+ pods_no_conflict_off( $pod_type );
1309
+ }
1310
+ } else {
1311
+ // Dot-traversal.
1312
+ $pod = $this->pod;
1313
+ $ids = array( $this->id() );
1314
  $all_fields = array();
1315
 
1316
  $lookup = $params->traverse;
1317
 
1318
+ // Get fields matching traversal names.
1319
+ if ( ! empty( $lookup ) ) {
 
 
 
 
 
 
 
 
 
 
 
1320
  $fields = $this->api->load_fields( array(
1321
+ 'name' => $lookup,
1322
+ 'type' => $tableless_field_types,
1323
+ 'object_fields' => true,
1324
+ // @todo support object fields too.
1325
  ) );
1326
 
1327
+ if ( ! empty( $fields ) ) {
1328
  foreach ( $fields as $field ) {
1329
+ if ( ! empty( $field ) ) {
1330
+ if ( ! isset( $all_fields[ $field['pod'] ] ) ) {
1331
+ $all_fields[ $field['pod'] ] = array();
1332
+ }
1333
 
1334
+ $all_fields[ $field['pod'] ][ $field['name'] ] = $field;
1335
  }
1336
  }
1337
  }
1338
 
1339
+ if ( ! empty( $this->pod_data['object_fields'] ) ) {
1340
+ $object_fields = (array) $this->pod_data['object_fields'];
1341
+
1342
+ foreach ( $object_fields as $object_field => $object_field_opt ) {
1343
+ if ( in_array( $object_field_opt['type'], $tableless_field_types, true ) ) {
1344
  $all_fields[ $this->pod ][ $object_field ] = $object_field_opt;
1345
  }
1346
  }
1347
  }
1348
+ }//end if
1349
 
1350
+ $last_type = '';
1351
+ $last_object = '';
1352
+ $last_pick_val = '';
1353
 
1354
+ $single_multi = pods_v( $field_type . '_format_type', $field_options, 'single' );
1355
 
1356
+ if ( 'multi' === $single_multi ) {
1357
+ $limit = (int) pods_v( $field_type . '_limit', $field_options, 0 );
1358
+ } else {
1359
  $limit = 1;
1360
+ }
1361
 
1362
+ // Loop through each traversal level.
 
 
1363
  foreach ( $params->traverse as $key => $field ) {
1364
  $last_loop = false;
1365
 
1366
+ if ( count( $params->traverse ) <= ( $key + 1 ) ) {
1367
  $last_loop = true;
1368
+ }
1369
 
1370
  $field_exists = isset( $all_fields[ $pod ][ $field ] );
1371
 
1372
+ $simple = false;
1373
  $last_options = array();
1374
 
1375
+ if (
1376
+ $field_exists &&
1377
+ 'pick' === $all_fields[ $pod ][ $field ]['type'] &&
1378
+ in_array( $all_fields[ $pod ][ $field ]['pick_object'], $simple_tableless_objects, true )
1379
+ ) {
1380
+ $simple = true;
1381
  $last_options = $all_fields[ $pod ][ $field ];
1382
  }
1383
 
1384
+ // Tableless handler.
1385
+ if ( $field_exists && ( ! $simple || ! in_array( $all_fields[ $pod ][ $field ]['type'], array(
1386
+ 'pick',
1387
+ 'taxonomy',
1388
+ 'comment',
1389
+ ), true ) ) ) {
1390
+ $type = $all_fields[ $pod ][ $field ]['type'];
1391
+ $pick_object = $all_fields[ $pod ][ $field ]['pick_object'];
1392
+ $pick_val = $all_fields[ $pod ][ $field ]['pick_val'];
1393
+
1394
+ if ( 'table' === $pick_object ) {
1395
+ $pick_val = pods_v( 'pick_table', $all_fields[ $pod ][ $field ]['options'], $pick_val, true );
1396
+ } elseif ( '__current__' === $pick_val ) {
1397
  $pick_val = $pod;
1398
  }
1399
 
1400
  $last_limit = 0;
1401
 
1402
+ if ( in_array( $type, $tableless_field_types, true ) ) {
1403
+ $single_multi = pods_v( "{$type}_format_type", $all_fields[ $pod ][ $field ]['options'], 'single' );
 
 
 
 
 
 
 
1404
 
1405
+ if ( 'multi' === $single_multi ) {
1406
+ $last_limit = (int) pods_v( "{$type}_limit", $all_fields[ $pod ][ $field ]['options'], 0 );
1407
+ } else {
1408
  $last_limit = 1;
1409
  }
1410
  }
1411
 
1412
+ $last_type = $type;
1413
+ $last_object = $pick_object;
1414
  $last_pick_val = $pick_val;
1415
+ $last_options = $all_fields[ $pod ][ $field ];
 
 
 
1416
 
1417
+ // Temporary hack until there's some better handling here.
1418
+ $last_limit *= count( $ids );
1419
 
1420
+ // Get related IDs.
1421
+ if ( ! isset( $all_fields[ $pod ][ $field ]['pod_id'] ) ) {
1422
+ $all_fields[ $pod ][ $field ]['pod_id'] = 0;
1423
  }
 
1424
 
1425
+ if ( isset( $all_fields[ $pod ][ $field ]['id'] ) ) {
1426
+ $ids = $this->api->lookup_related_items( $all_fields[ $pod ][ $field ]['id'], $all_fields[ $pod ][ $field ]['pod_id'], $ids, $all_fields[ $pod ][ $field ] );
 
 
 
 
1427
  }
1428
 
1429
+ // No items found.
1430
  if ( empty( $ids ) ) {
 
1431
  return false;
1432
+ } elseif ( 0 < $last_limit ) {
1433
+ // @todo This should return array() if not $params->single.
 
1434
  $ids = array_slice( $ids, 0, $last_limit );
1435
  }
1436
 
1437
+ // Get $pod if related to a Pod.
1438
+ if ( ! empty( $pick_object ) && ( ! empty( $pick_val ) || in_array( $pick_object, array(
1439
+ 'user',
1440
+ 'media',
1441
+ 'comment',
1442
+ ), true ) ) ) {
1443
+ if ( 'pod' === $pick_object ) {
1444
  $pod = $pick_val;
1445
+ } else {
 
 
1446
  $check = $this->api->get_table_info( $pick_object, $pick_val );
1447
 
1448
+ if ( ! empty( $check ) && ! empty( $check['pod'] ) ) {
1449
+ $pod = $check['pod']['name'];
 
1450
  }
1451
  }
1452
  }
1453
+ } else {
1454
+ // Assume last iteration.
 
 
 
 
 
 
1455
  $last_loop = true;
1456
+
1457
+ if ( 0 === $key ) {
1458
+ // This is also the first loop. Assume metadata or options to traverse into.
1459
+ $last_object = $this->pod_data['object_type'];
1460
+ $last_pick_val = $this->pod_data['name'];
1461
+ }
1462
+ }//end if
1463
 
1464
  if ( $last_loop ) {
1465
  $object_type = $last_object;
1466
+ $object = $last_pick_val;
1467
 
1468
+ if ( in_array( $last_type, PodsForm::file_field_types(), true ) ) {
1469
  $object_type = 'media';
1470
+ $object = 'attachment';
1471
  }
1472
 
1473
  $data = array();
1474
 
1475
  $table = $this->api->get_table_info( $object_type, $object, null, null, $last_options );
1476
 
1477
+ $join = array();
1478
+ $where = array();
1479
 
1480
+ if ( ! empty( $table['join'] ) ) {
1481
+ $join = (array) $table['join'];
1482
+ }
1483
 
1484
+ if ( ! empty( $ids ) || ! empty( $table['where'] ) ) {
1485
  foreach ( $ids as $id ) {
1486
+ $where[ $id ] = '`t`.`' . $table['field_id'] . '` = ' . (int) $id;
1487
  }
1488
 
1489
+ if ( ! empty( $where ) ) {
1490
  $where = array( implode( ' OR ', $where ) );
1491
  }
1492
 
1493
+ if ( ! empty( $table['where'] ) ) {
1494
+ // @codingStandardsIgnoreLine.
1495
+ $where = array_merge( $where, array_values( (array) $table['where'] ) );
1496
  }
1497
  }
1498
 
1499
  /**
1500
+ * Related object.
1501
+ *
1502
+ * @var $related_obj Pods|false
1503
  */
1504
  $related_obj = false;
1505
 
1506
+ // Check if we can return the full object/array or if we need to traverse into it.
1507
+ $is_field_output_full = false;
1508
+
1509
+ if ( false !== $field_exists && ( in_array( $last_type, $tableless_field_types, true ) && ! $simple ) ) {
1510
+ $is_field_output_full = true;
1511
+ }
1512
+
1513
+ if ( 'pod' === $object_type ) {
1514
  $related_obj = pods( $object, null, false );
1515
+ } elseif ( ! empty( $table['pod'] ) ) {
1516
+ $related_obj = pods( $table['pod']['name'], null, false );
1517
+ }
1518
 
1519
+ if ( $related_obj || ! empty( $table['table'] ) ) {
1520
  $sql = array(
1521
+ 'select' => '*, `t`.`' . $table['field_id'] . '` AS `pod_item_id`',
1522
+ 'table' => $table['table'],
1523
+ 'join' => $join,
1524
+ 'where' => $where,
1525
+ 'orderby' => $params->orderby,
1526
  'pagination' => false,
1527
+ 'search' => false,
1528
+ 'limit' => - 1,
1529
+ 'expires' => 180,
1530
+ // @todo This could potentially cause issues if someone changes the data within this time and persistent storage is used.
1531
  );
1532
 
1533
+ if ( ! empty( $table['where_default'] ) ) {
1534
+ $sql['where_default'] = $table['where_default'];
1535
+ }
1536
+
1537
+ // Output types.
1538
+ if ( in_array( $params->output, array( 'ids', 'objects', 'pods' ), true ) ) {
1539
+ $sql['select'] = '`t`.`' . $table['field_id'] . '` AS `pod_item_id`';
1540
+ } elseif ( 'names' === $params->output && ! empty( $table['field_index'] ) ) {
1541
+ $sql['select'] = '`t`.`' . $table['field_index'] . '` AS `pod_item_index`, `t`.`' . $table['field_id'] . '` AS `pod_item_id`';
1542
+ }
1543
 
1544
+ if ( ! empty( $params->params ) && is_array( $params->params ) ) {
1545
+ $where = $sql['where'];
1546
 
1547
+ // @codingStandardsIgnoreLine.
1548
  $sql = array_merge( $sql, $params->params );
1549
 
1550
+ if ( isset( $params->params['where'] ) ) {
1551
+ // @codingStandardsIgnoreLine.
1552
+ $sql['where'] = array_merge( (array) $where, (array) $params->params['where'] );
1553
+ }
1554
  }
1555
 
1556
+ $item_data = array();
1557
+
1558
+ if ( ! $related_obj || ! $related_obj->valid() ) {
1559
+ if ( ! is_object( $this->alt_data ) ) {
1560
  $this->alt_data = pods_data( null, 0, true, true );
1561
+ }
1562
 
1563
  $item_data = $this->alt_data->select( $sql );
1564
+ } else {
1565
+ // Support 'find' output ordering.
1566
+ if ( $ids && 'find' === $params->output && $is_field_output_full && empty( $sql['orderby'] ) ) {
1567
+ // Handle default orderby for ordering by the IDs.
1568
+ $order_ids = implode( ', ', array_map( 'absint', $ids ) );
1569
+
1570
+ $sql['orderby'] = 'FIELD( `t`.`' . $table['field_id'] . '`, ' . $order_ids . ' )';
1571
+ }
1572
+
1573
+ $related_obj->find( $sql );
1574
+
1575
+ // Support 'find' output.
1576
+ if ( 'find' === $params->output && $is_field_output_full ) {
1577
+ $data = $related_obj;
1578
+
1579
+ $is_field_output_full = true;
1580
+ } else {
1581
+ $item_data = $related_obj->data();
1582
+ }
1583
+ }//end if
1584
 
1585
  $items = array();
1586
 
1587
+ if ( ! empty( $item_data ) ) {
1588
  foreach ( $item_data as $item ) {
1589
+ if ( is_array( $item ) ) {
1590
  $item = (object) $item;
1591
+ }
1592
 
1593
+ if ( empty( $item->pod_item_id ) ) {
1594
  continue;
1595
+ }
1596
 
1597
+ // Bypass pass field.
1598
+ if ( isset( $item->user_pass ) ) {
1599
  unset( $item->user_pass );
1600
+ }
1601
 
1602
+ // Get Item ID.
1603
  $item_id = $item->pod_item_id;
1604
 
1605
+ // Output types.
1606
+ if ( 'ids' === $params->output ) {
 
 
 
1607
  $item = (int) $item_id;
1608
+ } elseif ( 'names' === $params->output && ! empty( $table['field_index'] ) ) {
1609
  $item = $item->pod_item_index;
1610
+ } elseif ( 'objects' === $params->output ) {
1611
+ if ( in_array( $object_type, array( 'post_type', 'media' ), true ) ) {
1612
  $item = get_post( $item_id );
1613
+ } elseif ( 'taxonomy' === $object_type ) {
1614
  $item = get_term( $item_id, $object );
1615
+ } elseif ( 'user' === $object_type ) {
1616
  $item = get_userdata( $item_id );
1617
 
1618
+ if ( ! empty( $item ) ) {
1619
+ // Get other vars.
1620
+ $roles = $item->roles;
1621
+ $caps = $item->caps;
1622
  $allcaps = $item->allcaps;
1623
 
1624
  $item = $item->data;
1625
 
1626
+ // Set other vars.
1627
+ $item->roles = $roles;
1628
+ $item->caps = $caps;
1629
  $item->allcaps = $allcaps;
1630
 
1631
  unset( $item->user_pass );
1632
  }
1633
+ } elseif ( 'comment' === $object_type ) {
 
1634
  $item = get_comment( $item_id );
1635
+ } else {
1636
  $item = (object) $item;
1637
+ }//end if
1638
+ } elseif ( 'pods' === $params->output ) {
1639
+ if ( in_array( $object_type, array( 'user', 'media' ), true ) ) {
1640
+ $item = pods( $object_type, (int) $item_id );
1641
+ } else {
1642
+ $item = pods( $object, (int) $item_id );
1643
+ }
1644
+ } else {
1645
+ // arrays.
1646
  $item = get_object_vars( (object) $item );
1647
+ }//end if
1648
 
1649
+ // Pass item data into $data.
1650
  $items[ $item_id ] = $item;
1651
+ }//end foreach
1652
 
1653
+ // Cleanup.
1654
  unset( $item_data );
1655
 
1656
+ // Return all of the data in the order expected.
1657
  if ( empty( $params->orderby ) ) {
1658
  foreach ( $ids as $id ) {
1659
+ if ( isset( $items[ $id ] ) ) {
1660
  $data[ $id ] = $items[ $id ];
1661
+ }
1662
+ }
1663
+ } else {
1664
+ // Use order set by orderby.
1665
+ foreach ( $items as $id => $v ) {
1666
+ // @codingStandardsIgnoreLine.
1667
+ if ( in_array( $id, $ids ) ) {
1668
+ $data[ $id ] = $v;
1669
+ }
1670
  }
1671
  }
1672
+ }//end if
1673
+ }//end if
1674
+
1675
+ if ( in_array( $last_type, $tableless_field_types, true ) || in_array( $last_type, array(
1676
+ 'boolean',
1677
+ 'number',
1678
+ 'currency',
1679
+ ), true ) ) {
1680
  $params->raw = true;
1681
+ }
1682
 
1683
+ if ( empty( $data ) ) {
1684
  $value = false;
1685
+ } else {
1686
+ $object_type = $table['type'];
1687
 
1688
+ if ( in_array( $table['type'], array( 'post_type', 'attachment', 'media' ), true ) ) {
1689
  $object_type = 'post';
1690
+ }
1691
+
1692
 
1693
+ $object_no_conflict = in_array( $object_type, array( 'post', 'taxonomy', 'user', 'comment', 'settings' ), true );
1694
 
1695
+ $no_conflict = pods_no_conflict_check( $object_type );
 
1696
 
1697
+ if ( $object_no_conflict && ! $no_conflict ) {
1698
+ // Temporarily enable no conflict.
1699
+ pods_no_conflict_on( $object_type );
1700
  }
1701
 
1702
+ if ( $is_field_output_full ) {
1703
+ // Return entire array.
1704
  $value = $data;
1705
+ } else {
1706
+ // Return an array of single column values.
1707
  $value = array();
1708
 
1709
  foreach ( $data as $item_id => $item ) {
1710
  // $field is 123x123, needs to be _src.123x123
1711
+ $traverse_fields = array_splice( $params->traverse, $key );
1712
+ $full_field = implode( '.', $traverse_fields );
1713
+ array_shift( $traverse_fields );
1714
 
1715
+ if ( is_array( $item ) && isset( $item[ $field ] ) ) {
1716
+ if ( $table['field_id'] === $field ) {
1717
+ $value[] = (int) $item[ $field ];
1718
+ } else {
1719
+ $value[] = $item[ $field ];
1720
+ }
1721
+ } elseif ( is_object( $item ) && isset( $item->{$field} ) ) {
1722
+ if ( $table['field_id'] === $field ) {
1723
+ $value[] = (int) $item->{$field};
1724
+ } else {
1725
+ $value[] = $item->{$field};
1726
+ }
1727
+ } elseif ( ! empty( $related_obj ) && 0 === strpos( $full_field, 'post_thumbnail' ) ) {
1728
+ // We want to catch post_thumbnail and post_thumbnail_url here
1729
+ $value[] = $related_obj->field( $full_field );
1730
+ } elseif (
1731
+ (
1732
+ ( false !== strpos( $full_field, '_src' ) || 'guid' === $field )
1733
+ && (
1734
+ in_array( $table['type'], array( 'attachment', 'media' ), true )
1735
+ || in_array( $last_type, PodsForm::file_field_types(), true )
1736
+ )
1737
+ )
1738
+ || ( in_array( $field, $permalink_fields, true ) && in_array( $last_type, PodsForm::file_field_types(), true ) )
1739
+ ) {
1740
+ // @todo Refactor the above condition statement.
1741
  $size = 'full';
1742
 
1743
+ if ( ! wp_attachment_is_image( $item_id ) ) {
1744
+ // No default sizes for non-images.
1745
+ // When a size is defined this will be overwritten.
1746
+ $size = null;
1747
+ }
1748
+
1749
+ if ( false !== strpos( $full_field, '_src.' ) && 5 < strlen( $full_field ) ) {
1750
  $size = substr( $full_field, 5 );
1751
+ } elseif ( false !== strpos( $full_field, '_src_relative.' ) && 14 < strlen( $full_field ) ) {
1752
  $size = substr( $full_field, 14 );
1753
+ } elseif ( false !== strpos( $full_field, '_src_schemeless.' ) && 16 < strlen( $full_field ) ) {
1754
  $size = substr( $full_field, 16 );
1755
+ }
1756
 
1757
+ if ( $size ) {
1758
+ $value_url = pods_image_url( $item_id, $size, 0, true );
1759
+ } else {
1760
+ $value_url = wp_get_attachment_url( $item_id );
1761
+ }
1762
 
1763
+ if ( ! empty( $value_url ) ) {
1764
+ if ( false !== strpos( $full_field, '_src_relative' ) ) {
1765
+ $value_url_parsed = wp_parse_url( $value_url );
1766
+ $value_url = $value_url_parsed['path'];
1767
+ } elseif ( false !== strpos( $full_field, '_src_schemeless' ) ) {
1768
+ $value_url = str_replace( array(
1769
+ 'http://',
1770
+ 'https://',
1771
+ ), '//', $value_url );
1772
+ }
1773
  }
 
 
1774
 
1775
+ if ( ! empty( $value_url ) ) {
1776
  $value[] = $value_url;
1777
+ }
1778
 
1779
  $params->raw_display = true;
1780
+ } elseif ( false !== strpos( $full_field, '_img' ) && ( in_array( $table['type'], array(
1781
+ 'attachment',
1782
+ 'media',
1783
+ ), true ) || in_array( $last_type, PodsForm::file_field_types(), true ) ) ) {
1784
  $size = 'full';
1785
 
1786
+ if ( false !== strpos( $full_field, '_img.' ) && 5 < strlen( $full_field ) ) {
1787
  $size = substr( $full_field, 5 );
1788
+ }
1789
 
1790
+ $value[] = pods_image( $item_id, $size, 0, array(), true );
1791
 
1792
  $params->raw_display = true;
1793
+ } elseif ( in_array( $field, array(
1794
+ '_link',
1795
+ 'detail_url',
1796
+ ), true ) || in_array( $field, array( 'permalink', 'the_permalink' ), true ) ) {
1797
+ if ( 'pod' === $object_type ) {
1798
  if ( is_object( $related_obj ) ) {
1799
  $related_obj->fetch( $item_id );
1800
 
1801
  $value[] = $related_obj->field( 'detail_url' );
1802
+ } else {
 
1803
  $value[] = '';
1804
+ }
1805
+ } elseif ( 'post' === $object_type ) {
1806
  $value[] = get_permalink( $item_id );
1807
+ } elseif ( 'taxonomy' === $object_type ) {
1808
  $value[] = get_term_link( $item_id, $object );
1809
+ } elseif ( 'user' === $object_type ) {
1810
  $value[] = get_author_posts_url( $item_id );
1811
+ } elseif ( 'comment' === $object_type ) {
1812
  $value[] = get_comment_link( $item_id );
1813
+ } else {
1814
  $value[] = '';
1815
+ }
1816
 
1817
  $params->raw_display = true;
1818
+ } elseif ( in_array( $object_type, array(
1819
+ 'post',
1820
+ 'taxonomy',
1821
+ 'user',
1822
+ 'comment',
1823
+ ), true ) ) {
1824
+ $metadata_object_id = $item_id;
1825
+
1826
+ $metadata_type = $object_type;
1827
+
1828
+ if ( 'post' === $object_type ) {
1829
+ // Support for WPML 'duplicated' translation handling.
1830
+ if ( did_action( 'wpml_loaded' ) && apply_filters( 'wpml_is_translated_post_type', false, $object ) ) {
1831
+ $master_post_id = (int) apply_filters( 'wpml_master_post_from_duplicate', $metadata_object_id );
1832
+
1833
+ if ( 0 < $master_post_id ) {
1834
+ $metadata_object_id = $master_post_id;
1835
+ }
1836
+ }
1837
+ } elseif ( 'taxonomy' === $object_type ) {
1838
+ $metadata_type = 'term';
1839
  }
1840
 
1841
+ $meta_value = get_metadata( $metadata_type, $metadata_object_id, $field, true );
1842
+ $value[] = pods_traverse( $traverse_fields, $meta_value );
1843
+ } elseif ( 'settings' === $object_type ) {
1844
+ $option_value = get_option( $object . '_' . $field );
1845
+ $value[] = pods_traverse( $traverse_fields, $option_value );
1846
+ }//end if
1847
+ }//end foreach
1848
+ }//end if
1849
+
1850
+ if ( $object_no_conflict && ! $no_conflict ) {
1851
+ // Revert temporarily no conflict mode.
1852
  pods_no_conflict_off( $object_type );
1853
+ }
1854
 
1855
+ // Handle Simple Relationships.
1856
  if ( $simple ) {
1857
+ if ( null === $params->single ) {
1858
  $params->single = false;
1859
+ }
1860
 
1861
  $value = PodsForm::field_method( 'pick', 'simple_value', $field, $value, $last_options, $all_fields[ $pod ], 0, true );
1862
+ } elseif ( false === $params->in_form && ! empty( $value ) && is_array( $value ) && false === $params->keyed ) {
 
1863
  $value = array_values( $value );
1864
+ }
1865
 
1866
+ // Return a single column value.
1867
+ if ( false === $params->in_form && 1 === $limit && ! empty( $value ) && is_array( $value ) && 1 === count( $value ) ) {
1868
  $value = current( $value );
1869
+ }
1870
+ }//end if
1871
+
1872
+ if ( $last_options ) {
1873
+ $last_field_data = $last_options;
1874
+ } elseif ( isset( $related_obj, $related_obj->fields, $related_obj->fields[ $field ] ) ) {
1875
+ // Save related field data for later to be used for display formatting
1876
+ $last_field_data = $related_obj->fields[ $field ];
1877
  }
1878
 
1879
  break;
1880
+ }//end if
1881
+ }//end foreach
1882
+ }//end if
1883
+ }//end if
1884
+ }//end if
1885
 
1886
+ if ( ! empty( $params->traverse ) && 1 < count( $params->traverse ) ) {
1887
  $field_names = implode( '.', $params->traverse );
1888
 
1889
  $this->row[ $field_names ] = $value;
1890
+ } elseif ( 'arrays' !== $params->output && in_array( $field_data['type'], $tableless_field_types, true ) ) {
 
1891
  $this->row[ '_' . $params->output . '_' . $params->full_name ] = $value;
1892
+ } elseif ( 'arrays' === $params->output || ! in_array( $field_data['type'], $tableless_field_types, true ) ) {
 
1893
  $this->row[ $params->full_name ] = $value;
1894
  }
1895
 
1896
+ if ( true === $params->single && is_array( $value ) && 1 === count( $value ) ) {
1897
  $value = current( $value );
1898
+ }
1899
+
1900
+ if ( ! empty( $last_field_data ) ) {
1901
+ $field_data = $last_field_data;
1902
+ }
1903
 
1904
+ if ( ! empty( $field_data ) && ( $params->display || ! $params->raw ) && ! $params->in_form && ! $params->raw_display ) {
1905
+ if ( $params->display || ( ( $params->get_meta || $params->deprecated ) && ! in_array( $field_data['type'], $tableless_field_types, true ) ) ) {
1906
+ $field_data['options'] = pods_v( 'options', $field_data, array(), true );
 
1907
 
1908
+ $post_temp = false;
1909
+ $old_post = null;
1910
+ $old_post_id = null;
1911
+ $post_ID = null;
1912
 
1913
+ if ( empty( $GLOBALS['post'] ) && 'post_type' === pods_v( 'type', $this->pod_data ) && 0 < $this->id() ) {
1914
  global $post_ID, $post;
1915
 
1916
  $post_temp = true;
1917
 
1918
+ $old_post = $post;
1919
+ $old_post_id = $post_ID;
1920
 
1921
+ // @codingStandardsIgnoreLine.
1922
+ $post = get_post( $this->id() );
1923
+ // @codingStandardsIgnoreLine.
1924
  $post_ID = $this->id();
1925
  }
1926
 
1927
+ $filter = pods_v( 'display_filter', $field_data['options'] );
1928
 
1929
  if ( 0 < strlen( $filter ) ) {
1930
  $args = array(
1931
  $filter,
1932
+ $value,
1933
  );
1934
 
1935
+ $filter_args = pods_v( 'display_filter_args', $field_data['options'] );
1936
 
1937
+ if ( ! empty( $filter_args ) ) {
1938
  $args = array_merge( $args, compact( $filter_args ) );
1939
+ }
1940
 
1941
  $value = call_user_func_array( 'apply_filters', $args );
1942
+ } elseif ( 1 === (int) pods_v( 'display_process', $field_data['options'], 1 ) ) {
1943
+ $value = PodsForm::display( $field_data['type'], $value, $params->name, array_merge( $field_data, $field_data['options'] ), $this->pod_data, $this->id() );
 
 
 
 
 
 
 
 
1944
  }
1945
 
1946
  if ( $post_temp ) {
1947
+ // @codingStandardsIgnoreLine.
1948
+ $post = $old_post;
1949
+ // @codingStandardsIgnoreLine.
1950
+ $post_ID = $old_post_id;
1951
  }
1952
+ } else {
1953
+ $value = PodsForm::value( $field_data['type'], $value, $params->name, array_merge( $field_data, $field_data['options'] ), $this->pod_data, $this->id() );
1954
+ }//end if
1955
+ }//end if
1956
+
1957
+ /**
1958
+ * Modify value returned by field() directly before output.
1959
+ *
1960
+ * Will not run if value was null
1961
+ *
1962
+ * @since unknown
1963
+ *
1964
+ * @param array|string|null $value Value to be returned.
1965
+ * @param array|object $row Current row being outputted.
1966
+ * @param array $params Params array passed to field().
1967
+ * @param object|Pods $this Current Pods object.
1968
+ */
1969
+ $value = apply_filters( 'pods_pods_field', $value, $this->row, $params, $this );
1970
 
1971
  return $value;
1972
  }
1974
  /**
1975
  * Check if an item field has a specific value in it
1976
  *
1977
+ * @param string $field Field name.
1978
+ * @param mixed $value Value to check.
1979
+ * @param int $id (optional) ID of the pod item to check.
1980
  *
1981
  * @return bool Whether the value was found
1982
  *
1983
  * @since 2.3.3
1984
  */
1985
+ public function has( $field, $value, $id = null ) {
1986
+
1987
  $pod =& $this;
1988
 
1989
+ if ( null === $id ) {
1990
  $id = $this->id();
1991
+ // @codingStandardsIgnoreLine.
1992
+ } elseif ( $id != $this->id() ) {
1993
  $pod = pods( $this->pod, $id );
1994
+ }
1995
 
1996
  $this->do_hook( 'has', $field, $value, $id );
1997
 
1998
+ if ( ! isset( $this->fields[ $field ] ) ) {
1999
  return false;
2000
+ }
2001
 
2002
+ // Tableless fields.
2003
+ if ( in_array( $this->fields[ $field ]['type'], PodsForm::tableless_field_types(), true ) ) {
2004
+ if ( ! is_array( $value ) ) {
2005
  $value = explode( ',', $value );
2006
+ }
2007
 
2008
+ if ( 'pick' === $this->fields[ $field ]['type'] && in_array( $this->fields[ $field ]['pick_object'], PodsForm::simple_tableless_objects(), true ) ) {
2009
  $current_value = $pod->raw( $field );
2010
 
2011
+ if ( ! empty( $current_value ) ) {
2012
  $current_value = (array) $current_value;
2013
+ }
2014
 
2015
  foreach ( $current_value as $v ) {
2016
+ // @codingStandardsIgnoreLine.
2017
+ if ( in_array( $v, $value ) ) {
2018
  return true;
2019
+ }
2020
  }
2021
+ } else {
2022
+ $related_ids = $this->api->lookup_related_items( $this->fields[ $field ]['id'], $this->pod_data['id'], $id, $this->fields[ $field ], $this->pod_data );
 
2023
 
2024
  foreach ( $value as $k => $v ) {
2025
+ if ( ! preg_match( '/[^\D]/', $v ) ) {
2026
  $value[ $k ] = (int) $v;
 
 
 
2027
  }
2028
+
2029
+ // @todo Convert slugs into IDs.
2030
  }
2031
 
2032
  foreach ( $related_ids as $v ) {
2033
+ // @codingStandardsIgnoreLine.
2034
+ if ( in_array( $v, $value ) ) {
2035
  return true;
2036
+ }
2037
  }
2038
+ }//end if
2039
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::text_field_types(), true ) ) {
2040
+ // Text fields.
 
2041
  $current_value = $pod->raw( $field );
2042
 
2043
+ if ( 0 < strlen( $current_value ) ) {
2044
  return stripos( $current_value, $value );
2045
+ }
2046
+ } else {
2047
+ // All other fields.
2048
  return $this->is( $field, $value, $id );
2049
+ }//end if
2050
 
2051
  return false;
2052
  }
2054
  /**
2055
  * Check if an item field is a specific value
2056
  *
2057
+ * @param string $field Field name.
2058
+ * @param mixed $value Value to check.
2059
+ * @param int $id (optional) ID of the pod item to check.
2060
  *
2061
  * @return bool Whether the value was found
2062
  *
2063
  * @since 2.3.3
2064
  */
2065
+ public function is( $field, $value, $id = null ) {
2066
+
2067
  $pod =& $this;
2068
 
2069
+ if ( null === $id ) {
2070
  $id = $this->id();
2071
+ // @codingStandardsIgnoreLine.
2072
+ } elseif ( $id != $this->id() ) {
2073
  $pod = pods( $this->pod, $id );
2074
+ }
2075
 
2076
  $this->do_hook( 'is', $field, $value, $id );
2077
 
2078
+ if ( ! isset( $this->fields[ $field ] ) ) {
2079
  return false;
2080
+ }
2081
 
2082
+ // Tableless fields.
2083
+ if ( in_array( $this->fields[ $field ]['type'], PodsForm::tableless_field_types(), true ) ) {
2084
+ if ( ! is_array( $value ) ) {
2085
  $value = explode( ',', $value );
2086
+ }
2087
 
2088
  $current_value = array();
2089
 
2090
+ if ( 'pick' === $this->fields[ $field ]['type'] && in_array( $this->fields[ $field ]['pick_object'], PodsForm::simple_tableless_objects(), true ) ) {
2091
  $current_value = $pod->raw( $field );
2092
 
2093
+ if ( ! empty( $current_value ) ) {
2094
  $current_value = (array) $current_value;
2095
+ }
2096
 
2097
  foreach ( $current_value as $v ) {
2098
+ // @codingStandardsIgnoreLine.
2099
+ if ( in_array( $v, $value ) ) {
2100
  return true;
2101
+ }
2102
  }
2103
+ } else {
2104
+ $related_ids = $this->api->lookup_related_items( $this->fields[ $field ]['id'], $this->pod_data['id'], $id, $this->fields[ $field ], $this->pod_data );
 
2105
 
2106
  foreach ( $value as $k => $v ) {
2107
+ if ( ! preg_match( '/[^\D]/', $v ) ) {
2108
  $value[ $k ] = (int) $v;
 
 
 
2109
  }
2110
+
2111
+ // @todo Convert slugs into IDs.
2112
  }
2113
 
2114
  foreach ( $related_ids as $v ) {
2115
+ // @codingStandardsIgnoreLine.
2116
+ if ( in_array( $v, $value ) ) {
2117
  return true;
2118
+ }
2119
  }
2120
+ }//end if
2121
 
2122
+ if ( ! empty( $current_value ) ) {
2123
  $current_value = array_filter( array_unique( $current_value ) );
2124
+ } else {
2125
  $current_value = array();
2126
+ }
2127
 
2128
+ if ( ! empty( $value ) ) {
2129
  $value = array_filter( array_unique( $value ) );
2130
+ } else {
2131
  $value = array();
2132
+ }
2133
 
2134
  sort( $current_value );
2135
  sort( $value );
2136
 
2137
+ if ( $value === $current_value ) {
2138
  return true;
2139
+ }
2140
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::number_field_types(), true ) ) {
2141
+ // Number fields.
2142
  $current_value = $pod->raw( $field );
2143
 
2144
+ if ( (float) $current_value === (float) $value ) {
2145
  return true;
2146
+ }
2147
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::date_field_types(), true ) ) {
2148
+ // Date fields.
2149
  $current_value = $pod->raw( $field );
2150
 
2151
  if ( 0 < strlen( $current_value ) ) {
2152
+ if ( strtotime( $current_value ) === strtotime( $value ) ) {
2153
  return true;
2154
+ }
2155
+ } elseif ( empty( $value ) ) {
2156
  return true;
2157
+ }
2158
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::text_field_types(), true ) ) {
2159
+ // Text fields.
2160
  $current_value = $pod->raw( $field );
2161
 
2162
+ if ( (string) $current_value === (string) $value ) {
2163
  return true;
2164
+ }
2165
+ } else {
2166
+ // All other fields.
2167
  $current_value = $pod->raw( $field );
2168
 
2169
+ if ( $current_value === $value ) {
2170
  return true;
2171
+ }
2172
+ }//end if
2173
 
2174
  return false;
2175
  }
2178
  * Return the item ID
2179
  *
2180
  * @return int
2181
+ * @since 2.0.0
2182
  */
2183
+ public function id() {
2184
+
2185
+ if ( isset( $this->data->row, $this->data->row['id'] ) ) {
2186
+ // If we already have data loaded return that ID.
2187
+ return $this->data->row['id'];
2188
+ }
2189
+
2190
  return $this->field( $this->data->field_id );
2191
  }
2192
 
2193
  /**
2194
  * Return the previous item ID, loops at the last id to return the first
2195
  *
2196
+ * @param int|null $id ID to start from.
2197
+ * @param array|object|null $params_override Override the find() parameters.
2198
  *
2199
  * @return int
2200
+ * @since 2.0.0
2201
  */
2202
+ public function prev_id( $id = null, $params_override = null ) {
2203
+
2204
+ if ( null === $id ) {
2205
  $id = $this->id();
2206
+ }
2207
 
2208
  $id = (int) $id;
2209
 
2210
  $params = array(
2211
+ 'select' => "`t`.`{$this->data->field_id}`",
2212
+ 'where' => "`t`.`{$this->data->field_id}` < {$id}",
2213
  'orderby' => "`t`.`{$this->data->field_id}` DESC",
2214
+ 'limit' => 1,
2215
  );
2216
 
2217
+ if ( ! empty( $params_override ) || ! empty( $this->params ) ) {
2218
+ if ( ! empty( $params_override ) ) {
2219
  $params = $params_override;
2220
+ } elseif ( ! empty( $this->params ) ) {
2221
  $params = $this->params;
2222
+ }
2223
 
2224
  if ( is_object( $params ) ) {
2225
  $params = get_object_vars( $params );
2226
  }
2227
 
2228
  if ( 0 < $id ) {
2229
+ if ( isset( $params['where'] ) && ! empty( $params['where'] ) ) {
2230
+ $params['where'] = (array) $params['where'];
2231
+ $params['where'][] = "`t`.`{$this->data->field_id}` < {$id}";
2232
+ } else {
2233
+ $params['where'] = "`t`.`{$this->data->field_id}` < {$id}";
 
2234
  }
2235
+ } elseif ( isset( $params['offset'] ) && 0 < $params['offset'] ) {
2236
+ $params['offset'] --;
2237
+ } elseif ( 0 < $this->row_number && ! isset( $params['offset'] ) && ! empty( $this->params ) ) {
2238
+ $params['offset'] = $this->row_number - 1;
2239
+ } else {
 
2240
  return 0;
2241
+ }
2242
 
2243
+ if ( isset( $params['orderby'] ) && ! empty( $params['orderby'] ) ) {
2244
+ if ( is_array( $params['orderby'] ) ) {
2245
+ foreach ( $params['orderby'] as $orderby => $dir ) {
2246
  $dir = strtoupper( $dir );
2247
 
2248
+ if ( ! in_array( $dir, array( 'ASC', 'DESC' ), true ) ) {
2249
  continue;
2250
  }
2251
 
2252
+ if ( 'ASC' === $dir ) {
2253
+ $params['orderby'][ $orderby ] = 'DESC';
2254
+ } else {
2255
+ $params['orderby'][ $orderby ] = 'ASC';
 
2256
  }
2257
  }
2258
 
2259
+ $params['orderby'][ $this->data->field_id ] = 'DESC';
2260
+ } elseif ( "`t`.`{$this->data->field_id}` DESC" !== $params['orderby'] ) {
2261
+ $params['orderby'] .= ", `t`.`{$this->data->field_id}` DESC";
2262
  }
2263
+ }//end if
 
 
 
2264
 
2265
+ $params['select'] = "`t`.`{$this->data->field_id}`";
2266
+ $params['limit'] = 1;
2267
+ }//end if
2268
 
2269
  $pod = pods( $this->pod, $params );
2270
 
2271
  $new_id = 0;
2272
 
2273
+ if ( $pod->fetch() ) {
2274
  $new_id = $pod->id();
2275
+ }
2276
 
2277
  $new_id = $this->do_hook( 'prev_id', $new_id, $id, $pod, $params_override );
2278
 
2282
  /**
2283
  * Return the next item ID, loops at the first id to return the last
2284
  *
2285
+ * @param int|null $id ID to start from.
2286
+ * @param array|object|null $params_override Override the find() parameters.
2287
  *
2288
  * @return int
2289
+ * @since 2.0.0
2290
  */
2291
+ public function next_id( $id = null, $params_override = null ) {
2292
+
2293
+ if ( null === $id ) {
2294
  $id = $this->id();
2295
+ }
2296
 
2297
  $id = (int) $id;
2298
 
2299
  $params = array(
2300
+ 'select' => "`t`.`{$this->data->field_id}`",
2301
+ 'where' => "{$id} < `t`.`{$this->data->field_id}`",
2302
  'orderby' => "`t`.`{$this->data->field_id}` ASC",
2303
+ 'limit' => 1,
2304
  );
2305
 
2306
+ if ( ! empty( $params_override ) || ! empty( $this->params ) ) {
2307
+ if ( ! empty( $params_override ) ) {
2308
  $params = $params_override;
2309
+ } elseif ( ! empty( $this->params ) ) {
2310
  $params = $this->params;
2311
+ }
2312
 
2313
  if ( is_object( $params ) ) {
2314
  $params = get_object_vars( $params );
2315
  }
2316
 
2317
  if ( 0 < $id ) {
2318
+ if ( isset( $params['where'] ) && ! empty( $params['where'] ) ) {
2319
+ $params['where'] = (array) $params['where'];
2320
+ $params['where'][] = "{$id} < `t`.`{$this->data->field_id}`";
2321
+ } else {
2322
+ $params['where'] = "{$id} < `t`.`{$this->data->field_id}`";
2323
  }
2324
+ } elseif ( ! isset( $params['offset'] ) ) {
2325
+ if ( ! empty( $this->params ) && - 1 < $this->row_number ) {
2326
+ $params['offset'] = $this->row_number + 1;
2327
+ } else {
2328
+ $params['offset'] = 1;
2329
  }
2330
+ } else {
2331
+ $params['offset'] ++;
2332
  }
 
 
 
 
 
 
 
 
2333
 
2334
+ $params['select'] = "`t`.`{$this->data->field_id}`";
2335
+ $params['limit'] = 1;
2336
+ }//end if
2337
 
2338
  $pod = pods( $this->pod, $params );
2339
 
2340
  $new_id = 0;
2341
 
2342
+ if ( $pod->fetch() ) {
2343
  $new_id = $pod->id();
2344
+ }
2345
 
2346
  $new_id = $this->do_hook( 'next_id', $new_id, $id, $pod, $params_override );
2347
 
2351
  /**
2352
  * Return the first item ID
2353
  *
2354
+ * @param array|object|null $params_override Override the find() parameters.
2355
  *
2356
  * @return int
2357
+ * @since 2.3.0
2358
  */
2359
+ public function first_id( $params_override = null ) {
2360
+
2361
  $params = array(
2362
+ 'select' => "`t`.`{$this->data->field_id}`",
2363
  'orderby' => "`t`.`{$this->data->field_id}` ASC",
2364
+ 'limit' => 1,
2365
  );
2366
 
2367
+ if ( ! empty( $params_override ) || ! empty( $this->params ) ) {
2368
+ if ( ! empty( $params_override ) ) {
2369
  $params = $params_override;
2370
+ } elseif ( ! empty( $this->params ) ) {
2371
  $params = $this->params;
2372
+ }
2373
 
2374
  if ( is_object( $params ) ) {
2375
  $params = get_object_vars( $params );
2376
  }
2377
 
2378
+ $params['select'] = "`t`.`{$this->data->field_id}`";
2379
+ $params['offset'] = 0;
2380
+ $params['limit'] = 1;
2381
  }
2382
 
2383
  $pod = pods( $this->pod, $params );
2384
 
2385
  $new_id = 0;
2386
 
2387
+ if ( $pod->fetch() ) {
2388
  $new_id = $pod->id();
2389
+ }
2390
 
2391
  $new_id = $this->do_hook( 'first_id', $new_id, $pod, $params_override );
2392
 
2396
  /**
2397
  * Return the last item ID
2398
  *
2399
+ * @param array|object|null $params_override Override the find() parameters.
2400
  *
2401
  * @return int
2402
+ * @since 2.3.0
2403
  */
2404
+ public function last_id( $params_override = null ) {
2405
+
2406
  $params = array(
2407
+ 'select' => "`t`.`{$this->data->field_id}`",
2408
  'orderby' => "`t`.`{$this->data->field_id}` DESC",
2409
+ 'limit' => 1,
2410
  );
2411
 
2412
+ if ( ! empty( $params_override ) || ! empty( $this->params ) ) {
2413
+ if ( ! empty( $params_override ) ) {
2414
  $params = $params_override;
2415
+ } elseif ( ! empty( $this->params ) ) {
2416
  $params = $this->params;
2417
+ }
2418
 
2419
  if ( is_object( $params ) ) {
2420
  $params = get_object_vars( $params );
2421
  }
2422
 
2423
+ if ( isset( $params['total_found'] ) ) {
2424
+ $params['offset'] = $params['total_found'] - 1;
2425
+ } else {
2426
+ $params['offset'] = $this->total_found() - 1;
2427
+ }
2428
 
2429
+ if ( isset( $params['orderby'] ) && ! empty( $params['orderby'] ) ) {
2430
+ if ( is_array( $params['orderby'] ) ) {
2431
+ foreach ( $params['orderby'] as $orderby => $dir ) {
2432
  $dir = strtoupper( $dir );
2433
 
2434
+ if ( ! in_array( $dir, array( 'ASC', 'DESC' ), true ) ) {
2435
  continue;
2436
  }
2437
 
2438
+ if ( 'ASC' === $dir ) {
2439
+ $params['orderby'][ $orderby ] = 'DESC';
2440
+ } else {
2441
+ $params['orderby'][ $orderby ] = 'ASC';
 
2442
  }
2443
  }
2444
 
2445
+ $params['orderby'][ $this->data->field_id ] = 'DESC';
2446
+ } elseif ( "`t`.`{$this->data->field_id}` DESC" !== $params['orderby'] ) {
2447
+ $params['orderby'] .= ", `t`.`{$this->data->field_id}` DESC";
 
2448
  }
2449
+ }//end if
2450
 
2451
+ $params['select'] = "`t`.`{$this->data->field_id}`";
2452
+ $params['limit'] = 1;
2453
+ }//end if
2454
 
2455
  $pod = pods( $this->pod, $params );
2456
 
2457
  $new_id = 0;
2458
 
2459
+ if ( $pod->fetch() ) {
2460
  $new_id = $pod->id();
2461
+ }
2462
 
2463
  $new_id = $this->do_hook( 'last_id', $new_id, $pod, $params_override );
2464
+
2465
+ return $new_id;
2466
  }
2467
 
2468
  /**
2469
  * Return the item name
2470
  *
2471
  * @return string
2472
+ * @since 2.0.0
2473
  */
2474
+ public function index() {
2475
+
2476
  return $this->field( $this->data->field_index );
2477
  }
2478
 
2479
  /**
2480
  * Find items of a pod, much like WP_Query, but with advanced table handling.
2481
  *
2482
+ * @param array|object $params An associative array of parameters.
2483
+ * @param int $limit (deprecated) Limit the number of items to find, -1 to return all items with no limit.
2484
+ * @param string $where (deprecated) SQL WHERE declaration to use.
2485
+ * @param string $sql (deprecated) For advanced use, a custom SQL query to run.
2486
  *
2487
  * @return \Pods The pod object
2488
+ * @since 2.0.0
2489
+ * @link https://pods.io/docs/find/
2490
  */
2491
+ public function find( $params = null, $limit = 15, $where = null, $sql = null ) {
2492
+
2493
+ $tableless_field_types = PodsForm::tableless_field_types();
2494
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
2495
 
2496
  $this->params = $params;
2497
 
2498
  $select = '`t`.*';
2499
 
2500
+ if ( 'table' === $this->pod_data['storage'] && ! in_array( $this->pod_data['type'], array(
2501
+ 'pod',
2502
+ 'table',
2503
+ ), true ) ) {
2504
  $select .= ', `d`.*';
2505
+ }
2506
 
2507
+ if ( empty( $this->data->table ) ) {
2508
  return $this;
2509
+ }
2510
 
2511
  $defaults = array(
2512
+ // Optimization parameters.
2513
+ 'table' => $this->data->table,
2514
+ 'select' => $select,
2515
+ 'join' => null,
2516
+ // Main query parameters.
2517
+ 'where' => $where,
2518
+ 'groupby' => null,
2519
+ 'having' => null,
2520
+ 'orderby' => null,
2521
+ // Pagination parameters.
2522
+ 'limit' => (int) $limit,
2523
+ 'offset' => null,
2524
+ 'page' => (int) $this->page,
2525
+ 'page_var' => $this->page_var,
2526
+ 'pagination' => (boolean) $this->pagination,
2527
+ // Search parameters.
2528
+ 'search' => (boolean) $this->search,
2529
+ 'search_var' => $this->search_var,
2530
+ 'search_query' => null,
2531
+ 'search_mode' => $this->search_mode,
2532
+ 'search_across' => false,
2533
  'search_across_picks' => false,
2534
  'search_across_files' => false,
2535
+ // Advanced parameters.
2536
+ 'filters' => $this->filters,
2537
+ 'sql' => $sql,
2538
+ // Caching parameters.
2539
+ 'expires' => null,
2540
+ 'cache_mode' => 'cache',
2541
  );
2542
 
2543
+ if ( is_array( $params ) ) {
2544
  $params = (object) array_merge( $defaults, $params );
2545
+ } elseif ( is_object( $params ) ) {
2546
  $params = (object) array_merge( $defaults, get_object_vars( $params ) );
2547
+ } else {
2548
+ $defaults['orderby'] = $params;
2549
+
2550
  $params = (object) $defaults;
2551
  }
2552
 
2553
+ /**
2554
+ * Filter the Pods::find() parameters.
2555
+ *
2556
+ * @param object $params Parameters to make lookup with.
2557
+ */
2558
+ $params = apply_filters( 'pods_pods_find', $params );
2559
 
2560
  $params->limit = (int) $params->limit;
2561
 
2562
+ if ( 0 === $params->limit ) {
2563
+ $params->limit = - 1;
2564
+ }
2565
 
2566
+ $this->limit = (int) $params->limit;
2567
+ $this->offset = (int) $params->offset;
2568
+ $this->page = (int) $params->page;
2569
+ $this->page_var = $params->page_var;
2570
  $this->pagination = (boolean) $params->pagination;
2571
+ $this->search = (boolean) $params->search;
2572
  $this->search_var = $params->search_var;
2573
+ $params->join = (array) $params->join;
2574
 
2575
+ if ( empty( $params->search_query ) ) {
2576
+ $params->search_query = pods_v_sanitized( $this->search_var, 'get', '' );
2577
+ }
2578
 
2579
+ // Allow orderby array ( 'field' => 'asc|desc' ).
2580
+ if ( ! empty( $params->orderby ) && is_array( $params->orderby ) ) {
2581
+ foreach ( $params->orderby as $k => $orderby ) {
2582
+ if ( ! is_numeric( $k ) ) {
2583
  $key = '';
2584
 
2585
  $order = 'ASC';
2586
 
2587
+ if ( 'DESC' === strtoupper( $orderby ) ) {
2588
  $order = 'DESC';
2589
+ }
2590
 
2591
+ if ( isset( $this->fields[ $k ] ) && in_array( $this->fields[ $k ]['type'], $tableless_field_types, true ) ) {
2592
+ if ( in_array( $this->fields[ $k ]['pick_object'], $simple_tableless_objects, true ) ) {
2593
+ if ( 'table' === $this->pod_data['storage'] ) {
2594
+ if ( ! in_array( $this->pod_data['type'], array( 'pod', 'table' ), true ) ) {
2595
  $key = "`d`.`{$k}`";
2596
+ } else {
2597
  $key = "`t`.`{$k}`";
2598
+ }
2599
+ } else {
2600
  $key = "`{$k}`.`meta_value`";
2601
+ }
2602
+ } else {
2603
+ $pick_val = $this->fields[ $k ]['pick_val'];
2604
 
2605
+ if ( '__current__' === $pick_val ) {
2606
  $pick_val = $this->pod;
2607
+ }
2608
 
2609
+ $table = $this->api->get_table_info( $this->fields[ $k ]['pick_object'], $pick_val );
2610
 
2611
+ if ( ! empty( $table ) ) {
2612
+ $key = "`{$k}`.`" . $table['field_index'] . '`';
2613
+ }
2614
+ }//end if
2615
+ }//end if
2616
 
2617
  if ( empty( $key ) ) {
2618
+ if ( ! in_array( $this->pod_data['type'], array( 'pod', 'table' ), true ) ) {
2619
+ if ( isset( $this->pod_data['object_fields'][ $k ] ) ) {
2620
  $key = "`t`.`{$k}`";
2621
+ } elseif ( isset( $this->fields[ $k ] ) ) {
2622
+ if ( 'table' === $this->pod_data['storage'] ) {
2623
  $key = "`d`.`{$k}`";
2624
+ } else {
2625
  $key = "`{$k}`.`meta_value`";
2626
+ }
2627
+ } else {
2628
+ $object_fields = (array) $this->pod_data['object_fields'];
2629
+
2630
+ foreach ( $object_fields as $object_field => $object_field_opt ) {
2631
+ if ( $object_field === $k || in_array( $k, $object_field_opt['alias'], true ) ) {
2632
  $key = "`t`.`{$object_field}`";
2633
+ }
2634
  }
2635
  }
2636
+ } elseif ( isset( $this->fields[ $k ] ) ) {
2637
+ if ( 'table' === $this->pod_data['storage'] ) {
 
2638
  $key = "`t`.`{$k}`";
2639
+ } else {
2640
  $key = "`{$k}`.`meta_value`";
2641
+ }
2642
+ }//end if
2643
 
2644
  if ( empty( $key ) ) {
2645
  $key = $k;
2646
 
2647
+ if ( false === strpos( $key, ' ' ) && false === strpos( $key, '`' ) ) {
2648
  $key = '`' . str_replace( '.', '`.`', $key ) . '`';
2649
+ }
2650
  }
2651
+ }//end if
2652
 
2653
  $orderby = $key;
2654
 
2655
+ if ( false === strpos( $orderby, ' ' ) ) {
2656
  $orderby .= ' ' . $order;
2657
+ }
2658
+
2659
+ $params->orderby[ $k ] = $orderby;
2660
+ }//end if
2661
+ }//end foreach
2662
+ }//end if
2663
 
2664
+ // Add prefix to $params->orderby if needed.
2665
+ if ( ! empty( $params->orderby ) ) {
2666
+ if ( ! is_array( $params->orderby ) ) {
2667
  $params->orderby = array( $params->orderby );
2668
+ }
2669
 
2670
+ foreach ( $params->orderby as $ok => $prefix_orderby ) {
2671
  if ( false === strpos( $prefix_orderby, ',' ) && false === strpos( $prefix_orderby, '(' ) && false === stripos( $prefix_orderby, ' AS ' ) && false === strpos( $prefix_orderby, '`' ) && false === strpos( $prefix_orderby, '.' ) ) {
2672
  if ( false !== stripos( $prefix_orderby, ' DESC' ) ) {
2673
+ $k = trim( str_ireplace( array( '`', ' DESC' ), '', $prefix_orderby ) );
2674
  $dir = 'DESC';
2675
+ } else {
2676
+ $k = trim( str_ireplace( array( '`', ' ASC' ), '', $prefix_orderby ) );
 
2677
  $dir = 'ASC';
2678
  }
2679
 
2680
  $key = $k;
2681
 
2682
+ if ( ! in_array( $this->pod_data['type'], array( 'pod', 'table' ), true ) ) {
2683
+ if ( isset( $this->pod_data['object_fields'][ $k ] ) ) {
2684
  $key = "`t`.`{$k}`";
2685
+ } elseif ( isset( $this->fields[ $k ] ) ) {
2686
+ if ( 'table' === $this->pod_data['storage'] ) {
2687
  $key = "`d`.`{$k}`";
2688
+ } else {
2689
  $key = "`{$k}`.`meta_value`";
2690
+ }
2691
+ } else {
2692
+ $object_fields = (array) $this->pod_data['object_fields'];
2693
+
2694
+ foreach ( $object_fields as $object_field => $object_field_opt ) {
2695
+ if ( $object_field === $k || in_array( $k, $object_field_opt['alias'], true ) ) {
2696
  $key = "`t`.`{$object_field}`";
2697
+ }
2698
  }
2699
  }
2700
+ } elseif ( isset( $this->fields[ $k ] ) ) {
2701
+ if ( 'table' === $this->pod_data['storage'] ) {
 
2702
  $key = "`t`.`{$k}`";
2703
+ } else {
2704
  $key = "`{$k}`.`meta_value`";
2705
+ }
2706
+ }//end if
2707
 
2708
  $prefix_orderby = "{$key} {$dir}";
2709
+
2710
+ $params->orderby[ $ok ] = $prefix_orderby;
2711
+ }//end if
2712
+ }//end foreach
2713
+ }//end if
2714
 
2715
  $this->data->select( $params );
2716
 
2721
  * Fetch an item from a Pod. If $id is null, it will return the next item in the list after running find().
2722
  * You can rewind the list back to the start by using reset().
2723
  *
2724
+ * Providing an $id will fetch a specific item from a Pod, much like a call to pods(), and can handle either an id
2725
+ * or slug.
2726
  *
2727
+ * @see PodsData::fetch
2728
  *
2729
+ * @param int $id ID or slug of the item to fetch.
2730
+ * @param bool $explicit_set Whether to set explicitly (use false when in loop).
2731
  *
2732
  * @return array An array of fields from the row
2733
  *
2734
+ * @since 2.0.0
2735
+ * @link https://pods.io/docs/fetch/
2736
  */
2737
+ public function fetch( $id = null, $explicit_set = true ) {
2738
+
2739
+ /**
2740
+ * Runs directly before an item is fetched by fetch().
2741
+ *
2742
+ * @since unknown
2743
+ *
2744
+ * @param int|string|null $id Item ID being fetched or null.
2745
+ * @param object|Pods $this Current Pods object.
2746
+ */
2747
+ do_action( 'pods_pods_fetch', $id, $this );
2748
+
2749
+ if ( ! empty( $id ) ) {
2750
  $this->params = array();
2751
+ }
2752
 
2753
  $this->data->fetch( $id, $explicit_set );
2754
 
2758
  /**
2759
  * (Re)set the MySQL result pointer
2760
  *
2761
+ * @see PodsData::reset
2762
  *
2763
+ * @param int $row ID of the row to reset to.
2764
  *
2765
  * @return \Pods The pod object
2766
  *
2767
+ * @since 2.0.0
2768
+ * @link https://pods.io/docs/reset/
2769
  */
2770
+ public function reset( $row = null ) {
2771
+
2772
+ /**
2773
+ * Runs directly before the Pods object is reset by reset()
2774
+ *
2775
+ * @since unknown
2776
+ *
2777
+ * @param int|string|null The ID of the row being reset to or null if being reset to the beginning.
2778
+ * @param object|Pods $this Current Pods object.
2779
+ */
2780
+ do_action( 'pods_pods_reset', $row, $this );
2781
 
2782
  $this->data->reset( $row );
2783
 
2789
  *
2790
  * This is different than the total number of rows found in the database, which you can get with total_found().
2791
  *
2792
+ * @see PodsData::total
2793
  *
2794
  * @return int Number of rows returned by find(), based on the 'limit' parameter set
2795
+ * @since 2.0.0
2796
+ * @link https://pods.io/docs/total/
2797
  */
2798
+ public function total() {
2799
+
2800
+ do_action( 'pods_pods_total', $this );
2801
 
2802
  $this->data->total();
2803
 
2811
  *
2812
  * This is different than the total number of rows limited by the current call, which you can get with total().
2813
  *
2814
+ * @see PodsData::total_found
2815
  *
2816
  * @return int Number of rows returned by find(), regardless of the 'limit' parameter
2817
+ * @since 2.0.0
2818
+ * @link https://pods.io/docs/total-found/
2819
  */
2820
+ public function total_found() {
2821
+
2822
+ /**
2823
+ * Runs directly before the value of total_found() is determined and returned.
2824
+ *
2825
+ * @since unknown
2826
+ *
2827
+ * @param object|Pods $this Current Pods object.
2828
+ */
2829
+ do_action( 'pods_pods_total_found', $this );
2830
 
2831
  $this->data->total_found();
2832
 
2838
  /**
2839
  * Fetch the total number of pages, based on total rows found and the last find() limit
2840
  *
2841
+ * @param null|int $limit Rows per page.
2842
+ * @param null|int $offset Offset of rows.
2843
+ * @param null|int $total Total rows.
2844
  *
2845
  * @return int Number of pages
2846
  * @since 2.3.10
2861
  $total = $this->total_found();
2862
  }
2863
 
2864
+ return ceil( ( $total - $offset ) / $limit );
 
 
2865
 
2866
  }
2867
 
2868
  /**
2869
  * Fetch the zebra switch
2870
  *
2871
+ * @see PodsData::zebra
2872
  *
2873
  * @return bool Zebra state
2874
  * @since 1.12
2875
  */
2876
+ public function zebra() {
2877
+
2878
  $this->do_hook( 'zebra' );
2879
 
2880
  return $this->data->zebra();
2883
  /**
2884
  * Fetch the nth state
2885
  *
2886
+ * @see PodsData::nth
2887
  *
2888
+ * @param int|string $nth The $nth to match on the PodsData::row_number.
2889
  *
2890
  * @return bool Whether $nth matches
2891
+ * @since 2.3.0
2892
  */
2893
+ public function nth( $nth = null ) {
2894
+
2895
  $this->do_hook( 'nth', $nth );
2896
 
2897
  return $this->data->nth( $nth );
2900
  /**
2901
  * Fetch the current position in the loop (starting at 1)
2902
  *
2903
+ * @see PodsData::position
2904
  *
2905
  * @return int Current row number (+1)
2906
+ * @since 2.3.0
2907
  */
2908
+ public function position() {
2909
+
2910
  $this->do_hook( 'position' );
2911
 
2912
  return $this->data->position();
2918
  *
2919
  * You may be looking for save() in most cases where you're setting a specific field.
2920
  *
2921
+ * @see PodsAPI::save_pod_item
2922
  *
2923
+ * @param array|string $data Either an associative array of field information or a field name.
2924
+ * @param mixed $value (optional) Value of the field, if $data is a field name.
2925
  *
2926
  * @return int The item ID
2927
  *
2928
+ * @since 2.0.0
2929
+ * @link https://pods.io/docs/add/
2930
  */
2931
+ public function add( $data = null, $value = null ) {
2932
+
2933
+ if ( null !== $value ) {
2934
  $data = array( $data => $value );
2935
+ }
2936
 
2937
  $data = (array) $this->do_hook( 'add', $data );
2938
 
2939
+ if ( empty( $data ) ) {
2940
  return 0;
2941
+ }
2942
 
2943
  $params = array(
2944
+ 'pod' => $this->pod,
2945
+ 'data' => $data,
2946
+ 'allow_custom_fields' => true,
2947
  );
2948
 
2949
  return $this->api->save_pod_item( $params );
2950
  }
2951
 
2952
  /**
2953
+ * Add an item to the values of a relationship field, add a value to a number field (field+1), add time to a date
2954
+ * field, or add text to a text field
2955
  *
2956
+ * @see PodsAPI::save_pod_item
2957
  *
2958
+ * @param string $field Field name.
2959
+ * @param mixed $value IDs to add, int|float to add to number field, string for dates (+1 day), or string for text.
2960
+ * @param int $id (optional) ID of the pod item to update.
2961
  *
2962
  * @return int The item ID
2963
  *
2964
+ * @since 2.3.0
2965
  */
2966
+ public function add_to( $field, $value, $id = null ) {
2967
+
2968
  $pod =& $this;
2969
 
2970
  $fetch = false;
2972
  if ( null === $id ) {
2973
  $fetch = true;
2974
 
2975
+ $id = $pod->id();
2976
+ // @codingStandardsIgnoreLine
2977
+ } elseif ( $id != $this->id() ) {
2978
  $pod = pods( $this->pod, $id );
2979
+ }
2980
 
2981
  $this->do_hook( 'add_to', $field, $value, $id );
2982
 
2983
+ if ( ! isset( $this->fields[ $field ] ) ) {
2984
  return $id;
2985
+ }
2986
 
2987
+ // Tableless fields.
2988
+ if ( in_array( $this->fields[ $field ]['type'], PodsForm::tableless_field_types(), true ) ) {
2989
+ if ( ! is_array( $value ) ) {
2990
  $value = explode( ',', $value );
2991
+ }
2992
 
2993
+ if ( 'pick' === $this->fields[ $field ]['type'] && in_array( $this->fields[ $field ]['pick_object'], PodsForm::simple_tableless_objects(), true ) ) {
2994
  $current_value = $pod->raw( $field );
2995
 
2996
+ if ( ! empty( $current_value ) || ( ! is_array( $current_value ) && 0 < strlen( $current_value ) ) ) {
2997
  $current_value = (array) $current_value;
2998
+ } else {
2999
  $current_value = array();
3000
+ }
3001
 
3002
  $value = array_merge( $current_value, $value );
3003
+ } else {
3004
+ $related_ids = $this->api->lookup_related_items( $this->fields[ $field ]['id'], $this->pod_data['id'], $id, $this->fields[ $field ], $this->pod_data );
 
3005
 
3006
  foreach ( $value as $k => $v ) {
3007
+ if ( ! preg_match( '/[^\D]/', $v ) ) {
3008
  $value[ $k ] = (int) $v;
3009
+ }
3010
  }
3011
 
3012
  $value = array_merge( $related_ids, $value );
3013
+ }//end if
3014
 
3015
+ if ( ! empty( $value ) ) {
3016
  $value = array_filter( array_unique( $value ) );
3017
+ } else {
3018
  $value = array();
3019
+ }
3020
 
3021
+ if ( empty( $value ) ) {
3022
  return $id;
3023
+ }
3024
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::number_field_types(), true ) ) {
3025
+ // Number fields.
3026
  $current_value = (float) $pod->raw( $field );
3027
 
3028
  $value = ( $current_value + (float) $value );
3029
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::date_field_types(), true ) ) {
3030
+ // Date fields.
 
3031
  $current_value = $pod->raw( $field );
3032
 
3033
+ if ( 0 < strlen( $current_value ) ) {
3034
  $value = strtotime( $value, strtotime( $current_value ) );
3035
+ } else {
3036
  $value = strtotime( $value );
3037
+ }
3038
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::text_field_types(), true ) ) {
3039
+ // Text fields.
3040
  $current_value = $pod->raw( $field );
3041
 
3042
+ if ( 0 < strlen( $current_value ) ) {
3043
  $value = $current_value . $value;
3044
+ }
3045
+ }//end if
 
3046
 
3047
+ // @todo handle object fields and taxonomies.
3048
  $params = array(
3049
+ 'pod' => $this->pod,
3050
+ 'id' => $id,
3051
  'data' => array(
3052
+ $field => $value,
3053
+ ),
3054
  );
3055
 
3056
  $id = $this->api->save_pod_item( $params );
3057
 
3058
+ if ( 0 < $id && $fetch ) {
3059
+ // Clear local var cache of field values.
3060
+ $pod->data->row = array();
3061
+
3062
  $pod->fetch( $id, false );
3063
+ }
3064
 
3065
  return $id;
3066
  }
3067
 
3068
  /**
3069
+ * Remove an item from the values of a relationship field, remove a value from a number field (field-1), remove
3070
+ * time to a date field
3071
  *
3072
+ * @see PodsAPI::save_pod_item
3073
  *
3074
+ * @param string $field Field name.
3075
+ * @param mixed $value IDs to add, int|float to add to number field, string for dates (-1 day), or string for text.
3076
+ * @param int $id (optional) ID of the pod item to update.
3077
  *
3078
  * @return int The item ID
3079
  *
3089
  $fetch = true;
3090
 
3091
  $id = $this->id();
3092
+ // @codingStandardsIgnoreLine
3093
+ } elseif ( $id != $this->id() ) {
3094
  $pod = pods( $this->pod, $id );
3095
  }
3096
 
3097
  $this->do_hook( 'remove_from', $field, $value, $id );
3098
 
3099
+ if ( ! isset( $this->fields[ $field ] ) ) {
3100
  return $id;
3101
  }
3102
 
3103
+ // Tableless fields.
3104
+ if ( in_array( $this->fields[ $field ]['type'], PodsForm::tableless_field_types(), true ) ) {
3105
  if ( empty( $value ) ) {
3106
  $value = array();
3107
  }
3108
 
3109
+ if ( ! empty( $value ) ) {
3110
+ if ( ! is_array( $value ) ) {
3111
  $value = explode( ',', $value );
3112
  }
3113
 
3114
+ if ( 'pick' === $this->fields[ $field ]['type'] && in_array( $this->fields[ $field ]['pick_object'], PodsForm::simple_tableless_objects(), true ) ) {
3115
  $current_value = $pod->raw( $field );
3116
 
3117
+ if ( ! empty( $current_value ) ) {
3118
  $current_value = (array) $current_value;
3119
  }
3120
 
3121
  foreach ( $current_value as $k => $v ) {
3122
+ // @codingStandardsIgnoreLine.
3123
  if ( in_array( $v, $value ) ) {
3124
  unset( $current_value[ $k ] );
3125
  }
3126
  }
3127
 
3128
  $value = $current_value;
3129
+ } else {
3130
+ $related_ids = $this->api->lookup_related_items( $this->fields[ $field ]['id'], $this->pod_data['id'], $id, $this->fields[ $field ], $this->pod_data );
 
3131
 
3132
  foreach ( $value as $k => $v ) {
3133
+ if ( ! preg_match( '/[^\D]/', $v ) ) {
3134
  $value[ $k ] = (int) $v;
3135
  }
 
 
3136
 
3137
+ // @todo Convert slugs into IDs.
3138
  }
3139
 
3140
  foreach ( $related_ids as $k => $v ) {
3141
+ // @codingStandardsIgnoreLine.
3142
  if ( in_array( $v, $value ) ) {
3143
  unset( $related_ids[ $k ] );
3144
  }
3145
  }
3146
 
3147
  $value = $related_ids;
3148
+ }//end if
3149
 
3150
+ if ( ! empty( $value ) ) {
3151
  $value = array_filter( array_unique( $value ) );
3152
+ } else {
 
3153
  $value = array();
3154
  }
3155
+ }//end if
3156
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::number_field_types(), true ) ) {
3157
+ // Number fields.
3158
+ // Date fields don't support empty for removing.
 
3159
  if ( empty( $value ) ) {
3160
  return $id;
3161
  }
3163
  $current_value = (float) $pod->raw( $field );
3164
 
3165
  $value = ( $current_value - (float) $value );
3166
+ } elseif ( in_array( $this->fields[ $field ]['type'], PodsForm::date_field_types(), true ) ) {
3167
+ // Date fields.
3168
+ // Date fields don't support empty for removing.
 
3169
  if ( empty( $value ) ) {
3170
  return $id;
3171
  }
3174
 
3175
  if ( 0 < strlen( $current_value ) ) {
3176
  $value = strtotime( $value, strtotime( $current_value ) );
3177
+ } else {
 
3178
  $value = strtotime( $value );
3179
  }
3180
 
3181
  $value = date_i18n( 'Y-m-d h:i:s', $value );
3182
+ }//end if
 
 
3183
 
3184
+ // @todo handle object fields and taxonomies.
3185
  $params = array(
3186
+ 'pod' => $this->pod,
3187
+ 'id' => $id,
3188
  'data' => array(
3189
+ $field => $value,
3190
+ ),
3191
  );
3192
 
3193
  $id = $this->api->save_pod_item( $params );
3194
 
3195
  if ( 0 < $id && $fetch ) {
3196
+ // Clear local var cache of field values.
3197
+ $pod->data->row = array();
3198
+
3199
  $pod->fetch( $id, false );
3200
  }
3201
 
3209
  * Though this function has the capacity to add new items, best practice should direct you
3210
  * to use add() for that instead.
3211
  *
3212
+ * @see PodsAPI::save_pod_item
3213
  *
3214
+ * @param array|string $data Either an associative array of field information or a field name.
3215
+ * @param mixed $value (optional) Value of the field, if $data is a field name.
3216
+ * @param int $id (optional) ID of the pod item to update.
3217
+ * @param array $params (optional) Additional params to send to save_pod_item.
3218
  *
3219
  * @return int The item ID
3220
  *
3221
+ * @since 2.0.0
3222
+ * @link https://pods.io/docs/save/
3223
  */
3224
+ public function save( $data = null, $value = null, $id = null, $params = null ) {
3225
+
3226
+ if ( null !== $data && ! is_array( $data ) ) {
3227
  $data = array( $data => $value );
3228
+ }
3229
 
3230
  $fetch = false;
3231
 
3232
+ // @codingStandardsIgnoreLine
3233
+ if ( null === $id || ( $this->row && $id == $this->id() ) ) {
3234
  $fetch = true;
3235
 
3236
+ if ( null === $id ) {
3237
+ $id = $this->id();
3238
+ }
3239
  }
3240
 
3241
  $data = (array) $this->do_hook( 'save', $data, $id );
3242
 
3243
+ if ( empty( $data ) && empty( $params['is_new_item'] ) ) {
3244
  return $id;
3245
+ }
3246
 
3247
  $default = array();
3248
 
3249
+ if ( ! empty( $params ) && is_array( $params ) ) {
3250
  $default = $params;
3251
+ }
3252
 
3253
  $params = array(
3254
+ 'pod' => $this->pod,
3255
+ 'id' => $id,
3256
+ 'data' => $data,
3257
  'allow_custom_fields' => true,
3258
+ 'clear_slug_cache' => false,
3259
  );
3260
 
3261
+ if ( ! empty( $default ) ) {
3262
  $params = array_merge( $params, $default );
3263
+ }
3264
 
3265
  $id = $this->api->save_pod_item( $params );
3266
 
3267
+ if ( 0 < $id && $fetch ) {
3268
+ // Clear local var cache of field values.
3269
+ $this->data->row = array();
3270
+
3271
  $this->fetch( $id, false );
3272
+ }
3273
 
3274
+ if ( ! empty( $this->pod_data['field_slug'] ) ) {
3275
  if ( 0 < $id && $fetch ) {
3276
+ $slug = $this->field( $this->pod_data['field_slug'] );
3277
+ } else {
3278
+ $slug = pods( $this->pod, $id )->field( $this->pod_data['field_slug'] );
 
3279
  }
3280
 
3281
  if ( 0 < strlen( $slug ) ) {
3289
  /**
3290
  * Delete an item
3291
  *
3292
+ * @see PodsAPI::delete_pod_item
3293
  *
3294
+ * @param int $id ID of the Pod item to delete.
3295
  *
3296
  * @return bool Whether the item was successfully deleted
3297
  *
3298
+ * @since 2.0.0
3299
+ * @link https://pods.io/docs/delete/
3300
  */
3301
+ public function delete( $id = null ) {
3302
+
3303
+ if ( null === $id ) {
3304
  $id = $this->id();
3305
+ }
3306
 
3307
  $id = (int) $this->do_hook( 'delete', $id );
3308
 
3309
+ if ( empty( $id ) ) {
3310
  return false;
3311
+ }
3312
 
3313
  $params = array(
3314
  'pod' => $this->pod,
3315
+ 'id' => $id,
3316
  );
3317
 
3318
  return $this->api->delete_pod_item( $params );
3321
  /**
3322
  * Reset Pod
3323
  *
3324
+ * @see PodsAPI::reset_pod
3325
  *
3326
  * @return bool Whether the Pod was successfully reset
3327
  *
3328
  * @since 2.1.1
3329
  */
3330
+ public function reset_pod() {
3331
+
3332
  $params = array( 'id' => $this->pod_id );
3333
 
3334
+ $this->data->id = null;
3335
+ $this->data->row = array();
3336
  $this->data->data = array();
3337
 
3338
+ $this->data->total = 0;
3339
  $this->data->total_found = 0;
3340
 
3341
  return $this->api->reset_pod( $params );
3344
  /**
3345
  * Duplicate an item
3346
  *
3347
+ * @see PodsAPI::duplicate_pod_item
3348
  *
3349
+ * @param int $id ID of the pod item to duplicate.
3350
  *
3351
  * @return int|bool ID of the new pod item
3352
  *
3353
+ * @since 2.0.0
3354
+ * @link https://pods.io/docs/duplicate/
3355
  */
3356
+ public function duplicate( $id = null ) {
3357
+
3358
+ if ( null === $id ) {
3359
  $id = $this->id();
3360
+ }
3361
 
3362
  $id = (int) $this->do_hook( 'duplicate', $id );
3363
 
3364
+ if ( empty( $id ) ) {
3365
  return false;
3366
+ }
3367
 
3368
  $params = array(
3369
  'pod' => $this->pod,
3370
+ 'id' => $id,
3371
  );
3372
 
3373
  return $this->api->duplicate_pod_item( $params );
3376
  /**
3377
  * Import data / Save multiple rows of data at once
3378
  *
3379
+ * @see PodsAPI::import
3380
  *
3381
+ * @param mixed $import_data PHP associative array or CSV input.
3382
+ * @param bool $numeric_mode Use IDs instead of the name field when matching.
3383
+ * @param string $format Format of import data, options are php or csv.
3384
  *
3385
  * @return array IDs of imported items
3386
  *
3387
+ * @since 2.3.0
3388
  */
3389
+ public function import( $import_data, $numeric_mode = false, $format = null ) {
3390
+
3391
  return $this->api->import( $import_data, $numeric_mode, $format );
3392
  }
3393
 
3394
  /**
3395
  * Export an item's data
3396
  *
3397
+ * @see PodsAPI::export_pod_item
3398
  *
3399
+ * @param array $fields (optional) Fields to export.
3400
+ * @param int $id (optional) ID of the pod item to export.
3401
+ * @param null|string $format (optional) The format of the export (php | json).
3402
  *
3403
  * @return array|bool Data array of the exported pod item
3404
  *
3405
+ * @since 2.0.0
3406
+ * @link https://pods.io/docs/export/
3407
  */
3408
+ public function export( $fields = null, $id = null, $format = null ) {
3409
+
3410
  $params = array(
3411
+ 'pod' => $this->pod,
3412
+ 'id' => $id,
3413
+ 'fields' => null,
3414
+ 'depth' => 2,
3415
+ 'flatten' => false,
3416
+ 'context' => null,
3417
+ 'format' => $format,
3418
  );
3419
 
3420
+ if ( is_array( $fields ) && ( isset( $fields['fields'] ) || isset( $fields['depth'] ) ) ) {
3421
  $params = array_merge( $params, $fields );
3422
+ } else {
3423
+ $params['fields'] = $fields;
3424
+ }
3425
 
3426
+ if ( isset( $params['fields'] ) && is_array( $params['fields'] ) && ! in_array( $this->pod_data['field_id'], $params['fields'], true ) ) {
3427
+ $params['fields'] = array_merge( array( $this->pod_data['field_id'] ), $params['fields'] );
3428
+ }
3429
 
3430
+ if ( null === $params['id'] ) {
3431
+ $params['id'] = $this->id();
3432
+ }
3433
 
3434
  $params = (array) $this->do_hook( 'export', $params );
3435
 
3436
+ if ( empty( $params['id'] ) ) {
3437
  return false;
3438
+ }
3439
 
3440
  $data = $this->api->export_pod_item( $params );
3441
 
3442
+ if ( ! empty( $params['format'] ) && 'json' === $params['format'] ) {
3443
+ $data = wp_json_encode( (array) $data );
 
 
3444
  }
3445
 
3446
  return $data;
3449
  /**
3450
  * Export data from all items
3451
  *
3452
+ * @see PodsAPI::export
3453
  *
3454
+ * @param array $params An associative array of parameters.
3455
  *
3456
  * @return array Data arrays of all exported pod items
3457
  *
3458
+ * @since 2.3.0
3459
  */
3460
+ public function export_data( $params = null ) {
3461
+
3462
  $defaults = array(
3463
  'fields' => null,
3464
+ 'depth' => 2,
3465
+ 'params' => null,
3466
  );
3467
 
3468
+ if ( empty( $params ) ) {
3469
  $params = $defaults;
3470
+ } else {
3471
  $params = array_merge( $defaults, (array) $params );
3472
+ }
3473
 
3474
  return $this->api->export( $this, $params );
3475
  }
3479
  * are simple, paginate and advanced. The base and format parameters
3480
  * are used only for the paginate view.
3481
  *
3482
+ * @param array|object $params Associative array of parameters.
3483
  *
3484
  * @return string Pagination HTML
3485
+ * @since 2.0.0
3486
+ * @link https://pods.io/docs/pagination/
3487
  */
3488
  public function pagination( $params = null ) {
3489
 
3490
  if ( empty( $params ) ) {
3491
  $params = array();
3492
+ } elseif ( ! is_array( $params ) ) {
 
3493
  $params = array( 'label' => $params );
3494
  }
3495
 
3496
+ $this->page_var = pods_v( 'page_var', $params, $this->page_var );
3497
 
3498
+ $url = pods_query_arg( null, null, $this->page_var );
3499
 
3500
  $append = '?';
3501
 
3504
  }
3505
 
3506
  $defaults = array(
3507
+ 'type' => 'advanced',
3508
+ 'label' => __( 'Go to page:', 'pods' ),
3509
+ 'show_label' => true,
3510
+ 'first_text' => __( '&laquo; First', 'pods' ),
3511
+ 'prev_text' => __( '&lsaquo; Previous', 'pods' ),
3512
+ 'next_text' => __( 'Next &rsaquo;', 'pods' ),
3513
+ 'last_text' => __( 'Last &raquo;', 'pods' ),
3514
+ 'prev_next' => true,
3515
+ 'first_last' => true,
3516
+ 'limit' => (int) $this->limit,
3517
+ 'offset' => (int) $this->offset,
3518
+ 'page' => max( 1, (int) $this->page ),
3519
+ 'mid_size' => 2,
3520
+ 'end_size' => 1,
3521
  'total_found' => $this->total_found(),
3522
+ 'page_var' => $this->page_var,
3523
+ 'base' => "{$url}{$append}%_%",
3524
+ 'format' => "{$this->page_var}=%#%",
3525
+ 'class' => '',
3526
+ 'link_class' => '',
3527
  );
3528
 
3529
+ if ( is_object( $params ) ) {
3530
+ $params = get_object_vars( $params );
3531
+ }
3532
+
3533
+ $params = (object) array_merge( $defaults, (array) $params );
3534
 
3535
  $params->total = $this->total_pages( $params->limit, $params->offset, $params->total_found );
3536
 
3537
+ if ( $params->limit < 1 || $params->total_found < 1 || 1 === $params->total || $params->total_found <= $params->offset ) {
3538
  return $this->do_hook( 'pagination', $this->do_hook( 'pagination_' . $params->type, '', $params ), $params );
3539
  }
3540
 
3541
  $pagination = $params->type;
3542
 
3543
+ if ( ! in_array( $params->type, array( 'simple', 'advanced', 'paginate', 'list' ), true ) ) {
3544
  $pagination = 'advanced';
3545
  }
3546
 
3557
  /**
3558
  * Return a filter form for searching a Pod
3559
  *
3560
+ * @param array|string $params Comma-separated list of fields or array of parameters.
3561
  *
3562
  * @return string Filters HTML
3563
  *
3564
+ * @since 2.0.0
3565
+ * @link https://pods.io/docs/filters/
3566
  */
3567
+ public function filters( $params = null ) {
3568
+
3569
  $defaults = array(
3570
  'fields' => $params,
3571
+ 'label' => '',
3572
  'action' => '',
3573
+ 'search' => '',
3574
  );
3575
 
3576
+ if ( is_array( $params ) ) {
3577
  $params = array_merge( $defaults, $params );
3578
+ } else {
3579
  $params = $defaults;
3580
+ }
3581
 
3582
  $pod =& $this;
3583
 
3584
+ /**
3585
+ * Filter the Pods::filters() parameters.
3586
+ *
3587
+ * @param array $params Parameters to filter with.
3588
+ * @param Pods $pod Pods object.
3589
+ */
3590
  $params = apply_filters( 'pods_filters_params', $params, $pod );
3591
 
3592
+ $fields = $params['fields'];
3593
 
3594
+ if ( null !== $fields && ! is_array( $fields ) && 0 < strlen( $fields ) ) {
3595
  $fields = explode( ',', $fields );
3596
+ }
3597
 
3598
+ $object_fields = (array) pods_v( 'object_fields', $this->pod_data, array(), true );
3599
 
3600
+ // Force array.
3601
+ if ( empty( $fields ) ) {
3602
  $fields = array();
3603
+ } else {
3604
+ // Temporary.
3605
+ $filter_fields = $fields;
3606
 
3607
  $fields = array();
3608
 
3610
  $name = $k;
3611
 
3612
  $defaults = array(
3613
+ 'name' => $name,
3614
  );
3615
 
3616
+ if ( ! is_array( $field ) ) {
3617
  $name = $field;
3618
 
3619
  $field = array(
3620
+ 'name' => $name,
3621
  );
3622
  }
3623
 
3624
+ // @codingStandardsIgnoreLine.
3625
  $field = array_merge( $defaults, $field );
3626
 
3627
+ $field['name'] = trim( $field['name'] );
3628
 
3629
+ if ( pods_v( 'hidden', $field, false, true ) ) {
3630
+ $field['type'] = 'hidden';
3631
+ }
3632
 
3633
+ if ( isset( $object_fields[ $field['name'] ] ) ) {
3634
+ // @codingStandardsIgnoreLine.
3635
+ $fields[ $field['name'] ] = array_merge( $object_fields[ $field['name'] ], $field );
3636
+ } elseif ( isset( $this->fields[ $field['name'] ] ) ) {
3637
+ // @codingStandardsIgnoreLine.
3638
+ $fields[ $field['name'] ] = array_merge( $this->fields[ $field['name'] ], $field );
3639
+ }
3640
+ }//end foreach
3641
 
3642
+ // Cleanup.
3643
+ unset( $filter_fields );
3644
+ }//end if
3645
 
3646
  $this->filters = array_keys( $fields );
3647
 
3648
+ $label = $params['label'];
3649
 
3650
+ if ( '' === $label ) {
3651
  $label = __( 'Search', 'pods' );
3652
+ }
3653
 
3654
+ $action = $params['action'];
3655
 
3656
+ $search = trim( $params['search'] );
3657
 
3658
+ if ( '' === $search ) {
3659
+ $search = pods_v_sanitized( $pod->search_var, 'get', '' );
3660
+ }
3661
 
3662
  ob_start();
3663
 
3665
 
3666
  $output = ob_get_clean();
3667
 
3668
+ /**
3669
+ * Filter the HTML output of filters()
3670
+ *
3671
+ * @since unknown
3672
+ *
3673
+ * @param string $output Filter output.
3674
+ * @param array $params Params array passed to filters().
3675
+ * @param object|Pods $this Current Pods object.
3676
+ */
3677
+ return apply_filters( 'pods_pods_filters', $output, $params, $this );
3678
  }
3679
 
3680
  /**
3681
  * Run a helper within a Pod Page or WP Template
3682
  *
3683
+ * @see Pods_Helpers::helper
3684
  *
3685
+ * @param string|array $helper Helper name.
3686
+ * @param string $value Value to run the helper on.
3687
+ * @param string $name Field name.
 
3688
  *
3689
  * @return mixed Anything returned by the helper
3690
+ * @since 2.0.0
3691
  */
3692
+ public function helper( $helper, $value = null, $name = null ) {
3693
+
3694
  $params = array(
3695
+ 'helper' => $helper,
3696
+ 'value' => $value,
3697
+ 'name' => $name,
3698
+ 'deprecated' => false,
3699
  );
3700
 
3701
+ if ( class_exists( 'Pods_Templates' ) ) {
3702
+ $params['deprecated'] = Pods_Templates::$deprecated;
3703
+ }
3704
 
3705
+ if ( is_array( $helper ) ) {
3706
  $params = array_merge( $params, $helper );
3707
+ }
3708
 
3709
+ if ( class_exists( 'Pods_Helpers' ) ) {
3710
  $value = Pods_Helpers::helper( $params, $this );
3711
+ } elseif ( is_callable( $params['helper'] ) ) {
3712
+ $disallowed = array(
3713
+ 'system',
3714
+ 'exec',
3715
+ 'popen',
3716
+ 'eval',
3717
+ 'preg_replace',
3718
+ 'create_function',
3719
+ 'include',
3720
+ 'include_once',
3721
+ 'require',
3722
+ 'require_once',
3723
+ );
3724
+
3725
+ $allowed = array();
3726
+
3727
+ /**
3728
+ * Allows adjusting the disallowed callbacks as needed.
3729
+ *
3730
+ * @param array $disallowed List of callbacks not allowed.
3731
+ * @param array $params Parameters used by Pods::helper() method.
3732
+ *
3733
+ * @since 2.7.0
3734
+ */
3735
+ $disallowed = apply_filters( 'pods_helper_disallowed_callbacks', $disallowed, $params );
3736
+
3737
+ /**
3738
+ * Allows adjusting the allowed allowed callbacks as needed.
3739
+ *
3740
+ * @param array $allowed List of callbacks explicitly allowed.
3741
+ * @param array $params Parameters used by Pods::helper() method.
3742
+ *
3743
+ * @since 2.7.0
3744
+ */
3745
+ $allowed = apply_filters( 'pods_helper_allowed_callbacks', $allowed, $params );
3746
+
3747
+ // Clean up helper callback (if string).
3748
+ if ( is_string( $params['helper'] ) ) {
3749
+ $params['helper'] = strip_tags( str_replace( array( '`', chr( 96 ) ), "'", $params['helper'] ) );
3750
+ }
3751
+
3752
+ $is_allowed = false;
3753
+
3754
+ if ( ! empty( $allowed ) ) {
3755
+ if ( in_array( $params['helper'], $allowed, true ) ) {
3756
+ $is_allowed = true;
3757
+ }
3758
+ } elseif ( ! in_array( $params['helper'], $disallowed, true ) ) {
3759
+ $is_allowed = true;
3760
+ }
3761
+
3762
+ if ( $is_allowed ) {
3763
+ $value = call_user_func( $params['helper'], $value );
3764
+ }
3765
+ } else {
3766
+ $value = apply_filters( $params['helper'], $value );
3767
+ }//end if
3768
 
3769
  return $value;
3770
  }
3772
  /**
3773
  * Display the page template
3774
  *
3775
+ * @see Pods_Templates::template
3776
  *
3777
+ * @param string $template_name The template name.
3778
+ * @param string|null $code Custom template code to use instead.
3779
+ * @param bool $deprecated Whether to use deprecated functionality based on old function usage.
3780
  *
3781
  * @return mixed Template output
3782
  *
3783
+ * @since 2.0.0
3784
+ * @link https://pods.io/docs/template/
3785
  */
3786
+ public function template( $template_name, $code = null, $deprecated = false ) {
3787
+
3788
  $out = null;
3789
 
3790
  $obj =& $this;
3791
 
3792
+ if ( ! empty( $code ) ) {
3793
+ // backwards compatibility.
3794
+ $code = str_replace( '$this->', '$obj->', $code );
3795
+
3796
+ /**
3797
+ * Filter the template code before running it.
3798
+ *
3799
+ * @param string $code Template code.
3800
+ * @param string $template_name Template name.
3801
+ * @param Pods $pod Pods object.
3802
+ */
3803
  $code = apply_filters( 'pods_templates_pre_template', $code, $template_name, $this );
3804
+
3805
+ /**
3806
+ * Filter the template code before running it.
3807
+ *
3808
+ * @param string $code Template code.
3809
+ * @param string $template_name Template name.
3810
+ * @param Pods $pod Pods object.
3811
+ */
3812
  $code = apply_filters( "pods_templates_pre_template_{$template_name}", $code, $template_name, $this );
3813
 
3814
  ob_start();
3815
 
3816
+ if ( ! empty( $code ) ) {
3817
+ // Only detail templates need $this->id.
3818
  if ( empty( $this->id ) ) {
3819
  while ( $this->fetch() ) {
3820
+ // @codingStandardsIgnoreLine
3821
  echo $this->do_magic_tags( $code );
3822
  }
3823
+ } else {
3824
+ // @codingStandardsIgnoreLine
3825
  echo $this->do_magic_tags( $code );
3826
+ }
3827
  }
3828
 
3829
  $out = ob_get_clean();
3830
 
3831
+ /**
3832
+ * Filter the template output.
3833
+ *
3834
+ * @param string $out Template output.
3835
+ * @param string $code Template code.
3836
+ * @param string $template_name Template name.
3837
+ * @param Pods $pod Pods object.
3838
+ */
3839
  $out = apply_filters( 'pods_templates_post_template', $out, $code, $template_name, $this );
3840
+
3841
+ /**
3842
+ * Filter the template output.
3843
+ *
3844
+ * @param string $out Template output.
3845
+ * @param string $code Template code.
3846
+ * @param string $template_name Template name.
3847
+ * @param Pods $pod Pods object.
3848
+ */
3849
  $out = apply_filters( "pods_templates_post_template_{$template_name}", $out, $code, $template_name, $this );
3850
+ } elseif ( class_exists( 'Pods_Templates' ) ) {
 
3851
  $out = Pods_Templates::template( $template_name, $code, $this, $deprecated );
3852
+ } elseif ( trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template_name ), ' /-' ) === $template_name ) {
3853
  ob_start();
3854
 
3855
  $default_templates = array(
3856
  'pods/' . $template_name,
3857
  'pods-' . $template_name,
3858
+ $template_name,
3859
  );
3860
 
3861
+ /**
3862
+ * Filter the default Pods Template files.
3863
+ *
3864
+ * @param array $default_templates Default Pods Template files.
3865
+ */
3866
  $default_templates = apply_filters( 'pods_template_default_templates', $default_templates );
3867
 
3868
+ // Only detail templates need $this->id.
3869
  if ( empty( $this->id ) ) {
3870
  while ( $this->fetch() ) {
3871
  pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
3872
  }
3873
+ } else {
 
3874
  pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
3875
+ }
3876
 
3877
  $out = ob_get_clean();
3878
 
3879
+ /**
3880
+ * Filter the template output.
3881
+ *
3882
+ * @param string $out Template output.
3883
+ * @param string $code Template code.
3884
+ * @param string $template_name Template name.
3885
+ * @param Pods $pod Pods object.
3886
+ */
3887
  $out = apply_filters( 'pods_templates_post_template', $out, $code, $template_name, $this );
3888
+
3889
+ /**
3890
+ * Filter the template output.
3891
+ *
3892
+ * @param string $out Template output.
3893
+ * @param string $code Template code.
3894
+ * @param string $template_name Template name.
3895
+ * @param Pods $pod Pods object.
3896
+ */
3897
  $out = apply_filters( "pods_templates_post_template_{$template_name}", $out, $code, $template_name, $this );
3898
+ }//end if
3899
 
3900
  return $out;
3901
  }
3905
  * and override options where needed. For WP object based Pods, you can pass through the WP object
3906
  * field names too, such as "post_title" or "post_content" for example.
3907
  *
3908
+ * @param array $params (optional) Fields to show on the form, defaults to all fields.
3909
+ * @param string $label (optional) Save button label, defaults to "Save Changes".
3910
+ * @param string $thank_you (optional) Thank you URL to send to upon success.
3911
  *
3912
  * @return bool|mixed
3913
+ * @since 2.0.0
3914
+ * @link https://pods.io/docs/form/
3915
  */
3916
+ public function form( $params = null, $label = null, $thank_you = null ) {
3917
+
3918
  $defaults = array(
3919
+ 'fields' => $params,
3920
+ 'label' => $label,
3921
+ 'thank_you' => $thank_you,
3922
+ 'fields_only' => false,
3923
  );
3924
 
3925
+ if ( is_array( $params ) ) {
3926
  $params = array_merge( $defaults, $params );
3927
+ } else {
3928
  $params = $defaults;
3929
+ }
3930
 
3931
  $pod =& $this;
3932
 
3933
  $params = $this->do_hook( 'form_params', $params );
3934
 
3935
+ $fields = $params['fields'];
3936
 
3937
+ if ( null !== $fields && ! is_array( $fields ) && 0 < strlen( $fields ) ) {
3938
  $fields = explode( ',', $fields );
3939
+ }
3940
 
3941
+ $object_fields = (array) pods_v( 'object_fields', $this->pod_data, array(), true );
3942
 
3943
  if ( empty( $fields ) ) {
3944
+ // Add core object fields if $fields is empty.
3945
  $fields = array_merge( $object_fields, $this->fields );
3946
  }
3947
 
3948
+ // Temporary.
3949
+ $form_fields = $fields;
3950
 
3951
  $fields = array();
3952
 
3954
  $name = $k;
3955
 
3956
  $defaults = array(
3957
+ 'name' => $name,
3958
  );
3959
 
3960
+ if ( ! is_array( $field ) ) {
3961
  $name = $field;
3962
 
3963
  $field = array(
3964
+ 'name' => $name,
3965
  );
3966
  }
3967
 
3968
+ // @codingStandardsIgnoreLine.
3969
  $field = array_merge( $defaults, $field );
3970
 
3971
+ $field['name'] = trim( $field['name'] );
3972
 
3973
+ $default_value = pods_v( 'default', $field );
3974
+ $value = pods_v( 'value', $field );
3975
 
3976
+ if ( empty( $field['name'] ) ) {
3977
+ $field['name'] = trim( $name );
 
 
 
3978
  }
3979
+
3980
+ if ( isset( $object_fields[ $field['name'] ] ) ) {
3981
+ // @codingStandardsIgnoreLine.
3982
+ $field = array_merge( $object_fields[ $field['name'] ], $field );
3983
+ } elseif ( isset( $this->fields[ $field['name'] ] ) ) {
3984
+ // @codingStandardsIgnoreLine.
3985
+ $field = array_merge( $this->fields[ $field['name'] ], $field );
3986
  }
3987
 
3988
+ if ( pods_v( 'hidden', $field, false, true ) ) {
3989
+ $field['type'] = 'hidden';
3990
+ }
3991
 
3992
+ $fields[ $field['name'] ] = $field;
3993
 
3994
  if ( empty( $this->id ) && null !== $default_value ) {
3995
+ $this->row_override[ $field['name'] ] = $default_value;
3996
+ } elseif ( ! empty( $this->id ) && null !== $value ) {
3997
+ $this->row[ $field['name'] ] = $value;
 
3998
  }
3999
+ }//end foreach
4000
 
4001
+ // Cleanup.
4002
+ unset( $form_fields );
4003
 
4004
  $fields = $this->do_hook( 'form_fields', $fields, $params );
4005
 
4006
+ $label = $params['label'];
4007
 
4008
+ if ( empty( $label ) ) {
4009
  $label = __( 'Save Changes', 'pods' );
4010
+ }
4011
 
4012
+ $thank_you = $params['thank_you'];
4013
+ $fields_only = $params['fields_only'];
4014
 
4015
+ PodsForm::$form_counter ++;
4016
 
4017
  ob_start();
4018
 
4019
  if ( empty( $thank_you ) ) {
4020
  $success = 'success';
4021
 
4022
+ if ( 1 < PodsForm::$form_counter ) {
4023
  $success .= PodsForm::$form_counter;
4024
+ }
4025
 
4026
+ $thank_you = pods_query_arg( array(
4027
+ 'success*' => null,
4028
+ $success => 1,
4029
+ ) );
4030
 
4031
+ if ( 1 === (int) pods_v( $success, 'get', 0 ) ) {
4032
  $message = __( 'Form submitted successfully', 'pods' );
4033
+
4034
  /**
4035
+ * Change the text of the message that appears on successful form submission.
4036
  *
4037
+ * @param string $message Success message.
4038
  *
4039
+ * @since 2.7.0
 
 
4040
  */
4041
  $message = apply_filters( 'pods_pod_form_success_message', $message );
4042
 
4043
+ echo '<div id="message" class="pods-form-front-success">' . wp_kses_post( $message ) . '</div>';
4044
  }
4045
+ }//end if
4046
 
4047
  pods_view( PODS_DIR . 'ui/front/form.php', compact( array_keys( get_defined_vars() ) ) );
4048
 
4049
  $output = ob_get_clean();
4050
 
4051
+ if ( empty( $this->id ) ) {
4052
  $this->row_override = array();
4053
+ }
4054
 
4055
  return $this->do_hook( 'form', $output, $fields, $label, $thank_you, $this, $this->id() );
4056
  }
4057
 
4058
  /**
4059
+ * Render a singular view for the Pod item content.
4060
+ *
4061
+ * @param array|string|null $fields (optional) Fields to show in the view, defaults to all fields.
4062
  *
4063
  * @return mixed
4064
  * @since 2.3.10
4067
 
4068
  $pod =& $this;
4069
 
4070
+ // Convert comma separated list of fields to an array.
4071
+ if ( null !== $fields && ! is_array( $fields ) && 0 < strlen( $fields ) ) {
4072
  $fields = explode( ',', $fields );
4073
  }
4074
 
4075
  $object_fields = (array) pods_v( 'object_fields', $this->pod_data, array(), true );
4076
 
4077
  if ( empty( $fields ) ) {
4078
+ // Add core object fields if $fields is empty.
4079
  $fields = array_merge( $object_fields, $this->fields );
4080
  }
4081
 
4082
+ // Temporary.
4083
+ $view_fields = $fields;
4084
 
4085
  $fields = array();
4086
 
4087
  foreach ( $view_fields as $name => $field ) {
 
4088
  $defaults = array(
4089
+ 'name' => $name,
4090
  );
4091
 
4092
+ if ( ! is_array( $field ) ) {
4093
  $name = $field;
4094
 
4095
  $field = array(
4096
+ 'name' => $name,
4097
  );
4098
  }
4099
 
4100
+ // @codingStandardsIgnoreLine.
4101
  $field = array_merge( $defaults, $field );
4102
 
4103
+ $field['name'] = trim( $field['name'] );
4104
 
4105
+ if ( empty( $field['name'] ) ) {
4106
+ $field['name'] = trim( $name );
4107
  }
4108
 
4109
+ if ( isset( $object_fields[ $field['name'] ] ) ) {
4110
+ // @codingStandardsIgnoreLine.
4111
+ $field = array_merge( $field, $object_fields[ $field['name'] ] );
4112
+ } elseif ( isset( $this->fields[ $field['name'] ] ) ) {
4113
+ // @codingStandardsIgnoreLine.
4114
+ $field = array_merge( $this->fields[ $field['name'] ], $field );
4115
+ }
4116
 
4117
+ if ( pods_v( 'hidden', $field, false, true ) || 'hidden' === $field['type'] ) {
4118
  continue;
4119
+ } elseif ( ! PodsForm::permission( $field['type'], $field['name'], $field['options'], $fields, $pod, $pod->id() ) ) {
 
4120
  continue;
4121
  }
4122
 
4123
+ $fields[ $field['name'] ] = $field;
4124
+ }//end foreach
4125
 
4126
+ // Cleanup.
4127
+ unset( $view_fields );
4128
 
4129
  $output = pods_view( PODS_DIR . 'ui/front/view.php', compact( array_keys( get_defined_vars() ) ), false, 'cache', true );
4130
 
4135
  /**
4136
  * Replace magic tags with their values
4137
  *
4138
+ * @param string $code The content to evaluate.
 
4139
  *
4140
  * @return string Code with Magic Tags evaluated
4141
  *
4142
+ * @since 2.0.0
4143
  */
4144
+ public function do_magic_tags( $code ) {
4145
+
4146
+ /**
4147
+ * Filters the Pods magic tags content before the default function.
4148
+ * Allows complete replacement of the Pods magic tag engine.
4149
+ *
4150
+ * @param null $pre Default is null which processes magic tags normally. Return any other value to override.
4151
+ * @param string $code The content to evaluate.
4152
+ * @param Pods $pods The Pods Object.
4153
+ *
4154
+ * @since 2.7.0
4155
+ */
4156
+ $pre = apply_filters( 'pods_pre_do_magic_tags', null, $code, $this );
4157
+ if ( null !== $pre ) {
4158
+ return $pre;
4159
+ }
4160
+
4161
  return preg_replace_callback( '/({@(.*?)})/m', array( $this, 'process_magic_tags' ), $code );
4162
  }
4163
 
4164
  /**
4165
  * Replace magic tags with their values
4166
  *
4167
+ * @param string|array $tag The magic tag to process.
 
4168
  *
4169
  * @return string Code with Magic Tags evaluated
4170
  *
4171
  * @since 2.0.2
4172
  */
4173
+ private function process_magic_tags( $tag ) {
4174
 
4175
  if ( is_array( $tag ) ) {
4176
+ if ( ! isset( $tag[2] ) && '' === trim( $tag[2] ) ) {
4177
  return '';
4178
+ }
4179
 
4180
+ $tag = $tag[2];
4181
  }
4182
 
4183
  $tag = trim( $tag, ' {@}' );
4184
  $tag = explode( ',', $tag );
4185
 
4186
+ if ( empty( $tag ) || ! isset( $tag[0] ) || '' === trim( $tag[0] ) ) {
4187
  return '';
 
 
 
4188
  }
4189
 
4190
+ $tag = pods_trim( $tag );
4191
 
4192
+ $field_name = $tag[0];
4193
 
4194
+ $helper_name = '';
4195
+ $before = '';
4196
+ $after = '';
4197
+
4198
+ if ( ! empty( $tag[1] ) ) {
4199
  $value = $this->field( $field_name );
4200
 
4201
+ $helper_name = $tag[1];
4202
 
4203
  $value = $this->helper( $helper_name, $value, $field_name );
4204
+ } else {
 
4205
  $value = $this->display( $field_name );
4206
+ }
4207
+
4208
+ // Process special magic tags but allow "empty" values for numbers.
4209
+ if (
4210
+ ! $value
4211
+ && ! is_numeric( $value )
4212
+ && pods_shortcode_allow_evaluate_tags()
4213
+ && ! $this->fields( $field_name )
4214
+ ) {
4215
+ // Do not pass before and after tags (key 2 and 3) or these get processed twice.
4216
+ $value = pods_evaluate_tag( implode( ',', array_slice( $tag, 0, 2 ) ) );
4217
+ }
4218
 
4219
+ if ( ! empty( $tag[2] ) ) {
4220
+ $before = $tag[2];
4221
+ }
4222
 
4223
+ if ( ! empty( $tag[3] ) ) {
4224
+ $after = $tag[3];
4225
+ }
4226
 
4227
+ /**
4228
+ * Filter the magic tag output for a value.
4229
+ *
4230
+ * @param string $value Magic tag output for value.
4231
+ * @param string $field_name Magic tag field name.
4232
+ * @param string $before Before content.
4233
+ * @param string $after After content.
4234
+ */
4235
  $value = apply_filters( 'pods_do_magic_tags', $value, $field_name, $helper_name, $before, $after );
4236
 
4237
+ if ( is_array( $value ) ) {
4238
+ $value = pods_serial_comma( $value, array(
4239
+ 'field' => $field_name,
4240
+ 'fields' => $this->fields,
4241
+ ) );
4242
+ }
4243
 
4244
+ if ( null !== $value && false !== $value ) {
4245
  return $before . $value . $after;
4246
+ }
4247
 
4248
  return '';
4249
  }
4252
  *
4253
  * Generate UI for Data Management
4254
  *
4255
+ * @param mixed $options Array or String containing Pod or Options to be used.
4256
+ * @param bool $amend Whether to amend the default UI options or replace entirely.
4257
  *
4258
+ * @return PodsUI|null UI object or null if custom UI used
4259
  *
4260
  * @since 2.3.10
4261
  */
4262
+ public function ui( $options = null, $amend = false ) {
4263
+
4264
  $num = '';
4265
 
4266
+ if ( empty( $options ) ) {
4267
  $options = array();
4268
+ } else {
4269
+ $num = pods_v_sanitized( 'num', $options, '' );
4270
 
4271
  if ( empty( $num ) ) {
4272
  $num = '';
4273
  }
4274
  }
4275
 
4276
+ $check_id = pods_v( 'id' . $num, 'get', null, true );
4277
+
4278
+ // @codingStandardsIgnoreLine
4279
+ if ( $this->id() != $check_id ) {
4280
+ $this->fetch( $check_id );
4281
+ }
4282
 
4283
+ if ( ! empty( $options ) && ! $amend ) {
4284
  $this->ui = $options;
4285
 
4286
  return pods_ui( $this );
4287
+ } elseif ( ! empty( $options ) || 'custom' !== pods_v( 'ui_style', $this->pod_data['options'], 'post_type', true ) ) {
4288
+ $actions_enabled = pods_v( 'ui_actions_enabled', $this->pod_data['options'] );
 
4289
 
4290
+ if ( ! empty( $actions_enabled ) ) {
4291
  $actions_enabled = (array) $actions_enabled;
4292
+ } else {
4293
  $actions_enabled = array();
4294
+ }
4295
 
4296
  $available_actions = array(
4297
  'add',
4299
  'duplicate',
4300
  'delete',
4301
  'reorder',
4302
+ 'export',
4303
  );
4304
 
4305
+ if ( ! empty( $actions_enabled ) ) {
4306
  $actions_disabled = array(
4307
+ 'view' => 'view',
4308
  );
4309
 
4310
  foreach ( $available_actions as $action ) {
4311
+ if ( ! in_array( $action, $actions_enabled, true ) ) {
4312
  $actions_disabled[ $action ] = $action;
4313
+ }
4314
  }
4315
+ } else {
 
4316
  $actions_disabled = array(
4317
  'duplicate' => 'duplicate',
4318
+ 'view' => 'view',
4319
+ 'export' => 'export',
4320
  );
4321
 
4322
+ if ( 1 === pods_v( 'ui_export', $this->pod_data['options'], 0 ) ) {
4323
+ unset( $actions_disabled['export'] );
4324
+ }
4325
+ }//end if
4326
 
4327
  if ( empty( $options ) ) {
4328
  $author_restrict = false;
4329
 
4330
+ if ( isset( $this->fields['author'] ) && 'pick' === $this->fields['author']['type'] && 'user' === $this->fields['author']['pick_object'] ) {
4331
  $author_restrict = 'author.ID';
4332
+ }
4333
 
4334
+ if ( ! pods_is_admin( array( 'pods', 'pods_content' ) ) ) {
4335
+ if ( ! current_user_can( 'pods_add_' . $this->pod ) ) {
4336
+ $actions_disabled['add'] = 'add';
4337
 
4338
+ if ( 'add' === pods_v( 'action' . $num ) ) {
4339
  $_GET[ 'action' . $num ] = 'manage';
4340
+ }
4341
  }
4342
 
4343
+ if ( ! $author_restrict && ! current_user_can( 'pods_edit_' . $this->pod ) && ! current_user_can( 'pods_edit_others_' . $this->pod ) ) {
4344
+ $actions_disabled['edit'] = 'edit';
4345
+ }
4346
 
4347
+ if ( ! $author_restrict && ! current_user_can( 'pods_delete_' . $this->pod ) && ! current_user_can( 'pods_delete_others_' . $this->pod ) ) {
4348
+ $actions_disabled['delete'] = 'delete';
4349
+ }
4350
 
4351
+ if ( ! current_user_can( 'pods_reorder_' . $this->pod ) ) {
4352
+ $actions_disabled['reorder'] = 'reorder';
4353
+ }
4354
 
4355
+ if ( ! current_user_can( 'pods_export_' . $this->pod ) ) {
4356
+ $actions_disabled['export'] = 'export';
4357
+ }
4358
+ }//end if
4359
+ }//end if
4360
 
4361
+ $_GET[ 'action' . $num ] = pods_v_sanitized( 'action' . $num, 'get', pods_v( 'action', $options, 'manage' ), true );
4362
 
4363
+ $index = $this->pod_data['field_id'];
4364
  $label = __( 'ID', 'pods' );
4365
 
4366
+ if ( isset( $this->pod_data['fields'][ $this->pod_data['field_index'] ] ) ) {
4367
+ $index = $this->pod_data['field_index'];
4368
+ $label = $this->pod_data['fields'][ $this->pod_data['field_index'] ];
4369
  }
4370
 
4371
  $manage = array(
4372
+ $index => $label,
4373
  );
4374
 
4375
+ if ( isset( $this->pod_data['fields']['modified'] ) ) {
4376
+ $manage['modified'] = $this->pod_data['fields']['modified']['label'];
4377
+ }
4378
 
4379
+ $manage_fields = (array) pods_v( 'ui_fields_manage', $this->pod_data['options'] );
4380
 
4381
+ if ( ! empty( $manage_fields ) ) {
4382
  $manage_new = array();
4383
 
4384
  foreach ( $manage_fields as $manage_field ) {
4385
+ if ( isset( $this->pod_data['fields'][ $manage_field ] ) ) {
4386
+ $manage_new[ $manage_field ] = $this->pod_data['fields'][ $manage_field ];
4387
+ } elseif ( isset( $this->pod_data['object_fields'][ $manage_field ] ) ) {
4388
+ $manage_new[ $manage_field ] = $this->pod_data['object_fields'][ $manage_field ];
4389
+ } elseif ( $manage_field === $this->pod_data['field_id'] ) {
4390
  $field = array(
4391
+ 'name' => $manage_field,
4392
  'label' => 'ID',
4393
+ 'type' => 'number',
4394
+ 'width' => '8%',
4395
  );
4396
 
4397
+ $manage_new[ $manage_field ] = PodsForm::field_setup( $field, null, $field['type'] );
4398
  }
4399
  }
4400
 
4401
+ if ( ! empty( $manage_new ) ) {
4402
  $manage = $manage_new;
4403
+ }
4404
+ }//end if
4405
 
4406
+ $pod_name = $this->pod;
4407
+ $manage = apply_filters( "pods_admin_ui_fields_{$pod_name}", apply_filters( 'pods_admin_ui_fields', $manage, $this->pod, $this ), $this->pod, $this );
4408
 
4409
+ $icon = pods_v( 'ui_icon', $this->pod_data['options'] );
4410
 
4411
+ if ( ! empty( $icon ) ) {
4412
  $icon = pods_image_url( $icon, '32x32' );
4413
+ }
4414
 
4415
+ $filters = pods_v( 'ui_filters', $this->pod_data['options'] );
4416
 
4417
+ if ( ! empty( $filters ) ) {
4418
  $filters_new = array();
4419
 
4420
+ $filters = (array) $filters;
4421
+
4422
  foreach ( $filters as $filter_field ) {
4423
+ if ( isset( $this->pod_data['fields'][ $filter_field ] ) ) {
4424
+ $filters_new[ $filter_field ] = $this->pod_data['fields'][ $filter_field ];
4425
+ } elseif ( isset( $this->pod_data['object_fields'][ $filter_field ] ) ) {
4426
+ $filters_new[ $filter_field ] = $this->pod_data['object_fields'][ $filter_field ];
4427
+ }
4428
  }
4429
 
4430
  $filters = $filters_new;
4431
  }
4432
 
4433
  $ui = array(
4434
+ 'fields' => array(
4435
+ 'manage' => $manage,
4436
+ 'add' => $this->pod_data['fields'],
4437
+ 'edit' => $this->pod_data['fields'],
4438
+ 'duplicate' => $this->pod_data['fields'],
4439
  ),
4440
+ 'icon' => $icon,
4441
+ 'actions_disabled' => $actions_disabled,
4442
+ 'actions_bulk' => array(),
4443
  );
4444
 
4445
+ if ( ! empty( $filters ) ) {
4446
+ $ui['fields']['search'] = $filters;
4447
+ $ui['filters'] = array_keys( $filters );
4448
+ $ui['filters_enhanced'] = true;
4449
  }
4450
 
4451
+ $reorder_field = pods_v( 'ui_reorder_field', $this->pod_data['options'] );
4452
+
4453
+ if ( ! empty( $reorder_field ) && in_array( 'reorder', $actions_enabled, true ) && ! in_array( 'reorder', $actions_disabled, true ) && ( ( ! empty( $this->pod_data['object_fields'] ) && isset( $this->pod_data['object_fields'][ $reorder_field ] ) ) || isset( $this->pod_data['fields'][ $reorder_field ] ) ) ) {
4454
+ $ui['reorder'] = array( 'on' => $reorder_field );
4455
+ $ui['orderby'] = $reorder_field;
4456
+ $ui['orderby_dir'] = 'ASC';
4457
+ }
4458
 
4459
+ if ( ! empty( $author_restrict ) ) {
4460
+ $ui['restrict'] = array( 'author_restrict' => $author_restrict );
 
 
4461
  }
4462
 
4463
+ if ( ! in_array( 'export', $ui['actions_disabled'], true ) ) {
4464
+ $ui['actions_bulk']['export'] = array(
4465
+ 'label' => __( 'Export', 'pods' ),
4466
+ // callback not needed, Pods has this built-in for export.
4467
+ );
4468
+ }
4469
 
4470
+ if ( ! in_array( 'delete', $ui['actions_disabled'], true ) ) {
4471
+ $ui['actions_bulk']['delete'] = array(
4472
+ 'label' => __( 'Delete', 'pods' ),
4473
+ // callback not needed, Pods has this built-in for delete.
 
 
4474
  );
4475
  }
4476
 
4477
+ $detail_url = pods_v( 'detail_url', $this->pod_data['options'] );
4478
 
4479
  if ( 0 < strlen( $detail_url ) ) {
4480
+ $ui['actions_custom'] = array(
4481
  'view_url' => array(
4482
  'label' => 'View',
4483
+ 'link' => get_site_url() . '/' . $detail_url,
4484
+ ),
4485
  );
4486
  }
4487
 
4488
+ // @todo Customize the Add New / Manage links to point to their correct menu items.
4489
+ $pod_name = $this->pod;
4490
+ $ui = apply_filters( "pods_admin_ui_{$pod_name}", apply_filters( 'pods_admin_ui', $ui, $this->pod, $this ), $this->pod, $this );
4491
 
4492
+ // Override UI options.
4493
  foreach ( $options as $option => $value ) {
4494
  $ui[ $option ] = $value;
4495
  }
4497
  $this->ui = $ui;
4498
 
4499
  return pods_ui( $this );
4500
+ }//end if
4501
 
4502
+ $pod_name = $this->pod;
4503
  do_action( 'pods_admin_ui_custom', $this );
4504
+ do_action( "pods_admin_ui_custom_{$pod_name}", $this );
4505
+
4506
+ return null;
4507
  }
4508
 
4509
  /**
4510
  * Handle filters / actions for the class
4511
  *
4512
+ * @see pods_do_hook
4513
+ *
4514
+ * @param string $name Hook name.
4515
  *
4516
  * @return mixed Value filtered
4517
  *
4518
+ * @since 2.0.0
4519
  */
4520
+ private function do_hook( $name ) {
4521
+
4522
  $args = func_get_args();
4523
 
4524
+ if ( empty( $args ) ) {
4525
  return false;
4526
+ }
4527
 
4528
+ // Remove first argument.
4529
+ array_shift( $args );
4530
 
4531
  return pods_do_hook( 'pods', $name, $args, $this );
4532
  }
4534
  /**
4535
  * Handle variables that have been deprecated and PodsData vars
4536
  *
4537
+ * @param string $name Property name.
4538
  *
4539
  * @return mixed
4540
  *
4541
+ * @since 2.0.0
4542
  */
4543
+ public function __get( $name ) {
4544
+
4545
  $name = (string) $name;
4546
 
4547
+ // PodsData vars.
4548
+ if ( isset( $this->data->{$name} ) && 0 === strpos( $name, 'field_' ) ) {
4549
  return $this->data->{$name};
4550
  }
4551
 
4552
+ if ( ! $this->deprecated ) {
4553
+ require_once PODS_DIR . 'deprecated/classes/Pods.php';
4554
+
4555
  $this->deprecated = new Pods_Deprecated( $this );
4556
  }
4557
 
4558
  $var = null;
4559
 
4560
+ $pod_class_exists = class_exists( 'Pod' );
4561
+
4562
  if ( isset( $this->deprecated->{$name} ) ) {
4563
+ if ( ! $pod_class_exists || Pod::$deprecated_notice ) {
4564
+ pods_deprecated( "Pods->{$name}", '2.0' );
4565
+ }
4566
 
4567
  $var = $this->deprecated->{$name};
4568
+ } elseif ( ! $pod_class_exists || Pod::$deprecated_notice ) {
 
4569
  pods_deprecated( "Pods->{$name}", '2.0' );
4570
+ }
4571
 
4572
  return $var;
4573
  }
4574
 
4575
  /**
4576
+ * Handle methods that have been deprecated and any aliasing.
4577
  *
4578
+ * @param string $name Function name.
4579
+ * @param array $args Arguments passed to function.
4580
  *
4581
+ * @return mixed|null
4582
  *
4583
+ * @since 2.0.0
4584
  */
4585
+ public function __call( $name, $args ) {
4586
+
4587
  $name = (string) $name;
4588
 
4589
+ // select > find alias.
4590
+ if ( 'select' === $name ) {
4591
  return call_user_func_array( array( $this, 'find' ), $args );
4592
  }
4593
 
4594
+ if ( ! $this->deprecated ) {
4595
+ require_once PODS_DIR . 'deprecated/classes/Pods.php';
4596
+
4597
  $this->deprecated = new Pods_Deprecated( $this );
4598
  }
4599
 
4600
+ $pod_class_exists = class_exists( 'Pod' );
4601
+
4602
+ if ( method_exists( $this->deprecated, $name ) ) {
4603
  return call_user_func_array( array( $this->deprecated, $name ), $args );
4604
+ // @codingStandardsIgnoreLine
4605
+ } elseif ( ! $pod_class_exists || Pod::$deprecated_notice ) {
4606
  pods_deprecated( "Pods::{$name}", '2.0' );
4607
+ }
4608
+
4609
+ return null;
4610
+ }
4611
+
4612
+ /**
4613
+ * Handle casting a Pods() object to string
4614
+ *
4615
+ * @return string Pod type and name in CURIE notation
4616
+ */
4617
+ public function __toString() {
4618
+
4619
+ $string = '';
4620
+
4621
+ if ( ! empty( $this->pod_data ) ) {
4622
+ $string = sprintf( '%s:%s', $this->pod_data['type'], $this->pod_data['name'] );
4623
+ }
4624
+
4625
+ return $string;
4626
+
4627
  }
4628
  }
classes/PodsAPI.php CHANGED
@@ -1,7849 +1,9410 @@
1
  <?php
 
2
  /**
3
  * @package Pods
4
  */
5
  class PodsAPI {
6
 
7
- /**
8
- * @var PodsAPI
9
- */
10
- static $instance = null;
11
-
12
- /**
13
- * @var bool
14
- */
15
- public $display_errors = false;
16
-
17
- /**
18
- * @var array|bool|mixed|null|void
19
- */
20
- public $pod_data;
21
-
22
- /**
23
- * @var
24
- */
25
- public $pod;
26
-
27
- /**
28
- * @var
29
- */
30
- public $pod_id;
31
-
32
- /**
33
- * @var
34
- */
35
- public $fields;
36
-
37
- /**
38
- * @var
39
- * @deprecated 2.0
40
- */
41
- public $format = null;
42
-
43
- /**
44
- * @var
45
- */
46
- private $deprecated;
47
-
48
- /**
49
- * Singleton handling for a basic pods_api() request
50
- *
51
- * @param string $pod (optional) The pod name
52
- * @param string $format (deprecated) Format for import/export, "php" or "csv"
53
- *
54
- * @return \PodsAPI
55
- *
56
- * @since 2.3.5
57
- */
58
- public static function init ( $pod = null, $format = null ) {
59
- if ( null !== $pod || null !== $format ) {
60
- return new PodsAPI( $pod, $format );
61
- }
62
- elseif ( !is_object( self::$instance ) ) {
63
- self::$instance = new PodsAPI();
64
- }
65
-
66
- return self::$instance;
67
- }
68
-
69
- /**
70
- * Store and retrieve data programatically
71
- *
72
- * @param string $pod (optional) The pod name
73
- * @param string $format (deprecated) Format for import/export, "php" or "csv"
74
- *
75
- * @return \PodsAPI
76
- *
77
- * @license http://www.gnu.org/licenses/gpl-2.0.html
78
- * @since 1.7.1
79
- */
80
- public function __construct ( $pod = null, $format = null ) {
81
- if ( null !== $pod && 0 < strlen( (string) $pod ) ) {
82
- if ( null !== $format ) {
83
- $this->format = $format;
84
-
85
- pods_deprecated( 'pods_api( $pod, $format )', '2.0', 'pods_api( $pod )' );
86
- }
87
-
88
- $pod = pods_clean_name( $pod );
89
-
90
- $pod = $this->load_pod( array( 'name' => $pod, 'table_info' => true ), false );
91
-
92
- if ( !empty( $pod ) ) {
93
- $this->pod_data = $pod;
94
- $this->pod = $pod[ 'name' ];
95
- $this->pod_id = $pod[ 'id' ];
96
- $this->fields = $pod[ 'fields' ];
97
- }
98
- }
99
- }
100
-
101
- /**
102
- * Save a WP object and its meta
103
- *
104
- * @param string $object_type Object type: post|user|comment|setting
105
- * @param array $data All post data to be saved
106
- * @param array $meta (optional) Associative array of meta keys and values
107
- * @param bool $strict (optional) Decides whether the previous saved meta should be deleted or not
108
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
109
- * @param array $fields (optional) The array of fields and their options, for further processing with
110
- *
111
- * @return bool|mixed
112
- *
113
- * @since 2.0
114
- */
115
- public function save_wp_object ( $object_type, $data, $meta = array(), $strict = false, $sanitized = false, $fields = array() ) {
116
- if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
117
- $object_type = 'post';
118
-
119
- if ( $sanitized ) {
120
- $data = pods_unsanitize( $data );
121
- $meta = pods_unsanitize( $meta );
122
- }
123
-
124
- if ( in_array( $object_type, array( 'post', 'user', 'comment' ) ) )
125
- return call_user_func( array( $this, 'save_' . $object_type ), $data, $meta, $strict, false, $fields );
126
- elseif ( 'settings' == $object_type ) {
127
- // Nothing to save
128
- if ( empty( $meta ) )
129
- return true;
130
-
131
- return $this->save_setting( pods_var( 'option_id', $data ), $meta, false );
132
- }
133
-
134
- return false;
135
- }
136
-
137
- /**
138
- * Delete a WP object
139
- *
140
- * @param string $object_type Object type: post|user|comment
141
- * @param int $id Object ID
142
- * @param bool $force_delete (optional) Force deletion instead of trashing (post types only)
143
- *
144
- * @return bool|mixed
145
- *
146
- * @since 2.0
147
- */
148
- public function delete_wp_object ( $object_type, $id, $force_delete = true ) {
149
- if ( in_array( $object_type, array( 'post_type', 'media' ) ) )
150
- $object_type = 'post';
151
-
152
- if ( empty( $id ) )
153
- return false;
154
-
155
- if ( in_array( $object_type, array( 'post' ) ) )
156
- return wp_delete_post( $id, $force_delete );
157
-
158
- if ( function_exists( 'wp_delete_' . $object_type ) )
159
- return call_user_func( 'wp_delete_' . $object_type, $id );
160
-
161
- return false;
162
- }
163
-
164
- /**
165
- * Save a post and it's meta
166
- *
167
- * @param array $post_data All post data to be saved (using wp_insert_post / wp_update_post)
168
- * @param array $post_meta (optional) All meta to be saved (set value to null to delete)
169
- * @param bool $strict (optional) Whether to delete previously saved meta not in $post_meta
170
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
171
- * @param array $fields (optional) The array of fields and their options, for further processing with
172
- *
173
- * @return mixed|void
174
- *
175
- * @since 2.0
176
- */
177
- public function save_post ( $post_data, $post_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
178
- $conflicted = pods_no_conflict_check( 'post' );
179
-
180
- if ( !$conflicted )
181
- pods_no_conflict_on( 'post' );
182
-
183
- if ( !is_array( $post_data ) || empty( $post_data ) )
184
- $post_data = array( 'post_title' => '' );
185
-
186
- if ( !is_array( $post_meta ) )
187
- $post_meta = array();
188
-
189
- if ( $sanitized ) {
190
- $post_data = pods_unsanitize( $post_data );
191
- $post_meta = pods_unsanitize( $post_meta );
192
- }
193
-
194
- if ( !isset( $post_data[ 'ID' ] ) || empty( $post_data[ 'ID' ] ) )
195
- $post_data[ 'ID' ] = wp_insert_post( $post_data, true );
196
- elseif ( 2 < count( $post_data ) || !isset( $post_data[ 'post_type' ] ) )
197
- wp_update_post( $post_data );
198
-
199
- if ( is_wp_error( $post_data[ 'ID' ] ) ) {
200
- if ( !$conflicted )
201
- pods_no_conflict_off( 'post' );
202
-
203
- /**
204
- * @var $post_error WP_Error
205
- */
206
- $post_error = $post_data[ 'ID' ];
207
-
208
- return pods_error( $post_error->get_error_message(), $this );
209
- }
210
-
211
- $this->save_post_meta( $post_data[ 'ID' ], $post_meta, $strict, $fields );
212
-
213
- if ( !$conflicted )
214
- pods_no_conflict_off( 'post' );
215
-
216
- return $post_data[ 'ID' ];
217
- }
218
-
219
- /**
220
- * Save a post's meta
221
- *
222
- * @param int $id Post ID
223
- * @param array $post_meta All meta to be saved (set value to null to delete)
224
- * @param bool $strict Whether to delete previously saved meta not in $post_meta
225
- * @param array $fields (optional) The array of fields and their options, for further processing with
226
- *
227
- * @return int Id of the post with the meta
228
- *
229
- * @since 2.0
230
- */
231
- public function save_post_meta ( $id, $post_meta = null, $strict = false, $fields = array() ) {
232
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
233
-
234
- $conflicted = pods_no_conflict_check( 'post' );
235
-
236
- if ( !$conflicted )
237
- pods_no_conflict_on( 'post' );
238
-
239
- if ( !is_array( $post_meta ) )
240
- $post_meta = array();
241
-
242
- $id = (int) $id;
243
-
244
- $meta = get_post_meta( $id );
245
-
246
- foreach ( $meta as $k => $value ) {
247
- if ( is_array( $value ) && 1 == count( $value ) )
248
- $meta[ $k ] = current( $value );
249
- }
250
-
251
- foreach ( $post_meta as $meta_key => $meta_value ) {
252
- if ( null === $meta_value || ( $strict && '' === $post_meta[ $meta_key ] ) ) {
253
- $old_meta_value = '';
254
-
255
- if ( isset( $meta[ $meta_key ] ) )
256
- $old_meta_value = $meta[ $meta_key ];
257
-
258
- delete_post_meta( $id, $meta_key, $old_meta_value );
259
- }
260
- else {
261
- $simple = false;
262
-
263
- if ( isset( $fields[ $meta_key ] ) && is_array( $meta_value ) ) {
264
- $field_data = $fields[ $meta_key ];
265
 
266
- $simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
267
- }
 
 
268
 
269
- if ( $simple ) {
270
- delete_post_meta( $id, $meta_key );
 
 
271
 
272
- update_post_meta( $id, '_pods_' . $meta_key, $meta_value );
 
 
 
273
 
274
- foreach ( $meta_value as $value ) {
275
- add_post_meta( $id, $meta_key, $value );
276
- }
277
- }
278
- else {
279
- update_post_meta( $id, $meta_key, $meta_value );
280
- }
281
- }
282
- }
283
 
284
- if ( $strict ) {
285
- foreach ( $meta as $meta_key => $meta_value ) {
286
- if ( !isset( $post_meta[ $meta_key ] ) )
287
- delete_post_meta( $id, $meta_key, $meta_value );
288
- }
289
- }
290
 
291
- if ( !$conflicted )
292
- pods_no_conflict_off( 'post' );
 
 
293
 
294
- return $id;
295
- }
 
 
 
296
 
297
- /**
298
- * Save a user and it's meta
299
- *
300
- * @param array $user_data All user data to be saved (using wp_insert_user / wp_update_user)
301
- * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
302
- * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
303
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
304
- * @param array $fields (optional) The array of fields and their options, for further processing with
305
- *
306
- * @return int Returns user id on success
307
- *
308
- * @since 2.0
309
- */
310
- public function save_user ( $user_data, $user_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
311
- if ( !is_array( $user_data ) || empty( $user_data ) )
312
- return pods_error( __( 'User data is required but is either invalid or empty', 'pods' ), $this );
313
 
314
- $conflicted = pods_no_conflict_check( 'user' );
 
 
 
 
315
 
316
- if ( !$conflicted )
317
- pods_no_conflict_on( 'user' );
 
 
 
 
318
 
319
- if ( !is_array( $user_meta ) )
320
- $user_meta = array();
 
 
 
 
321
 
322
- if ( $sanitized ) {
323
- $user_data = pods_unsanitize( $user_data );
324
- $user_meta = pods_unsanitize( $user_meta );
325
- }
 
 
 
 
 
 
 
326
 
327
- // Set role
328
- if ( isset( $user_meta[ 'role' ] ) ) {
329
- $user_data[ 'role' ] = $user_meta[ 'role' ];
 
 
330
 
331
- unset( $user_meta[ 'role' ] );
 
 
332
  }
333
 
334
- if ( !isset( $user_data[ 'ID' ] ) || empty( $user_data[ 'ID' ] ) )
335
- $user_data[ 'ID' ] = wp_insert_user( $user_data );
336
- elseif ( 1 < count( $user_data ) )
337
- wp_update_user( $user_data );
338
-
339
- if ( is_wp_error( $user_data[ 'ID' ] ) ) {
340
- if ( !$conflicted )
341
- pods_no_conflict_off( 'user' );
342
 
343
- /**
344
- * @var $user_error WP_Error
345
- */
346
- $user_error = $user_data[ 'ID' ];
 
 
 
 
 
 
 
 
347
 
348
- return pods_error( $user_error->get_error_message(), $this );
349
- }
 
350
 
351
- $this->save_user_meta( $user_data[ 'ID' ], $user_meta, $strict, $fields );
 
352
 
353
- if ( !$conflicted )
354
- pods_no_conflict_off( 'user' );
355
 
356
- return $user_data[ 'ID' ];
357
- }
358
 
359
- /**
360
- * Save a user meta
361
- *
362
- * @param int $id User ID
363
- * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
364
- * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
365
- * @param array $fields (optional) The array of fields and their options, for further processing with
366
- *
367
- * @return int User ID
368
- *
369
- * @since 2.0
370
- *
371
- */
372
- public function save_user_meta ( $id, $user_meta = null, $strict = false, $fields = array() ) {
373
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
374
 
375
- $conflicted = pods_no_conflict_check( 'user' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
 
377
- if ( !$conflicted )
378
- pods_no_conflict_on( 'user' );
 
379
 
380
- if ( !is_array( $user_meta ) )
381
- $user_meta = array();
 
382
 
383
- $id = (int) $id;
 
 
 
384
 
385
- $meta = get_user_meta( $id );
 
 
 
 
 
 
386
 
387
- foreach ( $user_meta as $meta_key => $meta_value ) {
388
- if ( null === $meta_value ) {
389
- $old_meta_value = '';
390
 
391
- if ( isset( $meta[ $meta_key ] ) )
392
- $old_meta_value = $meta[ $meta_key ];
393
 
394
- delete_user_meta( $id, $meta_key, $old_meta_value );
395
- }
396
- else {
397
- $simple = false;
 
 
 
 
 
 
 
 
398
 
399
- if ( isset( $fields[ $meta_key ] ) && is_array( $meta_value ) ) {
400
- $field_data = $fields[ $meta_key ];
 
401
 
402
- $simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
403
- }
 
404
 
405
- if ( $simple ) {
406
- delete_user_meta( $id, $meta_key );
 
407
 
408
- foreach ( $meta_value as $value ) {
409
- add_user_meta( $id, $meta_key, $value );
410
- }
411
- }
412
- else {
413
- update_user_meta( $id, $meta_key, $meta_value );
414
- }
415
- }
416
- }
417
 
418
- if ( $strict ) {
419
- foreach ( $meta as $meta_key => $meta_value ) {
420
- if ( !isset( $user_meta[ $meta_key ] ) )
421
- delete_user_meta( $id, $meta_key, $user_meta[ $meta_key ] );
422
- }
423
- }
424
-
425
- if ( !$conflicted )
426
- pods_no_conflict_off( 'user' );
427
-
428
- return $id;
429
- }
430
-
431
- /**
432
- * Save a comment and it's meta
433
- *
434
- * @param array $comment_data All comment data to be saved (using wp_insert_comment / wp_update_comment)
435
- * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
436
- * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
437
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
438
- * @param array $fields (optional) The array of fields and their options, for further processing with
439
- *
440
- * @return int Comment ID
441
- *
442
- * @since 2.0
443
- */
444
- public function save_comment ( $comment_data, $comment_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
445
- if ( !is_array( $comment_data ) || empty( $comment_data ) )
446
- return pods_error( __( 'Comment data is required but is either invalid or empty', 'pods' ), $this );
447
-
448
- $conflicted = pods_no_conflict_check( 'comment' );
449
-
450
- if ( !$conflicted )
451
- pods_no_conflict_on( 'comment' );
452
-
453
- if ( !is_array( $comment_meta ) )
454
- $comment_meta = array();
455
-
456
- if ( $sanitized ) {
457
- $comment_data = pods_unsanitize( $comment_data );
458
- $comment_meta = pods_unsanitize( $comment_meta );
459
- }
460
-
461
- if ( !isset( $comment_data[ 'comment_ID' ] ) || empty( $comment_data[ 'comment_ID' ] ) )
462
- $comment_data[ 'comment_ID' ] = wp_insert_comment( $comment_data );
463
- elseif ( 1 < count( $comment_data ) )
464
- wp_update_comment( $comment_data );
465
-
466
- if ( is_wp_error( $comment_data[ 'comment_ID' ] ) ) {
467
- if ( !$conflicted )
468
- pods_no_conflict_off( 'comment' );
469
-
470
- /**
471
- * @var $comment_error WP_Error
472
- */
473
- $comment_error = $comment_data[ 'comment_ID' ];
474
-
475
- return pods_error( $comment_error->get_error_message(), $this );
476
- }
477
-
478
- $this->save_comment_meta( $comment_data[ 'comment_ID' ], $comment_meta, $strict, $fields );
479
-
480
- if ( !$conflicted )
481
- pods_no_conflict_off( 'comment' );
482
-
483
- return $comment_data[ 'comment_ID' ];
484
- }
485
-
486
- /**
487
- * Save a comment meta
488
- *
489
- * @param int $id Comment ID
490
- * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
491
- * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
492
- * @param array $fields (optional) The array of fields and their options, for further processing with
493
- *
494
- * @return int Comment ID
495
- *
496
- * @since 2.0
497
- */
498
- public function save_comment_meta ( $id, $comment_meta = null, $strict = false, $fields = array() ) {
499
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
500
-
501
- $conflicted = pods_no_conflict_check( 'comment' );
502
-
503
- if ( !$conflicted )
504
- pods_no_conflict_on( 'comment' );
505
-
506
- if ( !is_array( $comment_meta ) )
507
- $comment_meta = array();
508
-
509
- $id = (int) $id;
510
-
511
- $meta = get_comment_meta( $id );
512
-
513
- foreach ( $comment_meta as $meta_key => $meta_value ) {
514
- if ( null === $meta_value ) {
515
- $old_meta_value = '';
516
-
517
- if ( isset( $meta[ $meta_key ] ) )
518
- $old_meta_value = $meta[ $meta_key ];
519
-
520
- delete_comment_meta( $id, $meta_key, $old_meta_value );
521
- }
522
- else {
523
- $simple = false;
524
 
525
- if ( isset( $fields[ $meta_key ] ) && is_array( $meta_value ) ) {
526
- $field_data = $fields[ $meta_key ];
527
 
528
- $simple = ( 'pick' == $field_data[ 'type' ] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
529
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
530
 
531
- if ( $simple ) {
532
- delete_comment_meta( $id, $meta_key );
533
 
534
- foreach ( $meta_value as $value ) {
535
- add_comment_meta( $id, $meta_key, $value );
536
- }
537
- }
538
- else {
539
- update_comment_meta( $id, $meta_key, $meta_value );
540
- }
541
- }
542
- }
543
-
544
- if ( $strict ) {
545
- foreach ( $meta as $meta_key => $meta_value ) {
546
- if ( !isset( $comment_meta[ $meta_key ] ) )
547
- delete_comment_meta( (int) $id, $meta_key, $comment_meta[ $meta_key ] );
548
- }
549
- }
550
-
551
- if ( !$conflicted )
552
- pods_no_conflict_off( 'comment' );
553
-
554
- return $id;
555
- }
556
-
557
- /**
558
- * Save a taxonomy's term
559
- *
560
- * @param int $term_ID Term ID, leave empty to add
561
- * @param string $term Term name
562
- * @param string $taxonomy Taxonomy name
563
- * @param array $term_data All term data to be saved (using wp_insert_term / wp_update_term)
564
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
565
- * @param array $fields (optional) The array of fields and their options, for further processing with
566
- *
567
- * @return int Term ID
568
- *
569
- * @since 2.0
570
- */
571
- public function save_term ( $term_ID, $term, $taxonomy, $term_data, $sanitized = false, $fields = array() ) {
572
- $conflicted = pods_no_conflict_check( 'taxonomy' );
573
-
574
- if ( !$conflicted )
575
- pods_no_conflict_on( 'taxonomy' );
576
-
577
- if ( !is_array( $term_data ) )
578
- $term_data = array();
579
-
580
- $term_ID = (int) $term_ID;
581
-
582
- if ( $sanitized ) {
583
- $term = pods_unsanitize( $term );
584
- $taxonomy = pods_unsanitize( $taxonomy );
585
- $term_data = pods_unsanitize( $term_data );
586
- }
587
-
588
- if ( empty( $term_ID ) )
589
- $term_ID = wp_insert_term( $term, $taxonomy, $term_data );
590
- else {
591
- if ( 0 < strlen( $term ) )
592
- $term_data[ 'term' ] = $term;
593
-
594
- if ( empty( $term_data ) ) {
595
- if ( !$conflicted )
596
- pods_no_conflict_off( 'taxonomy' );
597
-
598
- return pods_error( __( 'Taxonomy term data is required but is either invalid or empty', 'pods' ), $this );
599
- }
600
-
601
- wp_update_term( $term_ID, $taxonomy, $term_data );
602
- }
603
-
604
- if ( is_wp_error( $term_ID ) ) {
605
- if ( !$conflicted )
606
- pods_no_conflict_off( 'taxonomy' );
607
-
608
- return pods_error( $term_ID->get_error_message(), $this );
609
- }
610
- elseif ( is_array( $term_ID ) )
611
- $term_ID = $term_ID[ 'term_id' ];
612
-
613
- if ( !$conflicted )
614
- pods_no_conflict_off( 'taxonomy' );
615
-
616
- return $term_ID;
617
- }
618
-
619
- /**
620
- * Save a set of options
621
- *
622
- * @param string $setting Setting group name
623
- * @param array $option_data All option data to be saved
624
- * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
625
- *
626
- * @return bool
627
- *
628
- * @since 2.3
629
- */
630
- public function save_setting ( $setting, $option_data, $sanitized = false ) {
631
- if ( !is_array( $option_data ) || empty( $option_data ) )
632
- return pods_error( __( 'Setting data is required but is either invalid or empty', 'pods' ), $this );
633
-
634
- $conflicted = pods_no_conflict_check( 'settings' );
635
-
636
- if ( !$conflicted )
637
- pods_no_conflict_on( 'settings' );
638
-
639
- if ( $sanitized )
640
- $option_data = pods_unsanitize( $option_data );
641
-
642
- foreach ( $option_data as $option => $value ) {
643
- if ( !empty( $setting ) )
644
- $option = $setting . '_' . $option;
645
-
646
- update_option( $option, $value );
647
- }
648
-
649
- if ( !$conflicted )
650
- pods_no_conflict_off( 'settings' );
651
-
652
- return true;
653
- }
654
-
655
- /**
656
- * Rename a WP object's type
657
- *
658
- * @param string $object_type Object type: post|taxonomy|comment|setting
659
- * @param string $old_name The old name
660
- * @param string $new_name The new name
661
- *
662
- * @return bool
663
- *
664
- * @since 2.0
665
- */
666
- public function rename_wp_object_type ( $object_type, $old_name, $new_name ) {
667
- /**
668
- * @var $wpdb wpdb
669
- */
670
- global $wpdb;
671
-
672
- if ( 'post_type' == $object_type )
673
- $object_type = 'post';
674
-
675
- if ( 'post' == $object_type ) {
676
- pods_query( "UPDATE `{$wpdb->posts}` SET `post_type` = %s WHERE `post_type` = %s", array(
677
- $new_name,
678
- $old_name
679
- ) );
680
- }
681
- elseif ( 'taxonomy' == $object_type ) {
682
- pods_query( "UPDATE `{$wpdb->term_taxonomy}` SET `taxonomy` = %s WHERE `taxonomy` = %s", array(
683
- $new_name,
684
- $old_name
685
- ) );
686
- }
687
- elseif ( 'comment' == $object_type ) {
688
- pods_query( "UPDATE `{$wpdb->comments}` SET `comment_type` = %s WHERE `comment_type` = %s", array(
689
- $new_name,
690
- $old_name
691
- ) );
692
- }
693
- elseif ( 'settings' == $object_type ) {
694
- pods_query( "UPDATE `{$wpdb->options}` SET `option_name` = REPLACE( `option_name`, %s, %s ) WHERE `option_name` LIKE '" . pods_sanitize_like( $old_name ) . "_%'", array(
695
- $new_name . '_',
696
- $old_name . '_'
697
- ) );
698
- }
699
-
700
- return true;
701
- }
702
-
703
- /**
704
- * Get a list of core WP object fields for a specific object
705
- *
706
- * @param string $object The pod type to look for, possible values: post_type, user, comment, taxonomy
707
- * @param array $pod Array of Pod data
708
- * @param boolean $refresh Whether to force refresh the information
709
- *
710
- * @return array Array of fields
711
- *
712
- * @since 2.0
713
- */
714
- public function get_wp_object_fields ( $object = 'post_type', $pod = null, $refresh = false ) {
715
- $pod_name = pods_var_raw( 'name', $pod, $object, null, true );
716
-
717
- if ( 'media' == $pod_name ) {
718
- $object = 'post_type';
719
- $pod_name = 'attachment';
720
  }
721
 
722
- $fields = false;
723
-
724
- if ( pods_api_cache() )
725
- $fields = pods_transient_get( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ) );
726
-
727
- if ( false !== $fields && !$refresh )
728
- return $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
729
-
730
- $fields = array();
731
-
732
- if ( 'post_type' == $object ) {
733
- $fields = array(
734
- 'post_title' => array(
735
- 'name' => 'post_title',
736
- 'label' => 'Title',
737
- 'type' => 'text',
738
- 'alias' => array( 'title', 'name' ),
739
- 'options' => array(
740
- 'display_filter' => 'the_title',
741
- 'display_filter_args' => array( 'post_ID' )
742
- )
743
- ),
744
- 'post_content' => array(
745
- 'name' => 'post_content',
746
- 'label' => 'Content',
747
- 'type' => 'wysiwyg',
748
- 'alias' => array( 'content' ),
749
- 'options' => array(
750
- 'wysiwyg_allowed_html_tags' => '',
751
- 'display_filter' => 'the_content',
752
- 'pre_save' => 0
753
- )
754
- ),
755
- 'post_excerpt' => array(
756
- 'name' => 'post_excerpt',
757
- 'label' => 'Excerpt',
758
- 'type' => 'paragraph',
759
- 'alias' => array( 'excerpt' ),
760
- 'options' => array(
761
- 'paragraph_allow_html' => 1,
762
- 'paragraph_allowed_html_tags' => '',
763
- 'display_filter' => 'the_excerpt',
764
- 'pre_save' => 0
765
- )
766
- ),
767
- 'post_author' => array(
768
- 'name' => 'post_author',
769
- 'label' => 'Author',
770
- 'type' => 'pick',
771
- 'alias' => array( 'author' ),
772
- 'pick_object' => 'user',
773
- 'options' => array(
774
- 'pick_format_type' => 'single',
775
- 'pick_format_single' => 'autocomplete',
776
- 'default_value' => '{@user.ID}'
777
- )
778
- ),
779
- 'post_date' => array(
780
- 'name' => 'post_date',
781
- 'label' => 'Publish Date',
782
- 'type' => 'datetime',
783
- 'alias' => array( 'created', 'date' )
784
- ),
785
- 'post_date_gmt' => array(
786
- 'name' => 'post_date_gmt',
787
- 'label' => 'Publish Date (GMT)',
788
- 'type' => 'datetime',
789
- 'alias' => array(),
790
- 'hidden' => true
791
- ),
792
- 'post_status' => array(
793
- 'name' => 'post_status',
794
- 'label' => 'Status',
795
- 'type' => 'pick',
796
- 'pick_object' => 'post-status',
797
- 'default' => $this->do_hook( 'default_status_' . $pod_name, pods_var( 'default_status', pods_var_raw( 'options', $pod ), 'draft', null, true ), $pod ),
798
- 'alias' => array( 'status' )
799
- ),
800
- 'comment_status' => array(
801
- 'name' => 'comment_status',
802
- 'label' => 'Comment Status',
803
- 'type' => 'text',
804
- 'default' => get_option( 'default_comment_status', 'open' ),
805
- 'alias' => array(),
806
- 'data' => array(
807
- 'open' => __( 'Open', 'pods' ),
808
- 'closed' => __( 'Closed', 'pods' )
809
- )
810
- ),
811
- 'ping_status' => array(
812
- 'name' => 'ping_status',
813
- 'label' => 'Ping Status',
814
- 'default' => get_option( 'default_ping_status', 'open' ),
815
- 'type' => 'text',
816
- 'alias' => array(),
817
- 'data' => array(
818
- 'open' => __( 'Open', 'pods' ),
819
- 'closed' => __( 'Closed', 'pods' )
820
- )
821
- ),
822
- 'post_password' => array(
823
- 'name' => 'post_password',
824
- 'label' => 'Password',
825
- 'type' => 'text',
826
- 'alias' => array()
827
- ),
828
- 'post_name' => array(
829
- 'name' => 'post_name',
830
- 'label' => 'Permalink',
831
- 'type' => 'slug',
832
- 'alias' => array( 'slug', 'permalink' )
833
- ),
834
- 'to_ping' => array(
835
- 'name' => 'to_ping',
836
- 'label' => 'To Ping',
837
- 'type' => 'text',
838
- 'alias' => array(),
839
- 'hidden' => true
840
- ),
841
- 'pinged' => array(
842
- 'name' => 'pinged',
843
- 'label' => 'Pinged',
844
- 'type' => 'text',
845
- 'alias' => array(),
846
- 'hidden' => true
847
- ),
848
- 'post_modified' => array(
849
- 'name' => 'post_modified',
850
- 'label' => 'Last Modified Date',
851
- 'type' => 'datetime',
852
- 'alias' => array( 'modified' ),
853
- 'hidden' => true
854
- ),
855
- 'post_modified_gmt' => array(
856
- 'name' => 'post_modified_gmt',
857
- 'label' => 'Last Modified Date (GMT)',
858
- 'type' => 'datetime',
859
- 'alias' => array(),
860
- 'hidden' => true
861
- ),
862
- 'post_content_filtered' => array(
863
- 'name' => 'post_content_filtered',
864
- 'label' => 'Content (filtered)',
865
- 'type' => 'paragraph',
866
- 'alias' => array(),
867
- 'hidden' => true,
868
- 'options' => array(
869
- 'paragraph_allow_html' => 1,
870
- 'paragraph_oembed' => 1,
871
- 'paragraph_wptexturize' => 1,
872
- 'paragraph_convert_chars' => 1,
873
- 'paragraph_wpautop' => 1,
874
- 'paragraph_allow_shortcode' => 1,
875
- 'paragraph_allowed_html_tags' => ''
876
- )
877
- ),
878
- 'post_parent' => array(
879
- 'name' => 'post_parent',
880
- 'label' => 'Parent',
881
- 'type' => 'pick',
882
- 'pick_object' => 'post_type',
883
- 'pick_val' => '__current__',
884
- 'alias' => array( 'parent' ),
885
- 'data' => array(),
886
- 'hidden' => true
887
- ),
888
- 'guid' => array(
889
- 'name' => 'guid',
890
- 'label' => 'GUID',
891
- 'type' => 'text',
892
- 'alias' => array(),
893
- 'hidden' => true
894
- ),
895
- 'menu_order' => array(
896
- 'name' => 'menu_order',
897
- 'label' => 'Menu Order',
898
- 'type' => 'number',
899
- 'alias' => array()
900
- ),
901
- 'post_type' => array(
902
- 'name' => 'post_type',
903
- 'label' => 'Type',
904
- 'type' => 'text',
905
- 'alias' => array( 'type' ),
906
- 'hidden' => true
907
- ),
908
- 'post_mime_type' => array(
909
- 'name' => 'post_mime_type',
910
- 'label' => 'Mime Type',
911
- 'type' => 'text',
912
- 'alias' => array(),
913
- 'hidden' => true
914
- ),
915
- 'comment_count' => array(
916
- 'name' => 'comment_count',
917
- 'label' => 'Comment Count',
918
- 'type' => 'number',
919
- 'alias' => array(),
920
- 'hidden' => true
921
- )
922
- );
923
-
924
- if ( !empty( $pod ) ) {
925
- $taxonomies = get_object_taxonomies( $pod_name, 'objects' );
926
-
927
- foreach ( $taxonomies as $taxonomy ) {
928
- $fields[ $taxonomy->name ] = array(
929
- 'name' => $taxonomy->name,
930
- 'label' => $taxonomy->labels->name,
931
- 'type' => 'taxonomy',
932
- 'pick_object' => 'taxonomy',
933
- 'pick_val' => $taxonomy->name,
934
- 'alias' => array(),
935
- 'hidden' => true,
936
- 'options' => array(
937
- 'taxonomy_format_type' => 'multi'
938
- )
939
- );
940
- }
941
- }
942
- }
943
- elseif ( 'user' == $object ) {
944
- $fields = array(
945
- 'user_login' => array(
946
- 'name' => 'user_login',
947
- 'label' => 'Title',
948
- 'type' => 'text',
949
- 'alias' => array( 'login' ),
950
- 'options' => array(
951
- 'required' => 1
952
- )
953
- ),
954
- 'user_nicename' => array(
955
- 'name' => 'user_nicename',
956
- 'label' => 'Permalink',
957
- 'type' => 'slug',
958
- 'alias' => array( 'nicename', 'slug', 'permalink' )
959
- ),
960
- 'display_name' => array(
961
- 'name' => 'display_name',
962
- 'label' => 'Display Name',
963
- 'type' => 'text',
964
- 'alias' => array( 'title', 'name' )
965
- ),
966
- 'user_pass' => array(
967
- 'name' => 'user_pass',
968
- 'label' => 'Password',
969
- 'type' => 'text',
970
- 'alias' => array( 'password', 'pass' ),
971
- 'options' => array(
972
- 'required' => 1,
973
- 'text_format_type' => 'password'
974
- )
975
- ),
976
- 'user_email' => array(
977
- 'name' => 'user_email',
978
- 'label' => 'E-mail',
979
- 'type' => 'text',
980
- 'alias' => array( 'email' ),
981
- 'options' => array(
982
- 'required' => 1,
983
- 'text_format_type' => 'email'
984
- )
985
- ),
986
- 'user_url' => array(
987
- 'name' => 'user_url',
988
- 'label' => 'URL',
989
- 'type' => 'text',
990
- 'alias' => array( 'url', 'website' ),
991
- 'options' => array(
992
- 'required' => 1,
993
- 'text_format_type' => 'website',
994
- 'text_format_website' => 'normal'
995
- )
996
- ),
997
- 'user_registered' => array(
998
- 'name' => 'user_registered',
999
- 'label' => 'Registration Date',
1000
- 'type' => 'date',
1001
- 'alias' => array( 'created', 'date', 'registered' ),
1002
- 'options' => array(
1003
- 'date_format_type' => 'datetime'
1004
- )
1005
- )
1006
- );
1007
- }
1008
- elseif ( 'comment' == $object ) {
1009
- $fields = array(
1010
- 'comment_content' => array(
1011
- 'name' => 'comment_content',
1012
- 'label' => 'Content',
1013
- 'type' => 'wysiwyg',
1014
- 'alias' => array( 'content' )
1015
- ),
1016
- 'comment_approved' => array(
1017
- 'name' => 'comment_approved',
1018
- 'label' => 'Approved',
1019
- 'type' => 'number',
1020
- 'alias' => array( 'approved' )
1021
- ),
1022
- 'comment_post_ID' => array(
1023
- 'name' => 'comment_post_ID',
1024
- 'label' => 'Post',
1025
- 'type' => 'pick',
1026
- 'alias' => array( 'post', 'post_id' ),
1027
- 'data' => array()
1028
- ),
1029
- 'user_id' => array(
1030
- 'name' => 'user_id',
1031
- 'label' => 'Author',
1032
- 'type' => 'pick',
1033
- 'alias' => array( 'author' ),
1034
- 'pick_object' => 'user',
1035
- 'data' => array()
1036
- ),
1037
- 'comment_date' => array(
1038
- 'name' => 'comment_date',
1039
- 'label' => 'Date',
1040
- 'type' => 'date',
1041
- 'alias' => array( 'created', 'date' ),
1042
- 'options' => array(
1043
- 'date_format_type' => 'datetime'
1044
- )
1045
- )
1046
- );
1047
- }
1048
- elseif ( 'taxonomy' == $object ) {
1049
- $fields = array(
1050
- 'name' => array(
1051
- 'name' => 'name',
1052
- 'label' => 'Title',
1053
- 'type' => 'text',
1054
- 'alias' => array( 'title' )
1055
- ),
1056
- 'slug' => array(
1057
- 'name' => 'slug',
1058
- 'label' => 'Permalink',
1059
- 'type' => 'slug',
1060
- 'alias' => array( 'permalink' )
1061
- ),
1062
- 'description' => array(
1063
- 'name' => 'description',
1064
- 'label' => 'Description',
1065
- 'type' => 'wysiwyg',
1066
- 'alias' => array( 'content' )
1067
- ),
1068
- 'taxonomy' => array(
1069
- 'name' => 'taxonomy',
1070
- 'label' => 'Taxonomy',
1071
- 'type' => 'pick',
1072
- 'alias' => array()
1073
- ),
1074
- 'parent' => array(
1075
- 'name' => 'parent',
1076
- 'label' => 'Parent',
1077
- 'type' => 'pick',
1078
- 'pick_object' => 'taxonomy',
1079
- 'pick_val' => '__current__',
1080
- 'alias' => array( 'parent' ),
1081
- 'data' => array(),
1082
- 'hidden' => true
1083
- ),
1084
- 'term_taxonomy_id' => array(
1085
- 'name' => 'term_taxonomy_id',
1086
- 'label' => 'Term Taxonomy ID',
1087
- 'type' => 'number',
1088
- 'alias' => array( 'term_taxonomy_id' ),
1089
- 'hidden' => true
1090
- ),
1091
- 'term_group' => array(
1092
- 'name' => 'term_group',
1093
- 'label' => 'Term Group',
1094
- 'type' => 'number',
1095
- 'alias' => array( 'term_group' ),
1096
- 'hidden' => true
1097
- ),
1098
- 'count' => array(
1099
- 'name' => 'count',
1100
- 'label' => 'Count',
1101
- 'type' => 'number',
1102
- 'alias' => array( 'count' ),
1103
- 'hidden' => true
1104
- )
1105
- );
1106
- }
1107
-
1108
- $fields = $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
1109
-
1110
- foreach ( $fields as $field => $options ) {
1111
- if ( !isset( $options[ 'alias' ] ) )
1112
- $options[ 'alias' ] = array();
1113
- else
1114
- $options[ 'alias' ] = (array) $options[ 'alias' ];
1115
-
1116
- if ( !isset( $options[ 'name' ] ) )
1117
- $options[ 'name' ] = $field;
1118
-
1119
- $fields[ $field ] = $options;
1120
- }
1121
-
1122
- $fields = PodsForm::fields_setup( $fields );
1123
-
1124
- if ( did_action( 'init' ) && pods_api_cache() )
1125
- pods_transient_set( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ), $fields );
1126
-
1127
- return $fields;
1128
- }
1129
-
1130
- /**
1131
- *
1132
- * @see PodsAPI::save_pod
1133
- *
1134
- * Add a Pod via the Wizard
1135
- *
1136
- * $params['create_extend'] string Create or Extend a Content Type
1137
- * $params['create_pod_type'] string Pod Type (for Creating)
1138
- * $params['create_name'] string Pod Name (for Creating)
1139
- * $params['create_label_plural'] string Plural Label (for Creating)
1140
- * $params['create_label_singular'] string Singular Label (for Creating)
1141
- * $params['create_storage'] string Storage Type (for Creating Post Types)
1142
- * $params['create_storage_taxonomy'] string Storage Type (for Creating Taxonomies)
1143
- * $params['extend_pod_type'] string Pod Type (for Extending)
1144
- * $params['extend_post_type'] string Post Type (for Extending Post Types)
1145
- * $params['extend_taxonomy'] string Taxonomy (for Extending Taxonomies)
1146
- * $params['extend_storage'] string Storage Type (for Extending Post Types / Users / Comments)
1147
- *
1148
- * @param array $params An associative array of parameters
1149
- *
1150
- * @return bool|int Pod ID
1151
- * @since 2.0
1152
- */
1153
- public function add_pod ( $params ) {
1154
- $defaults = array(
1155
- 'create_extend' => 'create',
1156
- 'create_pod_type' => 'post_type',
1157
-
1158
- 'create_name' => '',
1159
- 'create_label_singular' => '',
1160
- 'create_label_plural' => '',
1161
- 'create_storage' => 'meta',
1162
- 'create_storage_taxonomy' => 'none',
1163
-
1164
- 'create_setting_name' => '',
1165
- 'create_label_title' => '',
1166
- 'create_label_menu' => '',
1167
- 'create_menu_location' => 'settings',
1168
-
1169
- 'extend_pod_type' => 'post_type',
1170
- 'extend_post_type' => 'post',
1171
- 'extend_taxonomy' => 'category',
1172
- 'extend_table' => '',
1173
- 'extend_storage_taxonomy' => 'table',
1174
- 'extend_storage' => 'meta'
1175
- );
1176
-
1177
- $params = (object) array_merge( $defaults, (array) $params );
1178
-
1179
- if ( empty( $params->create_extend ) || !in_array( $params->create_extend, array( 'create', 'extend' ) ) )
1180
- return pods_error( __( 'Please choose whether to Create or Extend a Content Type', 'pods' ), $this );
1181
-
1182
- $pod_params = array(
1183
- 'name' => '',
1184
- 'label' => '',
1185
- 'type' => '',
1186
- 'storage' => 'table',
1187
- 'object' => '',
1188
- 'options' => array()
1189
- );
1190
-
1191
- if ( 'create' == $params->create_extend ) {
1192
- $label = ucwords( str_replace( '_', ' ', $params->create_name ) );
1193
-
1194
- if ( !empty( $params->create_label_singular ) )
1195
- $label = $params->create_label_singular;
1196
-
1197
- $pod_params[ 'name' ] = $params->create_name;
1198
- $pod_params[ 'label' ] = ( !empty( $params->create_label_plural ) ? $params->create_label_plural : $label );
1199
- $pod_params[ 'type' ] = $params->create_pod_type;
1200
- $pod_params[ 'options' ] = array(
1201
- 'label_singular' => ( !empty( $params->create_label_singular ) ? $params->create_label_singular : $pod_params[ 'label' ] ),
1202
- 'public' => 1,
1203
- 'show_ui' => 1
1204
- );
1205
-
1206
- // Auto-generate name if not provided
1207
- if ( empty( $pod_params[ 'name' ] ) && !empty( $pod_params[ 'options' ][ 'label_singular' ] ) )
1208
- $pod_params[ 'name' ] = pods_clean_name( $pod_params[ 'options' ][ 'label_singular' ] );
1209
-
1210
- if ( 'post_type' == $pod_params[ 'type' ] ) {
1211
- if ( empty( $pod_params[ 'name' ] ) )
1212
- return pods_error( 'Please enter a Name for this Pod', $this );
1213
-
1214
- $pod_params[ 'storage' ] = $params->create_storage;
1215
-
1216
- if ( pods_tableless() )
1217
- $pod_params[ 'storage' ] = 'meta';
1218
- }
1219
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1220
- if ( empty( $pod_params[ 'name' ] ) )
1221
- return pods_error( 'Please enter a Name for this Pod', $this );
1222
-
1223
- $pod_params[ 'storage' ] = $params->create_storage_taxonomy;
1224
-
1225
- if ( pods_tableless() )
1226
- $pod_params[ 'storage' ] = 'none';
1227
- }
1228
- elseif ( 'pod' == $pod_params[ 'type' ] ) {
1229
- if ( empty( $pod_params[ 'name' ] ) )
1230
- return pods_error( 'Please enter a Name for this Pod', $this );
1231
-
1232
- if ( pods_tableless() ) {
1233
- $pod_params[ 'type' ] = 'post_type';
1234
- $pod_params[ 'storage' ] = 'meta';
1235
- }
1236
- }
1237
- elseif ( 'settings' == $pod_params[ 'type' ] ) {
1238
- $pod_params[ 'name' ] = $params->create_setting_name;
1239
- $pod_params[ 'label' ] = ( !empty( $params->create_label_title ) ? $params->create_label_title : ucwords( str_replace( '_', ' ', $params->create_setting_name ) ) );
1240
- $pod_params[ 'options' ] = array(
1241
- 'menu_name' => ( !empty( $params->create_label_menu ) ? $params->create_label_menu : $pod_params[ 'label' ] ),
1242
- 'menu_location' => $params->create_menu_location
1243
- );
1244
- $pod_params[ 'storage' ] = 'none';
1245
-
1246
- // Auto-generate name if not provided
1247
- if ( empty( $pod_params[ 'name' ] ) && !empty( $pod_params[ 'label' ] ) )
1248
- $pod_params[ 'name' ] = pods_clean_name( $pod_params[ 'label' ] );
1249
-
1250
- if ( empty( $pod_params[ 'name' ] ) )
1251
- return pods_error( 'Please enter a Name for this Pod', $this );
1252
- }
1253
- }
1254
- elseif ( 'extend' == $params->create_extend ) {
1255
- $pod_params[ 'type' ] = $params->extend_pod_type;
1256
-
1257
- if ( 'post_type' == $pod_params[ 'type' ] ) {
1258
- $pod_params[ 'storage' ] = $params->extend_storage;
1259
-
1260
- if ( pods_tableless() )
1261
- $pod_params[ 'storage' ] = 'meta';
1262
-
1263
- $pod_params[ 'name' ] = $params->extend_post_type;
1264
- }
1265
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1266
- $pod_params[ 'storage' ] = $params->extend_storage_taxonomy;
1267
-
1268
- if ( pods_tableless() )
1269
- $pod_params[ 'storage' ] = 'none';
1270
-
1271
- $pod_params[ 'name' ] = $params->extend_taxonomy;
1272
- }
1273
- elseif ( 'table' == $pod_params[ 'type' ] ) {
1274
- $pod_params[ 'storage' ] = 'table';
1275
- $pod_params[ 'name' ] = $params->extend_table;
1276
- }
1277
- else {
1278
- $pod_params[ 'storage' ] = $params->extend_storage;
1279
-
1280
- if ( pods_tableless() )
1281
- $pod_params[ 'storage' ] = 'meta';
1282
-
1283
- $pod_params[ 'name' ] = $params->extend_pod_type;
1284
- }
1285
-
1286
- $pod_params[ 'label' ] = ucwords( str_replace( '_', ' ', $pod_params[ 'name' ] ) );
1287
- $pod_params[ 'object' ] = $pod_params[ 'name' ];
1288
- }
1289
-
1290
- if ( empty( $pod_params[ 'object' ] ) ) {
1291
- if ( 'post_type' == $pod_params[ 'type' ] ) {
1292
- $check = get_post_type_object( $pod_params[ 'name' ] );
1293
-
1294
- if ( !empty( $check ) )
1295
- return pods_error( sprintf( __( 'Post Type %s already exists, try extending it instead', 'pods' ), $pod_params[ 'name' ] ), $this );
1296
-
1297
- $pod_params[ 'options' ][ 'supports_title' ] = 1;
1298
- $pod_params[ 'options' ][ 'supports_editor' ] = 1;
1299
- }
1300
- elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
1301
- $check = get_taxonomy( $pod_params[ 'name' ] );
1302
-
1303
- if ( !empty( $check ) )
1304
- return pods_error( sprintf( __( 'Taxonomy %s already exists, try extending it instead', 'pods' ), $pod_params[ 'name' ] ), $this );
1305
- }
1306
- }
1307
-
1308
- if ( !empty( $pod_params ) )
1309
- return $this->save_pod( $pod_params );
1310
-
1311
- return false;
1312
- }
1313
-
1314
- /**
1315
- * Add or edit a Pod
1316
- *
1317
- * $params['id'] int The Pod ID
1318
- * $params['name'] string The Pod name
1319
- * $params['label'] string The Pod label
1320
- * $params['type'] string The Pod type
1321
- * $params['object'] string The object being extended (if any)
1322
- * $params['storage'] string The Pod storage
1323
- * $params['options'] array Options
1324
- *
1325
- * @param array $params An associative array of parameters
1326
- * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed, will sanitize them if false.
1327
- * @param bool|int $db (optional) Whether to save into the DB or just return Pod array.
1328
- *
1329
- * @return int Pod ID
1330
- * @since 1.7.9
1331
- */
1332
- public function save_pod ( $params, $sanitized = false, $db = true ) {
1333
- $tableless_field_types = PodsForm::tableless_field_types();
1334
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
1335
-
1336
- $load_params = (object) $params;
1337
-
1338
- if ( isset( $load_params->id ) && isset( $load_params->name ) )
1339
- unset( $load_params->name );
1340
-
1341
- if ( isset( $load_params->old_name ) )
1342
- $load_params->name = $load_params->old_name;
1343
-
1344
- $load_params->table_info = true;
1345
-
1346
- $pod = $this->load_pod( $load_params, false );
1347
-
1348
- $params = (object) $params;
1349
-
1350
- if ( false === $sanitized )
1351
- $params = pods_sanitize( $params );
1352
-
1353
- $old_id = $old_name = $old_storage = null;
1354
 
1355
- $old_fields = $old_options = array();
 
 
1356
 
1357
- if ( isset( $params->name ) && ! isset( $params->object ) ) {
1358
- $params->name = pods_clean_name( $params->name );
 
1359
  }
1360
 
1361
- if ( !empty( $pod ) ) {
1362
- if ( isset( $params->id ) && 0 < $params->id )
1363
- $old_id = $params->id;
1364
-
1365
- $params->id = $pod[ 'id' ];
1366
-
1367
- $old_name = $pod[ 'name' ];
1368
- $old_storage = $pod[ 'storage' ];
1369
- $old_fields = $pod[ 'fields' ];
1370
- $old_options = $pod[ 'options' ];
1371
-
1372
- if ( !isset( $params->name ) && empty( $params->name ) )
1373
- $params->name = $pod[ 'name' ];
1374
-
1375
- if ( $old_name != $params->name && false !== $this->pod_exists( array( 'name' => $params->name ) ) )
1376
- return pods_error( sprintf( __( 'Pod %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1377
-
1378
- if ( $old_name != $params->name && in_array( $pod[ 'type' ], array( 'user', 'comment', 'media' ) ) && in_array( $pod[ 'object' ], array( 'user', 'comment', 'media' ) ) )
1379
- return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
1380
-
1381
- if ( $old_name != $params->name && in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && !empty( $pod[ 'object' ] ) && $pod[ 'object' ] == $old_name )
1382
- return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
1383
-
1384
- if ( $old_id != $params->id ) {
1385
- if ( $params->type == $pod[ 'type' ] && isset( $params->object ) && $params->object == $pod[ 'object' ] )
1386
- return pods_error( sprintf( __( 'Pod using %s already exists, you can not reuse an object across multiple pods', 'pods' ), $params->object ), $this );
1387
- else
1388
- return pods_error( sprintf( __( 'Pod %s already exists', 'pods' ), $params->name ), $this );
1389
- }
1390
- }
1391
- elseif ( in_array( $params->name, array( 'order','orderby','post_type' ) ) && 'post_type' == pods_var( 'type', $params ) ) {
1392
- return pods_error( sprintf( 'There are certain names that a Custom Post Types cannot be named and unfortunately, %s is one of them.', $params->name ), $this );
1393
- }
1394
- else {
1395
- $pod = array(
1396
- 'id' => 0,
1397
- 'name' => $params->name,
1398
- 'label' => $params->name,
1399
- 'description' => '',
1400
- 'type' => 'pod',
1401
- 'storage' => 'table',
1402
- 'object' => '',
1403
- 'alias' => '',
1404
- 'options' => array(),
1405
- 'fields' => array()
1406
- );
1407
- }
1408
-
1409
- // Blank out fields and options for AJAX calls (everything should be sent to it for a full overwrite)
1410
- if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
1411
- $pod[ 'fields' ] = array();
1412
- $pod[ 'options' ] = array();
1413
- }
1414
-
1415
- // Setup options
1416
- $options = get_object_vars( $params );
1417
-
1418
- if ( isset( $options[ 'method' ] ) )
1419
- unset( $options[ 'method' ] );
1420
-
1421
- $options_ignore = array(
1422
- 'object_type',
1423
- 'object_name',
1424
- 'table',
1425
- 'meta_table',
1426
- 'pod_table',
1427
- 'field_id',
1428
- 'field_index',
1429
- 'field_slug',
1430
- 'field_type',
1431
- 'field_parent',
1432
- 'field_parent_select',
1433
- 'meta_field_id',
1434
- 'meta_field_index',
1435
- 'meta_field_value',
1436
- 'pod_field_id',
1437
- 'pod_field_index',
1438
- 'object_fields',
1439
- 'join',
1440
- 'where',
1441
- 'where_default',
1442
- 'orderby',
1443
- 'pod',
1444
- 'recurse',
1445
- 'table_info',
1446
- 'attributes',
1447
- 'group',
1448
- 'grouped',
1449
- 'developer_mode',
1450
- 'dependency',
1451
- 'depends-on',
1452
- 'excludes-on'
1453
- );
1454
-
1455
- foreach ( $options_ignore as $ignore ) {
1456
- if ( isset( $options[ $ignore ] ) )
1457
- unset( $options[ $ignore ] );
1458
- }
1459
-
1460
- $exclude = array(
1461
- 'id',
1462
- 'name',
1463
- 'label',
1464
- 'description',
1465
- 'type',
1466
- 'storage',
1467
- 'object',
1468
- 'alias',
1469
- 'options',
1470
- 'fields'
1471
- );
1472
-
1473
- foreach ( $exclude as $k => $exclude_field ) {
1474
- $aliases = array( $exclude_field );
1475
-
1476
- if ( is_array( $exclude_field ) ) {
1477
- $aliases = array_merge( array( $k ), $exclude_field );
1478
- $exclude_field = $k;
1479
- }
1480
-
1481
- foreach ( $aliases as $alias ) {
1482
- if ( isset( $options[ $alias ] ) ) {
1483
- $pod[ $exclude_field ] = pods_trim( $options[ $alias ] );
1484
-
1485
- unset( $options[ $alias ] );
1486
- }
1487
- }
1488
- }
1489
-
1490
- if ( pods_tableless() && !in_array( $pod[ 'type' ], array( 'settings', 'table' ) ) ) {
1491
- if ( 'pod' == $pod[ 'type' ] )
1492
- $pod[ 'type' ] = 'post_type';
1493
-
1494
- if ( 'table' == $pod[ 'storage' ] ) {
1495
- if ( 'taxonomy' == $pod[ 'type' ] )
1496
- $pod[ 'storage' ] = 'none';
1497
- else
1498
- $pod[ 'storage' ] = 'meta';
1499
- }
1500
- }
1501
-
1502
- $pod[ 'options' ][ 'type' ] = $pod[ 'type' ];
1503
- $pod[ 'options' ][ 'storage' ] = $pod[ 'storage' ];
1504
- $pod[ 'options' ][ 'object' ] = $pod[ 'object' ];
1505
- $pod[ 'options' ][ 'alias' ] = $pod[ 'alias' ];
1506
-
1507
- $pod[ 'options' ] = array_merge( $pod[ 'options' ], $options );
1508
 
1509
- /**
1510
- * @var WP_Query
1511
- */
1512
- global $wp_query;
1513
 
1514
- $reserved_query_vars = array(
1515
- 'post_type',
1516
- 'taxonomy',
1517
- 'output'
1518
- );
1519
 
1520
- if ( is_object( $wp_query ) ) {
1521
- $reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
1522
  }
1523
 
1524
- if ( isset( $pod[ 'options' ][ 'query_var_string' ] ) ) {
1525
- if ( in_array( $pod[ 'options' ][ 'query_var_string' ], $reserved_query_vars ) ) {
1526
- $pod[ 'options' ][ 'query_var_string' ] = $pod[ 'options' ][ 'type' ] . '_' . $pod[ 'options' ][ 'query_var_string' ];
1527
- }
1528
- }
1529
-
1530
- if ( isset( $pod[ 'options' ][ 'query_var' ] ) ) {
1531
- if ( in_array( $pod[ 'options' ][ 'query_var' ], $reserved_query_vars ) ) {
1532
- $pod[ 'options' ][ 'query_var' ] = $pod[ 'options' ][ 'type' ] . '_' . $pod[ 'options' ][ 'query_var' ];
1533
- }
1534
- }
1535
-
1536
- if ( strlen( $pod[ 'label' ] ) < 1 )
1537
- $pod[ 'label' ] = $pod[ 'name' ];
1538
-
1539
- if ( 'post_type' == $pod[ 'type' ] ) {
1540
- // Max length for post types are 20 characters
1541
- $pod[ 'name' ] = substr( $pod[ 'name' ], 0, 20 );
1542
- }
1543
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1544
- // Max length for taxonomies are 32 characters
1545
- $pod[ 'name' ] = substr( $pod[ 'name' ], 0, 32 );
1546
- }
1547
-
1548
- $params->id = $pod[ 'id' ];
1549
- $params->name = $pod[ 'name' ];
1550
-
1551
- if ( null !== $old_name && $old_name != $params->name && empty( $pod[ 'object' ] ) ) {
1552
- if ( 'post_type' == $pod[ 'type' ] ) {
1553
- $check = get_post_type_object( $params->name );
1554
-
1555
- if ( !empty( $check ) )
1556
- return pods_error( sprintf( __( 'Post Type %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1557
- }
1558
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1559
- $check = get_taxonomy( $params->name );
1560
-
1561
- if ( !empty( $check ) )
1562
- return pods_error( sprintf( __( 'Taxonomy %s already exists, you cannot rename %s to that', 'pods' ), $params->name, $old_name ), $this );
1563
- }
1564
- }
1565
-
1566
- $field_table_operation = true;
1567
-
1568
- // Add new pod
1569
- if ( empty( $params->id ) ) {
1570
- if ( strlen( $params->name ) < 1 )
1571
- return pods_error( __( 'Pod name cannot be empty', 'pods' ), $this );
1572
-
1573
- $post_data = array(
1574
- 'post_name' => $pod[ 'name' ],
1575
- 'post_title' => $pod[ 'label' ],
1576
- 'post_content' => $pod[ 'description' ],
1577
- 'post_type' => '_pods_pod',
1578
- 'post_status' => 'publish'
1579
- );
1580
-
1581
- if ( 'pod' == $pod[ 'type' ] && ( !is_array( $pod[ 'fields' ] ) || empty( $pod[ 'fields' ] ) ) ) {
1582
- $pod[ 'fields' ] = array();
1583
-
1584
- $pod[ 'fields' ][ 'name' ] = array(
1585
- 'name' => 'name',
1586
- 'label' => 'Name',
1587
- 'type' => 'text',
1588
- 'options' => array(
1589
- 'required' => '1'
1590
- )
1591
- );
1592
-
1593
- $pod[ 'fields' ][ 'created' ] = array(
1594
- 'name' => 'created',
1595
- 'label' => 'Date Created',
1596
- 'type' => 'datetime',
1597
- 'options' => array(
1598
- 'datetime_format' => 'ymd_slash',
1599
- 'datetime_time_type' => '12',
1600
- 'datetime_time_format' => 'h_mm_ss_A'
1601
- )
1602
- );
1603
-
1604
- $pod[ 'fields' ][ 'modified' ] = array(
1605
- 'name' => 'modified',
1606
- 'label' => 'Date Modified',
1607
- 'type' => 'datetime',
1608
- 'options' => array(
1609
- 'datetime_format' => 'ymd_slash',
1610
- 'datetime_time_type' => '12',
1611
- 'datetime_time_format' => 'h_mm_ss_A'
1612
- )
1613
- );
1614
-
1615
- $pod[ 'fields' ][ 'author' ] = array(
1616
- 'name' => 'author',
1617
- 'label' => 'Author',
1618
- 'type' => 'pick',
1619
- 'pick_object' => 'user',
1620
- 'options' => array(
1621
- 'pick_format_type' => 'single',
1622
- 'pick_format_single' => 'autocomplete',
1623
- 'default_value' => '{@user.ID}'
1624
- )
1625
- );
1626
-
1627
- $pod[ 'fields' ][ 'permalink' ] = array(
1628
- 'name' => 'permalink',
1629
- 'label' => 'Permalink',
1630
- 'type' => 'slug',
1631
- 'description' => 'Leave blank to auto-generate from Name'
1632
- );
1633
-
1634
- if ( !isset( $pod[ 'options' ][ 'pod_index' ] ) )
1635
- $pod[ 'options' ][ 'pod_index' ] = 'name';
1636
- }
1637
-
1638
- $pod = $this->do_hook( 'save_pod_default_pod', $pod, $params, $sanitized, $db );
1639
-
1640
- $field_table_operation = false;
1641
- }
1642
- else {
1643
- $post_data = array(
1644
- 'ID' => $pod[ 'id' ],
1645
- 'post_name' => $pod[ 'name' ],
1646
- 'post_title' => $pod[ 'label' ],
1647
- 'post_content' => $pod[ 'description' ],
1648
- 'post_status' => 'publish'
1649
- );
1650
- }
1651
-
1652
- if ( true === $db ) {
1653
- if ( !has_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ) ) )
1654
- add_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ), 100, 6 );
1655
-
1656
- $conflicted = false;
1657
-
1658
- // Headway compatibility fix
1659
- if ( has_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 ) ) {
1660
- remove_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
1661
-
1662
- $conflicted = true;
1663
- }
1664
-
1665
- $params->id = $this->save_wp_object( 'post', $post_data, $pod[ 'options' ], true, true );
1666
-
1667
- if ( $conflicted )
1668
- add_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
1669
-
1670
- if ( false === $params->id )
1671
- return pods_error( __( 'Cannot save Pod', 'pods' ), $this );
1672
- }
1673
- elseif ( empty( $params->id ) )
1674
- $params->id = (int) $db;
1675
-
1676
- $pod[ 'id' ] = $params->id;
1677
-
1678
- // Setup / update tables
1679
- if ( 'table' != $pod[ 'type' ] && 'table' == $pod[ 'storage' ] && $old_storage != $pod[ 'storage' ] && $db ) {
1680
- $definitions = array( "`id` BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY" );
1681
-
1682
- $defined_fields = array();
1683
-
1684
- foreach ( $pod[ 'fields' ] as $field ) {
1685
- if ( !is_array( $field ) || !isset( $field[ 'name' ] ) || in_array( $field[ 'name' ], $defined_fields ) )
1686
- continue;
1687
-
1688
- $defined_fields[] = $field[ 'name' ];
1689
-
1690
- if ( !in_array( $field[ 'type' ], $tableless_field_types ) || ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) ) {
1691
- $definition = $this->get_field_definition( $field[ 'type' ], array_merge( $field, pods_var_raw( 'options', $field, array() ) ) );
1692
-
1693
- if ( 0 < strlen( $definition ) )
1694
- $definitions[] = "`{$field['name']}` " . $definition;
1695
- }
1696
- }
1697
-
1698
- pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`" );
1699
-
1700
- $result = pods_query( "CREATE TABLE `@wp_pods_{$params->name}` (" . implode( ', ', $definitions ) . ") DEFAULT CHARSET utf8", $this );
1701
-
1702
- if ( empty( $result ) )
1703
- return pods_error( __( 'Cannot add Database Table for Pod', 'pods' ), $this );
1704
-
1705
- }
1706
- elseif ( 'table' != $pod[ 'type' ] && 'table' == $pod[ 'storage' ] && $pod[ 'storage' ] == $old_storage && null !== $old_name && $old_name != $params->name && $db ) {
1707
- $result = pods_query( "ALTER TABLE `@wp_pods_{$old_name}` RENAME `@wp_pods_{$params->name}`", $this );
1708
-
1709
- if ( empty( $result ) )
1710
- return pods_error( __( 'Cannot update Database Table for Pod', 'pods' ), $this );
1711
- }
1712
-
1713
- /**
1714
- * @var $wpdb wpdb
1715
- */
1716
- global $wpdb;
1717
-
1718
- if ( null !== $old_name && $old_name != $params->name && $db ) {
1719
- // Rename items in the DB pointed at the old WP Object names
1720
- if ( 'post_type' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1721
- $this->rename_wp_object_type( 'post', $old_name, $params->name );
1722
- }
1723
- elseif ( 'taxonomy' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1724
- $this->rename_wp_object_type( 'taxonomy', $old_name, $params->name );
1725
- }
1726
- elseif ( 'comment' == $pod[ 'type' ] && empty( $pod[ 'object' ] ) ) {
1727
- $this->rename_wp_object_type( 'comment', $old_name, $params->name );
1728
- }
1729
- elseif ( 'settings' == $pod[ 'type' ] ) {
1730
- $this->rename_wp_object_type( 'settings', $old_name, $params->name );
1731
- }
1732
-
1733
- // Sync any related fields if the name has changed
1734
- $fields = pods_query( "
1735
- SELECT `p`.`ID`
1736
- FROM `{$wpdb->posts}` AS `p`
1737
- LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
1738
- LEFT JOIN `{$wpdb->postmeta}` AS `pm2` ON `pm2`.`post_id` = `p`.`ID`
1739
- WHERE
1740
- `p`.`post_type` = '_pods_field'
1741
- AND `pm`.`meta_key` = 'pick_object'
1742
- AND (
1743
- `pm`.`meta_value` = 'pod'
1744
- OR `pm`.`meta_value` = '" . $pod[ 'type' ] . "'
1745
- )
1746
- AND `pm2`.`meta_key` = 'pick_val'
1747
- AND `pm2`.`meta_value` = '{$old_name}'
1748
- " );
1749
-
1750
- if ( !empty( $fields ) ) {
1751
- foreach ( $fields as $field ) {
1752
- update_post_meta( $field->ID, 'pick_object', $pod[ 'type' ] );
1753
- update_post_meta( $field->ID, 'pick_val', $params->name );
1754
- }
1755
- }
1756
-
1757
- $fields = pods_query( "
1758
- SELECT `p`.`ID`
1759
- FROM `{$wpdb->posts}` AS `p`
1760
- LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
1761
- WHERE
1762
- `p`.`post_type` = '_pods_field'
1763
- AND `pm`.`meta_key` = 'pick_object'
1764
- AND (
1765
- `pm`.`meta_value` = 'pod-{$old_name}'
1766
- OR `pm`.`meta_value` = '" . $pod[ 'type' ] . "-{$old_name}'
1767
- )
1768
- " );
1769
 
1770
- if ( !empty( $fields ) ) {
1771
- foreach ( $fields as $field ) {
1772
- update_post_meta( $field->ID, 'pick_object', $pod[ 'type' ] );
1773
- update_post_meta( $field->ID, 'pick_val', $params->name );
1774
- }
1775
- }
1776
- }
1777
 
1778
- // Sync built-in options for post types and taxonomies
1779
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && empty( $pod[ 'object' ] ) && $db ) {
1780
- // Build list of 'built_in' for later
1781
- $built_in = array();
1782
 
1783
- foreach ( $pod[ 'options' ] as $key => $val ) {
1784
- if ( false === strpos( $key, 'built_in_' ) )
1785
- continue;
1786
- elseif ( false !== strpos( $key, 'built_in_post_types_' ) )
1787
- $built_in_type = 'post_type';
1788
- elseif ( false !== strpos( $key, 'built_in_taxonomies_' ) )
1789
- $built_in_type = 'taxonomy';
1790
- else
1791
- continue;
 
 
 
 
1792
 
1793
- if ( $built_in_type == $pod[ 'type' ] )
1794
- continue;
1795
 
1796
- if ( !isset( $built_in[ $built_in_type ] ) )
1797
- $built_in[ $built_in_type ] = array();
1798
 
1799
- $built_in_object = str_replace( array( 'built_in_post_types_', 'built_in_taxonomies_' ), '', $key );
 
 
1800
 
1801
- $built_in[ $built_in_type ][ $built_in_object ] = (int) $val;
1802
- }
 
1803
 
1804
- $lookup_option = $lookup_built_in = false;
1805
 
1806
- $lookup_name = $pod[ 'name' ];
1807
 
1808
- if ( 'post_type' == $pod[ 'type' ] ) {
1809
- $lookup_option = 'built_in_post_types_' . $lookup_name;
1810
- $lookup_built_in = 'taxonomy';
1811
- }
1812
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1813
- $lookup_option = 'built_in_taxonomies_' . $lookup_name;
1814
- $lookup_built_in = 'post_type';
1815
- }
1816
 
1817
- if ( !empty( $lookup_option ) && !empty( $lookup_built_in ) && isset( $built_in[ $lookup_built_in ] ) ) {
1818
- foreach ( $built_in[ $lookup_built_in ] as $built_in_object => $val ) {
1819
- $search_val = 1;
1820
 
1821
- if ( 1 == $val )
1822
- $search_val = 0;
 
1823
 
1824
- $query = "SELECT p.ID FROM {$wpdb->posts} AS p
1825
- LEFT JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.ID AND pm.meta_key = '{$lookup_option}'
1826
- LEFT JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.ID AND pm2.meta_key = 'type' AND pm2.meta_value = '{$lookup_built_in}'
1827
- LEFT JOIN {$wpdb->postmeta} AS pm3 ON pm3.post_id = p.ID AND pm3.meta_key = 'object' AND pm3.meta_value = ''
1828
- WHERE p.post_type = '_pods_pod' AND p.post_name = '{$built_in_object}'
1829
- AND pm2.meta_id IS NOT NULL
1830
- AND ( pm.meta_id IS NULL OR pm.meta_value = {$search_val} )";
1831
 
1832
- $results = pods_query( $query );
 
1833
 
1834
- if ( !empty( $results ) ) {
1835
- foreach ( $results as $the_pod ) {
1836
- delete_post_meta( $the_pod->ID, $lookup_option );
1837
 
1838
- add_post_meta( $the_pod->ID, $lookup_option, $val );
1839
- }
1840
- }
1841
- }
1842
- }
1843
- }
1844
 
1845
- $saved = array();
1846
- $errors = array();
1847
 
1848
- $field_index_change = false;
1849
- $field_index_id = 0;
 
1850
 
1851
- $id_required = false;
 
 
 
 
 
 
 
1852
 
1853
- $field_index = pods_var( 'pod_index', $pod[ 'options' ], 'id', null, true );
 
 
 
 
 
 
1854
 
1855
- if ( 'pod' == $pod[ 'type' ] && !empty( $pod[ 'fields' ] ) && isset( $pod[ 'fields' ][ $field_index ] ) )
1856
- $field_index_id = $pod[ 'fields' ][ $field_index ];
 
1857
 
1858
- if ( isset( $params->fields ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
1859
- $fields = array();
1860
 
1861
- if ( isset( $params->fields ) ) {
1862
- $params->fields = (array) $params->fields;
 
 
 
 
 
 
 
 
 
 
 
 
 
1863
 
1864
- $weight = 0;
 
 
1865
 
1866
- foreach ( $params->fields as $field ) {
1867
- if ( !is_array( $field ) || !isset( $field[ 'name' ] ) )
1868
- continue;
1869
 
1870
- if ( !isset( $field[ 'weight' ] ) ) {
1871
- $field[ 'weight' ] = $weight;
1872
-
1873
- $weight++;
1874
- }
1875
 
1876
- $fields[ $field[ 'name' ] ] = $field;
1877
- }
1878
- }
1879
 
1880
- $weight = 0;
 
 
 
1881
 
1882
- $saved_field_ids = array();
 
 
1883
 
1884
- foreach ( $pod[ 'fields' ] as $k => $field ) {
1885
- if ( !empty( $old_id ) && ( !is_array( $field ) || !isset( $field[ 'name' ] ) || !isset( $fields[ $field[ 'name' ] ] ) ) ) {
1886
- // Iterative change handling for setup-edit.php
1887
- if ( !is_array( $field ) && isset( $old_fields[ $k ] ) )
1888
- $saved[ $old_fields[ $k ][ 'name' ] ] = true;
1889
 
1890
- continue;
1891
- }
 
 
 
1892
 
1893
- if ( !empty( $old_id ) )
1894
- $field = array_merge( $field, $fields[ $field[ 'name' ] ] );
1895
-
1896
- $field[ 'pod' ] = $pod;
1897
-
1898
- if ( !isset( $field[ 'weight' ] ) ) {
1899
- $field[ 'weight' ] = $weight;
1900
-
1901
- $weight++;
1902
- }
1903
-
1904
- if ( 0 < $field_index_id && pods_var( 'id', $field ) == $field_index_id )
1905
- $field_index_change = $field[ 'name' ];
1906
-
1907
- if ( 0 < pods_var( 'id', $field ) )
1908
- $id_required = true;
1909
-
1910
- if ( $id_required )
1911
- $field[ 'id_required' ] = true;
1912
-
1913
- $field_data = $field;
1914
-
1915
- $field = $this->save_field( $field_data, $field_table_operation, true, $db );
1916
-
1917
- if ( true !== $db ) {
1918
- $pod[ 'fields' ][ $k ] = $field;
1919
- $saved_field_ids[] = $field[ 'id' ];
1920
- }
1921
- else {
1922
- if ( !empty( $field ) && 0 < $field ) {
1923
- $saved[ $field_data[ 'name' ] ] = true;
1924
- $saved_field_ids[] = $field;
1925
- }
1926
- else
1927
- $errors[] = sprintf( __( 'Cannot save the %s field', 'pods' ), $field_data[ 'name' ] );
1928
- }
1929
- }
1930
-
1931
- if ( true === $db ) {
1932
- foreach ( $old_fields as $field ) {
1933
- if ( isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) || isset( $saved[ $field[ 'name' ] ] ) || in_array( $field[ 'id' ], $saved_field_ids ) )
1934
- continue;
1935
-
1936
- if ( $field[ 'id' ] == $field_index_id )
1937
- $field_index_change = 'id';
1938
- elseif ( $field[ 'name' ] == $field_index )
1939
- $field_index_change = 'id';
1940
-
1941
- $this->delete_field( array(
1942
- 'id' => (int) $field[ 'id' ],
1943
- 'name' => $field[ 'name' ],
1944
- 'pod' => $pod
1945
- ), $field_table_operation );
1946
- }
1947
- }
1948
-
1949
- // Update field index if the name has changed or the field has been removed
1950
- if ( false !== $field_index_change && true === $db )
1951
- update_post_meta( $pod[ 'id' ], 'pod_index', $field_index_change );
1952
- }
1953
-
1954
- if ( !empty( $errors ) )
1955
- return pods_error( $errors, $this );
1956
-
1957
- $this->cache_flush_pods( $pod );
1958
-
1959
- if ( 'post_type' == $pod[ 'type' ] )
1960
- PodsMeta::$post_types[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
1961
- elseif ( 'taxonomy' == $pod[ 'type' ] )
1962
- PodsMeta::$taxonomies[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
1963
- elseif ( 'media' == $pod[ 'type' ] )
1964
- PodsMeta::$media[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
1965
- elseif ( 'user' == $pod[ 'type' ] )
1966
- PodsMeta::$user[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
1967
- elseif ( 'comment' == $pod[ 'type' ] )
1968
- PodsMeta::$comment[ $pod[ 'id' ] ] = $this->load_pod( array( 'name' => $pod[ 'name' ] ) );
1969
-
1970
- // Register Post Types / Taxonomies post-registration from PodsInit
1971
- if ( !empty( PodsInit::$content_types_registered ) && in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && empty( $pod[ 'object' ] ) ) {
1972
- global $pods_init;
1973
-
1974
- $pods_init->setup_content_types( true );
1975
- }
1976
-
1977
- if ( true === $db )
1978
- return $pod[ 'id' ];
1979
- else
1980
- return $pod;
1981
- }
1982
-
1983
- /**
1984
- * Add or edit a field within a Pod
1985
- *
1986
- * $params['id'] int Field ID (id OR pod_id+pod+name required)
1987
- * $params['pod_id'] int Pod ID (id OR pod_id+pod+name required)
1988
- * $params['pod'] string Pod name (id OR pod_id+pod+name required)
1989
- * $params['name'] string Field name (id OR pod_id+pod+name required)
1990
- * $params['label'] string (optional) Field label
1991
- * $params['type'] string (optional) Field type (avatar, boolean, code, color, currency, date, datetime, email, file, number, paragraph, password, phone, pick, slug, text, time, website, wysiwyg)
1992
- * $params['pick_object'] string (optional) Related Object (for relationships)
1993
- * $params['pick_val'] string (optional) Related Object name (for relationships)
1994
- * $params['sister_id'] int (optional) Related Field ID (for bidirectional relationships)
1995
- * $params['weight'] int (optional) Order in which the field appears
1996
- * $params['options'] array (optional) Options
1997
- *
1998
- * @param array $params An associative array of parameters
1999
- * @param bool $table_operation (optional) Whether or not to handle table operations
2000
- * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2001
- * @param bool|int $db (optional) Whether to save into the DB or just return field array.
2002
- *
2003
- * @return int|array The field ID or field array (if !$db)
2004
- * @since 1.7.9
2005
- */
2006
- public function save_field ( $params, $table_operation = true, $sanitized = false, $db = true ) {
2007
- /**
2008
- * @var $wpdb wpdb
2009
- */
2010
- global $wpdb;
2011
-
2012
- if ( true !== $db )
2013
- $table_operation = false;
2014
-
2015
- $tableless_field_types = PodsForm::tableless_field_types();
2016
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
2017
-
2018
- $params = (object) $params;
2019
-
2020
- if ( false === $sanitized )
2021
- $params = pods_sanitize( $params );
2022
-
2023
- if ( isset( $params->pod_id ) )
2024
- $params->pod_id = pods_absint( $params->pod_id );
2025
-
2026
- if ( true !== $db )
2027
- $params->pod_id = (int) $db;
2028
-
2029
- $pod = null;
2030
- $save_pod = false;
2031
- $id_required = false;
2032
-
2033
- if ( isset( $params->id_required ) ) {
2034
- unset( $params->id_required );
2035
-
2036
- $id_required = true;
2037
- }
2038
-
2039
- if ( ( !isset( $params->pod ) || empty( $params->pod ) ) && ( !isset( $params->pod_id ) || empty( $params->pod_id ) ) )
2040
- return pods_error( __( 'Pod ID or name is required', 'pods' ), $this );
2041
-
2042
- if ( isset( $params->pod ) && is_array( $params->pod ) ) {
2043
- $pod = $params->pod;
2044
-
2045
- $save_pod = true;
2046
- }
2047
- elseif ( ( !isset( $params->pod_id ) || empty( $params->pod_id ) ) && ( true === $db || 0 < $db ) )
2048
- $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => true ) );
2049
- elseif ( !isset( $params->pod ) && ( true === $db || 0 < $db ) )
2050
- $pod = $this->load_pod( array( 'id' => $params->pod_id, 'table_info' => true ) );
2051
- elseif ( true === $db || 0 < $db )
2052
- $pod = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true ) );
2053
-
2054
- if ( empty( $pod ) && true === $db )
2055
- return pods_error( __( 'Pod not found', 'pods' ), $this );
2056
-
2057
- $params->pod_id = $pod[ 'id' ];
2058
- $params->pod = $pod[ 'name' ];
2059
- $params->pod_data = $pod;
2060
-
2061
- $params->name = pods_clean_name( $params->name, true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
2062
-
2063
- if ( !isset( $params->id ) )
2064
- $params->id = 0;
2065
-
2066
- if ( empty( $params->name ) )
2067
- return pods_error( 'Pod field name is required', $this );
2068
-
2069
- $field = $this->load_field( $params );
2070
-
2071
- unset( $params->pod_data );
2072
-
2073
- $old_id = $old_name = $old_type = $old_definition = $old_simple = $old_options = $old_sister_id = null;
2074
-
2075
- if ( !empty( $field ) ) {
2076
- $old_id = pods_var( 'id', $field );
2077
- $old_name = pods_clean_name( $field[ 'name' ], true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
2078
- $old_type = $field[ 'type' ];
2079
- $old_options = $field[ 'options' ];
2080
- $old_sister_id = (int) pods_var( 'sister_id', $old_options, 0 );
2081
-
2082
- $old_simple = ( 'pick' == $old_type && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) );
2083
-
2084
- if ( isset( $params->name ) && !empty( $params->name ) )
2085
- $field[ 'name' ] = $params->name;
2086
-
2087
- if ( $old_name != $field[ 'name' ] && false !== $this->field_exists( $params ) )
2088
- return pods_error( sprintf( __( 'Field %s already exists, you cannot rename %s to that', 'pods' ), $field[ 'name' ], $old_name ), $this );
2089
-
2090
- if ( ( $id_required || !empty( $params->id ) ) && ( empty( $old_id ) || $old_id != $params->id ) )
2091
- return pods_error( sprintf( __( 'Field %s already exists', 'pods' ), $field[ 'name' ] ), $this );
2092
-
2093
- if ( empty( $params->id ) )
2094
- $params->id = $old_id;
2095
-
2096
- if ( !in_array( $old_type, $tableless_field_types ) || $old_simple ) {
2097
- $definition = $this->get_field_definition( $old_type, array_merge( $field, $old_options ) );
2098
-
2099
- if ( 0 < strlen( $definition ) )
2100
- $old_definition = "`{$old_name}` " . $definition;
2101
- }
2102
- }
2103
- else {
2104
- $field = array(
2105
- 'id' => 0,
2106
- 'pod_id' => $params->pod_id,
2107
- 'name' => $params->name,
2108
- 'label' => $params->name,
2109
- 'description' => '',
2110
- 'type' => 'text',
2111
- 'pick_object' => '',
2112
- 'pick_val' => '',
2113
- 'sister_id' => '',
2114
- 'weight' => null,
2115
- 'options' => array()
2116
- );
2117
- }
2118
-
2119
- // Setup options
2120
- $options = get_object_vars( $params );
2121
-
2122
- $options_ignore = array(
2123
- 'method',
2124
- 'table_info',
2125
- 'attributes',
2126
- 'group',
2127
- 'grouped',
2128
- 'developer_mode',
2129
- 'dependency',
2130
- 'depends-on',
2131
- 'excludes-on'
2132
- );
2133
-
2134
- foreach ( $options_ignore as $ignore ) {
2135
- if ( isset( $options[ $ignore ] ) )
2136
- unset( $options[ $ignore ] );
2137
- }
2138
-
2139
- if ( isset( $options[ 'method' ] ) )
2140
- unset( $options[ 'method' ] );
2141
- elseif ( isset( $options[ 'table_info' ] ) )
2142
- unset( $options[ 'table_info' ] );
2143
-
2144
- $exclude = array(
2145
- 'id',
2146
- 'pod_id',
2147
- 'pod',
2148
- 'name',
2149
- 'label',
2150
- 'description',
2151
- 'type',
2152
- 'pick_object',
2153
- 'pick_val',
2154
- 'sister_id',
2155
- 'weight',
2156
- 'options'
2157
- );
2158
-
2159
- foreach ( $exclude as $k => $exclude_field ) {
2160
- $aliases = array( $exclude_field );
2161
-
2162
- if ( is_array( $exclude_field ) ) {
2163
- $aliases = array_merge( array( $k ), $exclude_field );
2164
- $exclude_field = $k;
2165
- }
2166
-
2167
- foreach ( $aliases as $alias ) {
2168
- if ( isset( $options[ $alias ] ) ) {
2169
- $field[ $exclude_field ] = pods_trim( $options[ $alias ] );
2170
-
2171
- unset( $options[ $alias ] );
2172
- }
2173
- }
2174
- }
2175
-
2176
- if ( strlen( $field[ 'label' ] ) < 1 )
2177
- $field[ 'label' ] = $field[ 'name' ];
2178
-
2179
- $field[ 'options' ][ 'type' ] = $field[ 'type' ];
2180
-
2181
- if ( in_array( $field[ 'options' ][ 'type' ], $tableless_field_types ) ) {
2182
- // Clean up special drop-down in field editor and save out pick_val
2183
- $field[ 'pick_object' ] = pods_var( 'pick_object', $field, '', null, true );
2184
-
2185
- if ( 0 === strpos( $field[ 'pick_object' ], 'pod-' ) ) {
2186
- $field[ 'pick_val' ] = pods_str_replace( 'pod-', '', $field[ 'pick_object' ], 1 );
2187
- $field[ 'pick_object' ] = 'pod';
2188
- }
2189
- elseif ( 0 === strpos( $field[ 'pick_object' ], 'post_type-' ) ) {
2190
- $field[ 'pick_val' ] = pods_str_replace( 'post_type-', '', $field[ 'pick_object' ], 1 );
2191
- $field[ 'pick_object' ] = 'post_type';
2192
- }
2193
- elseif ( 0 === strpos( $field[ 'pick_object' ], 'taxonomy-' ) ) {
2194
- $field[ 'pick_val' ] = pods_str_replace( 'taxonomy-', '', $field[ 'pick_object' ], 1 );
2195
- $field[ 'pick_object' ] = 'taxonomy';
2196
- }
2197
- elseif ( 'table' == $field[ 'pick_object' ] && 0 < strlen( pods_var_raw( 'pick_table', $field[ 'options' ] ) ) ) {
2198
- $field[ 'pick_val' ] = $field[ 'options' ][ 'pick_table' ];
2199
- $field[ 'pick_object' ] = 'table';
2200
- }
2201
- elseif ( false === strpos( $field[ 'pick_object' ], '-' ) && !in_array( $field[ 'pick_object' ], array( 'pod', 'post_type', 'taxonomy' ) ) ) {
2202
- $field[ 'pick_val' ] = '';
2203
- }
2204
- elseif ( 'custom-simple' == $field[ 'pick_object' ] ) {
2205
- $field[ 'pick_val' ] = '';
2206
- }
2207
-
2208
- $field[ 'options' ][ 'pick_object' ] = $field[ 'pick_object' ];
2209
- $field[ 'options' ][ 'pick_val' ] = $field[ 'pick_val' ];
2210
- $field[ 'options' ][ 'sister_id' ] = pods_var( 'sister_id', $field );
2211
-
2212
- unset( $field[ 'pick_object' ] );
2213
- unset( $field[ 'pick_val' ] );
2214
-
2215
- if ( isset( $field[ 'sister_id' ] ) )
2216
- unset( $field[ 'sister_id' ] );
2217
- }
2218
-
2219
- $field[ 'options' ] = array_merge( $field[ 'options' ], $options );
2220
-
2221
- $object_fields = (array) pods_var_raw( 'object_fields', $pod, array(), null, true );
2222
-
2223
- if ( 0 < $old_id && defined( 'PODS_FIELD_STRICT' ) && !PODS_FIELD_STRICT )
2224
- $params->id = $field[ 'id' ] = $old_id;
2225
-
2226
- // Add new field
2227
- if ( !isset( $params->id ) || empty( $params->id ) || empty( $field ) ) {
2228
- if ( $table_operation && in_array( $field[ 'name' ], array( 'created', 'modified' ) ) && !in_array( $field[ 'type' ], array( 'date', 'datetime' ) ) && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2229
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2230
-
2231
- if ( $table_operation && 'author' == $field[ 'name' ] && 'pick' != $field[ 'type' ] && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2232
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2233
-
2234
- if ( in_array( $field[ 'name' ], array( 'id', 'ID' ) ) )
2235
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2236
-
2237
- foreach ( $object_fields as $object_field => $object_field_opt ) {
2238
- if ( $object_field == $field[ 'name' ] || in_array( $field[ 'name' ], $object_field_opt[ 'alias' ] ) )
2239
- return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name. Also consider what WordPress and Pods provide you built-in.', 'pods' ), $field[ 'name' ] ), $this );
2240
- }
2241
-
2242
- if ( in_array( $field[ 'name' ], array( 'rss' ) ) ) // Reserved post_name values that can't be used as field names
2243
- $field[ 'name' ] .= '2';
2244
-
2245
- if ( 'slug' == $field[ 'type' ] && true === $db ) {
2246
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy', 'user' ) ) )
2247
- return pods_error( __( 'This pod already has an internal WordPress permalink field', 'pods' ), $this );
2248
-
2249
- $slug_field = get_posts( array(
2250
- 'post_type' => '_pods_field',
2251
- 'orderby' => 'menu_order',
2252
- 'order' => 'ASC',
2253
- 'posts_per_page' => 1,
2254
- 'post_parent' => $field[ 'pod_id' ],
2255
- 'meta_query' => array(
2256
- array(
2257
- 'key' => 'type',
2258
- 'value' => 'slug'
2259
- )
2260
- )
2261
- ) );
2262
-
2263
- if ( !empty( $slug_field ) )
2264
- return pods_error( __( 'This pod already has a permalink field', 'pods' ), $this );
2265
- }
2266
-
2267
- // Sink the new field to the bottom of the list
2268
- if ( null === $field[ 'weight' ] ) {
2269
- $field[ 'weight' ] = 0;
2270
-
2271
- $bottom_most_field = get_posts( array(
2272
- 'post_type' => '_pods_field',
2273
- 'orderby' => 'menu_order',
2274
- 'order' => 'DESC',
2275
- 'posts_per_page' => 1,
2276
- 'post_parent' => $field[ 'pod_id' ]
2277
- ) );
2278
-
2279
- if ( !empty( $bottom_most_field ) )
2280
- $field[ 'weight' ] = pods_absint( $bottom_most_field[ 0 ]->menu_order ) + 1;
2281
- }
2282
-
2283
- $field[ 'weight' ] = pods_absint( $field[ 'weight' ] );
2284
-
2285
- $post_data = array(
2286
- 'post_name' => $field[ 'name' ],
2287
- 'post_title' => $field[ 'label' ],
2288
- 'post_content' => $field[ 'description' ],
2289
- 'post_type' => '_pods_field',
2290
- 'post_parent' => $field[ 'pod_id' ],
2291
- 'post_status' => 'publish',
2292
- 'menu_order' => $field[ 'weight' ]
2293
- );
2294
- }
2295
- else {
2296
- if ( in_array( $field[ 'name' ], array( 'id', 'ID' ) ) ) {
2297
- if ( null !== $old_name )
2298
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2299
- else
2300
- return pods_error( sprintf( __( '%s is not editable', 'pods' ), $field[ 'name' ] ), $this );
2301
- }
2302
-
2303
- if ( null !== $old_name && $field[ 'name' ] != $old_name && in_array( $field[ 'name' ], array( 'created', 'modified' ) ) && !in_array( $field[ 'type' ], array( 'date', 'datetime' ) ) && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2304
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2305
-
2306
- if ( null !== $old_name && $field[ 'name' ] != $old_name && 'author' == $field[ 'name' ] && 'pick' != $field[ 'type' ] && ( !defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) )
2307
- return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2308
-
2309
- foreach ( $object_fields as $object_field => $object_field_opt ) {
2310
- if ( $object_field != $field[ 'name' ] && !in_array( $field[ 'name' ], $object_field_opt[ 'alias' ] ) )
2311
- continue;
2312
-
2313
- if ( null !== $old_name )
2314
- return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name', 'pods' ), $field[ 'name' ] ), $this );
2315
- else
2316
- return pods_error( sprintf( __( '%s is not editable', 'pods' ), $field[ 'name' ] ), $this );
2317
- }
2318
-
2319
- $post_data = array(
2320
- 'ID' => $field[ 'id' ],
2321
- 'post_name' => $field[ 'name' ],
2322
- 'post_title' => $field[ 'label' ],
2323
- 'post_content' => $field[ 'description' ]
2324
- );
2325
-
2326
- if ( null !== $field[ 'weight' ] ) {
2327
- $field[ 'weight' ] = pods_absint( $field[ 'weight' ] );
2328
-
2329
- $post_data[ 'menu_order' ] = $field[ 'weight' ];
2330
- }
2331
- }
2332
-
2333
- if ( true === $db ) {
2334
- if ( !has_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ) ) )
2335
- add_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ), 100, 6 );
2336
-
2337
- $conflicted = false;
2338
-
2339
- // Headway compatibility fix
2340
- if ( has_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 ) ) {
2341
- remove_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2342
-
2343
- $conflicted = true;
2344
- }
2345
-
2346
- $params->id = $this->save_wp_object( 'post', $post_data, $field[ 'options' ], true, true );
2347
-
2348
- if ( $conflicted )
2349
- add_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2350
-
2351
- if ( false === $params->id )
2352
- return pods_error( __( 'Cannot save Field', 'pods' ), $this );
2353
- }
2354
- else
2355
- $params->id = $field[ 'name' ];
2356
-
2357
- $field[ 'id' ] = $params->id;
2358
-
2359
- $simple = ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field[ 'options' ] ), $simple_tableless_objects ) );
2360
-
2361
- $definition = false;
2362
-
2363
- if ( !in_array( $field[ 'type' ], $tableless_field_types ) || $simple ) {
2364
- $field_definition = $this->get_field_definition( $field[ 'type' ], array_merge( $field, $field[ 'options' ] ) );
2365
-
2366
- if ( 0 < strlen( $field_definition ) )
2367
- $definition = '`' . $field[ 'name' ] . '` ' . $field_definition;
2368
- }
2369
-
2370
- $sister_id = (int) pods_var( 'sister_id', $field[ 'options' ], 0 );
2371
-
2372
- if ( $table_operation && 'table' == $pod[ 'storage' ] && !pods_tableless() ) {
2373
- if ( !empty( $old_id ) ) {
2374
- if ( ( $field[ 'type' ] != $old_type || $old_simple != $simple ) && empty( $definition ) )
2375
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$old_name}`", false );
2376
- elseif ( 0 < strlen( $definition ) ) {
2377
- if ( $old_name != $field[ 'name' ] || $old_simple != $simple ) {
2378
- $test = false;
2379
-
2380
- if ( 0 < strlen( $old_definition ) )
2381
- $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `{$old_name}` {$definition}", false );
2382
-
2383
- // If the old field doesn't exist, continue to add a new field
2384
- if ( false === $test )
2385
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2386
- }
2387
- elseif ( null !== $old_definition && $definition != $old_definition ) {
2388
- $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `{$old_name}` {$definition}", false );
2389
-
2390
- // If the old field doesn't exist, continue to add a new field
2391
- if ( false === $test )
2392
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2393
- }
2394
- }
2395
- }
2396
- elseif ( 0 < strlen( $definition ) ) {
2397
- $test = false;
2398
-
2399
- if ( 0 < strlen( $old_definition ) )
2400
- $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `" . $field[ 'name' ] . "` {$definition}", false );
2401
-
2402
- // If the old field doesn't exist, continue to add a new field
2403
- if ( false === $test )
2404
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2405
- }
2406
- }
2407
-
2408
- if ( !empty( $old_id ) && 'meta' == $pod[ 'storage' ] && $old_name != $field[ 'name' ] && $pod[ 'meta_table' ] != $pod[ 'table' ] ) {
2409
- $prepare = array(
2410
- $field[ 'name' ],
2411
- $old_name
2412
- );
2413
-
2414
- // Users don't have a type
2415
- if ( !empty( $pod[ 'field_type' ] ) )
2416
- $prepare[] = $pod[ 'name' ];
2417
-
2418
- pods_query( "
2419
- UPDATE `{$pod[ 'meta_table' ]}` AS `m`
2420
- LEFT JOIN `{$pod[ 'table' ]}` AS `t`
2421
- ON `t`.`{$pod[ 'field_id' ]}` = `m`.`{$pod[ 'meta_field_id' ]}`
2422
- SET
2423
- `m`.`{$pod[ 'meta_field_index' ]}` = %s
2424
- WHERE
2425
- `m`.`{$pod[ 'meta_field_index' ]}` = %s
2426
- " . ( !empty( $pod[ 'field_type' ] ) ? " AND `t`.`{$pod[ 'field_type' ]}` = %s" : "" ),
2427
- $prepare
2428
- );
2429
- }
2430
-
2431
- if ( $field[ 'type' ] != $old_type && in_array( $old_type, $tableless_field_types ) ) {
2432
- delete_post_meta( $old_sister_id, 'sister_id' );
2433
-
2434
- if ( true === $db ) {
2435
- pods_query( "
2436
- DELETE pm
2437
- FROM {$wpdb->postmeta} AS pm
2438
- LEFT JOIN {$wpdb->posts} AS p
2439
- ON p.post_type = '_pods_field'
2440
- AND p.ID = pm.post_id
2441
- WHERE
2442
- p.ID IS NOT NULL
2443
- AND pm.meta_key = 'sister_id'
2444
- AND pm.meta_value = %d
2445
- ", array(
2446
- $params->id
2447
- )
2448
- );
2449
-
2450
- if ( !pods_tableless() ) {
2451
- pods_query( "DELETE FROM @wp_podsrel WHERE `field_id` = {$params->id}", false );
2452
-
2453
- pods_query( "
2454
- UPDATE `@wp_podsrel`
2455
- SET `related_field_id` = 0
2456
- WHERE `field_id` = %d
2457
- ", array(
2458
- $old_sister_id
2459
- )
2460
- );
2461
- }
2462
- }
2463
- }
2464
- elseif ( 0 < $sister_id ) {
2465
- update_post_meta( $sister_id, 'sister_id', $params->id );
2466
-
2467
- if ( true === $db && ( !pods_tableless() ) ) {
2468
- pods_query( "
2469
- UPDATE `@wp_podsrel`
2470
- SET `related_field_id` = %d
2471
- WHERE `field_id` = %d
2472
- ",
2473
- array(
2474
- $params->id,
2475
- $sister_id
2476
- )
2477
- );
2478
- }
2479
- }
2480
- elseif ( 0 < $old_sister_id ) {
2481
- delete_post_meta( $old_sister_id, 'sister_id' );
2482
-
2483
- if ( true === $db && ( !pods_tableless() ) ) {
2484
- pods_query( "
2485
- UPDATE `@wp_podsrel`
2486
- SET `related_field_id` = 0
2487
- WHERE `field_id` = %d
2488
- ", array(
2489
- $old_sister_id
2490
- )
2491
- );
2492
- }
2493
- }
2494
-
2495
- if ( !empty( $old_id ) && $old_name != $field[ 'name' ] && true === $db ) {
2496
- pods_query( "
2497
- UPDATE `@wp_postmeta`
2498
- SET `meta_value` = %s
2499
- WHERE
2500
- `post_id` = %d
2501
- AND `meta_key` = 'pod_index'
2502
- AND `meta_value` = %s
2503
- ", array(
2504
- $field[ 'name' ],
2505
- $pod[ 'id' ],
2506
- $old_name
2507
- )
2508
- );
2509
- }
2510
-
2511
- if ( !$save_pod )
2512
- $this->cache_flush_pods( $pod );
2513
- else {
2514
- pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
2515
-
2516
- if ( !empty( $old_id ) && $old_name != $field[ 'name' ] )
2517
- pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $old_name );
2518
- }
2519
-
2520
- if ( true === $db )
2521
- return $params->id;
2522
- else
2523
- return $field;
2524
- }
2525
-
2526
- /**
2527
- * Fix Pod / Field post_name to ensure they are exactly as saved (allow multiple posts w/ same post_name)
2528
- *
2529
- * @param string $slug Unique slug value
2530
- * @param int $post_ID Post ID
2531
- * @param string $post_status Post Status
2532
- * @param string $post_type Post Type
2533
- * @param int $post_parent Post Parent ID
2534
- * @param string $original_slug Original slug value
2535
- *
2536
- * @return string Final slug value
2537
- *
2538
- * @since 2.3.3
2539
- */
2540
- public function save_slug_fix ( $slug, $post_ID, $post_status, $post_type, $post_parent = 0, $original_slug = null ) {
2541
- if ( in_array( $post_type, array( '_pods_field', '_pods_pod' ) ) && false !== strpos( $slug, '-' ) )
2542
- $slug = $original_slug;
2543
-
2544
- return $slug;
2545
- }
2546
-
2547
- /**
2548
- * Add or Edit a Pods Object
2549
- *
2550
- * $params['id'] int The Object ID
2551
- * $params['name'] string The Object name
2552
- * $params['type'] string The Object type
2553
- * $params['options'] Associative array of Object options
2554
- *
2555
- * @param array|object $params An associative array of parameters
2556
- * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed, will sanitize them if false.
2557
- *
2558
- * @return int The Object ID
2559
- * @since 2.0
2560
- */
2561
- public function save_object ( $params, $sanitized = false ) {
2562
- $params = (object) $params;
2563
-
2564
- if ( false === $sanitized )
2565
- $params = pods_sanitize( $params );
2566
-
2567
- if ( !isset( $params->name ) || empty( $params->name ) )
2568
- return pods_error( __( 'Name must be given to save an Object', 'pods' ), $this );
2569
-
2570
- if ( !isset( $params->type ) || empty( $params->type ) )
2571
- return pods_error( __( 'Type must be given to save an Object', 'pods' ), $this );
2572
-
2573
- $object = array(
2574
- 'id' => 0,
2575
- 'name' => $params->name,
2576
- 'type' => $params->type,
2577
- 'code' => '',
2578
- 'options' => array()
2579
- );
2580
-
2581
- // Setup options
2582
- $options = get_object_vars( $params );
2583
-
2584
- if ( isset( $options[ 'method' ] ) )
2585
- unset( $options[ 'method' ] );
2586
-
2587
- $exclude = array(
2588
- 'id',
2589
- 'name',
2590
- 'helper_type',
2591
- 'code',
2592
- 'options',
2593
- 'status'
2594
- );
2595
-
2596
- foreach ( $exclude as $k => $exclude_field ) {
2597
- $aliases = array( $exclude_field );
2598
-
2599
- if ( is_array( $exclude_field ) ) {
2600
- $aliases = array_merge( array( $k ), $exclude_field );
2601
- $exclude_field = $k;
2602
- }
2603
-
2604
- foreach ( $aliases as $alias ) {
2605
- if ( isset( $options[ $alias ] ) ) {
2606
- $object[ $exclude_field ] = pods_trim( $options[ $alias ] );
2607
-
2608
- unset( $options[ $alias ] );
2609
- }
2610
- }
2611
- }
2612
-
2613
- if ( 'helper' == $object[ 'type' ] )
2614
- $object[ 'options' ][ 'helper_type' ] = $object[ 'helper_type' ];
2615
-
2616
- if ( isset( $object[ 'options' ][ 'code' ] ) )
2617
- unset( $object[ 'options' ][ 'code' ] );
2618
-
2619
- $object[ 'options' ] = array_merge( $object[ 'options' ], $options );
2620
-
2621
- $post_data = array(
2622
- 'post_name' => pods_clean_name( $object[ 'name' ], true),
2623
- 'post_title' => $object[ 'name' ],
2624
- 'post_content' => $object[ 'code' ],
2625
- 'post_type' => '_pods_' . $object[ 'type' ],
2626
- 'post_status' => 'publish'
2627
- );
2628
-
2629
- if ( !empty( $object[ 'id' ] ) )
2630
- $post_data[ 'ID' ] = $object[ 'id' ];
2631
-
2632
- if ( null !== pods_var( 'status', $object, null, null, true ) )
2633
- $post_data[ 'post_status' ] = pods_var( 'status', $object, null, null, true );
2634
-
2635
- remove_filter( 'content_save_pre', 'balanceTags', 50 );
2636
-
2637
- $post_data = pods_sanitize( $post_data );
2638
-
2639
- $params->id = $this->save_post( $post_data, $object[ 'options' ], true, true );
2640
-
2641
- pods_transient_clear( 'pods_objects_' . $params->type );
2642
- pods_transient_clear( 'pods_objects_' . $params->type . '_get' );
2643
-
2644
- return $params->id;
2645
- }
2646
-
2647
- /**
2648
- * @see PodsAPI::save_object
2649
- *
2650
- * Add or edit a Pod Template
2651
- *
2652
- * $params['id'] int The template ID
2653
- * $params['name'] string The template name
2654
- * $params['code'] string The template code
2655
- *
2656
- * @param array|object $params An associative array of parameters
2657
- * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2658
- *
2659
- * @return int The Template ID
2660
- *
2661
- * @since 1.7.9
2662
- */
2663
- public function save_template ( $params, $sanitized = false ) {
2664
- $params = (object) $params;
2665
-
2666
- $params->type = 'template';
2667
-
2668
- return $this->save_object( $params, $sanitized );
2669
- }
2670
-
2671
- /**
2672
- * @see PodsAPI::save_object
2673
- *
2674
- * Add or edit a Pod Page
2675
- *
2676
- * $params['id'] int The page ID
2677
- * $params['name'] string The page URI
2678
- * $params['code'] string The page code
2679
- *
2680
- * @param array|object $params An associative array of parameters
2681
- * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2682
- *
2683
- * @return int The page ID
2684
- * @since 1.7.9
2685
- */
2686
- public function save_page ( $params, $sanitized = false ) {
2687
- $params = (object) $params;
2688
-
2689
- if ( !isset( $params->name ) ) {
2690
- $params->name = $params->uri;
2691
- unset( $params->uri );
2692
- }
2693
-
2694
- if ( isset( $params->phpcode ) ) {
2695
- $params->code = $params->phpcode;
2696
- unset( $params->phpcode );
2697
- }
2698
-
2699
- $params->name = trim( $params->name, '/' );
2700
- $params->type = 'page';
2701
-
2702
- return $this->save_object( $params, $sanitized );
2703
- }
2704
-
2705
- /**
2706
- * @see PodsAPI::save_object
2707
- *
2708
- * Add or edit a Pod Helper
2709
- *
2710
- * $params['id'] int The helper ID
2711
- * $params['name'] string The helper name
2712
- * $params['helper_type'] string The helper type ("pre_save", "display", etc)
2713
- * $params['code'] string The helper code
2714
- *
2715
- * @param array $params An associative array of parameters
2716
- * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will sanitize them if false.
2717
- *
2718
- * @return int The helper ID
2719
- * @since 1.7.9
2720
- */
2721
- public function save_helper ( $params, $sanitized = false ) {
2722
- $params = (object) $params;
2723
-
2724
- if ( isset( $params->phpcode ) ) {
2725
- $params->code = $params->phpcode;
2726
- unset( $params->phpcode );
2727
- }
2728
-
2729
- if ( isset( $params->type ) ) {
2730
- $params->helper_type = $params->type;
2731
- unset( $params->type );
2732
- }
2733
-
2734
- $params->type = 'helper';
2735
-
2736
- return $this->save_object( $params, $sanitized );
2737
- }
2738
-
2739
- /**
2740
- * Add or edit a single pod item
2741
- *
2742
- * $params['pod'] string The Pod name (pod or pod_id is required)
2743
- * $params['pod_id'] string The Pod ID (pod or pod_id is required)
2744
- * $params['id'] int The item ID
2745
- * $params['data'] array (optional) Associative array of field names + values
2746
- * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
2747
- * $params['track_changed_fields'] bool Set to true to enable tracking of saved fields via PodsAPI::get_changed_fields()
2748
- *
2749
- * @param array|object $params An associative array of parameters
2750
- *
2751
- * @return int The item ID
2752
- *
2753
- * @since 1.7.9
2754
- */
2755
- public function save_pod_item ( $params ) {
2756
 
2757
- global $wpdb;
 
 
 
2758
 
2759
- $params = (object) pods_str_replace( '@wp_', '{prefix}', $params );
 
2760
 
2761
- $tableless_field_types = PodsForm::tableless_field_types();
2762
- $repeatable_field_types = PodsForm::repeatable_field_types();
2763
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
2764
 
2765
- // @deprecated 2.0
2766
- if ( isset( $params->datatype ) ) {
2767
- pods_deprecated( '$params->pod instead of $params->datatype', '2.0' );
2768
 
2769
- $params->pod = $params->datatype;
 
2770
 
2771
- unset( $params->datatype );
 
 
 
 
 
 
 
 
 
 
 
 
 
2772
 
2773
- if ( isset( $params->pod_id ) ) {
2774
- pods_deprecated( '$params->id instead of $params->pod_id', '2.0' );
2775
 
2776
- $params->id = $params->pod_id;
2777
 
2778
- unset( $params->pod_id );
2779
- }
 
2780
 
2781
- if ( isset( $params->data ) && !empty( $params->data ) && is_array( $params->data ) ) {
2782
- $check = current( $params->data );
 
2783
 
2784
- if ( is_array( $check ) ) {
2785
- pods_deprecated( 'PodsAPI::save_pod_items', '2.0' );
2786
 
2787
- return $this->save_pod_items( $params, $params->data );
2788
- }
2789
- }
2790
- }
2791
 
2792
- // @deprecated 2.0
2793
- if ( isset( $params->tbl_row_id ) ) {
2794
- pods_deprecated( '$params->id instead of $params->tbl_row_id', '2.0' );
2795
 
2796
- $params->id = $params->tbl_row_id;
 
 
2797
 
2798
- unset( $params->tbl_row_id );
2799
- }
 
2800
 
2801
- // @deprecated 2.0
2802
- if ( isset( $params->columns ) ) {
2803
- pods_deprecated( '$params->data instead of $params->columns', '2.0' );
2804
 
2805
- $params->data = $params->columns;
 
2806
 
2807
- unset( $params->columns );
2808
- }
2809
 
2810
- if ( !isset( $params->pod ) )
2811
- $params->pod = false;
2812
- if ( isset( $params->pod_id ) )
2813
- $params->pod_id = pods_absint( $params->pod_id );
2814
- else
2815
- $params->pod_id = 0;
2816
 
2817
- if ( isset( $params->id ) )
2818
- $params->id = pods_absint( $params->id );
2819
- else
2820
- $params->id = 0;
 
 
 
 
2821
 
2822
- if ( !isset( $params->from ) )
2823
- $params->from = 'save';
 
 
 
 
 
2824
 
2825
- if ( !isset( $params->location ) )
2826
- $params->location = null;
 
2827
 
2828
- if ( !isset( $params->track_changed_fields ) )
2829
- $params->track_changed_fields = false;
2830
 
2831
- /**
2832
- * Override $params['track_changed_fields']
2833
- *
2834
- * Use for globally setting field change tracking.
2835
- *
2836
- * @param bool
2837
- *
2838
- * @since 2.3.19
2839
- */
2840
- $track_changed_fields = apply_filters( 'pods_api_save_pod_item_track_changed_fields_' . $params->pod, (boolean) $params->track_changed_fields, $params );
2841
- $changed_fields = array();
 
 
 
 
2842
 
2843
- if ( !isset( $params->clear_slug_cache ) ) {
2844
- $params->clear_slug_cache = true;
2845
  }
2846
 
2847
- // Support for bulk edit
2848
- if ( isset( $params->id ) && !empty( $params->id ) && is_array( $params->id ) ) {
2849
- $ids = array();
2850
- $new_params = $params;
2851
 
2852
- foreach ( $params->id as $id ) {
2853
- $new_params->id = $id;
 
2854
 
2855
- $ids[] = $this->save_pod_item( $new_params );
2856
- }
 
2857
 
2858
- return $ids;
2859
- }
 
 
2860
 
2861
- // Allow Helpers to know what's going on, are we adding or saving?
2862
- $is_new_item = false;
 
 
 
 
2863
 
2864
- if ( empty( $params->id ) )
2865
- $is_new_item = true;
 
 
2866
 
2867
- if ( isset( $params->is_new_item ) )
2868
- $is_new_item = (boolean) $params->is_new_item;
 
 
2869
 
2870
- // Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls
2871
- $bypass_helpers = false;
2872
 
2873
- if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers )
2874
- $bypass_helpers = true;
2875
 
2876
- // Allow Custom Fields not defined by Pods to be saved
2877
- $allow_custom_fields = false;
 
2878
 
2879
- if ( isset( $params->allow_custom_fields ) && false !== $params->allow_custom_fields )
2880
- $allow_custom_fields = true;
2881
 
2882
- // Get array of Pods
2883
- $pod = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true ) );
 
 
 
 
 
 
 
 
 
 
 
2884
 
2885
- if ( false === $pod )
2886
- return pods_error( __( 'Pod not found', 'pods' ), $this );
2887
 
2888
- $params->pod = $pod[ 'name' ];
2889
- $params->pod_id = $pod[ 'id' ];
2890
 
2891
- if ( 'settings' == $pod[ 'type' ] )
2892
- $params->id = $pod[ 'id' ];
 
2893
 
2894
- $fields = $pod[ 'fields' ];
 
 
2895
 
2896
- $object_fields = (array) pods_var_raw( 'object_fields', $pod, array(), null, true );
2897
 
2898
- $fields_active = array();
2899
- $custom_data = array();
2900
 
2901
- // Find the active fields (loop through $params->data to retain order)
2902
- if ( !empty( $params->data ) && is_array( $params->data ) ) {
2903
- $custom_fields = array();
2904
 
2905
- foreach ( $params->data as $field => $value ) {
2906
- if ( isset( $object_fields[ $field ] ) ) {
2907
- $object_fields[ $field ][ 'value' ] = $value;
2908
- $fields_active[] = $field;
2909
- }
2910
- elseif ( isset( $fields[ $field ] ) ) {
2911
- if ( 'save' == $params->from || true === PodsForm::permission( $fields[ $field ][ 'type' ], $field, $fields[ $field ], $fields, $pod, $params->id, $params ) ) {
2912
- $fields[ $field ][ 'value' ] = $value;
2913
- $fields_active[] = $field;
2914
- }
2915
- elseif ( !pods_has_permissions( $fields[ $field ][ 'options' ] ) && pods_var( 'hidden', $fields[ $field ][ 'options' ], false ) ) {
2916
- $fields[ $field ][ 'value' ] = $value;
2917
- $fields_active[] = $field;
2918
- }
2919
- }
2920
- else {
2921
- $found = false;
2922
 
2923
- foreach ( $object_fields as $object_field => $object_field_opt ) {
2924
- if ( in_array( $field, $object_field_opt[ 'alias' ] ) ) {
2925
- $object_fields[ $object_field ][ 'value' ] = $value;
2926
- $fields_active[] = $object_field;
2927
 
2928
- $found = true;
 
2929
 
2930
- break;
2931
- }
2932
- }
2933
 
2934
- if ( $allow_custom_fields && !$found )
2935
- $custom_fields[] = $field;
2936
- }
2937
- }
2938
 
2939
- if ( $allow_custom_fields && !empty( $custom_fields ) ) {
2940
- foreach ( $custom_fields as $field ) {
2941
- $custom_data[ $field ] = $params->data[ $field ];
2942
- }
2943
- }
2944
 
2945
- if ( $pod[ 'type' ] === 'taxonomy' && isset( $params->data ) && !empty( $params->data ) ) {
2946
- $term_data = $params->data;
 
 
 
 
2947
  }
 
2948
 
2949
- unset( $params->data );
2950
- }
2951
-
2952
- if ( empty( $params->id ) && !in_array( 'created', $fields_active ) && isset( $fields[ 'created' ] ) && in_array( $fields[ 'created' ][ 'type' ], array( 'date', 'datetime' ) ) ) {
2953
- $fields[ 'created' ][ 'value' ] = current_time( 'mysql' );
2954
- $fields_active[] = 'created';
2955
  }
2956
 
2957
- if ( !in_array( 'modified', $fields_active ) && isset( $fields[ 'modified' ] ) && in_array( $fields[ 'modified' ][ 'type' ], array( 'date', 'datetime' ) ) ) {
2958
- $fields[ 'modified' ][ 'value' ] = current_time( 'mysql' );
2959
- $fields_active[] = 'modified';
2960
  }
2961
 
2962
- if ( in_array( $pod[ 'type' ], array( 'pod', 'table' ) ) && empty( $params->id ) && !empty( $pod[ 'pod_field_index' ] ) && in_array( $pod[ 'pod_field_index' ], $fields_active ) && !in_array( $pod[ 'pod_field_slug' ], $fields_active ) && isset( $fields[ $pod[ 'pod_field_slug' ] ] ) ) {
2963
- $fields[ $pod[ 'pod_field_slug' ] ][ 'value' ] = ''; // this will get picked up by slug pre_save method
2964
- $fields_active[] = $pod[ 'pod_field_slug' ];
2965
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2966
 
2967
- // Handle hidden fields
2968
- if ( empty( $params->id ) ) {
2969
- foreach ( $fields as $field => $field_data ) {
2970
- if ( in_array( $field, $fields_active ) )
2971
- continue;
2972
 
2973
- if ( in_array( $params->from, array( 'save', 'process_form' ) ) || true === PodsForm::permission( $fields[ $field ][ 'type' ], $field, $fields[ $field ], $fields, $pod, $params->id, $params ) ) {
2974
- $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
2975
 
2976
- if ( null !== $value && '' !== $value && false !== $value ) {
2977
- $fields[ $field ][ 'value' ] = $value;
2978
- $fields_active[] = $field;
2979
- }
2980
- }
2981
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
2982
 
2983
- // Set default field values for object fields
2984
- if ( !empty( $object_fields ) ) {
2985
- foreach ( $object_fields as $field => $field_data ) {
2986
- if ( in_array( $field, $fields_active ) ) {
2987
- continue;
2988
- }
2989
- elseif ( !isset( $field_data[ 'default' ] ) || strlen( $field_data[ 'default' ] ) < 1 ) {
2990
- continue;
2991
- }
2992
 
2993
- $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
2994
 
2995
- if ( null !== $value && '' !== $value && false !== $value ) {
2996
- $object_fields[ $field ][ 'value' ] = $value;
2997
- $fields_active[] = $field;
2998
- }
2999
- }
 
 
 
 
 
 
 
 
 
 
 
3000
  }
3001
 
3002
- // Set default field values for Pod fields
3003
- foreach ( $fields as $field => $field_data ) {
3004
- if ( in_array( $field, $fields_active ) ) {
3005
- continue;
 
 
 
 
 
 
 
3006
  }
3007
- elseif ( !isset( $field_data[ 'default' ] ) || strlen( $field_data[ 'default' ] ) < 1 ) {
3008
- continue;
 
3009
  }
 
3010
 
3011
- $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data[ 'type' ], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3012
 
3013
- if ( null !== $value && '' !== $value && false !== $value ) {
3014
- $fields[ $field ][ 'value' ] = $value;
3015
- $fields_active[] = $field;
3016
- }
 
3017
  }
3018
- }
3019
 
3020
- $columns =& $fields; // @deprecated 2.0
3021
- $active_columns =& $fields_active; // @deprecated 2.0
3022
- $params->tbl_row_id =& $params->id; // @deprecated 2.0
3023
 
3024
- $pre_save_helpers = $post_save_helpers = array();
 
 
3025
 
3026
- $pieces = array(
3027
- 'fields',
3028
- 'params',
3029
- 'pod',
3030
- 'fields_active',
3031
- 'object_fields',
3032
- 'custom_fields',
3033
- 'custom_data',
3034
- 'track_changed_fields',
3035
- 'changed_fields'
3036
- );
3037
 
3038
- if ( false === $bypass_helpers ) {
3039
- // Plugin hooks
3040
- $hooked = $this->do_hook( 'pre_save_pod_item', compact( $pieces ), $is_new_item, $params->id );
3041
 
3042
- if ( is_array( $hooked ) && !empty( $hooked ) )
3043
- extract( $hooked );
 
 
 
 
 
 
 
 
 
 
 
3044
 
3045
- $hooked = $this->do_hook( "pre_save_pod_item_{$params->pod}", compact( $pieces ), $is_new_item, $params->id );
3046
 
3047
- if ( is_array( $hooked ) && !empty( $hooked ) )
3048
- extract( $hooked );
3049
 
3050
- if ( $is_new_item ) {
3051
- $hooked = $this->do_hook( 'pre_create_pod_item', compact( $pieces ) );
3052
 
3053
- if ( is_array( $hooked ) && !empty( $hooked ) )
3054
- extract( $hooked );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3055
 
3056
- $hooked = $this->do_hook( "pre_create_pod_item_{$params->pod}", compact( $pieces ) );
3057
 
3058
- if ( is_array( $hooked ) && !empty( $hooked ) )
3059
- extract( $hooked );
3060
- }
3061
- else {
3062
- $hooked = $this->do_hook( 'pre_edit_pod_item', compact( $pieces ), $params->id );
3063
 
3064
- if ( is_array( $hooked ) && !empty( $hooked ) )
3065
- extract( $hooked );
3066
 
3067
- $hooked = $this->do_hook( "pre_edit_pod_item_{$params->pod}", compact( $pieces ), $params->id );
3068
 
3069
- if ( is_array( $hooked ) && !empty( $hooked ) )
3070
- extract( $hooked );
3071
- }
3072
 
3073
- // Call any pre-save helpers (if not bypassed)
3074
- if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
3075
- if ( !empty( $pod[ 'options' ] ) && is_array( $pod[ 'options' ] ) ) {
3076
- $helpers = array( 'pre_save_helpers', 'post_save_helpers' );
3077
 
3078
- foreach ( $helpers as $helper ) {
3079
- if ( isset( $pod[ 'options' ][ $helper ] ) && !empty( $pod[ 'options' ][ $helper ] ) )
3080
- ${$helper} = explode( ',', $pod[ 'options' ][ $helper ] );
3081
- }
3082
- }
3083
 
3084
- if ( !empty( $pre_save_helpers ) ) {
3085
- pods_deprecated( sprintf( __( 'Pre-save helpers are deprecated, use the action pods_pre_save_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
3086
 
3087
- foreach ( $pre_save_helpers as $helper ) {
3088
- $helper = $this->load_helper( array( 'name' => $helper ) );
 
3089
 
3090
- if ( false !== $helper )
3091
- eval( '?>' . $helper[ 'code' ] );
3092
- }
3093
- }
3094
- }
3095
- }
3096
 
3097
- if ( $track_changed_fields ) {
3098
- $changed_fields = $this->get_changed_fields( compact( $pieces ) );
3099
  }
3100
 
3101
- $table_data = $table_formats = $update_values = $rel_fields = $rel_field_ids = array();
 
3102
 
3103
- $object_type = $pod[ 'type' ];
3104
 
3105
- $object_ID = 'ID';
 
 
3106
 
3107
- if ( 'comment' == $object_type )
3108
- $object_ID = 'comment_ID';
 
3109
 
3110
- $object_data = $object_meta = $post_term_data = array();
 
3111
 
3112
- if ( 'settings' == $object_type )
3113
- $object_data[ 'option_id' ] = $pod[ 'name' ];
3114
- elseif ( !empty( $params->id ) )
3115
- $object_data[ $object_ID ] = $params->id;
 
 
 
 
3116
 
3117
- $fields_active = array_unique( $fields_active );
3118
 
3119
- // Loop through each active field, validating and preparing the table data
3120
- foreach ( $fields_active as $field ) {
3121
- if ( isset( $object_fields[ $field ] ) )
3122
- $field_data = $object_fields[ $field ];
3123
- elseif ( isset( $fields[ $field ] ) )
3124
- $field_data = $fields[ $field ];
3125
- else
3126
- continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3127
 
3128
- $value = $field_data[ 'value' ];
3129
- $type = $field_data[ 'type' ];
3130
- $options = pods_var( 'options', $field_data, array() );
 
 
 
 
 
 
 
 
 
 
 
 
3131
 
3132
- // WPML AJAX compatibility
3133
- if ( is_admin() && isset( $_GET[ 'page' ] ) && false !== strpos( $_GET[ 'page' ], '/menu/languages.php' ) && isset( $_POST[ 'icl_ajx_action' ] ) && isset( $_POST[ '_icl_nonce' ] ) && wp_verify_nonce( $_POST[ '_icl_nonce' ], $_POST[ 'icl_ajx_action' ] . '_nonce' ) )
3134
- $options[ 'unique' ] = $fields[ $field ][ 'options' ][ 'unique' ] = $options[ 'required' ] = $fields[ $field ][ 'options' ][ 'required' ] = 0;
3135
- else {
3136
- // Validate value
3137
- $validate = $this->handle_field_validation( $value, $field, $object_fields, $fields, $pod, $params );
3138
 
3139
- if ( false === $validate )
3140
- $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field_data[ 'label' ] );
3141
- elseif ( true !== $validate )
3142
- $validate = (array) $validate;
3143
 
3144
- if ( !is_bool( $validate ) && !empty( $validate ) )
3145
- return pods_error( $validate, $this );
3146
- }
3147
 
3148
- $value = PodsForm::pre_save( $field_data[ 'type' ], $value, $params->id, $field, array_merge( $field_data, $options ), array_merge( $fields, $object_fields ), $pod, $params );
 
 
3149
 
3150
- $field_data[ 'value' ] = $value;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3151
 
3152
- if ( isset( $object_fields[ $field ] ) ) {
3153
- if ( 'taxonomy' == $object_fields[ $field ][ 'type' ] ) {
3154
- $post_term_data[ $field ] = $value;
3155
- }
3156
- else {
3157
- $object_data[ $field ] = $value;
3158
- }
3159
- }
3160
- else {
3161
- $simple = ( 'pick' == $type && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
3162
- $simple = (boolean) $this->do_hook( 'tableless_custom', $simple, $field_data, $field, $fields, $pod, $params );
3163
-
3164
- // Handle Simple Relationships
3165
- if ( $simple ) {
3166
- if ( !is_array( $value ) )
3167
- $value = explode( ',', $value );
3168
-
3169
- $pick_limit = (int) pods_var_raw( 'pick_limit', $options, 0 );
3170
-
3171
- if ( 'single' == pods_var_raw( 'pick_format_type', $options ) )
3172
- $pick_limit = 1;
3173
-
3174
- if ( 'custom-simple' == pods_var( 'pick_object', $field_data ) ) {
3175
- $custom = pods_var_raw( 'pick_custom', $options, '' );
3176
-
3177
- $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $field_data[ 'name' ], $value, array_merge( $field_data, $options ), $pod, $params->id );
3178
-
3179
- if ( empty( $value ) || empty( $custom ) )
3180
- $value = '';
3181
- elseif ( !empty( $custom ) ) {
3182
- if ( !is_array( $custom ) ) {
3183
- $custom = explode( "\n", $custom );
3184
-
3185
- $custom_values = array();
3186
-
3187
- foreach ( $custom as $c => $cv ) {
3188
- if ( 0 < strlen( $cv ) ) {
3189
- $custom_label = explode( '|', $cv );
3190
-
3191
- if ( !isset( $custom_label[ 1 ] ) )
3192
- $custom_label[ 1 ] = $custom_label[ 0 ];
3193
-
3194
- $custom_values[ $custom_label[ 0 ] ] = $custom_label[ 1 ];
3195
- }
3196
- }
3197
- }
3198
- else
3199
- $custom_values = $custom;
3200
-
3201
- $values = array();
3202
-
3203
- foreach ( $value as $k => $v ) {
3204
- $v = pods_unsanitize( $v );
3205
-
3206
- if ( isset( $custom_values[ $v ] ) )
3207
- $values[ $k ] = $v;
3208
- }
3209
-
3210
- $value = $values;
3211
- }
3212
- }
3213
-
3214
- if ( 0 < $pick_limit && !empty( $value ) )
3215
- $value = array_slice( $value, 0, $pick_limit );
3216
-
3217
- // Don't save an empty array, just make it an empty string
3218
- if ( empty( $value ) )
3219
- $value = '';
3220
- elseif ( is_array( $value ) ) {
3221
- // If there's just one item, don't save as an array, save the string
3222
- if ( 1 == $pick_limit || 1 == count( $value ) )
3223
- $value = implode( '', $value );
3224
- // If storage is set to table, json encode, otherwise WP will serialize automatically
3225
- elseif ( 'table' == pods_var( 'storage', $pod ) )
3226
- $value = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : json_encode( $value );
3227
- }
3228
- }
3229
-
3230
- // Prepare all table / meta data
3231
- if ( !in_array( $type, $tableless_field_types ) || $simple ) {
3232
- if ( in_array( $type, $repeatable_field_types ) && 1 == pods_var( $type . '_repeatable', $field_data, 0 ) ) {
3233
- // Don't save an empty array, just make it an empty string
3234
- if ( empty( $value ) )
3235
- $value = '';
3236
- elseif ( is_array( $value ) ) {
3237
- // If there's just one item, don't save as an array, save the string
3238
- if ( 1 == count( $value ) )
3239
- $value = implode( '', $value );
3240
- // If storage is set to table, json encode, otherwise WP will serialize automatically
3241
- elseif ( 'table' == pods_var( 'storage', $pod ) )
3242
- $value = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : json_encode( $value );
3243
- }
3244
- }
3245
-
3246
- $table_data[ $field ] = str_replace( array( '{prefix}', '@wp_' ), array( '{/prefix/}', '{prefix}' ), $value ); // Fix for pods_query
3247
- $table_formats[] = PodsForm::prepare( $type, $options );
3248
-
3249
- $object_meta[ $field ] = $value;
3250
- }
3251
- // Store relational field data to be looped through later
3252
- else {
3253
- // Convert values from a comma-separated string into an array
3254
- if ( !is_array( $value ) )
3255
- $value = explode( ',', $value );
3256
-
3257
- $rel_fields[ $type ][ $field ] = $value;
3258
- $rel_field_ids[] = $field_data[ 'id' ];
3259
- }
3260
- }
3261
- }
3262
-
3263
- if ( 'post_type' == $pod[ 'type' ] ) {
3264
- $post_type = $pod[ 'name' ];
3265
-
3266
- if ( !empty( $pod[ 'object' ] ) )
3267
- $post_type = $pod[ 'object' ];
3268
-
3269
- $object_data[ 'post_type' ] = $post_type;
3270
- }
3271
-
3272
- if ( ( 'meta' == $pod[ 'storage' ] || 'settings' == $pod[ 'type' ] ) && !in_array( $pod[ 'type' ], array( 'taxonomy', 'pod', 'table', '' ) ) ) {
3273
- if ( $allow_custom_fields && !empty( $custom_data ) )
3274
- $object_meta = array_merge( $custom_data, $object_meta );
3275
-
3276
- $fields_to_send = array_flip( array_keys( $object_meta ) );
3277
 
3278
- foreach ( $fields_to_send as $field => $field_data ) {
3279
- if ( isset( $object_fields[ $field ] ) ) {
3280
- $field_data = $object_fields[ $field ];
3281
- }
3282
- elseif ( isset( $fields[ $field ] ) ) {
3283
- $field_data = $fields[ $field ];
3284
- }
3285
- else {
3286
- unset( $fields_to_send[ $field ] );
 
 
 
 
 
 
 
 
 
 
 
3287
  }
3288
 
3289
- $fields_to_send[ $field ] = $field_data;
 
 
3290
  }
 
3291
 
3292
- $params->id = $this->save_wp_object( $object_type, $object_data, $object_meta, false, true, $fields_to_send );
 
3293
 
3294
- if ( !empty( $params->id ) && 'settings' == $object_type )
3295
- $params->id = $pod[ 'id' ];
3296
- }
3297
- else {
3298
- if ( !in_array( $pod[ 'type' ], array( 'taxonomy', 'pod', 'table', '' ) ) )
3299
- $params->id = $this->save_wp_object( $object_type, $object_data, array(), false, true );
3300
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
3301
- $term = pods_v( $object_fields[ 'name' ][ 'name' ], $object_data, '', null, true );
3302
- if ( !isset( $term_data ) ) {
3303
- $term_data = array ();
3304
- }
3305
 
3306
- if ( empty( $params->id ) || !empty( $term_data ) ) {
3307
- $taxonomy = $pod[ 'name' ];
3308
 
3309
- if ( !empty( $pod[ 'object' ] ) )
3310
- $taxonomy = $pod[ 'object' ];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3311
 
3312
- $params->id = $this->save_term( $params->id, $term, $taxonomy, $term_data, true );
3313
- }
3314
- }
3315
 
3316
- if ( 'table' == $pod[ 'storage' ] ) {
3317
- // Every row should have an id set here, otherwise Pods with nothing
3318
- // but relationship fields won't get properly ID'd
3319
- if ( empty( $params->id ) )
3320
- $params->id = 0;
3321
 
3322
- $table_data = array( 'id' => $params->id ) + $table_data;
3323
- array_unshift( $table_formats, '%d' );
3324
 
3325
- if ( !empty( $table_data ) ) {
3326
- $sql = pods_data()->insert_on_duplicate( "@wp_pods_{$params->pod}", $table_data, $table_formats );
 
3327
 
3328
- $id = pods_query( $sql, 'Cannot add/save table row' );
3329
 
3330
- if ( empty( $params->id ) )
3331
- $params->id = $id;
3332
- }
3333
- }
3334
- }
3335
 
3336
- $params->id = (int) $params->id;
3337
 
3338
- // Save terms for taxonomies associated to a post type
3339
- if ( 0 < $params->id && 'post_type' == $pod[ 'type' ] && !empty( $post_term_data ) ) {
3340
- foreach ( $post_term_data as $post_taxonomy => $post_terms ) {
3341
- wp_set_object_terms( $params->id, $post_terms, $post_taxonomy );
3342
  }
3343
  }
3344
 
3345
- $no_conflict = pods_no_conflict_check( $pod[ 'type' ] );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3346
 
3347
- if ( !$no_conflict )
3348
- pods_no_conflict_on( $pod[ 'type' ] );
3349
 
3350
- // Save relationship / file data
3351
- if ( !empty( $rel_fields ) ) {
3352
- foreach ( $rel_fields as $type => $data ) {
3353
- // Only handle tableless fields
3354
- if ( !in_array( $type, $tableless_field_types ) ) {
3355
- continue;
3356
  }
3357
 
3358
- foreach ( $data as $field => $values ) {
3359
- $pick_val = pods_var( 'pick_val', $fields[ $field ] );
3360
 
3361
- if ( 'table' == pods_var( 'pick_object', $fields[ $field ] ) ) {
3362
- $pick_val = pods_var( 'pick_table', $fields[ $field ][ 'options' ], $pick_val, null, true );
3363
  }
3364
 
3365
- if ( '__current__' == $pick_val ) {
3366
- if ( is_object( $pod ) ) {
3367
- $pick_val = $pod->pod;
3368
- }
3369
- elseif ( is_array( $pod ) ) {
3370
- $pick_val = $pod[ 'name' ];
 
 
 
 
 
 
 
3371
  }
3372
- elseif ( 0 < strlen( $pod ) ) {
3373
- $pick_val = $pod;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3374
  }
3375
- }
 
3376
 
3377
- $fields[ $field ][ 'options' ][ 'table_info' ] = pods_api()->get_table_info( pods_var( 'pick_object', $fields[ $field ] ), $pick_val, null, null, $fields[ $field ][ 'options' ] );
 
 
 
 
3378
 
3379
- if ( isset( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ] ) && !empty( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ] ) && isset( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
3380
- $search_data = pods( $fields[ $field ][ 'options' ][ 'table_info' ][ 'pod' ][ 'name' ] );
 
3381
 
3382
- $data_mode = 'pods';
3383
- }
3384
- else {
3385
- $search_data = pods_data();
3386
- $search_data->table( $fields[ $field ][ 'options' ][ 'table_info' ] );
3387
 
3388
- $data_mode = 'data';
3389
- }
 
 
 
 
 
 
 
 
 
 
 
 
3390
 
3391
- $find_rel_params = array(
3392
- 'select' => "`t`.`{$search_data->field_id}`",
3393
- 'where' => "`t`.`{$search_data->field_slug}` = %s OR `t`.`{$search_data->field_index}` = %s",
3394
- 'limit' => 1,
3395
- 'pagination' => false,
3396
- 'search' => false
3397
- );
3398
 
3399
- if ( empty( $search_data->field_slug ) && !empty( $search_data->field_index ) ) {
3400
- $find_rel_params[ 'where' ] = "`t`.`{$search_data->field_index}` = %s";
3401
- }
3402
- elseif ( empty( $search_data->field_slug ) && empty( $search_data->field_index ) ) {
3403
- $find_rel_params = false;
3404
- }
3405
 
3406
- $related_limit = (int) pods_var_raw( $type . '_limit', $fields[ $field ][ 'options' ], 0 );
 
 
 
 
3407
 
3408
- if ( 'single' == pods_var_raw( $type . '_format_type', $fields[ $field ][ 'options' ] ) ) {
3409
- $related_limit = 1;
3410
- }
3411
 
3412
- // Enforce integers / unique values for IDs
3413
- $value_ids = array();
3414
 
3415
- $is_file_field = in_array( $type, PodsForm::file_field_types() );
3416
- $is_taggable = ( in_array( $type, PodsForm::tableless_field_types() ) && 1 == pods_v( $type . '_taggable', $fields[ $field ][ 'options' ] ) );
 
 
3417
 
3418
- // @todo Handle simple relationships eventually
3419
- foreach ( $values as $v ) {
3420
- if ( !empty( $v ) ) {
3421
- if ( !is_array( $v ) ) {
3422
- if ( !preg_match( '/[^0-9]/', $v ) ) {
3423
- $v = (int) $v;
3424
- }
3425
- // File handling
3426
- elseif ( $is_file_field ) {
3427
- // Get ID from GUID
3428
- $v = pods_image_id_from_field( $v );
3429
 
3430
- // If file not found, add it
3431
- if ( empty( $v ) ) {
3432
- $v = pods_attachment_import( $v );
3433
- }
3434
- }
3435
- // Reference by slug
3436
- else {
3437
- $v_data = false;
3438
 
3439
- if ( false !== $find_rel_params ) {
3440
- $rel_params = $find_rel_params;
3441
- $rel_params[ 'where' ] = $wpdb->prepare( $rel_params[ 'where' ], array( $v, $v ) );
3442
 
3443
- $search_data->select( $rel_params );
 
3444
 
3445
- $v_data = $search_data->fetch( $v );
3446
- }
 
 
3447
 
3448
- if ( !empty( $v_data ) && isset( $v_data[ $search_data->field_id ] ) ) {
3449
- $v = (int) $v_data[ $search_data->field_id ];
3450
- }
3451
- // Allow tagging for Pods objects
3452
- elseif ( $is_taggable && 'pods' == $data_mode ) {
3453
- $tag_data = array(
3454
- $search_data->field_index => $v
3455
- );
3456
 
3457
- if ( 'post_type' == $search_data->pod_data[ 'type' ] ) {
3458
- $tag_data[ 'post_status' ] = 'publish';
3459
- }
3460
 
3461
- /**
3462
- * Filter for changing tag before adding new item.
3463
- *
3464
- * @param array $tag_data Fields for creating new item.
3465
- * @param int $v Field ID of tag.
3466
- * @param obj $search_data Search object for tag.
3467
- * @param string $field Table info for field.
3468
- * @param array $pieces Field array.
3469
- *
3470
- * @since 2.3.19
3471
- */
3472
- $tag_data = apply_filters( 'pods_api_save_pod_item_taggable_data', $tag_data, $v, $search_data, $field, compact( $pieces ) );
3473
 
3474
- // Save $v to a new item on related object
3475
- $v = $search_data->add( $tag_data );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3476
 
3477
- // @todo Support non-Pods for tagging
3478
- }
3479
- }
3480
- }
3481
- elseif ( $is_file_field && isset( $v[ 'id' ] ) ) {
3482
- $v = (int) $v[ 'id' ];
3483
- }
3484
- else {
3485
- continue;
3486
- }
3487
 
3488
- if ( !empty( $v ) && !in_array( $v, $value_ids ) ) {
3489
- $value_ids[] = $v;
3490
- }
3491
- }
3492
- }
3493
-
3494
- $value_ids = array_unique( array_filter( $value_ids ) );
3495
-
3496
- // Limit values
3497
- if ( 0 < $related_limit && !empty( $value_ids ) )
3498
- $value_ids = array_slice( $value_ids, 0, $related_limit );
3499
-
3500
- // Get current values
3501
- if ( 'pick' == $type && isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] ) && isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'current_ids' ] ) )
3502
- $related_ids = PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'current_ids' ];
3503
- else
3504
- $related_ids = $this->lookup_related_items( $fields[ $field ][ 'id' ], $pod[ 'id' ], $params->id, $fields[ $field ], $pod );
3505
-
3506
- // Get ids to remove
3507
- $remove_ids = array_diff( $related_ids, $value_ids );
3508
-
3509
- // Delete relationships
3510
- if ( !empty( $remove_ids ) )
3511
- $this->delete_relationships( $params->id, $remove_ids, $pod, $fields[ $field ] );
3512
-
3513
- // Save relationships
3514
- if ( !empty( $value_ids ) )
3515
- $this->save_relationships( $params->id, $value_ids, $pod, $fields[ $field ] );
3516
-
3517
- // Run save function for field type (where needed)
3518
- PodsForm::save( $type, $values, $params->id, $field, array_merge( $fields[ $field ], $fields[ $field ][ 'options' ] ), array_merge( $fields, $object_fields ), $pod, $params );
3519
- }
3520
-
3521
- // Unset data no longer needed
3522
- if ( 'pick' == $type ) {
3523
- foreach ( $data as $field => $values ) {
3524
- if ( isset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] ) ) {
3525
- unset( PodsField_Pick::$related_data[ PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ][ 'related_field' ][ 'id' ] ] );
3526
- unset( PodsField_Pick::$related_data[ $fields[ $field ][ 'id' ] ] );
3527
- }
3528
- }
3529
- }
3530
- }
3531
- }
3532
-
3533
- if ( !$no_conflict )
3534
- pods_no_conflict_off( $pod[ 'type' ] );
3535
-
3536
- if ( false === $bypass_helpers ) {
3537
- $pieces = compact( $pieces );
3538
-
3539
- // Plugin hooks
3540
- $this->do_hook( 'post_save_pod_item', $pieces, $is_new_item, $params->id );
3541
- $this->do_hook( "post_save_pod_item_{$params->pod}", $pieces, $is_new_item, $params->id );
3542
-
3543
- if ( $is_new_item ) {
3544
- $this->do_hook( 'post_create_pod_item', $pieces, $params->id );
3545
- $this->do_hook( "post_create_pod_item_{$params->pod}", $pieces, $params->id );
3546
- }
3547
- else {
3548
- $this->do_hook( 'post_edit_pod_item', $pieces, $params->id );
3549
- $this->do_hook( "post_edit_pod_item_{$params->pod}", $pieces, $params->id );
3550
- }
3551
-
3552
- // Call any post-save helpers (if not bypassed)
3553
- if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
3554
- if ( !empty( $post_save_helpers ) ) {
3555
- pods_deprecated( sprintf( __( 'Post-save helpers are deprecated, use the action pods_post_save_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
3556
-
3557
- foreach ( $post_save_helpers as $helper ) {
3558
- $helper = $this->load_helper( array( 'name' => $helper ) );
3559
-
3560
- if ( false !== $helper && ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) )
3561
- eval( '?>' . $helper[ 'code' ] );
3562
- }
3563
- }
3564
- }
3565
- }
3566
-
3567
- // Clear cache
3568
- pods_cache_clear( $params->id, 'pods_items_' . $pod[ 'name' ] );
3569
-
3570
- if ( $params->clear_slug_cache && !empty( $pod[ 'field_slug' ] ) ) {
3571
- $slug = pods( $pod[ 'name' ], $params->id )->field( $pod[ 'field_slug' ] );
3572
 
3573
- if ( 0 < strlen( $slug ) ) {
3574
- pods_cache_clear( $slug, 'pods_items_' . $pod[ 'name' ] );
 
 
 
 
 
 
 
 
 
3575
  }
3576
  }
3577
 
3578
- // Clear WP meta cache
3579
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy', 'user', 'comment' ) ) ) {
3580
- $meta_type = $pod[ 'type' ];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3581
 
3582
- if ( 'post_type' == $meta_type )
3583
- $meta_type = 'post';
 
 
 
3584
 
3585
- wp_cache_delete( $params->id, $meta_type . '_meta' );
3586
- wp_cache_delete( $params->id, 'pods_' . $meta_type . '_meta' );
3587
- }
3588
 
3589
- // Success! Return the id
3590
- return $params->id;
 
3591
 
3592
- }
3593
 
3594
- /**
3595
- * @see PodsAPI::save_pod_item
3596
- * Add multiple pod items
3597
- *
3598
- * $params['pod'] string The Pod name (pod or pod_id is required)
3599
- * $params['pod_id'] string The Pod ID (pod or pod_id is required)
3600
- * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
3601
- *
3602
- * $data['id'] int The item ID (optional)
3603
- * $data['data'] array An associative array of field names + values
3604
- *
3605
- * @param array|object $params An associative array of parameters, data excluded
3606
- * @param array $data An associative array of pod ids and field names + values (arrays of field data)
3607
- *
3608
- * @return int The item ID
3609
- * @since 2.0
3610
- */
3611
- public function save_pod_items ( $params, $data ) {
3612
- $params = (object) $params;
3613
 
3614
- $ids = array();
 
3615
 
3616
- foreach ( $data as $fields ) {
3617
- $params->data = $fields;
 
 
3618
 
3619
- if ( isset( $fields[ 'id' ] ) && isset( $fields[ 'data' ] ) ) {
3620
- $params->id = $fields[ 'id' ];
3621
- $params->data = $fields[ 'data' ];
3622
- }
3623
 
3624
- $ids[] = $this->save_pod_item( $params );
3625
- }
 
3626
 
3627
- return $ids;
3628
- }
 
3629
 
3630
- /**
3631
- * Get the fields that have changed during a save
3632
- *
3633
- * @param array $pieces Pieces array from save_pod_item
3634
- *
3635
- * @return array Array of fields and values that have changed
3636
- */
3637
- public function get_changed_fields( $pieces ) {
3638
 
3639
- $fields = $pieces[ 'fields' ];
3640
- $fields_active = $pieces[ 'fields_active' ];
 
3641
 
3642
- $fields_changed = array();
 
3643
 
3644
- if ( 0 < $pieces[ 'params' ]->id ) {
3645
- $pod = pods( $pieces[ 'params' ]->pod, $pieces[ 'params' ]->id );
 
 
 
 
 
 
 
 
3646
 
3647
- foreach ( $fields_active as $field ) {
3648
- if ( isset( $fields[ $field ] ) && $pod->raw( $field ) != $fields[ $field ][ 'value' ] ) {
3649
- $fields_changed[ $field ] = $fields[ $field ][ 'value' ];
3650
- }
 
 
 
 
 
3651
  }
 
3652
  }
3653
 
3654
- return $fields_changed;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3655
 
3656
- }
 
3657
 
3658
- /**
3659
- * Save relationships
3660
- *
3661
- * @param int $id ID of item
3662
- * @param int|array $related_id ID or IDs to save
3663
- * @param array $pod Pod data
3664
- * @param array $field Field data
3665
- */
3666
- public function save_relationships ( $id, $related_ids, $pod, $field ) {
3667
- // Get current values
3668
- if ( 'pick' == $field[ 'type' ] && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ] ) && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'current_ids' ] ) )
3669
- $current_ids = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'current_ids' ];
3670
- else
3671
- $current_ids = $this->lookup_related_items( $field[ 'id' ], $pod[ 'id' ], $id, $field, $pod );
3672
-
3673
- if ( !is_array( $related_ids ) )
3674
- $related_ids = implode( ',', $related_ids );
3675
-
3676
- foreach ( $related_ids as $k => $related_id ) {
3677
- $related_ids[ $k ] = (int) $related_id;
3678
- }
3679
-
3680
- $related_ids = array_unique( array_filter( $related_ids ) );
3681
-
3682
- $related_limit = (int) pods_var_raw( $field[ 'type' ] . '_limit', $field[ 'options' ], 0 );
3683
-
3684
- if ( 'single' == pods_var_raw( $field[ 'type' ] . '_format_type', $field[ 'options' ] ) )
3685
- $related_limit = 1;
3686
-
3687
- // Limit values
3688
- if ( 0 < $related_limit && !empty( $related_ids ) )
3689
- $related_ids = array_slice( $related_ids, 0, $related_limit );
3690
-
3691
- // Post Types, Media, Users, and Comments (meta-based)
3692
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'media', 'user', 'comment' ) ) ) {
3693
- $object_type = $pod[ 'type' ];
3694
-
3695
- if ( 'post_type' == $object_type || 'media' == $object_type )
3696
- $object_type = 'post';
3697
-
3698
- delete_metadata( $object_type, $id, $field[ 'name' ] );
3699
-
3700
- if ( !empty( $related_ids ) ) {
3701
- update_metadata( $object_type, $id, '_pods_' . $field[ 'name' ], $related_ids );
3702
-
3703
- foreach ( $related_ids as $related_id ) {
3704
- add_metadata( $object_type, $id, $field[ 'name' ], $related_id );
3705
- }
3706
- }
3707
- else
3708
- delete_metadata( $object_type, $id, '_pods_' . $field[ 'name' ] );
3709
- }
3710
- // Custom Settings Pages (options-based)
3711
- elseif ( 'settings' == $pod[ 'type' ] ) {
3712
- if ( !empty( $related_ids ) )
3713
- update_option( $pod[ 'name' ] . '_' . $field[ 'name' ], $related_ids );
3714
- else
3715
- delete_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
3716
- }
3717
-
3718
- $related_pod_id = $related_field_id = 0;
3719
-
3720
- if ( 'pick' == $field[ 'type' ] && isset( PodsField_Pick::$related_data[ $field[ 'id' ] ] ) && !empty( PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_field' ] ) ) {
3721
- $related_pod_id = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_pod' ][ 'id' ];
3722
- $related_field_id = PodsField_Pick::$related_data[ $field[ 'id' ] ][ 'related_field' ][ 'id' ];
3723
- }
3724
-
3725
- // Relationships table
3726
- if ( !pods_tableless() ) {
3727
- $related_weight = 0;
3728
-
3729
- foreach ( $related_ids as $related_id ) {
3730
- if ( in_array( $related_id, $current_ids ) ) {
3731
- pods_query( "
3732
- UPDATE `@wp_podsrel`
3733
- SET
3734
- `pod_id` = %d,
3735
- `field_id` = %d,
3736
- `item_id` = %d,
3737
- `related_pod_id` = %d,
3738
- `related_field_id` = %d,
3739
- `related_item_id` = %d,
3740
- `weight` = %d
3741
- WHERE
3742
- `pod_id` = %d
3743
- AND `field_id` = %d
3744
- AND `item_id` = %d
3745
- AND `related_item_id` = %d
3746
- ", array(
3747
- $pod[ 'id' ],
3748
- $field[ 'id' ],
3749
- $id,
3750
- $related_pod_id,
3751
- $related_field_id,
3752
- $related_id,
3753
- $related_weight,
3754
-
3755
- $pod[ 'id' ],
3756
- $field[ 'id' ],
3757
- $id,
3758
- $related_id,
3759
- ) );
3760
- }
3761
- else {
3762
- pods_query( "
3763
- INSERT INTO `@wp_podsrel`
3764
- (
3765
- `pod_id`,
3766
- `field_id`,
3767
- `item_id`,
3768
- `related_pod_id`,
3769
- `related_field_id`,
3770
- `related_item_id`,
3771
- `weight`
3772
- )
3773
- VALUES ( %d, %d, %d, %d, %d, %d, %d )
3774
- ", array(
3775
- $pod[ 'id' ],
3776
- $field[ 'id' ],
3777
- $id,
3778
- $related_pod_id,
3779
- $related_field_id,
3780
- $related_id,
3781
- $related_weight
3782
- ) );
3783
- }
3784
-
3785
- $related_weight++;
3786
- }
3787
- }
3788
- }
3789
-
3790
- /**
3791
- * Duplicate a Pod
3792
- *
3793
- * $params['id'] int The Pod ID
3794
- * $params['name'] string The Pod name
3795
- * $params['new_name'] string The new Pod name
3796
- *
3797
- * @param array $params An associative array of parameters
3798
- * @param bool $strict (optional) Makes sure a pod exists, if it doesn't throws an error
3799
- *
3800
- * @return int New Pod ID
3801
- * @since 2.3
3802
- */
3803
- public function duplicate_pod ( $params, $strict = false ) {
3804
- if ( !is_object( $params ) && !is_array( $params ) ) {
3805
- if ( is_numeric( $params ) )
3806
- $params = array( 'id' => $params );
3807
- else
3808
- $params = array( 'name' => $params );
3809
-
3810
- $params = (object) pods_sanitize( $params );
3811
- }
3812
- else
3813
- $params = (object) pods_sanitize( $params );
3814
-
3815
- $params->table_info = false;
3816
-
3817
- $pod = $this->load_pod( $params, $strict );
3818
-
3819
- if ( empty( $pod ) ) {
3820
- if ( false !== $strict )
3821
- return pods_error( __( 'Pod not found', 'pods' ), $this );
3822
-
3823
- return false;
3824
- }
3825
- elseif ( in_array( $pod[ 'type' ], array( 'media', 'user', 'comment' ) ) ) {
3826
- if ( false !== $strict )
3827
- return pods_error( __( 'Pod not allowed to be duplicated', 'pods' ), $this );
3828
-
3829
- return false;
3830
- }
3831
- elseif ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && 0 < strlen( $pod[ 'object' ] ) ) {
3832
- $pod[ 'object' ] = '';
3833
- }
3834
-
3835
- unset( $pod[ 'id' ] );
3836
-
3837
- if ( isset( $params->new_name ) )
3838
- $pod[ 'name' ] = $params->new_name;
3839
-
3840
- $try = 1;
3841
-
3842
- $check_name = $pod[ 'name' ];
3843
- $new_label = $pod[ 'label' ];
3844
-
3845
- while ( $this->load_pod( array( 'name' => $check_name, 'table_info' => false ), false ) ) {
3846
- $try++;
3847
-
3848
- $check_name = $pod[ 'name' ] . $try;
3849
- $new_label = $pod[ 'label' ] . $try;
3850
- }
3851
-
3852
- $pod[ 'name' ] = $check_name;
3853
- $pod[ 'label' ] = $new_label;
3854
-
3855
- foreach ( $pod[ 'fields' ] as $field => $field_data ) {
3856
- unset( $pod[ 'fields' ][ $field ][ 'id' ] );
3857
- }
3858
-
3859
- return $this->save_pod( $pod );
3860
- }
3861
-
3862
- /**
3863
- * Duplicate a Field
3864
- *
3865
- * $params['pod_id'] int The Pod ID
3866
- * $params['pod'] string The Pod name
3867
- * $params['id'] int The Field ID
3868
- * $params['name'] string The Field name
3869
- * $params['new_name'] string The new Field name
3870
- *
3871
- * @param array $params An associative array of parameters
3872
- * @param bool $strict (optional) Makes sure a field exists, if it doesn't throws an error
3873
- *
3874
- * @return int New Field ID
3875
- * @since 2.3.10
3876
- */
3877
- public function duplicate_field( $params, $strict = false ) {
3878
 
3879
- if ( !is_object( $params ) && !is_array( $params ) ) {
3880
- if ( is_numeric( $params ) ) {
3881
- $params = array( 'id' => $params );
3882
  }
3883
- else {
3884
- $params = array( 'name' => $params );
 
 
 
 
 
3885
  }
3886
- }
3887
 
3888
- $params = (object) pods_sanitize( $params );
3889
 
3890
- $params->table_info = false;
 
 
3891
 
3892
- $field = $this->load_field( $params, $strict );
 
3893
 
3894
- if ( empty( $field ) ) {
3895
- if ( false !== $strict ) {
3896
- return pods_error( __( 'Field not found', 'pods' ), $this );
 
 
3897
  }
3898
 
3899
- return false;
3900
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3901
 
3902
- unset( $field[ 'id' ] );
 
 
3903
 
3904
- if ( isset( $params->new_name ) ) {
3905
- $field[ 'name' ] = $params->new_name;
3906
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3907
 
3908
- $try = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3909
 
3910
- $check_name = $field[ 'name' ];
3911
- $new_label = $field[ 'label' ];
 
 
 
3912
 
3913
- while ( $this->load_field( array( 'pod_id' => $field[ 'pod_id' ], 'name' => $check_name, 'table_info' => false ), false ) ) {
3914
- $try++;
3915
 
3916
- $check_name = $field[ 'name' ] . $try;
3917
- $new_label = $field[ 'label' ] . $try;
3918
- }
 
 
3919
 
3920
- $field[ 'name' ] = $check_name;
3921
- $field[ 'label' ] = $new_label;
 
3922
 
3923
- return $this->save_field( $field );
3924
 
3925
- }
3926
 
3927
- /**
3928
- * @see PodsAPI::save_pod_item
3929
- *
3930
- * Duplicate a pod item
3931
- *
3932
- * $params['pod'] string The Pod name
3933
- * $params['id'] int The item's ID from the wp_pods_* table
3934
- *
3935
- * @param array $params An associative array of parameters
3936
- *
3937
- * @return int The table row ID
3938
- * @since 1.12
3939
- */
3940
- public function duplicate_pod_item ( $params ) {
3941
- $params = (object) pods_sanitize( $params );
3942
-
3943
- $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => false ) );
3944
-
3945
- if ( false === $pod )
3946
- return pods_error( __( 'Pod not found', 'pods' ), $this );
3947
-
3948
- $pod = pods( $params->pod, $params->id );
3949
-
3950
- $params->pod = $pod->pod;
3951
- $params->pod_id = $pod->pod_id;
3952
-
3953
- $fields = (array) pods_var_raw( 'fields', $pod->pod_data, array(), null, true );
3954
- $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
3955
-
3956
- if ( !empty( $object_fields ) )
3957
- $fields = array_merge( $object_fields, $fields );
3958
-
3959
- $save_params = array(
3960
- 'pod' => $params->pod,
3961
- 'data' => array()
3962
- );
3963
-
3964
- foreach ( $fields as $field ) {
3965
- $value = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'ids' ) );
3966
-
3967
- if ( !empty( $value ) || ( !is_array( $value ) && 0 < strlen( $value ) ) )
3968
- $save_params[ 'data' ][ $field[ 'name' ] ] = $value;
3969
- }
3970
-
3971
- $save_params = $this->do_hook( 'duplicate_pod_item', $save_params, $pod->pod, $pod->id(), $params );
3972
-
3973
- $id = $this->save_pod_item( $save_params );
3974
-
3975
- return $id;
3976
- }
3977
-
3978
- /**
3979
- * @see pods()
3980
- *
3981
- * Export a pod item
3982
- *
3983
- * $params['pod'] string The Pod name
3984
- * $params['id'] int The item's ID from the wp_pods_* table
3985
- * $params['fields'] array The fields to export
3986
- * $params['depth'] int How many levels deep to export data
3987
- *
3988
- * @param array $params An associative array of parameters
3989
- * @param object $pod (optional) Pods object
3990
- *
3991
- * @return int The table row ID
3992
- * @since 1.12
3993
- */
3994
- public function export_pod_item ( $params, $pod = null ) {
3995
- if ( !is_object( $pod ) || 'Pods' != get_class( $pod ) ) {
3996
- if ( empty( $params ) )
3997
- return false;
3998
-
3999
- $params = (object) pods_sanitize( $params );
4000
-
4001
- $pod = pods( $params->pod, $params->id, false );
4002
-
4003
- if ( empty( $pod ) )
4004
- return false;
4005
- }
4006
-
4007
- $fields = (array) pods_var_raw( 'fields', $params, array(), null, true );
4008
- $depth = (int) pods_var_raw( 'depth', $params, 2, null, true );
4009
- $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4010
- $flatten = (boolean) pods_var( 'flatten', $params, false, null, true );
4011
-
4012
- if ( empty( $fields ) ) {
4013
- $fields = $pod->fields;
4014
- $fields = array_merge( $fields, $object_fields );
4015
- }
4016
-
4017
- $data = $this->export_pod_item_level( $pod, $fields, $depth, $flatten );
4018
-
4019
- $data = $this->do_hook( 'export_pod_item', $data, $pod->pod, $pod->id(), $pod, $fields, $depth, $flatten );
4020
-
4021
- return $data;
4022
- }
4023
-
4024
- /**
4025
- * Export a pod item by depth level
4026
- *
4027
- * @param Pods $pod Pods object
4028
- * @param array $fields Fields to export
4029
- * @param int $depth Depth limit
4030
- * @param boolean $flatten Whether to flatten arrays for display
4031
- * @param int $current_depth Current depth level
4032
- *
4033
- * @return array Data array
4034
- *
4035
- * @since 2.3
4036
- */
4037
- private function export_pod_item_level ( $pod, $fields, $depth, $flatten = false, $current_depth = 1 ) {
4038
- $tableless_field_types = PodsForm::tableless_field_types();
4039
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
4040
-
4041
- $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4042
-
4043
- $export_fields = array();
4044
-
4045
- foreach ( $fields as $k => $field ) {
4046
- if ( !is_array( $field ) ) {
4047
- $field = array(
4048
- 'id' => 0,
4049
- 'name' => $field
4050
- );
4051
- }
4052
-
4053
- if ( isset( $pod->fields[ $field[ 'name' ] ] ) ) {
4054
- $field = $pod->fields[ $field[ 'name' ] ];
4055
- $field[ 'lookup_name' ] = $field[ 'name' ];
4056
-
4057
- if ( in_array( $field[ 'type' ], $tableless_field_types ) && !in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) {
4058
- if ( 'pick' == $field[ 'type' ] ) {
4059
- if ( empty( $field[ 'table_info' ] ) )
4060
- $field[ 'table_info' ] = $this->get_table_info( pods_var_raw( 'pick_object', $field ), pods_var_raw( 'pick_val', $field ), null, null, $field );
4061
-
4062
- if ( !empty( $field[ 'table_info' ] ) )
4063
- $field[ 'lookup_name' ] .= '.' . $field[ 'table_info' ][ 'field_id' ];
4064
- }
4065
- elseif ( in_array( $field[ 'type' ], PodsForm::file_field_types() ) )
4066
- $field[ 'lookup_name' ] .= '.guid';
4067
- }
4068
-
4069
- $export_fields[ $field[ 'name' ] ] = $field;
4070
- }
4071
- elseif ( isset( $object_fields[ $field[ 'name' ] ] ) ) {
4072
- $field = $object_fields[ $field[ 'name' ] ];
4073
- $field[ 'lookup_name' ] = $field[ 'name' ];
4074
-
4075
- $export_fields[ $field[ 'name' ] ] = $field;
4076
- }
4077
- elseif ( $field[ 'name' ] == $pod->pod_data[ 'field_id' ] ) {
4078
- $field[ 'type' ] = 'number';
4079
- $field[ 'lookup_name' ] = $field[ 'name' ];
4080
-
4081
- $export_fields[ $field[ 'name' ] ] = $field;
4082
- }
4083
- }
4084
-
4085
- $data = array();
4086
-
4087
- foreach ( $export_fields as $field ) {
4088
- // Return IDs (or guid for files) if only one level deep
4089
- if ( 1 == $depth )
4090
- $data[ $field[ 'name' ] ] = $pod->field( array( 'name' => $field[ 'lookup_name' ], 'output' => 'arrays' ) );
4091
- // Recurse depth levels for pick fields if $depth allows
4092
- elseif ( ( -1 == $depth || $current_depth < $depth ) && 'pick' == $field[ 'type' ] && !in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) {
4093
- $related_data = array();
4094
-
4095
- $related_ids = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'ids' ) );
4096
-
4097
- if ( !empty( $related_ids ) ) {
4098
- $related_ids = (array) $related_ids;
4099
-
4100
- $pick_object = pods_var_raw( 'pick_object', $field );
4101
-
4102
- $related_pod = pods( pods_var_raw( 'pick_val', $field ), null, false );
4103
-
4104
- // If this isn't a Pod, return data exactly as Pods does normally
4105
- if ( empty( $related_pod ) || ( 'pod' != $pick_object && $pick_object != $related_pod->pod_data[ 'type' ] ) || $related_pod->pod == $pod->pod )
4106
- $related_data = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'arrays' ) );
4107
- else {
4108
- $related_object_fields = (array) pods_var_raw( 'object_fields', $related_pod->pod_data, array(), null, true );
4109
-
4110
- $related_fields = array_merge( $related_pod->fields, $related_object_fields );
4111
-
4112
- foreach ( $related_ids as $related_id ) {
4113
- if ( $related_pod->fetch( $related_id ) ) {
4114
- $related_item = $this->export_pod_item_level( $related_pod, $related_fields, $depth, $flatten, ( $current_depth + 1 ) );
4115
-
4116
- $related_data[ $related_id ] = $this->do_hook( 'export_pod_item_level', $related_item, $related_pod->pod, $related_pod->id(), $related_pod, $related_fields, $depth, $flatten, ( $current_depth + 1 ) );
4117
- }
4118
- }
4119
-
4120
- if ( $flatten && !empty( $related_data ) )
4121
- $related_data = pods_serial_comma( array_values( $related_data ), array( 'and' => '', 'field_index' => $related_pod->pod_data[ 'field_index' ] ) );
4122
- }
4123
- }
4124
-
4125
- $data[ $field[ 'name' ] ] = $related_data;
4126
- }
4127
- // Return data exactly as Pods does normally
4128
- else
4129
- $data[ $field[ 'name' ] ] = $pod->field( array( 'name' => $field[ 'name' ], 'output' => 'arrays' ) );
4130
-
4131
- if ( $flatten && is_array( $data[ $field[ 'name' ] ] ) )
4132
- $data[ $field[ 'name' ] ] = pods_serial_comma( $data[ $field[ 'name' ] ], array( 'field' => $field[ 'name' ], 'fields' => $export_fields, 'and' => '' ) );
4133
- }
4134
-
4135
- return $data;
4136
- }
4137
-
4138
- /**
4139
- * Reorder a Pod
4140
- *
4141
- * $params['pod'] string The Pod name
4142
- * $params['field'] string The field name of the field to reorder
4143
- * $params['order'] array The key => value array of items to reorder (key should be an integer)
4144
- *
4145
- * @param array $params An associative array of parameters
4146
- *
4147
- * @return bool
4148
- *
4149
- * @since 1.9.0
4150
- */
4151
- public function reorder_pod_item ( $params ) {
4152
- $params = (object) pods_sanitize( $params );
4153
-
4154
- // @deprecated 2.0
4155
- if ( isset( $params->datatype ) ) {
4156
- pods_deprecated( __( '$params->pod instead of $params->datatype', 'pods' ), '2.0' );
4157
-
4158
- $params->pod = $params->datatype;
4159
-
4160
- unset( $params->datatype );
4161
- }
4162
-
4163
- if ( null === pods_var_raw( 'pod', $params, null, null, true ) )
4164
- return pods_error( __( '$params->pod is required', 'pods' ), $this );
4165
-
4166
- if ( !is_array( $params->order ) )
4167
- $params->order = explode( ',', $params->order );
4168
-
4169
- $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => true ) );
4170
-
4171
- $params->name = $pod[ 'name' ];
4172
-
4173
- if ( false === $pod )
4174
- return pods_error( __( 'Pod is required', 'pods' ), $this );
4175
-
4176
- foreach ( $params->order as $order => $id ) {
4177
- if ( isset( $pod[ 'fields' ][ $params->field ] ) || isset( $pod[ 'object_fields' ][ $params->field ] ) ) {
4178
- if ( 'table' == $pod[ 'storage' ] && ( !pods_tableless() ) ) {
4179
- if ( isset( $pod[ 'fields' ][ $params->field ] ) )
4180
- pods_query( "UPDATE `@wp_pods_{$params->name}` SET `{$params->field}` = " . pods_absint( $order ) . " WHERE `id` = " . pods_absint( $id ) . " LIMIT 1" );
4181
- else
4182
- pods_query( "UPDATE `{$pod['table']}` SET `{$params->field}` = " . pods_absint( $order ) . " WHERE `{$pod['field_id']}` = " . pods_absint( $id ) . " LIMIT 1" );
4183
- }
4184
- else
4185
- $this->save_pod_item( array( 'pod' => $params->pod, 'pod_id' => $params->pod_id, 'id' => $id, 'data' => array( $params->field => pods_absint( $order ) ) ) );
4186
- }
4187
- }
4188
-
4189
- return true;
4190
- }
4191
-
4192
- /**
4193
- *
4194
- * Delete all content for a Pod
4195
- *
4196
- * $params['id'] int The Pod ID
4197
- * $params['name'] string The Pod name
4198
- *
4199
- * @param array $params An associative array of parameters
4200
- * @param array $pod Pod data
4201
- *
4202
- * @return bool
4203
- *
4204
- * @uses pods_query
4205
- * @uses pods_cache_clear
4206
- *
4207
- * @since 1.9.0
4208
- */
4209
- public function reset_pod ( $params, $pod = false ) {
4210
- $params = (object) pods_sanitize( $params );
4211
-
4212
- $params->table_info = true;
4213
-
4214
- if ( empty( $pod ) )
4215
- $pod = $this->load_pod( $params );
4216
-
4217
- if ( false === $pod )
4218
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4219
-
4220
- $params->id = $pod[ 'id' ];
4221
- $params->name = $pod[ 'name' ];
4222
-
4223
- if ( !pods_tableless() ) {
4224
- if ( 'table' == $pod[ 'storage' ] ) {
4225
- try {
4226
- pods_query( "TRUNCATE `@wp_pods_{$params->name}`", false );
4227
- }
4228
- catch ( Exception $e ) {
4229
- // Allow pod to be reset if the table doesn't exist
4230
- if ( false === strpos( $e->getMessage(), 'Unknown table' ) )
4231
- return pods_error( $e->getMessage(), $this );
4232
- }
4233
- }
4234
-
4235
- pods_query( "DELETE FROM `@wp_podsrel` WHERE `pod_id` = {$params->id} OR `related_pod_id` = {$params->id}", false );
4236
- }
4237
-
4238
- // @todo Delete relationships from tableless relationships
4239
-
4240
- // Delete all posts/revisions from this post type
4241
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'media' ) ) ) {
4242
- $type = pods_var( 'object', $pod, $pod[ 'name' ], null, true );
4243
-
4244
- $sql = "
4245
- DELETE `t`, `r`, `m`
4246
- FROM `{$pod['table']}` AS `t`
4247
- LEFT JOIN `{$pod['meta_table']}` AS `m`
4248
- ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4249
- LEFT JOIN `{$pod['table']}` AS `r`
4250
- ON `r`.`post_parent` = `t`.`{$pod['field_id']}` AND `r`.`post_status` = 'inherit'
4251
- WHERE `t`.`{$pod['field_type']}` = '{$type}'
4252
- ";
4253
-
4254
- pods_query( $sql, false );
4255
- }
4256
- // Delete all terms from this taxonomy
4257
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
4258
- $type = pods_var( 'object', $pod, $pod[ 'name' ], null, true );
4259
-
4260
- $sql = "
4261
- DELETE FROM `{$pod['table']}` AS `t`
4262
- " . $pod['join']['tt'] . "
4263
- WHERE " . implode( ' AND ', $pod['where'] ) . "
4264
- ";
4265
-
4266
- pods_query( $sql, false );
4267
- }
4268
- // Delete all users except the current one
4269
- elseif ( 'user' == $pod[ 'type' ] ) {
4270
- $sql = "
4271
- DELETE `t`, `m`
4272
- FROM `{$pod['table']}` AS `t`
4273
- LEFT JOIN `{$pod['meta_table']}` AS `m`
4274
- ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4275
- WHERE `t`.`{$pod['field_id']}` != " . (int) get_current_user_id() . "
4276
- ";
4277
-
4278
- pods_query( $sql, false );
4279
- }
4280
- // Delete all comments
4281
- elseif ( 'comment' == $pod[ 'type' ] ) {
4282
- $type = pods_var( 'object', $pod, $pod[ 'name' ], null, true );
4283
-
4284
- $sql = "
4285
- DELETE `t`, `m`
4286
- FROM `{$pod['table']}` AS `t`
4287
- LEFT JOIN `{$pod['meta_table']}` AS `m`
4288
- ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
4289
- WHERE `t`.`{$pod['field_type']}` = '{$type}'
4290
- ";
4291
-
4292
- pods_query( $sql, false );
4293
- }
4294
-
4295
- pods_cache_clear( true ); // only way to reliably clear out cached data across an entire group
4296
-
4297
- return true;
4298
- }
4299
-
4300
- /**
4301
- * Delete a Pod and all its content
4302
- *
4303
- * $params['id'] int The Pod ID
4304
- * $params['name'] string The Pod name
4305
- *
4306
- * @param array $params An associative array of parameters
4307
- * @param bool $strict (optional) Makes sure a pod exists, if it doesn't throws an error
4308
- * @param bool $delete_all (optional) Whether to delete all content from a WP object
4309
- *
4310
- * @uses PodsAPI::load_pod
4311
- * @uses wp_delete_post
4312
- * @uses pods_query
4313
- *
4314
- * @return bool
4315
- * @since 1.7.9
4316
- */
4317
- public function delete_pod ( $params, $strict = false, $delete_all = false ) {
4318
- /**
4319
- * @var $wpdb wpdb
4320
- */
4321
- global $wpdb;
4322
-
4323
- if ( !is_object( $params ) && !is_array( $params ) ) {
4324
- if ( is_numeric( $params ) )
4325
- $params = array( 'id' => $params );
4326
- else
4327
- $params = array( 'name' => $params );
4328
-
4329
- $params = (object) pods_sanitize( $params );
4330
- }
4331
- else
4332
- $params = (object) pods_sanitize( $params );
4333
-
4334
- $params->table_info = false;
4335
-
4336
- $pod = $this->load_pod( $params, $strict );
4337
-
4338
- if ( empty( $pod ) ) {
4339
- if ( false !== $strict )
4340
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4341
-
4342
- return false;
4343
- }
4344
-
4345
- $params->id = (int) $pod[ 'id' ];
4346
- $params->name = $pod[ 'name' ];
4347
-
4348
- foreach ( $pod[ 'fields' ] as $field ) {
4349
- $field[ 'pod' ] = $pod;
4350
-
4351
- $this->delete_field( $field, false );
4352
- }
4353
-
4354
- // Only delete the post once the fields are taken care of, it's not required anymore
4355
- $success = wp_delete_post( $params->id );
4356
-
4357
- if ( !$success )
4358
- return pods_error( __( 'Pod unable to be deleted', 'pods' ), $this );
4359
-
4360
- // Reset content
4361
- if ( $delete_all )
4362
- $this->reset_pod( $params, $pod );
4363
-
4364
- if ( !pods_tableless() ) {
4365
- if ( 'table' == $pod[ 'storage' ] ) {
4366
- try {
4367
- pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`", false );
4368
- }
4369
- catch ( Exception $e ) {
4370
- // Allow pod to be deleted if the table doesn't exist
4371
- if ( false === strpos( $e->getMessage(), 'Unknown table' ) )
4372
- return pods_error( $e->getMessage(), $this );
4373
- }
4374
- }
4375
-
4376
- pods_query( "DELETE FROM `@wp_podsrel` WHERE `pod_id` = {$params->id} OR `related_pod_id` = {$params->id}", false );
4377
- }
4378
-
4379
- // @todo Delete relationships from tableless relationships
4380
-
4381
- // Delete any relationship references
4382
- $sql = "
4383
- DELETE `pm`
4384
- FROM `{$wpdb->postmeta}` AS `pm`
4385
- LEFT JOIN `{$wpdb->posts}` AS `p`
4386
- ON `p`.`post_type` = '_pods_field'
4387
- AND `p`.`ID` = `pm`.`post_id`
4388
- LEFT JOIN `{$wpdb->postmeta}` AS `pm2`
4389
- ON `pm2`.`meta_key` = 'pick_object'
4390
- AND `pm2`.`meta_value` = 'pod'
4391
- AND `pm2`.`post_id` = `pm`.`post_id`
4392
- WHERE
4393
- `p`.`ID` IS NOT NULL
4394
- AND `pm2`.`meta_id` IS NOT NULL
4395
- AND `pm`.`meta_key` = 'pick_val'
4396
- AND `pm`.`meta_value` = '{$params->name}'
4397
- ";
4398
-
4399
- pods_query( $sql );
4400
-
4401
- $this->cache_flush_pods( $pod );
4402
-
4403
- return true;
4404
- }
4405
-
4406
- /**
4407
- * Drop a field within a Pod
4408
- *
4409
- * $params['id'] int The field ID
4410
- * $params['name'] int The field name
4411
- * $params['pod'] string The Pod name
4412
- * $params['pod_id'] string The Pod name
4413
- *
4414
- * @param array $params An associative array of parameters
4415
- * @param bool $table_operation Whether or not to handle table operations
4416
- *
4417
- * @uses PodsAPI::load_field
4418
- * @uses wp_delete_post
4419
- * @uses pods_query
4420
- *
4421
- * @return bool
4422
- * @since 1.7.9
4423
- */
4424
- public function delete_field ( $params, $table_operation = true ) {
4425
- /**
4426
- * @var $wpdb wpdb
4427
- */
4428
- global $wpdb;
4429
-
4430
- $tableless_field_types = PodsForm::tableless_field_types();
4431
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
4432
-
4433
- $params = (object) pods_sanitize( $params );
4434
-
4435
- if ( !isset( $params->pod ) )
4436
- $params->pod = '';
4437
-
4438
- if ( !isset( $params->pod_id ) )
4439
- $params->pod_id = 0;
4440
-
4441
- $pod = $params->pod;
4442
-
4443
- $save_pod = false;
4444
-
4445
- if ( !is_array( $pod ) )
4446
- $pod = $this->load_pod( array( 'name' => $pod, 'id' => $params->pod_id, 'table_info' => false ) );
4447
- else
4448
- $save_pod = true;
4449
-
4450
- if ( empty( $pod ) )
4451
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4452
-
4453
- $params->pod_id = $pod[ 'id' ];
4454
- $params->pod = $pod[ 'name' ];
4455
-
4456
- if ( !isset( $params->name ) )
4457
- $params->name = '';
4458
-
4459
- if ( !isset( $params->id ) )
4460
- $params->id = 0;
4461
-
4462
- $field = $this->load_field( array( 'name' => $params->name, 'id' => $params->id ) );
4463
-
4464
- if ( false === $field )
4465
- return pods_error( __( 'Field not found', 'pods' ), $this );
4466
-
4467
- $params->id = $field[ 'id' ];
4468
- $params->name = $field[ 'name' ];
4469
-
4470
- $simple = ( 'pick' == $field[ 'type' ] && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) );
4471
- $simple = (boolean) $this->do_hook( 'tableless_custom', $simple, $field, $pod, $params );
4472
-
4473
- if ( $table_operation && 'table' == $pod[ 'storage' ] && ( !in_array( $field[ 'type' ], $tableless_field_types ) || $simple ) )
4474
- pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$params->name}`", false );
4475
-
4476
- $success = wp_delete_post( $params->id );
4477
-
4478
- if ( !$success )
4479
- return pods_error( __( 'Field unable to be deleted', 'pods' ), $this );
4480
-
4481
- $wpdb->query( $wpdb->prepare( "DELETE pm FROM {$wpdb->postmeta} AS pm
4482
- LEFT JOIN {$wpdb->posts} AS p
4483
- ON p.post_type = '_pods_field' AND p.ID = pm.post_id
4484
- WHERE p.ID IS NOT NULL AND pm.meta_key = 'sister_id' AND pm.meta_value = %d", $params->id ) );
4485
-
4486
- if ( ( !pods_tableless() ) && $table_operation ) {
4487
- pods_query( "DELETE FROM `@wp_podsrel` WHERE (`pod_id` = {$params->pod_id} AND `field_id` = {$params->id}) OR (`related_pod_id` = {$params->pod_id} AND `related_field_id` = {$params->id})", false );
4488
- }
4489
-
4490
- // @todo Delete tableless relationship meta
4491
-
4492
- if ( true === $save_pod )
4493
- $this->cache_flush_pods( $pod );
4494
-
4495
- return true;
4496
- }
4497
-
4498
- /**
4499
- * Drop a Pod Object
4500
- *
4501
- * $params['id'] int The object ID
4502
- * $params['name'] string The object name
4503
- * $params['type'] string The object type
4504
- *
4505
- * @param array|object $params An associative array of parameters
4506
- *
4507
- * @uses wp_delete_post
4508
- *
4509
- * @return bool
4510
- * @since 2.0
4511
- */
4512
- public function delete_object ( $params ) {
4513
- $params = (object) $params;
4514
- $object = $this->load_object( $params );
4515
-
4516
- if ( empty( $object ) )
4517
- return pods_error( sprintf( __( "%s Object not found", 'pods' ), ucwords( $params->type ) ), $this );
4518
-
4519
- $success = wp_delete_post( $params->id );
4520
-
4521
- if ( !$success )
4522
- return pods_error( sprintf( __( "%s Object not deleted", 'pods' ), ucwords( $params->type ) ), $this );
4523
-
4524
- pods_transient_clear( 'pods_objects_' . $params->type );
4525
-
4526
- return true;
4527
- }
4528
-
4529
- /**
4530
- * @see PodsAPI::delete_object
4531
- *
4532
- * Drop a Pod Template
4533
- *
4534
- * $params['id'] int The template ID
4535
- * $params['name'] string The template name
4536
- *
4537
- * @param array $params An associative array of parameters
4538
- *
4539
- * @return bool
4540
- * @since 1.7.9
4541
- */
4542
- public function delete_template ( $params ) {
4543
- $params = (object) $params;
4544
- $params->type = 'template';
4545
- return $this->delete_object( $params );
4546
- }
4547
-
4548
- /**
4549
- * @see PodsAPI::delete_object
4550
- *
4551
- * Drop a Pod Page
4552
- *
4553
- * $params['id'] int The page ID
4554
- * $params['uri'] string The page URI
4555
- *
4556
- * @param array $params An associative array of parameters
4557
- *
4558
- * @return bool
4559
- * @since 1.7.9
4560
- */
4561
- public function delete_page ( $params ) {
4562
- $params = (object) $params;
4563
- if ( isset( $params->uri ) ) {
4564
- $params->name = $params->uri;
4565
- unset( $params->uri );
4566
- }
4567
- if ( isset( $params->name ) )
4568
- $params->name = trim( $params->name, '/' );
4569
- $params->type = 'page';
4570
- return $this->delete_object( $params );
4571
- }
4572
-
4573
- /**
4574
- * @see PodsAPI::delete_object
4575
- *
4576
- * Drop a Pod Helper
4577
- *
4578
- * $params['id'] int The helper ID
4579
- * $params['name'] string The helper name
4580
- *
4581
- * @param array $params An associative array of parameters
4582
- *
4583
- * @return bool
4584
- * @since 1.7.9
4585
- */
4586
- public function delete_helper ( $params ) {
4587
- $params = (object) $params;
4588
- $params->type = 'helper';
4589
- return $this->delete_object( $params );
4590
- }
4591
-
4592
- /**
4593
- * Drop a single pod item
4594
- *
4595
- * $params['id'] int (optional) The item's ID from the wp_pod_* table (used with datatype parameter)
4596
- * $params['pod'] string (optional) The Pod name (used with id parameter)
4597
- * $params['pod_id'] int (optional) The Pod ID (used with id parameter)
4598
- * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
4599
- *
4600
- * @param array $params An associative array of parameters
4601
- * @param bool $wp Whether to run WP object delete action
4602
- *
4603
- * @return bool
4604
- * @since 1.7.9
4605
- */
4606
- public function delete_pod_item ( $params, $wp = true ) {
4607
- $params = (object) pods_sanitize( $params );
4608
-
4609
- // @deprecated 2.0
4610
- if ( isset( $params->datatype_id ) || isset( $params->datatype ) || isset( $params->tbl_row_id ) ) {
4611
- if ( isset( $params->tbl_row_id ) ) {
4612
- pods_deprecated( __( '$params->id instead of $params->tbl_row_id', 'pods' ), '2.0' );
4613
- $params->id = $params->tbl_row_id;
4614
- unset( $params->tbl_row_id );
4615
- }
4616
-
4617
- if ( isset( $params->pod_id ) ) {
4618
- pods_deprecated( __( '$params->id instead of $params->pod_id', 'pods' ), '2.0' );
4619
- $params->id = $params->pod_id;
4620
- unset( $params->pod_id );
4621
- }
4622
-
4623
- if ( isset( $params->dataype_id ) ) {
4624
- pods_deprecated( __( '$params->pod_id instead of $params->datatype_id', 'pods' ), '2.0' );
4625
- $params->pod_id = $params->dataype_id;
4626
- unset( $params->dataype_id );
4627
- }
4628
-
4629
- if ( isset( $params->datatype ) ) {
4630
- pods_deprecated( __( '$params->pod instead of $params->datatype', 'pods' ), '2.0' );
4631
- $params->pod = $params->datatype;
4632
- unset( $params->datatype );
4633
- }
4634
- }
4635
-
4636
- if ( !isset( $params->id ) )
4637
- return pods_error( __( 'Pod Item not found', 'pods' ), $this );
4638
-
4639
- $params->id = pods_absint( $params->id );
4640
-
4641
- if ( !isset( $params->pod ) )
4642
- $params->pod = '';
4643
-
4644
- if ( !isset( $params->pod_id ) )
4645
- $params->pod_id = 0;
4646
-
4647
- $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => false ) );
4648
-
4649
- if ( false === $pod )
4650
- return pods_error( __( 'Pod not found', 'pods' ), $this );
4651
-
4652
- $params->pod_id = $pod[ 'id' ];
4653
- $params->pod = $pod[ 'name' ];
4654
-
4655
- // Allow Helpers to bypass subsequent helpers in recursive delete_pod_item calls
4656
- $bypass_helpers = false;
4657
-
4658
- if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers )
4659
- $bypass_helpers = true;
4660
-
4661
- $pre_delete_helpers = $post_delete_helpers = array();
4662
-
4663
- if ( false === $bypass_helpers ) {
4664
- // Plugin hook
4665
- $this->do_hook( 'pre_delete_pod_item', $params, $pod );
4666
- $this->do_hook( "pre_delete_pod_item_{$params->pod}", $params, $pod );
4667
-
4668
- // Call any pre-save helpers (if not bypassed)
4669
- if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
4670
- if ( !empty( $pod[ 'options' ] ) && is_array( $pod[ 'options' ] ) ) {
4671
- $helpers = array( 'pre_delete_helpers', 'post_delete_helpers' );
4672
-
4673
- foreach ( $helpers as $helper ) {
4674
- if ( isset( $pod[ 'options' ][ $helper ] ) && !empty( $pod[ 'options' ][ $helper ] ) )
4675
- ${$helper} = explode( ',', $pod[ 'options' ][ $helper ] );
4676
- }
4677
- }
4678
-
4679
- if ( !empty( $pre_delete_helpers ) ) {
4680
- pods_deprecated( sprintf( __( 'Pre-delete helpers are deprecated, use the action pods_pre_delete_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
4681
-
4682
- foreach ( $pre_delete_helpers as $helper ) {
4683
- $helper = $this->load_helper( array( 'name' => $helper ) );
4684
-
4685
- if ( false !== $helper )
4686
- eval( '?>' . $helper[ 'code' ] );
4687
- }
4688
- }
4689
- }
4690
- }
4691
-
4692
- // Delete object from relationship fields
4693
- $this->delete_object_from_relationships( $params->id, $pod );
4694
-
4695
- if ( 'table' == $pod[ 'storage' ] )
4696
- pods_query( "DELETE FROM `@wp_pods_{$params->pod}` WHERE `id` = {$params->id} LIMIT 1" );
4697
-
4698
- if ( $wp && 'taxonomy' == $pod[ 'type' ] ) {
4699
- $taxonomy = $pod[ 'name' ];
4700
-
4701
- if ( !empty( $pod[ 'object' ] ) )
4702
- $taxonomy = $pod[ 'object' ];
4703
-
4704
- wp_delete_term( $params->id, $taxonomy );
4705
- }
4706
- elseif ( $wp && !in_array( $pod[ 'type' ], array( 'pod', 'table', '', 'taxonomy' ) ) )
4707
- $this->delete_wp_object( $pod[ 'type' ], $params->id );
4708
-
4709
- if ( false === $bypass_helpers ) {
4710
- // Plugin hook
4711
- $this->do_hook( 'post_delete_pod_item', $params, $pod );
4712
- $this->do_hook( "post_delete_pod_item_{$params->pod}", $params, $pod );
4713
-
4714
- // Call any post-save helpers (if not bypassed)
4715
- if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) {
4716
- if ( !empty( $post_delete_helpers ) ) {
4717
- pods_deprecated( sprintf( __( 'Post-delete helpers are deprecated, use the action pods_post_delete_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
4718
-
4719
- foreach ( $post_delete_helpers as $helper ) {
4720
- $helper = $this->load_helper( array( 'name' => $helper ) );
4721
-
4722
- if ( false !== $helper )
4723
- eval( '?>' . $helper[ 'code' ] );
4724
- }
4725
- }
4726
- }
4727
- }
4728
-
4729
- pods_cache_clear( $params->id, 'pods_items_' . $params->pod );
4730
-
4731
- return true;
4732
- }
4733
-
4734
- /**
4735
- * Delete an object from tableless fields
4736
- *
4737
- * @param int $id
4738
- * @param string $type
4739
- * @param string $name
4740
- *
4741
- * @return bool
4742
- *
4743
- * @since 2.3
4744
- */
4745
- public function delete_object_from_relationships ( $id, $object, $name = null ) {
4746
- /**
4747
- * @var $pods_init \PodsInit
4748
- */
4749
- global $pods_init;
4750
-
4751
- $pod = false;
4752
-
4753
- // Run any bidirectional delete operations
4754
- if ( is_array( $object ) )
4755
- $pod = $object;
4756
- elseif ( is_object( $pods_init ) )
4757
- $pod = PodsInit::$meta->get_object( $object, $name );
4758
-
4759
- if ( !empty( $pod ) ) {
4760
- $object = $pod[ 'type' ];
4761
- $name = $pod[ 'name' ];
4762
-
4763
- foreach ( $pod[ 'fields' ] as $field ) {
4764
- PodsForm::delete( $field[ 'type' ], $id, $field[ 'name' ], array_merge( $field, $field[ 'options' ] ), $pod );
4765
- }
4766
- }
4767
-
4768
- // Lookup related fields (non-bidirectional)
4769
- $params = array(
4770
- 'where' => array(
4771
- array(
4772
- 'key' => 'type',
4773
- 'value' => 'pick'
4774
- ),
4775
- array(
4776
- 'key' => 'pick_object',
4777
- 'value' => $object
4778
- )
4779
- )
4780
- );
4781
-
4782
- if ( !empty( $name ) && $name != $object ) {
4783
- $params[ 'where' ][] = array(
4784
- 'key' => 'pick_val',
4785
- 'value' => $name
4786
- );
4787
- }
4788
-
4789
- $fields = $this->load_fields( $params, false );
4790
-
4791
- if ( !empty( $pod ) && 'media' == $pod[ 'type' ] ) {
4792
- $params[ 'where' ] = array(
4793
- array(
4794
- 'key' => 'type',
4795
- 'value' => 'file'
4796
- )
4797
- );
4798
-
4799
- $fields = array_merge( $fields, $this->load_fields( $params, false ) );
4800
- }
4801
-
4802
- if ( is_array( $fields ) && !empty( $fields ) ) {
4803
- foreach ( $fields as $related_field ) {
4804
- $related_pod = $this->load_pod( array( 'id' => $related_field[ 'pod_id' ], 'fields' => false ), false );
4805
-
4806
- if ( empty( $related_pod ) )
4807
- continue;
4808
-
4809
- $related_from = $this->lookup_related_items_from( $related_field[ 'id' ], $related_pod[ 'id' ], $id, $related_field, $related_pod );
4810
-
4811
- $this->delete_relationships( $related_from, $id, $related_pod, $related_field );
4812
- }
4813
- }
4814
-
4815
- if ( !empty( $pod ) && !pods_tableless() ) {
4816
- pods_query( "
4817
- DELETE FROM `@wp_podsrel`
4818
- WHERE
4819
- (
4820
- `pod_id` = %d
4821
- AND `item_id` = %d
4822
- )
4823
- OR (
4824
- `related_pod_id` = %d
4825
- AND `related_item_id` = %d
4826
- )
4827
- ", array(
4828
- $pod[ 'id' ],
4829
- $id,
4830
-
4831
- $pod[ 'id' ],
4832
- $id
4833
- ) );
4834
- }
4835
-
4836
- return true;
4837
- }
4838
-
4839
- /**
4840
- * Delete relationships
4841
- *
4842
- * @param int|array $related_id IDs for items to save
4843
- * @param int|array $id ID or IDs to remove
4844
- * @param array $related_pod Pod data
4845
- * @param array $related_field Field data
4846
- *
4847
- * @return void
4848
- *
4849
- * @since 2.3
4850
- */
4851
- public function delete_relationships ( $related_id, $id, $related_pod, $related_field ) {
4852
- if ( is_array( $related_id ) ) {
4853
- foreach ( $related_id as $rid ) {
4854
- $this->delete_relationships( $rid, $id, $related_pod, $related_field );
4855
- }
4856
-
4857
- return;
4858
- }
4859
-
4860
- if ( is_array( $id ) ) {
4861
- foreach ( $id as $rid ) {
4862
- $this->delete_relationships( $related_id, $rid, $related_pod, $related_field );
4863
- }
4864
-
4865
- return;
4866
- }
4867
-
4868
- $id = (int) $id;
4869
-
4870
- if ( empty( $id ) )
4871
- return;
4872
-
4873
- $related_ids = $this->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
4874
-
4875
- if ( empty( $related_ids ) )
4876
- return;
4877
- elseif ( !in_array( $id, $related_ids ) )
4878
- return;
4879
-
4880
- unset( $related_ids[ array_search( $id, $related_ids ) ] );
4881
-
4882
- $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
4883
-
4884
- if ( !$no_conflict )
4885
- pods_no_conflict_on( $related_pod[ 'type' ] );
4886
-
4887
- // Post Types, Media, Users, and Comments (meta-based)
4888
- if ( in_array( $related_pod[ 'type' ], array( 'post_type', 'media', 'user', 'comment' ) ) ) {
4889
- $object_type = $related_pod[ 'type' ];
4890
-
4891
- if ( 'post_type' == $object_type || 'media' == $object_type )
4892
- $object_type = 'post';
4893
-
4894
- delete_metadata( $object_type, $related_id, $related_field[ 'name' ] );
4895
-
4896
- if ( !empty( $related_ids ) ) {
4897
- update_metadata( $object_type, $related_id, '_pods_' . $related_field[ 'name' ], $related_ids );
4898
-
4899
- foreach ( $related_ids as $rel_id ) {
4900
- add_metadata( $object_type, $related_id, $related_field[ 'name' ], $rel_id );
4901
- }
4902
- }
4903
- else
4904
- delete_metadata( $object_type, $related_id, '_pods_' . $related_field[ 'name' ] );
4905
- }
4906
- // Custom Settings Pages (options-based)
4907
- elseif ( 'settings' == $related_pod[ 'type' ] ) {
4908
- if ( !empty( $related_ids ) )
4909
- update_option( $related_pod[ 'name' ] . '_' . $related_field[ 'name' ], $related_ids );
4910
- else
4911
- delete_option( $related_pod[ 'name' ] . '_' . $related_field[ 'name' ] );
4912
- }
4913
-
4914
- // Relationships table
4915
- if ( !pods_tableless() ) {
4916
- pods_query( "
4917
- DELETE FROM `@wp_podsrel`
4918
- WHERE
4919
- (
4920
- `pod_id` = %d
4921
- AND `field_id` = %d
4922
- AND `item_id` = %d
4923
- AND `related_item_id` = %d
4924
- )
4925
- OR (
4926
- `related_pod_id` = %d
4927
- AND `related_field_id` = %d
4928
- AND `related_item_id` = %d
4929
- AND `item_id` = %d
4930
- )
4931
- ", array(
4932
- $related_pod[ 'id' ],
4933
- $related_field[ 'id' ],
4934
- $related_id,
4935
- $id,
4936
-
4937
- $related_pod[ 'id' ],
4938
- $related_field[ 'id' ],
4939
- $related_id,
4940
- $id
4941
- ) );
4942
- }
4943
-
4944
- if ( !$no_conflict )
4945
- pods_no_conflict_off( $related_pod[ 'type' ] );
4946
- }
4947
-
4948
- /**
4949
- * Check if a Pod exists
4950
- *
4951
- * $params['id'] int Pod ID
4952
- * $params['name'] string Pod name
4953
- *
4954
- * @param array $params An associative array of parameters
4955
- *
4956
- * @return bool True if exists
4957
- *
4958
- * @since 1.12
4959
- */
4960
- public function pod_exists ( $params, $type = null ) {
4961
- if ( is_string( $params ) )
4962
- $params = array( 'name' => $params );
4963
-
4964
- $params = (object) pods_sanitize( $params );
4965
-
4966
- if ( !empty( $params->id ) || !empty( $params->name ) ) {
4967
- if ( !isset( $params->name ) )
4968
- $pod = get_post( $dummy = (int) $params->id );
4969
- else {
4970
- $pod = get_posts( array(
4971
- 'name' => $params->name,
4972
- 'post_type' => '_pods_pod',
4973
- 'posts_per_page' => 1
4974
- ) );
4975
- }
4976
-
4977
- if ( !empty( $pod ) && ( empty( $type ) || $type == get_post_meta( $pod->ID, 'type', true ) ) )
4978
- return true;
4979
- }
4980
-
4981
- return false;
4982
- }
4983
-
4984
- /**
4985
- * Load a Pod and all of its fields
4986
- *
4987
- * $params['id'] int The Pod ID
4988
- * $params['name'] string The Pod name
4989
- * $params['fields'] bool Whether to load fields (default is true)
4990
- *
4991
- * @param array|object $params An associative array of parameters or pod name as a string
4992
- * @param bool $strict Makes sure the pod exists, throws an error if it doesn't work
4993
- *
4994
- * @return array|bool|mixed|void
4995
- * @since 1.7.9
4996
- */
4997
- public function load_pod ( $params, $strict = true ) {
4998
-
4999
- /**
5000
- * @var $sitepress SitePress
5001
- * @var $wpdb wpdb
5002
- */
5003
- global $sitepress, $icl_adjust_id_url_filter_off, $wpdb;
5004
 
5005
- $current_language = false;
5006
- $load_fields = true;
 
5007
 
5008
- // WPML support
5009
- if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5010
- $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
5011
- // Polylang support
5012
- elseif ( function_exists( 'pll_current_language' ) )
5013
- $current_language = pll_current_language( 'slug' );
5014
 
5015
- if ( !is_array( $params ) && !is_object( $params ) )
5016
- $params = array( 'name' => $params, 'table_info' => false, 'fields' => true );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5017
 
5018
- if ( is_object( $params ) && isset( $params->fields ) && !$params->fields )
5019
- $load_fields = false;
5020
- elseif ( is_array( $params ) && isset( $params[ 'fields' ] ) && !$params[ 'fields' ] )
5021
- $load_fields = false;
 
5022
 
5023
- $transient = 'pods_' . $wpdb->prefix . '_pod';
 
 
5024
 
5025
- if ( !empty( $current_language ) )
5026
- $transient .= '_' . $current_language;
 
5027
 
5028
- if ( !$load_fields )
5029
- $transient .= '_nofields';
 
5030
 
5031
- if ( pods_var_raw( 'table_info', $params, false ) )
5032
- $transient .= '_tableinfo';
 
5033
 
5034
- if ( is_object( $params ) && isset( $params->post_name ) ) {
5035
- $pod = false;
 
5036
 
5037
- if ( pods_api_cache() )
5038
- $pod = pods_transient_get( $transient . '_' . $params->post_name );
 
 
 
5039
 
5040
- if ( false !== $pod && ( pods_var_raw( 'table_info', $params, false ) || isset( $pod[ 'table' ] ) ) ) {
5041
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5042
- $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
5043
 
5044
- return $pod;
5045
- }
5046
 
5047
- $_pod = get_object_vars( $params );
5048
- }
5049
- else {
5050
- $params = (object) pods_sanitize( $params );
5051
 
5052
- if ( ( !isset( $params->id ) || empty( $params->id ) ) && ( !isset( $params->name ) || empty( $params->name ) ) ) {
5053
- if ( $strict )
5054
- return pods_error( 'Either Pod ID or Name are required', $this );
5055
 
5056
- return false;
5057
- }
 
5058
 
5059
- if ( isset( $params->name ) ) {
5060
- $pod = false;
 
 
5061
 
5062
- if ( '_pods_pod' == $params->name ) {
5063
- $pod = array(
5064
- 'id' => 0,
5065
- 'name' => $params->name,
5066
- 'label' => __( 'Pods', 'pods' ),
5067
- 'type' => 'post_type',
5068
- 'storage' => 'meta',
5069
- 'options' => array(
5070
- 'label_singular' => __( 'Pod', 'pods' )
5071
- ),
5072
- 'fields' => array()
5073
- );
5074
- }
5075
- elseif ( '_pods_field' == $params->name ) {
5076
- $pod = array(
5077
- 'id' => 0,
5078
- 'name' => $params->name,
5079
- 'label' => __( 'Pod Fields', 'pods' ),
5080
- 'type' => 'post_type',
5081
- 'storage' => 'meta',
5082
- 'options' => array(
5083
- 'label_singular' => __( 'Pod Field', 'pods' )
5084
- ),
5085
- 'fields' => array()
5086
- );
5087
- }
5088
- elseif ( pods_api_cache() )
5089
- $pod = pods_transient_get( $transient . '_' . $params->name );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5090
 
5091
- if ( false !== $pod && ( pods_var_raw( 'table_info', $params, false ) || isset( $pod[ 'table' ] ) ) ) {
5092
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5093
- $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
 
5094
 
5095
- return $pod;
5096
- }
5097
- }
5098
 
5099
- if ( !isset( $params->name ) )
5100
- $pod = get_post( $dummy = (int) $params->id );
5101
- else {
5102
- $pod = get_posts( array(
5103
- 'name' => $params->name,
5104
- 'post_type' => '_pods_pod',
5105
- 'posts_per_page' => 1
5106
- ) );
5107
- }
5108
-
5109
- if ( empty( $pod ) ) {
5110
- if ( $strict )
5111
- return pods_error( __( 'Pod not found', 'pods' ), $this );
 
 
 
 
 
5112
 
5113
- return false;
5114
- }
5115
-
5116
- if ( is_array( $pod ) )
5117
- $pod = $pod[ 0 ];
5118
-
5119
- $_pod = get_object_vars( $pod );
5120
- }
5121
-
5122
- $pod = false;
5123
-
5124
- if ( pods_api_cache() )
5125
- $pod = pods_transient_get( $transient . '_' . $_pod[ 'post_name' ] );
5126
-
5127
- if ( false !== $pod && ( pods_var_raw( 'table_info', $params, false ) || isset( $pod[ 'table' ] ) ) ) {
5128
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) && is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5129
- $pod = array_merge( $pod, $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ) );
5130
-
5131
- return $pod;
5132
- }
5133
-
5134
- $pod = array(
5135
- 'id' => $_pod[ 'ID' ],
5136
- 'name' => $_pod[ 'post_name' ],
5137
- 'label' => $_pod[ 'post_title' ],
5138
- 'description' => $_pod[ 'post_content' ]
5139
- );
5140
-
5141
- if ( strlen( $pod[ 'label' ] ) < 1 )
5142
- $pod[ 'label' ] = $pod[ 'name' ];
5143
-
5144
- // @todo update with a method to put all options in
5145
- $defaults = array(
5146
- 'show_in_menu' => 1,
5147
- 'type' => 'post_type',
5148
- 'storage' => 'meta',
5149
- 'object' => '',
5150
- 'alias' => ''
5151
- );
5152
-
5153
- $pod[ 'options' ] = get_post_meta( $pod[ 'id' ] );
5154
-
5155
- foreach ( $pod[ 'options' ] as $option => $value ) {
5156
- if ( is_array( $value ) ) {
5157
- foreach ( $value as $k => $v ) {
5158
- if ( !is_array( $v ) )
5159
- $value[ $k ] = maybe_unserialize( $v );
5160
- }
5161
-
5162
- if ( 1 == count( $value ) )
5163
- $value = current( $value );
5164
- }
5165
- else
5166
- $value = maybe_unserialize( $value );
5167
-
5168
- $pod[ 'options' ][ $option ] = $value;
5169
- }
5170
-
5171
- $pod[ 'options' ] = array_merge( $defaults, $pod[ 'options' ] );
5172
-
5173
- $pod[ 'type' ] = $pod[ 'options' ][ 'type' ];
5174
- $pod[ 'storage' ] = $pod[ 'options' ][ 'storage' ];
5175
- $pod[ 'object' ] = $pod[ 'options' ][ 'object' ];
5176
- $pod[ 'alias' ] = $pod[ 'options' ][ 'alias' ];
5177
-
5178
- unset( $pod[ 'options' ][ 'type' ] );
5179
- unset( $pod[ 'options' ][ 'storage' ] );
5180
- unset( $pod[ 'options' ][ 'object' ] );
5181
- unset( $pod[ 'options' ][ 'alias' ] );
5182
-
5183
- if ( pods_var_raw( 'table_info', $params, false ) )
5184
- $pod = array_merge( $this->get_table_info( $pod[ 'type' ], $pod[ 'object' ], $pod[ 'name' ], $pod ), $pod );
5185
-
5186
- if ( isset( $pod[ 'pod' ] ) )
5187
- unset( $pod[ 'pod' ] );
5188
-
5189
- $pod[ 'fields' ] = array();
5190
-
5191
- $pod[ 'object_fields' ] = array();
5192
-
5193
- if ( 'pod' != $pod[ 'type' ] )
5194
- $pod[ 'object_fields' ] = $this->get_wp_object_fields( $pod[ 'type' ], $pod );
5195
-
5196
- $fields = get_posts( array(
5197
- 'post_type' => '_pods_field',
5198
- 'posts_per_page' => -1,
5199
- 'nopaging' => true,
5200
- 'post_parent' => $pod[ 'id' ],
5201
- 'orderby' => 'menu_order',
5202
- 'order' => 'ASC'
5203
- ) );
5204
-
5205
- if ( !empty( $fields ) ) {
5206
- foreach ( $fields as $field ) {
5207
- $field->pod = $pod[ 'name' ];
5208
- $field->table_info = (boolean) pods_var_raw( 'table_info', $params, false );
5209
-
5210
- if ( $load_fields ) {
5211
- $field = $this->load_field( $field );
5212
-
5213
- $field = PodsForm::field_setup( $field, null, $field[ 'type' ] );
5214
- }
5215
- else {
5216
- $field = array(
5217
- 'id' => $field->ID,
5218
- 'name' => $field->post_name,
5219
- 'label' => $field->post_title,
5220
- 'type' => get_post_meta( $field->ID, 'type', true )
5221
- );
5222
- }
5223
-
5224
- $pod[ 'fields' ][ $field[ 'name' ] ] = $field;
5225
- }
5226
- }
5227
-
5228
- if ( did_action( 'init' ) && pods_api_cache() )
5229
- pods_transient_set( $transient . '_' . $pod[ 'name' ], $pod );
5230
-
5231
- return $pod;
5232
- }
5233
-
5234
- /**
5235
- * Load a list of Pods based on filters specified.
5236
- *
5237
- * $params['type'] string/array Pod Type(s) to filter by
5238
- * $params['object'] string/array Pod Object(s) to filter by
5239
- * $params['options'] array Pod Option(s) key=>value array to filter by
5240
- * $params['orderby'] string ORDER BY clause of query
5241
- * $params['limit'] string Number of Pods to return
5242
- * $params['where'] string WHERE clause of query
5243
- * $params['ids'] string|array IDs of Objects
5244
- * $params['count'] boolean Return only a count of Pods
5245
- * $params['names'] boolean Return only an array of name => label
5246
- * $params['ids'] boolean Return only an array of ID => label
5247
- * $params['fields'] boolean Return pod fields with Pods (default is true)
5248
- * $params['key_names'] boolean Return pods keyed by name
5249
- *
5250
- * @param array $params An associative array of parameters
5251
- *
5252
- * @return array|mixed
5253
- *
5254
- * @uses PodsAPI::load_pod
5255
- *
5256
- * @since 2.0
5257
- */
5258
- public function load_pods ( $params = null ) {
5259
-
5260
- /**
5261
- * @var $sitepress SitePress
5262
- */
5263
- global $sitepress, $icl_adjust_id_url_filter_off;
5264
-
5265
- $current_language = false;
5266
-
5267
- // WPML support
5268
- if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
5269
- $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
5270
- // Polylang support
5271
- elseif ( function_exists( 'pll_current_language' ) )
5272
- $current_language = pll_current_language( 'slug' );
5273
-
5274
- $params = (object) pods_sanitize( $params );
5275
-
5276
- $order = 'ASC';
5277
- $orderby = 'menu_order title';
5278
- $limit = -1;
5279
- $ids = false;
5280
-
5281
- $meta_query = array();
5282
- $cache_key = '';
5283
-
5284
- if ( isset( $params->type ) && !empty( $params->type ) ) {
5285
- if ( !is_array( $params->type ) )
5286
- $params->type = array( trim( $params->type ) );
5287
-
5288
- sort( $params->type );
5289
-
5290
- $meta_query[] = array(
5291
- 'key' => 'type',
5292
- 'value' => $params->type,
5293
- 'compare' => 'IN'
5294
- );
5295
-
5296
- if ( 1 == count( $params->type ) )
5297
- $cache_key .= '_type_' . trim( implode( '', $params->type ) );
5298
- }
5299
-
5300
- if ( isset( $params->object ) && !empty( $params->object ) ) {
5301
- if ( !is_array( $params->object ) )
5302
- $params->object = array( $params->object );
5303
-
5304
- $params->object = pods_trim( $params->object );
5305
-
5306
- sort( $params->object );
5307
 
5308
- $meta_query[] = array(
5309
- 'key' => 'object',
5310
- 'value' => $params->object,
5311
- 'compare' => 'IN'
5312
- );
5313
-
5314
- if ( 1 == count( $params->object ) )
5315
- $cache_key .= '_object_' . trim( implode( '', $params->object ) );
5316
- }
5317
 
5318
- if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
5319
- foreach ( $params->options as $option => $value ) {
5320
- if ( !is_array( $value ) )
5321
- $value = array( $value );
5322
 
5323
- $value = pods_trim( $value );
 
 
5324
 
5325
- sort( $value );
 
 
 
 
 
 
5326
 
5327
- $meta_query[] = array(
5328
- 'key' => $option,
5329
- 'value' => pods_sanitize( $value ),
5330
- 'compare' => 'IN'
5331
- );
5332
- }
5333
 
5334
- $cache_key = '';
5335
- }
5336
 
5337
- if ( isset( $params->where ) && is_array( $params->where ) )
5338
- $meta_query = array_merge( $meta_query, (array) $params->where );
 
 
5339
 
5340
- if ( isset( $params->order ) && !empty( $params->order ) && in_array( strtoupper( $params->order ), array( 'ASC', 'DESC' ) ) )
5341
- $order = strtoupper( $params->order );
 
 
5342
 
5343
- if ( isset( $params->orderby ) && !empty( $params->orderby ) )
5344
- $orderby = strtoupper( $params->orderby );
5345
 
5346
- if ( isset( $params->limit ) && !empty( $params->limit ) )
5347
- $limit = pods_absint( $params->limit );
5348
 
5349
- if ( isset( $params->ids ) && !empty( $params->ids ) ) {
5350
- $ids = $params->ids;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5351
 
5352
- if ( !is_array( $ids ) )
5353
- $ids = explode( ',', $ids );
5354
- }
5355
 
5356
- if ( empty( $ids ) )
5357
- $ids = false;
5358
 
5359
- $pre_key = '';
 
 
 
 
 
 
 
 
 
 
5360
 
5361
- if ( !empty( $current_language ) )
5362
- $pre_key .= '_' . $current_language;
5363
 
5364
- if ( isset( $params->count ) && $params->count )
5365
- $pre_key .= '_count';
 
 
 
 
 
 
 
 
 
 
5366
 
5367
- if ( isset( $params->ids ) && $params->ids && !empty( $ids ) )
5368
- $pre_key .= '_ids_' . implode( '_', $ids );
 
5369
 
5370
- if ( isset( $params->names ) && $params->names )
5371
- $pre_key .= '_names';
5372
- elseif ( isset( $params->names_ids ) && $params->names_ids )
5373
- $pre_key .= '_names_ids';
5374
 
5375
- if ( isset( $params->key_names ) && $params->key_names )
5376
- $pre_key .= '_namekeys';
 
 
 
 
 
 
 
 
 
 
5377
 
5378
- if ( isset( $params->fields ) && !$params->fields )
5379
- $pre_key .= '_nofields';
5380
 
5381
- if ( isset( $params->table_info ) && $params->table_info )
5382
- $pre_key .= '_tableinfo';
 
 
 
 
 
 
 
 
 
 
 
5383
 
 
 
 
 
5384
 
5385
- $pre_key .= '_get';
 
 
5386
 
5387
- if ( empty( $cache_key ) )
5388
- $cache_key = 'pods' . $pre_key . '_all';
5389
- else
5390
- $cache_key = 'pods' . $pre_key . $cache_key;
5391
 
5392
- if ( pods_api_cache() && !empty( $cache_key ) && ( 'pods' . ( !empty( $current_language ) ? '_' . $current_language : '' ) . '_get_all' != $cache_key || empty( $meta_query ) ) && $limit < 1 && ( empty( $orderby ) || 'menu_order title' == $orderby ) && empty( $ids ) ) {
5393
- $the_pods = pods_transient_get( $cache_key );
5394
 
5395
- if ( false === $the_pods )
5396
- $the_pods = pods_cache_get( $cache_key, 'pods' );
5397
 
5398
- if ( !is_array( $the_pods ) && 'none' == $the_pods )
5399
- return array();
5400
- elseif ( false !== $the_pods )
5401
- return $the_pods;
5402
- }
5403
 
5404
- $the_pods = array();
 
 
 
5405
 
5406
- $args = array(
5407
- 'post_type' => '_pods_pod',
5408
- 'nopaging' => true,
5409
- 'posts_per_page' => $limit,
5410
- 'order' => $order,
5411
- 'orderby' => $orderby,
5412
- 'meta_query' => $meta_query,
5413
- );
5414
 
5415
- // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
5416
- if ( false != $ids ) {
5417
- $args[ 'post__in' ] = $ids;
5418
  }
5419
 
5420
- $_pods = get_posts( $args );
5421
 
5422
- $export_ignore = array(
5423
- 'object_type',
5424
- 'object_name',
5425
- 'table',
5426
- 'meta_table',
5427
- 'pod_table',
5428
- 'field_id',
5429
- 'field_index',
5430
- 'field_slug',
5431
- 'field_type',
5432
- 'field_parent',
5433
- 'field_parent_select',
5434
- 'meta_field_id',
5435
- 'meta_field_index',
5436
- 'meta_field_value',
5437
- 'pod_field_id',
5438
- 'pod_field_index',
5439
- 'object_fields',
5440
- 'join',
5441
- 'where',
5442
- 'where_default',
5443
- 'orderby',
5444
- 'pod',
5445
- 'recurse',
5446
- 'table_info',
5447
- 'attributes',
5448
- 'group',
5449
- 'grouped',
5450
- 'developer_mode',
5451
- 'dependency',
5452
- 'depends-on',
5453
- 'excludes-on'
5454
- );
5455
-
5456
- $total_fields = 0;
5457
-
5458
- if ( isset( $params->count ) && $params->count )
5459
- $the_pods = count( $_pods );
5460
- else {
5461
- foreach ( $_pods as $pod ) {
5462
- if ( isset( $params->names ) && $params->names )
5463
- $the_pods[ $pod->post_name ] = $pod->post_title;
5464
- elseif ( isset( $params->names_ids ) && $params->names_ids )
5465
- $the_pods[ $pod->ID ] = $pod->post_name;
5466
- else {
5467
- if ( isset( $params->fields ) && !$params->fields )
5468
- $pod->fields = false;
5469
-
5470
- $pod = $this->load_pod( $pod );
5471
-
5472
- // Remove extra data not needed
5473
- if ( pods_var( 'export', $params, false ) && ( !isset( $params->fields ) || $params->fields ) ) {
5474
- foreach ( $export_ignore as $ignore ) {
5475
- if ( isset( $pod[ $ignore ] ) )
5476
- unset( $pod[ $ignore ] );
5477
- }
5478
-
5479
- foreach ( $pod[ 'fields' ] as $field => $field_data ) {
5480
- if ( isset( $pod[ 'fields' ][ $field ][ 'table_info' ] ) )
5481
- unset( $pod[ 'fields' ][ $field ][ 'table_info' ] );
5482
- }
5483
- }
5484
-
5485
- $total_fields += count( $pod[ 'fields' ] );
5486
-
5487
- if ( isset( $params->key_names ) && $params->key_names )
5488
- $the_pods[ $pod[ 'name' ] ] = $pod;
5489
- else
5490
- $the_pods[ $pod[ 'id' ] ] = $pod;
5491
- }
5492
- }
5493
- }
5494
-
5495
- if ( ( !function_exists( 'pll_current_language' ) || ( isset( $params->refresh ) && $params->refresh ) ) && !empty( $cache_key ) && ( 'pods' != $cache_key || empty( $meta_query ) ) && $limit < 1 && ( empty( $orderby ) || 'menu_order title' == $orderby ) && empty( $ids ) ) {
5496
- // Too many Pods can cause issues with the DB when caching is not enabled
5497
- if ( 15 < count( $the_pods ) || 75 < count( $total_fields ) ) {
5498
- pods_transient_clear( $cache_key );
5499
-
5500
- if ( pods_api_cache() ) {
5501
- if ( empty( $the_pods ) && ( !isset( $params->count ) || !$params->count ) )
5502
- pods_cache_set( $cache_key, 'none', 'pods' );
5503
- else
5504
- pods_cache_set( $cache_key, $the_pods, 'pods' );
5505
- }
5506
- }
5507
- else {
5508
- pods_cache_clear( $cache_key, 'pods' );
5509
-
5510
- if ( pods_api_cache() ) {
5511
- if ( empty( $the_pods ) && ( !isset( $params->count ) || !$params->count ) )
5512
- pods_transient_set( $cache_key, 'none' );
5513
- else
5514
- pods_transient_set( $cache_key, $the_pods );
5515
- }
5516
- }
5517
- }
5518
-
5519
- return $the_pods;
5520
- }
5521
-
5522
- /**
5523
- * Check if a Pod's field exists
5524
- *
5525
- * $params['pod_id'] int The Pod ID
5526
- * $params['id'] int The field ID
5527
- * $params['name'] string The field name
5528
- *
5529
- * @param array $params An associative array of parameters
5530
- *
5531
- * @return bool
5532
- *
5533
- * @since 1.12
5534
- */
5535
- public function field_exists ( $params ) {
5536
- $params = (object) pods_sanitize( $params );
5537
-
5538
- if ( ( !empty( $params->id ) || !empty( $params->name ) ) && isset( $params->pod_id ) && !empty( $params->pod_id ) ) {
5539
- if ( !isset( $params->name ) )
5540
- $field = get_post( $dummy = (int) $params->id );
5541
- else {
5542
- $field = get_posts( array(
5543
- 'name' => $params->name,
5544
- 'post_type' => '_pods_field',
5545
- 'posts_per_page' => 1,
5546
- 'post_parent' => $params->pod_id
5547
- ) );
5548
- }
5549
-
5550
- if ( !empty( $field ) )
5551
- return true;
5552
- }
5553
-
5554
- return false;
5555
- }
5556
-
5557
- /**
5558
- * Load a field
5559
- *
5560
- * $params['pod_id'] int The Pod ID
5561
- * $params['pod'] string The Pod name
5562
- * $params['id'] int The field ID
5563
- * $params['name'] string The field name
5564
- * $params['table_info'] boolean Whether to lookup a pick field's table info
5565
- *
5566
- * @param array $params An associative array of parameters
5567
- * @param boolean $strict Whether to require a field exist or not when loading the info
5568
- *
5569
- * @return array|bool Array with field data, false if field not found
5570
- * @since 1.7.9
5571
- */
5572
- public function load_field ( $params, $strict = false ) {
5573
- $params = (object) $params;
5574
-
5575
- if ( !isset( $params->table_info ) )
5576
- $params->table_info = false;
5577
-
5578
- $pod = array();
5579
- $field = array();
5580
-
5581
- if ( isset( $params->post_title ) )
5582
- $_field = $params;
5583
- elseif ( isset( $params->id ) && !empty( $params->id ) )
5584
- $_field = get_post( $dumb = (int) $params->id );
5585
- else {
5586
- if ( !isset( $params->pod ) )
5587
- $params->pod = '';
5588
-
5589
- if ( !isset( $params->pod_id ) )
5590
- $params->pod_id = 0;
5591
-
5592
- if ( isset( $params->pod_data ) )
5593
- $pod = $params->pod_data;
5594
- else {
5595
- $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => false ), false );
5596
-
5597
- if ( false === $pod ) {
5598
- if ( $strict )
5599
- return pods_error( __( 'Pod not found', 'pods' ), $this );
5600
-
5601
- return false;
5602
- }
5603
- }
5604
-
5605
- $params->pod_id = $pod[ 'id' ];
5606
- $params->pod = $pod[ 'name' ];
5607
-
5608
- if ( empty( $params->name ) && empty( $params->pod ) && empty( $params->pod_id ) )
5609
- return pods_error( __( 'Either Field Name or Field ID / Pod ID are required', 'pods' ), $this );
5610
-
5611
- $params->name = pods_clean_name( $params->name, true, ( 'meta' == $pod[ 'storage' ] ? false : true ) );
5612
-
5613
- if ( isset( $pod[ 'fields' ][ $params->name ] ) && isset( $pod[ 'fields' ][ $params->name ][ 'id' ] ) )
5614
- return $pod[ 'fields' ][ $params->name ];
5615
-
5616
- $field = false;
5617
-
5618
- if ( pods_api_cache() )
5619
- $field = pods_transient_get( 'pods_field_' . $params->pod . '_' . $params->name );
5620
-
5621
- if ( empty( $field ) ) {
5622
- $field = get_posts( array(
5623
- 'name' => $params->name,
5624
- 'post_type' => '_pods_field',
5625
- 'posts_per_page' => 1,
5626
- 'post_parent' => $params->pod_id
5627
- ) );
5628
-
5629
- if ( empty( $field ) ) {
5630
- if ( $strict )
5631
- return pods_error( __( 'Field not found', 'pods' ), $this );
5632
-
5633
- return false;
5634
- }
5635
-
5636
- $_field = $field[ 0 ];
5637
-
5638
- $field = array();
5639
- }
5640
- }
5641
-
5642
- if ( empty( $_field ) ) {
5643
- if ( $strict )
5644
- return pods_error( __( 'Field not found', 'pods' ), $this );
5645
-
5646
- return false;
5647
- }
5648
-
5649
- $_field = get_object_vars( $_field );
5650
-
5651
- if ( !isset( $pod[ 'name' ] ) && !isset( $_field[ 'pod' ] ) ) {
5652
- if ( 0 < $_field[ 'post_parent' ] )
5653
- $pod = $this->load_pod( array( 'id' => $_field[ 'post_parent' ], 'table_info' => false ), false );
5654
-
5655
- if ( empty( $pod ) ) {
5656
- if ( $strict )
5657
- return pods_error( __( 'Pod for field not found', 'pods' ), $this );
5658
-
5659
- return false;
5660
- }
5661
- }
5662
-
5663
- if ( empty( $field ) ) {
5664
- if ( pods_api_cache() && ( isset( $pod[ 'name' ] ) || isset( $_field[ 'pod' ] ) ) )
5665
- $field = pods_transient_get( 'pods_field_' . pods_var( 'name', $pod, pods_var( 'pod', $_field ), null, true ) . '_' . $_field[ 'post_name' ] );
5666
-
5667
- if ( empty( $field ) ) {
5668
- $defaults = array(
5669
- 'type' => 'text'
5670
- );
5671
-
5672
- $field = array(
5673
- 'id' => $_field[ 'ID' ],
5674
- 'name' => $_field[ 'post_name' ],
5675
- 'label' => $_field[ 'post_title' ],
5676
- 'description' => $_field[ 'post_content' ],
5677
- 'weight' => $_field[ 'menu_order' ],
5678
- 'pod_id' => $_field[ 'post_parent' ],
5679
- 'pick_object' => '',
5680
- 'pick_val' => '',
5681
- 'sister_id' => '',
5682
- 'table_info' => array()
5683
- );
5684
-
5685
- if ( isset( $pod[ 'name' ] ) )
5686
- $field[ 'pod' ] = $pod[ 'name' ];
5687
- elseif ( isset( $_field[ 'pod' ] ) )
5688
- $field[ 'pod' ] = $_field[ 'pod' ];
5689
-
5690
- $field[ 'options' ] = get_post_meta( $field[ 'id' ] );
5691
-
5692
- $options_ignore = array(
5693
- 'method',
5694
- 'table_info',
5695
- 'attributes',
5696
- 'group',
5697
- 'grouped',
5698
- 'developer_mode',
5699
- 'dependency',
5700
- 'depends-on',
5701
- 'excludes-on'
5702
- );
5703
-
5704
- foreach ( $options_ignore as $ignore ) {
5705
- if ( isset( $field[ 'options' ][ $ignore ] ) )
5706
- unset( $field[ 'options' ][ $ignore ] );
5707
- }
5708
-
5709
- foreach ( $field[ 'options' ] as $option => $value ) {
5710
- if ( is_array( $value ) ) {
5711
- foreach ( $value as $k => $v ) {
5712
- if ( !is_array( $v ) )
5713
- $value[ $k ] = maybe_unserialize( $v );
5714
- }
5715
-
5716
- if ( 1 == count( $value ) )
5717
- $value = current( $value );
5718
- }
5719
- else
5720
- $value = maybe_unserialize( $value );
5721
-
5722
- $field[ 'options' ][ $option ] = $value;
5723
- }
5724
-
5725
- $field[ 'options' ] = array_merge( $defaults, $field[ 'options' ] );
5726
-
5727
- $field[ 'type' ] = $field[ 'options' ][ 'type' ];
5728
-
5729
- unset( $field[ 'options' ][ 'type' ] );
5730
-
5731
- if ( isset( $field[ 'options' ][ 'pick_object' ] ) ) {
5732
- $field[ 'pick_object' ] = $field[ 'options' ][ 'pick_object' ];
5733
-
5734
- unset( $field[ 'options' ][ 'pick_object' ] );
5735
- }
5736
-
5737
- if ( isset( $field[ 'options' ][ 'pick_val' ] ) ) {
5738
- $field[ 'pick_val' ] = $field[ 'options' ][ 'pick_val' ];
5739
-
5740
- unset( $field[ 'options' ][ 'pick_val' ] );
5741
- }
5742
-
5743
- if ( isset( $field[ 'options' ][ 'sister_id' ] ) ) {
5744
- $field[ 'sister_id' ] = $field[ 'options' ][ 'sister_id' ];
5745
-
5746
- unset( $field[ 'options' ][ 'sister_id' ] );
5747
- }
5748
-
5749
- if ( isset( $field[ 'options' ][ 'sister_field_id' ] ) )
5750
- unset( $field[ 'options' ][ 'sister_field_id' ] );
5751
-
5752
- if ( pods_api_cache() && ( isset( $pod[ 'name' ] ) || isset( $_field[ 'pod' ] ) ) )
5753
- pods_transient_set( 'pods_field_' . pods_var( 'name', $pod, pods_var( 'pod', $_field ), null, true ) . '_' . $field[ 'name' ], $field );
5754
- }
5755
- }
5756
-
5757
- $field[ 'table_info' ] = array();
5758
-
5759
- if ( 'pick' == $field[ 'type' ] && $params->table_info )
5760
- $field[ 'table_info' ] = $this->get_table_info( $field[ 'pick_object' ], $field[ 'pick_val' ], null, null, $field );
5761
-
5762
- return $field;
5763
- }
5764
-
5765
- /**
5766
- * Load fields by Pod, ID, Name, and/or Type
5767
- *
5768
- * $params['pod_id'] int The Pod ID
5769
- * $params['pod'] string The Pod name
5770
- * $params['id'] array The field IDs
5771
- * $params['name'] array The field names
5772
- * $params['type'] array The field types
5773
- * $params['options'] array Field Option(s) key=>value array to filter by
5774
- * $params['where'] string WHERE clause of query
5775
- *
5776
- * @param array $params An associative array of parameters
5777
- * @param bool $strict Whether to require a field exist or not when loading the info
5778
- *
5779
- * @return array Array of field data.
5780
- *
5781
- * @since 1.7.9
5782
- */
5783
- public function load_fields ( $params, $strict = false ) {
5784
- $params = (object) pods_sanitize( $params );
5785
-
5786
- if ( !isset( $params->pod ) || empty( $params->pod ) )
5787
- $params->pod = '';
5788
-
5789
- if ( !isset( $params->pod_id ) || empty( $params->pod_id ) )
5790
- $params->pod_id = 0;
5791
-
5792
- if ( !isset( $params->name ) || empty( $params->name ) )
5793
- $params->name = array();
5794
- else
5795
- $params->name = (array) $params->name;
5796
-
5797
- if ( !isset( $params->id ) || empty( $params->id ) )
5798
- $params->id = array();
5799
- else {
5800
- $params->id = (array) $params->id;
5801
-
5802
- foreach ( $params->id as &$id ) {
5803
- $id = pods_absint( $id );
5804
- }
5805
- }
5806
-
5807
- if ( !isset( $params->type ) || empty( $params->type ) )
5808
- $params->type = array();
5809
- else
5810
- $params->type = (array) $params->type;
5811
-
5812
- $fields = array();
5813
-
5814
- if ( !empty( $params->pod ) || !empty( $params->pod_id ) ) {
5815
- $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => true ), false );
5816
-
5817
- if ( false === $pod ) {
5818
- if ( $strict )
5819
- return pods_error( __( 'Pod not found', 'pods' ), $this );
5820
 
5821
- return $fields;
5822
- }
5823
 
5824
- $pod[ 'fields' ] = array_merge( pods_var_raw( 'object_fields', $pod, array() ), $pod[ 'fields' ] );
5825
-
5826
- foreach ( $pod[ 'fields' ] as $field ) {
5827
- if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) )
5828
- $fields[ $field[ 'name' ] ] = $field;
5829
- elseif ( in_array( $fields[ 'name' ], $params->name ) || in_array( $fields[ 'id' ], $params->id ) || in_array( $fields[ 'type' ], $params->type ) )
5830
- $fields[ $field[ 'name' ] ] = $field;
5831
- }
5832
- }
5833
- elseif ( ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) || ( isset( $params->where ) && !empty( $params->where ) && is_array( $params->where ) ) ) {
5834
- $order = 'ASC';
5835
- $orderby = 'menu_order title';
5836
- $limit = -1;
5837
- $ids = false;
5838
-
5839
- $meta_query = array();
5840
-
5841
- if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
5842
- foreach ( $params->options as $option => $value ) {
5843
- if ( !is_array( $value ) )
5844
- $value = array( $value );
5845
-
5846
- $value = pods_trim( $value );
5847
-
5848
- sort( $value );
5849
-
5850
- $meta_query[] = array(
5851
- 'key' => $option,
5852
- 'value' => pods_sanitize( $value ),
5853
- 'compare' => 'IN'
5854
- );
5855
- }
5856
- }
5857
-
5858
- if ( isset( $params->where ) && !empty( $params->where ) && is_array( $params->where ) )
5859
- $meta_query = array_merge( $meta_query, (array) $params->where );
5860
 
5861
- $args = array(
5862
- 'post_type' => '_pods_field',
5863
- 'nopaging' => true,
5864
- 'posts_per_page' => $limit,
5865
- 'order' => $order,
5866
- 'orderby' => $orderby,
5867
- 'meta_query' => $meta_query,
5868
- );
5869
 
5870
- // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
5871
- if ( false != $ids ) {
5872
- $args[ 'post__in' ] = $ids;
5873
- }
 
5874
 
5875
- $fields = false;
 
 
 
5876
 
5877
- if ( pods_api_cache() )
5878
- $fields = pods_cache_get( md5( json_encode( $args ) ), 'pods_load_fields' );
 
5879
 
5880
- if ( false !== $fields )
5881
- return $fields;
 
5882
 
5883
- $fields = array();
5884
 
5885
- $_fields = get_posts( $args );
 
 
 
 
 
 
 
 
 
 
 
 
5886
 
5887
- foreach ( $_fields as $field ) {
5888
- $field = $this->load_field( $field, false );
 
 
 
 
5889
 
5890
- if ( !empty( $field ) )
5891
- $fields[ $field[ 'id' ] ] = $field;
5892
- }
5893
 
5894
- if ( pods_api_cache() )
5895
- pods_cache_set( md5( json_encode( $args ) ), $fields, 'pods_load_fields' );
5896
- }
5897
- else {
5898
- if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) )
5899
- return pods_error( __( 'Either Field Name / Field ID / Field Type, or Pod Name / Pod ID are required', 'pods' ), $this );
5900
 
5901
- $lookup = array();
5902
 
5903
- if ( !empty( $params->name ) ) {
5904
- $fields = implode( "', '", $params->name );
 
 
 
 
 
 
 
 
 
 
5905
 
5906
- $lookup[] = "`post_name` IN ( '{$fields}' )";
5907
- }
5908
 
5909
- if ( !empty( $params->id ) ) {
5910
- $fields = implode( ", ", $params->id );
 
 
 
5911
 
5912
- $lookup[] = "`ID` IN ( {$fields} )";
5913
- }
 
5914
 
5915
- $lookup = implode( ' AND ', $lookup );
5916
 
5917
- $fields = false;
 
 
 
 
5918
 
5919
- if ( pods_api_cache() )
5920
- $fields = pods_cache_get( md5( $lookup ), 'pods_load_fields' );
5921
 
5922
- if ( false !== $fields )
5923
- return $fields;
 
 
 
 
 
5924
 
5925
- $result = pods_query( "SELECT `ID`, `post_name`, `post_parent` FROM `@wp_posts` WHERE `post_type` = '_pods_field' AND ( {$lookup} )" );
 
 
5926
 
5927
- $fields = array();
5928
 
5929
- if ( !empty( $result ) ) {
5930
- foreach ( $result as $field ) {
5931
- $field = $this->load_field( array(
5932
- 'id' => $field->ID,
5933
- 'name' => $field->post_name,
5934
- 'pod_id' => $field->post_parent
5935
- ), false );
5936
-
5937
- if ( !empty( $field ) && ( empty( $params->type ) || in_array( $field[ 'type' ], $params->type ) ) )
5938
- $fields[ $field[ 'id' ] ] = $field;
5939
- }
5940
- }
5941
-
5942
- if ( pods_api_cache() )
5943
- pods_cache_set( md5( $lookup ), $fields, 'pods_load_fields' );
5944
- }
5945
-
5946
- return $fields;
5947
- }
5948
-
5949
- /**
5950
- * Load a Pods Object
5951
- *
5952
- * $params['id'] int The Object ID
5953
- * $params['name'] string The Object name
5954
- * $params['type'] string The Object type
5955
- *
5956
- * @param array|object $params An associative array of parameters
5957
- * @param bool $strict
5958
- *
5959
- * @return array|bool
5960
- * @since 2.0
5961
- */
5962
- public function load_object ( $params, $strict = false ) {
5963
- if ( is_object( $params ) && isset( $params->post_title ) )
5964
- $_object = get_object_vars( $params );
5965
- else {
5966
- $params = (object) pods_sanitize( $params );
5967
-
5968
- if ( !isset( $params->type ) || empty( $params->type ) )
5969
- return pods_error( __( 'Object type is required', 'pods' ), $this );
5970
-
5971
- if ( ( !isset( $params->id ) || empty( $params->id ) ) && ( !isset( $params->name ) || empty( $params->name ) ) )
5972
- return pods_error( __( 'Either Object ID or Name are required', 'pods' ), $this );
5973
-
5974
- /**
5975
- * @var $wpdb wpdb
5976
- */
5977
- global $wpdb;
5978
-
5979
- if ( isset( $params->name ) )
5980
- $_object = pods_by_title( $params->name, ARRAY_A, '_pods_' . $params->type, 'publish' );
5981
- else {
5982
- $object = $params->id;
5983
-
5984
- $_object = get_post( $object, ARRAY_A );
5985
- }
5986
-
5987
- if ( empty( $_object ) ) {
5988
- if ( $strict )
5989
- return pods_error( __( 'Object not found', 'pods' ), $this );
5990
-
5991
- return false;
5992
- }
5993
- }
5994
-
5995
- $object = array(
5996
- 'id' => $_object[ 'ID' ],
5997
- 'name' => $_object[ 'post_title' ],
5998
- 'code' => $_object[ 'post_content' ],
5999
- 'type' => str_replace( '_pods_', '', $_object[ 'post_type' ] ),
6000
- 'slug' => $_object[ 'post_name' ]
6001
- );
6002
-
6003
- $object[ 'options' ] = get_post_meta( $object[ 'id' ] );
6004
-
6005
- foreach ( $object[ 'options' ] as $option => &$value ) {
6006
- if ( is_array( $value ) && 1 == count( $value ) )
6007
- $value = current( $value );
6008
- }
6009
-
6010
- return $object;
6011
- }
6012
-
6013
- /**
6014
- * Load Multiple Pods Objects
6015
- *
6016
- * $params['type'] string The Object type
6017
- * $params['options'] array Pod Option(s) key=>value array to filter by
6018
- * $params['orderby'] string ORDER BY clause of query
6019
- * $params['limit'] string Number of objects to return
6020
- * $params['where'] string WHERE clause of query
6021
- * $params['ids'] string|array IDs of Objects
6022
- *
6023
- * @param array|object $params An associative array of parameters
6024
- *
6025
- * @return array
6026
- * @since 2.0
6027
- */
6028
- public function load_objects ( $params ) {
6029
- $params = (object) pods_sanitize( $params );
6030
-
6031
- if ( !isset( $params->type ) || empty( $params->type ) )
6032
- return pods_error( __( 'Pods Object type is required', 'pods' ), $this );
6033
-
6034
- $order = 'ASC';
6035
- $orderby = 'menu_order';
6036
- $limit = -1;
6037
- $ids = false;
6038
-
6039
- $meta_query = array();
6040
- $cache_key = '';
6041
-
6042
- if ( isset( $params->options ) && !empty( $params->options ) && is_array( $params->options ) ) {
6043
- foreach ( $params->options as $option => $value ) {
6044
- if ( !is_array( $value ) )
6045
- $value = array( $value );
6046
-
6047
- $value = pods_trim( $value );
6048
-
6049
- sort( $value );
6050
-
6051
- $meta_query[] = array(
6052
- 'key' => $option,
6053
- 'value' => pods_sanitize( $value ),
6054
- 'compare' => 'IN'
6055
- );
6056
- }
6057
- }
6058
-
6059
- if ( isset( $params->where ) && is_array( $params->where ) )
6060
- $meta_query = array_merge( $meta_query, (array) $params->where );
6061
-
6062
- if ( isset( $params->order ) && !empty( $params->order ) && in_array( strtoupper( $params->order ), array( 'ASC', 'DESC' ) ) )
6063
- $order = strtoupper( $params->order );
6064
-
6065
- if ( isset( $params->orderby ) && !empty( $params->orderby ) )
6066
- $orderby = strtoupper( $params->orderby );
6067
 
6068
- if ( isset( $params->limit ) && !empty( $params->limit ) )
6069
- $limit = pods_absint( $params->limit );
6070
 
6071
- if ( isset( $params->ids ) && !empty( $params->ids ) ) {
6072
- $ids = $params->ids;
 
6073
 
6074
- if ( !is_array( $ids ) )
6075
- $ids = explode( ',', $ids );
6076
- }
 
 
6077
 
6078
- if ( empty( $ids ) )
6079
- $ids = false;
6080
-
6081
- if ( pods_api_cache() && empty( $meta_query ) && empty( $limit ) && ( empty( $orderby ) || 'menu_order' == $orderby ) && empty( $ids ) ) {
6082
- $cache_key = 'pods_objects_' . $params->type;
6083
 
6084
- $the_objects = pods_transient_get( $cache_key );
 
6085
 
6086
- if ( false !== $the_objects )
6087
- return $the_objects;
6088
- }
 
 
 
 
 
 
 
6089
 
6090
- $the_objects = array();
 
 
6091
 
6092
- $args = array(
6093
- 'post_type' => '_pods_' . $params->type,
6094
- 'nopaging' => true,
6095
- 'posts_per_page' => $limit,
6096
- 'order' => $order,
6097
- 'orderby' => $orderby,
6098
- 'meta_query' => $meta_query,
6099
  );
6100
 
6101
- // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
6102
- if ( false != $ids ) {
6103
- $args[ 'post__in' ] = $ids;
6104
- }
6105
 
6106
- $objects = get_posts( $args );
6107
 
6108
- foreach ( $objects as $object ) {
6109
- $object = $this->load_object( $object );
6110
-
6111
- $the_objects[ $object[ 'name' ] ] = $object;
6112
- }
6113
-
6114
- if ( pods_api_cache() && !empty( $cache_key ) )
6115
- pods_transient_set( $cache_key, $the_objects );
6116
-
6117
- return $the_objects;
6118
- }
6119
-
6120
- /**
6121
- * @see PodsAPI::load_object
6122
- *
6123
- * Load a Pod Template
6124
- *
6125
- * $params['id'] int The template ID
6126
- * $params['name'] string The template name
6127
- *
6128
- * @param array $params An associative array of parameters
6129
- *
6130
- * @return array|bool
6131
- * @since 1.7.9
6132
- */
6133
- public function load_template ( $params ) {
6134
- if ( !class_exists( 'Pods_Templates' ) )
6135
- return false;
6136
-
6137
- $params = (object) $params;
6138
- $params->type = 'template';
6139
- return $this->load_object( $params );
6140
- }
6141
-
6142
- /**
6143
- * @see PodsAPI::load_objects
6144
- *
6145
- * Load Multiple Pod Templates
6146
- *
6147
- * $params['where'] string The WHERE clause of query
6148
- * $params['options'] array Pod Option(s) key=>value array to filter by
6149
- * $params['orderby'] string ORDER BY clause of query
6150
- * $params['limit'] string Number of templates to return
6151
- *
6152
- * @param array $params (optional) An associative array of parameters
6153
- *
6154
- * @return array
6155
- *
6156
- * @since 2.0
6157
- */
6158
- public function load_templates ( $params = null ) {
6159
- if ( !class_exists( 'Pods_Templates' ) )
6160
- return array();
6161
-
6162
- $params = (object) $params;
6163
- $params->type = 'template';
6164
- return $this->load_objects( $params );
6165
- }
6166
-
6167
- /**
6168
- * @see PodsAPI::load_object
6169
- *
6170
- * Load a Pod Page
6171
- *
6172
- * $params['id'] int The page ID
6173
- * $params['name'] string The page URI
6174
- *
6175
- * @param array $params An associative array of parameters
6176
- *
6177
- * @return array|bool
6178
- *
6179
- * @since 1.7.9
6180
- */
6181
- public function load_page ( $params ) {
6182
- if ( !class_exists( 'Pods_Pages' ) )
6183
- return false;
6184
-
6185
- $params = (object) $params;
6186
- if ( !isset( $params->name ) && isset( $params->uri ) ) {
6187
- $params->name = $params->uri;
6188
- unset( $params->uri );
6189
- }
6190
- $params->type = 'page';
6191
- return $this->load_object( $params );
6192
- }
6193
-
6194
- /**
6195
- * @see PodsAPI::load_objects
6196
- *
6197
- * Load Multiple Pod Pages
6198
- *
6199
- * $params['where'] string The WHERE clause of query
6200
- * $params['options'] array Pod Option(s) key=>value array to filter by
6201
- * $params['orderby'] string ORDER BY clause of query
6202
- * $params['limit'] string Number of pages to return
6203
- *
6204
- * @param array $params (optional) An associative array of parameters
6205
- *
6206
- * @return array
6207
- *
6208
- * @since 2.0
6209
- */
6210
- public function load_pages ( $params = null ) {
6211
- if ( !class_exists( 'Pods_Pages' ) )
6212
- return array();
6213
-
6214
- $params = (object) $params;
6215
- $params->type = 'page';
6216
- return $this->load_objects( $params );
6217
- }
6218
-
6219
- /**
6220
- * @see PodsAPI::load_object
6221
- *
6222
- * Load a Pod Helper
6223
- *
6224
- * $params['id'] int The helper ID
6225
- * $params['name'] string The helper name
6226
- *
6227
- * @param array $params An associative array of parameters
6228
- *
6229
- * @return array|bool
6230
- *
6231
- * @since 1.7.9
6232
- */
6233
- public function load_helper ( $params ) {
6234
- if ( !class_exists( 'Pods_Helpers' ) )
6235
- return false;
6236
-
6237
- $params = (object) $params;
6238
- $params->type = 'helper';
6239
- return $this->load_object( $params );
6240
- }
6241
-
6242
- /**
6243
- * @see PodsAPI::load_objects
6244
- *
6245
- * Load Multiple Pod Helpers
6246
- *
6247
- * $params['where'] string The WHERE clause of query
6248
- * $params['options'] array Pod Option(s) key=>value array to filter by
6249
- * $params['orderby'] string ORDER BY clause of query
6250
- * $params['limit'] string Number of pages to return
6251
- *
6252
- * @param array $params (optional) An associative array of parameters
6253
- *
6254
- * @return array
6255
- *
6256
- * @since 2.0
6257
- */
6258
- public function load_helpers ( $params = null ) {
6259
- if ( !class_exists( 'Pods_Helpers' ) )
6260
- return array();
6261
-
6262
- $params = (object) $params;
6263
- $params->type = 'helper';
6264
- return $this->load_objects( $params );
6265
- }
6266
-
6267
- /**
6268
- * Load the pod item object
6269
- *
6270
- * $params['pod'] string The datatype name
6271
- * $params['id'] int (optional) The item's ID
6272
- *
6273
- * @param array $params An associative array of parameters
6274
- *
6275
- * @return bool|\Pods
6276
- *
6277
- * @uses pods()
6278
- *
6279
- * @since 2.0
6280
- */
6281
- public function load_pod_item ( $params ) {
6282
- $params = (object) pods_sanitize( $params );
6283
-
6284
- if ( !isset( $params->pod ) || empty( $params->pod ) )
6285
- return pods_error( __( 'Pod name required', 'pods' ), $this );
6286
- if ( !isset( $params->id ) || empty( $params->id ) )
6287
- return pods_error( __( 'Item ID required', 'pods' ), $this );
6288
-
6289
- $pod = false;
6290
-
6291
- if ( pods_api_cache() )
6292
- $pod = pods_cache_get( $params->id, 'pods_item_object_' . $params->pod );
6293
-
6294
- if ( false !== $pod )
6295
- return $pod;
6296
-
6297
- $pod = pods( $params->pod, $params->id );
6298
-
6299
- if ( pods_api_cache() )
6300
- pods_cache_set( $params->id, $pod, 'pods_item_object_' . $params->pod );
6301
-
6302
- return $pod;
6303
- }
6304
-
6305
- /**
6306
- * Load potential sister fields for a specific field
6307
- *
6308
- * $params['pod'] int The Pod name
6309
- * $params['related_pod'] string The related Pod name
6310
- *
6311
- * @param array $params An associative array of parameters
6312
- * @param array $pod (optional) Array of Pod data to use (to avoid lookup)
6313
- *
6314
- * @return array|bool
6315
- *
6316
- * @since 1.7.9
6317
- *
6318
- * @uses PodsAPI::load_pod
6319
- */
6320
- public function load_sister_fields ( $params, $pod = null ) {
6321
- $params = (object) pods_sanitize( $params );
6322
-
6323
- if ( empty( $pod ) ) {
6324
- $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => false ), false );
6325
-
6326
- if ( false === $pod )
6327
- return pods_error( __( 'Pod not found', 'pods' ), $this );
6328
- }
6329
-
6330
- $params->pod_id = $pod[ 'id' ];
6331
- $params->pod = $pod[ 'name' ];
6332
-
6333
- $type = false;
6334
-
6335
- if ( 0 === strpos( $params->related_pod, 'pod-' ) ) {
6336
- $params->related_pod = pods_str_replace( 'pod-', '', $params->related_pod, 1 );
6337
- $type = 'pod';
6338
- }
6339
- elseif ( 0 === strpos( $params->related_pod, 'post_type-' ) ) {
6340
- $params->related_pod = pods_str_replace( 'post_type-', '', $params->related_pod, 1 );
6341
- $type = 'post_type';
6342
- }
6343
- elseif ( 0 === strpos( $params->related_pod, 'taxonomy-' ) ) {
6344
- $params->related_pod = pods_str_replace( 'taxonomy-', '', $params->related_pod, 1 );
6345
- $type = 'taxonomy';
6346
- }
6347
-
6348
- $related_pod = $this->load_pod( array( 'name' => $params->related_pod, 'table_info' => false ), false );
6349
-
6350
- if ( false === $related_pod || ( false !== $type && 'pod' != $type && $type != $related_pod[ 'type' ] ) )
6351
- return pods_error( __( 'Related Pod not found', 'pods' ), $this );
6352
-
6353
- $params->related_pod_id = $related_pod[ 'id' ];
6354
- $params->related_pod = $related_pod[ 'name' ];
6355
-
6356
- $sister_fields = array();
6357
-
6358
- foreach ( $related_pod[ 'fields' ] as $field ) {
6359
- if ( 'pick' == $field[ 'type' ] && in_array( $field[ 'pick_object' ], array( $pod[ 'type' ], 'pod' ) ) && ( $params->pod == $field[ 'pick_object' ] || $params->pod == $field[ 'pick_val' ] ) )
6360
- $sister_fields[ $field[ 'id' ] ] = esc_html( $field[ 'label' ] . ' (' . $field[ 'name' ] . ')' );
6361
- }
6362
-
6363
- return $sister_fields;
6364
- }
6365
-
6366
- /**
6367
- * Takes a sql field such as tinyint and returns the pods field type, such as num.
6368
- *
6369
- * @param string $sql_field The SQL field to look for
6370
- *
6371
- * @return string The field type
6372
- *
6373
- * @since 2.0
6374
- */
6375
- public static function detect_pod_field_from_sql_data_type ( $sql_field ) {
6376
- $sql_field = strtolower( $sql_field );
6377
-
6378
- $field_to_field_map = array(
6379
- 'tinyint' => 'number',
6380
- 'smallint' => 'number',
6381
- 'mediumint' => 'number',
6382
- 'int' => 'number',
6383
- 'bigint' => 'number',
6384
- 'float' => 'number',
6385
- 'double' => 'number',
6386
- 'decimal' => 'number',
6387
- 'date' => 'date',
6388
- 'datetime' => 'datetime',
6389
- 'timestamp' => 'datetime',
6390
- 'time' => 'time',
6391
- 'year' => 'date',
6392
- 'varchar' => 'text',
6393
- 'text' => 'paragraph',
6394
- 'mediumtext' => 'paragraph',
6395
- 'longtext' => 'paragraph'
6396
- );
6397
-
6398
- return ( array_key_exists( $sql_field, $field_to_field_map ) ) ? $field_to_field_map[ $sql_field ] : 'paragraph';
6399
- }
6400
-
6401
- /**
6402
- * Gets all field types
6403
- *
6404
- * @return array Array of field types
6405
- *
6406
- * @uses PodsForm::field_loader
6407
- *
6408
- * @since 2.0
6409
- * @deprecated
6410
- */
6411
- public function get_field_types () {
6412
- return PodsForm::field_types();
6413
- }
6414
-
6415
- /**
6416
- * Gets the schema definition of a field.
6417
- *
6418
- * @param string $type Field type to look for
6419
- * @param array $options (optional) Options of the field to pass to the schema function.
6420
- *
6421
- * @return array|bool|mixed|null
6422
- *
6423
- * @since 2.0
6424
- */
6425
- private function get_field_definition ( $type, $options = null ) {
6426
- $definition = PodsForm::field_method( $type, 'schema', $options );
6427
-
6428
- return $this->do_hook( 'field_definition', $definition, $type, $options );
6429
- }
6430
-
6431
- /**
6432
- * @see PodsForm:validate
6433
- *
6434
- * Validates the value of a field.
6435
- *
6436
- * @param mixed $value The value to validate
6437
- * @param string $field Field to use for validation
6438
- * @param array $object_fields Fields of the object we're validating
6439
- * @param array $fields Array of all fields data
6440
- * @param array|Pods $pod Array of pod data (or Pods object)
6441
- * @param array|object $params Extra parameters to pass to the validation function of the field.
6442
- *
6443
- * @return array|bool
6444
- *
6445
- * @uses PodsForm::validate
6446
- *
6447
- * @since 2.0
6448
- */
6449
- public function handle_field_validation ( &$value, $field, $object_fields, $fields, $pod, $params ) {
6450
- $tableless_field_types = PodsForm::tableless_field_types();
6451
-
6452
- $fields = array_merge( $fields, $object_fields );
6453
-
6454
- $options = $fields[ $field ];
6455
-
6456
- $id = ( is_object( $params ) ? $params->id : ( is_object( $pod ) ? $pod->id() : 0 ) );
6457
-
6458
- if ( is_object( $pod ) )
6459
- $pod = $pod->pod_data;
6460
-
6461
- $type = $options[ 'type' ];
6462
- $label = $options[ 'label' ];
6463
- $label = empty( $label ) ? $field : $label;
6464
-
6465
- // Verify required fields
6466
- if ( 1 == pods_var( 'required', $options[ 'options' ], 0 ) && 'slug' != $type ) {
6467
- if ( '' == $value || null === $value || array() === $value || 0 === $value || '0' === $value || 0.00 === $value || '0.00' === $value )
6468
- return pods_error( sprintf( __( '%s is empty', 'pods' ), $label ), $this );
6469
-
6470
- if ( 'multi' == pods_var( 'pick_format_type', $options[ 'options' ] ) && 'autocomplete' != pods_var( 'pick_format_multi', $options[ 'options' ] ) ) {
6471
- $has_value = false;
6472
-
6473
- $check_value = (array) $value;
6474
-
6475
- foreach ( $check_value as $val ) {
6476
- if ( '' != $val && null !== $val && 0 !== $val && '0' !== $val ) {
6477
- $has_value = true;
6478
-
6479
- continue;
6480
- }
6481
- }
6482
-
6483
- if ( !$has_value )
6484
- return pods_error( sprintf( __( '%s is required', 'pods' ), $label ), $this );
6485
- }
6486
-
6487
- }
6488
-
6489
- // @todo move this to after pre-save preparations
6490
- // Verify unique fields
6491
- if ( 1 == pods_var( 'unique', $options[ 'options' ], 0 ) && '' !== $value && null !== $value && array() !== $value && 0 !== $value && '0' !== $value && 0.00 !== $value && '0.00' !== $value ) {
6492
- if ( empty( $pod ) )
6493
- return false;
6494
-
6495
- if ( !in_array( $type, $tableless_field_types ) ) {
6496
- $exclude = '';
6497
-
6498
- if ( !empty( $id ) )
6499
- $exclude = "AND `id` != {$id}";
6500
-
6501
- $check = false;
6502
-
6503
- $check_value = pods_sanitize( $value );
6504
-
6505
- // @todo handle meta-based fields
6506
- // Trigger an error if not unique
6507
- if ( 'table' == $pod[ 'storage' ] )
6508
- $check = pods_query( "SELECT `id` FROM `@wp_pods_" . $pod[ 'name' ] . "` WHERE `{$field}` = '{$check_value}' {$exclude} LIMIT 1", $this );
6509
-
6510
- if ( !empty( $check ) )
6511
- return pods_error( sprintf( __( '%s needs to be unique', 'pods' ), $label ), $this );
6512
- }
6513
- else {
6514
- // @todo handle tableless check
6515
- }
6516
- }
6517
-
6518
- $validate = PodsForm::validate( $options[ 'type' ], $value, $field, array_merge( $options, pods_var( 'options', $options, array() ) ), $fields, $pod, $id, $params );
6519
-
6520
- $validate = $this->do_hook( 'field_validation', $validate, $value, $field, $object_fields, $fields, $pod, $params );
6521
-
6522
- return $validate;
6523
- }
6524
-
6525
- /**
6526
- * Find items related to a parent field
6527
- *
6528
- * @param int $field_id The Field ID
6529
- * @param int $pod_id The Pod ID
6530
- * @param mixed $ids A comma-separated string (or array) of item IDs
6531
- * @param array $field Field data array
6532
- * @param array $pod Pod data array
6533
- *
6534
- * @return array
6535
- *
6536
- * @since 2.0
6537
- *
6538
- * @uses pods_query()
6539
- */
6540
- public function lookup_related_items ( $field_id, $pod_id, $ids, $field = null, $pod = null ) {
6541
- $related_ids = array();
6542
 
6543
- if ( !is_array( $ids ) )
6544
- $ids = explode( ',', $ids );
6545
 
6546
- foreach ( $ids as $k => $id ) {
6547
- $ids[ $k ] = (int) $id;
6548
- }
6549
 
6550
- $ids = array_unique( array_filter( $ids ) );
6551
 
6552
- $tableless_field_types = PodsForm::tableless_field_types();
 
6553
 
6554
- $field_type = pods_var( 'type', $field );
 
 
 
 
 
 
 
 
 
 
6555
 
6556
- if ( empty( $ids ) || !in_array( $field_type, $tableless_field_types ) )
6557
- return array();
6558
 
6559
- $related_pick_limit = 0;
6560
 
6561
- if ( empty( $field ) ) {
6562
- $field = $this->load_field( array( 'id' => $field_id ) );
6563
- }
6564
 
6565
- if ( !empty( $field ) ) {
6566
- $options = (array) pods_var_raw( 'options', $field, $field, null, true );
 
 
 
 
 
 
 
 
6567
 
6568
- $related_pick_limit = (int) pods_var( pods_var( 'type', $field ) . '_limit', $options, 0 );
 
 
 
6569
 
6570
- if ( 'single' == pods_var_raw( pods_var( 'type', $field ) . '_format_type', $options ) )
6571
- $related_pick_limit = 1;
6572
 
6573
- // Temporary hack until there's some better handling here
6574
- $related_pick_limit = $related_pick_limit * count( $ids );
6575
- }
6576
 
6577
- if ( 'taxonomy' == $field_type ) {
6578
- $related = wp_get_object_terms( $ids, pods_var( 'name', $field ), array( 'fields' => 'ids' ) );
 
6579
 
6580
- if ( !is_wp_error( $related ) ) {
6581
- $related_ids = $related;
6582
  }
 
 
6583
  }
6584
- elseif ( !pods_tableless() ) {
6585
- $ids = implode( ', ', $ids );
6586
-
6587
- $field_id = (int) $field_id;
6588
- $sister_id = (int) pods_var_raw( 'sister_id', $field, 0 );
6589
-
6590
- $related_where = "
6591
- `field_id` = {$field_id}
6592
- AND `item_id` IN ( {$ids} )
6593
- ";
6594
-
6595
- $sql = "
6596
- SELECT *
6597
- FROM `@wp_podsrel`
6598
- WHERE
6599
- {$related_where}
6600
- ORDER BY `weight`
6601
- ";
6602
-
6603
- $relationships = pods_query( $sql );
6604
-
6605
- if ( !empty( $relationships ) ) {
6606
- foreach ( $relationships as $relation ) {
6607
- if ( $field_id == $relation->field_id && !in_array( $relation->related_item_id, $related_ids ) )
6608
- $related_ids[] = (int) $relation->related_item_id;
6609
- elseif ( 0 < $sister_id && $field_id == $relation->related_field_id && !in_array( $relation->item_id, $related_ids ) )
6610
- $related_ids[] = (int) $relation->item_id;
6611
- }
6612
- }
6613
- }
6614
- else {
6615
- if ( !is_array( $pod ) )
6616
- $pod = $this->load_pod( array( 'id' => $pod_id, 'table_info' => false ), false );
6617
-
6618
- if ( !empty( $pod ) && in_array( $pod[ 'type' ], array( 'post_type', 'media', 'user', 'comment', 'settings' ) ) ) {
6619
- $meta_type = $pod[ 'type' ];
6620
-
6621
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'media' ) ) )
6622
- $meta_type = 'post';
6623
-
6624
- $no_conflict = pods_no_conflict_check( $meta_type );
6625
-
6626
- if ( !$no_conflict )
6627
- pods_no_conflict_on( $meta_type );
6628
-
6629
- foreach ( $ids as $id ) {
6630
- if ( 'settings' == $meta_type ) {
6631
- $related_id = get_option( '_pods_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
6632
-
6633
- if ( empty( $related_id ) )
6634
- $related_id = get_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
6635
-
6636
- if ( is_array( $related_id ) && !empty( $related_id ) ) {
6637
- foreach ( $related_id as $related ) {
6638
- if ( is_array( $related ) && !empty( $related ) ) {
6639
- if ( isset( $related[ 'id' ] ) )
6640
- $related_ids[] = (int) $related[ 'id' ];
6641
- else {
6642
- foreach ( $related as $r ) {
6643
- $related_ids[] = (int) $r;
6644
- }
6645
- }
6646
- }
6647
- else
6648
- $related_ids[] = (int) $related;
6649
- }
6650
- }
6651
- }
6652
- else {
6653
- $related_id = get_metadata( $meta_type, $id, '_pods_' . $field[ 'name' ], true );
6654
-
6655
- if ( empty( $related_id ) )
6656
- $related_id = get_metadata( $meta_type, $id, $field[ 'name' ] );
6657
-
6658
- if ( is_array( $related_id ) && !empty( $related_id ) ) {
6659
- foreach ( $related_id as $related ) {
6660
- if ( is_array( $related ) && !empty( $related ) ) {
6661
- if ( isset( $related[ 'id' ] ) )
6662
- $related_ids[] = (int) $related[ 'id' ];
6663
- else {
6664
- foreach ( $related as $r ) {
6665
- if ( isset( $related[ 'id' ] ) )
6666
- $related_ids[] = (int) $r[ 'id' ];
6667
- else
6668
- $related_ids[] = (int) $r;
6669
- }
6670
- }
6671
- }
6672
- else
6673
- $related_ids[] = (int) $related;
6674
- }
6675
- }
6676
- }
6677
- }
6678
-
6679
- if ( !$no_conflict )
6680
- pods_no_conflict_off( $meta_type );
6681
- }
6682
- }
6683
-
6684
- if ( is_array( $related_ids ) ) {
6685
- $related_ids = array_unique( array_filter( $related_ids ) );
6686
-
6687
- if ( 0 < $related_pick_limit && !empty( $related_ids ) )
6688
- $related_ids = array_slice( $related_ids, 0, $related_pick_limit );
6689
- }
6690
-
6691
- return $related_ids;
6692
- }
6693
-
6694
- /**
6695
- * Find related items related to an item
6696
- *
6697
- * @param int $field_id The Field ID
6698
- * @param int $pod_id The Pod ID
6699
- * @param int $id Item ID to get related IDs from
6700
- * @param array $field Field data array
6701
- * @param array $pod Pod data array
6702
- *
6703
- * @return array|bool
6704
- *
6705
- * @since 2.3
6706
- *
6707
- * @uses pods_query()
6708
- */
6709
- public function lookup_related_items_from ( $field_id, $pod_id, $id, $field = null, $pod = null ) {
6710
- $related_ids = false;
6711
-
6712
- $id = (int) $id;
6713
-
6714
- $tableless_field_types = PodsForm::tableless_field_types();
6715
-
6716
- if ( empty( $id ) || !in_array( pods_var( 'type', $field ), $tableless_field_types ) )
6717
- return false;
6718
-
6719
- $related_pick_limit = 0;
6720
-
6721
- if ( !empty( $field ) ) {
6722
- $options = (array) pods_var_raw( 'options', $field, $field, null, true );
6723
-
6724
- $related_pick_limit = (int) pods_var( 'pick_limit', $options, 0 );
6725
-
6726
- if ( 'single' == pods_var_raw( 'pick_format_type', $options ) )
6727
- $related_pick_limit = 1;
6728
- }
6729
-
6730
- if ( !pods_tableless() ) {
6731
- $field_id = (int) $field_id;
6732
- $sister_id = (int) pods_var_raw( 'sister_id', $field, 0 );
6733
-
6734
- $related_where = "
6735
- `field_id` = {$field_id}
6736
- AND `related_item_id` = {$id}
6737
- ";
6738
-
6739
- $sql = "
6740
- SELECT *
6741
- FROM `@wp_podsrel`
6742
- WHERE
6743
- {$related_where}
6744
- ORDER BY `weight`
6745
- ";
6746
-
6747
- $relationships = pods_query( $sql );
6748
-
6749
- if ( !empty( $relationships ) ) {
6750
- $related_ids = array();
6751
-
6752
- foreach ( $relationships as $relation ) {
6753
- if ( $field_id == $relation->field_id && !in_array( $relation->item_id, $related_ids ) )
6754
- $related_ids[] = (int) $relation->item_id;
6755
- elseif ( 0 < $sister_id && $field_id == $relation->related_field_id && !in_array( $relation->related_item_id, $related_ids ) )
6756
- $related_ids[] = (int) $relation->related_item_id;
6757
- }
6758
- }
6759
- }
6760
- else {
6761
- // @todo handle meta-based lookups
6762
- return false;
6763
-
6764
- if ( !is_array( $pod ) )
6765
- $pod = $this->load_pod( array( 'id' => $pod_id, 'table_info' => false ), false );
6766
-
6767
- if ( !empty( $pod ) && in_array( $pod[ 'type' ], array( 'post_type', 'media', 'user', 'comment', 'settings' ) ) ) {
6768
- $related_ids = array();
6769
-
6770
- $meta_type = $pod[ 'type' ];
6771
-
6772
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'media' ) ) )
6773
- $meta_type = 'post';
6774
-
6775
- $no_conflict = pods_no_conflict_check( $meta_type );
6776
-
6777
- if ( !$no_conflict )
6778
- pods_no_conflict_on( $meta_type );
6779
-
6780
- if ( 'settings' == $meta_type ) {
6781
- $related_id = get_option( '_pods_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
6782
-
6783
- if ( empty( $related_id ) )
6784
- $related_id = get_option( $pod[ 'name' ] . '_' . $field[ 'name' ] );
6785
-
6786
- if ( is_array( $related_id ) && !empty( $related_id ) ) {
6787
- foreach ( $related_id as $related ) {
6788
- if ( is_array( $related ) && !empty( $related ) ) {
6789
- if ( isset( $related[ 'id' ] ) )
6790
- $related_ids[] = (int) $related[ 'id' ];
6791
- else {
6792
- foreach ( $related as $r ) {
6793
- $related_ids[] = (int) $r;
6794
- }
6795
- }
6796
- }
6797
- else
6798
- $related_ids[] = (int) $related;
6799
- }
6800
- }
6801
- }
6802
- else {
6803
- $related_id = get_metadata( $meta_type, $id, '_pods_' . $field[ 'name' ], true );
6804
-
6805
- if ( empty( $related_id ) )
6806
- $related_id = get_metadata( $meta_type, $id, $field[ 'name' ] );
6807
-
6808
- if ( is_array( $related_id ) && !empty( $related_id ) ) {
6809
- foreach ( $related_id as $related ) {
6810
- if ( is_array( $related ) && !empty( $related ) ) {
6811
- if ( isset( $related[ 'id' ] ) )
6812
- $related_ids[] = (int) $related[ 'id' ];
6813
- else {
6814
- foreach ( $related as $r ) {
6815
- if ( isset( $related[ 'id' ] ) )
6816
- $related_ids[] = (int) $r[ 'id' ];
6817
- else
6818
- $related_ids[] = (int) $r;
6819
- }
6820
- }
6821
- }
6822
- else
6823
- $related_ids[] = (int) $related;
6824
- }
6825
- }
6826
- }
6827
-
6828
- if ( !$no_conflict )
6829
- pods_no_conflict_off( $meta_type );
6830
- }
6831
- }
6832
-
6833
- if ( is_array( $related_ids ) )
6834
- $related_ids = array_unique( array_filter( $related_ids ) );
6835
-
6836
- return $related_ids;
6837
- }
6838
-
6839
- /**
6840
- * Get information about an objects MySQL table
6841
- *
6842
- * @param string $object_type
6843
- * @param string $object The object to look for
6844
- * @param null $name (optional) Name of the pod to load
6845
- * @param array $pod (optional) Array with pod information
6846
- * @param array $field (optional) Array with field information
6847
- *
6848
- * @return array|bool
6849
- *
6850
- * @since 2.0
6851
- */
6852
- public function get_table_info ( $object_type, $object, $name = null, $pod = null, $field = null ) {
6853
- /**
6854
- * @var $wpdb wpdb
6855
- * @var $sitepress SitePress
6856
- * @var $icl_adjust_id_url_filter_off boolean
6857
- */
6858
- global $wpdb, $sitepress, $icl_adjust_id_url_filter_off, $polylang;
6859
 
6860
- // @todo Handle $object arrays for Post Types, Taxonomies, Comments (table pulled from first object in array)
 
 
6861
 
6862
- $info = array(
6863
- //'select' => '`t`.*',
6864
- 'object_type' => $object_type,
6865
- 'type' => null,
6866
- 'object_name' => $object,
6867
- 'object_hierarchical' => false,
 
 
 
6868
 
6869
- 'table' => $object,
6870
- 'meta_table' => $object,
6871
- 'pod_table' => $wpdb->prefix . 'pods_' . ( empty( $object ) ? $name : $object ),
 
 
 
 
 
 
 
 
 
 
6872
 
6873
- 'field_id' => 'id',
6874
- 'field_index' => 'name',
6875
- 'field_slug' => null,
6876
- 'field_type' => null,
6877
- 'field_parent' => null,
6878
- 'field_parent_select' => null,
6879
 
6880
- 'meta_field_id' => 'id',
6881
- 'meta_field_index' => 'name',
6882
- 'meta_field_value' => 'name',
6883
 
6884
- 'pod_field_id' => 'id',
6885
- 'pod_field_index' => 'name',
6886
- 'pod_field_slug' => null,
6887
- 'pod_field_parent' => null,
 
 
6888
 
6889
- 'join' => array(),
 
 
 
6890
 
6891
- 'where' => null,
6892
- 'where_default' => null,
 
 
 
 
6893
 
6894
- 'orderby' => null,
 
 
6895
 
6896
- 'pod' => null,
6897
- 'recurse' => false
6898
- );
6899
 
6900
- if ( empty( $object_type ) ) {
6901
- $object_type = 'post_type';
6902
- $object = 'post';
6903
- }
6904
 
6905
- $pod_name = $pod;
6906
 
6907
- if ( is_array( $pod_name ) )
6908
- $pod_name = pods_var_raw( 'name', $pod_name, ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $pod_name, JSON_UNESCAPED_UNICODE ) : json_encode( $pod_name ) ), null, true );
6909
 
6910
- $field_name = $field;
 
 
6911
 
6912
- if ( is_array( $field_name ) )
6913
- $field_name = pods_var_raw( 'name', $field_name, ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $pod_name, JSON_UNESCAPED_UNICODE ) : json_encode( $field_name ) ), null, true );
 
6914
 
6915
- $transient = 'pods_' . $wpdb->prefix . '_get_table_info_' . md5( $object_type . '_object_' . $object . '_name_' . $name . '_pod_' . $pod_name . '_field_' . $field_name );
6916
 
6917
- $current_language = false;
6918
- $current_language_t_id = $current_language_tt_id = 0;
 
6919
 
6920
- // WPML support
6921
- if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
6922
- $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
6923
- // Polylang support
6924
- elseif ( is_object( $polylang ) && function_exists( 'pll_current_language' ) ) {
6925
- $current_language = pods_sanitize( pll_current_language( 'slug' ) );
6926
-
6927
- if ( !empty( $current_language ) ) {
6928
- $current_language_t_id = (int) $polylang->get_language( $current_language )->term_id;
6929
- $current_language_tt_id = (int) $polylang->get_language( $current_language )->term_taxonomy_id;
6930
- }
6931
- }
6932
-
6933
- if ( !empty( $current_language ) )
6934
- $transient = 'pods_' . $wpdb->prefix . '_get_table_info_' . $current_language . '_' . md5( $object_type . '_object_' . $object . '_name_' . $name . '_pod_' . $pod_name . '_field_' . $field_name );
6935
 
6936
- $_info = false;
6937
-
6938
- if ( pods_api_cache() )
6939
- $_info = pods_transient_get( $transient );
6940
-
6941
- if ( pods_api_cache() && false === $_info && !did_action( 'init' ) )
6942
- $_info = pods_transient_get( $transient . '_pre_init' );
6943
-
6944
- if ( false !== $_info )
6945
- $info = $_info;
6946
- else {
6947
- if ( 'pod' == $object_type && null === $pod ) {
6948
- if ( empty( $name ) ) {
6949
- $prefix = 'pod-';
6950
-
6951
- // Make sure we actually have the prefix before trying anything with the name
6952
- if ( 0 === strpos( $object_type, $prefix ) )
6953
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
6954
- }
6955
-
6956
- if ( empty( $name ) && !empty( $object ) )
6957
- $name = $object;
6958
-
6959
- $pod = $this->load_pod( array( 'name' => $name, 'table_info' => false ), false );
6960
-
6961
- if ( !empty( $pod ) ) {
6962
- $object_type = $pod[ 'type' ];
6963
- $name = $pod[ 'name' ];
6964
- $object = $pod[ 'object' ];
6965
-
6966
- $info[ 'pod' ] = $pod;
6967
- }
6968
- }
6969
- elseif ( null === $pod ) {
6970
- if ( empty( $name ) ) {
6971
- $prefix = $object_type . '-';
6972
-
6973
- // Make sure we actually have the prefix before trying anything with the name
6974
- if ( 0 === strpos( $object_type, $prefix ) )
6975
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
6976
- }
6977
 
6978
- if ( empty( $name ) && !empty( $object ) )
6979
- $name = $object;
6980
-
6981
- if ( !empty( $name ) ) {
6982
- $pod = $this->load_pod( array( 'name' => $name, 'table_info' => false ), false );
 
 
 
6983
 
6984
- if ( !empty( $pod ) && ( null === $object_type || $object_type == $pod[ 'type' ] ) ) {
6985
- $object_type = $pod[ 'type' ];
6986
- $name = $pod[ 'name' ];
6987
- $object = $pod[ 'object' ];
6988
 
6989
- $info[ 'pod' ] = $pod;
6990
- }
6991
- }
6992
- }
6993
- elseif ( !empty( $pod ) )
6994
- $info[ 'pod' ] = $pod;
6995
 
6996
- if ( 0 === strpos( $object_type, 'pod' ) ) {
6997
- if ( empty( $name ) ) {
6998
- $prefix = 'pod-';
6999
 
7000
- // Make sure we actually have the prefix before trying anything with the name
7001
- if ( 0 === strpos( $object_type, $prefix ) )
7002
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7003
- }
 
 
7004
 
7005
- $object_type = 'pod';
 
 
 
 
7006
 
7007
- $info[ 'table' ] = $info[ 'meta_table' ] = $wpdb->prefix . 'pods_' . ( empty( $object ) ? $name : $object );
 
7008
 
7009
- if ( is_array( $info[ 'pod' ] ) && 'pod' == pods_var( 'type', $info[ 'pod' ] ) ) {
7010
- $info[ 'pod_field_index' ] = $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = pods_var( 'pod_index', $info[ 'pod' ][ 'options' ], 'id', null, true );
7011
-
7012
- $slug_field = get_posts( array(
7013
- 'post_type' => '_pods_field',
7014
- 'posts_per_page' => 1,
7015
- 'nopaging' => true,
7016
- 'post_parent' => $info[ 'pod' ][ 'id' ],
7017
- 'orderby' => 'menu_order',
7018
- 'order' => 'ASC',
7019
- 'meta_query' => array(
7020
- array(
7021
- 'key' => 'type',
7022
- 'value' => 'slug',
7023
- )
7024
- )
7025
- ) );
7026
-
7027
- if ( !empty( $slug_field ) ) {
7028
- $slug_field = $slug_field[ 0 ];
7029
-
7030
- $info[ 'field_slug' ] = $info[ 'pod_field_slug' ] = $slug_field->post_name;
7031
- }
7032
 
7033
- if ( 1 == pods_var( 'hierarchical', $info[ 'pod' ][ 'options' ], 0 ) ) {
7034
- $parent_field = pods_var( 'pod_parent', $info[ 'pod' ][ 'options' ], 'id', null, true );
7035
-
7036
- if ( !empty( $parent_field ) && isset( $info[ 'pod' ][ 'fields' ][ $parent_field ] ) ) {
7037
- $info[ 'object_hierarchical' ] = true;
7038
-
7039
- $info[ 'pod_field_parent' ] = $info[ 'field_parent' ] = $parent_field . '_select';
7040
- $info[ 'field_parent_select' ] = '`' . $parent_field . '`.`id` AS `' . $info[ 'field_parent' ] . '`';
7041
- }
7042
- }
7043
- }
7044
- }
7045
-
7046
- if ( 0 === strpos( $object_type, 'post_type' ) || 'media' == $object_type || in_array( pods_var_raw( 'type', $info[ 'pod' ] ), array( 'post_type', 'media' ) ) ) {
7047
- $info[ 'table' ] = $wpdb->posts;
7048
- $info[ 'meta_table' ] = $wpdb->postmeta;
7049
-
7050
- $info[ 'field_id' ] = 'ID';
7051
- $info[ 'field_index' ] = 'post_title';
7052
- $info[ 'field_slug' ] = 'post_name';
7053
- $info[ 'field_type' ] = 'post_type';
7054
- $info[ 'field_parent' ] = 'post_parent';
7055
- $info[ 'field_parent_select' ] = '`t`.`' . $info[ 'field_parent' ] . '`';
7056
-
7057
- $info[ 'meta_field_id' ] = 'post_id';
7058
- $info[ 'meta_field_index' ] = 'meta_key';
7059
- $info[ 'meta_field_value' ] = 'meta_value';
7060
-
7061
- if ( 'media' == $object_type )
7062
- $object = 'attachment';
7063
-
7064
- if ( empty( $name ) ) {
7065
- $prefix = 'post_type-';
7066
-
7067
- // Make sure we actually have the prefix before trying anything with the name
7068
- if ( 0 === strpos( $object_type, $prefix ) )
7069
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7070
- }
7071
-
7072
- if ( 'media' != $object_type )
7073
- $object_type = 'post_type';
7074
-
7075
- $post_type = pods_sanitize( ( empty( $object ) ? $name : $object ) );
7076
 
7077
- if ( 'attachment' == $post_type || 'media' == $object_type )
7078
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_media';
7079
- else
7080
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . pods_clean_name( $post_type, true, false );
7081
-
7082
- $post_type_object = get_post_type_object( $post_type );
7083
-
7084
- if ( is_object( $post_type_object ) && $post_type_object->hierarchical )
7085
- $info[ 'object_hierarchical' ] = true;
7086
 
7087
- /**
7088
- * Default Post Stati to query for.
7089
- *
7090
- * Use to change "default" post status from publish to any other status or statuses.
7091
- *
7092
- * @param array $post_stati List of post statuses. Default is 'publish'
7093
- * @param string $post_type Post type of current object
7094
- * @param array $info Array of information about the object.
7095
- * @param string $object Type of object
7096
- * @param string $name Name of pod to load
7097
- * @param array $pod Array with Pod information. Result of PodsAPI::load_pod()
7098
- * @param array $field Array with field information
7099
- *
7100
- * @since unknown
7101
- */
7102
- $post_stati = apply_filters( 'pods_api_get_table_info_default_post_status', array( 'publish' ), $post_type, $info, $object_type, $object, $name, $pod, $field );
7103
-
7104
- $info[ 'where' ] = array(
7105
- //'post_status' => '`t`.`post_status` IN ( "inherit", "publish" )', // @todo Figure out what statuses Attachments can be
7106
- 'post_type' => '`t`.`' . $info[ 'field_type' ] . '` = "' . $post_type . '"'
7107
- );
7108
-
7109
- if ( 'post_type' == $object_type )
7110
- $info[ 'where_default' ] = '`t`.`post_status` IN ( "' . implode( '", "', $post_stati ) . '" )';
7111
-
7112
- $info[ 'orderby' ] = '`t`.`menu_order`, `t`.`' . $info[ 'field_index' ] . '`, `t`.`post_date`';
7113
-
7114
- // WPML support
7115
- if ( is_object( $sitepress ) && $sitepress->is_translated_post_type( $post_type ) && !$icl_adjust_id_url_filter_off ) {
7116
- $info[ 'join' ][ 'wpml_translations' ] = "
7117
- LEFT JOIN `{$wpdb->prefix}icl_translations` AS `wpml_translations`
7118
- ON `wpml_translations`.`element_id` = `t`.`ID`
7119
- AND `wpml_translations`.`element_type` = 'post_{$post_type}'
7120
- AND `wpml_translations`.`language_code` = '{$current_language}'
7121
- ";
7122
-
7123
- $info[ 'join' ][ 'wpml_languages' ] = "
7124
- LEFT JOIN `{$wpdb->prefix}icl_languages` AS `wpml_languages`
7125
- ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
7126
- ";
7127
-
7128
- $info[ 'where' ][ 'wpml_languages' ] = "`wpml_languages`.`code` IS NOT NULL";
7129
- }
7130
- // Polylang support
7131
- elseif( is_object( $polylang ) && !empty( $current_language ) && function_exists( 'pll_is_translated_post_type' ) && pll_is_translated_post_type( $post_type ) ) {
7132
- $info[ 'join' ][ 'polylang_languages' ] = "
7133
- LEFT JOIN `{$wpdb->term_relationships}` AS `polylang_languages`
7134
- ON `polylang_languages`.`object_id` = `t`.`ID`
7135
- AND `polylang_languages`.`term_taxonomy_id` = {$current_language_tt_id}
7136
- ";
7137
-
7138
- $info[ 'where' ][ 'polylang_languages' ] = "`polylang_languages`.`object_id` IS NOT NULL";
7139
- }
7140
-
7141
- $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7142
- }
7143
- elseif ( 0 === strpos( $object_type, 'taxonomy' ) || in_array( $object_type, array( 'nav_menu', 'post_format' ) ) || 'taxonomy' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7144
- $info[ 'table' ] = $info[ 'meta_table' ] = $wpdb->terms;
7145
-
7146
- $info[ 'join' ][ 'tt' ] = "LEFT JOIN `{$wpdb->term_taxonomy}` AS `tt` ON `tt`.`term_id` = `t`.`term_id`";
7147
- $info[ 'field_id' ] = $info[ 'meta_field_id' ] = 'term_id';
7148
- $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = 'name';
7149
- $info[ 'field_slug' ] = 'slug';
7150
- $info[ 'field_type' ] = 'taxonomy';
7151
- $info[ 'field_parent' ] = 'parent';
7152
- $info[ 'field_parent_select' ] = '`tt`.`' . $info[ 'field_parent' ] . '`';
7153
-
7154
- if ( 'nav_menu' == $object_type )
7155
- $object = 'nav_menu';
7156
- elseif ( 'post_format' == $object_type )
7157
- $object = 'post_format';
7158
-
7159
- if ( empty( $name ) ) {
7160
- $prefix = 'taxonomy-';
7161
-
7162
- // Make sure we actually have the prefix before trying anything with the name
7163
- if ( 0 === strpos( $object_type, $prefix ) )
7164
- $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
7165
- }
7166
-
7167
- if ( !in_array( $object_type, array( 'nav_menu', 'post_format' ) ) )
7168
- $object_type = 'taxonomy';
7169
-
7170
- $taxonomy = pods_sanitize( ( empty( $object ) ? $name : $object ) );
7171
-
7172
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . pods_clean_name( $taxonomy, true, false );
7173
-
7174
- $taxonomy_object = get_taxonomy( $taxonomy );
7175
-
7176
- if ( is_object( $taxonomy_object ) && $taxonomy_object->hierarchical )
7177
- $info[ 'object_hierarchical' ] = true;
7178
-
7179
- $info[ 'where' ] = array(
7180
- 'tt.taxonomy' => '`tt`.`' . $info[ 'field_type' ] . '` = "' . $taxonomy . '"'
7181
- );
7182
-
7183
- // WPML Support
7184
- if ( is_object( $sitepress ) && $sitepress->is_translated_taxonomy( $taxonomy ) && !$icl_adjust_id_url_filter_off ) {
7185
- $info[ 'join' ][ 'wpml_translations' ] = "
7186
- LEFT JOIN `{$wpdb->prefix}icl_translations` AS `wpml_translations`
7187
- ON `wpml_translations`.`element_id` = `tt`.`term_taxonomy_id`
7188
- AND `wpml_translations`.`element_type` = 'tax_{$taxonomy}'
7189
- AND `wpml_translations`.`language_code` = '{$current_language}'
7190
- ";
7191
-
7192
- $info[ 'join' ][ 'wpml_languages' ] = "
7193
- LEFT JOIN `{$wpdb->prefix}icl_languages` AS `wpml_languages`
7194
- ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
7195
- ";
7196
-
7197
- $info[ 'where' ][ 'wpml_languages' ] = "`wpml_languages`.`code` IS NOT NULL";
7198
- }
7199
- // Polylang support
7200
- elseif ( is_object( $polylang ) && !empty( $current_language ) && function_exists( 'pll_is_translated_taxonomy' ) && pll_is_translated_taxonomy( $taxonomy ) ) {
7201
- $info[ 'join' ][ 'polylang_languages' ] = "
7202
- LEFT JOIN `{$wpdb->termmeta}` AS `polylang_languages`
7203
- ON `polylang_languages`.`term_id` = `t`.`term_id`
7204
- AND `polylang_languages`.`meta_value` = {$current_language_t_id}
7205
- ";
7206
-
7207
- $info[ 'where' ][ 'polylang_languages' ] = "`polylang_languages`.`term_id` IS NOT NULL";
7208
- }
7209
-
7210
- $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7211
- }
7212
- elseif ( 'user' == $object_type || 'user' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7213
- $info[ 'table' ] = $wpdb->users;
7214
- $info[ 'meta_table' ] = $wpdb->usermeta;
7215
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_user';
7216
-
7217
- $info[ 'field_id' ] = 'ID';
7218
- $info[ 'field_index' ] = 'display_name';
7219
- $info[ 'field_slug' ] = 'user_nicename';
7220
-
7221
- $info[ 'meta_field_id' ] = 'user_id';
7222
- $info[ 'meta_field_index' ] = 'meta_key';
7223
- $info[ 'meta_field_value' ] = 'meta_value';
7224
-
7225
- $info[ 'where' ] = array(
7226
- 'user_status' => '`t`.`user_status` = 0'
7227
- );
7228
-
7229
- $info[ 'object_fields' ] = $this->get_wp_object_fields( $object_type, $info[ 'pod' ] );
7230
- }
7231
- elseif ( 'comment' == $object_type || 'comment' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7232
- //$info[ 'object_hierarchical' ] = true;
7233
-
7234
- $info[ 'table' ] = $wpdb->comments;
7235
- $info[ 'meta_table' ] = $wpdb->commentmeta;
7236
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_comment';
7237
-
7238
- $info[ 'field_id' ] = 'comment_ID';
7239
- $info[ 'field_index' ] = 'comment_date';
7240
- $info[ 'field_type' ] = 'comment_type';
7241
- $info[ 'field_parent' ] = 'comment_parent';
7242
- $info[ 'field_parent_select' ] = '`t`.`' . $info[ 'field_parent' ] . '`';
7243
-
7244
- $info[ 'meta_field_id' ] = 'comment_id';
7245
- $info[ 'meta_field_index' ] = 'meta_key';
7246
- $info[ 'meta_field_value' ] = 'meta_value';
7247
-
7248
- $object = 'comment';
7249
-
7250
- $comment_type = ( empty( $object ) ? $name : $object );
7251
-
7252
- $comment_type_clause = '`t`.`' . $info[ 'field_type' ] . '` = "' . $comment_type . '"';
7253
-
7254
- if ( 'comment' == $comment_type ) {
7255
- $comment_type_clause = '( ' . $comment_type_clause . ' OR `t`.`' . $info[ 'field_type' ] . '` = "" )';
7256
- }
7257
-
7258
- $info[ 'where' ] = array(
7259
- 'comment_approved' => '`t`.`comment_approved` = 1',
7260
- 'comment_type' => $comment_type_clause
7261
- );
7262
-
7263
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` DESC, `t`.`' . $info[ 'field_id' ] . '`';
7264
- }
7265
- elseif ( in_array( $object_type, array( 'option', 'settings' ) ) || 'settings' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7266
- $info[ 'table' ] = $wpdb->options;
7267
- $info[ 'meta_table' ] = $wpdb->options;
7268
-
7269
- $info[ 'field_id' ] = 'option_id';
7270
- $info[ 'field_index' ] = 'option_name';
7271
-
7272
- $info[ 'meta_field_id' ] = 'option_id';
7273
- $info[ 'meta_field_index' ] = 'option_name';
7274
- $info[ 'meta_field_value' ] = 'option_value';
7275
-
7276
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC';
7277
- }
7278
- elseif ( is_multisite() && ( in_array( $object_type, array( 'site_option', 'site_settings' ) ) || 'site_settings' == pods_var_raw( 'type', $info[ 'pod' ] ) ) ) {
7279
- $info[ 'table' ] = $wpdb->sitemeta;
7280
- $info[ 'meta_table' ] = $wpdb->sitemeta;
7281
-
7282
- $info[ 'field_id' ] = 'site_id';
7283
- $info[ 'field_index' ] = 'meta_key';
7284
-
7285
- $info[ 'meta_field_id' ] = 'site_id';
7286
- $info[ 'meta_field_index' ] = 'meta_key';
7287
- $info[ 'meta_field_value' ] = 'meta_value';
7288
-
7289
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC';
7290
- }
7291
- elseif ( is_multisite() && 'network' == $object_type ) { // Network = Site
7292
- $info[ 'table' ] = $wpdb->site;
7293
- $info[ 'meta_table' ] = $wpdb->sitemeta;
7294
-
7295
- $info[ 'field_id' ] = 'id';
7296
- $info[ 'field_index' ] = 'domain';
7297
-
7298
- $info[ 'meta_field_id' ] = 'site_id';
7299
- $info[ 'meta_field_index' ] = 'meta_key';
7300
- $info[ 'meta_field_value' ] = 'meta_value';
7301
-
7302
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC, `t`.`path` ASC, `t`.`' . $info[ 'field_id' ] . '`';
7303
- }
7304
- elseif ( is_multisite() && 'site' == $object_type ) { // Site = Blog
7305
- $info[ 'table' ] = $wpdb->blogs;
7306
-
7307
- $info[ 'field_id' ] = 'blog_id';
7308
- $info[ 'field_index' ] = 'domain';
7309
- $info[ 'field_type' ] = 'site_id';
7310
-
7311
- $info[ 'where' ] = array(
7312
- 'archived' => '`t`.`archived` = 0',
7313
- 'spam' => '`t`.`spam` = 0',
7314
- 'deleted' => '`t`.`deleted` = 0',
7315
- 'site_id' => '`t`.`' . $info[ 'field_type' ] . '` = ' . (int) get_current_site()->id
7316
- );
7317
-
7318
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '` ASC, `t`.`path` ASC, `t`.`' . $info[ 'field_id' ] . '`';
7319
- }
7320
- elseif ( 'table' == $object_type || 'table' == pods_var_raw( 'type', $info[ 'pod' ] ) ) {
7321
- $info[ 'table' ] = ( empty( $object ) ? $name : $object );
7322
- $info[ 'pod_table' ] = $wpdb->prefix . 'pods_' . $info[ 'table' ];
7323
-
7324
- if ( !empty( $field ) && is_array( $field ) ) {
7325
- $info[ 'table' ] = pods_var_raw( 'pick_table', pods_var_raw( 'options', $field, $field ) );
7326
- $info[ 'field_id' ] = pods_var_raw( 'pick_table_id', pods_var_raw( 'options', $field, $field ) );
7327
- $info[ 'field_index' ] = $info[ 'meta_field_index' ] = $info[ 'meta_field_value' ] = pods_var_raw( 'pick_table_index', pods_var_raw( 'options', $field, $field ) );
7328
- }
7329
- }
7330
-
7331
- $info[ 'table' ] = pods_clean_name( $info[ 'table' ], false, false );
7332
- $info[ 'meta_table' ] = pods_clean_name( $info[ 'meta_table' ], false, false );
7333
- $info[ 'pod_table' ] = pods_clean_name( $info[ 'pod_table' ], false, false );
7334
-
7335
- $info[ 'field_id' ] = pods_clean_name( $info[ 'field_id' ], false, false );
7336
- $info[ 'field_index' ] = pods_clean_name( $info[ 'field_index' ], false, false );
7337
- $info[ 'field_slug' ] = pods_clean_name( $info[ 'field_slug' ], false, false );
7338
-
7339
- $info[ 'meta_field_id' ] = pods_clean_name( $info[ 'meta_field_id' ], false, false );
7340
- $info[ 'meta_field_index' ] = pods_clean_name( $info[ 'meta_field_index' ], false, false );
7341
- $info[ 'meta_field_value' ] = pods_clean_name( $info[ 'meta_field_value' ], false, false );
7342
-
7343
- if ( empty( $info[ 'orderby' ] ) )
7344
- $info[ 'orderby' ] = '`t`.`' . $info[ 'field_index' ] . '`, `t`.`' . $info[ 'field_id' ] . '`';
7345
-
7346
- if ( 'table' == pods_var_raw( 'storage', $info[ 'pod' ] ) && !in_array( $object_type, array( 'pod', 'table' ) ) ) {
7347
- $info[ 'join' ][ 'd' ] = 'LEFT JOIN `' . $info[ 'pod_table' ] . '` AS `d` ON `d`.`id` = `t`.`' . $info[ 'field_id' ] . '`';
7348
- //$info[ 'select' ] .= ', `d`.*';
7349
- }
7350
-
7351
- if ( !empty( $info[ 'pod' ] ) && is_array( $info[ 'pod' ] ) )
7352
- $info[ 'recurse' ] = true;
7353
-
7354
- $info[ 'type' ] = $object_type;
7355
- $info[ 'object_name' ] = $object;
7356
-
7357
- if ( pods_api_cache() ) {
7358
- if ( did_action( 'init' ) )
7359
- pods_transient_set( $transient, $info );
7360
- else
7361
- pods_transient_set( $transient . '_pre_init', $info );
7362
- }
7363
- }
7364
-
7365
- $info = $this->do_hook( 'get_table_info', $info, $object_type, $object, $name, $pod, $field );
7366
-
7367
- return $info;
7368
- }
7369
-
7370
- /**
7371
- * Export a package
7372
- *
7373
- * $params['pod'] string Pod Type IDs to export
7374
- * $params['template'] string Template IDs to export
7375
- * $params['podpage'] string Pod Page IDs to export
7376
- * $params['helper'] string Helper IDs to export
7377
- *
7378
- * @param array $params An associative array of parameters
7379
- *
7380
- * @return array|bool
7381
- *
7382
- * @since 1.9.0
7383
- * @deprecated 2.0
7384
- */
7385
- public function export_package ( $params ) {
7386
- if ( class_exists( 'Pods_Migrate_Packages' ) )
7387
- return Pods_Migrate_Packages::export( $params );
7388
-
7389
- return false;
7390
- }
7391
-
7392
- /**
7393
- * Replace an existing package
7394
- *
7395
- * @param mixed $data (optional) An associative array containing a package, or the json encoded package
7396
- *
7397
- * @return bool
7398
- *
7399
- * @since 1.9.8
7400
- * @deprecated 2.0
7401
- */
7402
- public function replace_package ( $data = false ) {
7403
- return $this->import_package( $data, true );
7404
- }
7405
-
7406
- /**
7407
- * Import a package
7408
- *
7409
- * @param mixed $data (optional) An associative array containing a package, or the json encoded package
7410
- * @param bool $replace (optional) Replace existing items when found
7411
- *
7412
- * @return bool
7413
- *
7414
- * @since 1.9.0
7415
- * @deprecated 2.0
7416
- */
7417
- public function import_package ( $data = false, $replace = false ) {
7418
- if ( class_exists( 'Pods_Migrate_Packages' ) )
7419
- return Pods_Migrate_Packages::import( $data, $replace );
7420
-
7421
- return false;
7422
- }
7423
-
7424
- /**
7425
- * Validate a package
7426
- *
7427
- * @param array|string $data (optional) An associative array containing a package, or the json encoded package
7428
- * @param bool $output (optional)
7429
- *
7430
- * @return array|bool
7431
- *
7432
- * @since 1.9.0
7433
- * @deprecated 2.0
7434
- */
7435
- public function validate_package ( $data = false, $output = false ) {
7436
- return true;
7437
- }
7438
-
7439
- /**
7440
- * Import data from an array or a CSV file.
7441
- *
7442
- * @param mixed $import_data PHP associative array or CSV input
7443
- * @param bool $numeric_mode Use IDs instead of the name field when matching
7444
- * @param string $format Format of import data, options are php or csv
7445
- *
7446
- * @return array IDs of imported items
7447
- *
7448
- * @since 1.7.1
7449
- * @todo This needs some love and use of table_info etc for relationships
7450
- */
7451
- public function import ( $import_data, $numeric_mode = false, $format = null ) {
7452
- /**
7453
- * @var $wpdb wpdb
7454
- */
7455
- global $wpdb;
7456
-
7457
- if ( null === $format && null !== $this->format )
7458
- $format = $this->format;
7459
-
7460
- if ( 'csv' == $format && !is_array( $import_data ) ) {
7461
- $data = pods_migrate( 'sv', ',' )->parse( $import_data );
7462
-
7463
- $import_data = $data[ 'items' ];
7464
- }
7465
-
7466
- pods_query( "SET NAMES utf8" );
7467
- pods_query( "SET CHARACTER SET utf8" );
7468
-
7469
- // Loop through the array of items
7470
- $ids = array();
7471
-
7472
- // Test to see if it's an array of arrays
7473
- if ( !is_array( @current( $import_data ) ) )
7474
- $import_data = array( $import_data );
7475
-
7476
- $pod = $this->load_pod( array( 'name' => $this->pod ) );
7477
-
7478
- if ( false === $pod )
7479
- return pods_error( __( 'Pod not found', 'pods' ), $this );
7480
-
7481
- $fields = array_merge( $pod[ 'fields' ], $pod[ 'object_fields' ] );
7482
-
7483
- $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
7484
-
7485
- foreach ( $import_data as $key => $data_row ) {
7486
- $data = array();
7487
-
7488
- // Loop through each field (use $fields so only valid fields get parsed)
7489
- foreach ( $fields as $field_name => $field_data ) {
7490
- if ( !isset( $data_row[ $field_name ] ) && !isset( $data_row[ $field_data[ 'label' ] ] ) )
7491
- continue;
7492
-
7493
- $field_id = $field_data[ 'id' ];
7494
- $type = $field_data[ 'type' ];
7495
- $pick_object = isset( $field_data[ 'pick_object' ] ) ? $field_data[ 'pick_object' ] : '';
7496
- $pick_val = isset( $field_data[ 'pick_val' ] ) ? $field_data[ 'pick_val' ] : '';
7497
-
7498
- if ( isset( $data_row[ $field_name] ) )
7499
- $field_value = $data_row[ $field_name ];
7500
- else
7501
- $field_value = $data_row[ $field_data[ 'label' ] ];
7502
-
7503
- if ( null !== $field_value && false !== $field_value && '' !== $field_value ) {
7504
- if ( 'pick' == $type || in_array( $type, PodsForm::file_field_types() ) ) {
7505
- $field_values = is_array( $field_value ) ? $field_value : array( $field_value );
7506
- $pick_values = array();
7507
-
7508
- foreach ( $field_values as $pick_value ) {
7509
- if ( in_array( $type, PodsForm::file_field_types() ) || 'media' == $pick_object ) {
7510
- $where = "`guid` = '" . pods_sanitize( $pick_value ) . "'";
7511
-
7512
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7513
- $where = "`ID` = " . pods_absint( $pick_value );
7514
-
7515
- $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = 'attachment' AND {$where} ORDER BY `ID`", $this );
7516
-
7517
- if ( !empty( $result ) )
7518
- $pick_values[] = $result[ 0 ]->id;
7519
- }
7520
- // @todo This could and should be abstracted better and simplified
7521
- elseif ( 'pick' == $type ) {
7522
- $related_pod = false;
7523
-
7524
- if ( 'pod' == $pick_object )
7525
- $related_pod = $this->load_pod( array( 'name' => $pick_val, 'table_info' => true ), false );
7526
-
7527
- if ( empty( $related_pod ) ) {
7528
- $related_pod = array(
7529
- 'id' => 0,
7530
- 'type' => $pick_object
7531
- );
7532
- }
7533
-
7534
- if ( in_array( 'taxonomy', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7535
- $where = "`t`.`name` = '" . pods_sanitize( $pick_value ) . "'";
7536
-
7537
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7538
- $where = "`tt`.`term_id` = " . pods_absint( $pick_value );
7539
-
7540
- $result = pods_query( "SELECT `t`.`term_id` AS `id` FROM `{$wpdb->term_taxonomy}` AS `tt` LEFT JOIN `{$wpdb->terms}` AS `t` ON `t`.`term_id` = `tt`.`term_id` WHERE `taxonomy` = '{$pick_val}' AND {$where} ORDER BY `t`.`term_id`", $this );
7541
-
7542
- if ( !empty( $result ) )
7543
- $pick_values[] = $result[ 0 ]->id;
7544
- }
7545
- elseif ( in_array( 'post_type', array( $pick_object, $related_pod[ 'type' ] ) ) || in_array( 'media', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7546
- $where = "`post_title` = '" . pods_sanitize( $pick_value ) . "'";
7547
-
7548
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7549
- $where = "`ID` = " . pods_absint( $pick_value );
7550
 
7551
- $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = '{$pick_val}' AND {$where} ORDER BY `ID`", $this );
 
7552
 
7553
- if ( !empty( $result ) )
7554
- $pick_values[] = $result[ 0 ]->id;
7555
- }
7556
- elseif ( in_array( 'user', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7557
- $where = "`user_login` = '" . pods_sanitize( $pick_value ) . "'";
7558
 
7559
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7560
- $where = "`ID` = " . pods_absint( $pick_value );
7561
 
7562
- $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->users}` WHERE {$where} ORDER BY `ID`", $this );
7563
-
7564
- if ( !empty( $result ) )
7565
- $pick_values[] = $result[ 0 ]->id;
7566
- }
7567
- elseif ( in_array( 'comment', array( $pick_object, $related_pod[ 'type' ] ) ) ) {
7568
- $where = "`comment_ID` = " . pods_absint( $pick_value );
7569
-
7570
- $result = pods_query( "SELECT `comment_ID` AS `id` FROM `{$wpdb->comments}` WHERE {$where} ORDER BY `ID`", $this );
7571
-
7572
- if ( !empty( $result ) )
7573
- $pick_values[] = $result[ 0 ]->id;
7574
- }
7575
- elseif ( in_array( $pick_object, $simple_tableless_objects ) )
7576
- $pick_values[] = $pick_value;
7577
- elseif ( !empty( $related_pod[ 'id' ] ) ) {
7578
- $where = "`" . $related_pod[ 'field_index' ] . "` = '" . pods_sanitize( $pick_value ) . "'";
7579
-
7580
- if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode )
7581
- $where = "`" . $related_pod[ 'field_id' ] . "` = " . pods_absint( $pick_value );
7582
-
7583
- $result = pods_query( "SELECT `" . $related_pod[ 'field_id' ] . "` AS `id` FROM `" . $related_pod[ 'table' ] . "` WHERE {$where} ORDER BY `" . $related_pod[ 'field_id' ] . "`", $this );
7584
-
7585
- if ( !empty( $result ) )
7586
- $pick_values[] = $result[ 0 ]->id;
7587
- }
7588
- }
7589
- }
7590
-
7591
- $field_value = implode( ',', $pick_values );
7592
- }
7593
-
7594
- $data[ $field_name ] = $field_value;
7595
- }
7596
- }
7597
-
7598
- if ( !empty( $data ) ) {
7599
- $params = array(
7600
- 'pod' => $this->pod,
7601
- 'data' => $data
7602
- );
7603
-
7604
- $ids[] = $this->save_pod_item( $params );
7605
- }
7606
- }
7607
-
7608
- return $ids;
7609
- }
7610
-
7611
- /**
7612
- * Export data from a Pod
7613
- *
7614
- * @param string|object $pod The pod name or Pods object
7615
- * @param array $params An associative array of parameters
7616
- *
7617
- * @return array Data arrays of all exported pod items
7618
- * @since 1.7.1
7619
- */
7620
- public function export ( $pod = null, $params = null ) {
7621
-
7622
- if ( empty( $pod ) ) {
7623
- $pod = $this->pod;
7624
- }
7625
-
7626
- $find = array(
7627
- 'limit' => -1,
7628
- 'search' => false,
7629
- 'pagination' => false
7630
- );
7631
-
7632
- if ( !empty( $params ) && isset( $params[ 'params' ] ) ) {
7633
- $find = array_merge( $find, (array) $params[ 'params' ] );
7634
-
7635
- unset( $params[ 'params' ] );
7636
-
7637
- $pod = pods( $pod, $find );
7638
- }
7639
- elseif ( !is_object( $pod ) ) {
7640
- $pod = pods( $pod, $find );
7641
- }
7642
-
7643
- $data = array();
7644
-
7645
- while ( $pod->fetch() ) {
7646
- $data[ $pod->id() ] = $this->export_pod_item( $params, $pod );
7647
- $data[ $pod->id() ][ 'ID' ] = (int) $pod->id();
7648
- }
7649
-
7650
- $data = $this->do_hook( 'export', $data, $pod->pod, $pod );
7651
-
7652
- return $data;
7653
- }
7654
-
7655
- /**
7656
- * Convert CSV to a PHP array
7657
- *
7658
- * @param string $data The CSV input
7659
- *
7660
- * @return array
7661
- * @since 1.7.1
7662
- *
7663
- * @deprecated 2.3.5
7664
- */
7665
- public function csv_to_php ( $data, $delimiter = ',' ) {
7666
- pods_deprecated( "PodsAPI->csv_to_php", '2.3.5' );
7667
-
7668
- $data = pods_migrate( 'sv', $delimiter, $data )->parse();
7669
-
7670
- return $data[ 'items' ];
7671
- }
7672
-
7673
- /**
7674
- * Clear Pod-related cache
7675
- *
7676
- * @param array $pod
7677
- *
7678
- * @return void
7679
- *
7680
- * @since 2.0
7681
- */
7682
- public function cache_flush_pods ( $pod = null ) {
7683
- /**
7684
- * @var $wpdb wpdb
7685
- */
7686
- global $wpdb;
7687
-
7688
- pods_transient_clear( 'pods' );
7689
- pods_transient_clear( 'pods_components' );
7690
-
7691
- if ( null !== $pod && is_array( $pod ) ) {
7692
- pods_transient_clear( 'pods_pod_' . $pod[ 'name' ] );
7693
- pods_cache_clear( $pod[ 'name' ], 'pods-class' );
7694
-
7695
- foreach ( $pod[ 'fields' ] as $field ) {
7696
- pods_transient_clear( 'pods_field_' . $pod[ 'name' ] . '_' . $field[ 'name' ] );
7697
- }
7698
-
7699
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) )
7700
- pods_transient_clear( 'pods_wp_cpt_ct' );
7701
- }
7702
- else
7703
- pods_transient_clear( 'pods_wp_cpt_ct' );
7704
-
7705
- $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_pods%'" );
7706
- $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_timeout_pods%'" );
7707
-
7708
- pods_cache_clear( true );
7709
-
7710
- pods_transient_set( 'pods_flush_rewrites', 1 );
7711
- }
7712
-
7713
- /**
7714
- * Process a Pod-based form
7715
- *
7716
- * @param mixed $params
7717
- * @param object $obj Pod object
7718
- * @param array $fields Fields being submitted in form ( key => settings )
7719
- * @param string $thank_you URL to send to upon success
7720
- *
7721
- * @return mixed
7722
- *
7723
- * @since 2.0
7724
- */
7725
- public function process_form ( $params, $obj = null, $fields = null, $thank_you = null ) {
7726
- $this->display_errors = false;
7727
-
7728
- $form = null;
7729
-
7730
- $nonce = pods_var( '_pods_nonce', $params );
7731
- $pod = pods_var( '_pods_pod', $params );
7732
- $id = pods_var( '_pods_id', $params );
7733
- $uri = pods_var( '_pods_uri', $params );
7734
- $form = pods_var( '_pods_form', $params );
7735
- $location = pods_var( '_pods_location', $params );
7736
-
7737
- if ( is_object( $obj ) ) {
7738
- $pod = $obj->pod;
7739
- $id = $obj->id();
7740
- }
7741
-
7742
- if ( !empty( $fields ) ) {
7743
- $fields = array_keys( $fields );
7744
- $form = implode( ',', $fields );
7745
- }
7746
- else
7747
- $fields = explode( ',', $form );
7748
-
7749
- if ( empty( $nonce ) || empty( $pod ) || empty( $uri ) || empty( $fields ) )
7750
- return pods_error( __( 'Invalid submission', 'pods' ), $this );
7751
-
7752
- $uid = @session_id();
7753
-
7754
- if ( is_user_logged_in() )
7755
- $uid = 'user_' . get_current_user_id();
7756
 
7757
- $field_hash = wp_create_nonce( 'pods_fields_' . $form );
7758
 
7759
- $action = 'pods_form_' . $pod . '_' . $uid . '_' . $id . '_' . $uri . '_' . $field_hash;
 
 
 
7760
 
7761
- if ( empty( $uid ) )
7762
- return pods_error( __( 'Access denied for your session, please refresh and try again.', 'pods' ), $this );
 
 
 
 
7763
 
7764
- if ( false === wp_verify_nonce( $nonce, $action ) )
7765
- return pods_error( __( 'Access denied, please refresh and try again.', 'pods' ), $this );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7766
 
7767
- $data = array();
7768
 
7769
- foreach ( $fields as $field ) {
7770
- $data[ $field ] = pods_var_raw( 'pods_field_' . $field, $params, '' );
7771
- }
7772
 
7773
- $params = array(
7774
- 'pod' => $pod,
7775
- 'id' => $id,
7776
- 'data' => $data,
7777
- 'from' => 'process_form',
7778
- 'location' => $location
7779
- );
7780
-
7781
- $id = $this->save_pod_item( $params );
7782
-
7783
- if ( 0 < $id && !empty( $thank_you ) ) {
7784
- $thank_you = str_replace( 'X_ID_X', $id, $thank_you );
7785
-
7786
- pods_redirect( $thank_you );
7787
- }
7788
-
7789
- return $id;
7790
- }
7791
-
7792
- /**
7793
- * Handle filters / actions for the class
7794
- *
7795
- * @since 2.0
7796
- */
7797
- private function do_hook () {
7798
- $args = func_get_args();
7799
- if ( empty( $args ) )
7800
- return false;
7801
- $name = array_shift( $args );
7802
- return pods_do_hook( "api", $name, $args, $this );
7803
- }
7804
-
7805
- /**
7806
- * Handle variables that have been deprecated
7807
- *
7808
- * @since 2.0
7809
- */
7810
- public function __get ( $name ) {
7811
- $name = (string) $name;
7812
-
7813
- if ( !isset( $this->deprecated ) ) {
7814
- require_once( PODS_DIR . 'deprecated/classes/PodsAPI.php' );
7815
- $this->deprecated = new PodsAPI_Deprecated( $this );
7816
- }
7817
-
7818
- $var = null;
7819
-
7820
- if ( isset( $this->deprecated->{$name} ) ) {
7821
- pods_deprecated( "PodsAPI->{$name}", '2.0' );
7822
-
7823
- $var = $this->deprecated->{$name};
7824
- }
7825
- else
7826
- pods_deprecated( "PodsAPI->{$name}", '2.0' );
7827
-
7828
- return $var;
7829
- }
7830
-
7831
- /**
7832
- * Handle methods that have been deprecated
7833
- *
7834
- * @since 2.0
7835
- */
7836
- public function __call ( $name, $args ) {
7837
- $name = (string) $name;
7838
-
7839
- if ( !isset( $this->deprecated ) ) {
7840
- require_once( PODS_DIR . 'deprecated/classes/PodsAPI.php' );
7841
- $this->deprecated = new PodsAPI_Deprecated( $this );
7842
- }
7843
-
7844
- if ( method_exists( $this->deprecated, $name ) )
7845
- return call_user_func_array( array( $this->deprecated, $name ), $args );
7846
- else
7847
- pods_deprecated( "PodsAPI::{$name}", '2.0' );
7848
- }
7849
  }
1
  <?php
2
+
3
  /**
4
  * @package Pods
5
  */
6
  class PodsAPI {
7
 
8
+ /**
9
+ * @var PodsAPI
10
+ */
11
+ static $instance = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ /**
14
+ * @var array PodsAPI
15
+ */
16
+ static $instances = array();
17
 
18
+ /**
19
+ * @var bool
20
+ */
21
+ public $display_errors = false;
22
 
23
+ /**
24
+ * @var array|bool|mixed|null|void
25
+ */
26
+ public $pod_data;
27
 
28
+ /**
29
+ * @var
30
+ */
31
+ public $pod;
 
 
 
 
 
32
 
33
+ /**
34
+ * @var
35
+ */
36
+ public $pod_id;
 
 
37
 
38
+ /**
39
+ * @var
40
+ */
41
+ public $fields;
42
 
43
+ /**
44
+ * @var
45
+ * @deprecated 2.0.0
46
+ */
47
+ public $format = null;
48
 
49
+ /**
50
+ * @var
51
+ */
52
+ private $deprecated;
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ /**
55
+ * @var array
56
+ * @since 2.5.0
57
+ */
58
+ private $fields_cache = array();
59
 
60
+ /**
61
+ * @var array
62
+ * @since 2.5.0
63
+ *
64
+ */
65
+ private static $table_info_cache = array();
66
 
67
+ /**
68
+ * @var array
69
+ * @since 2.5.0
70
+ *
71
+ */
72
+ private static $related_item_cache = array();
73
 
74
+ /**
75
+ * Singleton-ish handling for a basic pods_api() request
76
+ *
77
+ * @param string $pod (optional) The pod name
78
+ * @param string $format (deprecated) Format for import/export, "php" or "csv"
79
+ *
80
+ * @return \PodsAPI
81
+ *
82
+ * @since 2.3.5
83
+ */
84
+ public static function init( $pod = null, $format = null ) {
85
 
86
+ if ( null !== $pod || null !== $format ) {
87
+ if ( ! isset( self::$instances[ $pod ] ) ) {
88
+ // Cache API singleton per Pod
89
+ self::$instances[ $pod ] = new PodsAPI( $pod, $format );
90
+ }
91
 
92
+ return self::$instances[ $pod ];
93
+ } elseif ( ! is_object( self::$instance ) ) {
94
+ self::$instance = new PodsAPI();
95
  }
96
 
97
+ return self::$instance;
98
+ }
 
 
 
 
 
 
99
 
100
+ /**
101
+ * Store and retrieve data programatically
102
+ *
103
+ * @param string $pod (optional) The pod name
104
+ * @param string $format (deprecated) Format for import/export, "php" or "csv"
105
+ *
106
+ * @return \PodsAPI
107
+ *
108
+ * @license http://www.gnu.org/licenses/gpl-2.0.html
109
+ * @since 1.7.1
110
+ */
111
+ public function __construct( $pod = null, $format = null ) {
112
 
113
+ if ( null !== $pod && 0 < strlen( (string) $pod ) ) {
114
+ if ( null !== $format ) {
115
+ $this->format = $format;
116
 
117
+ pods_deprecated( 'pods_api( $pod, $format )', '2.0', 'pods_api( $pod )' );
118
+ }
119
 
120
+ $pod = pods_clean_name( $pod );
 
121
 
122
+ $pod = $this->load_pod( array( 'name' => $pod, 'table_info' => true ), false );
 
123
 
124
+ if ( ! empty( $pod ) ) {
125
+ $this->pod_data = $pod;
126
+ $this->pod = $pod['name'];
127
+ $this->pod_id = $pod['id'];
128
+ $this->fields = $pod['fields'];
129
+ }
130
+ }
131
+ }
 
 
 
 
 
 
 
132
 
133
+ /**
134
+ * Save a WP object and its meta
135
+ *
136
+ * @param string $object_type Object type: post|taxonomy|user|comment|setting
137
+ * @param array $data All post data to be saved
138
+ * @param array $meta (optional) Associative array of meta keys and values
139
+ * @param bool $strict (optional) Decides whether the previous saved meta should be deleted or not
140
+ * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before
141
+ * sending.
142
+ * @param array $fields (optional) The array of fields and their options, for further processing with
143
+ *
144
+ * @return bool|mixed
145
+ *
146
+ * @since 2.0.0
147
+ */
148
+ public function save_wp_object( $object_type, $data, $meta = array(), $strict = false, $sanitized = false, $fields = array() ) {
149
 
150
+ if ( in_array( $object_type, array( 'post_type', 'media' ) ) ) {
151
+ $object_type = 'post';
152
+ }
153
 
154
+ if ( 'taxonomy' === $object_type ) {
155
+ $object_type = 'term';
156
+ }
157
 
158
+ if ( $sanitized ) {
159
+ $data = pods_unsanitize( $data );
160
+ $meta = pods_unsanitize( $meta );
161
+ }
162
 
163
+ if ( in_array( $object_type, array( 'post', 'term', 'user', 'comment' ) ) ) {
164
+ return call_user_func( array( $this, 'save_' . $object_type ), $data, $meta, $strict, false, $fields );
165
+ } elseif ( 'settings' === $object_type ) {
166
+ // Nothing to save
167
+ if ( empty( $meta ) ) {
168
+ return true;
169
+ }
170
 
171
+ return $this->save_setting( pods_var( 'option_id', $data ), $meta, false );
172
+ }
 
173
 
174
+ return false;
175
+ }
176
 
177
+ /**
178
+ * Delete a WP object
179
+ *
180
+ * @param string $object_type Object type: post|user|comment
181
+ * @param int $id Object ID
182
+ * @param bool $force_delete (optional) Force deletion instead of trashing (post types only)
183
+ *
184
+ * @return bool|mixed
185
+ *
186
+ * @since 2.0.0
187
+ */
188
+ public function delete_wp_object( $object_type, $id, $force_delete = true ) {
189
 
190
+ if ( in_array( $object_type, array( 'post_type', 'media' ) ) ) {
191
+ $object_type = 'post';
192
+ }
193
 
194
+ if ( 'taxonomy' === $object_type ) {
195
+ $object_type = 'term';
196
+ }
197
 
198
+ if ( empty( $id ) ) {
199
+ return false;
200
+ }
201
 
202
+ if ( in_array( $object_type, array( 'post' ) ) ) {
203
+ return wp_delete_post( $id, $force_delete );
204
+ }
 
 
 
 
 
 
205
 
206
+ if ( function_exists( 'wp_delete_' . $object_type ) ) {
207
+ return call_user_func( 'wp_delete_' . $object_type, $id );
208
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
+ return false;
211
+ }
212
 
213
+ /**
214
+ * Save a post and it's meta
215
+ *
216
+ * @param array $post_data All post data to be saved (using wp_insert_post / wp_update_post)
217
+ * @param array $post_meta (optional) All meta to be saved (set value to null to delete)
218
+ * @param bool $strict (optional) Whether to delete previously saved meta not in $post_meta
219
+ * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before
220
+ * sending.
221
+ * @param array $fields (optional) The array of fields and their options, for further processing with
222
+ *
223
+ * @return mixed|void
224
+ *
225
+ * @since 2.0.0
226
+ */
227
+ public function save_post( $post_data, $post_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
228
 
229
+ $conflicted = pods_no_conflict_check( 'post' );
 
230
 
231
+ if ( ! $conflicted ) {
232
+ pods_no_conflict_on( 'post' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  }
234
 
235
+ if ( ! is_array( $post_data ) || empty( $post_data ) ) {
236
+ $post_data = array( 'post_title' => '' );
237
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
+ if ( ! is_array( $post_meta ) ) {
240
+ $post_meta = array();
241
+ }
242
 
243
+ if ( $sanitized ) {
244
+ $post_data = pods_unsanitize( $post_data );
245
+ $post_meta = pods_unsanitize( $post_meta );
246
  }
247
 
248
+ if ( ! isset( $post_data['ID'] ) || empty( $post_data['ID'] ) ) {
249
+ $post_data['ID'] = wp_insert_post( $post_data, true );
250
+ } elseif ( 2 < count( $post_data ) || ! isset( $post_data['post_type'] ) ) {
251
+ $post_data['ID'] = wp_update_post( $post_data, true );
252
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
254
+ if ( is_wp_error( $post_data['ID'] ) ) {
255
+ if ( ! $conflicted ) {
256
+ pods_no_conflict_off( 'post' );
257
+ }
258
 
259
+ /**
260
+ * @var $post_error WP_Error
261
+ */
262
+ $post_error = $post_data['ID'];
 
263
 
264
+ return pods_error( $post_error->get_error_message(), $this );
 
265
  }
266
 
267
+ $this->save_post_meta( $post_data['ID'], $post_meta, $strict, $fields );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
+ if ( ! $conflicted ) {
270
+ pods_no_conflict_off( 'post' );
271
+ }
 
 
 
 
272
 
273
+ return $post_data['ID'];
274
+ }
 
 
275
 
276
+ /**
277
+ * Save a post's meta
278
+ *
279
+ * @param int $id Post ID
280
+ * @param array $post_meta All meta to be saved (set value to null to delete)
281
+ * @param bool $strict Whether to delete previously saved meta not in $post_meta
282
+ * @param array $fields (optional) The array of fields and their options, for further processing with
283
+ *
284
+ * @return int Id of the post with the meta
285
+ *
286
+ * @since 2.0.0
287
+ */
288
+ public function save_post_meta( $id, $post_meta = null, $strict = false, $fields = array() ) {
289
 
290
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
 
291
 
292
+ $conflicted = pods_no_conflict_check( 'post' );
 
293
 
294
+ if ( ! $conflicted ) {
295
+ pods_no_conflict_on( 'post' );
296
+ }
297
 
298
+ if ( ! is_array( $post_meta ) ) {
299
+ $post_meta = array();
300
+ }
301
 
302
+ $id = (int) $id;
303
 
304
+ $meta = get_post_meta( $id );
305
 
306
+ foreach ( $meta as $k => $value ) {
307
+ if ( is_array( $value ) && 1 == count( $value ) ) {
308
+ $meta[ $k ] = current( $value );
309
+ }
310
+ }
 
 
 
311
 
312
+ foreach ( $post_meta as $meta_key => $meta_value ) {
313
+ if ( null === $meta_value || ( $strict && '' === $post_meta[ $meta_key ] ) ) {
314
+ $old_meta_value = '';
315
 
316
+ if ( isset( $meta[ $meta_key ] ) ) {
317
+ $old_meta_value = $meta[ $meta_key ];
318
+ }
319
 
320
+ delete_post_meta( $id, $meta_key, $old_meta_value );
321
+ } else {
322
+ $simple = false;
 
 
 
 
323
 
324
+ if ( isset( $fields[ $meta_key ] ) ) {
325
+ $field_data = $fields[ $meta_key ];
326
 
327
+ $simple = ( 'pick' === pods_v( 'type', $field_data ) && in_array( pods_v( 'pick_object', $field_data ), $simple_tableless_objects ) );
328
+ }
 
329
 
330
+ if ( $simple ) {
331
+ delete_post_meta( $id, $meta_key );
 
 
 
 
332
 
333
+ update_post_meta( $id, '_pods_' . $meta_key, $meta_value );
 
334
 
335
+ if ( ! is_array( $meta_value ) ) {
336
+ $meta_value = array( $meta_value );
337
+ }
338
 
339
+ foreach ( $meta_value as $value ) {
340
+ add_post_meta( $id, $meta_key, $value );
341
+ }
342
+ } else {
343
+ update_post_meta( $id, $meta_key, $meta_value );
344
+ }
345
+ }
346
+ }
347
 
348
+ if ( $strict ) {
349
+ foreach ( $meta as $meta_key => $meta_value ) {
350
+ if ( ! isset( $post_meta[ $meta_key ] ) ) {
351
+ delete_post_meta( $id, $meta_key, $meta_value );
352
+ }
353
+ }
354
+ }
355
 
356
+ if ( ! $conflicted ) {
357
+ pods_no_conflict_off( 'post' );
358
+ }
359
 
360
+ return $id;
361
+ }
362
 
363
+ /**
364
+ * Save a user and it's meta
365
+ *
366
+ * @param array $user_data All user data to be saved (using wp_insert_user / wp_update_user)
367
+ * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
368
+ * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
369
+ * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before
370
+ * sending.
371
+ * @param array $fields (optional) The array of fields and their options, for further processing with
372
+ *
373
+ * @return int Returns user id on success
374
+ *
375
+ * @since 2.0.0
376
+ */
377
+ public function save_user( $user_data, $user_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
378
 
379
+ if ( ! is_array( $user_data ) || empty( $user_data ) ) {
380
+ return pods_error( __( 'User data is required but is either invalid or empty', 'pods' ), $this );
381
+ }
382
 
383
+ $conflicted = pods_no_conflict_check( 'user' );
 
 
384
 
385
+ if ( ! $conflicted ) {
386
+ pods_no_conflict_on( 'user' );
387
+ }
 
 
388
 
389
+ if ( ! is_array( $user_meta ) ) {
390
+ $user_meta = array();
391
+ }
392
 
393
+ if ( $sanitized ) {
394
+ $user_data = pods_unsanitize( $user_data );
395
+ $user_meta = pods_unsanitize( $user_meta );
396
+ }
397
 
398
+ // Set role
399
+ if ( isset( $user_meta['role'] ) ) {
400
+ $user_data['role'] = $user_meta['role'];
401
 
402
+ unset( $user_meta['role'] );
403
+ }
 
 
 
404
 
405
+ if ( ! isset( $user_data['ID'] ) || empty( $user_data['ID'] ) ) {
406
+ $user_data['ID'] = wp_insert_user( $user_data );
407
+ } elseif ( 1 < count( $user_data ) ) {
408
+ wp_update_user( $user_data );
409
+ }
410
 
411
+ if ( is_wp_error( $user_data['ID'] ) ) {
412
+ if ( ! $conflicted ) {
413
+ pods_no_conflict_off( 'user' );
414
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415
 
416
+ /**
417
+ * @var $user_error WP_Error
418
+ */
419
+ $user_error = $user_data['ID'];
420
 
421
+ return pods_error( $user_error->get_error_message(), $this );
422
+ }
423
 
424
+ $this->save_user_meta( $user_data['ID'], $user_meta, $strict, $fields );
 
 
425
 
426
+ if ( ! $conflicted ) {
427
+ pods_no_conflict_off( 'user' );
428
+ }
429
 
430
+ return $user_data['ID'];
431
+ }
432
 
433
+ /**
434
+ * Save a user meta
435
+ *
436
+ * @param int $id User ID
437
+ * @param array $user_meta (optional) All meta to be saved (set value to null to delete)
438
+ * @param bool $strict (optional) Whether to delete previously saved meta not in $user_meta
439
+ * @param array $fields (optional) The array of fields and their options, for further processing with
440
+ *
441
+ * @return int User ID
442
+ *
443
+ * @since 2.0.0
444
+ *
445
+ */
446
+ public function save_user_meta( $id, $user_meta = null, $strict = false, $fields = array() ) {
447
 
448
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
 
449
 
450
+ $conflicted = pods_no_conflict_check( 'user' );
451
 
452
+ if ( ! $conflicted ) {
453
+ pods_no_conflict_on( 'user' );
454
+ }
455
 
456
+ if ( ! is_array( $user_meta ) ) {
457
+ $user_meta = array();
458
+ }
459
 
460
+ $id = (int) $id;
 
461
 
462
+ $meta = get_user_meta( $id );
 
 
 
463
 
464
+ foreach ( $user_meta as $meta_key => $meta_value ) {
465
+ if ( null === $meta_value ) {
466
+ $old_meta_value = '';
467
 
468
+ if ( isset( $meta[ $meta_key ] ) ) {
469
+ $old_meta_value = $meta[ $meta_key ];
470
+ }
471
 
472
+ delete_user_meta( $id, $meta_key, $old_meta_value );
473
+ } else {
474
+ $simple = false;
475
 
476
+ if ( isset( $fields[ $meta_key ] ) ) {
477
+ $field_data = $fields[ $meta_key ];
 
478
 
479
+ $simple = ( 'pick' === $field_data['type'] && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
480
+ }
481
 
482
+ if ( $simple ) {
483
+ delete_user_meta( $id, $meta_key );
484
 
485
+ if ( ! is_array( $meta_value ) ) {
486
+ $meta_value = array( $meta_value );
487
+ }
 
 
 
488
 
489
+ foreach ( $meta_value as $value ) {
490
+ add_user_meta( $id, $meta_key, $value );
491
+ }
492
+ } else {
493
+ update_user_meta( $id, $meta_key, $meta_value );
494
+ }
495
+ }
496
+ }
497
 
498
+ if ( $strict ) {
499
+ foreach ( $meta as $meta_key => $meta_value ) {
500
+ if ( ! isset( $user_meta[ $meta_key ] ) ) {
501
+ delete_user_meta( $id, $meta_key, $user_meta[ $meta_key ] );
502
+ }
503
+ }
504
+ }
505
 
506
+ if ( ! $conflicted ) {
507
+ pods_no_conflict_off( 'user' );
508
+ }
509
 
510
+ return $id;
511
+ }
512
 
513
+ /**
514
+ * Save a comment and it's meta
515
+ *
516
+ * @param array $comment_data All comment data to be saved (using wp_insert_comment / wp_update_comment)
517
+ * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
518
+ * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
519
+ * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before
520
+ * sending.
521
+ * @param array $fields (optional) The array of fields and their options, for further processing with
522
+ *
523
+ * @return int Comment ID
524
+ *
525
+ * @since 2.0.0
526
+ */
527
+ public function save_comment( $comment_data, $comment_meta = null, $strict = false, $sanitized = false, $fields = array() ) {
528
 
529
+ if ( ! is_array( $comment_data ) || empty( $comment_data ) ) {
530
+ return pods_error( __( 'Comment data is required but is either invalid or empty', 'pods' ), $this );
531
  }
532
 
533
+ $conflicted = pods_no_conflict_check( 'comment' );
 
 
 
534
 
535
+ if ( ! $conflicted ) {
536
+ pods_no_conflict_on( 'comment' );
537
+ }
538
 
539
+ if ( ! is_array( $comment_meta ) ) {
540
+ $comment_meta = array();
541
+ }
542
 
543
+ if ( $sanitized ) {
544
+ $comment_data = pods_unsanitize( $comment_data );
545
+ $comment_meta = pods_unsanitize( $comment_meta );
546
+ }
547
 
548
+ if ( ! isset( $comment_data['comment_ID'] ) || empty( $comment_data['comment_ID'] ) ) {
549
+ $comment_data['comment_ID'] = wp_insert_comment( pods_slash( $comment_data ) );
550
+ } elseif ( 1 < count( $comment_data ) ) {
551
+ // Expects slashed
552
+ wp_update_comment( $comment_data );
553
+ }
554
 
555
+ if ( is_wp_error( $comment_data['comment_ID'] ) ) {
556
+ if ( ! $conflicted ) {
557
+ pods_no_conflict_off( 'comment' );
558
+ }
559
 
560
+ /**
561
+ * @var $comment_error WP_Error
562
+ */
563
+ $comment_error = $comment_data['comment_ID'];
564
 
565
+ return pods_error( $comment_error->get_error_message(), $this );
566
+ }
567
 
568
+ $this->save_comment_meta( $comment_data['comment_ID'], $comment_meta, $strict, $fields );
 
569
 
570
+ if ( ! $conflicted ) {
571
+ pods_no_conflict_off( 'comment' );
572
+ }
573
 
574
+ return $comment_data['comment_ID'];
575
+ }
576
 
577
+ /**
578
+ * Save a comment meta
579
+ *
580
+ * @param int $id Comment ID
581
+ * @param array $comment_meta (optional) All meta to be saved (set value to null to delete)
582
+ * @param bool $strict (optional) Whether to delete previously saved meta not in $comment_meta
583
+ * @param array $fields (optional) The array of fields and their options, for further processing with
584
+ *
585
+ * @return int Comment ID
586
+ *
587
+ * @since 2.0.0
588
+ */
589
+ public function save_comment_meta( $id, $comment_meta = null, $strict = false, $fields = array() ) {
590
 
591
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
 
592
 
593
+ $conflicted = pods_no_conflict_check( 'comment' );
 
594
 
595
+ if ( ! $conflicted ) {
596
+ pods_no_conflict_on( 'comment' );
597
+ }
598
 
599
+ if ( ! is_array( $comment_meta ) ) {
600
+ $comment_meta = array();
601
+ }
602
 
603
+ $id = (int) $id;
604
 
605
+ $meta = get_comment_meta( $id );
 
606
 
607
+ foreach ( $comment_meta as $meta_key => $meta_value ) {
608
+ if ( null === $meta_value ) {
609
+ $old_meta_value = '';
610
 
611
+ if ( isset( $meta[ $meta_key ] ) ) {
612
+ $old_meta_value = $meta[ $meta_key ];
613
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
614
 
615
+ delete_comment_meta( $id, $meta_key, $old_meta_value );
616
+ } else {
617
+ $simple = false;
 
618
 
619
+ if ( isset( $fields[ $meta_key ] ) ) {
620
+ $field_data = $fields[ $meta_key ];
621
 
622
+ $simple = ( 'pick' === pods_v( 'type', $field_data ) && in_array( pods_v( 'pick_object', $field_data ), $simple_tableless_objects ) );
623
+ }
 
624
 
625
+ if ( $simple ) {
626
+ delete_comment_meta( $id, $meta_key );
 
 
627
 
628
+ if ( ! is_array( $meta_value ) ) {
629
+ $meta_value = array( $meta_value );
630
+ }
 
 
631
 
632
+ foreach ( $meta_value as $value ) {
633
+ add_comment_meta( $id, $meta_key, $value );
634
+ }
635
+ } else {
636
+ update_comment_meta( $id, $meta_key, $meta_value );
637
+ }
638
  }
639
+ }
640
 
641
+ if ( $strict ) {
642
+ foreach ( $meta as $meta_key => $meta_value ) {
643
+ if ( ! isset( $comment_meta[ $meta_key ] ) ) {
644
+ delete_comment_meta( (int) $id, $meta_key, $comment_meta[ $meta_key ] );
645
+ }
646
+ }
647
  }
648
 
649
+ if ( ! $conflicted ) {
650
+ pods_no_conflict_off( 'comment' );
 
651
  }
652
 
653
+ return $id;
654
+ }
655
+
656
+ /**
657
+ * Save a taxonomy's term
658
+ *
659
+ * @param array $term_data All term data to be saved (using wp_insert_term / wp_update_term)
660
+ * @param array $term_meta All meta to be saved (set value to null to delete)
661
+ * @param bool $strict (optional) Whether to delete previously saved meta not in $post_meta
662
+ * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before
663
+ * sending.
664
+ * @param array $fields (optional) The array of fields and their options, for further processing with
665
+ *
666
+ * @return int Term ID
667
+ *
668
+ * @since 2.0.0
669
+ */
670
+ public function save_term( $term_data, $term_meta, $strict = false, $sanitized = false, $fields = array() ) {
671
+
672
+ if ( empty( $term_data['taxonomy'] ) ) {
673
+ return 0;
674
+ }
675
+
676
+ $conflicted = pods_no_conflict_check( 'taxonomy' );
677
+
678
+ if ( ! is_array( $term_data ) || empty( $term_data ) ) {
679
+ $term_data = array( 'name' => '' );
680
+ }
681
+
682
+ if ( ! $conflicted ) {
683
+ pods_no_conflict_on( 'taxonomy' );
684
+ }
685
+
686
+ if ( ! is_array( $term_meta ) ) {
687
+ $term_meta = array();
688
+ }
689
+
690
+ if ( $sanitized ) {
691
+ $term_data = pods_unsanitize( $term_data );
692
+ $term_meta = pods_unsanitize( $term_meta );
693
+ }
694
+
695
+ $taxonomy = $term_data['taxonomy'];
696
+
697
+ unset( $term_data['taxonomy'] );
698
+
699
+ if ( empty( $term_data['term_id'] ) ) {
700
+ $term_name = $term_data['name'];
701
+
702
+ unset( $term_data['name'] );
703
+
704
+ $term_data['term_id'] = wp_insert_term( $term_name, $taxonomy, $term_data );
705
+ } elseif ( 1 < count( $term_data ) ) {
706
+ $term_data['term_id'] = wp_update_term( $term_data['term_id'], $taxonomy, $term_data );
707
+ }
708
+
709
+ if ( is_wp_error( $term_data['term_id'] ) ) {
710
+ if ( ! $conflicted ) {
711
+ pods_no_conflict_off( 'taxonomy' );
712
+ }
713
+
714
+ /**
715
+ * @var $term_error WP_Error
716
+ */
717
+ $term_error = $term_data['term_id'];
718
+
719
+ return pods_error( $term_error->get_error_message(), $this );
720
+ } elseif ( is_array( $term_data['term_id'] ) ) {
721
+ $term_data['term_id'] = $term_data['term_id']['term_id'];
722
+ }
723
+
724
+ $this->save_term_meta( $term_data['term_id'], $term_meta, $strict, $fields );
725
+
726
+ if ( ! $conflicted ) {
727
+ pods_no_conflict_off( 'taxonomy' );
728
+ }
729
+
730
+ return $term_data['term_id'];
731
+ }
732
+
733
+ /**
734
+ * Save a term's meta
735
+ *
736
+ * @param int $id Term ID
737
+ * @param array $term_meta All meta to be saved (set value to null to delete)
738
+ * @param bool $strict Whether to delete previously saved meta not in $term_meta
739
+ * @param array $fields (optional) The array of fields and their options, for further processing with
740
+ *
741
+ * @return int Id of the term with the meta
742
+ *
743
+ * @since 2.0.0
744
+ */
745
+ public function save_term_meta( $id, $term_meta = null, $strict = false, $fields = array() ) {
746
+
747
+ if ( ! function_exists( 'get_term_meta' ) ) {
748
+ return $id;
749
+ }
750
+
751
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
752
+
753
+ $conflicted = pods_no_conflict_check( 'taxonomy' );
754
+
755
+ if ( ! $conflicted ) {
756
+ pods_no_conflict_on( 'taxonomy' );
757
+ }
758
+
759
+ if ( ! is_array( $term_meta ) ) {
760
+ $term_meta = array();
761
+ }
762
+
763
+ $id = (int) $id;
764
+
765
+ $meta = get_term_meta( $id );
766
+
767
+ foreach ( $meta as $k => $value ) {
768
+ if ( is_array( $value ) && 1 == count( $value ) ) {
769
+ $meta[ $k ] = current( $value );
770
+ }
771
+ }
772
+
773
+ foreach ( $term_meta as $meta_key => $meta_value ) {
774
+ if ( null === $meta_value || ( $strict && '' === $term_meta[ $meta_key ] ) ) {
775
+ $old_meta_value = '';
776
+
777
+ if ( isset( $meta[ $meta_key ] ) ) {
778
+ $old_meta_value = $meta[ $meta_key ];
779
+ }
780
+
781
+ delete_term_meta( $id, $meta_key, $old_meta_value );
782
+ } else {
783
+ $simple = false;
784
+
785
+ if ( isset( $fields[ $meta_key ] ) ) {
786
+ $field_data = $fields[ $meta_key ];
787
+
788
+ $simple = ( 'pick' === pods_v( 'type', $field_data ) && in_array( pods_v( 'pick_object', $field_data ), $simple_tableless_objects ) );
789
+ }
790
+
791
+ if ( $simple ) {
792
+ delete_term_meta( $id, $meta_key );
793
+
794
+ update_term_meta( $id, '_pods_' . $meta_key, $meta_value );
795
+
796
+ if ( ! is_array( $meta_value ) ) {
797
+ $meta_value = array( $meta_value );
798
+ }
799
+
800
+ foreach ( $meta_value as $value ) {
801
+ add_term_meta( $id, $meta_key, $value );
802
+ }
803
+ } else {
804
+ update_term_meta( $id, $meta_key, $meta_value );
805
+ }
806
+ }
807
+ }
808
+
809
+ if ( $strict ) {
810
+ foreach ( $meta as $meta_key => $meta_value ) {
811
+ if ( ! isset( $term_meta[ $meta_key ] ) ) {
812
+ delete_term_meta( $id, $meta_key, $meta_value );
813
+ }
814
+ }
815
+ }
816
+
817
+ if ( ! $conflicted ) {
818
+ pods_no_conflict_off( 'taxonomy' );
819
+ }
820
+
821
+ return $id;
822
+ }
823
+
824
+ /**
825
+ * Save a set of options
826
+ *
827
+ * @param string $setting Setting group name
828
+ * @param array $option_data All option data to be saved
829
+ * @param bool $sanitized (optional) Will unsanitize the data, should be passed if the data is sanitized before
830
+ * sending.
831
+ *
832
+ * @return bool
833
+ *
834
+ * @since 2.3.0
835
+ */
836
+ public function save_setting( $setting, $option_data, $sanitized = false ) {
837
+
838
+ if ( ! is_array( $option_data ) || empty( $option_data ) ) {
839
+ return pods_error( __( 'Setting data is required but is either invalid or empty', 'pods' ), $this );
840
+ }
841
+
842
+ $conflicted = pods_no_conflict_check( 'settings' );
843
+
844
+ if ( ! $conflicted ) {
845
+ pods_no_conflict_on( 'settings' );
846
+ }
847
+
848
+ if ( $sanitized ) {
849
+ $option_data = pods_unsanitize( $option_data );
850
+ }
851
+
852
+ foreach ( $option_data as $option => $value ) {
853
+ if ( ! empty( $setting ) ) {
854
+ $option = $setting . '_' . $option;
855
+ }
856
+
857
+ update_option( $option, $value );
858
+ }
859
+
860
+ if ( ! $conflicted ) {
861
+ pods_no_conflict_off( 'settings' );
862
+ }
863
+
864
+ return true;
865
+ }
866
+
867
+ /**
868
+ * Rename a WP object's type
869
+ *
870
+ * @param string $object_type Object type: post|taxonomy|comment|setting
871
+ * @param string $old_name The old name
872
+ * @param string $new_name The new name
873
+ *
874
+ * @return bool
875
+ *
876
+ * @since 2.0.0
877
+ */
878
+ public function rename_wp_object_type( $object_type, $old_name, $new_name ) {
879
+
880
+ /**
881
+ * @var $wpdb wpdb
882
+ */
883
+ global $wpdb;
884
+
885
+ if ( 'post_type' === $object_type ) {
886
+ $object_type = 'post';
887
+ }
888
+
889
+ if ( 'post' === $object_type ) {
890
+ pods_query( "UPDATE `{$wpdb->posts}` SET `post_type` = %s WHERE `post_type` = %s", array(
891
+ $new_name,
892
+ $old_name
893
+ ) );
894
+ } elseif ( 'taxonomy' === $object_type ) {
895
+ pods_query( "UPDATE `{$wpdb->term_taxonomy}` SET `taxonomy` = %s WHERE `taxonomy` = %s", array(
896
+ $new_name,
897
+ $old_name
898
+ ) );
899
+ } elseif ( 'comment' === $object_type ) {
900
+ pods_query( "UPDATE `{$wpdb->comments}` SET `comment_type` = %s WHERE `comment_type` = %s", array(
901
+ $new_name,
902
+ $old_name
903
+ ) );
904
+ } elseif ( 'settings' === $object_type ) {
905
+ pods_query( "UPDATE `{$wpdb->options}` SET `option_name` = REPLACE( `option_name`, %s, %s ) WHERE `option_name` LIKE '" . pods_sanitize_like( $old_name ) . "_%'", array(
906
+ $new_name . '_',
907
+ $old_name . '_'
908
+ ) );
909
+ }
910
+
911
+ return true;
912
+ }
913
+
914
+ /**
915
+ * Get a list of core WP object fields for a specific object
916
+ *
917
+ * @param string $object The pod type to look for, possible values: post_type, user, comment, taxonomy
918
+ * @param array $pod Array of Pod data
919
+ * @param boolean $refresh Whether to force refresh the information
920
+ *
921
+ * @return array Array of fields
922
+ *
923
+ * @since 2.0.0
924
+ */
925
+ public function get_wp_object_fields( $object = 'post_type', $pod = null, $refresh = false ) {
926
+
927
+ $pod_name = pods_var_raw( 'name', $pod, $object, null, true );
928
+
929
+ if ( 'media' === $pod_name ) {
930
+ $object = 'post_type';
931
+ $pod_name = 'attachment';
932
+ }
933
+
934
+ $fields = false;
935
+
936
+ if ( pods_api_cache() ) {
937
+ $fields = pods_transient_get( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ) );
938
+ }
939
+
940
+ if ( false !== $fields && ! $refresh ) {
941
+ return $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
942
+ }
943
+
944
+ $fields = array();
945
+
946
+ if ( 'post_type' === $object ) {
947
+ $fields = array(
948
+ 'ID' => array(
949
+ 'name' => 'ID',
950
+ 'label' => 'ID',
951
+ 'type' => 'number',
952
+ 'alias' => array( 'id' ),
953
+ 'options' => array(
954
+ 'number_format' => '9999.99'
955
+ )
956
+ ),
957
+ 'post_title' => array(
958
+ 'name' => 'post_title',
959
+ 'label' => 'Title',
960
+ 'type' => 'text',
961
+ 'alias' => array( 'title', 'name' ),
962
+ 'options' => array(
963
+ 'display_filter' => 'the_title',
964
+ 'display_filter_args' => array( 'post_ID' )
965
+ )
966
+ ),
967
+ 'post_content' => array(
968
+ 'name' => 'post_content',
969
+ 'label' => 'Content',
970
+ 'type' => 'wysiwyg',
971
+ 'alias' => array( 'content' ),
972
+ 'options' => array(
973
+ 'wysiwyg_allowed_html_tags' => '',
974
+ 'display_filter' => 'the_content',
975
+ 'pre_save' => 0
976
+ )
977
+ ),
978
+ 'post_excerpt' => array(
979
+ 'name' => 'post_excerpt',
980
+ 'label' => 'Excerpt',
981
+ 'type' => 'paragraph',
982
+ 'alias' => array( 'excerpt' ),
983
+ 'options' => array(
984
+ 'paragraph_allow_html' => 1,
985
+ 'paragraph_allowed_html_tags' => '',
986
+ 'display_filter' => 'the_excerpt',
987
+ 'pre_save' => 0
988
+ )
989
+ ),
990
+ 'post_author' => array(
991
+ 'name' => 'post_author',
992
+ 'label' => 'Author',
993
+ 'type' => 'pick',
994
+ 'alias' => array( 'author' ),
995
+ 'pick_object' => 'user',
996
+ 'options' => array(
997
+ 'pick_format_type' => 'single',
998
+ 'pick_format_single' => 'autocomplete',
999
+ 'default_value' => '{@user.ID}'
1000
+ )
1001
+ ),
1002
+ 'post_date' => array(
1003
+ 'name' => 'post_date',
1004
+ 'label' => 'Publish Date',
1005
+ 'type' => 'datetime',
1006
+ 'alias' => array( 'created', 'date' )
1007
+ ),
1008
+ 'post_date_gmt' => array(
1009
+ 'name' => 'post_date_gmt',
1010
+ 'label' => 'Publish Date (GMT)',
1011
+ 'type' => 'datetime',
1012
+ 'alias' => array(),
1013
+ 'hidden' => true
1014
+ ),
1015
+ 'post_status' => array(
1016
+ 'name' => 'post_status',
1017
+ 'label' => 'Status',
1018
+ 'type' => 'pick',
1019
+ 'pick_object' => 'post-status',
1020
+ 'default' => $this->do_hook( 'default_status_' . $pod_name, pods_var( 'default_status', pods_var_raw( 'options', $pod ), 'draft', null, true ), $pod ),
1021
+ 'alias' => array( 'status' )
1022
+ ),
1023
+ 'comment_status' => array(
1024
+ 'name' => 'comment_status',
1025
+ 'label' => 'Comment Status',
1026
+ 'type' => 'text',
1027
+ 'default' => get_option( 'default_comment_status', 'open' ),
1028
+ 'alias' => array(),
1029
+ 'data' => array(
1030
+ 'open' => __( 'Open', 'pods' ),
1031
+ 'closed' => __( 'Closed', 'pods' )
1032
+ )
1033
+ ),
1034
+ 'ping_status' => array(
1035
+ 'name' => 'ping_status',
1036
+ 'label' => 'Ping Status',
1037
+ 'default' => get_option( 'default_ping_status', 'open' ),
1038
+ 'type' => 'text',
1039
+ 'alias' => array(),
1040
+ 'data' => array(
1041
+ 'open' => __( 'Open', 'pods' ),
1042
+ 'closed' => __( 'Closed', 'pods' )
1043
+ )
1044
+ ),
1045
+ 'post_password' => array(
1046
+ 'name' => 'post_password',
1047
+ 'label' => 'Password',
1048
+ 'type' => 'text',
1049
+ 'alias' => array()
1050
+ ),
1051
+ 'post_name' => array(
1052
+ 'name' => 'post_name',
1053
+ 'label' => 'Permalink',
1054
+ 'type' => 'slug',
1055
+ 'alias' => array( 'slug', 'permalink' )
1056
+ ),
1057
+ 'to_ping' => array(
1058
+ 'name' => 'to_ping',
1059
+ 'label' => 'To Ping',
1060
+ 'type' => 'text',
1061
+ 'alias' => array(),
1062
+ 'hidden' => true
1063
+ ),
1064
+ 'pinged' => array(
1065
+ 'name' => 'pinged',
1066
+ 'label' => 'Pinged',
1067
+ 'type' => 'text',
1068
+ 'alias' => array(),
1069
+ 'hidden' => true
1070
+ ),
1071
+ 'post_modified' => array(
1072
+ 'name' => 'post_modified',
1073
+ 'label' => 'Last Modified Date',
1074
+ 'type' => 'datetime',
1075
+ 'alias' => array( 'modified' ),
1076
+ 'hidden' => true
1077
+ ),
1078
+ 'post_modified_gmt' => array(
1079
+ 'name' => 'post_modified_gmt',
1080
+ 'label' => 'Last Modified Date (GMT)',
1081
+ 'type' => 'datetime',
1082
+ 'alias' => array(),
1083
+ 'hidden' => true
1084
+ ),
1085
+ 'post_content_filtered' => array(
1086
+ 'name' => 'post_content_filtered',
1087
+ 'label' => 'Content (filtered)',
1088
+ 'type' => 'paragraph',
1089
+ 'alias' => array(),
1090
+ 'hidden' => true,
1091
+ 'options' => array(
1092
+ 'paragraph_allow_html' => 1,
1093
+ 'paragraph_oembed' => 1,
1094
+ 'paragraph_wptexturize' => 1,
1095
+ 'paragraph_convert_chars' => 1,
1096
+ 'paragraph_wpautop' => 1,
1097
+ 'paragraph_allow_shortcode' => 1,
1098
+ 'paragraph_allowed_html_tags' => ''
1099
+ )
1100
+ ),
1101
+ 'post_parent' => array(
1102
+ 'name' => 'post_parent',
1103
+ 'label' => 'Parent',
1104
+ 'type' => 'pick',
1105
+ 'pick_object' => 'post_type',
1106
+ 'pick_val' => '__current__',
1107
+ 'alias' => array( 'parent' ),
1108
+ 'data' => array(),
1109
+ 'hidden' => true
1110
+ ),
1111
+ 'guid' => array(
1112
+ 'name' => 'guid',
1113
+ 'label' => 'GUID',
1114
+ 'type' => 'text',
1115
+ 'alias' => array(),
1116
+ 'hidden' => true
1117
+ ),
1118
+ 'menu_order' => array(
1119
+ 'name' => 'menu_order',
1120
+ 'label' => 'Menu Order',
1121
+ 'type' => 'number',
1122
+ 'alias' => array(),
1123
+ 'options' => array(
1124
+ 'number_format' => '9999.99'
1125
+ )
1126
+ ),
1127
+ 'post_type' => array(
1128
+ 'name' => 'post_type',
1129
+ 'label' => 'Type',
1130
+ 'type' => 'text',
1131
+ 'alias' => array( 'type' ),
1132
+ 'hidden' => true
1133
+ ),
1134
+ 'post_mime_type' => array(
1135
+ 'name' => 'post_mime_type',
1136
+ 'label' => 'Mime Type',
1137
+ 'type' => 'text',
1138
+ 'alias' => array(),
1139
+ 'hidden' => true
1140
+ ),
1141
+ 'comment_count' => array(
1142
+ 'name' => 'comment_count',
1143
+ 'label' => 'Comment Count',
1144
+ 'type' => 'number',
1145
+ 'alias' => array(),
1146
+ 'hidden' => true
1147
+ ),
1148
+ 'comments' => array(
1149
+ 'name' => 'comments',
1150
+ 'label' => 'Comments',
1151
+ 'type' => 'comment',
1152
+ 'pick_object' => 'comment',
1153
+ 'pick_val' => 'comment',
1154
+ 'alias' => array(),
1155
+ 'hidden' => true,
1156
+ 'options' => array(
1157
+ 'comment_format_type' => 'multi'
1158
+ )
1159
+ )
1160
+ );
1161
+
1162
+ if ( ! empty( $pod ) ) {
1163
+ $taxonomies = get_object_taxonomies( $pod_name, 'objects' );
1164
+
1165
+ foreach ( $taxonomies as $taxonomy ) {
1166
+ $fields[ $taxonomy->name ] = array(
1167
+ 'name' => $taxonomy->name,
1168
+ 'label' => $taxonomy->labels->name,
1169
+ 'type' => 'taxonomy',
1170
+ 'pick_object' => 'taxonomy',
1171
+ 'pick_val' => $taxonomy->name,
1172
+ 'alias' => array(),
1173
+ 'hidden' => true,
1174
+ 'options' => array(
1175
+ 'taxonomy_format_type' => 'multi'
1176
+ )
1177
+ );
1178
+ }
1179
+ }
1180
+ } elseif ( 'user' === $object ) {
1181
+ $fields = array(
1182
+ 'ID' => array(
1183
+ 'name' => 'ID',
1184
+ 'label' => 'ID',
1185
+ 'type' => 'number',
1186
+ 'alias' => array( 'id' ),
1187
+ 'options' => array(
1188
+ 'number_format' => '9999.99'
1189
+ )
1190
+ ),
1191
+ 'user_login' => array(
1192
+ 'name' => 'user_login',
1193
+ 'label' => 'Title',
1194
+ 'type' => 'text',
1195
+ 'alias' => array( 'login' ),
1196
+ 'options' => array(
1197
+ 'required' => 1
1198
+ )
1199
+ ),
1200
+ 'user_nicename' => array(
1201
+ 'name' => 'user_nicename',
1202
+ 'label' => 'Permalink',
1203
+ 'type' => 'slug',
1204
+ 'alias' => array( 'nicename', 'slug', 'permalink' )
1205
+ ),
1206
+ 'display_name' => array(
1207
+ 'name' => 'display_name',
1208
+ 'label' => 'Display Name',
1209
+ 'type' => 'text',
1210
+ 'alias' => array( 'title', 'name' )
1211
+ ),
1212
+ 'user_pass' => array(
1213
+ 'name' => 'user_pass',
1214
+ 'label' => 'Password',
1215
+ 'type' => 'text',
1216
+ 'alias' => array( 'password', 'pass' ),
1217
+ 'options' => array(
1218
+ 'required' => 1,
1219
+ 'text_format_type' => 'password'
1220
+ )
1221
+ ),
1222
+ 'user_email' => array(
1223
+ 'name' => 'user_email',
1224
+ 'label' => 'E-mail',
1225
+ 'type' => 'text',
1226
+ 'alias' => array( 'email' ),
1227
+ 'options' => array(
1228
+ 'required' => 1,
1229
+ 'text_format_type' => 'email'
1230
+ )
1231
+ ),
1232
+ 'user_url' => array(
1233
+ 'name' => 'user_url',
1234
+ 'label' => 'URL',
1235
+ 'type' => 'text',
1236
+ 'alias' => array( 'url', 'website' ),
1237
+ 'options' => array(
1238
+ 'required' => 0,
1239
+ 'text_format_type' => 'website',
1240
+ 'text_format_website' => 'normal'
1241
+ )
1242
+ ),
1243
+ 'user_registered' => array(
1244
+ 'name' => 'user_registered',
1245
+ 'label' => 'Registration Date',
1246
+ 'type' => 'date',
1247
+ 'alias' => array( 'created', 'date', 'registered' ),
1248
+ 'options' => array(
1249
+ 'date_format_type' => 'datetime'
1250
+ )
1251
+ )
1252
+ );
1253
+ } elseif ( 'comment' === $object ) {
1254
+ $fields = array(
1255
+ 'comment_ID' => array(
1256
+ 'name' => 'comment_ID',
1257
+ 'label' => 'ID',
1258
+ 'type' => 'number',
1259
+ 'alias' => array( 'id', 'ID', 'comment_id' ),
1260
+ 'options' => array(
1261
+ 'number_format' => '9999.99'
1262
+ )
1263
+ ),
1264
+ 'comment_content' => array(
1265
+ 'name' => 'comment_content',
1266
+ 'label' => 'Content',
1267
+ 'type' => 'wysiwyg',
1268
+ 'alias' => array( 'content' )
1269
+ ),
1270
+ 'comment_approved' => array(
1271
+ 'name' => 'comment_approved',
1272
+ 'label' => 'Approved',
1273
+ 'type' => 'number',
1274
+ 'alias' => array( 'approved' ),
1275
+ 'options' => array(
1276
+ 'number_format' => '9999.99'
1277
+ )
1278
+ ),
1279
+ 'comment_post_ID' => array(
1280
+ 'name' => 'comment_post_ID',
1281
+ 'label' => 'Post',
1282
+ 'type' => 'pick',
1283
+ 'alias' => array( 'post', 'post_id' ),
1284
+ 'data' => array()
1285
+ ),
1286
+ 'user_id' => array(
1287
+ 'name' => 'user_id',
1288
+ 'label' => 'Author',
1289
+ 'type' => 'pick',
1290
+ 'alias' => array( 'author' ),
1291
+ 'pick_object' => 'user',
1292
+ 'data' => array()
1293
+ ),
1294
+ 'comment_date' => array(
1295
+ 'name' => 'comment_date',
1296
+ 'label' => 'Date',
1297
+ 'type' => 'date',
1298
+ 'alias' => array( 'created', 'date' ),
1299
+ 'options' => array(
1300
+ 'date_format_type' => 'datetime'
1301
+ )
1302
+ ),
1303
+ 'comment_author' => array(
1304
+ 'name' => 'comment_author',
1305
+ 'label' => 'Author',
1306
+ 'type' => 'text',
1307
+ 'alias' => array( 'author' )
1308
+ ),
1309
+ 'comment_author_email' => array(
1310
+ 'name' => 'comment_author_email',
1311
+ 'label' => 'Author E-mail',
1312
+ 'type' => 'email',
1313
+ 'alias' => array( 'author_email' )
1314
+ ),
1315
+ 'comment_author_url' => array(
1316
+ 'name' => 'comment_author_url',
1317
+ 'label' => 'Author URL',
1318
+ 'type' => 'text',
1319
+ 'alias' => array( 'author_url' )
1320
+ ),
1321
+ 'comment_author_IP' => array(
1322
+ 'name' => 'comment_author_IP',
1323
+ 'label' => 'Author IP',
1324
+ 'type' => 'text',
1325
+ 'alias' => array( 'author_IP' )
1326
+ ),
1327
+ 'comment_type' => array(
1328
+ 'name' => 'comment_type',
1329
+ 'label' => 'Type',
1330
+ 'type' => 'text',
1331
+ 'alias' => array( 'type' ),
1332
+ 'hidden' => true
1333
+ ),
1334
+ 'comment_parent' => array(
1335
+ 'name' => 'comment_parent',
1336
+ 'label' => 'Parent',
1337
+ 'type' => 'pick',
1338
+ 'pick_object' => 'comment',
1339
+ 'pick_val' => '__current__',
1340
+ 'alias' => array( 'parent' ),
1341
+ 'data' => array(),
1342
+ 'hidden' => true
1343
+ )
1344
+ );
1345
+ } elseif ( 'taxonomy' === $object ) {
1346
+ $fields = array(
1347
+ 'term_id' => array(
1348
+ 'name' => 'term_id',
1349
+ 'label' => 'ID',
1350
+ 'type' => 'number',
1351
+ 'alias' => array( 'id', 'ID' ),
1352
+ 'options' => array(
1353
+ 'number_format' => '9999.99'
1354
+ )
1355
+ ),
1356
+ 'name' => array(
1357
+ 'name' => 'name',
1358
+ 'label' => 'Title',
1359
+ 'type' => 'text',
1360
+ 'alias' => array( 'title' )
1361
+ ),
1362
+ 'slug' => array(
1363
+ 'name' => 'slug',
1364
+ 'label' => 'Permalink',
1365
+ 'type' => 'slug',
1366
+ 'alias' => array( 'permalink' )
1367
+ ),
1368
+ 'description' => array(
1369
+ 'name' => 'description',
1370
+ 'label' => 'Description',
1371
+ 'type' => 'wysiwyg',
1372
+ 'alias' => array( 'content' )
1373
+ ),
1374
+ 'taxonomy' => array(
1375
+ 'name' => 'taxonomy',
1376
+ 'label' => 'Taxonomy',
1377
+ 'type' => 'text',
1378
+ 'alias' => array()
1379
+ ),
1380
+ 'parent' => array(
1381
+ 'name' => 'parent',
1382
+ 'label' => 'Parent',
1383
+ 'type' => 'pick',
1384
+ 'pick_object' => 'taxonomy',
1385
+ 'pick_val' => '__current__',
1386
+ 'alias' => array( 'parent' ),
1387
+ 'data' => array(),
1388
+ 'hidden' => true
1389
+ ),
1390
+ 'term_taxonomy_id' => array(
1391
+ 'name' => 'term_taxonomy_id',
1392
+ 'label' => 'Term Taxonomy ID',
1393
+ 'type' => 'number',
1394
+ 'alias' => array(),
1395
+ 'hidden' => true,
1396
+ 'options' => array(
1397
+ 'number_format' => '9999.99'
1398
+ )
1399
+ ),
1400
+ 'term_group' => array(
1401
+ 'name' => 'term_group',
1402
+ 'label' => 'Term Group',
1403
+ 'type' => 'number',
1404
+ 'alias' => array( 'group' ),
1405
+ 'hidden' => true,
1406
+ 'options' => array(
1407
+ 'number_format' => '9999.99'
1408
+ )
1409
+ ),
1410
+ 'count' => array(
1411
+ 'name' => 'count',
1412
+ 'label' => 'Count',
1413
+ 'type' => 'number',
1414
+ 'alias' => array(),
1415
+ 'hidden' => true,
1416
+ 'options' => array(
1417
+ 'number_format' => '9999.99'
1418
+ )
1419
+ )
1420
+ );
1421
+ }
1422
+
1423
+ $fields = $this->do_hook( 'get_wp_object_fields', $fields, $object, $pod );
1424
+
1425
+ foreach ( $fields as $field => $options ) {
1426
+ if ( ! isset( $options['alias'] ) ) {
1427
+ $options['alias'] = array();
1428
+ } else {
1429
+ $options['alias'] = (array) $options['alias'];
1430
+ }
1431
+
1432
+ if ( ! isset( $options['name'] ) ) {
1433
+ $options['name'] = $field;
1434
+ }
1435
+
1436
+ $fields[ $field ] = $options;
1437
+ }
1438
+
1439
+ $fields = PodsForm::fields_setup( $fields );
1440
+
1441
+ if ( did_action( 'init' ) && pods_api_cache() ) {
1442
+ pods_transient_set( trim( 'pods_api_object_fields_' . $object . $pod_name . '_', '_' ), $fields );
1443
+ }
1444
+
1445
+ return $fields;
1446
+ }
1447
+
1448
+ /**
1449
+ *
1450
+ * @see PodsAPI::save_pod
1451
+ *
1452
+ * Add a Pod via the Wizard
1453
+ *
1454
+ * $params['create_extend'] string Create or Extend a Content Type
1455
+ * $params['create_pod_type'] string Pod Type (for Creating)
1456
+ * $params['create_name'] string Pod Name (for Creating)
1457
+ * $params['create_label_plural'] string Plural Label (for Creating)
1458
+ * $params['create_label_singular'] string Singular Label (for Creating)
1459
+ * $params['create_storage'] string Storage Type (for Creating Post Types)
1460
+ * $params['create_storage_taxonomy'] string Storage Type (for Creating Taxonomies)
1461
+ * $params['extend_pod_type'] string Pod Type (for Extending)
1462
+ * $params['extend_post_type'] string Post Type (for Extending Post Types)
1463
+ * $params['extend_taxonomy'] string Taxonomy (for Extending Taxonomies)
1464
+ * $params['extend_storage'] string Storage Type (for Extending Post Types / Users / Comments)
1465
+ *
1466
+ * @param array $params An associative array of parameters
1467
+ *
1468
+ * @return bool|int Pod ID
1469
+ * @since 2.0.0
1470
+ */
1471
+ public function add_pod( $params ) {
1472
+
1473
+ $defaults = array(
1474
+ 'create_extend' => 'create',
1475
+ 'create_pod_type' => 'post_type',
1476
+
1477
+ 'create_name' => '',
1478
+ 'create_label_singular' => '',
1479
+ 'create_label_plural' => '',
1480
+ 'create_storage' => 'meta',
1481
+ 'create_storage_taxonomy' => '',
1482
+
1483
+ 'create_setting_name' => '',
1484
+ 'create_label_title' => '',
1485
+ 'create_label_menu' => '',
1486
+ 'create_menu_location' => 'settings',
1487
+
1488
+ 'extend_pod_type' => 'post_type',
1489
+ 'extend_post_type' => 'post',
1490
+ 'extend_taxonomy' => 'category',
1491
+ 'extend_table' => '',
1492
+ 'extend_storage' => 'meta',
1493
+ 'extend_storage_taxonomy' => '',
1494
+ );
1495
+
1496
+ if( !function_exists( 'get_term_meta' ) ) {
1497
+ $defaults['create_storage_taxonomy'] = 'none';
1498
+ $defaults['extend_storage_taxonomy' ] = 'table' ;
1499
+ }
1500
+
1501
+ $params = (object) array_merge( $defaults, (array) $params );
1502
+
1503
+ if ( empty( $params->create_extend ) || ! in_array( $params->create_extend, array( 'create', 'extend' ) ) ) {
1504
+ return pods_error( __( 'Please choose whether to Create or Extend a Content Type', 'pods' ), $this );
1505
+ }
1506
+
1507
+ $pod_params = array(
1508
+ 'name' => '',
1509
+ 'label' => '',
1510
+ 'type' => '',
1511
+ 'storage' => 'table',
1512
+ 'object' => '',
1513
+ 'options' => array(),
1514
+ 'create_extend' => $params->create_extend,
1515
+ );
1516
+
1517
+ if ( 'create' === $params->create_extend ) {
1518
+ $label = ucwords( str_replace( '_', ' ', $params->create_name ) );
1519
+
1520
+ if ( ! empty( $params->create_label_singular ) ) {
1521
+ $label = $params->create_label_singular;
1522
+ }
1523
+
1524
+ $pod_params['name'] = $params->create_name;
1525
+ $pod_params['label'] = ( ! empty( $params->create_label_plural ) ? $params->create_label_plural : $label );
1526
+ $pod_params['type'] = $params->create_pod_type;
1527
+ $pod_params['options'] = array(
1528
+ 'label_singular' => ( ! empty( $params->create_label_singular ) ? $params->create_label_singular : $pod_params['label'] ),
1529
+ 'public' => 1,
1530
+ 'show_ui' => 1
1531
+ );
1532
+
1533
+ // Auto-generate name if not provided
1534
+ if ( empty( $pod_params['name'] ) && ! empty( $pod_params['options']['label_singular'] ) ) {
1535
+ $pod_params['name'] = pods_clean_name( $pod_params['options']['label_singular'] );
1536
+ }
1537
+
1538
+ if ( 'post_type' === $pod_params['type'] ) {
1539
+ if ( empty( $pod_params['name'] ) ) {
1540
+ return pods_error( __( 'Please enter a Name for this Pod', 'pods' ), $this );
1541
+ }
1542
+
1543
+ $pod_params['storage'] = $params->create_storage;
1544
+
1545
+ if ( pods_tableless() ) {
1546
+ $pod_params['storage'] = 'meta';
1547
+ }
1548
+ } elseif ( 'taxonomy' === $pod_params['type'] ) {
1549
+ if ( empty( $pod_params['name'] ) ) {
1550
+ return pods_error( __( 'Please enter a Name for this Pod', 'pods' ), $this );
1551
+ }
1552
+
1553
+ $pod_params['storage'] = $params->create_storage;
1554
+
1555
+ if ( ! function_exists( 'get_term_meta' ) || ! empty( $params->create_storage_taxonomy ) ) {
1556
+ $pod_params['storage'] = $params->create_storage_taxonomy;
1557
+ }
1558
+
1559
+ if ( pods_tableless() ) {
1560
+ $pod_params['storage'] = ( function_exists( 'get_term_meta' ) ? 'meta' : 'none' );
1561
+ }
1562
+
1563
+ $pod_params['options']['hierarchical'] = 1;
1564
+ } elseif ( 'pod' === $pod_params['type'] ) {
1565
+ if ( empty( $pod_params['name'] ) ) {
1566
+ return pods_error( __( 'Please enter a Name for this Pod', 'pod' ), $this );
1567
+ }
1568
+
1569
+ if ( pods_tableless() ) {
1570
+ $pod_params['type'] = 'post_type';
1571
+ $pod_params['storage'] = 'meta';
1572
+ }
1573
+ } elseif ( 'settings' === $pod_params['type'] ) {
1574
+ $pod_params['name'] = $params->create_setting_name;
1575
+ $pod_params['label'] = ( ! empty( $params->create_label_title ) ? $params->create_label_title : ucwords( str_replace( '_', ' ', $params->create_setting_name ) ) );
1576
+ $pod_params['options'] = array(
1577
+ 'menu_name' => ( ! empty( $params->create_label_menu ) ? $params->create_label_menu : $pod_params['label'] ),
1578
+ 'menu_location' => $params->create_menu_location
1579
+ );
1580
+ $pod_params['storage'] = 'none';
1581
+
1582
+ // Auto-generate name if not provided
1583
+ if ( empty( $pod_params['name'] ) && ! empty( $pod_params['label'] ) ) {
1584
+ $pod_params['name'] = pods_clean_name( $pod_params['label'] );
1585
+ }
1586
+
1587
+ if ( empty( $pod_params['name'] ) ) {
1588
+ return pods_error( __( 'Please enter a Name for this Pod', 'pods' ), $this );
1589
+ }
1590
+ }
1591
+ } elseif ( 'extend' === $params->create_extend ) {
1592
+ $pod_params['type'] = $params->extend_pod_type;
1593
+
1594
+ if ( 'post_type' === $pod_params['type'] ) {
1595
+ $pod_params['storage'] = $params->extend_storage;
1596
+
1597
+ if ( pods_tableless() ) {
1598
+ $pod_params['storage'] = 'meta';
1599
+ }
1600
+
1601
+ $pod_params['name'] = $params->extend_post_type;
1602
+ } elseif ( 'taxonomy' === $pod_params['type'] ) {
1603
+ $pod_params['storage'] = $params->extend_storage;
1604
+
1605
+ if ( ! function_exists( 'get_term_meta' ) || ! empty( $params->extend_storage_taxonomy ) ) {
1606
+ $pod_params['storage'] = $params->extend_storage_taxonomy;
1607
+ }
1608
+
1609
+ if ( pods_tableless() ) {
1610
+ $pod_params['storage'] = ( function_exists( 'get_term_meta' ) ? 'meta' : 'none' );
1611
+ }
1612
+
1613
+ $pod_params['name'] = $params->extend_taxonomy;
1614
+ } elseif ( 'table' === $pod_params['type'] ) {
1615
+ $pod_params['storage'] = 'table';
1616
+ $pod_params['name'] = $params->extend_table;
1617
+ } else {
1618
+ $pod_params['storage'] = $params->extend_storage;
1619
+
1620
+ if ( pods_tableless() ) {
1621
+ $pod_params['storage'] = 'meta';
1622
+ }
1623
+
1624
+ $pod_params['name'] = $params->extend_pod_type;
1625
+ }
1626
+
1627
+ $pod_params['label'] = ucwords( str_replace( '_', ' ', $pod_params['name'] ) );
1628
+ $pod_params['object'] = $pod_params['name'];
1629
+ }
1630
+
1631
+ if ( empty( $pod_params['object'] ) ) {
1632
+ if ( 'post_type' === $pod_params['type'] ) {
1633
+ $check = get_post_type_object( $pod_params['name'] );
1634
+
1635
+ if ( ! empty( $check ) ) {
1636
+ return pods_error( sprintf( __( 'Post Type %s already exists, try extending it instead', 'pods' ), $pod_params['name'] ), $this );
1637
+ }
1638
+
1639
+ $pod_params['options']['supports_title'] = 1;
1640
+ $pod_params['options']['supports_editor'] = 1;
1641
+ } elseif ( 'taxonomy' === $pod_params['type'] ) {
1642
+ $check = get_taxonomy( $pod_params['name'] );
1643
+
1644
+ if ( ! empty( $check ) ) {
1645
+ return pods_error( sprintf( __( 'Taxonomy %s already exists, try extending it instead', 'pods' ), $pod_params['name'] ), $this );
1646
+ }
1647
+ }
1648
+ }
1649
+
1650
+ if ( ! empty( $pod_params ) ) {
1651
+ return $this->save_pod( $pod_params );
1652
+ }
1653
+
1654
+ return false;
1655
+ }
1656
+
1657
+ /**
1658
+ * Add or edit a Pod
1659
+ *
1660
+ * $params['id'] int The Pod ID
1661
+ * $params['name'] string The Pod name
1662
+ * $params['label'] string The Pod label
1663
+ * $params['type'] string The Pod type
1664
+ * $params['object'] string The object being extended (if any)
1665
+ * $params['storage'] string The Pod storage
1666
+ * $params['options'] array Options
1667
+ * $params['create_extend'] string Create or Extend a Content Type
1668
+ *
1669
+ * @param array $params An associative array of parameters
1670
+ * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed, will
1671
+ * sanitize them if false.
1672
+ * @param bool|int $db (optional) Whether to save into the DB or just return Pod array.
1673
+ *
1674
+ * @return int Pod ID
1675
+ * @since 1.7.9
1676
+ */
1677
+ public function save_pod( $params, $sanitized = false, $db = true ) {
1678
+
1679
+ $tableless_field_types = PodsForm::tableless_field_types();
1680
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
1681
+
1682
+ $extend = ( is_array( $params ) && ! empty( $params['create_extend'] ) && 'extend' === $params['create_extend'] );
1683
+ unset( $params['create_extend'] );
1684
+
1685
+ $load_params = (object) $params;
1686
+
1687
+ if ( isset( $load_params->id ) && isset( $load_params->name ) ) {
1688
+ unset( $load_params->name );
1689
+ }
1690
+
1691
+ if ( isset( $load_params->old_name ) ) {
1692
+ $load_params->name = $load_params->old_name;
1693
+ }
1694
+
1695
+ $load_params->table_info = true;
1696
+
1697
+ $pod = $this->load_pod( $load_params, false );
1698
+
1699
+ $params = (object) $params;
1700
+
1701
+ if ( false === $sanitized ) {
1702
+ $params = pods_sanitize( $params );
1703
+ }
1704
+
1705
+ $old_id = null;
1706
+ $old_name = null;
1707
+ $old_storage = null;
1708
+
1709
+ $old_fields = array();
1710
+ $old_options = array();
1711
+
1712
+ if ( isset( $params->name ) && ! isset( $params->object ) ) {
1713
+ $params->name = pods_clean_name( $params->name );
1714
+ }
1715
+
1716
+ if ( ! empty( $pod ) ) {
1717
+ // Existing pod (update).
1718
+
1719
+ if ( isset( $params->id ) && 0 < $params->id ) {
1720
+ $old_id = $params->id;
1721
+ }
1722
+
1723
+ $params->id = $pod['id'];
1724
+
1725
+ $old_name = $pod['name'];
1726
+ $old_storage = $pod['storage'];
1727
+ $old_fields = $pod['fields'];
1728
+ $old_options = $pod['options'];
1729
+
1730
+ // Check if name is intentionally not set, set it as current name.
1731
+ if ( ! isset( $params->name ) ) {
1732
+ $params->name = $pod['name'];
1733
+ }
1734
+
1735
+ if ( $old_name !== $params->name ) {
1736
+ if ( false !== $this->pod_exists( array( 'name' => $params->name ) ) ) {
1737
+ return pods_error( sprintf( __( 'Pod %1$s already exists, you cannot rename %2$s to that', 'pods' ), $params->name, $old_name ), $this );
1738
+ }
1739
+
1740
+ if (
1741
+ in_array( $pod['type'], array( 'user', 'comment', 'media' ), true )
1742
+ && in_array( $pod['object'], array( 'user', 'comment', 'media' ), true )
1743
+ ) {
1744
+ return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
1745
+ }
1746
+
1747
+ if (
1748
+ in_array( $pod['type'], array( 'post_type', 'taxonomy' ), true )
1749
+ && ! empty( $pod['object'] )
1750
+ && $pod['object'] == $old_name
1751
+ ) {
1752
+ return pods_error( sprintf( __( 'Pod %s cannot be renamed, it extends an existing WP Object', 'pods' ), $old_name ), $this );
1753
+ }
1754
+ }
1755
+
1756
+ if ( $old_id != $params->id ) {
1757
+ if ( $params->type == $pod['type'] && isset( $params->object ) && $params->object == $pod['object'] ) {
1758
+ return pods_error( sprintf( __( 'Pod using %s already exists, you can not reuse an object across multiple pods', 'pods' ), $params->object ), $this );
1759
+ } else {
1760
+ return pods_error( sprintf( __( 'Pod %s already exists', 'pods' ), $params->name ), $this );
1761
+ }
1762
+ }
1763
+ } else {
1764
+ // New pod (create).
1765
+
1766
+ if (
1767
+ in_array( $params->name, pods_reserved_keywords(), true )
1768
+ && in_array( pods_v( 'type', $params ), array( 'post_type', 'taxonomy' ), true )
1769
+ ) {
1770
+ $valid_name = false;
1771
+
1772
+ // Only if it's extending an existing content type then these
1773
+ // names are still allowed, even if they are reserved.
1774
+ if ( $extend ) {
1775
+ if ( 'post_type' === pods_v( 'type', $params ) ) {
1776
+ $valid_name = in_array( $params->name, get_post_types(), true );
1777
+ } elseif ( 'taxonomy' === pods_v( 'type', $params ) ) {
1778
+ $valid_name = in_array( $params->name, get_taxonomies(), true );
1779
+ }
1780
+ }
1781
+
1782
+ if ( ! $valid_name ) {
1783
+ return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name', 'pods' ), $params->name ), $this );
1784
+ }
1785
+ }
1786
+
1787
+ $pod = array(
1788
+ 'id' => 0,
1789
+ 'name' => $params->name,
1790
+ 'label' => $params->name,
1791
+ 'description' => '',
1792
+ 'type' => 'pod',
1793
+ 'storage' => 'table',
1794
+ 'object' => '',
1795
+ 'alias' => '',
1796
+ 'options' => array(),
1797
+ 'fields' => array(),
1798
+ );
1799
+ }
1800
+
1801
+ // Blank out fields and options for AJAX calls (everything should be sent to it for a full overwrite)
1802
+ if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
1803
+ $pod['fields'] = array();
1804
+ $pod['options'] = array();
1805
+ }
1806
+
1807
+ // Setup options
1808
+ $options = get_object_vars( $params );
1809
+
1810
+ if ( isset( $options['method'] ) ) {
1811
+ unset( $options['method'] );
1812
+ }
1813
+
1814
+ $options_ignore = array(
1815
+ 'object_type',
1816
+ 'object_name',
1817
+ 'table',
1818
+ 'meta_table',
1819
+ 'pod_table',
1820
+ 'field_id',
1821
+ 'field_index',
1822
+ 'field_slug',
1823
+ 'field_type',
1824
+ 'field_parent',
1825
+ 'field_parent_select',
1826
+ 'meta_field_id',
1827
+ 'meta_field_index',
1828
+ 'meta_field_value',
1829
+ 'pod_field_id',
1830
+ 'pod_field_index',
1831
+ 'object_fields',
1832
+ 'join',
1833
+ 'where',
1834
+ 'where_default',
1835
+ 'orderby',
1836
+ 'pod',
1837
+ 'recurse',
1838
+ 'table_info',
1839
+ 'attributes',
1840
+ 'group',
1841
+ 'grouped',
1842
+ 'developer_mode',
1843
+ 'dependency',
1844
+ 'depends-on',
1845
+ 'excludes-on'
1846
+ );
1847
+
1848
+ foreach ( $options_ignore as $ignore ) {
1849
+ if ( isset( $options[ $ignore ] ) ) {
1850
+ unset( $options[ $ignore ] );
1851
+ }
1852
+ }
1853
+
1854
+ $exclude = array(
1855
+ 'id',
1856
+ 'name',
1857
+ 'label',
1858
+ 'description',
1859
+ 'type',
1860
+ 'storage',
1861
+ 'object',
1862
+ 'alias',
1863
+ 'options',
1864
+ 'fields'
1865
+ );
1866
+
1867
+ foreach ( $exclude as $k => $exclude_field ) {
1868
+ $aliases = array( $exclude_field );
1869
+
1870
+ if ( is_array( $exclude_field ) ) {
1871
+ $aliases = array_merge( array( $k ), $exclude_field );
1872
+ $exclude_field = $k;
1873
+ }
1874
+
1875
+ foreach ( $aliases as $alias ) {
1876
+ if ( isset( $options[ $alias ] ) ) {
1877
+ $pod[ $exclude_field ] = pods_trim( $options[ $alias ] );
1878
+
1879
+ unset( $options[ $alias ] );
1880
+ }
1881
+ }
1882
+ }
1883
+
1884
+ if ( pods_tableless() && ! in_array( $pod['type'], array( 'settings', 'table' ) ) ) {
1885
+ if ( 'pod' === $pod['type'] ) {
1886
+ $pod['type'] = 'post_type';
1887
+ }
1888
+
1889
+ if ( 'table' === $pod['storage'] ) {
1890
+ if ( 'taxonomy' === $pod['type'] && ! function_exists( 'get_term_meta' ) ) {
1891
+ $pod['storage'] = 'none';
1892
+ } else {
1893
+ $pod['storage'] = 'meta';
1894
+ }
1895
+ }
1896
+ }
1897
+
1898
+ $pod['options']['type'] = $pod['type'];
1899
+ $pod['options']['storage'] = $pod['storage'];
1900
+ $pod['options']['object'] = $pod['object'];
1901
+ $pod['options']['alias'] = $pod['alias'];
1902
+
1903
+ $pod['options'] = array_merge( $pod['options'], $options );
1904
+
1905
+ /**
1906
+ * @var WP_Query
1907
+ */
1908
+ global $wp_query;
1909
+
1910
+ $reserved_query_vars = array(
1911
+ 'post_type',
1912
+ 'taxonomy',
1913
+ 'output'
1914
+ );
1915
+
1916
+ if ( is_object( $wp_query ) ) {
1917
+ $reserved_query_vars = array_merge( $reserved_query_vars, array_keys( $wp_query->fill_query_vars( array() ) ) );
1918
+ }
1919
+
1920
+ if ( isset( $pod['options']['query_var_string'] ) ) {
1921
+ if ( in_array( $pod['options']['query_var_string'], $reserved_query_vars ) ) {
1922
+ $pod['options']['query_var_string'] = $pod['options']['type'] . '_' . $pod['options']['query_var_string'];
1923
+ }
1924
+ }
1925
+
1926
+ if ( isset( $pod['options']['query_var'] ) ) {
1927
+ if ( in_array( $pod['options']['query_var'], $reserved_query_vars ) ) {
1928
+ $pod['options']['query_var'] = $pod['options']['type'] . '_' . $pod['options']['query_var'];
1929
+ }
1930
+ }
1931
+
1932
+ if ( strlen( $pod['label'] ) < 1 ) {
1933
+ $pod['label'] = $pod['name'];
1934
+ }
1935
+
1936
+ if ( 'post_type' === $pod['type'] ) {
1937
+ // Max length for post types are 20 characters
1938
+ $pod['name'] = substr( $pod['name'], 0, 20 );
1939
+ } elseif ( 'taxonomy' === $pod['type'] ) {
1940
+ // Max length for taxonomies are 32 characters
1941
+ $pod['name'] = substr( $pod['name'], 0, 32 );
1942
+ }
1943
+
1944
+ $params->id = $pod['id'];
1945
+ $params->name = $pod['name'];
1946
+
1947
+ if ( null !== $old_name && $old_name !== $params->name && empty( $pod['object'] ) ) {
1948
+ if ( 'post_type' === $pod['type'] ) {
1949
+ $check = get_post_type_object( $params->name );
1950
+
1951
+ if ( ! empty( $check ) ) {
1952
+ return pods_error( sprintf( __( 'Post Type %1$s already exists, you cannot rename %2$s to that', 'pods' ), $params->name, $old_name ), $this );
1953
+ }
1954
+ } elseif ( 'taxonomy' === $pod['type'] ) {
1955
+ $check = get_taxonomy( $params->name );
1956
+
1957
+ if ( ! empty( $check ) ) {
1958
+ return pods_error( sprintf( __( 'Taxonomy %1$s already exists, you cannot rename %2$s to that', 'pods' ), $params->name, $old_name ), $this );
1959
+ }
1960
+ }
1961
+ }
1962
+
1963
+ $field_table_operation = true;
1964
+
1965
+ // Add new pod
1966
+ if ( empty( $params->id ) ) {
1967
+ if ( strlen( $params->name ) < 1 ) {
1968
+ return pods_error( __( 'Pod name cannot be empty', 'pods' ), $this );
1969
+ }
1970
+
1971
+ $post_data = array(
1972
+ 'post_name' => $pod['name'],
1973
+ 'post_title' => $pod['label'],
1974
+ 'post_content' => $pod['description'],
1975
+ 'post_type' => '_pods_pod',
1976
+ 'post_status' => 'publish'
1977
+ );
1978
+
1979
+ if ( 'pod' === $pod['type'] && ( ! is_array( $pod['fields'] ) || empty( $pod['fields'] ) ) ) {
1980
+ $pod['fields'] = array();
1981
+
1982
+ $pod['fields']['name'] = array(
1983
+ 'name' => 'name',
1984
+ 'label' => 'Name',
1985
+ 'type' => 'text',
1986
+ 'options' => array(
1987
+ 'required' => '1'
1988
+ )
1989
+ );
1990
+
1991
+ $pod['fields']['created'] = array(
1992
+ 'name' => 'created',
1993
+ 'label' => 'Date Created',
1994
+ 'type' => 'datetime',
1995
+ 'options' => array(
1996
+ 'datetime_format' => 'ymd_slash',
1997
+ 'datetime_time_type' => '12',
1998
+ 'datetime_time_format' => 'h_mm_ss_A'
1999
+ )
2000
+ );
2001
+
2002
+ $pod['fields']['modified'] = array(
2003
+ 'name' => 'modified',
2004
+ 'label' => 'Date Modified',
2005
+ 'type' => 'datetime',
2006
+ 'options' => array(
2007
+ 'datetime_format' => 'ymd_slash',
2008
+ 'datetime_time_type' => '12',
2009
+ 'datetime_time_format' => 'h_mm_ss_A'
2010
+ )
2011
+ );
2012
+
2013
+ $pod['fields']['author'] = array(
2014
+ 'name' => 'author',
2015
+ 'label' => 'Author',
2016
+ 'type' => 'pick',
2017
+ 'pick_object' => 'user',
2018
+ 'options' => array(
2019
+ 'pick_format_type' => 'single',
2020
+ 'pick_format_single' => 'autocomplete',
2021
+ 'default_value' => '{@user.ID}'
2022
+ )
2023
+ );
2024
+
2025
+ $pod['fields']['permalink'] = array(
2026
+ 'name' => 'permalink',
2027
+ 'label' => 'Permalink',
2028
+ 'type' => 'slug',
2029
+ 'description' => 'Leave blank to auto-generate from Name'
2030
+ );
2031
+
2032
+ if ( ! isset( $pod['options']['pod_index'] ) ) {
2033
+ $pod['options']['pod_index'] = 'name';
2034
+ }
2035
+ }
2036
+
2037
+ $pod = $this->do_hook( 'save_pod_default_pod', $pod, $params, $sanitized, $db );
2038
+
2039
+ $field_table_operation = false;
2040
+ } else {
2041
+ $post_data = array(
2042
+ 'ID' => $pod['id'],
2043
+ 'post_name' => $pod['name'],
2044
+ 'post_title' => $pod['label'],
2045
+ 'post_content' => $pod['description'],
2046
+ 'post_status' => 'publish'
2047
+ );
2048
+ }
2049
+
2050
+ if ( true === $db ) {
2051
+ if ( ! has_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ) ) ) {
2052
+ add_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ), 100, 6 );
2053
+ }
2054
+
2055
+ $conflicted = false;
2056
+
2057
+ // Headway compatibility fix
2058
+ if ( has_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 ) ) {
2059
+ remove_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2060
+
2061
+ $conflicted = true;
2062
+ }
2063
+
2064
+ $params->id = $this->save_wp_object( 'post', $post_data, $pod['options'], true, true );
2065
+
2066
+ if ( $conflicted ) {
2067
+ add_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2068
+ }
2069
+
2070
+ if ( false === $params->id ) {
2071
+ return pods_error( __( 'Cannot save Pod', 'pods' ), $this );
2072
+ }
2073
+ } elseif ( empty( $params->id ) ) {
2074
+ $params->id = (int) $db;
2075
+ }
2076
+
2077
+ $pod['id'] = $params->id;
2078
+
2079
+ // Setup / update tables
2080
+ if ( 'table' !== $pod['type'] && 'table' === $pod['storage'] && $old_storage !== $pod['storage'] && $db ) {
2081
+ $definitions = array( "`id` BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY" );
2082
+
2083
+ $defined_fields = array();
2084
+
2085
+ foreach ( $pod['fields'] as $field ) {
2086
+ if ( ! is_array( $field ) || ! isset( $field['name'] ) || in_array( $field['name'], $defined_fields ) ) {
2087
+ continue;
2088
+ }
2089
+
2090
+ $defined_fields[] = $field['name'];
2091
+
2092
+ if ( ! in_array( $field['type'], $tableless_field_types ) || ( 'pick' === $field['type'] && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) ) {
2093
+ $definition = $this->get_field_definition( $field['type'], array_merge( $field, pods_var_raw( 'options', $field, array() ) ) );
2094
+
2095
+ if ( 0 < strlen( $definition ) ) {
2096
+ $definitions[] = "`{$field['name']}` " . $definition;
2097
+ }
2098
+ }
2099
+ }
2100
+
2101
+ pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`" );
2102
+
2103
+ /**
2104
+ * @todo Central function to fetch charset.
2105
+ * @see PodsUpgrade::install() L64-L76
2106
+ */
2107
+ $charset_collate = 'DEFAULT CHARSET utf8';
2108
+
2109
+ global $wpdb;
2110
+ if ( ! empty( $wpdb->charset ) ) {
2111
+ $charset_collate = "DEFAULT CHARSET {$wpdb->charset}";
2112
+ }
2113
+
2114
+ if ( ! empty( $wpdb->collate ) ) {
2115
+ $charset_collate .= " COLLATE {$wpdb->collate}";
2116
+ }
2117
+
2118
+ $result = pods_query( "CREATE TABLE `@wp_pods_{$params->name}` (" . implode( ', ', $definitions ) . ") {$charset_collate}", $this );
2119
+
2120
+ if ( empty( $result ) ) {
2121
+ return pods_error( __( 'Cannot add Database Table for Pod', 'pods' ), $this );
2122
+ }
2123
+
2124
+ } elseif ( 'table' !== $pod['type'] && 'table' === $pod['storage'] && $pod['storage'] == $old_storage && null !== $old_name && $old_name !== $params->name && $db ) {
2125
+ $result = pods_query( "ALTER TABLE `@wp_pods_{$old_name}` RENAME `@wp_pods_{$params->name}`", $this );
2126
+
2127
+ if ( empty( $result ) ) {
2128
+ return pods_error( __( 'Cannot update Database Table for Pod', 'pods' ), $this );
2129
+ }
2130
+ }
2131
+
2132
+ /**
2133
+ * @var $wpdb wpdb
2134
+ */
2135
+ global $wpdb;
2136
+
2137
+ if ( null !== $old_name && $old_name !== $params->name && $db ) {
2138
+ // Rename items in the DB pointed at the old WP Object names
2139
+ if ( 'post_type' === $pod['type'] && empty( $pod['object'] ) ) {
2140
+ $this->rename_wp_object_type( 'post', $old_name, $params->name );
2141
+ } elseif ( 'taxonomy' === $pod['type'] && empty( $pod['object'] ) ) {
2142
+ $this->rename_wp_object_type( 'taxonomy', $old_name, $params->name );
2143
+ } elseif ( 'comment' === $pod['type'] && empty( $pod['object'] ) ) {
2144
+ $this->rename_wp_object_type( 'comment', $old_name, $params->name );
2145
+ } elseif ( 'settings' === $pod['type'] ) {
2146
+ $this->rename_wp_object_type( 'settings', $old_name, $params->name );
2147
+ }
2148
+
2149
+ // Sync any related fields if the name has changed
2150
+ $fields = pods_query( "
2151
+ SELECT `p`.`ID`
2152
+ FROM `{$wpdb->posts}` AS `p`
2153
+ LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
2154
+ LEFT JOIN `{$wpdb->postmeta}` AS `pm2` ON `pm2`.`post_id` = `p`.`ID`
2155
+ WHERE
2156
+ `p`.`post_type` = '_pods_field'
2157
+ AND `pm`.`meta_key` = 'pick_object'
2158
+ AND (
2159
+ `pm`.`meta_value` = 'pod'
2160
+ OR `pm`.`meta_value` = '" . $pod['type'] . "'
2161
+ )
2162
+ AND `pm2`.`meta_key` = 'pick_val'
2163
+ AND `pm2`.`meta_value` = '{$old_name}'
2164
+ " );
2165
+
2166
+ if ( ! empty( $fields ) ) {
2167
+ foreach ( $fields as $field ) {
2168
+ update_post_meta( $field->ID, 'pick_object', $pod['type'] );
2169
+ update_post_meta( $field->ID, 'pick_val', $params->name );
2170
+ }
2171
+ }
2172
+
2173
+ $fields = pods_query( "
2174
+ SELECT `p`.`ID`
2175
+ FROM `{$wpdb->posts}` AS `p`
2176
+ LEFT JOIN `{$wpdb->postmeta}` AS `pm` ON `pm`.`post_id` = `p`.`ID`
2177
+ WHERE
2178
+ `p`.`post_type` = '_pods_field'
2179
+ AND `pm`.`meta_key` = 'pick_object'
2180
+ AND (
2181
+ `pm`.`meta_value` = 'pod-{$old_name}'
2182
+ OR `pm`.`meta_value` = '" . $pod['type'] . "-{$old_name}'
2183
+ )
2184
+ " );
2185
+
2186
+ if ( ! empty( $fields ) ) {
2187
+ foreach ( $fields as $field ) {
2188
+ update_post_meta( $field->ID, 'pick_object', $pod['type'] );
2189
+ update_post_meta( $field->ID, 'pick_val', $params->name );
2190
+ }
2191
+ }
2192
+ }
2193
+
2194
+ // Sync built-in options for post types and taxonomies
2195
+ if ( in_array( $pod['type'], array( 'post_type', 'taxonomy' ) ) && empty( $pod['object'] ) && $db ) {
2196
+ // Build list of 'built_in' for later
2197
+ $built_in = array();
2198
+
2199
+ foreach ( $pod['options'] as $key => $val ) {
2200
+ if ( false === strpos( $key, 'built_in_' ) ) {
2201
+ continue;
2202
+ } elseif ( false !== strpos( $key, 'built_in_post_types_' ) ) {
2203
+ $built_in_type = 'post_type';
2204
+ } elseif ( false !== strpos( $key, 'built_in_taxonomies_' ) ) {
2205
+ $built_in_type = 'taxonomy';
2206
+ } else {
2207
+ continue;
2208
+ }
2209
+
2210
+ if ( $built_in_type == $pod['type'] ) {
2211
+ continue;
2212
+ }
2213
+
2214
+ if ( ! isset( $built_in[ $built_in_type ] ) ) {
2215
+ $built_in[ $built_in_type ] = array();
2216
+ }
2217
+
2218
+ $built_in_object = str_replace( array( 'built_in_post_types_', 'built_in_taxonomies_' ), '', $key );
2219
+
2220
+ $built_in[ $built_in_type ][ $built_in_object ] = (int) $val;
2221
+ }
2222
+
2223
+ $lookup_option = false;
2224
+ $lookup_built_in = false;
2225
+
2226
+ $lookup_name = $pod['name'];
2227
+
2228
+ if ( 'post_type' === $pod['type'] ) {
2229
+ $lookup_option = 'built_in_post_types_' . $lookup_name;
2230
+ $lookup_built_in = 'taxonomy';
2231
+ } elseif ( 'taxonomy' === $pod['type'] ) {
2232
+ $lookup_option = 'built_in_taxonomies_' . $lookup_name;
2233
+ $lookup_built_in = 'post_type';
2234
+ }
2235
+
2236
+ if ( ! empty( $lookup_option ) && ! empty( $lookup_built_in ) && isset( $built_in[ $lookup_built_in ] ) ) {
2237
+ foreach ( $built_in[ $lookup_built_in ] as $built_in_object => $val ) {
2238
+ $search_val = 1;
2239
+
2240
+ if ( 1 == $val ) {
2241
+ $search_val = 0;
2242
+ }
2243
+
2244
+ $query = "SELECT p.ID FROM {$wpdb->posts} AS p
2245
+ LEFT JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.ID AND pm.meta_key = '{$lookup_option}'
2246
+ LEFT JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.ID AND pm2.meta_key = 'type' AND pm2.meta_value = '{$lookup_built_in}'
2247
+ LEFT JOIN {$wpdb->postmeta} AS pm3 ON pm3.post_id = p.ID AND pm3.meta_key = 'object' AND pm3.meta_value = ''
2248
+ WHERE p.post_type = '_pods_pod' AND p.post_name = '{$built_in_object}'
2249
+ AND pm2.meta_id IS NOT NULL
2250
+ AND ( pm.meta_id IS NULL OR pm.meta_value = {$search_val} )";
2251
+
2252
+ $results = pods_query( $query );
2253
+
2254
+ if ( ! empty( $results ) ) {
2255
+ foreach ( $results as $the_pod ) {
2256
+ delete_post_meta( $the_pod->ID, $lookup_option );
2257
+
2258
+ add_post_meta( $the_pod->ID, $lookup_option, $val );
2259
+ }
2260
+ }
2261
+ }
2262
+ }
2263
+ }
2264
+
2265
+ $saved = array();
2266
+ $errors = array();
2267
+
2268
+ $field_index_change = false;
2269
+ $field_index_id = 0;
2270
+
2271
+ $id_required = false;
2272
+
2273
+ $field_index = pods_var( 'pod_index', $pod['options'], 'id', null, true );
2274
+
2275
+ if ( 'pod' === $pod['type'] && ! empty( $pod['fields'] ) && isset( $pod['fields'][ $field_index ] ) ) {
2276
+ $field_index_id = $pod['fields'][ $field_index ];
2277
+ }
2278
+
2279
+ if ( isset( $params->fields ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
2280
+ $fields = array();
2281
+
2282
+ if ( isset( $params->fields ) ) {
2283
+ $params->fields = (array) $params->fields;
2284
+
2285
+ $weight = 0;
2286
+
2287
+ foreach ( $params->fields as $field ) {
2288
+ if ( ! is_array( $field ) || ! isset( $field['name'] ) ) {
2289
+ continue;
2290
+ }
2291
+
2292
+ if ( ! isset( $field['weight'] ) ) {
2293
+ $field['weight'] = $weight;
2294
+
2295
+ $weight ++;
2296
+ }
2297
+
2298
+ $fields[ $field['name'] ] = $field;
2299
+ }
2300
+ }
2301
+
2302
+ $weight = 0;
2303
+
2304
+ $saved_field_ids = array();
2305
+
2306
+ foreach ( $pod['fields'] as $k => $field ) {
2307
+ if ( ! empty( $old_id ) && ( ! is_array( $field ) || ! isset( $field['name'] ) || ! isset( $fields[ $field['name'] ] ) ) ) {
2308
+ // Iterative change handling for setup-edit.php
2309
+ if ( ! is_array( $field ) && isset( $old_fields[ $k ] ) ) {
2310
+ $saved[ $old_fields[ $k ]['name'] ] = true;
2311
+ }
2312
+
2313
+ continue;
2314
+ }
2315
+
2316
+ if ( ! empty( $old_id ) ) {
2317
+ $field = array_merge( $field, $fields[ $field['name'] ] );
2318
+ }
2319
+
2320
+ $field['pod'] = $pod;
2321
+
2322
+ if ( ! isset( $field['weight'] ) ) {
2323
+ $field['weight'] = $weight;
2324
+
2325
+ $weight ++;
2326
+ }
2327
+
2328
+ if ( 0 < $field_index_id && pods_var( 'id', $field ) == $field_index_id ) {
2329
+ $field_index_change = $field['name'];
2330
+ }
2331
+
2332
+ if ( 0 < pods_var( 'id', $field ) ) {
2333
+ $id_required = true;
2334
+ }
2335
+
2336
+ if ( $id_required ) {
2337
+ $field['id_required'] = true;
2338
+ }
2339
+
2340
+ $field_data = $field;
2341
+
2342
+ $field = $this->save_field( $field_data, $field_table_operation, true, $db );
2343
+
2344
+ if ( true !== $db ) {
2345
+ $pod['fields'][ $k ] = $field;
2346
+ $saved_field_ids[] = $field['id'];
2347
+ } else {
2348
+ if ( ! empty( $field ) && 0 < $field ) {
2349
+ $saved[ $field_data['name'] ] = true;
2350
+ $saved_field_ids[] = $field;
2351
+ } else {
2352
+ $errors[] = sprintf( __( 'Cannot save the %s field', 'pods' ), $field_data['name'] );
2353
+ }
2354
+ }
2355
+ }
2356
+
2357
+ if ( true === $db ) {
2358
+ foreach ( $old_fields as $field ) {
2359
+ if ( isset( $pod['fields'][ $field['name'] ] ) || isset( $saved[ $field['name'] ] ) || in_array( $field['id'], $saved_field_ids ) ) {
2360
+ continue;
2361
+ }
2362
+
2363
+ if ( $field['id'] == $field_index_id ) {
2364
+ $field_index_change = 'id';
2365
+ } elseif ( $field['name'] == $field_index ) {
2366
+ $field_index_change = 'id';
2367
+ }
2368
+
2369
+ $this->delete_field( array(
2370
+ 'id' => (int) $field['id'],
2371
+ 'name' => $field['name'],
2372
+ 'pod' => $pod
2373
+ ), $field_table_operation );
2374
+ }
2375
+ }
2376
+
2377
+ // Update field index if the name has changed or the field has been removed
2378
+ if ( false !== $field_index_change && true === $db ) {
2379
+ update_post_meta( $pod['id'], 'pod_index', $field_index_change );
2380
+ }
2381
+ }
2382
+
2383
+ $this->cache_flush_pods( $pod );
2384
+
2385
+ if ( ! empty( $errors ) ) {
2386
+ return pods_error( $errors, $this );
2387
+ }
2388
+
2389
+ $refresh_pod = $this->load_pod( array( 'name' => $pod['name'] ), false );
2390
+
2391
+ if ( $refresh_pod ) {
2392
+ $pod = $refresh_pod;
2393
+ }
2394
+
2395
+ if ( 'post_type' === $pod['type'] ) {
2396
+ PodsMeta::$post_types[ $pod['id'] ] = $pod;
2397
+ } elseif ( 'taxonomy' === $pod['type'] ) {
2398
+ PodsMeta::$taxonomies[ $pod['id'] ] = $pod;
2399
+ } elseif ( 'media' === $pod['type'] ) {
2400
+ PodsMeta::$media[ $pod['id'] ] = $pod;
2401
+ } elseif ( 'user' === $pod['type'] ) {
2402
+ PodsMeta::$user[ $pod['id'] ] = $pod;
2403
+ } elseif ( 'comment' === $pod['type'] ) {
2404
+ PodsMeta::$comment[ $pod['id'] ] = $pod;
2405
+ }
2406
+
2407
+ if ( ! class_exists( 'PodsInit' ) ) {
2408
+ pods_init();
2409
+ }
2410
+
2411
+ // Register Post Types / Taxonomies post-registration from PodsInit
2412
+ if (
2413
+ ! empty( PodsInit::$content_types_registered )
2414
+ && in_array( $pod['type'], array(
2415
+ 'post_type',
2416
+ 'taxonomy'
2417
+ ) )
2418
+ && empty( $pod['object'] )
2419
+ ) {
2420
+ pods_init()->setup_content_types( true );
2421
+ }
2422
+
2423
+ if ( true === $db ) {
2424
+ return $pod['id'];
2425
+ } else {
2426
+ return $pod;
2427
+ }
2428
+ }
2429
+
2430
+ /**
2431
+ * Add or edit a field within a Pod
2432
+ *
2433
+ * $params['id'] int Field ID (id OR pod_id+pod+name required)
2434
+ * $params['pod_id'] int Pod ID (id OR pod_id+pod+name required)
2435
+ * $params['pod'] string Pod name (id OR pod_id+pod+name required)
2436
+ * $params['name'] string Field name (id OR pod_id+pod+name required)
2437
+ * $params['label'] string (optional) Field label
2438
+ * $params['type'] string (optional) Field type (avatar, boolean, code, color, currency, date, datetime, email,
2439
+ * file, number, paragraph, password, phone, pick, slug, text, time, website, wysiwyg)
2440
+ * $params['pick_object'] string (optional) Related Object (for relationships)
2441
+ * $params['pick_val'] string (optional) Related Object name (for relationships)
2442
+ * $params['sister_id'] int (optional) Related Field ID (for bidirectional relationships)
2443
+ * $params['weight'] int (optional) Order in which the field appears
2444
+ * $params['options'] array (optional) Options
2445
+ *
2446
+ * @param array $params An associative array of parameters
2447
+ * @param bool $table_operation (optional) Whether or not to handle table operations
2448
+ * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed,
2449
+ * will sanitize them if false.
2450
+ * @param bool|int $db (optional) Whether to save into the DB or just return field array.
2451
+ *
2452
+ * @return int|array The field ID or field array (if !$db)
2453
+ * @since 1.7.9
2454
+ */
2455
+ public function save_field( $params, $table_operation = true, $sanitized = false, $db = true ) {
2456
+
2457
+ /**
2458
+ * @var $wpdb wpdb
2459
+ */
2460
+ global $wpdb;
2461
+
2462
+ if ( true !== $db ) {
2463
+ $table_operation = false;
2464
+ }
2465
+
2466
+ $tableless_field_types = PodsForm::tableless_field_types();
2467
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
2468
+
2469
+ $params = (object) $params;
2470
+
2471
+ if ( false === $sanitized ) {
2472
+ $params = pods_sanitize( $params );
2473
+ }
2474
+
2475
+ if ( isset( $params->pod_id ) ) {
2476
+ $params->pod_id = pods_absint( $params->pod_id );
2477
+ }
2478
+
2479
+ if ( true !== $db ) {
2480
+ $params->pod_id = (int) $db;
2481
+ }
2482
+
2483
+ $pod = null;
2484
+ $save_pod = false;
2485
+ $id_required = false;
2486
+
2487
+ if ( isset( $params->id_required ) ) {
2488
+ unset( $params->id_required );
2489
+
2490
+ $id_required = true;
2491
+ }
2492
+
2493
+ if ( ( ! isset( $params->pod ) || empty( $params->pod ) ) && ( ! isset( $params->pod_id ) || empty( $params->pod_id ) ) ) {
2494
+ return pods_error( __( 'Pod ID or name is required', 'pods' ), $this );
2495
+ }
2496
+
2497
+ if ( isset( $params->pod ) && is_array( $params->pod ) ) {
2498
+ $pod = $params->pod;
2499
+
2500
+ $save_pod = true;
2501
+ } elseif ( ( ! isset( $params->pod_id ) || empty( $params->pod_id ) ) && ( true === $db || 0 < $db ) ) {
2502
+ $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => true ) );
2503
+ } elseif ( ! isset( $params->pod ) && ( true === $db || 0 < $db ) ) {
2504
+ $pod = $this->load_pod( array( 'id' => $params->pod_id, 'table_info' => true ) );
2505
+ } elseif ( true === $db || 0 < $db ) {
2506
+ $pod = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true ) );
2507
+ }
2508
+
2509
+ if ( empty( $pod ) && true === $db ) {
2510
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
2511
+ }
2512
+
2513
+ $params->pod_id = $pod['id'];
2514
+ $params->pod = $pod['name'];
2515
+ $params->pod_data = $pod;
2516
+
2517
+ $params->name = pods_clean_name( $params->name, true, ( 'meta' === $pod['storage'] ? false : true ) );
2518
+
2519
+ if ( ! isset( $params->id ) ) {
2520
+ $params->id = 0;
2521
+ }
2522
+
2523
+ if ( empty( $params->name ) ) {
2524
+ return pods_error( __( 'Pod field name is required', 'pods' ), $this );
2525
+ }
2526
+
2527
+ $field = $this->load_field( $params );
2528
+
2529
+ unset( $params->pod_data );
2530
+
2531
+ $old_id = null;
2532
+ $old_name = null;
2533
+ $old_type = null;
2534
+ $old_definition = null;
2535
+ $old_simple = null;
2536
+ $old_options = null;
2537
+ $old_sister_id = null;
2538
+
2539
+ // @todo pods_reserved_keywords();
2540
+ $reserved_keywords = array( 'id', 'ID' );
2541
+
2542
+ if ( ! empty( $field ) ) {
2543
+ $old_id = pods_var( 'id', $field );
2544
+ $old_name = pods_clean_name( $field['name'], true, ( 'meta' === $pod['storage'] ? false : true ) );
2545
+ $old_type = $field['type'];
2546
+ $old_options = $field['options'];
2547
+ $old_sister_id = (int) pods_var( 'sister_id', $old_options, 0 );
2548
+
2549
+ $old_simple = ( 'pick' === $old_type && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) );
2550
+
2551
+ if ( isset( $params->name ) && ! empty( $params->name ) ) {
2552
+ $field['name'] = $params->name;
2553
+ }
2554
+
2555
+ if ( $old_name !== $field['name'] ) {
2556
+ if ( in_array( $field['name'], $reserved_keywords, true ) ) {
2557
+ return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name', 'pods' ), $field['name'] ), $this );
2558
+ }
2559
+
2560
+ if ( false !== $this->field_exists( $params ) ) {
2561
+ return pods_error( sprintf( __( 'Field %1$s already exists, you cannot rename %2$s to that', 'pods' ), $field['name'], $old_name ), $this );
2562
+ }
2563
+ }
2564
+
2565
+ if ( ( $id_required || ! empty( $params->id ) ) && ( empty( $old_id ) || $old_id != $params->id ) ) {
2566
+ return pods_error( sprintf( __( 'Field %s already exists', 'pods' ), $field['name'] ), $this );
2567
+ }
2568
+
2569
+ if ( empty( $params->id ) ) {
2570
+ $params->id = $old_id;
2571
+ }
2572
+
2573
+ if ( ! in_array( $old_type, $tableless_field_types ) || $old_simple ) {
2574
+ $definition = $this->get_field_definition( $old_type, array_merge( $field, $old_options ) );
2575
+
2576
+ if ( 0 < strlen( $definition ) ) {
2577
+ $old_definition = "`{$old_name}` " . $definition;
2578
+ }
2579
+ }
2580
+ } else {
2581
+ $field = array(
2582
+ 'id' => 0,
2583
+ 'pod_id' => $params->pod_id,
2584
+ 'name' => $params->name,
2585
+ 'label' => $params->name,
2586
+ 'description' => '',
2587
+ 'type' => 'text',
2588
+ 'pick_object' => '',
2589
+ 'pick_val' => '',
2590
+ 'sister_id' => '',
2591
+ 'weight' => null,
2592
+ 'options' => array()
2593
+ );
2594
+ }
2595
+
2596
+ // Setup options
2597
+ $options = get_object_vars( $params );
2598
+
2599
+ $options_ignore = array(
2600
+ 'method',
2601
+ 'table_info',
2602
+ 'attributes',
2603
+ 'group',
2604
+ 'grouped',
2605
+ 'developer_mode',
2606
+ 'dependency',
2607
+ 'depends-on',
2608
+ 'excludes-on'
2609
+ );
2610
+
2611
+ foreach ( $options_ignore as $ignore ) {
2612
+ if ( isset( $options[ $ignore ] ) ) {
2613
+ unset( $options[ $ignore ] );
2614
+ }
2615
+ }
2616
+
2617
+ if ( isset( $options['method'] ) ) {
2618
+ unset( $options['method'] );
2619
+ } elseif ( isset( $options['table_info'] ) ) {
2620
+ unset( $options['table_info'] );
2621
+ }
2622
+
2623
+ $exclude = array(
2624
+ 'id',
2625
+ 'pod_id',
2626
+ 'pod',
2627
+ 'name',
2628
+ 'label',
2629
+ 'description',
2630
+ 'type',
2631
+ 'pick_object',
2632
+ 'pick_val',
2633
+ 'sister_id',
2634
+ 'weight',
2635
+ 'options'
2636
+ );
2637
+
2638
+ foreach ( $exclude as $k => $exclude_field ) {
2639
+ $aliases = array( $exclude_field );
2640
+
2641
+ if ( is_array( $exclude_field ) ) {
2642
+ $aliases = array_merge( array( $k ), $exclude_field );
2643
+ $exclude_field = $k;
2644
+ }
2645
+
2646
+ foreach ( $aliases as $alias ) {
2647
+ if ( isset( $options[ $alias ] ) ) {
2648
+ $field[ $exclude_field ] = pods_trim( $options[ $alias ] );
2649
+
2650
+ unset( $options[ $alias ] );
2651
+ }
2652
+ }
2653
+ }
2654
+
2655
+ if ( strlen( $field['label'] ) < 1 ) {
2656
+ $field['label'] = $field['name'];
2657
+ }
2658
+
2659
+ $field['options']['type'] = $field['type'];
2660
+
2661
+ if ( in_array( $field['options']['type'], $tableless_field_types ) ) {
2662
+ // Clean up special drop-down in field editor and save out pick_val
2663
+ $field['pick_object'] = pods_var( 'pick_object', $field, '', null, true );
2664
+
2665
+ if ( 0 === strpos( $field['pick_object'], 'pod-' ) ) {
2666
+ $field['pick_val'] = pods_str_replace( 'pod-', '', $field['pick_object'], 1 );
2667
+ $field['pick_object'] = 'pod';
2668
+ } elseif ( 0 === strpos( $field['pick_object'], 'post_type-' ) ) {
2669
+ $field['pick_val'] = pods_str_replace( 'post_type-', '', $field['pick_object'], 1 );
2670
+ $field['pick_object'] = 'post_type';
2671
+ } elseif ( 0 === strpos( $field['pick_object'], 'taxonomy-' ) ) {
2672
+ $field['pick_val'] = pods_str_replace( 'taxonomy-', '', $field['pick_object'], 1 );
2673
+ $field['pick_object'] = 'taxonomy';
2674
+ } elseif ( 'table' === $field['pick_object'] && 0 < strlen( pods_var_raw( 'pick_table', $field['options'] ) ) ) {
2675
+ $field['pick_val'] = $field['options']['pick_table'];
2676
+ $field['pick_object'] = 'table';
2677
+ } elseif ( false === strpos( $field['pick_object'], '-' ) && ! in_array( $field['pick_object'], array(
2678
+ 'pod',
2679
+ 'post_type',
2680
+ 'taxonomy'
2681
+ ) ) ) {
2682
+ $field['pick_val'] = '';
2683
+ } elseif ( 'custom-simple' === $field['pick_object'] ) {
2684
+ $field['pick_val'] = '';
2685
+ }
2686
+
2687
+ $field['options']['pick_object'] = $field['pick_object'];
2688
+ $field['options']['pick_val'] = $field['pick_val'];
2689
+ $field['options']['sister_id'] = pods_var( 'sister_id', $field );
2690
+
2691
+ unset( $field['pick_object'] );
2692
+ unset( $field['pick_val'] );
2693
+
2694
+ if ( isset( $field['sister_id'] ) ) {
2695
+ unset( $field['sister_id'] );
2696
+ }
2697
+ }
2698
+
2699
+ $field['options'] = array_merge( $field['options'], $options );
2700
+
2701
+ $object_fields = (array) pods_var_raw( 'object_fields', $pod, array(), null, true );
2702
+
2703
+ if ( 0 < $old_id && defined( 'PODS_FIELD_STRICT' ) && ! PODS_FIELD_STRICT ) {
2704
+ $params->id = $old_id;
2705
+ $field['id'] = $old_id;
2706
+ }
2707
+
2708
+ // Add new field
2709
+ if ( ! isset( $params->id ) || empty( $params->id ) || empty( $field ) ) {
2710
+ if ( $table_operation && in_array( $field['name'], array(
2711
+ 'created',
2712
+ 'modified'
2713
+ ) ) && ! in_array( $field['type'], array(
2714
+ 'date',
2715
+ 'datetime'
2716
+ ) ) && ( ! defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) ) {
2717
+ return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field['name'] ), $this );
2718
+ }
2719
+
2720
+ if ( $table_operation && 'author' === $field['name'] && 'pick' !== $field['type'] && ( ! defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) ) {
2721
+ return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field['name'] ), $this );
2722
+ }
2723
+
2724
+ if ( in_array( $field['name'], $reserved_keywords, true ) ) {
2725
+ return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name', 'pods' ), $field['name'] ), $this );
2726
+ }
2727
+
2728
+ foreach ( $object_fields as $object_field => $object_field_opt ) {
2729
+ if ( $object_field == $field['name'] || in_array( $field['name'], $object_field_opt['alias'] ) ) {
2730
+ return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name. Also consider what WordPress and Pods provide you built-in.', 'pods' ), $field['name'] ), $this );
2731
+ }
2732
+ }
2733
+
2734
+ if ( in_array( $field['name'], array( 'rss' ) ) ) // Reserved post_name values that can't be used as field names
2735
+ {
2736
+ $field['name'] .= '2';
2737
+ }
2738
+
2739
+ if ( 'slug' === $field['type'] && true === $db ) {
2740
+ if ( in_array( $pod['type'], array( 'post_type', 'taxonomy', 'user' ) ) ) {
2741
+ return pods_error( __( 'This pod already has an internal WordPress permalink field', 'pods' ), $this );
2742
+ }
2743
+
2744
+ $slug_field = get_posts( array(
2745
+ 'post_type' => '_pods_field',
2746
+ 'orderby' => 'menu_order',
2747
+ 'order' => 'ASC',
2748
+ 'posts_per_page' => 1,
2749
+ 'post_parent' => $field['pod_id'],
2750
+ 'meta_query' => array(
2751
+ array(
2752
+ 'key' => 'type',
2753
+ 'value' => 'slug'
2754
+ )
2755
+ )
2756
+ ) );
2757
+
2758
+ if ( ! empty( $slug_field ) ) {
2759
+ return pods_error( __( 'This pod already has a permalink field', 'pods' ), $this );
2760
+ }
2761
+ }
2762
+
2763
+ // Sink the new field to the bottom of the list
2764
+ if ( null === $field['weight'] ) {
2765
+ $field['weight'] = 0;
2766
+
2767
+ $bottom_most_field = get_posts( array(
2768
+ 'post_type' => '_pods_field',
2769
+ 'orderby' => 'menu_order',
2770
+ 'order' => 'DESC',
2771
+ 'posts_per_page' => 1,
2772
+ 'post_parent' => $field['pod_id']
2773
+ ) );
2774
+
2775
+ if ( ! empty( $bottom_most_field ) ) {
2776
+ $field['weight'] = pods_absint( $bottom_most_field[0]->menu_order ) + 1;
2777
+ }
2778
+ }
2779
+
2780
+ $field['weight'] = pods_absint( $field['weight'] );
2781
+
2782
+ $post_data = array(
2783
+ 'post_name' => $field['name'],
2784
+ 'post_title' => $field['label'],
2785
+ 'post_content' => $field['description'],
2786
+ 'post_type' => '_pods_field',
2787
+ 'post_parent' => $field['pod_id'],
2788
+ 'post_status' => 'publish',
2789
+ 'menu_order' => $field['weight']
2790
+ );
2791
+ } else {
2792
+ if ( in_array( $field['name'], array( 'id', 'ID' ) ) ) {
2793
+ if ( null !== $old_name ) {
2794
+ return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field['name'] ), $this );
2795
+ } else {
2796
+ return pods_error( sprintf( __( '%s is not editable', 'pods' ), $field['name'] ), $this );
2797
+ }
2798
+ }
2799
+
2800
+ if ( null !== $old_name && $field['name'] !== $old_name && in_array( $field['name'], array(
2801
+ 'created',
2802
+ 'modified'
2803
+ ) ) && ! in_array( $field['type'], array(
2804
+ 'date',
2805
+ 'datetime'
2806
+ ) ) && ( ! defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) ) {
2807
+ return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field['name'] ), $this );
2808
+ }
2809
+
2810
+ if ( null !== $old_name && $field['name'] !== $old_name && 'author' === $field['name'] && 'pick' !== $field['type'] && ( ! defined( 'PODS_FIELD_STRICT' ) || PODS_FIELD_STRICT ) ) {
2811
+ return pods_error( sprintf( __( '%s is reserved for internal Pods usage, please try a different name', 'pods' ), $field['name'] ), $this );
2812
+ }
2813
+
2814
+ foreach ( $object_fields as $object_field => $object_field_opt ) {
2815
+ if ( $object_field !== $field['name'] && ! in_array( $field['name'], $object_field_opt['alias'] ) ) {
2816
+ continue;
2817
+ }
2818
+
2819
+ if ( null !== $old_name ) {
2820
+ return pods_error( sprintf( __( '%s is reserved for internal WordPress or Pods usage, please try a different name', 'pods' ), $field['name'] ), $this );
2821
+ } else {
2822
+ return pods_error( sprintf( __( '%s is not editable', 'pods' ), $field['name'] ), $this );
2823
+ }
2824
+ }
2825
+
2826
+ $post_data = array(
2827
+ 'ID' => $field['id'],
2828
+ 'post_name' => $field['name'],
2829
+ 'post_title' => $field['label'],
2830
+ 'post_content' => $field['description']
2831
+ );
2832
+
2833
+ if ( null !== $field['weight'] ) {
2834
+ $field['weight'] = pods_absint( $field['weight'] );
2835
+
2836
+ $post_data['menu_order'] = $field['weight'];
2837
+ }
2838
+ }
2839
+
2840
+ if ( true === $db ) {
2841
+ if ( ! has_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ) ) ) {
2842
+ add_filter( 'wp_unique_post_slug', array( $this, 'save_slug_fix' ), 100, 6 );
2843
+ }
2844
+
2845
+ $conflicted = false;
2846
+
2847
+ // Headway compatibility fix
2848
+ if ( has_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 ) ) {
2849
+ remove_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2850
+
2851
+ $conflicted = true;
2852
+ }
2853
+
2854
+ // Store the old field name
2855
+ if ( $old_name && $old_name !== $post_data['post_name'] ) {
2856
+ $field['options']['old_name'] = $old_name;
2857
+ }
2858
+
2859
+ $params->id = $this->save_wp_object( 'post', $post_data, $field['options'], true, true );
2860
+
2861
+ if ( $conflicted ) {
2862
+ add_filter( 'wp_insert_post_data', 'headway_clean_slug', 0 );
2863
+ }
2864
+
2865
+ if ( false === $params->id ) {
2866
+ return pods_error( __( 'Cannot save Field', 'pods' ), $this );
2867
+ }
2868
+ } else {
2869
+ $params->id = $field['name'];
2870
+ }
2871
+
2872
+ $field['id'] = $params->id;
2873
+
2874
+ $simple = ( 'pick' === $field['type'] && in_array( pods_v( 'pick_object', $field['options'] ), $simple_tableless_objects, true ) );
2875
+
2876
+ $definition = false;
2877
+
2878
+ if ( ! in_array( $field['type'], $tableless_field_types ) || $simple ) {
2879
+ $field_definition = $this->get_field_definition( $field['type'], array_merge( $field, $field['options'] ) );
2880
+
2881
+ if ( 0 < strlen( $field_definition ) ) {
2882
+ $definition = '`' . $field['name'] . '` ' . $field_definition;
2883
+ }
2884
+ }
2885
+
2886
+ $sister_id = (int) pods_var( 'sister_id', $field['options'], 0 );
2887
+
2888
+ $definition_mode = 'bypass';
2889
+
2890
+ if ( $table_operation && 'table' === $pod['storage'] && ! pods_tableless() ) {
2891
+ if ( ! empty( $old_id ) ) {
2892
+ if ( ( $field['type'] !== $old_type || $old_simple !== $simple ) && empty( $definition ) ) {
2893
+ $definition_mode = 'drop';
2894
+ } elseif ( 0 < strlen( $definition ) ) {
2895
+ if ( $old_name !== $field['name'] || $old_simple !== $simple || $old_definition !== $definition ) {
2896
+ $definition_mode = 'add';
2897
+
2898
+ if ( 0 < strlen( $old_definition ) ) {
2899
+ $definition_mode = 'change';
2900
+ }
2901
+ } elseif ( null !== $old_definition && $definition !== $old_definition ) {
2902
+ $definition_mode = 'change';
2903
+ }
2904
+ }
2905
+ } elseif ( 0 < strlen( $definition ) ) {
2906
+ $definition_mode = 'add';
2907
+
2908
+ if ( 0 < strlen( $old_definition ) ) {
2909
+ $definition_mode = 'change';
2910
+ }
2911
+ }
2912
+
2913
+ if ( 'bypass' !== $definition_mode ) {
2914
+ /**
2915
+ * Allow hooking into before the table has been altered for any custom functionality needed.
2916
+ *
2917
+ * @since 2.7.17
2918
+ *
2919
+ * @param string $definition_mode The definition mode used for the table field.
2920
+ * @param array $pod The pod object.
2921
+ * @param string $type The field type.
2922
+ * @param array $field The field object.
2923
+ * @param array $extra_info {
2924
+ * Extra information about the field.
2925
+ *
2926
+ * @type bool $simple Whether the field is a simple tableless field.
2927
+ * @type string $definition The field definition.
2928
+ * @type null|string $old_name The old field name (if preexisting).
2929
+ * @type null|string $old_definition The old field definition (if preexisting).
2930
+ * @type null|array $old_options The old field options (if preexisting).
2931
+ * }
2932
+ */
2933
+ do_action( 'pods_api_save_field_table_pre_alter', $definition_mode, $pod, $field['type'], $field, array(
2934
+ 'simple' => $simple,
2935
+ 'definition' => $definition,
2936
+ 'old_name' => $old_name,
2937
+ 'old_definition' => $old_definition,
2938
+ 'old_options' => $old_options,
2939
+ ) );
2940
+
2941
+ if ( 'drop' === $definition_mode ) {
2942
+ // Drop field column.
2943
+ pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$old_name}`", false );
2944
+ } elseif ( 'change' === $definition_mode ) {
2945
+ // Change field column definition.
2946
+ if ( $old_name && $old_name !== $field['name'] ) {
2947
+ $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` CHANGE `{$old_name}` {$definition}", false );
2948
+ } else {
2949
+ $test = pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` MODIFY {$definition}", false );
2950
+ }
2951
+
2952
+ if ( false === $test ) {
2953
+ $definition_mode = 'add';
2954
+ }
2955
+ }
2956
+
2957
+ // If the old field doesn't exist, continue to add a new field
2958
+ if ( 'add' === $definition_mode ) {
2959
+ pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` ADD COLUMN {$definition}", __( 'Cannot create new field', 'pods' ) );
2960
+ }
2961
+
2962
+ /**
2963
+ * Allow hooking into after the table has been altered for any custom functionality needed.
2964
+ *
2965
+ * @since 2.7.17
2966
+ *
2967
+ * @param string $definition_mode The definition mode used for the table field.
2968
+ * @param array $pod The pod object.
2969
+ * @param string $type The field type.
2970
+ * @param array $field The field object.
2971
+ * @param array $extra_info {
2972
+ * Extra information about the field.
2973
+ *
2974
+ * @type bool $simple Whether the field is a simple tableless field.
2975
+ * @type string $definition The field definition.
2976
+ * @type null|string $old_name The old field name (if preexisting).
2977
+ * @type null|string $old_definition The old field definition (if preexisting).
2978
+ * @type null|array $old_options The old field options (if preexisting).
2979
+ * }
2980
+ */
2981
+ do_action( 'pods_api_save_field_table_altered', $definition_mode, $pod, $field['type'], $field, array(
2982
+ 'simple' => $simple,
2983
+ 'definition' => $definition,
2984
+ 'old_name' => $old_name,
2985
+ 'old_definition' => $old_definition,
2986
+ 'old_options' => $old_options,
2987
+ ) );
2988
+ }
2989
+ }
2990
+
2991
+ if ( ! empty( $old_id ) && 'meta' === $pod['storage'] && $old_name !== $field['name'] && $pod['meta_table'] !== $pod['table'] ) {
2992
+ $prepare = array(
2993
+ $field['name'],
2994
+ $old_name
2995
+ );
2996
+
2997
+ // Users don't have a type
2998
+ if ( ! empty( $pod['field_type'] ) ) {
2999
+ $prepare[] = $pod['name'];
3000
+ }
3001
+
3002
+ $join_table = $pod['table'];
3003
+
3004
+ // Taxonomies are the odd type out, terrible I know
3005
+ if ( 'taxonomy' === $pod['type'] ) {
3006
+ // wp_term_taxonomy has the 'taxonomy' field we need to limit by
3007
+ $join_table = $wpdb->term_taxonomy;
3008
+ }
3009
+
3010
+ pods_query( "
3011
+ UPDATE `{$pod[ 'meta_table' ]}` AS `m`
3012
+ LEFT JOIN `{$join_table}` AS `t`
3013
+ ON `t`.`{$pod[ 'field_id' ]}` = `m`.`{$pod[ 'meta_field_id' ]}`
3014
+ SET
3015
+ `m`.`{$pod[ 'meta_field_index' ]}` = %s
3016
+ WHERE
3017
+ `m`.`{$pod[ 'meta_field_index' ]}` = %s
3018
+ " . ( ! empty( $pod['field_type'] ) ? " AND `t`.`{$pod[ 'field_type' ]}` = %s" : "" ), $prepare );
3019
+ }
3020
+
3021
+ if ( $field['type'] !== $old_type && in_array( $old_type, $tableless_field_types ) ) {
3022
+ delete_post_meta( $old_sister_id, 'sister_id' );
3023
+
3024
+ if ( true === $db ) {
3025
+ pods_query( "
3026
+ DELETE pm
3027
+ FROM {$wpdb->postmeta} AS pm
3028
+ LEFT JOIN {$wpdb->posts} AS p
3029
+ ON p.post_type = '_pods_field'
3030
+ AND p.ID = pm.post_id
3031
+ WHERE
3032
+ p.ID IS NOT NULL
3033
+ AND pm.meta_key = 'sister_id'
3034
+ AND pm.meta_value = %d
3035
+ ", array(
3036
+ $params->id
3037
+ ) );
3038
+
3039
+ if ( ! pods_tableless() ) {
3040
+ pods_query( "DELETE FROM @wp_podsrel WHERE `field_id` = {$params->id}", false );
3041
+
3042
+ pods_query( "
3043
+ UPDATE `@wp_podsrel`
3044
+ SET `related_field_id` = 0
3045
+ WHERE `field_id` = %d
3046
+ ", array(
3047
+ $old_sister_id
3048
+ ) );
3049
+ }
3050
+ }
3051
+ } elseif ( 0 < $sister_id ) {
3052
+ update_post_meta( $sister_id, 'sister_id', $params->id );
3053
+
3054
+ if ( true === $db && ( ! pods_tableless() ) ) {
3055
+ pods_query( "
3056
+ UPDATE `@wp_podsrel`
3057
+ SET `related_field_id` = %d
3058
+ WHERE `field_id` = %d
3059
+ ", array(
3060
+ $params->id,
3061
+ $sister_id
3062
+ ) );
3063
+ }
3064
+ } elseif ( 0 < $old_sister_id ) {
3065
+ delete_post_meta( $old_sister_id, 'sister_id' );
3066
+
3067
+ if ( true === $db && ( ! pods_tableless() ) ) {
3068
+ pods_query( "
3069
+ UPDATE `@wp_podsrel`
3070
+ SET `related_field_id` = 0
3071
+ WHERE `field_id` = %d
3072
+ ", array(
3073
+ $old_sister_id
3074
+ ) );
3075
+ }
3076
+ }
3077
+
3078
+ if ( ! empty( $old_id ) && $old_name !== $field['name'] && true === $db ) {
3079
+ pods_query( "
3080
+ UPDATE `@wp_postmeta`
3081
+ SET `meta_value` = %s
3082
+ WHERE
3083
+ `post_id` = %d
3084
+ AND `meta_key` = 'pod_index'
3085
+ AND `meta_value` = %s
3086
+ ", array(
3087
+ $field['name'],
3088
+ $pod['id'],
3089
+ $old_name
3090
+ ) );
3091
+ }
3092
+
3093
+ if ( ! $save_pod ) {
3094
+ $this->cache_flush_pods( $pod );
3095
+ } else {
3096
+ pods_transient_clear( 'pods_field_' . $pod['name'] . '_' . $field['name'] );
3097
+
3098
+ if ( ! empty( $old_id ) && $old_name !== $field['name'] ) {
3099
+ pods_transient_clear( 'pods_field_' . $pod['name'] . '_' . $old_name );
3100
+ }
3101
+ }
3102
+
3103
+ if ( true === $db ) {
3104
+ return $params->id;
3105
+ } else {
3106
+ return $field;
3107
+ }
3108
+ }
3109
+
3110
+ /**
3111
+ * Fix Pod / Field post_name to ensure they are exactly as saved (allow multiple posts w/ same post_name)
3112
+ *
3113
+ * @param string $slug Unique slug value
3114
+ * @param int $post_ID Post ID
3115
+ * @param string $post_status Post Status
3116
+ * @param string $post_type Post Type
3117
+ * @param int $post_parent Post Parent ID
3118
+ * @param string $original_slug Original slug value
3119
+ *
3120
+ * @return string Final slug value
3121
+ *
3122
+ * @since 2.3.3
3123
+ */
3124
+ public function save_slug_fix( $slug, $post_ID, $post_status, $post_type, $post_parent = 0, $original_slug = null ) {
3125
+
3126
+ if ( in_array( $post_type, array( '_pods_field', '_pods_pod' ) ) && false !== strpos( $slug, '-' ) ) {
3127
+ $slug = $original_slug;
3128
+ }
3129
+
3130
+ return $slug;
3131
+ }
3132
+
3133
+ /**
3134
+ * Add or Edit a Pods Object
3135
+ *
3136
+ * $params['id'] int The Object ID
3137
+ * $params['name'] string The Object name
3138
+ * $params['type'] string The Object type
3139
+ * $params['options'] Associative array of Object options
3140
+ *
3141
+ * @param array|object $params An associative array of parameters
3142
+ * @param bool $sanitized (optional) Decides whether the params have been sanitized before being passed,
3143
+ * will sanitize them if false.
3144
+ *
3145
+ * @return int The Object ID
3146
+ * @since 2.0.0
3147
+ */
3148
+ public function save_object( $params, $sanitized = false ) {
3149
+
3150
+ $params = (object) $params;
3151
+
3152
+ if ( false === $sanitized ) {
3153
+ $params = pods_sanitize( $params );
3154
+ }
3155
+
3156
+ if ( ! isset( $params->name ) || empty( $params->name ) ) {
3157
+ return pods_error( __( 'Name must be given to save an Object', 'pods' ), $this );
3158
+ }
3159
+
3160
+ if ( ! isset( $params->type ) || empty( $params->type ) ) {
3161
+ return pods_error( __( 'Type must be given to save an Object', 'pods' ), $this );
3162
+ }
3163
+
3164
+ $object = array(
3165
+ 'id' => 0,
3166
+ 'name' => $params->name,
3167
+ 'type' => $params->type,
3168
+ 'code' => '',
3169
+ 'options' => array()
3170
+ );
3171
+
3172
+ // Setup options
3173
+ $options = get_object_vars( $params );
3174
+
3175
+ if ( isset( $options['method'] ) ) {
3176
+ unset( $options['method'] );
3177
+ }
3178
+
3179
+ $exclude = array(
3180
+ 'id',
3181
+ 'name',
3182
+ 'helper_type',
3183
+ 'code',
3184
+ 'options',
3185
+ 'status'
3186
+ );
3187
+
3188
+ foreach ( $exclude as $k => $exclude_field ) {
3189
+ $aliases = array( $exclude_field );
3190
+
3191
+ if ( is_array( $exclude_field ) ) {
3192
+ $aliases = array_merge( array( $k ), $exclude_field );
3193
+ $exclude_field = $k;
3194
+ }
3195
+
3196
+ foreach ( $aliases as $alias ) {
3197
+ if ( isset( $options[ $alias ] ) ) {
3198
+ $object[ $exclude_field ] = pods_trim( $options[ $alias ] );
3199
+
3200
+ unset( $options[ $alias ] );
3201
+ }
3202
+ }
3203
+ }
3204
+
3205
+ if ( 'helper' === $object['type'] ) {
3206
+ $object['options']['helper_type'] = $object['helper_type'];
3207
+ }
3208
+
3209
+ if ( isset( $object['options']['code'] ) ) {
3210
+ unset( $object['options']['code'] );
3211
+ }
3212
+
3213
+ $object['options'] = array_merge( $object['options'], $options );
3214
+
3215
+ $post_data = array(
3216
+ 'post_name' => pods_clean_name( $object['name'], true ),
3217
+ 'post_title' => $object['name'],
3218
+ 'post_content' => $object['code'],
3219
+ 'post_type' => '_pods_' . $object['type'],
3220
+ 'post_status' => 'publish'
3221
+ );
3222
+
3223
+ if ( ! empty( $object['id'] ) ) {
3224
+ $post_data['ID'] = $object['id'];
3225
+ }
3226
+
3227
+ if ( null !== pods_var( 'status', $object, null, null, true ) ) {
3228
+ $post_data['post_status'] = pods_var( 'status', $object, null, null, true );
3229
+ }
3230
+
3231
+ remove_filter( 'content_save_pre', 'balanceTags', 50 );
3232
+
3233
+ $post_data = pods_sanitize( $post_data );
3234
+
3235
+ $params->id = $this->save_post( $post_data, $object['options'], true, true );
3236
+
3237
+ pods_transient_clear( 'pods_objects_' . $params->type );
3238
+ pods_transient_clear( 'pods_objects_' . $params->type . '_get' );
3239
+
3240
+ return $params->id;
3241
+ }
3242
+
3243
+ /**
3244
+ * @see PodsAPI::save_object
3245
+ *
3246
+ * Add or edit a Pod Template
3247
+ *
3248
+ * $params['id'] int The template ID
3249
+ * $params['name'] string The template name
3250
+ * $params['code'] string The template code
3251
+ *
3252
+ * @param array|object $params An associative array of parameters
3253
+ * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed,
3254
+ * will sanitize them if false.
3255
+ *
3256
+ * @return int The Template ID
3257
+ *
3258
+ * @since 1.7.9
3259
+ */
3260
+ public function save_template( $params, $sanitized = false ) {
3261
+
3262
+ $params = (object) $params;
3263
+
3264
+ $params->type = 'template';
3265
+
3266
+ return $this->save_object( $params, $sanitized );
3267
+ }
3268
+
3269
+ /**
3270
+ * @see PodsAPI::save_object
3271
+ *
3272
+ * Add or edit a Pod Page
3273
+ *
3274
+ * $params['id'] int The page ID
3275
+ * $params['name'] string The page URI
3276
+ * $params['code'] string The page code
3277
+ *
3278
+ * @param array|object $params An associative array of parameters
3279
+ * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed,
3280
+ * will sanitize them if false.
3281
+ *
3282
+ * @return int The page ID
3283
+ * @since 1.7.9
3284
+ */
3285
+ public function save_page( $params, $sanitized = false ) {
3286
+
3287
+ $params = (object) $params;
3288
+
3289
+ if ( ! isset( $params->name ) ) {
3290
+ $params->name = $params->uri;
3291
+ unset( $params->uri );
3292
+ }
3293
+
3294
+ if ( isset( $params->phpcode ) ) {
3295
+ $params->code = $params->phpcode;
3296
+ unset( $params->phpcode );
3297
+ }
3298
+
3299
+ $params->name = trim( $params->name, '/' );
3300
+ $params->type = 'page';
3301
+
3302
+ return $this->save_object( $params, $sanitized );
3303
+ }
3304
+
3305
+ /**
3306
+ * @see PodsAPI::save_object
3307
+ *
3308
+ * Add or edit a Pod Helper
3309
+ *
3310
+ * $params['id'] int The helper ID
3311
+ * $params['name'] string The helper name
3312
+ * $params['helper_type'] string The helper type ("pre_save", "display", etc)
3313
+ * $params['code'] string The helper code
3314
+ *
3315
+ * @param array $params An associative array of parameters
3316
+ * @param bool $sanitized (optional) Decides wether the params have been sanitized before being passed, will
3317
+ * sanitize them if false.
3318
+ *
3319
+ * @return int The helper ID
3320
+ * @since 1.7.9
3321
+ */
3322
+ public function save_helper( $params, $sanitized = false ) {
3323
+
3324
+ $params = (object) $params;
3325
+
3326
+ if ( isset( $params->phpcode ) ) {
3327
+ $params->code = $params->phpcode;
3328
+ unset( $params->phpcode );
3329
+ }
3330
+
3331
+ if ( isset( $params->type ) ) {
3332
+ $params->helper_type = $params->type;
3333
+ unset( $params->type );
3334
+ }
3335
+
3336
+ $params->type = 'helper';
3337
+
3338
+ return $this->save_object( $params, $sanitized );
3339
+ }
3340
+
3341
+ /**
3342
+ * Add or edit a single pod item
3343
+ *
3344
+ * $params['pod'] string The Pod name (pod or pod_id is required)
3345
+ * $params['pod_id'] string The Pod ID (pod or pod_id is required)
3346
+ * $params['id'] int|array The item ID, or an array of item IDs to save data for
3347
+ * $params['data'] array (optional) Associative array of field names + values
3348
+ * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
3349
+ * $params['track_changed_fields'] bool Set to true to enable tracking of saved fields via
3350
+ * PodsAPI::get_changed_fields()
3351
+ * $params['error_mode'] string Throw an 'exception', 'exit' with the message, return 'false', or return 'wp_error'
3352
+ *
3353
+ * @param array|object $params An associative array of parameters
3354
+ *
3355
+ * @return int|array The item ID, or an array of item IDs (if `id` is an array if IDs)
3356
+ *
3357
+ * @since 1.7.9
3358
+ */
3359
+ public function save_pod_item( $params ) {
3360
+
3361
+ global $wpdb;
3362
+
3363
+ $params = (object) pods_str_replace( '@wp_', '{prefix}', $params );
3364
+
3365
+ $tableless_field_types = PodsForm::tableless_field_types();
3366
+ $repeatable_field_types = PodsForm::repeatable_field_types();
3367
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
3368
+
3369
+ $error_mode = $this->display_errors;
3370
+
3371
+ if ( ! empty( $params->error_mode ) ) {
3372
+ $error_mode = $params->error_mode;
3373
+ }
3374
+
3375
+ // @deprecated 2.0.0
3376
+ if ( isset( $params->datatype ) ) {
3377
+ pods_deprecated( '$params->pod instead of $params->datatype', '2.0' );
3378
+
3379
+ $params->pod = $params->datatype;
3380
+
3381
+ unset( $params->datatype );
3382
+
3383
+ if ( isset( $params->pod_id ) ) {
3384
+ pods_deprecated( '$params->id instead of $params->pod_id', '2.0' );
3385
+
3386
+ $params->id = $params->pod_id;
3387
+
3388
+ unset( $params->pod_id );
3389
+ }
3390
+
3391
+ if ( isset( $params->data ) && ! empty( $params->data ) && is_array( $params->data ) ) {
3392
+ $check = current( $params->data );
3393
+
3394
+ if ( is_array( $check ) ) {
3395
+ pods_deprecated( 'PodsAPI::save_pod_items', '2.0' );
3396
+
3397
+ return $this->save_pod_items( $params, $params->data );
3398
+ }
3399
+ }
3400
+ }
3401
+
3402
+ // @deprecated 2.0.0
3403
+ if ( isset( $params->tbl_row_id ) ) {
3404
+ pods_deprecated( '$params->id instead of $params->tbl_row_id', '2.0' );
3405
+
3406
+ $params->id = $params->tbl_row_id;
3407
+
3408
+ unset( $params->tbl_row_id );
3409
+ }
3410
+
3411
+ // @deprecated 2.0.0
3412
+ if ( isset( $params->columns ) ) {
3413
+ pods_deprecated( '$params->data instead of $params->columns', '2.0' );
3414
+
3415
+ $params->data = $params->columns;
3416
+
3417
+ unset( $params->columns );
3418
+ }
3419
+
3420
+ if ( ! isset( $params->pod ) ) {
3421
+ $params->pod = false;
3422
+ }
3423
+ if ( isset( $params->pod_id ) ) {
3424
+ $params->pod_id = pods_absint( $params->pod_id );
3425
+ } else {
3426
+ $params->pod_id = 0;
3427
+ }
3428
+
3429
+ if ( isset( $params->id ) ) {
3430
+ $params->id = pods_absint( $params->id );
3431
+ } else {
3432
+ $params->id = 0;
3433
+ }
3434
+
3435
+ if ( ! isset( $params->from ) ) {
3436
+ $params->from = 'save';
3437
+ }
3438
+
3439
+ if ( ! isset( $params->location ) ) {
3440
+ $params->location = null;
3441
+ }
3442
+
3443
+ if ( ! isset( $params->track_changed_fields ) ) {
3444
+ $params->track_changed_fields = false;
3445
+ }
3446
+
3447
+ $pod_name = $params->pod;
3448
+ /**
3449
+ * Override $params['track_changed_fields']
3450
+ *
3451
+ * Use for globally setting field change tracking.
3452
+ *
3453
+ * @param bool
3454
+ *
3455
+ * @since 2.3.19
3456
+ */
3457
+ $track_changed_fields = apply_filters( "pods_api_save_pod_item_track_changed_fields_{$pod_name}", (boolean) $params->track_changed_fields, $params );
3458
+
3459
+ $changed_fields = array();
3460
+
3461
+ if ( ! isset( $params->clear_slug_cache ) ) {
3462
+ $params->clear_slug_cache = true;
3463
+ }
3464
+
3465
+ // Support for bulk edit
3466
+ if ( isset( $params->id ) && ! empty( $params->id ) && is_array( $params->id ) ) {
3467
+ $ids = array();
3468
+ $new_params = $params;
3469
+
3470
+ foreach ( $params->id as $id ) {
3471
+ $new_params->id = $id;
3472
+
3473
+ $ids[] = $this->save_pod_item( $new_params );
3474
+ }
3475
+
3476
+ return $ids;
3477
+ }
3478
+
3479
+ // Allow Helpers to know what's going on, are we adding or saving?
3480
+ $is_new_item = false;
3481
+
3482
+ if ( empty( $params->id ) ) {
3483
+ $is_new_item = true;
3484
+ }
3485
+
3486
+ if ( isset( $params->is_new_item ) ) {
3487
+ $is_new_item = (boolean) $params->is_new_item;
3488
+ }
3489
+
3490
+ // Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls
3491
+ $bypass_helpers = false;
3492
+
3493
+ if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers ) {
3494
+ $bypass_helpers = true;
3495
+ }
3496
+
3497
+ // Allow Custom Fields not defined by Pods to be saved
3498
+ $allow_custom_fields = false;
3499
+
3500
+ if ( isset( $params->allow_custom_fields ) && false !== $params->allow_custom_fields ) {
3501
+ $allow_custom_fields = true;
3502
+ }
3503
+
3504
+ // Get array of Pods
3505
+ $pod = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod, 'table_info' => true ) );
3506
+
3507
+ if ( false === $pod ) {
3508
+ return pods_error( __( 'Pod not found', 'pods' ), $error_mode );
3509
+ }
3510
+
3511
+ $params->pod = $pod['name'];
3512
+ $params->pod_id = $pod['id'];
3513
+
3514
+ if ( 'settings' === $pod['type'] ) {
3515
+ $params->id = $pod['id'];
3516
+ }
3517
+
3518
+ $fields = $pod['fields'];
3519
+
3520
+ $object_fields = (array) pods_var_raw( 'object_fields', $pod, array(), null, true );
3521
+
3522
+ $fields_active = array();
3523
+ $custom_data = array();
3524
+ $custom_fields = array();
3525
+
3526
+ // Find the active fields (loop through $params->data to retain order)
3527
+ if ( ! empty( $params->data ) && is_array( $params->data ) ) {
3528
+ foreach ( $params->data as $field => $value ) {
3529
+ if ( isset( $object_fields[ $field ] ) ) {
3530
+ $object_fields[ $field ]['value'] = $value;
3531
+ $fields_active[] = $field;
3532
+ } elseif ( isset( $fields[ $field ] ) ) {
3533
+ if ( 'save' === $params->from || true === PodsForm::permission( $fields[ $field ]['type'], $field, $fields[ $field ], $fields, $pod, $params->id, $params ) ) {
3534
+ $fields[ $field ]['value'] = $value;
3535
+ $fields_active[] = $field;
3536
+ } elseif ( ! pods_has_permissions( $fields[ $field ]['options'] ) && pods_var( 'hidden', $fields[ $field ]['options'], false ) ) {
3537
+ $fields[ $field ]['value'] = $value;
3538
+ $fields_active[] = $field;
3539
+ }
3540
+ } else {
3541
+ $found = false;
3542
+
3543
+ foreach ( $object_fields as $object_field => $object_field_opt ) {
3544
+ if ( in_array( $field, $object_field_opt['alias'] ) ) {
3545
+ $object_fields[ $object_field ]['value'] = $value;
3546
+ $fields_active[] = $object_field;
3547
+
3548
+ $found = true;
3549
+
3550
+ break;
3551
+ }
3552
+ }
3553
+
3554
+ if ( $allow_custom_fields && ! $found ) {
3555
+ $custom_fields[] = $field;
3556
+ }
3557
+ }
3558
+ }
3559
+
3560
+ if ( $allow_custom_fields && ! empty( $custom_fields ) ) {
3561
+ foreach ( $custom_fields as $field ) {
3562
+ $custom_data[ $field ] = $params->data[ $field ];
3563
+ }
3564
+ }
3565
+
3566
+ unset( $params->data );
3567
+ }
3568
+
3569
+ if ( empty( $params->id ) && ! in_array( 'created', $fields_active ) && isset( $fields['created'] ) && in_array( $fields['created']['type'], array(
3570
+ 'date',
3571
+ 'datetime'
3572
+ ) ) ) {
3573
+ $fields['created']['value'] = current_time( 'mysql' );
3574
+ $fields_active[] = 'created';
3575
+ }
3576
+
3577
+ if ( ! in_array( 'modified', $fields_active ) && isset( $fields['modified'] ) && in_array( $fields['modified']['type'], array(
3578
+ 'date',
3579
+ 'datetime'
3580
+ ) ) ) {
3581
+ $fields['modified']['value'] = current_time( 'mysql' );
3582
+ $fields_active[] = 'modified';
3583
+ }
3584
+
3585
+ if ( in_array( $pod['type'], array(
3586
+ 'pod',
3587
+ 'table'
3588
+ ) ) && empty( $params->id ) && ! empty( $pod['pod_field_index'] ) && in_array( $pod['pod_field_index'], $fields_active ) && ! in_array( $pod['pod_field_slug'], $fields_active ) && isset( $fields[ $pod['pod_field_slug'] ] ) ) {
3589
+ $fields[ $pod['pod_field_slug'] ]['value'] = ''; // this will get picked up by slug pre_save method
3590
+ $fields_active[] = $pod['pod_field_slug'];
3591
+ }
3592
+
3593
+ // Handle hidden fields
3594
+ if ( empty( $params->id ) ) {
3595
+ foreach ( $fields as $field => $field_data ) {
3596
+ if ( in_array( $field, $fields_active ) ) {
3597
+ continue;
3598
+ }
3599
+
3600
+ if ( in_array( $params->from, array(
3601
+ 'save',
3602
+ 'process_form'
3603
+ ) ) || true === PodsForm::permission( $fields[ $field ]['type'], $field, $fields[ $field ], $fields, $pod, $params->id, $params ) ) {
3604
+ $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data['type'], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3605
+
3606
+ if ( null !== $value && '' !== $value && false !== $value ) {
3607
+ $fields[ $field ]['value'] = $value;
3608
+ $fields_active[] = $field;
3609
+ }
3610
+ }
3611
+ }
3612
+
3613
+ // Set default field values for object fields
3614
+ if ( ! empty( $object_fields ) ) {
3615
+ foreach ( $object_fields as $field => $field_data ) {
3616
+ if ( in_array( $field, $fields_active ) ) {
3617
+ continue;
3618
+ } elseif ( ! isset( $field_data['default'] ) || strlen( $field_data['default'] ) < 1 ) {
3619
+ continue;
3620
+ }
3621
+
3622
+ $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data['type'], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3623
+
3624
+ if ( null !== $value && '' !== $value && false !== $value ) {
3625
+ $object_fields[ $field ]['value'] = $value;
3626
+ $fields_active[] = $field;
3627
+ }
3628
+ }
3629
+ }
3630
+
3631
+ // Set default field values for Pod fields
3632
+ foreach ( $fields as $field => $field_data ) {
3633
+ if ( in_array( $field, $fields_active ) ) {
3634
+ continue;
3635
+ } elseif ( ! isset( $field_data['default'] ) || strlen( $field_data['default'] ) < 1 ) {
3636
+ continue;
3637
+ }
3638
+
3639
+ $value = PodsForm::default_value( pods_var_raw( $field, 'post' ), $field_data['type'], $field, pods_var_raw( 'options', $field_data, $field_data, null, true ), $pod, $params->id );
3640
+
3641
+ if ( null !== $value && '' !== $value && false !== $value ) {
3642
+ $fields[ $field ]['value'] = $value;
3643
+ $fields_active[] = $field;
3644
+ }
3645
+ }
3646
+ }
3647
+
3648
+ $columns =& $fields; // @deprecated 2.0.0
3649
+ $active_columns =& $fields_active; // @deprecated 2.0.0
3650
+ $params->tbl_row_id =& $params->id; // @deprecated 2.0.0
3651
+
3652
+ $pre_save_helpers = array();
3653
+ $post_save_helpers = array();
3654
+
3655
+ $pieces = array(
3656
+ 'fields',
3657
+ 'params',
3658
+ 'pod',
3659
+ 'fields_active',
3660
+ 'object_fields',
3661
+ 'custom_fields',
3662
+ 'custom_data',
3663
+ 'track_changed_fields',
3664
+ 'changed_fields'
3665
+ );
3666
+
3667
+ if ( $track_changed_fields ) {
3668
+ self::handle_changed_fields( $params->pod, $params->id, 'set' );
3669
+ }
3670
+
3671
+ if ( false === $bypass_helpers ) {
3672
+ // Plugin hooks
3673
+ $hooked = $this->do_hook( 'pre_save_pod_item', compact( $pieces ), $is_new_item, $params->id );
3674
+
3675
+ if ( is_array( $hooked ) && ! empty( $hooked ) ) {
3676
+ extract( $hooked );
3677
+ }
3678
+
3679
+ $hooked = $this->do_hook( "pre_save_pod_item_{$params->pod}", compact( $pieces ), $is_new_item, $params->id );
3680
+
3681
+ if ( is_array( $hooked ) && ! empty( $hooked ) ) {
3682
+ extract( $hooked );
3683
+ }
3684
+
3685
+ if ( $is_new_item ) {
3686
+ $hooked = $this->do_hook( 'pre_create_pod_item', compact( $pieces ) );
3687
+
3688
+ if ( is_array( $hooked ) && ! empty( $hooked ) ) {
3689
+ extract( $hooked );
3690
+ }
3691
+
3692
+ $hooked = $this->do_hook( "pre_create_pod_item_{$params->pod}", compact( $pieces ) );
3693
+
3694
+ if ( is_array( $hooked ) && ! empty( $hooked ) ) {
3695
+ extract( $hooked );
3696
+ }
3697
+ } else {
3698
+ $hooked = $this->do_hook( 'pre_edit_pod_item', compact( $pieces ), $params->id );
3699
+
3700
+ if ( is_array( $hooked ) && ! empty( $hooked ) ) {
3701
+ extract( $hooked );
3702
+ }
3703
+
3704
+ $hooked = $this->do_hook( "pre_edit_pod_item_{$params->pod}", compact( $pieces ), $params->id );
3705
+
3706
+ if ( is_array( $hooked ) && ! empty( $hooked ) ) {
3707
+ extract( $hooked );
3708
+ }
3709
+ }
3710
+
3711
+ // Call any pre-save helpers (if not bypassed)
3712
+ if ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) {
3713
+ if ( ! empty( $pod['options'] ) && is_array( $pod['options'] ) ) {
3714
+ $helpers = array( 'pre_save_helpers', 'post_save_helpers' );
3715
+
3716
+ foreach ( $helpers as $helper ) {
3717
+ if ( isset( $pod['options'][ $helper ] ) && ! empty( $pod['options'][ $helper ] ) ) {
3718
+ ${$helper} = explode( ',', $pod['options'][ $helper ] );
3719
+ }
3720
+ }
3721
+ }
3722
+
3723
+ if ( ! empty( $pre_save_helpers ) ) {
3724
+ pods_deprecated( sprintf( __( 'Pre-save helpers are deprecated, use the action pods_pre_save_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
3725
+
3726
+ foreach ( $pre_save_helpers as $helper ) {
3727
+ $helper = $this->load_helper( array( 'name' => $helper ) );
3728
+
3729
+ if ( false !== $helper ) {
3730
+ eval( '?>' . $helper['code'] );
3731
+ }
3732
+ }
3733
+ }
3734
+ }
3735
+ }
3736
+
3737
+ $table_data = array();
3738
+ $table_formats = array();
3739
+ $update_values = array();
3740
+ $rel_fields = array();
3741
+ $rel_field_ids = array();
3742
+
3743
+ $object_type = $pod['type'];
3744
+
3745
+ $object_ID = 'ID';
3746
+
3747
+ if ( 'comment' === $object_type ) {
3748
+ $object_ID = 'comment_ID';
3749
+ } elseif ( 'taxonomy' === $object_type ) {
3750
+ $object_ID = 'term_id';
3751
+ }
3752
+
3753
+ $object_data = array();
3754
+ $object_meta = array();
3755
+ $post_term_data = array();
3756
+
3757
+ if ( 'settings' === $object_type ) {
3758
+ $object_data['option_id'] = $pod['name'];
3759
+ } elseif ( ! empty( $params->id ) ) {
3760
+ $object_data[ $object_ID ] = $params->id;
3761
+ }
3762
+
3763
+ $fields_active = array_unique( $fields_active );
3764
+
3765
+ // Loop through each active field, validating and preparing the table data
3766
+ foreach ( $fields_active as $field ) {
3767
+ if ( isset( $object_fields[ $field ] ) ) {
3768
+ $field_data = $object_fields[ $field ];
3769
+ } elseif ( isset( $fields[ $field ] ) ) {
3770
+ $field_data = $fields[ $field ];
3771
+ } else {
3772
+ continue;
3773
+ }
3774
+
3775
+ $value = $field_data['value'];
3776
+ $type = $field_data['type'];
3777
+ $options = pods_var( 'options', $field_data, array() );
3778
+
3779
+ // WPML AJAX compatibility
3780
+ if ( is_admin()
3781
+ && ( isset( $_POST['action'] ) && 'wpml_save_job_ajax' === $_POST['action'] )
3782
+ || ( isset( $_GET['page'] ) && false !== strpos( $_GET['page'], '/menu/languages.php' )
3783
+ && isset( $_POST['icl_ajx_action'] ) && isset( $_POST['_icl_nonce'] )
3784
+ && wp_verify_nonce( $_POST['_icl_nonce'], $_POST['icl_ajx_action'] . '_nonce' ) )
3785
+ ) {
3786
+ $options['unique'] = 0;
3787
+ $fields[ $field ]['options']['unique'] = 0;
3788
+ $options['required'] = 0;
3789
+ $fields[ $field ]['options']['required'] = 0;
3790
+ } else {
3791
+ // Validate value
3792
+ $validate = $this->handle_field_validation( $value, $field, $object_fields, $fields, $pod, $params );
3793
+
3794
+ if ( false === $validate ) {
3795
+ $validate = sprintf( __( 'There was an issue validating the field %s', 'pods' ), $field_data['label'] );
3796
+ } elseif ( true !== $validate ) {
3797
+ $validate = (array) $validate;
3798
+ }
3799
+
3800
+ if ( ! is_bool( $validate ) && ! empty( $validate ) ) {
3801
+ return pods_error( $validate, $error_mode );
3802
+ }
3803
+ }
3804
+
3805
+ $value = PodsForm::pre_save( $field_data['type'], $value, $params->id, $field, array_merge( $field_data, $options ), array_merge( $fields, $object_fields ), $pod, $params );
3806
+
3807
+ $field_data['value'] = $value;
3808
+
3809
+ if ( isset( $object_fields[ $field ] ) ) {
3810
+ // @todo Eventually support 'comment' field type saving here too
3811
+ if ( 'taxonomy' === $object_fields[ $field ]['type'] ) {
3812
+ $post_term_data[ $field ] = $value;
3813
+ } else {
3814
+ $object_data[ $field ] = $value;
3815
+ }
3816
+ } else {
3817
+ $simple = ( 'pick' === $type && in_array( pods_var( 'pick_object', $field_data ), $simple_tableless_objects ) );
3818
+ $simple = (boolean) $this->do_hook( 'tableless_custom', $simple, $field_data, $field, $fields, $pod, $params );
3819
+
3820
+ // Handle Simple Relationships
3821
+ if ( $simple ) {
3822
+ if ( ! is_array( $value ) ) {
3823
+ if ( 0 < strlen( $value ) ) {
3824
+ $value = array( $value );
3825
+ } else {
3826
+ $value = array();
3827
+ }
3828
+ }
3829
+
3830
+ $pick_limit = (int) pods_var_raw( 'pick_limit', $options, 0 );
3831
+
3832
+ if ( 'single' === pods_var_raw( 'pick_format_type', $options ) ) {
3833
+ $pick_limit = 1;
3834
+ }
3835
+
3836
+ if ( 'custom-simple' === pods_var( 'pick_object', $field_data ) ) {
3837
+ $custom = pods_var_raw( 'pick_custom', $options, '' );
3838
+
3839
+ $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $field_data['name'], $value, array_merge( $field_data, $options ), $pod, $params->id );
3840
+
3841
+ // Input values are unslashed. Unslash database values as well to ensure correct comparison.
3842
+ $custom = pods_unslash( $custom );
3843
+
3844
+ if ( empty( $value ) || empty( $custom ) ) {
3845
+ $value = '';
3846
+ } elseif ( ! empty( $custom ) ) {
3847
+ if ( ! is_array( $custom ) ) {
3848
+ $custom = explode( "\n", $custom );
3849
+
3850
+ $custom_values = array();
3851
+
3852
+ foreach ( $custom as $c => $cv ) {
3853
+ if ( 0 < strlen( $cv ) ) {
3854
+ $custom_label = explode( '|', $cv );
3855
+
3856
+ if ( ! isset( $custom_label[1] ) ) {
3857
+ $custom_label[1] = $custom_label[0];
3858
+ }
3859
+
3860
+ $custom_label[0] = trim( (string) $custom_label[0] );
3861
+ $custom_label[1] = trim( (string) $custom_label[1] );
3862
+ $custom_values[ $custom_label[0] ] = $custom_label[1];
3863
+ }
3864
+ }
3865
+ } else {
3866
+ $custom_values = $custom;
3867
+ }
3868
+
3869
+ $values = array();
3870
+
3871
+ foreach ( $value as $k => $v ) {
3872
+ $v = pods_unsanitize( $v );
3873
+
3874
+ if ( isset( $custom_values[ $v ] ) ) {
3875
+ $values[ $k ] = $v;
3876
+ }
3877
+ }
3878
+
3879
+ $value = $values;
3880
+ }
3881
+ }
3882
+
3883
+ if ( 0 < $pick_limit && ! empty( $value ) ) {
3884
+ $value = array_slice( $value, 0, $pick_limit );
3885
+ }
3886
+
3887
+ // Don't save an empty array, just make it an empty string
3888
+ if ( empty( $value ) ) {
3889
+ $value = '';
3890
+ } elseif ( is_array( $value ) ) {
3891
+ if ( 1 == $pick_limit || 1 == count( $value ) ) {
3892
+ // If there's just one item, don't save as an array, save the string
3893
+ $value = implode( '', $value );
3894
+ } elseif ( 'table' === pods_var( 'storage', $pod ) ) {
3895
+ // If storage is set to table, json encode, otherwise WP will serialize automatically
3896
+ $value = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : json_encode( $value );
3897
+ }
3898
+ }
3899
+ }
3900
+
3901
+ // Prepare all table / meta data
3902
+ if ( ! in_array( $type, $tableless_field_types ) || $simple ) {
3903
+ if ( in_array( $type, $repeatable_field_types ) && 1 == pods_var( $type . '_repeatable', $field_data, 0 ) ) {
3904
+ // Don't save an empty array, just make it an empty string
3905
+ if ( empty( $value ) ) {
3906
+ $value = '';
3907
+ } elseif ( is_array( $value ) ) {
3908
+ if ( 1 == count( $value ) ) {
3909
+ // If there's just one item, don't save as an array, save the string
3910
+ $value = implode( '', $value );
3911
+ } elseif ( 'table' === pods_var( 'storage', $pod ) ) {
3912
+ // If storage is set to table, json encode, otherwise WP will serialize automatically
3913
+ $value = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $value, JSON_UNESCAPED_UNICODE ) : json_encode( $value );
3914
+ }
3915
+ }
3916
+ }
3917
+
3918
+ $table_data[ $field ] = str_replace( array( '{prefix}', '@wp_' ), array(
3919
+ '{/prefix/}',
3920
+ '{prefix}'
3921
+ ), $value ); // Fix for pods_query
3922
+ $table_formats[] = PodsForm::prepare( $type, $options );
3923
+
3924
+ $object_meta[ $field ] = $value;
3925
+ } else {
3926
+ // Store relational field data to be looped through later
3927
+ // Convert values from a comma-separated string into an array
3928
+ if ( ! is_array( $value ) ) {
3929
+ $value = explode( ',', $value );
3930
+ }
3931
+
3932
+ $rel_fields[ $type ][ $field ] = $value;
3933
+ $rel_field_ids[] = $field_data['id'];
3934
+ }
3935
+ }
3936
+ }
3937
+
3938
+ if ( in_array( $pod['type'], array( 'post_type', 'taxonomy' ) ) ) {
3939
+ $object_name = $pod['name'];
3940
+
3941
+ if ( ! empty( $pod['object'] ) ) {
3942
+ $object_name = $pod['object'];
3943
+ }
3944
+
3945
+ $object_name_field = 'post_type';
3946
+
3947
+ if ( 'taxonomy' === $pod['type'] ) {
3948
+ $object_name_field = 'taxonomy';
3949
+ }
3950
+
3951
+ $object_data[ $object_name_field ] = $object_name;
3952
+ }
3953
+
3954
+ if ( ! in_array( $pod['type'], array( 'pod', 'table', '' ) ) ) {
3955
+ $meta_fields = array();
3956
+
3957
+ if ( 'meta' === $pod['storage'] || 'settings' === $pod['type'] || ( 'taxonomy' === $pod['type'] && 'none' === $pod['storage'] ) ) {
3958
+ $meta_fields = $object_meta;
3959
+ }
3960
+
3961
+ if ( $allow_custom_fields && ! empty( $custom_data ) ) {
3962
+ $meta_fields = array_merge( $custom_data, $meta_fields );
3963
+ }
3964
+
3965
+ $fields_to_send = array_flip( array_keys( $meta_fields ) );
3966
+
3967
+ foreach ( $fields_to_send as $field => $field_data ) {
3968
+ if ( isset( $object_fields[ $field ] ) ) {
3969
+ $field_data = $object_fields[ $field ];
3970
+ } elseif ( isset( $fields[ $field ] ) ) {
3971
+ $field_data = $fields[ $field ];
3972
+ } else {
3973
+ unset( $fields_to_send[ $field ] );
3974
+ }
3975
+
3976
+ $fields_to_send[ $field ] = $field_data;
3977
+ }
3978
+
3979
+ $params->id = $this->save_wp_object( $object_type, $object_data, $meta_fields, false, true, $fields_to_send );
3980
+
3981
+ if ( ! empty( $params->id ) && 'settings' === $pod['type'] ) {
3982
+ $params->id = $pod['id'];
3983
+ }
3984
+ }
3985
+
3986
+ if ( 'table' === $pod['storage'] ) {
3987
+ // Every row should have an id set here, otherwise Pods with nothing
3988
+ // but relationship fields won't get properly ID'd
3989
+ if ( empty( $params->id ) ) {
3990
+ $params->id = 0;
3991
+ }
3992
+
3993
+ $table_data = array( 'id' => $params->id ) + $table_data;
3994
+ array_unshift( $table_formats, '%d' );
3995
+
3996
+ if ( ! empty( $table_data ) ) {
3997
+ $sql = pods_data()->insert_on_duplicate( "@wp_pods_{$params->pod}", $table_data, $table_formats );
3998
+
3999
+ $id = pods_query( $sql, 'Cannot add/save table row' );
4000
+
4001
+ if ( empty( $params->id ) ) {
4002
+ $params->id = $id;
4003
+ }
4004
+ }
4005
+ }
4006
+
4007
+ $params->id = (int) $params->id;
4008
+
4009
+ // Save terms for taxonomies associated to a post type
4010
+ if ( 0 < $params->id && 'post_type' === $pod['type'] && ! empty( $post_term_data ) ) {
4011
+ foreach ( $post_term_data as $post_taxonomy => $post_terms ) {
4012
+ $post_terms = (array) $post_terms;
4013
+
4014
+ foreach ( $post_terms as $k => $v ) {
4015
+ if ( ! preg_match( '/[^0-9]/', $v ) ) {
4016
+ $v = (int) $v;
4017
+ }
4018
+
4019
+ $post_terms[ $k ] = $v;
4020
+ }
4021
+
4022
+ wp_set_object_terms( $params->id, $post_terms, $post_taxonomy );
4023
+ }
4024
+ }
4025
+
4026
+ $no_conflict = pods_no_conflict_check( $pod['type'] );
4027
+
4028
+ if ( ! $no_conflict ) {
4029
+ pods_no_conflict_on( $pod['type'] );
4030
+ }
4031
+
4032
+ // Save relationship / file data
4033
+ if ( ! empty( $rel_fields ) ) {
4034
+ foreach ( $rel_fields as $type => $data ) {
4035
+ // Only handle tableless fields
4036
+ if ( ! in_array( $type, $tableless_field_types ) ) {
4037
+ continue;
4038
+ }
4039
+
4040
+ foreach ( $data as $field => $values ) {
4041
+ $pick_val = pods_var( 'pick_val', $fields[ $field ] );
4042
+
4043
+ if ( 'table' === pods_var( 'pick_object', $fields[ $field ] ) ) {
4044
+ $pick_val = pods_var( 'pick_table', $fields[ $field ]['options'], $pick_val, null, true );
4045
+ }
4046
+
4047
+ if ( '__current__' === $pick_val ) {
4048
+ if ( is_object( $pod ) ) {
4049
+ $pick_val = $pod->pod;
4050
+ } elseif ( is_array( $pod ) ) {
4051
+ $pick_val = $pod['name'];
4052
+ } elseif ( 0 < strlen( $pod ) ) {
4053
+ $pick_val = $pod;
4054
+ }
4055
+ }
4056
+
4057
+ $fields[ $field ]['options']['table_info'] = pods_api()->get_table_info( pods_var( 'pick_object', $fields[ $field ] ), $pick_val, null, null, $fields[ $field ]['options'] );
4058
+
4059
+ if ( isset( $fields[ $field ]['options']['table_info']['pod'] ) && ! empty( $fields[ $field ]['options']['table_info']['pod'] ) && isset( $fields[ $field ]['options']['table_info']['pod']['name'] ) ) {
4060
+ $search_data = pods( $fields[ $field ]['options']['table_info']['pod']['name'] );
4061
+
4062
+ $data_mode = 'pods';
4063
+ } else {
4064
+ $search_data = pods_data();
4065
+ $search_data->table( $fields[ $field ]['options']['table_info'] );
4066
+
4067
+ $data_mode = 'data';
4068
+ }
4069
+
4070
+ $find_rel_params = array(
4071
+ 'select' => "`t`.`{$search_data->field_id}`",
4072
+ 'where' => "`t`.`{$search_data->field_slug}` = %s OR `t`.`{$search_data->field_index}` = %s",
4073
+ 'limit' => 1,
4074
+ 'pagination' => false,
4075
+ 'search' => false
4076
+ );
4077
+
4078
+ if ( empty( $search_data->field_slug ) && ! empty( $search_data->field_index ) ) {
4079
+ $find_rel_params['where'] = "`t`.`{$search_data->field_index}` = %s";
4080
+ } elseif ( empty( $search_data->field_slug ) && empty( $search_data->field_index ) ) {
4081
+ $find_rel_params = false;
4082
+ }
4083
+
4084
+ $related_limit = (int) pods_var_raw( $type . '_limit', $fields[ $field ]['options'], 0 );
4085
+
4086
+ if ( 'single' === pods_var_raw( $type . '_format_type', $fields[ $field ]['options'] ) ) {
4087
+ $related_limit = 1;
4088
+ }
4089
+
4090
+ // Enforce integers / unique values for IDs
4091
+ $value_ids = array();
4092
+
4093
+ $is_file_field = in_array( $type, PodsForm::file_field_types() );
4094
+ $is_taggable = ( in_array( $type, PodsForm::tableless_field_types() ) && 1 == pods_v( $type . '_taggable', $fields[ $field ]['options'] ) );
4095
+
4096
+ // @todo Handle simple relationships eventually
4097
+ foreach ( $values as $v ) {
4098
+ if ( ! empty( $v ) ) {
4099
+ if ( ! is_array( $v ) ) {
4100
+ if ( ! preg_match( '/[^0-9]/', $v ) ) {
4101
+ $v = (int) $v;
4102
+ } elseif ( $is_file_field ) {
4103
+ // File handling
4104
+ // Get ID from GUID
4105
+ $v = pods_image_id_from_field( $v );
4106
+
4107
+ // If file not found, add it
4108
+ if ( empty( $v ) ) {
4109
+ $v = pods_attachment_import( $v );
4110
+ }
4111
+ } else {
4112
+ // Reference by slug
4113
+ $v_data = false;
4114
+
4115
+ if ( false !== $find_rel_params ) {
4116
+ $rel_params = $find_rel_params;
4117
+ $rel_params['where'] = $wpdb->prepare( $rel_params['where'], array( $v, $v ) );
4118
+
4119
+ $search_data->select( $rel_params );
4120
+
4121
+ $v_data = $search_data->fetch( $v );
4122
+ }
4123
+
4124
+ if ( ! empty( $v_data ) && isset( $v_data[ $search_data->field_id ] ) ) {
4125
+ $v = (int) $v_data[ $search_data->field_id ];
4126
+ } elseif ( $is_taggable && 'pods' === $data_mode ) {
4127
+ // Allow tagging for Pods objects
4128
+ $tag_data = array(
4129
+ $search_data->field_index => $v
4130
+ );
4131
+
4132
+ if ( 'post_type' === $search_data->pod_data['type'] ) {
4133
+ $tag_data['post_status'] = 'publish';
4134
+ }
4135
+
4136
+ /**
4137
+ * Filter for changing tag before adding new item.
4138
+ *
4139
+ * @param array $tag_data Fields for creating new item.
4140
+ * @param int $v Field ID of tag.
4141
+ * @param Pods $search_data Search object for tag.
4142
+ * @param string $field Table info for field.
4143
+ * @param array $pieces Field array.
4144
+ *
4145
+ * @since 2.3.19
4146
+ */
4147
+ $tag_data = apply_filters( 'pods_api_save_pod_item_taggable_data', $tag_data, $v, $search_data, $field, compact( $pieces ) );
4148
+
4149
+ // Save $v to a new item on related object
4150
+ $v = $search_data->add( $tag_data );
4151
+
4152
+ // @todo Support non-Pods for tagging
4153
+ }
4154
+ }
4155
+ } elseif ( $is_file_field && isset( $v['id'] ) ) {
4156
+ $v = (int) $v['id'];
4157
+ } else {
4158
+ continue;
4159
+ }
4160
+
4161
+ if ( ! empty( $v ) && ! in_array( $v, $value_ids ) ) {
4162
+ $value_ids[] = $v;
4163
+ }
4164
+ }
4165
+ }
4166
+
4167
+ $value_ids = array_unique( array_filter( $value_ids ) );
4168
+
4169
+ // Filter unique values not equal to false in case of a multidimensional array
4170
+ $filtered_values = $this->array_filter_walker( $values );
4171
+ $serialized_values = array_map( 'serialize', $filtered_values );
4172
+ $unique_serialized_values = array_unique( $serialized_values );
4173
+
4174
+ $values = array_map( 'unserialize', $unique_serialized_values );
4175
+
4176
+ // Limit values
4177
+ if ( 0 < $related_limit && ! empty( $value_ids ) ) {
4178
+ $value_ids = array_slice( $value_ids, 0, $related_limit );
4179
+ $values = array_slice( $values, 0, $related_limit );
4180
+ }
4181
+
4182
+ // Get current values
4183
+ if ( 'pick' === $type && isset( PodsField_Pick::$related_data[ $fields[ $field ]['id'] ] ) && isset( PodsField_Pick::$related_data[ $fields[ $field ]['id'] ]['current_ids'] ) ) {
4184
+ $related_ids = PodsField_Pick::$related_data[ $fields[ $field ]['id'] ]['current_ids'];
4185
+ } else {
4186
+ $related_ids = $this->lookup_related_items( $fields[ $field ]['id'], $pod['id'], $params->id, $fields[ $field ], $pod );
4187
+ }
4188
+
4189
+ // Get ids to remove
4190
+ $remove_ids = array_diff( $related_ids, $value_ids );
4191
+
4192
+ // Delete relationships
4193
+ if ( ! empty( $remove_ids ) ) {
4194
+ $this->delete_relationships( $params->id, $remove_ids, $pod, $fields[ $field ] );
4195
+ }
4196
+
4197
+ // Save relationships
4198
+ if ( ! empty( $value_ids ) ) {
4199
+ $this->save_relationships( $params->id, $value_ids, $pod, $fields[ $field ] );
4200
+ }
4201
+
4202
+ $field_save_values = $value_ids;
4203
+
4204
+ if ( 'file' === $type ) {
4205
+ $field_save_values = $values;
4206
+ }
4207
+
4208
+ // Run save function for field type (where needed)
4209
+ PodsForm::save( $type, $field_save_values, $params->id, $field, array_merge( $fields[ $field ], $fields[ $field ]['options'] ), array_merge( $fields, $object_fields ), $pod, $params );
4210
+ }
4211
+
4212
+ // Unset data no longer needed
4213
+ if ( 'pick' === $type ) {
4214
+ foreach ( $data as $field => $values ) {
4215
+ if ( isset( PodsField_Pick::$related_data[ $fields[ $field ]['id'] ] ) ) {
4216
+ unset( PodsField_Pick::$related_data[ PodsField_Pick::$related_data[ $fields[ $field ]['id'] ]['related_field']['id'] ] );
4217
+ unset( PodsField_Pick::$related_data[ $fields[ $field ]['id'] ] );
4218
+ }
4219
+ }
4220
+ }
4221
+ }
4222
+ }
4223
+
4224
+ if ( ! $no_conflict ) {
4225
+ pods_no_conflict_off( $pod['type'] );
4226
+ }
4227
+
4228
+ // Clear cache
4229
+ pods_cache_clear( $params->id, 'pods_items_' . $pod['name'] );
4230
+
4231
+ if ( $params->clear_slug_cache && ! empty( $pod['field_slug'] ) ) {
4232
+ $slug = pods( $pod['name'], $params->id )->field( $pod['field_slug'] );
4233
+
4234
+ if ( 0 < strlen( $slug ) ) {
4235
+ pods_cache_clear( $slug, 'pods_items_' . $pod['name'] );
4236
+ }
4237
+ }
4238
+
4239
+ // Clear WP meta cache
4240
+ if ( in_array( $pod['type'], array( 'post_type', 'media', 'taxonomy', 'user', 'comment' ) ) ) {
4241
+ $meta_type = $pod['type'];
4242
+
4243
+ if ( 'post_type' === $meta_type ) {
4244
+ $meta_type = 'post';
4245
+ }
4246
+
4247
+ wp_cache_delete( $params->id, $meta_type . '_meta' );
4248
+ wp_cache_delete( $params->id, 'pods_' . $meta_type . '_meta' );
4249
+ }
4250
+
4251
+ if ( false === $bypass_helpers ) {
4252
+ if ( $track_changed_fields ) {
4253
+ $changed_fields = self::handle_changed_fields( $params->pod, $params->id, 'get' );
4254
+ }
4255
+
4256
+ $compact_pieces = compact( $pieces );
4257
+
4258
+ // Plugin hooks
4259
+ $this->do_hook( 'post_save_pod_item', $compact_pieces, $is_new_item, $params->id );
4260
+ $this->do_hook( "post_save_pod_item_{$params->pod}", $compact_pieces, $is_new_item, $params->id );
4261
+
4262
+ if ( $is_new_item ) {
4263
+ $this->do_hook( 'post_create_pod_item', $compact_pieces, $params->id );
4264
+ $this->do_hook( "post_create_pod_item_{$params->pod}", $compact_pieces, $params->id );
4265
+ } else {
4266
+ $this->do_hook( 'post_edit_pod_item', $compact_pieces, $params->id );
4267
+ $this->do_hook( "post_edit_pod_item_{$params->pod}", $compact_pieces, $params->id );
4268
+ }
4269
+
4270
+ // Call any post-save helpers (if not bypassed)
4271
+ if ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) {
4272
+ if ( ! empty( $post_save_helpers ) ) {
4273
+ pods_deprecated( sprintf( __( 'Post-save helpers are deprecated, use the action pods_post_save_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
4274
+
4275
+ foreach ( $post_save_helpers as $helper ) {
4276
+ $helper = $this->load_helper( array( 'name' => $helper ) );
4277
+
4278
+ if ( false !== $helper && ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) ) {
4279
+ eval( '?>' . $helper['code'] );
4280
+ }
4281
+ }
4282
+ }
4283
+ }
4284
+ }
4285
+
4286
+ // Success! Return the id
4287
+ return $params->id;
4288
+
4289
+ }
4290
+
4291
+ /**
4292
+ * @see PodsAPI::save_pod_item
4293
+ * Add multiple pod items
4294
+ *
4295
+ * $params['pod'] string The Pod name (pod or pod_id is required)
4296
+ * $params['pod_id'] string The Pod ID (pod or pod_id is required)
4297
+ * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
4298
+ *
4299
+ * $data['id'] int The item ID (optional)
4300
+ * $data['data'] array An associative array of field names + values
4301
+ *
4302
+ * @param array|object $params An associative array of parameters, data excluded
4303
+ * @param array $data An associative array of pod ids and field names + values (arrays of field data)
4304
+ *
4305
+ * @return int The item ID
4306
+ * @since 2.0.0
4307
+ */
4308
+ public function save_pod_items( $params, $data ) {
4309
+
4310
+ $params = (object) $params;
4311
+
4312
+ $ids = array();
4313
+
4314
+ foreach ( $data as $fields ) {
4315
+ $params->data = $fields;
4316
+
4317
+ if ( isset( $fields['id'] ) && isset( $fields['data'] ) ) {
4318
+ $params->id = $fields['id'];
4319
+ $params->data = $fields['data'];
4320
+ }
4321
+
4322
+ $ids[] = $this->save_pod_item( $params );
4323
+ }
4324
+
4325
+ return $ids;
4326
+ }
4327
+
4328
+ /**
4329
+ * Handle tracking changed fields or get them.
4330
+ *
4331
+ * @since 2.7.0
4332
+ *
4333
+ * @param string $pod
4334
+ * @param int $id
4335
+ * @param string $mode
4336
+ *
4337
+ * @return array List of changed fields (if $mode = 'get')
4338
+ */
4339
+ public static function handle_changed_fields( $pod, $id, $mode = 'set' ) {
4340
+
4341
+ static $changed_pods_cache = array();
4342
+ static $old_fields_cache = array();
4343
+ static $changed_fields_cache = array();
4344
+
4345
+ $cache_key = $pod . '|' . $id;
4346
+
4347
+ $export_params = array(
4348
+ 'depth' => 1,
4349
+ );
4350
+
4351
+ if ( in_array( $mode, array( 'set', 'reset' ), true ) ) {
4352
+ if ( isset( $changed_fields_cache[ $cache_key ] ) ) {
4353
+ unset( $changed_fields_cache[ $cache_key ] );
4354
+ }
4355
+
4356
+ if ( empty( $old_fields_cache[ $cache_key ] ) || 'reset' === $mode ) {
4357
+ $old_fields_cache[ $cache_key ] = array();
4358
+
4359
+ if ( ! empty( $id ) ) {
4360
+ if ( ! isset( $changed_pods_cache[ $pod ] ) ) {
4361
+ $changed_pods_cache[ $pod ] = pods( $pod );
4362
+ }
4363
+
4364
+ if ( $changed_pods_cache[ $pod ] && $changed_pods_cache[ $pod ]->valid() ) {
4365
+ $changed_pods_cache[ $pod ]->fetch( $id );
4366
+
4367
+ $old_fields_cache[ $cache_key ] = $changed_pods_cache[ $pod ]->export( $export_params );
4368
+ }
4369
+ }
4370
+ }
4371
+ }
4372
+
4373
+ $changed_fields = array();
4374
+
4375
+ if ( isset( $changed_fields_cache[ $cache_key ] ) ) {
4376
+ $changed_fields = $changed_fields_cache[ $cache_key ];
4377
+ } elseif ( isset( $old_fields_cache[ $cache_key ] ) ) {
4378
+ $old_fields = $old_fields_cache[ $cache_key ];
4379
+
4380
+ if ( 'get' === $mode ) {
4381
+ $changed_fields_cache[ $cache_key ] = array();
4382
+
4383
+ if ( ! empty( $changed_pods_cache[ $pod ] ) ) {
4384
+ if ( $id != $changed_pods_cache[ $pod ]->id() ) {
4385
+ $changed_pods_cache[ $pod ]->fetch( $id );
4386
+ }
4387
+
4388
+ $new_fields = $changed_pods_cache[ $pod ]->export( $export_params );
4389
+
4390
+ foreach ( $new_fields as $field => $value ) {
4391
+ if ( ! isset( $old_fields[ $field ] ) || $value != $old_fields[ $field ] ) {
4392
+ $changed_fields[ $field ] = $value;
4393
+ }
4394
+ }
4395
+
4396
+ $changed_fields_cache[ $cache_key ] = $changed_fields;
4397
+ }
4398
+ }
4399
+ }
4400
+
4401
+ return $changed_fields;
4402
+
4403
+ }
4404
+
4405
+ /**
4406
+ * Get the fields that have changed during a save
4407
+ *
4408
+ * @param array $pieces Pieces array from save_pod_item
4409
+ *
4410
+ * @return array Array of fields and values that have changed
4411
+ *
4412
+ * @deprecated 2.7.0 Use PodsAPI::handle_changed_fields
4413
+ */
4414
+ public function get_changed_fields( $pieces ) {
4415
+
4416
+ return self::handle_changed_fields( $pieces['params']->pod, $pieces['params']->id, 'get' );
4417
+
4418
+ }
4419
+
4420
+ /**
4421
+ * Save relationships
4422
+ *
4423
+ * @param int $id ID of item
4424
+ * @param int|array $related_id ID or IDs to save
4425
+ * @param array $pod Pod data
4426
+ * @param array $field Field data
4427
+ */
4428
+ public function save_relationships( $id, $related_ids, $pod, $field ) {
4429
+
4430
+ // Get current values
4431
+ if ( 'pick' === $field['type'] && isset( PodsField_Pick::$related_data[ $field['id'] ] ) && isset( PodsField_Pick::$related_data[ $field['id'] ]['current_ids'] ) ) {
4432
+ $current_ids = PodsField_Pick::$related_data[ $field['id'] ]['current_ids'];
4433
+ } else {
4434
+ $current_ids = $this->lookup_related_items( $field['id'], $pod['id'], $id, $field, $pod );
4435
+ }
4436
+
4437
+ if ( isset( self::$related_item_cache[ $pod['id'] ][ $field['id'] ] ) ) {
4438
+ // Delete relationship from cache
4439
+ unset( self::$related_item_cache[ $pod['id'] ][ $field['id'] ] );
4440
+ }
4441
+
4442
+ if ( ! is_array( $related_ids ) ) {
4443
+ $related_ids = implode( ',', $related_ids );
4444
+ }
4445
+
4446
+ foreach ( $related_ids as $k => $related_id ) {
4447
+ $related_ids[ $k ] = (int) $related_id;
4448
+ }
4449
+
4450
+ $related_ids = array_unique( array_filter( $related_ids ) );
4451
+
4452
+ $related_limit = (int) pods_var_raw( $field['type'] . '_limit', $field['options'], 0 );
4453
+
4454
+ if ( 'single' === pods_var_raw( $field['type'] . '_format_type', $field['options'] ) ) {
4455
+ $related_limit = 1;
4456
+ }
4457
+
4458
+ // Limit values
4459
+ if ( 0 < $related_limit && ! empty( $related_ids ) ) {
4460
+ $related_ids = array_slice( $related_ids, 0, $related_limit );
4461
+ }
4462
+
4463
+ // Post Types, Media, Users, and Comments (meta-based)
4464
+ if ( in_array( $pod['type'], array( 'post_type', 'media', 'taxonomy', 'user', 'comment' ) ) ) {
4465
+ $object_type = $pod['type'];
4466
+
4467
+ if ( in_array( $object_type, array( 'post_type', 'media' ) ) ) {
4468
+ $object_type = 'post';
4469
+ } elseif ( 'taxonomy' === $object_type ) {
4470
+ $object_type = 'term';
4471
+ }
4472
+
4473
+ delete_metadata( $object_type, $id, $field['name'] );
4474
+
4475
+ if ( ! empty( $related_ids ) ) {
4476
+ update_metadata( $object_type, $id, '_pods_' . $field['name'], $related_ids );
4477
+
4478
+ foreach ( $related_ids as $related_id ) {
4479
+ add_metadata( $object_type, $id, $field['name'], $related_id );
4480
+ }
4481
+ } else {
4482
+ delete_metadata( $object_type, $id, '_pods_' . $field['name'] );
4483
+ }
4484
+ } elseif ( 'settings' === $pod['type'] ) {
4485
+ // Custom Settings Pages (options-based)
4486
+ if ( ! empty( $related_ids ) ) {
4487
+ update_option( $pod['name'] . '_' . $field['name'], $related_ids );
4488
+ } else {
4489
+ delete_option( $pod['name'] . '_' . $field['name'] );
4490
+ }
4491
+ }
4492
+
4493
+ $related_pod_id = 0;
4494
+ $related_field_id = 0;
4495
+
4496
+ if ( 'pick' === $field['type'] && isset( PodsField_Pick::$related_data[ $field['id'] ] ) && ! empty( PodsField_Pick::$related_data[ $field['id'] ]['related_field'] ) ) {
4497
+ $related_pod_id = PodsField_Pick::$related_data[ $field['id'] ]['related_pod']['id'];
4498
+ $related_field_id = PodsField_Pick::$related_data[ $field['id'] ]['related_field']['id'];
4499
+ }
4500
+
4501
+ // Relationships table
4502
+ if ( ! pods_tableless() ) {
4503
+ $related_weight = 0;
4504
+
4505
+ foreach ( $related_ids as $related_id ) {
4506
+ if ( in_array( $related_id, $current_ids ) ) {
4507
+ pods_query( "
4508
+ UPDATE `@wp_podsrel`
4509
+ SET
4510
+ `pod_id` = %d,
4511
+ `field_id` = %d,
4512
+ `item_id` = %d,
4513
+ `related_pod_id` = %d,
4514
+ `related_field_id` = %d,
4515
+ `related_item_id` = %d,
4516
+ `weight` = %d
4517
+ WHERE
4518
+ `pod_id` = %d
4519
+ AND `field_id` = %d
4520
+ AND `item_id` = %d
4521
+ AND `related_item_id` = %d
4522
+ ", array(
4523
+ $pod['id'],
4524
+ $field['id'],
4525
+ $id,
4526
+ $related_pod_id,
4527
+ $related_field_id,
4528
+ $related_id,
4529
+ $related_weight,
4530
+
4531
+ $pod['id'],
4532
+ $field['id'],
4533
+ $id,
4534
+ $related_id,
4535
+ ) );
4536
+ } else {
4537
+ pods_query( "
4538
+ INSERT INTO `@wp_podsrel`
4539
+ (
4540
+ `pod_id`,
4541
+ `field_id`,
4542
+ `item_id`,
4543
+ `related_pod_id`,
4544
+ `related_field_id`,
4545
+ `related_item_id`,
4546
+ `weight`
4547
+ )
4548
+ VALUES ( %d, %d, %d, %d, %d, %d, %d )
4549
+ ", array(
4550
+ $pod['id'],
4551
+ $field['id'],
4552
+ $id,
4553
+ $related_pod_id,
4554
+ $related_field_id,
4555
+ $related_id,
4556
+ $related_weight
4557
+ ) );
4558
+ }
4559
+
4560
+ $related_weight ++;
4561
+ }
4562
+ }
4563
+ }
4564
+
4565
+ /**
4566
+ * Duplicate a Pod
4567
+ *
4568
+ * $params['id'] int The Pod ID
4569
+ * $params['name'] string The Pod name
4570
+ * $params['new_name'] string The new Pod name
4571
+ *
4572
+ * @param array $params An associative array of parameters
4573
+ * @param bool $strict (optional) Makes sure a pod exists, if it doesn't throws an error
4574
+ *
4575
+ * @return int New Pod ID
4576
+ * @since 2.3.0
4577
+ */
4578
+ public function duplicate_pod( $params, $strict = false ) {
4579
+
4580
+ if ( ! is_object( $params ) && ! is_array( $params ) ) {
4581
+ if ( is_numeric( $params ) ) {
4582
+ $params = array( 'id' => $params );
4583
+ } else {
4584
+ $params = array( 'name' => $params );
4585
+ }
4586
+
4587
+ $params = (object) pods_sanitize( $params );
4588
+ } else {
4589
+ $params = (object) pods_sanitize( $params );
4590
+ }
4591
+
4592
+ $params->table_info = false;
4593
+
4594
+ $pod = $this->load_pod( $params, $strict );
4595
+
4596
+ if ( empty( $pod ) ) {
4597
+ if ( false !== $strict ) {
4598
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
4599
+ }
4600
+
4601
+ return false;
4602
+ } elseif ( in_array( $pod['type'], array( 'media', 'user', 'comment' ) ) ) {
4603
+ if ( false !== $strict ) {
4604
+ return pods_error( __( 'Pod not allowed to be duplicated', 'pods' ), $this );
4605
+ }
4606
+
4607
+ return false;
4608
+ } elseif ( in_array( $pod['type'], array( 'post_type', 'taxonomy' ) ) && 0 < strlen( $pod['object'] ) ) {
4609
+ $pod['object'] = '';
4610
+ }
4611
+
4612
+ unset( $pod['id'] );
4613
+
4614
+ if ( isset( $params->new_name ) ) {
4615
+ $pod['name'] = $params->new_name;
4616
+ }
4617
+
4618
+ $try = 1;
4619
+
4620
+ $check_name = $pod['name'];
4621
+ $new_label = $pod['label'];
4622
+
4623
+ while ( $this->load_pod( array( 'name' => $check_name, 'table_info' => false ), false ) ) {
4624
+ $try ++;
4625
+
4626
+ $check_name = $pod['name'] . $try;
4627
+ $new_label = $pod['label'] . $try;
4628
+ }
4629
+
4630
+ $pod['name'] = $check_name;
4631
+ $pod['label'] = $new_label;
4632
+
4633
+ foreach ( $pod['fields'] as $field => $field_data ) {
4634
+ unset( $pod['fields'][ $field ]['id'] );
4635
+ }
4636
+
4637
+ return $this->save_pod( $pod );
4638
+ }
4639
+
4640
+ /**
4641
+ * Duplicate a Field
4642
+ *
4643
+ * $params['pod_id'] int The Pod ID
4644
+ * $params['pod'] string The Pod name
4645
+ * $params['id'] int The Field ID
4646
+ * $params['name'] string The Field name
4647
+ * $params['new_name'] string The new Field name
4648
+ *
4649
+ * @param array $params An associative array of parameters
4650
+ * @param bool $strict (optional) Makes sure a field exists, if it doesn't throws an error
4651
+ *
4652
+ * @return int New Field ID
4653
+ * @since 2.3.10
4654
+ */
4655
+ public function duplicate_field( $params, $strict = false ) {
4656
+
4657
+ if ( ! is_object( $params ) && ! is_array( $params ) ) {
4658
+ if ( is_numeric( $params ) ) {
4659
+ $params = array( 'id' => $params );
4660
+ } else {
4661
+ $params = array( 'name' => $params );
4662
+ }
4663
+ }
4664
+
4665
+ $params = (object) pods_sanitize( $params );
4666
+
4667
+ $params->table_info = false;
4668
+
4669
+ $field = $this->load_field( $params, $strict );
4670
+
4671
+ if ( empty( $field ) ) {
4672
+ if ( false !== $strict ) {
4673
+ return pods_error( __( 'Field not found', 'pods' ), $this );
4674
+ }
4675
+
4676
+ return false;
4677
+ }
4678
+
4679
+ unset( $field['id'] );
4680
+
4681
+ if ( isset( $params->new_name ) ) {
4682
+ $field['name'] = $params->new_name;
4683
+ }
4684
+
4685
+ $try = 1;
4686
+
4687
+ $check_name = $field['name'];
4688
+ $new_label = $field['label'];
4689
+
4690
+ while ( $this->load_field( array(
4691
+ 'pod_id' => $field['pod_id'],
4692
+ 'name' => $check_name,
4693
+ 'table_info' => false
4694
+ ), false ) ) {
4695
+ $try ++;
4696
+
4697
+ $check_name = $field['name'] . $try;
4698
+ $new_label = $field['label'] . $try;
4699
+ }
4700
+
4701
+ $field['name'] = $check_name;
4702
+ $field['label'] = $new_label;
4703
+
4704
+ return $this->save_field( $field );
4705
+
4706
+ }
4707
+
4708
+ /**
4709
+ * @see PodsAPI::save_pod_item
4710
+ *
4711
+ * Duplicate a pod item
4712
+ *
4713
+ * $params['pod'] string The Pod name
4714
+ * $params['id'] int The item's ID from the wp_pods_* table
4715
+ *
4716
+ * @param array $params An associative array of parameters
4717
+ *
4718
+ * @return int The table row ID
4719
+ *
4720
+ * @since 1.12
4721
+ */
4722
+ public function duplicate_pod_item( $params ) {
4723
+
4724
+ $params = (object) pods_sanitize( $params );
4725
+
4726
+ $load_pod_params = array(
4727
+ 'name' => $params->pod,
4728
+ 'table_info' => false,
4729
+ );
4730
+
4731
+ $pod = $this->load_pod( $load_pod_params );
4732
+
4733
+ if ( false === $pod ) {
4734
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
4735
+ }
4736
+
4737
+ $pod = pods( $params->pod, $params->id );
4738
+
4739
+ $params->pod = $pod->pod;
4740
+ $params->pod_id = $pod->pod_id;
4741
+
4742
+ $fields = (array) pods_var_raw( 'fields', $pod->pod_data, array(), null, true );
4743
+ $object_fields = (array) pods_var_raw( 'object_fields', $pod->pod_data, array(), null, true );
4744
+
4745
+ if ( ! empty( $object_fields ) ) {
4746
+ $fields = array_merge( $object_fields, $fields );
4747
+ }
4748
+
4749
+ $save_params = array(
4750
+ 'pod' => $params->pod,
4751
+ 'data' => array(),
4752
+ 'is_new_item' => true,
4753
+ );
4754
+
4755
+ $ignore_fields = array(
4756
+ $pod->pod_data['field_id'],
4757
+ $pod->pod_data['field_slug'],
4758
+ );
4759
+
4760
+ if ( in_array( $pod->pod_data['type'], array( 'post_type', 'media' ) ) ) {
4761
+ $ignore_fields = array(
4762
+ 'ID',
4763
+ 'post_name',
4764
+ 'post_date',
4765
+ 'post_date_gmt',
4766
+ 'post_modified',
4767
+ 'post_modified_gmt',
4768
+ 'guid',
4769
+ );
4770
+ } elseif ( 'term' === $pod->pod_data['type'] ) {
4771
+ $ignore_fields = array(
4772
+ 'term_id',
4773
+ 'term_taxonomy_id',
4774
+ 'slug',
4775
+ );
4776
+ } elseif ( 'user' === $pod->pod_data['type'] ) {
4777
+ $ignore_fields = array(
4778
+ 'ID',
4779
+ 'user_nicename',
4780
+ );
4781
+ } elseif ( 'comment' === $pod->pod_data['type'] ) {
4782
+ $ignore_fields = array(
4783
+ 'comment_ID',
4784
+ );
4785
+ } elseif ( 'settings' === $pod->pod_data['type'] ) {
4786
+ return pods_error( __( 'Settings do not support duplication.', 'pods' ), $this );
4787
+ }
4788
+
4789
+ /**
4790
+ * Filter the fields to ignore during duplication
4791
+ *
4792
+ * @since 2.6.6
4793
+ *
4794
+ * @param array $ignore_fields Fields to ignore and not save during duplication
4795
+ * @param Pods $pod Pod object
4796
+ * @param array $fields Fields on the pod to duplicate
4797
+ * @param object $params Params passed into duplicate_pod_item()
4798
+ */
4799
+ $ignore_fields = apply_filters( 'pods_api_duplicate_pod_item_ignore_fields', $ignore_fields, $pod, $fields, $params );
4800
+
4801
+ foreach ( $fields as $field ) {
4802
+ if ( in_array( $field['name'], $ignore_fields ) ) {
4803
+ continue;
4804
+ }
4805
+
4806
+ $field = array(
4807
+ 'name' => $field['name'],
4808
+ 'output' => 'ids'
4809
+ );
4810
+
4811
+ $value = $pod->field( $field );
4812
+
4813
+ // @todo Add post type compatibility to set unique post_title
4814
+ // @todo Add term compatibility to set unique name
4815
+ // @todo Add user compatibility to set unique user_login/user_email
4816
+
4817
+ if ( ! empty( $value ) || ( ! is_array( $value ) && 0 < strlen( $value ) ) ) {
4818
+ $save_params['data'][ $field['name'] ] = $value;
4819
+ }
4820
+ }
4821
+
4822
+ $save_params = $this->do_hook( 'duplicate_pod_item', $save_params, $pod->pod, $pod->id(), $params );
4823
+
4824
+ $id = $this->save_pod_item( $save_params );
4825
+
4826
+ return $id;
4827
+
4828
+ }
4829
+
4830
+ /**
4831
+ * @see pods()
4832
+ *
4833
+ * Export a pod item
4834
+ *
4835
+ * $params['pod'] string The Pod name
4836
+ * $params['id'] int The item's ID from the wp_pods_* table
4837
+ * $params['fields'] array The fields to export
4838
+ * $params['depth'] int How many levels deep to export data
4839
+ *
4840
+ * @param array $params An associative array of parameters
4841
+ * @param object $pod (optional) Pods object
4842
+ *
4843
+ * @return int The table row ID
4844
+ * @since 1.12
4845
+ */
4846
+ public function export_pod_item( $params, $pod = null ) {
4847
+
4848
+ if ( ! is_object( $pod ) || 'Pods' !== get_class( $pod ) ) {
4849
+ if ( empty( $params ) ) {
4850
+ return false;
4851
+ }
4852
+
4853
+ if ( is_object( $params ) ) {
4854
+ $params = get_object_vars( (object) $params );
4855
+ }
4856
+
4857
+ $params = pods_sanitize( $params );
4858
+
4859
+ $pod = pods( $params['pod'], $params['id'], false );
4860
+
4861
+ if ( empty( $pod ) ) {
4862
+ return false;
4863
+ }
4864
+ }
4865
+
4866
+ $params['fields'] = (array) pods_v( 'fields', $params, array(), true );
4867
+ $params['depth'] = (int) pods_v( 'depth', $params, 2, true );
4868
+ $params['object_fields'] = (array) pods_v( 'object_fields', $pod->pod_data, array(), true );
4869
+ $params['flatten'] = (boolean) pods_v( 'flatten', $params, false, true );
4870
+ $params['context'] = pods_v( 'context', $params, null, true );
4871
+
4872
+ if ( empty( $params['fields'] ) ) {
4873
+ $params['fields'] = array_merge( $pod->fields, $params['object_fields'] );
4874
+ }
4875
+
4876
+ $data = $this->export_pod_item_level( $pod, $params );
4877
+
4878
+ $data = $this->do_hook( 'export_pod_item', $data, $pod->pod, $pod->id(), $pod, $params['fields'], $params['depth'], $params['flatten'], $params );
4879
+
4880
+ return $data;
4881
+ }
4882
+
4883
+ /**
4884
+ * Export a pod item by depth level
4885
+ *
4886
+ * @param Pods $pod Pods object
4887
+ * @param array $params Export params
4888
+ *
4889
+ * @return array Data array
4890
+ *
4891
+ * @since 2.3.0
4892
+ */
4893
+ private function export_pod_item_level( $pod, $params ) {
4894
+
4895
+ $fields = $params['fields'];
4896
+ $depth = $params['depth'];
4897
+ $flatten = $params['flatten'];
4898
+ $current_depth = pods_v( 'current_depth', $params, 1, true );
4899
+ $context = $params['context'];
4900
+
4901
+ $tableless_field_types = PodsForm::tableless_field_types();
4902
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
4903
+
4904
+ $object_fields = (array) pods_v( 'object_fields', $pod->pod_data, array(), true );
4905
+
4906
+ $export_fields = array();
4907
+
4908
+ $pod_type = $pod->pod_data['type'];
4909
+
4910
+ if ( 'post_type' === $pod_type ) {
4911
+ $pod_type = 'post';
4912
+ } elseif ( 'taxonomy' === $pod_type ) {
4913
+ $pod_type = 'term';
4914
+ }
4915
+
4916
+ $registered_meta_keys = false;
4917
+
4918
+ if ( function_exists( 'get_registered_meta_keys' ) ) {
4919
+ $registered_meta_keys = get_registered_meta_keys( $pod_type );
4920
+ }
4921
+
4922
+ $show_in_rest = false;
4923
+
4924
+ // If in rest, check if this pod can be exposed
4925
+ if ( 'rest' === $context ) {
4926
+ $read_all = (int) pods_v( 'read_all', $pod->pod_data['options'], 0 );
4927
+
4928
+ if ( 1 === $read_all ) {
4929
+ $show_in_rest = true;
4930
+ }
4931
+ }
4932
+
4933
+ foreach ( $fields as $k => $field ) {
4934
+ if ( ! is_array( $field ) ) {
4935
+ $field = array(
4936
+ 'id' => 0,
4937
+ 'name' => $field
4938
+ );
4939
+ }
4940
+
4941
+ if ( isset( $pod->fields[ $field['name'] ] ) ) {
4942
+ // If in rest, check if this field can be exposed
4943
+ if ( 'rest' === $context && false === $show_in_rest ) {
4944
+ $show_in_rest = PodsRESTFields::field_allowed_to_extend( $field['name'], $pod, 'read' );
4945
+
4946
+ if ( false === $show_in_rest ) {
4947
+ // Fallback to checking $registered_meta_keys
4948
+ if ( false !== $registered_meta_keys ) {
4949
+ if ( ! isset( $registered_meta_keys[ $field['name'] ] ) ) {
4950
+ continue;
4951
+ } elseif ( empty( $registered_meta_keys[ $field['name'] ]['show_in_rest'] ) ) {
4952
+ continue;
4953
+ }
4954
+ }
4955
+ }
4956
+ }
4957
+
4958
+ $field = $pod->fields[ $field['name'] ];
4959
+ $field['lookup_name'] = $field['name'];
4960
+
4961
+ if ( in_array( $field['type'], $tableless_field_types, true ) && ! in_array( pods_v( 'pick_object', $field ), $simple_tableless_objects, true ) ) {
4962
+ if ( 'pick' === $field['type'] ) {
4963
+ if ( empty( $field['table_info'] ) ) {
4964
+ $field['table_info'] = $this->get_table_info( pods_v( 'pick_object', $field ), pods_v( 'pick_val', $field ), null, null, $field );
4965
+ }
4966
+
4967
+ if ( ! empty( $field['table_info'] ) && 'table' !== $field['table_info']['object_type'] ) {
4968
+ $field['lookup_name'] .= '.' . $field['table_info']['field_id'];
4969
+ }
4970
+ } elseif ( in_array( $field['type'], PodsForm::file_field_types() ) ) {
4971
+ $field['lookup_name'] .= '.guid';
4972
+ }
4973
+ }
4974
+
4975
+ $export_fields[ $field['name'] ] = $field;
4976
+ } elseif ( isset( $object_fields[ $field['name'] ] ) ) {
4977
+ $field = $object_fields[ $field['name'] ];
4978
+ $field['lookup_name'] = $field['name'];
4979
+
4980
+ $export_fields[ $field['name'] ] = $field;
4981
+ } elseif ( $field['name'] == $pod->pod_data['field_id'] ) {
4982
+ $field['type'] = 'number';
4983
+ $field['lookup_name'] = $field['name'];
4984
+
4985
+ $export_fields[ $field['name'] ] = $field;
4986
+ }
4987
+ }
4988
+
4989
+ $data = array();
4990
+
4991
+ foreach ( $export_fields as $field ) {
4992
+ // Return IDs (or guid for files) if only one level deep
4993
+ if ( 1 == $depth ) {
4994
+ $data[ $field['name'] ] = $pod->field( array( 'name' => $field['lookup_name'], 'output' => 'arrays' ) );
4995
+ } elseif ( ( - 1 == $depth || $current_depth < $depth ) && 'pick' === $field['type'] && ! in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) ) {
4996
+ // Recurse depth levels for pick fields if $depth allows
4997
+ $related_data = array();
4998
+
4999
+ $related_ids = $pod->field( array( 'name' => $field['name'], 'output' => 'ids' ) );
5000
+
5001
+ if ( ! empty( $related_ids ) ) {
5002
+ $related_ids = (array) $related_ids;
5003
+
5004
+ $pick_object = pods_var_raw( 'pick_object', $field );
5005
+
5006
+ $related_pod = pods( pods_var_raw( 'pick_val', $field ), null, false );
5007
+
5008
+ // If this isn't a Pod, return data exactly as Pods does normally
5009
+ if ( empty( $related_pod ) || ( 'pod' !== $pick_object && $pick_object !== $related_pod->pod_data['type'] ) || $related_pod->pod == $pod->pod ) {
5010
+ $related_data = $pod->field( array( 'name' => $field['name'], 'output' => 'arrays' ) );
5011
+ } else {
5012
+ $related_object_fields = (array) pods_var_raw( 'object_fields', $related_pod->pod_data, array(), null, true );
5013
+
5014
+ $related_fields = array_merge( $related_pod->fields, $related_object_fields );
5015
+
5016
+ foreach ( $related_ids as $related_id ) {
5017
+ if ( $related_pod->fetch( $related_id ) ) {
5018
+ $related_params = array(
5019
+ 'fields' => $related_fields,
5020
+ 'depth' => $depth,
5021
+ 'flatten' => $flatten,
5022
+ 'current_depth' => $current_depth + 1,
5023
+ 'context' => $context,
5024
+ );
5025
+
5026
+ $related_item = $this->export_pod_item_level( $related_pod, $related_params );
5027
+
5028
+ $related_item_data = $this->do_hook( 'export_pod_item_level', $related_item, $related_pod->pod, $related_pod->id(), $related_pod, $related_fields, $depth, $flatten, ( $current_depth + 1 ), $params );
5029
+
5030
+ if ( function_exists( 'wp_is_json_request' ) && wp_is_json_request() ) {
5031
+ // Don't pass IDs as keys for REST API context to ensure arrays of data are returned.
5032
+ $related_data[] = $related_item_data;
5033
+ } else {
5034
+ $related_data[ $related_id ] = $related_item_data;
5035
+ }
5036
+ }
5037
+ }
5038
+
5039
+ if ( $flatten && ! empty( $related_data ) ) {
5040
+ $related_data = pods_serial_comma( array_values( $related_data ), array(
5041
+ 'and' => '',
5042
+ 'field_index' => $related_pod->pod_data['field_index']
5043
+ ) );
5044
+ }
5045
+ }
5046
+ }
5047
+
5048
+ $data[ $field['name'] ] = $related_data;
5049
+ } else {
5050
+ // Return data exactly as Pods does normally
5051
+ $data[ $field['name'] ] = $pod->field( array( 'name' => $field['name'], 'output' => 'arrays' ) );
5052
+ }
5053
+
5054
+ if ( $flatten && is_array( $data[ $field['name'] ] ) ) {
5055
+ $data[ $field['name'] ] = pods_serial_comma( $data[ $field['name'] ], array(
5056
+ 'field' => $field['name'],
5057
+ 'fields' => $export_fields,
5058
+ 'and' => ''
5059
+ ) );
5060
+ }
5061
+ }
5062
+
5063
+ $data['id'] = (int) $pod->id();
5064
+
5065
+ return $data;
5066
+ }
5067
+
5068
+ /**
5069
+ * Reorder a Pod
5070
+ *
5071
+ * $params['pod'] string The Pod name
5072
+ * $params['field'] string The field name of the field to reorder
5073
+ * $params['order'] array The key => value array of items to reorder (key should be an integer)
5074
+ *
5075
+ * @param array $params An associative array of parameters
5076
+ *
5077
+ * @return bool
5078
+ *
5079
+ * @since 1.9.0
5080
+ */
5081
+ public function reorder_pod_item( $params ) {
5082
+
5083
+ $params = (object) pods_sanitize( $params );
5084
+
5085
+ // @deprecated 2.0.0
5086
+ if ( isset( $params->datatype ) ) {
5087
+ pods_deprecated( __( '$params->pod instead of $params->datatype', 'pods' ), '2.0' );
5088
+
5089
+ $params->pod = $params->datatype;
5090
+
5091
+ unset( $params->datatype );
5092
+ }
5093
+
5094
+ if ( null === pods_var_raw( 'pod', $params, null, null, true ) ) {
5095
+ return pods_error( __( '$params->pod is required', 'pods' ), $this );
5096
+ }
5097
+
5098
+ if ( ! is_array( $params->order ) ) {
5099
+ $params->order = explode( ',', $params->order );
5100
+ }
5101
+
5102
+ $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => true ) );
5103
+
5104
+ $params->name = $pod['name'];
5105
+
5106
+ if ( false === $pod ) {
5107
+ return pods_error( __( 'Pod is required', 'pods' ), $this );
5108
+ }
5109
+
5110
+ foreach ( $params->order as $order => $id ) {
5111
+ if ( isset( $pod['fields'][ $params->field ] ) || isset( $pod['object_fields'][ $params->field ] ) ) {
5112
+ if ( 'table' === $pod['storage'] && ( ! pods_tableless() ) ) {
5113
+ if ( isset( $pod['fields'][ $params->field ] ) ) {
5114
+ pods_query( "UPDATE `@wp_pods_{$params->name}` SET `{$params->field}` = " . pods_absint( $order ) . " WHERE `id` = " . pods_absint( $id ) . " LIMIT 1" );
5115
+ } else {
5116
+ pods_query( "UPDATE `{$pod['table']}` SET `{$params->field}` = " . pods_absint( $order ) . " WHERE `{$pod['field_id']}` = " . pods_absint( $id ) . " LIMIT 1" );
5117
+ }
5118
+ } else {
5119
+ $this->save_pod_item( array(
5120
+ 'pod' => $params->pod,
5121
+ 'pod_id' => $params->pod_id,
5122
+ 'id' => $id,
5123
+ 'data' => array( $params->field => pods_absint( $order ) )
5124
+ ) );
5125
+ }
5126
+ }
5127
+ }
5128
+
5129
+ return true;
5130
+ }
5131
+
5132
+ /**
5133
+ *
5134
+ * Delete all content for a Pod
5135
+ *
5136
+ * $params['id'] int The Pod ID
5137
+ * $params['name'] string The Pod name
5138
+ *
5139
+ * @param array $params An associative array of parameters
5140
+ * @param array $pod Pod data
5141
+ *
5142
+ * @return bool
5143
+ *
5144
+ * @uses pods_query
5145
+ * @uses pods_cache_clear
5146
+ *
5147
+ * @since 1.9.0
5148
+ */
5149
+ public function reset_pod( $params, $pod = false ) {
5150
+
5151
+ $params = (object) pods_sanitize( $params );
5152
+
5153
+ $params->table_info = true;
5154
+
5155
+ if ( empty( $pod ) ) {
5156
+ $pod = $this->load_pod( $params );
5157
+ }
5158
+
5159
+ if ( false === $pod ) {
5160
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
5161
+ }
5162
+
5163
+ $params->id = $pod['id'];
5164
+ $params->name = $pod['name'];
5165
+
5166
+ if ( ! pods_tableless() ) {
5167
+ if ( 'table' === $pod['storage'] ) {
5168
+ try {
5169
+ pods_query( "TRUNCATE `@wp_pods_{$params->name}`", false );
5170
+ } catch ( Exception $e ) {
5171
+ // Allow pod to be reset if the table doesn't exist
5172
+ if ( false === strpos( $e->getMessage(), 'Unknown table' ) ) {
5173
+ return pods_error( $e->getMessage(), $this );
5174
+ }
5175
+ }
5176
+ }
5177
+
5178
+ pods_query( "DELETE FROM `@wp_podsrel` WHERE `pod_id` = {$params->id} OR `related_pod_id` = {$params->id}", false );
5179
+ }
5180
+
5181
+ // @todo Delete relationships from tableless relationships
5182
+
5183
+ // Delete all posts/revisions from this post type
5184
+ if ( in_array( $pod['type'], array( 'post_type', 'media' ) ) ) {
5185
+ $type = pods_var( 'object', $pod, $pod['name'], null, true );
5186
+
5187
+ $sql = "
5188
+ DELETE `t`, `r`, `m`
5189
+ FROM `{$pod['table']}` AS `t`
5190
+ LEFT JOIN `{$pod['meta_table']}` AS `m`
5191
+ ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
5192
+ LEFT JOIN `{$pod['table']}` AS `r`
5193
+ ON `r`.`post_parent` = `t`.`{$pod['field_id']}` AND `r`.`post_status` = 'inherit'
5194
+ WHERE `t`.`{$pod['field_type']}` = '{$type}'
5195
+ ";
5196
+
5197
+ pods_query( $sql, false );
5198
+ } elseif ( 'taxonomy' === $pod['type'] ) {
5199
+ // Delete all terms from this taxonomy
5200
+ if ( function_exists( 'get_term_meta' ) ) {
5201
+ $sql = "
5202
+ DELETE `t`, `m`, `tt`, `tr`
5203
+ FROM `{$pod['table']}` AS `t`
5204
+ LEFT JOIN `{$pod['meta_table']}` AS `m`
5205
+ ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
5206
+ " . $pod['join']['tt'] . "
5207
+ " . $pod['join']['tr'] . "
5208
+ WHERE " . implode( ' AND ', $pod['where'] ) . "
5209
+ ";
5210
+ } else {
5211
+ $sql = "
5212
+ DELETE `t`, `tt`, `tr`
5213
+ FROM `{$pod['table']}` AS `t`
5214
+ " . $pod['join']['tt'] . "
5215
+ " . $pod['join']['tr'] . "
5216
+ WHERE " . implode( ' AND ', $pod['where'] ) . "
5217
+ ";
5218
+ }
5219
+
5220
+ pods_query( $sql, false );
5221
+ } elseif ( 'user' === $pod['type'] ) {
5222
+ // Delete all users except the current one
5223
+ $sql = "
5224
+ DELETE `t`, `m`
5225
+ FROM `{$pod['table']}` AS `t`
5226
+ LEFT JOIN `{$pod['meta_table']}` AS `m`
5227
+ ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
5228
+ WHERE `t`.`{$pod['field_id']}` != " . (int) get_current_user_id() . "
5229
+ ";
5230
+
5231
+ pods_query( $sql, false );
5232
+ } elseif ( 'comment' === $pod['type'] ) {
5233
+ // Delete all comments
5234
+ $type = pods_var( 'object', $pod, $pod['name'], null, true );
5235
+
5236
+ $sql = "
5237
+ DELETE `t`, `m`
5238
+ FROM `{$pod['table']}` AS `t`
5239
+ LEFT JOIN `{$pod['meta_table']}` AS `m`
5240
+ ON `m`.`{$pod['meta_field_id']}` = `t`.`{$pod['field_id']}`
5241
+ WHERE `t`.`{$pod['field_type']}` = '{$type}'
5242
+ ";
5243
+
5244
+ pods_query( $sql, false );
5245
+ }
5246
+
5247
+ pods_cache_clear( true ); // only way to reliably clear out cached data across an entire group
5248
+
5249
+ return true;
5250
+ }
5251
+
5252
+ /**
5253
+ * Delete a Pod and all its content
5254
+ *
5255
+ * $params['id'] int The Pod ID
5256
+ * $params['name'] string The Pod name
5257
+ *
5258
+ * @param array $params An associative array of parameters
5259
+ * @param bool $strict (optional) Makes sure a pod exists, if it doesn't throws an error
5260
+ * @param bool $delete_all (optional) Whether to delete all content from a WP object
5261
+ *
5262
+ * @uses PodsAPI::load_pod
5263
+ * @uses wp_delete_post
5264
+ * @uses pods_query
5265
+ *
5266
+ * @return bool
5267
+ * @since 1.7.9
5268
+ */
5269
+ public function delete_pod( $params, $strict = false, $delete_all = false ) {
5270
+
5271
+ /**
5272
+ * @var $wpdb wpdb
5273
+ */
5274
+ global $wpdb;
5275
+
5276
+ if ( ! is_object( $params ) && ! is_array( $params ) ) {
5277
+ if ( is_numeric( $params ) ) {
5278
+ $params = array( 'id' => $params );
5279
+ } else {
5280
+ $params = array( 'name' => $params );
5281
+ }
5282
+
5283
+ $params = (object) pods_sanitize( $params );
5284
+ } else {
5285
+ $params = (object) pods_sanitize( $params );
5286
+ }
5287
+
5288
+ if ( ! isset( $params->delete_all ) ) {
5289
+ $params->delete_all = $delete_all;
5290
+ }
5291
+
5292
+ $params->table_info = false;
5293
+
5294
+ $pod = $this->load_pod( $params, $strict );
5295
+
5296
+ if ( empty( $pod ) ) {
5297
+ if ( false !== $strict ) {
5298
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
5299
+ }
5300
+
5301
+ return false;
5302
+ }
5303
+
5304
+ $params->id = (int) $pod['id'];
5305
+ $params->name = $pod['name'];
5306
+
5307
+ // Reset content
5308
+ if ( true === $params->delete_all ) {
5309
+ $this->reset_pod( $params, $pod );
5310
+ }
5311
+
5312
+ foreach ( $pod['fields'] as $field ) {
5313
+ $field['pod'] = $pod;
5314
+
5315
+ $this->delete_field( $field, false );
5316
+ }
5317
+
5318
+ // Only delete the post once the fields are taken care of, it's not required anymore
5319
+ $success = wp_delete_post( $params->id );
5320
+
5321
+ if ( ! $success ) {
5322
+ return pods_error( __( 'Pod unable to be deleted', 'pods' ), $this );
5323
+ }
5324
+
5325
+ if ( ! pods_tableless() ) {
5326
+ if ( 'table' === $pod['storage'] ) {
5327
+ try {
5328
+ pods_query( "DROP TABLE IF EXISTS `@wp_pods_{$params->name}`", false );
5329
+ } catch ( Exception $e ) {
5330
+ // Allow pod to be deleted if the table doesn't exist
5331
+ if ( false === strpos( $e->getMessage(), 'Unknown table' ) ) {
5332
+ return pods_error( $e->getMessage(), $this );
5333
+ }
5334
+ }
5335
+ }
5336
+
5337
+ pods_query( "DELETE FROM `@wp_podsrel` WHERE `pod_id` = {$params->id} OR `related_pod_id` = {$params->id}", false );
5338
+ }
5339
+
5340
+ // @todo Delete relationships from tableless relationships
5341
+
5342
+ // Delete any relationship references
5343
+ $sql = "
5344
+ DELETE `pm`
5345
+ FROM `{$wpdb->postmeta}` AS `pm`
5346
+ LEFT JOIN `{$wpdb->posts}` AS `p`
5347
+ ON `p`.`post_type` = '_pods_field'
5348
+ AND `p`.`ID` = `pm`.`post_id`
5349
+ LEFT JOIN `{$wpdb->postmeta}` AS `pm2`
5350
+ ON `pm2`.`meta_key` = 'pick_object'
5351
+ AND `pm2`.`meta_value` = 'pod'
5352
+ AND `pm2`.`post_id` = `pm`.`post_id`
5353
+ WHERE
5354
+ `p`.`ID` IS NOT NULL
5355
+ AND `pm2`.`meta_id` IS NOT NULL
5356
+ AND `pm`.`meta_key` = 'pick_val'
5357
+ AND `pm`.`meta_value` = '{$params->name}'
5358
+ ";
5359
+
5360
+ pods_query( $sql );
5361
+
5362
+ $this->cache_flush_pods( $pod );
5363
+
5364
+ return true;
5365
+ }
5366
+
5367
+ /**
5368
+ * Drop a field within a Pod
5369
+ *
5370
+ * $params['id'] int The field ID
5371
+ * $params['name'] int The field name
5372
+ * $params['pod'] string The Pod name
5373
+ * $params['pod_id'] string The Pod name
5374
+ *
5375
+ * @param array $params An associative array of parameters
5376
+ * @param bool $table_operation Whether or not to handle table operations
5377
+ *
5378
+ * @uses PodsAPI::load_field
5379
+ * @uses wp_delete_post
5380
+ * @uses pods_query
5381
+ *
5382
+ * @return bool
5383
+ * @since 1.7.9
5384
+ */
5385
+ public function delete_field( $params, $table_operation = true ) {
5386
+
5387
+ /**
5388
+ * @var $wpdb wpdb
5389
+ */
5390
+ global $wpdb;
5391
+
5392
+ $tableless_field_types = PodsForm::tableless_field_types();
5393
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
5394
+
5395
+ $params = (object) pods_sanitize( $params );
5396
+
5397
+ if ( ! isset( $params->pod ) ) {
5398
+ $params->pod = '';
5399
+ }
5400
+
5401
+ if ( ! isset( $params->pod_id ) ) {
5402
+ $params->pod_id = 0;
5403
+ }
5404
+
5405
+ $pod = $params->pod;
5406
+
5407
+ $save_pod = false;
5408
+
5409
+ if ( ! is_array( $pod ) ) {
5410
+ $pod = $this->load_pod( array( 'name' => $pod, 'id' => $params->pod_id, 'table_info' => false ) );
5411
+ } else {
5412
+ $save_pod = true;
5413
+ }
5414
+
5415
+ if ( empty( $pod ) ) {
5416
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
5417
+ }
5418
+
5419
+ $params->pod_id = $pod['id'];
5420
+ $params->pod = $pod['name'];
5421
+
5422
+ if ( ! isset( $params->name ) ) {
5423
+ $params->name = '';
5424
+ }
5425
+
5426
+ if ( ! isset( $params->id ) ) {
5427
+ $params->id = 0;
5428
+ }
5429
+
5430
+ $field = $this->load_field( array(
5431
+ 'name' => $params->name,
5432
+ 'id' => $params->id,
5433
+ 'pod' => $params->pod,
5434
+ 'pod_id' => $params->pod_id
5435
+ ) );
5436
+
5437
+ if ( false === $field ) {
5438
+ return pods_error( __( 'Field not found', 'pods' ), $this );
5439
+ }
5440
+
5441
+ $params->id = $field['id'];
5442
+ $params->name = $field['name'];
5443
+
5444
+ $simple = ( 'pick' === $field['type'] && in_array( pods_var( 'pick_object', $field ), $simple_tableless_objects ) );
5445
+ $simple = (boolean) $this->do_hook( 'tableless_custom', $simple, $field, $pod, $params );
5446
+
5447
+ if ( $table_operation && 'table' === $pod['storage'] && ( ! in_array( $field['type'], $tableless_field_types ) || $simple ) ) {
5448
+ pods_query( "ALTER TABLE `@wp_pods_{$params->pod}` DROP COLUMN `{$params->name}`", false );
5449
+ }
5450
+
5451
+ $success = wp_delete_post( $params->id );
5452
+
5453
+ if ( ! $success ) {
5454
+ return pods_error( __( 'Field unable to be deleted', 'pods' ), $this );
5455
+ }
5456
+
5457
+ $wpdb->query( $wpdb->prepare( "DELETE pm FROM {$wpdb->postmeta} AS pm
5458
+ LEFT JOIN {$wpdb->posts} AS p
5459
+ ON p.post_type = '_pods_field' AND p.ID = pm.post_id
5460
+ WHERE p.ID IS NOT NULL AND pm.meta_key = 'sister_id' AND pm.meta_value = %d", $params->id ) );
5461
+
5462
+ if ( ( ! pods_tableless() ) && $table_operation ) {
5463
+ pods_query( "DELETE FROM `@wp_podsrel` WHERE (`pod_id` = {$params->pod_id} AND `field_id` = {$params->id}) OR (`related_pod_id` = {$params->pod_id} AND `related_field_id` = {$params->id})", false );
5464
+ }
5465
+
5466
+ // @todo Delete tableless relationship meta
5467
+
5468
+ if ( true === $save_pod ) {
5469
+ $this->cache_flush_pods( $pod );
5470
+ }
5471
+
5472
+ return true;
5473
+ }
5474
+
5475
+ /**
5476
+ * Drop a Pod Object
5477
+ *
5478
+ * $params['id'] int The object ID
5479
+ * $params['name'] string The object name
5480
+ * $params['type'] string The object type
5481
+ *
5482
+ * @param array|object $params An associative array of parameters
5483
+ *
5484
+ * @uses wp_delete_post
5485
+ *
5486
+ * @return bool
5487
+ * @since 2.0.0
5488
+ */
5489
+ public function delete_object( $params ) {
5490
+
5491
+ $params = (object) $params;
5492
+ $object = $this->load_object( $params );
5493
+
5494
+ if ( empty( $object ) ) {
5495
+ return pods_error( sprintf( __( "%s Object not found", 'pods' ), ucwords( $params->type ) ), $this );
5496
+ }
5497
+
5498
+ $success = wp_delete_post( $params->id );
5499
+
5500
+ if ( ! $success ) {
5501
+ return pods_error( sprintf( __( "%s Object not deleted", 'pods' ), ucwords( $params->type ) ), $this );
5502
+ }
5503
+
5504
+ pods_transient_clear( 'pods_objects_' . $params->type );
5505
+
5506
+ return true;
5507
+ }
5508
+
5509
+ /**
5510
+ * @see PodsAPI::delete_object
5511
+ *
5512
+ * Drop a Pod Template
5513
+ *
5514
+ * $params['id'] int The template ID
5515
+ * $params['name'] string The template name
5516
+ *
5517
+ * @param array $params An associative array of parameters
5518
+ *
5519
+ * @return bool
5520
+ * @since 1.7.9
5521
+ */
5522
+ public function delete_template( $params ) {
5523
+
5524
+ $params = (object) $params;
5525
+ $params->type = 'template';
5526
+
5527
+ return $this->delete_object( $params );
5528
+ }
5529
+
5530
+ /**
5531
+ * @see PodsAPI::delete_object
5532
+ *
5533
+ * Drop a Pod Page
5534
+ *
5535
+ * $params['id'] int The page ID
5536
+ * $params['uri'] string The page URI
5537
+ *
5538
+ * @param array $params An associative array of parameters
5539
+ *
5540
+ * @return bool
5541
+ * @since 1.7.9
5542
+ */
5543
+ public function delete_page( $params ) {
5544
+
5545
+ $params = (object) $params;
5546
+ if ( isset( $params->uri ) ) {
5547
+ $params->name = $params->uri;
5548
+ unset( $params->uri );
5549
+ }
5550
+ if ( isset( $params->name ) ) {
5551
+ $params->name = trim( $params->name, '/' );
5552
+ }
5553
+ $params->type = 'page';
5554
+
5555
+ return $this->delete_object( $params );
5556
+ }
5557
+
5558
+ /**
5559
+ * @see PodsAPI::delete_object
5560
+ *
5561
+ * Drop a Pod Helper
5562
+ *
5563
+ * $params['id'] int The helper ID
5564
+ * $params['name'] string The helper name
5565
+ *
5566
+ * @param array $params An associative array of parameters
5567
+ *
5568
+ * @return bool
5569
+ * @since 1.7.9
5570
+ */
5571
+ public function delete_helper( $params ) {
5572
+
5573
+ $params = (object) $params;
5574
+ $params->type = 'helper';
5575
+
5576
+ return $this->delete_object( $params );
5577
+ }
5578
+
5579
+ /**
5580
+ * Drop a single pod item
5581
+ *
5582
+ * $params['id'] int (optional) The item's ID from the wp_pod_* table (used with datatype parameter)
5583
+ * $params['pod'] string (optional) The Pod name (used with id parameter)
5584
+ * $params['pod_id'] int (optional) The Pod ID (used with id parameter)
5585
+ * $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
5586
+ *
5587
+ * @param array $params An associative array of parameters
5588
+ * @param bool $wp Whether to run WP object delete action
5589
+ *
5590
+ * @return bool
5591
+ * @since 1.7.9
5592
+ */
5593
+ public function delete_pod_item( $params, $wp = true ) {
5594
+
5595
+ $params = (object) pods_sanitize( $params );
5596
+
5597
+ // @deprecated 2.0.0
5598
+ if ( isset( $params->datatype_id ) || isset( $params->datatype ) || isset( $params->tbl_row_id ) ) {
5599
+ if ( isset( $params->tbl_row_id ) ) {
5600
+ pods_deprecated( __( '$params->id instead of $params->tbl_row_id', 'pods' ), '2.0' );
5601
+ $params->id = $params->tbl_row_id;
5602
+ unset( $params->tbl_row_id );
5603
+ }
5604
+
5605
+ if ( isset( $params->pod_id ) ) {
5606
+ pods_deprecated( __( '$params->id instead of $params->pod_id', 'pods' ), '2.0' );
5607
+ $params->id = $params->pod_id;
5608
+ unset( $params->pod_id );
5609
+ }
5610
+
5611
+ if ( isset( $params->dataype_id ) ) {
5612
+ pods_deprecated( __( '$params->pod_id instead of $params->datatype_id', 'pods' ), '2.0' );
5613
+ $params->pod_id = $params->dataype_id;
5614
+ unset( $params->dataype_id );
5615
+ }
5616
+
5617
+ if ( isset( $params->datatype ) ) {
5618
+ pods_deprecated( __( '$params->pod instead of $params->datatype', 'pods' ), '2.0' );
5619
+ $params->pod = $params->datatype;
5620
+ unset( $params->datatype );
5621
+ }
5622
+ }
5623
+
5624
+ if ( ! isset( $params->id ) ) {
5625
+ return pods_error( __( 'Pod Item not found', 'pods' ), $this );
5626
+ }
5627
+
5628
+ $params->id = pods_absint( $params->id );
5629
+
5630
+ if ( ! isset( $params->pod ) ) {
5631
+ $params->pod = '';
5632
+ }
5633
+
5634
+ if ( ! isset( $params->pod_id ) ) {
5635
+ $params->pod_id = 0;
5636
+ }
5637
+
5638
+ $pod = $this->load_pod( array( 'name' => $params->pod, 'id' => $params->pod_id, 'table_info' => false ) );
5639
+
5640
+ if ( false === $pod ) {
5641
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
5642
+ }
5643
+
5644
+ $params->pod_id = $pod['id'];
5645
+ $params->pod = $pod['name'];
5646
+
5647
+ // Allow Helpers to bypass subsequent helpers in recursive delete_pod_item calls
5648
+ $bypass_helpers = false;
5649
+
5650
+ if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers ) {
5651
+ $bypass_helpers = true;
5652
+ }
5653
+
5654
+ $pre_delete_helpers = array();
5655
+ $post_delete_helpers = array();
5656
+
5657
+ if ( false === $bypass_helpers ) {
5658
+ // Plugin hook
5659
+ $this->do_hook( 'pre_delete_pod_item', $params, $pod );
5660
+ $this->do_hook( "pre_delete_pod_item_{$params->pod}", $params, $pod );
5661
+
5662
+ // Call any pre-save helpers (if not bypassed)
5663
+ if ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) {
5664
+ if ( ! empty( $pod['options'] ) && is_array( $pod['options'] ) ) {
5665
+ $helpers = array( 'pre_delete_helpers', 'post_delete_helpers' );
5666
+
5667
+ foreach ( $helpers as $helper ) {
5668
+ if ( isset( $pod['options'][ $helper ] ) && ! empty( $pod['options'][ $helper ] ) ) {
5669
+ ${$helper} = explode( ',', $pod['options'][ $helper ] );
5670
+ }
5671
+ }
5672
+ }
5673
+
5674
+ if ( ! empty( $pre_delete_helpers ) ) {
5675
+ pods_deprecated( sprintf( __( 'Pre-delete helpers are deprecated, use the action pods_pre_delete_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
5676
+
5677
+ foreach ( $pre_delete_helpers as $helper ) {
5678
+ $helper = $this->load_helper( array( 'name' => $helper ) );
5679
+
5680
+ if ( false !== $helper ) {
5681
+ eval( '?>' . $helper['code'] );
5682
+ }
5683
+ }
5684
+ }
5685
+ }
5686
+ }
5687
+
5688
+ // Delete object from relationship fields
5689
+ $this->delete_object_from_relationships( $params->id, $pod );
5690
+
5691
+ if ( 'table' === $pod['storage'] ) {
5692
+ pods_query( "DELETE FROM `@wp_pods_{$params->pod}` WHERE `id` = {$params->id} LIMIT 1" );
5693
+ }
5694
+
5695
+ if ( $wp ) {
5696
+ if ( 'taxonomy' === $pod['type'] ) {
5697
+ $taxonomy = $pod['name'];
5698
+
5699
+ if ( ! empty( $pod['object'] ) ) {
5700
+ $taxonomy = $pod['object'];
5701
+ }
5702
+
5703
+ wp_delete_term( $params->id, $taxonomy );
5704
+ } elseif ( ! in_array( $pod['type'], array( 'pod', 'table', '', 'taxonomy' ) ) ) {
5705
+ $this->delete_wp_object( $pod['type'], $params->id );
5706
+ }
5707
+ }
5708
+
5709
+ if ( false === $bypass_helpers ) {
5710
+ // Plugin hook
5711
+ $this->do_hook( 'post_delete_pod_item', $params, $pod );
5712
+ $this->do_hook( "post_delete_pod_item_{$params->pod}", $params, $pod );
5713
+
5714
+ // Call any post-save helpers (if not bypassed)
5715
+ if ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) {
5716
+ if ( ! empty( $post_delete_helpers ) ) {
5717
+ pods_deprecated( sprintf( __( 'Post-delete helpers are deprecated, use the action pods_post_delete_pod_item_%s instead', 'pods' ), $params->pod ), '2.0' );
5718
+
5719
+ foreach ( $post_delete_helpers as $helper ) {
5720
+ $helper = $this->load_helper( array( 'name' => $helper ) );
5721
+
5722
+ if ( false !== $helper ) {
5723
+ eval( '?>' . $helper['code'] );
5724
+ }
5725
+ }
5726
+ }
5727
+ }
5728
+ }
5729
+
5730
+ pods_cache_clear( $params->id, 'pods_items_' . $params->pod );
5731
+
5732
+ return true;
5733
+ }
5734
+
5735
+ /**
5736
+ * Delete an object from tableless fields
5737
+ *
5738
+ * @param int $id
5739
+ * @param string $type
5740
+ * @param string $name
5741
+ *
5742
+ * @return bool
5743
+ *
5744
+ * @since 2.3.0
5745
+ */
5746
+ public function delete_object_from_relationships( $id, $object, $name = null ) {
5747
+
5748
+ /**
5749
+ * @var $pods_init \PodsInit
5750
+ * @todo Use pods_init() function?
5751
+ */
5752
+ global $pods_init;
5753
+
5754
+ $pod = false;
5755
+
5756
+ // Run any bidirectional delete operations
5757
+ if ( is_array( $object ) ) {
5758
+ $pod = $object;
5759
+ } elseif ( is_object( $pods_init ) ) {
5760
+ $pod = PodsInit::$meta->get_object( $object, $name );
5761
+ }
5762
+
5763
+ if ( ! empty( $pod ) ) {
5764
+ $object = $pod['type'];
5765
+ $name = $pod['name'];
5766
+
5767
+ foreach ( $pod['fields'] as $field ) {
5768
+ PodsForm::delete( $field['type'], $id, $field['name'], array_merge( $field, $field['options'] ), $pod );
5769
+ }
5770
+ }
5771
+
5772
+ // Lookup related fields (non-bidirectional)
5773
+ $params = array(
5774
+ 'where' => array(
5775
+ array(
5776
+ 'key' => 'type',
5777
+ 'value' => 'pick'
5778
+ ),
5779
+ array(
5780
+ 'key' => 'pick_object',
5781
+ 'value' => $object
5782
+ )
5783
+ )
5784
+ );
5785
+
5786
+ if ( ! empty( $name ) && $name !== $object ) {
5787
+ $params['where'][] = array(
5788
+ 'key' => 'pick_val',
5789
+ 'value' => $name
5790
+ );
5791
+ }
5792
+
5793
+ $fields = $this->load_fields( $params, false );
5794
+
5795
+ if ( ! empty( $pod ) && 'media' === $pod['type'] ) {
5796
+ $params['where'] = array(
5797
+ array(
5798
+ 'key' => 'type',
5799
+ 'value' => 'file'
5800
+ )
5801
+ );
5802
+
5803
+ $fields = array_merge( $fields, $this->load_fields( $params, false ) );
5804
+ }
5805
+
5806
+ if ( is_array( $fields ) && ! empty( $fields ) ) {
5807
+ foreach ( $fields as $related_field ) {
5808
+ $related_pod = $this->load_pod( array( 'id' => $related_field['pod_id'], 'fields' => false ), false );
5809
+
5810
+ if ( empty( $related_pod ) ) {
5811
+ continue;
5812
+ }
5813
+
5814
+ $related_from = $this->lookup_related_items_from( $related_field['id'], $related_pod['id'], $id, $related_field, $related_pod );
5815
+
5816
+ $this->delete_relationships( $related_from, $id, $related_pod, $related_field );
5817
+ }
5818
+ }
5819
+
5820
+ if ( ! empty( $pod ) && ! pods_tableless() ) {
5821
+ pods_query( "
5822
+ DELETE FROM `@wp_podsrel`
5823
+ WHERE
5824
+ (
5825
+ `pod_id` = %d
5826
+ AND `item_id` = %d
5827
+ )
5828
+ OR (
5829
+ `related_pod_id` = %d
5830
+ AND `related_item_id` = %d
5831
+ )
5832
+ ", array(
5833
+ $pod['id'],
5834
+ $id,
5835
+
5836
+ $pod['id'],
5837
+ $id
5838
+ ) );
5839
+ }
5840
+
5841
+ return true;
5842
+ }
5843
+
5844
+ /**
5845
+ * Delete relationships
5846
+ *
5847
+ * @param int|array $related_id IDs for items to save
5848
+ * @param int|array $id ID or IDs to remove
5849
+ * @param array $related_pod Pod data
5850
+ * @param array $related_field Field data
5851
+ *
5852
+ * @return void
5853
+ *
5854
+ * @since 2.3.0
5855
+ */
5856
+ public function delete_relationships( $related_id, $id, $related_pod, $related_field ) {
5857
+
5858
+ if ( is_array( $related_id ) ) {
5859
+ foreach ( $related_id as $rid ) {
5860
+ $this->delete_relationships( $rid, $id, $related_pod, $related_field );
5861
+ }
5862
+
5863
+ return;
5864
+ }
5865
+
5866
+ if ( is_array( $id ) ) {
5867
+ foreach ( $id as $rid ) {
5868
+ $this->delete_relationships( $related_id, $rid, $related_pod, $related_field );
5869
+ }
5870
+
5871
+ return;
5872
+ }
5873
+
5874
+ $id = (int) $id;
5875
+
5876
+ if ( empty( $id ) ) {
5877
+ return;
5878
+ }
5879
+
5880
+ $related_ids = $this->lookup_related_items( $related_field['id'], $related_pod['id'], $related_id, $related_field, $related_pod );
5881
+
5882
+ if ( empty( $related_ids ) ) {
5883
+ return;
5884
+ } elseif ( ! in_array( $id, $related_ids ) ) {
5885
+ return;
5886
+ }
5887
+
5888
+ if ( isset( self::$related_item_cache[ $related_pod['id'] ][ $related_field['id'] ] ) ) {
5889
+ // Delete relationship from cache
5890
+ unset( self::$related_item_cache[ $related_pod['id'] ][ $related_field['id'] ] );
5891
+ }
5892
+
5893
+ // @codingStandardsIgnoreLine
5894
+ unset( $related_ids[ array_search( $id, $related_ids ) ] );
5895
+
5896
+ $no_conflict = pods_no_conflict_check( $related_pod['type'] );
5897
+
5898
+ if ( ! $no_conflict ) {
5899
+ pods_no_conflict_on( $related_pod['type'] );
5900
+ }
5901
+
5902
+ // Post Types, Media, Users, and Comments (meta-based)
5903
+ if ( in_array( $related_pod['type'], array( 'post_type', 'media', 'taxonomy', 'user', 'comment' ) ) ) {
5904
+ $object_type = $related_pod['type'];
5905
+
5906
+ if ( in_array( $object_type, array( 'post_type', 'media' ) ) ) {
5907
+ $object_type = 'post';
5908
+ } elseif ( 'taxonomy' === $object_type ) {
5909
+ $object_type = 'term';
5910
+ }
5911
+
5912
+ delete_metadata( $object_type, $related_id, $related_field['name'] );
5913
+
5914
+ if ( ! empty( $related_ids ) ) {
5915
+ update_metadata( $object_type, $related_id, '_pods_' . $related_field['name'], $related_ids );
5916
+
5917
+ foreach ( $related_ids as $rel_id ) {
5918
+ add_metadata( $object_type, $related_id, $related_field['name'], $rel_id );
5919
+ }
5920
+ } else {
5921
+ delete_metadata( $object_type, $related_id, '_pods_' . $related_field['name'] );
5922
+ }
5923
+ } elseif ( 'settings' === $related_pod['type'] ) {
5924
+ // Custom Settings Pages (options-based)
5925
+ if ( ! empty( $related_ids ) ) {
5926
+ update_option( $related_pod['name'] . '_' . $related_field['name'], $related_ids );
5927
+ } else {
5928
+ delete_option( $related_pod['name'] . '_' . $related_field['name'] );
5929
+ }
5930
+ }
5931
+
5932
+ // Relationships table
5933
+ if ( ! pods_tableless() ) {
5934
+ pods_query( "
5935
+ DELETE FROM `@wp_podsrel`
5936
+ WHERE
5937
+ (
5938
+ `pod_id` = %d
5939
+ AND `field_id` = %d
5940
+ AND `item_id` = %d
5941
+ AND `related_item_id` = %d
5942
+ )
5943
+ OR (
5944
+ `related_pod_id` = %d
5945
+ AND `related_field_id` = %d
5946
+ AND `related_item_id` = %d
5947
+ AND `item_id` = %d
5948
+ )
5949
+ ", array(
5950
+ $related_pod['id'],
5951
+ $related_field['id'],
5952
+ $related_id,
5953
+ $id,
5954
+
5955
+ $related_pod['id'],
5956
+ $related_field['id'],
5957
+ $related_id,
5958
+ $id
5959
+ ) );
5960
+ }
5961
+
5962
+ if ( ! $no_conflict ) {
5963
+ pods_no_conflict_off( $related_pod['type'] );
5964
+ }
5965
+ }
5966
+
5967
+ /**
5968
+ * Check if a Pod exists
5969
+ *
5970
+ * $params['id'] int Pod ID
5971
+ * $params['name'] string Pod name
5972
+ *
5973
+ * @param array $params An associative array of parameters
5974
+ *
5975
+ * @return bool True if exists
5976
+ *
5977
+ * @since 1.12
5978
+ */
5979
+ public function pod_exists( $params, $type = null ) {
5980
+
5981
+ if ( is_string( $params ) ) {
5982
+ $params = array( 'name' => $params );
5983
+ }
5984
+
5985
+ $params = (object) pods_sanitize( $params );
5986
+
5987
+ if ( ! empty( $params->id ) || ! empty( $params->name ) ) {
5988
+ if ( ! isset( $params->name ) ) {
5989
+ $dummy = (int) $params->id;
5990
+ $pod = get_post( $dummy );
5991
+ } else {
5992
+ $pod = get_posts( array(
5993
+ 'name' => $params->name,
5994
+ 'post_type' => '_pods_pod',
5995
+ 'posts_per_page' => 1
5996
+ ) );
5997
+
5998
+ if ( is_array( $pod ) && ! empty( $pod[0] ) ) {
5999
+ $pod = $pod[0];
6000
+ }
6001
+ }
6002
+
6003
+ if ( ! empty( $pod ) && ( empty( $type ) || $type == get_post_meta( $pod->ID, 'type', true ) ) ) {
6004
+ return true;
6005
+ }
6006
+ }
6007
+
6008
+ return false;
6009
+ }
6010
+
6011
+ /**
6012
+ * Get number of pods for a specific pod type
6013
+ *
6014
+ * @param string $type Type to get count
6015
+ *
6016
+ * @return int Total number of pods for a type
6017
+ *
6018
+ * @since 2.6.6
6019
+ */
6020
+ public function get_pod_type_count( $type ) {
6021
+
6022
+ $args = array(
6023
+ 'post_type' => '_pods_pod',
6024
+ 'posts_per_page' => - 1,
6025
+ 'nopaging' => true,
6026
+ 'fields' => 'ids',
6027
+ 'meta_query' => array(
6028
+ array(
6029
+ 'key' => 'type',
6030
+ 'value' => $type,
6031
+ ),
6032
+ ),
6033
+ );
6034
+
6035
+ $posts = get_posts( $args );
6036
+
6037
+ $total = count( $posts );
6038
+
6039
+ return $total;
6040
+
6041
+ }
6042
+
6043
+ /**
6044
+ * Load a Pod and all of its fields
6045
+ *
6046
+ * $params['id'] int The Pod ID
6047
+ * $params['name'] string The Pod name
6048
+ * $params['fields'] bool Whether to load fields (default is true)
6049
+ * $params['bypass_cache'] boolean Bypass the cache when getting data
6050
+ *
6051
+ * @param array|object $params An associative array of parameters or pod name as a string
6052
+ * @param bool $strict Makes sure the pod exists, throws an error if it doesn't work
6053
+ *
6054
+ * @return array|bool|mixed|void
6055
+ * @since 1.7.9
6056
+ */
6057
+ public function load_pod( $params, $strict = true ) {
6058
+
6059
+ /**
6060
+ * @var $sitepress SitePress
6061
+ * @var $wpdb wpdb
6062
+ */
6063
+ global $wpdb;
6064
+
6065
+ $current_language = false;
6066
+ $load_fields = true;
6067
+ $bypass_cache = false;
6068
+
6069
+ // Get current language data
6070
+ $lang_data = pods_i18n()->get_current_language_data();
6071
+
6072
+ if ( $lang_data ) {
6073
+ if ( ! empty( $lang_data['language'] ) ) {
6074
+ $current_language = $lang_data['language'];
6075
+ }
6076
+ }
6077
+
6078
+ if ( ! is_array( $params ) && ! is_object( $params ) ) {
6079
+ $params = array( 'name' => $params, 'table_info' => false, 'fields' => true );
6080
+ }
6081
+
6082
+ if ( is_object( $params ) && ! is_a( $params, 'WP_Post' ) && isset( $params->fields ) && ! $params->fields ) {
6083
+ $load_fields = false;
6084
+ } elseif ( is_array( $params ) && isset( $params['fields'] ) && ! $params['fields'] ) {
6085
+ $load_fields = false;
6086
+ }
6087
+
6088
+ $table_info = false;
6089
+
6090
+ if ( is_object( $params ) && ! is_a( $params, 'WP_Post' ) && ! empty( $params->table_info ) ) {
6091
+ $table_info = true;
6092
+ } elseif ( is_array( $params ) && ! empty( $params['table_info'] ) ) {
6093
+ $table_info = true;
6094
+ }
6095
+
6096
+ $transient = 'pods_' . $wpdb->prefix . '_pod';
6097
+
6098
+ if ( ! empty( $current_language ) ) {
6099
+ $transient .= '_' . $current_language;
6100
+ }
6101
+
6102
+ if ( ! $load_fields ) {
6103
+ $transient .= '_nofields';
6104
+ }
6105
+
6106
+ if ( $table_info ) {
6107
+ $transient .= '_tableinfo';
6108
+ }
6109
+
6110
+ $check_pod = $params;
6111
+
6112
+ if ( is_object( $params ) && ! is_a( $params, 'WP_Post' ) && ! empty( $params->pod ) ) {
6113
+ $check_pod = $params->pod;
6114
+ } elseif ( is_array( $params ) && ! empty( $params['pod'] ) ) {
6115
+ $check_pod = $params['pod'];
6116
+ }
6117
+
6118
+ if ( is_object( $check_pod ) && ( is_a( $check_pod, 'WP_Post' ) || isset( $check_pod->post_name ) ) ) {
6119
+ $pod = false;
6120
+
6121
+ if ( pods_api_cache() ) {
6122
+ $pod = pods_transient_get( $transient . '_' . $check_pod->post_name );
6123
+ }
6124
+
6125
+ if ( false !== $pod && ( ! $table_info || isset( $pod['table'] ) ) ) {
6126
+ // @todo Is this needed anymore for WPML?
6127
+ if ( in_array( $pod['type'], array(
6128
+ 'post_type',
6129
+ 'taxonomy'
6130
+ ) ) && did_action( 'wpml_loaded' ) && apply_filters( 'wpml_setting', true, 'auto_adjust_ids' ) ) {
6131
+ $pod = array_merge( $pod, $this->get_table_info( $pod['type'], $pod['object'], $pod['name'], $pod ) );
6132
+ }
6133
+
6134
+ return $pod;
6135
+ }
6136
+
6137
+ $_pod = get_object_vars( $check_pod );
6138
+ } else {
6139
+ $params = (object) pods_sanitize( $params );
6140
+
6141
+ if ( ( ! isset( $params->id ) || empty( $params->id ) ) && ( ! isset( $params->name ) || empty( $params->name ) ) ) {
6142
+ if ( $strict ) {
6143
+ return pods_error( __( 'Either Pod ID or Name are required', 'pods' ), $this );
6144
+ }
6145
+
6146
+ return false;
6147
+ }
6148
+
6149
+ if ( ! empty( $params->bypass_cache ) ) {
6150
+ $bypass_cache = true;
6151
+ }
6152
+
6153
+ if ( isset( $params->name ) ) {
6154
+ $pod = false;
6155
+
6156
+ if ( '_pods_pod' === $params->name ) {
6157
+ $pod = array(
6158
+ 'id' => 0,
6159
+ 'name' => $params->name,
6160
+ 'object' => $params->name,
6161
+ 'label' => __( 'Pods', 'pods' ),
6162
+ 'type' => 'post_type',
6163
+ 'storage' => 'meta',
6164
+ 'options' => array(
6165
+ 'label_singular' => __( 'Pod', 'pods' )
6166
+ ),
6167
+ 'fields' => array()
6168
+ );
6169
+ } elseif ( '_pods_field' === $params->name ) {
6170
+ $pod = array(
6171
+ 'id' => 0,
6172
+ 'name' => $params->name,
6173
+ 'object' => $params->name,
6174
+ 'label' => __( 'Pod Fields', 'pods' ),
6175
+ 'type' => 'post_type',
6176
+ 'storage' => 'meta',
6177
+ 'options' => array(
6178
+ 'label_singular' => __( 'Pod Field', 'pods' )
6179
+ ),
6180
+ 'fields' => array()
6181
+ );
6182
+ } elseif ( ! $bypass_cache & pods_api_cache() ) {
6183
+ $pod = pods_transient_get( $transient . '_' . $params->name );
6184
+ }
6185
+
6186
+ if ( false !== $pod && ( ! $table_info || isset( $pod['table'] ) ) ) {
6187
+ if ( in_array( $pod['type'], array(
6188
+ 'post_type',
6189
+ 'taxonomy'
6190
+ ) ) && did_action( 'wpml_loaded' ) && apply_filters( 'wpml_setting', true, 'auto_adjust_ids' ) ) {
6191
+ $pod = array_merge( $pod, $this->get_table_info( $pod['type'], $pod['object'], $pod['name'], $pod ) );
6192
+ }
6193
+
6194
+ return $pod;
6195
+ }
6196
+ }
6197
+
6198
+ if ( ! isset( $params->name ) ) {
6199
+ $dummy = (int) $params->id;
6200
+ $pod = get_post( $dummy );
6201
+ } else {
6202
+ $pod = get_posts( array(
6203
+ 'name' => $params->name,
6204
+ 'post_type' => '_pods_pod',
6205
+ 'posts_per_page' => 1
6206
+ ) );
6207
+ }
6208
+
6209
+ if ( empty( $pod ) ) {
6210
+ if ( $strict ) {
6211
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
6212
+ }
6213
+
6214
+ return false;
6215
+ }
6216
+
6217
+ if ( is_array( $pod ) && ! empty( $pod[0] ) ) {
6218
+ $pod = $pod[0];
6219
+ }
6220
+
6221
+ $_pod = get_object_vars( $pod );
6222
+ }
6223
+
6224
+ $pod = false;
6225
+
6226
+ if ( ! $bypass_cache || pods_api_cache() ) {
6227
+ $pod = pods_transient_get( $transient . '_' . $_pod['post_name'] );
6228
+ }
6229
+
6230
+ if ( false !== $pod && ( ! $table_info || isset( $pod['table'] ) ) ) {
6231
+ if ( in_array( $pod['type'], array(
6232
+ 'post_type',
6233
+ 'taxonomy'
6234
+ ) ) && did_action( 'wpml_loaded' ) && apply_filters( 'wpml_setting', true, 'auto_adjust_ids' ) ) {
6235
+ $pod = array_merge( $pod, $this->get_table_info( $pod['type'], $pod['object'], $pod['name'], $pod ) );
6236
+ }
6237
+
6238
+ return $pod;
6239
+ }
6240
+
6241
+ $pod = array(
6242
+ 'id' => $_pod['ID'],
6243
+ 'name' => $_pod['post_name'],
6244
+ 'label' => $_pod['post_title'],
6245
+ 'description' => $_pod['post_content']
6246
+ );
6247
+
6248
+ if ( strlen( $pod['label'] ) < 1 ) {
6249
+ $pod['label'] = $pod['name'];
6250
+ }
6251
+
6252
+ // @todo update with a method to put all options in
6253
+ $defaults = array(
6254
+ 'show_in_menu' => 1,
6255
+ 'type' => 'post_type',
6256
+ 'storage' => 'meta',
6257
+ 'object' => '',
6258
+ 'alias' => ''
6259
+ );
6260
+
6261
+ if ( $bypass_cache ) {
6262
+ wp_cache_delete( $pod['id'], 'post_meta' );
6263
+
6264
+ update_postmeta_cache( array( $pod['id'] ) );
6265
+ }
6266
+
6267
+ $pod['options'] = get_post_meta( $pod['id'] );
6268
+
6269
+ foreach ( $pod['options'] as $option => $value ) {
6270
+ if ( is_array( $value ) ) {
6271
+ foreach ( $value as $k => $v ) {
6272
+ if ( ! is_array( $v ) ) {
6273
+ $value[ $k ] = maybe_unserialize( $v );
6274
+ }
6275
+ }
6276
+
6277
+ if ( 1 == count( $value ) ) {
6278
+ $value = current( $value );
6279
+ }
6280
+ } else {
6281
+ $value = maybe_unserialize( $value );
6282
+ }
6283
+
6284
+ $pod['options'][ $option ] = $value;
6285
+ }
6286
+
6287
+ $pod['options'] = array_merge( $defaults, $pod['options'] );
6288
+
6289
+ $pod['type'] = $pod['options']['type'];
6290
+ $pod['storage'] = $pod['options']['storage'];
6291
+ $pod['object'] = $pod['options']['object'];
6292
+ $pod['alias'] = $pod['options']['alias'];
6293
+
6294
+ unset( $pod['options']['type'] );
6295
+ unset( $pod['options']['storage'] );
6296
+ unset( $pod['options']['object'] );
6297
+ unset( $pod['options']['alias'] );
6298
+
6299
+ if ( $table_info ) {
6300
+ $pod = array_merge( $this->get_table_info( $pod['type'], $pod['object'], $pod['name'], $pod ), $pod );
6301
+ }
6302
+
6303
+ // Override old 'none' storage type
6304
+ if ( 'taxonomy' === $pod['type'] && 'none' === $pod['storage'] && function_exists( 'get_term_meta' ) ) {
6305
+ $pod['storage'] = 'meta';
6306
+ }
6307
+
6308
+ if ( isset( $pod['pod'] ) ) {
6309
+ unset( $pod['pod'] );
6310
+ }
6311
+
6312
+ $pod['fields'] = array();
6313
+
6314
+ $pod['object_fields'] = array();
6315
+
6316
+ if ( 'pod' !== $pod['type'] ) {
6317
+ $pod['object_fields'] = $this->get_wp_object_fields( $pod['type'], $pod );
6318
+ }
6319
+
6320
+ $fields = get_posts( array(
6321
+ 'post_type' => '_pods_field',
6322
+ 'posts_per_page' => - 1,
6323
+ 'nopaging' => true,
6324
+ 'post_parent' => $pod['id'],
6325
+ 'orderby' => 'menu_order',
6326
+ 'order' => 'ASC'
6327
+ ) );
6328
+
6329
+ if ( ! empty( $fields ) ) {
6330
+ foreach ( $fields as $field ) {
6331
+ $field->pod = $pod['name'];
6332
+ $field->table_info = $table_info;
6333
+ $field->bypass_cache = $bypass_cache;
6334
+
6335
+ if ( $load_fields ) {
6336
+ $field = $this->load_field( $field );
6337
+
6338
+ $field = PodsForm::field_setup( $field, null, $field['type'] );
6339
+ } else {
6340
+ if ( $bypass_cache ) {
6341
+ wp_cache_delete( $field->ID, 'post_meta' );
6342
+
6343
+ update_postmeta_cache( array( $field->ID ) );
6344
+ }
6345
+
6346
+ $field = array(
6347
+ 'id' => $field->ID,
6348
+ 'name' => $field->post_name,
6349
+ 'label' => $field->post_title,
6350
+ 'type' => get_post_meta( $field->ID, 'type', true )
6351
+ );
6352
+ }
6353
+
6354
+ $pod['fields'][ $field['name'] ] = $field;
6355
+ }
6356
+ }
6357
+
6358
+ if ( did_action( 'init' ) && pods_api_cache() ) {
6359
+ pods_transient_set( $transient . '_' . $pod['name'], $pod );
6360
+ }
6361
+
6362
+ return $pod;
6363
+ }
6364
+
6365
+ /**
6366
+ * Load a list of Pods based on filters specified.
6367
+ *
6368
+ * $params['type'] string/array Pod Type(s) to filter by
6369
+ * $params['object'] string/array Pod Object(s) to filter by
6370
+ * $params['options'] array Pod Option(s) key=>value array to filter by
6371
+ * $params['orderby'] string ORDER BY clause of query
6372
+ * $params['limit'] string Number of Pods to return
6373
+ * $params['where'] string WHERE clause of query
6374
+ * $params['ids'] string|array IDs of Objects
6375
+ * $params['count'] boolean Return only a count of Pods
6376
+ * $params['names'] boolean Return only an array of name => label
6377
+ * $params['ids'] boolean Return only an array of ID => label
6378
+ * $params['fields'] boolean Return pod fields with Pods (default is true)
6379
+ * $params['key_names'] boolean Return pods keyed by name
6380
+ * $params['bypass_cache'] boolean Bypass the cache when getting data
6381
+ *
6382
+ * @param array $params An associative array of parameters
6383
+ *
6384
+ * @return array|mixed
6385
+ *
6386
+ * @uses PodsAPI::load_pod
6387
+ *
6388
+ * @since 2.0.0
6389
+ */
6390
+ public function load_pods( $params = null ) {
6391
+
6392
+ $current_language = false;
6393
+
6394
+ // Get current language data
6395
+ $lang_data = pods_i18n()->get_current_language_data();
6396
+
6397
+ if ( $lang_data ) {
6398
+ if ( ! empty( $lang_data['language'] ) ) {
6399
+ $current_language = $lang_data['language'];
6400
+ }
6401
+ }
6402
+
6403
+ $params = (object) pods_sanitize( $params );
6404
+
6405
+ $order = 'ASC';
6406
+ $orderby = 'menu_order title';
6407
+ $limit = - 1;
6408
+ $ids = false;
6409
+
6410
+ $meta_query = array();
6411
+ $cache_key = '';
6412
+
6413
+ $bypass_cache = false;
6414
+
6415
+ if ( ! empty( $params->bypass_cache ) ) {
6416
+ $bypass_cache = true;
6417
+ }
6418
+
6419
+ if ( isset( $params->type ) && ! empty( $params->type ) ) {
6420
+ if ( ! is_array( $params->type ) ) {
6421
+ $params->type = array( trim( $params->type ) );
6422
+ }
6423
+
6424
+ sort( $params->type );
6425
+
6426
+ $meta_query[] = array(
6427
+ 'key' => 'type',
6428
+ 'value' => $params->type,
6429
+ 'compare' => 'IN'
6430
+ );
6431
+
6432
+ if ( 0 < count( $params->type ) ) {
6433
+ $cache_key .= '_type_' . trim( implode( '_', $params->type ) );
6434
+ }
6435
+ }
6436
+
6437
+ if ( isset( $params->object ) && ! empty( $params->object ) ) {
6438
+ if ( ! is_array( $params->object ) ) {
6439
+ $params->object = array( $params->object );
6440
+ }
6441
+
6442
+ $params->object = pods_trim( $params->object );
6443
+
6444
+ sort( $params->object );
6445
+
6446
+ $meta_query[] = array(
6447
+ 'key' => 'object',
6448
+ 'value' => $params->object,
6449
+ 'compare' => 'IN'
6450
+ );
6451
+
6452
+ if ( 1 == count( $params->object ) ) {
6453
+ $cache_key .= '_object_' . trim( implode( '', $params->object ) );
6454
+ }
6455
+ }
6456
+
6457
+ if ( isset( $params->options ) && ! empty( $params->options ) && is_array( $params->options ) ) {
6458
+ foreach ( $params->options as $option => $value ) {
6459
+ if ( ! is_array( $value ) ) {
6460
+ $value = array( $value );
6461
+ }
6462
+
6463
+ $value = pods_trim( $value );
6464
+
6465
+ sort( $value );
6466
+
6467
+ $meta_query[] = array(
6468
+ 'key' => $option,
6469
+ 'value' => pods_sanitize( $value ),
6470
+ 'compare' => 'IN'
6471
+ );
6472
+ }
6473
+
6474
+ $cache_key = '';
6475
+ }
6476
+
6477
+ if ( isset( $params->where ) && is_array( $params->where ) ) {
6478
+ $meta_query = array_merge( $meta_query, (array) $params->where );
6479
+ }
6480
+
6481
+ if ( isset( $params->order ) && ! empty( $params->order ) && in_array( strtoupper( $params->order ), array(
6482
+ 'ASC',
6483
+ 'DESC'
6484
+ ) ) ) {
6485
+ $order = strtoupper( $params->order );
6486
+ }
6487
+
6488
+ if ( isset( $params->orderby ) && ! empty( $params->orderby ) ) {
6489
+ $orderby = strtoupper( $params->orderby );
6490
+ }
6491
+
6492
+ if ( isset( $params->limit ) && ! empty( $params->limit ) ) {
6493
+ $limit = pods_absint( $params->limit );
6494
+ }
6495
+
6496
+ if ( isset( $params->ids ) && ! empty( $params->ids ) ) {
6497
+ $ids = $params->ids;
6498
+
6499
+ if ( ! is_array( $ids ) ) {
6500
+ $ids = explode( ',', $ids );
6501
+ }
6502
+ }
6503
+
6504
+ if ( empty( $ids ) ) {
6505
+ $ids = false;
6506
+ }
6507
+
6508
+ $pre_key = '';
6509
+
6510
+ if ( ! empty( $current_language ) ) {
6511
+ $pre_key .= '_' . $current_language;
6512
+ }
6513
+
6514
+ if ( isset( $params->count ) && $params->count ) {
6515
+ $pre_key .= '_count';
6516
+ }
6517
+
6518
+ if ( isset( $params->ids ) && $params->ids && ! empty( $ids ) ) {
6519
+ $pre_key .= '_ids_' . implode( '_', $ids );
6520
+ }
6521
+
6522
+ if ( isset( $params->names ) && $params->names ) {
6523
+ $pre_key .= '_names';
6524
+ } elseif ( isset( $params->names_ids ) && $params->names_ids ) {
6525
+ $pre_key .= '_names_ids';
6526
+ }
6527
+
6528
+ if ( isset( $params->key_names ) && $params->key_names ) {
6529
+ $pre_key .= '_namekeys';
6530
+ }
6531
+
6532
+ if ( isset( $params->fields ) && ! $params->fields ) {
6533
+ $pre_key .= '_nofields';
6534
+ }
6535
+
6536
+ if ( isset( $params->table_info ) && $params->table_info ) {
6537
+ $pre_key .= '_tableinfo';
6538
+ }
6539
+
6540
+ $pre_key .= '_get';
6541
+
6542
+ if ( empty( $cache_key ) ) {
6543
+ $cache_key = 'pods' . $pre_key . '_all';
6544
+ } else {
6545
+ $cache_key = 'pods' . $pre_key . $cache_key;
6546
+ }
6547
+
6548
+ if ( ! $bypass_cache && pods_api_cache() && ! empty( $cache_key ) && ( 'pods' . ( ! empty( $current_language ) ? '_' . $current_language : '' ) . '_get_all' !== $cache_key || empty( $meta_query ) ) && $limit < 1 && ( empty( $orderby ) || 'menu_order title' === $orderby ) && empty( $ids ) ) {
6549
+ $the_pods = pods_transient_get( $cache_key );
6550
+
6551
+ if ( false === $the_pods ) {
6552
+ $the_pods = pods_cache_get( $cache_key, 'pods' );
6553
+ }
6554
+
6555
+ if ( ! is_array( $the_pods ) && 'none' === $the_pods ) {
6556
+ return array();
6557
+ } elseif ( false !== $the_pods ) {
6558
+ return $the_pods;
6559
+ }
6560
+ }
6561
+
6562
+ $the_pods = array();
6563
+
6564
+ $args = array(
6565
+ 'post_type' => '_pods_pod',
6566
+ 'nopaging' => true,
6567
+ 'posts_per_page' => $limit,
6568
+ 'order' => $order,
6569
+ 'orderby' => $orderby,
6570
+ 'meta_query' => $meta_query,
6571
+ );
6572
+
6573
+ // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
6574
+ if ( false !== $ids ) {
6575
+ $args['post__in'] = $ids;
6576
+ }
6577
+
6578
+ $_pods = get_posts( $args );
6579
+
6580
+ $export_ignore = array(
6581
+ 'object_type',
6582
+ 'object_name',
6583
+ 'table',
6584
+ 'meta_table',
6585
+ 'pod_table',
6586
+ 'field_id',
6587
+ 'field_index',
6588
+ 'field_slug',
6589
+ 'field_type',
6590
+ 'field_parent',
6591
+ 'field_parent_select',
6592
+ 'meta_field_id',
6593
+ 'meta_field_index',
6594
+ 'meta_field_value',
6595
+ 'pod_field_id',
6596
+ 'pod_field_index',
6597
+ 'object_fields',
6598
+ 'join',
6599
+ 'where',
6600
+ 'where_default',
6601
+ 'orderby',
6602
+ 'pod',
6603
+ 'recurse',
6604
+ 'table_info',
6605
+ 'attributes',
6606
+ 'group',
6607
+ 'grouped',
6608
+ 'developer_mode',
6609
+ 'dependency',
6610
+ 'depends-on',
6611
+ 'excludes-on'
6612
+ );
6613
+
6614
+ $total_fields = 0;
6615
+
6616
+ if ( isset( $params->count ) && $params->count ) {
6617
+ $the_pods = count( $_pods );
6618
+ } else {
6619
+ foreach ( $_pods as $pod ) {
6620
+ if ( isset( $params->names ) && $params->names ) {
6621
+ $the_pods[ $pod->post_name ] = $pod->post_title;
6622
+ } elseif ( isset( $params->names_ids ) && $params->names_ids ) {
6623
+ $the_pods[ $pod->ID ] = $pod->post_name;
6624
+ } else {
6625
+ if ( isset( $params->fields ) && ! $params->fields ) {
6626
+ $pod->fields = false;
6627
+ }
6628
+
6629
+ $pod = $this->load_pod( array(
6630
+ 'pod' => $pod,
6631
+ 'table_info' => ! empty( $params->table_info ),
6632
+ 'bypass_cache' => $bypass_cache
6633
+ ) );
6634
+
6635
+ // Remove extra data not needed
6636
+ if ( pods_var( 'export', $params, false ) && ( ! isset( $params->fields ) || $params->fields ) ) {
6637
+ foreach ( $export_ignore as $ignore ) {
6638
+ if ( isset( $pod[ $ignore ] ) ) {
6639
+ unset( $pod[ $ignore ] );
6640
+ }
6641
+ }
6642
+
6643
+ foreach ( $pod['fields'] as $field => $field_data ) {
6644
+ if ( isset( $pod['fields'][ $field ]['table_info'] ) ) {
6645
+ unset( $pod['fields'][ $field ]['table_info'] );
6646
+ }
6647
+ }
6648
+ }
6649
+
6650
+ $total_fields += count( $pod['fields'] );
6651
+
6652
+ if ( isset( $params->key_names ) && $params->key_names ) {
6653
+ $the_pods[ $pod['name'] ] = $pod;
6654
+ } else {
6655
+ $the_pods[ $pod['id'] ] = $pod;
6656
+ }
6657
+ }
6658
+ }
6659
+ }
6660
+
6661
+ if ( ( ! function_exists( 'pll_current_language' ) || ! empty( $params->refresh ) ) && ! empty( $cache_key ) && ( 'pods' !== $cache_key || empty( $meta_query ) ) && $limit < 1 && ( empty( $orderby ) || 'menu_order title' === $orderby ) && empty( $ids ) ) {
6662
+ $total_pods = (int) ( is_array( $the_pods ) ) ? count( $the_pods ) : $the_pods;
6663
+ // Too many Pods can cause issues with the DB when caching is not enabled
6664
+ if ( 15 < $total_pods || 75 < (int) $total_fields ) {
6665
+ pods_transient_clear( $cache_key );
6666
+
6667
+ if ( pods_api_cache() ) {
6668
+ if ( empty( $the_pods ) && ( ! isset( $params->count ) || ! $params->count ) ) {
6669
+ pods_cache_set( $cache_key, 'none', 'pods' );
6670
+ } else {
6671
+ pods_cache_set( $cache_key, $the_pods, 'pods' );
6672
+ }
6673
+ }
6674
+ } else {
6675
+ pods_cache_clear( $cache_key, 'pods' );
6676
+
6677
+ if ( pods_api_cache() ) {
6678
+ if ( empty( $the_pods ) && ( ! isset( $params->count ) || ! $params->count ) ) {
6679
+ pods_transient_set( $cache_key, 'none' );
6680
+ } else {
6681
+ pods_transient_set( $cache_key, $the_pods );
6682
+ }
6683
+ }
6684
+ }
6685
+ }
6686
+
6687
+ return $the_pods;
6688
+ }
6689
+
6690
+ /**
6691
+ * Check if a Pod's field exists
6692
+ *
6693
+ * $params['pod_id'] int The Pod ID
6694
+ * $params['id'] int The field ID
6695
+ * $params['name'] string The field name
6696
+ *
6697
+ * @param array $params An associative array of parameters
6698
+ *
6699
+ * @return bool
6700
+ *
6701
+ * @since 1.12
6702
+ */
6703
+ public function field_exists( $params ) {
6704
+
6705
+ $params = (object) pods_sanitize( $params );
6706
+
6707
+ if ( ( ! empty( $params->id ) || ! empty( $params->name ) ) && isset( $params->pod_id ) && ! empty( $params->pod_id ) ) {
6708
+ if ( ! isset( $params->name ) ) {
6709
+ $dummy = (int) $params->id;
6710
+ $field = get_post( $dummy );
6711
+ } else {
6712
+ $field = get_posts( array(
6713
+ 'name' => $params->name,
6714
+ 'post_type' => '_pods_field',
6715
+ 'posts_per_page' => 1,
6716
+ 'post_parent' => $params->pod_id
6717
+ ) );
6718
+ }
6719
+
6720
+ if ( ! empty( $field ) ) {
6721
+ return true;
6722
+ }
6723
+ }
6724
+
6725
+ return false;
6726
+ }
6727
+
6728
+ /**
6729
+ * Load a field
6730
+ *
6731
+ * $params['pod_id'] int The Pod ID
6732
+ * $params['pod'] string The Pod name
6733
+ * $params['id'] int The field ID
6734
+ * $params['name'] string The field name
6735
+ * $params['table_info'] boolean Whether to lookup a pick field's table info
6736
+ * $params['bypass_cache'] boolean Bypass the cache when getting data
6737
+ *
6738
+ * @param array $params An associative array of parameters
6739
+ * @param boolean $strict Whether to require a field exist or not when loading the info
6740
+ *
6741
+ * @return array|bool Array with field data, false if field not found
6742
+ * @since 1.7.9
6743
+ */
6744
+ public function load_field( $params, $strict = false ) {
6745
+
6746
+ $params = (object) $params;
6747
+
6748
+ if ( ! isset( $params->table_info ) ) {
6749
+ $params->table_info = false;
6750
+ }
6751
+
6752
+ $bypass_cache = false;
6753
+
6754
+ if ( ! empty( $params->bypass_cache ) ) {
6755
+ $bypass_cache = true;
6756
+ }
6757
+
6758
+ $pod = array();
6759
+ $field = array();
6760
+
6761
+ if ( isset( $params->post_title ) ) {
6762
+ $_field = $params;
6763
+ } elseif ( isset( $params->id ) && ! empty( $params->id ) ) {
6764
+ $dummy = (int) $params->id;
6765
+ $_field = get_post( $dummy );
6766
+ } else {
6767
+ if ( ! isset( $params->pod ) ) {
6768
+ $params->pod = '';
6769
+ }
6770
+
6771
+ if ( ! isset( $params->pod_id ) ) {
6772
+ $params->pod_id = 0;
6773
+ }
6774
+
6775
+ if ( isset( $params->pod_data ) ) {
6776
+ $pod = $params->pod_data;
6777
+ } else {
6778
+ $pod = $this->load_pod( array(
6779
+ 'name' => $params->pod,
6780
+ 'id' => $params->pod_id,
6781
+ 'table_info' => false,
6782
+ 'bypass_cache' => $bypass_cache
6783
+ ), false );
6784
+
6785
+ if ( false === $pod ) {
6786
+ if ( $strict ) {
6787
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
6788
+ }
6789
+
6790
+ return false;
6791
+ }
6792
+ }
6793
+
6794
+ $params->pod_id = $pod['id'];
6795
+ $params->pod = $pod['name'];
6796
+
6797
+ if ( empty( $params->name ) && empty( $params->pod ) && empty( $params->pod_id ) ) {
6798
+ return pods_error( __( 'Either Field Name or Field ID / Pod ID are required', 'pods' ), $this );
6799
+ }
6800
+
6801
+ $params->name = pods_clean_name( $params->name, true, ( 'meta' === $pod['storage'] ? false : true ) );
6802
+
6803
+ if ( isset( $pod['fields'][ $params->name ] ) && isset( $pod['fields'][ $params->name ]['id'] ) ) {
6804
+ return $pod['fields'][ $params->name ];
6805
+ }
6806
+
6807
+ $field = false;
6808
+
6809
+ if ( ! $bypass_cache && pods_api_cache() ) {
6810
+ $field = pods_transient_get( 'pods_field_' . $params->pod . '_' . $params->name );
6811
+ }
6812
+
6813
+ if ( empty( $field ) ) {
6814
+ $field = get_posts( array(
6815
+ 'name' => $params->name,
6816
+ 'post_type' => '_pods_field',
6817
+ 'posts_per_page' => 1,
6818
+ 'post_parent' => $params->pod_id
6819
+ ) );
6820
+
6821
+ if ( empty( $field ) || empty( $field[0] ) ) {
6822
+ if ( $strict ) {
6823
+ return pods_error( __( 'Field not found', 'pods' ), $this );
6824
+ }
6825
+
6826
+ return false;
6827
+ }
6828
+
6829
+ $_field = $field[0];
6830
+
6831
+ $field = array();
6832
+ }
6833
+ }
6834
+
6835
+ if ( empty( $_field ) ) {
6836
+ if ( $strict ) {
6837
+ return pods_error( __( 'Field not found', 'pods' ), $this );
6838
+ }
6839
+
6840
+ return false;
6841
+ }
6842
+
6843
+ $_field = get_object_vars( $_field );
6844
+
6845
+ if ( ! isset( $pod['name'] ) && ! isset( $_field['pod'] ) ) {
6846
+ if ( 0 < $_field['post_parent'] ) {
6847
+ $pod = $this->load_pod( array( 'id' => $_field['post_parent'], 'table_info' => false ), false );
6848
+ }
6849
+
6850
+ if ( empty( $pod ) ) {
6851
+ if ( $strict ) {
6852
+ return pods_error( __( 'Pod for field not found', 'pods' ), $this );
6853
+ }
6854
+
6855
+ return false;
6856
+ }
6857
+ }
6858
+
6859
+ if ( empty( $field ) ) {
6860
+ if ( ! $bypass_cache && pods_api_cache() && ( isset( $pod['name'] ) || isset( $_field['pod'] ) ) ) {
6861
+ $field = pods_transient_get( 'pods_field_' . pods_var( 'name', $pod, pods_var( 'pod', $_field ), null, true ) . '_' . $_field['post_name'] );
6862
+ }
6863
+
6864
+ if ( empty( $field ) ) {
6865
+ $defaults = array(
6866
+ 'type' => 'text'
6867
+ );
6868
+
6869
+ $field = array(
6870
+ 'id' => $_field['ID'],
6871
+ 'name' => $_field['post_name'],
6872
+ 'label' => $_field['post_title'],
6873
+ 'description' => $_field['post_content'],
6874
+ 'weight' => $_field['menu_order'],
6875
+ 'pod_id' => $_field['post_parent'],
6876
+ 'pick_object' => '',
6877
+ 'pick_val' => '',
6878
+ 'sister_id' => '',
6879
+ 'table_info' => array()
6880
+ );
6881
+
6882
+ if ( isset( $pod['name'] ) ) {
6883
+ $field['pod'] = $pod['name'];
6884
+ } elseif ( isset( $_field['pod'] ) ) {
6885
+ $field['pod'] = $_field['pod'];
6886
+ }
6887
+
6888
+ if ( $bypass_cache ) {
6889
+ wp_cache_delete( $field['id'], 'post_meta' );
6890
+
6891
+ update_postmeta_cache( array( $field['id'] ) );
6892
+ }
6893
+
6894
+ $field['options'] = get_post_meta( $field['id'] );
6895
+
6896
+ $options_ignore = array(
6897
+ 'method',
6898
+ 'table_info',
6899
+ 'attributes',
6900
+ 'group',
6901
+ 'grouped',
6902
+ 'developer_mode',
6903
+ 'dependency',
6904
+ 'depends-on',
6905
+ 'excludes-on'
6906
+ );
6907
+
6908
+ foreach ( $options_ignore as $ignore ) {
6909
+ if ( isset( $field['options'][ $ignore ] ) ) {
6910
+ unset( $field['options'][ $ignore ] );
6911
+ }
6912
+ }
6913
+
6914
+ foreach ( $field['options'] as $option => $value ) {
6915
+ if ( is_array( $value ) ) {
6916
+ foreach ( $value as $k => $v ) {
6917
+ if ( ! is_array( $v ) ) {
6918
+ $value[ $k ] = maybe_unserialize( $v );
6919
+ }
6920
+ }
6921
+
6922
+ if ( 1 == count( $value ) ) {
6923
+ $value = current( $value );
6924
+ }
6925
+ } else {
6926
+ $value = maybe_unserialize( $value );
6927
+ }
6928
+
6929
+ $field['options'][ $option ] = $value;
6930
+ }
6931
+
6932
+ $field['options'] = array_merge( $defaults, $field['options'] );
6933
+
6934
+ $field['type'] = $field['options']['type'];
6935
+
6936
+ unset( $field['options']['type'] );
6937
+
6938
+ if ( isset( $field['options']['pick_object'] ) ) {
6939
+ $field['pick_object'] = $field['options']['pick_object'];
6940
+
6941
+ unset( $field['options']['pick_object'] );
6942
+ }
6943
+
6944
+ if ( isset( $field['options']['pick_val'] ) ) {
6945
+ $field['pick_val'] = $field['options']['pick_val'];
6946
+
6947
+ unset( $field['options']['pick_val'] );
6948
+ }
6949
+
6950
+ if ( isset( $field['options']['sister_id'] ) ) {
6951
+ $field['sister_id'] = $field['options']['sister_id'];
6952
+
6953
+ unset( $field['options']['sister_id'] );
6954
+ }
6955
+
6956
+ if ( isset( $field['options']['sister_field_id'] ) ) {
6957
+ unset( $field['options']['sister_field_id'] );
6958
+ }
6959
+
6960
+ if ( pods_api_cache() && ( isset( $pod['name'] ) || isset( $_field['pod'] ) ) ) {
6961
+ pods_transient_set( 'pods_field_' . pods_var( 'name', $pod, pods_var( 'pod', $_field ), null, true ) . '_' . $field['name'], $field );
6962
+ }
6963
+ }
6964
+ }
6965
+
6966
+ $field['table_info'] = array();
6967
+
6968
+ if ( 'pick' === $field['type'] && $params->table_info ) {
6969
+ $field['table_info'] = $this->get_table_info( $field['pick_object'], $field['pick_val'], null, null, $field );
6970
+ }
6971
+
6972
+ return $field;
6973
+ }
6974
+
6975
+ /**
6976
+ * Load fields by Pod, ID, Name, and/or Type
6977
+ *
6978
+ * $params['pod_id'] int The Pod ID
6979
+ * $params['pod'] string The Pod name
6980
+ * $params['id'] array The field IDs
6981
+ * $params['name'] array The field names
6982
+ * $params['type'] array The field types
6983
+ * $params['options'] array Field Option(s) key=>value array to filter by
6984
+ * $params['where'] string WHERE clause of query
6985
+ * $params['object_fields'] bool Whether to include the object fields for WP objects, default true
6986
+ *
6987
+ * @param array $params An associative array of parameters
6988
+ * @param bool $strict Whether to require a field exist or not when loading the info
6989
+ *
6990
+ * @return array Array of field data.
6991
+ *
6992
+ * @since 1.7.9
6993
+ */
6994
+ public function load_fields( $params, $strict = false ) {
6995
+
6996
+ // @todo Get away from using md5/serialize, I'm sure we can cache specific parts
6997
+ $cache_key = md5( serialize( $params ) );
6998
+ if ( isset( $this->fields_cache[ $cache_key ] ) ) {
6999
+ return $this->fields_cache[ $cache_key ];
7000
+ }
7001
+
7002
+ $params = (object) pods_sanitize( $params );
7003
+
7004
+ if ( ! isset( $params->pod ) || empty( $params->pod ) ) {
7005
+ $params->pod = '';
7006
+ }
7007
+
7008
+ if ( ! isset( $params->pod_id ) || empty( $params->pod_id ) ) {
7009
+ $params->pod_id = 0;
7010
+ }
7011
+
7012
+ if ( ! isset( $params->name ) || empty( $params->name ) ) {
7013
+ $params->name = array();
7014
+ } else {
7015
+ $params->name = (array) $params->name;
7016
+ }
7017
+
7018
+ if ( ! isset( $params->id ) || empty( $params->id ) ) {
7019
+ $params->id = array();
7020
+ } else {
7021
+ $params->id = (array) $params->id;
7022
+
7023
+ foreach ( $params->id as &$id ) {
7024
+ $id = pods_absint( $id );
7025
+ }
7026
+ }
7027
+
7028
+ if ( ! isset( $params->type ) || empty( $params->type ) ) {
7029
+ $params->type = array();
7030
+ } else {
7031
+ $params->type = (array) $params->type;
7032
+ }
7033
+
7034
+ if ( ! isset( $params->object_fields ) ) {
7035
+ $params->object_fields = true;
7036
+ } else {
7037
+ $params->object_fields = (boolean) $params->object_fields;
7038
+ }
7039
+
7040
+ $fields = array();
7041
+
7042
+ if ( ! empty( $params->pod ) || ! empty( $params->pod_id ) ) {
7043
+ $pod = $this->load_pod( array(
7044
+ 'name' => $params->pod,
7045
+ 'id' => $params->pod_id,
7046
+ 'table_info' => true
7047
+ ), false );
7048
+
7049
+ if ( false === $pod ) {
7050
+ if ( $strict ) {
7051
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
7052
+ }
7053
+
7054
+ return $fields;
7055
+ }
7056
+
7057
+ if ( $params->object_fields && ! empty( $pod['object_fields'] ) ) {
7058
+ $pod['fields'] = array_merge( $pod['object_fields'], $pod['fields'] );
7059
+ }
7060
+
7061
+ foreach ( $pod['fields'] as $field ) {
7062
+ if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) ) {
7063
+ $fields[ $field['name'] ] = $field;
7064
+ } elseif ( in_array( $fields['name'], $params->name ) || in_array( $fields['id'], $params->id ) || in_array( $fields['type'], $params->type ) ) {
7065
+ $fields[ $field['name'] ] = $field;
7066
+ }
7067
+ }
7068
+ } elseif ( ( isset( $params->options ) && ! empty( $params->options ) && is_array( $params->options ) ) || ( isset( $params->where ) && ! empty( $params->where ) && is_array( $params->where ) ) ) {
7069
+ $order = 'ASC';
7070
+ $orderby = 'menu_order title';
7071
+ $limit = - 1;
7072
+ $ids = false;
7073
+
7074
+ $meta_query = array();
7075
+
7076
+ if ( isset( $params->options ) && ! empty( $params->options ) && is_array( $params->options ) ) {
7077
+ foreach ( $params->options as $option => $value ) {
7078
+ if ( ! is_array( $value ) ) {
7079
+ $value = array( $value );
7080
+ }
7081
+
7082
+ $value = pods_trim( $value );
7083
+
7084
+ sort( $value );
7085
+
7086
+ $meta_query[] = array(
7087
+ 'key' => $option,
7088
+ 'value' => pods_sanitize( $value ),
7089
+ 'compare' => 'IN'
7090
+ );
7091
+ }
7092
+ }
7093
+
7094
+ if ( isset( $params->where ) && ! empty( $params->where ) && is_array( $params->where ) ) {
7095
+ $meta_query = array_merge( $meta_query, (array) $params->where );
7096
+ }
7097
+
7098
+ $args = array(
7099
+ 'post_type' => '_pods_field',
7100
+ 'nopaging' => true,
7101
+ 'posts_per_page' => $limit,
7102
+ 'order' => $order,
7103
+ 'orderby' => $orderby,
7104
+ 'meta_query' => $meta_query,
7105
+ );
7106
+
7107
+ // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
7108
+ if ( false !== $ids ) {
7109
+ $args['post__in'] = $ids;
7110
+ }
7111
+
7112
+ $fields = array();
7113
+
7114
+ $_fields = get_posts( $args );
7115
+
7116
+ foreach ( $_fields as $field ) {
7117
+ $field = $this->load_field( $field, false );
7118
+
7119
+ if ( ! empty( $field ) ) {
7120
+ $fields[ $field['id'] ] = $field;
7121
+ }
7122
+ }
7123
+ } else {
7124
+ if ( empty( $params->name ) && empty( $params->id ) && empty( $params->type ) ) {
7125
+ return pods_error( __( 'Either Field Name / Field ID / Field Type, or Pod Name / Pod ID are required', 'pods' ), $this );
7126
+ }
7127
+
7128
+ $lookup = array();
7129
+
7130
+ if ( ! empty( $params->name ) ) {
7131
+ $fields = implode( "', '", $params->name );
7132
+
7133
+ $lookup[] = "`post_name` IN ( '{$fields}' )";
7134
+ }
7135
+
7136
+ if ( ! empty( $params->id ) ) {
7137
+ $fields = implode( ", ", $params->id );
7138
+
7139
+ $lookup[] = "`ID` IN ( {$fields} )";
7140
+ }
7141
+
7142
+ $lookup = implode( ' AND ', $lookup );
7143
+
7144
+ $result = pods_query( "SELECT `ID`, `post_name`, `post_parent` FROM `@wp_posts` WHERE `post_type` = '_pods_field' AND ( {$lookup} )" );
7145
+
7146
+ $fields = array();
7147
+
7148
+ if ( ! empty( $result ) ) {
7149
+ foreach ( $result as $field ) {
7150
+ $field = $this->load_field( array(
7151
+ 'id' => $field->ID,
7152
+ 'name' => $field->post_name,
7153
+ 'pod_id' => $field->post_parent
7154
+ ), false );
7155
+
7156
+ if ( ! empty( $field ) && ( empty( $params->type ) || in_array( $field['type'], $params->type ) ) ) {
7157
+ $fields[ $field['id'] ] = $field;
7158
+ }
7159
+ }
7160
+ }
7161
+ }
7162
+ if ( isset( $cache_key ) ) {
7163
+ $this->fields_cache[ $cache_key ] = $fields;
7164
+ }
7165
+
7166
+ return $fields;
7167
+ }
7168
+
7169
+ /**
7170
+ * Load a Pods Object
7171
+ *
7172
+ * $params['id'] int The Object ID
7173
+ * $params['name'] string The Object name
7174
+ * $params['type'] string The Object type
7175
+ *
7176
+ * @param array|object $params An associative array of parameters
7177
+ * @param bool $strict
7178
+ *
7179
+ * @return array|bool
7180
+ * @since 2.0.0
7181
+ */
7182
+ public function load_object( $params, $strict = false ) {
7183
+
7184
+ if ( is_object( $params ) && isset( $params->post_title ) ) {
7185
+ $_object = get_object_vars( $params );
7186
+ } else {
7187
+ $params = (object) pods_sanitize( $params );
7188
+
7189
+ if ( ! isset( $params->type ) || empty( $params->type ) ) {
7190
+ return pods_error( __( 'Object type is required', 'pods' ), $this );
7191
+ }
7192
+
7193
+ if ( ( ! isset( $params->id ) || empty( $params->id ) ) && ( ! isset( $params->name ) || empty( $params->name ) ) ) {
7194
+ return pods_error( __( 'Either Object ID or Name are required', 'pods' ), $this );
7195
+ }
7196
+
7197
+ /**
7198
+ * @var $wpdb wpdb
7199
+ */
7200
+ global $wpdb;
7201
+
7202
+ if ( isset( $params->name ) ) {
7203
+ $_object = pods_by_title( $params->name, ARRAY_A, '_pods_' . $params->type, 'publish' );
7204
+ } else {
7205
+ $object = $params->id;
7206
+
7207
+ $_object = get_post( $object, ARRAY_A );
7208
+ }
7209
+
7210
+ if ( empty( $_object ) ) {
7211
+ if ( $strict ) {
7212
+ return pods_error( __( 'Object not found', 'pods' ), $this );
7213
+ }
7214
+
7215
+ return false;
7216
+ }
7217
+ }
7218
+
7219
+ $object = array(
7220
+ 'id' => $_object['ID'],
7221
+ 'name' => $_object['post_title'],
7222
+ 'code' => $_object['post_content'],
7223
+ 'type' => str_replace( '_pods_', '', $_object['post_type'] ),
7224
+ 'slug' => $_object['post_name']
7225
+ );
7226
+
7227
+ $object['options'] = get_post_meta( $object['id'] );
7228
+
7229
+ foreach ( $object['options'] as $option => &$value ) {
7230
+ if ( is_array( $value ) && 1 == count( $value ) ) {
7231
+ $value = current( $value );
7232
+ }
7233
+ }
7234
+
7235
+ return $object;
7236
+ }
7237
+
7238
+ /**
7239
+ * Load Multiple Pods Objects
7240
+ *
7241
+ * $params['type'] string The Object type
7242
+ * $params['options'] array Pod Option(s) key=>value array to filter by
7243
+ * $params['orderby'] string ORDER BY clause of query
7244
+ * $params['limit'] string Number of objects to return
7245
+ * $params['where'] string WHERE clause of query
7246
+ * $params['ids'] string|array IDs of Objects
7247
+ *
7248
+ * @param array|object $params An associative array of parameters
7249
+ *
7250
+ * @return array
7251
+ * @since 2.0.0
7252
+ */
7253
+ public function load_objects( $params ) {
7254
+
7255
+ $params = (object) pods_sanitize( $params );
7256
+
7257
+ if ( ! isset( $params->type ) || empty( $params->type ) ) {
7258
+ return pods_error( __( 'Pods Object type is required', 'pods' ), $this );
7259
+ }
7260
+
7261
+ $order = 'ASC';
7262
+ $orderby = 'menu_order';
7263
+ $limit = - 1;
7264
+ $ids = false;
7265
+
7266
+ $meta_query = array();
7267
+ $cache_key = '';
7268
+
7269
+ if ( isset( $params->options ) && ! empty( $params->options ) && is_array( $params->options ) ) {
7270
+ foreach ( $params->options as $option => $value ) {
7271
+ if ( ! is_array( $value ) ) {
7272
+ $value = array( $value );
7273
+ }
7274
+
7275
+ $value = pods_trim( $value );
7276
+
7277
+ sort( $value );
7278
+
7279
+ $meta_query[] = array(
7280
+ 'key' => $option,
7281
+ 'value' => pods_sanitize( $value ),
7282
+ 'compare' => 'IN'
7283
+ );
7284
+ }
7285
+ }
7286
+
7287
+ if ( isset( $params->where ) && is_array( $params->where ) ) {
7288
+ $meta_query = array_merge( $meta_query, (array) $params->where );
7289
+ }
7290
+
7291
+ if ( isset( $params->order ) && ! empty( $params->order ) && in_array( strtoupper( $params->order ), array(
7292
+ 'ASC',
7293
+ 'DESC'
7294
+ ) ) ) {
7295
+ $order = strtoupper( $params->order );
7296
+ }
7297
+
7298
+ if ( isset( $params->orderby ) && ! empty( $params->orderby ) ) {
7299
+ $orderby = strtoupper( $params->orderby );
7300
+ }
7301
+
7302
+ if ( isset( $params->limit ) && ! empty( $params->limit ) ) {
7303
+ $limit = pods_absint( $params->limit );
7304
+ }
7305
+
7306
+ if ( isset( $params->ids ) && ! empty( $params->ids ) ) {
7307
+ $ids = $params->ids;
7308
+
7309
+ if ( ! is_array( $ids ) ) {
7310
+ $ids = explode( ',', $ids );
7311
+ }
7312
+ }
7313
+
7314
+ if ( empty( $ids ) ) {
7315
+ $ids = false;
7316
+ }
7317
+
7318
+ if ( pods_api_cache() && empty( $meta_query ) && empty( $limit ) && ( empty( $orderby ) || 'menu_order' === $orderby ) && empty( $ids ) ) {
7319
+ $cache_key = 'pods_objects_' . $params->type;
7320
+
7321
+ $the_objects = pods_transient_get( $cache_key );
7322
+
7323
+ if ( false !== $the_objects ) {
7324
+ return $the_objects;
7325
+ }
7326
+ }
7327
+
7328
+ $the_objects = array();
7329
+
7330
+ $args = array(
7331
+ 'post_type' => '_pods_' . $params->type,
7332
+ 'nopaging' => true,
7333
+ 'posts_per_page' => $limit,
7334
+ 'order' => $order,
7335
+ 'orderby' => $orderby,
7336
+ 'meta_query' => $meta_query,
7337
+ );
7338
+
7339
+ // Only set post__in if there are ids to filter (see https://core.trac.wordpress.org/ticket/28099)
7340
+ if ( false !== $ids ) {
7341
+ $args['post__in'] = $ids;
7342
+ }
7343
+
7344
+ $objects = get_posts( $args );
7345
+
7346
+ foreach ( $objects as $object ) {
7347
+ $object = $this->load_object( $object );
7348
+
7349
+ $the_objects[ $object['name'] ] = $object;
7350
+ }
7351
+
7352
+ if ( pods_api_cache() && ! empty( $cache_key ) ) {
7353
+ pods_transient_set( $cache_key, $the_objects );
7354
+ }
7355
+
7356
+ return $the_objects;
7357
+ }
7358
+
7359
+ /**
7360
+ * @see PodsAPI::load_object
7361
+ *
7362
+ * Load a Pod Template
7363
+ *
7364
+ * $params['id'] int The template ID
7365
+ * $params['name'] string The template name
7366
+ *
7367
+ * @param array $params An associative array of parameters
7368
+ *
7369
+ * @return array|bool
7370
+ * @since 1.7.9
7371
+ */
7372
+ public function load_template( $params ) {
7373
+
7374
+ if ( ! class_exists( 'Pods_Templates' ) ) {
7375
+ return false;
7376
+ }
7377
+
7378
+ $params = (object) $params;
7379
+ $params->type = 'template';
7380
+
7381
+ return $this->load_object( $params );
7382
+ }
7383
+
7384
+ /**
7385
+ * @see PodsAPI::load_objects
7386
+ *
7387
+ * Load Multiple Pod Templates
7388
+ *
7389
+ * $params['where'] string The WHERE clause of query
7390
+ * $params['options'] array Pod Option(s) key=>value array to filter by
7391
+ * $params['orderby'] string ORDER BY clause of query
7392
+ * $params['limit'] string Number of templates to return
7393
+ *
7394
+ * @param array $params (optional) An associative array of parameters
7395
+ *
7396
+ * @return array
7397
+ *
7398
+ * @since 2.0.0
7399
+ */
7400
+ public function load_templates( $params = null ) {
7401
+
7402
+ if ( ! class_exists( 'Pods_Templates' ) ) {
7403
+ return array();
7404
+ }
7405
+
7406
+ $params = (object) $params;
7407
+ $params->type = 'template';
7408
+
7409
+ return $this->load_objects( $params );
7410
+ }
7411
+
7412
+ /**
7413
+ * @see PodsAPI::load_object
7414
+ *
7415
+ * Load a Pod Page
7416
+ *
7417
+ * $params['id'] int The page ID
7418
+ * $params['name'] string The page URI
7419
+ *
7420
+ * @param array $params An associative array of parameters
7421
+ *
7422
+ * @return array|bool
7423
+ *
7424
+ * @since 1.7.9
7425
+ */
7426
+ public function load_page( $params ) {
7427
+
7428
+ if ( ! class_exists( 'Pods_Pages' ) ) {
7429
+ return false;
7430
+ }
7431
+
7432
+ $params = (object) $params;
7433
+ if ( ! isset( $params->name ) && isset( $params->uri ) ) {
7434
+ $params->name = $params->uri;
7435
+ unset( $params->uri );
7436
+ }
7437
+ $params->type = 'page';
7438
+
7439
+ return $this->load_object( $params );
7440
+ }
7441
+
7442
+ /**
7443
+ * @see PodsAPI::load_objects
7444
+ *
7445
+ * Load Multiple Pod Pages
7446
+ *
7447
+ * $params['where'] string The WHERE clause of query
7448
+ * $params['options'] array Pod Option(s) key=>value array to filter by
7449
+ * $params['orderby'] string ORDER BY clause of query
7450
+ * $params['limit'] string Number of pages to return
7451
+ *
7452
+ * @param array $params (optional) An associative array of parameters
7453
+ *
7454
+ * @return array
7455
+ *
7456
+ * @since 2.0.0
7457
+ */
7458
+ public function load_pages( $params = null ) {
7459
+
7460
+ if ( ! class_exists( 'Pods_Pages' ) ) {
7461
+ return array();
7462
+ }
7463
+
7464
+ $params = (object) $params;
7465
+ $params->type = 'page';
7466
+
7467
+ return $this->load_objects( $params );
7468
+ }
7469
+
7470
+ /**
7471
+ * @see PodsAPI::load_object
7472
+ *
7473
+ * Load a Pod Helper
7474
+ *
7475
+ * $params['id'] int The helper ID
7476
+ * $params['name'] string The helper name
7477
+ *
7478
+ * @param array $params An associative array of parameters
7479
+ *
7480
+ * @return array|bool
7481
+ *
7482
+ * @since 1.7.9
7483
+ */
7484
+ public function load_helper( $params ) {
7485
+
7486
+ if ( ! class_exists( 'Pods_Helpers' ) ) {
7487
+ return false;
7488
+ }
7489
+
7490
+ $params = (object) $params;
7491
+ $params->type = 'helper';
7492
+
7493
+ return $this->load_object( $params );
7494
+ }
7495
+
7496
+ /**
7497
+ * @see PodsAPI::load_objects
7498
+ *
7499
+ * Load Multiple Pod Helpers
7500
+ *
7501
+ * $params['where'] string The WHERE clause of query
7502
+ * $params['options'] array Pod Option(s) key=>value array to filter by
7503
+ * $params['orderby'] string ORDER BY clause of query
7504
+ * $params['limit'] string Number of pages to return
7505
+ *
7506
+ * @param array $params (optional) An associative array of parameters
7507
+ *
7508
+ * @return array
7509
+ *
7510
+ * @since 2.0.0
7511
+ */
7512
+ public function load_helpers( $params = null ) {
7513
+
7514
+ if ( ! class_exists( 'Pods_Helpers' ) ) {
7515
+ return array();
7516
+ }
7517
+
7518
+ $params = (object) $params;
7519
+ $params->type = 'helper';
7520
+
7521
+ return $this->load_objects( $params );
7522
+ }
7523
+
7524
+ /**
7525
+ * Load the pod item object
7526
+ *
7527
+ * $params['pod'] string The datatype name
7528
+ * $params['id'] int (optional) The item's ID
7529
+ *
7530
+ * @param array $params An associative array of parameters
7531
+ *
7532
+ * @return bool|\Pods
7533
+ *
7534
+ * @uses pods()
7535
+ *
7536
+ * @since 2.0.0
7537
+ */
7538
+ public function load_pod_item( $params ) {
7539
+
7540
+ $params = (object) pods_sanitize( $params );
7541
+
7542
+ if ( ! isset( $params->pod ) || empty( $params->pod ) ) {
7543
+ return pods_error( __( 'Pod name required', 'pods' ), $this );
7544
+ }
7545
+ if ( ! isset( $params->id ) || empty( $params->id ) ) {
7546
+ return pods_error( __( 'Item ID required', 'pods' ), $this );
7547
+ }
7548
+
7549
+ $pod = false;
7550
+
7551
+ if ( pods_api_cache() ) {
7552
+ $pod = pods_cache_get( $params->id, 'pods_item_object_' . $params->pod );
7553
+ }
7554
+
7555
+ if ( false !== $pod ) {
7556
+ return $pod;
7557
+ }
7558
+
7559
+ $pod = pods( $params->pod, $params->id );
7560
+
7561
+ if ( pods_api_cache() ) {
7562
+ pods_cache_set( $params->id, $pod, 'pods_item_object_' . $params->pod );
7563
+ }
7564
+
7565
+ return $pod;
7566
+ }
7567
+
7568
+ /**
7569
+ * Load potential sister fields for a specific field
7570
+ *
7571
+ * $params['pod'] int The Pod name
7572
+ * $params['related_pod'] string The related Pod name
7573
+ *
7574
+ * @param array $params An associative array of parameters
7575
+ * @param array $pod (optional) Array of Pod data to use (to avoid lookup)
7576
+ *
7577
+ * @return array|bool
7578
+ *
7579
+ * @since 1.7.9
7580
+ *
7581
+ * @uses PodsAPI::load_pod
7582
+ */
7583
+ public function load_sister_fields( $params, $pod = null ) {
7584
+
7585
+ $params = (object) pods_sanitize( $params );
7586
+
7587
+ if ( empty( $pod ) ) {
7588
+ $pod = $this->load_pod( array( 'name' => $params->pod, 'table_info' => false ), false );
7589
+
7590
+ if ( false === $pod ) {
7591
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
7592
+ }
7593
+ }
7594
+
7595
+ $params->pod_id = $pod['id'];
7596
+ $params->pod = $pod['name'];
7597
+
7598
+ $type = false;
7599
+
7600
+ if ( 0 === strpos( $params->related_pod, 'pod-' ) ) {
7601
+ $params->related_pod = pods_str_replace( 'pod-', '', $params->related_pod, 1 );
7602
+ $type = 'pod';
7603
+ } elseif ( 0 === strpos( $params->related_pod, 'post_type-' ) ) {
7604
+ $params->related_pod = pods_str_replace( 'post_type-', '', $params->related_pod, 1 );
7605
+ $type = 'post_type';
7606
+ } elseif ( 0 === strpos( $params->related_pod, 'taxonomy-' ) ) {
7607
+ $params->related_pod = pods_str_replace( 'taxonomy-', '', $params->related_pod, 1 );
7608
+ $type = 'taxonomy';
7609
+ } elseif ( 'comment' === $params->related_pod ) {
7610
+ $type = $params->related_pod;
7611
+ }
7612
+
7613
+ $related_pod = $this->load_pod( array( 'name' => $params->related_pod, 'table_info' => false ), false );
7614
+
7615
+ if ( false === $related_pod || ( false !== $type && 'pod' !== $type && $type !== $related_pod['type'] ) ) {
7616
+ return pods_error( __( 'Related Pod not found', 'pods' ), $this );
7617
+ }
7618
+
7619
+ $params->related_pod_id = $related_pod['id'];
7620
+ $params->related_pod = $related_pod['name'];
7621
+
7622
+ $sister_fields = array();
7623
+
7624
+ foreach ( $related_pod['fields'] as $field ) {
7625
+ if ( 'pick' === $field['type'] && in_array( $field['pick_object'], array(
7626
+ $pod['type'],
7627
+ 'pod'
7628
+ ) ) && ( $params->pod == $field['pick_object'] || $params->pod == $field['pick_val'] ) ) {
7629
+ $sister_fields[ $field['id'] ] = esc_html( $field['label'] . ' (' . $field['name'] . ')' );
7630
+ }
7631
+ }
7632
+
7633
+ return $sister_fields;
7634
+ }
7635
+
7636
+ /**
7637
+ * Takes a sql field such as tinyint and returns the pods field type, such as num.
7638
+ *
7639
+ * @param string $sql_field The SQL field to look for
7640
+ *
7641
+ * @return string The field type
7642
+ *
7643
+ * @since 2.0.0
7644
+ */
7645
+ public static function detect_pod_field_from_sql_data_type( $sql_field ) {
7646
+
7647
+ $sql_field = strtolower( $sql_field );
7648
+
7649
+ $field_to_field_map = array(
7650
+ 'tinyint' => 'number',
7651
+ 'smallint' => 'number',
7652
+ 'mediumint' => 'number',
7653
+ 'int' => 'number',
7654
+ 'bigint' => 'number',
7655
+ 'float' => 'number',
7656
+ 'double' => 'number',
7657
+ 'decimal' => 'number',
7658
+ 'date' => 'date',
7659
+ 'datetime' => 'datetime',
7660
+ 'timestamp' => 'datetime',
7661
+ 'time' => 'time',
7662
+ 'year' => 'date',
7663
+ 'varchar' => 'text',
7664
+ 'text' => 'paragraph',
7665
+ 'mediumtext' => 'paragraph',
7666
+ 'longtext' => 'paragraph'
7667
+ );
7668
+
7669
+ return ( array_key_exists( $sql_field, $field_to_field_map ) ) ? $field_to_field_map[ $sql_field ] : 'paragraph';
7670
+ }
7671
+
7672
+ /**
7673
+ * Gets all field types
7674
+ *
7675
+ * @return array Array of field types
7676
+ *
7677
+ * @uses PodsForm::field_loader
7678
+ *
7679
+ * @since 2.0.0
7680
+ * @deprecated 2.3.0
7681
+ */
7682
+ public function get_field_types() {
7683
+
7684
+ return PodsForm::field_types();
7685
+ }
7686
+
7687
+ /**
7688
+ * Gets the schema definition of a field.
7689
+ *
7690
+ * @param string $type Field type to look for
7691
+ * @param array $options (optional) Options of the field to pass to the schema function.
7692
+ *
7693
+ * @return array|bool|mixed|null
7694
+ *
7695
+ * @since 2.0.0
7696
+ */
7697
+ private function get_field_definition( $type, $options = null ) {
7698
 
7699
+ $definition = PodsForm::field_method( $type, 'schema', $options );
 
 
 
 
7700
 
7701
+ return $this->do_hook( 'field_definition', $definition, $type, $options );
7702
+ }
7703
 
7704
+ /**
7705
+ * @see PodsForm:validate
7706
+ *
7707
+ * Validates the value of a field.
7708
+ *
7709
+ * @param mixed $value The value to validate
7710
+ * @param string $field Field to use for validation
7711
+ * @param array $object_fields Fields of the object we're validating
7712
+ * @param array $fields Array of all fields data
7713
+ * @param array|Pods $pod Array of pod data (or Pods object)
7714
+ * @param array|object $params Extra parameters to pass to the validation function of the field.
7715
+ *
7716
+ * @return array|bool
7717
+ *
7718
+ * @uses PodsForm::validate
7719
+ *
7720
+ * @since 2.0.0
7721
+ */
7722
+ public function handle_field_validation( &$value, $field, $object_fields, $fields, $pod, $params ) {
7723
 
7724
+ $tableless_field_types = PodsForm::tableless_field_types();
 
 
 
 
 
 
 
 
7725
 
7726
+ $fields = array_merge( $fields, $object_fields );
7727
 
7728
+ $options = $fields[ $field ];
7729
+
7730
+ $id = ( is_object( $params ) ? $params->id : ( is_object( $pod ) ? $pod->id() : 0 ) );
7731
+
7732
+ if ( is_object( $pod ) ) {
7733
+ $pod = $pod->pod_data;
7734
+ }
7735
+
7736
+ $type = $options['type'];
7737
+ $label = $options['label'];
7738
+ $label = empty( $label ) ? $field : $label;
7739
+
7740
+ // Verify required fields
7741
+ if ( 1 == pods_var( 'required', $options['options'], 0 ) && 'slug' !== $type ) {
7742
+ if ( '' === $value || null === $value || array() === $value ) {
7743
+ return pods_error( sprintf( __( '%s is empty', 'pods' ), $label ), $this );
7744
  }
7745
 
7746
+ if ( 'multi' === pods_var( 'pick_format_type', $options['options'] ) && 'autocomplete' !== pods_var( 'pick_format_multi', $options['options'] ) ) {
7747
+ $has_value = false;
7748
+
7749
+ $check_value = (array) $value;
7750
+
7751
+ foreach ( $check_value as $val ) {
7752
+ if ( '' !== $val && null !== $val && 0 !== $val && '0' !== $val ) {
7753
+ $has_value = true;
7754
+
7755
+ continue;
7756
+ }
7757
  }
7758
+
7759
+ if ( ! $has_value ) {
7760
+ return pods_error( sprintf( __( '%s is required', 'pods' ), $label ), $this );
7761
  }
7762
+ }
7763
 
7764
+ }
7765
 
7766
+ // @todo move this to after pre-save preparations
7767
+ // Verify unique fields
7768
+ if ( 1 == pods_var( 'unique', $options['options'], 0 ) && '' !== $value && null !== $value && array() !== $value ) {
7769
+ if ( empty( $pod ) ) {
7770
+ return false;
7771
  }
 
7772
 
7773
+ if ( ! in_array( $type, $tableless_field_types ) ) {
7774
+ $exclude = '';
 
7775
 
7776
+ if ( ! empty( $id ) ) {
7777
+ $exclude = "AND `id` != {$id}";
7778
+ }
7779
 
7780
+ $check = false;
 
 
 
 
 
 
 
 
 
 
7781
 
7782
+ $check_value = pods_sanitize( $value );
 
 
7783
 
7784
+ // @todo handle meta-based fields
7785
+ // Trigger an error if not unique
7786
+ if ( 'table' === $pod['storage'] ) {
7787
+ $check = pods_query( "SELECT `id` FROM `@wp_pods_" . $pod['name'] . "` WHERE `{$field}` = '{$check_value}' {$exclude} LIMIT 1", $this );
7788
+ }
7789
+
7790
+ if ( ! empty( $check ) ) {
7791
+ return pods_error( sprintf( __( '%s needs to be unique', 'pods' ), $label ), $this );
7792
+ }
7793
+ } else {
7794
+ // @todo handle tableless check
7795
+ }
7796
+ }
7797
 
7798
+ $validate = PodsForm::validate( $options['type'], $value, $field, array_merge( $options, pods_var( 'options', $options, array() ) ), $fields, $pod, $id, $params );
7799
 
7800
+ $validate = $this->do_hook( 'field_validation', $validate, $value, $field, $object_fields, $fields, $pod, $params );
 
7801
 
7802
+ return $validate;
7803
+ }
7804
 
7805
+ /**
7806
+ * Find items related to a parent field
7807
+ *
7808
+ * @param int $field_id The Field ID
7809
+ * @param int $pod_id The Pod ID
7810
+ * @param mixed $ids A comma-separated string (or array) of item IDs
7811
+ * @param array $field Field data array
7812
+ * @param array $pod Pod data array
7813
+ *
7814
+ * @return int[]
7815
+ *
7816
+ * @since 2.0.0
7817
+ *
7818
+ * @uses pods_query()
7819
+ */
7820
+ public function lookup_related_items( $field_id, $pod_id, $ids, $field = null, $pod = null ) {
7821
 
7822
+ $related_ids = array();
7823
 
7824
+ if ( ! is_array( $ids ) ) {
7825
+ $ids = explode( ',', $ids );
7826
+ }
 
 
7827
 
7828
+ $ids = array_map( 'absint', $ids );
 
7829
 
7830
+ $ids = array_unique( array_filter( $ids ) );
7831
 
7832
+ $idstring = implode( ',', $ids );
 
 
7833
 
7834
+ if ( 0 != $pod_id && 0 != $field_id && isset( self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ] ) ) {
7835
+ // Check cache first, no point in running the same query multiple times
7836
+ return self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ];
7837
+ }
7838
 
7839
+ $tableless_field_types = PodsForm::tableless_field_types();
 
 
 
 
7840
 
7841
+ $field_type = pods_v( 'type', $field );
 
7842
 
7843
+ if ( empty( $ids ) || ! in_array( $field_type, $tableless_field_types ) ) {
7844
+ return array();
7845
+ }
7846
 
7847
+ $related_pick_limit = 0;
 
 
 
 
 
7848
 
7849
+ if ( empty( $field ) ) {
7850
+ $field = $this->load_field( array( 'id' => $field_id ) );
7851
  }
7852
 
7853
+ if ( ! empty( $field ) ) {
7854
+ $options = (array) pods_var_raw( 'options', $field, $field, null, true );
7855
 
7856
+ $related_pick_limit = (int) pods_v( $field_type . '_limit', $options, 0 );
7857
 
7858
+ if ( 'single' === pods_var_raw( $field_type . '_format_type', $options ) ) {
7859
+ $related_pick_limit = 1;
7860
+ }
7861
 
7862
+ // Temporary hack until there's some better handling here
7863
+ $related_pick_limit = $related_pick_limit * count( $ids );
7864
+ }
7865
 
7866
+ if ( 'taxonomy' === $field_type ) {
7867
+ $related = wp_get_object_terms( $ids, pods_v( 'name', $field ), array( 'fields' => 'ids' ) );
7868
 
7869
+ if ( ! is_wp_error( $related ) ) {
7870
+ $related_ids = $related;
7871
+ }
7872
+ } elseif ( 'comment' === $field_type ) {
7873
+ $comment_args = array(
7874
+ 'post__in' => $ids,
7875
+ 'fields' => 'ids',
7876
+ );
7877
 
7878
+ $related = get_comments( $comment_args );
7879
 
7880
+ if ( ! is_wp_error( $related ) ) {
7881
+ $related_ids = $related;
7882
+ }
7883
+ } elseif ( ! pods_tableless() ) {
7884
+ $ids = implode( ', ', $ids );
7885
+
7886
+ $field_id = (int) $field_id;
7887
+ $sister_id = (int) pods_var_raw( 'sister_id', $field, 0 );
7888
+
7889
+ $related_where = "
7890
+ `field_id` = {$field_id}
7891
+ AND `item_id` IN ( {$ids} )
7892
+ ";
7893
+
7894
+ $sql = "
7895
+ SELECT item_id, related_item_id, related_field_id
7896
+ FROM `@wp_podsrel`
7897
+ WHERE
7898
+ {$related_where}
7899
+ ORDER BY `weight`
7900
+ ";
7901
+
7902
+ $relationships = pods_query( $sql );
7903
+
7904
+ if ( ! empty( $relationships ) ) {
7905
+ foreach ( $relationships as $relation ) {
7906
+ if ( ! in_array( $relation->related_item_id, $related_ids ) ) {
7907
+ $related_ids[] = (int) $relation->related_item_id;
7908
+ } elseif ( 0 < $sister_id && $field_id == $relation->related_field_id && ! in_array( $relation->item_id, $related_ids ) ) {
7909
+ $related_ids[] = (int) $relation->item_id;
7910
+ }
7911
+ }
7912
+ }
7913
+ } else {
7914
+ if ( ! is_array( $pod ) ) {
7915
+ $pod = $this->load_pod( array( 'id' => $pod_id, 'table_info' => false ), false );
7916
+ }
7917
 
7918
+ if ( ! empty( $pod ) && in_array( $pod['type'], array(
7919
+ 'post_type',
7920
+ 'media',
7921
+ 'taxonomy',
7922
+ 'user',
7923
+ 'comment',
7924
+ 'settings'
7925
+ ) ) ) {
7926
+ $meta_type = $pod['type'];
7927
+
7928
+ if ( in_array( $meta_type, array( 'post_type', 'media' ) ) ) {
7929
+ $meta_type = 'post';
7930
+ } elseif ( 'taxonomy' === $meta_type ) {
7931
+ $meta_type = 'term';
7932
+ }
7933
 
7934
+ $no_conflict = pods_no_conflict_check( ( 'term' === $meta_type ? 'taxonomy' : $meta_type ) );
 
 
 
 
 
7935
 
7936
+ if ( ! $no_conflict ) {
7937
+ pods_no_conflict_on( ( 'term' === $meta_type ? 'taxonomy' : $meta_type ) );
7938
+ }
 
7939
 
7940
+ foreach ( $ids as $id ) {
7941
+ if ( 'settings' === $meta_type ) {
7942
+ $related_id = get_option( '_pods_' . $pod['name'] . '_' . $field['name'] );
7943
 
7944
+ if ( empty( $related_id ) ) {
7945
+ $related_id = get_option( $pod['name'] . '_' . $field['name'] );
7946
+ }
7947
 
7948
+ if ( is_array( $related_id ) && ! empty( $related_id ) ) {
7949
+ foreach ( $related_id as $related ) {
7950
+ if ( is_array( $related ) && ! empty( $related ) ) {
7951
+ if ( isset( $related['id'] ) ) {
7952
+ $related_ids[] = (int) $related['id'];
7953
+ } else {
7954
+ foreach ( $related as $r ) {
7955
+ $related_ids[] = (int) $r;
7956
+ }
7957
+ }
7958
+ } else {
7959
+ $related_ids[] = (int) $related;
7960
+ }
7961
+ }
7962
+ }
7963
+ } else {
7964
+ $related_id = get_metadata( $meta_type, $id, '_pods_' . $field['name'], true );
7965
 
7966
+ if ( empty( $related_id ) ) {
7967
+ $related_id = get_metadata( $meta_type, $id, $field['name'] );
7968
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7969
 
7970
+ if ( is_array( $related_id ) && ! empty( $related_id ) ) {
7971
+ foreach ( $related_id as $related ) {
7972
+ if ( is_array( $related ) && ! empty( $related ) ) {
7973
+ if ( isset( $related['id'] ) ) {
7974
+ $related_ids[] = (int) $related['id'];
7975
+ } else {
7976
+ foreach ( $related as $r ) {
7977
+ if ( isset( $related['id'] ) ) {
7978
+ $related_ids[] = (int) $r['id'];
7979
+ } else {
7980
+ $related_ids[] = (int) $r;
7981
+ }
7982
+ }
7983
+ }
7984
+ } else {
7985
+ $related_ids[] = (int) $related;
7986
+ }
7987
+ }
7988
+ }
7989
+ }
7990
  }
7991
 
7992
+ if ( ! $no_conflict ) {
7993
+ pods_no_conflict_off( ( 'term' === $meta_type ? 'taxonomy' : $meta_type ) );
7994
+ }
7995
  }
7996
+ }
7997
 
7998
+ if ( is_array( $related_ids ) ) {
7999
+ $related_ids = array_unique( array_filter( $related_ids ) );
8000
 
8001
+ if ( 0 < $related_pick_limit && ! empty( $related_ids ) ) {
8002
+ $related_ids = array_slice( $related_ids, 0, $related_pick_limit );
8003
+ }
8004
+ }
8005
+ if ( 0 != $pod_id && 0 != $field_id && ! empty( $related_ids ) ) {
8006
+ // Only cache if $pod_id and $field_id were passed
8007
+ self::$related_item_cache[ $pod_id ][ $field_id ][ $idstring ] = $related_ids;
8008
+ }
 
 
 
8009
 
8010
+ return $related_ids;
8011
+ }
8012
 
8013
+ /**
8014
+ * Find related items related to an item
8015
+ *
8016
+ * @param int $field_id The Field ID
8017
+ * @param int $pod_id The Pod ID
8018
+ * @param int $id Item ID to get related IDs from
8019
+ * @param array $field Field data array
8020
+ * @param array $pod Pod data array
8021
+ *
8022
+ * @return array|bool
8023
+ *
8024
+ * @since 2.3.0
8025
+ *
8026
+ * @uses pods_query()
8027
+ */
8028
+ public function lookup_related_items_from( $field_id, $pod_id, $id, $field = null, $pod = null ) {
8029
 
8030
+ $related_ids = false;
 
 
8031
 
8032
+ $id = (int) $id;
 
 
 
 
8033
 
8034
+ $tableless_field_types = PodsForm::tableless_field_types();
 
8035
 
8036
+ if ( empty( $id ) || ! in_array( pods_v( 'type', $field ), $tableless_field_types ) ) {
8037
+ return false;
8038
+ }
8039
 
8040
+ $related_pick_limit = 0;
8041
 
8042
+ if ( ! empty( $field ) ) {
8043
+ $options = (array) pods_var_raw( 'options', $field, $field, null, true );
 
 
 
8044
 
8045
+ $related_pick_limit = (int) pods_v( 'pick_limit', $options, 0 );
8046
 
8047
+ if ( 'single' === pods_var_raw( 'pick_format_type', $options ) ) {
8048
+ $related_pick_limit = 1;
 
 
8049
  }
8050
  }
8051
 
8052
+ if ( ! pods_tableless() ) {
8053
+ $field_id = (int) $field_id;
8054
+ $sister_id = (int) pods_var_raw( 'sister_id', $field, 0 );
8055
+
8056
+ $related_where = "
8057
+ `field_id` = {$field_id}
8058
+ AND `related_item_id` = {$id}
8059
+ ";
8060
+
8061
+ $sql = "
8062
+ SELECT *
8063
+ FROM `@wp_podsrel`
8064
+ WHERE
8065
+ {$related_where}
8066
+ ORDER BY `weight`
8067
+ ";
8068
+
8069
+ $relationships = pods_query( $sql );
8070
+
8071
+ if ( ! empty( $relationships ) ) {
8072
+ $related_ids = array();
8073
+
8074
+ foreach ( $relationships as $relation ) {
8075
+ if ( $field_id == $relation->field_id && ! in_array( $relation->item_id, $related_ids ) ) {
8076
+ $related_ids[] = (int) $relation->item_id;
8077
+ } elseif ( 0 < $sister_id && $field_id == $relation->related_field_id && ! in_array( $relation->related_item_id, $related_ids ) ) {
8078
+ $related_ids[] = (int) $relation->related_item_id;
8079
+ }
8080
+ }
8081
+ }
8082
+ } else {
8083
+ // @todo handle meta-based lookups
8084
+ return false;
8085
+
8086
+ if ( ! is_array( $pod ) ) {
8087
+ $pod = $this->load_pod( array( 'id' => $pod_id, 'table_info' => false ), false );
8088
+ }
8089
+
8090
+ if ( ! empty( $pod ) && in_array( $pod['type'], array(
8091
+ 'post_type',
8092
+ 'media',
8093
+ 'taxonomy',
8094
+ 'user',
8095
+ 'comment',
8096
+ 'settings'
8097
+ ) ) ) {
8098
+ $related_ids = array();
8099
+
8100
+ $meta_type = $pod['type'];
8101
+
8102
+ if ( in_array( $meta_type, array( 'post_type', 'media' ) ) ) {
8103
+ $meta_type = 'post';
8104
+ } elseif ( 'taxonomy' === $meta_type ) {
8105
+ $meta_type = 'term';
8106
+ }
8107
 
8108
+ $no_conflict = pods_no_conflict_check( ( 'term' === $meta_type ? 'taxonomy' : $meta_type ) );
 
8109
 
8110
+ if ( ! $no_conflict ) {
8111
+ pods_no_conflict_on( ( 'term' === $meta_type ? 'taxonomy' : $meta_type ) );
 
 
 
 
8112
  }
8113
 
8114
+ if ( 'settings' === $meta_type ) {
8115
+ $related_id = get_option( '_pods_' . $pod['name'] . '_' . $field['name'] );
8116
 
8117
+ if ( empty( $related_id ) ) {
8118
+ $related_id = get_option( $pod['name'] . '_' . $field['name'] );
8119
  }
8120
 
8121
+ if ( is_array( $related_id ) && ! empty( $related_id ) ) {
8122
+ foreach ( $related_id as $related ) {
8123
+ if ( is_array( $related ) && ! empty( $related ) ) {
8124
+ if ( isset( $related['id'] ) ) {
8125
+ $related_ids[] = (int) $related['id'];
8126
+ } else {
8127
+ foreach ( $related as $r ) {
8128
+ $related_ids[] = (int) $r;
8129
+ }
8130
+ }
8131
+ } else {
8132
+ $related_ids[] = (int) $related;
8133
+ }
8134
  }
8135
+ }
8136
+ } else {
8137
+ $related_id = get_metadata( $meta_type, $id, '_pods_' . $field['name'], true );
8138
+
8139
+ if ( empty( $related_id ) ) {
8140
+ $related_id = get_metadata( $meta_type, $id, $field['name'] );
8141
+ }
8142
+
8143
+ if ( is_array( $related_id ) && ! empty( $related_id ) ) {
8144
+ foreach ( $related_id as $related ) {
8145
+ if ( is_array( $related ) && ! empty( $related ) ) {
8146
+ if ( isset( $related['id'] ) ) {
8147
+ $related_ids[] = (int) $related['id'];
8148
+ } else {
8149
+ foreach ( $related as $r ) {
8150
+ if ( isset( $related['id'] ) ) {
8151
+ $related_ids[] = (int) $r['id'];
8152
+ } else {
8153
+ $related_ids[] = (int) $r;
8154
+ }
8155
+ }
8156
+ }
8157
+ } else {
8158
+ $related_ids[] = (int) $related;
8159
+ }
8160
  }
8161
+ }
8162
+ }
8163
 
8164
+ if ( ! $no_conflict ) {
8165
+ pods_no_conflict_off( ( 'term' === $meta_type ? 'taxonomy' : $meta_type ) );
8166
+ }
8167
+ }
8168
+ }
8169
 
8170
+ if ( is_array( $related_ids ) ) {
8171
+ $related_ids = array_unique( array_filter( $related_ids ) );
8172
+ }
8173
 
8174
+ return $related_ids;
8175
+ }
 
 
 
8176
 
8177
+ /**
8178
+ *
8179
+ * Load the information about an objects MySQL table
8180
+ *
8181
+ * @param $object_type
8182
+ * @param string $object The object to look for
8183
+ * @param null $name (optional) Name of the pod to load
8184
+ * @param array $pod (optional) Array with pod information
8185
+ *
8186
+ * @return array
8187
+ *
8188
+ * @since 2.5.0
8189
+ */
8190
+ public function get_table_info_load( $object_type, $object, $name = null, $pod = null ) {
8191
 
8192
+ $info = array();
 
 
 
 
 
 
8193
 
8194
+ if ( 'pod' === $object_type && null === $pod ) {
8195
+ if ( empty( $name ) ) {
8196
+ $prefix = 'pod-';
 
 
 
8197
 
8198
+ // Make sure we actually have the prefix before trying anything with the name
8199
+ if ( 0 === strpos( $object_type, $prefix ) ) {
8200
+ $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
8201
+ }
8202
+ }
8203
 
8204
+ if ( empty( $name ) && ! empty( $object ) ) {
8205
+ $name = $object;
8206
+ }
8207
 
8208
+ $pod = $this->load_pod( array( 'name' => $name, 'table_info' => false ), false );
 
8209
 
8210
+ if ( ! empty( $pod ) ) {
8211
+ $object_type = $pod['type'];
8212
+ $name = $pod['name'];
8213
+ $object = $pod['object'];
8214
 
8215
+ $info['pod'] = $pod;
8216
+ }
8217
+ } elseif ( null === $pod ) {
8218
+ if ( empty( $name ) ) {
8219
+ $prefix = $object_type . '-';
 
 
 
 
 
 
8220
 
8221
+ // Make sure we actually have the prefix before trying anything with the name
8222
+ if ( 0 === strpos( $object_type, $prefix ) ) {
8223
+ $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
8224
+ }
8225
+ }
 
 
 
8226
 
8227
+ if ( empty( $name ) && ! empty( $object ) ) {
8228
+ $name = $object;
8229
+ }
8230
 
8231
+ if ( ! empty( $name ) ) {
8232
+ $pod = $this->load_pod( array( 'name' => $name, 'table_info' => false ), false );
8233
 
8234
+ if ( ! empty( $pod ) && ( null === $object_type || $object_type == $pod['type'] ) ) {
8235
+ $object_type = $pod['type'];
8236
+ $name = $pod['name'];
8237
+ $object = $pod['object'];
8238
 
8239
+ $info['pod'] = $pod;
8240
+ }
8241
+ }
8242
+ } elseif ( ! empty( $pod ) ) {
8243
+ $info['pod'] = $pod;
8244
+ }
 
 
8245
 
8246
+ if ( 0 === strpos( $object_type, 'pod' ) ) {
8247
+ if ( empty( $name ) ) {
8248
+ $prefix = 'pod-';
8249
 
8250
+ // Make sure we actually have the prefix before trying anything with the name
8251
+ if ( 0 === strpos( $object_type, $prefix ) ) {
8252
+ $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
8253
+ }
8254
+ }
 
 
 
 
 
 
 
8255
 
8256
+ $info['type'] = 'pod';
8257
+ global $wpdb;
8258
+
8259
+ $info['meta_table'] = $wpdb->prefix . 'pods_' . ( empty( $object ) ? $name : $object );
8260
+ $info['table'] = $info['meta_table'];
8261
+
8262
+ if ( is_array( $info['pod'] ) && 'pod' === pods_v( 'type', $info['pod'] ) ) {
8263
+ $info['meta_field_value'] = pods_v( 'pod_index', $info['pod']['options'], 'id', true );
8264
+ $info['pod_field_index'] = $info['meta_field_value'];
8265
+ $info['field_index'] = $info['meta_field_value'];
8266
+ $info['meta_field_index'] = $info['meta_field_value'];
8267
+
8268
+ $slug_field = get_posts( array(
8269
+ 'post_type' => '_pods_field',
8270
+ 'posts_per_page' => 1,
8271
+ 'nopaging' => true,
8272
+ 'post_parent' => $info['pod']['id'],
8273
+ 'orderby' => 'menu_order',
8274
+ 'order' => 'ASC',
8275
+ 'meta_query' => array(
8276
+ array(
8277
+ 'key' => 'type',
8278
+ 'value' => 'slug',
8279
+ )
8280
+ )
8281
+ ) );
8282
 
8283
+ if ( ! empty( $slug_field[0] ) ) {
8284
+ $slug_field = $slug_field[0];
 
 
 
 
 
 
 
 
8285
 
8286
+ $info['pod_field_slug'] = $slug_field->post_name;
8287
+ $info['field_slug'] = $slug_field->post_name;
8288
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8289
 
8290
+ if ( 1 == pods_v( 'hierarchical', $info['pod']['options'], 0 ) ) {
8291
+ $parent_field = pods_v( 'pod_parent', $info['pod']['options'], 'id', true );
8292
+
8293
+ if ( ! empty( $parent_field ) && isset( $info['pod']['fields'][ $parent_field ] ) ) {
8294
+ $info['object_hierarchical'] = true;
8295
+
8296
+ $info['field_parent'] = $parent_field . '_select';
8297
+ $info['pod_field_parent'] = $info['field_parent'];
8298
+ $info['field_parent_select'] = '`' . $parent_field . '`.`id` AS `' . $info['field_parent'] . '`';
8299
+ }
8300
+ }
8301
  }
8302
  }
8303
 
8304
+ return $info;
8305
+ }
8306
+
8307
+ /**
8308
+ * Get information about an objects MySQL table
8309
+ *
8310
+ * @param string $object_type
8311
+ * @param string $object The object to look for
8312
+ * @param null $name (optional) Name of the pod to load
8313
+ * @param array $pod (optional) Array with pod information
8314
+ * @param array $field (optional) Array with field information
8315
+ *
8316
+ * @return array|bool
8317
+ *
8318
+ * @since 2.0.0
8319
+ */
8320
+ public function get_table_info( $object_type, $object, $name = null, $pod = null, $field = null ) {
8321
+
8322
+ /**
8323
+ * @var $wpdb wpdb
8324
+ * @var $sitepress SitePress
8325
+ * @var $polylang object
8326
+ */
8327
+ /*
8328
+ * @todo wpml-comp Remove global object usage
8329
+ */
8330
+ global $wpdb, $sitepress, $polylang;
8331
+
8332
+ // @todo Handle $object arrays for Post Types, Taxonomies, Comments (table pulled from first object in array)
8333
+
8334
+ $info = array(
8335
+ //'select' => '`t`.*',
8336
+ 'object_type' => $object_type,
8337
+ 'type' => null,
8338
+ 'object_name' => $object,
8339
+ 'object_hierarchical' => false,
8340
+
8341
+ 'table' => $object,
8342
+ 'meta_table' => $object,
8343
+ 'pod_table' => $wpdb->prefix . 'pods_' . ( empty( $object ) ? $name : $object ),
8344
+
8345
+ 'field_id' => 'id',
8346
+ 'field_index' => 'name',
8347
+ 'field_slug' => null,
8348
+ 'field_type' => null,
8349
+ 'field_parent' => null,
8350
+ 'field_parent_select' => null,
8351
+
8352
+ 'meta_field_id' => 'id',
8353
+ 'meta_field_index' => 'name',
8354
+ 'meta_field_value' => 'name',
8355
+
8356
+ 'pod_field_id' => 'id',
8357
+ 'pod_field_index' => 'name',
8358
+ 'pod_field_slug' => null,
8359
+ 'pod_field_parent' => null,
8360
+
8361
+ 'join' => array(),
8362
+
8363
+ 'where' => null,
8364
+ 'where_default' => null,
8365
+
8366
+ 'orderby' => null,
8367
+
8368
+ 'pod' => null,
8369
+ 'recurse' => false
8370
+ );
8371
+
8372
+ if ( empty( $object_type ) ) {
8373
+ $object_type = 'post_type';
8374
+ $object = 'post';
8375
+ } elseif ( empty( $object ) && in_array( $object_type, array( 'user', 'media', 'comment' ), true ) ) {
8376
+ $object = $object_type;
8377
+ }
8378
+
8379
+ $pod_name = $pod;
8380
 
8381
+ if ( is_array( $pod_name ) ) {
8382
+ $pod_name = pods_var_raw( 'name', $pod_name, ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $pod_name, JSON_UNESCAPED_UNICODE ) : json_encode( $pod_name ) ), null, true );
8383
+ } else {
8384
+ $pod_name = $object;
8385
+ }
8386
 
8387
+ $field_name = $field;
 
 
8388
 
8389
+ if ( is_array( $field_name ) ) {
8390
+ $field_name = pods_var_raw( 'name', $field_name, ( version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $pod_name, JSON_UNESCAPED_UNICODE ) : json_encode( $field_name ) ), null, true );
8391
+ }
8392
 
8393
+ $transient = 'pods_' . $wpdb->prefix . '_get_table_info_' . md5( $object_type . '_object_' . $object . '_name_' . $name . '_pod_' . $pod_name . '_field_' . $field_name );
8394
 
8395
+ $current_language = false;
8396
+ $current_language_t_id = 0;
8397
+ $current_language_tt_id = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8398
 
8399
+ // Get current language data
8400
+ $lang_data = pods_i18n()->get_current_language_data();
8401
 
8402
+ if ( $lang_data ) {
8403
+ if ( ! empty( $lang_data['language'] ) ) {
8404
+ $current_language = $lang_data['language'];
8405
+ }
8406
 
8407
+ if ( ! empty( $lang_data['t_id'] ) ) {
8408
+ $current_language_t_id = $lang_data['t_id'];
8409
+ }
 
8410
 
8411
+ if ( ! empty( $lang_data['tt_id'] ) ) {
8412
+ $current_language_tt_id = $lang_data['tt_id'];
8413
+ }
8414
 
8415
+ if ( ! empty( $lang_data['tl_t_id'] ) ) {
8416
+ $current_language_tl_t_id = $lang_data['tl_t_id'];
8417
+ }
8418
 
8419
+ if ( ! empty( $lang_data['tl_tt_id'] ) ) {
8420
+ $current_language_tl_tt_id = $lang_data['tl_tt_id'];
8421
+ }
8422
+ }
 
 
 
 
8423
 
8424
+ if ( ! empty( $current_language ) ) {
8425
+ $transient = 'pods_' . $wpdb->prefix . '_get_table_info_' . $current_language . '_' . md5( $object_type . '_object_' . $object . '_name_' . $name . '_pod_' . $pod_name . '_field_' . $field_name );
8426
+ }
8427
 
8428
+ $_info = false;
8429
+ $transient_cached = false;
8430
 
8431
+ if ( isset( self::$table_info_cache[ $transient ] ) ) {
8432
+ // Prefer info from the object internal cache
8433
+ $_info = self::$table_info_cache[ $transient ];
8434
+ } elseif ( pods_api_cache() ) {
8435
+ $_info = pods_transient_get( $transient );
8436
+ if ( false === $_info && ! did_action( 'init' ) ) {
8437
+ $_info = pods_transient_get( $transient . '_pre_init' );
8438
+ }
8439
+ $transient_cached = true;
8440
+ }
8441
 
8442
+ if ( false !== $_info && is_array( $_info ) ) {
8443
+ // Data was cached, use that
8444
+ $info = $_info;
8445
+ } else {
8446
+ // Data not cached, load it up
8447
+ $_info = $this->get_table_info_load( $object_type, $object, $name, $pod );
8448
+ if ( isset( $_info['type'] ) ) {
8449
+ // Allow function to override $object_type
8450
+ $object_type = $_info['type'];
8451
  }
8452
+ $info = array_merge( $info, $_info );
8453
  }
8454
 
8455
+ if ( 0 === strpos( $object_type, 'post_type' ) || 'media' === $object_type || in_array( pods_var_raw( 'type', $info['pod'] ), array(
8456
+ 'post_type',
8457
+ 'media'
8458
+ ) ) ) {
8459
+ $info['table'] = $wpdb->posts;
8460
+ $info['meta_table'] = $wpdb->postmeta;
8461
+
8462
+ $info['field_id'] = 'ID';
8463
+ $info['field_index'] = 'post_title';
8464
+ $info['field_slug'] = 'post_name';
8465
+ $info['field_type'] = 'post_type';
8466
+ $info['field_parent'] = 'post_parent';
8467
+ $info['field_parent_select'] = '`t`.`' . $info['field_parent'] . '`';
8468
+
8469
+ $info['meta_field_id'] = 'post_id';
8470
+ $info['meta_field_index'] = 'meta_key';
8471
+ $info['meta_field_value'] = 'meta_value';
8472
+
8473
+ if ( 'media' === $object_type ) {
8474
+ $object = 'attachment';
8475
+ }
8476
 
8477
+ if ( empty( $name ) ) {
8478
+ $prefix = 'post_type-';
8479
 
8480
+ // Make sure we actually have the prefix before trying anything with the name
8481
+ if ( 0 === strpos( $object_type, $prefix ) ) {
8482
+ $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
8483
+ }
8484
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8485
 
8486
+ if ( 'media' !== $object_type ) {
8487
+ $object_type = 'post_type';
 
8488
  }
8489
+
8490
+ $post_type = pods_sanitize( ( empty( $object ) ? $name : $object ) );
8491
+
8492
+ if ( 'attachment' === $post_type || 'media' === $object_type ) {
8493
+ $info['pod_table'] = $wpdb->prefix . 'pods_media';
8494
+ } else {
8495
+ $info['pod_table'] = $wpdb->prefix . 'pods_' . pods_clean_name( $post_type, true, false );
8496
  }
 
8497
 
8498
+ $post_type_object = get_post_type_object( $post_type );
8499
 
8500
+ if ( is_object( $post_type_object ) && $post_type_object->hierarchical ) {
8501
+ $info['object_hierarchical'] = true;
8502
+ }
8503
 
8504
+ // Post Status default
8505
+ $post_status = array( 'publish' );
8506
 
8507
+ // Pick field post_status option
8508
+ if ( ! empty( $field['options']['pick_post_status'] ) ) {
8509
+ $post_status = (array) $field['options']['pick_post_status'];
8510
+ } elseif ( ! empty( $field['pick_post_status'] ) ) {
8511
+ $post_status = (array) $field['pick_post_status'];
8512
  }
8513
 
8514
+ /**
8515
+ * Default Post Status to query for.
8516
+ *
8517
+ * Use to change "default" post status from publish to any other status or statuses.
8518
+ *
8519
+ * @param array $post_status List of post statuses. Default is 'publish' or field setting (if available).
8520
+ * @param string $post_type Post type of current object.
8521
+ * @param array $info Array of information about the object.
8522
+ * @param string $object Type of object.
8523
+ * @param string $name Name of pod to load.
8524
+ * @param array $pod Array with Pod information. Result of PodsAPI::load_pod().
8525
+ * @param array $field Array with field information.
8526
+ *
8527
+ * @since unknown
8528
+ */
8529
+ $post_status = apply_filters( 'pods_api_get_table_info_default_post_status', $post_status, $post_type, $info, $object_type, $object, $name, $pod, $field );
8530
+
8531
+ $info['where'] = array(
8532
+ //'post_status' => '`t`.`post_status` IN ( "inherit", "publish" )', // @todo Figure out what statuses Attachments can be
8533
+ 'post_type' => '`t`.`' . $info['field_type'] . '` = "' . $post_type . '"'
8534
+ );
8535
 
8536
+ if ( 'post_type' === $object_type ) {
8537
+ $info['where_default'] = '`t`.`post_status` IN ( "' . implode( '", "', $post_status ) . '" )';
8538
+ }
8539
 
8540
+ $info['orderby'] = '`t`.`menu_order`, `t`.`' . $info['field_index'] . '`, `t`.`post_date`';
8541
+
8542
+ /*
8543
+ * @todo wpml-comp Check if WPML filters can be applied afterwards
8544
+ */
8545
+ // WPML support
8546
+ if ( did_action( 'wpml_loaded' ) && ! empty( $current_language ) && apply_filters( 'wpml_is_translated_post_type', false, $post_type ) && apply_filters( 'wpml_setting', true, 'auto_adjust_ids' ) ) {
8547
+ $info['join']['wpml_translations'] = "
8548
+ LEFT JOIN `{$wpdb->prefix}icl_translations` AS `wpml_translations`
8549
+ ON `wpml_translations`.`element_id` = `t`.`ID`
8550
+ AND `wpml_translations`.`element_type` = 'post_{$post_type}'
8551
+ AND `wpml_translations`.`language_code` = '{$current_language}'
8552
+ ";
8553
+
8554
+ $info['join']['wpml_languages'] = "
8555
+ LEFT JOIN `{$wpdb->prefix}icl_languages` AS `wpml_languages`
8556
+ ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
8557
+ ";
8558
+
8559
+ $info['where']['wpml_languages'] = "`wpml_languages`.`code` IS NOT NULL";
8560
+ } elseif ( ( function_exists( 'PLL' ) || is_object( $polylang ) ) && ! empty( $current_language ) && function_exists( 'pll_is_translated_post_type' ) && pll_is_translated_post_type( $post_type ) ) {
8561
+ // Polylang support
8562
+ $info['join']['polylang_languages'] = "
8563
+ LEFT JOIN `{$wpdb->term_relationships}` AS `polylang_languages`
8564
+ ON `polylang_languages`.`object_id` = `t`.`ID`
8565
+ AND `polylang_languages`.`term_taxonomy_id` = {$current_language_tt_id}
8566
+ ";
8567
+
8568
+ $info['where']['polylang_languages'] = "`polylang_languages`.`object_id` IS NOT NULL";
8569
+ }
8570
 
8571
+ $info['object_fields'] = $this->get_wp_object_fields( $object_type, $info['pod'] );
8572
+ } elseif ( 0 === strpos( $object_type, 'taxonomy' ) || in_array( $object_type, array(
8573
+ 'nav_menu',
8574
+ 'post_format'
8575
+ ) ) || 'taxonomy' === pods_var_raw( 'type', $info['pod'] ) ) {
8576
+ $info['table'] = $wpdb->terms;
8577
+ $info['meta_table'] = $wpdb->terms;
8578
+
8579
+ $info['join']['tt'] = "LEFT JOIN `{$wpdb->term_taxonomy}` AS `tt` ON `tt`.`term_id` = `t`.`term_id`";
8580
+ $info['join']['tr'] = "LEFT JOIN `{$wpdb->term_relationships}` AS `tr` ON `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`";
8581
+ $info['meta_field_id'] = 'term_id';
8582
+ $info['field_id'] = 'term_id';
8583
+ $info['meta_field_value'] = 'name';
8584
+ $info['field_index'] = 'name';
8585
+ $info['meta_field_index'] = 'name';
8586
+ $info['field_slug'] = 'slug';
8587
+ $info['field_type'] = 'taxonomy';
8588
+ $info['field_parent'] = 'parent';
8589
+ $info['field_parent_select'] = '`tt`.`' . $info['field_parent'] . '`';
8590
+
8591
+ if ( ! empty( $wpdb->termmeta ) ) {
8592
+ $info['meta_table'] = $wpdb->termmeta;
8593
+
8594
+ $info['meta_field_id'] = 'term_id';
8595
+ $info['meta_field_index'] = 'meta_key';
8596
+ $info['meta_field_value'] = 'meta_value';
8597
+ }
8598
 
8599
+ if ( 'nav_menu' === $object_type ) {
8600
+ $object = 'nav_menu';
8601
+ } elseif ( 'post_format' === $object_type ) {
8602
+ $object = 'post_format';
8603
+ }
8604
 
8605
+ if ( empty( $name ) ) {
8606
+ $prefix = 'taxonomy-';
8607
 
8608
+ // Make sure we actually have the prefix before trying anything with the name
8609
+ if ( 0 === strpos( $object_type, $prefix ) ) {
8610
+ $name = substr( $object_type, strlen( $prefix ), strlen( $object_type ) );
8611
+ }
8612
+ }
8613
 
8614
+ if ( ! in_array( $object_type, array( 'nav_menu', 'post_format' ) ) ) {
8615
+ $object_type = 'taxonomy';
8616
+ }
8617
 
8618
+ $taxonomy = pods_sanitize( ( empty( $object ) ? $name : $object ) );
8619
 
8620
+ $info['pod_table'] = $wpdb->prefix . 'pods_' . pods_clean_name( $taxonomy, true, false );
8621
 
8622
+ $taxonomy_object = get_taxonomy( $taxonomy );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8623
 
8624
+ if ( is_object( $taxonomy_object ) && $taxonomy_object->hierarchical ) {
8625
+ $info['object_hierarchical'] = true;
8626
+ }
8627
 
8628
+ $info['where'] = array(
8629
+ 'tt.taxonomy' => '`tt`.`' . $info['field_type'] . '` = "' . $taxonomy . '"'
8630
+ );
 
 
 
8631
 
8632
+ /*
8633
+ * @todo wpml-comp WPML API call for is_translated_taxononomy
8634
+ * @todo wpml-comp Check if WPML filters can be applied afterwards
8635
+ */
8636
+ // WPML Support
8637
+ if ( is_object( $sitepress ) && ! empty( $current_language ) && $sitepress->is_translated_taxonomy( $taxonomy ) && apply_filters( 'wpml_setting', true, 'auto_adjust_ids' ) ) {
8638
+ $info['join']['wpml_translations'] = "
8639
+ LEFT JOIN `{$wpdb->prefix}icl_translations` AS `wpml_translations`
8640
+ ON `wpml_translations`.`element_id` = `tt`.`term_taxonomy_id`
8641
+ AND `wpml_translations`.`element_type` = 'tax_{$taxonomy}'
8642
+ AND `wpml_translations`.`language_code` = '{$current_language}'
8643
+ ";
8644
+
8645
+ $info['join']['wpml_languages'] = "
8646
+ LEFT JOIN `{$wpdb->prefix}icl_languages` AS `wpml_languages`
8647
+ ON `wpml_languages`.`code` = `wpml_translations`.`language_code` AND `wpml_languages`.`active` = 1
8648
+ ";
8649
+
8650
+ $info['where']['wpml_languages'] = "`wpml_languages`.`code` IS NOT NULL";
8651
+ } elseif ( ( function_exists( 'PLL' ) || is_object( $polylang ) ) && ! empty( $current_language ) && ! empty( $current_language_tl_tt_id ) && function_exists( 'pll_is_translated_taxonomy' ) && pll_is_translated_taxonomy( $taxonomy ) ) {
8652
+ // Polylang support
8653
+ $info['join']['polylang_languages'] = "
8654
+ LEFT JOIN `{$wpdb->term_relationships}` AS `polylang_languages`
8655
+ ON `polylang_languages`.`object_id` = `t`.`term_id`
8656
+ AND `polylang_languages`.`term_taxonomy_id` = {$current_language_tl_tt_id}
8657
+ ";
8658
+
8659
+ $info['where']['polylang_languages'] = "`polylang_languages`.`object_id` IS NOT NULL";
8660
+ }
8661
 
8662
+ $info['object_fields'] = $this->get_wp_object_fields( $object_type, $info['pod'] );
8663
+ } elseif ( 'user' === $object_type || 'user' === pods_var_raw( 'type', $info['pod'] ) ) {
8664
+ $info['table'] = $wpdb->users;
8665
+ $info['meta_table'] = $wpdb->usermeta;
8666
+ $info['pod_table'] = $wpdb->prefix . 'pods_user';
8667
 
8668
+ $info['field_id'] = 'ID';
8669
+ $info['field_index'] = 'display_name';
8670
+ $info['field_slug'] = 'user_nicename';
8671
 
8672
+ $info['meta_field_id'] = 'user_id';
8673
+ $info['meta_field_index'] = 'meta_key';
8674
+ $info['meta_field_value'] = 'meta_value';
8675
 
8676
+ $info['where'] = array(
8677
+ 'user_status' => '`t`.`user_status` = 0'
8678
+ );
8679
 
8680
+ $info['object_fields'] = $this->get_wp_object_fields( $object_type, $info['pod'] );
8681
+ } elseif ( 'comment' === $object_type || 'comment' === pods_var_raw( 'type', $info['pod'] ) ) {
8682
+ //$info[ 'object_hierarchical' ] = true;
8683
 
8684
+ $info['table'] = $wpdb->comments;
8685
+ $info['meta_table'] = $wpdb->commentmeta;
8686
+ $info['pod_table'] = $wpdb->prefix . 'pods_comment';
8687
 
8688
+ $info['field_id'] = 'comment_ID';
8689
+ $info['field_index'] = 'comment_date';
8690
+ $info['field_type'] = 'comment_type';
8691
+ $info['field_parent'] = 'comment_parent';
8692
+ $info['field_parent_select'] = '`t`.`' . $info['field_parent'] . '`';
8693
 
8694
+ $info['meta_field_id'] = 'comment_id';
8695
+ $info['meta_field_index'] = 'meta_key';
8696
+ $info['meta_field_value'] = 'meta_value';
8697
 
8698
+ $object = 'comment';
 
8699
 
8700
+ $comment_type = ( empty( $object ) ? $name : $object );
 
 
 
8701
 
8702
+ $comment_type_clause = '`t`.`' . $info['field_type'] . '` = "' . $comment_type . '"';
 
 
8703
 
8704
+ if ( 'comment' === $comment_type ) {
8705
+ $comment_type_clause = '( ' . $comment_type_clause . ' OR `t`.`' . $info['field_type'] . '` = "" )';
8706
+ }
8707
 
8708
+ $info['where'] = array(
8709
+ 'comment_approved' => '`t`.`comment_approved` = 1',
8710
+ 'comment_type' => $comment_type_clause
8711
+ );
8712
 
8713
+ $info['orderby'] = '`t`.`' . $info['field_index'] . '` DESC, `t`.`' . $info['field_id'] . '`';
8714
+ } elseif ( in_array( $object_type, array(
8715
+ 'option',
8716
+ 'settings'
8717
+ ) ) || 'settings' === pods_var_raw( 'type', $info['pod'] ) ) {
8718
+ $info['table'] = $wpdb->options;
8719
+ $info['meta_table'] = $wpdb->options;
8720
+
8721
+ $info['field_id'] = 'option_id';
8722
+ $info['field_index'] = 'option_name';
8723
+
8724
+ $info['meta_field_id'] = 'option_id';
8725
+ $info['meta_field_index'] = 'option_name';
8726
+ $info['meta_field_value'] = 'option_value';
8727
+
8728
+ $info['orderby'] = '`t`.`' . $info['field_index'] . '` ASC';
8729
+ } elseif ( is_multisite() && ( in_array( $object_type, array(
8730
+ 'site_option',
8731
+ 'site_settings'
8732
+ ) ) || 'site_settings' === pods_var_raw( 'type', $info['pod'] ) ) ) {
8733
+ $info['table'] = $wpdb->sitemeta;
8734
+ $info['meta_table'] = $wpdb->sitemeta;
8735
+
8736
+ $info['field_id'] = 'site_id';
8737
+ $info['field_index'] = 'meta_key';
8738
+
8739
+ $info['meta_field_id'] = 'site_id';
8740
+ $info['meta_field_index'] = 'meta_key';
8741
+ $info['meta_field_value'] = 'meta_value';
8742
+
8743
+ $info['orderby'] = '`t`.`' . $info['field_index'] . '` ASC';
8744
+ } elseif ( is_multisite() && 'network' === $object_type ) { // Network = Site
8745
+ $info['table'] = $wpdb->site;
8746
+ $info['meta_table'] = $wpdb->sitemeta;
8747
+
8748
+ $info['field_id'] = 'id';
8749
+ $info['field_index'] = 'domain';
8750
+
8751
+ $info['meta_field_id'] = 'site_id';
8752
+ $info['meta_field_index'] = 'meta_key';
8753
+ $info['meta_field_value'] = 'meta_value';
8754
+
8755
+ $info['orderby'] = '`t`.`' . $info['field_index'] . '` ASC, `t`.`path` ASC, `t`.`' . $info['field_id'] . '`';
8756
+ } elseif ( is_multisite() && 'site' === $object_type ) { // Site = Blog
8757
+ $info['table'] = $wpdb->blogs;
8758
+
8759
+ $info['field_id'] = 'blog_id';
8760
+ $info['field_index'] = 'domain';
8761
+ $info['field_type'] = 'site_id';
8762
+
8763
+ $info['where'] = array(
8764
+ 'archived' => '`t`.`archived` = 0',
8765
+ 'spam' => '`t`.`spam` = 0',
8766
+ 'deleted' => '`t`.`deleted` = 0',
8767
+ 'site_id' => '`t`.`' . $info['field_type'] . '` = ' . (int) get_current_site()->id
8768
+ );
8769
 
8770
+ $info['orderby'] = '`t`.`' . $info['field_index'] . '` ASC, `t`.`path` ASC, `t`.`' . $info['field_id'] . '`';
8771
+ } elseif ( 'table' === $object_type || 'table' === pods_var_raw( 'type', $info['pod'] ) ) {
8772
+ $info['table'] = ( empty( $object ) ? $name : $object );
8773
+ $info['pod_table'] = $wpdb->prefix . 'pods_' . $info['table'];
8774
 
 
 
 
8775
 
8776
+ if ( ! empty( $field ) ) {
8777
+ if ( ! is_array( $field ) ) {
8778
+ if ( is_string( $pod ) ) {
8779
+ $pod = pods( $pod );
8780
+ }
8781
+ if ( $pod && ! empty( $pod->fields[ $field ] ) ) {
8782
+ $field = $pod->fields[ $field ];
8783
+ }
8784
+ }
8785
+ if ( is_array( $field ) ) {
8786
+ $info['table'] = pods_var_raw( 'pick_table', pods_var_raw( 'options', $field, $field ) );
8787
+ $info['field_id'] = pods_var_raw( 'pick_table_id', pods_var_raw( 'options', $field, $field ) );
8788
+ $info['meta_field_value'] = pods_var_raw( 'pick_table_index', pods_var_raw( 'options', $field, $field ) );
8789
+ $info['field_index'] = $info['meta_field_value'];
8790
+ $info['meta_field_index'] = $info['meta_field_value'];
8791
+ }
8792
+ }
8793
+ }
8794
 
8795
+ $info['table'] = pods_clean_name( $info['table'], false, false );
8796
+ $info['meta_table'] = pods_clean_name( $info['meta_table'], false, false );
8797
+ $info['pod_table'] = pods_clean_name( $info['pod_table'], false, false );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8798
 
8799
+ $info['field_id'] = pods_clean_name( $info['field_id'], false, false );
8800
+ $info['field_index'] = pods_clean_name( $info['field_index'], false, false );
8801
+ $info['field_slug'] = pods_clean_name( $info['field_slug'], false, false );
 
 
 
 
 
 
8802
 
8803
+ $info['meta_field_id'] = pods_clean_name( $info['meta_field_id'], false, false );
8804
+ $info['meta_field_index'] = pods_clean_name( $info['meta_field_index'], false, false );
8805
+ $info['meta_field_value'] = pods_clean_name( $info['meta_field_value'], false, false );
 
8806
 
8807
+ if ( empty( $info['orderby'] ) ) {
8808
+ $info['orderby'] = '`t`.`' . $info['field_index'] . '`, `t`.`' . $info['field_id'] . '`';
8809
+ }
8810
 
8811
+ if ( 'table' === pods_var_raw( 'storage', $info['pod'] ) && ! in_array( $object_type, array(
8812
+ 'pod',
8813
+ 'table'
8814
+ ) ) ) {
8815
+ $info['join']['d'] = 'LEFT JOIN `' . $info['pod_table'] . '` AS `d` ON `d`.`id` = `t`.`' . $info['field_id'] . '`';
8816
+ //$info[ 'select' ] .= ', `d`.*';
8817
+ }
8818
 
8819
+ if ( ! empty( $info['pod'] ) && is_array( $info['pod'] ) ) {
8820
+ $info['recurse'] = true;
8821
+ }
 
 
 
8822
 
8823
+ $info['type'] = $object_type;
8824
+ $info['object_name'] = $object;
8825
 
8826
+ if ( pods_api_cache() ) {
8827
+ if ( ! did_action( 'init' ) ) {
8828
+ $transient .= '_pre_init';
8829
+ }
8830
 
8831
+ if ( !$transient_cached ) {
8832
+ pods_transient_set( $transient, $info );
8833
+ }
8834
+ }
8835
 
8836
+ self::$table_info_cache[ $transient ] = apply_filters( 'pods_api_get_table_info', $info, $object_type, $object, $name, $pod, $field, $this );
 
8837
 
8838
+ return self::$table_info_cache[ $transient ];
8839
+ }
8840
 
8841
+ /**
8842
+ * Export a package
8843
+ *
8844
+ * $params['pods'] array Pod Type IDs to export
8845
+ * $params['templates'] array Template IDs to export
8846
+ * $params['pages'] array Pod Page IDs to export
8847
+ * $params['helpers'] array Helper IDs to export
8848
+ *
8849
+ * @param array $params An associative array of parameters
8850
+ *
8851
+ * @return array|bool
8852
+ *
8853
+ * @since 1.9.0
8854
+ * @deprecated 2.0.0
8855
+ */
8856
+ public function export_package( $params ) {
8857
 
8858
+ if ( class_exists( 'Pods_Migrate_Packages' ) ) {
8859
+ return Pods_Migrate_Packages::export( $params );
8860
+ }
8861
 
8862
+ return false;
8863
+ }
8864
 
8865
+ /**
8866
+ * Replace an existing package
8867
+ *
8868
+ * @param mixed $data (optional) An associative array containing a package, or the json encoded package
8869
+ *
8870
+ * @return bool
8871
+ *
8872
+ * @since 1.9.8
8873
+ * @deprecated 2.0.0
8874
+ */
8875
+ public function replace_package( $data = false ) {
8876
 
8877
+ return $this->import_package( $data, true );
8878
+ }
8879
 
8880
+ /**
8881
+ * Import a package
8882
+ *
8883
+ * @param mixed $data (optional) An associative array containing a package, or the json encoded package
8884
+ * @param bool $replace (optional) Replace existing items when found
8885
+ *
8886
+ * @return bool
8887
+ *
8888
+ * @since 1.9.0
8889
+ * @deprecated 2.0.0
8890
+ */
8891
+ public function import_package( $data = false, $replace = false ) {
8892
 
8893
+ if ( class_exists( 'Pods_Migrate_Packages' ) ) {
8894
+ return Pods_Migrate_Packages::import( $data, $replace );
8895
+ }
8896
 
8897
+ return false;
8898
+ }
 
 
8899
 
8900
+ /**
8901
+ * Validate a package
8902
+ *
8903
+ * @param array|string $data (optional) An associative array containing a package, or the json encoded package
8904
+ * @param bool $output (optional)
8905
+ *
8906
+ * @return array|bool
8907
+ *
8908
+ * @since 1.9.0
8909
+ * @deprecated 2.0.0
8910
+ */
8911
+ public function validate_package( $data = false, $output = false ) {
8912
 
8913
+ return true;
8914
+ }
8915
 
8916
+ /**
8917
+ * Import data from an array or a CSV file.
8918
+ *
8919
+ * @param mixed $import_data PHP associative array or CSV input
8920
+ * @param bool $numeric_mode Use IDs instead of the name field when matching
8921
+ * @param string $format Format of import data, options are php or csv
8922
+ *
8923
+ * @return array IDs of imported items
8924
+ *
8925
+ * @since 1.7.1
8926
+ * @todo This needs some love and use of table_info etc for relationships
8927
+ */
8928
+ public function import( $import_data, $numeric_mode = false, $format = null ) {
8929
 
8930
+ /**
8931
+ * @var $wpdb wpdb
8932
+ */
8933
+ global $wpdb;
8934
 
8935
+ if ( null === $format && null !== $this->format ) {
8936
+ $format = $this->format;
8937
+ }
8938
 
8939
+ if ( 'csv' === $format && ! is_array( $import_data ) ) {
8940
+ $data = pods_migrate( 'sv', ',' )->parse( $import_data );
 
 
8941
 
8942
+ $import_data = $data['items'];
8943
+ }
8944
 
8945
+ pods_query( "SET NAMES utf8" );
8946
+ pods_query( "SET CHARACTER SET utf8" );
8947
 
8948
+ // Loop through the array of items
8949
+ $ids = array();
 
 
 
8950
 
8951
+ // Test to see if it's an array of arrays
8952
+ if ( ! is_array( @current( $import_data ) ) ) {
8953
+ $import_data = array( $import_data );
8954
+ }
8955
 
8956
+ $pod = $this->load_pod( array( 'name' => $this->pod ) );
 
 
 
 
 
 
 
8957
 
8958
+ if ( false === $pod ) {
8959
+ return pods_error( __( 'Pod not found', 'pods' ), $this );
 
8960
  }
8961
 
8962
+ $fields = array_merge( $pod['fields'], $pod['object_fields'] );
8963
 
8964
+ $simple_tableless_objects = PodsForm::simple_tableless_objects();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8965
 
8966
+ foreach ( $import_data as $key => $data_row ) {
8967
+ $data = array();
8968
 
8969
+ // Loop through each field (use $fields so only valid fields get parsed)
8970
+ foreach ( $fields as $field_name => $field_data ) {
8971
+ if ( ! isset( $data_row[ $field_name ] ) && ! isset( $data_row[ $field_data['label'] ] ) ) {
8972
+ continue;
8973
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8974
 
8975
+ $field_id = $field_data['id'];
8976
+ $type = $field_data['type'];
8977
+ $pick_object = isset( $field_data['pick_object'] ) ? $field_data['pick_object'] : '';
8978
+ $pick_val = isset( $field_data['pick_val'] ) ? $field_data['pick_val'] : '';
 
 
 
 
8979
 
8980
+ if ( isset( $data_row[ $field_name ] ) ) {
8981
+ $field_value = $data_row[ $field_name ];
8982
+ } else {
8983
+ $field_value = $data_row[ $field_data['label'] ];
8984
+ }
8985
 
8986
+ if ( null !== $field_value && false !== $field_value && '' !== $field_value ) {
8987
+ if ( 'pick' === $type || in_array( $type, PodsForm::file_field_types() ) ) {
8988
+ $field_values = is_array( $field_value ) ? $field_value : array( $field_value );
8989
+ $pick_values = array();
8990
 
8991
+ foreach ( $field_values as $pick_value ) {
8992
+ if ( in_array( $type, PodsForm::file_field_types() ) || 'media' === $pick_object ) {
8993
+ $where = "`guid` = '" . pods_sanitize( $pick_value ) . "'";
8994
 
8995
+ if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode ) {
8996
+ $where = "`ID` = " . pods_absint( $pick_value );
8997
+ }
8998
 
8999
+ $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = 'attachment' AND {$where} ORDER BY `ID`", $this );
9000
 
9001
+ if ( ! empty( $result ) ) {
9002
+ $pick_values[] = $result[0]->id;
9003
+ }
9004
+ } elseif ( 'pick' === $type ) {
9005
+ // @todo This could and should be abstracted better and simplified
9006
+ $related_pod = false;
9007
+
9008
+ if ( 'pod' === $pick_object ) {
9009
+ $related_pod = $this->load_pod( array(
9010
+ 'name' => $pick_val,
9011
+ 'table_info' => true
9012
+ ), false );
9013
+ }
9014
 
9015
+ if ( empty( $related_pod ) ) {
9016
+ $related_pod = array(
9017
+ 'id' => 0,
9018
+ 'type' => $pick_object
9019
+ );
9020
+ }
9021
 
9022
+ if ( in_array( 'taxonomy', array( $pick_object, $related_pod['type'] ) ) ) {
9023
+ $where = "`t`.`name` = '" . pods_sanitize( $pick_value ) . "'";
 
9024
 
9025
+ if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode ) {
9026
+ $where = "`tt`.`term_id` = " . pods_absint( $pick_value );
9027
+ }
 
 
 
9028
 
9029
+ $result = pods_query( "SELECT `t`.`term_id` AS `id` FROM `{$wpdb->term_taxonomy}` AS `tt` LEFT JOIN `{$wpdb->terms}` AS `t` ON `t`.`term_id` = `tt`.`term_id` WHERE `taxonomy` = '{$pick_val}' AND {$where} ORDER BY `t`.`term_id` LIMIT 1", $this );
9030
 
9031
+ if ( ! empty( $result ) ) {
9032
+ $pick_values[] = $result[0]->id;
9033
+ }
9034
+ } elseif ( in_array( 'post_type', array(
9035
+ $pick_object,
9036
+ $related_pod['type']
9037
+ ) ) || in_array( 'media', array( $pick_object, $related_pod['type'] ) ) ) {
9038
+ $where = "`post_title` = '" . pods_sanitize( $pick_value ) . "'";
9039
+
9040
+ if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode ) {
9041
+ $where = "`ID` = " . pods_absint( $pick_value );
9042
+ }
9043
 
9044
+ $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = '{$pick_val}' AND {$where} ORDER BY `ID` LIMIT 1", $this );
 
9045
 
9046
+ if ( ! empty( $result ) ) {
9047
+ $pick_values[] = $result[0]->id;
9048
+ }
9049
+ } elseif ( in_array( 'user', array( $pick_object, $related_pod['type'] ) ) ) {
9050
+ $where = "`user_login` = '" . pods_sanitize( $pick_value ) . "'";
9051
 
9052
+ if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode ) {
9053
+ $where = "`ID` = " . pods_absint( $pick_value );
9054
+ }
9055
 
9056
+ $result = pods_query( "SELECT `ID` AS `id` FROM `{$wpdb->users}` WHERE {$where} ORDER BY `ID` LIMIT 1", $this );
9057
 
9058
+ if ( ! empty( $result ) ) {
9059
+ $pick_values[] = $result[0]->id;
9060
+ }
9061
+ } elseif ( in_array( 'comment', array( $pick_object, $related_pod['type'] ) ) ) {
9062
+ $where = "`comment_ID` = " . pods_absint( $pick_value );
9063
 
9064
+ $result = pods_query( "SELECT `comment_ID` AS `id` FROM `{$wpdb->comments}` WHERE {$where} ORDER BY `ID` LIMIT 1", $this );
 
9065
 
9066
+ if ( ! empty( $result ) ) {
9067
+ $pick_values[] = $result[0]->id;
9068
+ }
9069
+ } elseif ( in_array( $pick_object, $simple_tableless_objects ) ) {
9070
+ $pick_values[] = $pick_value;
9071
+ } elseif ( ! empty( $related_pod['id'] ) ) {
9072
+ $where = "`" . $related_pod['field_index'] . "` = '" . pods_sanitize( $pick_value ) . "'";
9073
 
9074
+ if ( 0 < pods_absint( $pick_value ) && false !== $numeric_mode ) {
9075
+ $where = "`" . $related_pod['field_id'] . "` = " . pods_absint( $pick_value );
9076
+ }
9077
 
9078
+ $result = pods_query( "SELECT `" . $related_pod['field_id'] . "` AS `id` FROM `" . $related_pod['table'] . "` WHERE {$where} ORDER BY `" . $related_pod['field_id'] . "` LIMIT 1", $this );
9079
 
9080
+ if ( ! empty( $result ) ) {
9081
+ $pick_values[] = $result[0]->id;
9082
+ }
9083
+ }
9084
+ }
9085
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9086
 
9087
+ $field_value = implode( ',', $pick_values );
9088
+ }
9089
 
9090
+ $data[ $field_name ] = $field_value;
9091
+ }
9092
+ }
9093
 
9094
+ if ( ! empty( $data ) ) {
9095
+ $params = array(
9096
+ 'pod' => $this->pod,
9097
+ 'data' => $data
9098
+ );
9099
 
9100
+ $ids[] = $this->save_pod_item( $params );
9101
+ }
9102
+ }
 
 
9103
 
9104
+ return $ids;
9105
+ }
9106
 
9107
+ /**
9108
+ * Export data from a Pod
9109
+ *
9110
+ * @param string|object $pod The pod name or Pods object
9111
+ * @param array $params An associative array of parameters
9112
+ *
9113
+ * @return array Data arrays of all exported pod items
9114
+ * @since 1.7.1
9115
+ */
9116
+ public function export( $pod = null, $params = null ) {
9117
 
9118
+ if ( empty( $pod ) ) {
9119
+ $pod = $this->pod;
9120
+ }
9121
 
9122
+ $find = array(
9123
+ 'limit' => - 1,
9124
+ 'search' => false,
9125
+ 'pagination' => false
 
 
 
9126
  );
9127
 
9128
+ if ( ! empty( $params ) && isset( $params['params'] ) ) {
9129
+ $find = array_merge( $find, (array) $params['params'] );
 
 
9130
 
9131
+ unset( $params['params'] );
9132
 
9133
+ $pod = pods( $pod, $find );
9134
+ } elseif ( ! is_object( $pod ) ) {
9135
+ $pod = pods( $pod, $find );
9136
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9137
 
9138
+ $data = array();
 
9139
 
9140
+ while ( $pod->fetch() ) {
9141
+ $data[ $pod->id() ] = $this->export_pod_item( $params, $pod );
9142
+ }
9143
 
9144
+ $data = $this->do_hook( 'export', $data, $pod->pod, $pod );
9145
 
9146
+ return $data;
9147
+ }
9148
 
9149
+ /**
9150
+ * Convert CSV to a PHP array
9151
+ *
9152
+ * @param string $data The CSV input
9153
+ *
9154
+ * @return array
9155
+ * @since 1.7.1
9156
+ *
9157
+ * @deprecated 2.3.5
9158
+ */
9159
+ public function csv_to_php( $data, $delimiter = ',' ) {
9160
 
9161
+ pods_deprecated( "PodsAPI->csv_to_php", '2.3.5' );
 
9162
 
9163
+ $data = pods_migrate( 'sv', $delimiter, $data )->parse();
9164
 
9165
+ return $data['items'];
9166
+ }
 
9167
 
9168
+ /**
9169
+ * Clear Pod-related cache
9170
+ *
9171
+ * @param array $pod
9172
+ *
9173
+ * @return void
9174
+ *
9175
+ * @since 2.0.0
9176
+ */
9177
+ public function cache_flush_pods( $pod = null ) {
9178
 
9179
+ /**
9180
+ * @var $wpdb wpdb
9181
+ */
9182
+ global $wpdb;
9183
 
9184
+ pods_transient_clear( 'pods' );
9185
+ pods_transient_clear( 'pods_components' );
9186
 
9187
+ if ( null !== $pod && is_array( $pod ) ) {
9188
+ pods_transient_clear( 'pods_pod_' . $pod['name'] );
9189
+ pods_cache_clear( $pod['name'], 'pods-class' );
9190
 
9191
+ foreach ( $pod['fields'] as $field ) {
9192
+ pods_transient_clear( 'pods_field_' . $pod['name'] . '_' . $field['name'] );
9193
+ }
9194
 
9195
+ if ( in_array( $pod['type'], array( 'post_type', 'taxonomy' ) ) ) {
9196
+ pods_transient_clear( 'pods_wp_cpt_ct' );
9197
  }
9198
+ } else {
9199
+ pods_transient_clear( 'pods_wp_cpt_ct' );
9200
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9201
 
9202
+ // Delete transients in the database
9203
+ $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_pods%'" );
9204
+ $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_timeout_pods%'" );
9205
 
9206
+ // Delete Pods Options Cache in the database
9207
+ $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_pods_option_%'" );
9208
+
9209
+ pods_cache_clear( true );
9210
+
9211
+ pods_transient_set( 'pods_flush_rewrites', 1 );
9212
+
9213
+ do_action( 'pods_cache_flushed' );
9214
+ }
9215
 
9216
+ /**
9217
+ * Process a Pod-based form
9218
+ *
9219
+ * @param mixed $params
9220
+ * @param object $obj Pod object
9221
+ * @param array $fields Fields being submitted in form ( key => settings )
9222
+ * @param string $thank_you URL to send to upon success
9223
+ *
9224
+ * @return mixed
9225
+ *
9226
+ * @since 2.0.0
9227
+ */
9228
+ public function process_form( $params, $obj = null, $fields = null, $thank_you = null ) {
9229
 
9230
+ $this->display_errors = false;
 
 
 
 
 
9231
 
9232
+ $form = null;
 
 
9233
 
9234
+ $nonce = pods_var( '_pods_nonce', $params );
9235
+ $pod = pods_var( '_pods_pod', $params );
9236
+ $id = pods_var( '_pods_id', $params );
9237
+ $uri = pods_var( '_pods_uri', $params );
9238
+ $form = pods_var( '_pods_form', $params );
9239
+ $location = pods_var( '_pods_location', $params );
9240
 
9241
+ if ( is_object( $obj ) ) {
9242
+ $pod = $obj->pod;
9243
+ $id = $obj->id();
9244
+ }
9245
 
9246
+ if ( ! empty( $fields ) ) {
9247
+ $fields = array_keys( $fields );
9248
+ $form = implode( ',', $fields );
9249
+ } else {
9250
+ $fields = explode( ',', $form );
9251
+ }
9252
 
9253
+ if ( empty( $nonce ) || empty( $pod ) || empty( $uri ) || empty( $fields ) ) {
9254
+ return pods_error( __( 'Invalid submission', 'pods' ), $this );
9255
+ }
9256
 
9257
+ $uid = pods_session_id();
 
 
9258
 
9259
+ if ( is_user_logged_in() ) {
9260
+ $uid = 'user_' . get_current_user_id();
9261
+ }
 
9262
 
9263
+ $field_hash = wp_create_nonce( 'pods_fields_' . $form );
9264
 
9265
+ $action = 'pods_form_' . $pod . '_' . $uid . '_' . $id . '_' . $uri . '_' . $field_hash;
 
9266
 
9267
+ if ( empty( $uid ) ) {
9268
+ return pods_error( __( 'Access denied for your session, please refresh and try again.', 'pods' ), $this );
9269
+ }
9270
 
9271
+ if ( false === wp_verify_nonce( $nonce, $action ) ) {
9272
+ return pods_error( __( 'Access denied, please refresh and try again.', 'pods' ), $this );
9273
+ }
9274
 
9275
+ $data = array();
9276
 
9277
+ foreach ( $fields as $field ) {
9278
+ $data[ $field ] = pods_var_raw( 'pods_field_' . $field, $params, '' );
9279
+ }
9280
 
9281
+ $params = array(
9282
+ 'pod' => $pod,
9283
+ 'id' => $id,
9284
+ 'data' => $data,
9285
+ 'from' => 'process_form',
9286
+ 'location' => $location
9287
+ );
 
 
 
 
 
 
 
 
9288
 
9289
+ $id = $this->save_pod_item( $params );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9290
 
9291
+ /**
9292
+ * Fires after the form has been processed and save_pod_item has run.
9293
+ *
9294
+ * @param int $id Item ID.
9295
+ * @param array $params save_pod_item parameters.
9296
+ * @param null|Pods $obj Pod object (if set).
9297
+ */
9298
+ do_action( 'pods_api_processed_form', $id, $params, $obj );
9299
 
9300
+ // Always return $id for AJAX requests.
9301
+ if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
9302
+ if ( 0 < $id && ! empty( $thank_you ) ) {
9303
+ $thank_you = str_replace( 'X_ID_X', $id, $thank_you );
9304
 
9305
+ pods_redirect( $thank_you, 302, false );
9306
+ }
9307
+ }
 
 
 
9308
 
9309
+ return $id;
9310
+ }
 
9311
 
9312
+ /**
9313
+ * Handle filters / actions for the class
9314
+ *
9315
+ * @since 2.0.0
9316
+ */
9317
+ private function do_hook() {
9318
 
9319
+ $args = func_get_args();
9320
+ if ( empty( $args ) ) {
9321
+ return false;
9322
+ }
9323
+ $name = array_shift( $args );
9324
 
9325
+ return pods_do_hook( "api", $name, $args, $this );
9326
+ }
9327
 
9328
+ /**
9329
+ * Handle variables that have been deprecated
9330
+ *
9331
+ * @since 2.0.0
9332
+ */
9333
+ public function __get( $name ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9334
 
9335
+ $name = (string) $name;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9336
 
9337
+ if ( ! isset( $this->deprecated ) ) {
9338
+ require_once( PODS_DIR . 'deprecated/classes/PodsAPI.php' );
9339
+ $this->deprecated = new PodsAPI_Deprecated( $this );
9340
+ }
 
 
 
 
 
9341
 
9342
+ $var = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9343
 
9344
+ if ( isset( $this->deprecated->{$name} ) ) {
9345
+ pods_deprecated( "PodsAPI->{$name}", '2.0' );
9346
 
9347
+ $var = $this->deprecated->{$name};
9348
+ } else {
9349
+ pods_deprecated( "PodsAPI->{$name}", '2.0' );
9350
+ }
 
9351
 
9352
+ return $var;
9353
+ }
9354
 
9355
+ /**
9356
+ * Handle methods that have been deprecated
9357
+ *
9358
+ * @since 2.0.0
9359
+ */
9360
+ public function __call( $name, $args ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9361
 
9362
+ $name = (string) $name;
9363
 
9364
+ if ( ! isset( $this->deprecated ) ) {
9365
+ require_once( PODS_DIR . 'deprecated/classes/PodsAPI.php' );
9366
+ $this->deprecated = new PodsAPI_Deprecated( $this );
9367
+ }
9368
 
9369
+ if ( method_exists( $this->deprecated, $name ) ) {
9370
+ return call_user_func_array( array( $this->deprecated, $name ), $args );
9371
+ } else {
9372
+ pods_deprecated( "PodsAPI::{$name}", '2.0' );
9373
+ }
9374
+ }
9375
 
9376
+ /**
9377
+ * Filter an array of arrays without causing PHP notices/warnings.
9378
+ *
9379
+ * @param array $values
9380
+ *
9381
+ * @return array
9382
+ *
9383
+ * @since 2.6.10
9384
+ */
9385
+ private function array_filter_walker( $values = array() ) {
9386
+
9387
+ $values = (array) $values;
9388
+
9389
+ foreach ( $values as $k => $v ) {
9390
+ if ( is_object( $v ) ) {
9391
+ // Skip objects
9392
+ continue;
9393
+ } elseif ( is_array( $v ) ) {
9394
+ if ( empty( $v ) ) {
9395
+ // Filter values with empty arrays
9396
+ unset( $values[ $k ] );
9397
+ }
9398
+ } else {
9399
+ if ( ! $v ) {
9400
+ // Filter empty values
9401
+ unset( $values[ $k ] );
9402
+ }
9403
+ }
9404
+ }
9405
 
9406
+ return $values;
9407
 
9408
+ }
 
 
9409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9410
  }
classes/PodsAdmin.php CHANGED
@@ -1,2233 +1,3352 @@
1
  <?php
 
2
  /**
3
  * @package Pods
4
  */
5
  class PodsAdmin {
6
 
7
- /**
8
- * @var PodsAdmin
9
- */
10
- static $instance = null;
11
-
12
- /**
13
- * Singleton handling for a basic pods_admin() request
14
- *
15
- * @return \PodsAdmin
16
- *
17
- * @since 2.3.5
18
- */
19
- public static function init () {
20
- if ( !is_object( self::$instance ) )
21
- self::$instance = new PodsAdmin();
22
-
23
- return self::$instance;
24
- }
25
-
26
- /**
27
- * Setup and Handle Admin functionality
28
- *
29
- * @return \PodsAdmin
30
- *
31
- * @license http://www.gnu.org/licenses/gpl-2.0.html
32
- * @since 2.0
33
- */
34
- public function __construct () {
35
- // Scripts / Stylesheets
36
- add_action( 'admin_enqueue_scripts', array( $this, 'admin_head' ), 20 );
37
-
38
- // AJAX $_POST fix
39
- add_action( 'admin_init', array( $this, 'admin_init' ), 9 );
40
-
41
- // Menus
42
- add_action( 'admin_menu', array( $this, 'admin_menu' ), 99 );
43
-
44
- // AJAX for Admin
45
- add_action( 'wp_ajax_pods_admin', array( $this, 'admin_ajax' ) );
46
- add_action( 'wp_ajax_nopriv_pods_admin', array( $this, 'admin_ajax' ) );
47
-
48
- // Add Media Bar button for Shortcode
49
- add_action( 'media_buttons', array( $this, 'media_button' ), 12 );
50
-
51
- // Add the Pods capabilities
52
- add_filter( 'members_get_capabilities', array( $this, 'admin_capabilities' ) );
53
-
54
- add_action( 'admin_head-media-upload-popup', array( $this, 'register_media_assets' ) );
55
- }
56
-
57
- /**
58
- * Init the admin area
59
- *
60
- * @since 2.0
61
- */
62
- public function admin_init () {
63
- // Fix for plugins that *don't do it right* so we don't cause issues for users
64
- if ( defined( 'DOING_AJAX' ) && !empty( $_POST ) ) {
65
- $pods_admin_ajax_actions = array(
66
- 'pods_admin',
67
- 'pods_relationship',
68
- 'pods_upload',
69
- 'pods_admin_components'
70
- );
71
-
72
- $pods_admin_ajax_actions = apply_filters( 'pods_admin_ajax_actions', $pods_admin_ajax_actions );
73
-
74
- if ( in_array( pods_var( 'action', 'get' ), $pods_admin_ajax_actions ) || in_array( pods_var( 'action', 'post' ), $pods_admin_ajax_actions ) ) {
75
- foreach ( $_POST as $key => $value ) {
76
- if ( 'action' == $key || 0 === strpos( $key, '_podsfix_' ) )
77
- continue;
78
-
79
- unset( $_POST[ $key ] );
80
-
81
- $_POST[ '_podsfix_' . $key ] = $value;
82
- }
83
- }
84
- }
85
- }
86
-
87
- /**
88
- * Attach requirements to admin header
89
- *
90
- * @since 2.0
91
- */
92
- public function admin_head () {
93
- wp_register_style( 'pods-admin', PODS_URL . 'ui/css/pods-admin.css', array(), PODS_VERSION );
94
-
95
- wp_register_script( 'pods-floatmenu', PODS_URL . 'ui/js/floatmenu.js', array(), PODS_VERSION );
96
-
97
- wp_register_script( 'pods-admin-importer', PODS_URL . 'ui/js/admin-importer.js', array(), PODS_VERSION );
98
-
99
- wp_register_style( 'pods-manage', PODS_URL . 'ui/css/pods-manage.css', array(), PODS_VERSION );
100
-
101
- wp_register_style( 'pods-wizard', PODS_URL . 'ui/css/pods-wizard.css', array(), PODS_VERSION );
102
-
103
- wp_register_script( 'pods-upgrade', PODS_URL . 'ui/js/jquery.pods.upgrade.js', array(), PODS_VERSION );
104
-
105
- wp_register_script( 'pods-migrate', PODS_URL . 'ui/js/jquery.pods.migrate.js', array(), PODS_VERSION );
106
-
107
- if ( isset( $_GET[ 'page' ] ) ) {
108
- $page = $_GET[ 'page' ];
109
- if ( 'pods' == $page || ( false !== strpos( $page, 'pods-' ) && 0 === strpos( $page, 'pods-' ) ) ) {
110
- ?>
111
- <script type="text/javascript">
112
- var PODS_URL = "<?php echo PODS_URL; ?>";
113
- </script>
114
- <?php
115
- wp_enqueue_script( 'jquery' );
116
- wp_enqueue_script( 'jquery-ui-core' );
117
- wp_enqueue_script( 'jquery-ui-sortable' );
118
-
119
- wp_enqueue_style( 'jquery-ui' );
120
-
121
- wp_enqueue_script( 'pods-floatmenu' );
122
-
123
- wp_enqueue_style( 'pods-qtip' );
124
- wp_enqueue_script( 'jquery-qtip' );
125
- wp_enqueue_script( 'pods-qtip-init' );
126
-
127
- wp_enqueue_script( 'pods' );
128
-
129
- if ( 0 === strpos( $page, 'pods-manage-' ) || 0 === strpos( $page, 'pods-add-new-' ) )
130
- wp_enqueue_script( 'post' );
131
- elseif ( 0 === strpos( $page, 'pods-settings-' ) ) {
132
- wp_enqueue_script( 'post' );
133
- wp_enqueue_style( 'pods-admin' );
134
- }
135
- else
136
- wp_enqueue_style( 'pods-admin' );
137
-
138
- if ( 'pods-advanced' == $page ) {
139
- wp_register_style( 'pods-advanced', PODS_URL . 'ui/css/pods-advanced.css', array(), '1.0' );
140
- wp_enqueue_style( 'pods-advanced' );
141
-
142
- wp_enqueue_script( 'jquery-ui-effects-core', PODS_URL . 'ui/js/jquery-ui/jquery.effects.core.js', array( 'jquery' ), '1.8.8' );
143
- wp_enqueue_script( 'jquery-ui-effects-fade', PODS_URL . 'ui/js/jquery-ui/jquery.effects.fade.js', array( 'jquery' ), '1.8.8' );
144
- wp_enqueue_script( 'jquery-ui-dialog' );
145
-
146
- wp_register_script( 'pods-advanced', PODS_URL . 'ui/js/advanced.js', array(), PODS_VERSION );
147
- wp_enqueue_script( 'pods-advanced' );
148
- }
149
- elseif ( 'pods-packages' == $page )
150
- wp_enqueue_style( 'pods-wizard' );
151
- elseif ( 'pods-wizard' == $page || 'pods-upgrade' == $page || ( in_array( $page, array( 'pods', 'pods-add-new' ) ) && in_array( pods_var( 'action', 'get', 'manage' ), array( 'add', 'manage' ) ) ) ) {
152
- wp_enqueue_style( 'pods-wizard' );
153
-
154
- if ( 'pods-upgrade' == $page )
155
- wp_enqueue_script( 'pods-upgrade' );
156
- }
157
- }
158
- }
159
- }
160
-
161
- /**
162
- * Build the admin menus
163
- *
164
- * @since 2.0
165
- */
166
- public function admin_menu () {
167
- $advanced_content_types = PodsMeta::$advanced_content_types;
168
- $taxonomies = PodsMeta::$taxonomies;
169
- $settings = PodsMeta::$settings;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
- $all_pods = pods_api()->load_pods( array( 'count' => true ) );
 
 
 
 
 
 
 
 
172
 
173
- if ( !PodsInit::$upgrade_needed || ( pods_is_admin() && 1 == pods_var( 'pods_upgrade_bypass' ) ) ) {
174
- $submenu_items = array();
 
 
 
 
 
 
 
 
 
 
175
 
176
- if ( !empty( $advanced_content_types ) ) {
177
- $submenu = array();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
- $pods_pages = 0;
 
180
 
181
- foreach ( (array) $advanced_content_types as $pod ) {
182
- if ( !isset( $pod[ 'name' ] ) || !isset( $pod[ 'options' ] ) || empty( $pod[ 'fields' ] ) )
183
- continue;
184
- elseif ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod[ 'name' ], 'pods_edit_' . $pod[ 'name' ], 'pods_delete_' . $pod[ 'name' ] ) ) )
185
- continue;
186
 
187
- if ( 1 == pods_var( 'show_in_menu', $pod[ 'options' ], 0 ) ) {
188
- $page_title = pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true );
189
- $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
190
 
191
- $menu_label = pods_var_raw( 'menu_name', $pod[ 'options' ], $page_title, null, true );
192
- $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
 
 
 
 
 
 
193
 
194
- $singular_label = pods_var_raw( 'label_singular', $pod[ 'options' ], pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true ), null, true );
195
- $plural_label = pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true );
196
 
197
- $menu_location = pods_var( 'menu_location', $pod[ 'options' ], 'objects' );
198
- $menu_location_custom = pods_var( 'menu_location_custom', $pod[ 'options' ], '' );
199
 
200
- $menu_position = pods_var_raw( 'menu_position', $pod[ 'options' ], '', null, true );
201
- $menu_icon = pods_evaluate_tags( pods_var_raw( 'menu_icon', $pod[ 'options' ], '', null, true ), true );
202
 
203
- if ( empty( $menu_position ) )
204
- $menu_position = null;
205
-
206
- $parent_page = null;
207
-
208
- if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ], 'pods_delete_' . $pod[ 'name' ] ) ) ) {
209
- if ( !empty( $menu_location_custom ) ) {
210
- if ( !isset( $submenu_items[ $menu_location_custom ] ) )
211
- $submenu_items[ $menu_location_custom ] = array();
212
 
213
- $submenu_items[ $menu_location_custom ][] = array( $menu_location_custom, $page_title, $menu_label, 'read', 'pods-manage-' . $pod[ 'name' ], array( $this, 'admin_content' ) );
 
 
214
 
215
- continue;
216
- }
217
- else {
218
- $pods_pages++;
219
 
220
- $parent_page = $page = 'pods-manage-' . $pod[ 'name' ];
221
 
222
- if ( empty( $menu_position ) )
223
- add_object_page( $page_title, $menu_label, 'read', $parent_page, '', $menu_icon );
224
- else
225
- add_menu_page( $page_title, $menu_label, 'read', $parent_page, '', $menu_icon, $menu_position );
226
 
227
- $all_title = $plural_label;
228
- $all_label = __( 'All', 'pods' ) . ' ' . $plural_label;
 
229
 
230
- if ( $page == pods_var( 'page', 'get' ) ) {
231
- if ( 'edit' == pods_var( 'action', 'get', 'manage' ) )
232
- $all_title = __( 'Edit', 'pods' ) . ' ' . $singular_label;
233
- elseif ( 'add' == pods_var( 'action', 'get', 'manage' ) )
234
- $all_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
235
- }
236
 
237
- add_submenu_page( $parent_page, $all_title, $all_label, 'read', $page, array( $this, 'admin_content' ) );
238
- }
239
- }
240
 
241
- if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod[ 'name' ] ) ) ) {
242
- $page = 'pods-add-new-' . $pod[ 'name' ];
243
 
244
- if ( null === $parent_page ) {
245
- $pods_pages++;
 
246
 
247
- $parent_page = $page;
248
 
249
- if ( empty( $menu_position ) )
250
- add_object_page( $page_title, $menu_label, 'read', $parent_page, '', $menu_icon );
251
- else
252
- add_menu_page( $page_title, $menu_label, 'read', $parent_page, '', $menu_icon, $menu_position );
253
- }
254
 
255
- $add_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
256
- $add_label = __( 'Add New', 'pods' );
257
 
258
- add_submenu_page( $parent_page, $add_title, $add_label, 'read', $page, array( $this, 'admin_content' ) );
259
- }
260
- }
261
- else
262
- $submenu[] = $pod;
263
- }
264
 
265
- $submenu = apply_filters( 'pods_admin_menu_secondary_content', $submenu );
 
266
 
267
- if ( !empty( $submenu ) && ( !defined( 'PODS_DISABLE_CONTENT_MENU' ) || !PODS_DISABLE_CONTENT_MENU ) ) {
268
- $parent_page = null;
269
 
270
- foreach ( $submenu as $item ) {
271
- $singular_label = pods_var_raw( 'label_singular', $item[ 'options' ], pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true ), null, true );
272
- $plural_label = pods_var_raw( 'label', $item, ucwords( str_replace( '_', ' ', $item[ 'name' ] ) ), null, true );
273
 
274
- if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $item[ 'name' ], 'pods_delete_' . $item[ 'name' ] ) ) ) {
275
- $page = 'pods-manage-' . $item[ 'name' ];
276
 
277
- if ( null === $parent_page ) {
278
- $parent_page = $page;
279
 
280
- add_menu_page( 'Pods', 'Pods', 'read', $parent_page, null, PODS_URL . 'ui/images/icon16.png', '58.5' );
281
- }
 
 
 
282
 
283
- $all_title = $plural_label;
284
- $all_label = __( 'Manage', 'pods' ) . ' ' . $plural_label;
285
 
286
- if ( $page == pods_var( 'page', 'get' ) ) {
287
- if ( 'edit' == pods_var( 'action', 'get', 'manage' ) )
288
- $all_title = __( 'Edit', 'pods' ) . ' ' . $singular_label;
289
- elseif ( 'add' == pods_var( 'action', 'get', 'manage' ) )
290
- $all_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
291
- }
292
 
293
- add_submenu_page( $parent_page, $all_title, $all_label, 'read', $page, array( $this, 'admin_content' ) );
294
- }
295
- elseif ( current_user_can( 'pods_add_' . $item[ 'name' ] ) ) {
296
- $page = 'pods-add-new-' . $item[ 'name' ];
297
-
298
- if ( null === $parent_page ) {
299
- $parent_page = $page;
300
-
301
- add_menu_page( 'Pods', 'Pods', 'read', $parent_page, null, PODS_URL . 'ui/images/icon16.png', '58.5' );
302
- }
303
-
304
- $add_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
305
- $add_label = __( 'Manage', 'pods' ) . ' ' . $plural_label;
306
-
307
- add_submenu_page( $parent_page, $add_title, $add_label, 'read', $page, array( $this, 'admin_content' ) );
308
- }
309
- }
310
- }
311
- }
312
-
313
- if ( !empty( $taxonomies ) ) {
314
- foreach ( (array) $taxonomies as $pod ) {
315
- if ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ] ) ) )
316
- continue;
317
-
318
- $page_title = pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true );
319
- $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
320
-
321
- $menu_label = pods_var_raw( 'menu_name', $pod[ 'options' ], $page_title, null, true );
322
- $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
323
-
324
- $menu_position = pods_var_raw( 'menu_position', $pod[ 'options' ], '', null, true );
325
- $menu_icon = pods_evaluate_tags( pods_var_raw( 'menu_icon', $pod[ 'options' ], '', null, true ), true );
326
-
327
- if ( empty( $menu_position ) )
328
- $menu_position = null;
329
-
330
- $menu_slug = 'edit-tags.php?taxonomy=' . $pod[ 'name' ];
331
- $menu_location = pods_var( 'menu_location', $pod[ 'options' ], 'default' );
332
- $menu_location_custom = pods_var( 'menu_location_custom', $pod[ 'options' ], '' );
333
-
334
- if ( 'default' == $menu_location )
335
- continue;
336
-
337
- $taxonomy_data = get_taxonomy( $pod[ 'name' ] );
338
-
339
- foreach ( (array) $taxonomy_data->object_type as $post_type ) {
340
- if ( 'post' == $post_type )
341
- remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=' . $pod[ 'name' ] );
342
- elseif ( 'attachment' == $post_type )
343
- remove_submenu_page( 'upload.php', 'edit-tags.php?taxonomy=' . $pod[ 'name' ] . '&amp;post_type=' . $post_type );
344
- else
345
- remove_submenu_page( 'edit.php?post_type=' . $post_type, 'edit-tags.php?taxonomy=' . $pod[ 'name' ] . '&amp;post_type=' . $post_type );
346
- }
347
-
348
- if ( 'settings' == $menu_location )
349
- add_options_page( $page_title, $menu_label, 'read', $menu_slug );
350
- elseif ( 'appearances' == $menu_location )
351
- add_theme_page( $page_title, $menu_label, 'read', $menu_slug );
352
- elseif ( 'objects' == $menu_location ) {
353
- if ( empty( $menu_position ) )
354
- add_object_page( $page_title, $menu_label, 'read', $menu_slug, '', $menu_icon );
355
- else
356
- add_menu_page( $page_title, $menu_label, 'read', $menu_slug, '', $menu_icon, $menu_position );
357
- }
358
- elseif ( 'top' == $menu_location )
359
- add_menu_page( $page_title, $menu_label, 'read', $menu_slug, '', $menu_icon, $menu_position );
360
- elseif ( 'submenu' == $menu_location && !empty( $menu_location_custom ) ) {
361
- if ( !isset( $submenu_items[ $menu_location_custom ] ) )
362
- $submenu_items[ $menu_location_custom ] = array();
363
-
364
- $submenu_items[ $menu_location_custom ][] = array( $menu_location_custom, $page_title, $menu_label, 'read', $menu_slug, '' );
365
- }
366
- }
367
- }
368
-
369
- if ( !empty( $settings ) ) {
370
- foreach ( (array) $settings as $pod ) {
371
- if ( !pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod[ 'name' ] ) ) )
372
- continue;
373
-
374
- $page_title = pods_var_raw( 'label', $pod, ucwords( str_replace( '_', ' ', $pod[ 'name' ] ) ), null, true );
375
- $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
376
-
377
- $menu_label = pods_var_raw( 'menu_name', $pod[ 'options' ], $page_title, null, true );
378
- $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
379
-
380
- $menu_position = pods_var_raw( 'menu_position', $pod[ 'options' ], '', null, true );
381
- $menu_icon = pods_evaluate_tags( pods_var_raw( 'menu_icon', $pod[ 'options' ], '', null, true ), true );
382
-
383
- if ( empty( $menu_position ) )
384
- $menu_position = null;
385
-
386
- $menu_slug = 'pods-settings-' . $pod[ 'name' ];
387
- $menu_location = pods_var( 'menu_location', $pod[ 'options' ], 'settings' );
388
- $menu_location_custom = pods_var( 'menu_location_custom', $pod[ 'options' ], '' );
389
-
390
- if ( 'settings' == $menu_location )
391
- add_options_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ) );
392
- elseif ( 'appearances' == $menu_location )
393
- add_theme_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ) );
394
- elseif ( 'objects' == $menu_location ) {
395
- if ( empty( $menu_position ) )
396
- add_object_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ), $menu_icon );
397
- else
398
- add_menu_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ), $menu_icon, $menu_position );
399
- }
400
- elseif ( 'top' == $menu_location )
401
- add_menu_page( $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ), $menu_icon, $menu_position );
402
- elseif ( 'submenu' == $menu_location && !empty( $menu_location_custom ) ) {
403
- if ( !isset( $submenu_items[ $menu_location_custom ] ) )
404
- $submenu_items[ $menu_location_custom ] = array();
405
-
406
- $submenu_items[ $menu_location_custom ][] = array( $menu_location_custom, $page_title, $menu_label, 'read', $menu_slug, array( $this, 'admin_content_settings' ) );
407
- }
408
- }
409
- }
410
-
411
- foreach ( $submenu_items as $items ) {
412
- foreach ( $items as $item ) {
413
- call_user_func_array( 'add_submenu_page', $item );
414
- }
415
- }
416
-
417
- $admin_menus = array(
418
- 'pods' => array(
419
- 'label' => __( 'Edit Pods', 'pods' ),
420
- 'function' => array( $this, 'admin_setup' ),
421
- 'access' => 'pods'
422
- ),
423
- 'pods-add-new' => array(
424
- 'label' => __( 'Add New', 'pods' ),
425
- 'function' => array( $this, 'admin_setup' ),
426
- 'access' => 'pods'
427
- ),
428
- 'pods-components' => array(
429
- 'label' => __( 'Components', 'pods' ),
430
- 'function' => array( $this, 'admin_components' ),
431
- 'access' => 'pods_components'
432
- ),
433
- 'pods-settings' => array(
434
- 'label' => __( 'Settings', 'pods' ),
435
- 'function' => array( $this, 'admin_settings' ),
436
- 'access' => 'pods_settings'
437
- ),
438
- 'pods-help' => array(
439
- 'label' => __( 'Help', 'pods' ),
440
- 'function' => array( $this, 'admin_help' )
441
- )
442
- );
443
-
444
- if ( empty( $all_pods ) )
445
- unset( $admin_menus[ 'pods' ] );
446
-
447
- add_filter( 'parent_file' , array( $this, 'parent_file' ) );
448
- }
449
- else {
450
- $admin_menus = array(
451
- 'pods-upgrade' => array(
452
- 'label' => __( 'Upgrade', 'pods' ),
453
- 'function' => array( $this, 'admin_upgrade' ),
454
- 'access' => 'manage_options'
455
- ),
456
- 'pods-settings' => array(
457
- 'label' => __( 'Settings', 'pods' ),
458
- 'function' => array( $this, 'admin_settings' ),
459
- 'access' => 'pods_settings'
460
- ),
461
- 'pods-help' => array(
462
- 'label' => __( 'Help', 'pods' ),
463
- 'function' => array( $this, 'admin_help' )
464
- )
465
- );
466
-
467
- add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
468
- }
469
 
470
- /**
471
- * Add or change Pods Admin menu items
472
- *
473
- * @params array $admin_menus The submenu items in Pods Admin menu.
474
- *
475
- * @since unknown
476
- */
477
- $admin_menus = apply_filters( 'pods_admin_menu', $admin_menus );
 
478
 
479
- $parent = false;
480
-
481
- if ( !empty( $admin_menus ) && ( !defined( 'PODS_DISABLE_ADMIN_MENU' ) || !PODS_DISABLE_ADMIN_MENU ) ) {
482
- foreach ( $admin_menus as $page => $menu_item ) {
483
- if ( !pods_is_admin( pods_var_raw( 'access', $menu_item ) ) )
484
- continue;
485
-
486
- // Don't just show the help page
487
- if ( false === $parent && 'pods-help' == $page )
488
- continue;
489
-
490
- if ( !isset( $menu_item[ 'label' ] ) )
491
- $menu_item[ 'label' ] = $page;
492
-
493
- if ( false === $parent ) {
494
- $parent = $page;
495
-
496
- $menu = __( 'Pods Admin', 'pods' );
497
-
498
- if ( 'pods-upgrade' == $parent )
499
- $menu = __( 'Pods Upgrade', 'pods' );
500
-
501
- add_menu_page( $menu, $menu, 'read', $parent, null, PODS_URL . 'ui/images/icon16.png' );
502
- }
503
-
504
- add_submenu_page( $parent, $menu_item[ 'label' ], $menu_item[ 'label' ], 'read', $page, $menu_item[ 'function' ] );
505
-
506
- if ( 'pods-components' == $page )
507
- PodsInit::$components->menu( $parent );
508
- }
509
- }
510
- }
511
-
512
- /**
513
- * Set the correct parent_file to highlight the correct top level menu
514
- */
515
- public function parent_file ( $parent_file ) {
516
- global $current_screen;
517
-
518
- if ( isset( $current_screen ) && ! empty( $current_screen->taxonomy ) ) {
519
- $taxonomies = PodsMeta::$taxonomies;
520
- if ( !empty( $taxonomies ) ) {
521
- foreach ( (array) $taxonomies as $pod ) {
522
- if ( $current_screen->taxonomy !== $pod[ 'name' ] )
523
- continue;
524
-
525
- $menu_slug = 'edit-tags.php?taxonomy=' . $pod[ 'name' ];
526
- $menu_location = pods_var( 'menu_location', $pod[ 'options' ], 'default' );
527
- $menu_location_custom = pods_var( 'menu_location_custom', $pod[ 'options' ], '' );
528
-
529
- if ( 'settings' == $menu_location )
530
- $parent_file = 'options-general.php';
531
- elseif ( 'appearances' == $menu_location )
532
- $parent_file = 'themes.php';
533
- elseif ( 'objects' == $menu_location )
534
- $parent_file = $menu_slug;
535
- elseif ( 'top' == $menu_location )
536
- $parent_file = $menu_slug;
537
- elseif ( 'submenu' == $menu_location && !empty( $menu_location_custom ) ) {
538
- $parent_file = $menu_location_custom;
539
- }
540
-
541
- break;
542
- }
543
- }
544
- }
545
-
546
- if ( isset( $current_screen ) && ! empty( $current_screen->post_type ) ) {
547
- global $submenu_file;
548
- $components = PodsInit::$components->components;
549
- foreach ( $components as $component => $component_data ) {
550
- if ( ! empty( $component_data[ 'MenuPage' ] ) && $parent_file === $component_data[ 'MenuPage' ] ) {
551
- $parent_file = 'pods';
552
- $submenu_file = $component_data[ 'MenuPage' ];
553
- }
554
- }
555
- }
556
-
557
- return $parent_file;
558
- }
559
-
560
- public function upgrade_notice () {
561
- echo '<div class="error fade"><p>';
562
- echo sprintf(
563
- __( '<strong>NOTICE:</strong> Pods %s requires your action to complete the upgrade. Please run the <a href="%s">Upgrade Wizard</a>.', 'pods' ),
564
- PODS_VERSION,
565
- admin_url( 'admin.php?page=pods-upgrade' )
566
- );
567
- echo '</p></div>';
568
- }
569
-
570
- /**
571
- * Create PodsUI content for the administration pages
572
- */
573
- public function admin_content () {
574
- $pod_name = str_replace( array( 'pods-manage-', 'pods-add-new-' ), '', $_GET[ 'page' ] );
575
-
576
- $pod = pods( $pod_name, pods_var( 'id', 'get', null, null, true ) );
577
-
578
- if ( false !== strpos( $_GET[ 'page' ], 'pods-add-new-' ) )
579
- $_GET[ 'action' ] = pods_var( 'action', 'get', 'add' );
580
-
581
- $pod->ui();
582
- }
583
-
584
- /**
585
- * Create PodsUI content for the settings administration pages
586
- */
587
- public function admin_content_settings () {
588
- $pod_name = str_replace( 'pods-settings-', '', $_GET[ 'page' ] );
589
-
590
- $pod = pods( $pod_name );
591
-
592
- if ( 'custom' != pods_var( 'ui_style', $pod->pod_data[ 'options' ], 'settings', null, true ) ) {
593
- $actions_disabled = array(
594
- 'manage' => 'manage',
595
- 'add' => 'add',
596
- 'delete' => 'delete',
597
- 'duplicate' => 'duplicate',
598
- 'view' => 'view',
599
- 'export' => 'export'
600
- );
601
-
602
- $_GET[ 'action' ] = 'edit';
603
-
604
- $page_title = pods_var_raw( 'label', $pod->pod_data, ucwords( str_replace( '_', ' ', $pod->pod_data[ 'name' ] ) ), null, true );
605
-
606
- $ui = array(
607
- 'pod' => $pod,
608
- 'fields' => array(
609
- 'edit' => $pod->pod_data[ 'fields' ]
610
- ),
611
- 'header' => array(
612
- 'edit' => $page_title
613
- ),
614
- 'label' => array(
615
- 'edit' => __( 'Save Changes', 'pods' )
616
- ),
617
- 'style' => pods_var( 'ui_style', $pod->pod_data[ 'options' ], 'settings', null, true ),
618
- 'icon' => pods_evaluate_tags( pods_var_raw( 'menu_icon', $pod->pod_data[ 'options' ] ), true ),
619
- 'actions_disabled' => $actions_disabled
620
- );
621
-
622
- $ui = apply_filters( 'pods_admin_ui_' . $pod->pod, apply_filters( 'pods_admin_ui', $ui, $pod->pod, $pod ), $pod->pod, $pod );
623
-
624
- // Force disabled actions, do not pass go, do not collect $two_hundred
625
- $ui[ 'actions_disabled' ] = $actions_disabled;
626
-
627
- pods_ui( $ui );
628
- }
629
- else {
630
- do_action( 'pods_admin_ui_custom', $pod );
631
- do_action( 'pods_admin_ui_custom_' . $pod->pod, $pod );
632
- }
633
- }
634
-
635
- /**
636
- * Add media button for Pods shortcode
637
- *
638
- * @param $context
639
- *
640
- * @return string
641
- */
642
- public function media_button ( $context = null ) {
643
 
644
- /**
645
- * Filter to remove Pods shortcode button from the post editor.
646
- *
647
- * @param bool. Set to false to block the shortcode button from appearing.
648
- * @param string $context
649
- *
650
- * @since 2.3.19
651
- */
652
- if ( !apply_filters( 'pods_admin_media_button', true, $context ) ) {
653
- return '';
654
- }
655
 
656
- $current_page = basename( $_SERVER[ 'PHP_SELF' ] );
657
- $current_page = explode( '?', $current_page );
658
- $current_page = explode( '#', $current_page[ 0 ] );
659
- $current_page = $current_page[ 0 ];
660
-
661
- // Only show the button on post type pages
662
- if ( !in_array( $current_page, array( 'post-new.php', 'post.php' ) ) )
663
- return '';
664
-
665
- add_action( 'admin_footer', array( $this, 'mce_popup' ) );
666
-
667
- echo '<a href="#TB_inline?width=640&inlineId=pods_shortcode_form" class="thickbox" id="add_pod_button" title="Pods Shortcode"><img src="' . PODS_URL . 'ui/images/icon16.png" alt="Pods Shortcode" /></a>';
668
- }
669
-
670
- /**
671
- * Enqueue assets for Media Library Popup
672
- */
673
- public function register_media_assets () {
674
- if ( 'pods_media_attachment' == pods_var( 'inlineId', 'get' ) )
675
- wp_enqueue_style( 'pods-attach' );
676
- }
677
-
678
- /**
679
- * Output Pods shortcode popup window
680
- */
681
- public function mce_popup () {
682
- pods_view( PODS_DIR . 'ui/admin/shortcode.php', compact( array_keys( get_defined_vars() ) ) );
683
- }
684
-
685
- /**
686
- * Handle main Pods Setup area for managing Pods and Fields
687
- */
688
- public function admin_setup () {
689
- $pods = pods_api()->load_pods( array( 'fields' => false ) );
690
-
691
- $view = pods_var( 'view', 'get', 'all', null, true );
692
-
693
- if ( empty( $pods ) && !isset( $_GET[ 'action' ] ) )
694
- $_GET[ 'action' ] = 'add';
695
-
696
- if ( 'pods-add-new' == $_GET[ 'page' ] ) {
697
- if ( isset( $_GET[ 'action' ] ) && 'add' != $_GET[ 'action' ] )
698
- pods_redirect( pods_var_update( array( 'page' => 'pods', 'action' => $_GET[ 'action' ] ) ) );
699
- else
700
- $_GET[ 'action' ] = 'add';
701
- }
702
- elseif ( isset( $_GET[ 'action' ] ) && 'add' == $_GET[ 'action' ] )
703
- pods_redirect( pods_var_update( array( 'page' => 'pods-add-new', 'action' => '' ) ) );
704
-
705
- $types = array(
706
- 'post_type' => __( 'Post Type (extended)', 'pods' ),
707
- 'taxonomy' => __( 'Taxonomy (extended)', 'pods' ),
708
- 'cpt' => __( 'Custom Post Type', 'pods' ),
709
- 'ct' => __( 'Custom Taxonomy', 'pods' ),
710
- 'user' => __( 'User (extended)', 'pods' ),
711
- 'media' => __( 'Media (extended)', 'pods' ),
712
- 'comment' => __( 'Comments (extended)', 'pods' ),
713
- 'pod' => __( 'Advanced Content Type', 'pods' ),
714
- 'settings' => __( 'Custom Settings Page', 'pods' )
715
- );
716
-
717
- $row = false;
718
-
719
- $pod_types_found = array();
720
-
721
- $fields = array(
722
- 'label' => array( 'label' => __( 'Label', 'pods' ) ),
723
- 'name' => array( 'label' => __( 'Name', 'pods' ) ),
724
- 'type' => array( 'label' => __( 'Type', 'pods' ) ),
725
- 'storage' => array(
726
- 'label' => __( 'Storage Type', 'pods' ),
727
- 'width' => '10%'
728
- ),
729
- 'field_count' => array(
730
- 'label' => __( 'Number of Fields', 'pods' ),
731
- 'width' => '8%'
732
- )
733
- );
734
-
735
- $total_fields = 0;
736
-
737
- foreach ( $pods as $k => $pod ) {
738
- if ( isset( $types[ $pod[ 'type' ] ] ) ) {
739
- if ( in_array( $pod[ 'type' ], array( 'post_type', 'taxonomy' ) ) ) {
740
- if ( empty( $pod[ 'object' ] ) ) {
741
- if ( 'post_type' == $pod[ 'type' ] )
742
- $pod[ 'type' ] = 'cpt';
743
- else
744
- $pod[ 'type' ] = 'ct';
745
- }
746
- }
747
-
748
- if ( !isset( $pod_types_found[ $pod[ 'type' ] ] ) )
749
- $pod_types_found[ $pod[ 'type' ] ] = 1;
750
- else
751
- $pod_types_found[ $pod[ 'type' ] ]++;
752
-
753
- if ( 'all' != $view && $view != $pod[ 'type' ] ) {
754
- unset( $pods[ $k ] );
755
-
756
- continue;
757
- }
758
-
759
- $pod[ 'real_type' ] = $pod[ 'type' ];
760
- $pod[ 'type' ] = $types[ $pod[ 'type' ] ];
761
- }
762
- elseif ( 'all' != $view )
763
- continue;
764
-
765
- $pod[ 'storage' ] = ucwords( $pod[ 'storage' ] );
766
-
767
- if ( $pod[ 'id' ] == pods_var( 'id' ) && 'delete' != pods_var( 'action' ) )
768
- $row = $pod;
769
-
770
- $pod = array(
771
- 'id' => $pod[ 'id' ],
772
- 'label' => pods_var_raw( 'label', $pod ),
773
- 'name' => pods_var_raw( 'name', $pod ),
774
- 'object' => pods_var_raw( 'object', $pod ),
775
- 'type' => pods_var_raw( 'type', $pod ),
776
- 'real_type' => pods_var_raw( 'real_type', $pod ),
777
- 'storage' => pods_var_raw( 'storage', $pod ),
778
- 'field_count' => count( $pod[ 'fields' ] )
779
- );
780
-
781
- $total_fields += $pod[ 'field_count' ];
782
-
783
- $pods[ $k ] = $pod;
784
- }
785
-
786
- if ( false === $row && 0 < pods_var( 'id' ) && 'delete' != pods_var( 'action' ) ) {
787
- pods_message( 'Pod not found', 'error' );
788
-
789
- unset( $_GET[ 'id' ] );
790
- unset( $_GET[ 'action' ] );
791
- }
792
-
793
- $ui = array(
794
- 'data' => $pods,
795
- 'row' => $row,
796
- 'total' => count( $pods ),
797
- 'total_found' => count( $pods ),
798
- 'icon' => PODS_URL . 'ui/images/icon32.png',
799
- 'items' => 'Pods',
800
- 'item' => 'Pod',
801
- 'fields' => array(
802
- 'manage' => $fields
803
- ),
804
- 'actions_disabled' => array( 'view', 'export' ),
805
- 'actions_custom' => array(
806
- 'add' => array( $this, 'admin_setup_add' ),
807
- 'edit' => array( $this, 'admin_setup_edit' ),
808
- 'duplicate' => array(
809
- 'callback' => array( $this, 'admin_setup_duplicate' ),
810
- 'restrict_callback' => array( $this, 'admin_setup_duplicate_restrict' )
811
- ),
812
- 'reset' => array(
813
- 'label' => __( 'Delete All Items', 'pods' ),
814
- 'confirm' => __( 'Are you sure you want to delete all items from this Pod? If this is an extended Pod, it will remove the original items extended too.', 'pods' ),
815
- 'callback' => array( $this, 'admin_setup_reset' ),
816
- 'restrict_callback' => array( $this, 'admin_setup_reset_restrict' )
817
- ),
818
- 'delete' => array( $this, 'admin_setup_delete' )
819
- ),
820
- 'action_links' => array(
821
- 'add' => pods_var_update( array( 'page' => 'pods-add-new', 'action' => '', 'id' => '', 'do' => '' ) )
822
- ),
823
- 'search' => false,
824
- 'searchable' => false,
825
- 'sortable' => true,
826
- 'pagination' => false,
827
- 'extra' => array(
828
- 'total' => ', ' . number_format_i18n( $total_fields ) . ' ' . _n( 'field', 'fields', $total_fields, 'pods' )
829
- )
830
- );
831
-
832
- if ( 1 < count( $pod_types_found ) ) {
833
- $ui[ 'views' ] = array( 'all' => __( 'All', 'pods' ) );
834
- $ui[ 'view' ] = $view;
835
- $ui[ 'heading' ] = array( 'views' => __( 'Type', 'pods' ) );
836
- $ui[ 'filters_enhanced' ] = true;
837
-
838
- foreach ( $pod_types_found as $pod_type => $number_found ) {
839
- $ui[ 'views' ][ $pod_type ] = $types[ $pod_type ];
840
- }
841
- }
842
-
843
- pods_ui( $ui );
844
- }
845
-
846
- /**
847
- * Get the add page of an object
848
- *
849
- * @param $obj
850
- */
851
- public function admin_setup_add ( $obj ) {
852
- pods_view( PODS_DIR . 'ui/admin/setup-add.php', compact( array_keys( get_defined_vars() ) ) );
853
- }
854
-
855
- /**
856
- * Get the edit page of an object
857
- *
858
- * @param $duplicate
859
- * @param $obj
860
- */
861
- public function admin_setup_edit ( $duplicate, $obj ) {
862
- pods_view( PODS_DIR . 'ui/admin/setup-edit.php', compact( array_keys( get_defined_vars() ) ) );
863
- }
864
-
865
- /**
866
- * Get list of Pod option tabs
867
- *
868
- * @return array
869
- */
870
- public function admin_setup_edit_tabs ( $pod ) {
871
- $fields = true;
872
- $labels = false;
873
- $admin_ui = false;
874
- $advanced = false;
875
-
876
- if ( 'post_type' == pods_var( 'type', $pod ) && strlen( pods_var( 'object', $pod ) ) < 1 ) {
877
- $labels = true;
878
- $admin_ui = true;
879
- $advanced = true;
880
- }
881
- elseif ( 'taxonomy' == pods_var( 'type', $pod ) && strlen( pods_var( 'object', $pod ) ) < 1 ) {
882
- $labels = true;
883
- $admin_ui = true;
884
- $advanced = true;
885
- }
886
- elseif ( 'pod' == pods_var( 'type', $pod ) ) {
887
- $labels = true;
888
- $admin_ui = true;
889
- $advanced = true;
890
- }
891
- elseif ( 'settings' == pods_var( 'type', $pod ) ) {
892
- $labels = true;
893
- $admin_ui = true;
894
- }
895
-
896
- if ( 'none' == pods_var( 'storage', $pod, 'none', null, true ) && 'settings' != pods_var( 'type', $pod ) )
897
- $fields = false;
898
-
899
- $tabs = array();
900
-
901
- if ( $fields )
902
- $tabs[ 'manage-fields' ] = __( 'Manage Fields', 'pods' );
903
-
904
- if ( $labels )
905
- $tabs[ 'labels' ] = __( 'Labels', 'pods' );
906
-
907
- if ( $admin_ui )
908
- $tabs[ 'admin-ui' ] = __( 'Admin UI', 'pods' );
909
-
910
- if ( $advanced )
911
- $tabs[ 'advanced' ] = __( 'Advanced Options', 'pods' );
912
-
913
- if ( 'taxonomy' == pods_var( 'type', $pod ) && !$fields )
914
- $tabs[ 'extra-fields' ] = __( 'Extra Fields', 'pods' );
915
 
916
- $addtl_args = compact( array( 'fields', 'labels', 'admin_ui', 'advanced' ) );
 
 
 
 
 
917
 
918
- /**
919
- * Add or modify tabs in Pods editor for a specific Pod
920
- *
921
- * @params array $tabs Tabs to set.
922
- * @params object $pod Current Pods object
923
- * @params array $addtl_args Additional args.
924
- *
925
- * @since unknown
926
- */
927
- $tabs = apply_filters( 'pods_admin_setup_edit_tabs_' . $pod[ 'type' ] . '_' . $pod[ 'name' ], $tabs, $pod, $addtl_args );
928
 
929
- /**
930
- * Add or modify tabs for any Pod in Pods editor of a specific post type.
931
- */
932
- $tabs = apply_filters( 'pods_admin_setup_edit_tabs_' . $pod[ 'type' ], $tabs, $pod, $addtl_args );
933
 
934
- /**
935
- * Add or modify tabs in Pods editor for all pods.
936
- */
937
- $tabs = apply_filters( 'pods_admin_setup_edit_tabs', $tabs, $pod, $addtl_args );
938
 
939
- return $tabs;
940
- }
941
-
942
- /**
943
- * Get list of Pod options
944
- *
945
- * @return array
946
- */
947
- public function admin_setup_edit_options ( $pod ) {
948
- $options = array();
949
-
950
- // @todo fill this in
951
- $options[ 'labels' ] = array(
952
- 'temporary' => 'This has the fields hardcoded' // :(
953
- );
954
-
955
- if ( 'post_type' == $pod[ 'type' ] ) {
956
- $options[ 'admin-ui' ] = array(
957
- 'description' => array(
958
- 'label' => __( 'Post Type Description', 'pods' ),
959
- 'help' => __( 'A short descriptive summary of what the post type is.', 'pods' ),
960
- 'type' => 'text',
961
- 'default' => ''
962
- ),
963
- 'show_ui' => array(
964
- 'label' => __( 'Show Admin UI', 'pods' ),
965
- 'help' => __( 'help', 'pods' ),
966
- 'type' => 'boolean',
967
- 'default' => pods_var_raw( 'public', $pod, true ),
968
- 'boolean_yes_label' => ''
969
- ),
970
- 'show_in_menu' => array(
971
- 'label' => __( 'Show Admin Menu in Dashboard', 'pods' ),
972
- 'help' => __( 'help', 'pods' ),
973
- 'type' => 'boolean',
974
- 'default' => pods_var_raw( 'public', $pod, true ),
975
- 'dependency' => true,
976
- 'boolean_yes_label' => ''
977
- ),
978
- 'menu_location_custom' => array(
979
- 'label' => __( 'Parent Menu ID (optional)', 'pods' ),
980
- 'help' => __( 'help', 'pods' ),
981
- 'type' => 'text',
982
- 'depends-on' => array( 'show_in_menu' => true )
983
- ),
984
- 'menu_name' => array(
985
- 'label' => __( 'Menu Name', 'pods' ),
986
- 'help' => __( 'help', 'pods' ),
987
- 'type' => 'text',
988
- 'default' => '',
989
- 'depends-on' => array( 'show_in_menu' => true )
990
- ),
991
- 'menu_position' => array(
992
- 'label' => __( 'Menu Position', 'pods' ),
993
- 'help' => __( 'help', 'pods' ),
994
- 'type' => 'number',
995
- 'default' => 0,
996
- 'depends-on' => array( 'show_in_menu' => true )
997
- ),
998
- 'menu_icon' => array(
999
- 'label' => __( 'Menu Icon', 'pods' ),
1000
- 'help' => __( 'URL or Dashicon name for the menu icon. You may specify the path to the icon using one of the <a href="http://pods.io/docs/build/special-magic-tags/#site-tags" target="_blank">site tag</a> type <a href="http://pods.io/docs/build/special-magic-tags/" target="_blank">special magic tags</a>. For example, for a file in your theme directory, use "{@template-url}/path/to/image.png". You may also use the name of a <a href="http://melchoyce.github.io/dashicons/" target="_blank">Dashicon</a>. For example, to use the empty star icon, use "dashicons-star-empty".', 'pods' ),
1001
- 'type' => 'text',
1002
- 'default' => '',
1003
- 'depends-on' => array( 'show_in_menu' => true )
1004
- ),
1005
- 'show_in_nav_menus' => array(
1006
- 'label' => __( 'Show in Navigation Menus', 'pods' ),
1007
- 'help' => __( 'help', 'pods' ),
1008
- 'type' => 'boolean',
1009
- 'default' => true,
1010
- 'boolean_yes_label' => ''
1011
- ),
1012
- 'show_in_admin_bar' => array(
1013
- 'label' => __( 'Show in Admin Bar "New" Menu', 'pods' ),
1014
- 'help' => __( 'help', 'pods' ),
1015
- 'type' => 'boolean',
1016
- 'default' => true,
1017
- 'boolean_yes_label' => ''
1018
- )
1019
- );
1020
-
1021
- $options[ 'advanced' ] = array(
1022
- 'public' => array(
1023
- 'label' => __( 'Public', 'pods' ),
1024
- 'help' => __( 'help', 'pods' ),
1025
- 'type' => 'boolean',
1026
- 'default' => true,
1027
- 'boolean_yes_label' => ''
1028
- ),
1029
- 'publicly_queryable' => array(
1030
- 'label' => __( 'Publicly Queryable', 'pods' ),
1031
- 'help' => __( 'help', 'pods' ),
1032
- 'type' => 'boolean',
1033
- 'default' => pods_var_raw( 'public', $pod, true ),
1034
- 'boolean_yes_label' => ''
1035
- ),
1036
- 'exclude_from_search' => array(
1037
- 'label' => __( 'Exclude from Search', 'pods' ),
1038
- 'help' => __( 'help', 'pods' ),
1039
- 'type' => 'boolean',
1040
- 'default' => !pods_var_raw( 'public', $pod, true ),
1041
- 'boolean_yes_label' => ''
1042
- ),
1043
- 'capability_type' => array(
1044
- 'label' => __( 'User Capability', 'pods' ),
1045
- 'help' => __( 'Uses these capabilties for access to this post type: edit_{capability}, read_{capability}, and delete_{capability}', 'pods' ),
1046
- 'type' => 'pick',
1047
- 'default' => 'post',
1048
- 'data' => array(
1049
- 'post' => 'post',
1050
- 'page' => 'page',
1051
- 'custom' => __( 'Custom Capability', 'pods' )
1052
- ),
1053
- 'dependency' => true
1054
- ),
1055
- 'capability_type_custom' => array(
1056
- 'label' => __( 'Custom User Capability', 'pods' ),
1057
- 'help' => __( 'help', 'pods' ),
1058
- 'type' => 'text',
1059
- 'default' => pods_var_raw( 'name', $pod ),
1060
- 'depends-on' => array( 'capability_type' => 'custom' )
1061
- ),
1062
- 'capability_type_extra' => array(
1063
- 'label' => __( 'Additional User Capabilities', 'pods' ),
1064
- 'help' => __( 'Enables additional capabilities for this Post Type including: delete_{capability}s, delete_private_{capability}s, delete_published_{capability}s, delete_others_{capability}s, edit_private_{capability}s, and edit_published_{capability}s', 'pods' ),
1065
- 'type' => 'boolean',
1066
- 'default' => true,
1067
- 'boolean_yes_label' => ''
1068
- ),
1069
- 'has_archive' => array(
1070
- 'label' => __( 'Enable Archive Page', 'pods' ),
1071
- 'help' => __( 'If enabled, creates an archive page with list of of items in this custom post type. Functions like a category page for posts. Can be controlled with a template in your theme called "archive-{$post-type}.php".', 'pods' ),
1072
- 'type' => 'boolean',
1073
- 'default' => false,
1074
- 'dependency' => true,
1075
- 'boolean_yes_label' => ''
1076
- ),
1077
- 'has_archive_slug' => array(
1078
- 'label' => __( 'Archive Page Slug Override', 'pods' ),
1079
- 'help' => __( 'If archive page is enabled, you can override the slug used by WordPress, which defaults to the name of the post type.', 'pods' ),
1080
- 'type' => 'text',
1081
- 'default' => '',
1082
- 'depends-on' => array( 'has_archive' => true )
1083
- ),
1084
- 'hierarchical' => array(
1085
- 'label' => __( 'Hierarchical', 'pods' ),
1086
- 'help' => __( 'Allows for parent/ child relationships between items, just like with Pages. Note: To edit relationships in the post editor, you must enable "Page Attributes" in the "Supports" section below.', 'pods' ),
1087
- 'type' => 'boolean',
1088
- 'default' => false,
1089
- 'dependency' => true,
1090
- 'boolean_yes_label' => ''
1091
- ),
1092
- 'label_parent_item_colon' => array(
1093
- 'label' => __( '<strong>Label: </strong> Parent <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1094
- 'help' => __( 'help', 'pods' ),
1095
- 'type' => 'text',
1096
- 'default' => '',
1097
- 'depends-on' => array( 'hierarchical' => true )
1098
- ),
1099
- 'label_parent' => array(
1100
- 'label' => __( '<strong>Label: </strong> Parent', 'pods' ),
1101
- 'help' => __( 'help', 'pods' ),
1102
- 'type' => 'text',
1103
- 'default' => '',
1104
- 'depends-on' => array( 'hierarchical' => true )
1105
- ),
1106
- 'rewrite' => array(
1107
- 'label' => __( 'Rewrite', 'pods' ),
1108
- 'help' => __( 'Allows you to use pretty permalinks, if set in WordPress Settings->Reading. If not enbabled, your links will be in the form of "example.com/?pod_name=post_slug" regardless of your permalink settings.', 'pods' ),
1109
- 'type' => 'boolean',
1110
- 'default' => true,
1111
- 'dependency' => true,
1112
- 'boolean_yes_label' => ''
1113
- ),
1114
- 'rewrite_custom_slug' => array(
1115
- 'label' => __( 'Custom Rewrite Slug', 'pods' ),
1116
- 'help' => __( 'Changes the first segment of the URL, which by default is the name of the Pod. For example, if your Pod is called "foo", if this field is left blank, your link will be "example.com/foo/post_slug", but if you were to enter "bar" your link will be "example.com/bar/post_slug".', 'pods' ),
1117
- 'type' => 'text',
1118
- 'default' => '',
1119
- 'depends-on' => array( 'rewrite' => true )
1120
- ),
1121
- 'rewrite_with_front' => array(
1122
- 'label' => __( 'Rewrite with Front', 'pods' ),
1123
- 'help' => __( 'Allows permalinks to be prepended with your front base (example: if your permalink structure is /blog/, then your links will be: Unchecked->/news/, Checked->/blog/news/)', 'pods' ),
1124
- 'type' => 'boolean',
1125
- 'default' => true,
1126
- 'depends-on' => array( 'rewrite' => true ),
1127
- 'boolean_yes_label' => ''
1128
- ),
1129
- 'rewrite_feeds' => array(
1130
- 'label' => __( 'Rewrite Feeds', 'pods' ),
1131
- 'help' => __( 'help', 'pods' ),
1132
- 'type' => 'boolean',
1133
- 'default' => false,
1134
- 'depends-on' => array( 'rewrite' => true ),
1135
- 'boolean_yes_label' => ''
1136
- ),
1137
- 'rewrite_pages' => array(
1138
- 'label' => __( 'Rewrite Pages', 'pods' ),
1139
- 'help' => __( 'help', 'pods' ),
1140
- 'type' => 'boolean',
1141
- 'default' => true,
1142
- 'depends-on' => array( 'rewrite' => true ),
1143
- 'boolean_yes_label' => ''
1144
- ),
1145
- 'query_var' => array(
1146
- 'label' => __( 'Query Var', 'pods' ),
1147
- 'help' => __( 'The Query Var is used in the URL and underneath the WordPress Rewrite API to tell WordPress what page or post type you are on. For a list of reserved Query Vars, read <a href="http://codex.wordpress.org/WordPress_Query_Vars">WordPress Query Vars</a> from the WordPress Codex.', 'pods' ),
1148
- 'type' => 'boolean',
1149
- 'default' => true,
1150
- 'boolean_yes_label' => ''
1151
- ),
1152
- 'can_export' => array(
1153
- 'label' => __( 'Exportable', 'pods' ),
1154
- 'help' => __( 'help', 'pods' ),
1155
- 'type' => 'boolean',
1156
- 'default' => true,
1157
- 'boolean_yes_label' => ''
1158
- ),
1159
- 'default_status' => array(
1160
- 'label' => __( 'Default Status', 'pods' ),
1161
- 'help' => __( 'help', 'pods' ),
1162
- 'type' => 'pick',
1163
- 'pick_object' => 'post-status',
1164
- 'default' => apply_filters( 'pods_api_default_status_' . pods_var_raw( 'name', $pod, 'post_type', null, true ), 'draft', $pod )
1165
- )
1166
- );
1167
- }
1168
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
1169
- $options[ 'admin-ui' ] = array(
1170
- 'show_ui' => array(
1171
- 'label' => __( 'Show Admin UI', 'pods' ),
1172
- 'help' => __( 'help', 'pods' ),
1173
- 'type' => 'boolean',
1174
- 'default' => pods_var_raw( 'public', $pod, true ),
1175
- 'dependency' => true,
1176
- 'boolean_yes_label' => ''
1177
- ),
1178
- 'menu_name' => array(
1179
- 'label' => __( 'Menu Name', 'pods' ),
1180
- 'help' => __( 'help', 'pods' ),
1181
- 'type' => 'text',
1182
- 'default' => '',
1183
- 'depends-on' => array( 'show_ui' => true )
1184
- ),
1185
- 'menu_location' => array(
1186
- 'label' => __( 'Menu Location', 'pods' ),
1187
- 'help' => __( 'help', 'pods' ),
1188
- 'type' => 'pick',
1189
- 'default' => 'default',
1190
- 'depends-on' => array( 'show_ui' => true ),
1191
- 'data' => array(
1192
- 'default' => __( 'Default - Add to associated Post Type(s) menus', 'pods' ),
1193
- 'settings' => __( 'Add to Settings menu', 'pods' ),
1194
- 'appearances' => __( 'Add to Appearances menu', 'pods' ),
1195
- 'objects' => __( 'Make a top-level menu item', 'pods' ),
1196
- 'top' => __( 'Make a new top-level menu item below Settings', 'pods' ),
1197
- 'submenu' => __( 'Add a submenu item to another menu', 'pods' )
1198
- ),
1199
- 'dependency' => true
1200
- ),
1201
- 'menu_location_custom' => array(
1202
- 'label' => __( 'Custom Menu Location', 'pods' ),
1203
- 'help' => __( 'help', 'pods' ),
1204
- 'type' => 'text',
1205
- 'depends-on' => array( 'menu_location' => 'submenu' )
1206
- ),
1207
- 'menu_position' => array(
1208
- 'label' => __( 'Menu Position', 'pods' ),
1209
- 'help' => __( 'help', 'pods' ),
1210
- 'type' => 'number',
1211
- 'default' => 0,
1212
- 'depends-on' => array( 'menu_location' => array( 'objects', 'top' ) )
1213
- ),
1214
- 'menu_icon' => array(
1215
- 'label' => __( 'Menu Icon URL', 'pods' ),
1216
- 'help' => __( 'help', 'pods' ),
1217
- 'type' => 'text',
1218
- 'default' => '',
1219
- 'depends-on' => array( 'menu_location' => array( 'objects', 'top' ) )
1220
- ),
1221
- 'show_in_nav_menus' => array(
1222
- 'label' => __( 'Show in Navigation Menus', 'pods' ),
1223
- 'help' => __( 'help', 'pods' ),
1224
- 'type' => 'boolean',
1225
- 'default' => pods_var_raw( 'public', $pod, true ),
1226
- 'boolean_yes_label' => ''
1227
- ),
1228
- 'show_tagcloud' => array(
1229
- 'label' => __( 'Allow in Tagcloud Widget', 'pods' ),
1230
- 'help' => __( 'help', 'pods' ),
1231
- 'type' => 'boolean',
1232
- 'default' => pods_var_raw( 'show_ui', $pod, pods_var_raw( 'public', $pod, true ) ),
1233
- 'boolean_yes_label' => ''
1234
- )
1235
- );
1236
-
1237
- if ( pods_version_check( 'wp', '3.5' ) ) {
1238
- $options[ 'admin-ui' ][ 'show_admin_column' ] = array(
1239
- 'label' => __( 'Show Taxonomy column on Post Types', 'pods' ),
1240
- 'help' => __( 'Whether to add a column for this taxonomy on the associated post types manage screens', 'pods' ),
1241
- 'type' => 'boolean',
1242
- 'default' => false,
1243
- 'boolean_yes_label' => ''
1244
- );
1245
- }
1246
 
1247
- // Integration for Single Value Taxonomy UI
1248
- if ( function_exists( 'tax_single_value_meta_box' ) ) {
1249
- $options[ 'admin-ui' ][ 'single_value' ] = array(
1250
- 'label' => __( 'Single Value Taxonomy', 'pods' ),
1251
- 'help' => __( 'Use a drop-down for the input instead of the WordPress default', 'pods' ),
1252
- 'type' => 'boolean',
1253
- 'default' => false,
1254
- 'boolean_yes_label' => ''
1255
- );
1256
-
1257
- $options[ 'admin-ui' ][ 'single_value_required' ] = array(
1258
- 'label' => __( 'Single Value Taxonomy - Required', 'pods' ),
1259
- 'help' => __( 'A term will be selected by default in the Post Editor, not optional', 'pods' ),
1260
- 'type' => 'boolean',
1261
- 'default' => false,
1262
- 'boolean_yes_label' => ''
1263
- );
1264
  }
 
 
1265
 
1266
- // @todo fill this in
1267
- $options[ 'advanced' ] = array(
1268
- 'temporary' => 'This type has the fields hardcoded' // :(
1269
- );
1270
- }
1271
- elseif ( 'settings' == $pod[ 'type' ] ) {
1272
- $options[ 'admin-ui' ] = array(
1273
- 'ui_style' => array(
1274
- 'label' => __( 'Admin UI Style', 'pods' ),
1275
- 'help' => __( 'help', 'pods' ),
1276
- 'type' => 'pick',
1277
- 'default' => 'settings',
1278
- 'data' => array(
1279
- 'settings' => __( 'Normal Settings Form', 'pods' ),
1280
- 'post_type' => __( 'Post Type UI', 'pods' ),
1281
- 'custom' => __( 'Custom (hook into pods_admin_ui_custom or pods_admin_ui_custom_{podname} action)', 'pods' )
1282
- ),
1283
- 'dependency' => true
1284
- ),
1285
- 'menu_location' => array(
1286
- 'label' => __( 'Menu Location', 'pods' ),
1287
- 'help' => __( 'help', 'pods' ),
1288
- 'type' => 'pick',
1289
- 'default' => 'settings',
1290
- 'data' => array(
1291
- 'settings' => __( 'Add to Settings menu', 'pods' ),
1292
- 'appearances' => __( 'Add to Appearances menu', 'pods' ),
1293
- 'top' => __( 'Make a new top-level menu item below Settings', 'pods' ),
1294
- 'submenu' => __( 'Add a submenu item to another menu', 'pods' )
1295
- ),
1296
- 'dependency' => true
1297
- ),
1298
- 'menu_location_custom' => array(
1299
- 'label' => __( 'Custom Menu Location', 'pods' ),
1300
- 'help' => __( 'help', 'pods' ),
1301
- 'type' => 'text',
1302
- 'depends-on' => array( 'menu_location' => 'submenu' )
1303
- ),
1304
- 'menu_position' => array(
1305
- 'label' => __( 'Menu Position', 'pods' ),
1306
- 'help' => __( 'help', 'pods' ),
1307
- 'type' => 'number',
1308
- 'default' => 0,
1309
- 'depends-on' => array( 'menu_location' => 'top' )
1310
- ),
1311
- 'menu_icon' => array(
1312
- 'label' => __( 'Menu Icon URL', 'pods' ),
1313
- 'help' => __( 'help', 'pods' ),
1314
- 'type' => 'text',
1315
- 'default' => '',
1316
- 'depends-on' => array( 'menu_location' => 'top' )
1317
- )
1318
- );
1319
-
1320
- // @todo fill this in
1321
- $options[ 'advanced' ] = array(
1322
- 'temporary' => 'This type has the fields hardcoded' // :(
1323
- );
1324
- }
1325
- elseif ( 'pod' == $pod[ 'type' ] ) {
1326
- $options[ 'admin-ui' ] = array(
1327
- 'ui_style' => array(
1328
- 'label' => __( 'Admin UI Style', 'pods' ),
1329
- 'help' => __( 'help', 'pods' ),
1330
- 'type' => 'pick',
1331
- 'default' => 'settings',
1332
- 'data' => array(
1333
- 'post_type' => __( 'Normal (Looks like the Post Type UI)', 'pods' ),
1334
- 'custom' => __( 'Custom (hook into pods_admin_ui_custom or pods_admin_ui_custom_{podname} action)', 'pods' )
1335
- ),
1336
- 'dependency' => true
1337
- ),
1338
- 'show_in_menu' => array(
1339
- 'label' => __( 'Show Admin Menu in Dashboard', 'pods' ),
1340
- 'help' => __( 'help', 'pods' ),
1341
- 'type' => 'boolean',
1342
- 'default' => false,
1343
- 'boolean_yes_label' => '',
1344
- 'dependency' => true
1345
- ),
1346
- 'menu_location_custom' => array(
1347
- 'label' => __( 'Parent Menu ID (optional)', 'pods' ),
1348
- 'help' => __( 'help', 'pods' ),
1349
- 'type' => 'text',
1350
- 'depends-on' => array( 'show_in_menu' => true )
1351
- ),
1352
- 'menu_position' => array(
1353
- 'label' => __( 'Menu Position', 'pods' ),
1354
- 'help' => __( 'help', 'pods' ),
1355
- 'type' => 'number',
1356
- 'default' => 0,
1357
- 'depends-on' => array( 'show_in_menu' => true )
1358
- ),
1359
- 'menu_icon' => array(
1360
- 'label' => __( 'Menu Icon URL', 'pods' ),
1361
- 'help' => __( 'This is the icon shown to the left of the menu text for this content type.', 'pods' ),
1362
- 'type' => 'text',
1363
- 'default' => '',
1364
- 'depends-on' => array( 'show_in_menu' => true )
1365
- ),
1366
- 'ui_icon' => array(
1367
- 'label' => __( 'Header Icon', 'pods' ),
1368
- 'help' => __( 'This is the icon shown to the left of the heading text at the top of the manage pages for this content type.', 'pods' ),
1369
- 'type' => 'file',
1370
- 'default' => '',
1371
- 'file_edit_title' => 0,
1372
- 'depends-on' => array( 'show_in_menu' => true )
1373
- ),
1374
- 'ui_actions_enabled' => array(
1375
- 'label' => __( 'Actions Available', 'pods' ),
1376
- 'help' => __( 'help', 'pods' ),
1377
- 'type' => 'pick',
1378
- 'default' => ( 1 == pods_var( 'ui_export', $pod ) ? array( 'add', 'edit', 'duplicate', 'delete', 'export' ) : array( 'add', 'edit', 'duplicate', 'delete' ) ),
1379
- 'data' => array(
1380
- 'add' => __( 'Add New', 'pods' ),
1381
- 'edit' => __( 'Edit', 'pods' ),
1382
- 'duplicate' => __( 'Duplicate', 'pods' ),
1383
- 'delete' => __( 'Delete', 'pods' ),
1384
- 'reorder' => __( 'Reorder', 'pods' ),
1385
- 'export' => __( 'Export', 'pods' )
1386
- ),
1387
- 'pick_format_type' => 'multi',
1388
- 'dependency' => true
1389
- ),
1390
- 'ui_reorder_field' => array(
1391
- 'label' => __( 'Reorder Field', 'pods' ),
1392
- 'help' => __( 'This is the field that will be reordered on, it should be numeric.', 'pods' ),
1393
- 'type' => 'text',
1394
- 'default' => 'menu_order',
1395
- 'depends-on' => array( 'ui_actions_enabled' => 'reorder' )
1396
- ),
1397
- 'ui_fields_manage' => array(
1398
- 'label' => __( 'Admin Table Columns', 'pods' ),
1399
- 'help' => __( 'help', 'pods' ),
1400
- 'type' => 'pick',
1401
- 'default' => array(),
1402
- 'data' => array(),
1403
- 'pick_format_type' => 'multi'
1404
- ),
1405
- 'ui_filters' => array(
1406
- 'label' => __( 'Search Filters', 'pods' ),
1407
- 'help' => __( 'help', 'pods' ),
1408
- 'type' => 'pick',
1409
- 'default' => array(),
1410
- 'data' => array(),
1411
- 'pick_format_type' => 'multi'
1412
- )
1413
- );
1414
-
1415
- if ( !empty( $pod[ 'fields' ] ) ) {
1416
- if ( isset( $pod[ 'fields' ][ pods_var_raw( 'pod_index', $pod, 'name' ) ] ) )
1417
- $options[ 'admin-ui' ][ 'ui_fields_manage' ][ 'default' ][] = pods_var_raw( 'pod_index', $pod, 'name' );
1418
-
1419
- if ( isset( $pod[ 'fields' ][ 'modified' ] ) )
1420
- $options[ 'admin-ui' ][ 'ui_fields_manage' ][ 'default' ][] = 'modified';
1421
-
1422
- foreach ( $pod[ 'fields' ] as $field ) {
1423
- $type = '';
1424
-
1425
- if ( isset( $field_types[ $field[ 'type' ] ] ) )
1426
- $type = ' <small>(' . $field_types[ $field[ 'type' ] ][ 'label' ] . ')</small>';
1427
-
1428
- $options[ 'admin-ui' ][ 'ui_fields_manage' ][ 'data' ][ $field[ 'name' ] ] = $field[ 'label' ] . $type;
1429
- $options[ 'admin-ui' ][ 'ui_filters' ][ 'data' ][ $field[ 'name' ] ] = $field[ 'label' ] . $type;
1430
- }
1431
-
1432
- $options[ 'admin-ui' ][ 'ui_fields_manage' ][ 'data' ][ 'id' ] = 'ID';
1433
- }
1434
- else {
1435
- unset( $options[ 'admin-ui' ][ 'ui_fields_manage' ] );
1436
- unset( $options[ 'admin-ui' ][ 'ui_filters' ] );
1437
- }
1438
-
1439
- // @todo fill this in
1440
- $options[ 'advanced' ] = array(
1441
- 'temporary' => 'This type has the fields hardcoded' // :(
1442
- );
1443
- }
1444
-
1445
- /**
1446
- * Add admin fields to the Pods editor for a specific Pod
1447
- *
1448
- * @params array $options The Options fields
1449
- * @params object $pod Current Pods object
1450
- *
1451
- * @since unkown
1452
- */
1453
- $options = apply_filters( 'pods_admin_setup_edit_options_' . $pod[ 'type' ] . '_' . $pod[ 'name' ], $options, $pod );
1454
 
1455
- /**
1456
- * Add admin fields to the Pods editor for any Pod of a specific content type.
1457
- */
1458
- $options = apply_filters( 'pods_admin_setup_edit_options_' . $pod[ 'type' ], $options, $pod );
1459
 
1460
- /**
1461
- * Add admin fields to the Pods editor for all Pods
1462
- */
1463
- $options = apply_filters( 'pods_admin_setup_edit_options', $options, $pod );
1464
 
1465
- return $options;
1466
- }
1467
-
1468
- /**
1469
- * Get list of Pod field option tabs
1470
- *
1471
- * @return array
1472
- */
1473
- public function admin_setup_edit_field_tabs ( $pod ) {
1474
- $core_tabs = array(
1475
- 'basic' => __( 'Basic', 'pods' ),
1476
- 'additional-field' => __( 'Additional Field Options', 'pods' ),
1477
- 'advanced' => __( 'Advanced', 'pods' )
1478
- );
1479
-
1480
- $tabs = apply_filters( 'pods_admin_setup_edit_field_tabs', array(), $pod );
1481
-
1482
- $tabs = array_merge( $core_tabs, $tabs );
1483
-
1484
- return $tabs;
1485
- }
1486
-
1487
- /**
1488
- * Get list of Pod field options
1489
- *
1490
- * @return array
1491
- */
1492
- public function admin_setup_edit_field_options ( $pod ) {
1493
- $options = array();
1494
-
1495
- $options[ 'additional-field' ] = array();
1496
-
1497
- $field_types = PodsForm::field_types();
1498
-
1499
- foreach ( $field_types as $type => $field_type_data ) {
1500
- /**
1501
- * @var $field_type PodsField
1502
- */
1503
- $field_type = PodsForm::field_loader( $type, $field_type_data[ 'file' ] );
1504
-
1505
- $field_type_vars = get_class_vars( get_class( $field_type ) );
1506
-
1507
- if ( !isset( $field_type_vars[ 'pod_types' ] ) )
1508
- $field_type_vars[ 'pod_types' ] = true;
1509
-
1510
- $options[ 'additional-field' ][ $type ] = array();
1511
-
1512
- // Only show supported field types
1513
- if ( true !== $field_type_vars[ 'pod_types' ] ) {
1514
- if ( empty( $field_type_vars[ 'pod_types' ] ) )
1515
- continue;
1516
- elseif ( is_array( $field_type_vars[ 'pod_types' ] ) && !in_array( pods_var( 'type', $pod ), $field_type_vars[ 'pod_types' ] ) )
1517
- continue;
1518
- elseif ( !is_array( $field_type_vars[ 'pod_types' ] ) && pods_var( 'type', $pod ) != $field_type_vars[ 'pod_types' ] )
1519
- continue;
1520
- }
1521
-
1522
- $options[ 'additional-field' ][ $type ] = PodsForm::ui_options( $type );
1523
- }
1524
-
1525
- $input_helpers = array(
1526
- '' => '-- Select --'
1527
- );
1528
-
1529
- if ( class_exists( 'Pods_Helpers' ) ) {
1530
- $helpers = pods_api()->load_helpers( array( 'options' => array( 'helper_type' => 'input' ) ) );
1531
-
1532
- foreach ( $helpers as $helper ) {
1533
- $input_helpers[ $helper[ 'name' ] ] = $helper[ 'name' ];
1534
- }
1535
- }
1536
-
1537
- $options[ 'advanced' ] = array(
1538
- __( 'Visual', 'pods' ) => array(
1539
- 'class' => array(
1540
- 'name' => 'class',
1541
- 'label' => __( 'Additional CSS Classes', 'pods' ),
1542
- 'help' => __( 'help', 'pods' ),
1543
- 'type' => 'text',
1544
- 'default' => ''
1545
- ),
1546
- 'input_helper' => array(
1547
- 'name' => 'input_helper',
1548
- 'label' => __( 'Input Helper', 'pods' ),
1549
- 'help' => __( 'help', 'pods' ),
1550
- 'type' => 'pick',
1551
- 'default' => '',
1552
- 'data' => $input_helpers
1553
- )
1554
- ),
1555
- __( 'Values', 'pods' ) => array(
1556
- 'default_value' => array(
1557
- 'name' => 'default_value',
1558
- 'label' => __( 'Default Value', 'pods' ),
1559
- 'help' => __( 'help', 'pods' ),
1560
- 'type' => 'text',
1561
- 'default' => ''
1562
- ),
1563
- 'default_value_parameter' => array(
1564
- 'name' => 'default_value_parameter',
1565
- 'label' => __( 'Set Default Value via Parameter', 'pods' ),
1566
- 'help' => __( 'help', 'pods' ),
1567
- 'type' => 'text',
1568
- 'default' => ''
1569
- )
1570
- ),
1571
- __( 'Visibility', 'pods' ) => array(
1572
- 'restrict_access' => array(
1573
- 'name' => 'restrict_access',
1574
- 'label' => __( 'Restrict Access', 'pods' ),
1575
- 'group' => array(
1576
- 'admin_only' => array(
1577
- 'name' => 'admin_only',
1578
- 'label' => __( 'Restrict access to Admins?', 'pods' ),
1579
- 'default' => 0,
1580
- 'type' => 'boolean',
1581
- 'dependency' => true,
1582
- 'help' => __( 'This field will only be able to be edited by users with the ability to manage_options or delete_users, or super admins of a WordPress Multisite network', 'pods' )
1583
- ),
1584
- 'restrict_role' => array(
1585
- 'name' => 'restrict_role',
1586
- 'label' => __( 'Restrict access by Role?', 'pods' ),
1587
- 'default' => 0,
1588
- 'type' => 'boolean',
1589
- 'dependency' => true
1590
- ),
1591
- 'restrict_capability' => array(
1592
- 'name' => 'restrict_capability',
1593
- 'label' => __( 'Restrict access by Capability?', 'pods' ),
1594
- 'default' => 0,
1595
- 'type' => 'boolean',
1596
- 'dependency' => true
1597
- ),
1598
- 'hidden' => array(
1599
- 'name' => 'hidden',
1600
- 'label' => __( 'Hide field from UI', 'pods' ),
1601
- 'default' => 0,
1602
- 'type' => 'boolean',
1603
- 'help' => __( 'This option is overriden by access restrictions. If the user does not have access to edit this field, it will be hidden. If no access restrictions are set, this field will always be hidden.', 'pods' )
1604
- ),
1605
- 'read_only' => array(
1606
- 'name' => 'read_only',
1607
- 'label' => __( 'Make field "Read Only" in UI', 'pods' ),
1608
- 'default' => 0,
1609
- 'type' => 'boolean',
1610
- 'help' => __( 'This option is overriden by access restrictions. If the user does not have access to edit this field, it will be read only. If no access restrictions are set, this field will always be read only.', 'pods' ),
1611
- 'depends-on' => array(
1612
- 'type' => array(
1613
- 'boolean',
1614
- 'color',
1615
- 'currency',
1616
- 'date',
1617
- 'datetime',
1618
- 'email',
1619
- 'number',
1620
- 'paragraph',
1621
- 'password',
1622
- 'phone',
1623
- 'slug',
1624
- 'text',
1625
- 'time',
1626
- 'website'
1627
- )
1628
- )
1629
- )
1630
- )
1631
- ),
1632
- 'roles_allowed' => array(
1633
- 'name' => 'roles_allowed',
1634
- 'label' => __( 'Role(s) Allowed', 'pods' ),
1635
- 'help' => __( 'help', 'pods' ),
1636
- 'type' => 'pick',
1637
- 'pick_object' => 'role',
1638
- 'pick_format_type' => 'multi',
1639
- 'default' => 'administrator',
1640
- 'depends-on' => array(
1641
- 'restrict_role' => true
1642
- )
1643
- ),
1644
- 'capability_allowed' => array(
1645
- 'name' => 'capability_allowed',
1646
- 'label' => __( 'Capability Allowed', 'pods' ),
1647
- 'help' => __( 'Comma-separated list of cababilities, for example add_podname_item, please see the Roles and Capabilities component for the complete list and a way to add your own.', 'pods' ),
1648
- 'type' => 'text',
1649
- 'default' => '',
1650
- 'depends-on' => array(
1651
- 'restrict_capability' => true
1652
- )
1653
- )
1654
- /*,
1655
- 'search' => array(
1656
- 'label' => __( 'Include in searches', 'pods' ),
1657
- 'help' => __( 'help', 'pods' ),
1658
- 'default' => 1,
1659
- 'type' => 'boolean',
1660
- )*/
1661
- )
1662
- /*,
1663
- __( 'Validation', 'pods' ) => array(
1664
- 'regex_validation' => array(
1665
- 'label' => __( 'RegEx Validation', 'pods' ),
1666
- 'help' => __( 'help', 'pods' ),
1667
- 'type' => 'text',
1668
- 'default' => ''
1669
- ),
1670
- 'message_regex' => array(
1671
- 'label' => __( 'Message if field does not pass RegEx', 'pods' ),
1672
- 'help' => __( 'help', 'pods' ),
1673
- 'type' => 'text',
1674
- 'default' => ''
1675
- ),
1676
- 'message_required' => array(
1677
- 'label' => __( 'Message if field is blank', 'pods' ),
1678
- 'help' => __( 'help', 'pods' ),
1679
- 'type' => 'text',
1680
- 'default' => '',
1681
- 'depends-on' => array( 'required' => true )
1682
- ),
1683
- 'message_unique' => array(
1684
- 'label' => __( 'Message if field is not unique', 'pods' ),
1685
- 'help' => __( 'help', 'pods' ),
1686
- 'type' => 'text',
1687
- 'default' => '',
1688
- 'depends-on' => array( 'unique' => true )
1689
- )
1690
- )*/
1691
- );
1692
-
1693
- if ( !class_exists( 'Pods_Helpers' ) )
1694
- unset( $options[ 'advanced-options' ][ 'input_helper' ] );
1695
-
1696
- $options = apply_filters( 'pods_admin_setup_edit_field_options', $options, $pod );
1697
-
1698
- return $options;
1699
- }
1700
-
1701
- /**
1702
- * Duplicate a pod
1703
- *
1704
- * @param $id
1705
- * @param $obj
1706
- *
1707
- * @return mixed
1708
- */
1709
- public function admin_setup_duplicate ( $obj ) {
1710
- $new_id = pods_api()->duplicate_pod( array( 'id' => $obj->id ) );
1711
-
1712
- if ( 0 < $new_id )
1713
- pods_redirect( pods_var_update( array( 'action' => 'edit', 'id' => $new_id, 'do' => 'duplicate' ) ) );
1714
- }
1715
 
1716
  /**
1717
- * Restrict Duplicate action to custom types, not extended
1718
  *
1719
- * @param bool $restricted
1720
- * @param array $restrict
1721
- * @param string $action
1722
- * @param array $row
1723
- * @param PodsUI $obj
1724
  *
1725
- * @since 2.3.10
1726
  */
1727
- public function admin_setup_duplicate_restrict( $restricted, $restrict, $action, $row, $obj ) {
1728
-
1729
- if ( in_array( $row[ 'real_type' ], array( 'user', 'media', 'comment' ) ) ) {
1730
- $restricted = true;
1731
- }
1732
-
1733
- return $restricted;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1734
 
1735
- }
 
 
1736
 
1737
- /**
1738
- * Reset a pod
1739
- *
1740
- * @param $obj
1741
- *
1742
- * @return mixed
1743
- */
1744
- public function admin_setup_reset ( $obj, $id ) {
1745
- $pod = pods_api()->load_pod( array( 'id' => $id ), false );
1746
 
1747
- if ( empty( $pod ) )
1748
- return $obj->error( __( 'Pod not found.', 'pods' ) );
 
1749
 
1750
- pods_api()->reset_pod( array( 'id' => $id ) );
1751
 
1752
- $obj->message( __( 'Pod reset successfully.', 'pods' ) );
 
 
 
 
1753
 
1754
- $obj->manage();
1755
- }
 
 
 
 
 
 
 
 
 
 
 
1756
 
1757
- /**
1758
- * Restrict Reset action from users and media
1759
- *
1760
- * @param bool $restricted
1761
- * @param array $restrict
1762
- * @param string $action
1763
- * @param array $row
1764
- * @param PodsUI $obj
1765
- *
1766
- * @since 2.3.10
1767
- */
1768
- public function admin_setup_reset_restrict( $restricted, $restrict, $action, $row, $obj ) {
1769
 
1770
- if ( in_array( $row[ 'real_type' ], array( 'user', 'media' ) ) ) {
1771
- $restricted = true;
1772
- }
1773
 
1774
- return $restricted;
 
 
 
 
 
 
 
1775
 
 
1776
  }
1777
 
1778
- /**
1779
- * Delete a pod
1780
- *
1781
- * @param $id
1782
- * @param $obj
1783
- *
1784
- * @return mixed
1785
- */
1786
- public function admin_setup_delete ( $id, $obj ) {
1787
- $pod = pods_api()->load_pod( array( 'id' => $id ), false );
1788
-
1789
- if ( empty( $pod ) )
1790
- return $obj->error( __( 'Pod not found.', 'pods' ) );
1791
 
1792
- pods_api()->delete_pod( array( 'id' => $id ) );
1793
-
1794
- unset( $obj->data[ $pod[ 'id' ] ] );
1795
-
1796
- $obj->total = count( $obj->data );
1797
- $obj->total_found = count( $obj->data );
1798
-
1799
- $obj->message( __( 'Pod deleted successfully.', 'pods' ) );
1800
- }
1801
-
1802
- /**
1803
- * Get advanced administration view.
1804
- */
1805
- public function admin_advanced () {
1806
- pods_view( PODS_DIR . 'ui/admin/advanced.php', compact( array_keys( get_defined_vars() ) ) );
1807
- }
1808
-
1809
- /**
1810
- * Get settings administration view
1811
- */
1812
- public function admin_settings () {
1813
- pods_view( PODS_DIR . 'ui/admin/settings.php', compact( array_keys( get_defined_vars() ) ) );
1814
- }
1815
-
1816
- /**
1817
- * Get components administration UI
1818
- */
1819
- public function admin_components () {
1820
- $components = PodsInit::$components->components;
1821
-
1822
- $view = pods_var( 'view', 'get', 'all', null, true );
1823
-
1824
- $recommended = array(
1825
- 'advanced-relationships',
1826
- 'advanced-content-types',
1827
- 'migrate-packages',
1828
- 'roles-and-capabilities',
1829
- 'pages',
1830
- 'table-storage',
1831
- 'templates'
1832
- );
1833
-
1834
- foreach ( $components as $component => &$component_data ) {
1835
- if ( !in_array( $view, array( 'all', 'recommended', 'dev' ) ) && ( !isset( $component_data[ 'Category' ] ) || $view != sanitize_title( $component_data[ 'Category' ] ) ) ) {
1836
- unset( $components[ $component ] );
1837
-
1838
- continue;
1839
- }
1840
- elseif ( 'recommended' == $view && !in_array( $component_data[ 'ID' ], $recommended ) ) {
1841
- unset( $components[ $component ] );
1842
-
1843
- continue;
1844
- }
1845
- elseif ( 'dev' == $view && pods_developer() && !pods_var_raw( 'DeveloperMode', $component_data, false ) ) {
1846
- unset( $components[ $component ] );
1847
-
1848
- continue;
1849
- }
1850
- elseif ( pods_var_raw( 'DeveloperMode', $component_data, false ) && !pods_developer() ) {
1851
- unset( $components[ $component ] );
1852
-
1853
- continue;
1854
- }
1855
- elseif ( !pods_var_raw( 'TablelessMode', $component_data, false ) && pods_tableless() ) {
1856
- unset( $components[ $component ] );
1857
-
1858
- continue;
1859
- }
1860
-
1861
- $component_data[ 'Name' ] = strip_tags( $component_data[ 'Name' ] );
1862
-
1863
- if ( pods_var_raw( 'DeveloperMode', $component_data, false ) )
1864
- $component_data[ 'Name' ] .= ' <em style="font-weight: normal; color:#333;">(Developer Preview)</em>';
1865
-
1866
- $meta = array();
1867
-
1868
- if ( !empty( $component_data[ 'Version' ] ) )
1869
- $meta[] = 'Version ' . $component_data[ 'Version' ];
1870
-
1871
- if ( empty( $component_data[ 'Author' ] ) ) {
1872
- $component_data[ 'Author' ] = 'Pods Framework Team';
1873
- $component_data[ 'AuthorURI' ] = 'http://pods.io/';
1874
- }
1875
-
1876
- if ( !empty( $component_data[ 'AuthorURI' ] ) )
1877
- $component_data[ 'Author' ] = '<a href="' . $component_data[ 'AuthorURI' ] . '">' . $component_data[ 'Author' ] . '</a>';
1878
-
1879
- $meta[] = sprintf( __( 'by %s', 'pods' ), $component_data[ 'Author' ] );
1880
-
1881
- if ( !empty( $component_data[ 'URI' ] ) )
1882
- $meta[] = '<a href="' . $component_data[ 'URI' ] . '">' . __( 'Visit component site', 'pods' ) . '</a>';
1883
-
1884
- $component_data[ 'Description' ] = wpautop( trim( make_clickable( strip_tags( $component_data[ 'Description' ], 'em,strong' ) ) ) );
1885
-
1886
- if ( !empty( $meta ) )
1887
- $component_data[ 'Description' ] .= '<div class="pods-component-meta" ' . ( !empty( $component_data[ 'Description' ] ) ? ' style="padding:8px 0 4px;"' : '' ) . '>' . implode( '&nbsp;&nbsp;|&nbsp;&nbsp;', $meta ) . '</div>';
1888
-
1889
- $component_data = array(
1890
- 'id' => $component_data[ 'ID' ],
1891
- 'name' => $component_data[ 'Name' ],
1892
- 'category' => $component_data[ 'Category' ],
1893
- 'version' => '',
1894
- 'description' => $component_data[ 'Description' ],
1895
- 'mustuse' => pods_var_raw( 'MustUse', $component_data, false ),
1896
- 'toggle' => 0
1897
- );
1898
-
1899
- if ( !empty( $component_data[ 'category' ] ) ) {
1900
- $category_url = pods_var_update( array( 'view' => sanitize_title( $component_data[ 'category' ] ), 'pg' => '', 'page' => $_GET[ 'page' ] ) );
1901
-
1902
- $component_data[ 'category' ] = '<a href="' . $category_url . '">' . $component_data[ 'category' ] . '</a>';
1903
- }
1904
-
1905
- if ( isset( PodsInit::$components->settings[ 'components' ][ $component_data[ 'id' ] ] ) && 0 != PodsInit::$components->settings[ 'components' ][ $component_data[ 'id' ] ] )
1906
- $component_data[ 'toggle' ] = 1;
1907
- elseif ( $component_data[ 'mustuse' ] )
1908
- $component_data[ 'toggle' ] = 1;
1909
- }
1910
-
1911
- $ui = array(
1912
- 'data' => $components,
1913
- 'total' => count( $components ),
1914
- 'total_found' => count( $components ),
1915
- 'icon' => PODS_URL . 'ui/images/icon32.png',
1916
- 'items' => 'Components',
1917
- 'item' => 'Component',
1918
- 'fields' => array(
1919
- 'manage' => array(
1920
- 'name' => array(
1921
- 'label' => __( 'Name', 'pods' ),
1922
- 'width' => '30%',
1923
- 'type' => 'text',
1924
- 'options' => array(
1925
- 'text_allow_html' => true
1926
- )
1927
- ),
1928
- 'category' => array(
1929
- 'label' => __( 'Category', 'pods' ),
1930
- 'width' => '10%',
1931
- 'type' => 'text',
1932
- 'options' => array(
1933
- 'text_allow_html' => true
1934
- )
1935
- ),
1936
- 'description' => array(
1937
- 'label' => __( 'Description', 'pods' ),
1938
- 'width' => '60%',
1939
- 'type' => 'text',
1940
- 'options' => array(
1941
- 'text_allow_html' => true,
1942
- 'text_allowed_html_tags' => 'strong em a ul ol li b i br div'
1943
- )
1944
- )
1945
- )
1946
- ),
1947
- 'actions_disabled' => array( 'duplicate', 'view', 'export', 'add', 'edit', 'delete' ),
1948
- 'actions_custom' => array(
1949
- 'toggle' => array( 'callback' => array( $this, 'admin_components_toggle' ) )
1950
- ),
1951
- 'filters_enhanced' => true,
1952
- 'views' => array(
1953
- 'all' => __( 'All', 'pods' ),
1954
- //'recommended' => __( 'Recommended', 'pods' ),
1955
- 'field-types' => __( 'Field Types', 'pods' ),
1956
- 'tools' => __( 'Tools', 'pods' ),
1957
- 'integration' => __( 'Integration', 'pods' ),
1958
- 'migration' => __( 'Migration', 'pods' ),
1959
- 'advanced' => __( 'Advanced', 'pods' )
1960
- ),
1961
- 'view' => $view,
1962
- 'heading' => array(
1963
- 'views' => __( 'Category', 'pods' )
1964
- ),
1965
- 'search' => false,
1966
- 'searchable' => false,
1967
- 'sortable' => false,
1968
- 'pagination' => false
1969
- );
1970
-
1971
- if ( pods_developer() )
1972
- $ui[ 'views' ][ 'dev' ] = __( 'Developer Preview', 'pods' );
1973
-
1974
- pods_ui( $ui );
1975
- }
1976
-
1977
- /**
1978
- * Toggle a component on or off
1979
- *
1980
- * @param PodsUI $ui
1981
- *
1982
- * @return bool
1983
- */
1984
- public function admin_components_toggle ( PodsUI $ui ) {
1985
- $component = $_GET[ 'id' ];
1986
-
1987
- if ( !empty( PodsInit::$components->components[ $component ][ 'PluginDependency' ] ) ) {
1988
- $dependency = explode( '|', PodsInit::$components->components[ $component ][ 'PluginDependency' ] );
1989
-
1990
- if ( !pods_is_plugin_active( $dependency[ 1 ] ) ) {
1991
- $website = 'http://wordpress.org/extend/plugins/' . dirname( $dependency[ 1 ] ) . '/';
1992
-
1993
- if ( isset( $dependency[ 2 ] ) )
1994
- $website = $dependency[ 2 ];
1995
-
1996
- if ( !empty( $website ) )
1997
- $website = ' ' . sprintf( __( 'You can find it at %s', 'pods' ), '<a href="' . $website . '" target="_blank">' . $website . '</a>' );
1998
-
1999
- $message = sprintf( __( 'The %s component requires that you have the <strong>%s</strong> plugin installed and activated.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ], $dependency[ 0 ] ) . $website;
2000
-
2001
- $ui->error( $message );
2002
-
2003
- $ui->manage();
2004
-
2005
- return;
2006
- }
2007
- }
2008
-
2009
- if ( !empty( PodsInit::$components->components[ $component ][ 'ThemeDependency' ] ) ) {
2010
- $dependency = explode( '|', PodsInit::$components->components[ $component ][ 'ThemeDependency' ] );
2011
-
2012
- if ( strtolower( $dependency[ 1 ] ) != strtolower( get_template() ) && strtolower( $dependency[ 1 ] ) != strtolower( get_stylesheet() ) ) {
2013
- $website = '';
2014
-
2015
- if ( isset( $dependency[ 2 ] ) )
2016
- $website = ' ' . sprintf( __( 'You can find it at %s', 'pods' ), '<a href="' . $dependency[ 2 ] . '" target="_blank">' . $dependency[ 2 ] . '</a>' );
2017
-
2018
- $message = sprintf( __( 'The %s component requires that you have the <strong>%s</strong> theme installed and activated.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ], $dependency[ 0 ] ) . $website;
2019
-
2020
- $ui->error( $message );
2021
-
2022
- $ui->manage();
2023
-
2024
- return;
2025
- }
2026
- }
2027
-
2028
- if ( !empty( PodsInit::$components->components[ $component ][ 'MustUse' ] ) ) {
2029
- $message = sprintf( __( 'The %s component can not be disabled from here. You must deactivate the plugin or theme that added it.', 'pods' ), PodsInit::$components->components[ $component ][ 'Name' ] );
2030
-
2031
- $ui->error( $message );
2032
-
2033
- $ui->manage();
2034
-
2035
- return;
2036
- }
2037
-
2038
- if ( 1 == pods_var( 'toggled' ) ) {
2039
- $toggle = PodsInit::$components->toggle( $component );
2040
-
2041
- if ( true === $toggle )
2042
- $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component enabled', 'pods' ) );
2043
- elseif ( false === $toggle )
2044
- $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component disabled', 'pods' ) );
2045
-
2046
- $components = PodsInit::$components->components;
2047
-
2048
- foreach ( $components as $component => &$component_data ) {
2049
- $toggle = 0;
2050
-
2051
- if ( isset( PodsInit::$components->settings[ 'components' ][ $component_data[ 'ID' ] ] ) ) {
2052
- if ( 0 != PodsInit::$components->settings[ 'components' ][ $component_data[ 'ID' ] ] )
2053
- $toggle = 1;
2054
- }
2055
- if ( true === $component_data[ 'DeveloperMode' ] ) {
2056
- if ( !pods_developer() ) {
2057
- unset( $components[ $component ] );
2058
- continue;
2059
- }
2060
- }
2061
-
2062
- $component_data = array(
2063
- 'id' => $component_data[ 'ID' ],
2064
- 'name' => $component_data[ 'Name' ],
2065
- 'description' => make_clickable( $component_data[ 'Description' ] ),
2066
- 'version' => $component_data[ 'Version' ],
2067
- 'author' => $component_data[ 'Author' ],
2068
- 'toggle' => $toggle
2069
- );
2070
- }
2071
-
2072
- $ui->data = $components;
2073
-
2074
- pods_transient_clear( 'pods_components' );
2075
-
2076
- $url = pods_var_update( array( 'toggled' => null ) );
2077
-
2078
- pods_redirect( $url );
2079
- }
2080
- elseif ( 1 == pods_var( 'toggle' ) )
2081
- $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component enabled', 'pods' ) );
2082
- else
2083
- $ui->message( PodsInit::$components->components[ $component ][ 'Name' ] . ' ' . __( 'Component disabled', 'pods' ) );
2084
-
2085
- $ui->manage();
2086
- }
2087
-
2088
- /**
2089
- * Get the admin upgrade page
2090
- */
2091
- public function admin_upgrade () {
2092
- foreach ( PodsInit::$upgrades as $old_version => $new_version ) {
2093
- if ( version_compare( $old_version, PodsInit::$version_last, '<=' ) && version_compare( PodsInit::$version_last, $new_version, '<' ) ) {
2094
- $new_version = str_replace( '.', '_', $new_version );
2095
-
2096
- pods_view( PODS_DIR . 'ui/admin/upgrade/upgrade_' . $new_version . '.php', compact( array_keys( get_defined_vars() ) ) );
2097
-
2098
- break;
2099
- }
2100
- }
2101
- }
2102
-
2103
- /**
2104
- * Get the admin help page
2105
- */
2106
- public function admin_help () {
2107
- pods_view( PODS_DIR . 'ui/admin/help.php', compact( array_keys( get_defined_vars() ) ) );
2108
- }
2109
-
2110
- /**
2111
- * Add pods specific capabilities.
2112
- *
2113
- * @param $capabilities List of extra capabilities to add
2114
- *
2115
- * @return array
2116
- */
2117
- public function admin_capabilities ( $capabilities ) {
2118
- $pods = pods_api()->load_pods( array( 'type' => array( 'pod', 'table', 'post_type', 'taxonomy', 'settings' ) ) );
2119
-
2120
- $capabilities[] = 'pods';
2121
- $capabilities[] = 'pods_content';
2122
- $capabilities[] = 'pods_settings';
2123
- $capabilities[] = 'pods_components';
2124
-
2125
- foreach ( $pods as $pod ) {
2126
- if ( 'settings' == $pod[ 'type' ] ) {
2127
- $capabilities[] = 'pods_edit_' . $pod[ 'name' ];
2128
- }
2129
- elseif ( 'post_type' == $pod[ 'type' ] ) {
2130
- $capability_type = pods_var( 'capability_type_custom', $pod[ 'options' ], pods_var_raw( 'name', $pod ) );
2131
-
2132
- if ( 'custom' == pods_var( 'capability_type', $pod[ 'options' ] ) && 0 < strlen( $capability_type ) ) {
2133
- $capabilities[] = 'read_' . $capability_type;
2134
- $capabilities[] = 'edit_' . $capability_type;
2135
- $capabilities[] = 'delete_' . $capability_type;
2136
-
2137
- if ( 1 == pods_var( 'capability_type_extra', $pod[ 'options' ], 1 ) ) {
2138
- $capabilities[] = 'read_private_' . $capability_type . 's';
2139
- $capabilities[] = 'edit_' . $capability_type . 's';
2140
- $capabilities[] = 'edit_others_' . $capability_type . 's';
2141
- $capabilities[] = 'edit_private_' . $capability_type . 's';
2142
- $capabilities[] = 'edit_published_' . $capability_type . 's';
2143
- $capabilities[] = 'publish_' . $capability_type . 's';
2144
- $capabilities[] = 'delete_' . $capability_type . 's';
2145
- $capabilities[] = 'delete_private_' . $capability_type . 's';
2146
- $capabilities[] = 'delete_published_' . $capability_type . 's';
2147
- $capabilities[] = 'delete_others_' . $capability_type . 's';
2148
- }
2149
- }
2150
- }
2151
- elseif ( 'taxonomy' == $pod[ 'type' ] ) {
2152
- if ( 1 == pods_var( 'capabilities', $pod[ 'options' ], 0 ) ) {
2153
- $capability_type = pods_var( 'capability_type_custom', $pod[ 'options' ], pods_var_raw( 'name', $pod ) . 's' );
2154
-
2155
- $capabilities[] = 'manage_' . $capability_type;
2156
- $capabilities[] = 'edit_' . $capability_type;
2157
- $capabilities[] = 'delete_' . $capability_type;
2158
- $capabilities[] = 'assign_' . $capability_type;
2159
- }
2160
- }
2161
- else {
2162
- $capabilities[] = 'pods_add_' . $pod[ 'name' ];
2163
- $capabilities[] = 'pods_edit_' . $pod[ 'name' ];
2164
-
2165
- if ( isset( $pod[ 'fields' ][ 'author' ] ) && 'pick' == $pod[ 'fields' ][ 'author' ][ 'type' ] && 'user' == $pod[ 'fields' ][ 'author' ][ 'pick_object' ] )
2166
- $capabilities[] = 'pods_edit_others_' . $pod[ 'name' ];
2167
-
2168
- $capabilities[] = 'pods_delete_' . $pod[ 'name' ];
2169
-
2170
- if ( isset( $pod[ 'fields' ][ 'author' ] ) && 'pick' == $pod[ 'fields' ][ 'author' ][ 'type' ] && 'user' == $pod[ 'fields' ][ 'author' ][ 'pick_object' ] )
2171
- $capabilities[] = 'pods_delete_others_' . $pod[ 'name' ];
2172
-
2173
- $actions_enabled = pods_var_raw( 'ui_actions_enabled', $pod[ 'options' ] );
2174
-
2175
- if ( !empty( $actions_enabled ) )
2176
- $actions_enabled = (array) $actions_enabled;
2177
- else
2178
- $actions_enabled = array();
2179
-
2180
- $available_actions = array(
2181
- 'add',
2182
- 'edit',
2183
- 'duplicate',
2184
- 'delete',
2185
- 'reorder',
2186
- 'export'
2187
- );
2188
-
2189
- if ( !empty( $actions_enabled ) ) {
2190
- $actions_disabled = array(
2191
- 'view' => 'view'
2192
- );
2193
-
2194
- foreach ( $available_actions as $action ) {
2195
- if ( !in_array( $action, $actions_enabled ) )
2196
- $actions_disabled[ $action ] = $action;
2197
- }
2198
-
2199
- if ( !in_array( 'export', $actions_disabled ) )
2200
- $capabilities[] = 'pods_export_' . $pod[ 'name' ];
2201
-
2202
- if ( !in_array( 'reorder', $actions_disabled ) )
2203
- $capabilities[] = 'pods_reorder_' . $pod[ 'name' ];
2204
- }
2205
- elseif ( 1 == pods_var( 'ui_export', $pod[ 'options' ], 0 ) )
2206
- $capabilities[] = 'pods_export_' . $pod[ 'name' ];
2207
- }
2208
- }
2209
-
2210
- return $capabilities;
2211
- }
2212
-
2213
- /**
2214
- * Handle ajax calls for the administration
2215
- */
2216
- public function admin_ajax () {
2217
- if ( false === headers_sent() ) {
2218
  pods_session_start();
2219
 
2220
- header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
2221
- }
2222
 
2223
- // Sanitize input
2224
- $params = pods_unslash( (array) $_POST );
 
2225
 
2226
  foreach ( $params as $key => $value ) {
2227
- if ( 'action' == $key )
2228
  continue;
 
2229
 
2230
- // Fixup $_POST data
2231
  $_POST[ str_replace( '_podsfix_', '', $key ) ] = $_POST[ $key ];
2232
 
2233
  // Fixup $params with unslashed data
@@ -2237,159 +3356,530 @@ class PodsAdmin {
2237
  unset( $params[ $key ] );
2238
  }
2239
 
2240
- $params = (object) $params;
2241
 
2242
- $methods = array(
2243
- 'add_pod' => array( 'priv' => true ),
2244
- 'save_pod' => array( 'priv' => true ),
2245
- 'load_sister_fields' => array( 'priv' => true ),
2246
- 'process_form' => array( 'custom_nonce' => true ), // priv handled through nonce
2247
- 'upgrade' => array( 'priv' => true ),
2248
- 'migrate' => array( 'priv' => true )
2249
- );
 
2250
 
2251
- $methods = apply_filters( 'pods_admin_ajax_methods', $methods, $this );
2252
-
2253
- if ( !isset( $params->method ) || !isset( $methods[ $params->method ] ) )
2254
- pods_error( 'Invalid AJAX request', $this );
2255
-
2256
- $defaults = array(
2257
- 'priv' => null,
2258
- 'name' => $params->method,
2259
- 'custom_nonce' => null
2260
- );
2261
-
2262
- $method = (object) array_merge( $defaults, (array) $methods[ $params->method ] );
2263
 
2264
- if ( true !== $method->custom_nonce && ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, 'pods-' . $params->method ) ) )
2265
- pods_error( __( 'Unauthorized request', 'pods' ), $this );
 
2266
 
2267
- // Cleaning up $params
2268
- unset( $params->action );
2269
- unset( $params->method );
 
 
2270
 
2271
- if ( true !== $method->custom_nonce )
2272
- unset( $params->_wpnonce );
2273
 
2274
- // Check permissions (convert to array to support multiple)
2275
- if ( !empty( $method->priv ) && !pods_is_admin( array( 'pods' ) ) && true !== $method->priv && !pods_is_admin( $method->priv ) )
2276
- pods_error( __( 'Access denied', 'pods' ), $this );
2277
 
2278
- $params->method = $method->name;
 
 
2279
 
2280
- $params = apply_filters( 'pods_api_' . $method->name, $params, $method );
 
 
2281
 
2282
- $api = pods_api();
 
 
 
2283
 
2284
- if ( 'upgrade' == $method->name )
2285
- $output = (string) pods_upgrade( $params->version )->ajax( $params );
2286
- elseif ( 'migrate' == $method->name )
2287
- $output = (string) apply_filters( 'pods_api_migrate_run', $params );
2288
- else {
2289
- if ( !method_exists( $api, $method->name ) )
2290
- pods_error( 'API method does not exist', $this );
2291
- elseif ( 'save_pod' == $method->name ) {
2292
- if ( isset( $params->field_data_json ) && is_array( $params->field_data_json ) ) {
2293
- $params->fields = $params->field_data_json;
2294
 
2295
- unset( $params->field_data_json );
2296
 
2297
- foreach ( $params->fields as $k => $v ) {
2298
- if ( empty( $v ) )
2299
- unset( $params->fields[ $k ] );
2300
- elseif ( !is_array( $v ) )
2301
- $params->fields[ $k ] = (array) @json_decode( $v, true );
2302
- }
2303
- }
2304
- }
2305
 
2306
- // Dynamically call the API method
2307
- $params = (array) $params;
2308
 
2309
- $output = call_user_func( array( $api, $method->name ), $params );
2310
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2311
 
2312
- // Output in json format
2313
- if ( false !== $output ) {
2314
- if ( is_array( $output ) || is_object( $output ) )
2315
- wp_send_json( $output );
2316
- else
2317
- echo $output;
2318
- }
2319
- else
2320
- pods_error( 'There was a problem with your request.' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2321
 
2322
- die(); // KBAI!
2323
- }
 
2324
 
2325
  /**
2326
  * Profiles the Pods configuration
2327
  *
2328
- * @param null|string|array $pod. Optional. Which Pod(s) to get configuration for. Can be a the name of one Pod, or an array of names of Pods, or null, which is the default, to profile all Pods.
2329
- * @param bool $full_field_info Optional. If true all info about each field is returned. If false, which is the default only name and type, will be returned.
 
 
 
2330
  *
2331
  * @return array
2332
  *
2333
- * @since 3.0.0
2334
  */
2335
- function configuration( $pod = null, $full_field_info = false ){
 
2336
  $api = pods_api();
2337
 
2338
- if ( is_null( $pod ) ) {
2339
  $the_pods = $api->load_pods();
2340
- }
2341
- elseif( is_array( $pod ) ) {
2342
  foreach ( $pod as $p ) {
2343
  $the_pods[] = $api->load_pod( $p );
2344
  }
2345
- }
2346
- else {
2347
  $the_pods[] = $api->load_pod( $pod );
2348
  }
2349
 
2350
- foreach( $the_pods as $pod ) {
2351
- $configuration[ $pod[ 'name' ] ] = array(
2352
- 'name' => $pod['name'],
2353
- 'ID' => $pod[ 'id' ],
2354
- 'storage' => $pod[ 'storage' ],
2355
- 'fields' => $pod[ 'fields' ],
2356
  );
2357
  }
2358
 
2359
  if ( ! $full_field_info ) {
2360
- foreach ( $the_pods as $pod ) {
2361
- $fields = $configuration[ $pod['name'] ][ 'fields' ];
2362
- unset( $configuration[ $pod['name'] ][ 'fields' ] );
 
 
2363
  foreach ( $fields as $field ) {
2364
- $info = array (
2365
- 'name' => $field[ 'name' ],
2366
- 'type' => $field[ 'type' ],
2367
  );
2368
 
2369
- if ( $info[ 'type' ] === 'pick' ) {
2370
- $info[ 'pick_object' ] = $field[ 'pick_object' ];
2371
- if ( isset ( $field[ 'pick_val' ] ) && $field[ 'pick_val' ] !== '' ) {
2372
- $info[ 'pick_val' ] = $field[ 'pick_val' ];
 
2373
  }
2374
  }
2375
 
2376
  if ( is_array( $info ) ) {
2377
- $configuration[ $pod[ 'name' ] ][ 'fields' ][ $field[ 'name' ] ] = $info;
2378
  }
2379
 
2380
  unset( $info );
2381
 
2382
- }
2383
-
2384
- }
2385
 
 
 
2386
  }
2387
 
2388
- if ( is_array ( $configuration ) ) {
2389
- return $configuration;
2390
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2391
  }
2392
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2393
  }
2394
 
2395
  }
1
  <?php
2
+
3
  /**
4
  * @package Pods
5
  */
6
  class PodsAdmin {
7
 
8
+ /**
9
+ * @var PodsAdmin
10
+ */
11
+ public static $instance = null;
12
+
13
+ /**
14
+ * Singleton handling for a basic pods_admin() request
15
+ *
16
+ * @return \PodsAdmin
17
+ *
18
+ * @since 2.3.5
19
+ */
20
+ public static function init() {
21
+
22
+ if ( ! is_object( self::$instance ) ) {
23
+ self::$instance = new PodsAdmin();
24
+ }
25
+
26
+ return self::$instance;
27
+ }
28
+
29
+ /**
30
+ * Setup and Handle Admin functionality
31
+ *
32
+ * @return \PodsAdmin
33
+ *
34
+ * @license http://www.gnu.org/licenses/gpl-2.0.html
35
+ * @since 2.0.0
36
+ */
37
+ public function __construct() {
38
+
39
+ // Scripts / Stylesheets
40
+ add_action( 'admin_enqueue_scripts', array( $this, 'admin_head' ), 20 );
41
+
42
+ // AJAX $_POST fix
43
+ add_action( 'admin_init', array( $this, 'admin_init' ), 9 );
44
+
45
+ // Menus
46
+ add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 );
47
+
48
+ // AJAX for Admin
49
+ add_action( 'wp_ajax_pods_admin', array( $this, 'admin_ajax' ) );
50
+ add_action( 'wp_ajax_nopriv_pods_admin', array( $this, 'admin_ajax' ) );
51
+
52
+ // Add Media Bar button for Shortcode
53
+ add_action( 'media_buttons', array( $this, 'media_button' ), 12 );
54
+
55
+ // Add the Pods capabilities
56
+ add_filter( 'members_get_capabilities', array( $this, 'admin_capabilities' ) );
57
+
58
+ add_action( 'admin_head-media-upload-popup', array( $this, 'register_media_assets' ) );
59
+
60
+ // Add our debug to Site Info.
61
+ add_filter( 'debug_information', array( $this, 'add_debug_information' ) );
62
+
63
+ $this->rest_admin();
64
+
65
+ }
66
+
67
+ /**
68
+ * Init the admin area
69
+ *
70
+ * @since 2.0.0
71
+ */
72
+ public function admin_init() {
73
+
74
+ // Fix for plugins that *don't do it right* so we don't cause issues for users
75
+ // @codingStandardsIgnoreLine
76
+ if ( defined( 'DOING_AJAX' ) && ! empty( $_POST ) ) {
77
+ $pods_admin_ajax_actions = array(
78
+ 'pods_admin',
79
+ 'pods_relationship',
80
+ 'pods_upload',
81
+ 'pods_admin_components',
82
+ );
83
+
84
+ /**
85
+ * Admin AJAX Callbacks
86
+ *
87
+ * @since unknown
88
+ *
89
+ * @param array $pods_admin_ajax_actions Array of actions to handle.
90
+ */
91
+ $pods_admin_ajax_actions = apply_filters( 'pods_admin_ajax_actions', $pods_admin_ajax_actions );
92
+
93
+ if ( in_array( pods_v( 'action' ), $pods_admin_ajax_actions, true ) || in_array( pods_v( 'action', 'post' ), $pods_admin_ajax_actions, true ) ) {
94
+ // @codingStandardsIgnoreLine
95
+ foreach ( $_POST as $key => $value ) {
96
+ if ( 'action' === $key || 0 === strpos( $key, '_podsfix_' ) ) {
97
+ continue;
98
+ }
99
+
100
+ // @codingStandardsIgnoreLine
101
+ unset( $_POST[ $key ] );
102
+
103
+ // @codingStandardsIgnoreLine
104
+ $_POST[ '_podsfix_' . $key ] = $value;
105
+ }
106
+ }
107
+ }//end if
108
+ }
109
+
110
+ /**
111
+ * Attach requirements to admin header
112
+ *
113
+ * @since 2.0.0
114
+ */
115
+ public function admin_head() {
116
+
117
+ wp_register_script( 'pods-floatmenu', PODS_URL . 'ui/js/floatmenu.js', array(), PODS_VERSION );
118
+
119
+ wp_register_script( 'pods-admin-importer', PODS_URL . 'ui/js/admin-importer.js', array(), PODS_VERSION );
120
+
121
+ wp_register_script( 'pods-upgrade', PODS_URL . 'ui/js/jquery.pods.upgrade.js', array(), PODS_VERSION );
122
+
123
+ wp_register_script( 'pods-migrate', PODS_URL . 'ui/js/jquery.pods.migrate.js', array(), PODS_VERSION );
124
+
125
+ // @codingStandardsIgnoreLine
126
+ if ( isset( $_GET['page'] ) ) {
127
+ // @codingStandardsIgnoreLine
128
+ $page = $_GET['page'];
129
+ if ( 'pods' === $page || ( false !== strpos( $page, 'pods-' ) && 0 === strpos( $page, 'pods-' ) ) ) {
130
+ ?>
131
+ <script type="text/javascript">
132
+ var PODS_URL = "<?php echo esc_js( PODS_URL ); ?>";
133
+ </script>
134
+ <?php
135
+ wp_enqueue_script( 'jquery' );
136
+ wp_enqueue_script( 'jquery-ui-core' );
137
+ wp_enqueue_script( 'jquery-ui-sortable' );
138
+
139
+ wp_enqueue_script( 'pods-floatmenu' );
140
+
141
+ wp_enqueue_script( 'jquery-qtip2' );
142
+ wp_enqueue_script( 'pods-qtip-init' );
143
+
144
+ wp_enqueue_script( 'pods' );
145
+
146
+ if ( 0 === strpos( $page, 'pods-manage-' ) || 0 === strpos( $page, 'pods-add-new-' ) ) {
147
+ wp_enqueue_script( 'post' );
148
+ } elseif ( 0 === strpos( $page, 'pods-settings-' ) ) {
149
+ wp_enqueue_script( 'post' );
150
+ }
151
+
152
+ if ( 'pods-advanced' === $page ) {
153
+ wp_register_script( 'pods-advanced', PODS_URL . 'ui/js/advanced.js', array(), PODS_VERSION );
154
+ wp_enqueue_script( 'jquery-ui-effects-core', PODS_URL . 'ui/js/jquery-ui/jquery.effects.core.js', array( 'jquery' ), '1.8.8' );
155
+ wp_enqueue_script( 'jquery-ui-effects-fade', PODS_URL . 'ui/js/jquery-ui/jquery.effects.fade.js', array( 'jquery' ), '1.8.8' );
156
+ wp_enqueue_script( 'jquery-ui-dialog' );
157
+ wp_enqueue_script( 'pods-advanced' );
158
+ } elseif ( 'pods-packages' === $page ) {
159
+ wp_enqueue_style( 'pods-wizard' );
160
+ } elseif ( 'pods-wizard' === $page || 'pods-upgrade' === $page || ( in_array(
161
+ $page, array(
162
+ 'pods',
163
+ 'pods-add-new',
164
+ ), true
165
+ ) && in_array(
166
+ pods_v( 'action', 'get', 'manage' ), array(
167
+ 'add',
168
+ 'manage',
169
+ ), true
170
+ ) ) ) {
171
+ wp_enqueue_style( 'pods-wizard' );
172
+
173
+ if ( 'pods-upgrade' === $page ) {
174
+ wp_enqueue_script( 'pods-upgrade' );
175
+ }
176
+ }//end if
177
+ }//end if
178
+ }//end if
179
+
180
+ /**
181
+ * Filter to disable default loading of the DFV script. By default, Pods
182
+ * will always enqueue the DFV script if is_admin()
183
+ *
184
+ * Example: add_filter( 'pods_default_enqueue_dfv', '__return_false');
185
+ *
186
+ * @param bool Whether or not to enqueue by default
187
+ *
188
+ * @since 2.7.10
189
+ */
190
+ if ( apply_filters( 'pods_default_enqueue_dfv', true ) ) {
191
+ wp_enqueue_script( 'pods-dfv' );
192
+ }
193
+
194
+ // New Styles Enqueue
195
+ wp_enqueue_style( 'pods-styles' );
196
+ }
197
+
198
+ /**
199
+ * Build the admin menus
200
+ *
201
+ * @since 2.0.0
202
+ */
203
+ public function admin_menu() {
204
+
205
+ $advanced_content_types = PodsMeta::$advanced_content_types;
206
+ $taxonomies = PodsMeta::$taxonomies;
207
+ $settings = PodsMeta::$settings;
208
+
209
+ $all_pods = pods_api()->load_pods( array( 'count' => true ) );
210
+
211
+ if ( ! PodsInit::$upgrade_needed || ( pods_is_admin() && 1 === (int) pods_v( 'pods_upgrade_bypass' ) ) ) {
212
+ $submenu_items = array();
213
+
214
+ if ( ! empty( $advanced_content_types ) ) {
215
+ $submenu = array();
216
+
217
+ $pods_pages = 0;
218
+
219
+ foreach ( (array) $advanced_content_types as $pod ) {
220
+ if ( ! isset( $pod['name'] ) || ! isset( $pod['options'] ) || empty( $pod['fields'] ) ) {
221
+ continue;
222
+ } elseif ( ! pods_is_admin(
223
+ array(
224
+ 'pods',
225
+ 'pods_content',
226
+ 'pods_add_' . $pod['name'],
227
+ 'pods_edit_' . $pod['name'],
228
+ 'pods_delete_' . $pod['name'],
229
+ )
230
+ ) ) {
231
+ continue;
232
+ }
233
+
234
+ $pod_name = $pod['name'];
235
+
236
+ $pod = apply_filters( "pods_advanced_content_type_pod_data_{$pod_name}", $pod, $pod['name'] );
237
+ $pod = apply_filters( 'pods_advanced_content_type_pod_data', $pod, $pod['name'] );
238
+
239
+ if ( 1 === (int) pods_v( 'show_in_menu', $pod['options'], 0 ) ) {
240
+ $page_title = pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), true );
241
+ $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
242
+
243
+ $menu_label = pods_v( 'menu_name', $pod['options'], $page_title, true );
244
+ $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
245
+
246
+ $singular_label = pods_v( 'label_singular', $pod['options'], pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), true ), true );
247
+ $plural_label = pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), true );
248
+
249
+ $menu_location = pods_v( 'menu_location', $pod['options'], 'objects' );
250
+ $menu_location_custom = pods_v( 'menu_location_custom', $pod['options'], '' );
251
+
252
+ $menu_position = pods_v( 'menu_position', $pod['options'], '', true );
253
+ $menu_icon = pods_evaluate_tags( pods_v( 'menu_icon', $pod['options'], '', true ), true );
254
+
255
+ if ( empty( $menu_position ) ) {
256
+ $menu_position = null;
257
+ }
258
+
259
+ $parent_page = null;
260
+
261
+ if ( pods_is_admin(
262
+ array(
263
+ 'pods',
264
+ 'pods_content',
265
+ 'pods_edit_' . $pod['name'],
266
+ 'pods_delete_' . $pod['name'],
267
+ )
268
+ ) ) {
269
+ if ( ! empty( $menu_location_custom ) ) {
270
+ if ( ! isset( $submenu_items[ $menu_location_custom ] ) ) {
271
+ $submenu_items[ $menu_location_custom ] = array();
272
+ }
273
+
274
+ $submenu_items[ $menu_location_custom ][] = array(
275
+ $menu_location_custom,
276
+ $page_title,
277
+ $menu_label,
278
+ 'read',
279
+ 'pods-manage-' . $pod['name'],
280
+ array( $this, 'admin_content' ),
281
+ );
282
+
283
+ continue;
284
+ } else {
285
+ $pods_pages ++;
286
+
287
+ $page = 'pods-manage-' . $pod['name'];
288
+ $parent_page = $page;
289
+
290
+ if ( empty( $menu_position ) ) {
291
+ $menu_position = null;
292
+ }
293
+ add_menu_page( $page_title, $menu_label, 'read', $parent_page, '', $menu_icon, $menu_position );
294
+
295
+ $all_title = $plural_label;
296
+ $all_label = pods_v( 'label_all_items', $pod['options'], __( 'All', 'pods' ) . ' ' . $plural_label );
297
+
298
+ if ( pods_v( 'page' ) === $page ) {
299
+ if ( 'edit' === pods_v( 'action', 'get', 'manage' ) ) {
300
+ $all_title = pods_v( 'label_edit_item', $pod['options'], __( 'Edit', 'pods' ) . ' ' . $singular_label );
301
+ } elseif ( 'add' === pods_v( 'action', 'get', 'manage' ) ) {
302
+ $all_title = pods_v( 'label_add_new_item', $pod['options'], __( 'Add New', 'pods' ) . ' ' . $singular_label );
303
+ }
304
+ }
305
+
306
+ add_submenu_page(
307
+ $parent_page, $all_title, $all_label, 'read', $page, array(
308
+ $this,
309
+ 'admin_content',
310
+ )
311
+ );
312
+ }//end if
313
+ }//end if
314
+
315
+ if ( pods_is_admin( array( 'pods', 'pods_content', 'pods_add_' . $pod['name'] ) ) ) {
316
+ $page = 'pods-add-new-' . $pod['name'];
317
+
318
+ if ( null === $parent_page ) {
319
+ $pods_pages ++;
320
+
321
+ $parent_page = $page;
322
+
323
+ if ( empty( $menu_position ) ) {
324
+ $menu_position = null;
325
+ }
326
+ add_menu_page( $page_title, $menu_label, 'read', $parent_page, '', $menu_icon, $menu_position );
327
+ }
328
+
329
+ $add_title = pods_v( 'label_add_new_item', $pod['options'], __( 'Add New', 'pods' ) . ' ' . $singular_label );
330
+ $add_label = pods_v( 'label_add_new', $pod['options'], __( 'Add New', 'pods' ) );
331
+
332
+ add_submenu_page(
333
+ $parent_page, $add_title, $add_label, 'read', $page, array(
334
+ $this,
335
+ 'admin_content',
336
+ )
337
+ );
338
+ }//end if
339
+ } else {
340
+ $submenu[] = $pod;
341
+ }//end if
342
+ }//end foreach
343
+
344
+ $submenu = apply_filters( 'pods_admin_menu_secondary_content', $submenu );
345
+
346
+ if ( ! empty( $submenu ) && ( ! defined( 'PODS_DISABLE_CONTENT_MENU' ) || ! PODS_DISABLE_CONTENT_MENU ) ) {
347
+ $parent_page = null;
348
+
349
+ foreach ( $submenu as $item ) {
350
+ $singular_label = pods_v( 'label_singular', $item['options'], pods_v( 'label', $item, ucwords( str_replace( '_', ' ', $item['name'] ) ), true ), true );
351
+ $plural_label = pods_v( 'label', $item, ucwords( str_replace( '_', ' ', $item['name'] ) ), true );
352
+
353
+ if ( pods_is_admin(
354
+ array(
355
+ 'pods',
356
+ 'pods_content',
357
+ 'pods_edit_' . $item['name'],
358
+ 'pods_delete_' . $item['name'],
359
+ )
360
+ ) ) {
361
+ $page = 'pods-manage-' . $item['name'];
362
+
363
+ if ( null === $parent_page ) {
364
+ $parent_page = $page;
365
+
366
+ add_menu_page( 'Pods', 'Pods', 'read', $parent_page, null, 'dashicons-pods', '58.5' );
367
+ }
368
+
369
+ $all_title = $plural_label;
370
+ $all_label = __( 'Manage', 'pods' ) . ' ' . $plural_label;
371
+
372
+ if ( pods_v( 'page' ) === $page ) {
373
+ if ( 'edit' === pods_v( 'action', 'get', 'manage' ) ) {
374
+ $all_title = __( 'Edit', 'pods' ) . ' ' . $singular_label;
375
+ } elseif ( 'add' === pods_v( 'action', 'get', 'manage' ) ) {
376
+ $all_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
377
+ }
378
+ }
379
+
380
+ add_submenu_page(
381
+ $parent_page, $all_title, $all_label, 'read', $page, array(
382
+ $this,
383
+ 'admin_content',
384
+ )
385
+ );
386
+ } elseif ( current_user_can( 'pods_add_' . $item['name'] ) ) {
387
+ $page = 'pods-add-new-' . $item['name'];
388
+
389
+ if ( null === $parent_page ) {
390
+ $parent_page = $page;
391
+
392
+ add_menu_page( 'Pods', 'Pods', 'read', $parent_page, null, 'dashicons-pods', '58.5' );
393
+ }
394
+
395
+ $add_title = __( 'Add New', 'pods' ) . ' ' . $singular_label;
396
+ $add_label = __( 'Manage', 'pods' ) . ' ' . $plural_label;
397
+
398
+ add_submenu_page(
399
+ $parent_page, $add_title, $add_label, 'read', $page, array(
400
+ $this,
401
+ 'admin_content',
402
+ )
403
+ );
404
+ }//end if
405
+ }//end foreach
406
+ }//end if
407
+ }//end if
408
+
409
+ if ( ! empty( $taxonomies ) ) {
410
+ foreach ( (array) $taxonomies as $pod ) {
411
+ // Default taxonomy capability
412
+ $capability = 'manage_categories';
413
+
414
+ if ( ! empty( $pod['options']['capability_type'] ) ) {
415
+ if ( 'custom' === $pod['options']['capability_type'] && ! empty( $pod['options']['capability_type_custom'] ) ) {
416
+ $capability = 'manage_' . (string) $pod['options']['capability_type_custom'] . '_terms';
417
+ }
418
+ }
419
+
420
+ // Check capabilities.
421
+ if ( ! pods_is_admin(
422
+ array(
423
+ 'pods',
424
+ 'pods_content',
425
+ 'pods_edit_' . $pod['name'],
426
+ $capability,
427
+ )
428
+ ) ) {
429
+ continue;
430
+ }
431
+
432
+ // Check UI settings
433
+ if ( 1 !== (int) pods_v( 'show_ui', $pod['options'], 0 ) || 1 !== (int) pods_v( 'show_in_menu', $pod['options'], 0 ) ) {
434
+ continue;
435
+ }
436
+
437
+ $menu_location = pods_v( 'menu_location', $pod['options'], 'default' );
438
+
439
+ if ( 'default' === $menu_location ) {
440
+ continue;
441
+ }
442
+
443
+ $page_title = pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), true );
444
+ $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
445
+
446
+ $menu_label = pods_v( 'menu_name', $pod['options'], $page_title, true );
447
+ $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
448
+
449
+ $menu_icon = pods_evaluate_tags( pods_v( 'menu_icon', $pod['options'], '', true ), true );
450
+ $menu_slug = 'edit-tags.php?taxonomy=' . $pod['name'];
451
+ $menu_location_custom = pods_v( 'menu_location_custom', $pod['options'], '' );
452
+
453
+ $menu_position = pods_v( 'menu_position', $pod['options'], '', true );
454
+ if ( empty( $menu_position ) ) {
455
+ $menu_position = null;
456
+ }
457
+
458
+ $taxonomy_data = get_taxonomy( $pod['name'] );
459
+
460
+ foreach ( (array) $taxonomy_data->object_type as $post_type ) {
461
+ if ( 'post' === $post_type ) {
462
+ remove_submenu_page( 'edit.php', $menu_slug );
463
+ } elseif ( 'attachment' === $post_type ) {
464
+ remove_submenu_page( 'upload.php', $menu_slug . '&amp;post_type=' . $post_type );
465
+ } else {
466
+ remove_submenu_page( 'edit.php?post_type=' . $post_type, $menu_slug . '&amp;post_type=' . $post_type );
467
+ }
468
+ }
469
+
470
+ if ( 'settings' === $menu_location ) {
471
+ add_options_page( $page_title, $menu_label, 'read', $menu_slug );
472
+ } elseif ( 'appearances' === $menu_location ) {
473
+ add_theme_page( $page_title, $menu_label, 'read', $menu_slug );
474
+ } elseif ( 'objects' === $menu_location || 'top' === $menu_location ) {
475
+ add_menu_page( $page_title, $menu_label, 'read', $menu_slug, '', $menu_icon, $menu_position );
476
+ } elseif ( 'submenu' === $menu_location && ! empty( $menu_location_custom ) ) {
477
+ if ( ! isset( $submenu_items[ $menu_location_custom ] ) ) {
478
+ $submenu_items[ $menu_location_custom ] = array();
479
+ }
480
+
481
+ $submenu_items[ $menu_location_custom ][] = array(
482
+ $menu_location_custom,
483
+ $page_title,
484
+ $menu_label,
485
+ 'read',
486
+ $menu_slug,
487
+ '',
488
+ );
489
+ }//end if
490
+ }//end foreach
491
+ }//end if
492
+
493
+ if ( ! empty( $settings ) ) {
494
+ foreach ( (array) $settings as $pod ) {
495
+ if ( ! pods_is_admin( array( 'pods', 'pods_content', 'pods_edit_' . $pod['name'] ) ) ) {
496
+ continue;
497
+ }
498
+
499
+ $page_title = pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', $pod['name'] ) ), true );
500
+ $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod );
501
+
502
+ $menu_label = pods_v( 'menu_name', $pod['options'], $page_title, true );
503
+ $menu_label = apply_filters( 'pods_admin_menu_label', $menu_label, $pod );
504
+
505
+ $menu_icon = pods_evaluate_tags( pods_v( 'menu_icon', $pod['options'], '', true ), true );
506
+
507
+ $menu_position = pods_v( 'menu_position', $pod['options'], '', true );
508
+ if ( empty( $menu_position ) ) {
509
+ $menu_position = null;
510
+ }
511
+
512
+ $menu_slug = 'pods-settings-' . $pod['name'];
513
+ $menu_location = pods_v( 'menu_location', $pod['options'], 'settings' );
514
+ $menu_location_custom = pods_v( 'menu_location_custom', $pod['options'], '' );
515
+ $menu_callback = array( $this, 'admin_content_settings' );
516
+
517
+ if ( 'settings' === $menu_location ) {
518
+ add_options_page( $page_title, $menu_label, 'read', $menu_slug, $menu_callback, $menu_position );
519
+ } elseif ( 'appearances' === $menu_location ) {
520
+ add_theme_page( $page_title, $menu_label, 'read', $menu_slug, $menu_callback, $menu_position );
521
+ } elseif ( 'objects' === $menu_location || 'top' === $menu_location ) {
522
+ add_menu_page( $page_title, $menu_label, 'read', $menu_slug, $menu_callback, $menu_icon, $menu_position );
523
+ } elseif ( 'submenu' === $menu_location && ! empty( $menu_location_custom ) ) {
524
+ if ( ! isset( $submenu_items[ $menu_location_custom ] ) ) {
525
+ $submenu_items[ $menu_location_custom ] = array();
526
+ }
527
+
528
+ $submenu_items[ $menu_location_custom ][] = array(
529
+ $menu_location_custom,
530
+ $page_title,
531
+ $menu_label,
532
+ 'read',
533
+ $menu_slug,
534
+ $menu_callback,
535
+ $menu_position,
536
+ );
537
+ }//end if
538
+ }//end foreach
539
+ }//end if
540
+
541
+ foreach ( $submenu_items as $items ) {
542
+ foreach ( $items as $item ) {
543
+ call_user_func_array( 'add_submenu_page', $item );
544
+ }
545
+ }
546
+
547
+ $admin_menus = array(
548
+ 'pods' => array(
549
+ 'label' => __( 'Edit Pods', 'pods' ),
550
+ 'function' => array( $this, 'admin_setup' ),
551
+ 'access' => 'pods',
552
+ ),
553
+ 'pods-add-new' => array(
554
+ 'label' => __( 'Add New', 'pods' ),
555
+ 'function' => array( $this, 'admin_setup' ),
556
+ 'access' => 'pods',
557
+ ),
558
+ 'pods-components' => array(
559
+ 'label' => __( 'Components', 'pods' ),
560
+ 'function' => array( $this, 'admin_components' ),
561
+ 'access' => 'pods_components',
562
+ ),
563
+ 'pods-settings' => array(
564
+ 'label' => __( 'Settings', 'pods' ),
565
+ 'function' => array( $this, 'admin_settings' ),
566
+ 'access' => 'pods_settings',
567
+ ),
568
+ 'pods-help' => array(
569
+ 'label' => __( 'Help', 'pods' ),
570
+ 'function' => array( $this, 'admin_help' ),
571
+ ),
572
+ );
573
+
574
+ if ( empty( $all_pods ) ) {
575
+ unset( $admin_menus['pods'] );
576
+
577
+ if ( 'pods' === pods_v( 'page', 'get' ) ) {
578
+ // Replace `pods` page param with first existing pod page and redirect.
579
+ $url = add_query_arg( 'page', key( $admin_menus ) );
580
+ $url = get_site_url( null, $url );
581
+
582
+ wp_safe_redirect( $url );
583
+ die();
584
+ }
585
+ }
586
+
587
+ add_filter( 'parent_file', array( $this, 'parent_file' ) );
588
+ } else {
589
+ $admin_menus = array(
590
+ 'pods-upgrade' => array(
591
+ 'label' => __( 'Upgrade', 'pods' ),
592
+ 'function' => array( $this, 'admin_upgrade' ),
593
+ 'access' => 'manage_options',
594
+ ),
595
+ 'pods-settings' => array(
596
+ 'label' => __( 'Settings', 'pods' ),
597
+ 'function' => array( $this, 'admin_settings' ),
598
+ 'access' => 'pods_settings',
599
+ ),
600
+ 'pods-help' => array(
601
+ 'label' => __( 'Help', 'pods' ),
602
+ 'function' => array( $this, 'admin_help' ),
603
+ ),
604
+ );
605
+
606
+ add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
607
+ }//end if
608
+
609
+ /**
610
+ * Add or change Pods Admin menu items
611
+ *
612
+ * @param array $admin_menus The submenu items in Pods Admin menu.
613
+ *
614
+ * @since unknown
615
+ */
616
+ $admin_menus = apply_filters( 'pods_admin_menu', $admin_menus );
617
+
618
+ $parent = false;
619
+
620
+ // PODS_LIGHT disables all Pods components so remove the components menu
621
+ if ( pods_light() ) {
622
+ unset( $admin_menus['pods-components'] );
623
+ }
624
+
625
+ if ( ! empty( $admin_menus ) && ( ! defined( 'PODS_DISABLE_ADMIN_MENU' ) || ! PODS_DISABLE_ADMIN_MENU ) ) {
626
+ foreach ( $admin_menus as $page => $menu_item ) {
627
+ if ( ! pods_is_admin( pods_v( 'access', $menu_item ) ) ) {
628
+ continue;
629
+ }
630
+
631
+ // Don't just show the help page
632
+ if ( false === $parent && 'pods-help' === $page ) {
633
+ continue;
634
+ }
635
+
636
+ if ( ! isset( $menu_item['label'] ) ) {
637
+ $menu_item['label'] = $page;
638
+ }
639
+
640
+ if ( false === $parent ) {
641
+ $parent = $page;
642
+
643
+ $menu = __( 'Pods Admin', 'pods' );
644
+
645
+ if ( 'pods-upgrade' === $parent ) {
646
+ $menu = __( 'Pods Upgrade', 'pods' );
647
+ }
648
+
649
+ add_menu_page( $menu, $menu, 'read', $parent, null, 'dashicons-pods' );
650
+ }
651
+
652
+ add_submenu_page( $parent, $menu_item['label'], $menu_item['label'], 'read', $page, $menu_item['function'] );
653
+
654
+ if ( 'pods-components' === $page && is_object( PodsInit::$components ) ) {
655
+ PodsInit::$components->menu( $parent );
656
+ }
657
+ }//end foreach
658
+ }//end if
659
+ }
660
+
661
+ /**
662
+ * Set the correct parent_file to highlight the correct top level menu
663
+ *
664
+ * @param string $parent_file The parent file.
665
+ *
666
+ * @return mixed|string
667
+ *
668
+ * @since unknown
669
+ */
670
+ public function parent_file( $parent_file ) {
671
+
672
+ global $current_screen;
673
+
674
+ if ( isset( $current_screen ) && ! empty( $current_screen->taxonomy ) ) {
675
+ $taxonomies = PodsMeta::$taxonomies;
676
+ if ( ! empty( $taxonomies ) ) {
677
+ foreach ( (array) $taxonomies as $pod ) {
678
+ if ( $current_screen->taxonomy !== $pod['name'] ) {
679
+ continue;
680
+ }
681
+
682
+ $menu_slug = 'edit-tags.php?taxonomy=' . $pod['name'];
683
+ $menu_location = pods_v( 'menu_location', $pod['options'], 'default' );
684
+ $menu_location_custom = pods_v( 'menu_location_custom', $pod['options'], '' );
685
+
686
+ if ( 'settings' === $menu_location ) {
687
+ $parent_file = 'options-general.php';
688
+ } elseif ( 'appearances' === $menu_location ) {
689
+ $parent_file = 'themes.php';
690
+ } elseif ( 'objects' === $menu_location ) {
691
+ $parent_file = $menu_slug;
692
+ } elseif ( 'top' === $menu_location ) {
693
+ $parent_file = $menu_slug;
694
+ } elseif ( 'submenu' === $menu_location && ! empty( $menu_location_custom ) ) {
695
+ $parent_file = $menu_location_custom;
696
+ }
697
+
698
+ break;
699
+ }//end foreach
700
+ }//end if
701
+ }//end if
702
+
703
+ if ( isset( $current_screen ) && ! empty( $current_screen->post_type ) && is_object( PodsInit::$components ) ) {
704
+ global $submenu_file;
705
+
706
+ $components = PodsInit::$components->components;
707
+
708
+ foreach ( $components as $component => $component_data ) {
709
+ if ( ! empty( $component_data['MenuPage'] ) && $parent_file === $component_data['MenuPage'] ) {
710
+ $parent_file = 'pods';
711
+
712
+ // @codingStandardsIgnoreLine
713
+ $submenu_file = $component_data['MenuPage'];
714
+ }
715
+ }
716
+ }
717
+
718
+ return $parent_file;
719
+ }
720
+
721
+ /**
722
+ * Show upgrade notice.
723
+ */
724
+ public function upgrade_notice() {
725
+
726
+ echo '<div class="error fade"><p>';
727
+ // @codingStandardsIgnoreLine
728
+ echo sprintf( __( '<strong>NOTICE:</strong> Pods %1$s requires your action to complete the upgrade. Please run the <a href="%2$s">Upgrade Wizard</a>.', 'pods' ), esc_html( PODS_VERSION ), esc_url( admin_url( 'admin.php?page=pods-upgrade' ) ) );
729
+ echo '</p></div>';
730
+ }
731
+
732
+ /**
733
+ * Create PodsUI content for the administration pages
734
+ */
735
+ public function admin_content() {
736
+
737
+ // @codingStandardsIgnoreLine
738
+ $pod_name = str_replace( array( 'pods-manage-', 'pods-add-new-' ), '', $_GET['page'] );
739
+
740
+ $pod = pods( $pod_name, pods_v( 'id', 'get', null, true ) );
741
+
742
+ // @codingStandardsIgnoreLine
743
+ if ( false !== strpos( $_GET['page'], 'pods-add-new-' ) ) {
744
+ // @codingStandardsIgnoreLine
745
+ $_GET['action'] = pods_v( 'action', 'get', 'add' );
746
+ }
747
+
748
+ $pod->ui();
749
+ }
750
+
751
+ /**
752
+ * Create PodsUI content for the settings administration pages
753
+ */
754
+ public function admin_content_settings() {
755
+
756
+ // @codingStandardsIgnoreLine
757
+ $pod_name = str_replace( 'pods-settings-', '', $_GET['page'] );
758
+
759
+ $pod = pods( $pod_name );
760
+
761
+ if ( 'custom' !== pods_v( 'ui_style', $pod->pod_data['options'], 'settings', true ) ) {
762
+ $actions_disabled = array(
763
+ 'manage' => 'manage',
764
+ 'add' => 'add',
765
+ 'delete' => 'delete',
766
+ 'duplicate' => 'duplicate',
767
+ 'view' => 'view',
768
+ 'export' => 'export',
769
+ );
770
+
771
+ // @codingStandardsIgnoreLine
772
+ $_GET['action'] = 'edit';
773
+
774
+ $page_title = pods_v( 'label', $pod->pod_data, ucwords( str_replace( '_', ' ', $pod->pod_data['name'] ) ), true );
775
+ $page_title = apply_filters( 'pods_admin_menu_page_title', $page_title, $pod->pod_data );
776
+
777
+ $ui = array(
778
+ 'pod' => $pod,
779
+ 'fields' => array(
780
+ 'edit' => $pod->pod_data['fields'],
781
+ ),
782
+ 'header' => array(
783
+ 'edit' => $page_title,
784
+ ),
785
+ 'label' => array(
786
+ 'edit' => __( 'Save Changes', 'pods' ),
787
+ ),
788
+ 'style' => pods_v( 'ui_style', $pod->pod_data['options'], 'settings', true ),
789
+ 'icon' => pods_evaluate_tags( pods_v( 'menu_icon', $pod->pod_data['options'] ), true ),
790
+ 'actions_disabled' => $actions_disabled,
791
+ );
792
+
793
+ $pod_pod_name = $pod->pod;
794
+
795
+ $ui = apply_filters( "pods_admin_ui_{$pod_pod_name}", apply_filters( 'pods_admin_ui', $ui, $pod->pod, $pod ), $pod->pod, $pod );
796
+
797
+ // Force disabled actions, do not pass go, do not collect $two_hundred
798
+ $ui['actions_disabled'] = $actions_disabled;
799
+
800
+ pods_ui( $ui );
801
+ } else {
802
+ $pod_pod_name = $pod->pod;
803
+ do_action( 'pods_admin_ui_custom', $pod );
804
+ do_action( "pods_admin_ui_custom_{$pod_pod_name}", $pod );
805
+ }//end if
806
+ }
807
+
808
+ /**
809
+ * Add media button for Pods shortcode
810
+ *
811
+ * @param string $context Media button context.
812
+ *
813
+ * @return string
814
+ */
815
+ public function media_button( $context = null ) {
816
+
817
+ // If shortcodes are disabled don't show the button
818
+ if ( defined( 'PODS_DISABLE_SHORTCODE' ) && PODS_DISABLE_SHORTCODE ) {
819
+ return '';
820
+ }
821
+
822
+ /**
823
+ * Filter to remove Pods shortcode button from the post editor.
824
+ *
825
+ * @param bool $show_button Set to false to block the shortcode button from appearing.
826
+ * @param string $context Media button context.
827
+ *
828
+ * @since 2.3.19
829
+ */
830
+ if ( ! apply_filters( 'pods_admin_media_button', true, $context ) ) {
831
+ return '';
832
+ }
833
+
834
+ $current_page = basename( $_SERVER['PHP_SELF'] );
835
+ $current_page = explode( '?', $current_page );
836
+ $current_page = explode( '#', $current_page[0] );
837
+ $current_page = $current_page[0];
838
+
839
+ // Only show the button on post type pages
840
+ if ( ! in_array(
841
+ $current_page, array(
842
+ 'post-new.php',
843
+ 'post.php',
844
+ ), true
845
+ ) ) {
846
+ return '';
847
+ }
848
+
849
+ add_action( 'admin_footer', array( $this, 'mce_popup' ) );
850
+
851
+ echo '<a href="#TB_inline?width=640&inlineId=pods_shortcode_form" class="thickbox button" id="add_pod_button" title="Pods Shortcode"><img style="padding: 0px 6px 0px 0px; margin: -3px 0px 0px;" src="' . esc_url( PODS_URL . 'ui/images/icon16.png' ) . '" alt="' . esc_attr__( 'Pods Shortcode', 'pods' ) . '" />' . esc_html__( 'Pods Shortcode', 'pods' ) . '</a>';
852
+ }
853
+
854
+ /**
855
+ * Enqueue assets for Media Library Popup
856
+ */
857
+ public function register_media_assets() {
858
+
859
+ if ( 'pods_media_attachment' === pods_v( 'inlineId' ) ) {
860
+ wp_enqueue_style( 'pods-styles' );
861
+ }
862
+ }
863
+
864
+ /**
865
+ * Output Pods shortcode popup window
866
+ */
867
+ public function mce_popup() {
868
+
869
+ pods_view( PODS_DIR . 'ui/admin/shortcode.php', compact( array_keys( get_defined_vars() ) ) );
870
+ }
871
+
872
+ /**
873
+ * Handle main Pods Setup area for managing Pods and Fields
874
+ */
875
+ public function admin_setup() {
876
+
877
+ $pods = pods_api()->load_pods( array( 'fields' => false ) );
878
+
879
+ $view = pods_v( 'view', 'get', 'all', true );
880
+
881
+ // @codingStandardsIgnoreLine
882
+ if ( empty( $pods ) && ! isset( $_GET['action'] ) ) {
883
+ // @codingStandardsIgnoreLine
884
+ $_GET['action'] = 'add';
885
+ }
886
+
887
+ // @codingStandardsIgnoreLine
888
+ if ( 'pods-add-new' === $_GET['page'] ) {
889
+ // @codingStandardsIgnoreLine
890
+ if ( isset( $_GET['action'] ) && 'add' !== $_GET['action'] ) {
891
+ pods_redirect(
892
+ pods_query_arg(
893
+ array(
894
+ 'page' => 'pods',
895
+ // @codingStandardsIgnoreLine
896
+ 'action' => $_GET['action'],
897
+ )
898
+ )
899
+ );
900
+ } else {
901
+ // @codingStandardsIgnoreLine
902
+ $_GET['action'] = 'add';
903
+ }
904
+ // @codingStandardsIgnoreLine
905
+ } elseif ( isset( $_GET['action'] ) && 'add' === $_GET['action'] ) {
906
+ pods_redirect(
907
+ pods_query_arg(
908
+ array(
909
+ 'page' => 'pods-add-new',
910
+ 'action' => '',
911
+ )
912
+ )
913
+ );
914
+ }//end if
915
+
916
+ $types = array(
917
+ 'post_type' => __( 'Post Type (extended)', 'pods' ),
918
+ 'taxonomy' => __( 'Taxonomy (extended)', 'pods' ),
919
+ 'cpt' => __( 'Custom Post Type', 'pods' ),
920
+ 'ct' => __( 'Custom Taxonomy', 'pods' ),
921
+ 'user' => __( 'User (extended)', 'pods' ),
922
+ 'media' => __( 'Media (extended)', 'pods' ),
923
+ 'comment' => __( 'Comments (extended)', 'pods' ),
924
+ 'pod' => __( 'Advanced Content Type', 'pods' ),
925
+ 'settings' => __( 'Custom Settings Page', 'pods' ),
926
+ );
927
+
928
+ $row = false;
929
+
930
+ $pod_types_found = array();
931
+
932
+ $fields = array(
933
+ 'label' => array( 'label' => __( 'Label', 'pods' ) ),
934
+ 'name' => array( 'label' => __( 'Name', 'pods' ) ),
935
+ 'type' => array( 'label' => __( 'Type', 'pods' ) ),
936
+ 'storage' => array(
937
+ 'label' => __( 'Storage Type', 'pods' ),
938
+ 'width' => '10%',
939
+ ),
940
+ 'field_count' => array(
941
+ 'label' => __( 'Number of Fields', 'pods' ),
942
+ 'width' => '8%',
943
+ ),
944
+ );
945
+
946
+ $total_fields = 0;
947
+
948
+ foreach ( $pods as $k => $pod ) {
949
+ if ( isset( $types[ $pod['type'] ] ) ) {
950
+ if ( in_array(
951
+ $pod['type'], array(
952
+ 'post_type',
953
+ 'taxonomy',
954
+ ), true
955
+ ) ) {
956
+ if ( empty( $pod['object'] ) ) {
957
+ if ( 'post_type' === $pod['type'] ) {
958
+ $pod['type'] = 'cpt';
959
+ } else {
960
+ $pod['type'] = 'ct';
961
+ }
962
+ }
963
+ }
964
+
965
+ if ( ! isset( $pod_types_found[ $pod['type'] ] ) ) {
966
+ $pod_types_found[ $pod['type'] ] = 1;
967
+ } else {
968
+ $pod_types_found[ $pod['type'] ] ++;
969
+ }
970
+
971
+ if ( 'all' !== $view && $view !== $pod['type'] ) {
972
+ unset( $pods[ $k ] );
973
+
974
+ continue;
975
+ }
976
+
977
+ $pod['real_type'] = $pod['type'];
978
+ $pod['type'] = $types[ $pod['type'] ];
979
+ } elseif ( 'all' !== $view ) {
980
+ continue;
981
+ }//end if
982
+
983
+ $pod['storage'] = ucwords( $pod['storage'] );
984
+
985
+ // @codingStandardsIgnoreLine
986
+ if ( $pod['id'] == pods_v( 'id' ) && 'delete' !== pods_v( 'action' ) ) {
987
+ $row = $pod;
988
+ }
989
+
990
+ $pod = array(
991
+ 'id' => $pod['id'],
992
+ 'label' => pods_v( 'label', $pod ),
993
+ 'name' => pods_v( 'name', $pod ),
994
+ 'object' => pods_v( 'object', $pod ),
995
+ 'type' => pods_v( 'type', $pod ),
996
+ 'real_type' => pods_v( 'real_type', $pod ),
997
+ 'storage' => pods_v( 'storage', $pod ),
998
+ 'field_count' => count( $pod['fields'] ),
999
+ );
1000
+
1001
+ $total_fields += $pod['field_count'];
1002
+
1003
+ $pods[ $k ] = $pod;
1004
+ }//end foreach
1005
+
1006
+ if ( false === $row && 0 < pods_v( 'id' ) && 'delete' !== pods_v( 'action' ) ) {
1007
+ pods_message( 'Pod not found', 'error' );
1008
+
1009
+ // @codingStandardsIgnoreLine
1010
+ unset( $_GET['id'], $_GET['action'] );
1011
+ }
1012
+
1013
+ $ui = array(
1014
+ 'data' => $pods,
1015
+ 'row' => $row,
1016
+ 'total' => count( $pods ),
1017
+ 'total_found' => count( $pods ),
1018
+ 'items' => 'Pods',
1019
+ 'item' => 'Pod',
1020
+ 'fields' => array(
1021
+ 'manage' => $fields,
1022
+ ),
1023
+ 'actions_disabled' => array( 'view', 'export' ),
1024
+ 'actions_custom' => array(
1025
+ 'add' => array( $this, 'admin_setup_add' ),
1026
+ 'edit' => array( $this, 'admin_setup_edit' ),
1027
+ 'duplicate' => array(
1028
+ 'callback' => array( $this, 'admin_setup_duplicate' ),
1029
+ 'restrict_callback' => array( $this, 'admin_setup_duplicate_restrict' ),
1030
+ ),
1031
+ 'reset' => array(
1032
+ 'label' => __( 'Delete All Items', 'pods' ),
1033
+ 'confirm' => __( 'Are you sure you want to delete all items from this Pod? If this is an extended Pod, it will remove the original items extended too.', 'pods' ),
1034
+ 'callback' => array( $this, 'admin_setup_reset' ),
1035
+ 'restrict_callback' => array( $this, 'admin_setup_reset_restrict' ),
1036
+ 'nonce' => true,
1037
+ ),
1038
+ 'delete' => array( $this, 'admin_setup_delete' ),
1039
+ ),
1040
+ 'action_links' => array(
1041
+ 'add' => pods_query_arg(
1042
+ array(
1043
+ 'page' => 'pods-add-new',
1044
+ 'action' => '',
1045
+ 'id' => '',
1046
+ 'do' => '',
1047
+ )
1048
+ ),
1049
+ ),
1050
+ 'search' => false,
1051
+ 'searchable' => false,
1052
+ 'sortable' => true,
1053
+ 'pagination' => false,
1054
+ 'extra' => array(
1055
+ 'total' => ', ' . number_format_i18n( $total_fields ) . ' ' . _n( 'field', 'fields', $total_fields, 'pods' ),
1056
+ ),
1057
+ );
1058
+
1059
+ if ( 1 < count( $pod_types_found ) ) {
1060
+ $ui['views'] = array( 'all' => __( 'All', 'pods' ) );
1061
+ $ui['view'] = $view;
1062
+ $ui['heading'] = array( 'views' => __( 'Type', 'pods' ) );
1063
+ $ui['filters_enhanced'] = true;
1064
+
1065
+ foreach ( $pod_types_found as $pod_type => $number_found ) {
1066
+ $ui['views'][ $pod_type ] = $types[ $pod_type ];
1067
+ }
1068
+ }
1069
+
1070
+ // Add our custom callouts.
1071
+ $this->handle_callouts_updates();
1072
+
1073
+ add_filter( 'pods_ui_manage_custom_container_classes', array( $this, 'admin_manage_container_class' ) );
1074
+ add_action( 'pods_ui_manage_after_container', array( $this, 'admin_manage_callouts' ) );
1075
+
1076
+ pods_ui( $ui );
1077
+ }
1078
+
1079
+ /**
1080
+ * Get list of callouts to show.
1081
+ *
1082
+ * @since 2.7.17
1083
+ *
1084
+ * @return array List of callouts.
1085
+ */
1086
+ public function get_callouts() {
1087
+ $force_callouts = false;
1088
+
1089
+ $page = pods_v( 'page' );
1090
+
1091
+ if ( in_array( $page, array( 'pods-settings', 'pods-help' ), true ) ) {
1092
+ $force_callouts = true;
1093
+ }
1094
+
1095
+ $callouts = get_option( 'pods_callouts' );
1096
+
1097
+ if ( ! $callouts ) {
1098
+ $callouts = array(
1099
+ 'friends_2020' => 1,
1100
+ );
1101
+ }
1102
+
1103
+ // Handle Friends of Pods 2020 callout logic.
1104
+ $callouts['friends_2020'] = ! isset( $callouts['friends_2020'] ) || $callouts['friends_2020'] || $force_callouts ? 1 : 0;
1105
+
1106
+ /**
1107
+ * Allow hooking into whether or not the specific callouts should show.
1108
+ *
1109
+ * @since 2.7.17
1110
+ *
1111
+ * @param array List of callouts to enable.
1112
+ */
1113
+ $callouts = apply_filters( 'pods_admin_callouts', $callouts );
1114
+
1115
+ return $callouts;
1116
+ }
1117
+
1118
+ /**
1119
+ * Handle callouts update logic.
1120
+ *
1121
+ * @since 2.7.17
1122
+ */
1123
+ public function handle_callouts_updates() {
1124
+ $callouts = get_option( 'pods_callouts' );
1125
+
1126
+ if ( ! $callouts ) {
1127
+ $callouts = array();
1128
+ }
1129
+
1130
+ $disable_pods = pods_v( 'pods_callout_dismiss' );
1131
+
1132
+ // Disable Friends of Pods 2020 callout.
1133
+ if ( 'friends_2020' === $disable_pods ) {
1134
+ $callouts['friends_2020'] = 0;
1135
+
1136
+ update_option( 'pods_callouts', $callouts );
1137
+ } elseif ( 'reset' === $disable_pods ) {
1138
+ $callouts = array();
1139
+
1140
+ update_option( 'pods_callouts', $callouts );
1141
+ }
1142
+ }
1143
+
1144
+ /**
1145
+ * Add class to container if we have callouts to show.
1146
+ *
1147
+ * @since 2.7.17
1148
+ *
1149
+ * @param array $classes List of classes to use.
1150
+ *
1151
+ * @return array List of classes to use.
1152
+ */
1153
+ public function admin_manage_container_class( $classes ) {
1154
+ $callouts = $this->get_callouts();
1155
+
1156
+ // Only get enabled callouts.
1157
+ $callouts = array_filter( $callouts );
1158
+
1159
+ if ( ! empty( $callouts ) ) {
1160
+ $classes[] = 'pods-admin--flex';
1161
+ }
1162
+
1163
+ return $classes;
1164
+ }
1165
+
1166
+ /**
1167
+ * Add callouts to let admins know about certain things.
1168
+ *
1169
+ * @since 2.7.17
1170
+ */
1171
+ public function admin_manage_callouts() {
1172
+ $force_callouts = false;
1173
+
1174
+ $page = pods_v( 'page' );
1175
+
1176
+ if ( in_array( $page, array( 'pods-settings', 'pods-help' ), true ) ) {
1177
+ $force_callouts = true;
1178
+ }
1179
+
1180
+ $callouts = $this->get_callouts();
1181
+
1182
+ if ( ! empty( $callouts['friends_2020'] ) ) {
1183
+ ?>
1184
+ <div class="pods-admin_friends-callout_container">
1185
+ <?php if ( ! $force_callouts ) : ?>
1186
+ <a href="<?php echo esc_url( add_query_arg( 'pods_callout_dismiss', 'friends_2020' ) ); ?>" class="pods-admin_friends-callout_close">
1187
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 100 125" enable-background="new 0 0 100 100" xml:space="preserve"><polygon points="95,17 83,5 50,38 17,5 5,17 38,50 5,83 17,95 50,62 83,95 95,83 62,50 "/></svg>
1188
+ </a>
1189
+ <?php endif; ?>
1190
+ <div class="pods-admin_friends-callout_logo-container">
1191
+ <svg version="1.1" viewBox="0 0 305 111" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><title>Friends of Pods Logo</title> <defs> <path id="a" d="m0.14762 49.116c0 27.103 21.919 49.075 48.956 49.075 19.888 0 37.007-11.888 44.669-28.962-2.1342-6.538-3.9812-18.041 3.3854-29.538-0.3152-1.624-0.71019-3.219-1.1807-4.781-22.589 22.49-40.827 24.596-54.558 24.229-0.12701-4e-3 -0.58883-0.079-0.71152-0.35667 0 0-0.20016-0.89933 0.38502-0.89933 26.307 0 41.29-15.518 53.531-26.865-0.66763-1.687-1.4264-3.3273-2.2695-4.9167-13.196-3.6393-18.267-14.475-20.067-20.221-6.9007-3.7267-14.796-5.8413-23.184-5.8413-27.037 0-48.956 21.972-48.956 49.076zm62.283-34.287s18.69-2.039 24.194 21.114c-20.424-1.412-24.194-21.114-24.194-21.114zm-7.4779 9.7423s16.57-0.55467 20.342 20.219c-17.938-2.602-20.342-20.219-20.342-20.219zm-9.1018 8.256s14.912-0.97567 18.728 17.626c-16.209-1.8273-18.728-17.626-18.728-17.626zm40.774 3.116c0.021279 0.0013333 0.040563 0.0026667 0.061842 0.0043333l-0.044886 0.067667c-0.0056523-0.024-0.011304-0.048-0.016957-0.072zm-48.709 3.445s13.103-0.859 16.456 15.486c-14.243-1.6047-16.456-15.486-16.456-15.486zm-10.498 1.6027s12.587-0.82333 15.808 14.877c-13.68-1.5417-15.808-14.877-15.808-14.877zm-10.391 0.72233s11.423-0.74667 14.347 13.502c-12.417-1.3997-14.347-13.502-14.347-13.502zm-11.298 2.0857s9.5832-2.526 13.712 9.0523c-0.44919 0.036667-0.88474 0.054333-1.3073 0.054333-9.6148-3.333e-4 -12.405-9.1067-12.405-9.1067zm81.565 0.964c-0.0073147-0.021333-0.014962-0.043667-0.021612-0.064667l0.066497 0.018333c-0.014962 0.015333-0.029924 0.030667-0.044886 0.046333 7.1338 21.557-5.5721 33.29-5.5721 33.29s-8.9279-18.395 5.5721-33.29zm-11.999 0.027333c0.017622 0.0023333 0.035576 5e-3 0.053198 0.0076667l-0.041561 0.056c-0.0036573-0.021667-0.0073147-0.042-0.011637-0.063667zm-10.716 5.6627c0.015959 2e-3 0.032916 0.0036666 0.048543 0.0056666l-0.036573 0.051c-0.0039899-0.019667-0.0076472-0.037333-0.01197-0.056667zm12.212 1.8693c-0.0023274-0.021-0.0046548-0.043333-0.0066497-0.064667l0.056523 0.035667c-0.016624 0.01-0.032916 0.019333-0.049873 0.029 2.2323 21.678-11.682 28.793-11.682 28.793s-4.4962-19.303 11.682-28.793zm-57.318 0.52633-0.018619 0.037333c-0.0039899-0.011667-0.0086447-0.023667-0.012302-0.034667 0.010307-1e-3 0.020614-0.0016667 0.030921-0.0026667zm34.9 2.0257c0.013964 0.0013333 0.027264 3e-3 0.041561 0.0043333l-0.031254 0.046333c-0.0036573-0.017-0.0069822-0.034-0.010307-0.050667zm-22.998 0.34067c0.012302 0.0016666 0.025269 3e-3 0.037571 0.0043333l-0.028594 0.039c-0.0029924-0.014667-0.0056523-0.028667-0.0089771-0.043333zm-16.9 3.542s8.0594-7.693 18.254 0.88467c-3.1137 2.6673-6.0605 3.5853-8.642 3.5853-5.6965 0-9.6125-4.47-9.6125-4.47zm28.752-2.8893c0.013632 0.0013334 0.027264 3e-3 0.040896 0.0043334l-0.030589 0.045c-0.0033249-0.016-0.0069822-0.033333-0.010307-0.049333zm21.183 2.4917c3.325e-4 -0.019 3.325e-4 -0.039333 6.65e-4 -0.058333l0.046548 0.039667c-0.015959 0.0063334-0.031254 0.012333-0.047213 0.018667-0.19118 19.436-13.172 23.733-13.172 23.733s-2.0415-17.662 13.172-23.733zm-31.651 1.257v0.05c-0.0099746-0.0083333-0.019617-0.016333-0.029259-0.024667 0.0093096-0.0086666 0.019284-0.017 0.029259-0.025333zm-6.6234 10.543s4.296-11.202 16.634-9.0197c-3.5273 8.062-9.0759 9.5947-12.782 9.5947-2.2719 0-3.8515-0.575-3.8515-0.575zm16.651-9.0593 0.018287 0.046c-0.01197-2e-3 -0.023606-0.0043334-0.035576-0.0063334 0.0059848-0.013333 0.011637-0.026333 0.017289-0.039667zm10.12 0.685c0.0029923-0.016333 0.0059847-0.033333 0.0089771-0.049667l0.034911 0.043c-0.014629 0.0023333-0.029259 0.0046667-0.043888 0.0066667-2.9518 16.71-14.755 17.762-14.755 17.762s0.77369-15.639 14.755-17.762z"/> </defs> <g fill="none" fill-rule="evenodd"> <g transform="translate(6.3172 6.3333)"> <mask id="b" fill="white"> <use xlink:href="#a"/> </mask> <polygon points="-3.1772 -3.2937 100.48 -3.2937 100.48 101.52 -3.1772 101.52" fill="#fff" mask="url(#b)"/> </g> <path d="m55.303 3.569c-28.538 0-51.754 23.273-51.754 51.88 0 28.607 23.216 51.88 51.754 51.88 28.538 0 51.754-23.273 51.754-51.88 0-28.607-23.217-51.88-51.754-51.88m0 107.18c-30.417 0-55.163-24.807-55.163-55.298 0-30.492 24.746-55.298 55.163-55.298 30.417 0 55.164 24.807 55.164 55.298 0 30.491-24.747 55.298-55.164 55.298" fill="#fff"/> <path d="m137.42 77.263-2.7699 22.725h-14.958l8.2174-67.434h22.252c4.001 0 7.4249 0.48597 10.272 1.4579 2.8469 0.97195 5.1859 2.3141 7.0171 4.0266 1.8312 1.7125 3.1777 3.7335 4.0395 6.0631 0.86176 2.3296 1.2926 4.8366 1.2926 7.521 0 3.6718-0.56167 7.0736-1.685 10.205-1.1234 3.1318-2.8392 5.8394-5.1474 8.1227s-5.232 4.0729-8.7714 5.3688-7.725 1.9439-12.557 1.9439h-7.2018zm4.1549-33.463-2.7238 22.123h7.248c2.1544 0 3.9625-0.31626 5.4244-0.9488 1.4619-0.63254 2.6468-1.5273 3.5547-2.6844 0.90792-1.1571 1.5619-2.5378 1.962-4.1423 0.4001-1.6045 0.60015-3.3632 0.60015-5.2763 0-1.3268-0.17696-2.5456-0.5309-3.6564-0.35394-1.1108-0.90022-2.0673-1.6389-2.8695-0.73865-0.80224-1.6696-1.4271-2.793-1.8745-1.1234-0.4474-2.4544-0.6711-3.9933-0.6711h-7.1095zm48.956 46.283c1.6004 0 3.0315-0.48597 4.2934-1.4579 1.2619-0.97195 2.3313-2.2987 3.2085-3.9803 0.87714-1.6816 1.5542-3.6409 2.0313-5.8779 0.47704-2.237 0.71556-4.6206 0.71556-7.1507 0-3.4867-0.46934-5.9782-1.408-7.4747-0.9387-1.4965-2.4698-2.2447-4.5934-2.2447-1.6004 0-3.0315 0.48597-4.2934 1.4579-1.2619 0.97195-2.3313 2.291-3.2085 3.9572s-1.5542 3.6255-2.0313 5.8779c-0.47704 2.2525-0.71556 4.6437-0.71556 7.1739 0 3.425 0.46934 5.9011 1.408 7.4284 0.9387 1.5273 2.4698 2.291 4.5934 2.291zm-1.2926 10.645c-2.6776 0-5.1782-0.4474-7.5019-1.3422-2.3237-0.89481-4.3395-2.1984-6.0477-3.9109-1.7081-1.7125-3.0469-3.8261-4.0164-6.3408-0.96948-2.5147-1.4542-5.4074-1.4542-8.6781 0-4.2581 0.70017-8.1689 2.1005-11.733 1.4004-3.5638 3.2854-6.6416 5.6552-9.2335s5.1166-4.6129 8.2405-6.0631 6.4093-2.1753 9.8563-2.1753c2.6776 0 5.1705 0.4474 7.4788 1.3422 2.3083 0.89481 4.3164 2.1984 6.0246 3.9109 1.7081 1.7125 3.0546 3.8261 4.0395 6.3408 0.98486 2.5147 1.4773 5.4074 1.4773 8.6781 0 4.1963-0.70017 8.0764-2.1005 11.64-1.4004 3.5638-3.2854 6.6493-5.6552 9.2566-2.3698 2.6073-5.1166 4.6437-8.2405 6.1094-3.1239 1.4656-6.4093 2.1984-9.8563 2.1984zm62.6-0.74053c-0.89253 0-1.6389-0.11571-2.239-0.34712-0.60015-0.23142-1.0772-0.54768-1.4311-0.9488s-0.60015-0.87937-0.73864-1.4348c-0.1385-0.5554-0.20774-1.1571-0.20774-1.805v-4.0266c-2.1852 2.9004-4.6011 5.176-7.248 6.8267-2.6468 1.6508-5.4629 2.4761-8.4482 2.4761-1.9697 0-3.7702-0.34712-5.4013-1.0414-1.6312-0.69425-3.0392-1.7819-4.2241-3.263s-2.1082-3.3709-2.7699-5.6697c-0.66171-2.2987-0.99255-5.0525-0.99255-8.2615 0-2.777 0.29238-5.4845 0.87714-8.1227 0.58476-2.6381 1.4003-5.1143 2.4468-7.4284s2.2929-4.4354 3.7394-6.3639c1.4465-1.9285 3.0392-3.5792 4.7781-4.9523 1.7389-1.3731 3.5778-2.4453 5.5168-3.2167s3.9394-1.1571 6.0015-1.1571c1.9697 0 3.7471 0.33169 5.3321 0.99508 1.585 0.66339 2.9777 1.535 4.178 2.615l3.0469-24.16h14.034l-8.5867 69.286h-7.6634zm-14.588-10.182c0.98486 0 2.0082-0.37026 3.07-1.1108s2.0928-1.7587 3.0931-3.0547 1.9312-2.8155 2.793-4.5589c0.86176-1.7433 1.6004-3.6178 2.2159-5.6234l1.4773-11.663c-0.89253-0.70968-1.8851-1.2188-2.9777-1.5273s-2.1313-0.46283-3.1162-0.46283c-1.662 0-3.1931 0.55539-4.5934 1.6662s-2.6083 2.5687-3.624 4.3737c-1.0156 1.805-1.8081 3.8646-2.3775 6.1788s-0.85406 4.6746-0.85406 7.0813c0 3.0547 0.44626 5.2685 1.3388 6.6416 0.89253 1.3731 2.0774 2.0596 3.5547 2.0596zm63.465-27.724c-0.43088 0.5554-0.83097 0.95651-1.2003 1.2034s-0.86175 0.37026-1.4773 0.37026-1.2234-0.13885-1.8235-0.41655-1.2465-0.59396-1.9389-0.9488c-0.69248-0.35484-1.4773-0.6711-2.3544-0.9488-0.87714-0.2777-1.8851-0.41655-3.0238-0.41655-2.1236 0-3.6394 0.40883-4.5473 1.2265-0.90792 0.81767-1.3619 1.8745-1.3619 3.1704 0 0.8331 0.25391 1.5428 0.76173 2.129 0.50782 0.58625 1.1772 1.1108 2.0082 1.5736 0.83098 0.46283 1.7774 0.88709 2.8392 1.2728 1.0618 0.38569 2.1467 0.81766 3.2547 1.2959 1.108 0.47826 2.1928 1.0259 3.2547 1.643s2.0082 1.3731 2.8392 2.2679c0.83098 0.89481 1.5004 1.9593 2.0082 3.1935 0.50782 1.2342 0.76173 2.6998 0.76173 4.3969 0 2.4067-0.47704 4.6823-1.4311 6.8267-0.95409 2.1445-2.3236 4.0112-4.1087 5.6002-1.7851 1.5891-3.9471 2.8541-6.4862 3.7952-2.5391 0.94109-5.3936 1.4116-8.5637 1.4116-1.5081 0-3.0007-0.15428-4.478-0.46283-1.4773-0.30856-2.8699-0.7251-4.178-1.2496s-2.5006-1.1494-3.5778-1.8745c-1.0772-0.7251-1.9543-1.5042-2.6314-2.3373l3.5086-5.2763c0.43088-0.61711 0.931-1.1031 1.5004-1.4579 0.56938-0.35484 1.2695-0.53225 2.1005-0.53225 0.76942 0 1.4311 0.18513 1.9851 0.5554 0.55399 0.37027 1.1541 0.77138 1.8004 1.2034s1.4157 0.83309 2.3083 1.2034c0.89253 0.37027 2.062 0.5554 3.5086 0.5554 2.0313 0 3.5393-0.45511 4.5242-1.3653 0.98486-0.91024 1.4773-1.9824 1.4773-3.2167 0-0.95652-0.25391-1.7433-0.76173-2.3604-0.50782-0.61711-1.1772-1.1494-2.0082-1.5968-0.83098-0.4474-1.7697-0.84852-2.8161-1.2034-1.0464-0.35484-2.1236-0.74824-3.2316-1.1802s-2.1852-0.93337-3.2316-1.5042c-1.0464-0.57083-1.9851-1.2882-2.8161-2.1522-0.83098-0.86395-1.5004-1.9285-2.0082-3.1935-0.50782-1.2651-0.76173-2.7924-0.76173-4.582 0-2.2216 0.40779-4.3814 1.2234-6.4796s2.0313-3.9572 3.6471-5.5771c1.6158-1.6199 3.624-2.9235 6.0246-3.9109s5.2013-1.4811 8.4021-1.4811c3.2008 0 6.0553 0.55539 8.5637 1.6662 2.5083 1.1108 4.578 2.4684 6.2092 4.0729l-3.6932 5.0911z" fill="#fff"/> <g transform="translate(128.86 3.5426)" fill="#fff"> <path d="m14.411 3.7958h-8.3873l-0.77821 6.399h7.0759l-0.3891 3.0104h-7.0471l-1.052 8.6095h-3.7613l2.5652-21.029h12.149l-0.37469 3.0104zm-0.59086 18.019 1.8014-14.936h1.8014c0.34587 0 0.62929 0.077562 0.85026 0.23269 0.22097 0.15513 0.33146 0.40236 0.33146 0.7417v0.050901c0 0.033934-0.0048037 0.13331-0.014411 0.29813s-0.019215 0.42417-0.028822 0.77806c-0.0096075 0.35388-0.028822 0.85077-0.057645 1.4907 0.60527-1.1732 1.2682-2.0797 1.9887-2.7196 0.72056-0.6399 1.4699-0.95985 2.2482-0.95985 0.39391 0 0.79262 0.087258 1.1961 0.26178l-0.6485 3.4322c-0.48038-0.2036-0.93673-0.30541-1.3691-0.30541-0.85507 0-1.6044 0.41205-2.2482 1.2362-0.6437 0.82411-1.1721 2.1136-1.5852 3.8685l-0.77821 6.5299h-3.4875zm16.083-14.921-1.7726 14.921h-3.5163l1.7726-14.921h3.5163zm0.8935-4.3484c0 0.31025-0.06485 0.60111-0.19455 0.87259-0.1297 0.27147-0.29783 0.50901-0.50439 0.71261-0.20656 0.2036-0.44434 0.366-0.71336 0.48719-0.26901 0.12119-0.54762 0.18179-0.83585 0.18179-0.27862 0-0.55003-0.060596-0.81423-0.18179-0.26421-0.12119-0.49719-0.28359-0.69894-0.48719s-0.36268-0.44114-0.48278-0.71261c-0.12009-0.27147-0.18014-0.56233-0.18014-0.87259s0.062448-0.60596 0.18735-0.88713 0.28822-0.52597 0.48998-0.73443c0.20176-0.20845 0.43474-0.37327 0.69894-0.49447 0.26421-0.12119 0.53562-0.18179 0.81423-0.18179 0.28823 0 0.56684 0.060596 0.83585 0.18179 0.26901 0.12119 0.50679 0.28359 0.71336 0.48719 0.20656 0.2036 0.37229 0.44599 0.49719 0.72716s0.18735 0.58172 0.18735 0.90167zm13.402 7.8097c0 0.66899-0.1321 1.2798-0.39631 1.8324-0.26421 0.55264-0.73737 1.0471-1.4195 1.4834-0.68213 0.4363-1.6068 0.81199-2.7742 1.1271-1.1673 0.3151-2.6541 0.56476-4.4603 0.74897v0.18906c0 2.3463 0.98476 3.5194 2.9543 3.5194 0.42273 0 0.79742-0.041205 1.1241-0.12362s0.61248-0.18179 0.85747-0.29813 0.46596-0.24723 0.66292-0.39266c0.19695-0.14543 0.37949-0.27632 0.54763-0.39266 0.16813-0.11635 0.33386-0.21572 0.49719-0.29813 0.16333-0.082411 0.34106-0.12362 0.53322-0.12362 0.11529 0 0.23058 0.026662 0.34587 0.079987 0.11529 0.053325 0.21136 0.13331 0.28822 0.23996l0.90791 1.1053c-0.5092 0.51386-1.0088 0.95984-1.4988 1.338-0.48998 0.37812-0.98957 0.68837-1.4988 0.93076-0.5092 0.24239-1.0472 0.42175-1.6141 0.5381-0.56684 0.11635-1.1865 0.17452-1.859 0.17452-0.86468 0-1.6477-0.14785-2.349-0.44356-0.70135-0.29571-1.3018-0.71261-1.8014-1.2507-0.49959-0.5381-0.88629-1.1877-1.1601-1.9488-0.27381-0.76109-0.41072-1.6119-0.41072-2.5523 0-0.78533 0.084065-1.5561 0.2522-2.3124 0.16813-0.75625 0.41072-1.4737 0.72777-2.1524 0.31705-0.67868 0.70615-1.304 1.1673-1.8761 0.46116-0.57203 0.98236-1.0665 1.5636-1.4834 0.58126-0.4169 1.2201-0.7417 1.9167-0.97439 0.69655-0.23269 1.4435-0.34903 2.2409-0.34903 0.77821 0 1.4579 0.1115 2.0392 0.33449 0.58126 0.223 1.0664 0.51143 1.4555 0.86532 0.3891 0.35388 0.67973 0.74897 0.87188 1.1853 0.19215 0.4363 0.28822 0.86289 0.28822 1.2798zm-4.8566-1.1634c-0.48038 0-0.92712 0.099377-1.3402 0.29813-0.41312 0.19876-0.78541 0.4775-1.1169 0.83623-0.33146 0.35873-0.61968 0.78775-0.86467 1.2871-0.24499 0.49932-0.43954 1.0447-0.58365 1.6361 1.1913-0.16482 2.1497-0.34419 2.875-0.5381 0.72537-0.19391 1.2874-0.40721 1.6861-0.6399 0.39871-0.23269 0.66292-0.47992 0.79262-0.7417 0.1297-0.26178 0.19455-0.54294 0.19455-0.8435 0-0.14543-0.031224-0.29571-0.093673-0.45084-0.062449-0.15513-0.15852-0.29329-0.28822-0.41448-0.1297-0.12119-0.29783-0.22299-0.50439-0.30541-0.20656-0.082411-0.45876-0.12362-0.75659-0.12362zm6.0095 12.623 1.7726-14.936h1.8158c0.37469 0 0.66532 0.092105 0.87188 0.27632 0.20656 0.18421 0.30984 0.47992 0.30984 0.88713l-0.10088 1.9342c0.74939-1.115 1.5804-1.9464 2.4931-2.4941 0.91272-0.54779 1.8542-0.82169 2.8246-0.82169 0.54763 0 1.0448 0.099377 1.4916 0.29813s0.82864 0.48962 1.1457 0.87259 0.56204 0.85319 0.73497 1.4107c0.17294 0.55749 0.2594 1.1998 0.2594 1.927 0 0.18421-0.0072055 0.37085-0.021617 0.55991s-0.031224 0.38539-0.050439 0.589l-1.1097 9.4967h-3.5596c0.19215-1.6385 0.35548-3.0274 0.48998-4.1666 0.13451-1.1392 0.24499-2.0869 0.33146-2.8432 0.086468-0.75625 0.15372-1.3501 0.20176-1.7815 0.048038-0.43145 0.084065-0.75866 0.10808-0.98166 0.024019-0.223 0.03843-0.37085 0.043234-0.44356s0.0072056-0.13331 0.0072056-0.18179c0-0.6399-0.11529-1.1029-0.34587-1.3889s-0.59086-0.42902-1.0808-0.42902c-0.39391 0-0.80222 0.11634-1.225 0.34903-0.42273 0.23269-0.82624 0.56233-1.2105 0.98893-0.3843 0.4266-0.73497 0.94045-1.052 1.5416-0.31705 0.60112-0.57645 1.2701-0.77821 2.0069l-0.80703 7.3297h-3.5596zm25.738 0c-0.43234 0-0.73497-0.1018-0.90791-0.30541-0.17294-0.2036-0.2594-0.46053-0.2594-0.77078l0.11529-2.1815c-0.71096 1.0665-1.5084 1.9124-2.3923 2.5378-0.88389 0.62536-1.8254 0.93803-2.8246 0.93803-0.61488 0-1.1745-0.11392-1.6789-0.34176-0.5044-0.22784-0.93433-0.5696-1.2898-1.0253s-0.62929-1.0326-0.82144-1.7306c-0.19215-0.69807-0.28822-1.5222-0.28822-2.4723 0-0.8435 0.086466-1.67 0.2594-2.4796 0.17294-0.80957 0.41792-1.5779 0.73497-2.3051 0.31705-0.72716 0.69414-1.3961 1.1313-2.0069 0.43714-0.61081 0.92472-1.1392 1.4627-1.5852 0.53802-0.44599 1.1121-0.79502 1.7221-1.0471s1.2418-0.37812 1.8951-0.37812c0.66292 0 1.2634 0.12119 1.8014 0.36358 0.53802 0.24239 1.0088 0.57203 1.4123 0.98893l0.96555-7.8097h3.4875l-2.6373 21.611h-1.8879zm-4.8854-2.6032c-0.60527 0-1.076-0.2545-1.4123-0.76351-0.33626-0.50901-0.50439-1.2871-0.50439-2.3342 0-0.80472 0.10088-1.607 0.30264-2.4069 0.20176-0.79988 0.48758-1.5198 0.85747-2.1597 0.36989-0.6399 0.81423-1.1586 1.333-1.5561 0.51881-0.39751 1.0952-0.59627 1.7293-0.59627 0.40352 0 0.81663 0.072715 1.2394 0.21815s0.79742 0.39751 1.1241 0.75624l-0.44675 3.6212c-0.23058 0.73685-0.50679 1.4228-0.82865 2.0579-0.32185 0.63505-0.67012 1.1877-1.0448 1.6579-0.37469 0.47023-0.76379 0.83865-1.1673 1.1053s-0.79742 0.39994-1.1817 0.39994zm19.549-9.5612c-0.10501 0.15274-0.20525 0.26014-0.30071 0.32219-0.095464 0.062051-0.21956 0.093077-0.37231 0.093077-0.16229 0-0.32935-0.047731-0.50118-0.14319-0.17183-0.095464-0.36992-0.20286-0.59426-0.32219-0.22434-0.11933-0.48209-0.22672-0.77325-0.32219s-0.64199-0.14319-1.0525-0.14319c-0.75416 0-1.3317 0.16945-1.7327 0.50834-0.40095 0.3389-0.60142 0.76132-0.60142 1.2673 0 0.29594 0.083529 0.54653 0.25059 0.75177s0.38424 0.38424 0.65154 0.53698c0.2673 0.15274 0.57278 0.29355 0.91645 0.42242s0.69688 0.2673 1.0596 0.41526 0.71597 0.31503 1.0596 0.50118c0.34367 0.18615 0.64915 0.41288 0.91645 0.68017 0.2673 0.2673 0.48447 0.58948 0.65154 0.96656s0.25059 0.82814 0.25059 1.3532c0 0.70643-0.14081 1.3794-0.42242 2.019-0.28162 0.63961-0.68495 1.2004-1.21 1.6825-0.52505 0.48209-1.1599 0.86394-1.9045 1.1456-0.74462 0.28162-1.5751 0.42242-2.4916 0.42242-0.46777 0-0.91883-0.045344-1.3532-0.13603-0.43436-0.09069-0.84246-0.21718-1.2243-0.37947-0.38185-0.16229-0.7279-0.35321-1.0382-0.57278-0.31026-0.21957-0.57516-0.45822-0.79473-0.71597l0.85917-1.346c0.10501-0.16229 0.2315-0.28639 0.37947-0.37231 0.14797-0.085917 0.32219-0.12888 0.52266-0.12888s0.38185 0.06205 0.54414 0.18615c0.16229 0.1241 0.35321 0.26014 0.57278 0.4081s0.48447 0.284 0.79473 0.4081 0.70881 0.18615 1.1957 0.18615c0.3914 0 0.73745-0.052504 1.0382-0.15751 0.30071-0.10501 0.55369-0.2482 0.75893-0.42958 0.20525-0.18138 0.36037-0.3914 0.46538-0.63006 0.10501-0.23866 0.15751-0.49163 0.15751-0.75893 0-0.32458-0.081143-0.59426-0.24343-0.80905-0.16229-0.21479-0.37946-0.40094-0.65154-0.55846-0.27207-0.15751-0.57994-0.29594-0.92361-0.41526s-0.69449-0.24582-1.0525-0.37947c-0.35799-0.13365-0.70643-0.28639-1.0453-0.45822s-0.64437-0.38901-0.91645-0.65154c-0.27207-0.26252-0.48925-0.58232-0.65154-0.9594-0.16229-0.37708-0.24343-0.83769-0.24343-1.3818 0-0.64915 0.1241-1.2816 0.37231-1.8973 0.24821-0.61574 0.61335-1.1599 1.0954-1.6324 0.48209-0.47254 1.074-0.85201 1.7756-1.1384 0.70166-0.28639 1.5059-0.42958 2.4128-0.42958 0.93554 0 1.7637 0.1599 2.4844 0.4797 0.72075 0.3198 1.3293 0.7279 1.8257 1.2243l-0.91645 1.2888z"/> <path d="m57.693 40.297c0.49639 0 0.94473-0.14341 1.345-0.43023 0.40031-0.28682 0.74258-0.6706 1.0268-1.1513 0.28422-0.48073 0.50239-1.0402 0.65451-1.6785 0.15212-0.63828 0.22818-1.3089 0.22818-2.0118 0-1.0099-0.17013-1.7533-0.5104-2.2299-0.34027-0.47669-0.85466-0.71504-1.5432-0.71504-0.49639 0-0.94473 0.14139-1.345 0.42417s-0.74058 0.66656-1.0208 1.1513c-0.28022 0.48477-0.49639 1.0463-0.6485 1.6846-0.15212 0.63828-0.22818 1.3129-0.22818 2.0239 0 1.0099 0.16813 1.7512 0.50439 2.2239s0.84866 0.70898 1.5372 0.70898zm-0.26421 2.3027c-0.68053 0-1.311-0.11513-1.8915-0.3454s-1.0808-0.56758-1.5012-1.012-0.75058-0.98974-0.99077-1.6361c-0.24019-0.64636-0.36028-1.3856-0.36028-2.2178 0-1.0746 0.17013-2.0764 0.5104-3.0056 0.34027-0.92915 0.80462-1.7331 1.3931-2.4117 0.58846-0.67868 1.275-1.2119 2.0596-1.5997 0.78461-0.38782 1.6213-0.58172 2.51-0.58172 0.68053 0 1.311 0.11513 1.8915 0.3454s1.0828 0.56758 1.5072 1.012c0.42433 0.44438 0.75659 0.98974 0.99678 1.6361 0.24019 0.64636 0.36028 1.3856 0.36028 2.2178 0 1.0665-0.17213 2.0623-0.5164 2.9874-0.34427 0.92511-0.81063 1.729-1.3991 2.4117-0.58846 0.68272-1.275 1.22-2.0596 1.6119-0.78461 0.39186-1.6213 0.58778-2.51 0.58778zm12.177-10.374-1.1649 9.6833-0.61248 2.2784c-0.11209 0.3959-0.28622 0.69888-0.52241 0.90894-0.23619 0.21007-0.57044 0.3151-1.0028 0.3151h-1.1889l1.5732-13.186-0.94874-0.16967c-0.17614-0.032318-0.32025-0.098973-0.43234-0.19997-0.11209-0.10099-0.16813-0.2444-0.16813-0.43023 0-0.0080796 0.0020015-0.024238 0.0060047-0.048477 0.0040031-0.024239 0.010008-0.078775 0.018014-0.16361 0.0080063-0.084835 0.022017-0.21411 0.042033-0.38782 0.020016-0.17371 0.050039-0.41811 0.09007-0.73322h1.6573l0.10808-0.92106c0.088069-0.711 0.27421-1.3493 0.55843-1.9148 0.28422-0.56557 0.6445-1.0463 1.0808-1.4422 0.43634-0.3959 0.93873-0.69888 1.5072-0.90894 0.56845-0.21007 1.1809-0.3151 1.8374-0.3151 0.5124 0 0.98476 0.076755 1.4171 0.23027l-0.26421 1.5513c-0.016013 0.088875-0.054042 0.15957-0.11409 0.21209-0.060047 0.052517-0.1341 0.090894-0.22217 0.11513s-0.18614 0.040398-0.29423 0.048477c-0.10808 0.0080796-0.21817 0.012119-0.33026 0.012119-0.32025 0-0.61448 0.044437-0.88269 0.13331s-0.50439 0.23228-0.70855 0.43023c-0.20416 0.19795-0.37229 0.45649-0.50439 0.77563-0.1321 0.31914-0.22618 0.70897-0.28222 1.1695l-0.096075 0.82411h2.7622l-0.27622 2.133h-2.6421z"/> </g> </g> </svg>
1192
+ </div>
1193
+ <div class="pods-admin_friends-callout_content-container">
1194
+ <h2 class="pods-admin_friends-callout_headline"><?php printf( esc_html__( 'We need %1$sYOU%2$s in 2020 and beyond', 'pods' ), '<span class="pods-admin_friends-you">', '</span>' ); ?></h2>
1195
+ <p class="pods-admin_friends-callout_text"><?php esc_html_e( 'Things are changing for Pods and we want you to be a part of it! Our goal is to be fully funded by users like you. Help us reach our goal of 200 recurring donors in 2020.', 'pods' ); ?></p>
1196
+ <div class="pods-admin_friends-callout_button-group">
1197
+ <a href="https://friends.pods.io/?utm_source=pods_plugin_callout&utm_medium=link&utm_campaign=friends_of_pods_2020" class="pods-admin_friends-callout_button"><?php esc_html_e( 'Learn More', 'pods' ); ?></a>
1198
+ <a href="https://friends.pods.io/membership-levels/?utm_source=pods_plugin_callout&utm_medium=link&utm_campaign=friends_of_pods_2020" class="pods-admin_friends-callout_button--join"><?php esc_html_e( 'Join Now', 'pods' ); ?></a>
1199
+ </div>
1200
+ </div>
1201
+ </div>
1202
+ <?php
1203
+ }
1204
+ }
1205
+
1206
+ /**
1207
+ * Get the add page of an object
1208
+ *
1209
+ * @param PodsUI $obj PodsUI object.
1210
+ */
1211
+ public function admin_setup_add( $obj ) {
1212
+
1213
+ pods_view( PODS_DIR . 'ui/admin/setup-add.php', compact( array_keys( get_defined_vars() ) ) );
1214
+ }
1215
+
1216
+ /**
1217
+ * Get the edit page of an object
1218
+ *
1219
+ * @param boolean $duplicate Whether the screen is for duplicating.
1220
+ * @param PodsUI $obj PodsUI object.
1221
+ */
1222
+ public function admin_setup_edit( $duplicate, $obj ) {
1223
+
1224
+ pods_view( PODS_DIR . 'ui/admin/setup-edit.php', compact( array_keys( get_defined_vars() ) ) );
1225
+ }
1226
+
1227
+ /**
1228
+ * Get list of Pod option tabs
1229
+ *
1230
+ * @param array $pod Pod options.
1231
+ *
1232
+ * @return array
1233
+ */
1234
+ public function admin_setup_edit_tabs( $pod ) {
1235
+
1236
+ $fields = true;
1237
+ $labels = false;
1238
+ $admin_ui = false;
1239
+ $advanced = false;
1240
+
1241
+ if ( 'post_type' === pods_v( 'type', $pod ) && '' === pods_v( 'object', $pod ) ) {
1242
+ $labels = true;
1243
+ $admin_ui = true;
1244
+ $advanced = true;
1245
+ } elseif ( 'taxonomy' === pods_v( 'type', $pod ) && '' === pods_v( 'object', $pod ) ) {
1246
+ $labels = true;
1247
+ $admin_ui = true;
1248
+ $advanced = true;
1249
+ } elseif ( 'pod' === pods_v( 'type', $pod ) ) {
1250
+ $labels = true;
1251
+ $admin_ui = true;
1252
+ $advanced = true;
1253
+ } elseif ( 'settings' === pods_v( 'type', $pod ) ) {
1254
+ $labels = true;
1255
+ $admin_ui = true;
1256
+ }
1257
+
1258
+ if ( ! function_exists( 'get_term_meta' ) && 'none' === pods_v( 'storage', $pod, 'none', true ) && 'taxonomy' === pods_v( 'type', $pod ) ) {
1259
+ $fields = false;
1260
+ }
1261
+
1262
+ $tabs = array();
1263
+
1264
+ if ( $fields ) {
1265
+ $tabs['manage-fields'] = __( 'Manage Fields', 'pods' );
1266
+ }
1267
+
1268
+ if ( $labels ) {
1269
+ $tabs['labels'] = __( 'Labels', 'pods' );
1270
+ }
1271
+
1272
+ if ( $admin_ui ) {
1273
+ $tabs['admin-ui'] = __( 'Admin UI', 'pods' );
1274
+ }
1275
+
1276
+ if ( $advanced ) {
1277
+ $tabs['advanced'] = __( 'Advanced Options', 'pods' );
1278
+ }
1279
+
1280
+ if ( 'taxonomy' === pods_v( 'type', $pod ) && ! $fields ) {
1281
+ $tabs['extra-fields'] = __( 'Extra Fields', 'pods' );
1282
+ }
1283
+
1284
+ $addtl_args = compact( array( 'fields', 'labels', 'admin_ui', 'advanced' ) );
1285
+
1286
+ $pod_type = $pod['type'];
1287
+ $pod_name = $pod['name'];
1288
+
1289
+ /**
1290
+ * Add or modify tabs in Pods editor for a specific Pod
1291
+ *
1292
+ * @param array $tabs Tabs to set.
1293
+ * @param object $pod Current Pods object.
1294
+ * @param array $addtl_args Additional args.
1295
+ *
1296
+ * @since unknown
1297
+ */
1298
+ $tabs = apply_filters( "pods_admin_setup_edit_tabs_{$pod_type}_{$pod_name}", $tabs, $pod, $addtl_args );
1299
+
1300
+ /**
1301
+ * Add or modify tabs for any Pod in Pods editor of a specific post type.
1302
+ */
1303
+ $tabs = apply_filters( "pods_admin_setup_edit_tabs_{$pod_type}", $tabs, $pod, $addtl_args );
1304
+
1305
+ /**
1306
+ * Add or modify tabs in Pods editor for all pods.
1307
+ */
1308
+ $tabs = apply_filters( 'pods_admin_setup_edit_tabs', $tabs, $pod, $addtl_args );
1309
+
1310
+ return $tabs;
1311
+ }
1312
+
1313
+ /**
1314
+ * Get list of Pod options
1315
+ *
1316
+ * @param array $pod Pod options.
1317
+ *
1318
+ * @return array
1319
+ */
1320
+ public function admin_setup_edit_options( $pod ) {
1321
+
1322
+ $options = array();
1323
+
1324
+ if ( '' === pods_v( 'object', $pod ) && 'settings' !== pods_v( 'type', $pod ) ) {
1325
+ $labels = array(
1326
+ 'label' => array(
1327
+ 'label' => __( 'Label', 'pods' ),
1328
+ 'help' => __( 'help', 'pods' ),
1329
+ 'type' => 'text',
1330
+ 'default' => str_replace( '_', ' ', pods_v( 'name', $pod ) ),
1331
+ 'text_max_length' => 30,
1332
+ ),
1333
+ 'label_singular' => array(
1334
+ 'label' => __( 'Singular Label', 'pods' ),
1335
+ 'help' => __( 'help', 'pods' ),
1336
+ 'type' => 'text',
1337
+ 'default' => pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', pods_v( 'name', $pod ) ) ) ),
1338
+ 'text_max_length' => 30,
1339
+ ),
1340
+ 'label_add_new' => array(
1341
+ 'label' => __( 'Add New', 'pods' ),
1342
+ 'help' => __( 'help', 'pods' ),
1343
+ 'type' => 'text',
1344
+ 'default' => '',
1345
+ 'object_type' => array( 'post_type', 'pod' ),
1346
+ ),
1347
+ 'label_add_new_item' => array(
1348
+ 'label' => __( 'Add new <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1349
+ 'help' => __( 'help', 'pods' ),
1350
+ 'type' => 'text',
1351
+ 'default' => '',
1352
+ ),
1353
+ 'label_new_item' => array(
1354
+ 'label' => __( 'New <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1355
+ 'help' => __( 'help', 'pods' ),
1356
+ 'type' => 'text',
1357
+ 'default' => '',
1358
+ 'object_type' => array( 'post_type', 'pod' ),
1359
+ ),
1360
+ 'label_new_item_name' => array(
1361
+ 'label' => __( 'New <span class="pods-slugged" data-sluggable="label_singular">Item</span> Name', 'pods' ),
1362
+ 'help' => __( 'help', 'pods' ),
1363
+ 'type' => 'text',
1364
+ 'default' => '',
1365
+ 'object_type' => array( 'taxonomy' ),
1366
+ ),
1367
+ 'label_edit' => array(
1368
+ 'label' => __( 'Edit', 'pods' ),
1369
+ 'help' => __( 'help', 'pods' ),
1370
+ 'type' => 'text',
1371
+ 'default' => '',
1372
+ 'object_type' => array( 'pod' ),
1373
+ ),
1374
+ 'label_edit_item' => array(
1375
+ 'label' => __( 'Edit <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1376
+ 'help' => __( 'help', 'pods' ),
1377
+ 'type' => 'text',
1378
+ 'default' => '',
1379
+ ),
1380
+ 'label_update_item' => array(
1381
+ 'label' => __( 'Update <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1382
+ 'help' => __( 'help', 'pods' ),
1383
+ 'type' => 'text',
1384
+ 'default' => '',
1385
+ 'object_type' => array( 'taxonomy', 'pod' ),
1386
+ ),
1387
+ 'label_duplicate' => array(
1388
+ 'label' => __( 'Duplicate', 'pods' ),
1389
+ 'help' => __( 'help', 'pods' ),
1390
+ 'type' => 'text',
1391
+ 'default' => '',
1392
+ 'object_type' => array( 'pod' ),
1393
+ ),
1394
+ 'label_duplicate_item' => array(
1395
+ 'label' => __( 'Duplicate <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1396
+ 'help' => __( 'help', 'pods' ),
1397
+ 'type' => 'text',
1398
+ 'default' => '',
1399
+ 'object_type' => array( 'pod' ),
1400
+ ),
1401
+ 'label_delete_item' => array(
1402
+ 'label' => __( 'Delete <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1403
+ 'help' => __( 'help', 'pods' ),
1404
+ 'type' => 'text',
1405
+ 'default' => '',
1406
+ 'object_type' => array( 'pod' ),
1407
+ ),
1408
+ 'label_view' => array(
1409
+ 'label' => __( 'View', 'pods' ),
1410
+ 'help' => __( 'help', 'pods' ),
1411
+ 'type' => 'text',
1412
+ 'default' => '',
1413
+ 'object_type' => array( 'pod' ),
1414
+ ),
1415
+ 'label_view_item' => array(
1416
+ 'label' => __( 'View <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1417
+ 'help' => __( 'help', 'pods' ),
1418
+ 'type' => 'text',
1419
+ 'default' => '',
1420
+ ),
1421
+ 'label_view_items' => array(
1422
+ 'label' => __( 'View <span class="pods-slugged" data-sluggable="label">Items</span>', 'pods' ),
1423
+ 'help' => __( 'help', 'pods' ),
1424
+ 'type' => 'text',
1425
+ 'default' => '',
1426
+ 'object_type' => array( 'post_type' ),
1427
+ ),
1428
+ 'label_back_to_manage' => array(
1429
+ 'label' => __( 'Back to Manage', 'pods' ),
1430
+ 'help' => __( 'help', 'pods' ),
1431
+ 'type' => 'text',
1432
+ 'default' => '',
1433
+ 'object_type' => array( 'pod' ),
1434
+ ),
1435
+ 'label_manage' => array(
1436
+ 'label' => __( 'Manage', 'pods' ),
1437
+ 'help' => __( 'help', 'pods' ),
1438
+ 'type' => 'text',
1439
+ 'default' => '',
1440
+ 'object_type' => array( 'pod' ),
1441
+ ),
1442
+ 'label_manage_items' => array(
1443
+ 'label' => __( 'Manage <span class="pods-slugged" data-sluggable="label">Items</span>', 'pods' ),
1444
+ 'help' => __( 'help', 'pods' ),
1445
+ 'type' => 'text',
1446
+ 'default' => '',
1447
+ 'object_type' => array( 'pod' ),
1448
+ ),
1449
+ 'label_reorder' => array(
1450
+ 'label' => __( 'Reorder', 'pods' ),
1451
+ 'help' => __( 'help', 'pods' ),
1452
+ 'type' => 'text',
1453
+ 'default' => '',
1454
+ 'object_type' => array( 'pod' ),
1455
+ ),
1456
+ 'label_reorder_items' => array(
1457
+ 'label' => __( 'Reorder <span class="pods-slugged" data-sluggable="label">Items</span>', 'pods' ),
1458
+ 'help' => __( 'help', 'pods' ),
1459
+ 'type' => 'text',
1460
+ 'default' => '',
1461
+ 'object_type' => array( 'pod' ),
1462
+ ),
1463
+ 'label_all_items' => array(
1464
+ 'label' => __( 'All <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1465
+ 'help' => __( 'help', 'pods' ),
1466
+ 'type' => 'text',
1467
+ 'default' => '',
1468
+ ),
1469
+ 'label_search' => array(
1470
+ 'label' => __( 'Search', 'pods' ),
1471
+ 'help' => __( 'help', 'pods' ),
1472
+ 'type' => 'text',
1473
+ 'default' => '',
1474
+ 'object_type' => array( 'pod' ),
1475
+ ),
1476
+ 'label_search_items' => array(
1477
+ 'label' => __( 'Search <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1478
+ 'help' => __( 'help', 'pods' ),
1479
+ 'type' => 'text',
1480
+ 'default' => '',
1481
+ ),
1482
+ 'label_popular_items' => array(
1483
+ 'label' => __( 'Popular <span class="pods-slugged" data-sluggable="label">Items</span>', 'pods' ),
1484
+ 'help' => __( 'help', 'pods' ),
1485
+ 'type' => 'text',
1486
+ 'default' => '',
1487
+ 'object_type' => array( 'taxonomy' ),
1488
+ ),
1489
+ // @todo Why was label_parent added previously? Can't find it in WP
1490
+ 'label_parent' => array(
1491
+ 'label' => __( 'Parent <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1492
+ 'help' => __( 'help', 'pods' ),
1493
+ 'type' => 'text',
1494
+ 'default' => '',
1495
+ 'object_type' => array( 'post_type', 'pod' ),
1496
+ ),
1497
+ 'label_parent_item' => array(
1498
+ 'label' => __( 'Parent <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1499
+ 'help' => __( 'help', 'pods' ),
1500
+ 'type' => 'text',
1501
+ 'default' => '',
1502
+ 'object_type' => array( 'taxonomy' ),
1503
+ ),
1504
+ 'label_parent_item_colon' => array(
1505
+ 'label' => __( 'Parent <span class="pods-slugged" data-sluggable="label_singular">Item</span>:', 'pods' ),
1506
+ 'help' => __( 'help', 'pods' ),
1507
+ 'type' => 'text',
1508
+ 'default' => '',
1509
+ ),
1510
+ 'label_not_found' => array(
1511
+ 'label' => __( 'Not Found', 'pods' ),
1512
+ 'help' => __( 'help', 'pods' ),
1513
+ 'type' => 'text',
1514
+ 'default' => '',
1515
+ ),
1516
+ 'label_no_items_found' => array(
1517
+ 'label' => __( 'No <span class="pods-slugged" data-sluggable="label_singular">Item</span> Found', 'pods' ),
1518
+ 'help' => __( 'help', 'pods' ),
1519
+ 'type' => 'text',
1520
+ 'default' => '',
1521
+ 'object_type' => array( 'pod' ),
1522
+ ),
1523
+ 'label_not_found_in_trash' => array(
1524
+ 'label' => __( 'Not Found in Trash', 'pods' ),
1525
+ 'help' => __( 'help', 'pods' ),
1526
+ 'type' => 'text',
1527
+ 'default' => '',
1528
+ 'object_type' => array( 'post_type', 'pod' ),
1529
+ ),
1530
+ 'label_archives' => array(
1531
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label_singular">Item</span> Archives', 'pods' ),
1532
+ 'help' => __( 'help', 'pods' ),
1533
+ 'type' => 'text',
1534
+ 'default' => '',
1535
+ 'object_type' => array( 'post_type' ),
1536
+ ),
1537
+ 'label_attributes' => array(
1538
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label_singular">Item</span> Attributes', 'pods' ),
1539
+ 'help' => __( 'help', 'pods' ),
1540
+ 'type' => 'text',
1541
+ 'default' => '',
1542
+ 'object_type' => array( 'post_type' ),
1543
+ ),
1544
+ 'label_insert_into_item' => array(
1545
+ 'label' => __( 'Insert into <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1546
+ 'help' => __( 'help', 'pods' ),
1547
+ 'type' => 'text',
1548
+ 'default' => '',
1549
+ 'object_type' => array( 'post_type' ),
1550
+ ),
1551
+ 'label_uploaded_to_this_item' => array(
1552
+ 'label' => __( 'Uploaded to this <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1553
+ 'help' => __( 'help', 'pods' ),
1554
+ 'type' => 'text',
1555
+ 'default' => '',
1556
+ 'object_type' => array( 'post_type' ),
1557
+ ),
1558
+ 'label_featured_image' => array(
1559
+ 'label' => __( 'Featured Image', 'pods' ),
1560
+ 'help' => __( 'help', 'pods' ),
1561
+ 'type' => 'text',
1562
+ 'default' => '',
1563
+ // 'depends-on' => array( 'supports_thumbnail' => true ), // @todo Dependency from other tabs not working
1564
+ 'object_type' => array( 'post_type' ),
1565
+ ),
1566
+ 'label_set_featured_image' => array(
1567
+ 'label' => __( 'Set featured Image', 'pods' ),
1568
+ 'help' => __( 'help', 'pods' ),
1569
+ 'type' => 'text',
1570
+ 'default' => '',
1571
+ // 'depends-on' => array( 'supports_thumbnail' => true ), // @todo Dependency from other tabs not working
1572
+ 'object_type' => array( 'post_type' ),
1573
+ ),
1574
+ 'label_remove_featured_image' => array(
1575
+ 'label' => __( 'Remove featured Image', 'pods' ),
1576
+ 'help' => __( 'help', 'pods' ),
1577
+ 'type' => 'text',
1578
+ 'default' => '',
1579
+ // 'depends-on' => array( 'supports_thumbnail' => true ), // @todo Dependency from other tabs not working
1580
+ 'object_type' => array( 'post_type' ),
1581
+ ),
1582
+ 'label_use_featured_image' => array(
1583
+ 'label' => __( 'Use as featured Image', 'pods' ),
1584
+ 'help' => __( 'help', 'pods' ),
1585
+ 'type' => 'text',
1586
+ 'default' => '',
1587
+ // 'depends-on' => array( 'supports_thumbnail' => true ), // @todo Dependency from other tabs not working
1588
+ 'object_type' => array( 'post_type' ),
1589
+ ),
1590
+ 'label_filter_items_list' => array(
1591
+ 'label' => __( 'Filter <span class="pods-slugged" data-sluggable="label">Items</span> lists', 'pods' ),
1592
+ 'help' => __( 'help', 'pods' ),
1593
+ 'type' => 'text',
1594
+ 'default' => '',
1595
+ 'object_type' => array( 'post_type' ),
1596
+ ),
1597
+ 'label_items_list_navigation' => array(
1598
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label">Items</span> list navigation', 'pods' ),
1599
+ 'help' => __( 'help', 'pods' ),
1600
+ 'type' => 'text',
1601
+ 'default' => '',
1602
+ 'object_type' => array( 'post_type', 'taxonomy' ),
1603
+ ),
1604
+ 'label_items_list' => array(
1605
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label">Items</span> list', 'pods' ),
1606
+ 'help' => __( 'help', 'pods' ),
1607
+ 'type' => 'text',
1608
+ 'default' => '',
1609
+ 'object_type' => array( 'post_type', 'taxonomy' ),
1610
+ ),
1611
+ 'label_separate_items_with_commas' => array(
1612
+ 'label' => __( 'Separate <span class="pods-slugged-lower" data-sluggable="label">items</span> with commas', 'pods' ),
1613
+ 'help' => __( 'help', 'pods' ),
1614
+ 'type' => 'text',
1615
+ 'default' => '',
1616
+ 'object_type' => array( 'taxonomy' ),
1617
+ ),
1618
+ 'label_add_or_remove_items' => array(
1619
+ 'label' => __( 'Add or remove <span class="pods-slugged-lower" data-sluggable="label">items</span>', 'pods' ),
1620
+ 'help' => __( 'help', 'pods' ),
1621
+ 'type' => 'text',
1622
+ 'default' => '',
1623
+ 'object_type' => array( 'taxonomy' ),
1624
+ ),
1625
+ 'label_choose_from_the_most_used' => array(
1626
+ 'label' => __( 'Choose from the most used <span class="pods-slugged-lower" data-sluggable="label">items</span>', 'pods' ),
1627
+ 'help' => __( 'help', 'pods' ),
1628
+ 'type' => 'text',
1629
+ 'default' => '',
1630
+ 'object_type' => array( 'taxonomy' ),
1631
+ ),
1632
+ 'label_no_terms' => array(
1633
+ 'label' => __( 'No <span class="pods-slugged-lower" data-sluggable="label">items</span>', 'pods' ),
1634
+ 'help' => __( 'help', 'pods' ),
1635
+ 'type' => 'text',
1636
+ 'default' => '',
1637
+ 'object_type' => array( 'taxonomy' ),
1638
+ ),
1639
+ 'label_item_published' => array(
1640
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label_singular">Item</span> Published.', 'pods' ),
1641
+ 'help' => __( 'help', 'pods' ),
1642
+ 'type' => 'text',
1643
+ 'default' => '',
1644
+ 'object_type' => array( 'post_type' ),
1645
+ ),
1646
+ 'label_item_published_privately' => array(
1647
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label_singular">Item</span> published privately.', 'pods' ),
1648
+ 'help' => __( 'help', 'pods' ),
1649
+ 'type' => 'text',
1650
+ 'default' => '',
1651
+ 'object_type' => array( 'post_type' ),
1652
+ ),
1653
+ 'label_item_reverted_to_draft' => array(
1654
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label_singular">Item</span> reverted to draft.', 'pods' ),
1655
+ 'help' => __( 'help', 'pods' ),
1656
+ 'type' => 'text',
1657
+ 'default' => '',
1658
+ 'object_type' => array( 'post_type' ),
1659
+ ),
1660
+ 'label_item_scheduled' => array(
1661
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label_singular">Item</span> scheduled.', 'pods' ),
1662
+ 'help' => __( 'help', 'pods' ),
1663
+ 'type' => 'text',
1664
+ 'default' => '',
1665
+ 'object_type' => array( 'post_type' ),
1666
+ ),
1667
+ 'label_item_updated' => array(
1668
+ 'label' => __( '<span class="pods-slugged" data-sluggable="label_singular">Item</span> updated.', 'pods' ),
1669
+ 'help' => __( 'help', 'pods' ),
1670
+ 'type' => 'text',
1671
+ 'default' => '',
1672
+ 'object_type' => array( 'post_type' ),
1673
+ ),
1674
+ );
1675
+
1676
+ $options['labels'] = array();
1677
+
1678
+ /**
1679
+ * Filter through all labels if they have an object_type set and match it against the current object type
1680
+ */
1681
+ foreach ( $labels as $label => $labeldata ) {
1682
+ if ( array_key_exists( 'object_type', $labeldata ) ) {
1683
+ if ( in_array( pods_v( 'type', $pod ), $labeldata['object_type'], true ) ) {
1684
+ // Do not add the object_type to the actual label data
1685
+ unset( $labeldata['object_type'] );
1686
+ $options['labels'][ $label ] = $labeldata;
1687
+ }
1688
+ } else {
1689
+ $options['labels'][ $label ] = $labeldata;
1690
+ }
1691
+ }
1692
+ } elseif ( 'settings' === pods_v( 'type', $pod ) ) {
1693
+
1694
+ $options['labels'] = array(
1695
+ 'label' => array(
1696
+ 'label' => __( 'Page Title', 'pods' ),
1697
+ 'help' => __( 'help', 'pods' ),
1698
+ 'type' => 'text',
1699
+ 'default' => str_replace( '_', ' ', pods_v( 'name', $pod ) ),
1700
+ 'text_max_length' => 30,
1701
+ ),
1702
+ 'menu_name' => array(
1703
+ 'label' => __( 'Menu Name', 'pods' ),
1704
+ 'help' => __( 'help', 'pods' ),
1705
+ 'type' => 'text',
1706
+ 'default' => pods_v( 'label', $pod, ucwords( str_replace( '_', ' ', pods_v( 'name', $pod ) ) ) ),
1707
+ 'text_max_length' => 30,
1708
+ ),
1709
+ );
1710
+ }//end if
1711
+
1712
+ if ( 'post_type' === $pod['type'] ) {
1713
+ $options['admin-ui'] = array(
1714
+ 'description' => array(
1715
+ 'label' => __( 'Post Type Description', 'pods' ),
1716
+ 'help' => __( 'A short descriptive summary of what the post type is.', 'pods' ),
1717
+ 'type' => 'text',
1718
+ 'default' => '',
1719
+ ),
1720
+ 'show_ui' => array(
1721
+ 'label' => __( 'Show Admin UI', 'pods' ),
1722
+ 'help' => __( 'Whether to generate a default UI for managing this post type in the admin.', 'pods' ),
1723
+ 'type' => 'boolean',
1724
+ 'default' => pods_v( 'public', $pod, true ),
1725
+ 'boolean_yes_label' => '',
1726
+ ),
1727
+ 'show_in_menu' => array(
1728
+ 'label' => __( 'Show Admin Menu in Dashboard', 'pods' ),
1729
+ 'help' => __( 'Whether to show the post type in the admin menu.', 'pods' ),
1730
+ 'type' => 'boolean',
1731
+ 'default' => pods_v( 'public', $pod, true ),
1732
+ 'dependency' => true,
1733
+ 'boolean_yes_label' => '',
1734
+ ),
1735
+ 'menu_location_custom' => array(
1736
+ 'label' => __( 'Parent Menu ID (optional)', 'pods' ),
1737
+ 'help' => __( 'help', 'pods' ),
1738
+ 'type' => 'text',
1739
+ 'depends-on' => array( 'show_in_menu' => true ),
1740
+ ),
1741
+ 'menu_name' => array(
1742
+ 'label' => __( 'Menu Name', 'pods' ),
1743
+ 'help' => __( 'help', 'pods' ),
1744
+ 'type' => 'text',
1745
+ 'default' => '',
1746
+ 'depends-on' => array( 'show_in_menu' => true ),
1747
+ ),
1748
+ 'menu_position' => array(
1749
+ 'label' => __( 'Menu Position', 'pods' ),
1750
+ 'help' => __( 'help', 'pods' ),
1751
+ 'type' => 'number',
1752
+ 'number_decimals' => 2,
1753
+ 'number_format' => '9999.99',
1754
+ 'number_format_soft' => 1,
1755
+ 'default' => 0,
1756
+ 'depends-on' => array( 'show_in_menu' => true ),
1757
+ ),
1758
+ 'menu_icon' => array(
1759
+ 'label' => __( 'Menu Icon', 'pods' ),
1760
+ 'help' => __( 'URL or Dashicon name for the menu icon. You may specify the path to the icon using one of the <a href="https://pods.io/docs/build/special-magic-tags/#site-tags" target="_blank" rel="noopener noreferrer">site tag</a> type <a href="https://pods.io/docs/build/special-magic-tags/" target="_blank" rel="noopener noreferrer">special magic tags</a>. For example, for a file in your theme directory, use "{@template-url}/path/to/image.png". You may also use the name of a <a href="https://developer.wordpress.org/resource/dashicons/" target="_blank" rel="noopener noreferrer">Dashicon</a>. For example, to use the empty star icon, use "dashicons-star-empty".', 'pods' ),
1761
+ 'type' => 'text',
1762
+ 'default' => '',
1763
+ 'depends-on' => array( 'show_in_menu' => true ),
1764
+ ),
1765
+ 'show_in_nav_menus' => array(
1766
+ 'label' => __( 'Show in Navigation Menus', 'pods' ),
1767
+ 'help' => __( 'help', 'pods' ),
1768
+ 'type' => 'boolean',
1769
+ 'default' => true,
1770
+ 'boolean_yes_label' => '',
1771
+ ),
1772
+ 'show_in_admin_bar' => array(
1773
+ 'label' => __( 'Show in Admin Bar "New" Menu', 'pods' ),
1774
+ 'help' => __( 'help', 'pods' ),
1775
+ 'type' => 'boolean',
1776
+ 'default' => true,
1777
+ 'dependency' => true,
1778
+ 'boolean_yes_label' => '',
1779
+ ),
1780
+ 'name_admin_bar' => array(
1781
+ 'label' => __( 'Admin bar name', 'pods' ),
1782
+ 'help' => __( 'Defaults to singular name', 'pods' ),
1783
+ 'type' => 'text',
1784
+ 'default' => '',
1785
+ 'depends-on' => array( 'show_in_admin_bar' => true ),
1786
+ ),
1787
+ );
1788
+
1789
+ $post_type_name = pods_v( 'name', $pod, 'post_type', true );
1790
+
1791
+ $options['advanced'] = array(
1792
+ 'public' => array(
1793
+ 'label' => __( 'Public', 'pods' ),
1794
+ 'help' => __( 'help', 'pods' ),
1795
+ 'type' => 'boolean',
1796
+ 'default' => true,
1797
+ 'boolean_yes_label' => '',
1798
+ ),
1799
+ 'publicly_queryable' => array(
1800
+ 'label' => __( 'Publicly Queryable', 'pods' ),
1801
+ 'help' => __( 'help', 'pods' ),
1802
+ 'type' => 'boolean',
1803
+ 'default' => pods_v( 'public', $pod, true ),
1804
+ 'boolean_yes_label' => '',
1805
+ ),
1806
+ 'exclude_from_search' => array(
1807
+ 'label' => __( 'Exclude from Search', 'pods' ),
1808
+ 'help' => __( 'help', 'pods' ),
1809
+ 'type' => 'boolean',
1810
+ 'default' => ! pods_v( 'public', $pod, true ),
1811
+ 'boolean_yes_label' => '',
1812
+ ),
1813
+ 'capability_type' => array(
1814
+ 'label' => __( 'User Capability', 'pods' ),
1815
+ 'help' => __( 'Uses these capabilties for access to this post type: edit_{capability}, read_{capability}, and delete_{capability}', 'pods' ),
1816
+ 'type' => 'pick',
1817
+ 'default' => 'post',
1818
+ 'data' => array(
1819
+ 'post' => 'post',
1820
+ 'page' => 'page',
1821
+ 'custom' => __( 'Custom Capability', 'pods' ),
1822
+ ),
1823
+ 'dependency' => true,
1824
+ ),
1825
+ 'capability_type_custom' => array(
1826
+ 'label' => __( 'Custom User Capability', 'pods' ),
1827
+ 'help' => __( 'help', 'pods' ),
1828
+ 'type' => 'text',
1829
+ 'default' => pods_v( 'name', $pod ),
1830
+ 'depends-on' => array( 'capability_type' => 'custom' ),
1831
+ ),
1832
+ 'capability_type_extra' => array(
1833
+ 'label' => __( 'Additional User Capabilities', 'pods' ),
1834
+ 'help' => __( 'Enables additional capabilities for this Post Type including: delete_{capability}s, delete_private_{capability}s, delete_published_{capability}s, delete_others_{capability}s, edit_private_{capability}s, and edit_published_{capability}s', 'pods' ),
1835
+ 'type' => 'boolean',
1836
+ 'default' => true,
1837
+ 'boolean_yes_label' => '',
1838
+ ),
1839
+ 'has_archive' => array(
1840
+ 'label' => __( 'Enable Archive Page', 'pods' ),
1841
+ 'help' => __( 'If enabled, creates an archive page with list of of items in this custom post type. Functions like a category page for posts. Can be controlled with a template in your theme called "archive-{$post-type}.php".', 'pods' ),
1842
+ 'type' => 'boolean',
1843
+ 'default' => false,
1844
+ 'dependency' => true,
1845
+ 'boolean_yes_label' => '',
1846
+ ),
1847
+ 'has_archive_slug' => array(
1848
+ 'label' => __( 'Archive Page Slug Override', 'pods' ),
1849
+ 'help' => __( 'If archive page is enabled, you can override the slug used by WordPress, which defaults to the name of the post type.', 'pods' ),
1850
+ 'type' => 'text',
1851
+ 'default' => '',
1852
+ 'depends-on' => array( 'has_archive' => true ),
1853
+ ),
1854
+ 'hierarchical' => array(
1855
+ 'label' => __( 'Hierarchical', 'pods' ),
1856
+ 'help' => __( 'Allows for parent/ child relationships between items, just like with Pages. Note: To edit relationships in the post editor, you must enable "Page Attributes" in the "Supports" section below.', 'pods' ),
1857
+ 'type' => 'boolean',
1858
+ 'default' => false,
1859
+ 'dependency' => true,
1860
+ 'boolean_yes_label' => '',
1861
+ ),
1862
+ 'label_parent_item_colon' => array(
1863
+ 'label' => __( '<strong>Label: </strong> Parent <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
1864
+ 'help' => __( 'help', 'pods' ),
1865
+ 'type' => 'text',
1866
+ 'default' => '',
1867
+ 'depends-on' => array( 'hierarchical' => true ),
1868
+ ),
1869
+ 'label_parent' => array(
1870
+ 'label' => __( '<strong>Label: </strong> Parent', 'pods' ),
1871
+ 'help' => __( 'help', 'pods' ),
1872
+ 'type' => 'text',
1873
+ 'default' => '',
1874
+ 'depends-on' => array( 'hierarchical' => true ),
1875
+ ),
1876
+ 'rewrite' => array(
1877
+ 'label' => __( 'Rewrite', 'pods' ),
1878
+ 'help' => __( 'Allows you to use pretty permalinks, if set in WordPress Settings->Permalinks. If not enabled, your links will be in the form of "example.com/?pod_name=post_slug" regardless of your permalink settings.', 'pods' ),
1879
+ 'type' => 'boolean',
1880
+ 'default' => true,
1881
+ 'dependency' => true,
1882
+ 'boolean_yes_label' => '',
1883
+ ),
1884
+ 'rewrite_custom_slug' => array(
1885
+ 'label' => __( 'Custom Rewrite Slug', 'pods' ),
1886
+ 'help' => __( 'Changes the first segment of the URL, which by default is the name of the Pod. For example, if your Pod is called "foo", if this field is left blank, your link will be "example.com/foo/post_slug", but if you were to enter "bar" your link will be "example.com/bar/post_slug".', 'pods' ),
1887
+ 'type' => 'text',
1888
+ 'default' => '',
1889
+ 'depends-on' => array( 'rewrite' => true ),
1890
+ ),
1891
+ 'rewrite_with_front' => array(
1892
+ 'label' => __( 'Rewrite with Front', 'pods' ),
1893
+ 'help' => __( 'Allows permalinks to be prepended with your front base (example: if your permalink structure is /blog/, then your links will be: Unchecked->/news/, Checked->/blog/news/)', 'pods' ),
1894
+ 'type' => 'boolean',
1895
+ 'default' => true,
1896
+ 'depends-on' => array( 'rewrite' => true ),
1897
+ 'boolean_yes_label' => '',
1898
+ ),
1899
+ 'rewrite_feeds' => array(
1900
+ 'label' => __( 'Rewrite Feeds', 'pods' ),
1901
+ 'help' => __( 'help', 'pods' ),
1902
+ 'type' => 'boolean',
1903
+ 'default' => false,
1904
+ 'depends-on' => array( 'rewrite' => true ),
1905
+ 'boolean_yes_label' => '',
1906
+ ),
1907
+ 'rewrite_pages' => array(
1908
+ 'label' => __( 'Rewrite Pages', 'pods' ),
1909
+ 'help' => __( 'help', 'pods' ),
1910
+ 'type' => 'boolean',
1911
+ 'default' => true,
1912
+ 'depends-on' => array( 'rewrite' => true ),
1913
+ 'boolean_yes_label' => '',
1914
+ ),
1915
+ 'query_var' => array(
1916
+ 'label' => __( 'Query Var', 'pods' ),
1917
+ 'help' => __( 'The Query Var is used in the URL and underneath the WordPress Rewrite API to tell WordPress what page or post type you are on. For a list of reserved Query Vars, read <a href="http://codex.wordpress.org/WordPress_Query_Vars">WordPress Query Vars</a> from the WordPress Codex.', 'pods' ),
1918
+ 'type' => 'boolean',
1919
+ 'default' => true,
1920
+ 'boolean_yes_label' => '',
1921
+ ),
1922
+ 'can_export' => array(
1923
+ 'label' => __( 'Exportable', 'pods' ),
1924
+ 'help' => __( 'help', 'pods' ),
1925
+ 'type' => 'boolean',
1926
+ 'default' => true,
1927
+ 'boolean_yes_label' => '',
1928
+ ),
1929
+ 'default_status' => array(
1930
+ 'label' => __( 'Default Status', 'pods' ),
1931
+ 'help' => __( 'help', 'pods' ),
1932
+ 'type' => 'pick',
1933
+ 'pick_object' => 'post-status',
1934
+ 'default' => apply_filters( "pods_api_default_status_{$post_type_name}", 'draft', $pod ),
1935
+ ),
1936
+ );
1937
+ } elseif ( 'taxonomy' === $pod['type'] ) {
1938
+ $options['admin-ui'] = array(
1939
+ 'description' => array(
1940
+ 'label' => __( 'Taxonomy Description', 'pods' ),
1941
+ 'help' => __( 'A short descriptive summary of what the taxonomy is.', 'pods' ),
1942
+ 'type' => 'text',
1943
+ 'default' => '',
1944
+ ),
1945
+ 'show_ui' => array(
1946
+ 'label' => __( 'Show Admin UI', 'pods' ),
1947
+ 'help' => __( 'Whether to generate a default UI for managing this taxonomy.', 'pods' ),
1948
+ 'type' => 'boolean',
1949
+ 'default' => pods_v( 'public', $pod, true ),
1950
+ 'dependency' => true,
1951
+ 'boolean_yes_label' => '',
1952
+ ),
1953
+ 'show_in_menu' => array(
1954
+ 'label' => __( 'Show Admin Menu in Dashboard', 'pods' ),
1955
+ 'help' => __( 'Whether to show the taxonomy in the admin menu.', 'pods' ),
1956
+ 'type' => 'boolean',
1957
+ 'default' => pods_v( 'public', $pod, true ),
1958
+ 'dependency' => true,
1959
+ 'depends-on' => array( 'show_ui' => true ),
1960
+ 'boolean_yes_label' => '',
1961
+ ),
1962
+ 'menu_name' => array(
1963
+ 'label' => __( 'Menu Name', 'pods' ),
1964
+ 'help' => __( 'help', 'pods' ),
1965
+ 'type' => 'text',
1966
+ 'default' => '',
1967
+ 'depends-on' => array( 'show_ui' => true ),
1968
+ ),
1969
+ 'menu_location' => array(
1970
+ 'label' => __( 'Menu Location', 'pods' ),
1971
+ 'help' => __( 'help', 'pods' ),
1972
+ 'type' => 'pick',
1973
+ 'default' => 'default',
1974
+ 'depends-on' => array( 'show_ui' => true ),
1975
+ 'data' => array(
1976
+ 'default' => __( 'Default - Add to associated Post Type(s) menus', 'pods' ),
1977
+ 'settings' => __( 'Add a submenu item to Settings menu', 'pods' ),
1978
+ 'appearances' => __( 'Add a submenu item to Appearances menu', 'pods' ),
1979
+ 'submenu' => __( 'Add a submenu item to another menu', 'pods' ),
1980
+ 'objects' => __( 'Make a new menu item', 'pods' ),
1981
+ 'top' => __( 'Make a new menu item below Settings', 'pods' ),
1982
+ ),
1983
+ 'dependency' => true,
1984
+ ),
1985
+ 'menu_location_custom' => array(
1986
+ 'label' => __( 'Custom Menu Location', 'pods' ),
1987
+ 'help' => __( 'help', 'pods' ),
1988
+ 'type' => 'text',
1989
+ 'depends-on' => array( 'menu_location' => 'submenu' ),
1990
+ ),
1991
+ 'menu_position' => array(
1992
+ 'label' => __( 'Menu Position', 'pods' ),
1993
+ 'help' => __( 'help', 'pods' ),
1994
+ 'type' => 'number',
1995
+ 'number_decimals' => 2,
1996
+ 'number_format' => '9999.99',
1997
+ 'number_format_soft' => 1,
1998
+ 'default' => 0,
1999
+ 'depends-on' => array( 'menu_location' => array( 'objects', 'top' ) ),
2000
+ ),
2001
+ 'menu_icon' => array(
2002
+ 'label' => __( 'Menu Icon URL', 'pods' ),
2003
+ 'help' => __( 'help', 'pods' ),
2004
+ 'type' => 'text',
2005
+ 'default' => '',
2006
+ 'depends-on' => array( 'menu_location' => array( 'objects', 'top' ) ),
2007
+ ),
2008
+ 'show_in_nav_menus' => array(
2009
+ 'label' => __( 'Show in Navigation Menus', 'pods' ),
2010
+ 'help' => __( 'help', 'pods' ),
2011
+ 'type' => 'boolean',
2012
+ 'default' => pods_v( 'public', $pod, true ),
2013
+ 'boolean_yes_label' => '',
2014
+ ),
2015
+ 'show_tagcloud' => array(
2016
+ 'label' => __( 'Allow in Tagcloud Widget', 'pods' ),
2017
+ 'help' => __( 'help', 'pods' ),
2018
+ 'type' => 'boolean',
2019
+ 'default' => pods_v( 'show_ui', $pod, pods_v( 'public', $pod, true ) ),
2020
+ 'boolean_yes_label' => '',
2021
+ ),
2022
+ // @todo check https://core.trac.wordpress.org/ticket/36964
2023
+ 'show_tagcloud_in_edit' => array(
2024
+ 'label' => __( 'Allow Tagcloud on term edit pages', 'pods' ),
2025
+ 'help' => __( 'help', 'pods' ),
2026
+ 'type' => 'boolean',
2027
+ 'default' => pods_v( 'show_ui', $pod, pods_v( 'show_tagcloud', $pod, true ) ),
2028
+ 'boolean_yes_label' => '',
2029
+ ),
2030
+ 'show_in_quick_edit' => array(
2031
+ 'label' => __( 'Allow in quick/bulk edit panel', 'pods' ),
2032
+ 'help' => __( 'help', 'pods' ),
2033
+ 'type' => 'boolean',
2034
+ 'default' => pods_v( 'show_ui', $pod, pods_v( 'public', $pod, true ) ),
2035
+ 'boolean_yes_label' => '',
2036
+ ),
2037
+ );
2038
+
2039
+ $options['admin-ui']['show_admin_column'] = array(
2040
+ 'label' => __( 'Show Taxonomy column on Post Types', 'pods' ),
2041
+ 'help' => __( 'Whether to add a column for this taxonomy on the associated post types manage screens', 'pods' ),
2042
+ 'type' => 'boolean',
2043
+ 'default' => false,
2044
+ 'boolean_yes_label' => '',
2045
+ );
2046
+
2047
+ // Integration for Single Value Taxonomy UI
2048
+ if ( function_exists( 'tax_single_value_meta_box' ) ) {
2049
+ $options['admin-ui']['single_value'] = array(
2050
+ 'label' => __( 'Single Value Taxonomy', 'pods' ),
2051
+ 'help' => __( 'Use a drop-down for the input instead of the WordPress default', 'pods' ),
2052
+ 'type' => 'boolean',
2053
+ 'default' => false,
2054
+ 'boolean_yes_label' => '',
2055
+ );
2056
+
2057
+ $options['admin-ui']['single_value_required'] = array(
2058
+ 'label' => __( 'Single Value Taxonomy - Required', 'pods' ),
2059
+ 'help' => __( 'A term will be selected by default in the Post Editor, not optional', 'pods' ),
2060
+ 'type' => 'boolean',
2061
+ 'default' => false,
2062
+ 'boolean_yes_label' => '',
2063
+ );
2064
+ }
2065
+
2066
+ $options['advanced'] = array(
2067
+ 'public' => array(
2068
+ 'label' => __( 'Public', 'pods' ),
2069
+ 'help' => __( 'help', 'pods' ),
2070
+ 'type' => 'boolean',
2071
+ 'default' => true,
2072
+ 'boolean_yes_label' => '',
2073
+ ),
2074
+ 'hierarchical' => array(
2075
+ 'label' => __( 'Hierarchical', 'pods' ),
2076
+ 'help' => __( 'Hierarchical taxonomies will have a list with checkboxes to select an existing category in the taxonomy admin box on the post edit page (like default post categories). Non-hierarchical taxonomies will just have an empty text field to type-in taxonomy terms to associate with the post (like default post tags).', 'pods' ),
2077
+ 'type' => 'boolean',
2078
+ 'default' => true,
2079
+ 'dependency' => true,
2080
+ 'boolean_yes_label' => '',
2081
+ ),
2082
+ 'label_parent_item_colon' => array(
2083
+ 'label' => __( '<strong>Label: </strong> Parent <span class="pods-slugged" data-sluggable="label_singular">Item</span>', 'pods' ),
2084
+ 'help' => __( 'help', 'pods' ),
2085
+ 'type' => 'text',
2086
+ 'default' => '',
2087
+ 'depends-on' => array( 'hierarchical' => true ),
2088
+ ),
2089
+ 'label_parent' => array(
2090
+ 'label' => __( '<strong>Label: </strong> Parent', 'pods' ),
2091
+ 'help' => __( 'help', 'pods' ),
2092
+ 'type' => 'text',
2093
+ 'default' => '',
2094
+ 'depends-on' => array( 'hierarchical' => true ),
2095
+ ),
2096
+ 'label_no_terms' => array(
2097
+ 'label' => __( '<strong>Label: </strong> No <span class="pods-slugged" data-sluggable="label">Items</span>', 'pods' ),
2098
+ 'help' => __( 'help', 'pods' ),
2099
+ 'type' => 'text',
2100
+ 'default' => '',
2101
+ 'depends-on' => array( 'hierarchical' => true ),
2102
+ ),
2103
+ 'rewrite' => array(
2104
+ 'label' => __( 'Rewrite', 'pods' ),
2105
+ 'help' => __( 'help', 'pods' ),
2106
+ 'type' => 'boolean',
2107
+ 'default' => true,
2108
+ 'dependency' => true,
2109
+ 'boolean_yes_label' => '',
2110
+ ),
2111
+ 'rewrite_custom_slug' => array(
2112
+ 'label' => __( 'Custom Rewrite Slug', 'pods' ),
2113
+ 'help' => __( 'help', 'pods' ),
2114
+ 'type' => 'text',
2115
+ 'default' => '',
2116
+ 'depends-on' => array( 'rewrite' => true ),
2117
+ ),
2118
+ 'rewrite_with_front' => array(
2119
+ 'label' => __( 'Rewrite with Front', 'pods' ),
2120
+ 'help' => __( 'Allows permalinks to be prepended with your front base (example: if your permalink structure is /blog/, then your links will be: Unchecked->/news/, Checked->/blog/news/)', 'pods' ),
2121
+ 'type' => 'boolean',
2122
+ 'default' => true,
2123
+ 'boolean_yes_label' => '',
2124
+ 'depends-on' => array( 'rewrite' => true ),
2125
+ ),
2126
+ 'rewrite_hierarchical' => array(
2127
+ 'label' => __( 'Hierarchical Permalinks', 'pods' ),
2128
+ 'help' => __( 'help', 'pods' ),
2129
+ 'type' => 'boolean',
2130
+ 'default' => true,
2131
+ 'boolean_yes_label' => '',
2132
+ 'depends-on' => array( 'rewrite' => true ),
2133
+ ),
2134
+ 'capability_type' => array(
2135
+ 'label' => __( 'User Capability', 'pods' ),
2136
+ 'help' => __( 'Uses WordPress term capabilities by default', 'pods' ),
2137
+ 'type' => 'pick',
2138
+ 'default' => 'default',
2139
+ 'data' => array(
2140
+ 'default' => 'Default',
2141
+ 'custom' => __( 'Custom Capability', 'pods' ),
2142
+ ),
2143
+ 'dependency' => true,
2144
+ ),
2145
+ 'capability_type_custom' => array(
2146
+ 'label' => __( 'Custom User Capability', 'pods' ),
2147
+ 'help' => __( 'Enables additional capabilities for this Taxonomy including: manage_{capability}_terms, edit_{capability}_terms, assign_{capability}_terms, and delete_{capability}_terms', 'pods' ),
2148
+ 'type' => 'text',
2149
+ 'default' => pods_v( 'name', $pod ),
2150
+ 'depends-on' => array( 'capability_type' => 'custom' ),
2151
+ ),
2152
+ 'query_var' => array(
2153
+ 'label' => __( 'Query Var', 'pods' ),
2154
+ 'help' => __( 'help', 'pods' ),
2155
+ 'type' => 'boolean',
2156
+ 'default' => false,
2157
+ 'boolean_yes_label' => '',
2158
+ ),
2159
+ 'query_var' => array(
2160
+ 'label' => __( 'Query Var', 'pods' ),
2161
+ 'help' => __( 'help', 'pods' ),
2162
+ 'type' => 'boolean',
2163
+ 'default' => false,
2164
+ 'dependency' => true,
2165
+ 'boolean_yes_label' => '',
2166
+ ),
2167
+ 'query_var_string' => array(
2168
+ 'label' => __( 'Custom Query Var Name', 'pods' ),
2169
+ 'help' => __( 'help', 'pods' ),
2170
+ 'type' => 'text',
2171
+ 'default' => '',
2172
+ 'depends-on' => array( 'query_var' => true ),
2173
+ ),
2174
+ 'sort' => array(
2175
+ 'label' => __( 'Remember order saved on Post Types', 'pods' ),
2176
+ 'help' => __( 'help', 'pods' ),
2177
+ 'type' => 'boolean',
2178
+ 'default' => false,
2179
+ 'boolean_yes_label' => '',
2180
+ ),
2181
+ 'update_count_callback' => array(
2182
+ 'label' => __( 'Function to call when updating counts', 'pods' ),
2183
+ 'help' => __( 'help', 'pods' ),
2184
+ 'type' => 'text',
2185
+ 'default' => '',
2186
+ ),
2187
+ );
2188
+ } elseif ( 'settings' === $pod['type'] ) {
2189
+ $options['admin-ui'] = array(
2190
+ 'ui_style' => array(
2191
+ 'label' => __( 'Admin UI Style', 'pods' ),
2192
+ 'help' => __( 'help', 'pods' ),
2193
+ 'type' => 'pick',
2194
+ 'default' => 'settings',
2195
+ 'data' => array(
2196
+ 'settings' => __( 'Normal Settings Form', 'pods' ),
2197
+ 'post_type' => __( 'Post Type UI', 'pods' ),
2198
+ 'custom' => __( 'Custom (hook into pods_admin_ui_custom or pods_admin_ui_custom_{podname} action)', 'pods' ),
2199
+ ),
2200
+ 'dependency' => true,
2201
+ ),
2202
+ 'menu_location' => array(
2203
+ 'label' => __( 'Menu Location', 'pods' ),
2204
+ 'help' => __( 'help', 'pods' ),
2205
+ 'type' => 'pick',
2206
+ 'default' => 'settings',
2207
+ 'data' => array(
2208
+ 'settings' => __( 'Add a submenu item to Settings menu', 'pods' ),
2209
+ 'appearances' => __( 'Add a submenu item to Appearances menu', 'pods' ),
2210
+ 'submenu' => __( 'Add a submenu item to another menu', 'pods' ),
2211
+ 'top' => __( 'Make a new menu item below Settings', 'pods' ),
2212
+ ),
2213
+ 'dependency' => true,
2214
+ ),
2215
+ 'menu_location_custom' => array(
2216
+ 'label' => __( 'Custom Menu Location', 'pods' ),
2217
+ 'help' => __( 'help', 'pods' ),
2218
+ 'type' => 'text',
2219
+ 'depends-on' => array( 'menu_location' => 'submenu' ),
2220
+ ),
2221
+ 'menu_position' => array(
2222
+ 'label' => __( 'Menu Position', 'pods' ),
2223
+ 'help' => __( 'help', 'pods' ),
2224
+ 'type' => 'number',
2225
+ 'number_decimals' => 2,
2226
+ 'number_format' => '9999.99',
2227
+ 'number_format_soft' => 1,
2228
+ 'default' => 0,
2229
+ 'depends-on' => array( 'menu_location' => 'top' ),
2230
+ ),
2231
+ 'menu_icon' => array(
2232
+ 'label' => __( 'Menu Icon URL', 'pods' ),
2233
+ 'help' => __( 'help', 'pods' ),
2234
+ 'type' => 'text',
2235
+ 'default' => '',
2236
+ 'depends-on' => array( 'menu_location' => 'top' ),
2237
+ ),
2238
+ );
2239
+
2240
+ // @todo fill this in
2241
+ $options['advanced'] = array(
2242
+ 'temporary' => 'This type has the fields hardcoded',
2243
+ // :(
2244
+ );
2245
+ } elseif ( 'pod' === $pod['type'] ) {
2246
+ $actions_enabled = array(
2247
+ 'add',
2248
+ 'edit',
2249
+ 'duplicate',
2250
+ 'delete',
2251
+ );
2252
+
2253
+ if ( 1 === (int) pods_v( 'ui_export', $pod ) ) {
2254
+ $actions_enabled = array(
2255
+ 'add',
2256
+ 'edit',
2257
+ 'duplicate',
2258
+ 'delete',
2259
+ 'export',
2260
+ );
2261
+ }
2262
+
2263
+ $options['admin-ui'] = array(
2264
+ 'ui_style' => array(
2265
+ 'label' => __( 'Admin UI Style', 'pods' ),
2266
+ 'help' => __( 'help', 'pods' ),
2267
+ 'type' => 'pick',
2268
+ 'default' => 'settings',
2269
+ 'data' => array(
2270
+ 'post_type' => __( 'Normal (Looks like the Post Type UI)', 'pods' ),
2271
+ 'custom' => __( 'Custom (hook into pods_admin_ui_custom or pods_admin_ui_custom_{podname} action)', 'pods' ),
2272
+ ),
2273
+ 'dependency' => true,
2274
+ ),
2275
+ 'show_in_menu' => array(
2276
+ 'label' => __( 'Show Admin Menu in Dashboard', 'pods' ),
2277
+ 'help' => __( 'help', 'pods' ),
2278
+ 'type' => 'boolean',
2279
+ 'default' => false,
2280
+ 'boolean_yes_label' => '',
2281
+ 'dependency' => true,
2282
+ ),
2283
+ 'menu_location_custom' => array(
2284
+ 'label' => __( 'Parent Menu ID (optional)', 'pods' ),
2285
+ 'help' => __( 'help', 'pods' ),
2286
+ 'type' => 'text',
2287
+ 'depends-on' => array( 'show_in_menu' => true ),
2288
+ ),
2289
+ 'menu_position' => array(
2290
+ 'label' => __( 'Menu Position', 'pods' ),
2291
+ 'help' => __( 'help', 'pods' ),
2292
+ 'type' => 'number',
2293
+ 'number_decimals' => 2,
2294
+ 'number_format' => '9999.99',
2295
+ 'number_format_soft' => 1,
2296
+ 'default' => 0,
2297
+ 'depends-on' => array( 'show_in_menu' => true ),
2298
+ ),
2299
+ 'menu_icon' => array(
2300
+ 'label' => __( 'Menu Icon URL', 'pods' ),
2301
+ 'help' => __( 'This is the icon shown to the left of the menu text for this content type.', 'pods' ),
2302
+ 'type' => 'text',
2303
+ 'default' => '',
2304
+ 'depends-on' => array( 'show_in_menu' => true ),
2305
+ ),
2306
+ 'ui_icon' => array(
2307
+ 'label' => __( 'Header Icon', 'pods' ),
2308
+ 'help' => __( 'This is the icon shown to the left of the heading text at the top of the manage pages for this content type.', 'pods' ),
2309
+ 'type' => 'file',
2310
+ 'default' => '',
2311
+ 'file_edit_title' => 0,
2312
+ 'depends-on' => array( 'show_in_menu' => true ),
2313
+ ),
2314
+ 'ui_actions_enabled' => array(
2315
+ 'label' => __( 'Actions Available', 'pods' ),
2316
+ 'help' => __( 'help', 'pods' ),
2317
+ 'type' => 'pick',
2318
+ 'default' => $actions_enabled,
2319
+ 'data' => array(
2320
+ 'add' => __( 'Add New', 'pods' ),
2321
+ 'edit' => __( 'Edit', 'pods' ),
2322
+ 'duplicate' => __( 'Duplicate', 'pods' ),
2323
+ 'delete' => __( 'Delete', 'pods' ),
2324
+ 'reorder' => __( 'Reorder', 'pods' ),
2325
+ 'export' => __( 'Export', 'pods' ),
2326
+ ),
2327
+ 'pick_format_type' => 'multi',
2328
+ 'dependency' => true,
2329
+ ),
2330
+ 'ui_reorder_field' => array(
2331
+ 'label' => __( 'Reorder Field', 'pods' ),
2332
+ 'help' => __( 'This is the field that will be reordered on, it should be numeric.', 'pods' ),
2333
+ 'type' => 'text',
2334
+ 'default' => 'menu_order',
2335
+ 'depends-on' => array( 'ui_actions_enabled' => 'reorder' ),
2336
+ ),
2337
+ 'ui_fields_manage' => array(
2338
+ 'label' => __( 'Admin Table Columns', 'pods' ),
2339
+ 'help' => __( 'help', 'pods' ),
2340
+ 'type' => 'pick',
2341
+ 'default' => array(),
2342
+ 'data' => array(),
2343
+ 'pick_format_type' => 'multi',
2344
+ ),
2345
+ 'ui_filters' => array(
2346
+ 'label' => __( 'Search Filters', 'pods' ),
2347
+ 'help' => __( 'help', 'pods' ),
2348
+ 'type' => 'pick',
2349
+ 'default' => array(),
2350
+ 'data' => array(),
2351
+ 'pick_format_type' => 'multi',
2352
+ ),
2353
+ );
2354
+
2355
+ if ( ! empty( $pod['fields'] ) ) {
2356
+ if ( isset( $pod['fields'][ pods_v( 'pod_index', $pod, 'name' ) ] ) ) {
2357
+ $options['admin-ui']['ui_fields_manage']['default'][] = pods_v( 'pod_index', $pod, 'name' );
2358
+ }
2359
+
2360
+ if ( isset( $pod['fields']['modified'] ) ) {
2361
+ $options['admin-ui']['ui_fields_manage']['default'][] = 'modified';
2362
+ }
2363
+
2364
+ foreach ( $pod['fields'] as $field ) {
2365
+ $type = '';
2366
+
2367
+ if ( isset( $field_types[ $field['type'] ] ) ) {
2368
+ $type = ' <small>(' . $field_types[ $field['type'] ]['label'] . ')</small>';
2369
+ }
2370
+
2371
+ $options['admin-ui']['ui_fields_manage']['data'][ $field['name'] ] = $field['label'] . $type;
2372
+ $options['admin-ui']['ui_filters']['data'][ $field['name'] ] = $field['label'] . $type;
2373
+ }
2374
+
2375
+ $options['admin-ui']['ui_fields_manage']['data']['id'] = 'ID';
2376
+ } else {
2377
+ unset( $options['admin-ui']['ui_fields_manage'] );
2378
+ unset( $options['admin-ui']['ui_filters'] );
2379
+ }//end if
2380
+
2381
+ // @todo fill this in
2382
+ $options['advanced'] = array(
2383
+ 'temporary' => 'This type has the fields hardcoded',
2384
+ // :(
2385
+ );
2386
+ }//end if
2387
+
2388
+ $pod_type = $pod['type'];
2389
+ $pod_name = $pod['name'];
2390
+
2391
+ /**
2392
+ * Add admin fields to the Pods editor for a specific Pod
2393
+ *
2394
+ * @param array $options The Options fields.
2395
+ * @param object $pod Current Pods object.
2396
+ *
2397
+ * @since unkown
2398
+ */
2399
+ $options = apply_filters( "pods_admin_setup_edit_options_{$pod_type}_{$pod_name}", $options, $pod );
2400
+
2401
+ /**
2402
+ * Add admin fields to the Pods editor for any Pod of a specific content type.
2403
+ *
2404
+ * @param array $options The Options fields.
2405
+ * @param object $pod Current Pods object.
2406
+ */
2407
+ $options = apply_filters( "pods_admin_setup_edit_options_{$pod_type}", $options, $pod );
2408
+
2409
+ /**
2410
+ * Add admin fields to the Pods editor for all Pods
2411
+ *
2412
+ * @param array $options The Options fields.
2413
+ * @param object $pod Current Pods object.
2414
+ */
2415
+ $options = apply_filters( 'pods_admin_setup_edit_options', $options, $pod );
2416
+
2417
+ return $options;
2418
+ }
2419
+
2420
+ /**
2421
+ * Get list of Pod field option tabs
2422
+ *
2423
+ * @param array $pod Pod options.
2424
+ *
2425
+ * @return array
2426
+ */
2427
+ public function admin_setup_edit_field_tabs( $pod ) {
2428
+
2429
+ $core_tabs = array(
2430
+ 'basic' => __( 'Basic', 'pods' ),
2431
+ 'additional-field' => __( 'Additional Field Options', 'pods' ),
2432
+ 'advanced' => __( 'Advanced', 'pods' ),
2433
+ );
2434
+
2435
+ /**
2436
+ * Field option tabs
2437
+ *
2438
+ * Use to add new tabs, default tabs are added after this filter (IE you can't remove/modify them with this, kthanksbye).
2439
+ *
2440
+ * @since unknown
2441
+ *
2442
+ * @param array $tabs Tabs to add, starts empty.
2443
+ * @param object|Pod $pod Current Pods object.
2444
+ */
2445
+ $tabs = apply_filters( 'pods_admin_setup_edit_field_tabs', array(), $pod );
2446
+
2447
+ $tabs = array_merge( $core_tabs, $tabs );
2448
+
2449
+ return $tabs;
2450
+ }
2451
+
2452
+ /**
2453
+ * Get list of Pod field options
2454
+ *
2455
+ * @param array $pod Pod options.
2456
+ *
2457
+ * @return array
2458
+ */
2459
+ public function admin_setup_edit_field_options( $pod ) {
2460
+
2461
+ $options = array();
2462
+
2463
+ $options['additional-field'] = array();
2464
+
2465
+ $field_types = PodsForm::field_types();
2466
+
2467
+ foreach ( $field_types as $type => $field_type_data ) {
2468
+ /**
2469
+ * @var $field_type PodsField
2470
+ */
2471
+ $field_type = PodsForm::field_loader( $type, $field_type_data['file'] );
2472
+
2473
+ $field_type_vars = get_class_vars( get_class( $field_type ) );
2474
+
2475
+ if ( ! isset( $field_type_vars['pod_types'] ) ) {
2476
+ $field_type_vars['pod_types'] = true;
2477
+ }
2478
+
2479
+ $options['additional-field'][ $type ] = array();
2480
+
2481
+ // Only show supported field types
2482
+ if ( true !== $field_type_vars['pod_types'] ) {
2483
+ if ( empty( $field_type_vars['pod_types'] ) ) {
2484
+ continue;
2485
+ } elseif ( is_array( $field_type_vars['pod_types'] ) && ! in_array( pods_v( 'type', $pod ), $field_type_vars['pod_types'], true ) ) {
2486
+ continue;
2487
+ } elseif ( ! is_array( $field_type_vars['pod_types'] ) && pods_v( 'type', $pod ) !== $field_type_vars['pod_types'] ) {
2488
+ continue;
2489
+ }
2490
+ }
2491
+
2492
+ $options['additional-field'][ $type ] = PodsForm::ui_options( $type );
2493
+
2494
+ /**
2495
+ * Modify Additional Field Options tab
2496
+ *
2497
+ * @since 2.7.0
2498
+ *
2499
+ * @param array $options Additional field type options,
2500
+ * @param string $type Field type,
2501
+ * @param array $options Tabs, indexed by label,
2502
+ * @param object|Pods $pod Pods object for the Pod this UI is for.
2503
+ */
2504
+ $options['additional-field'][ $type ] = apply_filters( "pods_admin_setup_edit_{$type}_additional_field_options", $options['additional-field'][ $type ], $type, $options, $pod );
2505
+ $options['additional-field'][ $type ] = apply_filters( 'pods_admin_setup_edit_additional_field_options', $options['additional-field'][ $type ], $type, $options, $pod );
2506
+ }//end foreach
2507
+
2508
+ $input_helpers = array(
2509
+ '' => '-- Select --',
2510
+ );
2511
+
2512
+ if ( class_exists( 'Pods_Helpers' ) ) {
2513
+ $helpers = pods_api()->load_helpers( array( 'options' => array( 'helper_type' => 'input' ) ) );
2514
+
2515
+ foreach ( $helpers as $helper ) {
2516
+ $input_helpers[ $helper['name'] ] = $helper['name'];
2517
+ }
2518
+ }
2519
+
2520
+ $options['advanced'] = array(
2521
+ __( 'Visual', 'pods' ) => array(
2522
+ 'class' => array(
2523
+ 'name' => 'class',
2524
+ 'label' => __( 'Additional CSS Classes', 'pods' ),
2525
+ 'help' => __( 'help', 'pods' ),
2526
+ 'type' => 'text',
2527
+ 'default' => '',
2528
+ ),
2529
+ 'input_helper' => array(
2530
+ 'name' => 'input_helper',
2531
+ 'label' => __( 'Input Helper', 'pods' ),
2532
+ 'help' => __( 'help', 'pods' ),
2533
+ 'type' => 'pick',
2534
+ 'default' => '',
2535
+ 'data' => $input_helpers,
2536
+ ),
2537
+ ),
2538
+ __( 'Values', 'pods' ) => array(
2539
+ 'default_value' => array(
2540
+ 'name' => 'default_value',
2541
+ 'label' => __( 'Default Value', 'pods' ),
2542
+ 'help' => __( 'help', 'pods' ),
2543
+ 'type' => 'text',
2544
+ 'default' => '',
2545
+ 'options' => array(
2546
+ 'text_max_length' => - 1,
2547
+ ),
2548
+ ),
2549
+ 'default_value_parameter' => array(
2550
+ 'name' => 'default_value_parameter',
2551
+ 'label' => __( 'Set Default Value via Parameter', 'pods' ),
2552
+ 'help' => __( 'help', 'pods' ),
2553
+ 'type' => 'text',
2554
+ 'default' => '',
2555
+ ),
2556
+ ),
2557
+ __( 'Visibility', 'pods' ) => array(
2558
+ 'restrict_access' => array(
2559
+ 'name' => 'restrict_access',
2560
+ 'label' => __( 'Restrict Access', 'pods' ),
2561
+ 'group' => array(
2562
+ 'admin_only' => array(
2563
+ 'name' => 'admin_only',
2564
+ 'label' => __( 'Restrict access to Admins?', 'pods' ),
2565
+ 'default' => 0,
2566
+ 'type' => 'boolean',
2567
+ 'dependency' => true,
2568
+ 'help' => __( 'This field will only be able to be edited by users with the ability to manage_options or delete_users, or super admins of a WordPress Multisite network', 'pods' ),
2569
+ ),
2570
+ 'restrict_role' => array(
2571
+ 'name' => 'restrict_role',
2572
+ 'label' => __( 'Restrict access by Role?', 'pods' ),
2573
+ 'default' => 0,
2574
+ 'type' => 'boolean',
2575
+ 'dependency' => true,
2576
+ ),
2577
+ 'restrict_capability' => array(
2578
+ 'name' => 'restrict_capability',
2579
+ 'label' => __( 'Restrict access by Capability?', 'pods' ),
2580
+ 'default' => 0,
2581
+ 'type' => 'boolean',
2582
+ 'dependency' => true,
2583
+ ),
2584
+ 'hidden' => array(
2585
+ 'name' => 'hidden',
2586
+ 'label' => __( 'Hide field from UI', 'pods' ),
2587
+ 'default' => 0,
2588
+ 'type' => 'boolean',
2589
+ 'help' => __( 'This option is overriden by access restrictions. If the user does not have access to edit this field, it will be hidden. If no access restrictions are set, this field will always be hidden.', 'pods' ),
2590
+ ),
2591
+ 'read_only' => array(
2592
+ 'name' => 'read_only',
2593
+ 'label' => __( 'Make field "Read Only" in UI', 'pods' ),
2594
+ 'default' => 0,
2595
+ 'type' => 'boolean',
2596
+ 'help' => __( 'This option is overriden by access restrictions. If the user does not have access to edit this field, it will be read only. If no access restrictions are set, this field will always be read only.', 'pods' ),
2597
+ 'depends-on' => array(
2598
+ 'type' => array(
2599
+ 'boolean',
2600
+ 'color',
2601
+ 'currency',
2602
+ 'date',
2603
+ 'datetime',
2604
+ 'email',
2605
+ 'number',
2606
+ 'paragraph',
2607
+ 'password',
2608
+ 'phone',
2609
+ 'slug',
2610
+ 'text',
2611
+ 'time',
2612
+ 'website',
2613
+ ),
2614
+ ),
2615
+ ),
2616
+ ),
2617
+ ),
2618
+ 'roles_allowed' => array(
2619
+ 'name' => 'roles_allowed',
2620
+ 'label' => __( 'Role(s) Allowed', 'pods' ),
2621
+ 'help' => __( 'help', 'pods' ),
2622
+ 'type' => 'pick',
2623
+ 'pick_object' => 'role',
2624
+ 'pick_format_type' => 'multi',
2625
+ 'default' => 'administrator',
2626
+ 'depends-on' => array(
2627
+ 'restrict_role' => true,
2628
+ ),
2629
+ ),
2630
+ 'capability_allowed' => array(
2631
+ 'name' => 'capability_allowed',
2632
+ 'label' => __( 'Capability Allowed', 'pods' ),
2633
+ 'help' => __( 'Comma-separated list of cababilities, for example add_podname_item, please see the Roles and Capabilities component for the complete list and a way to add your own.', 'pods' ),
2634
+ 'type' => 'text',
2635
+ 'default' => '',
2636
+ 'depends-on' => array(
2637
+ 'restrict_capability' => true,
2638
+ ),
2639
+ ),
2640
+ ),
2641
+ );
2642
+
2643
+ /*
2644
+ $options['advanced'][ __( 'Visibility', 'pods' ) ]['search'] = array(
2645
+ 'label' => __( 'Include in searches', 'pods' ),
2646
+ 'help' => __( 'help', 'pods' ),
2647
+ 'default' => 1,
2648
+ 'type' => 'boolean',
2649
+ );
2650
+ */
2651
+
2652
+ /*
2653
+ $options['advanced'][ __( 'Validation', 'pods' ) ] = array(
2654
+ 'regex_validation' => array(
2655
+ 'label' => __( 'RegEx Validation', 'pods' ),
2656
+ 'help' => __( 'help', 'pods' ),
2657
+ 'type' => 'text',
2658
+ 'default' => '',
2659
+ ),
2660
+ 'message_regex' => array(
2661
+ 'label' => __( 'Message if field does not pass RegEx', 'pods' ),
2662
+ 'help' => __( 'help', 'pods' ),
2663
+ 'type' => 'text',
2664
+ 'default' => '',
2665
+ ),
2666
+ 'message_required' => array(
2667
+ 'label' => __( 'Message if field is blank', 'pods' ),
2668
+ 'help' => __( 'help', 'pods' ),
2669
+ 'type' => 'text',
2670
+ 'default' => '',
2671
+ 'depends-on' => array( 'required' => true ),
2672
+ ),
2673
+ 'message_unique' => array(
2674
+ 'label' => __( 'Message if field is not unique', 'pods' ),
2675
+ 'help' => __( 'help', 'pods' ),
2676
+ 'type' => 'text',
2677
+ 'default' => '',
2678
+ 'depends-on' => array( 'unique' => true ),
2679
+ ),
2680
+ );
2681
+ */
2682
+
2683
+ if ( ! class_exists( 'Pods_Helpers' ) ) {
2684
+ unset( $options['advanced']['input_helper'] );
2685
+ }
2686
+
2687
+ /**
2688
+ * Modify tabs and their contents for field options
2689
+ *
2690
+ * @since unknown
2691
+ *
2692
+ * @param array $options Tabs, indexed by label.
2693
+ * @param object|Pods $pod Pods object for the Pod this UI is for.
2694
+ */
2695
+ $options = apply_filters( 'pods_admin_setup_edit_field_options', $options, $pod );
2696
+
2697
+ return $options;
2698
+ }
2699
+
2700
+ /**
2701
+ * Duplicate a pod
2702
+ *
2703
+ * @param PodsUI $obj PodsUI object.
2704
+ */
2705
+ public function admin_setup_duplicate( $obj ) {
2706
+
2707
+ $new_id = pods_api()->duplicate_pod( array( 'id' => $obj->id ) );
2708
+
2709
+ if ( 0 < $new_id ) {
2710
+ pods_redirect(
2711
+ pods_query_arg(
2712
+ array(
2713
+ 'action' => 'edit',
2714
+ 'id' => $new_id,
2715
+ 'do' => 'duplicate',
2716
+ )
2717
+ )
2718
+ );
2719
+ }
2720
+ }
2721
+
2722
+ /**
2723
+ * Restrict Duplicate action to custom types, not extended
2724
+ *
2725
+ * @param bool $restricted Whether action is restricted.
2726
+ * @param array $restrict Restriction array.
2727
+ * @param string $action Current action.
2728
+ * @param array $row Item data row.
2729
+ * @param PodsUI $obj PodsUI object.
2730
+ *
2731
+ * @since 2.3.10
2732
+ *
2733
+ * @return bool
2734
+ */
2735
+ public function admin_setup_duplicate_restrict( $restricted, $restrict, $action, $row, $obj ) {
2736
+
2737
+ if ( in_array(
2738
+ $row['real_type'], array(
2739
+ 'user',
2740
+ 'media',
2741
+ 'comment',
2742
+ ), true
2743
+ ) ) {
2744
+ $restricted = true;
2745
+ }
2746
+
2747
+ return $restricted;
2748
+
2749
+ }
2750
+
2751
+ /**
2752
+ * Reset a pod
2753
+ *
2754
+ * @param PodsUI $obj PodsUI object.
2755
+ * @param int|string $id Item ID.
2756
+ *
2757
+ * @return mixed
2758
+ */
2759
+ public function admin_setup_reset( $obj, $id ) {
2760
+
2761
+ $pod = pods_api()->load_pod( array( 'id' => $id ), false );
2762
+
2763
+ if ( empty( $pod ) ) {
2764
+ return $obj->error( __( 'Pod not found.', 'pods' ) );
2765
+ }
2766
+
2767
+ pods_api()->reset_pod( array( 'id' => $id ) );
2768
+
2769
+ $obj->message( __( 'Pod reset successfully.', 'pods' ) );
2770
+
2771
+ $obj->manage();
2772
+ }
2773
+
2774
+ /**
2775
+ * Restrict Reset action from users and media
2776
+ *
2777
+ * @param bool $restricted Whether action is restricted.
2778
+ * @param array $restrict Restriction array.
2779
+ * @param string $action Current action.
2780
+ * @param array $row Item data row.
2781
+ * @param PodsUI $obj PodsUI object.
2782
+ *
2783
+ * @since 2.3.10
2784
+ */
2785
+ public function admin_setup_reset_restrict( $restricted, $restrict, $action, $row, $obj ) {
2786
+
2787
+ if ( in_array(
2788
+ $row['real_type'], array(
2789
+ 'user',
2790
+ 'media',
2791
+ ), true
2792
+ ) ) {
2793
+ $restricted = true;
2794
+ }
2795
+
2796
+ return $restricted;
2797
+
2798
+ }
2799
+
2800
+ /**
2801
+ * Delete a pod
2802
+ *
2803
+ * @param int|string $id Item ID.
2804
+ * @param PodsUI $obj PodsUI object.
2805
+ *
2806
+ * @return mixed
2807
+ */
2808
+ public function admin_setup_delete( $id, $obj ) {
2809
+
2810
+ $pod = pods_api()->load_pod( array( 'id' => $id ), false );
2811
+
2812
+ if ( empty( $pod ) ) {
2813
+ return $obj->error( __( 'Pod not found.', 'pods' ) );
2814
+ }
2815
+
2816
+ pods_api()->delete_pod( array( 'id' => $id ) );
2817
+
2818
+ unset( $obj->data[ $pod['id'] ] );
2819
+
2820
+ $obj->total = count( $obj->data );
2821
+ $obj->total_found = count( $obj->data );
2822
+
2823
+ $obj->message( __( 'Pod deleted successfully.', 'pods' ) );
2824
+ }
2825
+
2826
+ /**
2827
+ * Get advanced administration view.
2828
+ */
2829
+ public function admin_advanced() {
2830
+
2831
+ pods_view( PODS_DIR . 'ui/admin/advanced.php', compact( array_keys( get_defined_vars() ) ) );
2832
+ }
2833
+
2834
+ /**
2835
+ * Get settings administration view
2836
+ */
2837
+ public function admin_settings() {
2838
+
2839
+ // Add our custom callouts.
2840
+ add_action( 'pods_admin_after_settings', array( $this, 'admin_manage_callouts' ) );
2841
+
2842
+ pods_view( PODS_DIR . 'ui/admin/settings.php', compact( array_keys( get_defined_vars() ) ) );
2843
+ }
2844
+
2845
+ /**
2846
+ * Get components administration UI
2847
+ */
2848
+ public function admin_components() {
2849
+
2850
+ if ( ! is_object( PodsInit::$components ) ) {
2851
+ return;
2852
+ }
2853
+
2854
+ $components = PodsInit::$components->components;
2855
+
2856
+ $view = pods_v( 'view', 'get', 'all', true );
2857
+
2858
+ $recommended = array(
2859
+ 'advanced-relationships',
2860
+ 'advanced-content-types',
2861
+ 'migrate-packages',
2862
+ 'roles-and-capabilities',
2863
+ 'pages',
2864
+ 'table-storage',
2865
+ 'templates',
2866
+ );
2867
+
2868
+ foreach ( $components as $component => &$component_data ) {
2869
+ if ( ! in_array(
2870
+ $view, array(
2871
+ 'all',
2872
+ 'recommended',
2873
+ 'dev',
2874
+ ), true
2875
+ ) && ( ! isset( $component_data['Category'] ) || sanitize_title( $component_data['Category'] ) !== $view ) ) {
2876
+ unset( $components[ $component ] );
2877
+
2878
+ continue;
2879
+ } elseif ( 'recommended' === $view && ! in_array( $component_data['ID'], $recommended, true ) ) {
2880
+ unset( $components[ $component ] );
2881
+
2882
+ continue;
2883
+ } elseif ( 'dev' === $view && pods_developer() && ! pods_v( 'DeveloperMode', $component_data, false ) ) {
2884
+ unset( $components[ $component ] );
2885
+
2886
+ continue;
2887
+ } elseif ( pods_v( 'DeveloperMode', $component_data, false ) && ! pods_developer() ) {
2888
+ unset( $components[ $component ] );
2889
+
2890
+ continue;
2891
+ } elseif ( ! pods_v( 'TablelessMode', $component_data, false ) && pods_tableless() ) {
2892
+ unset( $components[ $component ] );
2893
+
2894
+ continue;
2895
+ }//end if
2896
+
2897
+ $component_data['Name'] = strip_tags( $component_data['Name'] );
2898
+
2899
+ if ( pods_v( 'DeveloperMode', $component_data, false ) ) {
2900
+ $component_data['Name'] .= ' <em style="font-weight: normal; color:#333;">(Developer Preview)</em>';
2901
+ }
2902
+
2903
+ $meta = array();
2904
+
2905
+ if ( ! empty( $component_data['Version'] ) ) {
2906
+ $meta[] = sprintf( __( 'Version %s', 'pods' ), $component_data['Version'] );
2907
+ }
2908
+
2909
+ if ( empty( $component_data['Author'] ) ) {
2910
+ $component_data['Author'] = 'Pods Framework Team';
2911
+ $component_data['AuthorURI'] = 'https://pods.io/';
2912
+ }
2913
+
2914
+ if ( ! empty( $component_data['AuthorURI'] ) ) {
2915
+ $component_data['Author'] = '<a href="' . $component_data['AuthorURI'] . '">' . $component_data['Author'] . '</a>';
2916
+ }
2917
+
2918
+ $meta[] = sprintf( __( 'by %s', 'pods' ), $component_data['Author'] );
2919
+
2920
+ if ( ! empty( $component_data['URI'] ) ) {
2921
+ $meta[] = '<a href="' . $component_data['URI'] . '">' . __( 'Visit component site', 'pods' ) . '</a>';
2922
+ }
2923
+
2924
+ $component_data['Description'] = wpautop( trim( make_clickable( strip_tags( $component_data['Description'], 'em,strong' ) ) ) );
2925
+
2926
+ if ( ! empty( $meta ) ) {
2927
+ $description_style = '';
2928
+
2929
+ if ( ! empty( $component_data['Description'] ) ) {
2930
+ $description_style = ' style="padding:8px 0 4px;"';
2931
+ }
2932
+
2933
+ $component_data['Description'] .= '<div class="pods-component-meta" ' . $description_style . '>' . implode( '&nbsp;&nbsp;|&nbsp;&nbsp;', $meta ) . '</div>';
2934
+ }
2935
 
2936
+ $component_data = array(
2937
+ 'id' => $component_data['ID'],
2938
+ 'name' => $component_data['Name'],
2939
+ 'category' => $component_data['Category'],
2940
+ 'version' => '',
2941
+ 'description' => $component_data['Description'],
2942
+ 'mustuse' => pods_v( 'MustUse', $component_data, false ),
2943
+ 'toggle' => 0,
2944
+ );
2945
 
2946
+ if ( ! empty( $component_data['category'] ) ) {
2947
+ $category_url = pods_query_arg(
2948
+ array(
2949
+ 'view' => sanitize_title( $component_data['category'] ),
2950
+ 'pg' => '',
2951
+ // @codingStandardsIgnoreLine
2952
+ 'page' => $_GET['page'],
2953
+ )
2954
+ );
2955
+
2956
+ $component_data['category'] = '<a href="' . esc_url( $category_url ) . '">' . $component_data['category'] . '</a>';
2957
+ }
2958
 
2959
+ if ( isset( PodsInit::$components->settings['components'][ $component_data['id'] ] ) && 0 !== PodsInit::$components->settings['components'][ $component_data['id'] ] ) {
2960
+ $component_data['toggle'] = 1;
2961
+ } elseif ( $component_data['mustuse'] ) {
2962
+ $component_data['toggle'] = 1;
2963
+ }
2964
+ }//end foreach
2965
+
2966
+ $ui = array(
2967
+ 'data' => $components,
2968
+ 'total' => count( $components ),
2969
+ 'total_found' => count( $components ),
2970
+ 'items' => __( 'Components', 'pods' ),
2971
+ 'item' => __( 'Component', 'pods' ),
2972
+ 'fields' => array(
2973
+ 'manage' => array(
2974
+ 'name' => array(
2975
+ 'label' => __( 'Name', 'pods' ),
2976
+ 'width' => '30%',
2977
+ 'type' => 'text',
2978
+ 'options' => array(
2979
+ 'text_allow_html' => true,
2980
+ ),
2981
+ ),
2982
+ 'category' => array(
2983
+ 'label' => __( 'Category', 'pods' ),
2984
+ 'width' => '10%',
2985
+ 'type' => 'text',
2986
+ 'options' => array(
2987
+ 'text_allow_html' => true,
2988
+ ),
2989
+ ),
2990
+ 'description' => array(
2991
+ 'label' => __( 'Description', 'pods' ),
2992
+ 'width' => '60%',
2993
+ 'type' => 'text',
2994
+ 'options' => array(
2995
+ 'text_allow_html' => true,
2996
+ 'text_allowed_html_tags' => 'strong em a ul ol li b i br div',
2997
+ ),
2998
+ ),
2999
+ ),
3000
+ ),
3001
+ 'actions_disabled' => array( 'duplicate', 'view', 'export', 'add', 'edit', 'delete' ),
3002
+ 'actions_custom' => array(
3003
+ 'toggle' => array(
3004
+ 'callback' => array( $this, 'admin_components_toggle' ),
3005
+ 'nonce' => true,
3006
+ ),
3007
+ ),
3008
+ 'filters_enhanced' => true,
3009
+ 'views' => array(
3010
+ 'all' => __( 'All', 'pods' ),
3011
+ // 'recommended' => __( 'Recommended', 'pods' ),
3012
+ 'field-types' => __( 'Field Types', 'pods' ),
3013
+ 'tools' => __( 'Tools', 'pods' ),
3014
+ 'integration' => __( 'Integration', 'pods' ),
3015
+ 'migration' => __( 'Migration', 'pods' ),
3016
+ 'advanced' => __( 'Advanced', 'pods' ),
3017
+ ),
3018
+ 'view' => $view,
3019
+ 'heading' => array(
3020
+ 'views' => __( 'Category', 'pods' ),
3021
+ ),
3022
+ 'search' => false,
3023
+ 'searchable' => false,
3024
+ 'sortable' => false,
3025
+ 'pagination' => false,
3026
+ );
3027
+
3028
+ if ( pods_developer() ) {
3029
+ $ui['views']['dev'] = __( 'Developer Preview', 'pods' );
3030
+ }
3031
 
3032
+ // Add our custom callouts.
3033
+ $this->handle_callouts_updates();
3034
 
3035
+ add_filter( 'pods_ui_manage_custom_container_classes', array( $this, 'admin_manage_container_class' ) );
3036
+ add_action( 'pods_ui_manage_after_container', array( $this, 'admin_manage_callouts' ) );
 
 
 
3037
 
3038
+ pods_ui( $ui );
3039
+ }
 
3040
 
3041
+ /**
3042
+ * Toggle a component on or off
3043
+ *
3044
+ * @param PodsUI $ui PodsUI object.
3045
+ *
3046
+ * @return bool
3047
+ */
3048
+ public function admin_components_toggle( $ui ) {
3049
 
3050
+ // @codingStandardsIgnoreLine
3051
+ $component = $_GET['id'];
3052
 
3053
+ if ( ! empty( PodsInit::$components->components[ $component ]['PluginDependency'] ) ) {
3054
+ $dependency = explode( '|', PodsInit::$components->components[ $component ]['PluginDependency'] );
3055
 
3056
+ if ( ! pods_is_plugin_active( $dependency[1] ) ) {
3057
+ $website = 'http://wordpress.org/extend/plugins/' . dirname( $dependency[1] ) . '/';
3058
 
3059
+ if ( isset( $dependency[2] ) ) {
3060
+ $website = $dependency[2];
3061
+ }
 
 
 
 
 
 
3062
 
3063
+ if ( ! empty( $website ) ) {
3064
+ $website = ' ' . sprintf( __( 'You can find it at %s', 'pods' ), '<a href="' . $website . '" target="_blank" rel="noopener noreferrer">' . $website . '</a>' );
3065
+ }
3066
 
3067
+ $message = sprintf( __( 'The %1$s component requires that you have the <strong>%2$s</strong> plugin installed and activated.', 'pods' ), PodsInit::$components->components[ $component ]['Name'], $dependency[0] ) . $website;
 
 
 
3068
 
3069
+ $ui->error( $message );
3070
 
3071
+ $ui->manage();
 
 
 
3072
 
3073
+ return;
3074
+ }
3075
+ }//end if
3076
 
3077
+ if ( ! empty( PodsInit::$components->components[ $component ]['ThemeDependency'] ) ) {
3078
+ $dependency = explode( '|', PodsInit::$components->components[ $component ]['ThemeDependency'] );
 
 
 
 
3079
 
3080
+ $check = strtolower( $dependency[1] );
 
 
3081
 
3082
+ if ( strtolower( get_template() ) !== $check && strtolower( get_stylesheet() ) !== $check ) {
3083
+ $website = '';
3084
 
3085
+ if ( isset( $dependency[2] ) ) {
3086
+ $website = ' ' . sprintf( __( 'You can find it at %s', 'pods' ), '<a href="' . $dependency[2] . '" target="_blank" rel="noopener noreferrer">' . $dependency[2] . '</a>' );
3087
+ }
3088
 
3089
+ $message = sprintf( __( 'The %1$s component requires that you have the <strong>%2$s</strong> theme installed and activated.', 'pods' ), PodsInit::$components->components[ $component ]['Name'], $dependency[0] ) . $website;
3090
 
3091
+ $ui->error( $message );
 
 
 
 
3092
 
3093
+ $ui->manage();
 
3094
 
3095
+ return;
3096
+ }
3097
+ }//end if
 
 
 
3098
 
3099
+ if ( ! empty( PodsInit::$components->components[ $component ]['MustUse'] ) ) {
3100
+ $message = sprintf( __( 'The %s component can not be disabled from here. You must deactivate the plugin or theme that added it.', 'pods' ), PodsInit::$components->components[ $component ]['Name'] );
3101
 
3102
+ $ui->error( $message );
 
3103
 
3104
+ $ui->manage();
 
 
3105
 
3106
+ return;
3107
+ }
3108
 
3109
+ if ( 1 === (int) pods_v( 'toggled' ) ) {
3110
+ $toggle = PodsInit::$components->toggle( $component );
3111
 
3112
+ if ( true === $toggle ) {
3113
+ $ui->message( PodsInit::$components->components[ $component ]['Name'] . ' ' . __( 'Component enabled', 'pods' ) );
3114
+ } elseif ( false === $toggle ) {
3115
+ $ui->message( PodsInit::$components->components[ $component ]['Name'] . ' ' . __( 'Component disabled', 'pods' ) );
3116
+ }
3117
 
3118
+ $components = PodsInit::$components->components;
 
3119
 
3120
+ foreach ( $components as $component => &$component_data ) {
3121
+ $toggle = 0;
 
 
 
 
3122
 
3123
+ if ( isset( PodsInit::$components->settings['components'][ $component_data['ID'] ] ) ) {
3124
+ if ( 0 !== PodsInit::$components->settings['components'][ $component_data['ID'] ] ) {
3125
+ $toggle = 1;
3126
+ }
3127
+ }
3128
+ if ( true === $component_data['DeveloperMode'] ) {
3129
+ if ( ! pods_developer() ) {
3130
+ unset( $components[ $component ] );
3131
+ continue;
3132
+ }
3133
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3134
 
3135
+ $component_data = array(
3136
+ 'id' => $component_data['ID'],
3137
+ 'name' => $component_data['Name'],
3138
+ 'description' => make_clickable( $component_data['Description'] ),
3139
+ 'version' => $component_data['Version'],
3140
+ 'author' => $component_data['Author'],
3141
+ 'toggle' => $toggle,
3142
+ );
3143
+ }//end foreach
3144
 
3145
+ $ui->data = $components;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3146
 
3147
+ pods_transient_clear( 'pods_components' );
 
 
 
 
 
 
 
 
 
 
3148
 
3149
+ $url = pods_query_arg( array( 'toggled' => null ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3150
 
3151
+ pods_redirect( $url );
3152
+ } elseif ( 1 === (int) pods_v( 'toggle' ) ) {
3153
+ $ui->message( PodsInit::$components->components[ $component ]['Name'] . ' ' . __( 'Component enabled', 'pods' ) );
3154
+ } else {
3155
+ $ui->message( PodsInit::$components->components[ $component ]['Name'] . ' ' . __( 'Component disabled', 'pods' ) );
3156
+ }//end if
3157
 
3158
+ $ui->manage();
3159
+ }
 
 
 
 
 
 
 
 
3160
 
3161
+ /**
3162
+ * Get the admin upgrade page
3163
+ */
3164
+ public function admin_upgrade() {
3165
 
3166
+ foreach ( PodsInit::$upgrades as $old_version => $new_version ) {
3167
+ if ( version_compare( $old_version, PodsInit::$version_last, '<=' ) && version_compare( PodsInit::$version_last, $new_version, '<' ) ) {
3168
+ $new_version = str_replace( '.', '_', $new_version );
 
3169
 
3170
+ pods_view( PODS_DIR . 'ui/admin/upgrade/upgrade_' . $new_version . '.php', compact( array_keys( get_defined_vars() ) ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3171
 
3172
+ break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3173
  }
3174
+ }
3175
+ }
3176
 
3177
+ /**
3178
+ * Get the admin help page
3179
+ */
3180
+ public function admin_help() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3181
 
3182
+ // Add our custom callouts.
3183
+ $this->handle_callouts_updates();
 
 
3184
 
3185
+ add_action( 'pods_admin_after_help', array( $this, 'admin_manage_callouts' ) );
 
 
 
3186
 
3187
+ pods_view( PODS_DIR . 'ui/admin/help.php', compact( array_keys( get_defined_vars() ) ) );
3188
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3189
 
3190
  /**
3191
+ * Add pods specific capabilities.
3192
  *
3193
+ * @param array $capabilities List of extra capabilities to add.
 
 
 
 
3194
  *
3195
+ * @return array
3196
  */
3197
+ public function admin_capabilities( $capabilities ) {
3198
+
3199
+ $pods = pods_api()->load_pods(
3200
+ array(
3201
+ 'type' => array(
3202
+ 'settings',
3203
+ 'post_type',
3204
+ 'taxonomy',
3205
+ ),
3206
+ 'fields' => false,
3207
+ 'table_info' => false,
3208
+ )
3209
+ );
3210
+
3211
+ $other_pods = pods_api()->load_pods(
3212
+ array(
3213
+ 'type' => array(
3214
+ 'pod',
3215
+ 'table',
3216
+ ),
3217
+ 'table_info' => false,
3218
+ )
3219
+ );
3220
+
3221
+ $pods = array_merge( $pods, $other_pods );
3222
+
3223
+ $capabilities[] = 'pods';
3224
+ $capabilities[] = 'pods_content';
3225
+ $capabilities[] = 'pods_settings';
3226
+ $capabilities[] = 'pods_components';
3227
+
3228
+ foreach ( $pods as $pod ) {
3229
+ if ( 'settings' === $pod['type'] ) {
3230
+ $capabilities[] = 'pods_edit_' . $pod['name'];
3231
+ } elseif ( 'post_type' === $pod['type'] ) {
3232
+ $capability_type = pods_v_sanitized( 'capability_type_custom', $pod['options'], pods_v( 'name', $pod ) );
3233
+
3234
+ if ( 'custom' === pods_v( 'capability_type', $pod['options'] ) && 0 < strlen( $capability_type ) ) {
3235
+ $capabilities[] = 'read_' . $capability_type;
3236
+ $capabilities[] = 'edit_' . $capability_type;
3237
+ $capabilities[] = 'delete_' . $capability_type;
3238
+
3239
+ if ( 1 === (int) pods_v( 'capability_type_extra', $pod['options'], 1 ) ) {
3240
+ $capability_type_plural = $capability_type . 's';
3241
+
3242
+ $capabilities[] = 'read_private_' . $capability_type_plural;
3243
+ $capabilities[] = 'edit_' . $capability_type_plural;
3244
+ $capabilities[] = 'edit_others_' . $capability_type_plural;
3245
+ $capabilities[] = 'edit_private_' . $capability_type_plural;
3246
+ $capabilities[] = 'edit_published_' . $capability_type_plural;
3247
+ $capabilities[] = 'publish_' . $capability_type_plural;
3248
+ $capabilities[] = 'delete_' . $capability_type_plural;
3249
+ $capabilities[] = 'delete_private_' . $capability_type_plural;
3250
+ $capabilities[] = 'delete_published_' . $capability_type_plural;
3251
+ $capabilities[] = 'delete_others_' . $capability_type_plural;
3252
+ }
3253
+ }
3254
+ } elseif ( 'taxonomy' === $pod['type'] ) {
3255
+ if ( 'custom' === pods_v( 'capability_type', $pod['options'], 'terms' ) ) {
3256
+ $capability_type = pods_v_sanitized( 'capability_type_custom', $pod['options'], pods_v( 'name', $pod ) . 's' );
3257
+
3258
+ $capability_type .= '_term';
3259
+ $capability_type_plural = $capability_type . 's';
3260
+
3261
+ // Singular
3262
+ $capabilities[] = 'edit_' . $capability_type;
3263
+ $capabilities[] = 'delete_' . $capability_type;
3264
+ $capabilities[] = 'assign_' . $capability_type;
3265
+ // Plural
3266
+ $capabilities[] = 'manage_' . $capability_type_plural;
3267
+ $capabilities[] = 'edit_' . $capability_type_plural;
3268
+ $capabilities[] = 'delete_' . $capability_type_plural;
3269
+ $capabilities[] = 'assign_' . $capability_type_plural;
3270
+ }
3271
+ } else {
3272
+ $capabilities[] = 'pods_add_' . $pod['name'];
3273
+ $capabilities[] = 'pods_edit_' . $pod['name'];
3274
 
3275
+ if ( isset( $pod['fields']['author'] ) && 'pick' === $pod['fields']['author']['type'] && 'user' === $pod['fields']['author']['pick_object'] ) {
3276
+ $capabilities[] = 'pods_edit_others_' . $pod['name'];
3277
+ }
3278
 
3279
+ $capabilities[] = 'pods_delete_' . $pod['name'];
 
 
 
 
 
 
 
 
3280
 
3281
+ if ( isset( $pod['fields']['author'] ) && 'pick' === $pod['fields']['author']['type'] && 'user' === $pod['fields']['author']['pick_object'] ) {
3282
+ $capabilities[] = 'pods_delete_others_' . $pod['name'];
3283
+ }
3284
 
3285
+ $actions_enabled = pods_v( 'ui_actions_enabled', $pod['options'] );
3286
 
3287
+ if ( ! empty( $actions_enabled ) ) {
3288
+ $actions_enabled = (array) $actions_enabled;
3289
+ } else {
3290
+ $actions_enabled = array();
3291
+ }
3292
 
3293
+ $available_actions = array(
3294
+ 'add',
3295
+ 'edit',
3296
+ 'duplicate',
3297
+ 'delete',
3298
+ 'reorder',
3299
+ 'export',
3300
+ );
3301
+
3302
+ if ( ! empty( $actions_enabled ) ) {
3303
+ $actions_disabled = array(
3304
+ 'view' => 'view',
3305
+ );
3306
 
3307
+ foreach ( $available_actions as $action ) {
3308
+ if ( ! in_array( $action, $actions_enabled, true ) ) {
3309
+ $actions_disabled[ $action ] = $action;
3310
+ }
3311
+ }
 
 
 
 
 
 
 
3312
 
3313
+ if ( ! in_array( 'export', $actions_disabled, true ) ) {
3314
+ $capabilities[] = 'pods_export_' . $pod['name'];
3315
+ }
3316
 
3317
+ if ( ! in_array( 'reorder', $actions_disabled, true ) ) {
3318
+ $capabilities[] = 'pods_reorder_' . $pod['name'];
3319
+ }
3320
+ } elseif ( 1 === (int) pods_v( 'ui_export', $pod['options'], 0 ) ) {
3321
+ $capabilities[] = 'pods_export_' . $pod['name'];
3322
+ }//end if
3323
+ }//end if
3324
+ }//end foreach
3325
 
3326
+ return $capabilities;
3327
  }
3328
 
3329
+ /**
3330
+ * Handle ajax calls for the administration
3331
+ */
3332
+ public function admin_ajax() {
 
 
 
 
 
 
 
 
 
3333
 
3334
+ if ( false === headers_sent() ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3335
  pods_session_start();
3336
 
3337
+ header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ) );
3338
+ }
3339
 
3340
+ // Sanitize input
3341
+ // @codingStandardsIgnoreLine
3342
+ $params = pods_unslash( (array) $_POST );
3343
 
3344
  foreach ( $params as $key => $value ) {
3345
+ if ( 'action' === $key ) {
3346
  continue;
3347
+ }
3348
 
3349
+ // Fixup $_POST data @codingStandardsIgnoreLine
3350
  $_POST[ str_replace( '_podsfix_', '', $key ) ] = $_POST[ $key ];
3351
 
3352
  // Fixup $params with unslashed data
3356
  unset( $params[ $key ] );
3357
  }
3358
 
3359
+ $params = (object) $params;
3360
 
3361
+ $methods = array(
3362
+ 'add_pod' => array( 'priv' => true ),
3363
+ 'save_pod' => array( 'priv' => true ),
3364
+ 'load_sister_fields' => array( 'priv' => true ),
3365
+ 'process_form' => array( 'custom_nonce' => true ),
3366
+ // priv handled through nonce
3367
+ 'upgrade' => array( 'priv' => true ),
3368
+ 'migrate' => array( 'priv' => true ),
3369
+ );
3370
 
3371
+ /**
3372
+ * AJAX Callbacks in field editor
3373
+ *
3374
+ * @since unknown
3375
+ *
3376
+ * @param array $methods Callback methods.
3377
+ * @param PodsAdmin $obj PodsAdmin object.
3378
+ */
3379
+ $methods = apply_filters( 'pods_admin_ajax_methods', $methods, $this );
 
 
 
3380
 
3381
+ if ( ! isset( $params->method ) || ! isset( $methods[ $params->method ] ) ) {
3382
+ pods_error( __( 'Invalid AJAX request', 'pods' ), $this );
3383
+ }
3384
 
3385
+ $defaults = array(
3386
+ 'priv' => null,
3387
+ 'name' => $params->method,
3388
+ 'custom_nonce' => null,
3389
+ );
3390
 
3391
+ $method = (object) array_merge( $defaults, (array) $methods[ $params->method ] );
 
3392
 
3393
+ if ( true !== $method->custom_nonce && ( ! isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, 'pods-' . $params->method ) ) ) {
3394
+ pods_error( __( 'Unauthorized request', 'pods' ), $this );
3395
+ }
3396
 
3397
+ // Cleaning up $params
3398
+ unset( $params->action );
3399
+ unset( $params->method );
3400
 
3401
+ if ( true !== $method->custom_nonce ) {
3402
+ unset( $params->_wpnonce );
3403
+ }
3404
 
3405
+ // Check permissions (convert to array to support multiple)
3406
+ if ( ! empty( $method->priv ) && ! pods_is_admin( array( 'pods' ) ) && true !== $method->priv && ! pods_is_admin( $method->priv ) ) {
3407
+ pods_error( __( 'Access denied', 'pods' ), $this );
3408
+ }
3409
 
3410
+ $params->method = $method->name;
 
 
 
 
 
 
 
 
 
3411
 
3412
+ $method_name = $method->name;
3413
 
3414
+ $params = apply_filters( "pods_api_{$method_name}", $params, $method );
 
 
 
 
 
 
 
3415
 
3416
+ $api = pods_api();
 
3417
 
3418
+ $api->display_errors = false;
3419
+
3420
+ if ( 'upgrade' === $method->name ) {
3421
+ $output = (string) pods_upgrade( $params->version )->ajax( $params );
3422
+ } elseif ( 'migrate' === $method->name ) {
3423
+ $output = (string) apply_filters( 'pods_api_migrate_run', $params );
3424
+ } else {
3425
+ if ( ! method_exists( $api, $method->name ) ) {
3426
+ pods_error( __( 'API method does not exist', 'pods' ), $this );
3427
+ } elseif ( 'save_pod' === $method->name ) {
3428
+ if ( isset( $params->field_data_json ) && is_array( $params->field_data_json ) ) {
3429
+ $params->fields = $params->field_data_json;
3430
+
3431
+ unset( $params->field_data_json );
3432
+
3433
+ foreach ( $params->fields as $k => $v ) {
3434
+ if ( empty( $v ) ) {
3435
+ unset( $params->fields[ $k ] );
3436
+ } elseif ( ! is_array( $v ) ) {
3437
+ $params->fields[ $k ] = (array) @json_decode( $v, true );
3438
+ }
3439
+ }
3440
+ }
3441
+ }
3442
 
3443
+ // Dynamically call the API method
3444
+ $params = (array) $params;
3445
+
3446
+ $output = call_user_func( array( $api, $method->name ), $params );
3447
+ }//end if
3448
+
3449
+ // Output in json format
3450
+ if ( false !== $output ) {
3451
+
3452
+ /**
3453
+ * Pods Admin AJAX request was successful
3454
+ *
3455
+ * @since 2.6.8
3456
+ *
3457
+ * @param array $params AJAX parameters.
3458
+ * @param array|object|string $output Output for AJAX request.
3459
+ */
3460
+ do_action( "pods_admin_ajax_success_{$method->name}", $params, $output );
3461
+
3462
+ if ( is_array( $output ) || is_object( $output ) ) {
3463
+ wp_send_json( $output );
3464
+ } else {
3465
+ // @codingStandardsIgnoreLine
3466
+ echo $output;
3467
+ }
3468
+ } else {
3469
+ pods_error( __( 'There was a problem with your request.', 'pods' ) );
3470
+ }//end if
3471
 
3472
+ die();
3473
+ // KBAI!
3474
+ }
3475
 
3476
  /**
3477
  * Profiles the Pods configuration
3478
  *
3479
+ * @param null|string|array $pod Which Pod(s) to get configuration for. Can be a the name
3480
+ * of one Pod, or an array of names of Pods, or null, which is the
3481
+ * default, to profile all Pods.
3482
+ * @param bool $full_field_info If true all info about each field is returned. If false,
3483
+ * which is the default only name and type, will be returned.
3484
  *
3485
  * @return array
3486
  *
3487
+ * @since 2.7.0
3488
  */
3489
+ public function configuration( $pod = null, $full_field_info = false ) {
3490
+
3491
  $api = pods_api();
3492
 
3493
+ if ( null === $pod ) {
3494
  $the_pods = $api->load_pods();
3495
+ } elseif ( is_array( $pod ) ) {
 
3496
  foreach ( $pod as $p ) {
3497
  $the_pods[] = $api->load_pod( $p );
3498
  }
3499
+ } else {
 
3500
  $the_pods[] = $api->load_pod( $pod );
3501
  }
3502
 
3503
+ foreach ( $the_pods as $the_pod ) {
3504
+ $configuration[ $the_pod['name'] ] = array(
3505
+ 'name' => $the_pod['name'],
3506
+ 'ID' => $the_pod['id'],
3507
+ 'storage' => $the_pod['storage'],
3508
+ 'fields' => $the_pod['fields'],
3509
  );
3510
  }
3511
 
3512
  if ( ! $full_field_info ) {
3513
+ foreach ( $the_pods as $the_pod ) {
3514
+ $fields = $configuration[ $the_pod['name'] ]['fields'];
3515
+
3516
+ unset( $configuration[ $the_pod['name'] ]['fields'] );
3517
+
3518
  foreach ( $fields as $field ) {
3519
+ $info = array(
3520
+ 'name' => $field['name'],
3521
+ 'type' => $field['type'],
3522
  );
3523
 
3524
+ if ( 'pick' === $info['type'] ) {
3525
+ $info['pick_object'] = $field['pick_object'];
3526
+
3527
+ if ( isset( $field['pick_val'] ) && '' !== $field['pick_val'] ) {
3528
+ $info['pick_val'] = $field['pick_val'];
3529
  }
3530
  }
3531
 
3532
  if ( is_array( $info ) ) {
3533
+ $configuration[ $the_pod['name'] ]['fields'][ $field['name'] ] = $info;
3534
  }
3535
 
3536
  unset( $info );
3537
 
3538
+ }//end foreach
3539
+ }//end foreach
3540
+ }//end if
3541
 
3542
+ if ( is_array( $configuration ) ) {
3543
+ return $configuration;
3544
  }
3545
 
3546
+ }
 
3547
 
3548
+ /**
3549
+ * Build UI for extending REST API, if makes sense to do so.
3550
+ *
3551
+ * @since 2.6.0
3552
+ *
3553
+ * @access protected
3554
+ */
3555
+ protected function rest_admin() {
3556
+
3557
+ if ( function_exists( 'register_rest_field' ) ) {
3558
+ add_filter(
3559
+ 'pods_admin_setup_edit_field_options', array(
3560
+ $this,
3561
+ 'add_rest_fields_to_field_editor',
3562
+ ), 12, 2
3563
+ );
3564
+ add_filter( 'pods_admin_setup_edit_field_tabs', array( $this, 'add_rest_field_tab' ), 12 );
3565
  }
3566
 
3567
+ add_filter( 'pods_admin_setup_edit_tabs', array( $this, 'add_rest_settings_tab' ), 12, 2 );
3568
+ add_filter( 'pods_admin_setup_edit_options', array( $this, 'add_rest_settings_tab_fields' ), 12, 2 );
3569
+
3570
+ }
3571
+
3572
+ /**
3573
+ * Check if Pod type <em>could</em> extend core REST API response
3574
+ *
3575
+ * @since 2.5.6
3576
+ *
3577
+ * @access protected
3578
+ *
3579
+ * @param array $pod Pod options.
3580
+ *
3581
+ * @return bool
3582
+ */
3583
+ protected function restable_pod( $pod ) {
3584
+
3585
+ $type = $pod['type'];
3586
+
3587
+ $restable_types = array(
3588
+ 'post_type',
3589
+ 'user',
3590
+ 'taxonomy',
3591
+ 'media',
3592
+ );
3593
+
3594
+ return in_array( $type, $restable_types, true );
3595
+
3596
+ }
3597
+
3598
+ /**
3599
+ * Add a rest api tab.
3600
+ *
3601
+ * @since 2.6.0
3602
+ *
3603
+ * @param array $tabs Tab array.
3604
+ * @param array $pod Pod options.
3605
+ *
3606
+ * @return array
3607
+ */
3608
+ public function add_rest_settings_tab( $tabs, $pod ) {
3609
+
3610
+ $tabs['rest-api'] = __( 'REST API', 'pods' );
3611
+
3612
+ return $tabs;
3613
+
3614
+ }
3615
+
3616
+ /**
3617
+ * Populate REST API tab.
3618
+ *
3619
+ * @since 0.1.0
3620
+ *
3621
+ * @param array $options Tab options.
3622
+ * @param array $pod Pod options.
3623
+ *
3624
+ * @return array
3625
+ */
3626
+ public function add_rest_settings_tab_fields( $options, $pod ) {
3627
+
3628
+ if ( ! function_exists( 'register_rest_field' ) ) {
3629
+ $options['rest-api'] = array(
3630
+ 'no_dependencies' => array(
3631
+ 'label' => sprintf( __( 'Pods REST API support requires WordPress 4.3.1 or later and the %s or later.', 'pods' ), '<a href="https://pods.io/docs/build/extending-core-wordpress-rest-api-routes-with-pods/" target="_blank" rel="noopener noreferrer">WordPress REST API 2.0-beta9</a>' ),
3632
+ 'help' => sprintf( __( 'See %s for more information.', 'pods' ), '<a href="https://pods.io/docs/build/extending-core-wordpress-rest-api-routes-with-pods/" target="_blank" rel="noopener noreferrer">https://pods.io/docs/build/extending-core-wordpress-rest-api-routes-with-pods/</a>' ),
3633
+ 'type' => 'html',
3634
+ ),
3635
+ );
3636
+ } elseif ( $this->restable_pod( $pod ) ) {
3637
+ $options['rest-api'] = array(
3638
+ 'rest_enable' => array(
3639
+ 'label' => __( 'Enable', 'pods' ),
3640
+ 'help' => __( 'Add REST API support for this Pod.', 'pods' ),
3641
+ 'type' => 'boolean',
3642
+ 'default' => '',
3643
+ 'dependency' => true,
3644
+ ),
3645
+ 'rest_base' => array(
3646
+ 'label' => __( 'REST Base (if any)', 'pods' ),
3647
+ 'help' => __( 'This will form the url for the route. Default / empty value here will use the pod name.', 'pods' ),
3648
+ 'type' => 'text',
3649
+ 'default' => '',
3650
+ 'depends-on' => array( 'rest_enable' => true ),
3651
+ ),
3652
+ 'read_all' => array(
3653
+ 'label' => __( 'Show All Fields (read-only)', 'pods' ),
3654
+ 'help' => __( 'Show all fields in REST API. If unchecked fields must be enabled on a field by field basis.', 'pods' ),
3655
+ 'type' => 'boolean',
3656
+ 'default' => '',
3657
+ 'depends-on' => array( 'rest_enable' => true ),
3658
+ ),
3659
+ 'write_all' => array(
3660
+ 'label' => __( 'Allow All Fields To Be Updated', 'pods' ),
3661
+ 'help' => __( 'Allow all fields to be updated via the REST API. If unchecked fields must be enabled on a field by field basis.', 'pods' ),
3662
+ 'type' => 'boolean',
3663
+ 'default' => pods_v( 'name', $pod ),
3664
+ 'boolean_yes_label' => '',
3665
+ 'depends-on' => array( 'rest_enable' => true ),
3666
+ ),
3667
+
3668
+ );
3669
+
3670
+ } else {
3671
+ $options['rest-api'] = array(
3672
+ 'not_restable' => array(
3673
+ 'label' => __( 'Pods REST API support covers post type, taxonomy and user Pods.', 'pods' ),
3674
+ 'help' => sprintf( __( 'See %s for more information.', 'pods' ), '<a href="https://pods.io/docs/build/extending-core-wordpress-rest-api-routes-with-pods/" target="_blank" rel="noopener noreferrer">https://pods.io/docs/build/extending-core-wordpress-rest-api-routes-with-pods/"</a>' ),
3675
+ 'type' => 'html',
3676
+ ),
3677
+ );
3678
+
3679
+ }//end if
3680
+
3681
+ return $options;
3682
+
3683
+ }
3684
+
3685
+ /**
3686
+ * Add a REST API section to advanced tab of field editor.
3687
+ *
3688
+ * @since 2.5.6
3689
+ *
3690
+ * @param array $options Tab options.
3691
+ * @param array $pod Pod options.
3692
+ *
3693
+ * @return array
3694
+ */
3695
+ public function add_rest_fields_to_field_editor( $options, $pod ) {
3696
+
3697
+ if ( $this->restable_pod( $pod ) ) {
3698
+ $options['rest'][ __( 'Read/ Write', 'pods' ) ] = array(
3699
+ 'rest_read' => array(
3700
+ 'label' => __( 'Read via REST API?', 'pods' ),
3701
+ 'help' => __( 'Should this field be readable via the REST API? You must enable REST API support for this Pod.', 'pods' ),
3702
+ 'type' => 'boolean',
3703
+ 'default' => '',
3704
+ ),
3705
+ 'rest_write' => array(
3706
+ 'label' => __( 'Write via REST API?', 'pods' ),
3707
+ 'help' => __( 'Should this field be writeable via the REST API? You must enable REST API support for this Pod.', 'pods' ),
3708
+ 'type' => 'boolean',
3709
+ 'default' => '',
3710
+ ),
3711
+ );
3712
+ $options['rest'][ __( 'Relationship Field Options', 'pods' ) ] = array(
3713
+ 'rest_pick_response' => array(
3714
+ 'label' => __( 'Response Type', 'pods' ),
3715
+ 'help' => __( 'This will determine what amount of data for the related items will be returned.', 'pods' ),
3716
+ 'type' => 'pick',
3717
+ 'default' => 'array',
3718
+ 'depends-on' => array( 'type' => 'pick' ),
3719
+ 'dependency' => true,
3720
+ 'data' => array(
3721
+ 'array' => __( 'Full', 'pods' ),
3722
+ 'id' => __( 'ID only', 'pods' ),
3723
+ 'name' => __( 'Name', 'pods' ),
3724
+ ),
3725
+ ),
3726
+ 'rest_pick_depth' => array(
3727
+ 'label' => __( 'Depth', 'pods' ),
3728
+ 'help' => __( 'How far to traverse relationships in response', 'pods' ),
3729
+ 'type' => 'number',
3730
+ 'default' => '2',
3731
+ 'depends-on' => array(
3732
+ 'type' => 'pick',
3733
+ 'rest_pick_response' => 'array',
3734
+ ),
3735
+ ),
3736
+
3737
+ );
3738
+
3739
+ }//end if
3740
+
3741
+ return $options;
3742
+
3743
+ }
3744
+
3745
+ /**
3746
+ * Add REST field tab
3747
+ *
3748
+ * @since 2.5.6
3749
+ *
3750
+ * @param array $tabs Tab list.
3751
+ *
3752
+ * @return array
3753
+ */
3754
+ public function add_rest_field_tab( $tabs ) {
3755
+
3756
+ $tabs['rest'] = __( 'REST API', 'pods' );
3757
+
3758
+ return $tabs;
3759
+ }
3760
+
3761
+ /**
3762
+ * Add Pods-specific debug info to Site Info debug area.
3763
+ *
3764
+ * @since 2.7.13
3765
+ *
3766
+ * @param array $info Debug info.
3767
+ *
3768
+ * @return array Debug info with Pods-specific debug info added.
3769
+ */
3770
+ public function add_debug_information( $info ) {
3771
+ $info['pods'] = array(
3772
+ 'label' => 'Pods',
3773
+ 'description' => __( 'Debug information for Pods installations.', 'pods' ),
3774
+ 'fields' => array(
3775
+ 'pods-server-software' => array(
3776
+ 'label' => __( 'Server Software', 'pods' ),
3777
+ 'value' => ! empty( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : 'N/A',
3778
+ ),
3779
+ 'pods-user-agent' => array(
3780
+ 'label' => __( 'Your User Agent', 'pods' ),
3781
+ 'value' => ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : 'N/A',
3782
+ ),
3783
+ 'pods-session-save-path' => array(
3784
+ 'label' => __( 'Session Save Path', 'pods' ),
3785
+ 'value' => session_save_path(),
3786
+ ),
3787
+ 'pods-session-save-path-exists' => array(
3788
+ 'label' => __( 'Session Save Path Exists', 'pods' ),
3789
+ 'value' => file_exists( session_save_path() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3790
+ ),
3791
+ 'pods-session-save-path-writable' => array(
3792
+ 'label' => __( 'Session Save Path Writeable', 'pods' ),
3793
+ 'value' => is_writable( session_save_path() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3794
+ ),
3795
+ 'pods-session-max-lifetime' => array(
3796
+ 'label' => __( 'Session Max Lifetime', 'pods' ),
3797
+ 'value' => ini_get( 'session.gc_maxlifetime' ),
3798
+ ),
3799
+ 'pods-opcode-cache-apc' => array(
3800
+ 'label' => __( 'Opcode Cache: Apc', 'pods' ),
3801
+ 'value' => function_exists( 'apc_cache_info' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3802
+ ),
3803
+ 'pods-opcode-cache-memcached' => array(
3804
+ 'label' => __( 'Opcode Cache: Memcached', 'pods' ),
3805
+ 'value' => class_exists( 'eaccelerator_put' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3806
+ ),
3807
+ 'pods-opcode-cache-opcache' => array(
3808
+ 'label' => __( 'Opcode Cache: OPcache', 'pods' ),
3809
+ 'value' => function_exists( 'opcache_get_status' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3810
+ ),
3811
+ 'pods-opcode-cache-redis' => array(
3812
+ 'label' => __( 'Opcode Cache: Redis', 'pods' ),
3813
+ 'value' => class_exists( 'xcache_set' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3814
+ ),
3815
+ 'pods-object-cache-apc' => array(
3816
+ 'label' => __( 'Object Cache: APC', 'pods' ),
3817
+ 'value' => function_exists( 'apc_cache_info' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3818
+ ),
3819
+ 'pods-object-cache-apcu' => array(
3820
+ 'label' => __( 'Object Cache: APCu', 'pods' ),
3821
+ 'value' => function_exists( 'apcu_cache_info' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3822
+ ),
3823
+ 'pods-object-cache-memcache' => array(
3824
+ 'label' => __( 'Object Cache: Memcache', 'pods' ),
3825
+ 'value' => class_exists( 'Memcache' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3826
+ ),
3827
+ 'pods-object-cache-memcached' => array(
3828
+ 'label' => __( 'Object Cache: Memcached', 'pods' ),
3829
+ 'value' => class_exists( 'Memcached' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3830
+ ),
3831
+ 'pods-object-cache-redis' => array(
3832
+ 'label' => __( 'Object Cache: Redis', 'pods' ),
3833
+ 'value' => class_exists( 'Redis' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3834
+ ),
3835
+ 'pods-memory-current-usage' => array(
3836
+ 'label' => __( 'Current Memory Usage', 'pods' ),
3837
+ 'value' => number_format_i18n( memory_get_usage() / 1024 / 1024, 3 ) . 'M',
3838
+ ),
3839
+ 'pods-memory-current-usage-real' => array(
3840
+ 'label' => __( 'Current Memory Usage (real)', 'pods' ),
3841
+ 'value' => number_format_i18n( memory_get_usage( true ) / 1024 / 1024, 3 ) . 'M',
3842
+ ),
3843
+ 'pods-network-wide' => array(
3844
+ 'label' => __( 'Pods Network-Wide Activated', 'pods' ),
3845
+ 'value' => is_plugin_active_for_network( basename( PODS_DIR ) . '/init.php' ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3846
+ ),
3847
+ 'pods-install-location' => array(
3848
+ 'label' => __( 'Pods Install Location', 'pods' ),
3849
+ 'value' => PODS_DIR,
3850
+ ),
3851
+ 'pods-developer' => array(
3852
+ 'label' => __( 'Pods Developer Activated' ),
3853
+ 'value' => ( pods_developer() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3854
+ ),
3855
+ 'pods-tableless-mode' => array(
3856
+ 'label' => __( 'Pods Tableless Mode Activated', 'pods' ),
3857
+ 'value' => ( pods_tableless() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3858
+ ),
3859
+ 'pods-light-mode' => array(
3860
+ 'label' => __( 'Pods Light Mode Activated', 'pods' ),
3861
+ 'value' => ( pods_light() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3862
+ ),
3863
+ 'pods-strict' => array(
3864
+ 'label' => __( 'Pods Strict Activated' ),
3865
+ 'value' => ( pods_strict() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3866
+ ),
3867
+ 'pods-allow-deprecated' => array(
3868
+ 'label' => __( 'Pods Allow Deprecated' ),
3869
+ 'value' => ( pods_allow_deprecated() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3870
+ ),
3871
+ 'pods-api-cache' => array(
3872
+ 'label' => __( 'Pods API Cache Activated' ),
3873
+ 'value' => ( pods_api_cache() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3874
+ ),
3875
+ 'pods-shortcode-allow-evaluate-tags' => array(
3876
+ 'label' => __( 'Pods Shortcode Allow Evaluate Tags' ),
3877
+ 'value' => ( pods_shortcode_allow_evaluate_tags() ) ? __( 'Yes', 'pods' ) : __( 'No', 'pods' ),
3878
+ ),
3879
+ ),
3880
+ );
3881
+
3882
+ return $info;
3883
  }
3884
 
3885
  }
classes/PodsArray.php CHANGED
@@ -1,213 +1,254 @@
1
  <?php
 
2
  /**
3
  * @package Pods
4
  */
5
  class PodsArray implements ArrayAccess {
6
 
7
- /**
8
- * @var array|mixed
9
- */
10
- private $__container = array();
11
-
12
- /**
13
- * Alternative to get_object_vars to access an object as an array with simple functionality and accepts arrays to
14
- * add additional functionality. Additional functionality includes validation and setting default data.
15
- *
16
- * @param mixed $container Object (or existing Array)
17
- *
18
- * @return \PodsArray
19
- *
20
- * @license http://www.gnu.org/licenses/gpl-2.0.html
21
- * @since 2.0
22
- */
23
- public function __construct ( $container ) {
24
- if ( is_array( $container ) || is_object( $container ) )
25
- $this->__container = &$container;
26
- }
27
-
28
- /**
29
- * Set value from array usage $object['offset'] = 'value';
30
- *
31
- * @param mixed $offset Used to set index of Array or Variable name on Object
32
- * @param mixed $value Value to be set
33
- *
34
- * @return mixed|void
35
- * @since 2.0
36
- */
37
- public function offsetSet ( $offset, $value ) {
38
- if ( is_array( $this->__container ) )
39
- $this->__container[ $offset ] = $value;
40
- else
41
- $this->__container->$offset = $value;
42
- return $value;
43
- }
44
-
45
- /**
46
- * Get value from array usage $object['offset'];
47
- *
48
- * @param mixed $offset Used to get value of Array or Variable on Object
49
- *
50
- * @return mixed|null
51
- * @since 2.0
52
- */
53
- public function offsetGet ( $offset ) {
54
- if ( is_array( $this->__container ) ) {
55
- if ( isset( $this->__container[ $offset ] ) )
56
- return $this->__container[ $offset ];
57
- return null;
58
- }
59
- if ( isset( $this->__container->$offset ) )
60
- return $this->__container->$offset;
61
- return null;
62
- }
63
-
64
- /**
65
- * Get value from array usage $object['offset'];
66
- *
67
- * @param mixed $offset Used to get value of Array or Variable on Object
68
- *
69
- * @return bool
70
- * @since 2.0
71
- */
72
- public function offsetExists ( $offset ) {
73
- if ( is_array( $this->__container ) )
74
- return isset( $this->__container[ $offset ] );
75
- return isset( $this->__container->$offset );
76
- }
77
-
78
- /**
79
- * Get value from array usage $object['offset'];
80
- *
81
- * @param mixed $offset Used to unset index of Array or Variable on Object
82
- *
83
- * @since 2.0
84
- */
85
- public function offsetUnset ( $offset ) {
86
- if ( is_array( $this->__container ) )
87
- unset( $this->__container[ $offset ] );
88
- else
89
- unset( $this->__container->$offset );
90
- }
91
-
92
- /**
93
- * Validate value on a specific type and set default (if empty)
94
- *
95
- * @param mixed $offset Used to get value of Array or Variable on Object
96
- * @param mixed $default Used to set default value if it doesn't exist
97
- * @param string $type Used to force a specific type of variable (allowed: array, object, integer, absint, boolean)
98
- * @param mixed $extra Used in advanced types of variables
99
- *
100
- * @return array|bool|int|mixed|null|number|object
101
- * @since 2.0
102
- */
103
- public function validate ( $offset, $default = null, $type = null, $extra = null ) {
104
- if ( !$this->offsetExists( $offset ) )
105
- $this->offsetSet( $offset, $default );
106
-
107
- $value = $this->offsetGet( $offset );
108
-
109
- if ( empty( $value ) && null !== $default && false !== $value )
110
- $value = $default;
111
- if ( 'array' == $type || 'array_merge' == $type ) {
112
- if ( !is_array( $value ) )
113
- $value = explode( ',', $value );
114
-
115
- if ( 'array_merge' == $type && $value !== $default )
116
- $value = array_merge( $default, $value );
117
- }
118
- elseif ( 'object' == $type || 'object_merge' == $type ) {
119
- if ( !is_object( $value ) ) {
120
- if ( !is_array( $value ) )
121
- $value = explode( ',', $value );
122
- $value = (object) $value;
123
- }
124
-
125
- if ( 'object_merge' == $type && $value !== $default )
126
- $value = (object) array_merge( (array) $default, (array) $value );
127
- }
128
- elseif ( 'integer' == $type || 'int' == $type || 'absint' == $type ) {
129
- if ( !is_numeric( trim( $value ) ) )
130
- $value = 0;
131
- else
132
- $value = intval( $value );
133
-
134
- if ( 'absint' == $type )
135
- $value = abs( $value );
136
- }
137
- elseif ( 'boolean' == $type || 'bool' == $type )
138
- $value = (boolean) $value;
139
- elseif ( 'in_array' == $type && is_array( $default ) ) {
140
- if ( is_array( $value ) ) {
141
- foreach ( $value as $k => $v ) {
142
- if ( !in_array( $v, $extra ) )
143
- unset( $value[ $k ] );
144
- }
145
- }
146
- elseif ( !in_array( $value, $extra ) )
147
- $value = $default;
148
- }
149
- elseif ( 'isset' == $type && is_array( $default ) ) {
150
- if ( is_array( $value ) ) {
151
- foreach ( $value as $k => $v ) {
152
- if ( !isset( $extra[ $v ] ) )
153
- unset( $value[ $k ] );
154
- }
155
- }
156
- elseif ( !isset( $extra[ $value ] ) )
157
- $value = $default;
158
- }
159
-
160
- $this->offsetSet( $offset, $value );
161
-
162
- return $value;
163
- }
164
-
165
- /**
166
- * Dump the PodsArray object to array
167
- *
168
- * @return array Array version of the object
169
- *
170
- * @since 2.0
171
- */
172
- public function dump () {
173
- if ( is_array( $this->__container ) )
174
- return $this->__container;
175
- return get_object_vars( $this->__container );
176
- }
177
-
178
- /**
179
- * Mapping >> offsetSet
180
- *
181
- * @since 2.0
182
- */
183
- public function __set ( $offset, $value ) {
184
- return $this->offsetSet( $offset, $value );
185
- }
186
-
187
- /**
188
- * Mapping >> offsetGet
189
- *
190
- * @since 2.0
191
- */
192
- public function __get ( $offset ) {
193
- return $this->offsetGet( $offset );
194
- }
195
-
196
- /**
197
- * Mapping >> offsetExists
198
- *
199
- * @since 2.0
200
- */
201
- public function __isset ( $offset ) {
202
- return $this->offsetExists( $offset );
203
- }
204
-
205
- /**
206
- * Mapping >> offsetUnset
207
- *
208
- * @since 2.0
209
- */
210
- public function __unset ( $offset ) {
211
- $this->offsetUnset( $offset );
212
- }
1
  <?php
2
+
3
  /**
4
  * @package Pods
5
  */
6
  class PodsArray implements ArrayAccess {
7
 
8
+ /**
9
+ * @var array|mixed
10
+ */
11
+ private $__container = array();
12
+
13
+ /**
14
+ * Alternative to get_object_vars to access an object as an array with simple functionality and accepts arrays to
15
+ * add additional functionality. Additional functionality includes validation and setting default data.
16
+ *
17
+ * @param mixed $container Object (or existing Array).
18
+ *
19
+ * @return \PodsArray
20
+ *
21
+ * @license http://www.gnu.org/licenses/gpl-2.0.html
22
+ * @since 2.0.0
23
+ */
24
+ public function __construct( $container ) {
25
+
26
+ if ( is_array( $container ) || is_object( $container ) ) {
27
+ $this->__container = &$container;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Set value from array usage $object['offset'] = 'value';
33
+ *
34
+ * @param mixed $offset Used to set index of Array or Variable name on Object.
35
+ * @param mixed $value Value to be set.
36
+ *
37
+ * @return mixed
38
+ * @since 2.0.0
39
+ */
40
+ public function offsetSet( $offset, $value ) {
41
+
42
+ if ( is_array( $this->__container ) ) {
43
+ $this->__container[ $offset ] = $value;
44
+ } else {
45
+ $this->__container->{$offset} = $value;
46
+ }
47
+
48
+ return $value;
49
+ }
50
+
51
+ /**
52
+ * Get value from array usage $object['offset'];
53
+ *
54
+ * @param mixed $offset Used to get value of Array or Variable on Object.
55
+ *
56
+ * @return mixed|null
57
+ * @since 2.0.0
58
+ */
59
+ public function offsetGet( $offset ) {
60
+
61
+ if ( is_array( $this->__container ) ) {
62
+ if ( isset( $this->__container[ $offset ] ) ) {
63
+ return $this->__container[ $offset ];
64
+ }
65
+ } elseif ( isset( $this->__container->$offset ) ) {
66
+ return $this->__container->$offset;
67
+ }
68
+
69
+ return null;
70
+ }
71
+
72
+ /**
73
+ * Get value from array usage $object['offset'];
74
+ *
75
+ * @param mixed $offset Used to get value of Array or Variable on Object.
76
+ *
77
+ * @return bool
78
+ * @since 2.0.0
79
+ */
80
+ public function offsetExists( $offset ) {
81
+
82
+ if ( is_array( $this->__container ) ) {
83
+ return isset( $this->__container[ $offset ] );
84
+ }
85
+
86
+ return isset( $this->__container->$offset );
87
+ }
88
+
89
+ /**
90
+ * Get value from array usage $object['offset'];
91
+ *
92
+ * @param mixed $offset Used to unset index of Array or Variable on Object.
93
+ *
94
+ * @since 2.0.0
95
+ */
96
+ public function offsetUnset( $offset ) {
97
+
98
+ if ( is_array( $this->__container ) ) {
99
+ unset( $this->__container[ $offset ] );
100
+ } else {
101
+ unset( $this->__container->$offset );
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Validate value on a specific type and set default (if empty)
107
+ *
108
+ * @par