input.php
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 | |
| 5 | if( ! class_exists('acf_input') ) : |
| 6 | |
| 7 | class acf_input { |
| 8 | |
| 9 | |
| 10 | /* |
| 11 | * __construct |
| 12 | * |
| 13 | * This function will setup the class functionality |
| 14 | * |
| 15 | * @type function |
| 16 | * @date 5/03/2014 |
| 17 | * @since 5.0.0 |
| 18 | * |
| 19 | * @param n/a |
| 20 | * @return n/a |
| 21 | */ |
| 22 | |
| 23 | function __construct() { |
| 24 | |
| 25 | // vars |
| 26 | $this->admin_enqueue_scripts = 'admin_enqueue_scripts'; |
| 27 | $this->admin_head = 'admin_head'; |
| 28 | $this->admin_footer = 'admin_footer'; |
| 29 | $this->enqueued = false; |
| 30 | $this->data = array(); |
| 31 | |
| 32 | |
| 33 | // actions |
| 34 | add_action('acf/save_post', array($this, 'save_post'), 10, 1); |
| 35 | |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * get_data |
| 41 | * |
| 42 | * This function will return form data |
| 43 | * |
| 44 | * @type function |
| 45 | * @date 4/03/2016 |
| 46 | * @since 5.3.2 |
| 47 | * |
| 48 | * @param $key (mixed) |
| 49 | * @return (mixed) |
| 50 | */ |
| 51 | |
| 52 | function get_data( $key = false ) { |
| 53 | |
| 54 | // vars |
| 55 | $data = $this->data; |
| 56 | |
| 57 | |
| 58 | // key |
| 59 | if( $key && isset($data[ $key ]) ) { |
| 60 | |
| 61 | $data = $data[ $key ]; |
| 62 | |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // return |
| 67 | return $data; |
| 68 | |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | * set_data |
| 74 | * |
| 75 | * This function will se the form data |
| 76 | * |
| 77 | * @type function |
| 78 | * @date 4/03/2016 |
| 79 | * @since 5.3.2 |
| 80 | * |
| 81 | * @param $data (array) |
| 82 | * @return (array) |
| 83 | */ |
| 84 | |
| 85 | function set_data( $data ) { |
| 86 | |
| 87 | // defaults |
| 88 | $data = acf_parse_args($data, array( |
| 89 | 'post_id' => 0, // ID of current post |
| 90 | 'nonce' => 'post', // nonce used for $_POST validation |
| 91 | 'validation' => 1, // runs AJAX validation |
| 92 | 'ajax' => 0, // fetches new field groups via AJAX |
| 93 | 'changed' => 0, |
| 94 | )); |
| 95 | |
| 96 | |
| 97 | // update |
| 98 | $this->data = $data; |
| 99 | |
| 100 | |
| 101 | // enqueue uploader if page allows AJAX fields to appear |
| 102 | if( $data['ajax'] ) { |
| 103 | |
| 104 | add_action($this->admin_footer, 'acf_enqueue_uploader', 1); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | |
| 109 | // return |
| 110 | return $data; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /* |
| 116 | * enqueue |
| 117 | * |
| 118 | * This function will determin the actions to use for different pages |
| 119 | * |
| 120 | * @type function |
| 121 | * @date 13/01/2016 |
| 122 | * @since 5.3.2 |
| 123 | * |
| 124 | * @param n/a |
| 125 | * @return n/a |
| 126 | */ |
| 127 | |
| 128 | function enqueue() { |
| 129 | |
| 130 | // bail ealry if already enqueued |
| 131 | if( $this->enqueued ) return; |
| 132 | |
| 133 | |
| 134 | // update setting |
| 135 | $this->enqueued = true; |
| 136 | |
| 137 | |
| 138 | // global |
| 139 | global $pagenow; |
| 140 | |
| 141 | |
| 142 | // determine action hooks |
| 143 | if( $pagenow == 'customize.php' ) { |
| 144 | |
| 145 | $this->admin_head = 'customize_controls_print_scripts'; |
| 146 | $this->admin_footer = 'customize_controls_print_footer_scripts'; |
| 147 | |
| 148 | } elseif( $pagenow == 'wp-login.php' ) { |
| 149 | |
| 150 | $this->admin_enqueue_scripts = 'login_enqueue_scripts'; |
| 151 | $this->admin_head = 'login_head'; |
| 152 | $this->admin_footer = 'login_footer'; |
| 153 | |
| 154 | } elseif( !is_admin() ) { |
| 155 | |
| 156 | $this->admin_enqueue_scripts = 'wp_enqueue_scripts'; |
| 157 | $this->admin_head = 'wp_head'; |
| 158 | $this->admin_footer = 'wp_footer'; |
| 159 | |
| 160 | } |
| 161 | |
| 162 | |
| 163 | // actions |
| 164 | acf_maybe_add_action($this->admin_enqueue_scripts, array($this, 'admin_enqueue_scripts'), 20 ); |
| 165 | acf_maybe_add_action($this->admin_head, array($this, 'admin_head'), 20 ); |
| 166 | acf_maybe_add_action($this->admin_footer, array($this, 'admin_footer'), 20 ); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /* |
| 172 | * admin_enqueue_scripts |
| 173 | * |
| 174 | * The acf input screen admin_enqueue_scripts |
| 175 | * |
| 176 | * @type function |
| 177 | * @date 4/03/2016 |
| 178 | * @since 5.3.2 |
| 179 | * |
| 180 | * @param n/a |
| 181 | * @return n/a |
| 182 | */ |
| 183 | |
| 184 | function admin_enqueue_scripts() { |
| 185 | |
| 186 | // scripts |
| 187 | wp_enqueue_script('acf-input'); |
| 188 | |
| 189 | |
| 190 | // styles |
| 191 | wp_enqueue_style('acf-input'); |
| 192 | |
| 193 | |
| 194 | // do action |
| 195 | do_action('acf/input/admin_enqueue_scripts'); |
| 196 | |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* |
| 201 | * admin_head |
| 202 | * |
| 203 | * The acf input screen admin_head |
| 204 | * |
| 205 | * @type function |
| 206 | * @date 4/03/2016 |
| 207 | * @since 5.3.2 |
| 208 | * |
| 209 | * @param n/a |
| 210 | * @return n/a |
| 211 | */ |
| 212 | |
| 213 | function admin_head() { |
| 214 | |
| 215 | // do action |
| 216 | do_action('acf/input/admin_head'); |
| 217 | |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /* |
| 222 | * admin_footer |
| 223 | * |
| 224 | * The acf input screen admin_footer |
| 225 | * |
| 226 | * @type function |
| 227 | * @date 4/03/2016 |
| 228 | * @since 5.3.2 |
| 229 | * |
| 230 | * @param n/a |
| 231 | * @return n/a |
| 232 | */ |
| 233 | |
| 234 | function admin_footer() { |
| 235 | |
| 236 | // global |
| 237 | global $wp_version; |
| 238 | |
| 239 | |
| 240 | // options |
| 241 | $o = array( |
| 242 | 'post_id' => acf_get_form_data('post_id'), |
| 243 | 'nonce' => wp_create_nonce( 'acf_nonce' ), |
| 244 | 'admin_url' => admin_url(), |
| 245 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 246 | 'ajax' => acf_get_form_data('ajax'), |
| 247 | 'validation' => acf_get_form_data('validation'), |
| 248 | 'wp_version' => $wp_version, |
| 249 | 'acf_version' => acf_get_setting('version'), |
| 250 | 'browser' => acf_get_browser(), |
| 251 | 'locale' => get_locale(), |
| 252 | 'rtl' => is_rtl() |
| 253 | ); |
| 254 | |
| 255 | |
| 256 | // l10n |
| 257 | $l10n = apply_filters( 'acf/input/admin_l10n', array( |
| 258 | 'unload' => __('The changes you made will be lost if you navigate away from this page','acf'), |
| 259 | 'expand_details' => __('Expand Details','acf'), |
| 260 | 'collapse_details' => __('Collapse Details','acf'), |
| 261 | 'validation_successful' => __('Validation successful', 'acf'), |
| 262 | 'validation_failed' => __('Validation failed', 'acf'), |
| 263 | 'validation_failed_1' => __('1 field requires attention', 'acf'), |
| 264 | 'validation_failed_2' => __('%d fields require attention', 'acf'), |
| 265 | 'restricted' => __('Restricted','acf'), |
| 266 | 'are_you_sure' => __('Are you sure?','acf'), |
| 267 | 'yes' => __('Yes','acf'), |
| 268 | 'no' => __('No','acf'), |
| 269 | 'remove' => __('Remove','acf'), |
| 270 | 'cancel' => __('Cancel','acf') |
| 271 | )); |
| 272 | |
| 273 | |
| 274 | ?> |
| 275 | <script type="text/javascript"> |
| 276 | var acf = acf || null; |
| 277 | if( acf ) { |
| 278 | |
| 279 | acf.o = <?php echo json_encode($o); ?>; |
| 280 | acf.l10n = <?php echo json_encode($l10n); ?>; |
| 281 | <?php do_action('acf/input/admin_footer_js'); ?> |
| 282 | |
| 283 | } |
| 284 | </script> |
| 285 | <?php |
| 286 | |
| 287 | do_action('acf/input/admin_footer'); |
| 288 | |
| 289 | ?> |
| 290 | <script type="text/javascript"> |
| 291 | if( acf ) acf.do_action('prepare'); |
| 292 | </script> |
| 293 | <?php |
| 294 | |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /* |
| 299 | * save_post |
| 300 | * |
| 301 | * This function will save the $_POST data |
| 302 | * |
| 303 | * @type function |
| 304 | * @date 24/10/2014 |
| 305 | * @since 5.0.9 |
| 306 | * |
| 307 | * @param $post_id (int) |
| 308 | * @return n/a |
| 309 | */ |
| 310 | |
| 311 | function save_post( $post_id ) { |
| 312 | |
| 313 | // bail early if empty |
| 314 | // - post data may have be modified |
| 315 | if( empty($_POST['acf']) ) return; |
| 316 | |
| 317 | |
| 318 | // loop |
| 319 | foreach( $_POST['acf'] as $k => $v ) { |
| 320 | |
| 321 | // get field |
| 322 | $field = acf_get_field( $k ); |
| 323 | |
| 324 | |
| 325 | // continue if no field |
| 326 | if( !$field ) continue; |
| 327 | |
| 328 | |
| 329 | // update |
| 330 | acf_update_value( $v, $post_id, $field ); |
| 331 | |
| 332 | } |
| 333 | |
| 334 | } |
| 335 | |
| 336 | } |
| 337 | |
| 338 | // initialize |
| 339 | acf()->input = new acf_input(); |
| 340 | |
| 341 | endif; // class_exists check |
| 342 | |
| 343 | |
| 344 | |
| 345 | /* |
| 346 | * acf_enqueue_scripts |
| 347 | * |
| 348 | * alias of acf()->form->enqueue() |
| 349 | * |
| 350 | * @type function |
| 351 | * @date 6/10/13 |
| 352 | * @since 5.0.0 |
| 353 | * |
| 354 | * @param n/a |
| 355 | * @return n/a |
| 356 | */ |
| 357 | |
| 358 | function acf_enqueue_scripts() { |
| 359 | |
| 360 | return acf()->input->enqueue(); |
| 361 | |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /* |
| 366 | * acf_get_form_data |
| 367 | * |
| 368 | * alias of acf()->form->get_data() |
| 369 | * |
| 370 | * @type function |
| 371 | * @date 6/10/13 |
| 372 | * @since 5.0.0 |
| 373 | * |
| 374 | * @param n/a |
| 375 | * @return n/a |
| 376 | */ |
| 377 | |
| 378 | function acf_get_form_data( $key = false ) { |
| 379 | |
| 380 | return acf()->input->get_data( $key ); |
| 381 | |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* |
| 386 | * acf_set_form_data |
| 387 | * |
| 388 | * alias of acf()->form->set_data() |
| 389 | * |
| 390 | * @type function |
| 391 | * @date 6/10/13 |
| 392 | * @since 5.0.0 |
| 393 | * |
| 394 | * @param n/a |
| 395 | * @return n/a |
| 396 | */ |
| 397 | |
| 398 | function acf_set_form_data( $data = array() ) { |
| 399 | |
| 400 | return acf()->input->set_data( $data ); |
| 401 | |
| 402 | } |
| 403 | |
| 404 | |
| 405 | /* |
| 406 | * acf_enqueue_uploader |
| 407 | * |
| 408 | * This function will render a WP WYSIWYG and enqueue media |
| 409 | * |
| 410 | * @type function |
| 411 | * @date 27/10/2014 |
| 412 | * @since 5.0.9 |
| 413 | * |
| 414 | * @param n/a |
| 415 | * @return n/a |
| 416 | */ |
| 417 | |
| 418 | function acf_enqueue_uploader() { |
| 419 | |
| 420 | // bail early if doing ajax |
| 421 | if( acf_is_ajax() ) return; |
| 422 | |
| 423 | |
| 424 | // bail ealry if already run |
| 425 | if( acf_has_done('enqueue_uploader') ) return; |
| 426 | |
| 427 | |
| 428 | // enqueue media if user can upload |
| 429 | if( current_user_can('upload_files') ) { |
| 430 | |
| 431 | wp_enqueue_media(); |
| 432 | |
| 433 | } |
| 434 | |
| 435 | |
| 436 | // create dummy editor |
| 437 | ?><div id="acf-hidden-wp-editor" class="acf-hidden"><?php wp_editor( '', 'acf_content' ); ?></div><?php |
| 438 | |
| 439 | } |
| 440 | |
| 441 | |
| 442 | /* |
| 443 | * acf_form_data |
| 444 | * |
| 445 | * description |
| 446 | * |
| 447 | * @type function |
| 448 | * @date 15/10/13 |
| 449 | * @since 5.0.0 |
| 450 | * |
| 451 | * @param $post_id (int) |
| 452 | * @return $post_id (int) |
| 453 | */ |
| 454 | |
| 455 | function acf_form_data( $args = array() ) { |
| 456 | |
| 457 | // make sure scripts and styles have been included |
| 458 | // case: front end bbPress edit user |
| 459 | acf_enqueue_scripts(); |
| 460 | |
| 461 | |
| 462 | // set form data |
| 463 | $args = acf_set_form_data( $args ); |
| 464 | |
| 465 | |
| 466 | // hidden inputs |
| 467 | $inputs = $args; |
| 468 | $inputs['nonce'] = wp_create_nonce($inputs['nonce']); |
| 469 | |
| 470 | ?> |
| 471 | <div id="acf-form-data" class="acf-hidden"> |
| 472 | <?php foreach( $inputs as $k => $v ): ?> |
| 473 | <input type="hidden" id="_acf_<?php echo esc_attr($k); ?>" name="_acf_<?php echo esc_attr($k); ?>" value="<?php echo esc_attr($v); ?>" /> |
| 474 | <?php endforeach; ?> |
| 475 | <?php do_action('acf/input/form_data', $args); ?> |
| 476 | </div> |
| 477 | <?php |
| 478 | |
| 479 | } |
| 480 | |
| 481 | |
| 482 | /* |
| 483 | * acf_save_post |
| 484 | * |
| 485 | * description |
| 486 | * |
| 487 | * @type function |
| 488 | * @date 8/10/13 |
| 489 | * @since 5.0.0 |
| 490 | * |
| 491 | * @param $post_id (int) |
| 492 | * @return $post_id (int) |
| 493 | */ |
| 494 | |
| 495 | function acf_save_post( $post_id = 0, $values = null ) { |
| 496 | |
| 497 | // override $_POST |
| 498 | if( $values !== null ) { |
| 499 | $_POST['acf'] = $values; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | // bail early if no values |
| 504 | if( empty($_POST['acf']) ) return false; |
| 505 | |
| 506 | |
| 507 | // set form data |
| 508 | acf_set_form_data(array( |
| 509 | 'post_id' => $post_id |
| 510 | )); |
| 511 | |
| 512 | |
| 513 | // hook for 3rd party customization |
| 514 | do_action('acf/save_post', $post_id); |
| 515 | |
| 516 | |
| 517 | // return |
| 518 | return true; |
| 519 | |
| 520 | } |
| 521 |