Example #1
0
/**
 * Used to flush the .htaccess file
 *
 * @since 1.1.0 Remove empty spacings when .htaccess is generated
 * @since 1.0
 *
 * @param bool $force (default: false)
 * @return void
 */
function flush_rocket_htaccess($force = false)
{
    if (!$GLOBALS['is_apache']) {
        return;
    }
    $rules = '';
    $htaccess_file = get_home_path() . '.htaccess';
    if (is_writable($htaccess_file)) {
        // Get content of .htaccess file
        $ftmp = file_get_contents($htaccess_file);
        // Remove the WP Rocket marker
        $ftmp = preg_replace('/# BEGIN WP Rocket(.*)# END WP Rocket/isU', '', $ftmp);
        // Remove empty spacings
        $ftmp = str_replace("\n\n", "\n", $ftmp);
        if ($force === false) {
            $rules = get_rocket_htaccess_marker();
        }
        // Update the .htacces file
        rocket_put_content($htaccess_file, $rules . $ftmp);
    }
}
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
/**
 * Create the current config domain file
 * For example, if home_url() return example.com, the config domain file will be in /config/example.com
 *
 * @since 2.0
 *
 * @return void
 */
function rocket_generate_config_file()
{
    list($config_files_path, $buffer) = get_rocket_config_file();
    if (count($config_files_path)) {
        foreach ($config_files_path as $file) {
            rocket_put_content($file, $buffer);
        }
    }
}
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;
}
Example #5
0
function rocket_deactivation()
{
    if (!isset($_GET['rocket_nonce']) || !wp_verify_nonce($_GET['rocket_nonce'], 'force_deactivation')) {
        $causes = array();
        // .htaccess problem
        if ($GLOBALS['is_apache'] && !is_writable(get_home_path() . '.htaccess')) {
            $causes[] = 'htaccess';
        }
        // wp-config problem
        if (!is_writable(rocket_find_wpconfig_path())) {
            $causes[] = 'wpconfig';
        }
        if (count($causes)) {
            set_transient($GLOBALS['current_user']->ID . '_donotdeactivaterocket', $causes);
            wp_safe_redirect(wp_get_referer());
            die;
        }
    }
    // Delete config files
    rocket_delete_config_file();
    if (!count(glob(WP_ROCKET_CONFIG_PATH . '*.php'))) {
        // Delete All WP Rocket rules of the .htaccess file
        flush_rocket_htaccess(true);
        // Remove WP_CACHE constant in wp-config.php
        set_rocket_wp_cache_define(false);
        // Delete content of advanced-cache.php
        rocket_put_content(WP_CONTENT_DIR . '/advanced-cache.php', '');
    }
    // Update customer key & licence.
    wp_remote_get(WP_ROCKET_WEB_API . 'pause-licence.php', array('blocking' => false));
    delete_transient('rocket_check_licence_30');
    delete_transient('rocket_check_licence_1');
}
Example #6
0
function run_rocket_bot_after_clean_post($post, $purge_urls)
{
    // Run robot only if post is published
    if ($post->post_status != 'publish') {
        return false;
    }
    // Add Homepage URL to $purge_urls for bot crawl
    array_push($purge_urls, home_url());
    // Get the author page
    $purge_author = array(get_author_posts_url($post->post_author));
    // Get all dates archive page
    $purge_dates = get_rocket_post_dates_urls($post->ID);
    // Remove dates archives page and author page to preload cache
    $purge_urls = array_diff($purge_urls, $purge_dates, $purge_author);
    // Create json file and run WP Rocket Bot
    $json_encode_urls = '["' . implode('","', array_filter($purge_urls)) . '"]';
    if (rocket_put_content(WP_ROCKET_PATH . 'cache.json', $json_encode_urls)) {
        global $do_rocket_bot_cache_json;
        $do_rocket_bot_cache_json = true;
    }
}
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);
}