acf_controller_post

Acf_controller_post.

Defined (1)

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

/core/controllers/post.php  
  1. class acf_controller_post 
  2.  
  3. /** 
  4. * Constructor 
  5. * This function will construct all the neccessary actions and filters 
  6. * @type function 
  7. * @date 23/06/12 
  8. * @since 3.1.8 
  9. * @param N/A 
  10. * @return N/A 
  11. */ 
  12.  
  13. function __construct() 
  14. // actions 
  15. add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); 
  16. add_action('save_post', array($this, 'save_post'), 10, 1); 
  17.  
  18.  
  19. // ajax 
  20. add_action('wp_ajax_acf/post/render_fields', array($this, 'ajax_render_fields')); 
  21. add_action('wp_ajax_acf/post/get_style', array($this, 'ajax_get_style')); 
  22.  
  23.  
  24. /** 
  25. * validate_page 
  26. * This function will check if the current page is for a post/page edit form 
  27. * @type function 
  28. * @date 23/06/12 
  29. * @since 3.1.8 
  30. * @param N/A 
  31. * @return (boolean) 
  32. */ 
  33.  
  34. function validate_page() 
  35. // global 
  36. global $pagenow, $typenow; 
  37.  
  38.  
  39. // vars 
  40. $return = false; 
  41.  
  42.  
  43. // validate page 
  44. if( in_array( $pagenow, array('post.php', 'post-new.php') ) ) 
  45.  
  46. // validate post type 
  47. global $typenow; 
  48.  
  49. if( $typenow != "acf" ) 
  50. $return = true; 
  51.  
  52.  
  53.  
  54. // validate page (Shopp) 
  55. if( $pagenow == "admin.php" && isset( $_GET['page'] ) && $_GET['page'] == "shopp-products" && isset( $_GET['id'] ) ) 
  56. $return = true; 
  57.  
  58.  
  59. // return 
  60. return $return; 
  61.  
  62.  
  63. /** 
  64. * admin_enqueue_scripts 
  65. * This action is run after post query but before any admin script / head actions.  
  66. * It is a good place to register all actions. 
  67. * @type action (admin_enqueue_scripts) 
  68. * @date 26/01/13 
  69. * @since 3.6.0 
  70. * @param N/A 
  71. * @return N/A 
  72. */ 
  73.  
  74. function admin_enqueue_scripts() 
  75. // validate page 
  76. if( ! $this->validate_page() ) 
  77. return; 
  78.  
  79.  
  80. // actions 
  81. do_action('acf/input/admin_enqueue_scripts'); 
  82.  
  83. add_action('admin_head', array($this, 'admin_head')); 
  84.  
  85.  
  86. /** 
  87. * admin_head 
  88. * This action will find and add field groups to the current edit page 
  89. * @type action (admin_head) 
  90. * @date 23/06/12 
  91. * @since 3.1.8 
  92. * @param N/A 
  93. * @return N/A 
  94. */ 
  95.  
  96. function admin_head() 
  97. // globals 
  98. global $post, $pagenow, $typenow; 
  99.  
  100.  
  101. // shopp 
  102. if( $pagenow == "admin.php" && isset( $_GET['page'] ) && $_GET['page'] == "shopp-products" && isset( $_GET['id'] ) ) 
  103. $typenow = "shopp_product"; 
  104.  
  105.  
  106. // vars 
  107. $post_id = $post ? $post->ID : 0; 
  108.  
  109.  
  110. // get field groups 
  111. $filter = array(  
  112. 'post_id' => $post_id,  
  113. 'post_type' => $typenow  
  114. ); 
  115. $metabox_ids = array(); 
  116. $metabox_ids = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter ); 
  117.  
  118.  
  119. // get style of first field group 
  120. $style = ''; 
  121. if( isset($metabox_ids[0]) ) 
  122. $style = $this->get_style( $metabox_ids[0] ); 
  123.  
  124.  
  125. // Style 
  126. echo '<style type="text/css" id="acf_style" >' . $style . '</style>'; 
  127.  
  128.  
  129. // add user js + css 
  130. do_action('acf/input/admin_head'); 
  131.  
  132.  
  133. // get field groups 
  134. $acfs = apply_filters('acf/get_field_groups', array()); 
  135.  
  136.  
  137. if( $acfs ) 
  138. foreach( $acfs as $acf ) 
  139. // load options 
  140. $acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']); 
  141.  
  142.  
  143. // vars 
  144. $show = in_array( $acf['id'], $metabox_ids ) ? 1 : 0; 
  145.  
  146.  
  147. // priority 
  148. $priority = 'high'; 
  149. if( $acf['options']['position'] == 'side' ) 
  150. $priority = 'core'; 
  151. $priority = apply_filters('acf/input/meta_box_priority', $priority, $acf); 
  152.  
  153.  
  154. // add meta box 
  155. add_meta_box( 
  156. 'acf_' . $acf['id'],  
  157. $acf['title'],  
  158. array($this, 'meta_box_input'),  
  159. $typenow,  
  160. $acf['options']['position'],  
  161. $priority,  
  162. array( 'field_group' => $acf, 'show' => $show, 'post_id' => $post_id ) 
  163. ); 
  164.  
  165. // foreach($acfs as $acf) 
  166. // if($acfs) 
  167.  
  168.  
  169. // Allow 'acf_after_title' metabox position 
  170. add_action('edit_form_after_title', array($this, 'edit_form_after_title')); 
  171.  
  172.  
  173. // remove ACF from meta postbox 
  174. add_filter( 'is_protected_meta', array($this, 'is_protected_meta'), 10, 3 ); 
  175.  
  176.  
  177. /** 
  178. * edit_form_after_title 
  179. * This action will allow ACF to render metaboxes after the title 
  180. * @type action 
  181. * @date 17/08/13 
  182. * @param N/A 
  183. * @return N/A 
  184. */ 
  185.  
  186. function edit_form_after_title() 
  187. // globals 
  188. global $post, $wp_meta_boxes; 
  189.  
  190.  
  191. // render 
  192. do_meta_boxes( get_current_screen(), 'acf_after_title', $post); 
  193.  
  194.  
  195. // clean up 
  196. unset( $wp_meta_boxes['post']['acf_after_title'] ); 
  197.  
  198.  
  199. // preview hack 
  200. // the following code will add a hidden input which will trigger WP to create a revision apon save 
  201. // http://support.advancedcustomfields.com/forums/topic/preview-solution/#post-4106 
  202. ?> 
  203. <div style="display:none"> 
  204. <input type="hidden" name="acf_has_changed" id="acf-has-changed" value="0" /> 
  205. </div> 
  206. <?php 
  207.  
  208.  
  209. /** 
  210. * meta_box_input 
  211. * @description:  
  212. * @since 1.0.0 
  213. * @created: 23/06/12 
  214. */ 
  215.  
  216. function meta_box_input( $post, $args ) 
  217. // extract $args 
  218. extract( $args ); 
  219.  
  220.  
  221. // classes 
  222. $class = 'acf_postbox ' . $args['field_group']['options']['layout']; 
  223. $toggle_class = 'acf_postbox-toggle'; 
  224.  
  225.  
  226. if( ! $args['show'] ) 
  227. $class .= ' acf-hidden'; 
  228. $toggle_class .= ' acf-hidden'; 
  229.  
  230.  
  231. // HTML 
  232. if( $args['show'] ) 
  233. $fields = apply_filters('acf/field_group/get_fields', array(), $args['field_group']['id']); 
  234.  
  235. do_action('acf/create_fields', $fields, $args['post_id']); 
  236. else 
  237. echo '<div class="acf-replace-with-fields"><div class="acf-loading"></div></div>'; 
  238.  
  239.  
  240. // nonce 
  241. echo '<div style="display:none">'; 
  242. echo '<input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" />'; 
  243. ?> 
  244. <script type="text/javascript"> 
  245. (function($) { 
  246.  
  247. $('#<?php echo $id; ?>').addClass('<?php echo $class; ?>').removeClass('hide-if-js'); 
  248. $('#adv-settings label[for="<?php echo $id; ?>-hide"]').addClass('<?php echo $toggle_class; ?>'); 
  249.  
  250. })(jQuery);  
  251. </script> 
  252. <?php 
  253. echo '</div>'; 
  254.  
  255.  
  256. /** 
  257. * get_style 
  258. * @description: called by admin_head to generate acf css style (hide other metaboxes) 
  259. * @since 2.0.5 
  260. * @created: 23/06/12 
  261. */ 
  262.  
  263. function get_style( $acf_id ) 
  264. // vars 
  265. $options = apply_filters('acf/field_group/get_options', array(), $acf_id); 
  266. $html = ''; 
  267.  
  268.  
  269. // add style to html  
  270. if( in_array('permalink', $options['hide_on_screen']) ) 
  271. $html .= '#edit-slug-box {display: none;} '; 
  272. if( in_array('the_content', $options['hide_on_screen']) ) 
  273. $html .= '#postdivrich {display: none;} '; 
  274. if( in_array('excerpt', $options['hide_on_screen']) ) 
  275. $html .= '#postexcerpt, #screen-meta label[for=postexcerpt-hide] {display: none;} '; 
  276. if( in_array('custom_fields', $options['hide_on_screen']) ) 
  277. $html .= '#postcustom, #screen-meta label[for=postcustom-hide] { display: none; } '; 
  278. if( in_array('discussion', $options['hide_on_screen']) ) 
  279. $html .= '#commentstatusdiv, #screen-meta label[for=commentstatusdiv-hide] {display: none;} '; 
  280. if( in_array('comments', $options['hide_on_screen']) ) 
  281. $html .= '#commentsdiv, #screen-meta label[for=commentsdiv-hide] {display: none;} '; 
  282. if( in_array('slug', $options['hide_on_screen']) ) 
  283. $html .= '#slugdiv, #screen-meta label[for=slugdiv-hide] {display: none;} '; 
  284. if( in_array('author', $options['hide_on_screen']) ) 
  285. $html .= '#authordiv, #screen-meta label[for=authordiv-hide] {display: none;} '; 
  286. if( in_array('format', $options['hide_on_screen']) ) 
  287. $html .= '#formatdiv, #screen-meta label[for=formatdiv-hide] {display: none;} '; 
  288. if( in_array('featured_image', $options['hide_on_screen']) ) 
  289. $html .= '#postimagediv, #screen-meta label[for=postimagediv-hide] {display: none;} '; 
  290. if( in_array('revisions', $options['hide_on_screen']) ) 
  291. $html .= '#revisionsdiv, #screen-meta label[for=revisionsdiv-hide] {display: none;} '; 
  292. if( in_array('categories', $options['hide_on_screen']) ) 
  293. $html .= '#categorydiv, #screen-meta label[for=categorydiv-hide] {display: none;} '; 
  294. if( in_array('tags', $options['hide_on_screen']) ) 
  295. $html .= '#tagsdiv-post_tag, #screen-meta label[for=tagsdiv-post_tag-hide] {display: none;} '; 
  296. if( in_array('send-trackbacks', $options['hide_on_screen']) ) 
  297. $html .= '#trackbacksdiv, #screen-meta label[for=trackbacksdiv-hide] {display: none;} '; 
  298.  
  299.  
  300. return $html; 
  301.  
  302.  
  303. /** 
  304. * ajax_get_input_style 
  305. * @description: called by input-actions.js to hide / show other metaboxes 
  306. * @since 2.0.5 
  307. * @created: 23/06/12 
  308. */ 
  309.  
  310. function ajax_get_style() 
  311. // vars 
  312. $options = array( 
  313. 'acf_id' => 0,  
  314. 'nonce' => '' 
  315. ); 
  316.  
  317. // load post options 
  318. $options = array_merge($options, $_POST); 
  319.  
  320.  
  321. // verify nonce 
  322. if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') ) 
  323. die(0); 
  324.  
  325.  
  326. // return style 
  327. echo $this->get_style( $options['acf_id'] ); 
  328.  
  329.  
  330. // die 
  331. die; 
  332.  
  333.  
  334. /** 
  335. * ajax_render_fields 
  336. * @description:  
  337. * @since 3.1.6 
  338. * @created: 23/06/12 
  339. */ 
  340.  
  341. function ajax_render_fields() 
  342.  
  343. // defaults 
  344. $options = array( 
  345. 'acf_id' => 0,  
  346. 'post_id' => 0,  
  347. 'nonce' => '' 
  348. ); 
  349.  
  350.  
  351. // load post options 
  352. $options = array_merge($options, $_POST); 
  353.  
  354.  
  355. // verify nonce 
  356. if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') ) 
  357. die(0); 
  358.  
  359.  
  360. // get acfs 
  361. $acfs = apply_filters('acf/get_field_groups', array()); 
  362. if( $acfs ) 
  363. foreach( $acfs as $acf ) 
  364. if( $acf['id'] == $options['acf_id'] ) 
  365. $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']); 
  366.  
  367. do_action('acf/create_fields', $fields, $options['post_id']); 
  368.  
  369. break; 
  370.  
  371. die(); 
  372.  
  373.  
  374.  
  375. /** 
  376. * save_post 
  377. * @description: Saves the field / location / option data for a field group 
  378. * @since 1.0.0 
  379. * @created: 23/06/12 
  380. */ 
  381.  
  382. function save_post( $post_id ) 
  383. {  
  384.  
  385. // do not save if this is an auto save routine 
  386. if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
  387. return $post_id; 
  388.  
  389.  
  390. // verify nonce 
  391. if( !isset($_POST['acf_nonce'], $_POST['fields']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) 
  392. return $post_id; 
  393.  
  394.  
  395. // if save lock contains a value, the save_post action is already running for another post. 
  396. // this would imply that the user is hooking into an ACF update_value or save_post action and inserting a new post 
  397. // if this is the case, we do not want to save all the $POST data to this post. 
  398. if( isset($GLOBALS['acf_save_lock']) && $GLOBALS['acf_save_lock'] ) 
  399. return $post_id; 
  400.  
  401.  
  402. // update the post (may even be a revision / autosave preview) 
  403. do_action('acf/save_post', $post_id); 
  404.  
  405.  
  406.  
  407. /** 
  408. * is_protected_meta 
  409. * This function will remove any ACF meta from showing in the meta postbox 
  410. * @type function 
  411. * @date 12/04/2014 
  412. * @since 5.0.0 
  413. * @param $post_id (int) 
  414. * @return $post_id (int) 
  415. */ 
  416.  
  417. function is_protected_meta( $protected, $meta_key, $meta_type ) { 
  418.  
  419. // globals 
  420. global $post; 
  421.  
  422.  
  423. // if acf_get_field_reference returns a valid key, this is an acf value, so protect it! 
  424. if( !$protected ) { 
  425.  
  426. $reference = get_field_reference( $meta_key, $post->ID ); 
  427.  
  428. if( substr($reference, 0, 6) === 'field_' ) { 
  429.  
  430. $protected = true; 
  431.  
  432. }  
  433.  
  434.  
  435.  
  436. // return 
  437. return $protected; 
  438.  
  439.  
  440.