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 / Templates / Templates.php
pods / components / Templates Last commit date
assets 2 days ago includes 2 days ago Templates.php 2 days ago class-pods_templates.php 2 days ago
Templates.php
683 lines
1 <?php
2 /**
3 * Name: Templates
4 *
5 * Menu Name: Pod Templates
6 *
7 * Description: An easy to use templating engine for Pods. Use {@field_name} magic tags to output values, within your HTML markup.
8 *
9 * Version: 2.3
10 *
11 * Category: Advanced
12 *
13 * Menu Page: edit.php?post_type=_pods_template
14 * Menu Add Page: post-new.php?post_type=_pods_template
15 *
16 * @package Pods\Components
17 * @subpackage Templates
18 */
19
20 // Pull in the functions
21 require_once plugin_dir_path( __FILE__ ) . '/includes/functions-view_template.php';
22 require_once plugin_dir_path( __FILE__ ) . '/includes/functions-pod_reference.php';
23
24 // Pull in the Frontier Template System
25 require_once plugin_dir_path( __FILE__ ) . 'class-pods_templates.php';
26
27 // Pull in Auto Template
28 require_once dirname( __FILE__ ) . '/includes/auto-template/Pods_Templates_Auto_Template_Settings.php';
29 new Pods_Templates_Auto_Template_Settings();
30
31 Pods_Templates_Frontier::get_instance();
32
33 /**
34 * Class Pods_Templates
35 */
36 class Pods_Templates extends PodsComponent {
37
38 /**
39 * Pods object
40 *
41 * @var object
42 *
43 * @since 2.0.0
44 */
45 public static $obj = null;
46
47 /**
48 * Whether to enable deprecated functionality based on old function usage
49 *
50 * @var bool
51 *
52 * @since 2.0.0
53 */
54 public static $deprecated = false;
55
56 /**
57 * Object type
58 *
59 * @var string
60 *
61 * @since 2.0.0
62 */
63 private $object_type = '_pods_template';
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_template';
72
73 /**
74 * {@inheritdoc}
75 */
76 public function init() {
77
78 $args = array(
79 'label' => 'Pod Templates',
80 'labels' => array( 'singular_name' => 'Pod Template' ),
81 'public' => false,
82 'can_export' => false,
83 'show_ui' => true,
84 'show_in_menu' => false,
85 'query_var' => false,
86 'rewrite' => false,
87 'has_archive' => false,
88 'hierarchical' => false,
89 'supports' => array( 'title', 'author', 'revisions' ),
90 'menu_icon' => 'dashicons-pods',
91 );
92
93 if ( ! pods_is_admin() ) {
94 $args['capability_type'] = $this->capability_type;
95 }
96
97 $args = PodsInit::object_label_fix( $args, 'post_type' );
98
99 register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_template', $args ) );
100
101 if ( is_admin() ) {
102 add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
103
104 add_action( 'add_meta_boxes_' . $this->object_type, array( $this, 'edit_page_form' ) );
105
106 add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) );
107
108 add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 );
109 add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 );
110
111 add_action( 'pods_meta_save_pre_post__pods_template', array( $this, 'fix_filters' ), 10, 5 );
112 add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 );
113 add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 );
114 add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 );
115 add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) );
116
117 add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) );
118
119 }
120
121 add_filter( 'members_get_capabilities', array( $this, 'get_capabilities' ) );
122 }
123
124 /**
125 * @param $caps
126 *
127 * @return array
128 */
129 public function get_capabilities( $caps ) {
130
131 $caps = array_merge(
132 $caps, array(
133 'edit_' . $this->capability_type,
134 'read_' . $this->capability_type,
135 'delete_' . $this->capability_type,
136 'edit_' . $this->capability_type . 's',
137 'edit_others_' . $this->capability_type . 's',
138 'publish_' . $this->capability_type . 's',
139 'read_private_' . $this->capability_type . 's',
140 'edit_' . $this->capability_type . 's',
141 )
142 );
143
144 return $caps;
145 }
146
147 /**
148 * @param $post_types
149 *
150 * @return array
151 */
152 public function disable_builder_layout( $post_types ) {
153
154 $post_types[] = $this->object_type;
155
156 return $post_types;
157 }
158
159 /**
160 * Update Post Type messages
161 *
162 * @param array $messages
163 *
164 * @return array
165 * @since 2.0.2
166 */
167 public function setup_updated_messages( $messages ) {
168
169 global $post, $post_ID;
170
171 $post_type = get_post_type_object( $this->object_type );
172
173 $labels = $post_type->labels;
174
175 $messages[ $post_type->name ] = array(
176 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 ),
177 2 => __( 'Custom field updated.', 'pods' ),
178 3 => __( 'Custom field deleted.', 'pods' ),
179 4 => sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ),
180 /* translators: %s: date and time of the revision */
181 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,
182 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 ),
183 7 => sprintf( __( '%s saved.', 'pods' ), $labels->singular_name ),
184 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 ),
185 9 => sprintf(
186 __( '%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,
187 // translators: Publish box date format, see http://php.net/date
188 date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), $labels->singular_name
189 ),
190 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 ),
191 );
192
193 if ( false === (boolean) $post_type->public ) {
194 $messages[ $post_type->name ][1] = sprintf( __( '%s updated.', 'pods' ), $labels->singular_name );
195 $messages[ $post_type->name ][6] = sprintf( __( '%s published.', 'pods' ), $labels->singular_name );
196 $messages[ $post_type->name ][8] = sprintf( __( '%s submitted.', 'pods' ), $labels->singular_name );
197 $messages[ $post_type->name ][9] = sprintf(
198 __( '%1$s scheduled for: <strong>%2$s</strong>.', 'pods' ), $labels->singular_name,
199 // translators: Publish box date format, see http://php.net/date
200 date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
201 );
202 $messages[ $post_type->name ][10] = sprintf( __( '%s draft updated.', 'pods' ), $labels->singular_name );
203 }
204
205 return $messages;
206 }
207
208 /**
209 * Enqueue styles
210 *
211 * @since 2.0.0
212 */
213 public function admin_assets() {
214
215 wp_enqueue_style( 'pods-styles' );
216 }
217
218 /**
219 * Fix filters, specifically removing balanceTags
220 *
221 * @since 2.0.1
222 *
223 * @param $data
224 * @param null $pod
225 * @param null $id
226 * @param null $groups
227 * @param null $post
228 */
229 public function fix_filters( $data, $pod = null, $id = null, $groups = null, $post = null ) {
230
231 remove_filter( 'content_save_pre', 'balanceTags', 50 );
232 }
233
234 /**
235 * Remove unused row actions
236 *
237 * @since 2.0.5
238 *
239 * @param $actions
240 * @param $post
241 *
242 * @return
243 */
244 public function remove_row_actions( $actions, $post ) {
245
246 global $current_screen;
247
248 if ( ! is_object( $current_screen ) || $this->object_type != $current_screen->post_type ) {
249 return $actions;
250 }
251
252 if ( isset( $actions['view'] ) ) {
253 unset( $actions['view'] );
254 }
255
256 if ( isset( $actions['inline hide-if-no-js'] ) ) {
257 unset( $actions['inline hide-if-no-js'] );
258 }
259
260 // W3 Total Cache
261 if ( isset( $actions['pgcache_purge'] ) ) {
262 unset( $actions['pgcache_purge'] );
263 }
264
265 return $actions;
266 }
267
268 /**
269 * Remove unused bulk actions
270 *
271 * @since 2.0.5
272 *
273 * @param $actions
274 *
275 * @return
276 */
277 public function remove_bulk_actions( $actions ) {
278
279 if ( isset( $actions['edit'] ) ) {
280 unset( $actions['edit'] );
281 }
282
283 return $actions;
284 }
285
286 /**
287 * Clear cache on save
288 *
289 * @since 2.0.0
290 *
291 * @param $data
292 * @param null $pod
293 * @param null $id
294 * @param null $groups
295 * @param null $post
296 */
297 public function clear_cache( $data, $pod = null, $id = null, $groups = null, $post = null ) {
298
299 $old_post = $id;
300
301 if ( ! is_object( $id ) ) {
302 $old_post = null;
303 }
304
305 if ( is_object( $post ) && $this->object_type != $post->post_type ) {
306 return;
307 }
308
309 if ( ! is_array( $data ) && 0 < $data ) {
310 $post = $data;
311 $post = get_post( $post );
312 }
313
314 if ( $this->object_type == $post->object_type ) {
315 pods_transient_clear( 'pods_object_templates' );
316 }
317 }
318
319 /**
320 * Change post title placeholder text
321 *
322 * @since 2.0.0
323 *
324 * @param $text
325 * @param $post
326 *
327 * @return string|void
328 */
329 public function set_title_text( $text, $post ) {
330
331 return __( 'Enter template name here', 'pods' );
332 }
333
334 /**
335 * Edit page form
336 *
337 * @since 2.0.0
338 */
339 public function edit_page_form() {
340
341 global $post_type;
342
343 if ( $this->object_type != $post_type ) {
344 return;
345 }
346
347 add_filter( 'enter_title_here', array( $this, 'set_title_text' ), 10, 2 );
348 }
349
350 /**
351 * Add meta boxes to the page
352 *
353 * @since 2.0.0
354 */
355 public function add_meta_boxes() {
356
357 $pod = array(
358 'name' => $this->object_type,
359 'type' => 'post_type',
360 );
361
362 if ( isset( PodsMeta::$post_types[ $pod['name'] ] ) ) {
363 return;
364 }
365
366 $fields = array(
367 array(
368 'name' => 'admin_only',
369 'label' => __( 'Show to Admins Only?', 'pods' ),
370 'default' => 0,
371 'type' => 'boolean',
372 'dependency' => true,
373 ),
374 array(
375 'name' => 'restrict_capability',
376 'label' => __( 'Restrict access by Capability?', 'pods' ),
377 'help' => array(
378 __( '<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' ),
379 'http://codex.wordpress.org/Roles_and_Capabilities',
380 ),
381 'default' => 0,
382 'type' => 'boolean',
383 'dependency' => true,
384 ),
385 array(
386 'name' => 'capability_allowed',
387 'label' => __( 'Capability Allowed', 'pods' ),
388 'type' => 'pick',
389 'pick_object' => 'capability',
390 'pick_format_type' => 'multi',
391 'pick_format_multi' => 'autocomplete',
392 'pick_ajax' => false,
393 'default' => '',
394 'depends-on' => array(
395 'restrict_capability' => true,
396 ),
397 ),
398 array(
399 'name' => 'show_restrict_message',
400 'label' => __( 'Show no access message?', 'pods' ),
401 'default' => 1,
402 'type' => 'boolean',
403 'dependency' => true,
404 ),
405 array(
406 'name' => 'restrict_message',
407 'label' => __( 'No access message', 'pods' ),
408 'type' => 'wysiwyg',
409 'default' => __( 'You do not have access to view this content.', 'pods' ),
410 'wysiwyg_editor_height' => 200,
411 'depends-on' => array(
412 'show_restrict_message' => true,
413 ),
414 ),
415 );
416
417 pods_group_add( $pod, __( 'Restrict Access', 'pods' ), $fields, 'normal', 'high' );
418 }
419
420 /**
421 * Get the fields
422 *
423 * @param null $_null
424 * @param int $post_ID
425 * @param string $meta_key
426 * @param bool $single
427 *
428 * @return array|bool|int|mixed|null|string|void
429 */
430 public function get_meta( $_null, $post_ID = null, $meta_key = null, $single = false ) {
431
432 if ( 'code' === $meta_key ) {
433 $post = get_post( $post_ID );
434
435 if ( is_object( $post ) && $this->object_type == $post->post_type ) {
436 return $post->post_content;
437 }
438 }
439
440 return $_null;
441 }
442
443 /**
444 * Save the fields
445 *
446 * @param $_null
447 * @param int $post_ID
448 * @param string $meta_key
449 * @param string $meta_value
450 *
451 * @return bool|int|null
452 */
453 public function save_meta( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) {
454
455 if ( 'code' === $meta_key ) {
456 $post = get_post( $post_ID );
457
458 if ( is_object( $post ) && $this->object_type == $post->post_type ) {
459 $postdata = array(
460 'ID' => $post_ID,
461 'post_content' => $meta_value,
462 );
463
464 remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
465
466 $revisions = false;
467
468 if ( has_action( 'pre_post_update', 'wp_save_post_revision' ) ) {
469 remove_action( 'pre_post_update', 'wp_save_post_revision' );
470
471 $revisions = true;
472 }
473
474 wp_update_post( (object) $postdata );
475 // objects will be automatically sanitized
476 if ( $revisions ) {
477 add_action( 'pre_post_update', 'wp_save_post_revision' );
478 }
479
480 return true;
481 }//end if
482 }//end if
483
484 return $_null;
485 }
486
487 /**
488 * Display the page template
489 *
490 * @param string $template_name The template name
491 * @param string $code Custom template code to use instead
492 * @param object $obj The Pods object
493 * @param bool $deprecated Whether to use deprecated functionality based on old function usage
494 * @param bool $check_access Whether to check access for Posts that are Password-protected.
495 *
496 * @return mixed|string|void
497 * @since 2.0.0
498 */
499 public static function template( $template_name, $code = null, $obj = null, $deprecated = false, $check_access = false ) {
500
501 if ( ! empty( $obj ) ) {
502 self::$obj =& $obj;
503 } else {
504 $obj =& self::$obj;
505 }
506
507 self::$deprecated = $deprecated;
508
509 if ( empty( $obj ) || ! is_object( $obj ) ) {
510 return '';
511 }
512
513 $template = array(
514 'id' => 0,
515 'slug' => $template_name,
516 'code' => $code,
517 'options' => array(),
518 );
519
520 if ( empty( $code ) && ! empty( $template_name ) ) {
521 $template_obj = $obj->api->load_template( array( 'name' => $template_name ) );
522
523 if ( ! empty( $template_obj ) ) {
524 $template = $template_obj;
525
526 if ( ! empty( $template['code'] ) ) {
527 $code = $template['code'];
528 }
529
530 $options = pods_v( 'options', $template );
531
532 $permission = pods_permission( $options );
533
534 $permission = (boolean) apply_filters( 'pods_templates_permission', $permission, $code, $template, $obj );
535
536 if ( ! $permission ) {
537 if ( 1 === (int) pods_v( 'show_restrict_message', $options, 1 ) ) {
538 $message = pods_v( 'restrict_message', $options, __( 'You do not have access to view this content.', 'pods' ), true );
539 $message = PodsForm::field_method( 'wysiwyg', 'display', $message, 'restrict_message', $options );
540 return apply_filters( 'pods_templates_permission_denied', $message, $code, $template, $obj );
541 }
542 return '';
543 }
544 }
545 }
546
547 $slug = $template['slug'];
548
549 $code = apply_filters( 'pods_templates_pre_template', $code, $template, $obj );
550 $code = apply_filters( "pods_templates_pre_template_{$slug}", $code, $template, $obj );
551
552 $info = $check_access ? pods_info_from_args( [ 'pods' => $obj ] ) : [];
553
554 ob_start();
555
556 if ( ! empty( $code ) ) {
557 // Only detail templates need $this->id
558 if ( empty( $obj->id ) ) {
559 while ( $obj->fetch() ) {
560 $info['item_id'] = $obj->id();
561
562 // Ensure the post is not password protected.
563 if (
564 $check_access
565 && (
566 pods_access_bypass_post_with_password( $info )
567 || pods_access_bypass_private_post( $info )
568 )
569 ) {
570 continue;
571 }
572
573 echo self::do_template( $code, $obj );
574 }
575 } else {
576 $info['item_id'] = $obj->id();
577
578 if (
579 ! $check_access
580 || (
581 ! pods_access_bypass_post_with_password( $info )
582 && ! pods_access_bypass_private_post( $info )
583 )
584 ) {
585 echo self::do_template( $code, $obj );
586 }
587 }
588 } elseif ( $template_name == trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template_name ), ' /-' ) ) {
589 $default_templates = array(
590 'pods/' . $template_name,
591 'pods-' . $template_name,
592 $template_name,
593 );
594
595 $default_templates = apply_filters( 'pods_template_default_templates', $default_templates );
596
597 if ( empty( $obj->id ) ) {
598 while ( $obj->fetch() ) {
599 $info['item_id'] = $obj->id();
600
601 // Ensure the post is not password protected.
602 if (
603 $check_access
604 && (
605 pods_access_bypass_post_with_password( $info )
606 || pods_access_bypass_private_post( $info )
607 )
608 ) {
609 continue;
610 }
611
612 pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
613 }
614 } else {
615 $info['item_id'] = $obj->id();
616
617 if (
618 ! $check_access
619 || (
620 ! pods_access_bypass_post_with_password( $info )
621 && ! pods_access_bypass_private_post( $info )
622 )
623 ) {
624 pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
625 }
626 }
627 }//end if
628
629 $out = ob_get_clean();
630
631 $out = apply_filters( 'pods_templates_post_template', $out, $code, $template, $obj );
632 $out = apply_filters( "pods_templates_post_template_{$slug}", $out, $code, $template, $obj );
633
634 return $out;
635 }
636
637 /**
638 * Parse a template string
639 *
640 * @param string $code The template string to parse
641 * @param object $obj The Pods object
642 *
643 * @since 1.8.5
644 * @return mixed|string|void
645 */
646 public static function do_template( $code, $obj = null ) {
647
648 if ( ! empty( $obj ) ) {
649 self::$obj =& $obj;
650 } else {
651 $obj =& self::$obj;
652 }
653
654 if ( empty( $obj ) || ! is_object( $obj ) ) {
655 return '';
656 }
657
658 if ( false !== strpos( $code, '<?' ) && ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) ) {
659 pods_deprecated( 'Pod Template PHP code has been deprecated, please use WP Templates instead of embedding PHP.', '2.3' );
660
661 $code = str_replace( '$this->', '$obj->', $code );
662
663 ob_start();
664
665 eval( "?>$code" );
666
667 $out = ob_get_clean();
668 } else {
669 $out = $code;
670 }
671
672 $out = $obj->do_magic_tags( $out );
673
674 // Prevent blank whitespace from being output if nothing came through.
675 if ( '' === trim( $out ) ) {
676 $out = '';
677 }
678
679 return apply_filters( 'pods_templates_do_template', $out, $code, $obj );
680 }
681
682 }
683