PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.1.4.3
Pods – Custom Content Types and Fields v3.1.4.3
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / includes / access.php
pods / includes Last commit date
compatibility 2 days ago access.php 2 days ago classes.php 2 days ago compatibility.php 2 days ago data.php 2 days ago forms.php 2 days ago general.php 2 days ago media.php 2 days ago
access.php
3073 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 /**
9 * @package Pods\Global\Functions\Access
10 */
11
12 use Pods\Whatsit\Pod;
13
14 /**
15 * Normalize Pod information with a Pods object or object info.
16 *
17 * @since 3.1.0
18 *
19 * @param array $args {
20 * The arguments to use.
21 *
22 * @type string|null $object_type The object type.
23 * @type string|null $object_name The object name.
24 * @type int|string|null $item_id The item ID.
25 * @type Pods|null $pods The Pods object.
26 * @type Pod|null $pod The Pod object.
27 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
28 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
29 * }
30 *
31 * @return array {
32 * The arguments to use.
33 *
34 * @type string|null $object_type The object type (if set).
35 * @type string|null $object_name The object name (if set).
36 * @type int|string|null $item_id The item ID (if set).
37 * @type Pods|null $pods The Pods object (if built or provided).
38 * @type Pod|null $pod The Pod object (if built or provided).
39 * }
40 */
41 function pods_info_from_args( $args ) {
42 $info = array(
43 'object_type' => null,
44 'object_name' => null,
45 'item_id' => null,
46 'pods' => null,
47 'pod' => null,
48 );
49
50 $build_pods = false;
51 $build_pod = false;
52
53 if ( isset( $args['build_pods'] ) ) {
54 $build_pods = $args['build_pods'];
55
56 unset( $args['build_pods'] );
57 }
58
59 if ( isset( $args['build_pod'] ) ) {
60 $build_pod = $args['build_pod'];
61
62 unset( $args['build_pod'] );
63 }
64
65 // Merge in the args with the defaults.
66 $info = array_merge( $info, $args );
67
68 $object_type_set = null !== $info['object_type'];
69 $object_name_set = null !== $info['object_name'];
70
71 // Maybe auto-set the object name from the type if we can.
72 if (
73 $object_type_set
74 && ! $object_name_set
75 && in_array( $info['object_type'], array( 'comment', 'media', 'user' ), true )
76 ) {
77 $info['object_name'] = $info['object_type'];
78
79 $object_name_set = true;
80 }
81
82 // Normalize the Pods info to null if it's not valid.
83 if (
84 $info['pods'] instanceof Pods
85 && ! $info['pods']->is_valid()
86 ) {
87 $info['pods'] = null;
88 }
89
90 // Maybe build the Pods object from the info.
91 if (
92 $build_pods
93 && $object_name_set
94 && ! $info['pods'] instanceof Pods
95 ) {
96 $pods = pods_get_instance( $info['object_name'], $info['item_id'], true );
97
98 if (
99 $pods instanceof Pods
100 && $pods->is_valid()
101 && (
102 empty( $info['object_type'] )
103 || $info['object_type'] === $pods->pod_data->get_type()
104 )
105 ) {
106 $info['pods'] = $pods;
107
108 if ( ! $info['pod'] instanceof Pod ) {
109 $info['pod'] = clone $pods->pod_data;
110 }
111 }
112 } elseif (
113 $info['pods'] instanceof Pods
114 && $info['pods']->is_valid()
115 && ! $info['pod'] instanceof Pod
116 ) {
117 $info['pod'] = clone $info['pods']->pod_data;
118 }
119
120 // Maybe build the Pod object from the info.
121 if (
122 $build_pod
123 && $object_name_set
124 && ! $info['pod'] instanceof Pod
125 ) {
126 try {
127 $pod = pods_api()->load_pod( array(
128 'name' => $info['object_name'],
129 ) );
130 } catch ( Exception $e ) {
131 $pod = null;
132 }
133
134 if (
135 $pod instanceof Pod
136 && (
137 empty( $info['object_type'] )
138 || $info['object_type'] === $pod->get_type()
139 )
140 ) {
141 $info['pod'] = $pod;
142 }
143 }
144
145 if ( $info['pod'] instanceof Pod ) {
146 $info['object_type'] = $info['pod']->get_type();
147 $info['object_name'] = $info['pod']->get_name();
148 }
149
150 return $info;
151 }
152
153 /**
154 * Determine whether the current user has access to an object.
155 *
156 * @since 3.1.0
157 *
158 * @param array $args {
159 * The arguments to use.
160 *
161 * @type string|null $object_type The object type.
162 * @type string|null $object_name The object name.
163 * @type int|string|null $item_id The item ID.
164 * @type Pods|null $pods The Pods object.
165 * @type Pod|null $pod The Pod object.
166 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
167 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
168 * }
169 * @param int|null $user_id The user ID to check against, set to 0 or null for anonymous access check.
170 * @param string $access_type The type of access to check for (read, add, edit, delete).
171 * @param string|null $context The unique slug that can be referenced by hooks for context.
172 *
173 * @return bool Whether the current user has access to an object.
174 */
175 function pods_user_can_access_object( $args, $user_id, $access_type = 'edit', $context = null ) {
176 $info = pods_info_from_args( $args );
177
178 if ( null === $user_id ) {
179 $user_id = 0;
180 }
181
182 // Check if the user exists.
183 $user = get_userdata( $user_id );
184
185 if ( ! $user instanceof WP_User ) {
186 // If the user does not exist and it was not anonymous, do not allow access to an invalid user.
187 if ( 0 < $user_id ) {
188 return false;
189 }
190
191 // If the user was 0 to begin with (anonymous) then set up a user object to work with.
192 $user = new WP_User();
193 }
194
195 // Determine if this is a user in WP that has full access.
196 if ( $user_id && pods_is_user_admin( $user_id ) ) {
197 return true;
198 }
199
200 if ( 'pod' === $info['object_type'] || 'table' === $info['object_type'] ) {
201 // If no object name is provided, we cannot check access.
202 if ( empty( $info['object_name'] ) ) {
203 return false;
204 }
205
206 // Determine if this user has full content access.
207 if ( $user->has_cap( 'pods_content' ) ) {
208 return true;
209 }
210 }
211
212 $capabilities = pods_access_map_capabilities( $info, $user_id );
213
214 // Unsupported capabilities returned.
215 if ( null === $capabilities ) {
216 return false;
217 }
218
219 /**
220 * Allow filtering the list of capabilities used for checking access against an object.
221 *
222 * @since 3.1.0
223 *
224 * @param array $capabilities The list of capabilities used for checking access against an object.
225 * @param int $user_id The user ID to check against.
226 * @param array $info {
227 * The normalized Pod information referenced.
228 *
229 * @type string|null $object_type The object type (if set).
230 * @type string|null $object_name The object name (if set).
231 * @type int|string|null $item_id The item ID (if set).
232 * @type Pods|null $pods The Pods object (if built or provided).
233 * @type Pod|null $pod The Pod object (if built or provided).
234 * }
235 * @param string $access_type The type of access to check for (read, add, edit, delete).
236 * @param string|null $context The unique slug that can be referenced by hooks for context.
237 */
238 $capabilities = (array) apply_filters(
239 'pods_user_can_access_object_get_capabilities',
240 $capabilities,
241 $user_id,
242 $info,
243 $access_type,
244 $context
245 );
246
247 // No capability mapped, do not allow access.
248 if ( ! array_key_exists( $access_type, $capabilities ) ) {
249 return false;
250 }
251
252 /**
253 * Allow filtering whether a user has access to an object before the normal capability check runs.
254 *
255 * @since 3.1.0
256 *
257 * @param null|bool $can_access Whether a user has access to an object (return null to run normal check).
258 * @param int $user_id The user ID to check against.
259 * @param array $info {
260 * The normalized Pod information referenced.
261 *
262 * @type string|null $object_type The object type (if set).
263 * @type string|null $object_name The object name (if set).
264 * @type int|string|null $item_id The item ID (if set).
265 * @type Pods|null $pods The Pods object (if built or provided).
266 * @type Pod|null $pod The Pod object (if built or provided).
267 * }
268 * @param string $access_type The type of access to check for (read, add, edit, delete).
269 * @param string|null $context The unique slug that can be referenced by hooks for context.
270 * @param array $capabilities The list of capabilities used for checking access against an object.
271 */
272 $can_access = apply_filters(
273 'pods_user_can_access_object_pre_check',
274 null,
275 $user_id,
276 $info,
277 $access_type,
278 $context,
279 $capabilities
280 );
281
282 // Check for access override and return that instead.
283 if ( null !== $can_access ) {
284 return $can_access;
285 }
286
287 // If we are allowing all access, null will be set for the capability.
288 if ( null === $capabilities[ $access_type ] ) {
289 $can_access = true;
290 } else {
291 // Support multiple capability checks ("OR" logic).
292 $capabilities[ $access_type ] = (array) $capabilities[ $access_type ];
293
294 $can_access = false;
295
296 foreach ( $capabilities[ $access_type ] as $capability ) {
297 if ( $info['item_id'] ) {
298 $can_access = $user->has_cap( $capability, $info['item_id'] );
299 } else {
300 $can_access = $user->has_cap( $capability );
301 }
302
303 if ( $can_access ) {
304 break;
305 }
306 }
307 }
308
309 $is_read_access = 'read' === $access_type;
310
311 // Check for password-protected post.
312 if (
313 $can_access
314 && 'post_type' === $info['object_type']
315 && $info['item_id']
316 && (
317 (
318 $is_read_access
319 && pods_access_bypass_post_with_password( $info )
320 )
321 || (
322 ! $is_read_access
323 && post_password_required( $info['item_id'] )
324 )
325 )
326 ) {
327 $can_access = false;
328 }
329
330 /**
331 * Allow filtering whether a user has access to an object after the normal capability check runs.
332 *
333 * @since 3.1.0
334 *
335 * @param bool $can_access Whether a user has access to an object.
336 * @param int $user_id The user ID to check against.
337 * @param array $info {
338 * The normalized Pod information referenced.
339 *
340 * @type string|null $object_type The object type (if set).
341 * @type string|null $object_name The object name (if set).
342 * @type int|string|null $item_id The item ID (if set).
343 * @type Pods|null $pods The Pods object (if built or provided).
344 * @type Pod|null $pod The Pod object (if built or provided).
345 * }
346 * @param string $access_type The type of access to check for (read, add, edit, delete).
347 * @param string|null $context The unique slug that can be referenced by hooks for context.
348 * @param array $capabilities The list of capabilities used for checking access against an object.
349 */
350 return (bool) apply_filters(
351 'pods_user_can_access_object',
352 $can_access,
353 $user_id,
354 $info,
355 $access_type,
356 $context,
357 $capabilities
358 );
359 }
360
361 /**
362 * Determine whether the current user has access to an object.
363 *
364 * @since 3.1.0
365 *
366 * @param array $args {
367 * The arguments to use.
368 *
369 * @type string|null $object_type The object type.
370 * @type string|null $object_name The object name.
371 * @type int|string|null $item_id The item ID.
372 * @type Pods|null $pods The Pods object.
373 * @type Pod|null $pod The Pod object.
374 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
375 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
376 * }
377 * @param string $access_type The type of access to check for (read, add, edit, delete).
378 * @param string|null $context The unique slug that can be referenced by hooks for context.
379 *
380 * @return bool Whether the current user has access to an object.
381 */
382 function pods_current_user_can_access_object( $args, $access_type = 'edit', $context = null ) {
383 $user_id = null;
384
385 if ( is_user_logged_in() ) {
386 $user_id = get_current_user_id();
387 }
388
389 return pods_user_can_access_object( $args, $user_id, $access_type, $context );
390 }
391
392 /**
393 * Build and map the capabilities that a specific object type/name/ID have in relation to a user ID.
394 *
395 * @since 3.1.0
396 *
397 * @param array $args {
398 * The arguments to use.
399 *
400 * @type string|null $object_type The object type.
401 * @type string|null $object_name The object name.
402 * @type int|string|null $item_id The item ID.
403 * @type Pods|null $pods The Pods object.
404 * @type Pod|null $pod The Pod object.
405 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
406 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
407 * }
408 * @param int|null $user_id The user ID accessing the object.
409 * @param bool $strict Whether to strictly get the capabilities or have the 'read' capability evaluate to null if it's public (defaults to false).
410 *
411 * @return array|null The capabilities that a specific object type/name/ID have in relation to a user ID, or null if invalid.
412 */
413 function pods_access_map_capabilities( $args, $user_id = null, $strict = false ) {
414 $args['build_pods'] = true;
415 $args['build_pod'] = true;
416
417 $info = pods_info_from_args( $args );
418
419 // If no object type or name, we cannot check access.
420 if ( empty( $info['object_type'] ) || empty( $info['object_name'] ) ) {
421 return null;
422 }
423
424 $wp_object = null;
425
426 $capabilities = array();
427
428 if ( 'post_type' === $info['object_type'] ) {
429 $info['item_id'] = (int) $info['item_id'];
430
431 if ( $info['item_id'] ) {
432 $capabilities['read'] = 'read_post';
433 $capabilities['edit'] = 'edit_post';
434 $capabilities['delete'] = 'delete_post';
435 } else {
436 $capabilities['read'] = 'read';
437 $capabilities['edit'] = 'edit_posts';
438 $capabilities['delete'] = 'delete_posts';
439 }
440
441 $capabilities['add'] = 'create_posts';
442 $capabilities['read_private'] = 'read_private_posts';
443 $capabilities['edit_others'] = 'edit_others_posts';
444 $capabilities['delete_others'] = 'delete_others_posts';
445 $capabilities['delete_published'] = 'delete_published_posts';
446 $capabilities['delete_private'] = 'delete_private_posts';
447
448 // Maybe map capabilities to the post type.
449 $wp_object = get_post_type_object( $info['object_name'] );
450
451 if ( $info['item_id'] ) {
452 $post = get_post( $info['item_id'] );
453
454 // If the post was found, do fine-grained access checks.
455 if ( $post instanceof WP_Post ) {
456 $status_obj = get_post_status_object( $post->post_status );
457
458 // Check if the person is allowed to read other posts.
459 if (
460 $user_id
461 && $post->post_author
462 && (int) $user_id === (int) $post->post_author
463 ) {
464 // This is their own post, they can have access.
465 $capabilities['read'] = 'read';
466 } elseif (
467 ! $status_obj
468 || $status_obj->private
469 ) {
470 // This is a private post, check private post capability.
471 $capabilities['read'] = $capabilities['read_private'];
472 }
473 }
474 }
475 } elseif ( 'taxonomy' === $info['object_type'] ) {
476 $info['item_id'] = (int) $info['item_id'];
477
478 $capabilities['read'] = 'read';
479 $capabilities['add'] = 'manage_terms';
480 $capabilities['edit'] = 'edit_terms';
481 $capabilities['delete'] = 'delete_terms';
482
483 // Maybe map capabilities to the post type.
484 $wp_object = get_taxonomy( $info['object_name'] );
485 } elseif ( 'user' === $info['object_type'] ) {
486 $info['item_id'] = (int) $info['item_id'];
487
488 $capabilities['read'] = 'list_users';
489 $capabilities['add'] = 'create_users';
490 $capabilities['edit'] = 'edit_users';
491 $capabilities['delete'] = 'delete_users';
492
493 // If an object ID is provided, check for access for that specific user.
494 if ( ! empty( $info['item_id'] ) ) {
495 $capabilities['edit'] = 'edit_user';
496 $capabilities['delete'] = 'delete_user';
497 }
498
499 // Fake the WP object for the logic below.
500 $wp_object = (object) array(
501 'public' => false,
502 'cap' => (object) array(),
503 );
504 } elseif ( 'media' === $info['object_type'] ) {
505 $info['item_id'] = (int) $info['item_id'];
506
507 $capabilities['read'] = 'read';
508 $capabilities['add'] = 'upload_files';
509 $capabilities['edit'] = 'upload_files';
510 $capabilities['delete'] = 'upload_files';
511
512 // Fake the WP object for the logic below.
513 $wp_object = (object) array(
514 'public' => false,
515 'cap' => (object) array(),
516 );
517 } elseif ( 'comment' === $info['object_type'] ) {
518 $info['item_id'] = (int) $info['item_id'];
519
520 $capabilities['read'] = 'read';
521 $capabilities['add'] = 1 === (int) get_option( 'comment_registration' ) ? 'read' : null;
522 $capabilities['edit'] = 'moderate_comments';
523 $capabilities['delete'] = 'moderate_comments';
524
525 // If an object ID is provided, check for access for that specific user.
526 if ( ! empty( $info['item_id'] ) ) {
527 $capabilities['edit'] = 'edit_comment';
528 }
529
530 // Fake the WP object for the logic below.
531 $wp_object = (object) array(
532 'public' => true,
533 'cap' => (object) array(),
534 );
535 } elseif ( 'settings' === $info['object_type'] ) {
536 $capabilities['read'] = 'manage_options';
537 $capabilities['edit'] = 'manage_options';
538 $capabilities['delete'] = 'manage_options';
539
540 // Fake the WP object for the logic below.
541 $wp_object = (object) array(
542 'public' => false,
543 'cap' => (object) array(),
544 );
545 } elseif ( 'pod' === $info['object_type'] || 'table' === $info['object_type'] ) {
546 $info['item_id'] = (int) $info['item_id'];
547
548 $capabilities['read'] = 'pods_read_' . $info['object_name'];
549 $capabilities['add'] = 'pods_add_' . $info['object_name'];
550 $capabilities['edit'] = 'pods_edit_' . $info['object_name'];
551 $capabilities['delete'] = 'pods_delete_' . $info['object_name'];
552 $capabilities['edit_others'] = 'pods_edit_others_' . $info['object_name'];
553 $capabilities['delete_others'] = 'pods_delete_others_' . $info['object_name'];
554
555 $is_public = false;
556
557 if ( $info['pods'] instanceof Pods && $info['pod'] instanceof Pod ) {
558 // If an object ID is provided, check for access for that specific item.
559 if ( $info['item_id'] && $info['pods']->exists() ) {
560 // Check for author field.
561 $author_field = $info['pod']->get_field( 'author' );
562
563 $author_user_id = $author_field ? (int) $info['pods']->field( $author_field->get_name() . '.ID' ) : null;
564
565 // If we have an author field, check if they are the author.
566 if ( $author_field ) {
567 if ( $user_id && $author_user_id === $user_id ) {
568 // This is their own post, they can also have access if have edit access.
569 $capabilities['read'] = array(
570 $capabilities['read'],
571 'pods_edit_' . $info['object_name'],
572 );
573 } else {
574 // This is not their post, check if they have access to others.
575 $capabilities['edit'] = 'pods_edit_others_' . $info['object_name'];
576 $capabilities['delete'] = 'pods_delete_others_' . $info['object_name'];
577 }
578 }
579 }
580
581 $is_public = $info['pod']->get_arg( 'public', '0', true );
582 $is_public = filter_var( $is_public, FILTER_VALIDATE_BOOLEAN );
583
584 // Fake the WP object for the logic below.
585 $wp_object = (object) array(
586 'public' => $is_public,
587 'cap' => (object) array(),
588 );
589 }
590
591 if ( $is_public ) {
592 $capabilities['read'] = 'read';
593 }
594 }
595
596 // If no post type object is found, we cannot check access.
597 if ( ! $wp_object ) {
598 return null;
599 }
600
601 // Check if there are any capabilities mapped for this type object.
602 foreach ( $capabilities as $access_type => $capability ) {
603 if ( $capability ) {
604 if ( is_array( $capability ) ) {
605 foreach ( $capability as $k => $cap ) {
606 if ( isset( $wp_object->cap->{$cap} ) ) {
607 $capabilities[ $access_type ][ $k ] = $wp_object->cap->{$cap};
608 }
609 }
610 } elseif ( isset( $wp_object->cap->{$capability} ) ) {
611 $capabilities[ $access_type ] = $wp_object->cap->{$capability};
612 }
613 }
614 }
615
616 // If the object is public, allow read for anyone even logged out.
617 if ( ! $strict && $wp_object->public && 'read' === $capabilities['read'] && ! $user_id ) {
618 $capabilities['read'] = null;
619 }
620
621 /**
622 * Allow filtering the list of capabilities used for checking access against an object type or singular object.
623 *
624 * @since 3.1.0
625 *
626 * @param array $capabilities The list of capabilities used for checking access against an object type or singular object.
627 * @param int $user_id The user ID to check against.
628 * @param array $info {
629 * The normalized Pod information referenced.
630 *
631 * @type string|null $object_type The object type (if set).
632 * @type string|null $object_name The object name (if set).
633 * @type int|string|null $item_id The item ID (if set).
634 * @type Pods|null $pods The Pods object (if built or provided).
635 * @type Pod|null $pod The Pod object (if built or provided).
636 * }
637 */
638 return (array) apply_filters(
639 'pods_access_map_capabilities',
640 $capabilities,
641 $user_id,
642 $info
643 );
644 }
645
646 /**
647 * Determine whether the object type/name is public.
648 *
649 * @since 3.1.0
650 *
651 * @param array $args {
652 * The arguments to use.
653 *
654 * @type string|null $object_type The object type.
655 * @type string|null $object_name The object name.
656 * @type int|string|null $item_id The item ID.
657 * @type Pods|null $pods The Pods object.
658 * @type Pod|null $pod The Pod object.
659 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
660 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
661 * }
662 * @param string $context The context we are checking from (defaults to shortcode).
663 *
664 * @return bool Whether the object type/name is public.
665 */
666 function pods_is_type_public( $args, $context = 'shortcode' ) {
667 $args['build_pod'] = true;
668
669 $info = pods_info_from_args( $args );
670
671 $is_public = true;
672
673 $pod_has_public = null;
674
675 $is_post_type = 'post_type' === $info['object_type'];
676 $is_taxonomy = 'taxonomy' === $info['object_type'];
677 $is_pod = 'pod' === $info['object_type'];
678 $is_settings_pod = 'settings' === $info['object_type'];
679
680 $is_shortcode_context = 'shortcode' === $context;
681
682 if (
683 $info['pod'] instanceof Pod
684 && (
685 $is_post_type
686 || $is_taxonomy
687 || $is_pod
688 || $is_settings_pod
689 )
690 ) {
691 $is_extended = $info['pod']->is_extended();
692
693 if ( ! $is_extended ) {
694 $is_public = $info['pod']->get_arg( 'public', null, true );
695
696 if ( null !== $is_public ) {
697 $pod_has_public = true;
698
699 $is_public = filter_var( $is_public, FILTER_VALIDATE_BOOLEAN );
700
701 if ( $is_post_type || $is_taxonomy ) {
702 $is_public = $is_public && 1 === (int) $info['pod']->get_arg( 'publicly_queryable', $is_public, true );
703 }
704 }
705 }
706 }
707
708 // Maybe handle looking up the visibility based on the object type.
709 if ( null === $pod_has_public ) {
710 if ( $is_post_type ) {
711 // If no object name is provided, we cannot check if it is public.
712 if ( empty( $info['object_name'] ) ) {
713 $is_public = false;
714 } else {
715 $post_type_object = get_post_type_object( $info['object_name'] );
716
717 // Post type not found.
718 if ( ! $post_type_object ) {
719 $is_public = false;
720 } else {
721 $is_public = $post_type_object->public && $post_type_object->publicly_queryable;
722 }
723 }
724 } elseif ( $is_taxonomy ) {
725 // If no object name is provided, we cannot check if it is public.
726 if ( empty( $info['object_name'] ) ) {
727 $is_public = false;
728 } else {
729 $taxonomy_object = get_taxonomy( $info['object_name'] );
730
731 // Post type not found.
732 if ( ! $taxonomy_object ) {
733 $is_public = false;
734 } else {
735 $is_public = $taxonomy_object->public && $taxonomy_object->publicly_queryable;
736 }
737 }
738 } elseif ( 'user' === $info['object_type'] ) {
739 // Users are not public for shortcodes.
740 if ( $is_shortcode_context ) {
741 $is_public = false;
742 }
743 } elseif ( $is_pod || $is_settings_pod ) {
744 // Pods need special default handling for shortcodes.
745 if ( $is_shortcode_context ) {
746 $first_pods_version = get_option( 'pods_framework_version_first' );
747 $first_pods_version = '' === $first_pods_version ? PODS_VERSION : $first_pods_version;
748
749 $is_public = version_compare( $first_pods_version, '3.1.0-a-1', '<' ) ? true : false;
750 }
751 }
752 }
753
754 /**
755 * Allow filtering whether the object type/name is public.
756 *
757 * @since 3.1.0
758 *
759 * @param bool $is_public Whether the object type/name is public.
760 * @param array $info {
761 * The normalized Pod information referenced.
762 *
763 * @type string|null $object_type The object type (if set).
764 * @type string|null $object_name The object name (if set).
765 * @type int|string|null $item_id The item ID (if set).
766 * @type Pods|null $pods The Pods object (if built or provided).
767 * @type Pod|null $pod The Pod object (if built or provided).
768 * }
769 * @param string|null $context The context we are checking from (shortcode or null).
770 */
771 return (bool) apply_filters(
772 'pods_is_type_public',
773 $is_public,
774 $info,
775 $context
776 );
777 }
778
779 /**
780 * Determine whether a post should be bypassed because it it has a password.
781 *
782 * @since 3.1.0
783 *
784 * @param array $args {
785 * The arguments to use.
786 *
787 * @type string|null $object_type The object type.
788 * @type string|null $object_name The object name.
789 * @type int|string|null $item_id The item ID.
790 * @type Pods|null $pods The Pods object.
791 * @type Pod|null $pod The Pod object.
792 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
793 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
794 * }
795 *
796 * @return bool Whether a post should be bypassed because it it has a password.
797 */
798 function pods_access_bypass_post_with_password( $args ) {
799 $info = pods_info_from_args( $args );
800
801 if ( 'post_type' !== $info['object_type'] || ! $info['item_id'] ) {
802 return false;
803 }
804
805 $post = get_post( (int) $info['item_id'] );
806
807 if ( ! $post instanceof WP_Post ) {
808 return false;
809 }
810
811 // Bypass posts that have a password required but not provided.
812 $bypass_post_with_password = post_password_required( $post );
813
814 /**
815 * Allow filtering whether a post should be bypassed because it it has a password.
816 *
817 * @since 3.1.0
818 *
819 * @param bool $bypass_post_with_password Whether a post should be bypassed because it it has a password.
820 * @param array $info {
821 * The normalized Pod information referenced.
822 *
823 * @type string|null $object_type The object type (if set).
824 * @type string|null $object_name The object name (if set).
825 * @type int|string|null $item_id The item ID (if set).
826 * @type Pods|null $pods The Pods object (if built or provided).
827 * @type Pod|null $pod The Pod object (if built or provided).
828 * }
829 */
830 return (bool) apply_filters(
831 'pods_access_bypass_post_with_password',
832 $bypass_post_with_password,
833 $info
834 );
835 }
836
837 /**
838 * Determine whether a post should be bypassed because it is private and capabilities are not met.
839 *
840 * @since 3.1.0
841 *
842 * @param array $args {
843 * The arguments to use.
844 *
845 * @type string|null $object_type The object type.
846 * @type string|null $object_name The object name.
847 * @type int|string|null $item_id The item ID.
848 * @type Pods|null $pods The Pods object.
849 * @type Pod|null $pod The Pod object.
850 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
851 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
852 * }
853 *
854 * @return bool Whether a post should be bypassed because it is private and capabilities are not met.
855 */
856 function pods_access_bypass_private_post( $args ) {
857 $info = pods_info_from_args( $args );
858
859 if ( 'post_type' !== $info['object_type'] || ! $info['item_id'] ) {
860 return false;
861 }
862
863 $post = get_post( $info['item_id'] );
864
865 if ( ! $post instanceof WP_Post ) {
866 return false;
867 }
868
869 $bypass_private_post = false;
870
871 if ( ! is_post_publicly_viewable( $post ) ) {
872 $can_use_unrestricted = false;
873
874 // Check Pod dynamic features if the status is public.
875 if ( is_post_status_viewable( $post->post_status ) ) {
876 $can_use_unrestricted = pods_can_use_dynamic_feature_unrestricted( $info, 'display', 'read' );
877 }
878
879 if ( $can_use_unrestricted ) {
880 $bypass_private_post = false;
881 } else {
882 $bypass_private_post = ! pods_current_user_can_access_object( $info, 'read' );
883 }
884 }
885
886 /**
887 * Allow filtering whether a post should be bypassed because it is private.
888 *
889 * @since 3.1.0
890 *
891 * @param bool $bypass_private_post Whether a post should be bypassed because it is private.
892 * @param array $info {
893 * The normalized Pod information referenced.
894 *
895 * @type string|null $object_type The object type (if set).
896 * @type string|null $object_name The object name (if set).
897 * @type int|string|null $item_id The item ID (if set).
898 * @type Pods|null $pods The Pods object (if built or provided).
899 * @type Pod|null $pod The Pod object (if built or provided).
900 * }
901 */
902 return (bool) apply_filters(
903 'pods_access_bypass_private_post',
904 $bypass_private_post,
905 $info
906 );
907 }
908
909 /**
910 * Determine whether dynamic features can be used.
911 *
912 * @since 3.1.0
913 *
914 * @return bool Whether dynamic features can be used.
915 */
916 function pods_can_use_dynamic_features( ?Pod $pod = null ) {
917 // Check if the constant is defined and only override if no $pod is set or dynamic features are totally disabled.
918 if (
919 defined( 'PODS_DYNAMIC_FEATURES_ALLOW' )
920 && (
921 ! $pod
922 || ! PODS_DYNAMIC_FEATURES_ALLOW
923 )
924 ) {
925 return PODS_DYNAMIC_FEATURES_ALLOW;
926 }
927
928 $can_use_dynamic_features = apply_filters( 'pods_access_can_use_dynamic_features', null, $pod );
929
930 if ( is_bool( $can_use_dynamic_features ) ) {
931 return $can_use_dynamic_features;
932 }
933
934 // Check if all dynamic features are disabled.
935 $dynamic_features_allow = pods_get_setting( 'dynamic_features_allow', '1' );
936 $dynamic_features_allow = filter_var( $dynamic_features_allow, FILTER_VALIDATE_BOOLEAN );
937
938 if ( $dynamic_features_allow && $pod instanceof Pod ) {
939 // Check if all dynamic features are disabled for the Pod.
940 $dynamic_features_allow = $pod->get_arg( 'dynamic_features_allow', 'inherit' );
941
942 if ( 'inherit' === $dynamic_features_allow ) {
943 $dynamic_features_allow = pods_is_type_public(
944 array(
945 'pod' => $pod,
946 )
947 );
948 } else {
949 $dynamic_features_allow = filter_var( $dynamic_features_allow, FILTER_VALIDATE_BOOLEAN );
950 }
951 }
952
953 return $dynamic_features_allow;
954 }
955
956 /**
957 * Determine whether any or a specific dynamic feature can be used.
958 *
959 * @since 3.1.0
960 *
961 * @param string $type The dynamic feature type.
962 *
963 * @return bool Whether any or a specific dynamic feature can be used.
964 */
965 function pods_can_use_dynamic_feature( $type ) {
966 if ( ! pods_can_use_dynamic_features() ) {
967 return false;
968 }
969
970 if ( empty( $type ) ) {
971 return false;
972 }
973
974 // Handle the constants.
975 if ( 'view' === $type && defined( 'PODS_SHORTCODE_ALLOW_VIEWS' ) && ! PODS_SHORTCODE_ALLOW_VIEWS ) {
976 return false;
977 }
978
979 $can_use_dynamic_feature = apply_filters( 'pods_access_can_use_dynamic_feature', null, $type );
980
981 if ( is_bool( $can_use_dynamic_feature ) ) {
982 return $can_use_dynamic_feature;
983 }
984
985 $dynamic_features_enabled = (array) pods_get_setting( 'dynamic_features_enabled', array(
986 'display',
987 'form',
988 ) );
989 $dynamic_features_enabled = array_filter( $dynamic_features_enabled );
990
991 $constant_dynamic_features_enabled = defined( 'PODS_DYNAMIC_FEATURES_ENABLED' ) ? PODS_DYNAMIC_FEATURES_ENABLED : false;
992
993 if ( false !== $constant_dynamic_features_enabled && ! is_array( $constant_dynamic_features_enabled ) ) {
994 $constant_dynamic_features_enabled = explode( ',', $constant_dynamic_features_enabled );
995 $constant_dynamic_features_enabled = array_filter( $constant_dynamic_features_enabled );
996
997 $dynamic_features_enabled = $constant_dynamic_features_enabled;
998 }
999
1000 if ( empty( $dynamic_features_enabled ) ) {
1001 return false;
1002 }
1003
1004 return in_array( $type, $dynamic_features_enabled, true );
1005 }
1006
1007 /**
1008 * Determine whether specific dynamic feature is unrestricted.
1009 *
1010 * @since 3.1.0
1011 *
1012 * @param array $args {
1013 * The arguments to use.
1014 *
1015 * @type string|null $object_type The object type.
1016 * @type string|null $object_name The object name.
1017 * @type int|string|null $item_id The item ID.
1018 * @type Pods|null $pods The Pods object.
1019 * @type Pod|null $pod The Pod object.
1020 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
1021 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
1022 * }
1023 * @param string $type The dynamic feature type.
1024 * @param string $mode The dynamic feature mode (like "add" or "edit" for the form feature).
1025 *
1026 * @return bool Whether specific dynamic feature is unrestricted.
1027 */
1028 function pods_can_use_dynamic_feature_unrestricted( $args, $type, $mode = null ) {
1029 if ( ! pods_can_use_dynamic_feature( $type ) ) {
1030 return false;
1031 }
1032
1033 if ( defined( 'PODS_DYNAMIC_FEATURES_RESTRICT' ) && ! PODS_DYNAMIC_FEATURES_RESTRICT ) {
1034 return true;
1035 }
1036
1037 $can_use_dynamic_features_unrestricted = apply_filters( 'pods_access_can_use_dynamic_features_unrestricted', null, $args, $type, $mode );
1038
1039 if ( is_bool( $can_use_dynamic_features_unrestricted ) ) {
1040 return $can_use_dynamic_features_unrestricted;
1041 }
1042
1043 $can_use_unrestricted = false;
1044
1045 $args['build_pod'] = true;
1046
1047 $info = pods_info_from_args( $args );
1048
1049 if ( ! $info['pod'] ) {
1050 $can_use_unrestricted = false;
1051 } else {
1052 $is_public_content_type = pods_is_type_public( $info );
1053
1054 $default_restricted_dynamic_features = array(
1055 'form',
1056 );
1057
1058 if ( ! $is_public_content_type ) {
1059 $default_restricted_dynamic_features[] = 'display';
1060 }
1061
1062 $default_restricted_dynamic_features_forms = array(
1063 'edit',
1064 );
1065
1066 if ( ! $is_public_content_type ) {
1067 $default_restricted_dynamic_features_forms[] = 'add';
1068 }
1069
1070 // Check if all dynamic features are unrestricted.
1071 $restrict_dynamic_features = $info['pod']->get_arg( 'restrict_dynamic_features', '1' );
1072 $restrict_dynamic_features = filter_var( $restrict_dynamic_features, FILTER_VALIDATE_BOOLEAN );
1073
1074 if ( ! $restrict_dynamic_features ) {
1075 $can_use_unrestricted = true;
1076 } elseif ( ! empty( $type ) ) {
1077 if ( defined( 'PODS_DYNAMIC_FEATURES_RESTRICTED' ) && false !== PODS_DYNAMIC_FEATURES_RESTRICTED ) {
1078 $constant_restricted_dynamic_features = PODS_DYNAMIC_FEATURES_RESTRICTED;
1079
1080 if ( ! is_array( $constant_restricted_dynamic_features ) ) {
1081 $constant_restricted_dynamic_features = explode( ',', $constant_restricted_dynamic_features );
1082 }
1083
1084 $restricted_dynamic_features = $constant_restricted_dynamic_features;
1085 } else {
1086 $restricted_dynamic_features = (array) $info['pod']->get_arg( 'restricted_dynamic_features', $default_restricted_dynamic_features );
1087 }
1088
1089 $restricted_dynamic_features = array_filter( $restricted_dynamic_features );
1090
1091 if ( empty( $restricted_dynamic_features ) ) {
1092 $can_use_unrestricted = true;
1093 } else {
1094 $can_use_unrestricted = ! in_array( $type, $restricted_dynamic_features, true );
1095 }
1096
1097 if ( ! $can_use_unrestricted && 'form' === $type && $mode ) {
1098 if ( defined( 'PODS_DYNAMIC_FEATURES_RESTRICTED_FORMS' ) && false !== PODS_DYNAMIC_FEATURES_RESTRICTED_FORMS ) {
1099 $constant_restricted_dynamic_features_forms = PODS_DYNAMIC_FEATURES_RESTRICTED_FORMS;
1100
1101 if ( ! is_array( $constant_restricted_dynamic_features_forms ) ) {
1102 $constant_restricted_dynamic_features_forms = explode( ',', $constant_restricted_dynamic_features_forms );
1103 }
1104
1105 $restricted_dynamic_features_forms = $constant_restricted_dynamic_features_forms;
1106 } else {
1107 $restricted_dynamic_features_forms = (array) $info['pod']->get_arg( 'restricted_dynamic_features_forms', $default_restricted_dynamic_features_forms );
1108 }
1109
1110 $restricted_dynamic_features_forms = array_filter( $restricted_dynamic_features_forms );
1111
1112 if ( empty( $restricted_dynamic_features_forms ) ) {
1113 $can_use_unrestricted = true;
1114 } else {
1115 $can_use_unrestricted = ! in_array( $mode, $restricted_dynamic_features_forms, true );
1116 }
1117 }
1118 }
1119 }
1120
1121 return $can_use_unrestricted;
1122 }
1123
1124 /**
1125 * Get the access notice for admin user based on object type and object name.
1126 *
1127 * @since 3.1.0
1128 *
1129 * @param array $args {
1130 * The arguments to use.
1131 *
1132 * @type string|null $object_type The object type.
1133 * @type string|null $object_name The object name.
1134 * @type int|string|null $item_id The item ID.
1135 * @type Pods|null $pods The Pods object.
1136 * @type Pod|null $pod The Pod object.
1137 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
1138 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
1139 * }
1140 * @param bool $force_message Whether to force the message to show even if messages are hidden by a setting.
1141 * @param string|null $message A custom message to use for the notice text.
1142 *
1143 * @return string The access notice for admin user based on object type and object name.
1144 */
1145 function pods_get_access_admin_notice( $args, $force_message = false, $message = null ) {
1146 $args['build_pod'] = true;
1147
1148 $info = pods_info_from_args( $args );
1149
1150 $identifier_for_html = esc_html( json_encode( array(
1151 'object_type' => $info['object_type'],
1152 'object_name' => $info['object_name'],
1153 'item_id' => $info['item_id'],
1154 ) ) );
1155
1156 // Check if constant is hiding all notices.
1157 if ( ! $force_message && defined( 'PODS_ACCESS_HIDE_NOTICES' ) && PODS_ACCESS_HIDE_NOTICES ) {
1158 return '<!-- pods:access-notices/admin/hidden-by-constant ' . $identifier_for_html . ' -->';
1159 }
1160
1161 // Check notice setting for the Pod itself.
1162 if ( $info['pod'] instanceof Pod ) {
1163 $show_access_admin_notices_for_pod = $info['pod']->get_arg( 'show_access_admin_notices', 'inherit' );
1164
1165 if ( 'inherit' !== $show_access_admin_notices_for_pod ) {
1166 $show_access_admin_notices_for_pod = filter_var( $show_access_admin_notices_for_pod, FILTER_VALIDATE_BOOLEAN );
1167
1168 // Check if all notices have been dismissed for the pod.
1169 if ( ! $force_message && ! $show_access_admin_notices_for_pod ) {
1170 return '<!-- pods:access-notices/admin/hidden-by-pod ' . $identifier_for_html . ' -->';
1171 }
1172 }
1173 }
1174
1175 // Show notice that this content may not be visible to others.
1176 $show_access_admin_notices = pods_get_setting( 'show_access_admin_notices', true );
1177 $show_access_admin_notices = filter_var( $show_access_admin_notices, FILTER_VALIDATE_BOOLEAN );
1178
1179 // Check if all notices have been dismissed.
1180 if ( ! $force_message && ! $show_access_admin_notices ) {
1181 return '<!-- pods:access-notices/admin/hidden-by-setting ' . $identifier_for_html . ' -->';
1182 }
1183
1184 $summary = esc_html__( 'Pods Access Rights: Admin-only Notice', 'pods' );
1185
1186 if ( $message ) {
1187 $content = wpautop( $message );
1188 } else {
1189 $content = sprintf(
1190 '
1191 <p>
1192 %1$s
1193 <br />
1194 <span class="pods-ui-notice-action-links">
1195 <a href="%2$s" target="_blank" rel="noopener noreferrer">%3$s</a>
1196 | <a href="%4$s" target="_blank" rel="noopener noreferrer">%5$s</a>
1197 </span>
1198 </p>
1199 ',
1200 esc_html__( 'The content type or the content below is not public and may not be available to everyone else.', 'pods' ),
1201 esc_url( 'https://docs.pods.io/displaying-pods/access-rights-in-pods/' ),
1202 esc_html__( 'How access rights work with Pods (Documentation)', 'pods' ),
1203 esc_url( admin_url( 'admin.php?page=pods-settings#heading-security' ) ),
1204 esc_html__( 'Edit other access right options', 'pods' )
1205 );
1206 }
1207
1208 return '<!-- pods:access-notices/admin/message ' . $identifier_for_html . ' -->'
1209 . pods_message(
1210 sprintf(
1211 '
1212 <details open>
1213 <summary><strong>%1$s</strong></summary>
1214 %2$s
1215 </details>
1216 ',
1217 wp_strip_all_tags( ! empty( $info['summary'] ) ? $info['summary'] : $summary ),
1218 ! empty( $info['content'] ) ? wpautop( $info['content'] ) : $content
1219 ),
1220 'notice',
1221 true
1222 );
1223 }
1224
1225 /**
1226 * Get the access notice for non-admin user based on object type and object name.
1227 *
1228 * @since 3.1.0
1229 *
1230 * @param array $args {
1231 * The arguments to use.
1232 *
1233 * @type string|null $object_type The object type.
1234 * @type string|null $object_name The object name.
1235 * @type int|string|null $item_id The item ID.
1236 * @type Pods|null $pods The Pods object.
1237 * @type Pod|null $pod The Pod object.
1238 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
1239 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
1240 * }
1241 * @param bool $force_message Whether to force the message to show even if messages are hidden by a setting.
1242 * @param string|null $message A custom message to use for the notice text.
1243 *
1244 * @return string The access notice for non-admin user based on object type and object name.
1245 */
1246 function pods_get_access_user_notice( $args, $force_message = false, $message = null ) {
1247 $args['build_pod'] = true;
1248
1249 $info = pods_info_from_args( $args );
1250
1251 $identifier_for_html = esc_html( json_encode( array(
1252 'object_type' => $info['object_type'],
1253 'object_name' => $info['object_name'],
1254 'item_id' => $info['item_id'],
1255 ) ) );
1256
1257 // Check for password-protected post.
1258 if ( $info['item_id'] && pods_access_bypass_post_with_password( $info ) ) {
1259 $message = get_the_password_form( $info['item_id'] );
1260
1261 return '<!-- pods:access-notices/user/protected/message ' . $identifier_for_html . ' -->'
1262 . pods_message(
1263 sprintf(
1264 '<p><strong>%1$s</strong></p> %2$s',
1265 esc_html__( 'Access Restricted', 'pods' ),
1266 $message
1267 ),
1268 'error',
1269 true
1270 );
1271 }
1272
1273 // Check if constant is hiding all notices.
1274 if ( ! $force_message && defined( 'PODS_ACCESS_HIDE_NOTICES' ) && PODS_ACCESS_HIDE_NOTICES ) {
1275 return '<!-- pods:access-notices/user/hidden-by-constant ' . $identifier_for_html . ' -->';
1276 }
1277
1278 // Check notice setting for the Pod itself.
1279 if ( $info['pod'] instanceof Pod ) {
1280 $show_access_restricted_messages_for_pod = $info['pod']->get_arg( 'show_access_restricted_messages', 'inherit' );
1281
1282 if ( 'inherit' !== $show_access_restricted_messages_for_pod ) {
1283 $show_access_restricted_messages_for_pod = filter_var( $show_access_restricted_messages_for_pod, FILTER_VALIDATE_BOOLEAN );
1284
1285 // Check if all notices have been dismissed for the pod.
1286 if ( ! $force_message && ! $show_access_restricted_messages_for_pod ) {
1287 return '<!-- pods:access-notices/user/hidden-by-pod ' . $identifier_for_html . ' -->';
1288 }
1289 }
1290 }
1291
1292 // Show notice that this content may not be visible to others.
1293 $show_access_restricted_messages = pods_get_setting( 'show_access_restricted_messages', false );
1294 $show_access_restricted_messages = filter_var( $show_access_restricted_messages, FILTER_VALIDATE_BOOLEAN );
1295
1296 // Check if all notices have been dismissed.
1297 if ( ! $force_message && ! $show_access_restricted_messages ) {
1298 return '<!-- pods:access-notices/user/hidden-by-setting ' . $identifier_for_html . ' -->';
1299 }
1300
1301 $message = $message ?? esc_html__( 'You do not have access to this embedded content.', 'pods' );
1302
1303 return '<!-- pods:access-notices/user/message ' . $identifier_for_html . ' -->'
1304 . pods_message(
1305 sprintf(
1306 '<p><strong>%1$s:</strong> %2$s</p>',
1307 esc_html__( 'Access Restricted', 'pods' ),
1308 $message
1309 ),
1310 'error',
1311 true
1312 );
1313 }
1314
1315 /**
1316 * Determine whether SQL clauses can be used with dynamic features.
1317 *
1318 * @since 3.1.0
1319 *
1320 * @param null|string $clause_type The clause type to check if allowed, if null used then it checks if any clauses are allowed.
1321 *
1322 * @return bool Whether SQL clauses can be used with dynamic features.
1323 */
1324 function pods_can_use_dynamic_feature_sql_clauses( $clause_type = null ) {
1325 // Set default to most simple clause type check (simple).
1326 $clause_type = $clause_type ?: 'simple';
1327
1328 if ( defined( 'PODS_DISABLE_SHORTCODE_SQL' ) ) {
1329 // Negate the check since this is a "disable" constant.
1330 return ! PODS_DISABLE_SHORTCODE_SQL;
1331 }
1332
1333 if ( defined( 'PODS_DYNAMIC_FEATURES_ALLOW_SQL_CLAUSES' ) ) {
1334 $allow_sql_clauses = PODS_DYNAMIC_FEATURES_ALLOW_SQL_CLAUSES;
1335 } else {
1336 $cached_allow_sql_clauses = pods_transient_get( 'pods_dynamic_features_allow_sql_clauses' );
1337
1338 if ( is_string( $cached_allow_sql_clauses ) ) {
1339 $allow_sql_clauses = $cached_allow_sql_clauses;
1340 } else {
1341 $first_pods_version = get_option( 'pods_framework_version_first' );
1342 $first_pods_version = '' === $first_pods_version ? PODS_VERSION : $first_pods_version;
1343
1344 $allow_sql_clauses = pods_get_setting( 'dynamic_features_allow_sql_clauses', version_compare( $first_pods_version, '3.1.0-a-1', '<' ) ? 'simple' : '0' );
1345
1346 pods_transient_set( 'pods_dynamic_features_allow_sql_clauses', (string) $allow_sql_clauses );
1347 }
1348 }
1349
1350 if (
1351 false === $allow_sql_clauses
1352 || '0' === (string) $allow_sql_clauses
1353 ) {
1354 return false;
1355 }
1356
1357 // The "all" option is inclusive of "simple".
1358 if ( 'simple' === $clause_type && 'all' === $allow_sql_clauses ) {
1359 return true;
1360 }
1361
1362 return $clause_type === $allow_sql_clauses;
1363 }
1364
1365 /**
1366 * Determine whether a callback can be used.
1367 *
1368 * Only plain function-name string callbacks are permitted by default. Closures,
1369 * invokable objects, array callables ( [ $object, 'method' ] / [ 'Class', 'method' ] ),
1370 * and string class method references ( "Class::method" ) are rejected unless
1371 * class callbacks are enabled via the PODS_ALLOW_CLASS_CALLBACKS constant or the
1372 * "pods_access_allow_class_callbacks" filter.
1373 *
1374 * @since 3.1.0
1375 *
1376 * @param string|callable $callback The callback to check.
1377 * @param array $params Parameters used by Pods::helper() method.
1378 *
1379 * @return bool Whether the callback can be used.
1380 */
1381 function pods_access_callback_allowed( $callback, $params = array() ) {
1382 // Class-based callbacks are disabled by default; only plain function-name string callbacks are permitted. Set the PODS_ALLOW_CLASS_CALLBACKS constant to true (or use the "pods_access_allow_class_callbacks" filter) to permit closures, invokable objects, array callables, and "Class::method" strings.
1383 $allow_class_callbacks = defined( 'PODS_ALLOW_CLASS_CALLBACKS' ) && PODS_ALLOW_CLASS_CALLBACKS;
1384
1385 /**
1386 * Filter whether class-based callbacks are permitted (closures, invokable
1387 * objects, array callables, and "Class::method" strings).
1388 *
1389 * @since 3.3.9.1
1390 *
1391 * @param bool $allow_class_callbacks Whether class-based callbacks are allowed.
1392 * @param string|callable $callback The callback being checked.
1393 * @param array $params Parameters used by Pods::helper() method.
1394 */
1395 $allow_class_callbacks = (bool) apply_filters( 'pods_access_allow_class_callbacks', $allow_class_callbacks, $callback, $params );
1396
1397 if ( ! is_string( $callback ) ) {
1398 return $allow_class_callbacks;
1399 }
1400
1401 if ( ! pods_can_use_dynamic_feature( 'display' ) ) {
1402 return false;
1403 }
1404
1405 if (
1406 defined( 'PODS_DISPLAY_CALLBACKS' )
1407 && ! PODS_DISPLAY_CALLBACKS
1408 ) {
1409 return false;
1410 }
1411
1412 /**
1413 * Allows changing whether callbacks are allowed to run.
1414 *
1415 * @param bool $allow_callbacks Whether callbacks are allowed to run.
1416 * @param array $params Parameters used by Pods::helper() method.
1417 *
1418 * @since 2.8.0
1419 */
1420 $allow_callbacks = (bool) apply_filters( 'pods_helper_allow_callbacks', true, $params );
1421
1422 if ( ! $allow_callbacks ) {
1423 return false;
1424 }
1425
1426 /*
1427 * Allowed callbacks. A callback must appear here (or in a user/filter
1428 * addition) to be usable. Comparison is case- and namespace-insensitive,
1429 * so entries are lowercase.
1430 */
1431 $allowed = array(
1432 // Escaping / output.
1433 'esc_attr',
1434 'esc_html',
1435 'esc_js',
1436 'esc_url',
1437
1438 // Post display-by-ID.
1439 'get_permalink',
1440 'get_the_date',
1441 'get_the_excerpt',
1442 'get_the_modified_date',
1443 'get_the_modified_time',
1444 'get_the_post_thumbnail',
1445 'get_the_post_thumbnail_url',
1446 'get_the_time',
1447 'get_the_title',
1448
1449 // Term display-by-ID.
1450 'get_cat_name',
1451 'get_category_link',
1452 'get_tag_link',
1453 'get_term_link',
1454
1455 // User display-by-ID.
1456 'get_author_posts_url',
1457 'get_avatar',
1458 'get_avatar_url',
1459
1460 // Formatting (PHP).
1461 'abs',
1462 'absint',
1463 'ceil',
1464 'floatval',
1465 'floor',
1466 'htmlentities',
1467 'htmlspecialchars',
1468 'intval',
1469 'ltrim',
1470 'nl2br',
1471 'normalize_whitespace',
1472 'number_format',
1473 'number_format_i18n',
1474 'round',
1475 'rtrim',
1476 'str_word_count',
1477 'strrev',
1478 'strtolower',
1479 'strtoupper',
1480 'trim',
1481 'ucfirst',
1482 'ucwords',
1483 'wordwrap',
1484 'wpautop',
1485
1486 // Formatting (WP)
1487 'make_clickable',
1488 'sanitize_html_class',
1489 'sanitize_title',
1490 'sanitize_title_with_dashes',
1491 'strip_tags',
1492 'wp_kses_data',
1493 'wp_kses_post',
1494 'wp_strip_all_tags',
1495 'wp_trim_words',
1496 'wptexturize',
1497
1498 // Formatting (Pods)
1499 'pods_serial_comma',
1500 );
1501
1502 if ( defined( 'PODS_DISPLAY_CALLBACKS' ) ) {
1503 $display_callbacks = PODS_DISPLAY_CALLBACKS;
1504 } else {
1505 $display_callbacks = pods_get_setting( 'display_callbacks', 'restricted' );
1506 }
1507
1508 if ( '0' === $display_callbacks ) {
1509 return false;
1510 }
1511
1512 // Maybe specify additional allowed callbacks on top of the built-in list.
1513 if ( 'customized' === $display_callbacks ) {
1514 if ( defined( 'PODS_DISPLAY_CALLBACKS_ALLOWED' ) ) {
1515 $display_callbacks_allowed = PODS_DISPLAY_CALLBACKS_ALLOWED;
1516 } else {
1517 $display_callbacks_allowed = pods_get_setting( 'display_callbacks_allowed', '' );
1518 }
1519
1520 if ( ! is_array( $display_callbacks_allowed ) ) {
1521 $display_callbacks_allowed = str_replace( "\n", ',', $display_callbacks_allowed );
1522 $display_callbacks_allowed = explode( ',', $display_callbacks_allowed );
1523 }
1524
1525 $display_callbacks_allowed = array_map( 'trim', $display_callbacks_allowed );
1526 $display_callbacks_allowed = array_filter( $display_callbacks_allowed );
1527
1528 /**
1529 * Allow filtering the custom prefix used for the display callbacks that can be used with Pods.
1530 *
1531 * Default: custom_pods_callback_
1532 *
1533 * @since 3.3.9.2
1534 *
1535 * @param string $custom_prefix The custom prefix used for the display callbacks that can be used with Pods.
1536 */
1537 $custom_prefix = apply_filters( 'pods_access_callbacks_custom_prefix', 'custom_pods_callback_' );
1538
1539 $display_callbacks_allowed = array_values(
1540 array_filter(
1541 $display_callbacks_allowed,
1542 function ( $name ) use ( $custom_prefix ) {
1543 $normalized = ltrim( strtolower( (string) $name ), '\\' );
1544
1545 return 0 === strpos( $normalized, $custom_prefix );
1546 }
1547 )
1548 );
1549
1550 if ( ! empty( $display_callbacks_allowed ) ) {
1551 $allowed = array_merge( $allowed, $display_callbacks_allowed );
1552 }
1553 }
1554
1555 /**
1556 * Allows adjusting the allowed callbacks as needed.
1557 *
1558 * @param array $allowed List of callbacks explicitly allowed.
1559 * @param array $params Parameters used by Pods::helper() method.
1560 *
1561 * @since 2.7.0
1562 */
1563 $allowed = apply_filters( 'pods_helper_allowed_callbacks', $allowed, $params );
1564
1565 // Clean up helper callback (if string).
1566 if ( is_string( $callback ) ) {
1567 $callback = wp_strip_all_tags( str_replace( array( '`', chr( 96 ) ), "'", $callback ) );
1568 }
1569
1570 /*
1571 * Normalize for comparison. PHP function/method names are case-insensitive
1572 * and may be written with a leading namespace separator, so "SYSTEM",
1573 * "System", and "\system" must all be treated as "system". The allowed list
1574 * is normalized the same way so matching is consistent.
1575 */
1576 $normalized_callback = ltrim( strtolower( trim( (string) $callback ) ), '\\' );
1577
1578 /*
1579 * Reject class method callbacks expressed as strings unless class callbacks
1580 * are explicitly enabled. The scope resolution operator "::" only appears in
1581 * static method references such as "Class::method", "\Namespace\Class::method",
1582 * or "parent::method".
1583 */
1584 if ( ! $allow_class_callbacks && false !== strpos( $normalized_callback, '::' ) ) {
1585 pods_access_record_disallowed_display_callback( $callback );
1586
1587 return false;
1588 }
1589
1590 $allowed = array_map( 'strtolower', $allowed );
1591
1592 /*
1593 * Class method strings skip the built-in allow list when class callbacks
1594 * are enabled.
1595 */
1596 if ( $allow_class_callbacks && false !== strpos( $normalized_callback, '::' ) ) {
1597 return true;
1598 }
1599
1600 $is_allowed = in_array( $normalized_callback, $allowed, true );
1601
1602 if ( ! $is_allowed ) {
1603 pods_access_record_disallowed_display_callback( $callback );
1604 }
1605
1606 return $is_allowed;
1607 }
1608
1609 /**
1610 * Get the unique list of disallowed display callbacks stored in cache.
1611 *
1612 * @since 3.3.9.2
1613 *
1614 * @return string[] Unique callback names.
1615 */
1616 function pods_get_disallowed_display_callbacks() {
1617 $existing = pods_transient_get( 'pods_disallowed_display_callbacks' );
1618
1619 if ( empty( $existing ) ) {
1620 return array();
1621 }
1622
1623 $existing = array_filter( array_map( 'trim', explode( ',', (string) $existing ) ) );
1624
1625 return array_values( array_unique( $existing ) );
1626 }
1627
1628 /**
1629 * Record a disallowed display callback into the cache.
1630 *
1631 * Stores a unique comma-separated list for up to 30 days. Recording is skipped
1632 * when display callback notices are disabled.
1633 *
1634 * @since 3.3.9.2
1635 *
1636 * @param string $callback The cleaned callback name that was rejected.
1637 */
1638 function pods_access_record_disallowed_display_callback( $callback ) {
1639 if ( ! pods_is_truthy( pods_get_setting( 'show_display_callback_notices', '1' ) ) ) {
1640 return;
1641 }
1642
1643 $callback = trim( $callback );
1644
1645 if ( '' === $callback ) {
1646 return;
1647 }
1648
1649 $existing = pods_get_disallowed_display_callbacks();
1650
1651 if ( in_array( $callback, $existing, true ) ) {
1652 return;
1653 }
1654
1655 $existing[] = $callback;
1656
1657 pods_transient_set(
1658 'pods_disallowed_display_callbacks',
1659 implode( ',', array_unique( $existing ) ),
1660 30 * DAY_IN_SECONDS
1661 );
1662 }
1663
1664 /**
1665 * Clear the cached list of disallowed display callbacks.
1666 *
1667 * @since 3.3.9.2
1668 */
1669 function pods_access_clear_disallowed_display_callbacks() {
1670 pods_transient_clear( 'pods_disallowed_display_callbacks' );
1671 }
1672
1673 /**
1674 * Get the pod access tab options for a specific pod.
1675 *
1676 * @since 3.1.0
1677 *
1678 * @param string $pod_type The pod type.
1679 * @param string $pod_name The pod name.
1680 * @param null|Pod $pod The pod object.
1681 *
1682 * @return array The pod access tab options for a specific pod.
1683 */
1684 function pods_access_pod_options( $pod_type, $pod_name, ?Pod $pod = null ) {
1685 $first_pods_version = get_option( 'pods_framework_version_first' );
1686 $first_pods_version = '' === $first_pods_version ? PODS_VERSION : $first_pods_version;
1687
1688 $options = array();
1689
1690 $options['security_access_rights_info'] = array(
1691 'label' => __( 'How access rights work in Pods', 'pods' ),
1692 'type' => 'html',
1693 'html_content' => sprintf(
1694 '
1695 <p>%1$s</p>
1696 <p><a href="https://docs.pods.io/displaying-pods/access-rights-in-pods/" target="_blank" rel="noopener noreferrer">%2$s</a> <span class="dashicon dashicons dashicons-external"></span></p>
1697 ',
1698 __( 'Pods handles access rights similar to how WordPress itself works.', 'pods' ),
1699 __( 'Read more about how access rights work in Pods on our Documentation site', 'pods' )
1700 ),
1701 );
1702
1703 if ( 'pod' === $pod_type ) {
1704 $options['public'] = array(
1705 'label' => __( 'Public', 'pods' ),
1706 'help' => __( 'You can still embed Pods Content and Forms through PHP and make use of other features directly through code.', 'pods' ),
1707 'description' => __( 'When a content type is public, it can be viewed by anyone when it is embedded through Dynamic Features. Otherwise, a user will need to have the corresponding "read" capability for the content type.', 'pods' ),
1708 'type' => 'boolean',
1709 'default' => version_compare( $first_pods_version, '3.1.0-a-1', '<' ) ? true : false,
1710 'boolean_yes_label' => '',
1711 );
1712 }
1713
1714 if ( pods_can_use_dynamic_features() ) {
1715 $options['dynamic_features_allow'] = array(
1716 'label' => __( 'Dynamic Features', 'pods' ),
1717 'help' => array(
1718 __( 'Enabling Dynamic Features will also enable the additional access rights checks for user access. This ensures that people viewing embedded content and forms have the required capabilities. Even when Dynamic Features are disabled, you can still embed Pods Content and Forms through PHP and make use of other features directly through code.', 'pods' ),
1719 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
1720 ),
1721 'description' => __( 'Dynamic features include Pods Shortcodes, Blocks, and Widgets which let you embed content and forms on your site.', 'pods' ),
1722 'type' => 'pick',
1723 'default' => 'inherit',
1724 'pick_format_type' => 'single',
1725 'pick_format_single' => 'radio',
1726 'data' => array(
1727 'inherit' => __( 'WP Default - If the content type is marked "Public" with WordPress then Dynamic Features will be enabled.', 'pods' ),
1728 '1' => __( 'Enable Dynamic Features including Pods Shortcodes, Blocks, and Widgets for this content type', 'pods' ),
1729 '0' => __( 'Disable All Dynamic Features in Pods for this content type', 'pods' ),
1730 ),
1731 'dependency' => true,
1732 );
1733
1734 $is_public_content_type = pods_is_type_public(
1735 array(
1736 'pod' => $pod,
1737 )
1738 );
1739
1740 $options['restrict_dynamic_features'] = array(
1741 'label' => __( 'Restrict Dynamic Features', 'pods' ),
1742 'help' => array(
1743 __( 'This will check access rights for whether someone should have access to specific content before a they can view, modify, or interact with that content.', 'pods' ),
1744 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
1745 ),
1746 'description' => sprintf(
1747 '<strong>%1$s</strong> %2$s',
1748 esc_html__( 'Warning:', 'pods' ),
1749 esc_html__( 'If you have authors/contributors on your site then disabling this would give them access to embedding content/forms without access checks for them or whoever views the embeds on the front of your site. Caution is always advised before giving access to other users you may not trust.', 'pods' )
1750 ),
1751 'type' => 'pick',
1752 'default' => '1',
1753 'pick_format_type' => 'single',
1754 'pick_format_single' => 'radio',
1755 'data' => array(
1756 '0' => __( 'Unrestricted - Do not check for access rights for embedded content (only use this if you trust ALL users who have access to create content)', 'pods' ),
1757 '1' => __( 'Restricted - Check access rights for embedded content', 'pods' ),
1758 ),
1759 'excludes-on' => array( 'dynamic_features_allow' => '0' ),
1760 );
1761
1762 $default_restricted_dynamic_features = array(
1763 'form',
1764 );
1765
1766 if ( ! $is_public_content_type ) {
1767 $default_restricted_dynamic_features[] = 'display';
1768 }
1769
1770 $options['restricted_dynamic_features'] = array(
1771 'label' => __( 'Dynamic Features to Restrict', 'pods' ),
1772 'help' => array(
1773 __( 'This will check access rights for the dynamic feature for whether someone should have access to specific content before a they can view, modify, or interact with that content.', 'pods' ),
1774 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
1775 ),
1776 'type' => 'pick',
1777 'default' => $default_restricted_dynamic_features,
1778 'pick_format_type' => 'multi',
1779 'pick_format_multi' => 'checkbox',
1780 'data' => array(
1781 'display' => __( 'Restricted Display - Shortcodes and Blocks that allow querying content from this Pod and displaying any field will check access rights.', 'pods' ),
1782 'form' => __( 'Restricted Forms - The Form Shortcode and Block submitting new content or editing existing content will check access rights.', 'pods' ),
1783 ),
1784 'depends-on' => array( 'restrict_dynamic_features' => '1' ),
1785 'excludes-on' => array( 'dynamic_features_allow' => '0' ),
1786 );
1787
1788 $default_restricted_dynamic_features_forms = array(
1789 'edit',
1790 );
1791
1792 if ( ! $is_public_content_type ) {
1793 $default_restricted_dynamic_features_forms[] = 'add';
1794 }
1795
1796 $options['restricted_dynamic_features_forms'] = array(
1797 'label' => __( 'Dynamic Features to Restrict for Forms', 'pods' ),
1798 'help' => array(
1799 __( 'This will check access rights for whether someone should have access to specific content before a they can add or edit content.', 'pods' ),
1800 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
1801 ),
1802 'type' => 'pick',
1803 'default' => $default_restricted_dynamic_features_forms,
1804 'pick_format_type' => 'multi',
1805 'pick_format_multi' => 'checkbox',
1806 'data' => array(
1807 'add' => __( 'Restricted Add New Forms - Embedding the Form Shortcode and Block to allow for adding new content will check access rights.', 'pods' ),
1808 'edit' => __( 'Restricted Edit Forms - Embedding the Form Shortcode and Block to allow for editing existing content will check access rights.', 'pods' ),
1809 ),
1810 'depends-on-multi' => array( 'restricted_dynamic_features' => 'form' ),
1811 'excludes-on' => array( 'dynamic_features_allow' => '0' ),
1812 );
1813
1814 $options['show_access_restricted_messages'] = array(
1815 'label' => __( 'Access-related Restricted Messages', 'pods' ),
1816 'help' => array(
1817 __( 'Access-related Restricted Messages will show to anyone who does not have access to add/edit/read a specific item from a content type.', 'pods' ),
1818 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
1819 ),
1820 'type' => 'pick',
1821 'default' => 'inherit',
1822 'pick_format_type' => 'single',
1823 'pick_format_single' => 'radio',
1824 'data' => array(
1825 '1' => __( 'Enable access-related restricted messages for forms/content displayed (instead of the form/content output)', 'pods' ),
1826 '0' => __( 'Disable access-related restricted messages for forms/content displayed (the form/content output will be blank)', 'pods' ),
1827 'inherit' => __( 'Default - Use the global Pods setting for this', 'pods' ),
1828 ),
1829 'depends-on' => array( 'restrict_dynamic_features' => '1' ),
1830 'excludes-on' => array( 'dynamic_features_allow' => '0' ),
1831 );
1832
1833 $options['show_access_admin_notices'] = array(
1834 'label' => __( 'Access-related Admin Notices', 'pods' ),
1835 'help' => array(
1836 __( 'Access-related Admin Notices will only show to admins and will appear above content/forms that may not be entirely public.', 'pods' ),
1837 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
1838 ),
1839 'type' => 'pick',
1840 'default' => 'inherit',
1841 'pick_format_type' => 'single',
1842 'pick_format_single' => 'radio',
1843 'data' => array(
1844 '1' => __( 'Enable access-related admin notices above forms/content displayed', 'pods' ),
1845 '0' => __( 'Disable access-related admin notices above forms/content displayed', 'pods' ),
1846 'inherit' => __( 'Default - Use the global Pods setting for this', 'pods' ),
1847 ),
1848 'depends-on' => array( 'restrict_dynamic_features' => '1' ),
1849 'excludes-on' => array( 'dynamic_features_allow' => '0' ),
1850 );
1851 }
1852
1853 $options['security_access_rights_preview'] = array(
1854 'label' => __( 'Capabilities preview', 'pods' ),
1855 'type' => 'html',
1856 'html_content' => '
1857 <p>' . esc_html__( 'Below is a list of capabilities that a user will normally need for this content.', 'pods' ) . '</p>
1858 ' . pods_access_get_capabilities_preview( $pod_type, $pod_name ),
1859 );
1860
1861 return $options;
1862 }
1863
1864 /**
1865 * Get the list of dynamic features allow options.
1866 *
1867 * @since 3.1.0
1868 *
1869 * @return array The list of dynamic features allow options.
1870 */
1871 function pods_access_get_dynamic_features_allow_options() {
1872 return array(
1873 'inherit' => __( 'WP Default (if content type is Public)', 'pods' ),
1874 '1' => __( 'Enabled', 'pods' ),
1875 '0' => '🔒 ' . __( 'Disabled', 'pods' ),
1876 );
1877 }
1878
1879 /**
1880 * Get the list of restricted dynamic features options.
1881 *
1882 * @since 3.1.0
1883 *
1884 * @return array The list of restricted dynamic features options.
1885 */
1886 function pods_access_get_restricted_dynamic_features_options() {
1887 return array(
1888 'display' => '🔒 ' . __( 'Display', 'pods' ),
1889 'form' => '🔒 ' . __( 'Form', 'pods' ),
1890 );
1891 }
1892
1893 /**
1894 * Get the access rights capabilities preview HTML.
1895 *
1896 * @since 3.1.0
1897 *
1898 * @param string $pod_type The pod type.
1899 * @param string $pod_name The pod name.
1900 *
1901 * @return string The access rights capabilities preview HTML.
1902 */
1903 function pods_access_get_capabilities_preview( $pod_type, $pod_name ) {
1904 $capabilities = pods_access_map_capabilities(
1905 array(
1906 'object_type' => $pod_type,
1907 'object_name' => $pod_name,
1908 ),
1909 null,
1910 true
1911 );
1912
1913 if ( null === $capabilities ) {
1914 $capabilities = array(
1915 'read' => null,
1916 'add' => null,
1917 'edit' => null,
1918 'delete' => null,
1919 );
1920 }
1921
1922 $capabilities_preview = array(
1923 'read' => esc_html__( 'Read capability', 'pods' ),
1924 'add' => esc_html__( 'Add New capability', 'pods' ),
1925 'edit' => esc_html__( 'Edit capability', 'pods' ),
1926 'delete' => esc_html__( 'Delete capability', 'pods' ),
1927 'read_private' => esc_html__( 'Read Private capability', 'pods' ),
1928 'edit_others' => esc_html__( 'Edit Others capability', 'pods' ),
1929 'delete_others' => esc_html__( 'Delete Others capability', 'pods' ),
1930 'delete_published' => esc_html__( 'Delete Published capability', 'pods' ),
1931 'delete_private' => esc_html__( 'Delete Private capability', 'pods' ),
1932 );
1933
1934 $capabilities_preview_list = array(
1935 '<strong>' . $capabilities_preview['read'] . ':</strong> ' . ( $capabilities['read'] ?: __( 'Not restricted', 'pods' ) ),
1936 );
1937
1938 if ( 'settings' !== $pod_type ) {
1939 $capabilities_preview_list[] = '<strong>' . $capabilities_preview['add'] . ':</strong> ' . ( $capabilities['add'] ?: __( 'Not restricted', 'pods' ) );
1940 }
1941
1942 $capabilities_preview_list[] = '<strong>' . $capabilities_preview['edit'] . ':</strong> ' . ( $capabilities['edit'] ?: __( 'Not restricted', 'pods' ) );
1943
1944 if ( 'settings' !== $pod_type ) {
1945 $capabilities_preview_list[] = '<strong>' . $capabilities_preview['delete'] . ':</strong> ' . ( $capabilities['delete'] ?: __( 'Not restricted', 'pods' ) );
1946 }
1947
1948 if ( $capabilities && array_key_exists( 'read_private', $capabilities ) ) {
1949 $capabilities_preview_list[] = '<strong>' . $capabilities_preview['read_private'] . ':</strong> ' . ( $capabilities['read_private'] ?: __( 'Not restricted', 'pods' ) );
1950 }
1951
1952 if ( $capabilities && array_key_exists( 'edit_others', $capabilities ) ) {
1953 $capabilities_preview_list[] = '<strong>' . $capabilities_preview['edit_others'] . ':</strong> ' . ( $capabilities['edit_others'] ?: __( 'Not restricted', 'pods' ) );
1954 }
1955
1956 if ( $capabilities && array_key_exists( 'delete_others', $capabilities ) ) {
1957 $capabilities_preview_list[] = '<strong>' . $capabilities_preview['delete_others'] . ':</strong> ' . ( $capabilities['delete_others'] ?: __( 'Not restricted', 'pods' ) );
1958 }
1959
1960 if ( $capabilities && array_key_exists( 'delete_published', $capabilities ) ) {
1961 $capabilities_preview_list[] = '<strong>' . $capabilities_preview['delete_published'] . ':</strong> ' . ( $capabilities['delete_published'] ?: __( 'Not restricted', 'pods' ) );
1962 }
1963
1964 if ( $capabilities && array_key_exists( 'delete_private', $capabilities ) ) {
1965 $capabilities_preview_list[] = '<strong>' . $capabilities_preview['delete_private'] . ':</strong> ' . ( $capabilities['delete_private'] ?: __( 'Not restricted', 'pods' ) );
1966 }
1967
1968 return '
1969 <ul>
1970 <li>' . implode( '</li><li>', $capabilities_preview_list ) . '</li>
1971 </ul>
1972 ';
1973 }
1974
1975 /**
1976 * Get the pod settings config for access-related settings.
1977 *
1978 * @since 3.1.0
1979 *
1980 * @return array The pod settings config for access-related settings.
1981 */
1982 function pods_access_settings_config() {
1983 $first_pods_version = get_option( 'pods_framework_version_first' );
1984 $first_pods_version = '' === $first_pods_version ? PODS_VERSION : $first_pods_version;
1985
1986 $fields = array();
1987
1988 $fields['dynamic_features_allow'] = array(
1989 'name' => 'dynamic_features_allow',
1990 'label' => __( 'Dynamic Features', 'pods' ),
1991 'help' => array(
1992 __( 'Enabling Dynamic Features will also enable the additional access rights checks for user access. This ensures that people viewing embedded content and forms have the required capabilties. Even when Dynamic Features are disabled, you can still embed Pods Content and Forms through PHP and make use of other features directly through code.', 'pods' ),
1993 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
1994 ),
1995 'description' => __( 'Dynamic features include Pods Shortcodes, Blocks, and Widgets which let you embed content and forms on your site.', 'pods' ),
1996 'type' => 'pick',
1997 'default' => '1',
1998 'pick_format_type' => 'single',
1999 'pick_format_single' => 'radio',
2000 'data' => array(
2001 '1' => __( 'Enable Dynamic Features including Pods Shortcodes, Blocks, and Widgets', 'pods' ),
2002 '0' => __( 'Disable All Dynamic Features in Pods', 'pods' ),
2003 ),
2004 'site_health_data' => array(
2005 '1' => __( 'Enable', 'pods' ),
2006 '0' => __( 'Disable', 'pods' ),
2007 ),
2008 'site_health_include_in_info' => true,
2009 );
2010
2011 $fields['security_access_rights_info'] = array(
2012 'name' => 'security_access_rights_info',
2013 'label' => __( 'How access rights work in Pods', 'pods' ),
2014 'type' => 'html',
2015 'html_content' => sprintf(
2016 '
2017 <p>%1$s</p>
2018 <p><a href="https://docs.pods.io/displaying-pods/access-rights-in-pods/" target="_blank" rel="noopener noreferrer">%2$s</a> <span class="dashicon dashicons dashicons-external"></span></p>
2019 ',
2020 __( 'Pods handles access rights similar to how WordPress itself works.', 'pods' ),
2021 __( 'Read more about how access rights work in Pods on our Documentation site', 'pods' )
2022 ),
2023 'depends-on' => array( 'dynamic_features_allow' => '1' ),
2024 );
2025
2026 $fields['dynamic_features_enabled'] = array(
2027 'name' => 'dynamic_features_enabled',
2028 'label' => __( 'Dynamic Features to Enable', 'pods' ),
2029 'help' => array(
2030 __( 'You can choose one or more dynamic features to enable. By default, only Display and Form are enabled.', 'pods' ),
2031 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
2032 ),
2033 'type' => 'pick',
2034 'default' => array(
2035 'display',
2036 'form',
2037 ),
2038 'pick_format_type' => 'multi',
2039 'pick_format_multi' => 'checkbox',
2040 'data' => array(
2041 'display' => __( 'Display - Shortcodes and Blocks that allow querying content from *any* Pod and displaying any field (WordPress access rights are still checked).', 'pods' ),
2042 'form' => __( 'Form - The Form Shortcode and Block that allows submitting new content or editing existing content from *any* Pod (WordPress access rights are still checked).', 'pods' ),
2043 'view' => __( 'View - The View Shortcode and Block that allows embedding *any* theme file on a page.', 'pods' ),
2044 ),
2045 'site_health_data' => array(
2046 'display' => __( 'Display', 'pods' ),
2047 'form' => __( 'Form', 'pods' ),
2048 'view' => __( 'View', 'pods' ),
2049 ),
2050 'depends-on' => array( 'dynamic_features_allow' => '1' ),
2051 'site_health_include_in_info' => true,
2052 );
2053
2054 $fields['show_access_restricted_messages'] = array(
2055 'name' => 'show_access_restricted_messages',
2056 'label' => __( 'Access-related Restricted Messages', 'pods' ),
2057 'help' => array(
2058 __( 'Access-related Restricted Messages will show to anyone who does not have access to add/edit/read a specific item from a content type.', 'pods' ),
2059 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
2060 ),
2061 'type' => 'pick',
2062 'default' => '0',
2063 'pick_format_type' => 'single',
2064 'pick_format_single' => 'radio',
2065 'data' => array(
2066 '1' => __( 'Enable access-related restricted messages for forms/content displayed (instead of the form/content output)', 'pods' ),
2067 '0' => __( 'Disable access-related restricted messages for forms/content displayed (the form/content output will be blank)', 'pods' ),
2068 ),
2069 'site_health_data' => array(
2070 '1' => __( 'Enable', 'pods' ),
2071 '0' => __( 'Disable', 'pods' ),
2072 ),
2073 'site_health_include_in_info' => true,
2074 'depends-on' => array( 'dynamic_features_allow' => '1' ),
2075 );
2076
2077 $fields['show_access_admin_notices'] = array(
2078 'name' => 'show_access_admin_notices',
2079 'label' => __( 'Access-related Admin Notices', 'pods' ),
2080 'help' => array(
2081 __( 'Access-related Admin Notices will only show to admins and will appear above content/forms that may not be entirely public.', 'pods' ),
2082 'https://docs.pods.io/displaying-pods/access-rights-in-pods/',
2083 ),
2084 'type' => 'pick',
2085 'default' => '1',
2086 'pick_format_type' => 'single',
2087 'pick_format_single' => 'radio',
2088 'data' => array(
2089 '1' => __( 'Enable access-related admin notices above forms/content displayed', 'pods' ),
2090 '0' => __( 'Disable access-related admin notices above forms/content displayed', 'pods' ),
2091 ),
2092 'site_health_data' => array(
2093 '1' => __( 'Enable', 'pods' ),
2094 '0' => __( 'Disable', 'pods' ),
2095 ),
2096 'site_health_include_in_info' => true,
2097 'depends-on' => array( 'dynamic_features_allow' => '1' ),
2098 );
2099
2100 $fields['dynamic_features_allow_sql_clauses'] = array(
2101 'name' => 'dynamic_features_allow_sql_clauses',
2102 'label' => __( 'Allow SQL clauses to be used in Dynamic Features', 'pods' ),
2103 'description' => __( 'SQL clauses in general should only be enabled for sites with trusted users. Since WordPress allows anyone to enter any shortcode or block in the editor, any person with the Contributor role or higher could have access to use this.', 'pods' ),
2104 'type' => 'pick',
2105 'default' => version_compare( $first_pods_version, '3.1.0-a-1', '<' ) ? 'simple' : '0',
2106 'pick_format_type' => 'single',
2107 'pick_format_single' => 'radio',
2108 'data' => array(
2109 'all' => __( 'Unrestricted - Enable ALL SQL clause usage through dynamic features (only use this if you trust ALL users who have access to create content)', 'pods' ),
2110 'simple' => __( 'Restricted - Enable Simple SQL clause usage (only SELECT, WHERE, and ORDER BY) through dynamic features (only use this if you trust ALL users who have access to create content)', 'pods' ),
2111 '0' => __( 'Disable SQL clause usage through dynamic features', 'pods' ),
2112 ),
2113 'site_health_data' => array(
2114 'all' => __( 'Unrestricted', 'pods' ),
2115 'simple' => __( 'Restricted', 'pods' ),
2116 '0' => __( 'Disable', 'pods' ),
2117 ),
2118 'depends-on' => array(
2119 'dynamic_features_allow' => '1',
2120 ),
2121 'depends-on-multi' => array(
2122 'dynamic_features_enabled' => 'display',
2123 ),
2124 'site_health_include_in_info' => true,
2125 );
2126
2127 $fields['display_callbacks'] = array(
2128 'name' => 'display_callbacks',
2129 'label' => __( 'Display callbacks', 'pods' ),
2130 'description' => __( 'Callbacks can be used when using Pods Templating syntax like {@my_field,my_callback} in your magic tags.', 'pods' ),
2131 'type' => 'pick',
2132 'default' => 'restricted',
2133 'pick_format_type' => 'single',
2134 'pick_format_single' => 'radio',
2135 'data' => array(
2136 'restricted' => __( 'Restricted - Certain system PHP functions are disallowed from being used for security reasons.', 'pods' ),
2137 'customized' => __( 'Customized - Only allow a list of specific PHP function callbacks.', 'pods' ),
2138 '0' => __( 'Disable display callbacks', 'pods' ),
2139 ),
2140 'site_health_data' => array(
2141 'restricted' => __( 'Restricted', 'pods' ),
2142 'customized' => __( 'Customized', 'pods' ),
2143 '0' => __( 'Disable', 'pods' ),
2144 ),
2145 'depends-on' => array(
2146 'dynamic_features_allow' => '1',
2147 ),
2148 'depends-on-multi' => array(
2149 'dynamic_features_enabled' => 'display',
2150 ),
2151 'site_health_include_in_info' => true,
2152 );
2153
2154 $fields['display_callbacks_allowed'] = array(
2155 'name' => 'display_callbacks_allowed',
2156 'label' => __( 'Display callbacks allowed', 'pods' ),
2157 'description' => __( 'Please provide a comma-separated list of additional PHP function names to allow in callbacks, on top of the built-in safe list. Each additional function name must start with "custom_pods_callback_" and any other names are ignored for security purposes. You may choose to add custom PHP filter for "pods_access_callbacks_custom_prefix" to change this prefix.', 'pods' ),
2158 'type' => 'text',
2159 'default' => '',
2160 'depends-on' => array(
2161 'dynamic_features_allow' => '1',
2162 'display_callbacks' => 'customized',
2163 ),
2164 'depends-on-multi' => array(
2165 'dynamic_features_enabled' => 'display',
2166 ),
2167 'site_health_include_in_info' => true,
2168 );
2169
2170 $fields['show_display_callback_notices'] = array(
2171 'name' => 'show_display_callback_notices',
2172 'label' => __( 'Display callback notices', 'pods' ),
2173 'description' => __( 'When enabled, Pods will record disallowed display callbacks as they are detected and show an admin notice on the Pods Settings page listing those callbacks.', 'pods' ),
2174 'type' => 'pick',
2175 'default' => '1',
2176 'pick_format_type' => 'single',
2177 'pick_format_single' => 'radio',
2178 'data' => array(
2179 '1' => __( 'Enable admin notices when disallowed display callbacks are detected', 'pods' ),
2180 '0' => __( 'Disable admin notices when disallowed display callbacks are detected', 'pods' ),
2181 ),
2182 'site_health_data' => array(
2183 '1' => __( 'Enable', 'pods' ),
2184 '0' => __( 'Disable', 'pods' ),
2185 ),
2186 'depends-on' => array(
2187 'dynamic_features_allow' => '1',
2188 ),
2189 'depends-on-multi' => array(
2190 'dynamic_features_enabled' => 'display',
2191 ),
2192 'site_health_include_in_info' => true,
2193 );
2194
2195 return $fields;
2196 }
2197
2198 /**
2199 * Get the bleep placeholder text.
2200 *
2201 * @since 3.1.0
2202 *
2203 * @return string The bleep placeholder text.
2204 */
2205 function pods_access_bleep_placeholder() {
2206 return '****************';
2207 }
2208
2209 /**
2210 * Process the value and bleep it if it needs to be.
2211 *
2212 * @since 3.1.0
2213 *
2214 * @param string|mixed $value The value to be bleeped.
2215 *
2216 * @return string|mixed The bleeped text if not empty, otherwise the value as it was.
2217 */
2218 function pods_access_bleep_text( $value ) {
2219 $bleep_text = pods_access_bleep_placeholder();
2220
2221 if ( 0 < strlen( (string) $value ) ) {
2222 $value = $bleep_text;
2223 }
2224
2225 return $value;
2226 }
2227
2228 /**
2229 * Process the data and bleep anything that needs to be.
2230 *
2231 * @since 3.1.0
2232 *
2233 * @param array|object $data The data to be bleeped.
2234 * @param array $additional_bleep_properties The additional properties to be bleeped from objects and arrays.
2235 *
2236 * @return array|object The bleeped data.
2237 */
2238 function pods_access_bleep_data( $data, $additional_bleep_properties = array() ) {
2239 $bleep_properties = array(
2240 'user_pass',
2241 'user_activation_key',
2242 'post_password',
2243 );
2244
2245 /**
2246 * Allow filtering the additional properties to be bleeped from objects and arrays.
2247 *
2248 * @since 3.1.0
2249 *
2250 * @param array $additional_bleep_properties The additional properties to be bleeped from objects and arrays.
2251 * @param array|object $data The data to be bleeped.
2252 */
2253 $additional_bleep_properties = apply_filters( 'pods_access_bleep_properties', $additional_bleep_properties, $data );
2254
2255 $bleep_properties = array_merge( $bleep_properties, $additional_bleep_properties );
2256
2257 $bleep_text = pods_access_bleep_placeholder();
2258
2259 if ( is_object( $data ) ) {
2260 foreach ( $bleep_properties as $bleep_property ) {
2261 if ( isset( $data->{$bleep_property} ) ) {
2262 $data->{$bleep_property} = 0 < strlen( (string) $data->{$bleep_property} ) ? $bleep_text : '';
2263 }
2264 }
2265 } elseif ( is_array( $data ) ) {
2266 foreach ( $bleep_properties as $bleep_property ) {
2267 if ( isset( $data[ $bleep_property ] ) ) {
2268 $data[ $bleep_property ] = 0 < strlen( (string) $data[ $bleep_property ] ) ? $bleep_text : '';
2269 }
2270 }
2271 }
2272
2273 return $data;
2274 }
2275
2276 /**
2277 * Process the data and bleep anything that needs to be.
2278 *
2279 * @since 3.1.0
2280 *
2281 * @param array $items The items to be bleeped.
2282 * @param array $additional_bleep_properties The additional properties to be bleeped from objects and arrays.
2283 *
2284 * @return array|object The bleeped data.
2285 */
2286 function pods_access_bleep_items( $items, $additional_bleep_properties = array() ) {
2287 // Call the pods_access_bleep_data() function for all items in the $items array.
2288 return array_map(
2289 static function ( $item ) use ( $additional_bleep_properties ) {
2290 return pods_access_bleep_data( $item, $additional_bleep_properties );
2291 },
2292 $items
2293 );
2294 }
2295
2296 /**
2297 * Determine whether the SQL fragment is allowed to be used.
2298 *
2299 * @since 3.1.0
2300 *
2301 * @param string $sql The SQL fragment to check.
2302 * @param string $context The SQL fragment context.
2303 * @param array $args {
2304 * The arguments to use.
2305 *
2306 * @type string|null $object_type The object type.
2307 * @type string|null $object_name The object name.
2308 * @type int|string|null $item_id The item ID.
2309 * @type Pods|null $pods The Pods object.
2310 * @type Pod|null $pod The Pod object.
2311 * @type bool $build_pods Whether to try to build a Pods object from the object type/name/ID (false by default).
2312 * @type bool $build_pod Whether to try to build a Pod object from the object type/name (false by default).
2313 * }
2314 *
2315 * @param object|null $params The parameters passed to Pods::find() or PodsData::select().
2316 *
2317 * @return bool Whether the SQL fragment is allowed to be used.
2318 */
2319 function pods_access_sql_fragment_is_allowed( $sql, $context, $args = array(), $params = null ) {
2320 $context = strtoupper( $context );
2321
2322 $info = pods_info_from_args( $args );
2323
2324 /**
2325 * Allows filtering whether the SQL fragment is allowed to be used.
2326 *
2327 * @since 3.1.0
2328 *
2329 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2330 * @param string $sql The SQL fragment to check.
2331 * @param string $context The SQL fragment context.
2332 * @param array $info Pod information.
2333 * @param object|null $params The parameters passed to Pods::find() or PodsData::select().
2334 */
2335 return (bool) apply_filters( 'pods_access_sql_fragment_is_allowed', true, $sql, $context, $info, $params );
2336 }
2337
2338 add_filter( 'pods_access_sql_fragment_is_allowed', 'pods_access_sql_fragment_disallow_mismatch_parenthesis', 10, 2 );
2339 add_filter( 'pods_access_sql_fragment_is_allowed', 'pods_access_sql_fragment_disallow_comments', 10, 2 );
2340 add_filter( 'pods_access_sql_fragment_is_allowed', 'pods_access_sql_fragment_disallow_unsafe_functions', 10, 2 );
2341 add_filter( 'pods_access_sql_fragment_is_allowed', 'pods_access_sql_fragment_disallow_unsafe_keywords', 10, 2 );
2342 add_filter( 'pods_access_sql_fragment_is_allowed', 'pods_access_sql_fragment_disallow_unsafe_tables', 10, 2 );
2343 add_filter( 'pods_access_sql_fragment_is_allowed', 'pods_access_sql_fragment_disallow_double_hyphens', 10, 2 );
2344 add_filter( 'pods_access_sql_fragment_is_allowed', 'pods_access_sql_fragment_disallow_subqueries', 10, 2 );
2345 add_filter( 'pods_access_sql_fragment_is_allowed', 'pods_access_sql_fragment_disallow_post_status', 10, 5 );
2346
2347 /**
2348 * Disallow parenthesis in SQL fragments that are not balanced at every position.
2349 *
2350 * @since 3.1.0
2351 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2352 * @param string $sql The SQL fragment to check.
2353 * @return bool Whether the SQL fragment is allowed to be used.
2354 */
2355 function pods_access_sql_fragment_disallow_mismatch_parenthesis( $allowed, $sql ) {
2356 if ( ! $allowed ) {
2357 return $allowed;
2358 }
2359
2360 // Remove quoted string literals ('' and "" quoting, with backslash/doubled-quote escaping).
2361 $stripped = preg_replace(
2362 array(
2363 "/'(?:[^'\\\\]|\\\\.|'')*'/s",
2364 '/"(?:[^"\\\\]|\\\\.|"")*"/s',
2365 ),
2366 '',
2367 $sql
2368 );
2369
2370 if ( null === $stripped ) {
2371 // preg_replace failed (e.g. malformed input); fail closed.
2372 return false;
2373 }
2374
2375 $depth = 0;
2376 $length = strlen( $stripped );
2377
2378 for ( $i = 0; $i < $length; $i++ ) {
2379 $char = $stripped[ $i ];
2380
2381 if ( '(' === $char ) {
2382 $depth++;
2383 } elseif ( ')' === $char ) {
2384 $depth--;
2385
2386 // More closes than opens at this point: the fragment escapes its wrapping.
2387 if ( $depth < 0 ) {
2388 return false;
2389 }
2390 }
2391 }
2392
2393 return 0 === $depth;
2394 }
2395
2396 /**
2397 * Disallow unsafe functions from being used in SQL fragments.
2398 *
2399 * @since 3.1.0
2400 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2401 * @param string $sql The SQL fragment to check.
2402 * @return bool Whether the SQL fragment is allowed to be used.
2403 */
2404 function pods_access_sql_fragment_disallow_unsafe_functions( $allowed, $sql ) {
2405 if ( ! $allowed ) {
2406 return $allowed;
2407 }
2408
2409 $unsafe_functions = array(
2410 // Server / database / session information functions.
2411 'USER',
2412 'CURRENT_USER',
2413 'SESSION_USER',
2414 'SYSTEM_USER',
2415 'DATABASE',
2416 'SCHEMA',
2417 'VERSION',
2418 'CONNECTION_ID',
2419 'CURRENT_ROLE',
2420 'ROW_COUNT',
2421 'LAST_INSERT_ID',
2422 'CHARSET',
2423 'COLLATION',
2424 'COERCIBILITY',
2425 'STATEMENT_DIGEST',
2426 'STATEMENT_DIGEST_TEXT',
2427
2428 // Filesystem access.
2429 'LOAD_FILE',
2430
2431 // Timing / locking functions.
2432 'SLEEP',
2433 'BENCHMARK',
2434 'GET_LOCK',
2435 'RELEASE_LOCK',
2436 'RELEASE_ALL_LOCKS',
2437 'IS_FREE_LOCK',
2438 'IS_USED_LOCK',
2439 'WAIT_FOR_EXECUTED_GTID_SET',
2440 'WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS',
2441 'MASTER_POS_WAIT',
2442 'SOURCE_POS_WAIT',
2443 'GTID_SUBSET',
2444 'GTID_SUBTRACT',
2445
2446 // Encoding / encryption / compression functions.
2447 'FROM_BASE64',
2448 'TO_BASE64',
2449 'UNHEX',
2450 'AES_ENCRYPT',
2451 'AES_DECRYPT',
2452 'DES_ENCRYPT',
2453 'DES_DECRYPT',
2454 'ENCODE',
2455 'DECODE',
2456 'COMPRESS',
2457 'UNCOMPRESS',
2458 'UNCOMPRESSED_LENGTH',
2459
2460 // Error-based extraction (leak data through forced XPath / other errors).
2461 'EXTRACTVALUE',
2462 'UPDATEXML',
2463
2464 // Deprecated analysis clause.
2465 'ANALYSE',
2466
2467 // Common lib_mysqludf_sys UDFs.
2468 'SYS_EXEC',
2469 'SYS_EVAL',
2470 );
2471
2472 /**
2473 * Allow filtering the list of additional unsafe functions to disallow.
2474 *
2475 * @since 3.1.0
2476 *
2477 * @param array $unsafe_functions The list of unsafe functions to disallow.
2478 * @param string $sql The SQL fragment to check.
2479 */
2480 $additional_unsafe_functions = (array) apply_filters( 'pods_access_sql_fragment_disallow_unsafe_functions', $unsafe_functions, $sql );
2481
2482 $unsafe_functions = array_unique( array_filter( array_merge( $unsafe_functions, $additional_unsafe_functions ) ) );
2483
2484 foreach ( $unsafe_functions as $unsafe_function ) {
2485 if ( 1 === (int) preg_match( '/\s*' . preg_quote( $unsafe_function, '/' ) . '\s*\(/i', $sql ) ) {
2486 return false;
2487 }
2488 }
2489
2490 return $allowed;
2491 }
2492
2493 /**
2494 * Disallow unsafe tables from being used in SQL fragments.
2495 *
2496 * @since 3.1.0
2497 *
2498 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2499 * @param string $sql The SQL fragment to check.
2500 *
2501 * @return bool Whether the SQL fragment is allowed to be used.
2502 */
2503 function pods_access_sql_fragment_disallow_unsafe_tables( $allowed, $sql ) {
2504 if ( ! $allowed ) {
2505 return $allowed;
2506 }
2507
2508 $unsafe_tables = array(
2509 'mysql.',
2510 'information_schema.',
2511 'performance_schema.',
2512 'sys.',
2513 );
2514
2515 /**
2516 * Allow filtering the list of unsafe tables to disallow.
2517 *
2518 * @since 3.1.0
2519 *
2520 * @param array $unsafe_tables The list of unsafe tables to disallow.
2521 * @param string $sql The SQL fragment to check.
2522 */
2523 $unsafe_tables = (array) apply_filters( 'pods_access_sql_fragment_disallow_unsafe_tables', $unsafe_tables, $sql );
2524
2525 $unsafe_tables = array_filter( $unsafe_tables );
2526
2527 /*
2528 * Normalize the fragment before matching so that identifier quoting and
2529 * spacing around the "." separator cannot be used to evade the check, e.g.
2530 * "`information_schema`.`tables`" or "information_schema . tables" both
2531 * normalize to "information_schema.tables".
2532 */
2533 $normalized_sql = str_replace( '`', '', $sql );
2534 $normalized_sql = preg_replace( '/\s*\.\s*/', '.', $normalized_sql );
2535
2536 foreach ( $unsafe_tables as $unsafe_table ) {
2537 if ( 1 === (int) preg_match( '/' . preg_quote( $unsafe_table, '/' ) . '/i', $normalized_sql ) ) {
2538 return false;
2539 }
2540 }
2541
2542 return $allowed;
2543 }
2544
2545 /**
2546 * Disallow double hyphens from being used in SQL fragments.
2547 *
2548 * @since 3.1.0
2549 *
2550 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2551 * @param string $sql The SQL fragment to check.
2552 *
2553 * @return bool Whether the SQL fragment is allowed to be used.
2554 */
2555 function pods_access_sql_fragment_disallow_double_hyphens( $allowed, $sql ) {
2556 return (
2557 $allowed
2558 && false === strpos( $sql, '--' )
2559 );
2560 }
2561
2562 /**
2563 * Disallow SQL comment markers from being used in SQL fragments.
2564 *
2565 * @since 3.1.0
2566 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2567 * @param string $sql The SQL fragment to check.
2568 * @return bool Whether the SQL fragment is allowed to be used.
2569 */
2570 function pods_access_sql_fragment_disallow_comments( $allowed, $sql ) {
2571 if ( ! $allowed ) {
2572 return $allowed;
2573 }
2574
2575 if (
2576 false !== strpos( $sql, '--' )
2577 || false !== strpos( $sql, '/*' )
2578 || false !== strpos( $sql, '*/' )
2579 ) {
2580 return false;
2581 }
2582
2583 // Strip quoted string literals so a "#" inside a value is not treated as a comment.
2584 $stripped = preg_replace(
2585 array(
2586 "/'(?:[^'\\\\]|\\\\.|'')*'/s",
2587 '/"(?:[^"\\\\]|\\\\.|"")*"/s',
2588 ),
2589 '',
2590 $sql
2591 );
2592
2593 if ( null === $stripped ) {
2594 // preg_replace failed (e.g. malformed input); fail closed.
2595 return false;
2596 }
2597
2598 return false === strpos( $stripped, '#' );
2599 }
2600
2601 /**
2602 * Disallow unsafe keywords from being used in SQL fragments.
2603 *
2604 * @since 3.1.0
2605 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2606 * @param string $sql The SQL fragment to check.
2607 * @return bool Whether the SQL fragment is allowed to be used.
2608 */
2609 function pods_access_sql_fragment_disallow_unsafe_keywords( $allowed, $sql ) {
2610 if ( ! $allowed ) {
2611 return $allowed;
2612 }
2613
2614 $unsafe_patterns = array(
2615 // System / session variables.
2616 '/@@/',
2617 // Combining result sets.
2618 '/\bUNION\b/i',
2619 // File output keywords.
2620 '/\bINTO\s+(?:OUTFILE|DUMPFILE)\b/i',
2621 // File read keywords.
2622 '/\bLOAD\s+DATA\b/i',
2623 // Statement separator.
2624 '/;/',
2625 );
2626
2627 /**
2628 * Allow filtering the list of unsafe keyword patterns to disallow.
2629 *
2630 * Each entry is a full PCRE pattern (including delimiters and flags) that is
2631 * tested against the SQL fragment; a match disallows the fragment.
2632 *
2633 * @since 3.1.0
2634 *
2635 * @param array $unsafe_patterns The list of unsafe keyword patterns to disallow.
2636 * @param string $sql The SQL fragment to check.
2637 */
2638 $unsafe_patterns = (array) apply_filters( 'pods_access_sql_fragment_disallow_unsafe_keywords', $unsafe_patterns, $sql );
2639
2640 $unsafe_patterns = array_filter( $unsafe_patterns );
2641
2642 foreach ( $unsafe_patterns as $unsafe_pattern ) {
2643 if ( 1 === (int) preg_match( $unsafe_pattern, $sql ) ) {
2644 return false;
2645 }
2646 }
2647
2648 return $allowed;
2649 }
2650
2651 /**
2652 * Disallow subqueries from being used in SQL fragments.
2653 *
2654 * @since 3.1.0
2655 *
2656 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2657 * @param string $sql The SQL fragment to check.
2658 *
2659 * @return bool Whether the SQL fragment is allowed to be used.
2660 */
2661 function pods_access_sql_fragment_disallow_subqueries( $allowed, $sql ) {
2662 return (
2663 $allowed
2664 && 0 === (int) preg_match( '/\s*SELECT(\s|\()+/i', $sql )
2665 );
2666 }
2667
2668 /**
2669 * Disallow post_status from being used in the WHERE/HAVING/FIELD SQL fragment unless they have admin access,
2670 * can edit posts for the post type, or the fragment only compares post_status to publish.
2671 *
2672 * @since 3.1.0
2673 *
2674 * @param bool $allowed Whether the SQL fragment is allowed to be used.
2675 * @param string $sql The SQL fragment to check.
2676 * @param string $context The SQL fragment context.
2677 * @param array $info Pod information.
2678 * @param object|null $params The parameters passed to Pods::find() or PodsData::select().
2679 *
2680 * @return bool Whether the SQL fragment is allowed to be used.
2681 */
2682 function pods_access_sql_fragment_disallow_post_status( $allowed, $sql, $context, $info, $params = null ) {
2683 if ( ! $allowed ) {
2684 return $allowed;
2685 }
2686
2687 if ( 'WHERE' !== $context && 'HAVING' !== $context && 'FIELD' !== $context ) {
2688 return true;
2689 }
2690
2691 // Check if post_status is allowed.
2692 if ( false === stripos( $sql, 'post_status' ) ) {
2693 return true;
2694 }
2695
2696 if ( empty( $params ) || empty( $params->from ) || ! in_array( $params->from, array( 'dynamic-embed', 'pick/get_object_data' ), true ) ) {
2697 return true;
2698 }
2699
2700 if ( pods_is_admin() ) {
2701 return true;
2702 }
2703
2704 if (
2705 ! empty( $info['object_type'] )
2706 && 'post_type' === $info['object_type']
2707 && ! empty( $info['object_name'] )
2708 ) {
2709 $post_type_object = get_post_type_object( $info['object_name'] );
2710
2711 if (
2712 $post_type_object instanceof WP_Post_Type
2713 && $post_type_object->cap->edit_posts
2714 && current_user_can( $post_type_object->cap->edit_posts )
2715 ) {
2716 return true;
2717 }
2718 }
2719
2720 // Check for variations and exclude them, if post_status still matches then return false.
2721 $safe_sql = preg_replace(
2722 '/post_status\s*=\s*(?:\'publish\'|"publish")/i',
2723 '',
2724 $sql
2725 );
2726
2727 return (
2728 null === $safe_sql
2729 || false === stripos( $safe_sql, 'post_status' )
2730 );
2731 }
2732
2733 /**
2734 * Safely unserialize data if it's PHP serialized.
2735 *
2736 * @since 3.1.0
2737 *
2738 * @param string|mixed $data The data to unserialize.
2739 *
2740 * @return array|string|mixed The unserialized data if it was PHP serialized, otherwise the data as it was.
2741 */
2742 function pods_maybe_safely_unserialize( $data ) {
2743 // The $options parameter of unserialize() requires PHP 7.0+.
2744 if ( version_compare( PHP_VERSION, '7.0', '<' ) ) {
2745 // On PHP < 7, refuse payloads that contain a serialized object; other data falls back to the normal WP function, to help prevent security issues.
2746 if ( is_string( $data ) && preg_match( '/(?:^|;|{)[OC]:\d+:"/', $data ) ) {
2747 return $data;
2748 }
2749
2750 // Fall back to normal WP function.
2751 return maybe_unserialize( $data );
2752 }
2753
2754 // Check if the data is serialized.
2755 if ( is_serialized( $data ) ) {
2756 $data = trim( $data );
2757
2758 // Unserialize the data but exclude classes.
2759 return @unserialize( $data, array( 'allowed_classes' => false ) );
2760 }
2761
2762 return $data;
2763 }
2764
2765 /**
2766 * Get the field name map used for Pods form nonce hidden inputs.
2767 *
2768 * @since 3.3.9.2
2769 *
2770 * @param string $context The form context. Accepts 'form' or 'meta'.
2771 * @param string $group_key Optional group key used to suffix the field names so multiple
2772 * groups on the same page do not collide (defaults to empty).
2773 *
2774 * @return array {
2775 * The hidden field names.
2776 *
2777 * @type string $nonce The nonce field name.
2778 * @type string $pod The pod field name.
2779 * @type string $id The item ID field name.
2780 * @type string $uri The URI hash field name.
2781 * @type string $form The field list field name.
2782 * }
2783 */
2784 function pods_access_form_field_names( $context, $group_key = '' ) {
2785 $prefix = '_pods_';
2786
2787 if ( 'meta' === $context ) {
2788 $prefix = 'pods_meta_';
2789 }
2790
2791 $suffix = '';
2792
2793 if ( is_scalar( $group_key ) && '' !== (string) $group_key ) {
2794 $group_key = sanitize_key( (string) $group_key );
2795
2796 if ( '' !== $group_key ) {
2797 $suffix = '_' . $group_key;
2798 }
2799 }
2800
2801 return array(
2802 'nonce' => $prefix . 'nonce' . $suffix,
2803 'pod' => $prefix . 'pod' . $suffix,
2804 'id' => $prefix . 'id' . $suffix,
2805 'uri' => $prefix . 'uri' . $suffix,
2806 'form' => $prefix . 'form' . $suffix,
2807 );
2808 }
2809
2810 /**
2811 * Get the UID used for Pods form nonces.
2812 *
2813 * @since 3.3.9.2
2814 *
2815 * @return string The UID.
2816 */
2817 function pods_access_form_uid() {
2818 if ( is_user_logged_in() ) {
2819 return 'user_' . get_current_user_id();
2820 }
2821
2822 return pods_session_id();
2823 }
2824
2825 /**
2826 * Get the URI hash used for Pods form nonces.
2827 *
2828 * @since 3.3.9.2
2829 *
2830 * @param string|null $path The request path. Defaults to the current path.
2831 *
2832 * @return string The URI hash.
2833 */
2834 function pods_access_form_uri_hash( $path = null ) {
2835 if ( null === $path || '' === $path ) {
2836 $path = pods_current_path();
2837 }
2838
2839 return wp_create_nonce( 'pods_uri_' . (string) $path );
2840 }
2841
2842 /**
2843 * Normalize a list of form fields to a comma-separated string.
2844 *
2845 * @since 3.3.9.2
2846 *
2847 * @param array|string $submitted_fields The fields array or comma-separated string.
2848 *
2849 * @return string The normalized field list.
2850 */
2851 function pods_access_form_normalize_fields( $submitted_fields ) {
2852 if ( is_string( $submitted_fields ) ) {
2853 return $submitted_fields;
2854 }
2855
2856 if ( ! is_array( $submitted_fields ) ) {
2857 return '';
2858 }
2859
2860 if ( isset( $submitted_fields[0] ) && is_string( $submitted_fields[0] ) ) {
2861 $names = array();
2862
2863 foreach ( $submitted_fields as $submitted_field ) {
2864 if ( ! is_scalar( $submitted_field ) ) {
2865 return implode( ',', array_keys( $submitted_fields ) );
2866 }
2867
2868 $names[] = (string) $submitted_field;
2869 }
2870
2871 return implode( ',', $names );
2872 }
2873
2874 return implode( ',', array_keys( $submitted_fields ) );
2875 }
2876
2877 /**
2878 * Get the field hash used for Pods form nonces.
2879 *
2880 * @since 3.3.9.2
2881 *
2882 * @param array|string $submitted_fields The fields array or comma-separated string.
2883 *
2884 * @return string The field hash.
2885 */
2886 function pods_access_form_field_hash( $submitted_fields ) {
2887 $form = pods_access_form_normalize_fields( $submitted_fields );
2888
2889 return wp_create_nonce( 'pods_fields_' . $form );
2890 }
2891
2892 /**
2893 * Build the nonce action string for a Pods form.
2894 *
2895 * @since 3.3.9.2
2896 *
2897 * @param string $pod The Pod name.
2898 * @param int|string $id The item ID.
2899 * @param array|string $submitted_fields The fields array or comma-separated string.
2900 * @param string|null $uri_hash The URI hash. Defaults to the current path hash.
2901 * @param string|null $uid The UID. Defaults to the current user or session ID.
2902 *
2903 * @return string The nonce action string.
2904 */
2905 function pods_access_form_nonce_action( $pod, $id, $submitted_fields, $uri_hash = null, $uid = null ) {
2906 if ( null === $uri_hash ) {
2907 $uri_hash = pods_access_form_uri_hash();
2908 }
2909
2910 if ( null === $uid ) {
2911 $uid = pods_access_form_uid();
2912 }
2913
2914 $field_hash = pods_access_form_field_hash( $submitted_fields );
2915
2916 return 'pods_form_' . (string) $pod . '_' . (string) $uid . '_' . (int) $id . '_' . (string) $uri_hash . '_' . (string) $field_hash;
2917 }
2918
2919 /**
2920 * Create a Pods form nonce.
2921 *
2922 * @since 3.3.9.2
2923 *
2924 * @param string $pod The Pod name.
2925 * @param int|string $id The item ID.
2926 * @param array|string $submitted_fields The fields array or comma-separated string.
2927 * @param string|null $uri_hash The URI hash. Defaults to the current path hash.
2928 *
2929 * @return string The nonce.
2930 */
2931 function pods_access_create_form_nonce( $pod, $id, $submitted_fields, $uri_hash = null ) {
2932 return wp_create_nonce( pods_access_form_nonce_action( $pod, $id, $submitted_fields, $uri_hash ) );
2933 }
2934
2935 /**
2936 * Verify a Pods form nonce.
2937 *
2938 * @since 3.3.9.2
2939 *
2940 * @param string $nonce The nonce value.
2941 * @param string $pod The Pod name.
2942 * @param int|string $id The item ID.
2943 * @param array|string $submitted_fields The fields array or comma-separated string.
2944 * @param string|null $uri_hash The URI hash. Defaults to the current path hash.
2945 *
2946 * @return bool Whether the nonce is valid.
2947 */
2948 function pods_access_verify_form_nonce( $nonce, $pod, $id, $submitted_fields, $uri_hash = null ) {
2949 if ( ! is_scalar( $nonce ) || '' === $nonce ) {
2950 return false;
2951 }
2952
2953 $uid = pods_access_form_uid();
2954
2955 if ( empty( $uid ) ) {
2956 return false;
2957 }
2958
2959 $action = pods_access_form_nonce_action( $pod, $id, $submitted_fields, $uri_hash, $uid );
2960
2961 return false !== wp_verify_nonce( (string) $nonce, $action );
2962 }
2963
2964 /**
2965 * Get hidden fields for a Pods form nonce as an HTML string.
2966 *
2967 * @since 3.3.9.2
2968 *
2969 * @param string $pod The Pod name.
2970 * @param int|string $id The item ID.
2971 * @param array|string $submitted_fields The fields array or comma-separated string.
2972 * @param array|null $nonce_field_names The hidden nonce field names. Defaults to standard nonce form fields.
2973 * @param string|null $uri_hash The URI hash. Defaults to the current path hash.
2974 *
2975 * @return string The hidden field HTML.
2976 */
2977 function pods_access_get_form_nonce_fields( $pod, $id, $submitted_fields, $nonce_field_names = null, $uri_hash = null ) {
2978 if ( null === $nonce_field_names ) {
2979 $nonce_field_names = pods_access_form_field_names( 'form' );
2980 }
2981
2982 if ( null === $uri_hash ) {
2983 $uri_hash = pods_access_form_uri_hash();
2984 }
2985
2986 $form = pods_access_form_normalize_fields( $submitted_fields );
2987 $nonce = pods_access_create_form_nonce( $pod, $id, $submitted_fields, $uri_hash );
2988
2989 $html = PodsForm::field( $nonce_field_names['nonce'], $nonce, 'hidden' );
2990 $html .= PodsForm::field( $nonce_field_names['pod'], (string) $pod, 'hidden' );
2991 $html .= PodsForm::field( $nonce_field_names['id'], (int) $id, 'hidden' );
2992 $html .= PodsForm::field( $nonce_field_names['uri'], (string) $uri_hash, 'hidden' );
2993 $html .= PodsForm::field( $nonce_field_names['form'], $form, 'hidden' );
2994
2995 return $html;
2996 }
2997
2998 /**
2999 * Output hidden fields for a Pods form nonce.
3000 *
3001 * @since 3.3.9.2
3002 *
3003 * @param string $pod The Pod name.
3004 * @param int|string $id The item ID.
3005 * @param array|string $submitted_fields The fields array or comma-separated string.
3006 * @param array|null $nonce_field_names The hidden nonce field names. Defaults to standard nonce form fields.
3007 * @param string|null $uri_hash The URI hash. Defaults to the current path hash.
3008 *
3009 * @return void
3010 */
3011 function pods_access_output_form_nonce_fields( $pod, $id, $submitted_fields, $nonce_field_names = null, $uri_hash = null ) {
3012 echo pods_access_get_form_nonce_fields( $pod, $id, $submitted_fields, $nonce_field_names, $uri_hash ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
3013 }
3014
3015 /**
3016 * Verify a Pods form nonce from the request.
3017 *
3018 * @since 3.3.9.2
3019 *
3020 * @param array|null $nonce_field_names The hidden nonce field names. Defaults to standard nonce form fields.
3021 * @param string $source The request source. Defaults to 'post'.
3022 *
3023 * @return bool Whether the nonce is valid.
3024 */
3025 function pods_access_verify_form_nonce_from_request( $nonce_field_names = null, $source = 'post' ) {
3026 if ( null === $nonce_field_names ) {
3027 $nonce_field_names = pods_access_form_field_names( 'form' );
3028 }
3029
3030 $nonce = pods_v( $nonce_field_names['nonce'], $source );
3031 $pod = pods_v( $nonce_field_names['pod'], $source );
3032 $id = pods_v( $nonce_field_names['id'], $source );
3033 $uri = pods_v( $nonce_field_names['uri'], $source );
3034 $form = pods_v( $nonce_field_names['form'], $source );
3035
3036 if (
3037 ! is_string( $nonce )
3038 || ! is_string( $pod )
3039 || ( ! is_string( $id ) && ! is_numeric( $id ) )
3040 || ! is_string( $uri )
3041 || ! is_string( $form )
3042 || '' === $nonce
3043 || '' === $pod
3044 || '' === $uri
3045 || '' === $form
3046 ) {
3047 return false;
3048 }
3049
3050 return pods_access_verify_form_nonce( (string) $nonce, (string) $pod, (int) $id, (string) $form, (string) $uri );
3051 }
3052
3053 /**
3054 * Determine whether a Pods form nonce is present in the request.
3055 *
3056 * This does not verify the nonce value, only whether the nonce field was submitted.
3057 *
3058 * @since 3.3.9.2
3059 *
3060 * @param string $context The form context. Accepts 'form' or 'meta'.
3061 * @param string $group_key Optional group key used to suffix the field names so multiple
3062 * groups on the same page do not collide (defaults to empty).
3063 * @param string $source The request source. Defaults to 'post'.
3064 *
3065 * @return bool Whether the nonce field is present.
3066 */
3067 function pods_access_form_nonce_present_in_request( $context = 'form', $group_key = '', $source = 'post' ) {
3068 $nonce_field_names = pods_access_form_field_names( $context, $group_key );
3069 $nonce = pods_v( $nonce_field_names['nonce'], $source );
3070
3071 return is_string( $nonce );
3072 }
3073