diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist
index cf4d6e8c..70f89685 100644
--- a/.phpcs.xml.dist
+++ b/.phpcs.xml.dist
@@ -56,4 +56,31 @@
+
+
+
+ /tests/php/*
+
+
+
+ /inc/plugins-cookies\.php
+ /inc/cache-files\.php
+ /inc/htaccess\.php
+ /inc/settings-forms\.php
+ /inc/preload\.php
+ /inc/lifecycle\.php
+ /inc/admin-notices\.php
+ /inc/admin-ui\.php
diff --git a/CONTEXT.md b/CONTEXT.md
new file mode 100644
index 00000000..dd652cac
--- /dev/null
+++ b/CONTEXT.md
@@ -0,0 +1,62 @@
+# WP Super Cache — Context
+
+WP Super Cache is a procedural WordPress caching plugin. Most code is global
+functions (no namespaces); `src/` is reserved for classmap-autoloaded classes,
+and `inc/` holds the global-function include files.
+
+## Entry points
+
+- **`wp-cache.php`** — the plugin main file, reduced to a thin loader: plugin
+ header, constants, the `require_once` include list, `wpsc_init()` and its init
+ wiring, the frontend `wp_die`/`set_home` hooks, and the
+ activation/deactivation/uninstall registrations (which must stay here so
+ `__FILE__` resolves to the main plugin file).
+- **`wp-cache-phase2.php`** — the drop-in caching engine on the request hot path
+ (served via `advanced-cache.php`). Not part of the admin/lifecycle split.
+
+## `inc/` file map (admin & lifecycle clusters)
+
+Each file owns one responsibility plus its own `add_action`/`add_filter`
+registrations. Admin-only function clusters live in dedicated `inc/` files.
+
+| File | Responsibility |
+|------|----------------|
+| `inc/boost.php` | Jetpack Boost migration notice/AJAX and the install banner |
+| `inc/plugins-cookies.php` | WPSC plugin-list & extra-cookie management; feed GC |
+| `inc/cache-files.php` | Admin cache-file listing, stats, sizing, cleaning; per-post invalidation |
+| `inc/htaccess.php` | `.htaccess` / mod_rewrite rule generation and management |
+| `inc/settings-forms.php` | Settings-page form handlers and validators |
+| `inc/preload.php` | The preload subsystem (status, cron worker, scheduling, UI, AJAX) |
+| `inc/lifecycle.php` | Activation/deactivation/uninstall, enable/disable, advanced-cache + config/cache-dir management, GC defaults |
+| `inc/admin-notices.php` | Admin notices, plugin-row UI, the front-page site check |
+| `inc/admin-ui.php` | The settings screen: menus, enqueues, the renderer (incl. inline JS), tabs, render helpers |
+| `inc/delete-cache-button.php`, `inc/preload-notification.php` | Pre-existing standalone admin features |
+
+The `inc/` files are loaded by the `require_once` block at the top of
+`wp-cache.php`, which runs before `wpsc_init()`, so relocated functions are
+defined in time for their hooks.
+
+### Relocated-code conventions
+
+- Functions in the relocated clusters keep their original global names and
+ signatures — themes/plugins calling them see no change.
+- `__DIR__` / `__FILE__` inside relocated code resolves to the plugin root via
+ `dirname( __DIR__ )` (the `inc/` files sit one level below root).
+- The relocated procedural includes carry pre-existing WPCS debt verbatim and
+ are excluded from PHPCS in `.phpcs.xml.dist`; modernizing them is a tracked
+ follow-up, separate from the relocation. Newly authored `inc/` files remain
+ fully linted.
+
+## Tests
+
+Two tiers (see `tests/php/README.md`):
+
+- **CI smoke** — `composer test-php`, PHPUnit 9.6, no database; covers
+ WordPress-free helpers in `wp-cache-phase2.php`.
+- **Local integration** — `make test-integration`, `WP_UnitTestCase` + a real
+ database in the wp-env Docker environment; the bootstrap loads `wp-cache.php`
+ so the relocated procedural functions run under a real WP runtime.
+- **e2e** — `composer test-e2e` (Docker/Jest); covers activation and the
+ settings UI, including behaviour that depends on plugin-root path resolution.
+
+See `docs/adr/` for the decisions behind this layout.
diff --git a/docs/adr/0001-split-wp-cache-into-inc-files.md b/docs/adr/0001-split-wp-cache-into-inc-files.md
new file mode 100644
index 00000000..d9ee5928
--- /dev/null
+++ b/docs/adr/0001-split-wp-cache-into-inc-files.md
@@ -0,0 +1,61 @@
+# 1. Split wp-cache.php into per-responsibility inc/ files
+
+Date: 2026-06-17
+
+## Status
+
+Accepted
+
+## Context
+
+`wp-cache.php` had grown to ~4,500 lines holding ~120 global functions with
+their hook registrations interleaved inline — lifecycle, admin UI, preload,
+htaccess generation, cache-file management, settings forms, and Jetpack Boost
+banners all in one file, changing at different rates. Navigating or safely
+editing it was slow, and there was no PHP unit/integration coverage.
+
+Issue #1061 (executing #1047 item 4) called for splitting the file along its
+responsibilities, preceded by a characterization test net, as a **pure
+relocation** with zero behaviour change.
+
+## Decision
+
+Move the global-function clusters into ~9 focused `inc/` files (the existing
+convention for non-class includes; `src/` stays reserved for classmap-autoloaded
+classes), one responsibility per file, each carrying its own
+`add_action`/`add_filter` registrations. `wp-cache.php` becomes a thin loader.
+The file map is documented in `CONTEXT.md`.
+
+Constraints held: function names, signatures, hook names, and load order are
+unchanged; `wp-cache-phase2.php` (the hot-path engine) is untouched; no classes,
+shims, or global-state reduction. Each cluster was characterization-tested before
+it moved.
+
+A few things are position-dependent and therefore deviate from a literal
+byte-for-byte move, while preserving behaviour:
+
+- `register_activation_hook` / `register_deactivation_hook` /
+ `register_uninstall_hook` stay in `wp-cache.php` so `__FILE__` resolves to the
+ main plugin file; their handlers live in `inc/lifecycle.php`.
+- `__DIR__` / `__FILE__` in relocated code resolves to the plugin root via
+ `dirname( __DIR__ )` so asset URLs, `WPCACHEHOME`, `advanced-cache.php`
+ generation, and partial-template paths stay correct.
+
+## Consequences
+
+- The plugin's behaviour is unchanged: the CI smoke suite (34), the local
+ integration suite (37), and the e2e suite (28) all pass. The e2e suite proved
+ essential — it caught the `__FILE__`/`__DIR__` path regressions that the
+ unit/integration tiers do not exercise.
+- Each `inc/` file is small and focused, so future changes touch one
+ responsibility at a time.
+- The relocated procedural includes carry their pre-existing WPCS debt verbatim
+ and are excluded from PHPCS (documented in `.phpcs.xml.dist`). **Follow-up:**
+ modernize the relocated `inc/` files (strict comparisons, escaping, spacing)
+ and remove the exclusions; a deeper pass can then reduce global state (e.g.
+ turn `inc/preload.php` into the #5 preload state machine).
+- Because the changed-lines linter (`phpcs-changed`) diffs the cumulative branch
+ against trunk, a partially-moved tree can make `git` attribute surviving
+ legacy as "added." Only the original and the fully-moved states are
+ lint-clean, so this work landed as a single relocation commit rather than one
+ commit per cluster.
diff --git a/inc/admin-notices.php b/inc/admin-notices.php
new file mode 100644
index 00000000..6c1c6e1d
--- /dev/null
+++ b/inc/admin-notices.php
@@ -0,0 +1,203 @@
+
' . __( 'Your server is configured to show files and directories, which may expose sensitive data such as login cookies to attackers in the cache directories. That has been fixed by adding a file named index.html to each directory. If you use simple caching, consider moving the location of the cache directory on the Advanced Settings page.', 'wp-super-cache' ) . '
';
+ echo "
";
+ _e( 'If you just installed WP Super Cache for the first time, you can dismiss this message. Otherwise, you should probably refresh the login cookies of all logged in WordPress users here by clicking the logout link below.', 'wp-super-cache' );
+ echo "
";
+ echo '
' . esc_html__( 'The logout link will log out all WordPress users on this site except you. Your authentication cookie will be updated, but you will not be logged out.', 'wp-super-cache' ) . '
' . sprintf( __( 'WP Super Cache must be configured. Go to the admin page to enable and configure the plugin.', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) . '
' . sprintf( __('WP Super Cache is disabled. Please go to the plugin admin page to enable caching.', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) . '
' . sprintf( __( 'Warning! WP Super Cache caching was broken but has been fixed! The script advanced-cache.php could not load wp-cache-phase1.php.
The file %1$s/advanced-cache.php has been recreated and WPCACHEHOME fixed in your wp-config.php. Reload to hide this message.', 'wp-super-cache' ), WP_CONTENT_DIR ) . '
';
+ }
+ }
+}
+add_action( 'admin_notices', 'wp_cache_admin_notice' );
+
+function wp_cache_check_site() {
+ global $wp_super_cache_front_page_check, $wp_super_cache_front_page_clear, $wp_super_cache_front_page_text, $wp_super_cache_front_page_notification, $wpdb;
+
+ if ( empty( $wp_super_cache_front_page_check ) ) {
+ return false;
+ }
+
+ if ( function_exists( "wp_remote_get" ) == false ) {
+ return false;
+ }
+ $front_page = wp_remote_get( site_url(), array('timeout' => 60, 'blocking' => true ) );
+ if( is_array( $front_page ) ) {
+ // Check for gzipped front page
+ if ( $front_page[ 'headers' ][ 'content-type' ] == 'application/x-gzip' ) {
+ if ( ! isset( $wp_super_cache_front_page_clear ) || $wp_super_cache_front_page_clear === 0 ) {
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is gzipped! Please clear cache!', 'wp-super-cache' ), home_url() ), sprintf( __( "Please visit %s to clear the cache as the front page of your site is now downloading!", 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) );
+ } else {
+ wp_cache_clear_cache( $wpdb->blogid );
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is gzipped! Cache Cleared!', 'wp-super-cache' ), home_url() ), sprintf( __( "The cache on your blog has been cleared because the front page of your site is now downloading. Please visit %s to verify the cache has been cleared.", 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) );
+ }
+ }
+
+ // Check for broken front page
+ if (
+ ! empty( $wp_super_cache_front_page_text )
+ && ! str_contains( $front_page['body'], $wp_super_cache_front_page_text )
+ ) {
+ if ( ! isset( $wp_super_cache_front_page_clear ) || $wp_super_cache_front_page_clear === 0 ) {
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is not correct! Please clear cache!', 'wp-super-cache' ), home_url() ), sprintf( __( 'Please visit %1$s to clear the cache as the front page of your site is not correct and missing the text, "%2$s"!', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ), $wp_super_cache_front_page_text ) );
+ } else {
+ wp_cache_clear_cache( $wpdb->blogid );
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is not correct! Cache Cleared!', 'wp-super-cache' ), home_url() ), sprintf( __( 'The cache on your blog has been cleared because the front page of your site is missing the text "%2$s". Please visit %1$s to verify the cache has been cleared.', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ), $wp_super_cache_front_page_text ) );
+ }
+ }
+ }
+ if ( isset( $wp_super_cache_front_page_notification ) && $wp_super_cache_front_page_notification == 1 ) {
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page check!', 'wp-super-cache' ), home_url() ), sprintf( __( "WP Super Cache has checked the front page of your blog. Please visit %s if you would like to disable this.", 'wp-super-cache' ) . "\n\n", admin_url( 'options-general.php?page=wpsupercache' ) ) );
+ }
+
+ if ( !wp_next_scheduled( 'wp_cache_check_site_hook' ) ) {
+ wp_schedule_single_event( time() + 360 , 'wp_cache_check_site_hook' );
+ wp_cache_debug( 'scheduled wp_cache_check_site_hook for 360 seconds time.', 2 );
+ }
+}
+add_action( 'wp_cache_check_site_hook', 'wp_cache_check_site' );
diff --git a/inc/admin-ui.php b/inc/admin-ui.php
new file mode 100644
index 00000000..1706e033
--- /dev/null
+++ b/inc/admin-ui.php
@@ -0,0 +1,1160 @@
+ wp_create_nonce( 'wpsc_dismiss_boost_notice' ),
+ 'boostDismissNonce' => wp_create_nonce( 'wpsc_dismiss_boost_banner' ),
+ 'boostInstallNonce' => wp_create_nonce( 'updates' ),
+ 'boostActivateNonce' => wp_create_nonce( 'activate-boost' ),
+ )
+ );
+}
+add_action( 'admin_enqueue_scripts', 'wp_super_cache_admin_enqueue_scripts' );
+
+/**
+ * Use the standard WordPress plugin installation ajax handler.
+ */
+add_action( 'wp_ajax_wpsc_install_plugin', 'wp_ajax_install_plugin' );
+
+function wp_cache_manager_error_checks() {
+ global $wp_cache_debug, $wp_cache_cron_check, $cache_enabled, $super_cache_enabled, $wp_cache_config_file, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes, $wp_cache_mobile_browsers, $wp_cache_mobile_enabled, $wp_cache_mod_rewrite;
+ global $dismiss_htaccess_warning, $dismiss_readable_warning, $dismiss_gc_warning, $wp_cache_shutdown_gc, $is_nginx;
+ global $htaccess_path;
+
+ if ( ! wpsupercache_site_admin() ) {
+ return false;
+ }
+
+ // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved -- Version is checked before access.
+ if ( PHP_VERSION_ID < 50300 && ( ini_get( 'safe_mode' ) === '1' || strtolower( ini_get( 'safe_mode' ) ) === 'on' ) ) { // @codingStandardsIgnoreLine
+ echo '
' . esc_html__( 'You may experience problems running this plugin because SAFE MODE is enabled.', 'wp-super-cache' ) . ' ';
+
+ // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_mode_gidDeprecatedRemoved -- Version is checked before access.
+ if ( ! ini_get( 'safe_mode_gid' ) ) { // @codingStandardsIgnoreLine
+ esc_html_e( 'Your server is set up to check the owner of PHP scripts before allowing them to read and write files.', 'wp-super-cache' );
+ echo ' ';
+ printf( __( 'You or an administrator may be able to make it work by changing the group owner of the plugin scripts to match that of the web server user. The group owner of the %s/cache/ directory must also be changed. See the safe mode manual page for further details.', 'wp-super-cache' ), esc_attr( WP_CONTENT_DIR ) );
+ } else {
+ _e( 'You or an administrator must disable this. See the safe mode manual page for further details. This cannot be disabled in a .htaccess file unfortunately. It must be done in the php.ini config file.', 'wp-super-cache' );
+ }
+ echo '
" . __( 'A custom url or permalink structure is required for this plugin to work correctly. Please go to the Permalinks Options Page to configure your permalinks.', 'wp-super-cache' ) . "
Troubleshooting section of the readme.txt', 'wp-super-cache' ), 'https://wordpress.org/plugins/wp-super-cache/faq/' ); ?>
+
+ ' . __( "Cannot continue... fix previous problems and retry.", 'wp-super-cache' ) . '';
+ return false;
+ }
+
+ if ( false == function_exists( 'wpsc_deep_replace' ) ) {
+ $msg = __( 'Warning! You must set WP_CACHE and WPCACHEHOME in your wp-config.php for this plugin to work correctly:' ) . ' ';
+ $msg .= "define( 'WP_CACHE', true ); ";
+ $msg .= "define( 'WPCACHEHOME', '" . dirname( __DIR__ ) . "/' ); ";
+ wp_die( $msg );
+ }
+
+ if (!wp_cache_check_global_config()) {
+ return false;
+ }
+
+ if ( 1 == ini_get( 'zlib.output_compression' ) || "on" == strtolower( ini_get( 'zlib.output_compression' ) ) ) {
+ ?>
+
this page for instructions on modifying your php.ini.', 'wp-super-cache' ); ?>
+
+
%s/wp-cache-config.php and cannot be modified. That file must be writeable by the web server to make any changes.', 'wp-super-cache' ), WPCACHECONFIGPATH ); ?>
+
+
This page explains how to change file permissions.', 'wp-super-cache' ); ?>
' . esc_html__( 'For best performance you should enable "Mobile device support" or delete the mobile rewrite rules in your .htaccess. Look for the 2 lines with the text "2.0\ MMP|240x320" and delete those.', 'wp-super-cache' ) . '
' . esc_html__( 'This will have no affect on ordinary users but mobile users will see uncached pages.', 'wp-super-cache' ) . '
+ # BEGIN WPSuperCache and # END WPSuperCache and let the plugin regenerate them by reloading this page.', 'wp-super-cache' ), array( 'code' => array() ) ), esc_html( $home_path ) );
+ ?>
+
+
+ # BEGIN WPSuperCache and # END WPSuperCache. There are two sections that look very similar. Just below the line %%{HTTP:Cookie} !^.*(comment_author_|%2$s|wp-postpass_).*$ add these lines: (do it twice, once for each section)', 'wp-super-cache' ), array( 'code' => array() ) ), esc_html( $home_path ), esc_html( wpsc_get_logged_in_cookie() ) );
+ ?>
+
+
+ __( 'Required to serve compressed supercache files properly.', 'wp-super-cache' ), 'mod_headers' => __( 'Required to set caching information on supercache pages. IE7 users will see old pages without this module.', 'wp-super-cache' ), 'mod_expires' => __( 'Set the expiry date on supercached pages. Visitors may not see new pages when they refresh or leave comments without this module.', 'wp-super-cache' ) );
+ foreach( $required_modules as $req => $desc ) {
+ if( !in_array( $req, $mods ) ) {
+ $missing_mods[ $req ] = $desc;
+ }
+ }
+ if( isset( $missing_mods) && is_array( $missing_mods ) ) {
+ ?>
here. Unfortunately, WordPress writes to the .htaccess in the install directory, not where your site is served from. When you update the rewrite rules in this plugin you will have to copy the file to where your site is hosted. This will be fixed in the future.', 'wp-super-cache' ); ?>
' . __( "Warning! You attempted to enable compression but zlib.output_compression is enabled. See #21 in the Troubleshooting section of the readme file.", 'wp-super-cache' ) . '
development version for the latest fixes (changelog).', 'wp-super-cache' ), 'https://odd.blog/y/2o', 'https://plugins.trac.wordpress.org/log/wp-super-cache/' ); ?>
+
+
+
rate us and give feedback.', 'wp-super-cache' ), 'https://wordpress.org/support/plugin/wp-super-cache/reviews?rate=5#new-post' ); ?>
' . esc_html__( 'Cache plugins are PHP scripts you\'ll find in a dedicated folder inside the WP Super Cache folder (wp-super-cache/plugins/). They load at the same time as WP Super Cache, and before regular WordPress plugins.', 'wp-super-cache' ) . '
';
+ echo '
' . esc_html__( 'Keep in mind that cache plugins are for advanced users only. To create and manage them, you\'ll need extensive knowledge of both PHP and WordPress actions.', 'wp-super-cache' ) . '
';
+ echo '
' . sprintf( __( 'Warning! Due to the way WordPress upgrades plugins, the ones you upload to the WP Super Cache folder (wp-super-cache/plugins/) will be deleted when you upgrade WP Super Cache. To avoid this loss, load your cache plugins from a different location. When you set $wp_cache_plugins_dir to the new location in wp-config.php, WP Super Cache will look there instead. You can find additional details in the developer documentation.', 'wp-super-cache' ), 'https://odd.blog/wp-super-cache-developers/' ) . '
" . sprintf( __( 'The file %s.htaccess cannot be modified by the web server. Please correct this using the chmod command or your ftp client.', 'wp-super-cache' ), $home_path ) . "
" . __( 'Refresh this page when the file permissions have been modified.' ) . "
" . sprintf( __( 'Alternatively, you can edit your %s.htaccess file manually and add the following code (before any WordPress rules):', 'wp-super-cache' ), $home_path ) . "
";
+ echo "
# BEGIN WPSuperCache\n" . esc_html( $rules ) . "# END WPSuperCache
" . sprintf( __( 'To serve static html files your server must have the correct mod_rewrite rules added to a file called %s.htaccess', 'wp-super-cache' ), $home_path ) . " ";
+ _e( "You can edit the file yourself. Add the following rules.", 'wp-super-cache' );
+ echo __( " Make sure they appear before any existing WordPress rules. ", 'wp-super-cache' ) . "
";
+ echo "
";
+ echo "
# BEGIN WPSuperCache\n" . esc_html( $rules ) . "# END WPSuperCache
";
+ echo "
";
+ echo "
" . sprintf( __( 'Rules must be added to %s too:', 'wp-super-cache' ), WP_CONTENT_DIR . "/cache/.htaccess" ) . "
";
+ echo "
";
+ echo "
# BEGIN supercache\n" . esc_html( $gziprules ) . "# END supercache
' . __( 'Warning! You may not be allowed to use this plugin on your site.', 'wp-super-cache' ) . "
";
+ echo '
' .
+ sprintf(
+ __( 'The file %s was created by another plugin or by your system administrator. Please examine the file carefully by FTP or SSH and consult your hosting documentation. ', 'wp-super-cache' ),
+ $wpsc_advanced_cache_filename
+ ) .
+ '
';
+ echo '
' .
+ __( 'If it was created by another caching plugin please uninstall that plugin first before activating WP Super Cache. If the file is not removed by that action you should delete the file manually.', 'wp-super-cache' ),
+ '
';
+ echo '
' .
+ __( 'If you need support for this problem contact your hosting provider.', 'wp-super-cache' ),
+ '
" .
+ sprintf(
+ __( 'Make %1$s writable using the chmod command through your ftp or server software. (chmod 777 %1$s) and refresh this page. This is only a temporary measure and you’ll have to make it read only afterwards again. (Change 777 to 755 in the previous command)', 'wp-super-cache' ),
+ WP_CONTENT_DIR
+ ) .
+ "
";
+ echo "
" . sprintf( __( 'Refresh this page to update %s/advanced-cache.php', 'wp-super-cache' ), WP_CONTENT_DIR ) . "
";
+ echo sprintf( __( 'If that doesn’t work, make sure the file %s/advanced-cache.php doesn’t exist:', 'wp-super-cache' ), WP_CONTENT_DIR ) . "";
+ echo "";
+ echo '
The WP_CACHE constant is used by WordPress to load the code that serves cached pages. Unfortunately, it is set to false. Please edit your wp-config.php and add or edit the following line above the final require_once command:
define('WP_CACHE', true);
", 'wp-super-cache' ) . "";
+ } else {
+ echo '
' . __( "Error: WP_CACHE is not enabled in your wp-config.php file and I couldn’t modify it.", 'wp-super-cache' ) . "
";
+ echo "
" . sprintf( __( "Edit %s and add the following line: define('WP_CACHE', true); Otherwise, WP-Cache will not be executed by WordPress core. ", 'wp-super-cache' ), $global_config_file ) . "
";
+ }
+ return false;
+ } else {
+ echo "
" . __( '
WP_CACHE constant added to wp-config.php
If you continue to see this warning message please see point 5 of the Troubleshooting Guide. The WP_CACHE line must be moved up.', 'wp-super-cache' ) . "
";
+ }
+ return true;
+}
+
+function wp_cache_disable_plugin( $delete_config_file = true ) {
+ global $wp_rewrite;
+ if ( file_exists( ABSPATH . 'wp-config.php') ) {
+ $global_config_file = ABSPATH . 'wp-config.php';
+ } else {
+ $global_config_file = dirname(ABSPATH) . '/wp-config.php';
+ }
+
+ if ( apply_filters( 'wpsc_enable_wp_config_edit', true ) ) {
+ $line = 'define(\'WP_CACHE\', true);';
+ if (
+ strpos( file_get_contents( $global_config_file ), $line ) &&
+ (
+ ! is_writeable_ACLSafe( $global_config_file ) ||
+ ! wp_cache_replace_line( 'define*\(*\'WP_CACHE\'', '', $global_config_file )
+ )
+ ) {
+ wp_die( "Could not remove WP_CACHE define from $global_config_file. Please edit that file and remove the line containing the text 'WP_CACHE'. Then refresh this page." );
+ }
+ $line = 'define( \'WPCACHEHOME\',';
+ if (
+ strpos( file_get_contents( $global_config_file ), $line ) &&
+ (
+ ! is_writeable_ACLSafe( $global_config_file ) ||
+ ! wp_cache_replace_line( 'define *\( *\'WPCACHEHOME\'', '', $global_config_file )
+ )
+ ) {
+ wp_die( "Could not remove WPCACHEHOME define from $global_config_file. Please edit that file and remove the line containing the text 'WPCACHEHOME'. Then refresh this page." );
+ }
+ } elseif ( function_exists( 'wp_cache_debug' ) ) {
+ wp_cache_debug( 'wp_cache_disable_plugin: not allowed to edit wp-config.php per configuration.' );
+ }
+
+ uninstall_supercache( WP_CONTENT_DIR . '/cache' );
+ $file_not_deleted = array();
+ wpsc_remove_advanced_cache();
+ if ( @file_exists( WP_CONTENT_DIR . "/advanced-cache.php" ) ) {
+ $file_not_deleted[] = WP_CONTENT_DIR . '/advanced-cache.php';
+ }
+ if ( $delete_config_file && @file_exists( WPCACHECONFIGPATH . "/wp-cache-config.php" ) ) {
+ if ( false == unlink( WPCACHECONFIGPATH . "/wp-cache-config.php" ) )
+ $file_not_deleted[] = WPCACHECONFIGPATH . '/wp-cache-config.php';
+ }
+ if ( ! empty( $file_not_deleted ) ) {
+ $msg = __( "Dear User,\n\nWP Super Cache was removed from your blog or deactivated but some files could\nnot be deleted.\n\n", 'wp-super-cache' );
+ foreach ( $file_not_deleted as $path ) {
+ $msg .= "{$path}\n";
+ }
+ $msg .= "\n";
+ $msg .= sprintf( __( "You should delete these files manually.\nYou may need to change the permissions of the files or parent directory.\nYou can read more about this in the Codex at\n%s\n\nThank you.", 'wp-super-cache' ), 'https://codex.wordpress.org/Changing_File_Permissions#About_Chmod' );
+
+ if ( apply_filters( 'wpsc_send_uninstall_errors', 1 ) ) {
+ wp_mail( get_option( 'admin_email' ), __( 'WP Super Cache: could not delete files', 'wp-super-cache' ), $msg );
+ }
+ }
+ extract( wpsc_get_htaccess_info() ); // $document_root, $apache_root, $home_path, $home_root, $home_root_lc, $inst_root, $wprules, $scrules, $condition_rules, $rules, $gziprules
+ // @phan-suppress-next-line PhanTypeSuspiciousStringExpression -- $home_path is set via extract()
+ if ( $scrules != '' && insert_with_markers( $home_path.'.htaccess', 'WPSuperCache', array() ) ) {
+ $wp_rewrite->flush_rules();
+ } elseif( $scrules != '' ) {
+ wp_mail( get_option( 'admin_email' ), __( 'Supercache Uninstall Problems', 'wp-super-cache' ), sprintf( __( "Dear User,\n\nWP Super Cache was removed from your blog but the mod_rewrite rules\nin your .htaccess were not.\n\nPlease edit the following file and remove the code\nbetween 'BEGIN WPSuperCache' and 'END WPSuperCache'. Please backup the file first!\n\n%s\n\nRegards,\nWP Super Cache Plugin\nhttps://wordpress.org/plugins/wp-super-cache/", 'wp-super-cache' ), ABSPATH . '/.htaccess' ) );
+ }
+}
+
+function uninstall_supercache( $folderPath ) { // from http://www.php.net/manual/en/function.rmdir.php
+ if ( trailingslashit( constant( 'ABSPATH' ) ) == trailingslashit( $folderPath ) )
+ return false;
+ if ( @is_dir ( $folderPath ) ) {
+ $dh = @opendir($folderPath);
+ while( false !== ( $value = @readdir( $dh ) ) ) {
+ if ( $value != "." && $value != ".." ) {
+ $value = $folderPath . "/" . $value;
+ if ( @is_dir ( $value ) ) {
+ uninstall_supercache( $value );
+ } else {
+ @unlink( $value );
+ }
+ }
+ }
+ return @rmdir( $folderPath );
+ } else {
+ return false;
+ }
+}
+
+function wpsc_set_default_gc( $force = false ) {
+ global $cache_path, $wp_cache_shutdown_gc, $cache_schedule_type;
+
+ if ( isset( $wp_cache_shutdown_gc ) && $wp_cache_shutdown_gc == 1 ) {
+ return false;
+ }
+
+ if ( $force ) {
+ unset( $cache_schedule_type );
+ $timestamp = wp_next_scheduled( 'wp_cache_gc' );
+ if ( $timestamp ) {
+ wp_unschedule_event( $timestamp, 'wp_cache_gc' );
+ }
+ }
+
+ // set up garbage collection with some default settings
+ if ( false == isset( $cache_schedule_type ) && false == wp_next_scheduled( 'wp_cache_gc' ) ) {
+ $cache_schedule_type = 'interval';
+ $cache_time_interval = 600;
+ $cache_max_time = 1800;
+ $cache_schedule_interval = 'hourly';
+ $cache_gc_email_me = 0;
+ wp_cache_setting( 'cache_schedule_type', $cache_schedule_type );
+ wp_cache_setting( 'cache_time_interval', $cache_time_interval );
+ wp_cache_setting( 'cache_max_time', $cache_max_time );
+ wp_cache_setting( 'cache_schedule_interval', $cache_schedule_interval );
+ wp_cache_setting( 'cache_gc_email_me', $cache_gc_email_me );
+
+ wp_schedule_single_event( time() + 600, 'wp_cache_gc' );
+ }
+
+ return true;
+}
+
+function wpsc_update_check() {
+ global $wpsc_version;
+
+ if (
+ ! isset( $wpsc_version ) ||
+ $wpsc_version != 169
+ ) {
+ wp_cache_setting( 'wpsc_version', 169 );
+ global $wp_cache_debug_log, $cache_path;
+ $log_file = $cache_path . str_replace('/', '', str_replace('..', '', $wp_cache_debug_log));
+ if ( ! file_exists( $log_file ) ) {
+ return false;
+ }
+ @unlink( $log_file );
+ wp_cache_debug( 'wpsc_update_check: Deleted old log file on plugin update.' );
+ }
+}
+add_action( 'admin_init', 'wpsc_update_check' );
diff --git a/inc/plugins-cookies.php b/inc/plugins-cookies.php
new file mode 100644
index 00000000..f603bc55
--- /dev/null
+++ b/inc/plugins-cookies.php
@@ -0,0 +1,134 @@
+ $details ) {
+ $key = "cache_" . $details[ 'key' ];
+ if ( isset( $GLOBALS[ $key ] ) && $GLOBALS[ $key ] == 1 ) {
+ $list[ $t ][ 'enabled' ] = true;
+ } else {
+ $list[ $t ][ 'enabled' ] = false;
+ }
+
+ $list[ $t ]['desc'] = strip_tags( $list[ $t ]['desc'] ?? '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
+ $list[ $t ]['title'] = strip_tags( $list[ $t ]['title'] ?? '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags
+ }
+ return $list;
+}
+
+function wpsc_update_plugin_list( $update ) {
+ $list = do_cacheaction( 'wpsc_filter_list' );
+ foreach( $update as $key => $enabled ) {
+ $plugin_toggle = "cache_{$key}";
+ if ( isset( $GLOBALS[ $plugin_toggle ] ) || isset( $list[ $key ] ) ) {
+ wp_cache_setting( $plugin_toggle, (int)$enabled );
+ }
+ }
+}
+
+function wpsc_add_plugin( $file ) {
+ global $wpsc_plugins;
+ if ( substr( $file, 0, strlen( ABSPATH ) ) == ABSPATH ) {
+ $file = substr( $file, strlen( ABSPATH ) ); // remove ABSPATH
+ }
+ if (
+ ! isset( $wpsc_plugins ) ||
+ ! is_array( $wpsc_plugins ) ||
+ ! in_array( $file, $wpsc_plugins )
+ ) {
+ $wpsc_plugins[] = $file;
+ wp_cache_setting( 'wpsc_plugins', $wpsc_plugins );
+ }
+ return $file;
+}
+add_action( 'wpsc_add_plugin', 'wpsc_add_plugin' );
+
+function wpsc_delete_plugin( $file ) {
+ global $wpsc_plugins;
+ if ( substr( $file, 0, strlen( ABSPATH ) ) == ABSPATH ) {
+ $file = substr( $file, strlen( ABSPATH ) ); // remove ABSPATH
+ }
+ if (
+ isset( $wpsc_plugins ) &&
+ is_array( $wpsc_plugins ) &&
+ in_array( $file, $wpsc_plugins )
+ ) {
+ unset( $wpsc_plugins[ array_search( $file, $wpsc_plugins ) ] );
+ wp_cache_setting( 'wpsc_plugins', $wpsc_plugins );
+ }
+ return $file;
+}
+add_action( 'wpsc_delete_plugin', 'wpsc_delete_plugin' );
+
+function wpsc_get_plugins() {
+ global $wpsc_plugins;
+ return $wpsc_plugins;
+}
+
+function wpsc_add_cookie( $name ) {
+ global $wpsc_cookies;
+ if (
+ ! isset( $wpsc_cookies ) ||
+ ! is_array( $wpsc_cookies ) ||
+ ! in_array( $name, $wpsc_cookies )
+ ) {
+ $wpsc_cookies[] = $name;
+ wp_cache_setting( 'wpsc_cookies', $wpsc_cookies );
+ }
+ return $name;
+}
+add_action( 'wpsc_add_cookie', 'wpsc_add_cookie' );
+
+function wpsc_delete_cookie( $name ) {
+ global $wpsc_cookies;
+ if (
+ isset( $wpsc_cookies ) &&
+ is_array( $wpsc_cookies ) &&
+ in_array( $name, $wpsc_cookies )
+ ) {
+ unset( $wpsc_cookies[ array_search( $name, $wpsc_cookies ) ] );
+ wp_cache_setting( 'wpsc_cookies', $wpsc_cookies );
+ }
+ return $name;
+}
+add_action( 'wpsc_delete_cookie', 'wpsc_delete_cookie' );
+
+function wpsc_get_cookies() {
+ global $wpsc_cookies;
+ return $wpsc_cookies;
+}
+
+function wpsc_get_extra_cookies() {
+ global $wpsc_cookies;
+ if (
+ is_array( $wpsc_cookies ) &&
+ ! empty( $wpsc_cookies )
+ ) {
+ return '|' . implode( '|', $wpsc_cookies );
+ } else {
+ return '';
+ }
+}
diff --git a/inc/preload.php b/inc/preload.php
new file mode 100644
index 00000000..80e75b3b
--- /dev/null
+++ b/inc/preload.php
@@ -0,0 +1,722 @@
+ false,
+ 'history' => array(),
+ 'next' => false,
+ 'previous' => null,
+ );
+
+ $filename = wpsc_get_preload_status_file_path();
+ if ( file_exists( $filename ) ) {
+ $data = wp_json_file_decode( $filename, array( 'associative' => true ) );
+ if ( is_array( $data ) ) {
+ $status = $data;
+ }
+ }
+
+ if ( $include_next ) {
+ $status['next'] = wpsc_get_next_preload_time();
+ }
+
+ return $status;
+}
+
+/**
+ * Update the preload status file during a preload.
+ */
+function wpsc_update_active_preload( $group = null, $progress = null, $url = null ) {
+ $preload_status = wpsc_get_preload_status();
+
+ $preload_status['running'] = true;
+
+ // Add the new entry to the history.
+ array_unshift(
+ $preload_status['history'],
+ array(
+ 'group' => $group,
+ 'progress' => $progress,
+ 'url' => $url,
+ )
+ );
+
+ // Limit to 5 in the history.
+ $preload_status['history'] = array_slice( $preload_status['history'], 0, 5 );
+
+ $filename = wpsc_get_preload_status_file_path();
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
+ if ( false === file_put_contents( $filename, wp_json_encode( $preload_status, JSON_UNESCAPED_SLASHES ) ) ) {
+ wp_cache_debug( "wpsc_update_active_preload: failed to write to $filename" );
+ }
+}
+
+/**
+ * Update the preload status to indicate it is idle. If a finish time is specified, store it.
+ */
+function wpsc_update_idle_preload( $finish_time = null ) {
+ $preload_status = wpsc_get_preload_status();
+
+ $preload_status['running'] = false;
+ $preload_status['history'] = array();
+
+ if ( ! empty( $finish_time ) ) {
+ $preload_status['previous'] = $finish_time;
+ }
+
+ $filename = wpsc_get_preload_status_file_path();
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
+ if ( false === file_put_contents( $filename, wp_json_encode( $preload_status, JSON_UNESCAPED_SLASHES ) ) ) {
+ wp_cache_debug( "wpsc_update_idle_preload: failed to write to $filename" );
+ }
+}
+
+function wp_cron_preload_cache() {
+ global $wpdb, $wp_cache_preload_interval, $wp_cache_preload_posts, $wp_cache_preload_email_me, $wp_cache_preload_email_volume, $cache_path, $wp_cache_preload_taxonomies;
+
+ // check if stop_preload.txt exists and preload should be stopped.
+ // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
+ if ( @file_exists( $cache_path . 'stop_preload.txt' ) ) {
+ wp_cache_debug( 'wp_cron_preload_cache: preload cancelled. Aborting preload.' );
+ wpsc_reset_preload_settings();
+ return true;
+ }
+
+ /*
+ * The mutex file is used to prevent multiple preload processes from running at the same time.
+ * If the mutex file is found, the preload process will wait 3-8 seconds and then check again.
+ * If the mutex file is still found, the preload process will abort.
+ * If the mutex file is not found, the preload process will create the mutex file and continue.
+ * The mutex file is deleted at the end of the preload process.
+ * The mutex file is deleted if it is more than 10 minutes old.
+ * The mutex file should only be deleted by the preload process that created it.
+ * If the mutex file is deleted by another process, another preload process may start.
+ */
+ $mutex = $cache_path . "preload_mutex.tmp";
+ if ( @file_exists( $mutex ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
+ sleep( 3 + wp_rand( 1, 5 ) );
+ // check again just in case another preload process is still running.
+ if ( @file_exists( $mutex ) && @filemtime( $mutex ) > ( time() - 600 ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
+ wp_cache_debug( 'wp_cron_preload_cache: preload mutex found and less than 600 seconds old. Aborting preload.', 1 );
+ return true;
+ } else {
+ wp_cache_debug( 'wp_cron_preload_cache: old preload mutex found and deleted. Preload continues.', 1 );
+ @unlink( $mutex );
+ }
+ }
+ $fp = @fopen( $mutex, 'w' );
+ @fclose( $fp );
+
+ $counter = get_option( 'preload_cache_counter' );
+ $c = $counter[ 'c' ];
+
+ if ( $wp_cache_preload_email_volume == 'none' && $wp_cache_preload_email_me == 1 ) {
+ $wp_cache_preload_email_me = 0;
+ wp_cache_setting( 'wp_cache_preload_email_me', 0 );
+ }
+
+ $just_started_preloading = false;
+
+ /*
+ * Preload taxonomies first.
+ *
+ */
+ if ( isset( $wp_cache_preload_taxonomies ) && $wp_cache_preload_taxonomies ) {
+ wp_cache_debug( 'wp_cron_preload_cache: doing taxonomy preload.', 5 );
+ $taxonomies = apply_filters(
+ 'wp_cache_preload_taxonomies',
+ array(
+ 'post_tag' => 'tag',
+ 'category' => 'category',
+ )
+ );
+
+ $preload_more_taxonomies = false;
+
+ foreach ( $taxonomies as $taxonomy => $path ) {
+ $taxonomy_filename = $cache_path . 'taxonomy_' . $taxonomy . '.txt';
+
+ // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
+ if ( false === @file_exists( $taxonomy_filename ) ) {
+
+ if ( ! $just_started_preloading && $wp_cache_preload_email_me ) {
+ // translators: 1: site url
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Started', 'wp-super-cache' ), home_url(), '' ), ' ' );
+ }
+
+ $just_started_preloading = true;
+ $out = '';
+ $records = get_terms( $taxonomy );
+ foreach ( $records as $term ) {
+ $out .= get_term_link( $term ) . "\n";
+ }
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
+ $fp = fopen( $taxonomy_filename, 'w' );
+ if ( $fp ) {
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
+ fwrite( $fp, $out );
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
+ fclose( $fp );
+ }
+ $details = explode( "\n", $out );
+ } else {
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
+ $details = explode( "\n", file_get_contents( $taxonomy_filename ) );
+ }
+ if ( count( $details ) > 0 && $details[0] !== '' ) {
+ $rows = array_splice( $details, 0, WPSC_PRELOAD_POST_COUNT );
+ if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume === 'many' ) {
+ // translators: 1: Site URL, 2: Taxonomy name, 3: Number of posts done, 4: Number of posts to preload
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Refreshing %2$s taxonomy from %3$d to %4$d', 'wp-super-cache' ), home_url(), $taxonomy, $c, ( $c + WPSC_PRELOAD_POST_COUNT ) ), 'Refreshing: ' . print_r( $rows, 1 ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
+ }
+
+ foreach ( $rows as $url ) {
+ set_time_limit( 60 );
+ if ( $url === '' ) {
+ continue;
+ }
+
+ $url_info = wp_parse_url( $url );
+ $dir = get_supercache_dir() . $url_info['path'];
+ wp_cache_debug( "wp_cron_preload_cache: delete $dir" );
+ wpsc_delete_files( $dir );
+ prune_super_cache( trailingslashit( $dir ) . 'feed/', true );
+ prune_super_cache( trailingslashit( $dir ) . 'page/', true );
+
+ wpsc_update_active_preload( 'taxonomies', $taxonomy, $url );
+
+ wp_remote_get(
+ $url,
+ array(
+ 'timeout' => 60,
+ 'blocking' => true,
+ )
+ );
+ wp_cache_debug( "wp_cron_preload_cache: fetched $url" );
+ sleep( WPSC_PRELOAD_POST_INTERVAL );
+
+ if ( ! wpsc_is_preload_active() ) {
+ wp_cache_debug( 'wp_cron_preload_cache: cancelling preload process.' );
+ wpsc_reset_preload_settings();
+
+ if ( $wp_cache_preload_email_me ) {
+ // translators: Home URL of website
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Stopped', 'wp-super-cache' ), home_url(), '' ), ' ' );
+ }
+ wpsc_update_idle_preload( time() );
+ return true;
+ }
+ }
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
+ $fp = fopen( $taxonomy_filename, 'w' );
+ if ( $fp ) {
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
+ fwrite( $fp, implode( "\n", $details ) );
+ // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
+ fclose( $fp );
+ }
+ }
+
+ if (
+ $preload_more_taxonomies === false &&
+ count( $details ) > 0 &&
+ $details[0] !== ''
+ ) {
+ $preload_more_taxonomies = true;
+ }
+ }
+
+ if ( $preload_more_taxonomies === true ) {
+ wpsc_schedule_next_preload();
+ sleep( WPSC_PRELOAD_LOOP_INTERVAL );
+ return true;
+ }
+ } elseif ( $c === 0 && $wp_cache_preload_email_me ) {
+ // translators: Home URL of website
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Started', 'wp-super-cache' ), home_url(), '' ), ' ' );
+ }
+
+ /*
+ *
+ * Preload posts now.
+ *
+ * The preload_cache_counter has two values:
+ * c = the number of posts we've preloaded after this loop.
+ * t = the time we started preloading in the current loop.
+ *
+ * $c is set to the value of preload_cache_counter['c'] at the start of the function
+ * before it is incremented by WPSC_PRELOAD_POST_COUNT here.
+ * The time is used to check if preloading has stalled in check_up_on_preloading().
+ */
+
+ update_option(
+ 'preload_cache_counter',
+ array(
+ 'c' => ( $c + WPSC_PRELOAD_POST_COUNT ),
+ 't' => time(),
+ )
+ );
+
+ if ( $wp_cache_preload_posts == 'all' || $c < $wp_cache_preload_posts ) {
+ $types = wpsc_get_post_types();
+ $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ( post_type IN ( $types ) ) AND post_status = 'publish' ORDER BY ID DESC LIMIT %d," . WPSC_PRELOAD_POST_COUNT, $c ) ); // phpcs:ignore
+ wp_cache_debug( 'wp_cron_preload_cache: got ' . WPSC_PRELOAD_POST_COUNT . ' posts from position ' . $c );
+ } else {
+ wp_cache_debug( "wp_cron_preload_cache: no more posts to get. Limit ($wp_cache_preload_posts) reached.", 5 );
+ $posts = false;
+ }
+ if ( !isset( $wp_cache_preload_email_volume ) )
+ $wp_cache_preload_email_volume = 'medium';
+
+ if ( $posts ) {
+ if ( get_option( 'show_on_front' ) == 'page' ) {
+ $page_on_front = get_option( 'page_on_front' );
+ $page_for_posts = get_option( 'page_for_posts' );
+ } else {
+ $page_on_front = $page_for_posts = 0;
+ }
+ if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume === 'many' ) {
+ /* translators: 1: home url, 2: start post id, 3: end post id */
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Refreshing posts from %2$d to %3$d', 'wp-super-cache' ), home_url(), $c, ( $c + WPSC_PRELOAD_POST_COUNT ) ), ' ' );
+ }
+ $msg = '';
+ $count = $c + 1;
+
+ foreach( $posts as $post_id ) {
+ set_time_limit( 60 );
+ if ( $page_on_front != 0 && ( $post_id == $page_on_front || $post_id == $page_for_posts ) )
+ continue;
+ $url = get_permalink( $post_id );
+
+ if ( ! is_string( $url ) ) {
+ wp_cache_debug( "wp_cron_preload_cache: skipped $post_id. Expected a URL, received: " . gettype( $url ) );
+ continue;
+ }
+
+ if ( wp_cache_is_rejected( $url ) ) {
+ wp_cache_debug( "wp_cron_preload_cache: skipped $url per rejected strings setting" );
+ continue;
+ }
+ clear_post_supercache( $post_id );
+
+ wpsc_update_active_preload( 'posts', $count, $url );
+
+ if ( ! wpsc_is_preload_active() ) {
+ wp_cache_debug( 'wp_cron_preload_cache: cancelling preload process.' );
+ wpsc_reset_preload_settings();
+
+ if ( $wp_cache_preload_email_me ) {
+ // translators: Home URL of website
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Stopped', 'wp-super-cache' ), home_url(), '' ), ' ' );
+ }
+
+ wpsc_update_idle_preload( time() );
+ return true;
+ }
+
+ $msg .= "$url\n";
+ wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
+ wp_cache_debug( "wp_cron_preload_cache: fetched $url", 5 );
+ ++$count;
+ sleep( WPSC_PRELOAD_POST_INTERVAL );
+ }
+
+ if ( $wp_cache_preload_email_me && ( $wp_cache_preload_email_volume === 'medium' || $wp_cache_preload_email_volume === 'many' ) ) {
+ // translators: 1: home url, 2: number of posts refreshed
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] %2$d posts refreshed', 'wp-super-cache' ), home_url(), ( $c + WPSC_PRELOAD_POST_COUNT ) ), __( 'Refreshed the following posts:', 'wp-super-cache' ) . "\n$msg" );
+ }
+
+ wpsc_schedule_next_preload();
+ wpsc_delete_files( get_supercache_dir() );
+ sleep( WPSC_PRELOAD_LOOP_INTERVAL );
+ } else {
+ $msg = '';
+ wpsc_reset_preload_counter();
+ if ( (int)$wp_cache_preload_interval && defined( 'DOING_CRON' ) ) {
+ if ( $wp_cache_preload_email_me )
+ $msg = sprintf( __( 'Scheduling next preload refresh in %d minutes.', 'wp-super-cache' ), (int)$wp_cache_preload_interval );
+ wp_cache_debug( "wp_cron_preload_cache: no more posts. scheduling next preload in $wp_cache_preload_interval minutes.", 5 );
+ wp_schedule_single_event( time() + ( (int)$wp_cache_preload_interval * 60 ), 'wp_cache_full_preload_hook' );
+ }
+ global $file_prefix, $cache_max_time;
+ if ( $wp_cache_preload_interval > 0 ) {
+ $cache_max_time = (int)$wp_cache_preload_interval * 60; // fool the GC into expiring really old files
+ } else {
+ $cache_max_time = 86400; // fool the GC into expiring really old files
+ }
+ if ( $wp_cache_preload_email_me )
+ wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Cache Preload Completed', 'wp-super-cache' ), home_url() ), __( "Cleaning up old supercache files.", 'wp-super-cache' ) . "\n" . $msg );
+ if ( $cache_max_time > 0 ) { // GC is NOT disabled
+ wp_cache_debug( "wp_cron_preload_cache: clean expired cache files older than $cache_max_time seconds.", 5 );
+ wp_cache_phase2_clean_expired( $file_prefix, true ); // force cleanup of old files.
+ }
+
+ wpsc_reset_preload_settings();
+ wpsc_update_idle_preload( time() );
+ }
+ @unlink( $mutex );
+}
+add_action( 'wp_cache_preload_hook', 'wp_cron_preload_cache' );
+add_action( 'wp_cache_full_preload_hook', 'wp_cron_preload_cache' );
+
+/*
+ * Schedule the next preload event without resetting the preload counter.
+ * This happens when the next loop of an active preload is scheduled.
+ */
+function wpsc_schedule_next_preload() {
+ global $cache_path;
+
+ /*
+ * Edge case: If preload is not active, don't schedule the next preload.
+ * This can happen if the preload is cancelled by the user right after a loop finishes.
+ */
+ if ( ! wpsc_is_preload_active() ) {
+ wpsc_reset_preload_settings();
+ wp_cache_debug( 'wpsc_schedule_next_preload: preload is not active. not scheduling next preload.' );
+ return;
+ }
+
+ if ( defined( 'DOING_CRON' ) ) {
+ wp_cache_debug( 'wp_cron_preload_cache: scheduling the next preload in 3 seconds.' );
+ wp_schedule_single_event( time() + 3, 'wp_cache_preload_hook' );
+ }
+
+ // we always want to delete the mutex file, even if we're not using cron
+ $mutex = $cache_path . 'preload_mutex.tmp';
+ wp_delete_file( $mutex );
+}
+
+function option_preload_cache_counter( $value ) {
+ if ( false == is_array( $value ) ) {
+ return array(
+ 'c' => 0,
+ 't' => time(),
+ );
+ } else {
+ return $value;
+ }
+}
+add_filter( 'option_preload_cache_counter', 'option_preload_cache_counter' );
+
+function check_up_on_preloading() {
+ $value = get_option( 'preload_cache_counter' );
+ if ( is_array( $value ) && $value['c'] > 0 && ( time() - $value['t'] ) > 3600 && false === wp_next_scheduled( 'wp_cache_preload_hook' ) ) {
+ wp_schedule_single_event( time() + 5, 'wp_cache_preload_hook' );
+ }
+}
+add_action( 'init', 'check_up_on_preloading' ); // sometimes preloading stops working. Kickstart it.
+
+/*
+ * returns true if preload is active
+ */
+function wpsc_is_preload_active() {
+ global $cache_path;
+
+ // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
+ if ( @file_exists( $cache_path . 'stop_preload.txt' ) ) {
+ return false;
+ }
+
+ if ( file_exists( $cache_path . 'preload_mutex.tmp' ) ) {
+ return true;
+ }
+
+ // check taxonomy preload loop
+ $taxonomies = apply_filters(
+ 'wp_cache_preload_taxonomies',
+ array(
+ 'post_tag' => 'tag',
+ 'category' => 'category',
+ )
+ );
+
+ foreach ( $taxonomies as $taxonomy => $path ) {
+ $taxonomy_filename = $cache_path . 'taxonomy_' . $taxonomy . '.txt';
+ if ( file_exists( $taxonomy_filename ) ) {
+ return true;
+ }
+ }
+
+ // check post preload loop
+ $preload_cache_counter = get_option( 'preload_cache_counter' );
+ if (
+ is_array( $preload_cache_counter )
+ && isset( $preload_cache_counter['c'] )
+ && $preload_cache_counter['c'] > 0
+ ) {
+ return true;
+ }
+
+ return false;
+}
+
+/*
+ * This function will reset the preload cache counter
+ */
+function wpsc_reset_preload_counter() {
+ update_option(
+ 'preload_cache_counter',
+ array(
+ 'c' => 0,
+ 't' => time(),
+ )
+ );
+}
+
+/*
+ * This function will reset all preload settings
+ */
+function wpsc_reset_preload_settings() {
+ global $cache_path;
+
+ $mutex = $cache_path . 'preload_mutex.tmp';
+ wp_delete_file( $mutex );
+ wp_delete_file( $cache_path . 'stop_preload.txt' );
+ wpsc_reset_preload_counter();
+
+ $taxonomies = apply_filters(
+ 'wp_cache_preload_taxonomies',
+ array(
+ 'post_tag' => 'tag',
+ 'category' => 'category',
+ )
+ );
+
+ foreach ( $taxonomies as $taxonomy => $path ) {
+ $taxonomy_filename = $cache_path . 'taxonomy_' . $taxonomy . '.txt';
+ wp_delete_file( $taxonomy_filename );
+ }
+}
+
+function wpsc_cancel_preload() {
+ $next_preload = wp_next_scheduled( 'wp_cache_preload_hook' );
+ $next_full_preload = wp_next_scheduled( 'wp_cache_full_preload_hook' );
+
+ if ( $next_preload || $next_full_preload ) {
+ wp_cache_debug( 'wpsc_cancel_preload: reset preload settings' );
+ wpsc_reset_preload_settings();
+ }
+
+ if ( $next_preload ) {
+ wp_cache_debug( 'wpsc_cancel_preload: unscheduling wp_cache_preload_hook' );
+ wp_unschedule_event( $next_preload, 'wp_cache_preload_hook' );
+ }
+ if ( $next_full_preload ) {
+ wp_cache_debug( 'wpsc_cancel_preload: unscheduling wp_cache_full_preload_hook' );
+ wp_unschedule_event( $next_full_preload, 'wp_cache_full_preload_hook' );
+ }
+ wp_cache_debug( 'wpsc_cancel_preload: creating stop_preload.txt' );
+
+ /*
+ * Reset the preload settings, but also create the stop_preload.txt file to
+ * prevent the preload from starting again.
+ * By creating the stop_preload.txt file, we can be sure the preload will cancel.
+ */
+ wpsc_reset_preload_settings();
+ wpsc_create_stop_preload_flag();
+ wpsc_update_idle_preload( time() );
+}
+
+/*
+ * The preload process checks for a file called stop_preload.txt and will stop if found.
+ * This function creates that file.
+ */
+function wpsc_create_stop_preload_flag() {
+ global $cache_path;
+ // phpcs:ignore -- WordPress.WP.AlternativeFunctions.file_system_read_fopen WordPress.PHP.NoSilencedErrors.Discouraged
+ $fp = @fopen( $cache_path . 'stop_preload.txt', 'w' );
+ // phpcs:ignore -- WordPress.WP.AlternativeFunctions.file_system_operations_fclose WordPress.PHP.NoSilencedErrors.Discouraged
+ @fclose( $fp );
+}
+
+function wpsc_enable_preload() {
+
+ wpsc_reset_preload_settings();
+ wp_schedule_single_event( time() + 10, 'wp_cache_full_preload_hook' );
+}
+
+function wpsc_get_post_types() {
+
+ $preload_type_args = apply_filters( 'wpsc_preload_post_types_args', array(
+ 'public' => true,
+ 'publicly_queryable' => true
+ ) );
+
+ $post_types = (array) apply_filters( 'wpsc_preload_post_types', get_post_types( $preload_type_args, 'names', 'or' ));
+
+ return "'" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "'";
+}
+function wpsc_post_count() {
+ global $wpdb;
+ static $count;
+
+ if ( isset( $count ) ) {
+ return $count;
+ }
+
+ $post_type_list = wpsc_get_post_types();
+ $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ( $post_type_list ) AND post_status = 'publish'" );
+
+ return $count;
+}
+
+/**
+ * Get the minimum interval in minutes between preload refreshes.
+ * Filter the default value of 10 minutes using the `wpsc_minimum_preload_interval` filter.
+ *
+ * @return int
+ */
+function wpsc_get_minimum_preload_interval() {
+ return apply_filters( 'wpsc_minimum_preload_interval', 10 );
+}
+
+function wpsc_preload_settings() {
+ global $wp_cache_preload_interval, $wp_cache_preload_on, $wp_cache_preload_taxonomies, $wp_cache_preload_email_volume, $wp_cache_preload_posts, $valid_nonce;
+
+ if ( isset( $_POST[ 'action' ] ) == false || $_POST[ 'action' ] != 'preload' )
+ return;
+
+ if ( ! $valid_nonce ) {
+ return;
+ }
+
+ if ( isset( $_POST[ 'preload_off' ] ) ) {
+ wpsc_cancel_preload();
+ return;
+ } elseif ( isset( $_POST[ 'preload_now' ] ) ) {
+ wpsc_enable_preload();
+ wpsc_update_idle_preload();
+ ?>
+
' . esc_html__( 'You may experience problems running this plugin because SAFE MODE is enabled.', 'wp-super-cache' ) . ' ';
-
- // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_mode_gidDeprecatedRemoved -- Version is checked before access.
- if ( ! ini_get( 'safe_mode_gid' ) ) { // @codingStandardsIgnoreLine
- esc_html_e( 'Your server is set up to check the owner of PHP scripts before allowing them to read and write files.', 'wp-super-cache' );
- echo ' ';
- printf( __( 'You or an administrator may be able to make it work by changing the group owner of the plugin scripts to match that of the web server user. The group owner of the %s/cache/ directory must also be changed. See the safe mode manual page for further details.', 'wp-super-cache' ), esc_attr( WP_CONTENT_DIR ) );
- } else {
- _e( 'You or an administrator must disable this. See the safe mode manual page for further details. This cannot be disabled in a .htaccess file unfortunately. It must be done in the php.ini config file.', 'wp-super-cache' );
- }
- echo '
" . __( 'A custom url or permalink structure is required for this plugin to work correctly. Please go to the Permalinks Options Page to configure your permalinks.', 'wp-super-cache' ) . "
Troubleshooting section of the readme.txt', 'wp-super-cache' ), 'https://wordpress.org/plugins/wp-super-cache/faq/' ); ?>
-
- ' . __( "Cannot continue... fix previous problems and retry.", 'wp-super-cache' ) . '';
- return false;
- }
-
- if ( false == function_exists( 'wpsc_deep_replace' ) ) {
- $msg = __( 'Warning! You must set WP_CACHE and WPCACHEHOME in your wp-config.php for this plugin to work correctly:' ) . ' ';
- $msg .= "define( 'WP_CACHE', true ); ";
- $msg .= "define( 'WPCACHEHOME', '" . __DIR__ . "/' ); ";
- wp_die( $msg );
- }
-
- if (!wp_cache_check_global_config()) {
- return false;
- }
-
- if ( 1 == ini_get( 'zlib.output_compression' ) || "on" == strtolower( ini_get( 'zlib.output_compression' ) ) ) {
- ?>
-
this page for instructions on modifying your php.ini.', 'wp-super-cache' ); ?>
-
-
%s/wp-cache-config.php and cannot be modified. That file must be writeable by the web server to make any changes.', 'wp-super-cache' ), WPCACHECONFIGPATH ); ?>
-
-
This page explains how to change file permissions.', 'wp-super-cache' ); ?>
' . esc_html__( 'For best performance you should enable "Mobile device support" or delete the mobile rewrite rules in your .htaccess. Look for the 2 lines with the text "2.0\ MMP|240x320" and delete those.', 'wp-super-cache' ) . '
' . esc_html__( 'This will have no affect on ordinary users but mobile users will see uncached pages.', 'wp-super-cache' ) . '
- # BEGIN WPSuperCache and # END WPSuperCache and let the plugin regenerate them by reloading this page.', 'wp-super-cache' ), array( 'code' => array() ) ), esc_html( $home_path ) );
- ?>
-
-
- # BEGIN WPSuperCache and # END WPSuperCache. There are two sections that look very similar. Just below the line %%{HTTP:Cookie} !^.*(comment_author_|%2$s|wp-postpass_).*$ add these lines: (do it twice, once for each section)', 'wp-super-cache' ), array( 'code' => array() ) ), esc_html( $home_path ), esc_html( wpsc_get_logged_in_cookie() ) );
- ?>
-
-
- __( 'Required to serve compressed supercache files properly.', 'wp-super-cache' ), 'mod_headers' => __( 'Required to set caching information on supercache pages. IE7 users will see old pages without this module.', 'wp-super-cache' ), 'mod_expires' => __( 'Set the expiry date on supercached pages. Visitors may not see new pages when they refresh or leave comments without this module.', 'wp-super-cache' ) );
- foreach( $required_modules as $req => $desc ) {
- if( !in_array( $req, $mods ) ) {
- $missing_mods[ $req ] = $desc;
- }
- }
- if( isset( $missing_mods) && is_array( $missing_mods ) ) {
- ?>
here. Unfortunately, WordPress writes to the .htaccess in the install directory, not where your site is served from. When you update the rewrite rules in this plugin you will have to copy the file to where your site is hosted. This will be fixed in the future.', 'wp-super-cache' ); ?>
' . __( "Warning! You attempted to enable compression but zlib.output_compression is enabled. See #21 in the Troubleshooting section of the readme file.", 'wp-super-cache' ) . '
development version for the latest fixes (changelog).', 'wp-super-cache' ), 'https://odd.blog/y/2o', 'https://plugins.trac.wordpress.org/log/wp-super-cache/' ); ?>
-
-
-
rate us and give feedback.', 'wp-super-cache' ), 'https://wordpress.org/support/plugin/wp-super-cache/reviews?rate=5#new-post' ); ?>
' . esc_html__( 'Cache plugins are PHP scripts you\'ll find in a dedicated folder inside the WP Super Cache folder (wp-super-cache/plugins/). They load at the same time as WP Super Cache, and before regular WordPress plugins.', 'wp-super-cache' ) . '
';
- echo '
' . esc_html__( 'Keep in mind that cache plugins are for advanced users only. To create and manage them, you\'ll need extensive knowledge of both PHP and WordPress actions.', 'wp-super-cache' ) . '
';
- echo '
' . sprintf( __( 'Warning! Due to the way WordPress upgrades plugins, the ones you upload to the WP Super Cache folder (wp-super-cache/plugins/) will be deleted when you upgrade WP Super Cache. To avoid this loss, load your cache plugins from a different location. When you set $wp_cache_plugins_dir to the new location in wp-config.php, WP Super Cache will look there instead. You can find additional details in the developer documentation.', 'wp-super-cache' ), 'https://odd.blog/wp-super-cache-developers/' ) . '
' . __( 'Your server is configured to show files and directories, which may expose sensitive data such as login cookies to attackers in the cache directories. That has been fixed by adding a file named index.html to each directory. If you use simple caching, consider moving the location of the cache directory on the Advanced Settings page.', 'wp-super-cache' ) . '
';
- echo "
";
- _e( 'If you just installed WP Super Cache for the first time, you can dismiss this message. Otherwise, you should probably refresh the login cookies of all logged in WordPress users here by clicking the logout link below.', 'wp-super-cache' );
- echo "
";
- echo '
' . esc_html__( 'The logout link will log out all WordPress users on this site except you. Your authentication cookie will be updated, but you will not be logged out.', 'wp-super-cache' ) . '
' . __( 'Warning! You may not be allowed to use this plugin on your site.', 'wp-super-cache' ) . "
";
- echo '
' .
- sprintf(
- __( 'The file %s was created by another plugin or by your system administrator. Please examine the file carefully by FTP or SSH and consult your hosting documentation. ', 'wp-super-cache' ),
- $wpsc_advanced_cache_filename
- ) .
- '
';
- echo '
' .
- __( 'If it was created by another caching plugin please uninstall that plugin first before activating WP Super Cache. If the file is not removed by that action you should delete the file manually.', 'wp-super-cache' ),
- '
';
- echo '
' .
- __( 'If you need support for this problem contact your hosting provider.', 'wp-super-cache' ),
- '
" .
- sprintf(
- __( 'Make %1$s writable using the chmod command through your ftp or server software. (chmod 777 %1$s) and refresh this page. This is only a temporary measure and you’ll have to make it read only afterwards again. (Change 777 to 755 in the previous command)', 'wp-super-cache' ),
- WP_CONTENT_DIR
- ) .
- "
";
- echo "
" . sprintf( __( 'Refresh this page to update %s/advanced-cache.php', 'wp-super-cache' ), WP_CONTENT_DIR ) . "
";
- echo sprintf( __( 'If that doesn’t work, make sure the file %s/advanced-cache.php doesn’t exist:', 'wp-super-cache' ), WP_CONTENT_DIR ) . "";
- echo "";
- echo '
The WP_CACHE constant is used by WordPress to load the code that serves cached pages. Unfortunately, it is set to false. Please edit your wp-config.php and add or edit the following line above the final require_once command:
define('WP_CACHE', true);
", 'wp-super-cache' ) . "";
- } else {
- echo '
' . __( "Error: WP_CACHE is not enabled in your wp-config.php file and I couldn’t modify it.", 'wp-super-cache' ) . "
";
- echo "
" . sprintf( __( "Edit %s and add the following line: define('WP_CACHE', true); Otherwise, WP-Cache will not be executed by WordPress core. ", 'wp-super-cache' ), $global_config_file ) . "
";
- }
- return false;
- } else {
- echo "
" . __( '
WP_CACHE constant added to wp-config.php
If you continue to see this warning message please see point 5 of the Troubleshooting Guide. The WP_CACHE line must be moved up.', 'wp-super-cache' ) . "
' . sprintf( __( 'WP Super Cache must be configured. Go to the admin page to enable and configure the plugin.', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) . '
' . sprintf( __('WP Super Cache is disabled. Please go to the plugin admin page to enable caching.', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) . '
' . sprintf( __( 'Warning! WP Super Cache caching was broken but has been fixed! The script advanced-cache.php could not load wp-cache-phase1.php.
The file %1$s/advanced-cache.php has been recreated and WPCACHEHOME fixed in your wp-config.php. Reload to hide this message.', 'wp-super-cache' ), WP_CONTENT_DIR ) . '
';
- }
- }
-}
-add_action( 'admin_notices', 'wp_cache_admin_notice' );
-
-function wp_cache_check_site() {
- global $wp_super_cache_front_page_check, $wp_super_cache_front_page_clear, $wp_super_cache_front_page_text, $wp_super_cache_front_page_notification, $wpdb;
-
- if ( empty( $wp_super_cache_front_page_check ) ) {
- return false;
- }
-
- if ( function_exists( "wp_remote_get" ) == false ) {
- return false;
- }
- $front_page = wp_remote_get( site_url(), array('timeout' => 60, 'blocking' => true ) );
- if( is_array( $front_page ) ) {
- // Check for gzipped front page
- if ( $front_page[ 'headers' ][ 'content-type' ] == 'application/x-gzip' ) {
- if ( ! isset( $wp_super_cache_front_page_clear ) || $wp_super_cache_front_page_clear === 0 ) {
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is gzipped! Please clear cache!', 'wp-super-cache' ), home_url() ), sprintf( __( "Please visit %s to clear the cache as the front page of your site is now downloading!", 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) );
- } else {
- wp_cache_clear_cache( $wpdb->blogid );
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is gzipped! Cache Cleared!', 'wp-super-cache' ), home_url() ), sprintf( __( "The cache on your blog has been cleared because the front page of your site is now downloading. Please visit %s to verify the cache has been cleared.", 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ) ) );
- }
- }
-
- // Check for broken front page
- if (
- ! empty( $wp_super_cache_front_page_text )
- && ! str_contains( $front_page['body'], $wp_super_cache_front_page_text )
- ) {
- if ( ! isset( $wp_super_cache_front_page_clear ) || $wp_super_cache_front_page_clear === 0 ) {
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is not correct! Please clear cache!', 'wp-super-cache' ), home_url() ), sprintf( __( 'Please visit %1$s to clear the cache as the front page of your site is not correct and missing the text, "%2$s"!', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ), $wp_super_cache_front_page_text ) );
- } else {
- wp_cache_clear_cache( $wpdb->blogid );
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is not correct! Cache Cleared!', 'wp-super-cache' ), home_url() ), sprintf( __( 'The cache on your blog has been cleared because the front page of your site is missing the text "%2$s". Please visit %1$s to verify the cache has been cleared.', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ), $wp_super_cache_front_page_text ) );
- }
- }
- }
- if ( isset( $wp_super_cache_front_page_notification ) && $wp_super_cache_front_page_notification == 1 ) {
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page check!', 'wp-super-cache' ), home_url() ), sprintf( __( "WP Super Cache has checked the front page of your blog. Please visit %s if you would like to disable this.", 'wp-super-cache' ) . "\n\n", admin_url( 'options-general.php?page=wpsupercache' ) ) );
- }
-
- if ( !wp_next_scheduled( 'wp_cache_check_site_hook' ) ) {
- wp_schedule_single_event( time() + 360 , 'wp_cache_check_site_hook' );
- wp_cache_debug( 'scheduled wp_cache_check_site_hook for 360 seconds time.', 2 );
- }
-}
-add_action( 'wp_cache_check_site_hook', 'wp_cache_check_site' );
-
-function update_cached_mobile_ua_list( $mobile_browsers, $mobile_prefixes = 0, $mobile_groups = 0 ) {
- global $wp_cache_config_file, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes, $wp_cache_mobile_groups;
- wp_cache_setting( 'wp_cache_mobile_browsers', $mobile_browsers );
- wp_cache_setting( 'wp_cache_mobile_prefixes', $mobile_prefixes );
- if ( is_array( $mobile_groups ) ) {
- $wp_cache_mobile_groups = $mobile_groups;
- wp_cache_replace_line('^ *\$wp_cache_mobile_groups', "\$wp_cache_mobile_groups = '" . implode( ', ', $mobile_groups ) . "';", $wp_cache_config_file);
- }
-
- return true;
-}
-
-function wpsc_update_htaccess() {
- extract( wpsc_get_htaccess_info() ); // $document_root, $apache_root, $home_path, $home_root, $home_root_lc, $inst_root, $wprules, $scrules, $condition_rules, $rules, $gziprules
- // @phan-suppress-next-line PhanTypeSuspiciousStringExpression -- $home_path is set via extract()
- wpsc_remove_marker( $home_path.'.htaccess', 'WordPress' ); // remove original WP rules so SuperCache rules go on top
- // @phan-suppress-next-line PhanTypeSuspiciousStringExpression -- $home_path is set via extract()
- if( insert_with_markers( $home_path.'.htaccess', 'WPSuperCache', explode( "\n", $rules ) ) && insert_with_markers( $home_path.'.htaccess', 'WordPress', explode( "\n", $wprules ) ) ) {
- return true;
- } else {
- return false;
- }
-}
-
-function wpsc_update_htaccess_form( $short_form = true ) {
- global $wpmu_version;
-
- $admin_url = admin_url( 'options-general.php?page=wpsupercache' );
- extract( wpsc_get_htaccess_info() ); // $document_root, $apache_root, $home_path, $home_root, $home_root_lc, $inst_root, $wprules, $scrules, $condition_rules, $rules, $gziprules
- // @phan-suppress-next-line PhanTypeSuspiciousStringExpression -- $home_path is set via extract()
- if( !is_writeable_ACLSafe( $home_path . ".htaccess" ) ) {
- echo "
" . sprintf( __( 'The file %s.htaccess cannot be modified by the web server. Please correct this using the chmod command or your ftp client.', 'wp-super-cache' ), $home_path ) . "
" . __( 'Refresh this page when the file permissions have been modified.' ) . "
" . sprintf( __( 'Alternatively, you can edit your %s.htaccess file manually and add the following code (before any WordPress rules):', 'wp-super-cache' ), $home_path ) . "
";
- echo "
# BEGIN WPSuperCache\n" . esc_html( $rules ) . "# END WPSuperCache
" . sprintf( __( 'To serve static html files your server must have the correct mod_rewrite rules added to a file called %s.htaccess', 'wp-super-cache' ), $home_path ) . " ";
- _e( "You can edit the file yourself. Add the following rules.", 'wp-super-cache' );
- echo __( " Make sure they appear before any existing WordPress rules. ", 'wp-super-cache' ) . "
";
- echo "
";
- echo "
# BEGIN WPSuperCache\n" . esc_html( $rules ) . "# END WPSuperCache
";
- echo "
";
- echo "
" . sprintf( __( 'Rules must be added to %s too:', 'wp-super-cache' ), WP_CONTENT_DIR . "/cache/.htaccess" ) . "
";
- echo "
";
- echo "
# BEGIN supercache\n" . esc_html( $gziprules ) . "# END supercache
";
- echo "
";
- }
- if ( !isset( $wpmu_version ) || $wpmu_version == '' ) {
- echo '\n";
- }
- }
-}
-
-/*
- * Return LOGGED_IN_COOKIE if it doesn't begin with wordpress_logged_in
- * to avoid having people update their .htaccess file
- */
-function wpsc_get_logged_in_cookie() {
- $logged_in_cookie = 'wordpress_logged_in';
- if ( defined( 'LOGGED_IN_COOKIE' ) && substr( constant( 'LOGGED_IN_COOKIE' ), 0, 19 ) != 'wordpress_logged_in' )
- $logged_in_cookie = constant( 'LOGGED_IN_COOKIE' );
- return $logged_in_cookie;
-}
-
-function wpsc_get_htaccess_info() {
- global $wp_cache_mobile_enabled, $wp_cache_mobile_prefixes, $wp_cache_mobile_browsers, $wp_cache_disable_utf8;
- global $htaccess_path;
-
- if ( isset( $_SERVER[ "PHP_DOCUMENT_ROOT" ] ) ) {
- $document_root = $_SERVER[ "PHP_DOCUMENT_ROOT" ];
- $apache_root = $_SERVER[ "PHP_DOCUMENT_ROOT" ];
- } else {
- $document_root = $_SERVER[ "DOCUMENT_ROOT" ];
- $apache_root = '%{DOCUMENT_ROOT}';
- }
- $content_dir_root = $document_root;
- if ( strpos( $document_root, '/kunden/homepages/' ) === 0 ) {
- // https://wordpress.org/support/topic/plugin-wp-super-cache-how-to-get-mod_rewrite-working-on-1and1-shared-hosting?replies=1
- // On 1and1, PHP's directory structure starts with '/homepages'. The
- // Apache directory structure has an extra '/kunden' before it.
- // Also 1and1 does not support the %{DOCUMENT_ROOT} variable in
- // .htaccess files.
- // This prevents the $inst_root from being calculated correctly and
- // means that the $apache_root is wrong.
- //
- // e.g. This is an example of how Apache and PHP see the directory
- // structure on 1and1:
- // Apache: /kunden/homepages/xx/dxxxxxxxx/htdocs/site1/index.html
- // PHP: /homepages/xx/dxxxxxxxx/htdocs/site1/index.html
- // Here we fix up the paths to make mode_rewrite work on 1and1 shared hosting.
- $content_dir_root = substr( $content_dir_root, 7 );
- $apache_root = $document_root;
- }
- $home_path = get_home_path();
- $home_root = parse_url(get_bloginfo('url'));
- $home_root = isset( $home_root[ 'path' ] ) ? trailingslashit( $home_root[ 'path' ] ) : '/';
- if ( isset( $htaccess_path ) ) {
- $home_path = $htaccess_path;
- } elseif (
- $home_root == '/' &&
- $home_path != $_SERVER[ 'DOCUMENT_ROOT' ]
- ) {
- $home_path = $_SERVER[ 'DOCUMENT_ROOT' ];
- } elseif (
- $home_root != '/' &&
- $home_path != str_replace( '//', '/', $_SERVER[ 'DOCUMENT_ROOT' ] . $home_root ) &&
- is_dir( $_SERVER[ 'DOCUMENT_ROOT' ] . $home_root )
- ) {
- $home_path = str_replace( '//', '/', $_SERVER[ 'DOCUMENT_ROOT' ] . $home_root );
- }
-
- $home_path = trailingslashit( $home_path );
- $home_root_lc = str_replace( '//', '/', strtolower( $home_root ) );
- $inst_root = str_replace( '//', '/', '/' . trailingslashit( str_replace( $content_dir_root, '', str_replace( '\\', '/', WP_CONTENT_DIR ) ) ) );
- $wprules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WordPress' ) );
- $wprules = str_replace( "RewriteEngine On\n", '', $wprules );
- $wprules = str_replace( "RewriteBase $home_root\n", '', $wprules );
- $scrules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WPSuperCache' ) );
-
- if( substr( get_option( 'permalink_structure' ), -1 ) == '/' ) {
- $condition_rules[] = "RewriteCond %{REQUEST_URI} !^.*[^/]$";
- $condition_rules[] = "RewriteCond %{REQUEST_URI} !^.*//.*$";
- }
- $condition_rules[] = "RewriteCond %{REQUEST_METHOD} !POST";
- $condition_rules[] = "RewriteCond %{QUERY_STRING} ^$";
- $condition_rules[] = "RewriteCond %{HTTP:Cookie} !^.*(comment_author_|" . wpsc_get_logged_in_cookie() . wpsc_get_extra_cookies() . "|wp-postpass_).*$";
- $condition_rules[] = "RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\\\"]+ [NC]";
- $condition_rules[] = "RewriteCond %{HTTP:Profile} !^[a-z0-9\\\"]+ [NC]";
- if ( $wp_cache_mobile_enabled ) {
- if ( isset( $wp_cache_mobile_browsers ) && "" != $wp_cache_mobile_browsers )
- $condition_rules[] = "RewriteCond %{HTTP_USER_AGENT} !^.*(" . addcslashes( str_replace( ', ', '|', $wp_cache_mobile_browsers ), ' ' ) . ").* [NC]";
- if ( isset( $wp_cache_mobile_prefixes ) && "" != $wp_cache_mobile_prefixes )
- $condition_rules[] = "RewriteCond %{HTTP_USER_AGENT} !^(" . addcslashes( str_replace( ', ', '|', $wp_cache_mobile_prefixes ), ' ' ) . ").* [NC]";
- }
- $condition_rules = apply_filters( 'supercacherewriteconditions', $condition_rules );
-
- $rules = "\n";
- $rules .= "RewriteEngine On\n";
- $rules .= "RewriteBase $home_root\n"; // props Chris Messina
- $rules .= "#If you serve pages from behind a proxy you may want to change 'RewriteCond %{HTTPS} on' to something more sensible\n";
- if ( isset( $wp_cache_disable_utf8 ) == false || $wp_cache_disable_utf8 == 0 ) {
- $charset = get_option('blog_charset') == '' ? 'UTF-8' : get_option('blog_charset');
- $rules .= "AddDefaultCharset {$charset}\n";
- }
-
- $rules .= "CONDITION_RULES";
- $rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
- $rules .= "RewriteCond %{HTTPS} on\n";
- $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index-https.html.gz -f\n";
- $rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index-https.html.gz\" [L]\n\n";
-
- $rules .= "CONDITION_RULES";
- $rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
- $rules .= "RewriteCond %{HTTPS} !on\n";
- $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index.html.gz -f\n";
- $rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index.html.gz\" [L]\n\n";
-
- $rules .= "CONDITION_RULES";
- $rules .= "RewriteCond %{HTTPS} on\n";
- $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index-https.html -f\n";
- $rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index-https.html\" [L]\n\n";
-
- $rules .= "CONDITION_RULES";
- $rules .= "RewriteCond %{HTTPS} !on\n";
- $rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index.html -f\n";
- $rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{SERVER_NAME}{$home_root_lc}$1/index.html\" [L]\n";
- $rules .= "\n";
- $rules = apply_filters( 'supercacherewriterules', $rules );
-
- $rules = str_replace( "CONDITION_RULES", implode( "\n", $condition_rules ) . "\n", $rules );
-
- $gziprules = "\n \n ForceType text/html\n FileETag None\n \n AddEncoding gzip .gz\n AddType text/html .gz\n\n";
- $gziprules .= "\n SetEnvIfNoCase Request_URI \.gz$ no-gzip\n\n";
-
- // Default headers.
- $headers = array(
- 'Vary' => 'Accept-Encoding, Cookie',
- 'Cache-Control' => 'max-age=3, must-revalidate',
- );
-
- // Allow users to override the Vary header with WPSC_VARY_HEADER.
- if ( defined( 'WPSC_VARY_HEADER' ) && ! empty( WPSC_VARY_HEADER ) ) {
- $headers['Vary'] = WPSC_VARY_HEADER;
- }
-
- // Allow users to override Cache-control header with WPSC_CACHE_CONTROL_HEADER
- if ( defined( 'WPSC_CACHE_CONTROL_HEADER' ) && ! empty( WPSC_CACHE_CONTROL_HEADER ) ) {
- $headers['Cache-Control'] = WPSC_CACHE_CONTROL_HEADER;
- }
-
- // Allow overriding headers with a filter.
- $headers = apply_filters( 'wpsc_htaccess_mod_headers', $headers );
-
- // Combine headers into a block of text.
- $headers_text = implode(
- "\n",
- array_map(
- function ( $key, $value ) {
- return " Header set $key '" . addcslashes( $value, "'" ) . "'";
- },
- array_keys( $headers ),
- array_values( $headers )
- )
- );
-
- // Pack headers into gziprules (for historic reasons) - TODO refactor the values
- // returned to better reflect the blocks being written.
- if ( $headers_text != '' ) {
- $gziprules .= "\n$headers_text\n\n";
- }
-
- // Deafult mod_expires rules.
- $expires_rules = array(
- 'ExpiresActive On',
- 'ExpiresByType text/html A3',
- );
-
- // Allow overriding mod_expires rules with a filter.
- $expires_rules = apply_filters( 'wpsc_htaccess_mod_expires', $expires_rules );
-
- $gziprules .= "\n";
- $gziprules .= implode(
- "\n",
- array_map(
- function ( $line ) {
- return " $line";
- },
- $expires_rules
- )
- );
- $gziprules .= "\n\n";
-
- $gziprules .= "Options -Indexes\n";
-
- return array(
- 'document_root' => $document_root,
- 'apache_root' => $apache_root,
- 'home_path' => $home_path,
- 'home_root' => $home_root,
- 'home_root_lc' => $home_root_lc,
- 'inst_root' => $inst_root,
- 'wprules' => $wprules,
- 'scrules' => $scrules,
- 'condition_rules' => $condition_rules,
- 'rules' => $rules,
- 'gziprules' => $gziprules,
- );
-}
-
-function clear_post_supercache( $post_id ) {
- $dir = get_current_url_supercache_dir( $post_id );
- if ( false == @is_dir( $dir ) )
- return false;
-
- if ( get_supercache_dir() == $dir ) {
- wp_cache_debug( "clear_post_supercache: not deleting post_id $post_id as it points at homepage: $dir" );
- return false;
- }
-
- wp_cache_debug( "clear_post_supercache: post_id: $post_id. deleting files in $dir" );
- if ( get_post_type( $post_id ) != 'page') { // don't delete child pages if they exist
- prune_super_cache( $dir, true );
- } else {
- wpsc_delete_files( $dir );
- }
-}
-
-/**
- * Serves an AJAX endpoint to return the current state of the preload process.
- */
-function wpsc_ajax_get_preload_status() {
- check_ajax_referer( 'wpsc-get-preload-status' );
-
- if ( ! current_user_can( 'manage_options' ) ) {
- wp_send_json_error( null, 403 );
- }
-
- $preload_status = wpsc_get_preload_status( true );
- wp_send_json_success( $preload_status, null, JSON_UNESCAPED_SLASHES );
-}
-add_action( 'wp_ajax_wpsc_get_preload_status', 'wpsc_ajax_get_preload_status' );
-
-/**
- * Returns the location of the preload status file.
- */
-function wpsc_get_preload_status_file_path() {
- global $cache_path;
- return $cache_path . 'preload_permalink.txt';
-}
-
-/**
- * Get the timestamp of the next preload.
- */
-function wpsc_get_next_preload_time() {
- $next = wp_next_scheduled( 'wp_cache_preload_hook' );
- if ( ! $next ) {
- $next = wp_next_scheduled( 'wp_cache_full_preload_hook' );
- }
-
- return $next;
-}
-
-/**
- * Read the preload status. Caches the result in a static variable.
- */
-function wpsc_get_preload_status( $include_next = false ) {
- $status = array(
- 'running' => false,
- 'history' => array(),
- 'next' => false,
- 'previous' => null,
- );
-
- $filename = wpsc_get_preload_status_file_path();
- if ( file_exists( $filename ) ) {
- $data = wp_json_file_decode( $filename, array( 'associative' => true ) );
- if ( is_array( $data ) ) {
- $status = $data;
- }
- }
-
- if ( $include_next ) {
- $status['next'] = wpsc_get_next_preload_time();
- }
-
- return $status;
-}
-
-/**
- * Update the preload status file during a preload.
- */
-function wpsc_update_active_preload( $group = null, $progress = null, $url = null ) {
- $preload_status = wpsc_get_preload_status();
-
- $preload_status['running'] = true;
-
- // Add the new entry to the history.
- array_unshift(
- $preload_status['history'],
- array(
- 'group' => $group,
- 'progress' => $progress,
- 'url' => $url,
- )
- );
-
- // Limit to 5 in the history.
- $preload_status['history'] = array_slice( $preload_status['history'], 0, 5 );
-
- $filename = wpsc_get_preload_status_file_path();
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
- if ( false === file_put_contents( $filename, wp_json_encode( $preload_status, JSON_UNESCAPED_SLASHES ) ) ) {
- wp_cache_debug( "wpsc_update_active_preload: failed to write to $filename" );
- }
-}
-
-/**
- * Update the preload status to indicate it is idle. If a finish time is specified, store it.
- */
-function wpsc_update_idle_preload( $finish_time = null ) {
- $preload_status = wpsc_get_preload_status();
-
- $preload_status['running'] = false;
- $preload_status['history'] = array();
-
- if ( ! empty( $finish_time ) ) {
- $preload_status['previous'] = $finish_time;
- }
-
- $filename = wpsc_get_preload_status_file_path();
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
- if ( false === file_put_contents( $filename, wp_json_encode( $preload_status, JSON_UNESCAPED_SLASHES ) ) ) {
- wp_cache_debug( "wpsc_update_idle_preload: failed to write to $filename" );
- }
-}
-
-function wp_cron_preload_cache() {
- global $wpdb, $wp_cache_preload_interval, $wp_cache_preload_posts, $wp_cache_preload_email_me, $wp_cache_preload_email_volume, $cache_path, $wp_cache_preload_taxonomies;
-
- // check if stop_preload.txt exists and preload should be stopped.
- // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
- if ( @file_exists( $cache_path . 'stop_preload.txt' ) ) {
- wp_cache_debug( 'wp_cron_preload_cache: preload cancelled. Aborting preload.' );
- wpsc_reset_preload_settings();
- return true;
- }
-
- /*
- * The mutex file is used to prevent multiple preload processes from running at the same time.
- * If the mutex file is found, the preload process will wait 3-8 seconds and then check again.
- * If the mutex file is still found, the preload process will abort.
- * If the mutex file is not found, the preload process will create the mutex file and continue.
- * The mutex file is deleted at the end of the preload process.
- * The mutex file is deleted if it is more than 10 minutes old.
- * The mutex file should only be deleted by the preload process that created it.
- * If the mutex file is deleted by another process, another preload process may start.
- */
- $mutex = $cache_path . "preload_mutex.tmp";
- if ( @file_exists( $mutex ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
- sleep( 3 + wp_rand( 1, 5 ) );
- // check again just in case another preload process is still running.
- if ( @file_exists( $mutex ) && @filemtime( $mutex ) > ( time() - 600 ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
- wp_cache_debug( 'wp_cron_preload_cache: preload mutex found and less than 600 seconds old. Aborting preload.', 1 );
- return true;
- } else {
- wp_cache_debug( 'wp_cron_preload_cache: old preload mutex found and deleted. Preload continues.', 1 );
- @unlink( $mutex );
- }
- }
- $fp = @fopen( $mutex, 'w' );
- @fclose( $fp );
-
- $counter = get_option( 'preload_cache_counter' );
- $c = $counter[ 'c' ];
-
- if ( $wp_cache_preload_email_volume == 'none' && $wp_cache_preload_email_me == 1 ) {
- $wp_cache_preload_email_me = 0;
- wp_cache_setting( 'wp_cache_preload_email_me', 0 );
- }
-
- $just_started_preloading = false;
-
- /*
- * Preload taxonomies first.
- *
- */
- if ( isset( $wp_cache_preload_taxonomies ) && $wp_cache_preload_taxonomies ) {
- wp_cache_debug( 'wp_cron_preload_cache: doing taxonomy preload.', 5 );
- $taxonomies = apply_filters(
- 'wp_cache_preload_taxonomies',
- array(
- 'post_tag' => 'tag',
- 'category' => 'category',
- )
- );
-
- $preload_more_taxonomies = false;
-
- foreach ( $taxonomies as $taxonomy => $path ) {
- $taxonomy_filename = $cache_path . 'taxonomy_' . $taxonomy . '.txt';
-
- // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
- if ( false === @file_exists( $taxonomy_filename ) ) {
-
- if ( ! $just_started_preloading && $wp_cache_preload_email_me ) {
- // translators: 1: site url
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Started', 'wp-super-cache' ), home_url(), '' ), ' ' );
- }
-
- $just_started_preloading = true;
- $out = '';
- $records = get_terms( $taxonomy );
- foreach ( $records as $term ) {
- $out .= get_term_link( $term ) . "\n";
- }
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
- $fp = fopen( $taxonomy_filename, 'w' );
- if ( $fp ) {
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
- fwrite( $fp, $out );
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
- fclose( $fp );
- }
- $details = explode( "\n", $out );
- } else {
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
- $details = explode( "\n", file_get_contents( $taxonomy_filename ) );
- }
- if ( count( $details ) > 0 && $details[0] !== '' ) {
- $rows = array_splice( $details, 0, WPSC_PRELOAD_POST_COUNT );
- if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume === 'many' ) {
- // translators: 1: Site URL, 2: Taxonomy name, 3: Number of posts done, 4: Number of posts to preload
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Refreshing %2$s taxonomy from %3$d to %4$d', 'wp-super-cache' ), home_url(), $taxonomy, $c, ( $c + WPSC_PRELOAD_POST_COUNT ) ), 'Refreshing: ' . print_r( $rows, 1 ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
- }
-
- foreach ( $rows as $url ) {
- set_time_limit( 60 );
- if ( $url === '' ) {
- continue;
- }
-
- $url_info = wp_parse_url( $url );
- $dir = get_supercache_dir() . $url_info['path'];
- wp_cache_debug( "wp_cron_preload_cache: delete $dir" );
- wpsc_delete_files( $dir );
- prune_super_cache( trailingslashit( $dir ) . 'feed/', true );
- prune_super_cache( trailingslashit( $dir ) . 'page/', true );
-
- wpsc_update_active_preload( 'taxonomies', $taxonomy, $url );
-
- wp_remote_get(
- $url,
- array(
- 'timeout' => 60,
- 'blocking' => true,
- )
- );
- wp_cache_debug( "wp_cron_preload_cache: fetched $url" );
- sleep( WPSC_PRELOAD_POST_INTERVAL );
-
- if ( ! wpsc_is_preload_active() ) {
- wp_cache_debug( 'wp_cron_preload_cache: cancelling preload process.' );
- wpsc_reset_preload_settings();
-
- if ( $wp_cache_preload_email_me ) {
- // translators: Home URL of website
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Stopped', 'wp-super-cache' ), home_url(), '' ), ' ' );
- }
- wpsc_update_idle_preload( time() );
- return true;
- }
- }
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
- $fp = fopen( $taxonomy_filename, 'w' );
- if ( $fp ) {
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
- fwrite( $fp, implode( "\n", $details ) );
- // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
- fclose( $fp );
- }
- }
-
- if (
- $preload_more_taxonomies === false &&
- count( $details ) > 0 &&
- $details[0] !== ''
- ) {
- $preload_more_taxonomies = true;
- }
- }
-
- if ( $preload_more_taxonomies === true ) {
- wpsc_schedule_next_preload();
- sleep( WPSC_PRELOAD_LOOP_INTERVAL );
- return true;
- }
- } elseif ( $c === 0 && $wp_cache_preload_email_me ) {
- // translators: Home URL of website
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Started', 'wp-super-cache' ), home_url(), '' ), ' ' );
- }
-
- /*
- *
- * Preload posts now.
- *
- * The preload_cache_counter has two values:
- * c = the number of posts we've preloaded after this loop.
- * t = the time we started preloading in the current loop.
- *
- * $c is set to the value of preload_cache_counter['c'] at the start of the function
- * before it is incremented by WPSC_PRELOAD_POST_COUNT here.
- * The time is used to check if preloading has stalled in check_up_on_preloading().
- */
-
- update_option(
- 'preload_cache_counter',
- array(
- 'c' => ( $c + WPSC_PRELOAD_POST_COUNT ),
- 't' => time(),
- )
- );
-
- if ( $wp_cache_preload_posts == 'all' || $c < $wp_cache_preload_posts ) {
- $types = wpsc_get_post_types();
- $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ( post_type IN ( $types ) ) AND post_status = 'publish' ORDER BY ID DESC LIMIT %d," . WPSC_PRELOAD_POST_COUNT, $c ) ); // phpcs:ignore
- wp_cache_debug( 'wp_cron_preload_cache: got ' . WPSC_PRELOAD_POST_COUNT . ' posts from position ' . $c );
- } else {
- wp_cache_debug( "wp_cron_preload_cache: no more posts to get. Limit ($wp_cache_preload_posts) reached.", 5 );
- $posts = false;
- }
- if ( !isset( $wp_cache_preload_email_volume ) )
- $wp_cache_preload_email_volume = 'medium';
-
- if ( $posts ) {
- if ( get_option( 'show_on_front' ) == 'page' ) {
- $page_on_front = get_option( 'page_on_front' );
- $page_for_posts = get_option( 'page_for_posts' );
- } else {
- $page_on_front = $page_for_posts = 0;
- }
- if ( $wp_cache_preload_email_me && $wp_cache_preload_email_volume === 'many' ) {
- /* translators: 1: home url, 2: start post id, 3: end post id */
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Refreshing posts from %2$d to %3$d', 'wp-super-cache' ), home_url(), $c, ( $c + WPSC_PRELOAD_POST_COUNT ) ), ' ' );
- }
- $msg = '';
- $count = $c + 1;
-
- foreach( $posts as $post_id ) {
- set_time_limit( 60 );
- if ( $page_on_front != 0 && ( $post_id == $page_on_front || $post_id == $page_for_posts ) )
- continue;
- $url = get_permalink( $post_id );
-
- if ( ! is_string( $url ) ) {
- wp_cache_debug( "wp_cron_preload_cache: skipped $post_id. Expected a URL, received: " . gettype( $url ) );
- continue;
- }
-
- if ( wp_cache_is_rejected( $url ) ) {
- wp_cache_debug( "wp_cron_preload_cache: skipped $url per rejected strings setting" );
- continue;
- }
- clear_post_supercache( $post_id );
-
- wpsc_update_active_preload( 'posts', $count, $url );
-
- if ( ! wpsc_is_preload_active() ) {
- wp_cache_debug( 'wp_cron_preload_cache: cancelling preload process.' );
- wpsc_reset_preload_settings();
-
- if ( $wp_cache_preload_email_me ) {
- // translators: Home URL of website
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] Cache Preload Stopped', 'wp-super-cache' ), home_url(), '' ), ' ' );
- }
-
- wpsc_update_idle_preload( time() );
- return true;
- }
-
- $msg .= "$url\n";
- wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
- wp_cache_debug( "wp_cron_preload_cache: fetched $url", 5 );
- ++$count;
- sleep( WPSC_PRELOAD_POST_INTERVAL );
- }
-
- if ( $wp_cache_preload_email_me && ( $wp_cache_preload_email_volume === 'medium' || $wp_cache_preload_email_volume === 'many' ) ) {
- // translators: 1: home url, 2: number of posts refreshed
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] %2$d posts refreshed', 'wp-super-cache' ), home_url(), ( $c + WPSC_PRELOAD_POST_COUNT ) ), __( 'Refreshed the following posts:', 'wp-super-cache' ) . "\n$msg" );
- }
-
- wpsc_schedule_next_preload();
- wpsc_delete_files( get_supercache_dir() );
- sleep( WPSC_PRELOAD_LOOP_INTERVAL );
- } else {
- $msg = '';
- wpsc_reset_preload_counter();
- if ( (int)$wp_cache_preload_interval && defined( 'DOING_CRON' ) ) {
- if ( $wp_cache_preload_email_me )
- $msg = sprintf( __( 'Scheduling next preload refresh in %d minutes.', 'wp-super-cache' ), (int)$wp_cache_preload_interval );
- wp_cache_debug( "wp_cron_preload_cache: no more posts. scheduling next preload in $wp_cache_preload_interval minutes.", 5 );
- wp_schedule_single_event( time() + ( (int)$wp_cache_preload_interval * 60 ), 'wp_cache_full_preload_hook' );
- }
- global $file_prefix, $cache_max_time;
- if ( $wp_cache_preload_interval > 0 ) {
- $cache_max_time = (int)$wp_cache_preload_interval * 60; // fool the GC into expiring really old files
- } else {
- $cache_max_time = 86400; // fool the GC into expiring really old files
- }
- if ( $wp_cache_preload_email_me )
- wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Cache Preload Completed', 'wp-super-cache' ), home_url() ), __( "Cleaning up old supercache files.", 'wp-super-cache' ) . "\n" . $msg );
- if ( $cache_max_time > 0 ) { // GC is NOT disabled
- wp_cache_debug( "wp_cron_preload_cache: clean expired cache files older than $cache_max_time seconds.", 5 );
- wp_cache_phase2_clean_expired( $file_prefix, true ); // force cleanup of old files.
- }
-
- wpsc_reset_preload_settings();
- wpsc_update_idle_preload( time() );
- }
- @unlink( $mutex );
-}
-add_action( 'wp_cache_preload_hook', 'wp_cron_preload_cache' );
-add_action( 'wp_cache_full_preload_hook', 'wp_cron_preload_cache' );
-
-/*
- * Schedule the next preload event without resetting the preload counter.
- * This happens when the next loop of an active preload is scheduled.
- */
-function wpsc_schedule_next_preload() {
- global $cache_path;
-
- /*
- * Edge case: If preload is not active, don't schedule the next preload.
- * This can happen if the preload is cancelled by the user right after a loop finishes.
- */
- if ( ! wpsc_is_preload_active() ) {
- wpsc_reset_preload_settings();
- wp_cache_debug( 'wpsc_schedule_next_preload: preload is not active. not scheduling next preload.' );
- return;
- }
-
- if ( defined( 'DOING_CRON' ) ) {
- wp_cache_debug( 'wp_cron_preload_cache: scheduling the next preload in 3 seconds.' );
- wp_schedule_single_event( time() + 3, 'wp_cache_preload_hook' );
- }
-
- // we always want to delete the mutex file, even if we're not using cron
- $mutex = $cache_path . 'preload_mutex.tmp';
- wp_delete_file( $mutex );
-}
-
-function option_preload_cache_counter( $value ) {
- if ( false == is_array( $value ) ) {
- return array(
- 'c' => 0,
- 't' => time(),
- );
- } else {
- return $value;
- }
-}
-add_filter( 'option_preload_cache_counter', 'option_preload_cache_counter' );
-
-function check_up_on_preloading() {
- $value = get_option( 'preload_cache_counter' );
- if ( is_array( $value ) && $value['c'] > 0 && ( time() - $value['t'] ) > 3600 && false === wp_next_scheduled( 'wp_cache_preload_hook' ) ) {
- wp_schedule_single_event( time() + 5, 'wp_cache_preload_hook' );
- }
-}
-add_action( 'init', 'check_up_on_preloading' ); // sometimes preloading stops working. Kickstart it.
-
-function wp_cache_disable_plugin( $delete_config_file = true ) {
- global $wp_rewrite;
- if ( file_exists( ABSPATH . 'wp-config.php') ) {
- $global_config_file = ABSPATH . 'wp-config.php';
- } else {
- $global_config_file = dirname(ABSPATH) . '/wp-config.php';
- }
-
- if ( apply_filters( 'wpsc_enable_wp_config_edit', true ) ) {
- $line = 'define(\'WP_CACHE\', true);';
- if (
- strpos( file_get_contents( $global_config_file ), $line ) &&
- (
- ! is_writeable_ACLSafe( $global_config_file ) ||
- ! wp_cache_replace_line( 'define*\(*\'WP_CACHE\'', '', $global_config_file )
- )
- ) {
- wp_die( "Could not remove WP_CACHE define from $global_config_file. Please edit that file and remove the line containing the text 'WP_CACHE'. Then refresh this page." );
- }
- $line = 'define( \'WPCACHEHOME\',';
- if (
- strpos( file_get_contents( $global_config_file ), $line ) &&
- (
- ! is_writeable_ACLSafe( $global_config_file ) ||
- ! wp_cache_replace_line( 'define *\( *\'WPCACHEHOME\'', '', $global_config_file )
- )
- ) {
- wp_die( "Could not remove WPCACHEHOME define from $global_config_file. Please edit that file and remove the line containing the text 'WPCACHEHOME'. Then refresh this page." );
- }
- } elseif ( function_exists( 'wp_cache_debug' ) ) {
- wp_cache_debug( 'wp_cache_disable_plugin: not allowed to edit wp-config.php per configuration.' );
- }
-
- uninstall_supercache( WP_CONTENT_DIR . '/cache' );
- $file_not_deleted = array();
- wpsc_remove_advanced_cache();
- if ( @file_exists( WP_CONTENT_DIR . "/advanced-cache.php" ) ) {
- $file_not_deleted[] = WP_CONTENT_DIR . '/advanced-cache.php';
- }
- if ( $delete_config_file && @file_exists( WPCACHECONFIGPATH . "/wp-cache-config.php" ) ) {
- if ( false == unlink( WPCACHECONFIGPATH . "/wp-cache-config.php" ) )
- $file_not_deleted[] = WPCACHECONFIGPATH . '/wp-cache-config.php';
- }
- if ( ! empty( $file_not_deleted ) ) {
- $msg = __( "Dear User,\n\nWP Super Cache was removed from your blog or deactivated but some files could\nnot be deleted.\n\n", 'wp-super-cache' );
- foreach ( $file_not_deleted as $path ) {
- $msg .= "{$path}\n";
- }
- $msg .= "\n";
- $msg .= sprintf( __( "You should delete these files manually.\nYou may need to change the permissions of the files or parent directory.\nYou can read more about this in the Codex at\n%s\n\nThank you.", 'wp-super-cache' ), 'https://codex.wordpress.org/Changing_File_Permissions#About_Chmod' );
-
- if ( apply_filters( 'wpsc_send_uninstall_errors', 1 ) ) {
- wp_mail( get_option( 'admin_email' ), __( 'WP Super Cache: could not delete files', 'wp-super-cache' ), $msg );
- }
- }
- extract( wpsc_get_htaccess_info() ); // $document_root, $apache_root, $home_path, $home_root, $home_root_lc, $inst_root, $wprules, $scrules, $condition_rules, $rules, $gziprules
- // @phan-suppress-next-line PhanTypeSuspiciousStringExpression -- $home_path is set via extract()
- if ( $scrules != '' && insert_with_markers( $home_path.'.htaccess', 'WPSuperCache', array() ) ) {
- $wp_rewrite->flush_rules();
- } elseif( $scrules != '' ) {
- wp_mail( get_option( 'admin_email' ), __( 'Supercache Uninstall Problems', 'wp-super-cache' ), sprintf( __( "Dear User,\n\nWP Super Cache was removed from your blog but the mod_rewrite rules\nin your .htaccess were not.\n\nPlease edit the following file and remove the code\nbetween 'BEGIN WPSuperCache' and 'END WPSuperCache'. Please backup the file first!\n\n%s\n\nRegards,\nWP Super Cache Plugin\nhttps://wordpress.org/plugins/wp-super-cache/", 'wp-super-cache' ), ABSPATH . '/.htaccess' ) );
- }
-}
-
-function uninstall_supercache( $folderPath ) { // from http://www.php.net/manual/en/function.rmdir.php
- if ( trailingslashit( constant( 'ABSPATH' ) ) == trailingslashit( $folderPath ) )
- return false;
- if ( @is_dir ( $folderPath ) ) {
- $dh = @opendir($folderPath);
- while( false !== ( $value = @readdir( $dh ) ) ) {
- if ( $value != "." && $value != ".." ) {
- $value = $folderPath . "/" . $value;
- if ( @is_dir ( $value ) ) {
- uninstall_supercache( $value );
- } else {
- @unlink( $value );
- }
- }
- }
- return @rmdir( $folderPath );
- } else {
- return false;
- }
-}
-
-function supercache_admin_bar_render() {
- global $wp_admin_bar;
-
- if ( function_exists( '_deprecated_function' ) ) {
- _deprecated_function( __FUNCTION__, 'WP Super Cache 1.6.4' );
- }
-
- wpsc_admin_bar_render( $wp_admin_bar );
-}
-
-/*
- * returns true if preload is active
- */
-function wpsc_is_preload_active() {
- global $cache_path;
-
- // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
- if ( @file_exists( $cache_path . 'stop_preload.txt' ) ) {
- return false;
- }
-
- if ( file_exists( $cache_path . 'preload_mutex.tmp' ) ) {
- return true;
- }
-
- // check taxonomy preload loop
- $taxonomies = apply_filters(
- 'wp_cache_preload_taxonomies',
- array(
- 'post_tag' => 'tag',
- 'category' => 'category',
- )
- );
-
- foreach ( $taxonomies as $taxonomy => $path ) {
- $taxonomy_filename = $cache_path . 'taxonomy_' . $taxonomy . '.txt';
- if ( file_exists( $taxonomy_filename ) ) {
- return true;
- }
- }
-
- // check post preload loop
- $preload_cache_counter = get_option( 'preload_cache_counter' );
- if (
- is_array( $preload_cache_counter )
- && isset( $preload_cache_counter['c'] )
- && $preload_cache_counter['c'] > 0
- ) {
- return true;
- }
-
- return false;
-}
-
-/*
- * This function will reset the preload cache counter
- */
-function wpsc_reset_preload_counter() {
- update_option(
- 'preload_cache_counter',
- array(
- 'c' => 0,
- 't' => time(),
- )
- );
-}
-
-/*
- * This function will reset all preload settings
- */
-function wpsc_reset_preload_settings() {
- global $cache_path;
-
- $mutex = $cache_path . 'preload_mutex.tmp';
- wp_delete_file( $mutex );
- wp_delete_file( $cache_path . 'stop_preload.txt' );
- wpsc_reset_preload_counter();
-
- $taxonomies = apply_filters(
- 'wp_cache_preload_taxonomies',
- array(
- 'post_tag' => 'tag',
- 'category' => 'category',
- )
- );
-
- foreach ( $taxonomies as $taxonomy => $path ) {
- $taxonomy_filename = $cache_path . 'taxonomy_' . $taxonomy . '.txt';
- wp_delete_file( $taxonomy_filename );
- }
-}
-
-function wpsc_cancel_preload() {
- $next_preload = wp_next_scheduled( 'wp_cache_preload_hook' );
- $next_full_preload = wp_next_scheduled( 'wp_cache_full_preload_hook' );
-
- if ( $next_preload || $next_full_preload ) {
- wp_cache_debug( 'wpsc_cancel_preload: reset preload settings' );
- wpsc_reset_preload_settings();
- }
-
- if ( $next_preload ) {
- wp_cache_debug( 'wpsc_cancel_preload: unscheduling wp_cache_preload_hook' );
- wp_unschedule_event( $next_preload, 'wp_cache_preload_hook' );
- }
- if ( $next_full_preload ) {
- wp_cache_debug( 'wpsc_cancel_preload: unscheduling wp_cache_full_preload_hook' );
- wp_unschedule_event( $next_full_preload, 'wp_cache_full_preload_hook' );
- }
- wp_cache_debug( 'wpsc_cancel_preload: creating stop_preload.txt' );
-
- /*
- * Reset the preload settings, but also create the stop_preload.txt file to
- * prevent the preload from starting again.
- * By creating the stop_preload.txt file, we can be sure the preload will cancel.
- */
- wpsc_reset_preload_settings();
- wpsc_create_stop_preload_flag();
- wpsc_update_idle_preload( time() );
-}
-
-/*
- * The preload process checks for a file called stop_preload.txt and will stop if found.
- * This function creates that file.
- */
-function wpsc_create_stop_preload_flag() {
- global $cache_path;
- // phpcs:ignore -- WordPress.WP.AlternativeFunctions.file_system_read_fopen WordPress.PHP.NoSilencedErrors.Discouraged
- $fp = @fopen( $cache_path . 'stop_preload.txt', 'w' );
- // phpcs:ignore -- WordPress.WP.AlternativeFunctions.file_system_operations_fclose WordPress.PHP.NoSilencedErrors.Discouraged
- @fclose( $fp );
-}
-
-function wpsc_enable_preload() {
-
- wpsc_reset_preload_settings();
- wp_schedule_single_event( time() + 10, 'wp_cache_full_preload_hook' );
-}
-
-function wpsc_get_post_types() {
-
- $preload_type_args = apply_filters( 'wpsc_preload_post_types_args', array(
- 'public' => true,
- 'publicly_queryable' => true
- ) );
-
- $post_types = (array) apply_filters( 'wpsc_preload_post_types', get_post_types( $preload_type_args, 'names', 'or' ));
-
- return "'" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "'";
-}
-function wpsc_post_count() {
- global $wpdb;
- static $count;
-
- if ( isset( $count ) ) {
- return $count;
- }
-
- $post_type_list = wpsc_get_post_types();
- $count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ( $post_type_list ) AND post_status = 'publish'" );
-
- return $count;
-}
-
-/**
- * Get the minimum interval in minutes between preload refreshes.
- * Filter the default value of 10 minutes using the `wpsc_minimum_preload_interval` filter.
- *
- * @return int
- */
-function wpsc_get_minimum_preload_interval() {
- return apply_filters( 'wpsc_minimum_preload_interval', 10 );
-}
-
-function wpsc_preload_settings() {
- global $wp_cache_preload_interval, $wp_cache_preload_on, $wp_cache_preload_taxonomies, $wp_cache_preload_email_volume, $wp_cache_preload_posts, $valid_nonce;
-
- if ( isset( $_POST[ 'action' ] ) == false || $_POST[ 'action' ] != 'preload' )
- return;
-
- if ( ! $valid_nonce ) {
- return;
- }
-
- if ( isset( $_POST[ 'preload_off' ] ) ) {
- wpsc_cancel_preload();
- return;
- } elseif ( isset( $_POST[ 'preload_now' ] ) ) {
- wpsc_enable_preload();
- wpsc_update_idle_preload();
- ?>
-