Pods_Templates

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

Defined (1)

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

/components/Templates/Templates.php  
  1. class Pods_Templates extends PodsComponent { 
  2.  
  3. /** 
  4. * Pods object 
  5. * @var object 
  6. * @since 2.0 
  7. */ 
  8. static $obj = null; 
  9.  
  10. /** 
  11. * Whether to enable deprecated functionality based on old function usage 
  12. * @var bool 
  13. * @since 2.0 
  14. */ 
  15. static $deprecated = false; 
  16.  
  17. /** 
  18. * Object type 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. private $object_type = '_pods_template'; 
  23.  
  24. /** 
  25. * Do things like register/enqueue scripts and stylesheets 
  26. * @since 2.0 
  27. */ 
  28. public function __construct () { 
  29. $args = array( 
  30. 'label' => 'Pod Templates',  
  31. 'labels' => array( 'singular_name' => 'Pod Template' ),  
  32. 'public' => false,  
  33. 'can_export' => false,  
  34. 'show_ui' => true,  
  35. 'show_in_menu' => false,  
  36. 'query_var' => false,  
  37. 'rewrite' => false,  
  38. 'has_archive' => false,  
  39. 'hierarchical' => false,  
  40. 'supports' => array( 'title', 'author', 'revisions' ),  
  41. 'menu_icon' => 'dashicons-pods' 
  42. ); 
  43.  
  44. if ( !pods_is_admin() ) 
  45. $args[ 'capability_type' ] = 'pods_template'; 
  46.  
  47. $args = PodsInit::object_label_fix( $args, 'post_type' ); 
  48.  
  49. register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_template', $args ) ); 
  50.  
  51. if ( is_admin() ) { 
  52. add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 ); 
  53.  
  54. add_action( 'dbx_post_advanced', array( $this, 'edit_page_form' ), 10 ); 
  55.  
  56. add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) ); 
  57.  
  58. add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 ); 
  59. add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 ); 
  60.  
  61. add_action( 'pods_meta_save_pre_post__pods_template', array( $this, 'fix_filters' ), 10, 5 ); 
  62. add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 ); 
  63. add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 ); 
  64. add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 ); 
  65. add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) ); 
  66.  
  67. add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) ); 
  68.  
  69.  
  70. public function disable_builder_layout ( $post_types ) { 
  71. $post_types[] = $this->object_type; 
  72.  
  73. return $post_types; 
  74.  
  75. /** 
  76. * Update Post Type messages 
  77. * @param array $messages 
  78. * @return array 
  79. * @since 2.0.2 
  80. */ 
  81. public function setup_updated_messages ( $messages ) { 
  82. global $post, $post_ID; 
  83.  
  84. $post_type = get_post_type_object( $this->object_type ); 
  85.  
  86. $labels = $post_type->labels; 
  87.  
  88. $messages[ $post_type->name ] = array( 
  89. 1 => sprintf( __( '%s updated. <a href="%s">%s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),  
  90. 2 => __( 'Custom field updated.', 'pods' ),  
  91. 3 => __( 'Custom field deleted.', 'pods' ),  
  92. 4 => sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ),  
  93. /** translators: %s: date and time of the revision */ 
  94. 5 => isset( $_GET[ 'revision' ] ) ? sprintf( __( '%s restored to revision from %s', 'pods' ), $labels->singular_name, wp_post_revision_title( (int) $_GET[ 'revision' ], false ) ) : false,  
  95. 6 => sprintf( __( '%s published. <a href="%s">%s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),  
  96. 7 => sprintf( __( '%s saved.', 'pods' ), $labels->singular_name ),  
  97. 8 => sprintf( __( '%s submitted. <a target="_blank" href="%s">Preview %s</a>', 'pods' ),  
  98. $labels->singular_name,  
  99. esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ),  
  100. $labels->singular_name 
  101. ),  
  102. 9 => sprintf( __( '%s scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview %s</a>', 'pods' ),  
  103. $labels->singular_name,  
  104. // translators: Publish box date format, see http://php.net/date 
  105. date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ),  
  106. esc_url( get_permalink( $post_ID ) ),  
  107. $labels->singular_name 
  108. ),  
  109. 10 => sprintf( __( '%s draft updated. <a target="_blank" href="%s">Preview %s</a>', 'pods' ), $labels->singular_name, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels->singular_name ) 
  110. ); 
  111.  
  112. if ( false === (boolean) $post_type->public ) { 
  113. $messages[ $post_type->name ][ 1 ] = sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ); 
  114. $messages[ $post_type->name ][ 6 ] = sprintf( __( '%s published.', 'pods' ), $labels->singular_name ); 
  115. $messages[ $post_type->name ][ 8 ] = sprintf( __( '%s submitted.', 'pods' ), $labels->singular_name ); 
  116. $messages[ $post_type->name ][ 9 ] = sprintf( __( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ),  
  117. $labels->singular_name,  
  118. // translators: Publish box date format, see http://php.net/date 
  119. date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) 
  120. ); 
  121. $messages[ $post_type->name ][ 10 ] = sprintf( __( '%s draft updated.', 'pods' ), $labels->singular_name ); 
  122.  
  123. return $messages; 
  124.  
  125. /** 
  126. * Enqueue styles 
  127. * @since 2.0 
  128. */ 
  129. public function admin_assets () { 
  130. wp_enqueue_style( 'pods-admin' ); 
  131.  
  132. /** 
  133. * Fix filters, specifically removing balanceTags 
  134. * @since 2.0.1 
  135. */ 
  136. public function fix_filters ( $data, $pod = null, $id = null, $groups = null, $post = null ) { 
  137. remove_filter( 'content_save_pre', 'balanceTags', 50 ); 
  138.  
  139. /** 
  140. * Remove unused row actions 
  141. * @since 2.0.5 
  142. */ 
  143. function remove_row_actions ( $actions, $post ) { 
  144. global $current_screen; 
  145.  
  146. if ( !is_object( $current_screen ) || $this->object_type != $current_screen->post_type ) 
  147. return $actions; 
  148.  
  149. if ( isset( $actions[ 'view' ] ) ) 
  150. unset( $actions[ 'view' ] ); 
  151.  
  152. if ( isset( $actions[ 'inline hide-if-no-js' ] ) ) 
  153. unset( $actions[ 'inline hide-if-no-js' ] ); 
  154.  
  155. // W3 Total Cache 
  156. if ( isset( $actions[ 'pgcache_purge' ] ) ) 
  157. unset( $actions[ 'pgcache_purge' ] ); 
  158.  
  159. return $actions; 
  160.  
  161. /** 
  162. * Remove unused bulk actions 
  163. * @since 2.0.5 
  164. */ 
  165. public function remove_bulk_actions ( $actions ) { 
  166. if ( isset( $actions[ 'edit' ] ) ) 
  167. unset( $actions[ 'edit' ] ); 
  168.  
  169. return $actions; 
  170.  
  171. /** 
  172. * Clear cache on save 
  173. * @since 2.0 
  174. */ 
  175. public function clear_cache ( $data, $pod = null, $id = null, $groups = null, $post = null ) { 
  176. $old_post = $id; 
  177.  
  178. if ( !is_object( $id ) ) 
  179. $old_post = null; 
  180.  
  181. if ( is_object( $post ) && $this->object_type != $post->post_type ) 
  182. return; 
  183.  
  184. if ( !is_array( $data ) && 0 < $data ) { 
  185. $post = $data; 
  186. $post = get_post( $post ); 
  187.  
  188. if ( $this->object_type == $post->object_type ) 
  189. pods_transient_clear( 'pods_object_templates' ); 
  190.  
  191. /** 
  192. * Change post title placeholder text 
  193. * @since 2.0 
  194. */ 
  195. public function set_title_text ( $text, $post ) { 
  196. return __( 'Enter template name here', 'pods' ); 
  197.  
  198. /** 
  199. * Edit page form 
  200. * @since 2.0 
  201. */ 
  202. public function edit_page_form () { 
  203. global $post_type; 
  204.  
  205. if ( $this->object_type != $post_type ) 
  206. return; 
  207.  
  208. add_filter( 'enter_title_here', array( $this, 'set_title_text' ), 10, 2 ); 
  209.  
  210. /** 
  211. * Add meta boxes to the page 
  212. * @since 2.0 
  213. */ 
  214. public function add_meta_boxes () { 
  215. $pod = array( 
  216. 'name' => $this->object_type,  
  217. 'type' => 'post_type' 
  218. ); 
  219.  
  220. if ( isset( PodsMeta::$post_types[ $pod[ 'name' ] ] ) ) 
  221. return; 
  222.  
  223. $fields = array( 
  224. array( 
  225. 'name' => 'admin_only',  
  226. 'label' => __( 'Show to Admins Only?', 'pods' ),  
  227. 'default' => 0,  
  228. 'type' => 'boolean',  
  229. 'dependency' => true 
  230. ),  
  231. array( 
  232. 'name' => 'restrict_capability',  
  233. 'label' => __( 'Restrict access by Capability?', 'pods' ),  
  234. 'help' => array( 
  235. __( '<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' ),  
  236. 'http://codex.wordpress.org/Roles_and_Capabilities' 
  237. ),  
  238. 'default' => 0,  
  239. 'type' => 'boolean',  
  240. 'dependency' => true 
  241. ),  
  242. array( 
  243. 'name' => 'capability_allowed',  
  244. 'label' => __( 'Capability Allowed', 'pods' ),  
  245. 'type' => 'pick',  
  246. 'pick_object' => 'capability',  
  247. 'pick_format_type' => 'multi',  
  248. 'pick_format_multi' => 'autocomplete',  
  249. 'pick_ajax' => false,  
  250. 'default' => '',  
  251. 'depends-on' => array( 
  252. 'restrict_capability' => true 
  253. ); 
  254.  
  255. pods_group_add( $pod, __( 'Restrict Access', 'pods' ), $fields, 'normal', 'high' ); 
  256.  
  257. /** 
  258. * Get the fields 
  259. * @param null $_null 
  260. * @param int $post_ID 
  261. * @param string $meta_key 
  262. * @param bool $single 
  263. * @return array|bool|int|mixed|null|string|void 
  264. */ 
  265. public function get_meta ( $_null, $post_ID = null, $meta_key = null, $single = false ) { 
  266. if ( 'code' == $meta_key ) { 
  267. $post = get_post( $post_ID ); 
  268.  
  269. if ( is_object( $post ) && $this->object_type == $post->post_type ) 
  270. return $post->post_content; 
  271.  
  272. return $_null; 
  273.  
  274. /** 
  275. * Save the fields 
  276. * @param $_null 
  277. * @param int $post_ID 
  278. * @param string $meta_key 
  279. * @param string $meta_value 
  280. * @return bool|int|null 
  281. */ 
  282. public function save_meta ( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) { 
  283. if ( 'code' == $meta_key ) { 
  284. $post = get_post( $post_ID ); 
  285.  
  286. if ( is_object( $post ) && $this->object_type == $post->post_type ) { 
  287. $postdata = array( 
  288. 'ID' => $post_ID,  
  289. 'post_content' => $meta_value 
  290. ); 
  291.  
  292. remove_filter( current_filter(), array( $this, __FUNCTION__ ), 10 ); 
  293.  
  294. $revisions = false; 
  295.  
  296. if ( has_action( 'pre_post_update', 'wp_save_post_revision' ) ) { 
  297. remove_action( 'pre_post_update', 'wp_save_post_revision' ); 
  298.  
  299. $revisions = true; 
  300.  
  301. wp_update_post( (object) $postdata ); // objects will be automatically sanitized 
  302.  
  303. if ( $revisions ) 
  304. add_action( 'pre_post_update', 'wp_save_post_revision' ); 
  305.  
  306. return true; 
  307.  
  308. return $_null; 
  309.  
  310. /** 
  311. * Display the page template 
  312. * @param string $template_name The template name 
  313. * @param string $code Custom template code to use instead 
  314. * @param object $obj The Pods object 
  315. * @param bool $deprecated Whether to use deprecated functionality based on old function usage 
  316. * @return mixed|string|void 
  317. * @since 2.0 
  318. */ 
  319. public static function template ( $template_name, $code = null, $obj = null, $deprecated = false ) { 
  320. if ( !empty( $obj ) ) 
  321. self::$obj =& $obj; 
  322. else 
  323. $obj =& self::$obj; 
  324.  
  325. self::$deprecated = $deprecated; 
  326.  
  327. if ( empty( $obj ) || !is_object( $obj ) ) 
  328. return ''; 
  329.  
  330. $template = array( 
  331. 'id' => 0,  
  332. 'slug' => $template_name,  
  333. 'code' => $code,  
  334. 'options' => array(),  
  335. ); 
  336.  
  337. if ( empty( $code ) && !empty( $template_name ) ) { 
  338. $template_obj = $obj->api->load_template( array( 'name' => $template_name ) ); 
  339.  
  340. if ( !empty( $template_obj ) ) { 
  341. $template = $template_obj; 
  342.  
  343. if ( !empty( $template[ 'code' ] ) ) 
  344. $code = $template[ 'code' ]; 
  345.  
  346. $permission = pods_permission( $template[ 'options' ] ); 
  347.  
  348. $permission = (boolean) apply_filters( 'pods_templates_permission', $permission, $code, $template, $obj ); 
  349.  
  350. if ( !$permission ) { 
  351. return apply_filters( 'pods_templates_permission_denied', __( 'You do not have access to view this content.', 'pods' ), $code, $template, $obj ); 
  352.  
  353. $code = apply_filters( 'pods_templates_pre_template', $code, $template, $obj ); 
  354. $code = apply_filters( 'pods_templates_pre_template_' . $template[ 'slug' ], $code, $template, $obj ); 
  355.  
  356. ob_start(); 
  357.  
  358. if ( !empty( $code ) ) { 
  359. // Only detail templates need $this->id 
  360. if ( empty( $obj->id ) ) { 
  361. while ( $obj->fetch() ) { 
  362. echo self::do_template( $code, $obj ); 
  363. else 
  364. echo self::do_template( $code, $obj ); 
  365. elseif ( $template_name == trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template_name ), ' /-' ) ) { 
  366. $default_templates = array( 
  367. 'pods/' . $template_name,  
  368. 'pods-' . $template_name,  
  369. $template_name 
  370. ); 
  371.  
  372. $default_templates = apply_filters( 'pods_template_default_templates', $default_templates ); 
  373.  
  374. if ( empty( $obj->id ) ) { 
  375. while ( $obj->fetch() ) { 
  376. pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) ); 
  377. else 
  378. pods_template_part( $default_templates, compact( array_keys( get_defined_vars() ) ) ); 
  379.  
  380.  
  381. $out = ob_get_clean(); 
  382.  
  383. $out = apply_filters( 'pods_templates_post_template', $out, $code, $template, $obj ); 
  384. $out = apply_filters( 'pods_templates_post_template_' . $template[ 'slug' ], $out, $code, $template, $obj ); 
  385.  
  386. return $out; 
  387.  
  388. /** 
  389. * Parse a template string 
  390. * @param string $code The template string to parse 
  391. * @param object $obj The Pods object 
  392. * @since 1.8.5 
  393. */ 
  394. public static function do_template ( $code, $obj = null ) { 
  395. if ( !empty( $obj ) ) 
  396. self::$obj =& $obj; 
  397. else 
  398. $obj =& self::$obj; 
  399.  
  400. if ( empty( $obj ) || !is_object( $obj ) ) 
  401. return ''; 
  402.  
  403. $code = trim( $code ); 
  404.  
  405. if ( false !== strpos( $code, '<?' ) && ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) ) { 
  406. pods_deprecated( 'Pod Template PHP code has been deprecated, please use WP Templates instead of embedding PHP.', '2.3' ); 
  407.  
  408. $code = str_replace( '$this->', '$obj->', $code ); 
  409.  
  410. ob_start(); 
  411.  
  412. eval( "?>$code" ); 
  413.  
  414. $out = ob_get_clean(); 
  415. else 
  416. $out = $code; 
  417.  
  418. $out = $obj->do_magic_tags( $out ); 
  419.  
  420. return apply_filters( 'pods_templates_do_template', $out, $code, $obj ); 
  421.