Source of file script-loader.php
Size: 13,367 Bytes - Last Modified: 2017-07-17T16:46:02+00:00
/home/travis/build/Limych/GeniBase/src/gb/script-loader.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 | <?php /** * GeniBase scripts and styles default loader. * * Several constants are used to manage the loading, concatenating and compression of scripts and CSS: * define('GB_SCRIPT_DEBUG', true); loads the development (non-minified) versions of all scripts and CSS, and disables compression and concatenation, * define('CONCATENATE_SCRIPTS', false); disables compression and concatenation of scripts and CSS, * define('COMPRESS_SCRIPTS', false); disables compression of scripts, * define('COMPRESS_CSS', false); disables compression of CSS, * define('ENFORCE_GZIP', true); forces gzip for compression (default is deflate). * * The globals $concatenate_scripts, $compress_scripts and $compress_css can be set by plugins * to temporarily override the above settings. Also a compression test is run once and the result is saved * as option 'can_compress_scripts' (0/1). The test will run again if that option is deleted. * * @package GeniBase * @since 2.0.0 * * @copyright Copyright © WordPress Team * @copyright Partially copyright © 2015, Andrey Khrolenok (andrey@khrolenok.ru) */ // Direct execution forbidden for this script if (! defined('GB_VERSION') || count(get_included_files()) == 1) die('<b>ERROR:</b> Direct execution forbidden!'); require_once (GB_CORE_DIR . '/class.gb-dependencies.php'); require_once (GB_CORE_DIR . '/class.gb-scripts.php'); require_once (GB_CORE_DIR . '/functions.gb-scripts.php'); require_once (GB_CORE_DIR . '/class.gb-styles.php'); require_once (GB_CORE_DIR . '/functions.gb-styles.php'); /** * Register all GeniBase scripts. * * Localizes some of them. * args order: $scripts->add( 'handle', 'url', 'dependencies', 'query-string', 1 ); * when last arg === 1 queues the script for the footer * * @since 2.0.0 * * @param GB_Scripts $scripts * object. */ function gb_default_scripts(&$scripts) { if (! defined('GB_SCRIPT_DEBUG')) define('GB_SCRIPT_DEBUG', GB_DEBUG); if (! $guessurl = site_url()) $guessurl = gb_guess_url(); $scripts->base_url = $guessurl; $scripts->content_url = defined('GB_CONTENT_URL') ? GB_CONTENT_URL : ''; $scripts->default_version = get_siteinfo('version'); $scripts->default_dirs = array( GB_CORE_URL . '/js/' ); $suffix = GB_SCRIPT_DEBUG ? '' : '.min'; // Vendor libraries and friends $scripts->add('gb-core', false, array( 'jquery', 'bootstrap' ), null); $scripts->add('jquery', "//code.jquery.com/jquery-3.2.1$suffix.js", array(), null); $scripts->add('bootstrap', "//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap$suffix.js", array(), null); $scripts->add_data('gb-core', 'integrity', (empty($suffix) ? 'sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=' // Uncompressed : 'sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=')); // Minified // $scripts->add_data('bootstrap', 'integrity', // (empty($suffix) ? '' // Uncompressed // TODO: Fill in // : '')); // Minified // TODO: Fill in $scripts->add('zxcvbn-async', GB_ADMIN_URL . "/js/zxcvbn-async$suffix.js", array(), '1.0'); GB_Hooks::did_action('init') && $scripts->localize('zxcvbn-async', '_zxcvbnSettings', array( 'src' => GB_ADMIN_URL . '/js/zxcvbn.min.js' )); $scripts->add('password-strength-meter', GB_ADMIN_URL . "/js/password-strength-meter$suffix.js", array( 'gb-core', 'zxcvbn-async' ), false, 1); GB_Hooks::did_action('init') && $scripts->localize('password-strength-meter', 'pwsL10n', array( 'empty' => __('Strength indicator'), 'short' => _x('Very weak', 'password strength'), 'bad' => _x('Weak', 'password strength'), 'good' => _x('Medium', 'password strength'), 'strong' => _x('Strong', 'password strength'), 'mismatch' => __('Mismatch') )); // Admin mode scripts $scripts->add('user-profile', GB_ADMIN_URL . "/js/user-profile$suffix.js", array( 'gb-core', 'password-strength-meter'/* , 'wp-util' */ ), false, 1); } /** * Assign default styles to $styles object. * * Nothing is returned, because the $styles parameter is passed by reference. * Meaning that whatever object is passed will be updated without having to * reassign the variable that was passed back to the same value. This saves * memory. * * Adding default styles is not the only task, it also assigns the base_url * property, the default version, and text direction for the object. * * @since 2.0.0 * * @param GB_Styles $styles * object. */ function gb_default_styles(&$styles) { if (! defined('GB_SCRIPT_DEBUG')) define('GB_SCRIPT_DEBUG', GB_DEBUG); if (! $guessurl = site_url()) $guessurl = gb_guess_url(); $styles->base_url = $guessurl; $styles->content_url = defined('GB_CONTENT_URL') ? GB_CONTENT_URL : ''; $styles->default_version = get_siteinfo('version'); $styles->text_direction = function_exists('is_rtl') && is_rtl() ? 'rtl' : 'ltr'; $styles->default_dirs = array( GB_CORE_URL . '/css/' ); $open_sans_font_url = ''; $suffix = GB_SCRIPT_DEBUG ? '' : '.min'; // Common dependencies $styles->add('icons-material', "//fonts.googleapis.com/icon?family=Material+Icons"); // $styles->add( 'dashicons', "/wp-includes/css/dashicons$suffix.css" ); // Common styles $styles->add('gb-core', GB_CORE_URL . "/load-style.php/core$suffix", array( 'bootstrap' )); $styles->add('bootstrap', "//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap$suffix.css"); // $styles->add('responsive-tables', GB_CORE_URL . "/css/responsive-tables.css", array( // 'gb-core' // )); // $styles->add('forms', GB_CORE_URL . "/load-style.php/forms$suffix"/* , array('gb-core') */ ); // $styles->add_data('bootstrap', 'integrity', // (empty($suffix) ? '' // Uncompressed // TODO: Fill in // : '')); // Minified // TODO: Fill in // Admin styles $styles->add('install', GB_ADMIN_URL . "/load-style.php/install$suffix", array( 'gb-core' )); $styles->add('ie', GB_ADMIN_URL . "/css/ie$suffix.css"); $styles->add_data('ie', 'conditional', 'lte IE 7'); } /** * Prints the script queue in the HTML head on admin pages. * * Postpones the scripts that were queued for the footer. * print_footer_scripts() is called in the footer to print these scripts. * * @since 2.0.0 * * @see gb_print_scripts() */ function print_head_scripts() { global $concatenate_scripts; if (! GB_Hooks::did_action('gb_print_scripts')) { /** * This action is documented in gb/functions.gb-scripts.php */ GB_Hooks::do_action('gb_print_scripts'); } script_concat_settings(); _gb_scripts()->do_concat = $concatenate_scripts; _gb_scripts()->do_concat = FALSE; // TODO: Remove for enable concatenate mode _gb_scripts()->do_head_items(); /** * Filter whether to print the head scripts. * * @since 2.1.0 * * @param bool $print * Whether to print the head scripts. Default true. */ if (GB_Hooks::apply_filters('print_head_scripts', true)) _print_scripts(); _gb_scripts()->reset(); return _gb_scripts()->done; } /** * Prints the scripts that were queued for the footer or too late for the HTML head. * * @since 2.0.0 */ function print_footer_scripts() { global $concatenate_scripts; script_concat_settings(); _gb_scripts()->do_concat = $concatenate_scripts; _gb_scripts()->do_concat = FALSE; // TODO: Remove for enable concatenate mode // _gb_scripts()->do_concat = true; // TODO: Remove for enable concatenate mode _gb_scripts()->do_footer_items(); /** * Filter whether to print the footer scripts. * * @since 2.1.0 * * @param bool $print * Whether to print the footer scripts. Default true. */ if (GB_Hooks::apply_filters('print_footer_scripts', true)) _print_scripts(); _gb_scripts()->reset(); return _gb_scripts()->done; } /** * * @internal use */ function _print_scripts() { global $compress_scripts; $zip = $compress_scripts ? 1 : 0; if ($zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP) $zip = 'gzip'; if ($concat = trim(_gb_scripts()->concat, ', ')) { if (! empty(_gb_scripts()->print_code)) { echo "\n<script type='text/javascript'>\n"; echo "/* <![CDATA[ */\n"; // not needed in HTML 5 echo _gb_scripts()->print_code; echo "/* ]]> */\n"; echo "</script>\n"; } $src = _gb_scripts()->base_url . "/gb/load-scripts.php?t=scr&c={$zip}&ver=" . _gb_scripts()->default_version; echo "<script type='text/javascript' src='" . esc_attr($src) . "'></script>\n"; } if (! empty(_gb_scripts()->print_html)) echo _gb_scripts()->print_html; } /** * Prints the script queue in the HTML head on the front end. * * Postpones the scripts that were queued for the footer. * gb_print_footer_scripts() is called in the footer to print these scripts. * * @since 2.0.0 */ function gb_print_head_scripts() { if (! GB_Hooks::did_action('gb_print_scripts')) { /** * This action is documented in gb/functions.gb-scripts.php */ GB_Hooks::do_action('gb_print_scripts'); } return print_head_scripts(); } /** * Private, for use in *_footer_scripts hooks * * @since 2.0.0 */ function _gb_footer_scripts() { print_late_styles(); print_footer_scripts(); } /** * Hooks to print the scripts and styles in the footer. * * @since 2.0.0 */ function gb_print_footer_scripts() { /** * Fires when footer scripts are printed. * * @since 2.1.0 */ GB_Hooks::do_action('gb_print_footer_scripts'); } /** * Wrapper for GB_Hooks::do_action('gb_enqueue_scripts') * * Allows plugins to queue scripts for the front end using gb_enqueue_script(). * Runs first in gb_head(). * * @since 2.1.0 */ function gb_enqueue_scripts() { /** * Fires when scripts and styles are enqueued. * * @since 2.1.0 */ GB_Hooks::do_action('gb_enqueue_scripts'); } /** * Prints the styles that were queued too late for the HTML head. * * @since 2.0.0 */ function print_late_styles() { global $concatenate_scripts; gb_styles()->do_concat = $concatenate_scripts; gb_styles()->do_concat = FALSE; // TODO: Remove for enable concatenate mode gb_styles()->do_footer_items(); /** * Filter whether to print the styles queued too late for the HTML head. * * @since 2.0.0 * * @param bool $print * Whether to print the 'late' styles. Default true. */ if (GB_Hooks::apply_filters('print_late_styles', true)) _print_styles(); gb_styles()->reset(); return gb_styles()->done; } /** * * @internal use */ function _print_styles() { global $compress_css; $zip = $compress_css ? 1 : 0; if ($zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP) $zip = 'gzip'; if (! empty(gb_styles()->concat)) { $dir = gb_styles()->text_direction; $ver = gb_styles()->default_version; $href = gb_styles()->base_url . "/gb/load-styles.php?c={$zip}&dir={$dir}&load=" . trim(gb_styles()->concat, ', ') . '&ver=' . $ver; echo "<link rel='stylesheet' href='" . esc_attr($href) . "' type='text/css' media='all' />\n"; if (! empty(gb_styles()->print_code)) { echo "<style type='text/css'>\n"; echo gb_styles()->print_code; echo "\n</style>\n"; } } if (! empty(gb_styles()->print_html)) echo gb_styles()->print_html; } /** * Determine the concatenation and compression settings for scripts and styles. * * @since 2.0.0 */ function script_concat_settings() { global $concatenate_scripts, $compress_scripts, $compress_css; $compressed_output = (ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler')); if (! isset($concatenate_scripts)) { $concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true; if( /*!is_admin() ||*/ (defined('GB_SCRIPT_DEBUG') && GB_SCRIPT_DEBUG)) $concatenate_scripts = false; } if (! isset($compress_scripts)) { $compress_scripts = defined('COMPRESS_SCRIPTS') ? COMPRESS_SCRIPTS : true; if ($compress_scripts && ( /*!get_site_option('can_compress_scripts') ||*/ $compressed_output)) $compress_scripts = false; } if (! isset($compress_css)) { $compress_css = defined('COMPRESS_CSS') ? COMPRESS_CSS : true; if ($compress_css && ( /*!get_site_option('can_compress_scripts') ||*/ $compressed_output)) $compress_css = false; } } // TODO: actions GB_Hooks::add_action('gb_default_scripts', 'gb_default_scripts'); // GB_Hooks::add_filter( 'gb_print_scripts', 'gb_just_in_time_script_localization' ); // GB_Hooks::add_filter( 'print_scripts_array', 'gb_prototype_before_jquery' ); GB_Hooks::add_action('gb_default_styles', 'gb_default_styles'); // GB_Hooks::add_filter( 'style_loader_src', 'gb_style_loader_src', 10, 2 ); |