PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 6.0
Hustle – Email Marketing, Lead Generation, Optins, Popups v6.0
7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / lib / wpmu-lib / inc / class-thelib-ui.php
wordpress-popup / lib / wpmu-lib / inc Last commit date
class-thelib-array.php 8 years ago class-thelib-core.php 8 years ago class-thelib-debug.php 8 years ago class-thelib-html.php 8 years ago class-thelib-net.php 8 years ago class-thelib-session.php 8 years ago class-thelib-ui.php 8 years ago class-thelib-updates.php 8 years ago class-thelib.php 8 years ago
class-thelib-ui.php
537 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 wp_enqueue_style( $alias, $url );
335 }
336 }
337 }
338 }
339
340 /**
341 * Action hook for enqueue script (for PHP <5.3 only)
342 *
343 * @since 1.0.1
344 * @internal
345 */
346 public function _enqueue_script_callback() {
347 global $hook_suffix;
348
349 $items = $this->_get( 'js' );
350 $this->_clear( 'js' );
351 $hook = $hook_suffix;
352
353 if ( empty( $hook ) ) { $hook = 'front'; }
354
355 foreach ( $items as $item ) {
356 extract( $item ); // url, alias, onpage
357
358 if ( empty( $onpage ) ) { $onpage = 'all'; }
359
360 // onpage == 'all' will always load the script.
361 // otherwise onpage must match the enqueue-hook.
362 if ( 'all' == $onpage || $hook == $onpage ) {
363 // Load the Media-library functions.
364 if ( 'wpmu:media' === $url ) {
365 wp_enqueue_media();
366 continue;
367 }
368
369 // Register script if it has an URL.
370 if ( ! empty( $url ) ) {
371 wp_register_script( $alias, $url, array( 'jquery' ), false, true );
372 }
373
374 // Enqueue the script for output in the page footer.
375 wp_enqueue_script( $alias );
376 }
377 }
378 }
379
380 /**
381 * Prints extra script data to the page.
382 *
383 * @action `wp_head`
384 * @since 1.1.1
385 * @internal
386 */
387 public function _print_script_data() {
388 $data = $this->_get( 'js_data' );
389 $this->_clear( 'js_data' );
390
391 // Append javascript data to the script output.
392 if ( is_array( $data ) ) {
393 $collected = array();
394
395 foreach ( $data as $item ) {
396 if ( ! is_array( $item ) ) { continue; }
397 $key = sanitize_html_class( $item[0] );
398 $obj = array( 'window.' . $key => $item[1] );
399 $collected = self::$core->array->merge_recursive_distinct( $collected, $obj );
400 }
401
402 echo '<script>';
403 foreach ( $collected as $var => $value ) {
404 printf(
405 '%1$s = %2$s;',
406 $var,
407 json_encode( $value )
408 );
409 }
410 echo '</script>';
411 }
412 }
413
414
415 /**
416 * Prints extra javascript code to the page.
417 *
418 * @action `wp_foot`
419 * @since 1.1.3
420 * @internal
421 */
422 public function _print_script_code() {
423 $data = $this->_get( 'js_script' );
424 $this->_clear( 'js_script' );
425
426 // Append javascript data to the script output.
427 if ( is_array( $data ) ) {
428 foreach ( $data as $item ) {
429 printf(
430 '<script>try { %1$s } catch( err ){ window.console.log(err.message); }</script>',
431 $item
432 );
433 }
434 }
435 }
436
437 /**
438 * Display an admin notice.
439 *
440 * @since 1.0.0
441 * @api
442 *
443 * @param string $text Text to display.
444 * @param string $class Message-type [updated|error]
445 * @param string $screen Limit message to this screen-ID
446 * @param string $id Message ID. Prevents adding duplicate messages.
447 */
448 public function admin_message( $text, $class = '', $screen = '', $id = '' ) {
449 switch ( $class ) {
450 case 'red':
451 case 'err':
452 case 'error':
453 $class = 'error';
454 break;
455
456 case 'warning':
457 case 'orange':
458 $class = 'warning';
459 break;
460
461 case 'info':
462 case 'blue':
463 $class = 'info';
464 break;
465
466 default:
467 $class = 'updated';
468 break;
469 }
470
471 // Check if the message is already queued...
472 $items = self::_sess_get( 'admin_notice' );
473 foreach ( $items as $key => $data ) {
474 if (
475 $data['text'] == $text &&
476 $data['class'] == $class &&
477 $data['screen'] == $screen
478 ) {
479 return; // Don't add duplicate message to queue.
480 }
481
482 /**
483 * `$id` prevents adding duplicate messages.
484 *
485 * @since 1.1.0
486 */
487 if ( ! empty( $id ) && $data['id'] == $id ) {
488 return; // Don't add duplicate message to queue.
489 }
490 }
491
492 self::_sess_add( 'admin_notice', compact( 'text', 'class', 'screen', 'id' ) );
493 $this->add_action( 'admin_notices', '_admin_notice_callback', 1 );
494 $this->add_action( 'network_admin_notices', '_admin_notice_callback', 1 );
495 }
496
497 /**
498 * Action hook for admin notices (for PHP <5.3 only)
499 *
500 * @since 1.0.1
501 * @internal
502 */
503 public function _admin_notice_callback() {
504 $items = self::_sess_get( 'admin_notice' );
505 self::_sess_clear( 'admin_notice' );
506 $screen_info = get_current_screen();
507 $screen_id = $screen_info->id;
508
509 foreach ( $items as $item ) {
510 extract( $item ); // text, class, screen, id
511 if ( empty( $screen ) || $screen_id == $screen ) {
512 printf(
513 '<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>',
514 esc_attr( $class ),
515 $text,
516 esc_attr( $id ),
517 __( 'Dismiss this notice.' )
518 );
519 }
520 }
521 }
522
523 /**
524 * Checks the DB for persistent data from last request.
525 * If persistent data exists the appropriate hooks are set to process them.
526 *
527 * @since 1.0.7
528 * @internal
529 */
530 public function _check_admin_notices() {
531 if ( self::_sess_have( 'admin_notice' ) ) {
532 $this->add_action( 'admin_notices', '_admin_notice_callback', 1 );
533 $this->add_action( 'network_admin_notices', '_admin_notice_callback', 1 );
534 }
535 }
536
537 }