Esempio n. 1
0
function rocket_insert_deferred_js($buffer)
{
    // Don't add anything on 404 page or on a page without these query strings
    if (is_404() || !empty($_GET) && !isset($_GET['utm_source'], $_GET['utm_medium'], $_GET['utm_campaign']) && !isset($_GET['fb_action_ids'], $_GET['fb_action_types'], $_GET['fb_source']) && !isset($_GET['gclid']) && !isset($_GET['permalink_name']) && !isset($_GET['lp-variation-id']) && !isset($_GET['lang'])) {
        return;
    }
    /**
     * Filter LABjs file URL
     *
     * @since 1.1.0
     *
     * @param string LABjs file URL
     */
    $labjs_src = WP_ROCKET_FRONT_JS_URL . 'LAB.' . WP_ROCKET_LAB_JS_VERSION . '.min.js';
    $labjs_src = get_rocket_cdn_url($labjs_src, array('all', 'css_js', 'js'));
    $labjs_src = apply_filters('rocket_labjs_src', $labjs_src);
    /**
     * Filter list of LABjs options
     *
     * @since 1.1.0
     *
     * @param array List of LABjs options
     */
    $labjs_options = apply_filters('rocket_labjs_options', array('AlwaysPreserveOrder' => true));
    /**
     * Filter list of Deferred JavaScript files waiting to load
     *
     * @since 1.1.0
     *
     * @param array List of Deferred JavaScript files waiting to load
     */
    $deferred_js_wait = apply_filters('rocket_minify_deferred_js_wait', get_rocket_option('deferred_js_wait'));
    $defer = '<script src="' . $labjs_src . '" data-no-minify="1"></script>';
    $defer .= '<script>';
    $defer .= '$LAB';
    // Set LABjs options
    // All options is available in http://labjs.com/documentation.php#optionsobject
    if (count($labjs_options)) {
        $defer .= '.setOptions(' . json_encode($labjs_options) . ')';
    }
    $deferred_js_files = get_rocket_deferred_js_files();
    foreach ($deferred_js_files as $k => $js) {
        $wait = $deferred_js_wait[$k] == '1' ? '.wait(' . esc_js(apply_filters('rocket_labjs_wait_callback', false, $js)) . ')' : '';
        $defer .= '.script("' . esc_js($js) . '")' . $wait;
    }
    $defer .= ';</script>';
    echo $defer;
}
Esempio n. 2
0
function __rocket_extract_js_files_from_footer()
{
    global $rocket_enqueue_js_in_footer, $wp_scripts, $pagenow;
    /** This filter is documented in inc/front/process.php */
    $rocket_cache_search = apply_filters('rocket_cache_search', false);
    if (isset($wp_scripts->in_footer) && !is_array($wp_scripts->in_footer) || !get_rocket_option('minify_js', false) || in_array($pagenow, array('wp-login.php', 'wp-register.php')) || defined('DONOTMINIFYJS') && DONOTMINIFYJS || defined('DONOTCACHEPAGE') && DONOTCACHEPAGE || is_rocket_post_excluded_option('minify_js') || is_404() || is_search() && !$rocket_cache_search) {
        return;
    }
    // Digg Digg (https://wordpress.org/plugins/digg-digg/)
    if (defined('DD_PLUGIN_URL')) {
        $rocket_enqueue_js_in_footer[] = DD_PLUGIN_URL . '/js/diggdigg-floating-bar.js';
    }
    // nrelate Flyout (https://wordpress.org/plugins/nrelate-flyout/)
    if (defined('NRELATE_PLUGIN_VERSION')) {
        $rocket_enqueue_js_in_footer[] = NRELATE_JS_DEBUG ? 'http://staticrepo.nrelate.com/common_wp/' . NRELATE_PLUGIN_VERSION . '/nrelate_js.js' : NRELATE_ADMIN_URL . '/nrelate_js.min.js';
    }
    $home_host = parse_url(home_url(), PHP_URL_HOST);
    $deferred_js_files = get_rocket_deferred_js_files();
    $excluded_js = get_rocket_exclude_js();
    $excluded_external_js = get_rocket_minify_excluded_external_js();
    foreach ($wp_scripts->in_footer as $handle) {
        $script_src = $wp_scripts->registered[$handle]->src;
        $script_src = strstr($script_src, '/wp-includes/js/') ? $wp_scripts->base_url . $script_src : $script_src;
        $script_src_cleaned = str_replace(array('http:', 'https:', '//' . $home_host), '', $script_src);
        if (in_array($handle, $wp_scripts->queue) && !in_array(parse_url($script_src, PHP_URL_HOST), $excluded_external_js) && !in_array($script_src, $deferred_js_files) && !in_array(parse_url($script_src, PHP_URL_PATH), $excluded_js) && !in_array(parse_url($script_src_cleaned, PHP_URL_PATH), $excluded_js)) {
            // Dequeue JS files without extension
            if (pathinfo($script_src, PATHINFO_EXTENSION) == '') {
                wp_dequeue_script($handle);
            }
            // Add protocol on external JS to prevent conflict
            if ($home_host != parse_url($script_src, PHP_URL_HOST) && strpos($script_src, 'http://') === false && strpos($script_src, 'https://') === false) {
                $script_src = set_url_scheme($script_src);
            }
            // Add dependency enqueued in the footer
            foreach ($wp_scripts->registered[$handle]->deps as $handle_dep) {
                if (in_array($handle_dep, $wp_scripts->in_footer)) {
                    $src = $wp_scripts->registered[$handle_dep]->src;
                    $src = strstr($src, '/wp-includes/js/') ? $wp_scripts->base_url . $src : $src;
                    $rocket_enqueue_js_in_footer[$handle_dep] = rocket_set_internal_url_scheme($src);
                }
            }
            $rocket_enqueue_js_in_footer[$handle] = rocket_set_internal_url_scheme($script_src);
        }
    }
}