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