WordPress.org

Ready to get started?Download WordPress

Forums

WP Super Cache
[resolved] Proposed alternative to mfunc/mclude (11 posts)

  1. reviewmylife
    Member
    Posted 3 years ago #

    Hi, WP Super Cache is a great plugin. But I wanted to suggest an improvement.

    The current mfunc/mclude syntax is a bit over complicated. I think people get confused with it because unless you read the PHP code you might not realise that WP Super Cache actually removed the PHP code from inbetween the tags, and then copies the code fragments in mfunc/mclude tags into its own templated PHP tags. A bit wierd!

    Instead of something like this:

    <!--mclude /scripts/adverts.php-->
    <?php require_once(ABSPATH . '/scripts/adverts.php'); ?>
    <!--/mclude-->
    <!--mfunc rml_banner_ad() -->
    <?php rml_banner_ad(); ?>
    <!--/mfunc-->
    <!--mfunc rml_stuff() -->
    <?php rml_stuff(); ?>
    <!--/mfunc-->

    I propose a new dynamic-code tag so the above could be simplified to:

    <!--dynamic-content-->
    <?php
    include_once(ABSPATH . '/scripts/adverts.php');
    rml_banner_ad();
    rml_stuff();
    ?>
    <!--/dynamic-content-->

    This looks simpler, and means you can put any amount of PHP in the tags.

    I hacked together an update to make this work. It just needs an extra block in wp-cache-phase2.php I'm including the previous mfunc/mclude block for context.

    if ( preg_match('/<!--mclude|<!--mfunc/', $buffer)) { //Dynamic content
    		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Dynamic content found in buffer.", 4 );
    		$store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
    				"<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
    		$store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
    				"<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
    		$wp_cache_meta[ 'dynamic' ] = true;
    		/* Clean function calls in tag */
    		$buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
    		$buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
    		$store = apply_filters( 'wpsupercache_buffer', $store );
    		// Append WP Super Cache or Live page comment tag
    		wp_cache_append_tag($buffer);
    		global $wp_super_cache_late_init;
    		if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
    			$buffer .= '<!-- Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details. -->';
    
    		if ( false == $wp_cache_object_cache ) {
    			if( $fr )
    				fputs($fr, $store);
    		} else {
    			wp_cache_set( $oc_key, $store, 'supercache', $cache_max_time );
    		}
    	} else if ( preg_match('/<!--dynamic-content-->/', $buffer)) { //Dynamic content
    		if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Dynamic content found in buffer.", 4 );
    		$store = preg_replace('|<!--dynamic-content-->(.*?)<!--/dynamic-content-->|is',
    				"<!--dynamic-content-->\n$1\n<!--/dynamic-content-->", $buffer);
    		$wp_cache_meta[ 'dynamic' ] = true;
    		/* Clean function calls in tag */
    		$store = apply_filters( 'wpsupercache_buffer', $store );
    		// Append WP Super Cache or Live page comment tag
    		wp_cache_append_tag($buffer);
    		global $wp_super_cache_late_init;
    		if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
    			$buffer .= '<!-- Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details. -->';
    
    		if ( false == $wp_cache_object_cache ) {
    			if( $fr )
    				fputs($fr, $store);
    		} else {
    			wp_cache_set( $oc_key, $store, 'supercache', $cache_max_time );
    		}

    What do you think? Any chance of getting something like this into an updated version of WP Super Cache?

    Cheers.

  2. linko
    Member
    Posted 3 years ago #

    Hey this is really a nice idea, where there any replies?

    This is script work for you reviewmaylife?

    Thanks all!

  3. saharusa
    Member
    Posted 3 years ago #

    I think it's a very good idea, easier to use.

  4. reviewmylife
    Member
    Posted 3 years ago #

    linko - yes the proposed dynamic-content part of the script does work.

  5. Donncha O Caoimh
    Member
    Plugin Author

    Posted 3 years ago #

    I like it. I'll merge it into the development version and reply here when it's ready to be tested.

  6. Donncha O Caoimh
    Member
    Plugin Author

    Posted 3 years ago #

    Actually, this won't work. The code within the php tags in your example will execute when the cached page is generated, and the resulting text is cached by the plugin. That's why the code is duplicated in the original mfunc and mclude tags.

    The php code executes when the page is cached, and the code in the mfunc tag for example will be cached by the plugin.

  7. reviewmylife
    Member
    Posted 3 years ago #

    Hi donncha, You are right! But I'm not giving up yet - I think I have the solution this time. I've been testing this properly (clearing cache, checking the HTML returned on the first and subsequent loads, and checking the on-disk cached file to make sure it all looks correct.

    Instead of:

    <!--mclude /scripts/adverts.php-->
    <?php include_once(ABSPATH . '/scripts/adverts.php'); ?>
    <!--/mclude-->
    <!--mfunc rml_sidebar_ad() -->
    <?php rml_sidebar_ad(); ?>
    <!--/mfunc-->
    <!--mfunc rml_more_stuff() -->
    <?php rml_more_stuff(); ?>
    <!--/mfunc-->

    We now have:

    <!--dynamic-content--><?php
    include_once(ABSPATH . '/scripts/adverts.php');
    rml_sidebar_ad();
    rml_more_stuff();
    ?><!--
    include_once(ABSPATH . '/scripts/adverts.php');
    rml_sidebar_ad();
    rml_more_stuff();
    --><!--/dynamic-content-->

    So the PHP code is executed when the page is first loaded, and the commented out identical code in comments is inserted into the on-disk cache file ready for running on subsequent goes. Not as nice looking as I'd hoped but still a lot clearer than the mfunc/mclude code.

    Here are the WP Super Cache changes. This modifies the existing mfunc/mclude block rather than adding a new one.

    if ( preg_match('/<!--mclude|<!--mfunc/', $buffer) || preg_match('/<!--dynamic-content-->/', $buffer)) { //Dynamic content
            if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Dynamic content found in buffer.", 4 );
            $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
                    "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
            $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
                    "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
            $store = preg_replace('|<!--dynamic-content-->(.*?)<!--(.*?)--><!--/dynamic-content-->|is',
                    "<!--dynamic-content-->\n<?php$2?>\n<!--/dynamic-content-->", $store);
            $wp_cache_meta[ 'dynamic' ] = true;
            /* Clean function calls in tag */
            $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
            $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
            $buffer = preg_replace('|<!--dynamic-content-->(.*?)<!--(.*?)--><!--/dynamic-content-->|is',
                    "<!--dynamic-content-->$1<!--/dynamic-content-->", $buffer);
            $store = apply_filters( 'wpsupercache_buffer', $store );
            // Append WP Super Cache or Live page comment tag
            wp_cache_append_tag($buffer);
            global $wp_super_cache_late_init;
            if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
                $buffer .= '<!-- Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details. -->';
    
            if ( false == $wp_cache_object_cache ) {
                if( $fr )
                    fputs($fr, $store);
            } else {
                wp_cache_set( $oc_key, $store, 'supercache', $cache_max_time );
            }
  8. Donncha O Caoimh
    Member
    Plugin Author

    Posted 3 years ago #

    Looks good. I've just checked it into trunk if you want to give the development version a go (wait 15 minutes if you're reading this as soon as I post this).

    I'll need to update the documentation too of course. Thanks for the patch!

  9. Donncha O Caoimh
    Member
    Plugin Author

    Posted 3 years ago #

    I should have said in my previous comment, I changed the "dynamic-content" text to "dynamic-cached-content" as the former text is probably used by other apps too.

  10. reviewmylife
    Member
    Posted 3 years ago #

    Thanks for adding it. Looking forward to getting rid of those mfunc/mclude tags from my template!

  11. phazei
    Member
    Posted 3 years ago #

    To prevent conflicts in the global namespace it's more standard to select a common prefix for everything you're doing.

    Picking something like "dynamic-cached-content" is completely generic. Yeah, making it long makes it less likely to conflict, but it also makes it more confusing and every time something is added there is nothing connecting anything.

    "super_cache_dynamic" would be much more sensical.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic