PluginProbe
Advanced Custom Fields (ACF®) / 5.9.8
Advanced Custom Fields (ACF®) v5.9.8
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / forms / form-taxonomy.php
form-taxonomy.php
416 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * ACF Taxonomy Form Class
5 *
6 * All the logic for adding fields to taxonomy terms
7 *
8 * @class acf_form_taxonomy
9 * @package ACF
10 * @subpackage Forms
11 */
12
13 if( ! class_exists('acf_form_taxonomy') ) :
14
15 class acf_form_taxonomy {
16
17 var $view = 'add';
18
19
20 /*
21 * __construct
22 *
23 * This function will setup the class functionality
24 *
25 * @type function
26 * @date 5/03/2014
27 * @since 5.0.0
28 *
29 * @param n/a
30 * @return n/a
31 */
32
33 function __construct() {
34
35 // actions
36 add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
37
38
39 // save
40 add_action('create_term', array($this, 'save_term'), 10, 3);
41 add_action('edit_term', array($this, 'save_term'), 10, 3);
42
43
44 // delete
45 add_action('delete_term', array($this, 'delete_term'), 10, 4);
46
47 }
48
49
50 /*
51 * validate_page
52 *
53 * This function will check if the current page is for a post/page edit form
54 *
55 * @type function
56 * @date 23/06/12
57 * @since 3.1.8
58 *
59 * @param n/a
60 * @return (boolean)
61 */
62
63 function validate_page() {
64
65 // global
66 global $pagenow;
67
68
69 // validate page
70 if( $pagenow === 'edit-tags.php' || $pagenow === 'term.php' ) {
71
72 return true;
73
74 }
75
76
77 // return
78 return false;
79 }
80
81
82 /*
83 * admin_enqueue_scripts
84 *
85 * This action is run after post query but before any admin script / head actions.
86 * It is a good place to register all actions.
87 *
88 * @type action (admin_enqueue_scripts)
89 * @date 26/01/13
90 * @since 3.6.0
91 *
92 * @param N/A
93 * @return N/A
94 */
95
96 function admin_enqueue_scripts() {
97
98 // validate page
99 if( !$this->validate_page() ) {
100
101 return;
102
103 }
104
105
106 // vars
107 $screen = get_current_screen();
108 $taxonomy = $screen->taxonomy;
109
110
111 // load acf scripts
112 acf_enqueue_scripts();
113
114
115 // actions
116 add_action('admin_footer', array($this, 'admin_footer'), 10, 1);
117 add_action("{$taxonomy}_add_form_fields", array($this, 'add_term'), 10, 1);
118 add_action("{$taxonomy}_edit_form", array($this, 'edit_term'), 10, 2);
119
120 }
121
122
123 /*
124 * add_term
125 *
126 * description
127 *
128 * @type function
129 * @date 8/10/13
130 * @since 5.0.0
131 *
132 * @param $post_id (int)
133 * @return $post_id (int)
134 */
135
136 function add_term( $taxonomy ) {
137
138 // vars
139 $post_id = 'term_0';
140
141
142 // update vars
143 $this->view = 'add';
144
145
146 // get field groups
147 $field_groups = acf_get_field_groups(array(
148 'taxonomy' => $taxonomy
149 ));
150
151
152 // render
153 if( !empty($field_groups) ) {
154
155 // data
156 acf_form_data(array(
157 'screen' => 'taxonomy',
158 'post_id' => $post_id,
159 ));
160
161 // wrap
162 echo '<div id="acf-term-fields" class="acf-fields -clear">';
163
164 // loop
165 foreach( $field_groups as $field_group ) {
166 $fields = acf_get_fields( $field_group );
167 acf_render_fields( $fields, $post_id, 'div', 'field' );
168 }
169
170 // wrap
171 echo '</div>';
172
173 }
174
175 }
176
177
178 /*
179 * edit_term
180 *
181 * description
182 *
183 * @type function
184 * @date 8/10/13
185 * @since 5.0.0
186 *
187 * @param $post_id (int)
188 * @return $post_id (int)
189 */
190
191 function edit_term( $term, $taxonomy ) {
192
193 // vars
194 $post_id = 'term_' . $term->term_id;
195
196
197 // update vars
198 $this->view = 'edit';
199
200
201 // get field groups
202 $field_groups = acf_get_field_groups(array(
203 'taxonomy' => $taxonomy
204 ));
205
206
207 // render
208 if( !empty($field_groups) ) {
209
210 acf_form_data(array(
211 'screen' => 'taxonomy',
212 'post_id' => $post_id,
213 ));
214
215 foreach( $field_groups as $field_group ) {
216
217 // title
218 if( $field_group['style'] == 'default' ) {
219 echo '<h2>' . $field_group['title'] . '</h2>';
220 }
221
222 // fields
223 echo '<table class="form-table">';
224 $fields = acf_get_fields( $field_group );
225 acf_render_fields( $fields, $post_id, 'tr', 'field' );
226 echo '</table>';
227
228 }
229
230 }
231
232 }
233
234
235 /*
236 * admin_footer
237 *
238 * description
239 *
240 * @type function
241 * @date 27/03/2015
242 * @since 5.1.5
243 *
244 * @param $post_id (int)
245 * @return $post_id (int)
246 */
247
248 function admin_footer() {
249
250 ?>
251 <script type="text/javascript">
252 (function($) {
253
254 // Define vars.
255 var view = '<?php echo $this->view; ?>';
256 var $form = $('#' + view + 'tag');
257 var $submit = $('#' + view + 'tag input[type="submit"]:last');
258
259 // Add missing spinner.
260 if( !$submit.next('.spinner').length ) {
261 $submit.after('<span class="spinner"></span>');
262 }
263
264 <?php
265
266 // View: Add.
267 if( $this->view == 'add' ): ?>
268
269 // vars
270 var $fields = $('#acf-term-fields');
271 var html = '';
272
273 // Store a copy of the $fields html used later to replace after AJAX request.
274 // Hook into 'prepare' action to allow ACF core helpers to first modify DOM.
275 // Fixes issue where hidden #acf-hidden-wp-editor is initialized again.
276 acf.addAction('prepare', function(){
277 html = $fields.html();
278 }, 6);
279
280 // WP triggers click as primary action
281 $submit.on('click', function( e ){
282
283 // validate
284 var valid = acf.validateForm({
285 form: $form,
286 event: e,
287 reset: true
288 });
289
290 // if not valid, stop event and allow validation to continue
291 if( !valid ) {
292 e.preventDefault();
293 e.stopImmediatePropagation();
294 }
295 });
296
297 // listen to AJAX add-tag complete
298 $(document).ajaxComplete(function(event, xhr, settings) {
299
300 // bail early if is other ajax call
301 if( settings.data.indexOf('action=add-tag') == -1 ) {
302 return;
303 }
304
305 // bail early if response contains error
306 if( xhr.responseText.indexOf('wp_error') !== -1 ) {
307 return;
308 }
309
310 // action for 3rd party customization
311 acf.doAction('remove', $fields);
312
313 // reset HTML
314 $fields.html( html );
315
316 // action for 3rd party customization
317 acf.doAction('append', $fields);
318
319 // reset unload
320 acf.unload.reset();
321 });
322
323 <?php endif; ?>
324
325 })(jQuery);
326 </script>
327 <?php
328
329 }
330
331
332 /*
333 * save_term
334 *
335 * description
336 *
337 * @type function
338 * @date 8/10/13
339 * @since 5.0.0
340 *
341 * @param $post_id (int)
342 * @return $post_id (int)
343 */
344
345 function save_term( $term_id, $tt_id, $taxonomy ) {
346
347 // vars
348 $post_id = 'term_' . $term_id;
349
350
351 // verify and remove nonce
352 if( !acf_verify_nonce('taxonomy') ) return $term_id;
353
354
355 // valied and show errors
356 acf_validate_save_post( true );
357
358
359 // save
360 acf_save_post( $post_id );
361
362 }
363
364
365 /*
366 * delete_term
367 *
368 * description
369 *
370 * @type function
371 * @date 15/10/13
372 * @since 5.0.0
373 *
374 * @param $post_id (int)
375 * @return $post_id (int)
376 */
377
378 function delete_term( $term, $tt_id, $taxonomy, $deleted_term ) {
379
380 // bail early if termmeta table exists
381 if( acf_isset_termmeta() ) return $term;
382
383
384 // globals
385 global $wpdb;
386
387
388 // vars
389 $search = $taxonomy . '_' . $term . '_%';
390 $_search = '_' . $search;
391
392
393 // escape '_'
394 // http://stackoverflow.com/questions/2300285/how-do-i-escape-in-sql-server
395 $search = str_replace('_', '\_', $search);
396 $_search = str_replace('_', '\_', $_search);
397
398
399 // delete
400 $result = $wpdb->query($wpdb->prepare(
401 "DELETE FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s",
402 $search,
403 $_search
404 ));
405
406 }
407
408 }
409
410 new acf_form_taxonomy();
411
412 endif;
413
414
415 ?>
416