WordPress.org

Plugin Directory

Changeset 791220


Ignore:
Timestamp:
10/21/13 14:13:46 (6 months ago)
Author:
donncha
Message:

Added and output buffer example plugin

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-super-cache/trunk/plugins/dynamic-cache-test.php

    r706419 r791220  
    4848 */ 
    4949 
    50  
    5150/* 
    52  
    53 define( 'DYNAMIC_CACHE_TEST_TAG', '' ); 
     51define( 'DYNAMIC_CACHE_TEST_TAG', '' ); // CHANGE THIS! 
     52define( 'DYNAMIC_OUTPUT_BUFFER_TAG', '' ); // CHANGE THIS! 
    5453 
    5554if ( DYNAMIC_CACHE_TEST_TAG == '' ) 
    5655    return false; 
     56 
     57// To use an output buffer when generating the dynamic section of your web page 
     58// you must generate the HTML/JS/CSS and store it before the page is completed. 
     59// That stored text is used by the wpsc_cachedata filter later when the same 
     60// function is called again. 
     61// You must also create a safety function. This function checks that your newly 
     62// cached page is ready to be processed by the wpsc_cachedata filter. It's not 
     63// required for already cached pages. 
     64// See dynamic_output_buffer_test_safety() for an example. You must add this  
     65// to avoid the following error: 
     66// "PHP Fatal error:  ob_start(): Cannot use output buffering in output buffering display handlers in..." 
     67// 
     68// Steps to run example plugin: 
     69// 1. Add the DYNAMIC_OUTPUT_BUFFER_TAG text (as defined above) to your theme where the dynamic content should be. 
     70// 2. Call dynamic_output_buffer_test() from your theme or an action like wp_footer 
     71// 3. Clear all cached files. 
     72 
     73function dynamic_output_buffer_test( &$cachedata = 0 ) { 
     74    if ( defined( 'DYNAMIC_OB_TEXT' ) ) 
     75        return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, DYNAMIC_OB_TEXT, $cachedata ); 
     76 
     77    ob_start(); 
     78    // call the sidebar function, do something dynamic 
     79    echo "<p>This is a test. The current time on the server is: " . date( 'H:i:s' ) . "</p>"; 
     80    $text = ob_get_contents(); 
     81    ob_end_clean(); 
     82 
     83    if ( $cachedata === 0 ) // called directly from the theme so store the output 
     84        define( 'DYNAMIC_OB_TEXT', $text ); 
     85    else // called via the wpsc_cachedata filter. We only get here in cached pages in wp-cache-phase1.php 
     86        return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, $text, $cachedata ); 
     87 
     88} 
     89add_cacheaction( 'wpsc_cachedata', 'dynamic_output_buffer_test' ); 
     90 
     91function dynamic_output_buffer_test_safety( $safety ) { 
     92    if ( defined( 'DYNAMIC_OB_TEXT' ) ) 
     93        return 1; // ready to replace tag with dynamic content. 
     94    else 
     95        return 0; // tag cannot be replaced. 
     96} 
     97add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' ); 
    5798 
    5899function dynamic_cache_test_filter( &$cachedata) { 
     
    69110} 
    70111add_cacheaction( 'add_cacheaction', 'dynamic_cache_test_init' ); 
    71  
    72112*/ 
    73  
    74113?> 
Note: See TracChangeset for help on using the changeset viewer.