Pods_Helpers

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

Defined (1)

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

/components/Helpers.php  
  1. class Pods_Helpers extends PodsComponent { 
  2.  
  3. /** 
  4. * Pods object 
  5. * @var object 
  6. * @since 2.0 
  7. */ 
  8. static $obj = null; 
  9.  
  10. /** 
  11. * Object type 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. private $object_type = '_pods_helper'; 
  16.  
  17. /** 
  18. * Do things like register/enqueue scripts and stylesheets 
  19. * @since 2.0 
  20. */ 
  21. public function __construct () { 
  22. $args = array( 
  23. 'label' => 'Pod Helpers',  
  24. 'labels' => array( 'singular_name' => 'Pod Helper' ),  
  25. 'public' => false,  
  26. 'can_export' => false,  
  27. 'show_ui' => true,  
  28. 'show_in_menu' => false,  
  29. 'query_var' => false,  
  30. 'rewrite' => false,  
  31. 'has_archive' => false,  
  32. 'hierarchical' => false,  
  33. 'supports' => array( 'title', 'author', 'revisions' ),  
  34. 'menu_icon' => 'dashicons-pods' 
  35. ); 
  36.  
  37. if ( !pods_is_admin() ) 
  38. $args[ 'capability_type' ] = 'pods_helper'; 
  39.  
  40. $args = PodsInit::object_label_fix( $args, 'post_type' ); 
  41.  
  42. register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_helper', $args ) ); 
  43.  
  44. if ( is_admin() ) { 
  45. add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 ); 
  46.  
  47. add_action( 'dbx_post_advanced', array( $this, 'edit_page_form' ), 10 ); 
  48.  
  49. add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) ); 
  50. add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 ); 
  51. add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 ); 
  52.  
  53. add_action( 'pods_meta_save_pre_post__pods_helper', array( $this, 'fix_filters' ), 10, 5 ); 
  54. add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 ); 
  55. add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 ); 
  56. add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 ); 
  57. add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) ); 
  58.  
  59. add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) ); 
  60.  
  61. public function disable_builder_layout ( $post_types ) { 
  62. $post_types[] = $this->object_type; 
  63.  
  64. return $post_types; 
  65.  
  66. /** 
  67. * Update Post Type messages 
  68. * @param array $messages 
  69. * @return array 
  70. * @since 2.0.2 
  71. */ 
  72. public function setup_updated_messages ( $messages ) { 
  73. global $post, $post_ID; 
  74.  
  75. $post_type = get_post_type_object( $this->object_type ); 
  76.  
  77. $labels = $post_type->labels; 
  78.  
  79. $messages[ $post_type->name ] = array( 
  80. 1 => sprintf( __( '%s updated. <a href="%s">%s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),  
  81. 2 => __( 'Custom field updated.', 'pods' ),  
  82. 3 => __( 'Custom field deleted.', 'pods' ),  
  83. 4 => sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ),  
  84. /** translators: %s: date and time of the revision */ 
  85. 5 => isset( $_GET[ 'revision' ] ) ? sprintf( __( '%s restored to revision from %s', 'pods' ), $labels->singular_name, wp_post_revision_title( (int) $_GET[ 'revision' ], false ) ) : false,  
  86. 6 => sprintf( __( '%s published. <a href="%s">%s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),  
  87. 7 => sprintf( __( '%s saved.', 'pods' ), $labels->singular_name ),  
  88. 8 => sprintf( __( '%s submitted. <a target="_blank" href="%s">Preview %s</a>', 'pods' ),  
  89. $labels->singular_name,  
  90. esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ),  
  91. $labels->singular_name 
  92. ),  
  93. 9 => sprintf( __( '%s scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview %s</a>', 'pods' ),  
  94. $labels->singular_name,  
  95. // translators: Publish box date format, see http://php.net/date 
  96. date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ),  
  97. esc_url( get_permalink( $post_ID ) ),  
  98. $labels->singular_name 
  99. ),  
  100. 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 ) 
  101. ); 
  102.  
  103. if ( false === (boolean) $post_type->public ) { 
  104. $messages[ $post_type->name ][ 1 ] = sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ); 
  105. $messages[ $post_type->name ][ 6 ] = sprintf( __( '%s published.', 'pods' ), $labels->singular_name ); 
  106. $messages[ $post_type->name ][ 8 ] = sprintf( __( '%s submitted.', 'pods' ), $labels->singular_name ); 
  107. $messages[ $post_type->name ][ 9 ] = sprintf( __( '%s scheduled for: <strong>%1$s</strong>.', 'pods' ),  
  108. $labels->singular_name,  
  109. // translators: Publish box date format, see http://php.net/date 
  110. date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) 
  111. ); 
  112. $messages[ $post_type->name ][ 10 ] = sprintf( __( '%s draft updated.', 'pods' ), $labels->singular_name ); 
  113.  
  114. return $messages; 
  115.  
  116. /** 
  117. * Enqueue styles 
  118. * @since 2.0 
  119. */ 
  120. public function admin_assets () { 
  121. wp_enqueue_style( 'pods-admin' ); 
  122.  
  123. /** 
  124. * Fix filters, specifically removing balanceTags 
  125. * @since 2.0.1 
  126. */ 
  127. public function fix_filters( $data, $pod = null, $id = null, $groups = null, $post = null ) { 
  128. remove_filter( 'content_save_pre', 'balanceTags', 50 ); 
  129.  
  130. /** 
  131. * Remove unused row actions 
  132. * @since 2.0.5 
  133. */ 
  134. function remove_row_actions ( $actions, $post ) { 
  135. global $current_screen; 
  136.  
  137. if ( !is_object( $current_screen ) || $this->object_type != $current_screen->post_type ) 
  138. return $actions; 
  139.  
  140. if ( isset( $actions[ 'view' ] ) ) 
  141. unset( $actions[ 'view' ] ); 
  142.  
  143. if ( isset( $actions[ 'inline hide-if-no-js' ] ) ) 
  144. unset( $actions[ 'inline hide-if-no-js' ] ); 
  145.  
  146. // W3 Total Cache 
  147. if ( isset( $actions[ 'pgcache_purge' ] ) ) 
  148. unset( $actions[ 'pgcache_purge' ] ); 
  149.  
  150. return $actions; 
  151.  
  152. /** 
  153. * Remove unused bulk actions 
  154. * @since 2.0.5 
  155. */ 
  156. public function remove_bulk_actions ( $actions ) { 
  157. if ( isset( $actions[ 'edit' ] ) ) 
  158. unset( $actions[ 'edit' ] ); 
  159.  
  160. return $actions; 
  161.  
  162. /** 
  163. * Clear cache on save 
  164. * @since 2.0 
  165. */ 
  166. public function clear_cache ( $data, $pod = null, $id = null, $groups = null, $post = null ) { 
  167. $old_post = $id; 
  168.  
  169. if ( !is_object( $id ) ) 
  170. $old_post = null; 
  171.  
  172. if ( is_object( $post ) && $this->object_type != $post->post_type ) 
  173. return; 
  174.  
  175. if ( !is_array( $data ) && 0 < $data ) { 
  176. $post = $data; 
  177. $post = get_post( $post ); 
  178.  
  179. if ( $this->object_type == $post->object_type ) 
  180. pods_transient_clear( 'pods_object_helpers' ); 
  181.  
  182. /** 
  183. * Change post title placeholder text 
  184. * @since 2.0 
  185. */ 
  186. public function set_title_text ( $text, $post ) { 
  187. return __( 'Enter helper name here', 'pods' ); 
  188.  
  189. /** 
  190. * Edit page form 
  191. * @since 2.0 
  192. */ 
  193. public function edit_page_form () { 
  194. global $post_type; 
  195.  
  196. if ( $this->object_type != $post_type ) 
  197. return; 
  198.  
  199. add_filter( 'enter_title_here', array( $this, 'set_title_text' ), 10, 2 ); 
  200.  
  201. /** 
  202. * Add meta boxes to the page 
  203. * @since 2.0 
  204. */ 
  205. public function add_meta_boxes () { 
  206. $pod = array( 
  207. 'name' => $this->object_type,  
  208. 'type' => 'post_type' 
  209. ); 
  210.  
  211. if ( isset( PodsMeta::$post_types[ $pod[ 'name' ] ] ) ) 
  212. return; 
  213.  
  214. $fields = array( 
  215. array( 
  216. 'name' => 'helper_type',  
  217. 'label' => __( 'Helper Type', 'pods' ),  
  218. 'type' => 'pick',  
  219. 'default' => 'display',  
  220. 'data' => array( 
  221. 'input' => 'Input (change form fields)',  
  222. 'display' => 'Display (change field output when using magic tags)',  
  223. 'pre_save' => 'Pre-Save (change form fields before saving)',  
  224. 'post_save' => 'Post-Save',  
  225. 'pre_delete' => 'Pre-Delete',  
  226. 'post_delete' => 'Post-Delete',  
  227. ),  
  228. array( 
  229. 'name' => 'code',  
  230. 'label' => __( 'Code', 'pods' ),  
  231. 'type' => 'code' 
  232. ); 
  233.  
  234. pods_group_add( $pod, __( 'Helper', 'pods' ), $fields, 'normal', 'high' ); 
  235.  
  236. /** 
  237. * Get the fields 
  238. * @param null $_null 
  239. * @param null $post_ID 
  240. * @param null $meta_key 
  241. * @param bool $single 
  242. * @return array|bool|int|mixed|null|string|void 
  243. */ 
  244. public function get_meta( $_null, $post_ID = null, $meta_key = null, $single = false ) { 
  245. if ( 'code' == $meta_key ) { 
  246. $post = get_post( $post_ID ); 
  247.  
  248. if ( is_object( $post ) && $this->object_type == $post->post_type ) 
  249. return $post->post_content; 
  250.  
  251. return $_null; 
  252.  
  253. /** 
  254. * Save the fields 
  255. * @param $_null 
  256. * @param int $post_ID 
  257. * @param string $meta_key 
  258. * @param string $meta_value 
  259. * @return bool|int|null 
  260. */ 
  261. public function save_meta ( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) { 
  262. if ( 'code' == $meta_key ) { 
  263. $post = get_post( $post_ID ); 
  264.  
  265. if ( is_object( $post ) && $this->object_type == $post->post_type ) { 
  266. $postdata = array( 
  267. 'ID' => $post_ID,  
  268. 'post_content' => $meta_value 
  269. ); 
  270.  
  271. remove_filter( current_filter(), array( $this, __FUNCTION__ ), 10 ); 
  272.  
  273. $revisions = false; 
  274.  
  275. if ( has_action( 'pre_post_update', 'wp_save_post_revision' ) ) { 
  276. remove_action( 'pre_post_update', 'wp_save_post_revision' ); 
  277.  
  278. $revisions = true; 
  279.  
  280. wp_update_post( (object) $postdata ); // objects will be automatically sanitized 
  281.  
  282. if ( $revisions ) 
  283. add_action( 'pre_post_update', 'wp_save_post_revision' ); 
  284.  
  285. return true; 
  286.  
  287. return $_null; 
  288.  
  289. /** 
  290. * @static 
  291. * Run a helper within a Pod Page or WP Template 
  292. * $params['helper'] string Helper name 
  293. * $params['value'] string Value to run Helper on 
  294. * $params['name'] string Field name 
  295. * @param array $params An associative array of parameters 
  296. * @param null $obj 
  297. * @return mixed Anything returned by the helper 
  298. * @since 2.0 
  299. */ 
  300. public static function helper ( $params, $obj = null ) { 
  301. /** 
  302. * @var $obj Pods 
  303. */ 
  304. if ( !empty( $obj ) ) 
  305. self::$obj =& $obj; 
  306. else 
  307. $obj =& self::$obj; 
  308.  
  309. if ( empty( $obj ) || !is_object( $obj ) ) 
  310. return ''; 
  311.  
  312. $defaults = array( 
  313. 'helper' => '',  
  314. 'value' => '',  
  315. 'name' => '',  
  316. 'deprecated' => false 
  317. ); 
  318.  
  319. if ( is_array( $params ) ) 
  320. $params = array_merge( $defaults, $params ); 
  321. else 
  322. $params = $defaults; 
  323.  
  324. $params = (object) $params; 
  325.  
  326. if ( empty( $params->helper ) ) 
  327. return pods_error( 'Helper name required', $obj ); 
  328. elseif ( !is_array( $params->helper ) ) 
  329. $params->helper = trim( $params->helper ); 
  330.  
  331. if ( !isset( $params->value ) ) 
  332. $params->value = null; 
  333.  
  334. if ( true === $params->deprecated && is_array( $params->value ) && !empty( $params->value ) && !isset( $params->value[ 0 ] ) ) 
  335. $params->value = array( $params->value ); 
  336.  
  337. if ( !isset( $params->name ) ) 
  338. $params->name = null; 
  339.  
  340. $helper = $obj->api->load_helper( array( 'name' => $params->helper ) ); 
  341.  
  342. ob_start(); 
  343.  
  344. if ( !empty( $helper ) && !empty( $helper[ 'code' ] ) ) { 
  345. $code = $helper[ 'code' ]; 
  346.  
  347. $code = str_replace( '$this->', '$obj->', $code ); 
  348.  
  349. $value =& $params->value; 
  350. $name =& $params->name; 
  351.  
  352. $_safe_params = $params; 
  353.  
  354. if ( !defined( 'PODS_DISABLE_EVAL' ) || !PODS_DISABLE_EVAL ) 
  355. eval( "?>{$code}" ); 
  356. else 
  357. echo $code; 
  358.  
  359. $params = $_safe_params; 
  360. elseif ( is_callable( (string) $params->helper ) ) 
  361. echo call_user_func( (string) $params->helper, $params->value, $params->name, $params, $obj ); 
  362.  
  363. $out = ob_get_clean(); 
  364.  
  365. $out = apply_filters( 'pods_helpers_post_helper', $out, $params, $helper ); 
  366. $out = apply_filters( 'pods_helpers_post_helper_' . $helper[ 'slug' ], $out, $params, $helper ); 
  367.  
  368. return $out;