/blog.old/wp-content/plugins/wp-super-cache/plugins/dynamic-cache-test.php

https://github.com/chopsuei3/oscc · PHP · 113 lines · 2 code · 4 blank · 107 comment · 0 complexity · d71f7818f2caad92ab5443021f7698d9 MD5 · raw file

  1. <?php
  2. /*
  3. * A function that uses the wpsc_cachedata filter to add a small message
  4. * and the current server time to every web page. The time increments
  5. * on every reload.
  6. *
  7. * On the Advanced Settings page enable "Enable dynamic caching".
  8. *
  9. * dynamic_cache_test_init()
  10. * This function is the first one to be called. This function hooks
  11. * dynamic_cache_test_template() to the WordPress action, wp_footer.
  12. * This script is loaded before WordPress is and the add_action()
  13. * function isn't defined at this time.
  14. * This init function hooks onto the cache action "add_cacheaction"
  15. * that fires after WordPress (and add_action) is loaded.
  16. *
  17. *
  18. * dynamic_cache_test_template_tag()
  19. * This function hooks on to wp_footer and displays the secret template
  20. * tag that will be replaced by our dynamic content on each page view.
  21. *
  22. *
  23. * dynamic_cache_test_filter()
  24. * This function hooks on to the filter through which all the cached data
  25. * sent to visitors is sent.
  26. * In this simple example the template tag is replaced by a html comment
  27. * containing the text "Hello world at " and the current server time.
  28. * If you want to use the output of a WordPress plugin or command you
  29. * must enable "late init" on the settings page. Each time you reload
  30. * the cached page this time will change. View the page source to examine
  31. * this text.
  32. *
  33. * Plugin authors: NEVER define the template tag for your users. Make them
  34. * choose one so it will be unique to their site.
  35. *
  36. * **** MAKE SURE YOU KEEP THE TEMPLATE TAG SECRET ****
  37. *
  38. */
  39. /*
  40. * Uncomment the code below, enable dynamic caching on the Advanced Settings
  41. * page and clear the cache.
  42. * Be sure to define DYNAMIC_CACHE_TEST_TAG too. Make it a random string
  43. * that will never appear on your website. In your own application this
  44. * tag can be whatever you like.
  45. */
  46. /*
  47. define( 'DYNAMIC_CACHE_TEST_TAG', '' ); // CHANGE THIS!
  48. define( 'DYNAMIC_OUTPUT_BUFFER_TAG', '' ); // CHANGE THIS!
  49. if ( DYNAMIC_CACHE_TEST_TAG == '' )
  50. return false;
  51. // To use an output buffer when generating the dynamic section of your web page
  52. // you must generate the HTML/JS/CSS and store it before the page is completed.
  53. // That stored text is used by the wpsc_cachedata filter later when the same
  54. // function is called again.
  55. // You must also create a safety function. This function checks that your newly
  56. // cached page is ready to be processed by the wpsc_cachedata filter. It's not
  57. // required for already cached pages.
  58. // See dynamic_output_buffer_test_safety() for an example. You must add this
  59. // to avoid the following error:
  60. // "PHP Fatal error: ob_start(): Cannot use output buffering in output buffering display handlers in..."
  61. //
  62. // Steps to run example plugin:
  63. // 1. Add the DYNAMIC_OUTPUT_BUFFER_TAG text (as defined above) to your theme where the dynamic content should be.
  64. // 2. Call dynamic_output_buffer_test() from your theme or an action like wp_footer
  65. // 3. Clear all cached files.
  66. function dynamic_output_buffer_test( &$cachedata = 0 ) {
  67. if ( defined( 'DYNAMIC_OB_TEXT' ) )
  68. return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, DYNAMIC_OB_TEXT, $cachedata );
  69. ob_start();
  70. // call the sidebar function, do something dynamic
  71. echo "<p>This is a test. The current time on the server is: " . date( 'H:i:s' ) . "</p>";
  72. $text = ob_get_contents();
  73. ob_end_clean();
  74. if ( $cachedata === 0 ) // called directly from the theme so store the output
  75. define( 'DYNAMIC_OB_TEXT', $text );
  76. else // called via the wpsc_cachedata filter. We only get here in cached pages in wp-cache-phase1.php
  77. return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, $text, $cachedata );
  78. }
  79. add_cacheaction( 'wpsc_cachedata', 'dynamic_output_buffer_test' );
  80. function dynamic_output_buffer_test_safety( $safety ) {
  81. if ( defined( 'DYNAMIC_OB_TEXT' ) )
  82. return 1; // ready to replace tag with dynamic content.
  83. else
  84. return 0; // tag cannot be replaced.
  85. }
  86. add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' );
  87. function dynamic_cache_test_filter( &$cachedata) {
  88. return str_replace( DYNAMIC_CACHE_TEST_TAG, "<!-- Hello world at " . date( 'H:i:s' ) . " -->", $cachedata );
  89. }
  90. add_cacheaction( 'wpsc_cachedata', 'dynamic_cache_test_filter' );
  91. function dynamic_cache_test_template_tag() {
  92. echo DYNAMIC_CACHE_TEST_TAG; // This is the template tag
  93. }
  94. function dynamic_cache_test_init() {
  95. add_action( 'wp_footer', 'dynamic_cache_test_template_tag' );
  96. }
  97. add_cacheaction( 'add_cacheaction', 'dynamic_cache_test_init' );
  98. */
  99. ?>