assets.php
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 | |
| 5 | if( ! class_exists('ACF_Assets') ) : |
| 6 | |
| 7 | class ACF_Assets { |
| 8 | |
| 9 | /** @var array Storage for translations */ |
| 10 | var $text = array(); |
| 11 | |
| 12 | /** @var array Storage for data */ |
| 13 | var $data = array(); |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * __construct |
| 18 | * |
| 19 | * description |
| 20 | * |
| 21 | * @date 10/4/18 |
| 22 | * @since 5.6.9 |
| 23 | * |
| 24 | * @param void |
| 25 | * @return void |
| 26 | */ |
| 27 | |
| 28 | function __construct() { |
| 29 | |
| 30 | // actions |
| 31 | add_action('init', array($this, 'register_scripts')); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * add_text |
| 37 | * |
| 38 | * description |
| 39 | * |
| 40 | * @date 13/4/18 |
| 41 | * @since 5.6.9 |
| 42 | * |
| 43 | * @param type $var Description. Default. |
| 44 | * @return type Description. |
| 45 | */ |
| 46 | |
| 47 | function add_text( $text ) { |
| 48 | foreach( (array) $text as $k => $v ) { |
| 49 | $this->text[ $k ] = $v; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * add_data |
| 56 | * |
| 57 | * description |
| 58 | * |
| 59 | * @date 13/4/18 |
| 60 | * @since 5.6.9 |
| 61 | * |
| 62 | * @param type $var Description. Default. |
| 63 | * @return type Description. |
| 64 | */ |
| 65 | |
| 66 | function add_data( $data ) { |
| 67 | foreach( (array) $data as $k => $v ) { |
| 68 | $this->data[ $k ] = $v; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * register_scripts |
| 75 | * |
| 76 | * description |
| 77 | * |
| 78 | * @date 13/4/18 |
| 79 | * @since 5.6.9 |
| 80 | * |
| 81 | * @param type $var Description. Default. |
| 82 | * @return type Description. |
| 83 | */ |
| 84 | |
| 85 | function register_scripts() { |
| 86 | |
| 87 | // vars |
| 88 | $version = acf_get_setting('version'); |
| 89 | $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
| 90 | |
| 91 | // scripts |
| 92 | wp_register_script('acf-input', acf_get_url("assets/js/acf-input{$min}.js"), array('jquery', 'jquery-ui-sortable', 'jquery-ui-resizable'), $version ); |
| 93 | wp_register_script('acf-field-group', acf_get_url("assets/js/acf-field-group{$min}.js"), array('acf-input'), $version ); |
| 94 | |
| 95 | // styles |
| 96 | wp_register_style('acf-global', acf_get_url('assets/css/acf-global.css'), array(), $version ); |
| 97 | wp_register_style('acf-input', acf_get_url('assets/css/acf-input.css'), array('acf-global'), $version ); |
| 98 | wp_register_style('acf-field-group', acf_get_url('assets/css/acf-field-group.css'), array('acf-input'), $version ); |
| 99 | |
| 100 | // action |
| 101 | do_action('acf/register_scripts', $version, $min); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * enqueue_scripts |
| 107 | * |
| 108 | * Enqueue scripts for input |
| 109 | * |
| 110 | * @date 13/4/18 |
| 111 | * @since 5.6.9 |
| 112 | * |
| 113 | * @param type $var Description. Default. |
| 114 | * @return type Description. |
| 115 | */ |
| 116 | |
| 117 | function enqueue_scripts( $args = array() ) { |
| 118 | |
| 119 | // run only once |
| 120 | if( acf_has_done('enqueue_scripts') ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // defaults |
| 125 | $args = wp_parse_args($args, array( |
| 126 | |
| 127 | // force tinymce editor to be enqueued |
| 128 | 'uploader' => false, |
| 129 | |
| 130 | // priority used for action callbacks, defaults to 20 which runs after defaults |
| 131 | 'priority' => 20, |
| 132 | |
| 133 | // action prefix |
| 134 | 'context' => is_admin() ? 'admin' : 'wp' |
| 135 | )); |
| 136 | |
| 137 | // define actions |
| 138 | $actions = array( |
| 139 | 'admin_enqueue_scripts' => $args['context'] . '_enqueue_scripts', |
| 140 | 'admin_print_scripts' => $args['context'] . '_print_scripts', |
| 141 | 'admin_head' => $args['context'] . '_head', |
| 142 | 'admin_footer' => $args['context'] . '_footer', |
| 143 | 'admin_print_footer_scripts' => $args['context'] . '_print_footer_scripts', |
| 144 | ); |
| 145 | |
| 146 | // fix customizer actions where head and footer are not available |
| 147 | if( $args['context'] == 'customize_controls' ) { |
| 148 | $actions['admin_head'] = $actions['admin_print_scripts']; |
| 149 | $actions['admin_footer'] = $actions['admin_print_footer_scripts']; |
| 150 | } |
| 151 | |
| 152 | // add actions |
| 153 | foreach( $actions as $function => $action ) { |
| 154 | acf_maybe_add_action( $action, array($this, $function), $args['priority'] ); |
| 155 | } |
| 156 | |
| 157 | // enqueue uploader |
| 158 | // WP requires a lot of JS + inline scripes to create the media modal and should be avoioded when possible. |
| 159 | // - priority must be less than 10 to allow WP to enqueue |
| 160 | if( $args['uploader'] ) { |
| 161 | add_action($actions['admin_footer'], 'acf_enqueue_uploader', 5); |
| 162 | } |
| 163 | |
| 164 | // localize text |
| 165 | acf_localize_text(array( |
| 166 | |
| 167 | // unload |
| 168 | 'The changes you made will be lost if you navigate away from this page' => __('The changes you made will be lost if you navigate away from this page', 'acf'), |
| 169 | |
| 170 | // media |
| 171 | 'Select.verb' => _x('Select', 'verb', 'acf'), |
| 172 | 'Edit.verb' => _x('Edit', 'verb', 'acf'), |
| 173 | 'Update.verb' => _x('Update', 'verb', 'acf'), |
| 174 | 'Uploaded to this post' => __('Uploaded to this post', 'acf'), |
| 175 | 'Expand Details' => __('Expand Details', 'acf'), |
| 176 | 'Collapse Details' => __('Collapse Details', 'acf'), |
| 177 | 'Restricted' => __('Restricted', 'acf'), |
| 178 | 'All images' => __('All images', 'acf'), |
| 179 | |
| 180 | // validation |
| 181 | 'Validation successful' => __('Validation successful', 'acf'), |
| 182 | 'Validation failed' => __('Validation failed', 'acf'), |
| 183 | '1 field requires attention' => __('1 field requires attention', 'acf'), |
| 184 | '%d fields require attention' => __('%d fields require attention', 'acf'), |
| 185 | |
| 186 | // tooltip |
| 187 | 'Are you sure?' => __('Are you sure?','acf'), |
| 188 | 'Yes' => __('Yes','acf'), |
| 189 | 'No' => __('No','acf'), |
| 190 | 'Remove' => __('Remove','acf'), |
| 191 | 'Cancel' => __('Cancel','acf'), |
| 192 | |
| 193 | // conditions |
| 194 | 'Has any value' => __('Has any value', 'acf'), |
| 195 | 'Has no value' => __('Has no value', 'acf'), |
| 196 | 'Value is equal to' => __('Value is equal to', 'acf'), |
| 197 | 'Value is not equal to' => __('Value is not equal to', 'acf'), |
| 198 | 'Value matches pattern' => __('Value matches pattern', 'acf'), |
| 199 | 'Value contains' => __('Value contains', 'acf'), |
| 200 | 'Value is greater than' => __('Value is greater than', 'acf'), |
| 201 | 'Value is less than' => __('Value is less than', 'acf'), |
| 202 | 'Selection is greater than' => __('Selection is greater than', 'acf'), |
| 203 | 'Selection is less than' => __('Selection is less than', 'acf'), |
| 204 | |
| 205 | // misc |
| 206 | 'Edit field group' => __('Edit field group', 'acf'), |
| 207 | )); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * admin_enqueue_scripts |
| 213 | * |
| 214 | * description |
| 215 | * |
| 216 | * @date 16/4/18 |
| 217 | * @since 5.6.9 |
| 218 | * |
| 219 | * @param type $var Description. Default. |
| 220 | * @return type Description. |
| 221 | */ |
| 222 | |
| 223 | function admin_enqueue_scripts() { |
| 224 | |
| 225 | // enqueue |
| 226 | wp_enqueue_script('acf-input'); |
| 227 | wp_enqueue_style('acf-input'); |
| 228 | |
| 229 | // vars |
| 230 | $text = array(); |
| 231 | |
| 232 | // actions |
| 233 | do_action('acf/enqueue_scripts'); |
| 234 | do_action('acf/admin_enqueue_scripts'); |
| 235 | do_action('acf/input/admin_enqueue_scripts'); |
| 236 | |
| 237 | // only include translated strings |
| 238 | foreach( $this->text as $k => $v ) { |
| 239 | if( str_replace('.verb', '', $k) !== $v ) { |
| 240 | $text[ $k ] = $v; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // localize text |
| 245 | if( $text ) { |
| 246 | wp_localize_script( 'acf-input', 'acfL10n', $text ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * admin_print_scripts |
| 253 | * |
| 254 | * description |
| 255 | * |
| 256 | * @date 18/4/18 |
| 257 | * @since 5.6.9 |
| 258 | * |
| 259 | * @param type $var Description. Default. |
| 260 | * @return type Description. |
| 261 | */ |
| 262 | |
| 263 | function admin_print_scripts() { |
| 264 | do_action('acf/admin_print_scripts'); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /** |
| 269 | * admin_head |
| 270 | * |
| 271 | * description |
| 272 | * |
| 273 | * @date 16/4/18 |
| 274 | * @since 5.6.9 |
| 275 | * |
| 276 | * @param type $var Description. Default. |
| 277 | * @return type Description. |
| 278 | */ |
| 279 | |
| 280 | function admin_head() { |
| 281 | |
| 282 | // actions |
| 283 | do_action('acf/admin_head'); |
| 284 | do_action('acf/input/admin_head'); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /** |
| 289 | * admin_footer |
| 290 | * |
| 291 | * description |
| 292 | * |
| 293 | * @date 16/4/18 |
| 294 | * @since 5.6.9 |
| 295 | * |
| 296 | * @param type $var Description. Default. |
| 297 | * @return type Description. |
| 298 | */ |
| 299 | |
| 300 | function admin_footer() { |
| 301 | |
| 302 | // global |
| 303 | global $wp_version; |
| 304 | |
| 305 | // get data |
| 306 | $data = wp_parse_args($this->data, array( |
| 307 | 'screen' => acf_get_form_data('screen'), |
| 308 | 'post_id' => acf_get_form_data('post_id'), |
| 309 | 'nonce' => wp_create_nonce( 'acf_nonce' ), |
| 310 | 'admin_url' => admin_url(), |
| 311 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 312 | 'validation' => acf_get_form_data('validation'), |
| 313 | 'wp_version' => $wp_version, |
| 314 | 'acf_version' => acf_get_setting('version'), |
| 315 | 'browser' => acf_get_browser(), |
| 316 | 'locale' => acf_get_locale(), |
| 317 | 'rtl' => is_rtl() |
| 318 | )); |
| 319 | |
| 320 | // get l10n (old) |
| 321 | $l10n = apply_filters( 'acf/input/admin_l10n', array() ); |
| 322 | |
| 323 | // todo: force 'acf-input' script enqueue if not yet included |
| 324 | // - fixes potential timing issue if acf_enqueue_assest() was called during body |
| 325 | |
| 326 | // localize data |
| 327 | ?> |
| 328 | <script type="text/javascript"> |
| 329 | acf.data = <?php echo wp_json_encode($data); ?>; |
| 330 | acf.l10n = <?php echo wp_json_encode($l10n); ?>; |
| 331 | </script> |
| 332 | <?php |
| 333 | |
| 334 | // actions |
| 335 | do_action('acf/admin_footer'); |
| 336 | do_action('acf/input/admin_footer'); |
| 337 | |
| 338 | // trigger prepare |
| 339 | ?> |
| 340 | <script type="text/javascript"> |
| 341 | acf.doAction('prepare'); |
| 342 | </script> |
| 343 | <?php |
| 344 | |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | * admin_print_footer_scripts |
| 350 | * |
| 351 | * description |
| 352 | * |
| 353 | * @date 18/4/18 |
| 354 | * @since 5.6.9 |
| 355 | * |
| 356 | * @param type $var Description. Default. |
| 357 | * @return type Description. |
| 358 | */ |
| 359 | |
| 360 | function admin_print_footer_scripts() { |
| 361 | do_action('acf/admin_print_footer_scripts'); |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * enqueue_uploader |
| 366 | * |
| 367 | * This function will render a WP WYSIWYG and enqueue media |
| 368 | * |
| 369 | * @type function |
| 370 | * @date 27/10/2014 |
| 371 | * @since 5.0.9 |
| 372 | * |
| 373 | * @param n/a |
| 374 | * @return n/a |
| 375 | */ |
| 376 | |
| 377 | function enqueue_uploader() { |
| 378 | |
| 379 | // run only once |
| 380 | if( acf_has_done('enqueue_uploader') ) { |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | // bail early if doing ajax |
| 385 | if( acf_is_ajax() ) { |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | // enqueue media if user can upload |
| 390 | if( current_user_can('upload_files') ) { |
| 391 | wp_enqueue_media(); |
| 392 | } |
| 393 | |
| 394 | // create dummy editor |
| 395 | ?> |
| 396 | <div id="acf-hidden-wp-editor" class="acf-hidden"> |
| 397 | <?php wp_editor( '', 'acf_content' ); ?> |
| 398 | </div> |
| 399 | <?php |
| 400 | |
| 401 | // action |
| 402 | do_action('acf/enqueue_uploader'); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // instantiate |
| 407 | acf_new_instance('ACF_Assets'); |
| 408 | |
| 409 | endif; // class_exists check |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * acf_localize_text |
| 414 | * |
| 415 | * description |
| 416 | * |
| 417 | * @date 13/4/18 |
| 418 | * @since 5.6.9 |
| 419 | * |
| 420 | * @param type $var Description. Default. |
| 421 | * @return type Description. |
| 422 | */ |
| 423 | |
| 424 | function acf_localize_text( $text ) { |
| 425 | return acf_get_instance('ACF_Assets')->add_text( $text ); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | /** |
| 430 | * acf_localize_data |
| 431 | * |
| 432 | * description |
| 433 | * |
| 434 | * @date 13/4/18 |
| 435 | * @since 5.6.9 |
| 436 | * |
| 437 | * @param type $var Description. Default. |
| 438 | * @return type Description. |
| 439 | */ |
| 440 | |
| 441 | function acf_localize_data( $data ) { |
| 442 | return acf_get_instance('ACF_Assets')->add_data( $data ); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | /* |
| 447 | * acf_enqueue_scripts |
| 448 | * |
| 449 | * |
| 450 | * |
| 451 | * @type function |
| 452 | * @date 6/10/13 |
| 453 | * @since 5.0.0 |
| 454 | * |
| 455 | * @param n/a |
| 456 | * @return n/a |
| 457 | */ |
| 458 | |
| 459 | function acf_enqueue_scripts( $args = array() ) { |
| 460 | return acf_get_instance('ACF_Assets')->enqueue_scripts( $args ); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | /* |
| 465 | * acf_enqueue_uploader |
| 466 | * |
| 467 | * This function will render a WP WYSIWYG and enqueue media |
| 468 | * |
| 469 | * @type function |
| 470 | * @date 27/10/2014 |
| 471 | * @since 5.0.9 |
| 472 | * |
| 473 | * @param n/a |
| 474 | * @return n/a |
| 475 | */ |
| 476 | |
| 477 | function acf_enqueue_uploader() { |
| 478 | return acf_get_instance('ACF_Assets')->enqueue_uploader(); |
| 479 | } |
| 480 | |
| 481 | ?> |