-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-umw-multisite-jetpack-control.php
173 lines (154 loc) · 6.81 KB
/
class-umw-multisite-jetpack-control.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Plugin Name: UMW Multisite JetPack Control
* Description: Allows code-based control over which JetPack modules are available to site admins https://bitbucket.org/umwedu/umw-multisite-jetpack-control/
* Version: 1.2
* Author: cgrymala
* Network: true
* License: GPL2
*/
if ( ! class_exists( 'UMW_Multisite_JetPack_Control' ) ) {
class UMW_Multisite_JetPack_Control {
function __construct() {
add_filter( 'jetpack_get_default_modules', array( $this, 'get_default_modules' ) );
add_filter( 'jetpack_get_available_modules', array( $this, 'get_available_modules' ) );
add_action( 'init', array( $this, 'activate_forced_modules' ) );
// Make sure the "Activate" nag for the Site Management module doesn't appear
add_filter( 'can_display_jetpack_manage_notice', array( $this, '__return_false' ) );
}
/**
* Retrieve the list of blocked modules
* @return array the array of blocked modules
*/
function get_blocked_modules() {
return apply_filters( 'umw-blocked-jetpack-modules', array(
'contact-form', /* No need when we have Gravity Forms installed */
'protect', /* Might be affecting Login for Large Groups */
'publicize', /* Stuck Behind a Paywall Now */
'custom-content-types', /* Currently useless */
'gravatar-hovercards', /* Causes some JS conflicts throughout the site */
'sso', /* Would conflict with our sign-on system */
'manage', /* Would be ridiculous with the number of sites running off of one install */
'minileven', /* We don't want a third-party mobile theme */
'monitor', /* We have other systems in-place to keep an eye on site uptime */
'notes', /* We don't want to receive notifications about every little thing that happens throughout the install */
'photon', /* Until we have a better chance to investigate how this effects us, we need it turned off */
'photon-cdn', /* Hide the accellerator option */
'site-icon', /* We have a single favicon that needs to be used */
'verification-tools', /* We don't want them to verify that someone other than us owns the site */
'videopress', /* We don't have a VideoPress subscription. No need to confuse users */
'vaultpress', /* We don't have a VaultPress subscription. No need to confuse users */
'stats', /* We have a different analytics service */
'comments', /* Seeing errors in the logs, so let's turn this off */
'enhanced-distribution', /* What does this do? */
'likes', /* extraneous */
'subscriptions', /* extraneous */
'sitemaps', /* extraneous */
'seo-tools', /* extraneous */
'wordads', /* paid */
'google-analytics', /* paid */
'masterbar', /* Hide the WordPress.com Toolbar Option */
'jetpack-search', /* Hide the WordPress.com Search setting which prompts an upgrade */
'search', /* Hide the WordPress.com search Option */
'external-media' /* Hide external media */
) );
}
/**
* Retrieve a list of modules that should be forcibly activated on all sites
* @return array the array of forced modules
*/
function get_forced_modules() {
return apply_filters( 'umw-forced-jetpack-modules', array(
'tiled-gallery', /* Very useful, doesn't hurt to have it active */
'carousel', /* Very useful, doesn't hurt to have it active */
'shortcodes', /* Very useful, doesn't hurt to have it active */
'widget-visibility', /* Very useful, doesn't hurt to have it active */
'custom-css', /* Very useful, doesn't hurt to have it active */
'widgets', /* Very useful, doesn't hurt to have it active */
) );
}
/**
* Make sure that none of our blocked modules are activated by default
* @param array $modules an auto-indexed array of the module slugs
* @return array the modified array of default modules
*/
function get_default_modules( $modules=array() ) {
if ( empty( $modules ) || ! is_array( $modules ) )
return $modules;
$modules = array_unique( array_merge( $modules, $this->get_forced_modules() ) );
$good_modules = array();
$bad_modules = $this->get_blocked_modules();
foreach ( $modules as $m ) {
if ( ! in_array( $m, $bad_modules ) ) {
$good_modules[] = $m;
}
}
return $good_modules;
}
/**
* Make sure that none of our blocked modules are available for activation
* @param array $modules an associative array using the module slugs as keys
* @return array the modified array of available modules
*/
function get_available_modules( $modules=array() ) {
if ( empty( $modules ) || ! is_array( $modules ) )
return $modules;
$good_modules = array();
$bad_modules = $this->get_blocked_modules();
foreach( $modules as $k=>$v ) {
if ( ! in_array( $k, $bad_modules ) ) {
$good_modules[$k] = $v;
}
}
return $good_modules;
}
/**
* Forcibly deactivate modules that are blocked
*/
function deactivate_blocked_modules() {
$bad_modules = $this->get_blocked_modules();
if ( ! class_exists( 'Jetpack_Options' ) )
return false;
$active = Jetpack_Options::get_option( 'active_modules', array() );
$good_modules = array_diff( $active, $bad_modules );
/*error_log( '[JetPack Force Deactivate Debug]: The difference between the active & bad modules arrays looks like: ' . "\n" . print_r( $good_modules, true ) );*/
Jetpack_Options::update_option( 'active_modules', $good_modules );
}
/**
* Forcibly activate modules that we need/want active everywhere
* This method also handles forcibly deactivating blocked modules, so
* we don't need to run that separate method unless our list of
* forced modules is empty.
*/
function activate_forced_modules() {
$forced_modules = $this->get_forced_modules();
if ( empty( $forced_modules ) )
return $this->deactivate_blocked_modules();
$bad_modules = $this->get_blocked_modules();
if ( ! class_exists( 'Jetpack_Options' ) )
return false;
$active = Jetpack_Options::get_option( 'active_modules', array() );
/* Add our forced modules to the list of good modules */
$good_modules = array_unique( array_merge( $active, $forced_modules ) );
/* If a moddule is both forced & blocked for some reason, default to blocking it */
$good_modules = array_diff( $good_modules, $bad_modules );
Jetpack_Options::update_option( 'active_modules', $good_modules );
}
/**
* Return boolean false
* Currently, this function is only used in one hook, but I'm separating it out,
* expecting that we'll need to use it more in the future
* Doing this, rather than create_func() or encapsulation makes it compatible with
* more versions of PHP
* @return bool false
*/
function __return_false() {
return false;
}
}
function inst_umw_multisite_jetpack_control_obj() {
global $umw_multisite_jetpack_control_obj;
$umw_multisite_jetpack_control_obj = new UMW_Multisite_JetPack_Control;
}
inst_umw_multisite_jetpack_control_obj();
}