PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.7.31.4
Pods – Custom Content Types and Fields v2.7.31.4
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 / components / Pages.php
pods / components Last commit date
Builder 2 days ago I18n 2 days ago Migrate-CPTUI 2 days ago Migrate-Packages 2 days ago Roles 2 days ago Templates 2 days ago Advanced-Content-Types.php 2 days ago Advanced-Relationships.php 2 days ago Helpers.php 2 days ago Markdown.php 2 days ago Pages.php 2 days ago Table-Storage.php 2 days ago
Pages.php
1384 lines
1 <?php
2 /**
3 * Name: Pages
4 *
5 * Menu Name: Pod Pages
6 *
7 * Description: Creates advanced URL structures using wildcards in order to enable the front-end display of Pods Advanced Content Types. Not recommended for use with other content types.
8 *
9 * Version: 2.3
10 *
11 * Category: Advanced
12 *
13 * Menu Page: edit.php?post_type=_pods_page
14 * Menu Add Page: post-new.php?post_type=_pods_page
15 *
16 * @package Pods\Components
17 * @subpackage Pages
18 */
19
20 if ( class_exists( 'Pods_Pages' ) ) {
21 return;
22 }
23
24 /**
25 * Class Pods_Pages
26 */
27 class Pods_Pages extends PodsComponent {
28
29 /**
30 * Current Pod Page
31 *
32 * @var array
33 *
34 * @since 2.0.0
35 */
36 public static $exists = null;
37
38 /**
39 * Object type
40 *
41 * @var string
42 *
43 * @since 2.0.0
44 */
45 private $object_type = '_pods_page';
46
47 /**
48 * Whether the page has been checked already
49 *
50 * @var bool
51 *
52 * @since 2.1.0
53 */
54 public static $checked = false;
55
56 /**
57 * Keep track of if pods_content has been called yet
58 *
59 * @var bool
60 *
61 * @since 2.3.0
62 */
63 public static $content_called = false;
64
65 /**
66 * The capability type.
67 *
68 * @link https://codex.wordpress.org/Function_Reference/register_post_type
69 * @var string
70 */
71 private $capability_type = 'pods_page';
72
73 /**
74 * {@inheritdoc}
75 */
76 public function init() {
77
78 add_shortcode( 'pods-content', array( $this, 'shortcode' ) );
79
80 $args = array(
81 'label' => 'Pod Pages',
82 'labels' => array( 'singular_name' => 'Pod Page' ),
83 'public' => false,
84 'can_export' => false,
85 'show_ui' => true,
86 'show_in_menu' => false,
87 'query_var' => false,
88 'rewrite' => false,
89 'has_archive' => false,
90 'hierarchical' => false,
91 'supports' => array( 'title', 'author', 'revisions' ),
92 'menu_icon' => 'dashicons-pods',
93 );
94
95 if ( ! pods_is_admin() ) {
96 $args['capability_type'] = $this->capability_type;
97 }
98
99 $args = PodsInit::object_label_fix( $args, 'post_type' );
100
101 register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_page', $args ) );
102
103 add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 );
104
105 if ( ! is_admin() ) {
106 add_action( 'load_textdomain', array( $this, 'page_check' ), 12 );
107 } else {
108 add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
109
110 add_action( 'add_meta_boxes_' . $this->object_type, array( $this, 'edit_page_form' ) );
111
112 add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) );
113 add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 );
114 add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 );
115
116 add_action( 'pods_meta_save_pre_post__pods_page', array( $this, 'fix_filters' ), 10, 5 );
117 add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 );
118 add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 );
119 add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 );
120 add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) );
121
122 add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) );
123 }
124
125 add_filter( 'members_get_capabilities', array( $this, 'get_capabilities' ) );
126 }
127
128 /**
129 * @param $caps
130 *
131 * @return array
132 */
133 public function get_capabilities( $caps ) {
134
135 $caps = array_merge(
136 $caps, array(
137 'edit_' . $this->capability_type,
138 'read_' . $this->capability_type,
139 'delete_' . $this->capability_type,
140 'edit_' . $this->capability_type . 's',
141 'edit_others_' . $this->capability_type . 's',
142 'publish_' . $this->capability_type . 's',
143 'read_private_' . $this->capability_type . 's',
144 'edit_' . $this->capability_type . 's',
145 )
146 );
147
148 return $caps;
149 }
150
151 /**
152 * Pod Page Content Shortcode support for use anywhere that supports WP Shortcodes
153 *
154 * @param array $tags An associative array of shortcode properties
155 * @param string $content Not currently used
156 *
157 * @return string
158 * @since 2.3.9
159 */
160 public function shortcode( $tags, $content = null ) {
161
162 if ( ! isset( $tags['page'] ) || empty( $tags['page'] ) ) {
163 $tags['page'] = null;
164 }
165
166 $pods_page = self::exists( $tags['page'] );
167
168 if ( empty( $pods_page ) ) {
169 return '<p>Pods Page not found</p>';
170 }
171
172 return self::content( true, $pods_page );
173 }
174
175 /**
176 * Disable this Post Type from appearing in the Builder layouts list
177 *
178 * @param array $post_types
179 *
180 * @return array
181 */
182 public function disable_builder_layout( $post_types ) {
183
184 $post_types[] = $this->object_type;
185
186 return $post_types;
187 }
188
189 /**
190 * Update Post Type messages
191 *
192 * @param array $messages
193 *
194 * @return array
195 * @since 2.0.2
196 */
197 public function setup_updated_messages( $messages ) {
198
199 global $post, $post_ID;
200
201 $post_type = get_post_type_object( $this->object_type );
202
203 $labels = $post_type->labels;
204
205 $messages[ $post_type->name ] = array(
206 1 => sprintf( __( '%1$s updated. <a href="%2$s">%3$s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),
207 2 => __( 'Custom field updated.', 'pods' ),
208 3 => __( 'Custom field deleted.', 'pods' ),
209 4 => sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ),
210 /* translators: %s: date and time of the revision */
211 5 => isset( $_GET['revision'] ) ? sprintf( __( '%1$s restored to revision from %2$s', 'pods' ), $labels->singular_name, wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
212 6 => sprintf( __( '%1$s published. <a href="%2$s">%3$s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),
213 7 => sprintf( __( '%s saved.', 'pods' ), $labels->singular_name ),
214 8 => sprintf( __( '%1$s submitted. <a target="_blank" rel="noopener noreferrer" href="%2$s">Preview %3$s</a>', 'pods' ), $labels->singular_name, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels->singular_name ),
215 9 => sprintf(
216 __( '%1$s scheduled for: <strong>%2$s</strong>. <a target="_blank" rel="noopener noreferrer" href="%3$s">Preview %4$s</a>', 'pods' ), $labels->singular_name,
217 // translators: Publish box date format, see http://php.net/date
218 date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), $labels->singular_name
219 ),
220 10 => sprintf( __( '%1$s draft updated. <a target="_blank" rel="noopener noreferrer" href="%2$s">Preview %3$s</a>', 'pods' ), $labels->singular_name, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels->singular_name ),
221 );
222
223 if ( false === (boolean) $post_type->public ) {
224 $messages[ $post_type->name ][1] = sprintf( __( '%s updated.', 'pods' ), $labels->singular_name );
225 $messages[ $post_type->name ][6] = sprintf( __( '%s published.', 'pods' ), $labels->singular_name );
226 $messages[ $post_type->name ][8] = sprintf( __( '%s submitted.', 'pods' ), $labels->singular_name );
227 $messages[ $post_type->name ][9] = sprintf(
228 __( '%1$s scheduled for: <strong>%2$s</strong>.', 'pods' ), $labels->singular_name,
229 // translators: Publish box date format, see http://php.net/date
230 date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
231 );
232 $messages[ $post_type->name ][10] = sprintf( __( '%s draft updated.', 'pods' ), $labels->singular_name );
233 }
234
235 return $messages;
236 }
237
238 /**
239 * Enqueue styles
240 *
241 * @since 2.0.0
242 */
243 public function admin_assets() {
244
245 wp_enqueue_style( 'pods-styles' );
246 }
247
248 /**
249 * Fix filters, specifically removing balanceTags
250 *
251 * @since 2.0.1
252 *
253 * @param $data
254 * @param null $pod
255 * @param null $id
256 * @param null $groups
257 * @param null $post
258 */
259 public function fix_filters( $data, $pod = null, $id = null, $groups = null, $post = null ) {
260
261 remove_filter( 'content_save_pre', 'balanceTags', 50 );
262 }
263
264 /**
265 * Remove unused row actions
266 *
267 * @since 2.0.5
268 *
269 * @param $actions
270 * @param $post
271 *
272 * @return
273 */
274 public function remove_row_actions( $actions, $post ) {
275
276 global $current_screen;
277
278 if ( ! is_object( $current_screen ) || $this->object_type != $current_screen->post_type ) {
279 return $actions;
280 }
281
282 if ( isset( $actions['view'] ) ) {
283 unset( $actions['view'] );
284 }
285
286 if ( isset( $actions['inline hide-if-no-js'] ) ) {
287 unset( $actions['inline hide-if-no-js'] );
288 }
289
290 // W3 Total Cache
291 if ( isset( $actions['pgcache_purge'] ) ) {
292 unset( $actions['pgcache_purge'] );
293 }
294
295 return $actions;
296 }
297
298 /**
299 * Remove unused bulk actions
300 *
301 * @since 2.0.5
302 *
303 * @param $actions
304 *
305 * @return
306 */
307 public function remove_bulk_actions( $actions ) {
308
309 if ( isset( $actions['edit'] ) ) {
310 unset( $actions['edit'] );
311 }
312
313 return $actions;
314 }
315
316 /**
317 * Clear cache on save
318 *
319 * @since 2.0.0
320 *
321 * @param $data
322 * @param null $pod
323 * @param null $id
324 * @param null $groups
325 * @param null $post
326 */
327 public function clear_cache( $data, $pod = null, $id = null, $groups = null, $post = null ) {
328
329 $old_post = $id;
330
331 if ( ! is_object( $id ) ) {
332 $old_post = null;
333 }
334
335 if ( is_object( $post ) && $this->object_type != $post->post_type ) {
336 return;
337 }
338
339 if ( ! is_array( $data ) && 0 < $data ) {
340 $post = $data;
341 $post = get_post( $post );
342 }
343
344 if ( $this->object_type == $post->post_type ) {
345 pods_transient_clear( 'pods_object_pages' );
346
347 if ( is_object( $old_post ) && $this->object_type == $old_post->post_type ) {
348 pods_cache_clear( $old_post->post_title, 'pods_object_page_wildcard' );
349 }
350
351 pods_cache_clear( $post->post_title, 'pods_object_page_wildcard' );
352
353 self::flush_rewrites();
354 }
355 }
356
357 /**
358 * Change post title placeholder text
359 *
360 * @since 2.0.0
361 *
362 * @param $text
363 * @param $post
364 *
365 * @return string|void
366 */
367 public function set_title_text( $text, $post ) {
368
369 return __( 'Enter URL here', 'pods' );
370 }
371
372 /**
373 * Edit page form
374 *
375 * @since 2.0.0
376 */
377 public function edit_page_form() {
378 add_filter( 'enter_title_here', array( $this, 'set_title_text' ), 10, 2 );
379 }
380
381 /**
382 * Filter permalinks and adjust for pod pages
383 *
384 * @param $post_link
385 * @param $post
386 *
387 * @return mixed
388 */
389 public function post_type_link( $post_link, $post ) {
390
391 if ( empty( $post ) || $this->object_type != $post->post_type ) {
392 return $post_link;
393 }
394
395 $post_link = get_site_url() . '/';
396
397 if ( false === strpos( $post->post_title, '*' ) ) {
398 $post_link .= trim( $post->post_title, '/ ' ) . '/';
399 }
400
401 return $post_link;
402 }
403
404 /**
405 * Add meta boxes to the page
406 *
407 * @since 2.0.0
408 */
409 public function add_meta_boxes() {
410
411 $pod = array(
412 'name' => $this->object_type,
413 'type' => 'post_type',
414 );
415
416 if ( isset( PodsMeta::$post_types[ $pod['name'] ] ) ) {
417 return;
418 }
419
420 if ( ! function_exists( 'get_page_templates' ) ) {
421 include_once ABSPATH . 'wp-admin/includes/theme.php';
422 }
423
424 $page_templates = apply_filters( 'pods_page_templates', get_page_templates() );
425
426 $page_templates[ __( '-- Page Template --', 'pods' ) ] = '';
427
428 $page_templates[ __( 'Custom (uses only Pod Page content)', 'pods' ) ] = '_custom';
429
430 if ( ! in_array( 'pods.php', $page_templates, true ) && locate_template( array( 'pods.php', false ) ) ) {
431 $page_templates[ __( 'Pods (Pods Default)', 'pods' ) ] = 'pods.php';
432 }
433
434 if ( ! in_array( 'page.php', $page_templates, true ) && locate_template( array( 'page.php', false ) ) ) {
435 $page_templates[ __( 'Page (WP Default)', 'pods' ) ] = 'page.php';
436 }
437
438 if ( ! in_array( 'index.php', $page_templates, true ) && locate_template( array( 'index.php', false ) ) ) {
439 $page_templates[ __( 'Index (WP Fallback)', 'pods' ) ] = 'index.php';
440 }
441
442 ksort( $page_templates );
443
444 $page_templates = array_flip( $page_templates );
445
446 $fields = array(
447 array(
448 'name' => 'page_title',
449 'label' => __( 'Page Title', 'pods' ),
450 'type' => 'text',
451 ),
452 array(
453 'name' => 'code',
454 'label' => __( 'Page Code', 'pods' ),
455 'type' => 'code',
456 'attributes' => array(
457 'id' => 'content',
458 ),
459 'label_options' => array(
460 'attributes' => array(
461 'for' => 'content',
462 ),
463 ),
464 ),
465 array(
466 'name' => 'precode',
467 'label' => __( 'Page Precode', 'pods' ),
468 'type' => 'code',
469 'help' => __( 'Precode will run before your theme outputs the page. It is expected that this value will be a block of PHP. You must open the PHP tag here, as we do not open it for you by default.', 'pods' ),
470 ),
471 array(
472 'name' => 'page_template',
473 'label' => __( 'Page Template', 'pods' ),
474 'type' => 'pick',
475 'data' => $page_templates,
476 ),
477 );
478
479 pods_group_add( $pod, __( 'Page', 'pods' ), $fields, 'normal', 'high' );
480
481 $associated_pods = array(
482 0 => __( '-- Select a Pod --', 'pods' ),
483 );
484
485 $all_pods = pods_api()->load_pods( array( 'names' => true ) );
486
487 if ( ! empty( $all_pods ) ) {
488 foreach ( $all_pods as $pod_name => $pod_label ) {
489 $associated_pods[ $pod_name ] = $pod_label . ' (' . $pod_name . ')';
490 }
491 } else {
492 $associated_pods[0] = __( 'None Found', 'pods' );
493 }
494
495 $fields = array(
496 array(
497 'name' => 'pod',
498 'label' => __( 'Associated Pod', 'pods' ),
499 'default' => 0,
500 'type' => 'pick',
501 'data' => $associated_pods,
502 'dependency' => true,
503 ),
504 array(
505 'name' => 'pod_slug',
506 'label' => __( 'Wildcard Slug', 'pods' ),
507 'help' => __( 'Setting the Wildcard Slug is an easy way to setup a detail page. You can use the special tag {@url.2} to match the *third* level of the URL of a Pod Page named "first/second/*" part of the pod page. This is functionally the same as using pods_v_sanitized( 2, "url" ) in PHP.', 'pods' ),
508 'type' => 'text',
509 'excludes-on' => array( 'pod' => 0 ),
510 ),
511 );
512
513 pods_group_add( $pod, __( 'Pod Association', 'pods' ), $fields, 'normal', 'high' );
514
515 $fields = array(
516 array(
517 'name' => 'admin_only',
518 'label' => __( 'Restrict access to Admins?', 'pods' ),
519 'default' => 0,
520 'type' => 'boolean',
521 'dependency' => true,
522 ),
523 array(
524 'name' => 'restrict_role',
525 'label' => __( 'Restrict access by Role?', 'pods' ),
526 'help' => array(
527 __( '<h6>Roles</h6> Roles are assigned to users to provide them access to specific functionality in WordPress. Please see the Roles and Capabilities component in Pods for an easy tool to add your own roles and edit existing ones.', 'pods' ),
528 'http://codex.wordpress.org/Roles_and_Capabilities',
529 ),
530 'default' => 0,
531 'type' => 'boolean',
532 'dependency' => true,
533 ),
534 array(
535 'name' => 'roles_allowed',
536 'label' => __( 'Role(s) Allowed', 'pods' ),
537 'type' => 'pick',
538 'pick_object' => 'role',
539 'pick_format_type' => 'multi',
540 'pick_format_multi' => 'autocomplete',
541 'pick_ajax' => false,
542 'default' => '',
543 'depends-on' => array(
544 'restrict_role' => true,
545 ),
546 ),
547 array(
548 'name' => 'restrict_capability',
549 'label' => __( 'Restrict access by Capability?', 'pods' ),
550 'help' => array(
551 __( '<h6>Capabilities</h6> Capabilities denote access to specific functionality in WordPress, and are assigned to specific User Roles. Please see the Roles and Capabilities component in Pods for an easy tool to add your own capabilities and roles.', 'pods' ),
552 'http://codex.wordpress.org/Roles_and_Capabilities',
553 ),
554 'default' => 0,
555 'type' => 'boolean',
556 'dependency' => true,
557 ),
558 array(
559 'name' => 'capability_allowed',
560 'label' => __( 'Capability Allowed', 'pods' ),
561 'type' => 'pick',
562 'pick_object' => 'capability',
563 'pick_format_type' => 'multi',
564 'pick_format_multi' => 'autocomplete',
565 'pick_ajax' => false,
566 'default' => '',
567 'depends-on' => array(
568 'restrict_capability' => true,
569 ),
570 ),
571 array(
572 'name' => 'restrict_redirect',
573 'label' => __( 'Redirect if Restricted?', 'pods' ),
574 'default' => 0,
575 'type' => 'boolean',
576 'dependency' => true,
577 ),
578 array(
579 'name' => 'restrict_redirect_login',
580 'label' => __( 'Redirect to WP Login page', 'pods' ),
581 'default' => 0,
582 'type' => 'boolean',
583 'dependency' => true,
584 'depends-on' => array(
585 'restrict_redirect' => true,
586 ),
587 ),
588 array(
589 'name' => 'restrict_redirect_url',
590 'label' => __( 'Redirect to a Custom URL', 'pods' ),
591 'default' => '',
592 'type' => 'text',
593 'depends-on' => array(
594 'restrict_redirect' => true,
595 'restrict_redirect_login' => false,
596 ),
597 ),
598 );
599
600 pods_group_add( $pod, __( 'Restrict Access', 'pods' ), $fields, 'normal', 'high' );
601 }
602
603 /**
604 * Get the fields
605 *
606 * @param null $_null
607 * @param int $post_ID
608 * @param string $meta_key
609 * @param bool $single
610 *
611 * @return array|bool|int|mixed|null|string|void
612 */
613 public function get_meta( $_null, $post_ID = null, $meta_key = null, $single = false ) {
614
615 if ( 'code' === $meta_key ) {
616 $post = get_post( $post_ID );
617
618 if ( is_object( $post ) && $this->object_type == $post->post_type ) {
619 return $post->post_content;
620 }
621 }
622
623 return $_null;
624 }
625
626 /**
627 * Save the fields
628 *
629 * @param $_null
630 * @param int $post_ID
631 * @param string $meta_key
632 * @param null $meta_value
633 *
634 * @return bool|int|null
635 */
636 public function save_meta( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) {
637
638 if ( 'code' === $meta_key ) {
639 $post = get_post( $post_ID );
640
641 if ( is_object( $post ) && $this->object_type == $post->post_type ) {
642 $postdata = array(
643 'ID' => $post_ID,
644 'post_content' => $meta_value,
645 );
646
647 remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
648
649 $revisions = false;
650
651 if ( has_action( 'pre_post_update', 'wp_save_post_revision' ) ) {
652 remove_action( 'pre_post_update', 'wp_save_post_revision' );
653
654 $revisions = true;
655 }
656
657 wp_update_post( (object) $postdata );
658 // objects will be automatically sanitized
659 if ( $revisions ) {
660 add_action( 'pre_post_update', 'wp_save_post_revision' );
661 }
662
663 return true;
664 }//end if
665 }//end if
666
667 return $_null;
668 }
669
670 /**
671 * Flush Pod Page Rewrite cache
672 *
673 * @return array Pod Page Rewrites
674 *
675 * @since 2.3.4
676 */
677 public static function flush_rewrites() {
678
679 $args = array(
680 'post_type' => '_pods_page',
681 'nopaging' => true,
682 'posts_per_page' => - 1,
683 'post_status' => 'publish',
684 'order' => 'ASC',
685 'orderby' => 'title',
686 );
687
688 $pod_pages = get_posts( $args );
689
690 $pod_page_rewrites = array();
691
692 foreach ( $pod_pages as $pod_page ) {
693 $pod_page_rewrites[ $pod_page->ID ] = $pod_page->post_title;
694 }
695
696 uksort( $pod_page_rewrites, 'pods_page_length_sort' );
697
698 pods_transient_set( 'pods_object_page_rewrites', $pod_page_rewrites );
699
700 $pod_page_rewrites = array_flip( $pod_page_rewrites );
701
702 return $pod_page_rewrites;
703 }
704
705 /**
706 * Check to see if Pod Page exists and return data
707 *
708 * $uri not required, if NULL then returns REQUEST_URI matching Pod Page
709 *
710 * @param string $uri The Pod Page URI to check if exists
711 *
712 * @return array|bool
713 */
714 public static function exists( $uri = null ) {
715
716 if ( null === $uri ) {
717 $uri = parse_url( pods_current_url() );
718 $uri = $uri['path'];
719 } else {
720 $uri = explode( '?', $uri );
721 $uri = explode( '#', $uri[0] );
722 $uri = $uri[0];
723 }
724
725 $home = parse_url( get_home_url() );
726
727 if ( ! empty( $home ) && isset( $home['path'] ) && '/' !== $home['path'] ) {
728 $uri = substr( $uri, strlen( $home['path'] ) );
729 }
730
731 $uri = trim( $uri, '/' );
732 $uri_depth = count( array_filter( explode( '/', $uri ) ) ) - 1;
733
734 $pods_page_exclusions = array(
735 'wp-admin',
736 'wp-content',
737 'wp-includes',
738 'index.php',
739 'wp-login.php',
740 'wp-signup.php',
741 );
742
743 $pods_page_exclusions = apply_filters( 'pods_page_exclusions', $pods_page_exclusions );
744
745 if ( is_admin() || empty( $uri ) ) {
746 return false;
747 }
748
749 foreach ( $pods_page_exclusions as $exclusion ) {
750 if ( 0 === strpos( $uri, $exclusion ) ) {
751 return false;
752 }
753 }
754
755 $object = apply_filters( 'pods_page_exists', false, $uri );
756 if ( ! empty( $object ) ) {
757 return $object;
758 }
759
760 if ( false === strpos( $uri, '*' ) && ! apply_filters( 'pods_page_regex_matching', false ) ) {
761 $object = pods_by_title( $uri, ARRAY_A, '_pods_page', 'publish' );
762 }
763
764 $wildcard = false;
765
766 if ( empty( $object ) ) {
767 if ( false === strpos( $uri, '*' ) ) {
768 $object = pods_cache_get( $uri, 'pods_object_page_wildcard' );
769
770 if ( ! empty( $object ) ) {
771 return $object;
772 }
773 }
774
775 $pod_page_rewrites = pods_transient_get( 'pods_object_page_rewrites' );
776
777 if ( empty( $pod_page_rewrites ) ) {
778 $pod_page_rewrites = self::flush_rewrites();
779 } else {
780 $pod_page_rewrites = array_flip( $pod_page_rewrites );
781 }
782
783 $found_rewrite_page_id = 0;
784
785 if ( ! empty( $pod_page_rewrites ) ) {
786 foreach ( $pod_page_rewrites as $pod_page => $pod_page_id ) {
787 if ( ! apply_filters( 'pods_page_regex_matching', false ) ) {
788 if ( false === strpos( $pod_page, '*' ) ) {
789 continue;
790 }
791
792 $depth_check = strlen( $pod_page ) - strlen( str_replace( '/', '', $pod_page ) );
793
794 $pod_page = preg_quote( $pod_page, '/' );
795
796 $pod_page = str_replace( '\\*', '(.*)', $pod_page );
797
798 if ( $uri_depth == $depth_check && preg_match( '/^' . $pod_page . '$/', $uri ) ) {
799 $found_rewrite_page_id = $pod_page_id;
800
801 break;
802 }
803 } elseif ( preg_match( '/^' . str_replace( '/', '\\/', $pod_page ) . '$/', $uri ) ) {
804 $found_rewrite_page_id = $pod_page_id;
805
806 break;
807 }//end if
808 }//end foreach
809
810 if ( ! empty( $found_rewrite_page_id ) ) {
811 $object = get_post( $found_rewrite_page_id, ARRAY_A );
812
813 if ( empty( $object ) || '_pods_page' !== $object['post_type'] ) {
814 $object = false;
815 }
816 }
817 }//end if
818
819 $wildcard = true;
820 }//end if
821
822 if ( ! empty( $object ) ) {
823 $object = array(
824 'id' => $object['ID'],
825 'uri' => $object['post_title'],
826 'code' => $object['post_content'],
827 'phpcode' => $object['post_content'],
828 // phpcode is deprecated
829 'precode' => get_post_meta( $object['ID'], 'precode', true ),
830 'page_template' => get_post_meta( $object['ID'], 'page_template', true ),
831 'title' => get_post_meta( $object['ID'], 'page_title', true ),
832 'options' => array(
833 'admin_only' => (boolean) get_post_meta( $object['ID'], 'admin_only', true ),
834 'restrict_role' => (boolean) get_post_meta( $object['ID'], 'restrict_role', true ),
835 'restrict_capability' => (boolean) get_post_meta( $object['ID'], 'restrict_capability', true ),
836 'roles_allowed' => get_post_meta( $object['ID'], 'roles_allowed', true ),
837 'capability_allowed' => get_post_meta( $object['ID'], 'capability_allowed', true ),
838 'restrict_redirect' => (boolean) get_post_meta( $object['ID'], 'restrict_redirect', true ),
839 'restrict_redirect_login' => (boolean) get_post_meta( $object['ID'], 'restrict_redirect_login', true ),
840 'restrict_redirect_url' => get_post_meta( $object['ID'], 'restrict_redirect_url', true ),
841 'pod' => get_post_meta( $object['ID'], 'pod', true ),
842 'pod_slug' => get_post_meta( $object['ID'], 'pod_slug', true ),
843 ),
844 );
845
846 if ( $wildcard ) {
847 pods_cache_set( $uri, $object, 'pods_object_page_wildcard', 3600 );
848 }
849
850 return $object;
851 }//end if
852
853 return false;
854 }
855
856 /**
857 * Check if a Pod Page exists
858 */
859 public function page_check() {
860
861 if ( self::$checked ) {
862 return;
863 }
864
865 global $pods;
866
867 // Fix any global confusion wherever this runs
868 if ( isset( $pods ) && ! isset( $GLOBALS['pods'] ) ) {
869 $GLOBALS['pods'] =& $pods;
870 } elseif ( ! isset( $pods ) && isset( $GLOBALS['pods'] ) ) {
871 $pods =& $GLOBALS['pods'];
872 }
873
874 if ( ! defined( 'PODS_DISABLE_POD_PAGE_CHECK' ) || ! PODS_DISABLE_POD_PAGE_CHECK ) {
875 if ( null === self::$exists ) {
876 self::$exists = pod_page_exists();
877 }
878
879 if ( false !== self::$exists ) {
880 $pods = apply_filters( 'pods_global', $pods, self::$exists );
881
882 if ( ! is_wp_error( $pods ) && ( is_object( $pods ) || 404 != $pods ) ) {
883 add_action( 'template_redirect', array( $this, 'template_redirect' ) );
884 add_filter( 'redirect_canonical', '__return_false' );
885 add_action( 'wp_head', array( $this, 'wp_head' ) );
886 add_filter( 'wp_title', array( $this, 'wp_title' ), 0, 3 );
887 add_filter( 'body_class', array( $this, 'body_class' ), 0, 1 );
888 add_filter( 'status_header', array( $this, 'status_header' ) );
889 add_action( 'after_setup_theme', array( $this, 'precode' ) );
890 add_action( 'wp', array( $this, 'silence_404' ), 1 );
891
892 // Genesis theme integration
893 add_action( 'genesis_loop', 'pods_content', 11 );
894 }
895 }
896
897 self::$checked = true;
898 }//end if
899 }
900
901 /**
902 * Output Pod Page Content
903 *
904 * @param bool $return Whether to return or not (default is to echo)
905 *
906 * @param bool $pods_page
907 *
908 * @return string
909 */
910 public static function content( $return = false, $pods_page = false ) {
911
912 if ( empty( $pods_page ) ) {
913 $pods_page = self::$exists;
914 }
915
916 $content = false;
917
918 if ( $pods_page == self::$exists && self::$content_called ) {
919 return $content;
920 }
921
922 if ( ! empty( $pods_page ) ) {
923 /**
924 * @var $pods \Pods
925 */
926 global $pods;
927
928 // Fix any global confusion wherever this runs
929 if ( isset( $pods ) && ! isset( $GLOBALS['pods'] ) ) {
930 $GLOBALS['pods'] =& $pods;
931 } elseif ( ! isset( $pods ) && isset( $GLOBALS['pods'] ) ) {
932 $pods =& $GLOBALS['pods'];
933 }
934
935 if ( 0 < strlen( trim( $pods_page['code'] ) ) ) {
936 $content = trim( $pods_page['code'] );
937 }
938
939 ob_start();
940
941 do_action( 'pods_content_pre', $pods_page, $content );
942
943 if ( 0 < strlen( $content ) ) {
944 if ( false !== strpos( $content, '<?' ) && ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) ) {
945 pods_deprecated( 'Pod Page PHP code has been deprecated, please use WP Page Templates or hook into the pods_content filter instead of embedding PHP.', '2.1' );
946
947 eval( "?>$content" );
948 } elseif ( is_object( $pods ) && ! empty( $pods->id ) ) {
949 echo $pods->do_magic_tags( $content );
950 } else {
951 echo $content;
952 }
953 }
954
955 do_action( 'pods_content_post', $pods_page, $content );
956
957 $content = ob_get_clean();
958
959 if ( $pods_page == self::$exists ) {
960 self::$content_called = true;
961 }
962 }//end if
963
964 $content = apply_filters( 'pods_content', $content, $pods_page );
965
966 if ( $return ) {
967 return $content;
968 }
969
970 echo $content;
971 }
972
973 /**
974 * Run any precode for current Pod Page
975 */
976 public function precode() {
977
978 global $pods;
979
980 // Fix any global confusion wherever this runs
981 if ( isset( $pods ) && ! isset( $GLOBALS['pods'] ) ) {
982 $GLOBALS['pods'] =& $pods;
983 } elseif ( ! isset( $pods ) && isset( $GLOBALS['pods'] ) ) {
984 $pods =& $GLOBALS['pods'];
985 }
986
987 if ( false !== self::$exists ) {
988 $permission = pods_permission( self::$exists['options'] );
989
990 $permission = (boolean) apply_filters( 'pods_pages_permission', $permission, self::$exists );
991
992 if ( $permission ) {
993 $content = false;
994
995 if ( ! is_object( $pods ) && 404 != $pods && 0 < strlen( pods_var( 'pod', self::$exists['options'] ) ) ) {
996 $slug = pods_var_raw( 'pod_slug', self::$exists['options'], null, null, true );
997
998 // Handle special magic tags
999 if ( 0 < strlen( $slug ) ) {
1000 $slug = pods_evaluate_tags( $slug, true );
1001 }
1002
1003 $pods = pods( pods_var( 'pod', self::$exists['options'] ), $slug );
1004
1005 // Auto 404 handling if item doesn't exist
1006 if ( 0 < strlen( $slug ) && ! $pods->exists() && apply_filters( 'pods_pages_auto_404', true, $slug, $pods, self::$exists ) ) {
1007 $pods = 404;
1008 }
1009 }
1010
1011 if ( 0 < strlen( trim( self::$exists['precode'] ) ) ) {
1012 $content = self::$exists['precode'];
1013 }
1014
1015 if ( false !== $content && ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) ) {
1016 pods_deprecated( 'Pod Page Precode has been deprecated, please use WP Page Templates or hook into the pods_content filter instead of embedding PHP.', '2.1' );
1017
1018 eval( "?>$content" );
1019 }
1020
1021 do_action( 'pods_page_precode', self::$exists, $pods, $content );
1022 } elseif ( self::$exists['options']['restrict_redirect'] ) {
1023 $redirect_url = '';
1024
1025 if ( self::$exists['options']['restrict_redirect_login'] ) {
1026 $redirect_url = wp_login_url( pods_current_url() );
1027 } elseif ( ! empty( self::$exists['options']['restrict_redirect_url'] ) ) {
1028 $redirect_url = self::$exists['options']['restrict_redirect_url'];
1029 }
1030
1031 if ( ! empty( $redirect_url ) ) {
1032 wp_redirect( $redirect_url );
1033 die();
1034 }
1035 }//end if
1036
1037 if ( ! $permission || ( ! is_object( $pods ) && ( 404 == $pods || is_wp_error( $pods ) ) ) ) {
1038 remove_action( 'template_redirect', array( $this, 'template_redirect' ) );
1039 remove_action( 'wp_head', array( $this, 'wp_head' ) );
1040 remove_filter( 'redirect_canonical', '__return_false' );
1041 remove_filter( 'wp_title', array( $this, 'wp_title' ) );
1042 remove_filter( 'body_class', array( $this, 'body_class' ) );
1043 remove_filter( 'status_header', array( $this, 'status_header' ) );
1044 remove_action( 'wp', array( $this, 'silence_404' ), 1 );
1045 }
1046 }//end if
1047 }
1048
1049 /**
1050 *
1051 */
1052 public function wp_head() {
1053
1054 global $pods;
1055
1056 do_action( 'pods_wp_head' );
1057
1058 if ( ! defined( 'PODS_DISABLE_VERSION_OUTPUT' ) || ! PODS_DISABLE_VERSION_OUTPUT ) {
1059 ?>
1060 <!-- Pods Framework <?php echo esc_html( PODS_VERSION ); ?> -->
1061 <?php
1062 }
1063 if ( ( ! defined( 'PODS_DISABLE_META' ) || ! PODS_DISABLE_META ) && is_object( $pods ) && ! is_wp_error( $pods ) ) {
1064
1065 if ( isset( $pods->meta ) && is_array( $pods->meta ) && ! empty( $pods->meta ) ) {
1066 foreach ( $pods->meta as $name => $content ) {
1067 if ( 'title' === $name ) {
1068 continue;
1069 }
1070 ?>
1071 <meta name="<?php echo esc_attr( $name ); ?>" content="<?php echo esc_attr( $content ); ?>" />
1072 <?php
1073 }
1074 }
1075
1076 if ( isset( $pods->meta_properties ) && is_array( $pods->meta_properties ) && ! empty( $pods->meta_properties ) ) {
1077 foreach ( $pods->meta_properties as $property => $content ) {
1078 ?>
1079 <meta property="<?php echo esc_attr( $property ); ?>" content="<?php echo esc_attr( $content ); ?>" />
1080 <?php
1081 }
1082 }
1083
1084 if ( isset( $pods->meta_extra ) && 0 < strlen( $pods->meta_extra ) ) {
1085 echo $pods->meta_extra;
1086 }
1087 }//end if
1088 }
1089
1090 /**
1091 * @param $title
1092 * @param $sep
1093 * @param $seplocation
1094 *
1095 * @return mixed|void
1096 */
1097 public function wp_title( $title, $sep, $seplocation ) {
1098
1099 global $pods;
1100
1101 $page_title = trim( self::$exists['title'] );
1102
1103 if ( 0 < strlen( $page_title ) ) {
1104 if ( is_object( $pods ) && ! is_wp_error( $pods ) ) {
1105 $page_title = $pods->do_magic_tags( $page_title );
1106 }
1107
1108 $title = ( 'right' === $seplocation ) ? "{$page_title} {$sep} " : " {$sep} {$page_title}";
1109 } elseif ( strlen( trim( $title ) ) < 1 ) {
1110 $uri = explode( '?', $_SERVER['REQUEST_URI'] );
1111 $uri = preg_replace( '@^([/]?)(.*?)([/]?)$@', '$2', $uri[0] );
1112 $uri = preg_replace( '@(-|_)@', ' ', $uri );
1113 $uri = explode( '/', $uri );
1114
1115 $title = '';
1116
1117 foreach ( $uri as $key => $page_title ) {
1118 $title .= ( 'right' === $seplocation ) ? ucwords( $page_title ) . " {$sep} " : " {$sep} " . ucwords( $page_title );
1119 }
1120 }
1121
1122 if ( ( ! defined( 'PODS_DISABLE_META' ) || ! PODS_DISABLE_META ) && is_object( $pods ) && ! is_wp_error( $pods ) && isset( $pods->meta ) && is_array( $pods->meta ) && isset( $pods->meta['title'] ) ) {
1123 $title = $pods->meta['title'];
1124 }
1125
1126 return apply_filters( 'pods_title', $title, $sep, $seplocation, self::$exists );
1127 }
1128
1129 /**
1130 * @param $classes
1131 *
1132 * @return mixed|void
1133 */
1134 public function body_class( $classes ) {
1135
1136 global $pods;
1137
1138 if ( defined( 'PODS_DISABLE_BODY_CLASSES' ) && PODS_DISABLE_BODY_CLASSES ) {
1139 return $classes;
1140 }
1141
1142 $classes[] = 'pods';
1143
1144 $uri = explode( '?', self::$exists['uri'] );
1145 $uri = explode( '#', $uri[0] );
1146
1147 $class = str_replace( array( '*', '/' ), array( '_w_', '-' ), $uri[0] );
1148 $class = sanitize_title( $class );
1149 $class = str_replace( array( '_', '--', '--' ), '-', $class );
1150 $class = trim( $class, '-' );
1151
1152 $classes[] = 'pod-page-' . $class;
1153
1154 if ( is_object( $pods ) && ! is_wp_error( $pods ) ) {
1155 $class = sanitize_title( $pods->pod );
1156 $class = str_replace( array( '_', '--', '--' ), '-', $class );
1157 $class = trim( $class, '-' );
1158 $classes[] = 'pod-' . $class;
1159 }
1160
1161 if ( is_object( $pods ) && ! is_wp_error( $pods ) && isset( $pods->body_classes ) ) {
1162 $classes[] = $pods->body_classes;
1163 }
1164
1165 return apply_filters( 'pods_body_class', $classes, $uri );
1166 }
1167
1168 /**
1169 * @return string
1170 */
1171 public function status_header() {
1172
1173 return $_SERVER['SERVER_PROTOCOL'] . ' 200 OK';
1174 }
1175
1176 /**
1177 *
1178 */
1179 public function silence_404() {
1180
1181 global $wp_query;
1182
1183 $wp_query->query_vars['error'] = '';
1184 $wp_query->is_404 = false;
1185 }
1186
1187 /**
1188 *
1189 */
1190 public function template_redirect() {
1191
1192 global $pods;
1193
1194 if ( false !== self::$exists ) {
1195 /*
1196 * Create pods.php in your theme directory, and
1197 * style it to suit your needs. Some helpful functions:
1198 *
1199 * get_header()
1200 * pods_content()
1201 * get_sidebar()
1202 * get_footer()
1203 */
1204 $template = self::$exists['page_template'];
1205 $template = apply_filters( 'pods_page_template', $template, self::$exists );
1206
1207 $render_function = apply_filters( 'pods_template_redirect', null, $template, self::$exists );
1208
1209 do_action( 'pods_page', $template, self::$exists );
1210
1211 if ( '_custom' === $template ) {
1212 pods_content();
1213 } elseif ( null !== $render_function && is_callable( $render_function ) ) {
1214 call_user_func( $render_function, $template, self::$exists );
1215 } elseif ( ( ! defined( 'PODS_DISABLE_DYNAMIC_TEMPLATE' ) || ! PODS_DISABLE_DYNAMIC_TEMPLATE ) && is_object( $pods ) && ! is_wp_error( $pods ) && isset( $pods->page_template ) && ! empty( $pods->page_template ) && '' != locate_template( array( $pods->page_template ), true ) ) {
1216 $template = $pods->page_template;
1217 // found the template and included it, we're good to go!
1218 } elseif ( ! empty( self::$exists['page_template'] ) && '' != locate_template( array( self::$exists['page_template'] ), true ) ) {
1219 $template = self::$exists['page_template'];
1220 // found the template and included it, we're good to go!
1221 } else {
1222 $located_template = apply_filters( 'pods_page_locate_template', $template, self::$exists );
1223
1224 if ( $template !== $located_template ) {
1225 $template = $located_template;
1226 } else {
1227 $default_templates = array();
1228
1229 $uri = explode( '?', self::$exists['uri'] );
1230 $uri = explode( '#', $uri[0] );
1231
1232 $page_path = explode( '/', $uri[0] );
1233
1234 while ( $last = array_pop( $page_path ) ) {
1235 $file_name = str_replace( '*', '-w-', implode( '/', $page_path ) . '/' . $last );
1236 $sanitized = sanitize_title( $file_name );
1237
1238 $default_templates[] = 'pods/' . trim( str_replace( '--', '-', $sanitized ), ' -' ) . '.php';
1239 $default_templates[] = 'pods-' . trim( str_replace( '--', '-', $sanitized ), ' -' ) . '.php';
1240 }
1241
1242 $default_templates[] = 'pods.php';
1243
1244 $default_templates = apply_filters( 'pods_page_default_templates', $default_templates );
1245
1246 $template = locate_template( $default_templates, true );
1247
1248 if ( '' !== $template ) {
1249 // found the template and included it, we're good to go!
1250 } else {
1251 $template = false;
1252
1253 // templates not found in theme, default output
1254 do_action( 'pods_page_default', $template, self::$exists );
1255
1256 get_header();
1257 pods_content();
1258 get_sidebar();
1259 get_footer();
1260 }
1261 }//end if
1262 }//end if
1263
1264 do_action( 'pods_page_end', $template, self::$exists );
1265
1266 exit;
1267 }//end if
1268 }
1269 }
1270
1271 /**
1272 * Find out if the current page is a Pod Page
1273 *
1274 * @param string $uri The Pod Page URI to check if currently on
1275 *
1276 * @return bool
1277 * @since 1.7.5
1278 */
1279 function is_pod_page( $uri = null ) {
1280
1281 if ( false !== Pods_Pages::$exists && ( null === $uri || $uri == Pods_Pages::$exists['uri'] || $uri == Pods_Pages::$exists['id'] ) ) {
1282 return true;
1283 }
1284
1285 return false;
1286 }
1287
1288 /**
1289 * Check for a specific page template for the current pod page
1290 *
1291 * @param string $template The theme template file
1292 *
1293 * @return bool
1294 * @since 2.3.7
1295 */
1296 function is_pod_page_template( $template = null ) {
1297
1298 if ( false !== Pods_Pages::$exists && $template == Pods_Pages::$exists['page_template'] ) {
1299 return true;
1300 }
1301
1302 return false;
1303 }
1304
1305 /**
1306 * Get the current Pod Page URI
1307 *
1308 * @return string|bool
1309 * @since 2.3.3
1310 */
1311 function get_pod_page_uri() {
1312
1313 $pod_page = Pods_Pages::exists();
1314
1315 if ( ! empty( $pod_page ) ) {
1316 return $pod_page['uri'];
1317 }
1318
1319 return false;
1320 }
1321
1322 /**
1323 * Check to see if Pod Page exists and return data
1324 *
1325 * $uri not required, if NULL then returns REQUEST_URI matching Pod Page
1326 *
1327 * @param string $uri The Pod Page URI to check if exists
1328 *
1329 * @return array
1330 *
1331 * @since 1.7.5
1332 */
1333 function pod_page_exists( $uri = null ) {
1334
1335 return Pods_Pages::exists( $uri );
1336 }
1337
1338 /**
1339 * Output Pod Page Content
1340 *
1341 * @param bool $return Whether to return or not (default is to echo)
1342 *
1343 * @param bool $pods_page
1344 *
1345 * @return string
1346 * @since 1.7.0
1347 */
1348 function pods_content( $return = false, $pods_page = false ) {
1349
1350 return Pods_Pages::content( $return, $pods_page );
1351 }
1352
1353 /**
1354 * Sort an array by length of items, descending, for use with uksort()
1355 *
1356 * @param string $a First array item
1357 * @param string $b Second array item
1358 *
1359 * @return int Length difference
1360 *
1361 * @since 2.3.4
1362 */
1363 function pods_page_length_sort( $a, $b ) {
1364
1365 return strlen( $b ) - strlen( $a );
1366 }
1367
1368 /**
1369 * Flush Pod Page Rewrite cache
1370 *
1371 * @return array Pod Page Rewrites
1372 *
1373 * @since 2.3.4
1374 */
1375 function pods_page_flush_rewrites() {
1376
1377 return Pods_Pages::flush_rewrites();
1378 }
1379
1380 /*
1381 * Deprecated global variable
1382 */
1383 $GLOBALS['pod_page_exists'] =& Pods_Pages::$exists;
1384