Example #1
0
function rocket_specify_image_dimensions($buffer)
{
    /**
     * Filter images dimensions attributes
     *
     * @since 2.2
     *
     * @param bool Do the job or not
     */
    if (!apply_filters('rocket_specify_image_dimensions', false)) {
        return $buffer;
    }
    // Get all images without width or height attribute
    preg_match_all('/<img(?:[^>](?!(height|width)=))*+>/i', $buffer, $images_match);
    foreach ($images_match[0] as $image) {
        // Don't touch lazy-load file (no conflit with Photon (Jetpack))
        if (strpos($image, 'data-lazy-original') || strpos($image, 'data-no-image-dimensions')) {
            continue;
        }
        $tmp = $image;
        // Get link of the file
        preg_match('/src=[\'"]([^\'"]+)/', $image, $src_match);
        // Get infos of the URL
        $image_url = parse_url($src_match[1]);
        // Check if the link isn't external
        if (empty($image_url['host']) || $image_url['host'] == rocket_remove_url_protocol(home_url())) {
            // Get image attributes
            $sizes = getimagesize(ABSPATH . $image_url['path']);
        } else {
            /**
             * Filter distant images dimensions attributes
             *
             * @since 2.2
             *
             * @param bool Do the job or not
             */
            if (ini_get('allow_url_fopen') && apply_filters('rocket_specify_image_dimensions_for_distant', false)) {
                // Get image attributes
                $sizes = getimagesize($image_url['scheme'] . '://' . $image_url['host'] . $image_url['path']);
            }
        }
        if (!empty($sizes)) {
            // Add width and width attribute
            $image = str_replace('<img', '<img ' . $sizes[3], $image);
            // Replace image with new attributes
            $buffer = str_replace($tmp, $image, $buffer);
        }
    }
    return $buffer;
}
Example #2
0
/**
 * Delete one or several cache files
 *
 * @since 2.0 	Delete cache files for all users
 * @since 1.1.0 Add filter rocket_clean_files
 * @since 1.0
 *
 * @param string|array $urls URLs of cache files to be deleted
 * @return void
 */
function rocket_clean_files($urls)
{
    if (is_string($urls)) {
        $urls = (array) $urls;
    }
    /**
     * Filter URLs that the cache file to be deleted
     *
     * @since 1.1.0
     * @param array URLs that will be returned.
     */
    $urls = apply_filters('rocket_clean_files', $urls);
    $urls = array_filter($urls);
    foreach ($urls as $url) {
        /**
         * Fires before the cache file is deleted
         *
         * @since 1.0
         *
         * @param string $url The URL that the cache file to be deleted
         */
        do_action('before_rocket_clean_file', $url);
        /** This filter is documented in inc/front/htaccess.php */
        if (apply_filters('rocket_url_no_dots', false)) {
            $url = str_replace('.', '_', $url);
        }
        if ($dirs = glob(WP_ROCKET_CACHE_PATH . rocket_remove_url_protocol($url), GLOB_NOSORT)) {
            foreach ($dirs as $dir) {
                rocket_rrmdir($dir);
            }
        }
        /**
         * Fires after the cache file is deleted
         *
         * @since 1.0
         *
         * @param string $url The URL that the cache file was deleted
         */
        do_action('after_rocket_clean_file', $url);
    }
}
Example #3
0
/**
 * Rewrite rules to serve the cache file
 *
 * @since 1.0
 *
 * @return string $rules Rules that will be printed
 */
function get_rocket_htaccess_mod_rewrite()
{
    // No rewrite rules for multisite
    if (is_multisite()) {
        return;
    }
    // No rewrite rules for Korean
    if (defined('WPLANG') && 'ko_KR' == WPLANG || 'ko_KR' == get_locale()) {
        return;
    }
    // Get root base
    $home_root = parse_url(home_url());
    $home_root = isset($home_root['path']) ? trailingslashit($home_root['path']) : '/';
    $site_root = parse_url(site_url());
    $site_root = isset($site_root['path']) ? trailingslashit($site_root['path']) : '';
    // Get cache root
    if (strpos(ABSPATH, WP_ROCKET_CACHE_PATH) === false) {
        $cache_root = str_replace($_SERVER['DOCUMENT_ROOT'], '', WP_ROCKET_CACHE_PATH);
    } else {
        $cache_root = $site_root . str_replace(ABSPATH, '', WP_ROCKET_CACHE_PATH);
    }
    /**
     * Replace the dots by underscores to avoid some bugs on some shared hosting services on filenames (not multisite compatible!)
     *
     * @since 1.3.0
     *
     * @param bool true will replace the . by _
     */
    $HTTP_HOST = apply_filters('rocket_url_no_dots', false) ? rocket_remove_url_protocol(home_url()) : '%{HTTP_HOST}';
    /**
     * Allow the path to be fully printed or dependant od %DOCUMENT_ROOT (forced for 1&1 by default)
     *
     * @since 1.3.0
     *
     * @param bool true will force the path to be full
     */
    $is_1and1_or_force = apply_filters('rocket_force_full_path', strpos($_SERVER['DOCUMENT_ROOT'], '/kunden/') === 0);
    $rules = '';
    $gzip_rules = '';
    $enc = '';
    /**
     * Allow to serve gzip cache file
     *
     * @since 2.4
     *
     * @param bool true will force to serve gzip cache file
     */
    if (function_exists('gzencode') && apply_filters('rocket_force_gzip_htaccess_rules', true)) {
        $rules = '<IfModule mod_mime.c>' . PHP_EOL;
        $rules .= 'AddType text/html .html_gzip' . PHP_EOL;
        $rules .= 'AddEncoding gzip .html_gzip' . PHP_EOL;
        $rules .= '</IfModule>' . PHP_EOL;
        $rules .= '<IfModule mod_setenvif.c>' . PHP_EOL;
        $rules .= 'SetEnvIfNoCase Request_URI \\.html_gzip$ no-gzip' . PHP_EOL;
        $rules .= '</IfModule>' . PHP_EOL . PHP_EOL;
        $gzip_rules .= 'RewriteCond %{HTTP:Accept-Encoding} gzip' . PHP_EOL;
        $gzip_rules .= 'RewriteRule .* - [E=WPR_ENC:_gzip]' . PHP_EOL;
        $enc = '%{ENV:WPR_ENC}';
    }
    $rules .= '<IfModule mod_rewrite.c>' . PHP_EOL;
    $rules .= 'RewriteEngine On' . PHP_EOL;
    $rules .= 'RewriteBase ' . $home_root . PHP_EOL;
    $rules .= get_rocket_htaccess_ssl_rewritecond();
    $rules .= $gzip_rules;
    $rules .= 'RewriteCond %{REQUEST_METHOD} GET' . PHP_EOL;
    $rules .= 'RewriteCond %{QUERY_STRING} =""' . PHP_EOL;
    if ($cookies = get_rocket_cache_reject_cookies()) {
        $rules .= 'RewriteCond %{HTTP:Cookie} !(' . $cookies . ') [NC]' . PHP_EOL;
    }
    if ($uri = get_rocket_cache_reject_uri()) {
        $rules .= 'RewriteCond %{REQUEST_URI} !^(' . $uri . ')$ [NC]' . PHP_EOL;
    }
    $rules .= !is_rocket_cache_mobile() ? get_rocket_htaccess_mobile_rewritecond() : '';
    if ($ua = get_rocket_cache_reject_ua()) {
        $rules .= 'RewriteCond %{HTTP_USER_AGENT} !^(' . $ua . ').* [NC]' . PHP_EOL;
    }
    if ($is_1and1_or_force) {
        $rules .= 'RewriteCond "' . str_replace('/kunden/', '/', WP_ROCKET_CACHE_PATH) . $HTTP_HOST . '%{REQUEST_URI}/index%{ENV:WPR_SSL}.html' . $enc . '" -f' . PHP_EOL;
    } else {
        $rules .= 'RewriteCond "%{DOCUMENT_ROOT}/' . ltrim($cache_root, '/') . $HTTP_HOST . '%{REQUEST_URI}/index%{ENV:WPR_SSL}.html' . $enc . '" -f' . PHP_EOL;
    }
    $rules .= 'RewriteRule .* "' . $cache_root . $HTTP_HOST . '%{REQUEST_URI}/index%{ENV:WPR_SSL}.html' . $enc . '" [L]' . PHP_EOL;
    $rules .= '</IfModule>' . PHP_EOL;
    /**
     * Filter rewrite rules to serve the cache file
     *
     * @since 1.0
     *
     * @param string $rules Rules that will be printed
     */
    $rules = apply_filters('rocket_htaccess_mod_rewrite', $rules);
    return $rules;
}