custom.php
6 years ago
email.php
6 years ago
option.php
6 years ago
post.php
6 years ago
term.php
6 years ago
user.php
6 years ago
term.php
371 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_form_term')): |
| 7 | |
| 8 | class acfe_form_term{ |
| 9 | |
| 10 | function __construct(){ |
| 11 | |
| 12 | /* |
| 13 | * Form |
| 14 | */ |
| 15 | add_filter('acfe/form/load/action/term', array($this, 'load'), 1, 2); |
| 16 | add_action('acfe/form/submit/action/term', array($this, 'submit'), 1, 2); |
| 17 | |
| 18 | /* |
| 19 | * Admin |
| 20 | */ |
| 21 | add_filter('acf/prepare_field/name=acfe_form_term_save_meta', array(acfe()->acfe_form, 'map_fields')); |
| 22 | add_filter('acf/prepare_field/name=acfe_form_term_load_meta', array(acfe()->acfe_form, 'map_fields_deep')); |
| 23 | |
| 24 | add_filter('acf/prepare_field/name=acfe_form_term_map_name', array(acfe()->acfe_form, 'map_fields_deep')); |
| 25 | add_filter('acf/prepare_field/name=acfe_form_term_map_slug', array(acfe()->acfe_form, 'map_fields_deep')); |
| 26 | add_filter('acf/prepare_field/name=acfe_form_term_map_taxonomy', array(acfe()->acfe_form, 'map_fields_deep')); |
| 27 | add_filter('acf/prepare_field/name=acfe_form_term_map_parent', array(acfe()->acfe_form, 'map_fields_deep')); |
| 28 | add_filter('acf/prepare_field/name=acfe_form_term_map_description', array(acfe()->acfe_form, 'map_fields_deep')); |
| 29 | |
| 30 | } |
| 31 | |
| 32 | function load($form, $post_id){ |
| 33 | |
| 34 | // Form |
| 35 | $form_name = acf_maybe_get($form, 'form_name'); |
| 36 | $form_id = acf_maybe_get($form, 'form_id'); |
| 37 | $post_info = acf_get_post_id_info($post_id); |
| 38 | |
| 39 | // Action |
| 40 | $term_action = get_sub_field('acfe_form_term_action'); |
| 41 | |
| 42 | // Load values |
| 43 | $load_values = get_sub_field('acfe_form_term_load_values'); |
| 44 | $load_source = get_sub_field('acfe_form_term_load_source'); |
| 45 | $load_meta = get_sub_field('acfe_form_term_load_meta'); |
| 46 | |
| 47 | // Load values |
| 48 | if(!$load_values) |
| 49 | return $form; |
| 50 | |
| 51 | $_name = get_sub_field('acfe_form_term_map_name'); |
| 52 | $_slug = get_sub_field('acfe_form_term_map_slug'); |
| 53 | $_taxonomy = get_sub_field('acfe_form_term_map_taxonomy'); |
| 54 | $_parent = get_sub_field('acfe_form_term_map_parent'); |
| 55 | $_description = get_sub_field('acfe_form_term_map_description'); |
| 56 | |
| 57 | $_term_id = 0; |
| 58 | |
| 59 | // Custom Term ID |
| 60 | if($load_source !== 'current_term'){ |
| 61 | |
| 62 | $_term_id = $load_source; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | // Current Term |
| 67 | elseif($load_source === 'current_term'){ |
| 68 | |
| 69 | if($post_info['type'] === 'term') |
| 70 | $_term_id = $post_info['id']; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | $_term_id = apply_filters('acfe/form/load/action/term/' . $term_action . '_id', $_term_id, $form); |
| 75 | $_term_id = apply_filters('acfe/form/load/action/term/' . $term_action . '_id/name=' . $form_name, $_term_id, $form); |
| 76 | $_term_id = apply_filters('acfe/form/load/action/term/' . $term_action . '_id/id=' . $form_id, $_term_id, $form); |
| 77 | |
| 78 | // Invalid Term ID |
| 79 | if(!$_term_id) |
| 80 | return $form; |
| 81 | |
| 82 | // Name |
| 83 | if(acf_is_field_key($_name)){ |
| 84 | |
| 85 | $key = array_search($_name, $load_meta); |
| 86 | |
| 87 | if($key !== false){ |
| 88 | |
| 89 | unset($load_meta[$key]); |
| 90 | $form['map'][$_name]['value'] = get_term_field('name', $_term_id); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | } |
| 95 | |
| 96 | // Slug |
| 97 | if(acf_is_field_key($_slug)){ |
| 98 | |
| 99 | $key = array_search($_slug, $load_meta); |
| 100 | |
| 101 | if($key !== false){ |
| 102 | |
| 103 | unset($load_meta[$key]); |
| 104 | $form['map'][$_slug]['value'] = get_term_field('slug', $_term_id); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | |
| 110 | // Taxonomy |
| 111 | if(acf_is_field_key($_taxonomy)){ |
| 112 | |
| 113 | $key = array_search($_taxonomy, $load_meta); |
| 114 | |
| 115 | if($key !== false){ |
| 116 | |
| 117 | unset($load_meta[$key]); |
| 118 | $form['map'][$_taxonomy]['value'] = get_term_field('taxonomy', $_term_id); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | // Parent |
| 125 | if(acf_is_field_key($_parent)){ |
| 126 | |
| 127 | $key = array_search($_parent, $load_meta); |
| 128 | |
| 129 | if($key !== false){ |
| 130 | |
| 131 | unset($load_meta[$key]); |
| 132 | |
| 133 | $get_term_field = get_term_field('parent', $_term_id); |
| 134 | |
| 135 | if(!empty($get_term_field)) |
| 136 | $form['map'][$_parent]['value'] = get_term_field('parent', $_term_id); |
| 137 | |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | // Description |
| 143 | if(acf_is_field_key($_description)){ |
| 144 | |
| 145 | $key = array_search($_description, $load_meta); |
| 146 | |
| 147 | if($key !== false){ |
| 148 | |
| 149 | unset($load_meta[$key]); |
| 150 | $form['map'][$_description]['value'] = get_term_field('description', $_term_id); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |
| 156 | // Load others values |
| 157 | if(!empty($load_meta)){ |
| 158 | |
| 159 | foreach($load_meta as $field_key){ |
| 160 | |
| 161 | $field = acf_get_field($field_key); |
| 162 | |
| 163 | $form['map'][$field_key]['value'] = acf_get_value('term_' . $_term_id, $field); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | } |
| 168 | |
| 169 | return $form; |
| 170 | |
| 171 | } |
| 172 | |
| 173 | function submit($form, $post_id){ |
| 174 | |
| 175 | $form_name = acf_maybe_get($form, 'form_name'); |
| 176 | $form_id = acf_maybe_get($form, 'form_id'); |
| 177 | $post_info = acf_get_post_id_info($post_id); |
| 178 | |
| 179 | // Action |
| 180 | $term_action = get_sub_field('acfe_form_term_action'); |
| 181 | |
| 182 | // Mapping |
| 183 | $map = array( |
| 184 | 'name' => get_sub_field('acfe_form_term_map_name'), |
| 185 | 'slug' => get_sub_field('acfe_form_term_map_slug'), |
| 186 | 'taxonomy' => get_sub_field('acfe_form_term_map_taxonomy'), |
| 187 | 'parent' => get_sub_field('acfe_form_term_map_parent'), |
| 188 | 'description' => get_sub_field('acfe_form_term_map_description'), |
| 189 | ); |
| 190 | |
| 191 | // Fields |
| 192 | $_target = get_sub_field('acfe_form_term_save_target'); |
| 193 | |
| 194 | $_name_group = get_sub_field('acfe_form_term_save_name_group'); |
| 195 | $_name = $_name_group['acfe_form_term_save_name']; |
| 196 | $_name_custom = $_name_group['acfe_form_term_save_name_custom']; |
| 197 | |
| 198 | $_slug_group = get_sub_field('acfe_form_term_save_slug_group'); |
| 199 | $_slug = $_slug_group['acfe_form_term_save_slug']; |
| 200 | $_slug_custom = $_slug_group['acfe_form_term_save_slug_custom']; |
| 201 | |
| 202 | $_taxonomy = get_sub_field('acfe_form_term_save_taxonomy'); |
| 203 | $_parent = get_sub_field('acfe_form_term_save_parent'); |
| 204 | |
| 205 | $_description_group = get_sub_field('acfe_form_term_save_description_group'); |
| 206 | $_description = $_description_group['acfe_form_term_save_description']; |
| 207 | $_description_custom = $_description_group['acfe_form_term_save_description_custom']; |
| 208 | |
| 209 | // args |
| 210 | $args = array(); |
| 211 | |
| 212 | // Insert term |
| 213 | $_term_id = 0; |
| 214 | |
| 215 | // Update user |
| 216 | if($term_action === 'update_term'){ |
| 217 | |
| 218 | // Custom Term ID |
| 219 | $_term_id = $_target; |
| 220 | |
| 221 | // Current Term |
| 222 | if($_target === 'current_term'){ |
| 223 | |
| 224 | if($post_info['type'] === 'term') |
| 225 | $_term_id = $post_info['id']; |
| 226 | |
| 227 | // Invalid Term ID |
| 228 | if(!$_term_id) |
| 229 | return; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | $args['ID'] = $_term_id; |
| 234 | |
| 235 | } |
| 236 | |
| 237 | // Name |
| 238 | if(!empty($map['name'])){ |
| 239 | |
| 240 | $args['name'] = acfe_form_map_field_value($map['name'], $_POST['acf'], $_term_id); |
| 241 | |
| 242 | }elseif($_name === 'custom'){ |
| 243 | |
| 244 | $args['name'] = acfe_form_map_field_value($_name_custom, $_POST['acf'], $_term_id); |
| 245 | |
| 246 | } |
| 247 | |
| 248 | // Slug |
| 249 | if(!empty($map['slug'])){ |
| 250 | |
| 251 | $args['slug'] = acfe_form_map_field_value($map['slug'], $_POST['acf'], $_term_id); |
| 252 | |
| 253 | }elseif($_slug === 'custom'){ |
| 254 | |
| 255 | $args['slug'] = acfe_form_map_field_value($_slug_custom, $_POST['acf'], $_term_id); |
| 256 | |
| 257 | } |
| 258 | |
| 259 | // Taxonomy |
| 260 | if(!empty($map['taxonomy'])){ |
| 261 | |
| 262 | $args['taxonomy'] = acfe_form_map_field_value($map['taxonomy'], $_POST['acf'], $_term_id); |
| 263 | |
| 264 | }elseif(!empty($_taxonomy)){ |
| 265 | |
| 266 | $args['taxonomy'] = $_taxonomy; |
| 267 | |
| 268 | } |
| 269 | |
| 270 | // Parent |
| 271 | if(!empty($map['parent'])){ |
| 272 | |
| 273 | $args['parent'] = acfe_form_map_field_value($map['parent'], $_POST['acf'], $_term_id); |
| 274 | |
| 275 | }elseif(!empty($_parent)){ |
| 276 | |
| 277 | // Custom Term ID |
| 278 | $args['parent'] = $_parent; |
| 279 | |
| 280 | // Current Term |
| 281 | if($_parent === 'current_term'){ |
| 282 | |
| 283 | if($post_info['type'] === 'term') |
| 284 | $args['parent'] = $post_info['id']; |
| 285 | |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | |
| 290 | // Description |
| 291 | if(!empty($map['description'])){ |
| 292 | |
| 293 | $args['description'] = acfe_form_map_field_value($map['description'], $_POST['acf'], $_term_id); |
| 294 | |
| 295 | }elseif($_description === 'custom'){ |
| 296 | |
| 297 | $args['description'] = acfe_form_map_field_value($_description_custom, $_POST['acf'], $_term_id); |
| 298 | |
| 299 | } |
| 300 | |
| 301 | $args = apply_filters('acfe/form/submit/action/term/' . $term_action . '_args', $args, $form); |
| 302 | $args = apply_filters('acfe/form/submit/action/term/' . $term_action . '_args/name=' . $form_name, $args, $form); |
| 303 | $args = apply_filters('acfe/form/submit/action/term/' . $term_action . '_args/id=' . $form_id, $args, $form); |
| 304 | |
| 305 | // Insert Term |
| 306 | if($term_action === 'insert_term'){ |
| 307 | |
| 308 | if(!isset($args['name']) || !isset($args['taxonomy'])){ |
| 309 | |
| 310 | $args = false; |
| 311 | |
| 312 | } |
| 313 | |
| 314 | } |
| 315 | |
| 316 | if($args === false) |
| 317 | return; |
| 318 | |
| 319 | // Insert Term |
| 320 | if($term_action === 'insert_term'){ |
| 321 | |
| 322 | $_insert_term = wp_insert_term($args['name'], $args['taxonomy'], $args); |
| 323 | |
| 324 | } |
| 325 | |
| 326 | // Update Term |
| 327 | elseif($term_action === 'update_term'){ |
| 328 | |
| 329 | $_insert_term = wp_update_term($args['ID'], $args['taxonomy'], $args); |
| 330 | |
| 331 | } |
| 332 | |
| 333 | // Term Error |
| 334 | if(is_wp_error($_insert_term)) |
| 335 | return; |
| 336 | |
| 337 | $_term_id = $_insert_term['term_id']; |
| 338 | |
| 339 | do_action('acfe/form/submit/action/term/' . $term_action, $form, $_term_id, $args); |
| 340 | do_action('acfe/form/submit/action/term/' . $term_action . '/name=' . $form_name, $form, $_term_id, $args); |
| 341 | do_action('acfe/form/submit/action/term/' . $term_action . '/id=' . $form_id, $form, $_term_id, $args); |
| 342 | |
| 343 | // Meta save |
| 344 | $save_meta = get_sub_field('acfe_form_term_save_meta'); |
| 345 | |
| 346 | if(!empty($save_meta)){ |
| 347 | |
| 348 | $data = acfe_form_filter_meta($save_meta, $_POST['acf']); |
| 349 | |
| 350 | if(!empty($data)){ |
| 351 | |
| 352 | // Backup original acf post data |
| 353 | $acf = $_POST['acf']; |
| 354 | |
| 355 | // Save meta fields |
| 356 | acf_save_post('term_' . $_term_id, $data); |
| 357 | |
| 358 | // Restore original acf post data |
| 359 | $_POST['acf'] = $acf; |
| 360 | |
| 361 | } |
| 362 | |
| 363 | } |
| 364 | |
| 365 | } |
| 366 | |
| 367 | } |
| 368 | |
| 369 | new acfe_form_term(); |
| 370 | |
| 371 | endif; |