forked from pods-framework/pods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPodsAPI.php
More file actions
2936 lines (2613 loc) · 120 KB
/
PodsAPI.php
File metadata and controls
2936 lines (2613 loc) · 120 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class PodsAPI {
public $display_errors = false;
public $pod;
public $pod_id;
public $pod_data;
public $fields;
public $format = 'php';
/**
* Store and retrieve data programatically
*
* @param string $dtname (optional) The pod name
* @param string $format (optional) Format for import/export, "php" or "csv"
*
* @license http://www.gnu.org/licenses/gpl-2.0.html
* @since 1.7.1
*/
public function __construct ( $pod = null, $format = 'php' ) {
if ( null !== $pod && 0 < strlen( (string) $pod ) ) {
$this->format = $format;
$this->pod_data = $this->load_pod( array( 'name' => $pod ) );
if ( false !== $this->pod_data && is_array( $this->pod_data ) ) {
$this->pod = $this->pod_data[ 'name' ];
$this->pod_id = $this->pod_data[ 'id' ];
$this->fields = $this->pod_data[ 'fields' ];
}
else
return false;
}
}
/**
* Add a Pod via the Wizard
*
* $params['create_extend'] string Create or Extend a Content Type
* $params['create_pod_type'] string Pod Type (for Creating)
* $params['create_name'] string Pod Name (for Creating)
* $params['create_label_plural'] string Plural Label (for Creating)
* $params['create_label_singular'] string Singular Label (for Creating)
* $params['create_storage'] string Storage Type (for Creating Post Types)
* $params['extend_pod_type'] string Pod Type (for Extending)
* $params['extend_post_type'] string Post Type (for Extending Post Types)
* $params['extend_taxonomy'] string Taxonomy (for Extending Taxonomies)
* $params['extend_storage'] string Storage Type (for Extending Post Types / Users / Comments)
*
* @param array $params An associative array of parameters
*
* @since 2.0.0
*/
public function add_pod ( $params ) {
$defaults = array(
'create_extend' => 'create',
'create_pod_type' => 'post_type',
'create_name' => '',
'create_label_plural' => '',
'create_label_singular' => '',
'create_storage' => 'meta',
'extend_pod_type' => 'post_type',
'extend_post_type' => 'post',
'extend_taxonomy' => 'category',
'extend_storage' => 'meta'
);
$params = (object) array_merge( $defaults, (array) $params );
if ( empty( $params->create_extend ) || !in_array( $params->create_extend, array( 'create', 'extend' ) ) )
return pods_error( __( 'Please choose whether to Create or Extend a Content Type', $this ) );
$pod_params = array();
if ( 'create' == $params->create_extend ) {
if ( empty( $params->create_name ) )
return pods_error( 'Please enter a Name for this Pod', $this );
$pod_params = array(
'name' => $params->create_name,
'type' => $params->create_pod_type,
'storage' => $params->create_storage,
'options' => array(
'cpt_show_ui' => 1,
'ct_show_ui' => 1,
'cpt_public' => 1,
'ct_public' => 1
)
);
if ( 'post_type' == $pod_params[ 'type' ] ) {
$pod_params[ 'options' ][ 'cpt_label' ] = ( !empty( $params->create_label_plural ) ? $params->create_label_plural : ucwords( str_replace( '_', ' ', $params->create_name ) ) );
$pod_params[ 'options' ][ 'cpt_singular_label' ] = ( !empty( $params->create_label_singular ) ? $params->create_label_singular : ucwords( str_replace( '_', ' ', $params->create_name ) ) );
}
elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
$pod_params[ 'storage' ] = 'table';
$pod_params[ 'options' ][ 'ct_label' ] = ( !empty( $params->create_label_plural ) ? $params->create_label_plural : ucwords( str_replace( '_', ' ', $params->create_name ) ) );
$pod_params[ 'options' ][ 'ct_singular_label' ] = ( !empty( $params->create_label_singular ) ? $params->create_label_singular : ucwords( str_replace( '_', ' ', $params->create_name ) ) );
}
elseif ( 'pod' == $pod_params[ 'type' ] ) {
$pod_params[ 'storage' ] = 'table';
$pod_params[ 'options' ][ 'label' ] = ( !empty( $params->create_label_plural ) ? $params->create_label_plural : ucwords( str_replace( '_', ' ', $params->create_name ) ) );
$pod_params[ 'options' ][ 'singular_label' ] = ( !empty( $params->create_label_singular ) ? $params->create_label_singular : ucwords( str_replace( '_', ' ', $params->create_name ) ) );
}
}
elseif ( 'extend' == $params->create_extend ) {
$pod_params = array(
'type' => $params->extend_pod_type,
'storage' => $params->extend_storage,
'options' => array()
);
if ( 'post_type' == $pod_params[ 'type' ] )
$pod_params[ 'name' ] = $pod_params[ 'object' ] = $params->extend_post_type;
elseif ( 'taxonomy' == $pod_params[ 'type' ] ) {
$pod_params[ 'storage' ] = 'table';
$pod_params[ 'name' ] = $pod_params[ 'object' ] = $params->extend_taxonomy;
}
else
$pod_params[ 'name' ] = $pod_params[ 'object' ] = $params->extend_pod_type;
}
if ( !empty( $pod_params ) )
return $this->save_pod( $pod_params );
return false;
}
/**
* Add or edit a Pod
*
* $params['id'] int The Pod ID
* $params['name'] string The Pod name
* $params['type'] string The Pod type
* $params['object'] string Object name
* $params['storage'] string Storage type
* $params['alias'] string Alias
* $params['weight'] int Weight (used to sort admin menu)
* $params['options'] array Options
*
* @param array $params An associative array of parameters
*
* @since 1.7.9
*
* @todo Save new fields with save_field, delete with delete_field (it updates cache etc)
*
*/
public function save_pod ( $params ) {
$pod = $this->load_pod( $params, false );
$params = (object) pods_sanitize( $params );
$old_name = $old_id = null;
if ( !empty( $pod ) ) {
if ( isset( $params->id ) && 0 < $params->id )
$old_id = $params->id;
$params->id = $pod[ 'id' ];
if ( isset( $params->name ) && 0 < strlen( $params->name ) )
$old_name = $pod[ 'name' ];
else
$params->name = $old_name = $pod[ 'name' ];
if ( $old_name != $params->name && false !== $this->pod_exists( array( 'name' => $params->name ) ) )
return pods_error( 'Pod ' . $params->name . ' already exists, you cannot rename ' . $old_name . ' to that', $this );
elseif ( $old_id != $params->id ) {
if ( $params->type == $pod[ 'type' ] && isset( $params->object ) && $params->object == $pod[ 'object' ] )
return pods_error( 'Pod using ' . $params->object . ' already exists, you can not reuse an object across multiple pods', $this );
else
return pods_error( 'Pod ' . $params->name . ' already exists', $this );
}
}
// Add new pod
if ( empty( $params->id ) ) {
$params->name = pods_clean_name( $params->name );
if ( strlen( $params->name ) < 1 )
return pods_error( 'Pod name cannot be empty', $this );
$check = pods_query( "SELECT `id` FROM `@wp_pods` WHERE `name` = '{$params->name}' LIMIT 1", $this );
if ( !empty( $check ) )
return pods_error( 'Pod ' . $params->name . ' already exists, you can not add one using the same name', $this );
$pod = array( 'name' => $params->name, 'options' => '', 'type' => 'pod', 'storage' => 'table' );
if ( isset( $params->type ) && 0 < strlen( $params->type ) )
$pod[ 'type' ] = $params->type;
if ( isset( $params->object ) && 0 < strlen( $params->object ) )
$pod[ 'object' ] = $params->object;
if ( isset( $params->storage ) && 0 < strlen( $params->storage ) )
$pod[ 'storage' ] = $params->storage;
if ( isset( $params->alias ) && 0 < strlen( $params->alias ) )
$pod[ 'alias' ] = $params->alias;
if ( isset( $params->weight ) && 0 < strlen( $params->weight ) )
$pod[ 'weight' ] = $params->weight;
if ( !isset( $params->options ) || empty( $params->options ) ) {
$options = get_object_vars( $params );
$exclude = array(
'id',
'name',
'type',
'object',
'storage',
'alias',
'weight',
'options',
'field_data'
);
foreach ( $exclude as $exclude_field ) {
if ( isset( $options[ $exclude_field ] ) )
unset( $options[ $exclude_field ] );
}
$params->options = '';
if ( !empty( $options ) )
$params->options = $options;
}
if ( !empty( $params->options ) )
$params->options = pods_sanitize( str_replace( '@wp_', '{prefix}', json_encode( $params->options ) ) );
$params->id = pods_query( "INSERT INTO `@wp_pods` (`" . implode( '`,`', array_keys( $pod ) ) . "`) VALUES ('" . implode( "','", $pod ) . "')", $this );
if ( false === $params->id )
return pods_error( 'Cannot add entry for new Pod', $this );
$field_columns = array(
'pod_id' => $params->id,
'name' => '',
'label' => '',
'type' => 'text',
'pick_object' => '',
'pick_val' => '',
'sister_field_id' => 0,
'weight' => 0,
'options' => ''
);
$fields = array();
$weight = 0;
if ( 'pod' == $params->type ) {
$fields[] = array(
'name' => 'name',
'label' => 'Name',
'type' => 'text',
'weight' => $weight,
'options' => array( 'required' => '1' )
);
$weight++;
$fields[] = array(
'name' => 'created',
'label' => 'Date Created',
'type' => 'date',
'weight' => $weight
);
$weight++;
$fields[] = array(
'name' => 'modified',
'label' => 'Date Modified',
'type' => 'date',
'weight' => $weight
);
$weight++;
$fields[] = array(
'name' => 'author',
'label' => 'Author',
'type' => 'pick',
'pick_object' => 'user',
'weight' => $weight
);
$weight++;
$fields[] = array(
'name' => 'permalink',
'label' => 'Permalink',
'type' => 'slug',
'weight' => $weight,
'options' => array( 'comment' => 'Leave blank to auto-generate from Name' )
);
$weight++;
}
if ( isset( $params->fields ) && is_array( $params->fields ) && !empty( $params->fields ) )
$fields = $params->fields;
$rows = array();
$definitions = array( "`id` BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY" );
foreach ( $fields as $field ) {
$row = array();
foreach ( $field_columns as $field_name => $default ) {
$row[ $field_name ] = $default;
if ( isset( $field[ $field_name ] ) )
$row[ $field_name ] = $field[ $field_name ];
}
if ( !empty( $row[ 'options' ] ) ) {
if ( is_array( $row[ 'options' ] ) ) {
$options = $row[ 'options' ];
$exclude = array_keys( $field_columns );
foreach ( $exclude as $exclude_field ) {
if ( isset( $options[ $exclude_field ] ) )
unset( $options[ $exclude_field ] );
}
$row[ 'options' ] = '';
if ( !empty( $options ) )
$row[ 'options' ] = pods_sanitize( str_replace( '@wp_', '{prefix}', json_encode( $options ) ) );
}
}
$rows[] = implode( "','", $row );
if ( !in_array( $row[ 'type' ], array( 'pick', 'file' ) ) )
$definitions[] = "`{$field['name']}` " . $this->get_field_definition( $field[ 'type' ] );
}
if ( 'table' == $pod[ 'storage' ] ) {
$result = pods_query( "CREATE TABLE `@wp_pods_tbl_{$params->name}` (" . implode( ', ', $definitions ) . ") DEFAULT CHARSET utf8", $this );
if ( empty( $result ) )
return pods_error( 'Cannot add Database Table for new Pod' );
}
if ( !empty( $rows ) ) {
$result = pods_query( "INSERT INTO `@wp_pods_fields` (`" . implode( '`,`', array_keys( $field_columns ) ) . "`) VALUES ('" . implode( "'),('", $rows ) . "')", $this );
if ( empty( $result ) )
return pods_error( 'Cannot add fields for new Pod' );
}
}
// Edit existing pod
else {
if ( empty( $pod ) )
return pods_error( 'Pod not found' );
$set = array();
if ( !isset( $params->options ) || empty( $params->options ) ) {
$options = get_object_vars( $params );
$exclude = array( 'id', 'name', 'type', 'object', 'options', 'field_data' );
foreach ( $exclude as $exclude_field ) {
if ( isset( $options[ $exclude_field ] ) ) {
if ( 'field_data' != $exclude_field )
$set[] = "`{$exclude_field}` = '{$params->$exclude_field}'";
unset( $options[ $exclude_field ] );
}
}
$params->options = '';
if ( !empty( $options ) )
$params->options = $options;
}
if ( !empty( $params->options ) )
$params->options = pods_sanitize( str_replace( '@wp_', '{prefix}', json_encode( $params->options ) ) );
$saved = array();
if ( isset( $params->field_data ) && is_array( $params->field_data ) && !empty( $params->field_data ) ) {
$weight = 0;
foreach ( $params->field_data as $field_data ) {
if ( !is_array( $field_data ) )
continue;
$field_data[ 'pod' ] = $pod;
$field_data[ 'weight' ] = $weight;
$field_id = $this->save_field( $field_data );
if ( 0 < $field_id )
$saved[ $field_id ] = true;
else {
return pods_error( 'Cannot edit field', $this );
}
$weight++;
}
foreach ( $pod[ 'fields' ] as $field ) {
if ( !isset( $saved[ $field[ 'id' ] ] ) ) {
$this->delete_field( array(
'id' => (int) $field[ 'id' ],
'name' => (int) $field[ 'name' ],
'pod' => $pod
) );
}
}
}
$set[] = "`options` = '{$params->options}'";
$set = implode( ', ', $set );
pods_query( "UPDATE `@wp_pods` SET {$set} WHERE `id` = {$params->id}", $this );
if ( 'table' == $pod[ 'storage' ] && null !== $old_name && $old_name != $params->name ) {
pods_query( "ALTER TABLE `@wp_pods_tbl_{$old_name}` RENAME `@wp_pods_tbl_{$params->name}`", $this );
}
}
$this->cache_flush_pods( $pod );
return $params->id;
}
/**
* Add or edit a column within a Pod
*
* $params['id'] int The field ID
* $params['pod_id'] int The Pod ID
* $params['pod'] string The Pod name
* $params['name'] string The field name
* $params['label'] string The field label
* $params['type'] string The column type ("txt", "desc", "pick", etc)
* $params['pick_object'] string The related PICK object name
* $params['pick_val'] string The related PICK object value
* $params['sister_field_id'] int (optional) The related field ID
* $params['weight'] int The field weight
* $params['options'] array The field options
*
* @param array $params An associative array of parameters
*
* @since 1.7.9
*/
public function save_field ( $params ) {
$params = (object) $params;
if ( isset( $params->pod_id ) ) {
$params->pod_id = pods_absint( $params->pod_id );
}
$pod = null;
$save_pod = false;
if ( !isset( $params->pod_id ) || empty( $params->pod_id ) ) {
if ( isset( $params->pod ) ) {
$pod = $params->pod;
if ( !is_array( $pod ) )
$pod = $this->load_pod( array( 'name' => $pod ) );
else
$save_pod = true;
if ( empty( $pod ) )
return pods_error( 'Pod ID or name is required', $this );
else {
$params->pod_id = $pod[ 'id' ];
$params->pod = $pod[ 'name' ];
}
}
else
return pods_error( 'Pod ID or name is required', $this );
}
elseif ( !isset( $params->pod ) ) {
$pod = $this->load_pod( array( 'id' => $params->pod_id ) );
if ( empty( $pod ) )
return pods_error( 'Pod not found', $this );
else {
$params->pod_id = $pod[ 'id' ];
$params->pod = $pod[ 'name' ];
}
}
if ( !isset( $pod ) )
$pod = $this->load_pod( array( 'id' => $params->pod_id ) );
$params->name = pods_clean_name( $params->name );
if ( empty( $params->name ) )
return pods_error( 'Pod Column name is required', $this );
$defaults = array(
'id' => 0,
'pod_id' => 0,
'name' => '',
'label' => '',
'type' => '',
'pick_object' => '',
'pick_val' => '',
'sister_field_id' => '',
'weight' => 0,
'options' => array()
);
$params = (object) array_merge( $defaults, (array) $params );
$tableless_field_types = $this->do_hook( 'tableless_field_types', array( 'pick', 'file' ) );
// Add new column
if ( !isset( $params->id ) || empty( $params->id ) ) {
if ( in_array( $params->name, array( 'p' ) ) ) // there are more, let's add them as we find them
return pods_error( "$params->name is reserved for internal WordPress usage, please try a different name", $this );
if ( in_array( $params->name, array( 'id', 'created', 'modified', 'author' ) ) )
return pods_error( "$params->name is reserved for internal Pods usage, please try a different name", $this );
$sql = "SELECT `id` FROM `@wp_pods_fields` WHERE `pod_id` = {$params->pod_id} AND `name` = '{$params->name}' LIMIT 1";
$result = pods_query( $sql, $this );
if ( !empty( $result ) )
return pods_error( "Pod Column {$params->name} already exists", $this );
if ( 'slug' == $params->type ) {
$sql = "SELECT `id` FROM `@wp_pods_fields` WHERE `pod_id` = {$params->pod_id} AND `type` = 'slug' LIMIT 1";
$result = pods_query( $sql, $this );
if ( !empty( $result ) )
return pods_error( 'This pod already has a permalink column', $this );
}
// Sink the new column to the bottom of the list
if ( !isset( $params->weight ) ) {
$params->weight = 0;
$result = pods_query( "SELECT `weight` FROM `@wp_pods_fields` WHERE `pod_id` = {$params->pod_id} ORDER BY `weight` DESC LIMIT 1", $this );
if ( !empty( $result ) )
$params->weight = pods_absint( $result[ 0 ]->weight ) + 1;
}
$params->sister_field_id = pods_absint( $params->sister_field_id );
$params->weight = pods_absint( $params->weight );
if ( !isset( $params->options ) || empty( $params->options ) ) {
$options = get_object_vars( $params );
$exclude = array(
'id',
'pod_id',
'pod',
'name',
'label',
'type',
'pick_object',
'pick_val',
'sister_field_id',
'weight',
'options'
);
foreach ( $exclude as $exclude_field ) {
if ( isset( $options[ $exclude_field ] ) )
unset( $options[ $exclude_field ] );
}
$params->options = '';
if ( !empty( $options ) )
$params->options = $options;
}
if ( !empty( $params->options ) ) {
$params->options = pods_sanitize( str_replace( '@wp_', '{prefix}', json_encode( $params->options ) ) );
}
$field_id = pods_query( "INSERT INTO `@wp_pods_fields` (`pod_id`, `name`, `label`, `type`, `pick_object`, `pick_val`, `sister_field_id`, `weight`, `options`) VALUES ('{$params->pod_id}', '{$params->name}', '{$params->label}', '{$params->type}', '{$params->pick_object}', '{$params->pick_val}', {$params->sister_field_id}, {$params->weight}, '{$params->options}')", 'Cannot add new field' );
if ( empty( $field_id ) )
return pods_error( "Cannot add new field {$params->name}", $this );
if ( 'table' == $pod[ 'storage' ] && !in_array( $params->type, $tableless_field_types ) ) {
$dbtype = $this->get_field_definition( $params->type );
pods_query( "ALTER TABLE `@wp_pods_tbl_{$params->pod}` ADD COLUMN `{$params->name}` {$dbtype}", 'Cannot create new column' );
}
elseif ( 0 < $params->sister_field_id ) {
pods_query( "UPDATE `@wp_pods_fields` SET `sister_field_id` = '{$field_id}' WHERE `id` = {$params->sister_field_id} LIMIT 1", 'Cannot update sister field' );
}
$params->id = $field_id;
}
// Edit existing column
else {
$params->id = pods_absint( $params->id );
if ( 'id' == $params->name ) {
return pods_error( "$params->name is not editable", $this );
}
$sql = "SELECT `id` FROM `@wp_pods_fields` WHERE `pod_id` = {$params->pod_id} AND `id` != {$params->id} AND name = '{$params->name}' LIMIT 1";
$check = pods_query( $sql, $this );
if ( !empty( $check ) )
return pods_error( "Column {$params->name} already exists", $this );
$sql = "SELECT * FROM `@wp_pods_fields` WHERE `id` = {$params->id} LIMIT 1";
$result = pods_query( $sql, $this );
if ( empty( $result ) )
return pods_error( "Column {$params->name} not found, cannot edit", $this );
$old_type = $result[ 0 ]->type;
$old_name = $result[ 0 ]->name;
$dbtype = $this->get_field_definition( $params->type );
$params->pick_val = ( 'pick' != $params->type || empty( $params->pick_val ) ) ? '' : "$params->pick_val";
$params->sister_field_id = pods_absint( $params->sister_field_id );
$params->weight = pods_absint( $params->weight );
if ( $params->type != $old_type ) {
if ( in_array( $params->type, $tableless_field_types ) ) {
if ( 'table' == $pod[ 'storage' ] && !in_array( $old_type, $tableless_field_types ) ) {
pods_query( "ALTER TABLE `@wp_pods_tbl_$params->pod` DROP COLUMN `$old_name`" );
}
}
elseif ( in_array( $old_type, $tableless_field_types ) ) {
if ( 'table' == $pod[ 'storage' ] )
pods_query( "ALTER TABLE `@wp_pods_tbl_$params->pod` ADD COLUMN `$params->name` $dbtype", 'Cannot create column' );
pods_query( "UPDATE @wp_pods_fields SET sister_field_id = NULL WHERE sister_field_id = $params->id" );
pods_query( "DELETE FROM @wp_pods_rel WHERE field_id = $params->id" );
}
else {
pods_query( "ALTER TABLE `@wp_pods_tbl_$params->pod` CHANGE `$old_name` `$params->name` $dbtype" );
}
}
elseif ( 'table' == $pod[ 'storage' ] && $params->name != $old_name && !in_array( $params->type, $tableless_field_types ) ) {
pods_query( "ALTER TABLE `@wp_pods_tbl_$params->pod` CHANGE `$old_name` `$params->name` $dbtype" );
}
if ( !isset( $params->options ) || empty( $params->options ) ) {
$options = get_object_vars( $params );
$exclude = array(
'id',
'pod_id',
'pod',
'name',
'label',
'type',
'pick_object',
'pick_val',
'sister_field_id',
'weight',
'options'
);
foreach ( $exclude as $exclude_field ) {
if ( isset( $options[ $exclude_field ] ) )
unset( $options[ $exclude_field ] );
}
$params->options = '';
if ( !empty( $options ) )
$params->options = $options;
}
if ( !empty( $params->options ) ) {
$params->options = pods_sanitize( str_replace( '@wp_', '{prefix}', json_encode( $params->options ) ) );
}
pods_query( "UPDATE `@wp_pods_fields` SET `name` = '{$params->name}', `label` = '{$params->label}', `type` = '{$params->type}', `pick_object` = '{$params->pick_object}', `pick_val` = '{$params->pick_val}', `sister_field_id` = {$params->sister_field_id}, `weight` = {$params->weight}, `options` = '{$params->options}' WHERE `id` = {$params->id} LIMIT 1", 'Cannot edit column' );
}
if ( !$save_pod )
$this->cache_flush_pods( $pod );
return $params->id;
}
/**
* Add or Edit a Pods Object
*
* $params['id'] int The Object ID
* $params['name'] string The Object name
* $params['type'] string The Object type
* $params['options'] Associative array of Object options
*
* @param array $params An associative array of parameters
*
* @since 2.0.0
*/
public function save_object ( $params ) {
$params = (object) $params;
if ( !isset( $params->name ) || empty( $params->name ) )
return pods_error( 'Name must be given to save an Object', $this );
if ( !isset( $params->type ) || empty( $params->type ) )
return pods_error( 'Type must be given to save an Object', $this );
if ( !isset( $params->options ) || empty( $params->options ) ) {
$options = get_object_vars( $params );
$exclude = array( 'id', 'name', 'type', 'options' );
foreach ( $exclude as $exclude_field ) {
if ( isset( $options[ $exclude_field ] ) )
unset( $options[ $exclude_field ] );
}
$params->options = '';
if ( !empty( $options ) )
$params->options = $options;
}
if ( !empty( $params->options ) )
$params->options = str_replace( '@wp_', '{prefix}', json_encode( $params->options ) );
$params = pods_sanitize( $params );
if ( isset( $params->id ) && !empty( $params->id ) ) {
$params->id = pods_absint( $params->id );
$result = pods_query( "UPDATE `@wp_pods_objects` SET `name` = '{$params->name}', `type` = '{$params->type}', `options` = '{$params->options}' WHERE `id` = " . pods_absint( $params->id ) );
if ( empty( $result ) )
return pods_error( ucwords( $params->type ) . ' Object not saved', $this );
}
else {
$sql = "SELECT id FROM `@wp_pods_objects` WHERE `name` = '{$params->name}' LIMIT 1";
$check = pods_query( $sql, $this );
if ( !empty( $check ) )
return pods_error( ucwords( $params->type ) . " Object {$params->name} already exists", $this );
$object_id = pods_query( "INSERT INTO `@wp_pods_objects` (`name`, `type`, `options`) VALUES ('{$params->name}', '{$params->type}', '{$params->options}')" );
if ( empty( $object_id ) )
return pods_error( ucwords( $params->type ) . ' Object not saved', $this );
$params->id = $object_id;
}
delete_transient( 'pods_object_' . $params->type );
delete_transient( 'pods_object_' . $params->type . '_' . $params->name );
return $params->id;
}
/**
* Add or edit a Pod Template
*
* $params['id'] int The template ID
* $params['name'] string The template name
* $params['code'] string The template code
*
* @param array $params An associative array of parameters
*
* @since 1.7.9
*/
public function save_template ( $params ) {
$params = (object) $params;
$params->type = 'template';
return $this->save_object( $params );
}
/**
* Add or edit a Pod Page
*
* $params['id'] int The page ID
* $params['uri'] string The page URI
* $params['phpcode'] string The page code
*
* @param array $params An associative array of parameters
*
* @since 1.7.9
*/
public function save_page ( $params ) {
$params = (object) $params;
if ( !isset( $params->name ) ) {
$params->name = $params->uri;
unset( $params->uri );
}
$params->name = trim( $params->name, '/' );
$params->type = 'page';
return $this->save_object( $params );
}
/**
* Add or edit a Pod Helper
*
* $params['id'] int The helper ID
* $params['name'] string The helper name
* $params['helper_type'] string The helper type ("pre_save", "display", etc)
* $params['phpcode'] string The helper code
*
* @param array $params An associative array of parameters
*
* @since 1.7.9
*/
public function save_helper ( $params ) {
$params = (object) $params;
$params->type = 'helper';
return $this->save_object( $params );
}
/**
* Save the entire role structure
*
* @param array $params An associative array of parameters
*
* @since 1.7.9
*/
public function save_roles ( $params ) {
$params = pods_sanitize( $params );
$roles = array();
foreach ( $params as $key => $val ) {
if ( 'action' != $key ) {
$tmp = empty( $val ) ? array() : explode( ',', $val );
$roles[ $key ] = $tmp;
}
}
delete_option( 'pods_roles' );
add_option( 'pods_roles', serialize( $roles ) );
}
/**
* Add or edit a single pod item
*
* $params['pod'] string The Pod name (pod or pod_id is required)
* $params['pod_id'] string The Pod ID (pod or pod_id is required)
* $params['id'] int The item ID
* $params['data'] array (optional) Associative array of column names + values
* $params['bypass_helpers'] bool Set to true to bypass running pre-save and post-save helpers
*
* @param array $params An associative array of parameters
*
* @return int The item ID
* @since 1.7.9
*/
public function save_pod_item ( $params ) {
$params = (object) str_replace( '@wp_', '{prefix}', pods_sanitize( $params ) );
// @deprecated 2.0.0
if ( isset( $params->datatype ) ) {
pods_deprecated( '$params->pod instead of $params->datatype', '2.0.0' );
$params->pod = $params->datatype;
unset( $params->datatype );
if ( isset( $params->pod_id ) ) {
pods_deprecated( '$params->id instead of $params->pod_id', '2.0.0' );
$params->id = $params->pod_id;
unset( $params->pod_id );
}
if ( isset( $params->data ) && !empty( $params->data ) && is_array( $params->data ) ) {
pods_deprecated( 'PodsAPI::save_pod_items', '2.0.0' );
return $this->save_pod_items( $params, $params->data );
}
}
// @deprecated 2.0.0
if ( isset( $params->tbl_row_id ) ) {
pods_deprecated( '$params->id instead of $params->tbl_row_id', '2.0.0' );
$params->id = $params->tbl_row_id;
unset( $params->tbl_row_id );
}
// @deprecated 2.0.0
if ( isset( $params->columns ) ) {
pods_deprecated( '$params->data instead of $params->columns', '2.0.0' );
$params->data = $params->columns;
unset( $params->columns );
}
if ( !isset( $params->pod ) )
$params->pod = false;
if ( isset( $params->pod_id ) )
$params->pod_id = pods_absint( $params->pod_id );
else
$params->pod_id = 0;
if ( isset( $params->id ) )
$params->id = pods_absint( $params->id );
else
$params->id = 0;
// Support for bulk edit
if ( isset( $params->id ) && !empty( $params->id ) && is_array( $params->id ) ) {
$ids = array();
$new_params = $params;
foreach ( $params->id as $id ) {
$new_params->id = $id;
$ids[] = $this->save_pod_item( $new_params );
}
return $ids;
}
// Allow Helpers to know what's going on, are we adding or saving?
$is_new_item = false;
if ( empty( $params->id ) ) {
$is_new_item = true;
}
// Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls
$bypass_helpers = false;
if ( isset( $params->bypass_helpers ) && false !== $params->bypass_helpers ) {
$bypass_helpers = true;
}
// Get array of Pods
if ( empty( $this->pod_data ) || ( $this->pod != $params->pod && $this->pod_id != $params->pod_id ) )
$this->pod_data = $this->load_pod( array( 'id' => $params->pod_id, 'name' => $params->pod ) );
if ( false === $this->pod_data )
return pods_error( "Pod not found", $this );
$this->pod = $params->pod = $this->pod_data[ 'name' ];
$this->pod_id = $params->pod_id = $this->pod_data[ 'id' ];
$this->fields = $this->pod_data[ 'fields' ];
$fields =& $this->fields; // easy to use variable for helpers
$fields_active = array();
// Find the active columns (loop through $params->data to retain order)
if ( !empty( $params->data ) && is_array( $params->data ) ) {
foreach ( $params->data as $field => $value ) {
if ( isset( $fields[ $field ] ) ) {
$fields[ $field ][ 'value' ] = $value;
$fields_active[] = $field;
}
}
unset( $params->data );
}
$columns =& $fields; // @deprecated 2.0.0
$active_columns =& $fields_active; // @deprecated 2.0.0
$pre_save_helpers = $post_save_helpers = array();
if ( false === $bypass_helpers ) {
// Plugin hook
$this->do_hook( 'pre_save_pod_item', $params, $fields );
$this->do_hook( "pre_save_pod_item_{$params->pod}", $params );
if ( false !== $is_new_item ) {
$this->do_hook( 'pre_create_pod_item', $params );
$this->do_hook( "pre_create_pod_item_{$params->pod}", $params );
}
else {
$this->do_hook( 'pre_edit_pod_item', $params );
$this->do_hook( "pre_edit_pod_item_{$params->pod}", $params );
}
// Call any pre-save helpers (if not bypassed)
if ( !defined( 'PODS_DISABLE_EVAL' ) || PODS_DISABLE_EVAL ) {
if ( !empty( $this->pod_data[ 'options' ] ) && is_array( $this->pod_data[ 'options' ] ) ) {
$helpers = array( 'pre_save_helpers', 'post_save_helpers' );
foreach ( $helpers as $helper ) {
if ( isset( $this->pod_data[ 'options' ][ $helper ] ) && !empty( $this->pod_data[ 'options' ][ $helper ] ) )
${$helper} = explode( ',', $this->pod_data[ 'options' ][ $helper ] );
}
}
if ( !empty( $pre_save_helpers ) ) {
pods_deprecated( 'Pre-save helpers are deprecated, use the action pods_pre_save_pod_item_' . $params->pod . ' instead', '2.0.0' );
foreach ( $pre_save_helpers as $helper ) {
$helper = $this->load_helper( array( 'name' => $helper ) );
if ( false !== $helper )
echo eval( '?>' . $helper[ 'code' ] );
}
}
}
}
$table_data = $rel_fields = $rel_field_ids = array();
// Loop through each active column, validating and preparing the table data
foreach ( $fields_active as $field ) {
$value = $fields[ $field ][ 'value' ];
$type = $fields[ $field ][ 'type' ];
// Validate value
$value = $this->handle_field_validation( $value, $field, $fields, $params );
if ( false === $value )
return false;
// Prepare all table (non-relational) data
if ( !in_array( $type, array( 'pick', 'file' ) ) )
$table_data[] = "`{$field}` = '{$value}'";
// Store relational column data to be looped through later
else {
$rel_fields[ $type ][ $field ] = $value;
$rel_field_ids[] = $fields[ $field ][ 'id' ];
}
}
// @todo: Use REPLACE INTO instead and set defaults on created / modified / author (and check if they exist)
if ( false !== $is_new_item ) {
$current_time = current_time( 'mysql' );
$author = 0;
if ( is_user_logged_in() ) {
global $user_ID;
get_currentuserinfo();
$author = pods_absint( $user_ID );
}
$params->id = pods_query( "INSERT INTO `@wp_pods_tbl_{$params->pod}` (`created`, `modified`, `author`) VALUES ('{$current_time}', '{$current_time}', {$author})", 'Cannot add new table row' );
}
// Save the table row
if ( !empty( $table_data ) ) {
$table_data = implode( ', ', $table_data );
pods_query( "UPDATE `@wp_pods_tbl_{$params->pod}` SET {$table_data} WHERE `id` = {$params->id} LIMIT 1" );
}
// Save relational column data
if ( !empty( $rel_fields ) ) {
// E.g. $rel_fields['pick']['related_events'] = '3,15';
foreach ( $rel_fields as $type => $data ) {
foreach ( $data as $field => $values ) {
$field_id = pods_absint( $fields[ $field ][ 'id' ] );
// Remove existing relationships
pods_query( "DELETE FROM `@wp_pods_rel` WHERE `pod_id` = {$params->pod_id} AND `field_id` = {$field_id} AND `item_id` = {$params->id}", $this );
// Convert values from a comma-separated string into an array
if ( !is_array( $values ) )
$values = explode( ',', $values );
// File relationships
if ( 'file' == $type ) {
if ( empty( $values ) )
continue;
$weight = 0;
foreach ( $values as $id ) {
$id = pods_absint( $id );
pods_query( "INSERT INTO `@wp_pods_rel` (`pod_id`, `field_id`, `item_id`, `related_item_id`, `weight`) VALUES ({$params->pod_id}, {$field_id}, {$params->id}, {$id}, {$weight})" );
$weight++;
}
}
// Pick relationships
elseif ( 'pick' == $type ) {
$pick_object = $fields[ $field ][ 'pick_object' ]; // pod, post_type, taxonomy, etc..
$pick_val = $fields[ $field ][ 'pick_val' ]; // pod name, post type name, taxonomy name, etc..
$related_pod_id = $related_field_id = 0;
if ( 'pod' == $pick_object ) {
$related_pod = $this->load_pod( array( 'name' => $pick_val ) );
if ( false !== $related_pod )
$related_pod_id = $related_pod[ 'id' ];
if ( 0 < $fields[ $field ][ 'sister_field_id' ] ) {
foreach ( $related_pod[ 'fields' ] as $field ) {
if ( 'pick' == $field[ 'type' ] && $fields[ $field ][ 'sister_field_id' ] == $field[ 'id' ] ) {
$related_field_id = $field[ 'id' ];
break;
}
}
}
}
// Delete existing sister relationships
if ( !empty( $related_field_id ) && !empty( $related_pod_id ) && in_array( $related_field_id, $rel_field_ids ) ) {
pods_query( "DELETE FROM `@wp_pods_rel` WHERE `pod_id` = {$related_pod_id} AND `field_id` = {$related_field_id} AND `related_pod_id` = {$params->pod_id} AND `related_field_id` = {$field_id} AND `related_item_id` = {$params->id}", $this );
}
if ( empty( $values ) )
continue;
// Add relationship values
$weight = 0;
foreach ( $values as $id ) {
if ( !empty( $related_pod_id ) && !empty( $related_field_id ) ) {
$related_weight = 0;
$result = pods_query( "SELECT `weight` FROM `@wp_pods_rel` WHERE `pod_id` = {$related_pod_id} AND `field_id` = {$related_field_id} ORDER BY `weight` DESC LIMIT 1", $this );
if ( !empty( $result ) )
$related_weight = pods_absint( $result[ 0 ]->weight ) + 1;
pods_query( "INSERT INTO `@wp_pods_rel` (`pod_id`, `field_id`, `item_id`, `related_pod_id`, `related_field_id`, `related_item_id`, `weight`) VALUES ({$related_pod_id}, {$related_field_id}, {$id}, {$params->pod_id}, {$field_id}, {$params->id}, {$related_weight}", 'Cannot add sister relationship' );
}
pods_query( "INSERT INTO `@wp_pods_rel` (`pod_id`, `field_id`, `item_id`, `related_pod_id`, `related_field_id`, `related_item_id`, `weight`) VALUES ({$params->pod_id}, {$field_id}, {$params->id}, {$related_pod_id}, {$related_field_id}, {$id}, {$weight})", 'Cannot add relationship' );
$weight++;
}
}
}
}
}
if ( false === $bypass_helpers ) {
// Plugin hook