Example #1
1
function rocket_new_upgrade($wp_rocket_version, $actual_version)
{
    if (version_compare($actual_version, '1.0.1', '<')) {
        wp_clear_scheduled_hook('rocket_check_event');
    }
    if (version_compare($actual_version, '1.2.0', '<')) {
        // Delete old WP Rocket cache dir
        rocket_rrmdir(WP_ROCKET_PATH . 'cache');
        // Create new WP Rocket cache dir
        if (!is_dir(WP_ROCKET_CACHE_PATH)) {
            mkdir(WP_ROCKET_CACHE_PATH);
        }
    }
    if (version_compare($actual_version, '1.3.0', '<')) {
        rocket_dismiss_box('rocket_warning_plugin_modification');
    }
    if (version_compare($actual_version, '1.3.3', '<')) {
        // Clean cache
        rocket_clean_domain();
        // Create cache files
        run_rocket_bot('cache-preload');
    }
    if (version_compare($actual_version, '2.0', '<')) {
        // Add secret cache key
        $options = get_option(WP_ROCKET_SLUG);
        $options['secret_cache_key'] = create_rocket_uniqid();
        update_option(WP_ROCKET_SLUG, $options);
        global $wp_filesystem;
        if (!$wp_filesystem) {
            require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
            require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
            $wp_filesystem = new WP_Filesystem_Direct(new StdClass());
        }
        // Get chmod of old folder cache
        $chmod = is_dir(WP_CONTENT_DIR . '/wp-rocket-cache') ? substr(sprintf('%o', fileperms(WP_CONTENT_DIR . '/wp-rocket-cache')), -4) : CHMOD_WP_ROCKET_CACHE_DIRS;
        // Check and create cache folder in wp-content if not already exist
        if (!$wp_filesystem->is_dir(WP_CONTENT_DIR . '/cache')) {
            $wp_filesystem->mkdir(WP_CONTENT_DIR . '/cache', octdec($chmod));
        }
        $wp_filesystem->mkdir(WP_CONTENT_DIR . '/cache/wp-rocket', octdec($chmod));
        // Move old cache folder in new path
        @rename(WP_CONTENT_DIR . '/wp-rocket-cache', WP_CONTENT_DIR . '/cache/wp-rocket');
        // Add WP_CACHE constant in wp-config.php
        set_rocket_wp_cache_define(true);
        // Create advanced-cache.php file
        rocket_generate_advanced_cache_file();
        // Create config file
        rocket_generate_config_file();
    }
    if (version_compare($actual_version, '2.1', '<')) {
        rocket_reset_white_label_values(false);
        // Create minify cache folder if not exist
        if (!is_dir(WP_ROCKET_MINIFY_CACHE_PATH)) {
            rocket_mkdir_p(WP_ROCKET_MINIFY_CACHE_PATH);
        }
        // Create config domain folder if not exist
        if (!is_dir(WP_ROCKET_CONFIG_PATH)) {
            rocket_mkdir_p(WP_ROCKET_CONFIG_PATH);
        }
        // Create advanced-cache.php file
        rocket_generate_advanced_cache_file();
        // Create config file
        rocket_generate_config_file();
    }
    if (version_compare($actual_version, '2.3.3', '<')) {
        // Clean cache
        rocket_clean_domain();
        // Create cache files
        run_rocket_bot('cache-preload');
    }
    if (version_compare($actual_version, '2.3.9', '<')) {
        // Regenerate config file
        rocket_generate_config_file();
    }
    if (version_compare($actual_version, '2.4.1', '<')) {
        // Regenerate advanced-cache.php file
        rocket_generate_advanced_cache_file();
        delete_transient('rocket_ask_for_update');
    }
    if (version_compare($actual_version, '2.6', '<')) {
        // Activate Inline CSS & JS minification if HTML minification is activated
        $options = get_option(WP_ROCKET_SLUG);
        if (!empty($options['minify_html'])) {
            $options['minify_html_inline_css'] = 1;
            $options['minify_html_inline_js'] = 1;
        }
        update_option(WP_ROCKET_SLUG, $options);
        // Regenerate advanced-cache.php file
        rocket_generate_advanced_cache_file();
    }
}
Example #2
0
/**
 * Check if minify cache file exist and create it if not
 *
 * @since 2.1
 *
 * @param string $url 		 The minified URL with Google Minify Code
 * @param string $pretty_url The minified URL cache file
 * @return bool
 */
function rocket_fetch_and_cache_minify($url, $pretty_url)
{
    // Check if php-curl is enabled
    if (!function_exists('curl_init') || !function_exists('curl_exec')) {
        return false;
    }
    $pretty_path = str_replace(WP_ROCKET_MINIFY_CACHE_URL, WP_ROCKET_MINIFY_CACHE_PATH, $pretty_url);
    // If minify cache file is already exist, return to get a coffee :)
    if (file_exists($pretty_path)) {
        return true;
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'WP-Rocket-Minify');
    $content = curl_exec($ch);
    curl_close($ch);
    if ($content) {
        // Create cache folders of the request uri
        $cache_path = WP_ROCKET_MINIFY_CACHE_PATH . get_current_blog_id() . '/';
        if (!is_dir($cache_path)) {
            rocket_mkdir_p($cache_path);
        }
        // Apply CDN on CSS properties
        if (strrpos($pretty_path, '.css')) {
            $content = rocket_cdn_css_properties($content);
        }
        // Save cache file
        if (rocket_put_content($pretty_path, $content)) {
            return true;
        }
    }
    return false;
}
Example #3
0
/**
 * Recursive directory creation based on full path.
 *
 * @since 1.3.4
 *
 * @source wp_mkdir_p() in /wp-includes/functions.php
 */
function rocket_mkdir_p($target)
{
    // from php.net/mkdir user contributed notes
    $target = str_replace('//', '/', $target);
    // safe mode fails with a trailing slash under certain PHP versions.
    $target = rtrim($target, '/');
    // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
    if (empty($target)) {
        $target = '/';
    }
    if (file_exists($target)) {
        return @is_dir($target);
    }
    // Attempting to create the directory may clutter up our display.
    if (rocket_mkdir($target)) {
        return true;
    } elseif (is_dir(dirname($target))) {
        return false;
    }
    // If the above failed, attempt to create the parent node, then try again.
    if ($target != '/' && rocket_mkdir_p(dirname($target))) {
        return rocket_mkdir_p($target);
    }
    return false;
}
Example #4
0
/**
 * The famous callback, it puts contents in a cache file if buffer length > 255 (IE do not read pages under 255 c. )
 *
 * @since 1.3.0 Add filter rocket_buffer
 * @since 1.0
 */
function do_rocket_callback($buffer)
{
    /**
     * Allow to cache search results
     *
     * @since 2.3.8
     *
     * @param bool true will force caching search results
     */
    $rocket_cache_search = apply_filters('rocket_cache_search', false);
    /**
     * Allow to override the DONOTCACHEPAGE behavior.
     * To warn conflict with some plugins like Thrive Leads.
     *
     * @since 2.5
     *
     * @param bool true will force the override
     */
    $rocket_override_donotcachepage = apply_filters('rocket_override_donotcachepage', false);
    if (strlen($buffer) > 255 && (function_exists('is_404') && !is_404()) && (function_exists('is_search') && !is_search() || $rocket_cache_search) && (!defined('DONOTCACHEPAGE') || !DONOTCACHEPAGE || $rocket_override_donotcachepage)) {
        global $request_uri_path, $rocket_cache_filepath;
        // This hook is used for:
        // - Add width and height attributes on images
        // - Deferred JavaScript files
        // - DNS Prefechting
        // - Minification HTML/CSS/JavaScript
        $buffer = apply_filters('rocket_buffer', $buffer);
        $footprint = '';
        $is_html = true;
        if (!preg_match('/(<\\/html>)/i', $buffer)) {
            $is_html = false;
        }
        /**
         * Allow to the generate the caching file
         *
         * @since 2.5
         *
         * @param bool true will force the caching file generation
         */
        if (apply_filters('do_rocket_generate_caching_files', true)) {
            // Create cache folders of the request uri
            rocket_mkdir_p($request_uri_path);
            if ($is_html) {
                $footprint = get_rocket_footprint();
            }
            // Save the cache file
            rocket_put_content($rocket_cache_filepath, $buffer . $footprint);
            if (function_exists('gzencode')) {
                rocket_put_content($rocket_cache_filepath . '_gzip', gzencode($buffer . get_rocket_footprint(), apply_filters('rocket_gzencode_level_compression', 3)));
            }
        }
        // Send headers with the last modified time of the cache file
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($rocket_cache_filepath)) . ' GMT');
        if ($is_html) {
            $footprint = get_rocket_footprint(false);
        }
        $buffer = $buffer . $footprint;
    }
    return $buffer;
}
function __rocket_fix_mailchimp_main_css()
{
    if (!defined('MCSF_VER') || !function_exists('mailchimpSF_main_css')) {
        return;
    }
    $blog_id = get_current_blog_id();
    $cache_path = WP_ROCKET_MINIFY_CACHE_PATH . $blog_id . '/';
    $cache_url = WP_ROCKET_MINIFY_CACHE_URL . $blog_id . '/';
    $css_path = $cache_path . 'mailchimpSF_main_css.css';
    if (!is_dir($cache_path)) {
        rocket_mkdir_p($cache_path);
    }
    if (!file_exists($css_path)) {
        ob_start();
        mailchimpSF_main_css();
        $content = ob_get_contents();
        ob_end_clean();
        rocket_put_content($css_path, $content);
    }
    wp_deregister_style('mailchimpSF_main_css');
    wp_register_style('mailchimpSF_main_css', $cache_url . 'mailchimpSF_main_css.css', null, MCSF_VER);
}