Shoora
4/22/2019 - 9:36 AM

Auto-configure Autoptimize across a WordPress network

Auto-configure Autoptimize across a WordPress network

<?php
/**
 * Plugin Name: RT Autoautoptimize
 * Plugin URI: http://www.this-play.nl
 * Description: Automatically configures default settings for the Autoptimize plugin across a WordPress network
 * Version: 0.9
 * Author: Roy Tanck
 * Author URI: http://www.this-play.nl
 * License: GPL2
 */

/**
 * Auto-configure Autoptimize if not already configured
 */
if( !function_exists( 'rt_autoautoptimize' ) ){

	function rt_autoautoptimize(){

		// do nothing if we're in the admin
		if( is_admin() ){
			return;
		}

		// check if the plugin is already configured
		$installed = get_option( 'autoptimize_configured', 'no' );

		if( $installed == 'no' ){

			// default Autoptimize settings for plugin version 1.9.4
			$defaults = array(
				'autoptimize_version' 			=> '1.9.3',
				'autoptimize_html'			=> 'on',
				'autoptimize_html_keepcomments'		=> '',
				'autoptimize_js' 			=> 'on',
				'autoptimize_js_exclude' 		=> 's_sid,smowtion_size,sc_project,WAU_,wau_add,comment-form-quicktags,edToolbar,ch_client,seal.js',
				'autoptimize_js_trycatch' 		=> '',
				'autoptimize_js_justhead' 		=> '',
				'autoptimize_js_forcehead' 		=> '',
				'autoptimize_css' 			=> 'on',
				'autoptimize_css_exclude' 		=> 'admin-bar.min.css, dashicons.min.css',
				'autoptimize_css_justhead' 		=> '',
				'autoptimize_css_datauris' 		=> '',
				'autoptimize_css_defer' 		=> '',
				'autoptimize_css_defer_inline'		=> '',
				'autoptimize_css_inline' 		=> '',
				'autoptimize_cdn_url' 			=> '',
				'autoptimize_cache_clean' 		=> '0',
				'autoptimize_cache_nogzip' 		=> 'on',
				'autoptimize_show_adv' 			=> '0',
				'autoptimize_configured'		=> 'yes'
			);

			// loop through the defaults array, and update each option with the corresponding value
			foreach( $defaults as $key=>$value ){
				update_option( $key, $value );
			}

			// log this to the error log
			error_log('autoautoptimize done');

		}

	}

	// hook into init
	add_action( 'init', 'rt_autoautoptimize' );

}


/**
 * Apply the default configuration to newly created sites
 */
if( !function_exists('rt_autoautoptimize_newblog') ){
	function rt_autoautoptimize_newblog( $blog_id, $user_id, $domain, $path, $site_id, $meta ){
		if( !empty( $blog_id ) && function_exists('rt_autoautoptimize') ){
			// apply the auto-configuration
			rt_autoautoptimize();
		}
	}
	add_action( 'wpmu_new_blog', 'rt_autoautoptimize_newblog', 10, 6 );
}

?>