function rocket_minify_files($files, $force_pretty_url = true, $pretty_filename = null) { echo get_rocket_minify_files($files, $force_pretty_url, $pretty_filename); }
/** * Used to minify and concat JavaScript files * * @since 1.1.0 Fix Bug with externals URLs like //ajax.google.com * @since 1.0.2 Remove the filter, remove the array_chunk, add an automatic way to cut strings to 255c max * @since 1.0 */ function rocket_minify_js($buffer) { $home_host = parse_url(home_url(), PHP_URL_HOST); $internal_files = array(); $external_tags = array(); $excluded_tags = ''; $excluded_js = implode('|', get_rocket_exclude_js()); $excluded_js = str_replace('//' . $home_host, '', $excluded_js); $js_in_footer = get_rocket_minify_js_in_footer(); $wp_content_dirname = ltrim(str_replace(home_url(), '', WP_CONTENT_URL), '/') . '/'; $excluded_external_js = get_rocket_minify_excluded_external_js(); // Get all JS files with this regex preg_match_all(apply_filters('rocket_minify_js_regex_pattern', '#<script[^>]+?src=[\'|"]([^\'|"]+\\.js?)[\'|"]?.*>(?:<\\/script>)#i'), $buffer, $tags_match); $i = 0; foreach ($tags_match[0] as $tag) { // Chek if the file is already minify by get_rocket_minify_files // or the file is rejected to the process if (!strpos($tag, 'data-minify=') && !strpos($tag, 'data-no-minify=')) { // To check if a tag is to exclude of the minify process $excluded_tag = false; // Get JS URL with scheme $js_url_with_scheme = set_url_scheme($tags_match[1][$i]); // Get URL infos $js_url = parse_url($js_url_with_scheme); // Get host for all langs $langs_host = array(); if ($langs = get_rocket_i18n_uri()) { foreach ($langs as $lang) { $langs_host[] = parse_url($lang, PHP_URL_HOST); } } // Get host of CNAMES $cnames_host = get_rocket_cnames_host(array('all', 'css_and_js', 'js')); // Check if the link isn't external // Insert the relative path to the array without query string if (isset($js_url['host']) && ($js_url['host'] == $home_host || in_array($js_url['host'], $cnames_host) || in_array($js_url['host'], $langs_host)) || !isset($js_url['host']) && preg_match('#(' . $wp_content_dirname . '|wp-includes)#', $js_url['path'])) { // Check if it isn't a file to exclude if (!preg_match('#^(' . $excluded_js . ')$#', $js_url['path']) && pathinfo($js_url['path'], PATHINFO_EXTENSION) == 'js') { $internal_files[] = $js_url['path']; } else { $excluded_tag = true; } // If it's an excluded external file } else { if (isset($js_url['host']) && in_array($js_url['host'], $excluded_external_js)) { $excluded_tag = true; // If it's an external file } else { if (!in_array($tags_match[1][$i], $js_in_footer) && !in_array($js_url_with_scheme, $js_in_footer)) { $external_tags[] = $tag; } } } // Remove the tag if (!$excluded_tag) { $buffer = str_replace($tag, '', $buffer); } } $i++; } // Get external JS tags and remove duplicate scripts $external_tags = implode('', array_unique($external_tags)); // Exclude JS files to insert in footer foreach ($internal_files as $k => $url) { if (in_array(set_url_scheme('//' . $home_host . $url), $js_in_footer) || in_array($url, $js_in_footer)) { unset($internal_files[$k]); } } // Insert the minify JS file return array($buffer, $external_tags . get_rocket_minify_files($internal_files)); }