acf_everything_fields

The Advanced Custom Fields acf everything fields class.

Defined (1)

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

/core/controllers/everything_fields.php  
  1. class acf_everything_fields  
  2.  
  3. var $settings,  
  4. $data; 
  5.  
  6.  
  7. /** 
  8. * __construct 
  9. * @description:  
  10. * @since 3.1.8 
  11. * @created: 23/06/12 
  12. */ 
  13.  
  14. function __construct() 
  15. // data for passing variables 
  16. $this->data = array( 
  17. 'page_id' => '', // a string used to load values 
  18. 'metabox_ids' => array(),  
  19. 'page_type' => '', // taxonomy / user / media 
  20. 'page_action' => '', // add / edit 
  21. 'option_name' => '', // key used to find value in wp_options table. eg: user_1, category_4 
  22. ); 
  23.  
  24.  
  25. // actions 
  26. add_action('admin_menu', array($this, 'admin_menu')); 
  27. add_action('wp_ajax_acf/everything_fields', array($this, 'acf_everything_fields')); 
  28.  
  29.  
  30. // attachment 
  31. add_filter('attachment_fields_to_edit', array($this, 'attachment_fields_to_edit'), 10, 2); 
  32. add_filter('attachment_fields_to_save', array($this, 'save_attachment'), 10, 2); 
  33.  
  34.  
  35. // save 
  36. add_action('create_term', array($this, 'save_taxonomy')); 
  37. add_action('edited_term', array($this, 'save_taxonomy')); 
  38. add_action('edit_user_profile_update', array($this, 'save_user')); 
  39. add_action('personal_options_update', array($this, 'save_user')); 
  40. add_action('user_register', array($this, 'save_user')); 
  41.  
  42.  
  43. // shopp 
  44. add_action('shopp_category_saved', array($this, 'shopp_category_saved')); 
  45.  
  46.  
  47. // delete 
  48. add_action('delete_term', array($this, 'delete_term'), 10, 4); 
  49.  
  50.  
  51. /** 
  52. * attachment_fields_to_edit 
  53. * Adds ACF fields to the attachment form fields 
  54. * @type filter 
  55. * @date 14/07/13 
  56. * @param {array} $form_fields 
  57. * @return {object} $post 
  58. */ 
  59.  
  60. function attachment_fields_to_edit( $form_fields, $post )  
  61. // vars 
  62. $screen = get_current_screen(); 
  63. $post_id = $post->ID; 
  64.  
  65.  
  66. if( $screen && $screen->base == 'post' ) { 
  67.  
  68. return $form_fields; 
  69.  
  70.  
  71.  
  72. // get field groups 
  73. $filter = array( 'post_type' => 'attachment' ); 
  74. $metabox_ids = array(); 
  75. $metabox_ids = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter ); 
  76.  
  77.  
  78. // validate 
  79. if( empty($metabox_ids) ) 
  80. return $form_fields;  
  81.  
  82.  
  83. $acfs = apply_filters('acf/get_field_groups', array()); 
  84.  
  85.  
  86. if( is_array($acfs) ) { foreach( $acfs as $acf ) { 
  87.  
  88. // only add the chosen field groups 
  89. if( !in_array( $acf['id'], $metabox_ids ) ) 
  90. continue; 
  91.  
  92.  
  93. // load fields 
  94. $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']); 
  95.  
  96.  
  97. if( is_array($fields) ) { foreach( $fields as $i => $field ) { 
  98.  
  99. // if they didn't select a type, skip this field 
  100. if( !$field || !$field['type'] || $field['type'] == 'null' ) 
  101. continue; 
  102.  
  103.  
  104. // set value 
  105. if( !isset($field['value']) ) 
  106. $field['value'] = apply_filters('acf/load_value', false, $post_id, $field); 
  107. $field['value'] = apply_filters('acf/format_value', $field['value'], $post_id, $field); 
  108.  
  109.  
  110. // create field 
  111. $field['name'] = 'fields[' . $field['key'] . ']'; 
  112.  
  113. ob_start(); 
  114.  
  115. do_action('acf/create_field', $field); 
  116.  
  117. $html = ob_get_contents(); 
  118.  
  119. ob_end_clean(); 
  120.  
  121.  
  122. $form_fields[ $field['name'] ] = array( 
  123. 'label' => $field['label'],  
  124. 'input' => 'html',  
  125. 'html' => $html 
  126. ); 
  127.  
  128. }}; 
  129.  
  130.  
  131. }} 
  132.  
  133.  
  134. // return 
  135. return $form_fields; 
  136.  
  137.  
  138. /** 
  139. * save_attachment 
  140. * Triggers the acf/save_post action 
  141. * @type action 
  142. * @date 14/07/13 
  143. * @param {array} $post 
  144. * @return {array} $attachment 
  145. */ 
  146.  
  147. function save_attachment( $post, $attachment ) 
  148. // verify nonce 
  149. /** 
  150. if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) 
  151. return $post; 
  152. */ 
  153.  
  154.  
  155. // $post_id to save against 
  156. $post_id = $post['ID']; 
  157.  
  158.  
  159. // update the post 
  160. do_action('acf/save_post', $post_id); 
  161.  
  162.  
  163. return $post; 
  164.  
  165.  
  166. /** 
  167. * validate_page 
  168. * @description: returns true | false. Used to stop a function from continuing 
  169. * @since 3.2.6 
  170. * @created: 23/06/12 
  171. */ 
  172.  
  173. function validate_page() 
  174. // global 
  175. global $pagenow, $wp_version; 
  176.  
  177.  
  178. // vars 
  179. $return = false; 
  180.  
  181.  
  182. // validate page 
  183. if( in_array( $pagenow, array( 'edit-tags.php', 'profile.php', 'user-new.php', 'user-edit.php', 'media.php' ) ) ) 
  184. $return = true; 
  185.  
  186.  
  187. // validate page (Shopp) 
  188. if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" ) 
  189. $return = true; 
  190.  
  191.  
  192. // WP4 
  193. if( $pagenow === 'upload.php' && version_compare($wp_version, '4.0', '>=') ) { 
  194.  
  195. $return = true; 
  196.  
  197.  
  198.  
  199. // return 
  200. return $return; 
  201.  
  202.  
  203. /**-------------------------------------------------------------------------------------- 
  204. * admin_menu 
  205. * @author Elliot Condon 
  206. * @since 3.1.8 
  207. *  
  208. *-------------------------------------------------------------------------------------*/ 
  209.  
  210. function admin_menu()  
  211.  
  212. global $pagenow; 
  213.  
  214.  
  215. // validate page 
  216. if( ! $this->validate_page() ) return; 
  217.  
  218.  
  219. // set page type 
  220. $filter = array(); 
  221.  
  222. if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" ) 
  223. // filter 
  224. $_GET['id'] = filter_var($_GET['id'], FILTER_SANITIZE_STRING); 
  225.  
  226.  
  227. $this->data['page_type'] = "shopp_category"; 
  228. $filter['ef_taxonomy'] = "shopp_category"; 
  229.  
  230. $this->data['page_action'] = "add"; 
  231. $this->data['option_name'] = ""; 
  232.  
  233. if( $_GET['id'] != "new" ) 
  234. $this->data['page_action'] = "edit"; 
  235. $this->data['option_name'] = "shopp_category_" . $_GET['id']; 
  236.  
  237. if( $pagenow == "edit-tags.php" && isset($_GET['taxonomy']) ) 
  238. // filter 
  239. $_GET['taxonomy'] = filter_var($_GET['taxonomy'], FILTER_SANITIZE_STRING); 
  240.  
  241.  
  242. $this->data['page_type'] = "taxonomy"; 
  243. $filter['ef_taxonomy'] = $_GET['taxonomy']; 
  244.  
  245. $this->data['page_action'] = "add"; 
  246. $this->data['option_name'] = ""; 
  247.  
  248. if( isset($_GET['action']) && $_GET['action'] == "edit" ) 
  249. // filter 
  250. $_GET['tag_ID'] = filter_var($_GET['tag_ID'], FILTER_SANITIZE_NUMBER_INT); 
  251.  
  252. $this->data['page_action'] = "edit"; 
  253. $this->data['option_name'] = $_GET['taxonomy'] . "_" . $_GET['tag_ID']; 
  254.  
  255. elseif( $pagenow == "profile.php" ) 
  256.  
  257. $this->data['page_type'] = "user"; 
  258. $filter['ef_user'] = get_current_user_id(); 
  259.  
  260. $this->data['page_action'] = "edit"; 
  261. $this->data['option_name'] = "user_" . get_current_user_id(); 
  262.  
  263. elseif( $pagenow == "user-edit.php" && isset($_GET['user_id']) ) 
  264. // filter 
  265. $_GET['user_id'] = filter_var($_GET['user_id'], FILTER_SANITIZE_NUMBER_INT); 
  266.  
  267.  
  268. $this->data['page_type'] = "user"; 
  269. $filter['ef_user'] = $_GET['user_id']; 
  270.  
  271. $this->data['page_action'] = "edit"; 
  272. $this->data['option_name'] = "user_" . $_GET['user_id']; 
  273.  
  274. elseif( $pagenow == "user-new.php" ) 
  275. $this->data['page_type'] = "user"; 
  276. $filter['ef_user'] ='all'; 
  277.  
  278. $this->data['page_action'] = "add"; 
  279. $this->data['option_name'] = ""; 
  280.  
  281. elseif( $pagenow == "media.php" || $pagenow == 'upload.php' ) 
  282.  
  283. $this->data['page_type'] = "media"; 
  284. $filter['post_type'] = 'attachment'; 
  285.  
  286. $this->data['page_action'] = "add"; 
  287. $this->data['option_name'] = ""; 
  288.  
  289. if(isset($_GET['attachment_id'])) 
  290. // filter 
  291. $_GET['attachment_id'] = filter_var($_GET['attachment_id'], FILTER_SANITIZE_NUMBER_INT); 
  292.  
  293.  
  294. $this->data['page_action'] = "edit"; 
  295. $this->data['option_name'] = $_GET['attachment_id']; 
  296.  
  297.  
  298.  
  299. // get field groups 
  300. $metabox_ids = array(); 
  301. $this->data['metabox_ids'] = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter ); 
  302.  
  303.  
  304. // dont continue if no ids were found 
  305. if( empty( $this->data['metabox_ids'] ) ) 
  306. return false;  
  307.  
  308.  
  309. // actions 
  310. add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); 
  311. add_action('admin_head', array($this, 'admin_head')); 
  312.  
  313.  
  314.  
  315.  
  316. /** 
  317. * admin_enqueue_scripts 
  318. * @description:  
  319. * @since: 3.6 
  320. * @created: 30/01/13 
  321. */ 
  322.  
  323. function admin_enqueue_scripts() 
  324. do_action('acf/input/admin_enqueue_scripts'); 
  325.  
  326.  
  327. /**-------------------------------------------------------------------------------------- 
  328. * admin_head 
  329. * @author Elliot Condon 
  330. * @since 3.1.8 
  331. *  
  332. *-------------------------------------------------------------------------------------*/ 
  333.  
  334. function admin_head() 
  335. {  
  336. global $pagenow; 
  337.  
  338.  
  339. // add user js + css 
  340. do_action('acf/input/admin_head'); 
  341.  
  342.  
  343. ?> 
  344. <script type="text/javascript"> 
  345. (function($) { 
  346.  
  347. acf.data = { 
  348. action : 'acf/everything_fields',  
  349. metabox_ids : '<?php echo implode( ', ', $this->data['metabox_ids'] ); ?>',  
  350. page_type : '<?php echo $this->data['page_type']; ?>',  
  351. page_action : '<?php echo $this->data['page_action']; ?>',  
  352. option_name : '<?php echo $this->data['option_name']; ?>' 
  353. }; 
  354.  
  355. $(document).ready(function() { 
  356.  
  357. $.ajax({ 
  358. url: ajaxurl,  
  359. data: acf.data,  
  360. type: 'post',  
  361. dataType: 'html',  
  362. success: function(html) { 
  363.  
  364. <?php  
  365. if($this->data['page_type'] == "user") 
  366. if($this->data['page_action'] == "add") 
  367. echo "$('#createuser > table.form-table:last > tbody').append( html );"; 
  368. else 
  369. echo "$('#your-profile .form-table:last').after( html );"; 
  370. elseif($this->data['page_type'] == "shopp_category") 
  371. echo "$('#post-body-content').append( html );"; 
  372. elseif($this->data['page_type'] == "taxonomy") 
  373. if($this->data['page_action'] == "add") 
  374. echo "$('#addtag > p.submit').before( html );"; 
  375. else 
  376. echo "$('#edittag > table.form-table:first > tbody').append( html );"; 
  377. elseif($this->data['page_type'] == "media") 
  378. if($this->data['page_action'] == "add") 
  379. echo "$('#addtag > p.submit').before( html );"; 
  380. else 
  381. echo "$('#media-single-form table tbody tr.submit').before( html );"; 
  382. ?> 
  383.  
  384. setTimeout( function() {  
  385. $(document).trigger('acf/setup_fields', $('#wpbody') );  
  386. }, 200); 
  387.  
  388. }); 
  389.  
  390.  
  391. /* 
  392. * Taxonomy Add 
  393. * @description:  
  394. * @since: 3.6 
  395. * @created: 24/02/13 
  396. */ 
  397.  
  398. $(document).ajaxComplete(function(event, xhr, settings) { 
  399.  
  400. // vars 
  401. data = acf.helpers.url_to_object(settings.data); 
  402.  
  403.  
  404. // validate 
  405. if( data.action != 'add-tag' ) 
  406. return; 
  407.  
  408.  
  409. // vars 
  410. var $el = $('#addtag'); 
  411.  
  412.  
  413. // clear WYSIWYG field 
  414. $el.find('.acf_wysiwyg textarea').each(function() { 
  415.  
  416.  
  417. // vars 
  418. var textarea = $(this),  
  419. id = textarea.attr('id'),  
  420. editor = tinyMCE.get( id ); 
  421.  
  422. if( editor ) 
  423. editor.setContent(''); 
  424. editor.save(); 
  425.  
  426. }); 
  427.  
  428.  
  429. // clear image / file fields 
  430. $el.find('.field .active').removeClass('active'); 
  431.  
  432.  
  433. // clear checkbox 
  434. $el.find('input[type="checkbox"]').removeAttr('checked'); 
  435.  
  436. }); 
  437.  
  438. }); 
  439.  
  440.  
  441. })(jQuery); 
  442. </script> 
  443. <?php 
  444.  
  445.  
  446.  
  447. /**-------------------------------------------------------------------------------------- 
  448. * save_taxonomy 
  449. * @author Elliot Condon 
  450. * @since 3.1.8 
  451. *  
  452. *-------------------------------------------------------------------------------------*/ 
  453.  
  454. function save_taxonomy( $term_id ) 
  455. // verify nonce 
  456. if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) 
  457. return $term_id; 
  458.  
  459.  
  460. // for some weird reason, this is triggered by saving a menu...  
  461. if( !isset($_POST['taxonomy']) ) 
  462. return; 
  463.  
  464. // $post_id to save against 
  465. $post_id = $_POST['taxonomy'] . '_' . $term_id; 
  466.  
  467.  
  468. // update the post 
  469. do_action('acf/save_post', $post_id); 
  470.  
  471.  
  472.  
  473. /**-------------------------------------------------------------------------------------- 
  474. * profile_save 
  475. * @author Elliot Condon 
  476. * @since 3.1.8 
  477. *  
  478. *-------------------------------------------------------------------------------------*/ 
  479.  
  480. function save_user( $user_id ) 
  481. // verify nonce 
  482. if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) 
  483. return $user_id; 
  484.  
  485.  
  486. // $post_id to save against 
  487. $post_id = 'user_' . $user_id; 
  488.  
  489.  
  490. // update the post 
  491. do_action('acf/save_post', $post_id); 
  492.  
  493.  
  494.  
  495. /** 
  496. * shopp_category_saved 
  497. * @description:  
  498. * @since 3.5.2 
  499. * @created: 27/11/12 
  500. */ 
  501.  
  502. function shopp_category_saved( $category ) 
  503. // verify nonce 
  504. if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) 
  505. return $category; 
  506.  
  507.  
  508. // $post_id to save against 
  509. $post_id = 'shopp_category_' . $category->id; 
  510.  
  511.  
  512. // update the post 
  513. do_action('acf/save_post', $post_id); 
  514.  
  515.  
  516.  
  517.  
  518. /**-------------------------------------------------------------------------------------- 
  519. * acf_everything_fields 
  520. * @description Ajax call that renders the html needed for the page 
  521. * @author Elliot Condon 
  522. * @since 3.1.8 
  523. *  
  524. *-------------------------------------------------------------------------------------*/ 
  525.  
  526. function acf_everything_fields() 
  527. // defaults 
  528. $defaults = array( 
  529. 'metabox_ids' => '',  
  530. 'page_type' => '',  
  531. 'page_action' => '',  
  532. 'option_name' => '',  
  533. ); 
  534.  
  535.  
  536. // load post options 
  537. $options = array_merge($defaults, $_POST); 
  538.  
  539.  
  540. // metabox ids is a string with commas 
  541. $options['metabox_ids'] = explode( ', ', $options['metabox_ids'] ); 
  542.  
  543.  
  544. // get acfs 
  545. $acfs = apply_filters('acf/get_field_groups', false); 
  546.  
  547.  
  548. // layout 
  549. $layout = 'tr';  
  550. if( $options['page_type'] == "taxonomy" && $options['page_action'] == "add") 
  551. $layout = 'div'; 
  552. if( $options['page_type'] == "shopp_category") 
  553. $layout = 'metabox'; 
  554.  
  555.  
  556. if( $acfs ) 
  557. foreach( $acfs as $acf ) 
  558. // load options 
  559. $acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']); 
  560.  
  561.  
  562. // only add the chosen field groups 
  563. if( !in_array( $acf['id'], $options['metabox_ids'] ) ) 
  564. continue; 
  565.  
  566.  
  567. // layout dictates heading 
  568. $title = true; 
  569.  
  570. if( $acf['options']['layout'] == 'no_box' ) 
  571. $title = false; 
  572.  
  573.  
  574. // title  
  575. if( $options['page_action'] == "edit" && $options['page_type'] == 'user' ) 
  576. if( $title ) 
  577. echo '<h3>' .$acf['title'] . '</h3>'; 
  578.  
  579. echo '<table class="form-table"><tbody>'; 
  580.  
  581.  
  582. // wrapper 
  583. if( $layout == 'tr' ) 
  584. //nonce 
  585. echo '<tr style="display:none;"><td colspan="2"><input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" /></td></tr>'; 
  586. else 
  587. //nonce 
  588. echo '<input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" />'; 
  589.  
  590. if( $layout == 'metabox' ) 
  591. echo '<div class="postbox acf_postbox" id="acf_'. $acf['id'] .'">'; 
  592. echo '<div title="Click to toggle" class="handlediv"><br></div><h3 class="hndle"><span>' . $acf['title'] . '</span></h3>'; 
  593. echo '<div class="inside">'; 
  594.  
  595.  
  596. // load fields 
  597. $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']); 
  598.  
  599.  
  600. if( is_array($fields) ) { foreach( $fields as $field ) { 
  601.  
  602. // if they didn't select a type, skip this field 
  603. if( !$field['type'] || $field['type'] == 'null' ) continue; 
  604.  
  605.  
  606. // set value 
  607. if( !isset($field['value']) ) 
  608. $field['value'] = apply_filters('acf/load_value', false, $options['option_name'], $field); 
  609. $field['value'] = apply_filters('acf/format_value', $field['value'], $options['option_name'], $field); 
  610.  
  611.  
  612. // required 
  613. $required_class = ""; 
  614. $required_label = ""; 
  615.  
  616. if( $field['required'] ) 
  617. $required_class = ' required'; 
  618. $required_label = ' <span class="required">*</span>'; 
  619.  
  620.  
  621. if( $layout == 'metabox' ) 
  622. echo '<div id="acf-' . $field['name'] . '" class="field field_type-' . $field['type'] . ' field_key-' . $field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">'; 
  623.  
  624. echo '<p class="label">'; 
  625. echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>'; 
  626. echo $field['instructions']; 
  627. echo '</p>'; 
  628.  
  629. $field['name'] = 'fields[' . $field['key'] . ']'; 
  630. do_action('acf/create_field', $field); 
  631.  
  632. echo '</div>'; 
  633. elseif( $layout == 'div' ) 
  634. echo '<div id="acf-' . $field['name'] . '" class="form-field field field_type-' . $field['type'] . ' field_key-' . $field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">'; 
  635.  
  636. echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';  
  637. $field['name'] = 'fields[' . $field['key'] . ']'; 
  638. do_action('acf/create_field', $field ); 
  639. if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>'; 
  640.  
  641. echo '</div>'; 
  642. else 
  643. echo '<tr id="acf-' . $field['name'] . '" class="form-field field field_type-' . $field['type'] . ' field_key-'.$field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">'; 
  644. echo '<th valign="top" scope="row"><label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label></th>';  
  645. echo '<td>'; 
  646. $field['name'] = 'fields[' . $field['key'] . ']'; 
  647. do_action('acf/create_field', $field ); 
  648.  
  649. if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>'; 
  650. echo '</td>'; 
  651. echo '</tr>'; 
  652.  
  653.  
  654. }} 
  655.  
  656.  
  657.  
  658. // wrapper 
  659. if( $layout == 'metabox' ) 
  660. echo '</div></div>'; 
  661.  
  662.  
  663. // title  
  664. if( $options['page_action'] == "edit" && $options['page_type'] == 'user' ) 
  665. echo '</tbody></table>'; 
  666.  
  667.  
  668. // foreach($acfs as $acf) 
  669. // if($acfs) 
  670.  
  671. // exit for ajax 
  672. die(); 
  673.  
  674.  
  675.  
  676. /** 
  677. * delete_term 
  678. * @description:  
  679. * @since: 3.5.7 
  680. * @created: 12/01/13 
  681. */ 
  682.  
  683. function delete_term( $term, $tt_id, $taxonomy, $deleted_term ) 
  684. global $wpdb; 
  685.  
  686. $values = $wpdb->query($wpdb->prepare( 
  687. "DELETE FROM $wpdb->options WHERE option_name LIKE %s",  
  688. '%' . $taxonomy . '_' . $term . '%' 
  689. )); 
  690.  
  691.