Advertisement
UHLHosting

No

Jan 17th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Class and Function List:
  5. * Function list:
  6. * - enable_dynamic_caching()
  7. * - html_cache()
  8. * - get_html_cache()
  9. * - add_cache_stats_in_admin_menu()
  10. * - cache_stats_page()
  11. * - clean_cache()
  12. * - add_cache_stats_in_admin_bar()
  13. * - clean_cache_after_7_days()
  14. */
  15. // Enable dynamic caching
  16. add_action('init', 'enable_dynamic_caching');
  17.  
  18. function enable_dynamic_caching()
  19. {
  20. // Create HTML cache of pages in WordPress
  21. if (!is_admin())
  22. {
  23. ob_start('html_cache');
  24. }
  25. }
  26.  
  27. // Serve the HTML pages to visitors
  28. function html_cache($buffer)
  29. {
  30. // Do not cache WooCommerce pages
  31. if (function_exists('is_woocommerce') && is_woocommerce())
  32. {
  33. return $buffer;
  34. }
  35. // Do not cache logged in users
  36. if (is_user_logged_in())
  37. {
  38. return $buffer;
  39. }
  40. // Add a comment in footer source code with page load time
  41. $buffer .= <<<EOT
  42. <!-- {get_bloginfo('name')} -->
  43. <!-- Page generated in {timer_stop(0)} seconds. -->
  44. <!-- Last edited: {get_the_modified_date()}. -->
  45. <!-- Page size: {size_format(strlen($buffer))}. -->
  46. <!-- Expiration date: {date('Y-m-d H:i:s', time() + 604800)}. -->
  47. <!-- Cache file: {WP_CONTENT_DIR}/cache/{md5($_SERVER['REQUEST_URI'])}.html -->
  48. EOT;
  49. // $buffer .= '<!-- Page generated in ' . timer_stop(0) . ' seconds. -->' . "\n" . '<!-- Last edited: ' . get_the_modified_date() . '. -->' . "\n" . '<!-- Page size: ' . size_format(strlen($buffer)) . '. -->' . "\n" . '<!-- Expiration date: ' . date('Y-m-d H:i:s', time() + 604800) . '. -->' . "\n" . '<!-- Cache file: ' . WP_CONTENT_DIR . '/cache/' . md5($_SERVER['REQUEST_URI']) . '.html -->' . "\n" . '<!-- ' . get_bloginfo('name') . ' -->' . "\n";
  50. // Update the HTML pages on changes in content
  51. if (is_singular())
  52. {
  53. $buffer = get_html_cache($buffer);
  54. }
  55. return $buffer;
  56. }
  57.  
  58. // Get HTML cache
  59. function get_html_cache($buffer)
  60. {
  61. $url = $_SERVER['REQUEST_URI'];
  62. $html_cache_file = md5($url) . '.html';
  63. $html_cache_path = WP_CONTENT_DIR . '/cache/' . $html_cache_file;
  64. global $wp_filesystem;
  65. if (!$wp_filesystem)
  66. {
  67. require_once (ABSPATH . '/wp-admin/includes/file.php');
  68. WP_Filesystem();
  69. }
  70. if ($wp_filesystem->exists($html_cache_path))
  71. {
  72. $buffer = file_get_contents($html_cache_path);
  73. }
  74. else
  75. {
  76. file_put_contents($html_cache_path, $buffer);
  77. }
  78. return $buffer;
  79. }
  80.  
  81. // Add cache stats in Cache menu in Wordpress admin
  82. add_action('admin_menu', 'add_cache_stats_in_admin_menu');
  83.  
  84. function add_cache_stats_in_admin_menu()
  85. {
  86. add_submenu_page('options-general.php', 'Cache Stats', 'Cache Stats', 'manage_options', 'cache-stats', 'cache_stats_page');
  87. }
  88.  
  89. function cache_stats_page()
  90. {
  91. $cache_path = WP_CONTENT_DIR . '/cache/';
  92. $cache_files = glob($cache_path . '*.html');
  93. $cache_count = count($cache_files);
  94. $cache_size = 0;
  95. foreach ($cache_files as $cache_file)
  96. {
  97. $cache_size += filesize($cache_file);
  98. }
  99. $cache_size = size_format($cache_size);
  100. echo '<div class="wrap">';
  101. echo '<h1>Cache Stats</h1>';
  102. echo '<p>Number of cached pages: ' . $cache_count . '</p>';
  103. echo '<p>Total cache size: ' . $cache_size . '</p>';
  104. echo '<form method="post">';
  105. echo '<input type="submit" name="clean_cache" value="Clean Cache" class="button button-primary">';
  106. echo '</form>';
  107. echo '</div>';
  108. }
  109.  
  110. // Clean cache
  111. add_action('admin_init', 'clean_cache');
  112.  
  113. function clean_cache()
  114. {
  115. if (isset($_POST['clean_cache']))
  116. {
  117. $cache_path = WP_CONTENT_DIR . '/cache/';
  118. $cache_files = glob($cache_path . '*.html');
  119. foreach ($cache_files as $cache_file)
  120. {
  121. unlink($cache_file);
  122. }
  123. wp_redirect(admin_url('options-general.php?page=cache-stats'));
  124. }
  125. }
  126.  
  127. // Add cache stats in WordPress top bar
  128. add_action('admin_bar_menu', 'add_cache_stats_in_admin_bar', 100);
  129.  
  130. function add_cache_stats_in_admin_bar($wp_admin_bar)
  131. {
  132. $cache_path = WP_CONTENT_DIR . '/cache/';
  133. $cache_files = glob($cache_path . '*.html');
  134. $cache_count = count($cache_files);
  135. $cache_size = 0;
  136. foreach ($cache_files as $cache_file)
  137. {
  138. $cache_size += filesize($cache_file);
  139. }
  140. $cache_size = size_format($cache_size);
  141. $args = array(
  142. 'id' => 'cache-stats',
  143. 'title' => 'Cache Stats',
  144. 'meta' => array(
  145. 'class' => 'cache-stats',
  146. 'title' => 'Cache Stats'
  147. )
  148. );
  149. $wp_admin_bar->add_node($args);
  150. $args = array(
  151. 'id' => 'cache-stats-number',
  152. 'title' => 'Number of cached pages: ' . $cache_count,
  153. 'parent' => 'cache-stats',
  154. 'meta' => array(
  155. 'class' => 'cache-stats-number',
  156. 'title' => 'Number of cached pages: ' . $cache_count
  157. )
  158. );
  159. $wp_admin_bar->add_node($args);
  160. $args = array(
  161. 'id' => 'cache-stats-size',
  162. 'title' => 'Total cache size: ' . $cache_size,
  163. 'parent' => 'cache-stats',
  164. 'meta' => array(
  165. 'class' => 'cache-stats-size',
  166. 'title' => 'Total cache size: ' . $cache_size
  167. )
  168. );
  169. $wp_admin_bar->add_node($args);
  170. }
  171.  
  172. // Clean cache after 7 days
  173. add_action('init', 'clean_cache_after_7_days');
  174.  
  175. function clean_cache_after_7_days()
  176. {
  177. $cache_path = WP_CONTENT_DIR . '/cache/';
  178. $cache_files = glob($cache_path . '*.html');
  179. foreach ($cache_files as $cache_file)
  180. {
  181. if (filemtime($cache_file) < time() - 604800)
  182. {
  183. unlink($cache_file);
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement