PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.9.19.4
Pods – Custom Content Types and Fields v2.9.19.4
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 / class-pods_templates.php
pods / components / Templates Last commit date
assets 2 weeks ago includes 2 weeks ago Templates.php 2 weeks ago class-pods_templates.php 2 weeks ago
class-pods_templates.php
531 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_script( $this->plugin_slug . '-admin-scripts', $this->get_url( 'assets/js/panel.js', __FILE__ ), array(), self::VERSION );
128 wp_enqueue_script( 'pods_codemirror' );
129 wp_enqueue_script( 'pods-codemirror-overlay' );
130 wp_enqueue_script( 'pods-codemirror-hints' );
131 wp_enqueue_script( $this->plugin_slug . '-cm-editor', $this->get_url( 'assets/js/editor1.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
132 wp_enqueue_script( 'pods-codemirror-mode-xml' );
133 wp_enqueue_script( 'pods-codemirror-mode-html' );
134 wp_enqueue_script( 'pods-codemirror-mode-css' );
135 }//end if
136
137 }
138
139 /**
140 * Process a field value
141 *
142 * @param $type
143 * @param $value
144 *
145 * @return mixed
146 */
147 public function process_value( $type, $value ) {
148
149 switch ( $type ) {
150 default:
151 return $value;
152 break;
153
154 }
155
156 return $value;
157
158 }
159
160 /**
161 * Register metaboxes.
162 *
163 * @return null
164 */
165 public function activate_metaboxes() {
166
167 add_action( 'add_meta_boxes__pods_template', array( $this, 'add_metaboxes' ), 5, 2 );
168 add_action( 'save_post', array( $this, 'save_post_metaboxes' ), 1, 2 );
169
170 }
171
172 /**
173 * setup meta boxes.
174 *
175 * @param bool $post
176 *
177 * @return null
178 */
179 public function add_metaboxes( $post = false ) {
180
181 if ( ! empty( $post ) ) {
182 if ( ! in_array( $post->post_type, array( '_pods_template' ), true ) ) {
183 return;
184 }
185
186 $slug = $post->post_type;
187 } else {
188 $screen = get_current_screen();
189 if ( ! $screen || ! in_array( $screen->base, array( '_pods_template' ), true ) ) {
190 return;
191 }
192
193 $slug = $screen->base;
194 }
195
196 $this->plugin_screen_hook_suffix[ $slug ] = $post->post_type;
197
198 // Required Styles for metabox
199 wp_enqueue_style( $this->plugin_slug . '-view_template-styles', $this->get_url( 'assets/css/styles-view_template.css', __FILE__ ), array(), self::VERSION );
200
201 // Required scripts for metabox
202 wp_enqueue_script( 'jquery-ui-resizable' );
203 wp_enqueue_script( $this->plugin_slug . '-handlebarsjs', $this->get_url( 'assets/js/handlebars2.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
204 wp_enqueue_script( $this->plugin_slug . '-baldrickjs', $this->get_url( 'assets/js/jquery.baldrick3.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
205 wp_enqueue_script( $this->plugin_slug . '-handlebars-baldrick', $this->get_url( 'assets/js/handlebars.baldrick2.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
206 wp_enqueue_style( $this->plugin_slug . '-pod_reference-styles', $this->get_url( 'assets/css/styles-pod_reference.css', __FILE__ ), array(), self::VERSION );
207
208 // add metabox
209 add_meta_box(
210 'view_template', __( 'Template', 'pods' ), array(
211 $this,
212 'render_metaboxes_custom',
213 ), '_pods_template', 'normal', 'high', array(
214 'slug' => 'view_template',
215 'groups' => array(),
216 )
217 );
218
219 add_meta_box(
220 'pod_reference', __( 'Pod Reference', 'pods' ), array(
221 $this,
222 'render_metaboxes_custom',
223 ), '_pods_template', 'side', 'default', array(
224 'slug' => 'pod_reference',
225 'groups' => array(),
226 )
227 );
228
229 }
230
231 /**
232 * render template based meta boxes.
233 *
234 * @param $post
235 * @param $args
236 *
237 * @return null
238 */
239 public function render_metaboxes_custom( $post, $args ) {
240
241 // include the metabox view
242 echo '<input type="hidden" name="pods_templates_metabox" id="pods_templates_metabox" value="' . esc_attr( wp_create_nonce( plugin_basename( __FILE__ ) ) ) . '" />';
243 echo '<input type="hidden" name="pods_templates_metabox_prefix[]" value="' . esc_attr( $args['args']['slug'] ) . '" />';
244
245 // get post meta to $atts $ post content - ir the widget option
246 if ( ! empty( $post ) ) {
247 $atts = get_post_meta( $post->ID, $args['args']['slug'], true );
248 $content = $post->post_content;
249 } else {
250 $atts = get_option( $args['args']['slug'] );
251 $content = '';
252 }
253
254 if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $args['args']['slug'] . '.php' ) ) {
255 include $this->get_path( __FILE__ ) . 'includes/element-' . $args['args']['slug'] . '.php';
256 } elseif ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $args['args']['slug'] . '.html' ) ) {
257 include $this->get_path( __FILE__ ) . 'includes/element-' . $args['args']['slug'] . '.html';
258 }
259 // add script
260 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args['args']['slug'] . '.php' ) ) {
261 echo "<script type=\"text/javascript\">\r\n";
262 include $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args['args']['slug'] . '.php';
263 echo "</script>\r\n";
264 } elseif ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args['args']['slug'] . '.js' ) ) {
265 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 );
266 }
267
268 }
269
270 /**
271 * save metabox data
272 *
273 * @param $pid
274 * @param $post
275 */
276 public function save_post_metaboxes( $pid, $post ) {
277
278 if ( ! isset( $_POST['pods_templates_metabox'] ) || ! isset( $_POST['pods_templates_metabox_prefix'] ) ) {
279 return;
280 }
281
282 if ( ! wp_verify_nonce( $_POST['pods_templates_metabox'], plugin_basename( __FILE__ ) ) ) {
283 return $post->ID;
284 }
285 if ( ! current_user_can( 'edit_post', $post->ID ) ) {
286 return $post->ID;
287 }
288 if ( $post->post_type == 'revision' ) {
289 return;
290 }
291
292 foreach ( $_POST['pods_templates_metabox_prefix'] as $prefix ) {
293 if ( ! isset( $_POST[ $prefix ] ) ) {
294 continue;
295 }
296
297 delete_post_meta( $post->ID, $prefix );
298 add_post_meta( $post->ID, $prefix, $_POST[ $prefix ] );
299 }
300
301 // Clean the Pods Blocks cache so that any new/updated templates show up.
302 pods_transient_clear( 'pods_blocks' );
303 pods_transient_clear( 'pods_blocks_js' );
304 }
305
306 /**
307 * create and register an instance ID
308 *
309 * @param $id
310 * @param $process
311 *
312 * @return string
313 */
314 public function element_instance_id( $id, $process ) {
315
316 $this->element_instances[ $id ][ $process ][] = true;
317 $count = count( $this->element_instances[ $id ][ $process ] );
318 if ( $count > 1 ) {
319 return $id . ( $count - 1 );
320 }
321
322 return $id;
323 }
324
325 /**
326 * Render the element
327 *
328 * @param $atts
329 * @param $content
330 * @param $slug
331 * @param bool $head
332 *
333 * @return string|void
334 */
335 public function render_element( $atts, $content, $slug, $head = false ) {
336
337 $raw_atts = $atts;
338
339 if ( ! empty( $head ) ) {
340 $instanceID = $this->element_instance_id( 'pods_templates' . $slug, 'header' );
341 } else {
342 $instanceID = $this->element_instance_id( 'pods_templates' . $slug, 'footer' );
343 }
344
345 // $configfiles = glob($this->get_path( __FILE__ ) .'configs/'.$slug.'-*.php');
346 if ( file_exists( $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php' ) ) {
347 include $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php';
348
349 $defaults = array();
350 foreach ( $configfiles as $file ) {
351
352 include $file;
353 foreach ( $group['fields'] as $variable => $conf ) {
354 if ( ! empty( $group['multiple'] ) ) {
355 $value = array( $this->process_value( $conf['type'], $conf['default'] ) );
356 } else {
357 $value = $this->process_value( $conf['type'], $conf['default'] );
358 }
359 if ( ! empty( $group['multiple'] ) ) {
360 if ( isset( $atts[ $variable . '_1' ] ) ) {
361 $index = 1;
362 $value = array();
363 while ( isset( $atts[ $variable . '_' . $index ] ) ) {
364 $value[] = $this->process_value( $conf['type'], $atts[ $variable . '_' . $index ] );
365 $index ++;
366 }
367 } elseif ( isset( $atts[ $variable ] ) ) {
368 if ( is_array( $atts[ $variable ] ) ) {
369 foreach ( $atts[ $variable ] as &$varval ) {
370 $varval = $this->process_value( $conf['type'], $varval );
371 }
372 $value = $atts[ $variable ];
373 } else {
374 $value[] = $this->process_value( $conf['type'], $atts[ $variable ] );
375 }
376 }
377 } else {
378 if ( isset( $atts[ $variable ] ) ) {
379 $value = $this->process_value( $conf['type'], $atts[ $variable ] );
380 }
381 }//end if
382
383 if ( ! empty( $group['multiple'] ) && ! empty( $value ) ) {
384 foreach ( $value as $key => $val ) {
385 $groups[ $group['master'] ][ $key ][ $variable ] = $val;
386 }
387 }
388 $defaults[ $variable ] = $value;
389 }//end foreach
390 }//end foreach
391 $atts = $defaults;
392 }//end if
393
394 // pull in the assets
395 $assets = array();
396 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/assets-' . $slug . '.php' ) ) {
397 include $this->get_path( __FILE__ ) . 'assets/assets-' . $slug . '.php';
398 }
399
400 ob_start();
401 if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.php' ) ) {
402 include $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.php';
403 } else {
404 if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.html' ) ) {
405 include $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.html';
406 }
407 }
408 $out = ob_get_clean();
409
410 if ( ! empty( $head ) ) {
411
412 // process headers - CSS
413 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.php' ) ) {
414 ob_start();
415 include $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.php';
416 $this->element_header_styles[] = ob_get_clean();
417 add_action( 'wp_head', array( $this, 'header_styles' ) );
418 } else {
419 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.css' ) ) {
420 wp_enqueue_style( $this->plugin_slug . '-' . $slug . '-styles', $this->get_url( 'assets/css/styles-' . $slug . '.css', __FILE__ ), array(), self::VERSION );
421 }
422 }
423 // process headers - JS
424 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.php' ) ) {
425 ob_start();
426 include $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.php';
427 $this->element_footer_scripts[] = ob_get_clean();
428 } else {
429 if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.js' ) ) {
430 wp_enqueue_script( $this->plugin_slug . '-' . $slug . '-script', $this->get_url( 'assets/js/scripts-' . $slug . '.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
431 }
432 }
433 // get clean do shortcode for header checking
434 ob_start();
435 pods_do_shortcode(
436 $out, array(
437 'each',
438 'pod_sub_template',
439 'once',
440 'pod_once_template',
441 'before',
442 'pod_before_template',
443 'after',
444 'pod_after_template',
445 'if',
446 'pod_if_field',
447 )
448 );
449 ob_get_clean();
450
451 return;
452 }//end if
453
454 return pods_do_shortcode(
455 $out, array(
456 'each',
457 'pod_sub_template',
458 'once',
459 'pod_once_template',
460 'before',
461 'pod_before_template',
462 'after',
463 'pod_after_template',
464 'if',
465 'pod_if_field',
466 )
467 );
468 }
469
470 /**
471 * Render any header styles
472 */
473 public function header_styles() {
474
475 if ( ! empty( $this->element_header_styles ) ) {
476 echo "<style type=\"text/css\">\r\n";
477 foreach ( $this->element_header_styles as $styles ) {
478 echo $styles . "\r\n";
479 }
480 echo "</style>\r\n";
481 }
482 }
483
484 /**
485 * Render any footer scripts
486 */
487 public function footer_scripts() {
488
489 if ( ! empty( $this->element_footer_scripts ) ) {
490 echo "<script type=\"text/javascript\">\r\n";
491 foreach ( $this->element_footer_scripts as $script ) {
492 echo $script . "\r\n";
493 }
494 echo "</script>\r\n";
495 }
496 }
497
498 /**
499 *
500 * Get the current URL
501 *
502 * @param null $src
503 * @param null $path
504 *
505 * @return string
506 */
507 public static function get_url( $src = null, $path = null ) {
508
509 if ( ! empty( $path ) ) {
510 return plugins_url( $src, $path );
511 }
512
513 return trailingslashit( plugins_url( $path, __FILE__ ) );
514 }
515
516 /**
517 *
518 * Get the current URL
519 *
520 * @param null $src
521 *
522 * @return string
523 */
524 public static function get_path( $src = null ) {
525
526 return plugin_dir_path( $src );
527
528 }
529
530 }
531