PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/df_home/static/test/portalbkd/wp-content/plugins/wordpress-popup/inc/external/wpmu-lib/inc/class-thelib-ui.php

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