Pods_Templates_Frontier

The Pods - Custom Content Types and Fields Pods Templates Frontier class.

Defined (1)

The class is defined in the following location(s).

/components/Templates/class-pods_templates.php  
  1. class Pods_Templates_Frontier { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. const VERSION = '1.00'; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. protected $plugin_slug = 'pods_templates'; 
  12.  
  13. /** 
  14. * @var object 
  15. */ 
  16. protected static $instance = null; 
  17.  
  18. /** 
  19. * @var array 
  20. */ 
  21. protected $element_instances = array(); 
  22.  
  23. /** 
  24. * @var array 
  25. */ 
  26. protected $element_css_once = array(); 
  27.  
  28. /** 
  29. * @var array 
  30. */ 
  31. protected $elements = array(); 
  32.  
  33. /** 
  34. * @var string 
  35. */ 
  36. protected $plugin_screen_hook_suffix = null; 
  37.  
  38. /** 
  39. * Initialize the plugin by setting localization, filters, and administration functions. 
  40. */ 
  41. private function __construct() { 
  42.  
  43. add_filter( 'pods_templates_pre_template', 'frontier_prefilter_template', 25, 4 ); 
  44. // Load admin style sheet and JavaScript. 
  45. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_stylescripts' ), 20 ); 
  46. add_action( 'wp_footer', array( $this, 'footer_scripts' ) ); 
  47. add_action( 'init', array( $this, 'activate_metaboxes' ) ); 
  48.  
  49. /** 
  50. * Return an instance of this class. 
  51. * @return object A single instance of this class. 
  52. */ 
  53. public static function get_instance() { 
  54.  
  55. // If the single instance hasn't been set, set it now. 
  56. if ( null == self::$instance ) { 
  57. self::$instance = new self; 
  58.  
  59. return self::$instance; 
  60.  
  61. /** 
  62. * Register and enqueue admin-specific style sheet. 
  63. * @return null 
  64. */ 
  65. public function enqueue_admin_stylescripts() { 
  66.  
  67. $screen = get_current_screen(); 
  68.  
  69. if ( !isset( $this->plugin_screen_hook_suffix ) ) { 
  70. return; 
  71.  
  72. if ( in_array( $screen->id, $this->plugin_screen_hook_suffix ) ) { 
  73. $slug = array_search( $screen->id, $this->plugin_screen_hook_suffix ); 
  74. //$configfiles = glob( $this->get_path( __FILE__ ) .'configs/'.$slug.'-*.php' ); 
  75. if ( file_exists( $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php' ) ) { 
  76. include $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php'; 
  77.  
  78. if ( !empty( $configfiles ) ) { 
  79.  
  80. foreach ( $configfiles as $key => $fieldfile ) { 
  81. include $fieldfile; 
  82. if ( !empty( $group[ 'scripts' ] ) ) { 
  83. foreach ( $group[ 'scripts' ] as $script ) { 
  84. wp_enqueue_script( $this->plugin_slug . '-' . strtok( $script, '.' ), $this->get_url( 'assets/js/' . $script, __FILE__ ), array( 'jquery' ) ); 
  85. if ( !empty( $group[ 'styles' ] ) ) { 
  86. foreach ( $group[ 'styles' ] as $style ) { 
  87. wp_enqueue_style( $this->plugin_slug . '-' . strtok( $style, '.' ), $this->get_url( 'assets/css/' . $style, __FILE__ ) ); 
  88. wp_enqueue_style( $this->plugin_slug . '-admin-styles', $this->get_url( 'assets/css/panel.css', __FILE__ ), array(), self::VERSION ); 
  89. wp_enqueue_style( 'pods-codemirror' ); 
  90. wp_enqueue_script( $this->plugin_slug . '-admin-scripts', $this->get_url( 'assets/js/panel.js', __FILE__ ), array(), self::VERSION ); 
  91. wp_enqueue_script( 'pods_codemirror' ); 
  92. wp_enqueue_script( 'pods-codemirror-overlay' ); 
  93. wp_enqueue_script( 'pods-codemirror-hints' ); 
  94. wp_enqueue_script( $this->plugin_slug . '-cm-editor', $this->get_url( 'assets/js/editor1.js', __FILE__ ), array( 'jquery' ), self::VERSION, true ); 
  95. wp_enqueue_script( 'pods-codemirror-mode-xml' ); 
  96. wp_enqueue_script( 'pods-codemirror-mode-html' ); 
  97. wp_enqueue_script( 'pods-codemirror-mode-css' ); 
  98.  
  99.  
  100. /** 
  101. * Process a field value 
  102. */ 
  103. public function process_value( $type, $value ) { 
  104.  
  105. switch ( $type ) { 
  106. default: 
  107. return $value; 
  108. break; 
  109.  
  110.  
  111. return $value; 
  112.  
  113.  
  114. /** 
  115. * Register metaboxes. 
  116. * @return null 
  117. */ 
  118. public function activate_metaboxes() { 
  119.  
  120. add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ), 5, 4 ); 
  121. add_action( 'save_post', array( $this, 'save_post_metaboxes' ), 1, 2 ); 
  122.  
  123.  
  124. /** 
  125. * setup meta boxes. 
  126. * @return null 
  127. */ 
  128. function add_metaboxes( $slug, $post = false ) { 
  129.  
  130. if ( !empty( $post ) ) { 
  131. if ( !in_array( $post->post_type, array( '_pods_template' ) ) ) { 
  132. return; 
  133. else { 
  134. $screen = get_current_screen(); 
  135. if ( !in_array( $screen->base, array( '_pods_template' ) ) ) { 
  136. return; 
  137.  
  138. $this->plugin_screen_hook_suffix[ $slug ] = $slug; 
  139.  
  140. // Required Styles for metabox 
  141. wp_enqueue_style( $this->plugin_slug . '-view_template-styles', $this->get_url( 'assets/css/styles-view_template.css', __FILE__ ), array(), self::VERSION ); 
  142.  
  143. // Required scripts for metabox 
  144. wp_enqueue_script( 'jquery-ui-resizable' ); 
  145. wp_enqueue_script( $this->plugin_slug . '-handlebarsjs', $this->get_url( 'assets/js/handlebars2.js', __FILE__ ), array( 'jquery' ), self::VERSION, true ); 
  146. wp_enqueue_script( $this->plugin_slug . '-baldrickjs', $this->get_url( 'assets/js/jquery.baldrick3.js', __FILE__ ), array( 'jquery' ), self::VERSION, true ); 
  147. wp_enqueue_script( $this->plugin_slug . '-handlebars-baldrick', $this->get_url( 'assets/js/handlebars.baldrick2.js', __FILE__ ), array( 'jquery' ), self::VERSION, true ); 
  148. wp_enqueue_style( $this->plugin_slug . '-pod_reference-styles', $this->get_url( 'assets/css/styles-pod_reference.css', __FILE__ ), array(), self::VERSION ); 
  149.  
  150. // add metabox 
  151. add_meta_box( 'view_template', __( 'Template', 'pods' ), array( 
  152. $this,  
  153. 'render_metaboxes_custom' 
  154. ), '_pods_template', 'normal', 'high', array( 
  155. 'slug' => 'view_template',  
  156. 'groups' => array() 
  157. ) ); 
  158. add_meta_box( 'pod_reference', __( 'Pod Reference', 'pods' ), array( 
  159. $this,  
  160. 'render_metaboxes_custom' 
  161. ), '_pods_template', 'side', 'default', array( 
  162. 'slug' => 'pod_reference',  
  163. 'groups' => array() 
  164. ) ); 
  165.  
  166.  
  167. /** 
  168. * render template based meta boxes. 
  169. * @return null 
  170. */ 
  171. function render_metaboxes_custom( $post, $args ) { 
  172.  
  173. // include the metabox view 
  174. echo '<input type="hidden" name="pods_templates_metabox" id="pods_templates_metabox" value="' . esc_attr( wp_create_nonce( plugin_basename( __FILE__ ) ) ) . '" />'; 
  175. echo '<input type="hidden" name="pods_templates_metabox_prefix[]" value="' . esc_attr( $args[ 'args' ][ 'slug' ] ) . '" />'; 
  176.  
  177. //get post meta to $atts $ post content - ir the widget option 
  178. if ( !empty( $post ) ) { 
  179. $atts = get_post_meta( $post->ID, $args[ 'args' ][ 'slug' ], true ); 
  180. $content = $post->post_content; 
  181. else { 
  182. $atts = get_option( $args[ 'args' ][ 'slug' ] ); 
  183. $content = ''; 
  184.  
  185. if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $args[ 'args' ][ 'slug' ] . '.php' ) ) { 
  186. include $this->get_path( __FILE__ ) . 'includes/element-' . $args[ 'args' ][ 'slug' ] . '.php'; 
  187. elseif ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $args[ 'args' ][ 'slug' ] . '.html' ) ) { 
  188. include $this->get_path( __FILE__ ) . 'includes/element-' . $args[ 'args' ][ 'slug' ] . '.html'; 
  189. // add script 
  190. if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args[ 'args' ][ 'slug' ] . '.php' ) ) { 
  191. echo "<script type=\"text/javascript\">\r\n"; 
  192. include $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args[ 'args' ][ 'slug' ] . '.php'; 
  193. echo "</script>\r\n"; 
  194. elseif ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $args[ 'args' ][ 'slug' ] . '.js' ) ) { 
  195. 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); 
  196.  
  197.  
  198. /** 
  199. * save metabox data 
  200. */ 
  201. function save_post_metaboxes( $pid, $post ) { 
  202.  
  203. if ( !isset( $_POST[ 'pods_templates_metabox' ] ) || !isset( $_POST[ 'pods_templates_metabox_prefix' ] ) ) { 
  204. return; 
  205.  
  206. if ( !wp_verify_nonce( $_POST[ 'pods_templates_metabox' ], plugin_basename( __FILE__ ) ) ) { 
  207. return $post->ID; 
  208. if ( !current_user_can( 'edit_post', $post->ID ) ) { 
  209. return $post->ID; 
  210. if ( $post->post_type == 'revision' ) { 
  211. return; 
  212.  
  213. foreach ( $_POST[ 'pods_templates_metabox_prefix' ] as $prefix ) { 
  214. if ( !isset( $_POST[ $prefix ] ) ) { 
  215. continue; 
  216.  
  217. delete_post_meta( $post->ID, $prefix ); 
  218. add_post_meta( $post->ID, $prefix, $_POST[ $prefix ] ); 
  219.  
  220. /** 
  221. * create and register an instance ID 
  222. */ 
  223. public function element_instance_id( $id, $process ) { 
  224.  
  225. $this->element_instances[ $id ][ $process ][ ] = true; 
  226. $count = count( $this->element_instances[ $id ][ $process ] ); 
  227. if ( $count > 1 ) { 
  228. return $id . ( $count - 1 ); 
  229.  
  230. return $id; 
  231.  
  232. /** 
  233. * Render the element 
  234. */ 
  235. public function render_element( $atts, $content, $slug, $head = false ) { 
  236.  
  237. $raw_atts = $atts; 
  238.  
  239. if ( !empty( $head ) ) { 
  240. $instanceID = $this->element_instance_id( 'pods_templates' . $slug, 'header' ); 
  241. else { 
  242. $instanceID = $this->element_instance_id( 'pods_templates' . $slug, 'footer' ); 
  243.  
  244. //$configfiles = glob($this->get_path( __FILE__ ) .'configs/'.$slug.'-*.php'); 
  245. if ( file_exists( $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php' ) ) { 
  246. include $this->get_path( __FILE__ ) . 'configs/fieldgroups-' . $slug . '.php'; 
  247.  
  248. $defaults = array(); 
  249. foreach ( $configfiles as $file ) { 
  250.  
  251. include $file; 
  252. foreach ( $group[ 'fields' ] as $variable => $conf ) { 
  253. if ( !empty( $group[ 'multiple' ] ) ) { 
  254. $value = array( $this->process_value( $conf[ 'type' ], $conf[ 'default' ] ) ); 
  255. else { 
  256. $value = $this->process_value( $conf[ 'type' ], $conf[ 'default' ] ); 
  257. if ( !empty( $group[ 'multiple' ] ) ) { 
  258. if ( isset( $atts[ $variable . '_1' ] ) ) { 
  259. $index = 1; 
  260. $value = array(); 
  261. while ( isset( $atts[ $variable . '_' . $index ] ) ) { 
  262. $value[ ] = $this->process_value( $conf[ 'type' ], $atts[ $variable . '_' . $index ] ); 
  263. $index++; 
  264. elseif ( isset( $atts[ $variable ] ) ) { 
  265. if ( is_array( $atts[ $variable ] ) ) { 
  266. foreach ( $atts[ $variable ] as &$varval ) { 
  267. $varval = $this->process_value( $conf[ 'type' ], $varval ); 
  268. $value = $atts[ $variable ]; 
  269. else { 
  270. $value[ ] = $this->process_value( $conf[ 'type' ], $atts[ $variable ] ); 
  271. else { 
  272. if ( isset( $atts[ $variable ] ) ) { 
  273. $value = $this->process_value( $conf[ 'type' ], $atts[ $variable ] ); 
  274.  
  275. if ( !empty( $group[ 'multiple' ] ) && !empty( $value ) ) { 
  276. foreach ( $value as $key => $val ) { 
  277. $groups[ $group[ 'master' ] ][ $key ][ $variable ] = $val; 
  278. $defaults[ $variable ] = $value; 
  279. $atts = $defaults; 
  280.  
  281. // pull in the assets 
  282. $assets = array(); 
  283. if ( file_exists( $this->get_path( __FILE__ ) . 'assets/assets-' . $slug . '.php' ) ) { 
  284. include $this->get_path( __FILE__ ) . 'assets/assets-' . $slug . '.php'; 
  285.  
  286. ob_start(); 
  287. if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.php' ) ) { 
  288. include $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.php'; 
  289. else { 
  290. if ( file_exists( $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.html' ) ) { 
  291. include $this->get_path( __FILE__ ) . 'includes/element-' . $slug . '.html'; 
  292. $out = ob_get_clean(); 
  293.  
  294. if ( !empty( $head ) ) { 
  295.  
  296. // process headers - CSS 
  297. if ( file_exists( $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.php' ) ) { 
  298. ob_start(); 
  299. include $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.php'; 
  300. $this->element_header_styles[ ] = ob_get_clean(); 
  301. add_action( 'wp_head', array( $this, 'header_styles' ) ); 
  302. else { 
  303. if ( file_exists( $this->get_path( __FILE__ ) . 'assets/css/styles-' . $slug . '.css' ) ) { 
  304. wp_enqueue_style( $this->plugin_slug . '-' . $slug . '-styles', $this->get_url( 'assets/css/styles-' . $slug . '.css', __FILE__ ), array(), self::VERSION ); 
  305. // process headers - JS 
  306. if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.php' ) ) { 
  307. ob_start(); 
  308. include $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.php'; 
  309. $this->element_footer_scripts[ ] = ob_get_clean(); 
  310. else { 
  311. if ( file_exists( $this->get_path( __FILE__ ) . 'assets/js/scripts-' . $slug . '.js' ) ) { 
  312. wp_enqueue_script( $this->plugin_slug . '-' . $slug . '-script', $this->get_url( 'assets/js/scripts-' . $slug . '.js', __FILE__ ), array( 'jquery' ), self::VERSION, true ); 
  313. // get clean do shortcode for header checking 
  314. ob_start(); 
  315. pods_do_shortcode( $out, array( 'each', 'pod_sub_template', 'once', 'pod_once_template', 'before', 'pod_before_template', 'after', 'pod_after_template', 'if', 'pod_if_field' ) ); 
  316. ob_get_clean(); 
  317.  
  318. return; 
  319.  
  320. return pods_do_shortcode( $out, array( 'each', 'pod_sub_template', 'once', 'pod_once_template', 'before', 'pod_before_template', 'after', 'pod_after_template', 'if', 'pod_if_field' ) ); 
  321.  
  322. /** 
  323. * Render any header styles 
  324. */ 
  325. public function header_styles() { 
  326.  
  327. if ( !empty( $this->element_header_styles ) ) { 
  328. echo "<style type=\"text/css\">\r\n"; 
  329. foreach ( $this->element_header_styles as $styles ) { 
  330. echo $styles . "\r\n"; 
  331. echo "</style>\r\n"; 
  332.  
  333. /** 
  334. * Render any footer scripts 
  335. */ 
  336. public function footer_scripts() { 
  337.  
  338. if ( !empty( $this->element_footer_scripts ) ) { 
  339. echo "<script type=\"text/javascript\">\r\n"; 
  340. foreach ( $this->element_footer_scripts as $script ) { 
  341. echo $script . "\r\n"; 
  342. echo "</script>\r\n"; 
  343.  
  344. /*** 
  345. * Get the current URL 
  346. */ 
  347. static function get_url( $src = null, $path = null ) { 
  348.  
  349. if ( !empty( $path ) ) { 
  350. return plugins_url( $src, $path ); 
  351.  
  352. return trailingslashit( plugins_url( $path, __FILE__ ) ); 
  353.  
  354. /*** 
  355. * Get the current URL 
  356. */ 
  357. static function get_path( $src = null ) { 
  358.  
  359. return plugin_dir_path( $src ); 
  360.  
  361.