forked from pcfreak30/multisite-wp-rocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultisite-wp-rocket.php
106 lines (93 loc) · 3.45 KB
/
multisite-wp-rocket.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/*
* Plugin Name: Multisite Support for WP Rocket
* Plugin URI: https://github.com/pcfreak30/multisite-wp-rocket
* Description: Plugin to enable WP-Rocket to be managed in multisite
* Version: 0.1.1
* Author: Derrick Hammer
* Author URI: https://www.derrickhammer.com
* License: GPL3
* Network: true
*/
/**
* Deactivate and show error if WP-Rocket is missing
*
* @since 0.1.0
*
*/
function multisite_wp_rocket_plugins_loaded() {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$error = false;
if ( validate_plugin( 'wp-rocket/wp-rocket.php' ) || ! is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) {
$error = true;
add_action( 'network_admin_notices', 'multisite_wp_rocket_activate_error_no_wprocket' );
}
if ( $error ) {
deactivate_plugins( basename( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . basename( __FILE__ ) );
} else {
add_action( 'pre_update_option_' . WP_ROCKET_SLUG, 'multisite_wp_rocket_update_option', 10, 3 );
add_action( 'pre_option_' . WP_ROCKET_SLUG, 'multisite_wp_rocket_get_option', 10, 2 );
}
}
/**
* Error function if WP Rocket is missing
*
* @since 0.1.0
*
*/
function multisite_wp_rocket_activate_error_no_wprocket() {
$info = get_plugin_data( __FILE__ );
_e( sprintf( '
<div class="error notice">
<p>Opps! %s requires WP Rocket! Please Download at <a href="http://www.wp-rocket.me">www.wp-rocket.me</a></p>
</div>', $info['Name'] ) );
}
function multisite_wp_rocket_admin_menu() {
global $pagenow;
if ( function_exists( 'get_rocket_option' ) ) {
$wl_plugin_name = get_rocket_option( 'wl_plugin_name', WP_ROCKET_PLUGIN_NAME );
$wl_plugin_slug = sanitize_key( $wl_plugin_name );
if ( 'options-general.php' == $pagenow && ! empty( $_GET['page'] ) && $wl_plugin_slug == $_GET['page'] ) {
wp_redirect( add_query_arg( 'page', $wl_plugin_slug, network_admin_url( 'settings.php' ) ) );
exit();
}
add_submenu_page( 'settings.php', $wl_plugin_name, $wl_plugin_name, 'manage_network_options', $wl_plugin_slug, 'multisite_wp_rocket_display_options' );
}
}
function multisite_wp_rocket_display_options() {
require ABSPATH . 'wp-admin/options-head.php';
ob_start();
rocket_display_options();
$output = ob_get_clean();
echo str_replace( 'options.php', '../options.php', $output );
}
function multisite_wp_rocket_update_option( $value, $old_value, $option ) {
update_site_option( $option, $value );
return $old_value;
}
function multisite_wp_rocket_get_option( $value, $option ) {
return get_site_option( $option );
}
function multisite_wp_rocket_after_save_options() {
foreach ( get_sites( array( 'fields' => 'ids', 'site__not_in' => array( BLOG_ID_CURRENT_SITE ) ) ) as $blog_id ) {
switch_to_blog( $blog_id );
rocket_generate_config_file();
restore_current_blog();
}
}
function multisite_wp_rocket_purge_cache() {
$lang = isset( $_GET['lang'] ) && $_GET['lang'] != 'all' ? sanitize_key( $_GET['lang'] ) : '';
foreach ( get_sites( array( 'fields' => 'ids' ) ) as $blog_id ) {
switch_to_blog( $blog_id );
rocket_clean_domain( $lang );
rocket_clean_minify();
restore_current_blog();
}
}
if ( is_subdomain_install() ) {
add_action( 'update_option_' . WP_ROCKET_SLUG, 'multisite_wp_rocket_after_save_options', 10 );
}
add_action( 'network_admin_menu', 'multisite_wp_rocket_admin_menu', 9 );
add_action( 'admin_menu', 'multisite_wp_rocket_admin_menu', 9 );
add_action( 'plugins_loaded', 'multisite_wp_rocket_plugins_loaded', 11 );
add_action( 'admin_post_purge_cache', 'multisite_wp_rocket_purge_cache', 9 );