Advertisement
UHLHosting

cache minimal

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