/df_home/static/test/portalbkd/wp-content/plugins/wp-super-cache/plugins/dynamic-cache-test.php

https://gitlab.com/darmawan.fatria/df-skp-2014 · PHP · 182 lines · 47 code · 13 blank · 122 comment · 8 complexity · 5769526f43801e0f3d5fcedbb9952f49 MD5 · raw file

  1. <?php
  2. /*
  3. * On the Advanced Settings page enable "Enable dynamic caching" and clear
  4. * the cache.
  5. *
  6. * Plugin authors: NEVER define the template tag for your users. Make them
  7. * choose one so it will be unique to their site.
  8. *
  9. * There are two examples in this file. Both use template tags that must be
  10. * kept secret.
  11. *
  12. * GLOSSARY:
  13. *
  14. * Dynamic content: the text or widget you want to show visitors to your site
  15. * that changes every time it's viewed.
  16. * Placeholder/template tag: the string of random characters placed in your
  17. * theme file or printed in an action where the dynamic content will go.
  18. * Output buffer (ob): any text that is printed by PHP to be sent to the browser
  19. * but captured by PHP for further manipulation.
  20. * OB Callback function: A function that is called when the output buffer is
  21. * filled with a html page. The contents of the page are passed to the function
  22. * for processing.
  23. *
  24. * **** MAKE SURE YOU KEEP THE TEMPLATE TAG SECRET ****
  25. */
  26. /*
  27. * EXAMPLE 1
  28. * http://ocaoimh.ie/2013/10/21/shiny-new-dynamic-content-wp-super-cache/
  29. * Replace a string in your theme with the dynamic content.
  30. *
  31. * dynamic_cache_test_init()
  32. * This function is the first one to be called. This function hooks
  33. * dynamic_cache_test_template() to the WordPress action, wp_footer.
  34. * This script is loaded before WordPress is and the add_action()
  35. * function isn't defined at this time.
  36. * This init function hooks onto the cache action "add_cacheaction"
  37. * that fires after WordPress (and add_action) is loaded.
  38. *
  39. *
  40. * dynamic_cache_test_template_tag()
  41. * This function hooks on to wp_footer and displays the secret template
  42. * tag that will be replaced by our dynamic content on each page view.
  43. *
  44. *
  45. * dynamic_cache_test_filter()
  46. * This function hooks on to the filter through which all the cached data
  47. * sent to visitors is sent.
  48. * In this simple example the template tag is replaced by a html comment
  49. * containing the text "Hello world at " and the current server time.
  50. * If you want to use the output of a WordPress plugin or command you
  51. * must enable "late init" on the settings page. Each time you reload
  52. * the cached page this time will change. View the page source to examine
  53. * this text.
  54. *
  55. * Chronology of a request:
  56. * 1. dynamic_cache_test_init() hooks dynamic_cache_test_template_tag() on
  57. * to the wp_footer action. dynamic_cache_test_filter() is hooked on to
  58. * the wpsc_cachedata filter.
  59. * 2. An output buffer is created by WP Super Cache.
  60. * 3. Most of the page is generated by WordPress.
  61. * 4. The wp_footer action fires and the TAG is printed to the page.
  62. * 5. Processing continues and the page is created.
  63. * 6. The output buffer finishes. A WP Super Cache callback function runs
  64. * and saves the output buffer to a cache file. The wpsc_cachedata
  65. * filter is called.
  66. * 7. The function dynamic_cache_test_filter() runs and replaces the TAG in
  67. * the buffer with the "Hello world" string.
  68. * 8. The output buffer is pushed to the browser to be displayed.
  69. */
  70. define( 'DYNAMIC_CACHE_TEST_TAG', '' ); // Change this to a secret placeholder tag
  71. if ( DYNAMIC_CACHE_TEST_TAG != '' ) {
  72. function dynamic_cache_test_safety( $safety ) {
  73. return 1;
  74. }
  75. add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_cache_test_safety' );
  76. function dynamic_cache_test_filter( &$cachedata) {
  77. return str_replace( DYNAMIC_CACHE_TEST_TAG, "<!-- Hello world at " . date( 'H:i:s' ) . " -->", $cachedata );
  78. }
  79. add_cacheaction( 'wpsc_cachedata', 'dynamic_cache_test_filter' );
  80. function dynamic_cache_test_template_tag() {
  81. echo DYNAMIC_CACHE_TEST_TAG; // This is the template tag
  82. }
  83. function dynamic_cache_test_init() {
  84. add_action( 'wp_footer', 'dynamic_cache_test_template_tag' );
  85. }
  86. add_cacheaction( 'add_cacheaction', 'dynamic_cache_test_init' );
  87. }
  88. /*
  89. * EXAMPLE 2
  90. *
  91. * This is going to be complicated. Hang on!
  92. *
  93. * When the cache file for a new page is generated the plugin uses an output
  94. * buffer to capture the page. A callback function processes the buffer and
  95. * writes to the cache file. The placeholder tag for any dynamic content has
  96. * to be written to that cache file but also, it has to be replaced with
  97. * dynamic content before the page is shown to the user.
  98. * More on output buffers here: http://php.net/ob_start
  99. *
  100. * Unfortunately an extra output buffer is often required when capturing dynamic
  101. * content such as sidebar widgets. Due to a quirk of the way PHP works it's
  102. * not possible to have an output buffer run in an output buffer callback. That
  103. * dynamic content has to be generated before the callback function is reached.
  104. * The following error occurs when an output buffer is created in the
  105. * callback function of another output buffer:
  106. * "PHP Fatal error: ob_start(): Cannot use output buffering in output buffering display handlers in..."
  107. *
  108. * In this example the function add_action() isn't available when this file is
  109. * loaded so dynamic_output_buffer_init() is hooked on to the "add_cacheaction"
  110. * cacheaction. That function then hooks dynamic_output_buffer_test() on to the
  111. * familiar wp_footer action.
  112. *
  113. * The first time dynamic_output_buffer_test() runs it generates the dynamic
  114. * content and captures it with ob_start() in the DYNAMIC_OB_TEXT constant.
  115. *
  116. * When the main WP Super Cache output buffer is ready the callback is called.
  117. * This fires the wpsc_cachedata_safety filter. If the DYNAMIC_OB_TEXT constant
  118. * is set, which means dynamic content is ready, then it returns 1, a signal
  119. * that everything is ok.
  120. * Finally, the wpsc_cachedata filter is run. The function
  121. * dynamic_output_buffer_test() is hooked on to it. Since DYNAMIC_OB_TEXT is
  122. * set it replaces the placeholder text with that constant.
  123. * The resulting html is then sent to the browser.
  124. *
  125. * Already cached pages call the safety filter, and then the wpsc_cachedata
  126. * filter so any hooked function must be ready to generate dynamic content. The
  127. * very last line of dynamic_output_buffer_test() replaces the placeholder tag
  128. * with the dynamic content in the cache file.
  129. *
  130. *
  131. * Use an output buffer to capture dynamic content while the page is generated
  132. * and insert into the right place:
  133. * Remember to add the DYNAMIC_OUTPUT_BUFFER_TAG text (as defined below) to
  134. * your theme where the dynamic content should be.
  135. *
  136. * dynamic_output_buffer_test() is a function that uses the wpsc_cachedata
  137. * filter to add a small message and the current server time to every web
  138. * page. The time increments on every reload.
  139. *
  140. */
  141. define( 'DYNAMIC_OUTPUT_BUFFER_TAG', '' ); // Change this to a secret placeholder tag
  142. if ( DYNAMIC_OUTPUT_BUFFER_TAG != '' ) {
  143. function dynamic_output_buffer_test( &$cachedata = 0 ) {
  144. if ( defined( 'DYNAMIC_OB_TEXT' ) )
  145. return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, DYNAMIC_OB_TEXT, $cachedata );
  146. ob_start();
  147. // call the sidebar function, do something dynamic
  148. echo "<p>This is a test. The current time on the server is: " . date( 'H:i:s' ) . "</p>";
  149. $text = ob_get_contents();
  150. ob_end_clean();
  151. if ( $cachedata === 0 ) { // called directly from the theme so store the output
  152. define( 'DYNAMIC_OB_TEXT', $text );
  153. } else // called via the wpsc_cachedata filter. We only get here in cached pages in wp-cache-phase1.php
  154. return str_replace( DYNAMIC_OUTPUT_BUFFER_TAG, $text, $cachedata );
  155. }
  156. add_cacheaction( 'wpsc_cachedata', 'dynamic_output_buffer_test' );
  157. function dynamic_output_buffer_init() {
  158. add_action( 'wp_footer', 'dynamic_output_buffer_test' );
  159. }
  160. add_cacheaction( 'add_cacheaction', 'dynamic_output_buffer_init' );
  161. function dynamic_output_buffer_test_safety( $safety ) {
  162. if ( defined( 'DYNAMIC_OB_TEXT' ) ) // this is set when you call dynamic_output_buffer_test() from the theme
  163. return 1; // ready to replace tag with dynamic content.
  164. else
  165. return 0; // tag cannot be replaced.
  166. }
  167. add_cacheaction( 'wpsc_cachedata_safety', 'dynamic_output_buffer_test_safety' );
  168. }
  169. ?>