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