form-taxonomy.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /*
  3. * ACF Taxonomy Form Class
  4. *
  5. * All the logic for adding fields to taxonomy terms
  6. *
  7. * @class acf_form_taxonomy
  8. * @package ACF
  9. * @subpackage Forms
  10. */
  11. if ( ! class_exists( 'acf_form_taxonomy' ) ) :
  12. class acf_form_taxonomy {
  13. var $view = 'add';
  14. /*
  15. * __construct
  16. *
  17. * This function will setup the class functionality
  18. *
  19. * @type function
  20. * @date 5/03/2014
  21. * @since 5.0.0
  22. *
  23. * @param n/a
  24. * @return n/a
  25. */
  26. function __construct() {
  27. // actions
  28. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  29. // save
  30. add_action( 'create_term', array( $this, 'save_term' ), 10, 3 );
  31. add_action( 'edit_term', array( $this, 'save_term' ), 10, 3 );
  32. // delete
  33. add_action( 'delete_term', array( $this, 'delete_term' ), 10, 4 );
  34. }
  35. /*
  36. * validate_page
  37. *
  38. * This function will check if the current page is for a post/page edit form
  39. *
  40. * @type function
  41. * @date 23/06/12
  42. * @since 3.1.8
  43. *
  44. * @param n/a
  45. * @return (boolean)
  46. */
  47. function validate_page() {
  48. // global
  49. global $pagenow;
  50. // validate page
  51. if ( $pagenow === 'edit-tags.php' || $pagenow === 'term.php' ) {
  52. return true;
  53. }
  54. // return
  55. return false;
  56. }
  57. /*
  58. * admin_enqueue_scripts
  59. *
  60. * This action is run after post query but before any admin script / head actions.
  61. * It is a good place to register all actions.
  62. *
  63. * @type action (admin_enqueue_scripts)
  64. * @date 26/01/13
  65. * @since 3.6.0
  66. *
  67. * @param N/A
  68. * @return N/A
  69. */
  70. function admin_enqueue_scripts() {
  71. // validate page
  72. if ( ! $this->validate_page() ) {
  73. return;
  74. }
  75. // vars
  76. $screen = get_current_screen();
  77. $taxonomy = $screen->taxonomy;
  78. // load acf scripts
  79. acf_enqueue_scripts();
  80. // actions
  81. add_action( 'admin_footer', array( $this, 'admin_footer' ), 10, 1 );
  82. add_action( "{$taxonomy}_add_form_fields", array( $this, 'add_term' ), 10, 1 );
  83. add_action( "{$taxonomy}_edit_form", array( $this, 'edit_term' ), 10, 2 );
  84. }
  85. /*
  86. * add_term
  87. *
  88. * description
  89. *
  90. * @type function
  91. * @date 8/10/13
  92. * @since 5.0.0
  93. *
  94. * @param $post_id (int)
  95. * @return $post_id (int)
  96. */
  97. function add_term( $taxonomy ) {
  98. // vars
  99. $post_id = 'term_0';
  100. // update vars
  101. $this->view = 'add';
  102. // get field groups
  103. $field_groups = acf_get_field_groups(
  104. array(
  105. 'taxonomy' => $taxonomy,
  106. )
  107. );
  108. // render
  109. if ( ! empty( $field_groups ) ) {
  110. // data
  111. acf_form_data(
  112. array(
  113. 'screen' => 'taxonomy',
  114. 'post_id' => $post_id,
  115. )
  116. );
  117. // wrap
  118. echo '<div id="acf-term-fields" class="acf-fields -clear">';
  119. // loop
  120. foreach ( $field_groups as $field_group ) {
  121. $fields = acf_get_fields( $field_group );
  122. acf_render_fields( $fields, $post_id, 'div', 'field' );
  123. }
  124. // wrap
  125. echo '</div>';
  126. }
  127. }
  128. /*
  129. * edit_term
  130. *
  131. * description
  132. *
  133. * @type function
  134. * @date 8/10/13
  135. * @since 5.0.0
  136. *
  137. * @param $post_id (int)
  138. * @return $post_id (int)
  139. */
  140. function edit_term( $term, $taxonomy ) {
  141. // vars
  142. $post_id = 'term_' . $term->term_id;
  143. // update vars
  144. $this->view = 'edit';
  145. // get field groups
  146. $field_groups = acf_get_field_groups(
  147. array(
  148. 'taxonomy' => $taxonomy,
  149. )
  150. );
  151. // render
  152. if ( ! empty( $field_groups ) ) {
  153. acf_form_data(
  154. array(
  155. 'screen' => 'taxonomy',
  156. 'post_id' => $post_id,
  157. )
  158. );
  159. foreach ( $field_groups as $field_group ) {
  160. // title
  161. if ( $field_group['style'] == 'default' ) {
  162. echo '<h2>' . $field_group['title'] . '</h2>';
  163. }
  164. // fields
  165. echo '<table class="form-table">';
  166. $fields = acf_get_fields( $field_group );
  167. acf_render_fields( $fields, $post_id, 'tr', 'field' );
  168. echo '</table>';
  169. }
  170. }
  171. }
  172. /*
  173. * admin_footer
  174. *
  175. * description
  176. *
  177. * @type function
  178. * @date 27/03/2015
  179. * @since 5.1.5
  180. *
  181. * @param $post_id (int)
  182. * @return $post_id (int)
  183. */
  184. function admin_footer() {
  185. ?>
  186. <script type="text/javascript">
  187. (function($) {
  188. // Define vars.
  189. var view = '<?php echo $this->view; ?>';
  190. var $form = $('#' + view + 'tag');
  191. var $submit = $('#' + view + 'tag input[type="submit"]:last');
  192. // Add missing spinner.
  193. if( !$submit.next('.spinner').length ) {
  194. $submit.after('<span class="spinner"></span>');
  195. }
  196. <?php
  197. // View: Add.
  198. if ( $this->view == 'add' ) :
  199. ?>
  200. // vars
  201. var $fields = $('#acf-term-fields');
  202. var html = '';
  203. // Store a copy of the $fields html used later to replace after AJAX request.
  204. // Hook into 'prepare' action to allow ACF core helpers to first modify DOM.
  205. // Fixes issue where hidden #acf-hidden-wp-editor is initialized again.
  206. acf.addAction('prepare', function(){
  207. html = $fields.html();
  208. }, 6);
  209. // WP triggers click as primary action
  210. $submit.on('click', function( e ){
  211. // validate
  212. var valid = acf.validateForm({
  213. form: $form,
  214. event: e,
  215. reset: true
  216. });
  217. // if not valid, stop event and allow validation to continue
  218. if( !valid ) {
  219. e.preventDefault();
  220. e.stopImmediatePropagation();
  221. }
  222. });
  223. // listen to AJAX add-tag complete
  224. $(document).ajaxComplete(function(event, xhr, settings) {
  225. // bail early if is other ajax call
  226. if( settings.data.indexOf('action=add-tag') == -1 ) {
  227. return;
  228. }
  229. // bail early if response contains error
  230. if( xhr.responseText.indexOf('wp_error') !== -1 ) {
  231. return;
  232. }
  233. // action for 3rd party customization
  234. acf.doAction('remove', $fields);
  235. // reset HTML
  236. $fields.html( html );
  237. // action for 3rd party customization
  238. acf.doAction('append', $fields);
  239. // reset unload
  240. acf.unload.reset();
  241. });
  242. <?php endif; ?>
  243. })(jQuery);
  244. </script>
  245. <?php
  246. }
  247. /*
  248. * save_term
  249. *
  250. * description
  251. *
  252. * @type function
  253. * @date 8/10/13
  254. * @since 5.0.0
  255. *
  256. * @param $post_id (int)
  257. * @return $post_id (int)
  258. */
  259. function save_term( $term_id, $tt_id, $taxonomy ) {
  260. // vars
  261. $post_id = 'term_' . $term_id;
  262. // verify and remove nonce
  263. if ( ! acf_verify_nonce( 'taxonomy' ) ) {
  264. return $term_id;
  265. }
  266. // valied and show errors
  267. acf_validate_save_post( true );
  268. // save
  269. acf_save_post( $post_id );
  270. }
  271. /*
  272. * delete_term
  273. *
  274. * description
  275. *
  276. * @type function
  277. * @date 15/10/13
  278. * @since 5.0.0
  279. *
  280. * @param $post_id (int)
  281. * @return $post_id (int)
  282. */
  283. function delete_term( $term, $tt_id, $taxonomy, $deleted_term ) {
  284. // bail early if termmeta table exists
  285. if ( acf_isset_termmeta() ) {
  286. return $term;
  287. }
  288. // globals
  289. global $wpdb;
  290. // vars
  291. $search = $taxonomy . '_' . $term . '_%';
  292. $_search = '_' . $search;
  293. // escape '_'
  294. // http://stackoverflow.com/questions/2300285/how-do-i-escape-in-sql-server
  295. $search = str_replace( '_', '\_', $search );
  296. $_search = str_replace( '_', '\_', $_search );
  297. // delete
  298. $result = $wpdb->query(
  299. $wpdb->prepare(
  300. "DELETE FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s",
  301. $search,
  302. $_search
  303. )
  304. );
  305. }
  306. }
  307. new acf_form_taxonomy();
  308. endif;
  309. ?>