Skip to content

Commit

Permalink
*Add full multisite compatibilty using site transients and network wi…
Browse files Browse the repository at this point in the history
…de queue tables
  • Loading branch information
pcfreak30 committed Mar 26, 2017
1 parent 9af9878 commit 0189287
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 33 deletions.
59 changes: 51 additions & 8 deletions lib/class-wp-criticalcss-background-process.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );

abstract class WP_CriticalCSS_Background_Process extends WP_Background_Process {
/**
Expand All @@ -21,9 +22,14 @@ public function get_batch() {
$batch->data = array();
$batch->key = '';
if ( ! $this->is_queue_empty() ) {
if ( is_multisite() ) {
$table = "{$wpdb->base_prefix}{$this->action}_queue";
} else {
$table = "{$wpdb->prefix}{$this->action}_queue";
}
$result = $wpdb->get_row( "
SELECT *
FROM `{$wpdb->prefix}{$this->action}_queue`
FROM `{$table}`
LIMIT 1
" );
$batch = new stdClass();
Expand Down Expand Up @@ -73,13 +79,23 @@ public function create_table() {
include_once ABSPATH . 'wp-admin/includes/upgrade.php';

$charset_collate = $wpdb->get_charset_collate();
dbDelta( "CREATE TABLE {$wpdb->prefix}{$this->action}_queue (
if ( is_multisite() ) {
$table = "{$wpdb->base_prefix}{$this->action}_queue";
} else {
$table = "{$wpdb->prefix}{$this->action}_queue";
}
$sql = "CREATE TABLE $table (
id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
template VARCHAR(255),
object_id BIGINT(10),
type VARCHAR (10),
url TEXT,
data TEXT,
";
if ( is_multisite() ) {
$sql .= "blog_id BIGINT(20)";
}
dbDelta( "$sql
PRIMARY KEY (id)
) {$charset_collate};" );
}
Expand All @@ -88,9 +104,13 @@ public function get_item_exists( $item ) {
global $wpdb;

$args = array();

if ( is_multisite() ) {
$table = "{$wpdb->base_prefix}{$this->action}_queue";
} else {
$table = "{$wpdb->prefix}{$this->action}_queue";
}
$sql = "SELECT *
FROM `{$wpdb->prefix}{$this->action}_queue`
FROM `{$table}`
WHERE ";
if ( 'url' == $item['type'] ) {
$sql .= '`url` = %s';
Expand All @@ -104,6 +124,10 @@ public function get_item_exists( $item ) {
$sql .= ' AND `template` = %s';
$args[] = $item['template'];
}
if ( is_multisite() ) {
$sql .= ' AND `blog_id` = %d';
$args[] = get_current_blog_id();
}
$result = $wpdb->get_row( $wpdb->prepare( $sql, $args ) );

if ( is_null( $result ) ) {
Expand All @@ -115,17 +139,26 @@ public function get_item_exists( $item ) {

public function save() {
global $wpdb;
if ( is_multisite() ) {
$table = "{$wpdb->base_prefix}{$this->action}_queue";
} else {
$table = "{$wpdb->prefix}{$this->action}_queue";
}
foreach ( $this->data as $item ) {
$data = array_merge( array(), $item );
unset( $data['object_id'] );
unset( $data['type'] );
unset( $data['url'] );
unset( $data['template'] );
if ( is_multisite() ) {
unset( $data['blog_id'] );
}
unset( $data['template'] );
$item['data'] = maybe_serialize( $data );
$item = array_diff_key( $item, $data );
$wpdb->insert( "{$wpdb->prefix}{$this->action}_queue", $item );
$wpdb->insert( $table, $item );
if ( class_exists( 'WPECommon' ) ) {
$wpdb->query( "DELETE q1 FROM {$wpdb->prefix}{$this->action}_queue q1, {$wpdb->prefix}{$this->action}_queue q2 WHERE q1.id > q2.id
$wpdb->query( "DELETE q1 FROM $table q1, $table q2 WHERE q1.id > q2.id
AND (
(
q1.object_id = q2.object_id AND q1.type != 'url' AND q2.type != 'url'
Expand Down Expand Up @@ -160,11 +193,21 @@ public function update( $key, $items ) {

public function purge() {
global $wpdb;
$wpdb->query( "TRUNCATE `{$wpdb->prefix}{$this->action}_queue`" );
if ( is_multisite() ) {
$table = "{$wpdb->base_prefix}{$this->action}_queue";
} else {
$table = "{$wpdb->prefix}{$this->action}_queue";
}
$wpdb->query( "TRUNCATE `{$table}`" );
}

public function delete( $key ) {
global $wpdb;
$wpdb->delete( "{$wpdb->prefix}{$this->action}_queue", array( 'id' => (int) $key ) );
if ( is_multisite() ) {
$table = "{$wpdb->base_prefix}{$this->action}_queue";
} else {
$table = "{$wpdb->prefix}{$this->action}_queue";
}
$wpdb->delete( $table, array( 'id' => (int) $key ) );
}
}
36 changes: 32 additions & 4 deletions lib/class-wp-criticalcss-queue-list-table.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

/**
Expand Down Expand Up @@ -42,6 +42,9 @@ function get_columns() {
'status' => __( 'Status', WP_CriticalCSS::LANG_DOMAIN ),
'queue_position' => __( 'Queue Position', WP_CriticalCSS::LANG_DOMAIN ),
);
if ( is_multisite() ) {
$columns = array_merge( array( 'blog_id' => __( 'Blog', WP_CriticalCSS::LANG_DOMAIN ) ), $columns );
}

return $columns;
}
Expand All @@ -57,12 +60,18 @@ public function prepare_items() {

$per_page = $this->get_items_per_page( 'queue_items_per_page', 20 );

if ( is_multisite() ) {
$table = "{$wpdb->base_prefix}wp_criticalcss_api_queue";
} else {
$table = "{$wpdb->prefix}wp_criticalcss_api_queue";
}

$total_items = $wpdb->get_var( "SELECT COUNT(id) FROM {$wpdb->prefix}wp_criticalcss_api_queue" );

$paged = $this->get_pagenum();
$start = ( $paged - 1 ) * $per_page;

$this->items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wp_criticalcss_api_queue LIMIT %d,%d", $start, $per_page ), ARRAY_A );
$this->items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} LIMIT %d,%d", $start, $per_page ), ARRAY_A );

$this->set_pagination_args( array(
'total_items' => $total_items,
Expand All @@ -85,7 +94,26 @@ protected function get_bulk_actions() {
/**
* @param array $item
*
* @return false|mixed|string|\WP_Error
* @return string
*/
protected function column_blog_id( array $item ) {
if ( empty( $item['blog_id'] ) ) {
return __( 'N/A', WP_CriticalCSS::LANG_DOMAIN );
}

$details = get_blog_details( array( 'blog_id' => $item['blog_id'] ) );

if ( empty( $details ) ) {
return __( 'Blog Deleted', WP_CriticalCSS::LANG_DOMAIN );
}

return $details->blogname;
}

/**
* @param array $item
*
* @return string
*/
protected function column_url( array $item ) {
$settings = WP_CriticalCSS::get_settings();
Expand All @@ -99,7 +127,7 @@ protected function column_url( array $item ) {
/**
* @param array $item
*
* @return false|mixed|string|\WP_Error
* @return string
*/
protected function column_template( array $item ) {
$settings = WP_CriticalCSS::get_settings();
Expand Down
72 changes: 51 additions & 21 deletions lib/class-wp-criticalcss.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ public static function activate() {
$wpdb->get_results( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", '_transient_criticalcss_%', '_transient_timeout_criticalcss_%' ) );
}

if ( is_multisite() ) {
foreach ( get_sites( array( 'fields' => 'ids', 'site__not_in' => array( 1 ) ) ) as $blog_id ) {
switch_to_blog( $blog_id );
$wpdb->query( "DROP TABLE {$wpdb->prefix}_wp_criticalcss_web_check_queue IF EXISTS" );
$wpdb->query( "DROP TABLE {$wpdb->prefix}_wp_criticalcss_api_queue IF EXISTS" );
restore_current_blog();
}
}

self::update_settings( array_merge( array(
'web_check_interval' => DAY_IN_SECONDS,
'template_cache' => 'off',
Expand Down Expand Up @@ -613,7 +622,11 @@ protected static function get_current_page_type() {
$template = self::$_template;
}

return compact( 'object_id', 'type', 'url', 'template' );
if ( is_multisite() ) {
$blog_id = get_current_blog_id();
}

return compact( 'object_id', 'type', 'url', 'template', 'blog_id' );
}

public static function get_item_hash( $item ) {
Expand All @@ -633,7 +646,15 @@ protected static function get_cache_fragment( $path ) {
array_unshift( $path, 'cache' );
}

return get_transient( self::TRANSIENT_PREFIX . implode( '_', $path ) );
return self::get_transient( self::TRANSIENT_PREFIX . implode( '_', $path ) );
}

protected static function get_transient() {
if ( is_multisite() ) {
return call_user_func_array( 'get_site_transient', func_get_args() );
} else {
return call_user_func_array( 'get_transient', func_get_args() );
}
}

protected static function update_cache_fragment( $path, $value ) {
Expand All @@ -654,23 +675,23 @@ protected static function build_cache_tree( $path ) {
$transient_cache_id .= '_cache';
}
$transient_cache_id .= '_1';
$cache = get_transient( $transient_cache_id );
$cache = self::get_transient( $transient_cache_id );
$transient_value = array();
if ( $i + 1 < $levels ) {
$transient_value[] = self::TRANSIENT_PREFIX . implode( '_', array_slice( $path, 0, $i + 2 ) );
}
if ( ! is_null( $cache ) && false !== $cache ) {
$transient_value = array_unique( array_merge( $cache, $transient_value ) );
}
set_transient( $transient_cache_id, $transient_value, $expire );
self::set_transient( $transient_cache_id, $transient_value, $expire );
$transient_counter_id = $transient_id;
if ( 'cache' != $path[ $i ] ) {
$transient_counter_id .= '_cache';
}
$transient_counter_id .= '_count';
$transient_counter = get_transient( $transient_counter_id );
$transient_counter = self::get_transient( $transient_counter_id );
if ( is_null( $transient_counter ) || false === $transient_counter ) {
set_transient( $transient_counter_id, 1, $expire );
self::set_transient( $transient_counter_id, 1, $expire );
}
}
}
Expand All @@ -688,6 +709,14 @@ public static function get_expire_period() {
return absint( self::$_settings['web_check_interval'] );
}

protected static function set_transient() {
if ( is_multisite() ) {
call_user_func_array( 'set_site_transient', func_get_args() );
} else {
call_user_func_array( 'set_transient', func_get_args() );
}
}

protected static function update_tree_branch( $path, $value ) {
$branch = self::TRANSIENT_PREFIX . implode( '_', $path );
$parent_path = array_slice( $path, 0, count( $path ) - 1 );
Expand All @@ -699,16 +728,16 @@ protected static function update_tree_branch( $path, $value ) {
$cache_transient .= '_cache';
}
$counter_transient .= '_count';
$counter = (int) get_transient( $counter_transient );
$counter = (int) self::get_transient( $counter_transient );
$cache_transient .= "_{$counter}";
$cache = get_transient( $cache_transient );
$cache = self::get_transient( $cache_transient );
$count = count( $cache );
$cache_keys = array_flip( $cache );
$expire = self::get_expire_period();
if ( ! isset( $cache_keys[ $branch ] ) ) {
if ( $count >= apply_filters( 'rocket_async_css_max_branch_length', 50 ) ) {
$counter ++;
set_transient( $counter_transient, $counter, $expire );
self::set_transient( $counter_transient, $counter, $expire );
$cache_transient = $parent;
if ( 'cache' != end( $parent_path ) ) {
$cache_transient .= '_cache';
Expand All @@ -717,9 +746,9 @@ protected static function update_tree_branch( $path, $value ) {
$cache = array();
}
$cache[] = $branch;
set_transient( $cache_transient, $cache, $expire );
self::set_transient( $cache_transient, $cache, $expire );
}
set_transient( $branch, $value, $expire );
self::set_transient( $branch, $value, $expire );
}

/**
Expand Down Expand Up @@ -948,13 +977,6 @@ public static function sync_options( $value, $old_value ) {
*
*/
public static function reset_web_check_transients() {
if ( is_multisite() ) {
foreach ( get_sites( array( 'fields' => 'ids', 'site__not_in' => get_current_blog_id() ) ) as $blog_id ) {
switch_to_blog( $blog_id );
self::delete_cache_branch();
restore_current_blog();
}
}
self::delete_cache_branch();
}

Expand All @@ -970,7 +992,7 @@ protected static function delete_cache_branch( $path = array() ) {
$counter = get_transient( $counter_transient );

if ( is_null( $counter ) || false === $counter ) {
delete_transient( rtrim( $path, '_' ) );
self::delete_transient( rtrim( $path, '_' ) );

return;
}
Expand All @@ -981,10 +1003,18 @@ protected static function delete_cache_branch( $path = array() ) {
foreach ( $cache as $sub_branch ) {
self::delete_cache_branch( "{$sub_branch}_" );
}
delete_transient( $transient_name );
self::delete_transient( $transient_name );
}
}
delete_transient( $counter_transient );
self::delete_transient( $counter_transient );
}

protected static function delete_transient() {
if ( is_multisite() ) {
return call_user_func_array( 'delete_site_transient', func_get_args() );
} else {
return call_user_func_array( 'delete_transient', func_get_args() );
}
}

public static function reset_web_check_post_transient( $post ) {
Expand Down

0 comments on commit 0189287

Please sign in to comment.