Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
08:49
created

Pods_Templates::add_meta_boxes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 47
rs 9.0303
c 0
b 0
f 0
eloc 34
nc 2
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Name: Templates
4
 *
5
 * Description: An easy to use templating engine for Pods. Use {@field_name} magic tags to output values, within your
6
 * HTML markup.
7
 *
8
 * Version: 2.3
9
 *
10
 * Category: Advanced
11
 *
12
 * Menu Page: edit.php?post_type=_pods_template
13
 * Menu Add Page: post-new.php?post_type=_pods_template
14
 *
15
 * @package    Pods\Components
16
 * @subpackage Templates
17
 */
18
19
// Pull in the functions
20
require_once plugin_dir_path( __FILE__ ) . '/includes/functions-view_template.php';
21
require_once plugin_dir_path( __FILE__ ) . '/includes/functions-pod_reference.php';
22
23
// Pull in the Frontier Template System
24
require_once plugin_dir_path( __FILE__ ) . 'class-pods_templates.php';
25
26
// Pull in Auto Template
27
require_once dirname( __FILE__ ) . '/includes/auto-template/Pods_Templates_Auto_Template_Settings.php';
28
new Pods_Templates_Auto_Template_Settings();
29
30
Pods_Templates_Frontier::get_instance();
31
32
/**
33
 * Class Pods_Templates
34
 */
35
class Pods_Templates extends PodsComponent {
36
37
	/**
38
	 * Pods object
39
	 *
40
	 * @var object
41
	 *
42
	 * @since 2.0
43
	 */
44
	public static $obj = null;
45
46
	/**
47
	 * Whether to enable deprecated functionality based on old function usage
48
	 *
49
	 * @var bool
50
	 *
51
	 * @since 2.0
52
	 */
53
	public static $deprecated = false;
54
55
	/**
56
	 * Object type
57
	 *
58
	 * @var string
59
	 *
60
	 * @since 2.0
61
	 */
62
	private $object_type = '_pods_template';
63
64
	/**
65
	 * The capability type.
66
	 *
67
	 * @link https://codex.wordpress.org/Function_Reference/register_post_type
68
	 * @var string
69
	 */
70
	private $capability_type = 'pods_template';
71
72
	/**
73
	 * Do things like register/enqueue scripts and stylesheets
74
	 *
75
	 * @since 2.0
76
	 */
77
	public function __construct() {
78
79
		$args = array(
80
			'label'        => 'Pod Templates',
81
			'labels'       => array( 'singular_name' => 'Pod Template' ),
82
			'public'       => false,
83
			'can_export'   => false,
84
			'show_ui'      => true,
85
			'show_in_menu' => false,
86
			'query_var'    => false,
87
			'rewrite'      => false,
88
			'has_archive'  => false,
89
			'hierarchical' => false,
90
			'supports'     => array( 'title', 'author', 'revisions' ),
91
			'menu_icon'    => 'dashicons-pods',
92
		);
93
94
		if ( ! pods_is_admin() ) {
95
			$args['capability_type'] = $this->capability_type;
96
		}
97
98
		$args = PodsInit::object_label_fix( $args, 'post_type' );
99
100
		register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_template', $args ) );
101
102
		if ( is_admin() ) {
103
			add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
104
105
			add_action( 'dbx_post_advanced', array( $this, 'edit_page_form' ) );
106
107
			add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) );
108
109
			add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 );
110
			add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 );
111
112
			add_action( 'pods_meta_save_pre_post__pods_template', array( $this, 'fix_filters' ), 10, 5 );
113
			add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 );
114
			add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 );
115
			add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 );
116
			add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) );
117
118
			add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) );
119
120
		}
121
122
		add_filter( 'members_get_capabilities', array( $this, 'get_capabilities' ) );
123
	}
124
125
	/**
126
	 * @param $caps
127
	 *
128
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
129
	 */
130
	public function get_capabilities( $caps ) {
131
132
		$caps = array_merge(
133
			$caps, array(
134
				'edit_' . $this->capability_type,
135
				'read_' . $this->capability_type,
136
				'delete_' . $this->capability_type,
137
				'edit_' . $this->capability_type . 's',
138
				'edit_others_' . $this->capability_type . 's',
139
				'publish_' . $this->capability_type . 's',
140
				'read_private_' . $this->capability_type . 's',
141
				'edit_' . $this->capability_type . 's',
142
			)
143
		);
144
145
		return $caps;
146
	}
147
148
	/**
149
	 * @param $post_types
150
	 *
151
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
152
	 */
153
	public function disable_builder_layout( $post_types ) {
154
155
		$post_types[] = $this->object_type;
156
157
		return $post_types;
158
	}
159
160
	/**
161
	 * Update Post Type messages
162
	 *
163
	 * @param array $messages
164
	 *
165
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<*,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
166
	 * @since 2.0.2
167
	 */
168
	public function setup_updated_messages( $messages ) {
169
170
		global $post, $post_ID;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
171
172
		$post_type = get_post_type_object( $this->object_type );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
173
174
		$labels = $post_type->labels;
175
176
		$messages[ $post_type->name ] = array(
177
			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 ),
178
			2  => __( 'Custom field updated.', 'pods' ),
179
			3  => __( 'Custom field deleted.', 'pods' ),
180
			4  => sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ),
181
			/* translators: %s: date and time of the revision */
182
			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,
183
			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 ),
184
			7  => sprintf( __( '%s saved.', 'pods' ), $labels->singular_name ),
185
			8  => sprintf( __( '%1$s submitted. <a target="_blank" 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 ),
186
			9  => sprintf(
187
				__( '%s scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview %s</a>', 'pods' ), $labels->singular_name,
188
				// translators: Publish box date format, see http://php.net/date
189
				date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), $labels->singular_name
190
			),
191
			10 => sprintf( __( '%1$s draft updated. <a target="_blank" 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 ),
192
		);
193
194
		if ( false === (boolean) $post_type->public ) {
195
			$messages[ $post_type->name ][1] = sprintf( __( '%s updated.', 'pods' ), $labels->singular_name );
196
			$messages[ $post_type->name ][6] = sprintf( __( '%s published.', 'pods' ), $labels->singular_name );
197
			$messages[ $post_type->name ][8] = sprintf( __( '%s submitted.', 'pods' ), $labels->singular_name );
198
			$messages[ $post_type->name ][9] = sprintf(
199
				__( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ), $labels->singular_name,
200
				// translators: Publish box date format, see http://php.net/date
201
				date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
202
			);
203
			$messages[ $post_type->name ][10] = sprintf( __( '%s draft updated.', 'pods' ), $labels->singular_name );
204
		}
205
206
		return $messages;
207
	}
208
209
	/**
210
	 * Enqueue styles
211
	 *
212
	 * @since 2.0
213
	 */
214
	public function admin_assets() {
215
216
		wp_enqueue_style( 'pods-styles' );
217
	}
218
219
	/**
220
	 * Fix filters, specifically removing balanceTags
221
	 *
222
	 * @since 2.0.1
223
	 *
224
	 * @param      $data
225
	 * @param null $pod
226
	 * @param null $id
227
	 * @param null $groups
228
	 * @param null $post
229
	 */
230
	public function fix_filters( $data, $pod = null, $id = null, $groups = null, $post = null ) {
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
231
232
		remove_filter( 'content_save_pre', 'balanceTags', 50 );
233
	}
234
235
	/**
236
	 * Remove unused row actions
237
	 *
238
	 * @since 2.0.5
239
	 *
240
	 * @param $actions
241
	 * @param $post
242
	 *
243
	 * @return
244
	 */
245
	public function remove_row_actions( $actions, $post ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
246
247
		global $current_screen;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
248
249
		if ( ! is_object( $current_screen ) || $this->object_type != $current_screen->post_type ) {
250
			return $actions;
251
		}
252
253
		if ( isset( $actions['view'] ) ) {
254
			unset( $actions['view'] );
255
		}
256
257
		if ( isset( $actions['inline hide-if-no-js'] ) ) {
258
			unset( $actions['inline hide-if-no-js'] );
259
		}
260
261
		// W3 Total Cache
262
		if ( isset( $actions['pgcache_purge'] ) ) {
263
			unset( $actions['pgcache_purge'] );
264
		}
265
266
		return $actions;
267
	}
268
269
	/**
270
	 * Remove unused bulk actions
271
	 *
272
	 * @since 2.0.5
273
	 *
274
	 * @param $actions
275
	 *
276
	 * @return
277
	 */
278
	public function remove_bulk_actions( $actions ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
279
280
		if ( isset( $actions['edit'] ) ) {
281
			unset( $actions['edit'] );
282
		}
283
284
		return $actions;
285
	}
286
287
	/**
288
	 * Clear cache on save
289
	 *
290
	 * @since 2.0
291
	 *
292
	 * @param      $data
293
	 * @param null $pod
294
	 * @param null $id
295
	 * @param null $groups
296
	 * @param null $post
297
	 */
298
	public function clear_cache( $data, $pod = null, $id = null, $groups = null, $post = null ) {
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
299
300
		$old_post = $id;
301
302
		if ( ! is_object( $id ) ) {
303
			$old_post = null;
304
		}
305
306
		if ( is_object( $post ) && $this->object_type != $post->post_type ) {
307
			return;
308
		}
309
310
		if ( ! is_array( $data ) && 0 < $data ) {
311
			$post = $data;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
312
			$post = get_post( $post );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
313
		}
314
315
		if ( $this->object_type == $post->object_type ) {
316
			pods_transient_clear( 'pods_object_templates' );
317
		}
318
	}
319
320
	/**
321
	 * Change post title placeholder text
322
	 *
323
	 * @since 2.0
324
	 *
325
	 * @param $text
326
	 * @param $post
327
	 *
328
	 * @return string|void
329
	 */
330
	public function set_title_text( $text, $post ) {
331
332
		return __( 'Enter template name here', 'pods' );
333
	}
334
335
	/**
336
	 * Edit page form
337
	 *
338
	 * @since 2.0
339
	 */
340
	public function edit_page_form() {
341
342
		global $post_type;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
343
344
		if ( $this->object_type != $post_type ) {
345
			return;
346
		}
347
348
		add_filter( 'enter_title_here', array( $this, 'set_title_text' ), 10, 2 );
349
	}
350
351
	/**
352
	 * Add meta boxes to the page
353
	 *
354
	 * @since 2.0
355
	 */
356
	public function add_meta_boxes() {
357
358
		$pod = array(
359
			'name' => $this->object_type,
360
			'type' => 'post_type',
361
		);
362
363
		if ( isset( PodsMeta::$post_types[ $pod['name'] ] ) ) {
364
			return;
365
		}
366
367
		$fields = array(
368
			array(
369
				'name'       => 'admin_only',
370
				'label'      => __( 'Show to Admins Only?', 'pods' ),
371
				'default'    => 0,
372
				'type'       => 'boolean',
373
				'dependency' => true,
374
			),
375
			array(
376
				'name'       => 'restrict_capability',
377
				'label'      => __( 'Restrict access by Capability?', 'pods' ),
378
				'help'       => array(
379
					__( '<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' ),
380
					'http://codex.wordpress.org/Roles_and_Capabilities',
381
				),
382
				'default'    => 0,
383
				'type'       => 'boolean',
384
				'dependency' => true,
385
			),
386
			array(
387
				'name'              => 'capability_allowed',
388
				'label'             => __( 'Capability Allowed', 'pods' ),
389
				'type'              => 'pick',
390
				'pick_object'       => 'capability',
391
				'pick_format_type'  => 'multi',
392
				'pick_format_multi' => 'autocomplete',
393
				'pick_ajax'         => false,
394
				'default'           => '',
395
				'depends-on'        => array(
396
					'restrict_capability' => true,
397
				),
398
			),
399
		);
400
401
		pods_group_add( $pod, __( 'Restrict Access', 'pods' ), $fields, 'normal', 'high' );
402
	}
403
404
	/**
405
	 * Get the fields
406
	 *
407
	 * @param null   $_null
408
	 * @param int    $post_ID
0 ignored issues
show
Documentation introduced by
Should the type for parameter $post_ID not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
409
	 * @param string $meta_key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $meta_key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
410
	 * @param bool   $single
411
	 *
412
	 * @return array|bool|int|mixed|null|string|void
413
	 */
414
	public function get_meta( $_null, $post_ID = null, $meta_key = null, $single = false ) {
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
415
416
		if ( 'code' === $meta_key ) {
417
			$post = get_post( $post_ID );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
418
419
			if ( is_object( $post ) && $this->object_type == $post->post_type ) {
420
				return $post->post_content;
421
			}
422
		}
423
424
		return $_null;
425
	}
426
427
	/**
428
	 * Save the fields
429
	 *
430
	 * @param        $_null
431
	 * @param int    $post_ID
0 ignored issues
show
Documentation introduced by
Should the type for parameter $post_ID not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
432
	 * @param string $meta_key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $meta_key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
433
	 * @param string $meta_value
0 ignored issues
show
Documentation introduced by
Should the type for parameter $meta_value not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
434
	 *
435
	 * @return bool|int|null
436
	 */
437
	public function save_meta( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) {
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
438
439
		if ( 'code' === $meta_key ) {
440
			$post = get_post( $post_ID );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
441
442
			if ( is_object( $post ) && $this->object_type == $post->post_type ) {
443
				$postdata = array(
444
					'ID'           => $post_ID,
445
					'post_content' => $meta_value,
446
				);
447
448
				remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
449
450
				$revisions = false;
451
452
				if ( has_action( 'pre_post_update', 'wp_save_post_revision' ) ) {
453
					remove_action( 'pre_post_update', 'wp_save_post_revision' );
454
455
					$revisions = true;
456
				}
457
458
				wp_update_post( (object) $postdata );
459
				// objects will be automatically sanitized
460
				if ( $revisions ) {
461
					add_action( 'pre_post_update', 'wp_save_post_revision' );
462
				}
463
464
				return true;
465
			}//end if
466
		}//end if
467
468
		return $_null;
469
	}
470
471
	/**
472
	 * Display the page template
473
	 *
474
	 * @param string $template_name The template name
475
	 * @param string $code          Custom template code to use instead
0 ignored issues
show
Documentation introduced by
Should the type for parameter $code not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
476
	 * @param object $obj           The Pods object
0 ignored issues
show
Documentation introduced by
Should the type for parameter $obj not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
477
	 * @param bool   $deprecated    Whether to use deprecated functionality based on old function usage
478
	 *
479
	 * @return mixed|string|void
480
	 * @since 2.0
481
	 */
482
	public static function template( $template_name, $code = null, $obj = null, $deprecated = false ) {
483
484
		if ( ! empty( $obj ) ) {
485
			self::$obj =& $obj;
486
		} else {
487
			$obj =& self::$obj;
488
		}
489
490
		self::$deprecated = $deprecated;
491
492
		if ( empty( $obj ) || ! is_object( $obj ) ) {
493
			return '';
494
		}
495
496
		$template = array(
497
			'id'      => 0,
498
			'slug'    => $template_name,
499
			'code'    => $code,
500
			'options' => array(),
501
		);
502
503
		if ( empty( $code ) && ! empty( $template_name ) ) {
504
			$template_obj = $obj->api->load_template( array( 'name' => $template_name ) );
505
506
			if ( ! empty( $template_obj ) ) {
507
				$template = $template_obj;
508
509
				if ( ! empty( $template['code'] ) ) {
510
					$code = $template['code'];
511
				}
512
513
				$permission = pods_permission( $template['options'] );
514
515
				$permission = (boolean) apply_filters( 'pods_templates_permission', $permission, $code, $template, $obj );
516
517
				if ( ! $permission ) {
518
					return apply_filters( 'pods_templates_permission_denied', __( 'You do not have access to view this content.', 'pods' ), $code, $template, $obj );
519
				}
520
			}
521
		}
522
523
		$code = apply_filters( 'pods_templates_pre_template', $code, $template, $obj );
524
		$code = apply_filters( 'pods_templates_pre_template_' . $template['slug'], $code, $template, $obj );
525
526
		ob_start();
527
528
		if ( ! empty( $code ) ) {
529
			// Only detail templates need $this->id
530
			if ( empty( $obj->id ) ) {
531
				while ( $obj->fetch() ) {
532
					echo self::do_template( $code, $obj );
533
				}
534
			} else {
535
				echo self::do_template( $code, $obj );
536
			}
537
		} elseif ( $template_name == trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template_name ), ' /-' ) ) {
538
			$default_templates = array(
539
				'pods/' . $template_name,
540
				'pods-' . $template_name,
541
				$template_name,
542
			);
543
544
			$default_templates = apply_filters( 'pods_template_default_templates', $default_templates );
545
546
			if ( empty( $obj->id ) ) {
547
				while ( $obj->fetch() ) {
548
					pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
549
				}
550
			} else {
551
				pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) );
552
			}
553
		}//end if
554
555
		$out = ob_get_clean();
556
557
		$out = apply_filters( 'pods_templates_post_template', $out, $code, $template, $obj );
558
		$out = apply_filters( 'pods_templates_post_template_' . $template['slug'], $out, $code, $template, $obj );
559
560
		return $out;
561
	}
562
563
	/**
564
	 * Parse a template string
565
	 *
566
	 * @param string $code The template string to parse
567
	 * @param object $obj  The Pods object
0 ignored issues
show
Documentation introduced by
Should the type for parameter $obj not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
568
	 *
569
	 * @since 1.8.5
570
	 * @return mixed|string|void
571
	 */
572
	public static function do_template( $code, $obj = null ) {
573
574
		if ( ! empty( $obj ) ) {
575
			self::$obj =& $obj;
576
		} else {
577
			$obj =& self::$obj;
578
		}
579
580
		if ( empty( $obj ) || ! is_object( $obj ) ) {
581
			return '';
582
		}
583
584
		$code = trim( $code );
585
586
		if ( false !== strpos( $code, '<?' ) && ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) ) {
587
			pods_deprecated( 'Pod Template PHP code has been deprecated, please use WP Templates instead of embedding PHP.', '2.3' );
588
589
			$code = str_replace( '$this->', '$obj->', $code );
590
591
			ob_start();
592
593
			eval( "?>$code" );
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
introduced by
eval is prohibited, please use Anonymous functions instead.
Loading history...
594
595
			$out = ob_get_clean();
596
		} else {
597
			$out = $code;
598
		}
599
600
		$out = $obj->do_magic_tags( $out );
601
602
		return apply_filters( 'pods_templates_do_template', $out, $code, $obj );
603
	}
604
605
}
606