Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spacedmonkey/ee2d8fb3d2337568839316ab521caaba to your computer and use it in GitHub Desktop.
Save spacedmonkey/ee2d8fb3d2337568839316ab521caaba to your computer and use it in GitHub Desktop.
<?php
/**
* Sync key options with blog meta
*
* @package Blog_Meta_Sync
* @author Jonathan Harris <[email protected]>
* @license GPL-2.0+
* @link http://www.jonathandavidharris.co.uk/
* @copyright 2017 Spacedmonkey
*
* @wordpress-plugin
* Plugin Name: Blog Meta option syncing
* Plugin URI: https://www.github.com/spacedmonkey/advanced-nav-cache
* Description: Sync key options with blog meta
* Version: 1.0.0
* Author: Jonathan Harris
* Author URI: http://www.jonathandavidharris.co.uk/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: https://www.github.com/spacedmonkey/blog-meta-sync
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
class Blog_Meta_Sync{
/**
* List of filters to run this string replace on
*
* @since 1.0.0
*
* @var array
*/
protected $filters = array(
'stylesheet',
'blog_charset',
'template',
'WPLANG',
'blogname',
'siteurl',
'post_count',
'home',
'allowedthemes',
'blog_public',
'WPLANG',
'blogdescription',
'db_version',
'db_upgraded',
'active_plugins',
'users_can_register',
'admin_email',
'wp_user_roles'
);
public function __construct(){
foreach( $this->filters as $filter ){
add_filter( 'pre_option_'.$filter, array( $this, 'pre_get_option' ), 1, 2 );
}
add_action( 'updated_option', array( $this, 'updated_option' ), 10, 3 );
add_action( 'added_option', array( $this, 'added_option' ), 10, 2 );
add_action( 'delete_option', array( $this, 'delete_option' ), 10, 1 );
add_action( 'wp_upgrade', array( $this, 'wp_upgrade' ), 10, 1 );
add_action( 'pre_get_sites', array( $this, 'pre_get_sites' ), 15, 1 );
add_action( 'added_blog_meta', array( $this, 'added_blog_meta' ), 10, 4 );
add_action( 'updated_blog_meta', array( $this, 'updated_blog_meta' ), 10, 4 );
add_action( 'deleted_blog_meta', array( $this, 'deleted_blog_meta' ), 10, 3 );
add_action( 'wpmu_new_blog', array( $this, 'wpmu_new_blog' ), 10, 1 );
}
function pre_get_option( $value, $option ){
$blog_id = get_current_blog_id();
$meta = get_blog_meta( $blog_id, $option, true );
if( false != $meta ){
$value = $meta;
} else{
add_filter( 'option_'.$option, array( $this, 'get_option' ), 1, 2 );
}
return $value;
}
function get_option( $value, $option ){
if( !$this->check_option( $option ) || false == $value ){
return;
}
$blog_id = get_current_blog_id();
update_blog_meta( $blog_id, $option, $value );
}
function updated_option( $option, $old_value, $value ){
if( !$this->check_option( $option ) ){
return;
}
$blog_id = get_current_blog_id();
update_blog_meta( $blog_id, $option, $value, $old_value );
}
function added_option( $option, $value ){
if( !$this->check_option( $option ) ){
return;
}
$blog_id = get_current_blog_id();
add_blog_meta( $blog_id, $option, $value, true );
}
function delete_option( $option ){
if( !$this->check_option( $option ) ){
return;
}
$blog_id = get_current_blog_id();
delete_blog_meta( $blog_id, $option );
}
function wp_upgrade( $wp_db_version ){
$blog_id = get_current_blog_id();
update_blog_meta( $blog_id, 'wp_db_version', $wp_db_version );
}
function pre_get_sites( $site_query ) {
if ( ! isset( $site_query->query_var_defaults['update_blog_meta_cache'] ) ) {
$site_query->query_var_defaults['update_blog_meta_cache'] = true;
}
}
function added_blog_meta( $meta_id, $id, $option, $value ){
if( !$this->check_option( $option ) ){
return;
}
remove_action( 'updated_option', array( $this, 'updated_option' ), 10, 3 );
remove_action( 'added_option', array( $this, 'added_option' ), 10, 2 );
add_blog_option( $id, $option, $value );
add_action( 'updated_option', array( $this, 'updated_option' ), 10, 3 );
add_action( 'added_option', array( $this, 'added_option' ), 10, 2 );
}
function updated_blog_meta( $meta_id, $id, $option, $value ){
if( !$this->check_option( $option ) ){
return;
}
remove_action( 'updated_option', array( $this, 'updated_option' ), 10, 3 );
remove_action( 'added_option', array( $this, 'added_option' ), 10, 2 );
update_blog_option( $id, $option, $value );
add_action( 'updated_option', array( $this, 'updated_option' ), 10, 3 );
add_action( 'added_option', array( $this, 'added_option' ), 10, 2 );
}
function deleted_blog_meta( $meta_id, $id, $option ){
if( !$this->check_option( $option ) ){
return;
}
remove_action( 'delete_option', array( $this, 'delete_option' ), 10, 1 );
delete_blog_option( $id, $option );
add_action( 'delete_option', array( $this, 'delete_option' ), 10, 1 );
}
function wpmu_new_blog( $blog_id ){
switch_to_blog( $blog_id );
foreach( $this->filters as $option ){
get_option( $option );
}
restore_current_blog();
}
protected function check_option( $option ){
return in_array( $option, $this->filters );
}
}
new Blog_Meta_Sync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment