WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: WP Super Cache] Supercaching all template types (4 posts)

  1. bradbeattie
    Member
    Posted 4 years ago #

    The following modification enables wp-super-cache for different themes (like in wp-mobile, for instance). The default behaviour is to use wp-cache for mobile themes, but this doesn't always satisfy performace demands. Anywho, go nuts with it.

    .htaccess modification

    # Base template with gzip
    RewriteCond %{REQUEST_URI} !^.*[^/]$
    RewriteCond %{REQUEST_URI} !^.*//.*$
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
    RewriteCond %{HTTP_user_agent} !^.*(Android|2.0\ MMP|240x320|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|hiptop|IEMobile|iPhone|iPod|KYOCERA/WX310K|LG/U990|MIDP-2.0|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|Playstation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|Windows\ CE|WinWAP).*
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/base-template/$1/index.html.gz -f
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/base-template/$1/index.html.gz [L]
    
    # Base template without gzip
    RewriteCond %{REQUEST_URI} !^.*[^/]$
    RewriteCond %{REQUEST_URI} !^.*//.*$
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
    RewriteCond %{HTTP_user_agent} !^.*(Android|2.0\ MMP|240x320|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|hiptop|IEMobile|iPhone|iPod|KYOCERA/WX310K|LG/U990|MIDP-2.0|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|Playstation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|Windows\ CE|WinWAP).*
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/base-template/$1/index.html -f
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/base-template/$1/index.html [L]
    
    # iPhone template with gzip
    RewriteCond %{REQUEST_URI} !^.*[^/]$
    RewriteCond %{REQUEST_URI} !^.*//.*$
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
    RewriteCond %{HTTP_user_agent} ^.*(iPhone|iPod).*
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/iphone-template/$1/index.html.gz -f
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/iphone-template/$1/index.html.gz [L]
    
    # iPhone template without gzip
    RewriteCond %{REQUEST_URI} !^.*[^/]$
    RewriteCond %{REQUEST_URI} !^.*//.*$
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
    RewriteCond %{HTTP_user_agent} ^.*(iPhone|iPod).*
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/iphone-template/$1/index.html -f
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/iphone-template/$1/index.html [L]
    
    # Mobile template with gzip
    RewriteCond %{REQUEST_URI} !^.*[^/]$
    RewriteCond %{REQUEST_URI} !^.*//.*$
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
    RewriteCond %{HTTP_user_agent} ^.*(Android|2.0\ MMP|240x320|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.0|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|Playstation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|Windows\ CE|WinWAP).*
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/mobile-template/$1/index.html.gz -f
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/mobile-template/$1/index.html.gz [L]
    
    # Mobile template without gzip
    RewriteCond %{REQUEST_URI} !^.*[^/]$
    RewriteCond %{REQUEST_URI} !^.*//.*$
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} !.*=.*
    RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress|wp-postpass_).*$
    RewriteCond %{HTTP_user_agent} ^.*(Android|2.0\ MMP|240x320|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.0|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|Playstation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|Windows\ CE|WinWAP).*
    RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/mobile-template/$1/index.html -f
    RewriteRule ^(.*) /wp-content/cache/supercache/%{HTTP_HOST}/mobile-template/$1/index.html [L]

    wp-cache-phase2.php

    diff -ur Old/wp-content/plugins/wp-super-cache/wp-cache-phase2.php New/wp-content/plugins/wp-super-cache/wp-cache-phase2.php
    --- Old/wp-content/plugins/wp-super-cache/wp-cache-phase2.php   2009-08-12 13:12:18.000000000 -0700
    +++ New/wp-content/plugins/wp-super-cache/wp-cache-phase2.php   2009-08-12 14:49:43.000000000 -0700
    @@ -194,9 +194,14 @@
            global $cached_direct_pages, $cache_path, $wp_cache_request_uri;
            $uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $wp_cache_request_uri ) ) ) );
            $uri = str_replace( '\\', '', $uri );
    -       $dir = strtolower(preg_replace('/:.*$/', '',  $_SERVER["HTTP_HOST"])) . $uri; // To avoid XSS attacks
    +
    +    if (strpos(get_template_directory(), 'carrington')) $template = 'mobile-template/';
    +    else if (strpos(get_template_directory(), 'iwphone')) $template = 'iphone-template/';
    +    else $template = 'base-template/';
    +
    +       $dir = strtolower(preg_replace('/:.*$/', '',  $_SERVER["HTTP_HOST"])) . '/' . $template . $uri; // To avoid XSS attacks
            $dir = apply_filters( 'supercache_dir', $dir );
            $dir = trailingslashit( $cache_path . 'supercache/' . $dir );
            if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) {
    @@ -630,8 +635,12 @@
    
            $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) );
            $dir = $cache_path . 'supercache/' . $siteurl;
    -       prune_super_cache( $dir . $permalink, true, true );
    -       @rmdir( $dir . $permalink );
    +       prune_super_cache( $dir . 'base-template/' . $permalink, true, true );
    +       prune_super_cache( $dir . 'iphone-template/' . $permalink, true, true );
    +       prune_super_cache( $dir . 'mobile-template/' . $permalink, true, true );
    +       @rmdir( $dir . 'base-template/' . $permalink );
    +       @rmdir( $dir . 'iphone-template/' . $permalink );
    +       @rmdir( $dir . 'mobile-template/' . $permalink );
            prune_super_cache( $dir . 'page/', true );
     }
    
    @@ -648,8 +657,12 @@
            if( $super_cache_enabled ) {
                    $siteurl = trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) );
                    // make sure the front page has a rebuild file
    -               prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html', true, true );
    -               prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html.gz', true, true );
    +               prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'base-template/' . 'index.html', true, true );
    +               prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'base-template/' . 'index.html.gz', true, true );
    +               prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'iphone-template/' . 'index.html', true, true );
    +               prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'iphone-template/' . 'index.html.gz', true, true );
    +               prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'mobile-template/' . 'index.html', true, true );
    +               prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'mobile-template/' . 'index.html.gz', true, true );
                    wp_cache_post_id_gc( $siteurl, $post_id );
                    if( get_option( 'show_on_front' ) == 'page' ) {
                            wp_cache_post_id_gc( $siteurl, get_option( 'page_on_front' ) );
    @@ -673,14 +686,22 @@
                                            if ($meta[ 'blog_id' ] == $blog_id  && (!$meta[ 'post' ] || $meta[ 'post' ] == $post_id) ) {
                                                    @unlink($meta_pathname);
                                                    @unlink($content_pathname);
    -                                               @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
    -                                               @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
    +                                               @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'base-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
    +                                               @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'base-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
    +                                               @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'iphone-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
    +                                               @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'iphone-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
    +                                               @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'mobile-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
    +                                               @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'mobile-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
                                            }
                                    } elseif ($meta[ 'blog_id' ] == $blog_id) {
                                            @unlink($meta_pathname);
                                            @unlink($content_pathname);
    -                                       @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
    -                                       @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
    +                                       @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'base-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
    +                                       @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'base-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
    +                                       @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'iphone-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
    +                                       @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'iphone-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
    +                                       @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'mobile-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
    +                                       @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . 'mobile-template/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
                                    }
    
                            }
  2. Donncha O Caoimh
    Member
    Posted 4 years ago #

    Looks good. As I said in my email reply, the php code probably doesn't need to (or should) check for the existence of the mobile plugins. If mobile support is enabled, then leave it up to the plugin to create the correct html.

  3. zImage
    Member
    Posted 4 years ago #

    The latest WP Super Cache exempts quite a few mobile browsers by listing them in the .htaccess. However, I noticed there are some differences between this list and the list of user agents in latest version of WP Touch plugin. Here's the list of missing UA:

    aspen
    dream
    cupcake
    incognito
    webmate

    Moreover, I have a Nokia N810 Internet Tablet that uses MicroB (gecko based) browser and Fennec. I requested these to be included in the next version of WP Touch, so may be it's good to include these in next version of WP Super Cache as weill. UA are:

    Tablet browser
    Fennec

    Kindest regards.

  4. Simon Wheatley
    Code for the People
    Posted 4 years ago #

    Thanks for the patch, bradbeattie. I've modified the code to cope with WPTouch as well as the Carrington and iWPhone plugins/themes as follows:

    if (strpos(get_template_directory(), 'carrington'))<br />
    		$template = 'mobile-template/';<br />
    	else if (strpos(get_template_directory(), 'iwphone') || strpos(get_template_directory(), 'wptouch') )<br />
    		$template = 'iphone-template/';<br />
    	else<br />
    		$template = 'base-template/';<br />

    On a long term note, would it work to replace these conditionals with a filter, something like the following so that plugins can define their own cache dirs relatively easily (would require co-operation from the various mobile theme plugins, obviously):

    $template = apply_filters( 'wp_supercache_tpl', 'base-template/' );<br />

    I found that WPTouch's switch theme functionality wasn't compatible with super caching the mobile templates (the .htaccess rules above don't cope with the WPTouch cookie which stores this information, so when the pages were served in super cache mode you might sometimes get the wrong HTML dependent on which template mode the first person to see the page (i.e. when the cached file was generated) was using). This could have been coped with by adding yet another cache dir for iphone-template-showing-full (or similar) but I preferred to remove the theme switch with the following diff (the two files in question are wptouch.php, and footer.php in the contained default theme within the WPTouch plugin):

    Index: /Users/simon/Projects/SF/site/wp-content/plugins/wptouch/wptouch.php<br />
    ===================================================================<br />
    --- /Users/simon/Projects/SF/site/wp-content/plugins/wptouch/wptouch.php	(revision 2531)<br />
    +++ /Users/simon/Projects/SF/site/wp-content/plugins/wptouch/wptouch.php	(working copy)<br />
    @@ -349,15 +349,19 @@<br />
     		}</p>
    <p> 		$settings = bnc_wptouch_get_settings();<br />
    -		if (isset($_COOKIE[$key])) {<br />
    -			$this->desired_view = $_COOKIE[$key];<br />
    -		} else {<br />
    +		// SW: Commented out the select by cookie stuff<br />
    +//		error_log( "Checking for view" );<br />
    +//		if (isset($_COOKIE[$key])) {<br />
    +//			$this->desired_view = $_COOKIE[$key];<br />
    +//			error_log( "Using cookie selection of $this->desired_view" );<br />
    +//		} else {<br />
    +//			error_log( "Not using cookie selection" );<br />
     			if ( $settings['enable-regular-default'] ) {<br />
     				$this->desired_view = 'normal';<br />
     			} else {<br />
     		  		$this->desired_view = 'mobile';<br />
     			}<br />
    -		}<br />
    +//		}<br />
     	}</p>
    <p> 	function detectAppleMobile($query = '') {<br />
    Index: /Users/simon/Projects/SF/site/wp-content/plugins/wptouch/themes/default/footer.php<br />
    ===================================================================<br />
    --- /Users/simon/Projects/SF/site/wp-content/plugins/wptouch/themes/default/footer.php	(revision 2531)<br />
    +++ /Users/simon/Projects/SF/site/wp-content/plugins/wptouch/themes/default/footer.php	(working copy)<br />
    @@ -1,10 +1,15 @@<br />
     <div id="footer"></p>
    <p>+<!-- SW: Commenting out core footer switch link, as it doesn't work when we're caching mobile templates through the patched WP Super Cache<br />
     	<center><br />
     		<div id="wptouch-switch-link"><br />
    -			<?php wptouch_core_footer_switch_link(); ?><br />
    +			<?php<br />
    +				// SW: Commenting out as explained above<br />
    +			 	// wptouch_core_footer_switch_link();<br />
    +			?><br />
     		</div><br />
     	</center><br />
    +--></p>
    <p> 	<p><?php _e( "All content Copyright �", "wptouch" ); ?> <?php $str = bnc_get_header_title(); echo stripslashes($str); ?></p><br />
     	<p><?php _e( 'Powered by', 'wptouch' ); ?> <a href="http://www.wordpress.org/">WordPress <?php bloginfo('version'); ?></a> <?php _e( '+', 'wptouch' ); ?> <a href="http://www.wptouch.com"><?php WPtouch(); ?></a></p><br />

    Hope this helps someone.

    S

Topic Closed

This topic has been closed to new replies.

About this Topic