-
-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathgeneral.php
More file actions
5386 lines (4589 loc) · 148 KB
/
Copy pathgeneral.php
File metadata and controls
5386 lines (4589 loc) · 148 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
// Don't load directly.
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}
/**
* @package Pods\Global\Functions\General
*/
use Pods\Admin\Settings;
use Pods\API\Whatsit\Value_Field;
use Pods\Config_Handler;
use Pods\Container\Container_DI52;
use Pods\Permissions;
use Pods\Data\Map_Field_Values;
use Pods\Whatsit;
use Pods\Whatsit\Field;
use Pods\Whatsit\Object_Field;
use Pods\Whatsit\Pod;
use Pods\Whatsit\Store;
/**
* Standardize queries and error reporting. It replaces @wp_ with $wpdb->prefix.
*
* @see PodsData::query
*
* @param string|array $sql The SQL query or an array with the SQL query and the values to prepare.
* @param string $error (optional) The failure message to use for Database errors.
* @param string $results_error (optional) Throw an error if a records are found.
* @param string $no_results_error (optional) Throw an error if no records are found.
*
* @return array|bool|mixed|null|void
* @since 2.0.0
*/
function pods_query( $sql, $error = 'Database Error', $results_error = null, $no_results_error = null ) {
try {
$podsdata = pods_data( null, null, true, false );
} catch ( Exception $exception ) {
return null;
}
// If the $error is the $prepare array, set the $error to the default message.
if ( is_array( $error ) ) {
if ( ! is_array( $sql ) ) {
$sql = [ $sql, $error ];
}
$error = 'Database Error';
}
if ( is_array( $sql ) ) {
$sql = array_values( $sql );
$sql[0] = apply_filters( 'pods_query_sql', $sql[0], $error, $results_error, $no_results_error );
$sql[0] = $podsdata->get_sql( $sql[0] );
} else {
$sql = apply_filters( 'pods_query_sql', $sql, $error, $results_error, $no_results_error );
$sql = $podsdata->get_sql( $sql );
}
if ( 1 === (int) pods_v( 'pods_debug_sql_all' ) && is_user_logged_in() && pods_is_admin( [ 'pods' ] ) ) {
$debug_sql = $sql;
if ( is_array( $debug_sql ) ) {
$debug_sql = print_r( $debug_sql, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
}
echo '<textarea cols="100" rows="24">';
echo esc_textarea( $debug_sql );
echo '</textarea>';
}
return PodsData::query( $sql, $error, $results_error, $no_results_error );
}
/**
* Prepare and run the query.
*
* @since 2.8.22
*
* @see PodsData::query
*
* @param string $sql SQL Query
* @param array $prepare Variables to prepare for the SQL query.
* @param string $error (optional) The failure message
* @param string $results_error (optional) Throw an error if a records are found
* @param string $no_results_error (optional) Throw an error if no records are found
*
* @return array|bool|mixed|null|void
*/
function pods_query_prepare( $sql, $prepare, $error = 'Database Error', $results_error = null, $no_results_error = null ) {
return pods_query( [ $sql, $prepare ], $error, $results_error, $no_results_error );
}
/**
* Standardize filters / actions
*
* @param string $scope Scope of the filter / action (ui for PodsUI, api for PodsAPI, etc..)
* @param string $name Name of filter / action to run
* @param mixed $args (optional) Arguments to send to filter / action
* @param object $obj (optional) Object to reference for filter / action
*
* @return mixed
* @since 2.0.0
* @todo Need to figure out how to handle $scope = 'pods' for the Pods class
*/
function pods_do_hook( $scope, $name, $args = null, $obj = null ) {
// Add filter name
array_unshift( $args, "pods_{$scope}_{$name}" );
// Add object
$args[] = $obj;
// Run apply_filters and give it all the arguments
$args = call_user_func_array( 'apply_filters', $args );
return $args;
}
/**
* Message / Notice handling for Admin UI
*
* @param string $message The notice / error message shown.
* @param string $type The message type (success, info, warning, error, or nag).
* @param bool $return Whether to return the message.
* @param bool $is_dismissible Whether the notice is dismissible.
*
* @return string|null The message or null if not returning.
*/
function pods_message( $message, $type = null, $return = false, $is_dismissible = true ) {
$message = (string) $message;
$supported_notice_types = [
'success',
'info',
'warning',
'error',
];
// The "nag" type will be treated as "info", but we want to check to see if the constant has it turned off.
if ( 'nag' === $type && defined( 'DISABLE_NAG_NOTICES' ) && DISABLE_NAG_NOTICES ) {
if ( $return ) {
return '';
}
return null;
}
if ( empty( $type ) || ! in_array( $type, $supported_notice_types, true ) ) {
$type = 'info';
}
// Maybe handle WP-CLI messages.
if ( defined( 'WP_CLI' ) ) {
if ( 'error' === $type ) {
WP_CLI::warning( $message );
} else {
WP_CLI::line( $message );
}
if ( $return ) {
return '';
}
return null;
}
// Maybe wrap the message.
if ( false === strpos( $message, '</p>' ) ) {
$message = '<p>' . $message . '</p>';
}
$div_id = 'message';
$div_classes = [
'notice',
'notice-' . $type,
];
if ( $is_dismissible ) {
$div_classes[] = 'is-dismissible';
}
$div_classes = array_filter( $div_classes );
// Maybe prefix the id/classes on frontend.
if ( ! is_admin() ) {
$div_classes = array_map(
static function ( $class_name ) {
return 'pods-ui-notice-' . $class_name;
},
$div_classes
);
$div_classes[] = 'pods-ui-notice-front';
// No ID here since there may be multiple on the page.
$div_attr_id = '';
wp_enqueue_style( 'pods-form' );
$div_id = '';
} else {
$div_attr_id = 'id="' . esc_attr( $div_id ) . '"';
}
$div_classes = implode( ' ', $div_classes );
if ( pods_render_is_in_block() ) {
$attrs = ' ' . get_block_wrapper_attributes(
array_filter(
[
'id' => $div_id,
'class' => 'pods-ui-notice ' . esc_attr( $div_classes ),
]
)
);
} else {
$attrs = $div_attr_id . ' class="pods-ui-notice ' . esc_attr( $div_classes ) . '"';
}
$html = '<div ' . $attrs . '>' . wp_kses_post( $message ) . '</div>';
if ( $return ) {
return $html;
}
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return null;
}
$GLOBALS['pods_errors'] = [];
/**
* The default exception handler for Pods errors.
*
* @since 2.9.4
*
* @param string|array $error The error message(s) to be thrown / displayed.
*/
function pods_error_exception( $error ) {
pods_error( $error, 'final_exception' );
}
/**
* Error Handling which throws / displays errors
*
* @param string|array $error The error message(s) to be thrown / displayed.
* @param object|boolean|null $obj If $obj->display_errors is set and is set to true it will display errors,
* if boolean and is set to true it will display errors.
*
* @return mixed
*
* @throws Exception Throws exception for developer-oriented error handling.
*
* @since 2.0.0
*/
function pods_error( $error, $obj = null ) {
global $pods_errors;
$display_errors = $obj;
if ( is_object( $obj ) && isset( $obj->display_errors ) ) {
$display_errors = $obj->display_errors;
}
$error_mode = 'exception';
if ( true === $display_errors ) {
$error_mode = 'exit';
} elseif ( false === $display_errors ) {
$error_mode = 'exception';
} elseif ( is_string( $display_errors ) ) {
$error_mode = $display_errors;
}
if ( is_object( $error ) && $error instanceof Exception ) {
$error_mode = 'exception';
if ( 'final_exception' === $display_errors ) {
$error_mode = 'exit';
}
/** @var Exception $error */
$error = $error->getMessage();
}
/**
* @var string $error_mode Throw an exception, exit with the message, return false, or return WP_Error
*/
if ( ! in_array( $error_mode, [ 'exception', 'exit', 'false', 'wp_error', 'json' ], true ) ) {
$error_mode = 'exception';
}
/**
* When running a Pods shortcode, never exit and only return exception.
*/
if ( pods_doing_shortcode() ) {
$error_mode = 'exception';
} elseif ( pods_doing_json() ) {
$error_mode = 'json';
}
/**
* Filter the error mode used by pods_error.
*
* @param string $error_mode Error mode
* @param string|array $error Error message(s)
* @param object|boolean|string|null $obj
*/
$error_mode = apply_filters( 'pods_error_mode', $error_mode, $error, $obj );
if ( is_array( $error ) ) {
$error = array_map( 'wp_kses_post', $error );
if ( 1 === count( $error ) ) {
$error = current( $error );
// Create WP_Error for use later.
$wp_error = new WP_Error( 'pods-error-' . md5( $error ), $error );
} else {
// Create WP_Error for use later.
$wp_error = new WP_Error();
foreach ( $error as $error_message ) {
$wp_error->add( 'pods-error-' . md5( $error_message ), $error_message );
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
$error = __( 'The following issue occurred:', 'pods' ) . "\n\n- " . implode( "\n- ", $error );
} else {
$error = __( 'The following issues occurred:', 'pods' ) . "\n<ul><li>" . implode( "</li>\n<li>", $error ) . '</li></ul>';
}
}
} else {
if ( is_object( $error ) ) {
$error = __( 'An unknown error has occurred', 'pods' );
}
$error = wp_kses_post( $error );
// Create WP_Error for use later.
$wp_error = new WP_Error( 'pods-error-' . md5( $error ), $error );
}//end if
$pods_errors = [];
// Support testing debug messages.
if ( function_exists( 'codecept_debug' ) ) {
codecept_debug( 'Pods Debug Error: ' . $error );
pods_debug( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
}
if ( ! empty( $error ) ) {
if ( 'exception' === $error_mode ) {
$exception_bypass = apply_filters( 'pods_error_exception', null, $error );
if ( null !== $exception_bypass ) {
return $exception_bypass;
}
$pods_errors = $error;
/**
* Allow filtering whether the fallback is enabled to catch uncaught exceptions.
*
* @since 2.8.0
*
* @param bool $exception_fallback_enabled Whether the fallback is enabled to catch uncaught exceptions.
* @param string $error The error information.
*/
$exception_fallback_enabled = apply_filters( 'pods_error_exception_fallback_enabled', true, $error );
if ( $exception_fallback_enabled ) {
set_exception_handler( 'pods_error_exception' );
}
throw new Exception( wp_kses_post( $error ) );
} elseif ( 'exit' === $error_mode ) {
$die_bypass = apply_filters( 'pods_error_die', null, $error );
if ( null !== $die_bypass ) {
return $die_bypass;
}
// die with error
if ( ! defined( 'DOING_AJAX' ) && ! headers_sent() && ( is_admin() || false !== strpos( (string) $_SERVER['REQUEST_URI'], 'wp-comments-post.php' ) ) ) {
wp_die( wp_kses_post( $error ), '', [ 'back_link' => true ] );
} else {
die( wp_kses_post( sprintf( '<e>%s</e>', $error ) ) );
}
} elseif ( 'wp_error' === $error_mode ) {
return $wp_error;
} elseif ( 'json' === $error_mode ) {
$meta_box_loader_compat = (int) pods_v( 'meta-box-loader', 'request', 0 );
// Check if this is a back-compat meta box save request.
if ( 1 === $meta_box_loader_compat ) {
// Do not block this page.
error_log( 'Pods Meta Save Error:' . $error ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
} else {
wp_send_json( [
'message' => $error,
], 500 );
}
}//end if
}//end if
return false;
}
/**
* Get the last known timing difference.
*
* @since 2.9.10
*
* @return float The last known timing difference.
*/
function pods_get_timing() {
static $timer;
$now = microtime( true );
if ( ! $timer ) {
$timer = $now;
}
$last_diff = $now - $timer;
$timer = $now;
return $last_diff;
}
/**
* Get the last known timing difference as text.
*
* @since 2.9.10
*
* @return string The last known timing difference as text.
*/
function pods_get_debug_timing() {
return '[debug timing: ' . number_format( pods_get_timing(), 4 ) . 's]';
}
/**
* Debug variable used in pods_debug to count the instances debug is used
*/
global $pods_debug;
$pods_debug = 0;
/**
* Output information about a variable using var_dump() to save a few lines and is compatible with Xdebug and WP-CLI.
*
* @since 2.0.0
*
* @param mixed $debug The error message to be thrown / displayed.
* @param bool $die If set to true, a die() will occur, if set to (int) 2 then a wp_die() will occur.
* @param string $prefix Extra information to output above the debug information.
*/
function pods_debug( $debug = '_null', $die = false, $prefix = '_null' ) {
global $pods_debug;
$pods_debug ++;
if ( function_exists( 'codecept_debug' ) ) {
if ( $debug instanceof Exception ) {
$debug = 'Exception bypassed: [' . $debug->getCode() . '] ' . $debug->getMessage();
} elseif ( ! is_string( $debug ) ) {
$debug = wp_json_encode( $debug, JSON_PRETTY_PRINT );
}
if ( '_null' === $prefix ) {
$prefix = 'Pods Debug';
}
codecept_debug( $prefix . ': ' . $debug . ' ' . pods_get_debug_timing() );
return;
}
if ( defined( 'WP_CLI' ) ) {
if ( ! is_string( $debug ) ) {
$debug = wp_json_encode( $debug, JSON_PRETTY_PRINT );
}
if ( '_null' === $prefix ) {
$prefix = 'Pods Debug';
}
WP_CLI::debug( $prefix . ': ' . $debug );
return;
}
ob_start();
if ( '_null' !== $prefix ) {
var_dump( $prefix ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump
}
$debug_line_number = 0;
if ( '_null' !== $debug ) {
var_dump( $debug ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump
$debug_line_number = __LINE__ - 2;
} else {
var_dump( 'Pods Debug #' . $pods_debug . ' ' . pods_get_debug_timing() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump
$debug_line_number = __LINE__ - 2;
}
$debug_line_check = sprintf(
'<small>%s:%s:</small>',
__FILE__,
$debug_line_number
);
$debug = ob_get_clean();
if ( false === strpos( (string) $debug, "<pre class='xdebug-var-dump'" ) ) {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
$debug = esc_html( $debug );
}
$debug = '<pre>' . $debug . '</pre>';
} elseif ( false !== strpos( (string) $debug, $debug_line_check ) ) {
// Attempt to replace the backtrace file/line from our var_dump() above with where the pods_debug() itself was called.
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
if ( ! empty( $backtrace[0] ) ) {
$debug_line_replace = sprintf(
'<small>%s:%s:</small>',
$backtrace[0]['file'],
$backtrace[0]['line']
);
$debug = str_replace( $debug_line_check, $debug_line_replace, $debug );
}
}
$debug = '<e>' . wp_kses_post( $debug );
if ( 2 === $die ) {
wp_die( $debug, '', [ 'back_link' => true ] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} elseif ( true === $die ) {
die( $debug ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo $debug; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Log debug information if WP_DEBUG_LOG is used.
*
* @since 2.9.12
*
* @param mixed $debug The error message to be thrown / displayed.
*/
function pods_debug_log( $debug ) {
if ( function_exists( 'codecept_debug' ) || defined( 'WP_CLI' ) ) {
pods_debug( $debug, false, 'Output from pods_debug_log()' );
return;
}
if ( pods_is_debug_logging_enabled() ) {
$debug_backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
// 0 = This function
// 1 = The place this function was called.
$function = $debug_backtrace[1]['function'] ?? '';
if ( ! empty( $debug_backtrace[1]['class'] ) ) {
$function = $debug_backtrace[1]['class'] . '::' . $function;
}
// Debug backtrace will set the line for the call on the next trace up the chain.
$line = $debug_backtrace[0]['line']; // Yes, this should be a 0 instead of a 1 here.
pods_debug_log_data( $debug, 'general', $function, $line );
}
if ( in_array( strtolower( (string) WP_DEBUG_LOG ), [ 'true', '1' ], true ) ) {
$log_path = WP_CONTENT_DIR . '/debug.log';
} elseif ( is_string( WP_DEBUG_LOG ) ) {
$log_path = WP_DEBUG_LOG;
} else {
return;
}
ob_start();
var_dump( $debug ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump
$debug_line_number = __LINE__ - 2;
$debug_line_check = sprintf(
'<small>%s:%s:</small>',
__FILE__,
$debug_line_number
);
$debug = ob_get_clean();
if ( false === strpos( (string) $debug, "<pre class='xdebug-var-dump'" ) ) {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
$debug = esc_html( $debug );
}
} elseif ( false !== strpos( (string) $debug, $debug_line_check ) ) {
// Attempt to replace the backtrace file/line from our var_dump() above with where the pods_debug() itself was called.
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
if ( ! empty( $backtrace[0] ) ) {
$debug_line_replace = sprintf(
'<small>%s:%s:</small>',
$backtrace[0]['file'],
$backtrace[0]['line']
);
$debug = str_replace( $debug_line_check, $debug_line_replace, $debug );
}
}
$debug = 'pods_debug_log: ' . $debug;
// Log the debug line.
error_log( $debug, 0, $log_path ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
/**
* Check if debug is enabled and should be displayed.
*
* @return bool
* @since 2.7.13
*/
function pods_is_debug_display() {
return ( WP_DEBUG && WP_DEBUG_DISPLAY );
}
/**
* Determine if user has admin access
*
* @since 2.3.5
*
* @param string|array $capabilities Additional capabilities to check
*
* @return bool Whether user has admin access
*/
function pods_is_admin( $capabilities = null ) {
// Normalize the capability we are checking.
if ( empty( $capabilities ) ) {
$capabilities = [];
} else {
$capabilities = (array) $capabilities;
}
if ( is_user_logged_in() ) {
// Check if on multisite and this is a super admin.
if ( is_multisite() && is_super_admin() ) {
return apply_filters( 'pods_is_admin', true, $capabilities, '_super_admin' );
}
// Get all the capabilities including Pods Admin capabilities.
$capabilities = pods_get_admin_capabilities( $capabilities );
// Check if the user has access to any of the capabilities.
foreach ( $capabilities as $capability ) {
if ( current_user_can( $capability ) ) {
return apply_filters( 'pods_is_admin', true, $capabilities, $capability );
}
}
}
return apply_filters( 'pods_is_admin', false, $capabilities, null );
}
/**
* Determine if a specific user has admin access.
*
* @since 3.0.0
*
* @param string|array $capabilities Additional capabilities to check.
*
* @return bool Whether user has admin access.
*/
function pods_is_user_admin( int $user_id, $capabilities = null ): bool {
// Invalid user.
if ( $user_id < 1 ) {
return false;
}
// Normalize the capability we are checking.
if ( empty( $capabilities ) ) {
$capabilities = [];
} else {
$capabilities = (array) $capabilities;
}
// Check if on multisite and this is a super admin.
if ( is_multisite() && is_super_admin( $user_id ) ) {
return apply_filters( 'pods_is_user_admin', true, $user_id, $capabilities, '_super_admin' );
}
// Get all the capabilities including Pods Admin capabilities.
$capabilities = pods_get_admin_capabilities( $capabilities );
// Check if the user exists.
$user = get_userdata( $user_id );
if ( ! $user instanceof WP_User ) {
return false;
}
// Check if the user has access to any of the capabilities.
foreach ( $capabilities as $capability ) {
if ( user_can( $user, $capability ) ) {
return apply_filters( 'pods_is_user_admin', true, $user_id, $capabilities, $capability );
}
}
return apply_filters( 'pods_is_user_admin', false, $user_id, $capabilities, null );
}
/**
* Get the list of Pods Admin capabilities.
*
* @since 3.0.0
*
* @param string|array $capabilities Additional capabilities to merge and include.
*
* @return array The list of Pods Admin capabilities.
*/
function pods_get_admin_capabilities( array $other_capabilities = [] ): array {
$pods_admin_capabilities = [];
if ( ! is_multisite() ) {
// Default is_super_admin() checks against this capability.
$pods_admin_capabilities[] = 'delete_users';
}
$pods_admin_capabilities = (array) apply_filters( 'pods_admin_capabilities', $pods_admin_capabilities, $other_capabilities );
return array_unique( array_filter( array_merge( $pods_admin_capabilities, $other_capabilities ) ) );
}
/**
* Determine if Developer Mode is enabled
*
* @return bool Whether Developer Mode is enabled
*
* @since 2.3.0
*/
function pods_developer() {
if ( defined( 'PODS_DEVELOPER' ) && PODS_DEVELOPER ) {
return true;
}
return false;
}
/**
* Determine if Tableless Mode is enabled
*
* @return bool Whether Tableless Mode is enabled
*
* @since 2.3.0
*/
function pods_tableless() {
if ( defined( 'PODS_TABLELESS' ) && PODS_TABLELESS ) {
return true;
}
return false;
}
/**
* Determine whether the wp_podsrel table is enabled.
*
* @since 2.8.0
*
* @param null|Field $field The field object.
* @param null|string $context The context of the podsrel check (lookup/save).
*
* @return bool Whether the wp_podsrel table is enabled.
*/
function pods_podsrel_enabled( $field = null, $context = null ) {
// Disabled when Pods Tableless mode is on.
if ( pods_tableless() ) {
return false;
}
/**
* Allow filtering of whether the wp_podsrel table is enabled.
*
* @since 2.8.0
*
* @param bool $enabled Whether the wp_podsrel table is enabled.
* @param null|Field $field The field object.
* @param null|string $context The context of the podsrel check (lookup/save).
*/
return (bool) apply_filters( 'pods_podsrel_enabled', true, $field, $context );
}
/**
* Determine whether relationship meta storage is enabled.
*
* @since 2.8.0
*
* @param null|array|Field $field The field object.
* @param null|array|Pod $pod The pod object.
*
* @return bool Whether relationship meta storage is enabled.
*/
function pods_relationship_meta_storage_enabled( $field = null, $pod = null ) {
/**
* Allow filtering of whether relationship meta storage is enabled.
*
* @since 2.8.0
*
* @param bool $enabled Whether relationship meta storage table is enabled.
* @param null|array|Field $field The field object.
* @param null|array|Pod $pod The pod object.
*/
return (bool) apply_filters( 'pods_relationship_meta_storage_enabled', true, $field, $pod );
}
/**
* Determine whether relationship meta storage is enabled for simple relationships.
*
* @since 2.8.9
*
* @param null|array|Field $field The field object.
* @param null|array|Pod $pod The pod object.
*
* @return bool Whether relationship meta storage is enabled.
*/
function pods_relationship_meta_storage_enabled_for_simple_relationships( $field = null, $pod = null ) {
$enabled = null === $pod || 'meta' === $pod['storage'] || 'settings' === $pod['storage'];
/**
* Allow filtering of whether relationship meta storage is enabled for simple relationships.
*
* @since 2.8.9
*
* @param bool $enabled Whether relationship meta storage table is enabled for simple relationships.
* @param null|array|Field $field The field object.
* @param null|array|Pod $pod The pod object.
*/
return (bool) apply_filters( 'pods_relationship_meta_storage_enabled_for_simple_relationships', $enabled, $field, $pod );
}
/**
* Determine whether relationship table storage is enabled for simple relationships.
*
* @since 2.8.9
*
* @param null|array|Field $field The field object.
* @param null|array|Pod $pod The pod object.
*
* @return bool Whether relationship table storage is enabled.
*/
function pods_relationship_table_storage_enabled_for_simple_relationships( $field = null, $pod = null ) {
$enabled = null === $pod || 'table' === $pod['storage'];
/**
* Allow filtering of whether relationship table storage is enabled for simple relationships.
*
* @since 2.8.9
*
* @param bool $enabled Whether relationship table storage table is enabled for simple relationships.
* @param null|array|Field $field The field object.
* @param null|array|Pod $pod The pod object.
*/
return (bool) apply_filters( 'pods_relationship_table_storage_enabled_for_simple_relationships', $enabled, $field, $pod );
}
/**
* Determine whether relationship table storage is enabled for object based relationships.
*
* @since 2.8.16
*
* @param null|array|Field $field The field object.
* @param null|array|Pod $pod The pod object.
*
* @return bool Whether relationship table storage is enabled.
*/
function pods_relationship_table_storage_enabled_for_object_relationships( $field = null, $pod = null ) {
/**
* Allow filtering of whether relationship table storage is enabled for object based relationships.
*
* @since 2.8.16
*
* @param bool $enabled Whether relationship table storage table is enabled for object based relationships.
* @param null|array|Field $field The field object.
* @param null|array|Pod $pod The pod object.
*/
return (bool) apply_filters( 'pods_relationship_table_storage_enabled_for_object_relationships', false, $field, $pod );
}
/**
* Determine if Light Mode is enabled
*
* @return bool Whether Light Mode is enabled
*
* @since 2.7.13
*/
function pods_light() {
if ( defined( 'PODS_LIGHT' ) && PODS_LIGHT ) {
return true;
}
return false;
}
/**
* Determine whether Pods is in a demo from pods.io or not.
*
* @return bool Whether Pods is in a demo.
*
* @since 2.9.12
*/
function pods_is_demo() {
return (
1 === (int) pods_v( 'pods_wasm_demo' )
|| 1 === (int) pods_v( 'pods_demo' )
);
}
/**
* Determine if Strict Mode is enabled
*
* @param bool $include_debug Whether to include WP_DEBUG in strictness level
*
* @return bool Whether Strict Mode is enabled
*
* @since 2.3.5
*/
function pods_strict( $include_debug = true ) {
$strict = false;
if ( defined( 'PODS_STRICT' ) && PODS_STRICT ) {
// Deprecated PODS_STRICT_MODE since 2.3.5
$strict = true;
} elseif ( defined( 'PODS_STRICT_MODE' ) && PODS_STRICT_MODE && pods_allow_deprecated( false ) ) {
$strict = true;
} elseif ( $include_debug && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$strict = true;
}
/**
* Allow filtering of whether strict mode is enabled.
*
* @param boolean $strict Whether strict mode is enabled.
*
* @since 2.8.0
*/
return apply_filters( 'pods_strict_mode', $strict );
}
/**
* Determine if Deprecated Mode is enabled
*
* @param bool $include_debug Whether to include strict mode
*
* @return bool Whether Deprecated Mode is enabled
*
* @since 2.3.10
*/
function pods_allow_deprecated( $strict = true ) {
if ( $strict && pods_strict( false ) ) {
return false;
} elseif ( ! defined( 'PODS_DEPRECATED' ) || PODS_DEPRECATED ) {
return true;
}
return false;
}
/**
* Determine if Pods API Caching is enabled
*
* @return bool Whether Pods API Caching is enabled
*
* @since 2.3.9
*/
function pods_api_cache() {
if ( defined( 'PODS_API_CACHE' ) && ! PODS_API_CACHE ) {
return false;
}
/**
* Filter whether to use the Pods API cache.
*
* @param boolean $use_cache Whether to use the Pods API cache.
*
* @since 2.8.0