PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/components/Helpers.php

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