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', 'term.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. } elseif( $pagenow == "edit-tags.php" || $pagenow == "term.php" ) { 
  238.  
  239. // vars 
  240. $taxonomy = 'post_tag'; 
  241. $term_id = 0; 
  242.  
  243.  
  244. // $_GET 
  245. if( !empty($_GET['taxonomy']) ) { 
  246.  
  247. $taxonomy = filter_var($_GET['taxonomy'], FILTER_SANITIZE_STRING); 
  248.  
  249.  
  250. if( !empty($_GET['tag_ID']) ) { 
  251.  
  252. $term_id = filter_var($_GET['tag_ID'], FILTER_SANITIZE_NUMBER_INT); 
  253.  
  254.  
  255.  
  256. // update filter 
  257. $filter['ef_taxonomy'] = $taxonomy; 
  258.  
  259.  
  260. // add 
  261. $this->data['page_type'] = "taxonomy"; 
  262. $this->data['page_action'] = "add"; 
  263. $this->data['option_name'] = ""; 
  264.  
  265.  
  266. // edit 
  267. if( $term_id ) { 
  268.  
  269. $this->data['page_action'] = "edit"; 
  270. $this->data['option_name'] = $taxonomy . "_" . $term_id; 
  271.  
  272.  
  273. elseif( $pagenow == "profile.php" ) 
  274.  
  275. $this->data['page_type'] = "user"; 
  276. $filter['ef_user'] = get_current_user_id(); 
  277.  
  278. $this->data['page_action'] = "edit"; 
  279. $this->data['option_name'] = "user_" . get_current_user_id(); 
  280.  
  281. elseif( $pagenow == "user-edit.php" && isset($_GET['user_id']) ) 
  282. // filter 
  283. $_GET['user_id'] = filter_var($_GET['user_id'], FILTER_SANITIZE_NUMBER_INT); 
  284.  
  285.  
  286. $this->data['page_type'] = "user"; 
  287. $filter['ef_user'] = $_GET['user_id']; 
  288.  
  289. $this->data['page_action'] = "edit"; 
  290. $this->data['option_name'] = "user_" . $_GET['user_id']; 
  291.  
  292. elseif( $pagenow == "user-new.php" ) 
  293. $this->data['page_type'] = "user"; 
  294. $filter['ef_user'] ='all'; 
  295.  
  296. $this->data['page_action'] = "add"; 
  297. $this->data['option_name'] = ""; 
  298.  
  299. elseif( $pagenow == "media.php" || $pagenow == 'upload.php' ) 
  300.  
  301. $this->data['page_type'] = "media"; 
  302. $filter['post_type'] = 'attachment'; 
  303.  
  304. $this->data['page_action'] = "add"; 
  305. $this->data['option_name'] = ""; 
  306.  
  307. if(isset($_GET['attachment_id'])) 
  308. // filter 
  309. $_GET['attachment_id'] = filter_var($_GET['attachment_id'], FILTER_SANITIZE_NUMBER_INT); 
  310.  
  311.  
  312. $this->data['page_action'] = "edit"; 
  313. $this->data['option_name'] = $_GET['attachment_id']; 
  314.  
  315.  
  316.  
  317. // get field groups 
  318. $metabox_ids = array(); 
  319. $this->data['metabox_ids'] = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter ); 
  320.  
  321.  
  322. // dont continue if no ids were found 
  323. if( empty( $this->data['metabox_ids'] ) ) 
  324. return false;  
  325.  
  326.  
  327. // actions 
  328. add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); 
  329. add_action('admin_head', array($this, 'admin_head')); 
  330.  
  331.  
  332.  
  333.  
  334. /** 
  335. * admin_enqueue_scripts 
  336. * @description:  
  337. * @since: 3.6 
  338. * @created: 30/01/13 
  339. */ 
  340.  
  341. function admin_enqueue_scripts() 
  342. do_action('acf/input/admin_enqueue_scripts'); 
  343.  
  344.  
  345. /**-------------------------------------------------------------------------------------- 
  346. * admin_head 
  347. * @author Elliot Condon 
  348. * @since 3.1.8 
  349. *  
  350. *-------------------------------------------------------------------------------------*/ 
  351.  
  352. function admin_head() 
  353. {  
  354. global $pagenow; 
  355.  
  356.  
  357. // add user js + css 
  358. do_action('acf/input/admin_head'); 
  359.  
  360.  
  361. ?> 
  362. <script type="text/javascript"> 
  363. (function($) { 
  364.  
  365. acf.data = { 
  366. action : 'acf/everything_fields',  
  367. metabox_ids : '<?php echo implode( ', ', $this->data['metabox_ids'] ); ?>',  
  368. page_type : '<?php echo $this->data['page_type']; ?>',  
  369. page_action : '<?php echo $this->data['page_action']; ?>',  
  370. option_name : '<?php echo $this->data['option_name']; ?>' 
  371. }; 
  372.  
  373. $(document).ready(function() { 
  374.  
  375. $.ajax({ 
  376. url: ajaxurl,  
  377. data: acf.data,  
  378. type: 'post',  
  379. dataType: 'html',  
  380. success: function(html) { 
  381.  
  382. <?php  
  383. if($this->data['page_type'] == "user") 
  384. if($this->data['page_action'] == "add") 
  385. echo "$('#createuser > table.form-table:last > tbody').append( html );"; 
  386. else 
  387. echo "$('#your-profile .form-table:last').after( html );"; 
  388. elseif($this->data['page_type'] == "shopp_category") 
  389. echo "$('#post-body-content').append( html );"; 
  390. elseif($this->data['page_type'] == "taxonomy") 
  391. if($this->data['page_action'] == "add") 
  392. echo "$('#addtag > p.submit').before( html );"; 
  393. else 
  394. echo "$('#edittag > table.form-table:first > tbody').append( html );"; 
  395. elseif($this->data['page_type'] == "media") 
  396. if($this->data['page_action'] == "add") 
  397. echo "$('#addtag > p.submit').before( html );"; 
  398. else 
  399. echo "$('#media-single-form table tbody tr.submit').before( html );"; 
  400. ?> 
  401.  
  402. setTimeout( function() {  
  403. $(document).trigger('acf/setup_fields', $('#wpbody') );  
  404. }, 200); 
  405.  
  406. }); 
  407.  
  408.  
  409. /* 
  410. * Taxonomy Add 
  411. * @description:  
  412. * @since: 3.6 
  413. * @created: 24/02/13 
  414. */ 
  415.  
  416. $(document).ajaxComplete(function(event, xhr, settings) { 
  417.  
  418. // vars 
  419. data = acf.helpers.url_to_object(settings.data); 
  420.  
  421.  
  422. // validate 
  423. if( data.action != 'add-tag' ) 
  424. return; 
  425.  
  426.  
  427. // vars 
  428. var $el = $('#addtag'); 
  429.  
  430.  
  431. // clear WYSIWYG field 
  432. $el.find('.acf_wysiwyg textarea').each(function() { 
  433.  
  434.  
  435. // vars 
  436. var textarea = $(this),  
  437. id = textarea.attr('id'),  
  438. editor = tinyMCE.get( id ); 
  439.  
  440. if( editor ) 
  441. editor.setContent(''); 
  442. editor.save(); 
  443.  
  444. }); 
  445.  
  446.  
  447. // clear image / file fields 
  448. $el.find('.field .active').removeClass('active'); 
  449.  
  450.  
  451. // clear checkbox 
  452. $el.find('input[type="checkbox"]').removeAttr('checked'); 
  453.  
  454. }); 
  455.  
  456. }); 
  457.  
  458.  
  459. })(jQuery); 
  460. </script> 
  461. <?php 
  462.  
  463.  
  464.  
  465. /**-------------------------------------------------------------------------------------- 
  466. * save_taxonomy 
  467. * @author Elliot Condon 
  468. * @since 3.1.8 
  469. *  
  470. *-------------------------------------------------------------------------------------*/ 
  471.  
  472. function save_taxonomy( $term_id ) 
  473. // verify nonce 
  474. if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) 
  475. return $term_id; 
  476.  
  477.  
  478. // for some weird reason, this is triggered by saving a menu...  
  479. if( !isset($_POST['taxonomy']) ) 
  480. return; 
  481.  
  482. // $post_id to save against 
  483. $post_id = $_POST['taxonomy'] . '_' . $term_id; 
  484.  
  485.  
  486. // update the post 
  487. do_action('acf/save_post', $post_id); 
  488.  
  489.  
  490.  
  491. /**-------------------------------------------------------------------------------------- 
  492. * profile_save 
  493. * @author Elliot Condon 
  494. * @since 3.1.8 
  495. *  
  496. *-------------------------------------------------------------------------------------*/ 
  497.  
  498. function save_user( $user_id ) 
  499. // verify nonce 
  500. if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) 
  501. return $user_id; 
  502.  
  503.  
  504. // $post_id to save against 
  505. $post_id = 'user_' . $user_id; 
  506.  
  507.  
  508. // update the post 
  509. do_action('acf/save_post', $post_id); 
  510.  
  511.  
  512.  
  513. /** 
  514. * shopp_category_saved 
  515. * @description:  
  516. * @since 3.5.2 
  517. * @created: 27/11/12 
  518. */ 
  519.  
  520. function shopp_category_saved( $category ) 
  521. // verify nonce 
  522. if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) 
  523. return $category; 
  524.  
  525.  
  526. // $post_id to save against 
  527. $post_id = 'shopp_category_' . $category->id; 
  528.  
  529.  
  530. // update the post 
  531. do_action('acf/save_post', $post_id); 
  532.  
  533.  
  534.  
  535.  
  536. /**-------------------------------------------------------------------------------------- 
  537. * acf_everything_fields 
  538. * @description Ajax call that renders the html needed for the page 
  539. * @author Elliot Condon 
  540. * @since 3.1.8 
  541. *  
  542. *-------------------------------------------------------------------------------------*/ 
  543.  
  544. function acf_everything_fields() 
  545. // defaults 
  546. $defaults = array( 
  547. 'metabox_ids' => '',  
  548. 'page_type' => '',  
  549. 'page_action' => '',  
  550. 'option_name' => '',  
  551. ); 
  552.  
  553.  
  554. // load post options 
  555. $options = array_merge($defaults, $_POST); 
  556.  
  557.  
  558. // metabox ids is a string with commas 
  559. $options['metabox_ids'] = explode( ', ', $options['metabox_ids'] ); 
  560.  
  561.  
  562. // get acfs 
  563. $acfs = apply_filters('acf/get_field_groups', false); 
  564.  
  565.  
  566. // layout 
  567. $layout = 'tr';  
  568. if( $options['page_type'] == "taxonomy" && $options['page_action'] == "add") 
  569. $layout = 'div'; 
  570. if( $options['page_type'] == "shopp_category") 
  571. $layout = 'metabox'; 
  572.  
  573.  
  574. if( $acfs ) 
  575. foreach( $acfs as $acf ) 
  576. // load options 
  577. $acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']); 
  578.  
  579.  
  580. // only add the chosen field groups 
  581. if( !in_array( $acf['id'], $options['metabox_ids'] ) ) 
  582. continue; 
  583.  
  584.  
  585. // layout dictates heading 
  586. $title = true; 
  587.  
  588. if( $acf['options']['layout'] == 'no_box' ) 
  589. $title = false; 
  590.  
  591.  
  592. // title  
  593. if( $options['page_action'] == "edit" && $options['page_type'] == 'user' ) 
  594. if( $title ) 
  595. echo '<h3>' .$acf['title'] . '</h3>'; 
  596.  
  597. echo '<table class="form-table"><tbody>'; 
  598.  
  599.  
  600. // wrapper 
  601. if( $layout == 'tr' ) 
  602. //nonce 
  603. echo '<tr style="display:none;"><td colspan="2"><input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" /></td></tr>'; 
  604. else 
  605. //nonce 
  606. echo '<input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" />'; 
  607.  
  608. if( $layout == 'metabox' ) 
  609. echo '<div class="postbox acf_postbox" id="acf_'. $acf['id'] .'">'; 
  610. echo '<div title="Click to toggle" class="handlediv"><br></div><h3 class="hndle"><span>' . $acf['title'] . '</span></h3>'; 
  611. echo '<div class="inside">'; 
  612.  
  613.  
  614. // load fields 
  615. $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']); 
  616.  
  617.  
  618. if( is_array($fields) ) { foreach( $fields as $field ) { 
  619.  
  620. // if they didn't select a type, skip this field 
  621. if( !$field['type'] || $field['type'] == 'null' ) continue; 
  622.  
  623.  
  624. // set value 
  625. if( !isset($field['value']) ) 
  626. $field['value'] = apply_filters('acf/load_value', false, $options['option_name'], $field); 
  627. $field['value'] = apply_filters('acf/format_value', $field['value'], $options['option_name'], $field); 
  628.  
  629.  
  630. // required 
  631. $required_class = ""; 
  632. $required_label = ""; 
  633.  
  634. if( $field['required'] ) 
  635. $required_class = ' required'; 
  636. $required_label = ' <span class="required">*</span>'; 
  637.  
  638.  
  639. if( $layout == 'metabox' ) 
  640. 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'] . '">'; 
  641.  
  642. echo '<p class="label">'; 
  643. echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>'; 
  644. echo $field['instructions']; 
  645. echo '</p>'; 
  646.  
  647. $field['name'] = 'fields[' . $field['key'] . ']'; 
  648. do_action('acf/create_field', $field); 
  649.  
  650. echo '</div>'; 
  651. elseif( $layout == 'div' ) 
  652. 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'] . '">'; 
  653.  
  654. echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';  
  655. $field['name'] = 'fields[' . $field['key'] . ']'; 
  656. do_action('acf/create_field', $field ); 
  657. if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>'; 
  658.  
  659. echo '</div>'; 
  660. else 
  661. 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'] . '">'; 
  662. echo '<th valign="top" scope="row"><label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label></th>';  
  663. echo '<td>'; 
  664. $field['name'] = 'fields[' . $field['key'] . ']'; 
  665. do_action('acf/create_field', $field ); 
  666.  
  667. if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>'; 
  668. echo '</td>'; 
  669. echo '</tr>'; 
  670.  
  671.  
  672. }} 
  673.  
  674.  
  675.  
  676. // wrapper 
  677. if( $layout == 'metabox' ) 
  678. echo '</div></div>'; 
  679.  
  680.  
  681. // title  
  682. if( $options['page_action'] == "edit" && $options['page_type'] == 'user' ) 
  683. echo '</tbody></table>'; 
  684.  
  685.  
  686. // foreach($acfs as $acf) 
  687. // if($acfs) 
  688.  
  689. // exit for ajax 
  690. die(); 
  691.  
  692.  
  693.  
  694. /** 
  695. * delete_term 
  696. * @description:  
  697. * @since: 3.5.7 
  698. * @created: 12/01/13 
  699. */ 
  700.  
  701. function delete_term( $term, $tt_id, $taxonomy, $deleted_term ) 
  702. global $wpdb; 
  703.  
  704. $values = $wpdb->query($wpdb->prepare( 
  705. "DELETE FROM $wpdb->options WHERE option_name LIKE %s",  
  706. '%' . $taxonomy . '_' . $term . '%' 
  707. )); 
  708.  
  709.