PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.7
Pods – Custom Content Types and Fields v3.2.7
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 / class-pods_templates.php
pods / components / Templates Last commit date
assets 2 years ago includes 2 years ago Templates.php 1 year ago class-pods_templates.php 2 years ago
class-pods_templates.php
534 lines
1 <?php
2 /**
3 * Pods_Templates_Frontier
4 *
5 * @package Pods_Templates_Frontier
6 * @author David Cramer <david@digilab.co.za>
7 * @license GPL-2.0+
8 * @link
9 * @copyright 2014 David Cramer
10 */
11
12 /**
13 * Plugin class.
14 *
15 * @package Pods_Templates_Frontier
16 * @author David Cramer <david@digilab.co.za>
17 */
18 if ( class_exists( 'Pods_Frontier_Template_Editor' ) || class_exists( 'Pods_Templates_Frontier' ) ) {
19 return;
20 }
21
22 /**
23 * Class Pods_Templates_Frontier
24 */
25 class Pods_Templates_Frontier {
26
27 /**
28 * @var string
29 */
30 const VERSION = '1.00';
31
32 /**
33 * @var string
34 */
35 protected $plugin_slug = 'pods_templates';
36
37 /**
38 * @var object
39 */
40 protected static $instance = null;
41
42 /**
43 * @var array
44 */
45 protected $element_instances = array();
46
47 /**
48 * @var array
49 */
50 protected $element_css_once = array();
51
52 /**
53 * @var array
54 */
55 protected $elements = array();
56
57 /**
58 * @var string
59 */
60 protected $plugin_screen_hook_suffix = null;
61
62 /**
63 * Initialize the plugin by setting localization, filters, and administration functions.
64 */
65 private function __construct() {
66
67 add_filter( 'pods_templates_pre_template', 'frontier_prefilter_template', 25, 4 );
68 // Load admin style sheet and JavaScript.
69 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_stylescripts' ), 20 );
70 add_action( 'wp_footer', array( $this, 'footer_scripts' ) );
71 add_action( 'init', array( $this, 'activate_metaboxes' ) );
72 }
73
74 /**
75 * Return an instance of this class.
76 *
77 * @return object A single instance of this class.
78 */
79 public static function get_instance() {
80
81 // If the single instance hasn't been set, set it now.
82 if ( null == self::$instance ) {
83 self::$instance = new self();
84 }
85
86 return self::$instance;
87 }
88
89 /**
90 * Register and enqueue admin-specific style sheet.
91 *
92 * @return null
93 */
94 public function enqueue_admin_stylescripts() {
95
96 $screen = get_current_screen();
97
98 if ( ! $screen || ! isset( $this->plugin_screen_hook_suffix ) ) {
99 return;
100 }
101
102 if ( in_array( $screen->id, $this->plugin_screen_hook_suffix, true ) ) {
103 $slug = array_search( $screen->id, $this->plugin_screen_hook_suffix );
104 // $configfiles = glob( $this->get_path( __FILE__ ) .'configs/'.$slug.'-*.php' );
105 if ( file_exists( $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php' ) ) {
106 include $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php';
107 }
108
109 if ( ! empty( $configfiles ) ) {
110
111 foreach ( $configfiles as $key => $fieldfile ) {
112 include $fieldfile;
113 if ( ! empty( $group['scripts'] ) ) {
114 foreach ( $group['scripts'] as $script ) {
115 wp_enqueue_script( $this->plugin_slug . '-' . strtok( $script, '.' ), $this->get_url( 'assets/js/' . $script, __FILE__ ), array( 'jquery' ) );
116 }
117 }
118 if ( ! empty( $group['styles'] ) ) {
119 foreach ( $group['styles'] as $style ) {
120 wp_enqueue_style( $this->plugin_slug . '-' . strtok( $style, '.' ), $this->get_url( 'assets/css/' . $style, __FILE__ ) );
121 }
122 }
123 }
124 }
125 wp_enqueue_style( $this->plugin_slug . '-admin-styles', $this->get_url( 'assets/css/panel.css', __FILE__ ), array(), self::VERSION );
126 wp_enqueue_style( 'pods-codemirror' );
127 wp_enqueue_style( 'pods-codemirror-hints' );
128 wp_enqueue_script( $this->plugin_slug . '-admin-scripts', $this->get_url( 'assets/js/panel.js', __FILE__ ), array(), self::VERSION );
129 wp_enqueue_script( 'pods-codemirror' );
130 wp_enqueue_script( 'pods-codemirror-overlay' );
131 wp_enqueue_script( 'pods-codemirror-hints' );
132 wp_enqueue_script( $this->plugin_slug . '-cm-editor', $this->get_url( 'assets/js/editor1.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
133 wp_enqueue_script( 'pods-codemirror-mode-xml' );
134 wp_enqueue_script( 'pods-codemirror-mode-html' );
135 wp_enqueue_script( 'pods-codemirror-mode-css' );
136 }//end if
137
138 }
139
140 /**
141 * Process a field value
142 *
143 * @param $type
144 * @param $value
145 *
146 * @return mixed
147 */
148 public function process_value( $type, $value ) {
149
150 switch ( $type ) {
151 default:
152 return $value;
153 break;
154
155 }
156
157 return $value;
158
159 }
160
161 /**
162 * Register metaboxes.
163 *
164 * @return null
165 */
166 public function activate_metaboxes() {
167
168 add_action( 'add_meta_boxes__pods_template', array( $this, 'add_metaboxes' ), 5, 2 );
169 add_action( 'save_post', array( $this, 'save_post_metaboxes' ), 1, 2 );
170
171 }
172
173 /**
174 * setup meta boxes.
175 *
176 * @param bool $post
177 *
178 * @return null
179 */
180 public function add_metaboxes( $post = false ) {
181
182 if ( ! empty( $post ) ) {
183 if ( ! in_array( $post->post_type, array( '_pods_template' ), true ) ) {
184 return;
185 }
186
187 $slug = $post->post_type;
188 } else {
189 $screen = get_current_screen();
190 if ( ! $screen || ! in_array( $screen->base, array( '_pods_template' ), true ) ) {
191 return;
192 }
193
194 $slug = $screen->base;
195 }
196
197 $this->plugin_screen_hook_suffix[ $slug ] = $post->post_type;
198
199 // Required Styles for metabox
200 wp_enqueue_style( $this->plugin_slug . '-view_template-styles', $this->get_url( 'assets/css/styles-view_template.css', __FILE__ ), array(), self::VERSION );
201
202 // Required scripts for metabox
203 wp_enqueue_script( 'jquery-ui-resizable' );
204 wp_enqueue_script( $this->plugin_slug . '-handlebarsjs', $this->get_url( 'assets/js/handlebars2.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
205 wp_enqueue_script( $this->plugin_slug . '-baldrickjs', $this->get_url( 'assets/js/jquery.baldrick3.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
206 wp_enqueue_script( $this->plugin_slug . '-handlebars-baldrick', $this->get_url( 'assets/js/handlebars.baldrick2.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
207 wp_enqueue_style( $this->plugin_slug . '-pod_reference-styles', $this->get_url( 'assets/css/styles-pod_reference.css', __FILE__ ), array(), self::VERSION );
208
209 // add metabox
210 add_meta_box(
211 'view_template', __( 'Template', 'pods' ), array(
212 $this,
213 'render_metaboxes_custom',
214 ), '_pods_template', 'normal', 'high', array(
215 'slug' => 'view_template',
216 'groups' => array(),
217 )
218 );
219
220 add_meta_box(
221 'pod_reference', __( 'Pod Reference', 'pods' ), array(
222 $this,
223 'render_metaboxes_custom',
224 ), '_pods_template', 'side', 'default', array(
225 'slug' => 'pod_reference',
226 'groups' => array(),
227 )
228 );
229
230 }
231
232 /**
233 * render template based meta boxes.
234 *
235 * @param $post
236 * @param $args
237 *
238 * @return null
239 */
240 public function render_metaboxes_custom( $post, $args ) {
241 wp_enqueue_script( 'jquery' );
242 wp_enqueue_script( 'jquery-ui-core' );
243
244 // include the metabox view
245 echo '<input type="hidden" name="pods_templates_metabox" id="pods_templates_metabox" value="' . esc_attr( wp_create_nonce( plugin_basename( __FILE__ ) ) ) . '" />';
246 echo '<input type="hidden" name="pods_templates_metabox_prefix[]" value="' . esc_attr( $args['args']['slug'] ) . '" />';
247
248 // get post meta to $atts $ post content - ir the widget option
249 if ( ! empty( $post ) ) {
250 $atts = get_post_meta( $post->ID, $args['args']['slug'], true );
251 $content = $post->post_content;
252 } else {
253 $atts = get_option( $args['args']['slug'] );
254 $content = '';
255 }
256
257 if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $args['args']['slug'] . '.php' ) ) {
258 include $this->get_path( __FILE__ ) . 'includes/element-' . $args['args']['slug'] . '.php';
259 } elseif ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $args['args']['slug'] . '.html' ) ) {
260 include $this->get_path( __FILE__ ) . 'includes/element-' . $args['args']['slug'] . '.html';
261 }
262 // add script
263 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args['args']['slug'] . '.php' ) ) {
264 echo "<script type=\"text/javascript\">\r\n";
265 include $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args['args']['slug'] . '.php';
266 echo "</script>\r\n";
267 } elseif ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args['args']['slug'] . '.js' ) ) {
268 wp_enqueue_script( $this->plugin_slug . '-' . $args['args']['slug'] . '-script', $this->get_url( 'assets/js/scripts-' . $args['args']['slug'] . '.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
269 }
270
271 }
272
273 /**
274 * save metabox data
275 *
276 * @param $pid
277 * @param $post
278 */
279 public function save_post_metaboxes( $pid, $post ) {
280
281 if ( ! isset( $_POST['pods_templates_metabox'] ) || ! isset( $_POST['pods_templates_metabox_prefix'] ) ) {
282 return;
283 }
284
285 if ( ! wp_verify_nonce( $_POST['pods_templates_metabox'], plugin_basename( __FILE__ ) ) ) {
286 return $post->ID;
287 }
288 if ( ! current_user_can( 'edit_post', $post->ID ) ) {
289 return $post->ID;
290 }
291 if ( $post->post_type == 'revision' ) {
292 return;
293 }
294
295 foreach ( $_POST['pods_templates_metabox_prefix'] as $prefix ) {
296 if ( ! isset( $_POST[ $prefix ] ) ) {
297 continue;
298 }
299
300 delete_post_meta( $post->ID, $prefix );
301 add_post_meta( $post->ID, $prefix, $_POST[ $prefix ] );
302 }
303
304 // Clean the Pods Blocks cache so that any new/updated templates show up.
305 pods_transient_clear( 'pods_blocks' );
306 pods_transient_clear( 'pods_blocks_js' );
307 }
308
309 /**
310 * create and register an instance ID
311 *
312 * @param $id
313 * @param $process
314 *
315 * @return string
316 */
317 public function element_instance_id( $id, $process ) {
318
319 $this->element_instances[ $id ][ $process ][] = true;
320 $count = count( $this->element_instances[ $id ][ $process ] );
321 if ( $count > 1 ) {
322 return $id . ( $count - 1 );
323 }
324
325 return $id;
326 }
327
328 /**
329 * Render the element
330 *
331 * @param $atts
332 * @param $content
333 * @param $slug
334 * @param bool $head
335 *
336 * @return string|void
337 */
338 public function render_element( $atts, $content, $slug, $head = false ) {
339
340 $raw_atts = $atts;
341
342 if ( ! empty( $head ) ) {
343 $instanceID = $this->element_instance_id( 'pods_templates' . $slug, 'header' );
344 } else {
345 $instanceID = $this->element_instance_id( 'pods_templates' . $slug, 'footer' );
346 }
347
348 // $configfiles = glob($this->get_path( __FILE__ ) .'configs/'.$slug.'-*.php');
349 if ( file_exists( $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php' ) ) {
350 include $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php';
351
352 $defaults = array();
353 foreach ( $configfiles as $file ) {
354
355 include $file;
356 foreach ( $group['fields'] as $variable => $conf ) {
357 if ( ! empty( $group['multiple'] ) ) {
358 $value = array( $this->process_value( $conf['type'], $conf['default'] ) );
359 } else {
360 $value = $this->process_value( $conf['type'], $conf['default'] );
361 }
362 if ( ! empty( $group['multiple'] ) ) {
363 if ( isset( $atts[ $variable . '_1' ] ) ) {
364 $index = 1;
365 $value = array();
366 while ( isset( $atts[ $variable . '_' . $index ] ) ) {
367 $value[] = $this->process_value( $conf['type'], $atts[ $variable . '_' . $index ] );
368 $index ++;
369 }
370 } elseif ( isset( $atts[ $variable ] ) ) {
371 if ( is_array( $atts[ $variable ] ) ) {
372 foreach ( $atts[ $variable ] as &$varval ) {
373 $varval = $this->process_value( $conf['type'], $varval );
374 }
375 $value = $atts[ $variable ];
376 } else {
377 $value[] = $this->process_value( $conf['type'], $atts[ $variable ] );
378 }
379 }
380 } else {
381 if ( isset( $atts[ $variable ] ) ) {
382 $value = $this->process_value( $conf['type'], $atts[ $variable ] );
383 }
384 }//end if
385
386 if ( ! empty( $group['multiple'] ) && ! empty( $value ) ) {
387 foreach ( $value as $key => $val ) {
388 $groups[ $group['master'] ][ $key ][ $variable ] = $val;
389 }
390 }
391 $defaults[ $variable ] = $value;
392 }//end foreach
393 }//end foreach
394 $atts = $defaults;
395 }//end if
396
397 // pull in the assets
398 $assets = array();
399 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/assets-' . $slug . '.php' ) ) {
400 include $this->get_path( __FILE__ ) . 'assets/assets-' . $slug . '.php';
401 }
402
403 ob_start();
404 if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.php' ) ) {
405 include $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.php';
406 } else {
407 if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.html' ) ) {
408 include $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.html';
409 }
410 }
411 $out = ob_get_clean();
412
413 if ( ! empty( $head ) ) {
414
415 // process headers - CSS
416 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.php' ) ) {
417 ob_start();
418 include $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.php';
419 $this->element_header_styles[] = ob_get_clean();
420 add_action( 'wp_head', array( $this, 'header_styles' ) );
421 } else {
422 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.css' ) ) {
423 wp_enqueue_style( $this->plugin_slug . '-' . $slug . '-styles', $this->get_url( 'assets/css/styles-' . $slug . '.css', __FILE__ ), array(), self::VERSION );
424 }
425 }
426 // process headers - JS
427 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.php' ) ) {
428 ob_start();
429 include $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.php';
430 $this->element_footer_scripts[] = ob_get_clean();
431 } else {
432 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.js' ) ) {
433 wp_enqueue_script( $this->plugin_slug . '-' . $slug . '-script', $this->get_url( 'assets/js/scripts-' . $slug . '.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
434 }
435 }
436 // get clean do shortcode for header checking
437 ob_start();
438 pods_do_shortcode(
439 $out, array(
440 'each',
441 'pod_sub_template',
442 'once',
443 'pod_once_template',
444 'before',
445 'pod_before_template',
446 'after',
447 'pod_after_template',
448 'if',
449 'pod_if_field',
450 )
451 );
452 ob_get_clean();
453
454 return;
455 }//end if
456
457 return pods_do_shortcode(
458 $out, array(
459 'each',
460 'pod_sub_template',
461 'once',
462 'pod_once_template',
463 'before',
464 'pod_before_template',
465 'after',
466 'pod_after_template',
467 'if',
468 'pod_if_field',
469 )
470 );
471 }
472
473 /**
474 * Render any header styles
475 */
476 public function header_styles() {
477
478 if ( ! empty( $this->element_header_styles ) ) {
479 echo "<style type=\"text/css\">\r\n";
480 foreach ( $this->element_header_styles as $styles ) {
481 echo $styles . "\r\n";
482 }
483 echo "</style>\r\n";
484 }
485 }
486
487 /**
488 * Render any footer scripts
489 */
490 public function footer_scripts() {
491
492 if ( ! empty( $this->element_footer_scripts ) ) {
493 echo "<script type=\"text/javascript\">\r\n";
494 foreach ( $this->element_footer_scripts as $script ) {
495 echo $script . "\r\n";
496 }
497 echo "</script>\r\n";
498 }
499 }
500
501 /**
502 *
503 * Get the current URL
504 *
505 * @param null $src
506 * @param null $path
507 *
508 * @return string
509 */
510 public static function get_url( $src = null, $path = null ) {
511
512 if ( ! empty( $path ) ) {
513 return plugins_url( $src, $path );
514 }
515
516 return trailingslashit( plugins_url( $path, __FILE__ ) );
517 }
518
519 /**
520 *
521 * Get the current URL
522 *
523 * @param null $src
524 *
525 * @return string
526 */
527 public static function get_path( $src = null ) {
528
529 return plugin_dir_path( $src );
530
531 }
532
533 }
534