everything_fields.php
| 1 | <?php |
| 2 | |
| 3 | /*-------------------------------------------------------------------------- |
| 4 | * |
| 5 | * Everything_fields |
| 6 | * |
| 7 | * @author Elliot Condon |
| 8 | * @since 3.1.8 |
| 9 | * |
| 10 | *-------------------------------------------------------------------------*/ |
| 11 | |
| 12 | |
| 13 | class acf_everything_fields |
| 14 | { |
| 15 | |
| 16 | var $parent; |
| 17 | var $dir; |
| 18 | var $data; |
| 19 | |
| 20 | /*-------------------------------------------------------------------------------------- |
| 21 | * |
| 22 | * Everything_fields |
| 23 | * |
| 24 | * @author Elliot Condon |
| 25 | * @since 3.1.8 |
| 26 | * |
| 27 | *-------------------------------------------------------------------------------------*/ |
| 28 | |
| 29 | function __construct($parent) |
| 30 | { |
| 31 | // vars |
| 32 | $this->parent = $parent; |
| 33 | $this->dir = $parent->dir; |
| 34 | |
| 35 | |
| 36 | // data for passing variables |
| 37 | $this->data = array( |
| 38 | 'page_id' => '', // a string used to load values |
| 39 | 'metabox_ids' => array(), |
| 40 | 'page_type' => '', // taxonomy / user / media |
| 41 | 'page_action' => '', // add / edit |
| 42 | 'option_name' => '', // key used to find value in wp_options table. eg: user_1, category_4 |
| 43 | ); |
| 44 | |
| 45 | |
| 46 | // actions |
| 47 | add_action('admin_menu', array($this,'admin_menu')); |
| 48 | add_action('wp_ajax_acf_everything_fields', array($this, 'acf_everything_fields')); |
| 49 | |
| 50 | |
| 51 | // save |
| 52 | add_action('create_term', array($this, 'save_taxonomy')); |
| 53 | add_action('edited_term', array($this, 'save_taxonomy')); |
| 54 | |
| 55 | add_action('edit_user_profile_update', array($this, 'save_user')); |
| 56 | add_action('personal_options_update', array($this, 'save_user')); |
| 57 | add_action('user_register', array($this, 'save_user')); |
| 58 | |
| 59 | |
| 60 | add_filter("attachment_fields_to_save", array($this, 'save_attachment'), null , 2); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /*-------------------------------------------------------------------------------------- |
| 66 | * |
| 67 | * admin_menu |
| 68 | * |
| 69 | * @author Elliot Condon |
| 70 | * @since 3.1.8 |
| 71 | * |
| 72 | *-------------------------------------------------------------------------------------*/ |
| 73 | |
| 74 | function admin_menu() |
| 75 | { |
| 76 | |
| 77 | global $pagenow; |
| 78 | |
| 79 | // we dont want to waste php memory, only check for pages we care about |
| 80 | if( !in_array( $pagenow, array( 'edit-tags.php', 'profile.php', 'user-new.php', 'user-edit.php', 'media.php' ) ) ) |
| 81 | { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | // set page type |
| 87 | $options = array(); |
| 88 | |
| 89 | if( $pagenow == "edit-tags.php" && isset($_GET['taxonomy']) ) |
| 90 | { |
| 91 | |
| 92 | $this->data['page_type'] = "taxonomy"; |
| 93 | $options['ef_taxonomy'] = $_GET['taxonomy']; |
| 94 | |
| 95 | $this->data['page_action'] = "add"; |
| 96 | $this->data['option_name'] = ""; |
| 97 | |
| 98 | if(isset($_GET['action']) && $_GET['action'] == "edit") |
| 99 | { |
| 100 | $this->data['page_action'] = "edit"; |
| 101 | $this->data['option_name'] = $_GET['taxonomy'] . "_" . $_GET['tag_ID']; |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | elseif( $pagenow == "profile.php" ) |
| 106 | { |
| 107 | |
| 108 | $this->data['page_type'] = "user"; |
| 109 | $options['ef_user'] = get_current_user_id(); |
| 110 | |
| 111 | $this->data['page_action'] = "edit"; |
| 112 | $this->data['option_name'] = "user_" . get_current_user_id(); |
| 113 | |
| 114 | } |
| 115 | elseif( $pagenow == "user-edit.php" && isset($_GET['user_id']) ) |
| 116 | { |
| 117 | |
| 118 | $this->data['page_type'] = "user"; |
| 119 | $options['ef_user'] = $_GET['user_id']; |
| 120 | |
| 121 | $this->data['page_action'] = "edit"; |
| 122 | $this->data['option_name'] = "user_" . $_GET['user_id']; |
| 123 | |
| 124 | } |
| 125 | elseif( $pagenow == "user-new.php" ) |
| 126 | { |
| 127 | $this->data['page_type'] = "user"; |
| 128 | $options['ef_user'] ='all'; |
| 129 | |
| 130 | $this->data['page_action'] = "add"; |
| 131 | $this->data['option_name'] = ""; |
| 132 | |
| 133 | } |
| 134 | elseif( $pagenow == "media.php" ) |
| 135 | { |
| 136 | |
| 137 | $this->data['page_type'] = "media"; |
| 138 | $options['ef_media'] = 'all'; |
| 139 | |
| 140 | $this->data['page_action'] = "add"; |
| 141 | $this->data['option_name'] = ""; |
| 142 | |
| 143 | if(isset($_GET['attachment_id'])) |
| 144 | { |
| 145 | $this->data['page_action'] = "edit"; |
| 146 | $this->data['option_name'] = $_GET['attachment_id']; |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | |
| 152 | // find metabox id's for this page |
| 153 | $this->data['metabox_ids'] = $this->parent->get_input_metabox_ids( $options , false ); |
| 154 | |
| 155 | |
| 156 | // dont continue if no ids were found |
| 157 | if(empty( $this->data['metabox_ids'] )) |
| 158 | { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | // some fields require js + css |
| 164 | do_action('acf_print_scripts-input'); |
| 165 | do_action('acf_print_styles-input'); |
| 166 | |
| 167 | |
| 168 | // Add admin head |
| 169 | add_action('admin_head-'.$pagenow, array($this,'admin_head')); |
| 170 | //add_action('admin_footer-'.$pagenow, array($this,'admin_footer')); |
| 171 | |
| 172 | |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /*-------------------------------------------------------------------------------------- |
| 177 | * |
| 178 | * admin_head |
| 179 | * |
| 180 | * @author Elliot Condon |
| 181 | * @since 3.1.8 |
| 182 | * |
| 183 | *-------------------------------------------------------------------------------------*/ |
| 184 | |
| 185 | function admin_head() |
| 186 | { |
| 187 | global $pagenow; |
| 188 | |
| 189 | |
| 190 | // Style |
| 191 | echo '<link rel="stylesheet" type="text/css" href="'.$this->parent->dir.'/css/global.css?ver=' . $this->parent->version . '" />'; |
| 192 | echo '<link rel="stylesheet" type="text/css" href="'.$this->parent->dir.'/css/input.css?ver=' . $this->parent->version . '" />'; |
| 193 | |
| 194 | |
| 195 | // Javascript |
| 196 | echo '<script type="text/javascript" src="'.$this->parent->dir.'/js/input-actions.js?ver=' . $this->parent->version . '" ></script>'; |
| 197 | echo '<script type="text/javascript">acf.post_id = 0;</script>'; |
| 198 | |
| 199 | |
| 200 | // add user js + css |
| 201 | do_action('acf_head-input'); |
| 202 | |
| 203 | |
| 204 | ?> |
| 205 | <script type="text/javascript"> |
| 206 | (function($){ |
| 207 | |
| 208 | acf.data = { |
| 209 | action : 'acf_everything_fields', |
| 210 | metabox_ids : '<?php echo implode( ',', $this->data['metabox_ids'] ); ?>', |
| 211 | page_type : '<?php echo $this->data['page_type']; ?>', |
| 212 | page_action : '<?php echo $this->data['page_action']; ?>', |
| 213 | option_name : '<?php echo $this->data['option_name']; ?>' |
| 214 | }; |
| 215 | |
| 216 | $(document).ready(function(){ |
| 217 | |
| 218 | $.ajax({ |
| 219 | url: ajaxurl, |
| 220 | data: acf.data, |
| 221 | type: 'post', |
| 222 | dataType: 'html', |
| 223 | success: function(html){ |
| 224 | <?php |
| 225 | |
| 226 | if($this->data['page_type'] == "user") |
| 227 | { |
| 228 | if($this->data['page_action'] == "add") |
| 229 | { |
| 230 | echo "$('#createuser > table.form-table > tbody').append( html );"; |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | echo "$('#your-profile > p.submit').before( html );"; |
| 235 | } |
| 236 | } |
| 237 | elseif($this->data['page_type'] == "taxonomy") |
| 238 | { |
| 239 | if($this->data['page_action'] == "add") |
| 240 | { |
| 241 | echo "$('#addtag > p.submit').before( html );"; |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | echo "$('#edittag > p.submit').before( html );"; |
| 246 | } |
| 247 | } |
| 248 | elseif($this->data['page_type'] == "media") |
| 249 | { |
| 250 | if($this->data['page_action'] == "add") |
| 251 | { |
| 252 | echo "$('#addtag > p.submit').before( html );"; |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | echo "$('#media-single-form table tbody tr.submit').before( html );"; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | echo "setTimeout( function(){ $(document).trigger('acf/setup_fields', $('#wpbody') ); }, 200);"; |
| 261 | |
| 262 | ?> |
| 263 | } |
| 264 | }); |
| 265 | |
| 266 | }); |
| 267 | })(jQuery); |
| 268 | </script> |
| 269 | <?php |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | /*-------------------------------------------------------------------------------------- |
| 275 | * |
| 276 | * save_taxonomy |
| 277 | * |
| 278 | * @author Elliot Condon |
| 279 | * @since 3.1.8 |
| 280 | * |
| 281 | *-------------------------------------------------------------------------------------*/ |
| 282 | |
| 283 | function save_taxonomy( $term_id ) |
| 284 | { |
| 285 | // validate |
| 286 | if( !isset( $_POST['fields'] ) ) |
| 287 | { |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | // options name to save against |
| 293 | $option_name = $_POST['taxonomy'] . '_' . $term_id; |
| 294 | |
| 295 | |
| 296 | // save fields |
| 297 | $fields = $_POST['fields']; |
| 298 | |
| 299 | foreach($fields as $key => $value) |
| 300 | { |
| 301 | // get field |
| 302 | $field = $this->parent->get_acf_field($key); |
| 303 | |
| 304 | $this->parent->update_value( $option_name , $field, $value ); |
| 305 | } |
| 306 | |
| 307 | } |
| 308 | |
| 309 | |
| 310 | /*-------------------------------------------------------------------------------------- |
| 311 | * |
| 312 | * profile_save |
| 313 | * |
| 314 | * @author Elliot Condon |
| 315 | * @since 3.1.8 |
| 316 | * |
| 317 | *-------------------------------------------------------------------------------------*/ |
| 318 | |
| 319 | function save_user( $user_id ) |
| 320 | { |
| 321 | |
| 322 | // validate |
| 323 | if( !isset( $_POST['fields'] ) ) |
| 324 | { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | // options name to save against |
| 330 | $option_name = 'user_' . $user_id; |
| 331 | |
| 332 | |
| 333 | // save fields |
| 334 | $fields = $_POST['fields']; |
| 335 | |
| 336 | foreach($fields as $key => $value) |
| 337 | { |
| 338 | // get field |
| 339 | $field = $this->parent->get_acf_field($key); |
| 340 | |
| 341 | $this->parent->update_value( $option_name , $field, $value ); |
| 342 | } |
| 343 | |
| 344 | |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /*-------------------------------------------------------------------------------------- |
| 349 | * |
| 350 | * save_attachment |
| 351 | * |
| 352 | * @author Elliot Condon |
| 353 | * @since 3.1.8 |
| 354 | * |
| 355 | *-------------------------------------------------------------------------------------*/ |
| 356 | |
| 357 | function save_attachment( $post, $attachment ) |
| 358 | { |
| 359 | |
| 360 | // validate |
| 361 | if( !isset( $_POST['fields'] ) ) |
| 362 | { |
| 363 | return $post; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | // save fields |
| 368 | $fields = $_POST['fields']; |
| 369 | |
| 370 | foreach($fields as $key => $value) |
| 371 | { |
| 372 | // get field |
| 373 | $field = $this->parent->get_acf_field($key); |
| 374 | |
| 375 | $this->parent->update_value( $post['ID'] , $field, $value ); |
| 376 | } |
| 377 | |
| 378 | return $post; |
| 379 | |
| 380 | |
| 381 | } |
| 382 | |
| 383 | |
| 384 | /*-------------------------------------------------------------------------------------- |
| 385 | * |
| 386 | * acf_everything_fields |
| 387 | * |
| 388 | * @description Ajax call that renders the html needed for the page |
| 389 | * @author Elliot Condon |
| 390 | * @since 3.1.8 |
| 391 | * |
| 392 | *-------------------------------------------------------------------------------------*/ |
| 393 | |
| 394 | function acf_everything_fields() |
| 395 | { |
| 396 | // defaults |
| 397 | $defaults = array( |
| 398 | 'metabox_ids' => '', |
| 399 | 'page_type' => '', |
| 400 | 'page_action' => '', |
| 401 | 'option_name' => '', |
| 402 | ); |
| 403 | |
| 404 | |
| 405 | // load post options |
| 406 | $options = array_merge($defaults, $_POST); |
| 407 | |
| 408 | |
| 409 | // metabox ids is a string with commas |
| 410 | $options['metabox_ids'] = explode( ',', $options['metabox_ids'] ); |
| 411 | |
| 412 | |
| 413 | // get acfs |
| 414 | $acfs = $this->parent->get_field_groups(); |
| 415 | |
| 416 | |
| 417 | if($acfs) |
| 418 | { |
| 419 | foreach($acfs as $acf) |
| 420 | { |
| 421 | // only add the chosen field groups |
| 422 | if( !in_array( $acf['id'], $options['metabox_ids'] ) ) |
| 423 | { |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | // needs fields |
| 429 | if(!$acf['fields']) |
| 430 | { |
| 431 | continue; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | // title |
| 436 | if( $options['page_action'] == "edit" && $options['page_type'] != "media") |
| 437 | { |
| 438 | echo '<h3>' . get_the_title( $acf['id'] ) . '</h3>'; |
| 439 | echo '<table class="form-table">'; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | // render |
| 444 | foreach($acf['fields'] as $field) |
| 445 | { |
| 446 | |
| 447 | // if they didn't select a type, skip this field |
| 448 | if($field['type'] == 'null') continue; |
| 449 | |
| 450 | // set value |
| 451 | $field['value'] = $this->parent->get_value( $options['option_name'], $field); |
| 452 | |
| 453 | // required |
| 454 | if(!isset($field['required'])) |
| 455 | { |
| 456 | $field['required'] = "0"; |
| 457 | } |
| 458 | |
| 459 | $required_class = ""; |
| 460 | $required_label = ""; |
| 461 | |
| 462 | if($field['required'] == "1") |
| 463 | { |
| 464 | $required_class = ' required'; |
| 465 | $required_label = ' <span class="required">*</span>'; |
| 466 | } |
| 467 | |
| 468 | if( $options['page_type'] == "taxonomy" && $options['page_action'] == "add") |
| 469 | { |
| 470 | echo '<div id="acf-' . $field['name'] . '" class="form-field' . $required_class . '">'; |
| 471 | echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>'; |
| 472 | $field['name'] = 'fields[' . $field['key'] . ']'; |
| 473 | $this->parent->create_field($field); |
| 474 | if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>'; |
| 475 | echo '</div>'; |
| 476 | } |
| 477 | else |
| 478 | { |
| 479 | echo '<tr id="acf-' . $field['name'] . '" class="field form-field' . $required_class . '">'; |
| 480 | echo '<th valign="top" scope="row"><label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label></th>'; |
| 481 | echo '<td>'; |
| 482 | $field['name'] = 'fields[' . $field['key'] . ']'; |
| 483 | $this->parent->create_field($field); |
| 484 | if($field['instructions']) echo '<span class="description">' . $field['instructions'] . '</span>'; |
| 485 | echo '</td>'; |
| 486 | echo '</tr>'; |
| 487 | |
| 488 | } |
| 489 | |
| 490 | |
| 491 | } |
| 492 | // foreach($fields as $field) |
| 493 | |
| 494 | |
| 495 | // footer |
| 496 | if( $options['page_action'] == "edit" && $options['page_type'] != "media") |
| 497 | { |
| 498 | echo '</table>'; |
| 499 | } |
| 500 | } |
| 501 | // foreach($acfs as $acf) |
| 502 | } |
| 503 | // if($acfs) |
| 504 | |
| 505 | // exit for ajax |
| 506 | die(); |
| 507 | |
| 508 | } |
| 509 | |
| 510 | |
| 511 | } |
| 512 | |
| 513 | ?> |