From 3dbc17c1ddb60b9ad81c07f570f0d7e5f9f325c7 Mon Sep 17 00:00:00 2001 From: stodorovic Date: Fri, 7 Sep 2018 11:48:13 +0200 Subject: [PATCH 1/2] Optimization for multisite plugin --- plugins/multisite.php | 73 +++++++++++++++++++++++++++++-------------- wp-cache-base.php | 5 +-- wp-cache-phase1.php | 6 ---- 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/plugins/multisite.php b/plugins/multisite.php index df71f9ac..88e371e7 100644 --- a/plugins/multisite.php +++ b/plugins/multisite.php @@ -1,13 +1,28 @@ 'enable_cache', 'id' => $blog_id ) ), 'wp-cache' . $blog_id ) . '">' . __( 'Enable', 'wp-super-cache' ) . ''; - } else { - echo '' . __( 'Disable', 'wp-super-cache' ) . ''; - } + $cache_option = (int) get_blog_option( $blog_id, 'wp_super_cache_disabled' ) === 1; + + printf( '%s', + esc_url_raw( wp_nonce_url( + add_query_arg( array( + 'action' => $cache_option ? 'enable_cache' : 'disable_cache', + 'id' => $blog_id, + ) ), + 'wp-cache' . $blog_id + ) ), + esc_html( $cache_option ? __( 'Enable', 'wp-super-cache' ) : __( 'Disable', 'wp-super-cache' ) ) + ); } function wp_super_cache_multisite_notice() { - if ( 'wpsupercache' === filter_input( INPUT_GET, 'page' ) ) { - echo '

' . __( 'Caching has been disabled on this blog on the Network Admin Sites page.', 'wp-super-cache' ) . '

'; + if ( filter_input( INPUT_GET, 'page' ) === 'wpsupercache' ) { + echo '

' . esc_html__( 'Caching has been disabled on this blog on the Network Admin Sites page.', 'wp-super-cache' ) . '

'; } } function wp_super_cache_override_on_flag() { - global $cache_enabled, $super_cache_enabled; - if ( true !== $cache_enabled ) { + global $cache_enabled, $super_cache_enabled, $cache_path, $blogcacheid, $blog_cache_dir, $current_blog; + + if ( is_object( $current_blog ) ) { + $blogcacheid = trim( is_subdomain_install() ? $current_blog->domain : $current_blog->path, '/' ); + $blog_cache_dir = $cache_path . 'blogs/' . $blogcacheid . '/'; + } + + if ( true !== $cache_enabled || (int) get_option( 'wp_super_cache_disabled' ) !== 1 ) { return false; } - if ( 1 === (int) get_option( 'wp_super_cache_disabled' ) ) { - $cache_enabled = false; - $super_cache_enabled = false; - define( 'DONOTCACHEPAGE', 1 ); + $cache_enabled = false; + $super_cache_enabled = false; + defined( 'DONOTCACHEPAGE' ) || define( 'DONOTCACHEPAGE', 1 ); + + if ( is_admin() ) { define( 'SUBMITDISABLED', 'disabled style="color: #aaa" ' ); - if ( is_admin() ) { - add_action( 'admin_notices', 'wp_super_cache_multisite_notice' ); - } + add_action( 'admin_notices', 'wp_super_cache_multisite_notice' ); } } diff --git a/wp-cache-base.php b/wp-cache-base.php index df65196c..935ec838 100644 --- a/wp-cache-base.php +++ b/wp-cache-base.php @@ -16,11 +16,8 @@ $blog_cache_dir = $cache_path; if ( is_multisite() ) { - global $current_blog; - if ( is_object( $current_blog ) && function_exists( 'is_subdomain_install' ) ) { - $blogcacheid = is_subdomain_install() ? $current_blog->domain : trim( $current_blog->path, '/' ); - } elseif ( ( defined( 'SUBDOMAIN_INSTALL' ) && SUBDOMAIN_INSTALL ) || ( defined( 'VHOST' ) && VHOST === 'yes' ) ) { + if ( ( defined( 'SUBDOMAIN_INSTALL' ) && SUBDOMAIN_INSTALL ) || ( defined( 'VHOST' ) && VHOST === 'yes' ) ) { $blogcacheid = $WPSC_HTTP_HOST; } else { $request_uri = str_replace( '..', '', preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', $_SERVER['REQUEST_URI'] ) ); diff --git a/wp-cache-phase1.php b/wp-cache-phase1.php index 3a3e047f..3132b368 100644 --- a/wp-cache-phase1.php +++ b/wp-cache-phase1.php @@ -20,12 +20,6 @@ require WPCACHEHOME . 'wp-cache-base.php'; -if ( $blogcacheid != '' ) { - $blog_cache_dir = str_replace( '//', '/', $cache_path . 'blogs/' . $blogcacheid . '/' ); -} else { - $blog_cache_dir = $cache_path; -} - $wp_cache_phase1_loaded = true; $mutex_filename = 'wp_cache_mutex.lock'; From a5b443eaa521182124348162d534626a0dfa442a Mon Sep 17 00:00:00 2001 From: stodorovic Date: Fri, 14 Sep 2018 12:15:52 +0200 Subject: [PATCH 2/2] PHPCS fixes for plugins/multisite.php --- plugins/multisite.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/plugins/multisite.php b/plugins/multisite.php index 88e371e7..ccc746a3 100644 --- a/plugins/multisite.php +++ b/plugins/multisite.php @@ -37,9 +37,9 @@ function wp_super_cache_blogs_field( $name, $blog_id ) { $blog_id = (int) $blog_id; - if ( isset( $_GET['id'], $_GET['action'], $_GET['_wpnonce'] ) + if ( isset( $_GET['id'], $_GET['action'], $_GET['_wpnonce'] ) // WPCS: input var ok. && filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT ) === $blog_id - && wp_verify_nonce( $_GET['_wpnonce'], 'wp-cache' . $blog_id ) + && wp_verify_nonce( $_GET['_wpnonce'], 'wp-cache' . $blog_id ) // WPCS: sanitization ok. ) { if ( filter_input( INPUT_GET, 'action' ) === 'disable_cache' ) { add_blog_option( $blog_id, 'wp_super_cache_disabled', 1 ); @@ -50,16 +50,13 @@ function wp_super_cache_blogs_field( $name, $blog_id ) { $cache_option = (int) get_blog_option( $blog_id, 'wp_super_cache_disabled' ) === 1; - printf( '%s', - esc_url_raw( wp_nonce_url( - add_query_arg( array( - 'action' => $cache_option ? 'enable_cache' : 'disable_cache', - 'id' => $blog_id, - ) ), - 'wp-cache' . $blog_id - ) ), - esc_html( $cache_option ? __( 'Enable', 'wp-super-cache' ) : __( 'Disable', 'wp-super-cache' ) ) + $action_text = $cache_option ? __( 'Enable', 'wp-super-cache' ) : __( 'Disable', 'wp-super-cache' ); + $action_args = array( + 'action' => $cache_option ? 'enable_cache' : 'disable_cache', + 'id' => $blog_id, + '_wpnonce' => wp_create_nonce( 'wp-cache' . $blog_id ), ); + printf( '%s', esc_url_raw( add_query_arg( $action_args ) ), esc_html( $action_text ) ); } function wp_super_cache_multisite_notice() {