-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-control.php
388 lines (331 loc) · 14.2 KB
/
update-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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/*
* Plugin Name: Update Control
* Plugin URI: http://github.com/chipbennett/update-control/
* Description: Adds a manual toggle to the WordPress Admin Interface for managing auto-updates.
* Author: George Stephanis, Chip Bennett
* Version: 1.5
* Author URI: http://chipbennett.net
*/
/**
* Update Control Class
*/
class Stephanis_Update_Control {
public static function go() {
if ( is_multisite() && ! is_main_site() ) {
// Multisite check
// only run on the main site of a multisite network
return;
} else {
// Let's roll!
add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
add_action( 'init', array( __CLASS__, 'setup_upgrade_filters' ) );
}
}
public static function setup_upgrade_filters() {
$options = self::get_options();
// Do these at priority 1, so other folks can easily override it.
if ( 'no' == $options['active'] ) {
add_filter( 'automatic_updater_disabled', '__return_true', 1 );
} else {
if ( in_array( $options['core'], array( 'dev', 'major', 'minor' ) ) ) {
add_filter( 'allow_' . $options['core'] . '_auto_core_updates', '__return_true', 1 );
}
if ( $options['plugin'] ) {
add_filter( 'auto_update_plugin', '__return_true', 1 );
}
if ( $options['theme'] ) {
add_filter( 'auto_update_theme', '__return_true', 1 );
}
if ( ! $options['translation'] ) {
add_filter( 'auto_update_translation', '__return_false', 1 );
}
if ( $options['vcscheck'] ) {
add_filter( 'automatic_updates_is_vcs_checkout', '__return_false', 1 );
}
if ( 'no' == $options['emailactive'] || ! ( $options['successemail'] || $options['failureemail'] || $options['criticalemail'] ) ) {
add_filter( 'auto_core_update_send_email', '__return_false', 1 );
} else {
add_filter( 'auto_core_update_send_email', array( __CLASS__, 'filter_email' ), 1, 2 );
}
if ( $options['debugemail'] ) {
add_filter( 'automatic_updates_send_debug_email', '__return_false', 1 );
}
}
}
public static function filter_email( $bool, $type ) {
$options = self::get_options();
if ( 'success' == $type && ! $options['successemail'] )
return false;
if ( 'fail' == $type && ! $options['failureemail'] )
return false;
if ( 'critical' == $type && ! $options['criticalemail'] )
return false;
return $bool;
}
public static function get_options() {
$defaults = array(
'active' => 'yes',
'core' => 'minor',
'plugin' => false,
'theme' => false,
'translation' => true,
'toggleadvanced' => 'hide',
'vcscheck' => false,
'emailactive' => 'yes',
'successemail' => true,
'failureemail' => true,
'criticalemail' => true,
'debugemail' => false,
);
$args = get_option( 'update_control_options', array() );
return wp_parse_args( $args, $defaults );
}
public static function get_option( $key ) {
$options = self::get_options();
if ( isset( $options[ $key ] ) ) {
return $options[ $key ];
}
return null;
}
public static function register_settings() {
add_settings_section(
'update-control',
esc_html__( 'Automatic Updates', 'update-control' ),
array( __CLASS__, 'update_control_settings_section' ),
'general'
);
if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) {
return;
}
add_settings_field(
'update_control_active',
sprintf( '<label for="update_control_active">%1$s</label>', __( 'Automatic Updates Enabled?', 'update-control' ) ),
array( __CLASS__, 'update_control_active_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_core',
sprintf( '<label for="update_control_core">%1$s</label>', __( 'Automatic Core Update Level?', 'update-control' ) ),
array( __CLASS__, 'update_control_core_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_plugin',
sprintf( '<label for="update_control_plugin">%1$s</label>', __( 'Permit Automatic Plugin Updates?', 'update-control' ) ),
array( __CLASS__, 'update_control_plugin_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_theme',
sprintf( '<label for="update_control_theme">%1$s</label>', __( 'Permit Automatic Theme Updates?', 'update-control' ) ),
array( __CLASS__, 'update_control_theme_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_translation',
sprintf( '<label for="update_control_translation">%1$s</label>', __( 'Permit Automatic Translation Updates?', 'update-control' ) ),
array( __CLASS__, 'update_control_translation_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_toggleadvanced',
sprintf( '<label for="update_control_toggleadvanced">%1$s</label>', __( 'Advanced Settings', 'update-control' ) ),
array( __CLASS__, 'update_control_toggleadvanced_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_vcscheck',
sprintf( '<label for="update_control_vcscheck">%1$s</label>', __( 'Enable updates for VCS installations?', 'update-control' ) ),
array( __CLASS__, 'update_control_vcscheck_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_email_active',
sprintf( '<label for="update_control_email_active">%1$s</label>', __( 'Update Emails Enabled?', 'update-control' ) ),
array( __CLASS__, 'update_control_email_active_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_email_success',
sprintf( '<label for="update_control_email_success">%1$s</label>', __( 'Send Emails for Successful Updates?', 'update-control' ) ),
array( __CLASS__, 'update_control_email_success_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_email_failure',
sprintf( '<label for="update_control_email_failure">%1$s</label>', __( 'Send Emails for Failed Updates?', 'update-control' ) ),
array( __CLASS__, 'update_control_email_failure_cb' ),
'general',
'update-control'
);
add_settings_field(
'update_control_email_critical',
sprintf( '<label for="update_control_email_critical">%1$s</label>', __( 'Send Emails for Critically Failed Updates?', 'update-control' ) ),
array( __CLASS__, 'update_control_email_critical_cb' ),
'general',
'update-control'
);
global $wp_version;
if ( false !== strpos( $wp_version, '-' ) ) {
add_settings_field(
'update_control_email_debug',
sprintf( '<label for="update_control_email_debug">%1$s</label>', __( 'Disable Debug Emails for Development Versions?', 'update-control' ) ),
array( __CLASS__, 'update_control_email_debug_cb' ),
'general',
'update-control'
);
}
register_setting( 'general', 'update_control_options', array( __CLASS__, 'sanitize_options' ) );
}
public static function update_control_settings_section() {
if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) : ?>
<p id="update-control-settings-section">
<?php _e( 'You have the <code>AUTOMATIC_UPDATER_DISABLED</code> constant set. Automatic updates are disabled.', 'update-control' ); ?>
</p>
<?php else : ?>
<p id="update-control-settings-section">
<?php _e( 'This section lets you specify what areas of your WordPress install will be permitted to auto-update.', 'update-control' ); ?>
</p>
<?php
$update_core_obj = get_site_transient( 'update_core' );
$last_checked = $update_core_obj->last_checked;
?>
<script>
jQuery(document).ready(function($){
$('#update_control_active').change(function(){
if ( 'yes' != $(this).val() ) {
$('.update_control_dependency').attr( 'readonly', 'readonly' );
$('#update_control_toggleadvanced').val('no');
$('.update_control_advanced' ).parent().parent().css( 'display', 'none' );
} else {
$('.update_control_dependency' ).removeAttr( 'readonly' );
}
}).trigger('change');
$('#update_control_toggleadvanced').change(function(){
if ( 'hide' != $(this).val() ) {
$('.update_control_advanced').parent().parent().css( { 'display' : 'table-row' } );
$('.update_control_advanced').parent().siblings( 'th' ).css( { 'display' : 'block', 'padding-left' : '20px' } );
} else {
$('.update_control_advanced' ).parent().parent().css( 'display', 'none' );
}
}).trigger('change');
$('#update_control_email_active').change(function(){
if ( 'yes' != $(this).val() ) {
$('.update_control_email_dependency.update_control_advanced').attr( 'readonly', 'readonly' );
$('.update_control_email_dependency.update_control_advanced').parent().siblings( 'th' ).children().css( { 'padding-left' : '20px', 'display' : 'block' } );
} else {
$('.update_control_email_dependency.update_control_advanced' ).removeAttr( 'readonly' );
$('.update_control_email_dependency.update_control_advanced').parent().siblings( 'th' ).children().css( { 'padding-left' : '20px', 'display' : 'block' } );
}
}).trigger('change');
});
</script>
<style>
.update_control_dependency[readonly],
.update_control_email_dependency[readonly] {
opacity: 0.4;
}
</style>
<?php endif;
}
public static function update_control_active_cb() {
?>
<select id="update_control_active" name="update_control_options[active]">
<option <?php selected( 'yes' == self::get_option( 'active' ) ); ?> value="yes"><?php _e( 'Yes', 'update-control' ); ?></option>
<option <?php selected( 'no' == self::get_option( 'active' ) ); ?> value="no"><?php _e( 'No', 'update-control' ); ?></option>
</select>
<?php
}
public static function update_control_core_cb() {
?>
<select class="update_control_dependency" id="update_control_core" name="update_control_options[core]">
<option <?php selected( 'minor' == self::get_option( 'core' ) ); ?> value="minor"><?php _e( 'Minor Updates', 'update-control' ); ?></option>
<option <?php selected( 'major' == self::get_option( 'core' ) ); ?> value="major"><?php _e( 'Major Updates', 'update-control' ); ?></option>
<option <?php selected( 'dev' == self::get_option( 'core' ) ); ?> value="dev"><?php _e( 'Development Updates', 'update-control' ); ?></option>
</select>
<?php
}
public static function update_control_plugin_cb() {
?>
<input type="checkbox" class="update_control_dependency" id="update_control_plugin" name="update_control_options[plugin]" <?php checked( self::get_option( 'plugin' ) ); ?> />
<?php
}
public static function update_control_theme_cb() {
?>
<input type="checkbox" class="update_control_dependency" id="update_control_theme" name="update_control_options[theme]" <?php checked( self::get_option( 'theme' ) ); ?> />
<?php
}
public static function update_control_translation_cb() {
?>
<input type="checkbox" class="update_control_dependency" id="update_control_translation" name="update_control_options[translation]" <?php checked( self::get_option( 'translation' ) ); ?> />
<?php
}
public static function update_control_toggleadvanced_cb() {
?>
<select class="update_control_dependency" id="update_control_toggleadvanced" name="update_control_options[toggleadvanced]">
<option <?php selected( 'show' == self::get_option( 'toggleadvanced' ) ); ?> value="show"><?php _e( 'Show', 'update-control' ); ?></option>
<option <?php selected( 'hide' == self::get_option( 'toggleadvanced' ) ); ?> value="hide"><?php _e( 'Hide', 'update-control' ); ?></option>
</select>
<?php
}
public static function update_control_vcscheck_cb() {
?>
<input type="checkbox" class="update_control_advanced" id="update_control_vcscheck" name="update_control_options[vcscheck]" <?php checked( self::get_option( 'vcscheck' ) ); ?> />
<?php
}
public static function update_control_email_active_cb() {
?>
<select class="update_control_advanced" id="update_control_email_active" name="update_control_options[emailactive]">
<option <?php selected( 'yes' == self::get_option( 'emailactive' ) ); ?> value="yes"><?php _e( 'Yes', 'update-control' ); ?></option>
<option <?php selected( 'no' == self::get_option( 'emailactive' ) ); ?> value="no"><?php _e( 'No', 'update-control' ); ?></option>
</select>
<?php
}
public static function update_control_email_success_cb() {
?>
<input type="checkbox" class="update_control_email_dependency update_control_advanced" id="update_control_email_success" name="update_control_options[successemail]" <?php checked( self::get_option( 'successemail' ) ); ?> />
<?php
}
public static function update_control_email_failure_cb() {
?>
<input type="checkbox" class="update_control_email_dependency update_control_advanced" id="update_control_email_failure" name="update_control_options[failureemail]" <?php checked( self::get_option( 'failureemail' ) ); ?> />
<?php
}
public static function update_control_email_critical_cb() {
?>
<input type="checkbox" class="update_control_email_dependency update_control_advanced" id="update_control_email_critical" name="update_control_options[criticalemail]" <?php checked( self::get_option( 'criticalemail' ) ); ?> />
<?php
}
public static function update_control_email_debug_cb() {
?>
<input type="checkbox" class="update_control_advanced" id="update_control_email_debug" name="update_control_options[debugemail]" <?php checked( self::get_option( 'debugemail' ) ); ?> />
<?php
}
public static function sanitize_options( $options ) {
$options = (array) $options;
$options['active'] = ( in_array( $options['active'], array( 'yes', 'no' ) ) ? $options['active'] : 'yes' );
$options['core'] = ( in_array( $options['core'], array( 'minor', 'major', 'dev' ) ) ? $options['core'] : 'minor' );
$options['plugin'] = ! empty( $options['plugin'] );
$options['theme'] = ! empty( $options['theme'] );
$options['translation'] = ! empty( $options['translation'] );
$options['toggleadvanced'] = 'hide';
$options['vcscheck'] = ! empty( $options['vcscheck'] );
$options['emailactive'] = ( in_array( $options['emailactive'], array( 'yes', 'no' ) ) ? $options['emailactive'] : 'yes' );
$options['successemail'] = ! empty( $options['successemail'] );
$options['failureemail'] = ! empty( $options['failureemail'] );
$options['criticalemail'] = ! empty( $options['criticalemail'] );
$options['debugemail'] = ! empty( $options['debugemail'] );
return $options;
}
}
add_action( 'init', array( 'Stephanis_Update_Control', 'go' ), 0 );