class-thelib-array.php
5 years ago
class-thelib-core.php
5 years ago
class-thelib-debug.php
5 years ago
class-thelib-html.php
5 years ago
class-thelib-net.php
5 years ago
class-thelib-session.php
5 years ago
class-thelib-ui.php
5 years ago
class-thelib-updates.php
5 years ago
class-thelib.php
5 years ago
class-thelib-ui.php
555 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The UI component. |
| 4 | * Access via function `lib3()->ui`. |
| 5 | * |
| 6 | * @since 1.1.4 |
| 7 | */ |
| 8 | class TheLib_Ui extends TheLib { |
| 9 | |
| 10 | // Load the main JS module (the wpmUi object) and basic styles. |
| 11 | const MODULE_CORE = 'core'; |
| 12 | |
| 13 | // Load CSS animations. |
| 14 | const MODULE_ANIMATION = 'animate'; |
| 15 | |
| 16 | // Custom select2 integration. JS: wpmUi.upgrade_multiselect() |
| 17 | const MODULE_SELECT = 'select'; |
| 18 | |
| 19 | // Vertical navigation support. |
| 20 | const MODULE_VNAV = 'vnav'; |
| 21 | |
| 22 | // Styles for lib3()->html->element() output. |
| 23 | const MODULE_HTML = 'html_element'; |
| 24 | |
| 25 | // WordPress media gallery support. |
| 26 | const MODULE_MEDIA = 'media'; |
| 27 | |
| 28 | // Fontawesome CSS icons. |
| 29 | const MODULE_ICONS = 'fontawesome'; |
| 30 | |
| 31 | // jQuery datepicker, draggable, jQuery-UI styles. |
| 32 | const MODULE_JQUERY = 'jquery-ui'; |
| 33 | |
| 34 | /** |
| 35 | * Class constructor |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | * @internal |
| 39 | */ |
| 40 | public function __construct() { |
| 41 | parent::__construct(); |
| 42 | |
| 43 | // Check for persistent data from last request that needs to be processed. |
| 44 | $this->add_action( |
| 45 | 'plugins_loaded', |
| 46 | '_check_admin_notices' |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Enqueue core UI files (CSS/JS). |
| 52 | * |
| 53 | * Defined modules: |
| 54 | * - core |
| 55 | * - select |
| 56 | * - vnav |
| 57 | * - card-list |
| 58 | * - html-element |
| 59 | * - media |
| 60 | * - fontawesome |
| 61 | * - jquery-ui |
| 62 | * |
| 63 | * All undefined modules are assumed to be a valid CSS or JS file-name. |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | * @api |
| 67 | * |
| 68 | * @param string $modules The module to load. |
| 69 | * @param string $onpage A page hook; files will only be loaded on this page. |
| 70 | */ |
| 71 | public function add( $module = 'core', $onpage = 'all' ) { |
| 72 | switch ( $module ) { |
| 73 | case 'core': |
| 74 | $this->css( $this->_css_url( 'wpmu-ui.3.min.css' ), $onpage ); |
| 75 | $this->js( $this->_js_url( 'wpmu-ui.3.min.js' ), $onpage ); |
| 76 | break; |
| 77 | |
| 78 | case 'animate': |
| 79 | case 'animation': |
| 80 | $this->css( $this->_css_url( 'animate.3.min.css' ), $onpage ); |
| 81 | break; |
| 82 | |
| 83 | case 'select': |
| 84 | $this->css( $this->_css_url( 'select2.3.min.css' ), $onpage ); |
| 85 | $this->js( $this->_js_url( 'select2.3.min.js' ), $onpage ); |
| 86 | break; |
| 87 | |
| 88 | case 'vnav': |
| 89 | $this->css( $this->_css_url( 'wpmu-vnav.3.min.css' ), $onpage ); |
| 90 | $this->js( $this->_js_url( 'wpmu-vnav.3.min.js' ), $onpage ); |
| 91 | break; |
| 92 | |
| 93 | case 'html-element': |
| 94 | case 'html_element': |
| 95 | case 'html': |
| 96 | $this->css( $this->_css_url( 'wpmu-html.3.min.css' ), $onpage ); |
| 97 | break; |
| 98 | |
| 99 | case 'media': |
| 100 | $this->js( 'wpmu:media', $onpage ); |
| 101 | break; |
| 102 | |
| 103 | case 'fontawesome': |
| 104 | case 'icons': |
| 105 | $this->css( $this->_css_url( 'fontawesome.3.min.css' ), $onpage ); |
| 106 | break; |
| 107 | |
| 108 | case 'jquery-ui': |
| 109 | case 'jquery': |
| 110 | $this->js( 'jquery-ui-core', $onpage ); |
| 111 | $this->js( 'jquery-ui-datepicker', $onpage ); |
| 112 | $this->js( 'jquery-ui-draggable', $onpage ); |
| 113 | $this->css( $this->_css_url( 'jquery-ui.wpmui.3.min.css' ), $onpage ); |
| 114 | break; |
| 115 | |
| 116 | default: |
| 117 | $ext = strrchr( $module, '.' ); |
| 118 | |
| 119 | if ( WDEV_UNMINIFIED ) { |
| 120 | $module = str_replace( '.min' . $ext, $ext, $module ); |
| 121 | } |
| 122 | if ( '.css' === $ext ) { |
| 123 | $this->css( $module, $onpage, 20 ); |
| 124 | } else if ( '.js' === $ext ) { |
| 125 | $this->js( $module, $onpage, 20 ); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Adds a variable to javascript. |
| 132 | * |
| 133 | * @since 1.0.7 |
| 134 | * @api |
| 135 | * |
| 136 | * @param string $name Name of the variable |
| 137 | * @param mixed $data Value of the variable |
| 138 | */ |
| 139 | public function data( $name, $data ) { |
| 140 | $this->_add( 'js_data_hook', true ); |
| 141 | |
| 142 | // Determine which hook should print the data. |
| 143 | $hook = ( is_admin() ? 'admin_footer' : 'wp_footer' ); |
| 144 | |
| 145 | // Enqueue the data for output with javascript sources. |
| 146 | $this->_add( 'js_data', array( $name, $data ) ); |
| 147 | |
| 148 | $this->add_action( $hook, '_print_script_data' ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Adds custom javascript to the page footer. |
| 153 | * |
| 154 | * @since 1.1.3 |
| 155 | * @api |
| 156 | * |
| 157 | * @param string $jscript The javascript code. |
| 158 | */ |
| 159 | public function script( $jscript ) { |
| 160 | $this->_add( 'js_data_hook', true ); |
| 161 | |
| 162 | // Determine which hook should print the data. |
| 163 | $hook = ( is_admin() ? 'admin_footer' : 'wp_footer' ); |
| 164 | |
| 165 | // Enqueue the data for output with javascript sources. |
| 166 | $this->_add( 'js_script', $jscript ); |
| 167 | |
| 168 | $this->add_action( $hook, '_print_script_code' ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Enqueue a javascript file. |
| 173 | * |
| 174 | * @since 1.0.0 |
| 175 | * @api |
| 176 | * |
| 177 | * @param string $url Full URL to the javascript file. |
| 178 | * @param string $onpage A page hook; files will only be loaded on this page. |
| 179 | * @param int $priority Loading order. The higher the number, the later it is loaded. |
| 180 | */ |
| 181 | public function js( $url, $onpage = 'all', $priority = 10 ) { |
| 182 | $this->_prepare_js_or_css( $url, 'js', $onpage, $priority ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Enqueue a css file. |
| 187 | * |
| 188 | * @since 1.0.0 |
| 189 | * @api |
| 190 | * |
| 191 | * @param string $url Full URL to the css filename. |
| 192 | * @param string $onpage A page hook; files will only be loaded on this page. |
| 193 | * @param int $priority Loading order. The higher the number, the later it is loaded. |
| 194 | */ |
| 195 | public function css( $url, $onpage = 'all', $priority = 10 ) { |
| 196 | $this->_prepare_js_or_css( $url, 'css', $onpage, $priority ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Prepare to enqueue a javascript or css file. |
| 201 | * |
| 202 | * @since 1.0.7 |
| 203 | * @internal |
| 204 | * |
| 205 | * @param string $url Full URL to the javascript/css file. |
| 206 | * @param string $type 'css' or 'js' |
| 207 | * @param string $onpage A page hook; files will only be loaded on this page. |
| 208 | * @param int $priority Loading order. The higher the number, the later it is loaded. |
| 209 | */ |
| 210 | protected function _prepare_js_or_css( $url, $type, $onpage, $priority ) { |
| 211 | $this->_add( 'js_or_css', compact( 'url', 'type', 'onpage', 'priority' ) ); |
| 212 | |
| 213 | $this->add_action( 'init', '_add_js_or_css' ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Returns the JS/CSS handle of the item. |
| 218 | * This is a private helper function used by array_map() |
| 219 | * |
| 220 | * @since 1.0.7 |
| 221 | * @internal |
| 222 | */ |
| 223 | public function _get_script_handle( $item ) { |
| 224 | if ( ! property_exists( $item, 'handle' ) ) { |
| 225 | $item->handle = ''; |
| 226 | } |
| 227 | |
| 228 | return $item->handle; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Enqueues either a css or javascript file |
| 233 | * |
| 234 | * @since 1.0.0 |
| 235 | * @internal |
| 236 | */ |
| 237 | public function _add_js_or_css() { |
| 238 | global $wp_styles, $wp_scripts; |
| 239 | |
| 240 | $scripts = $this->_get( 'js_or_css' ); |
| 241 | $this->_clear( 'js_or_css' ); |
| 242 | |
| 243 | // Prevent adding the same URL twice. |
| 244 | $done_urls = array(); |
| 245 | |
| 246 | foreach ( $scripts as $script ) { |
| 247 | extract( $script ); // url, type, onpage, priority |
| 248 | |
| 249 | // Skip Front-End files in Admin Dashboard. |
| 250 | if ( 'front' === $onpage && is_admin() ) { continue; } |
| 251 | |
| 252 | // Prevent adding the same URL twice. |
| 253 | if ( in_array( $url, $done_urls ) ) { continue; } |
| 254 | $done_urls[] = $url; |
| 255 | |
| 256 | $type = ( 'css' === $type || 'style' === $type ? 'css' : 'js' ); |
| 257 | |
| 258 | // The $handle values are intentionally not cached: |
| 259 | // Any plugin/theme could add new handles at any moment... |
| 260 | $handles = array(); |
| 261 | if ( 'css' == $type ) { |
| 262 | if ( ! is_a( $wp_styles, 'WP_Styles' ) ) { |
| 263 | $wp_styles = new WP_Styles(); |
| 264 | } |
| 265 | $handles = array_values( |
| 266 | array_map( |
| 267 | array( $this, '_get_script_handle' ), |
| 268 | $wp_styles->registered |
| 269 | ) |
| 270 | ); |
| 271 | $type_callback = '_enqueue_style_callback'; |
| 272 | } else { |
| 273 | if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) { |
| 274 | $wp_scripts = new WP_Scripts(); |
| 275 | } |
| 276 | $handles = array_values( |
| 277 | array_map( |
| 278 | array( $this, '_get_script_handle' ), |
| 279 | $wp_scripts->registered |
| 280 | ) |
| 281 | ); |
| 282 | $type_callback = '_enqueue_script_callback'; |
| 283 | } |
| 284 | |
| 285 | if ( in_array( $url, $handles ) ) { |
| 286 | $alias = $url; |
| 287 | $url = ''; |
| 288 | } else { |
| 289 | // Get the filename from the URL, then sanitize it and prefix "wpmu-" |
| 290 | $urlparts = explode( '?', $url, 2 ); |
| 291 | $alias = 'wpmu-' . sanitize_title( basename( $urlparts[0] ) ); |
| 292 | } |
| 293 | $onpage = empty( $onpage ) ? 'all' : $onpage; |
| 294 | |
| 295 | if ( ! is_admin() ) { |
| 296 | $hook = 'wp_enqueue_scripts'; |
| 297 | } else { |
| 298 | $hook = 'admin_enqueue_scripts'; |
| 299 | } |
| 300 | |
| 301 | $item = compact( 'url', 'alias', 'onpage' ); |
| 302 | $this->_add( $type, $item ); |
| 303 | |
| 304 | $this->add_action( $hook, $type_callback, 100 + $priority ); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Action hook for enqueue style (for PHP <5.3 only) |
| 310 | * |
| 311 | * @since 1.0.1 |
| 312 | * @internal |
| 313 | */ |
| 314 | public function _enqueue_style_callback() { |
| 315 | global $hook_suffix; |
| 316 | |
| 317 | $items = $this->_get( 'css' ); |
| 318 | $this->_clear( 'css' ); |
| 319 | $hook = $hook_suffix; |
| 320 | |
| 321 | if ( empty( $hook ) ) { $hook = 'front'; } |
| 322 | |
| 323 | foreach ( $items as $item ) { |
| 324 | extract( $item ); // url, alias, onpage |
| 325 | |
| 326 | if ( empty( $onpage ) ) { $onpage = 'all'; } |
| 327 | |
| 328 | // onpage == 'all' will always load the script. |
| 329 | // otherwise onpage must match the enqueue-hook. |
| 330 | if ( 'all' == $onpage || $hook == $onpage ) { |
| 331 | if ( empty( $url ) ) { |
| 332 | wp_enqueue_style( $alias ); |
| 333 | } else { |
| 334 | /** |
| 335 | * Allow to setup styles version. |
| 336 | * |
| 337 | * @since 3.0.6 |
| 338 | * |
| 339 | * @param string WPMU Version |
| 340 | * @param string $alias Css handler. |
| 341 | */ |
| 342 | $version = apply_filters( 'wpmu_style_version', TheLib3_Wrap::get_version(), $alias ); |
| 343 | wp_enqueue_style( $alias, $url, array(), $version ); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Action hook for enqueue script (for PHP <5.3 only) |
| 351 | * |
| 352 | * @since 1.0.1 |
| 353 | * @internal |
| 354 | */ |
| 355 | public function _enqueue_script_callback() { |
| 356 | global $hook_suffix; |
| 357 | |
| 358 | $items = $this->_get( 'js' ); |
| 359 | $this->_clear( 'js' ); |
| 360 | $hook = $hook_suffix; |
| 361 | |
| 362 | if ( empty( $hook ) ) { $hook = 'front'; } |
| 363 | |
| 364 | foreach ( $items as $item ) { |
| 365 | extract( $item ); // url, alias, onpage |
| 366 | |
| 367 | if ( empty( $onpage ) ) { $onpage = 'all'; } |
| 368 | |
| 369 | // onpage == 'all' will always load the script. |
| 370 | // otherwise onpage must match the enqueue-hook. |
| 371 | if ( 'all' == $onpage || $hook == $onpage ) { |
| 372 | // Load the Media-library functions. |
| 373 | if ( 'wpmu:media' === $url ) { |
| 374 | wp_enqueue_media(); |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | // Register script if it has an URL. |
| 379 | if ( ! empty( $url ) ) { |
| 380 | /** |
| 381 | * Allow to setup script version. |
| 382 | * |
| 383 | * @since 3.0.6 |
| 384 | * |
| 385 | * @param string WPMU Version |
| 386 | * @param string $alias Css handler. |
| 387 | */ |
| 388 | $version = apply_filters( 'wpmu_script_version', TheLib3_Wrap::get_version(), $alias ); |
| 389 | wp_register_script( $alias, $url, array( 'jquery' ), $version, true ); |
| 390 | } |
| 391 | |
| 392 | // Enqueue the script for output in the page footer. |
| 393 | wp_enqueue_script( $alias ); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Prints extra script data to the page. |
| 400 | * |
| 401 | * @action `wp_head` |
| 402 | * @since 1.1.1 |
| 403 | * @internal |
| 404 | */ |
| 405 | public function _print_script_data() { |
| 406 | $data = $this->_get( 'js_data' ); |
| 407 | $this->_clear( 'js_data' ); |
| 408 | |
| 409 | // Append javascript data to the script output. |
| 410 | if ( is_array( $data ) ) { |
| 411 | $collected = array(); |
| 412 | |
| 413 | foreach ( $data as $item ) { |
| 414 | if ( ! is_array( $item ) ) { continue; } |
| 415 | $key = sanitize_html_class( $item[0] ); |
| 416 | $obj = array( 'window.' . $key => $item[1] ); |
| 417 | $collected = self::$core->array->merge_recursive_distinct( $collected, $obj ); |
| 418 | } |
| 419 | |
| 420 | echo '<script>'; |
| 421 | foreach ( $collected as $var => $value ) { |
| 422 | printf( |
| 423 | '%1$s = %2$s;', |
| 424 | $var, |
| 425 | json_encode( $value ) |
| 426 | ); |
| 427 | } |
| 428 | echo '</script>'; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /** |
| 434 | * Prints extra javascript code to the page. |
| 435 | * |
| 436 | * @action `wp_foot` |
| 437 | * @since 1.1.3 |
| 438 | * @internal |
| 439 | */ |
| 440 | public function _print_script_code() { |
| 441 | $data = $this->_get( 'js_script' ); |
| 442 | $this->_clear( 'js_script' ); |
| 443 | |
| 444 | // Append javascript data to the script output. |
| 445 | if ( is_array( $data ) ) { |
| 446 | foreach ( $data as $item ) { |
| 447 | printf( |
| 448 | '<script>try { %1$s } catch( err ){ window.console.log(err.message); }</script>', |
| 449 | $item |
| 450 | ); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Display an admin notice. |
| 457 | * |
| 458 | * @since 1.0.0 |
| 459 | * @api |
| 460 | * |
| 461 | * @param string $text Text to display. |
| 462 | * @param string $class Message-type [updated|error] |
| 463 | * @param string $screen Limit message to this screen-ID |
| 464 | * @param string $id Message ID. Prevents adding duplicate messages. |
| 465 | */ |
| 466 | public function admin_message( $text, $class = '', $screen = '', $id = '' ) { |
| 467 | switch ( $class ) { |
| 468 | case 'red': |
| 469 | case 'err': |
| 470 | case 'error': |
| 471 | $class = 'error'; |
| 472 | break; |
| 473 | |
| 474 | case 'warning': |
| 475 | case 'orange': |
| 476 | $class = 'warning'; |
| 477 | break; |
| 478 | |
| 479 | case 'info': |
| 480 | case 'blue': |
| 481 | $class = 'info'; |
| 482 | break; |
| 483 | |
| 484 | default: |
| 485 | $class = 'updated'; |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | // Check if the message is already queued... |
| 490 | $items = self::_sess_get( 'admin_notice' ); |
| 491 | foreach ( $items as $key => $data ) { |
| 492 | if ( |
| 493 | $data['text'] == $text && |
| 494 | $data['class'] == $class && |
| 495 | $data['screen'] == $screen |
| 496 | ) { |
| 497 | return; // Don't add duplicate message to queue. |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * `$id` prevents adding duplicate messages. |
| 502 | * |
| 503 | * @since 1.1.0 |
| 504 | */ |
| 505 | if ( ! empty( $id ) && $data['id'] == $id ) { |
| 506 | return; // Don't add duplicate message to queue. |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | self::_sess_add( 'admin_notice', compact( 'text', 'class', 'screen', 'id' ) ); |
| 511 | $this->add_action( 'admin_notices', '_admin_notice_callback', 1 ); |
| 512 | $this->add_action( 'network_admin_notices', '_admin_notice_callback', 1 ); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Action hook for admin notices (for PHP <5.3 only) |
| 517 | * |
| 518 | * @since 1.0.1 |
| 519 | * @internal |
| 520 | */ |
| 521 | public function _admin_notice_callback() { |
| 522 | $items = self::_sess_get( 'admin_notice' ); |
| 523 | self::_sess_clear( 'admin_notice' ); |
| 524 | $screen_info = get_current_screen(); |
| 525 | $screen_id = $screen_info->id; |
| 526 | |
| 527 | foreach ( $items as $item ) { |
| 528 | extract( $item ); // text, class, screen, id |
| 529 | if ( empty( $screen ) || $screen_id == $screen ) { |
| 530 | printf( |
| 531 | '<div class="%1$s notice notice-%1$s is-dismissible %3$s"><p>%2$s</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">%4$s</span></button></div>', |
| 532 | esc_attr( $class ), |
| 533 | $text, |
| 534 | esc_attr( $id ), |
| 535 | __( 'Dismiss this notice.' ) |
| 536 | ); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Checks the DB for persistent data from last request. |
| 543 | * If persistent data exists the appropriate hooks are set to process them. |
| 544 | * |
| 545 | * @since 1.0.7 |
| 546 | * @internal |
| 547 | */ |
| 548 | public function _check_admin_notices() { |
| 549 | if ( self::_sess_have( 'admin_notice' ) ) { |
| 550 | $this->add_action( 'admin_notices', '_admin_notice_callback', 1 ); |
| 551 | $this->add_action( 'network_admin_notices', '_admin_notice_callback', 1 ); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 |