PluginProbe
Update Control / 1.4
Update Control v1.4
trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.3 1.3.1 1.3.2 1.4 1.5 1.5.1
update-control / update-control.php

update-control.php in Update Control 1.4, at update-control.php

385 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: Update Control
4 * Plugin URI: http://github.com/chipbennett/update-control/
5 * Description: Adds a manual toggle to the WordPress Admin Interface for managing auto-updates.
6 * Author: George Stephanis, Chip Bennett
7 * Version: 1.4
8 * Author URI: http://chipbennett.net
9 */
10
11 /**
12 * Update Control Class
13 */
14 class Stephanis_Update_Control {
15
16 public static function go() {
17 if ( is_multisite() && ! is_main_site() ) {
18 // Multisite check
19 // only run on the main site of a multisite network
20 return;
21 } else {
22 // Let's roll!
23 add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
24 add_action( 'init', array( __CLASS__, 'setup_upgrade_filters' ) );
25 }
26 }
27
28 public static function setup_upgrade_filters() {
29 $options = self::get_options();
30
31 // Do these at priority 1, so other folks can easily override it.
32
33 if ( 'no' == $options['active'] ) {
34
35 add_filter( 'automatic_updater_disabled', '__return_true', 1 );
36
37 } else {
38
39 if ( in_array( $options['core'], array( 'dev', 'major', 'minor' ) ) ) {
40 add_filter( 'allow_' . $options['core'] . '_auto_core_updates', '__return_true', 1 );
41 }
42
43 if ( $options['plugin'] ) {
44 add_filter( 'auto_update_plugin', '__return_true', 1 );
45 }
46
47 if ( $options['theme'] ) {
48 add_filter( 'auto_update_theme', '__return_true', 1 );
49 }
50
51 if ( ! $options['translation'] ) {
52 add_filter( 'auto_update_translation', '__return_false', 1 );
53 }
54
55 if ( $options['vcscheck'] ) {
56 add_filter( 'automatic_updates_is_vcs_checkout', '__return_false', 1 );
57 }
58
59 if ( 'no' == $options['emailactive'] || ! ( $options['successemail'] || $options['failureemail'] || $options['criticalemail'] ) ) {
60 add_filter( 'auto_core_update_send_email', '__return_false', 1 );
61 } else {
62 add_filter( 'auto_core_update_send_email', array( __CLASS__, 'filter_email' ), 1, 2 );
63 }
64
65 if ( $options['debugemail'] ) {
66 add_filter( 'automatic_updates_send_debug_email ', '__return_true', 1 );
67 }
68
69 }
70
71 }
72
73 public static function filter_email( $bool, $type ) {
74 $options = self::get_options();
75
76 if ( 'success' == $type && ! $options['successemail'] )
77 return false;
78
79 if ( 'fail' == $type && ! $options['failureemail'] )
80 return false;
81
82 if ( 'critical' == $type && ! $options['criticalemail'] )
83 return false;
84
85 return $bool;
86 }
87
88 public static function get_options() {
89 $defaults = array(
90 'active' => 'yes',
91 'core' => 'minor',
92 'plugin' => false,
93 'theme' => false,
94 'translation' => true,
95 'toggleadvanced' => 'hide',
96 'vcscheck' => false,
97 'emailactive' => 'yes',
98 'successemail' => true,
99 'failureemail' => true,
100 'criticalemail' => true,
101 'debugemail' => false,
102 );
103 $args = get_option( 'update_control_options', array() );
104 return wp_parse_args( $args, $defaults );
105 }
106
107 public static function get_option( $key ) {
108 $options = self::get_options();
109 if ( isset( $options[ $key ] ) ) {
110 return $options[ $key ];
111 }
112 return null;
113 }
114
115 public static function register_settings() {
116 add_settings_section(
117 'update-control',
118 esc_html__( 'Automatic Updates', 'update-control' ),
119 array( __CLASS__, 'update_control_settings_section' ),
120 'general'
121 );
122
123 if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) {
124 return;
125 }
126
127 add_settings_field(
128 'update_control_active',
129 sprintf( '<label for="update_control_active">%1$s</label>', __( 'Automatic Updates Enabled?', 'update-control' ) ),
130 array( __CLASS__, 'update_control_active_cb' ),
131 'general',
132 'update-control'
133 );
134
135 add_settings_field(
136 'update_control_core',
137 sprintf( '<label for="update_control_core">%1$s</label>', __( 'Automatic Core Update Level?', 'update-control' ) ),
138 array( __CLASS__, 'update_control_core_cb' ),
139 'general',
140 'update-control'
141 );
142
143 add_settings_field(
144 'update_control_plugin',
145 sprintf( '<label for="update_control_plugin">%1$s</label>', __( 'Permit Automatic Plugin Updates?', 'update-control' ) ),
146 array( __CLASS__, 'update_control_plugin_cb' ),
147 'general',
148 'update-control'
149 );
150
151 add_settings_field(
152 'update_control_theme',
153 sprintf( '<label for="update_control_theme">%1$s</label>', __( 'Permit Automatic Theme Updates?', 'update-control' ) ),
154 array( __CLASS__, 'update_control_theme_cb' ),
155 'general',
156 'update-control'
157 );
158
159 add_settings_field(
160 'update_control_translation',
161 sprintf( '<label for="update_control_translation">%1$s</label>', __( 'Permit Automatic Translation Updates?', 'update-control' ) ),
162 array( __CLASS__, 'update_control_translation_cb' ),
163 'general',
164 'update-control'
165 );
166
167 add_settings_field(
168 'update_control_toggleadvanced',
169 sprintf( '<label for="update_control_toggleadvanced">%1$s</label>', __( 'Advanced Settings', 'update-control' ) ),
170 array( __CLASS__, 'update_control_toggleadvanced_cb' ),
171 'general',
172 'update-control'
173 );
174
175 add_settings_field(
176 'update_control_vcscheck',
177 sprintf( '<label for="update_control_vcscheck">%1$s</label>', __( 'Enable updates for VCS installations?', 'update-control' ) ),
178 array( __CLASS__, 'update_control_vcscheck_cb' ),
179 'general',
180 'update-control'
181 );
182
183 add_settings_field(
184 'update_control_email_active',
185 sprintf( '<label for="update_control_email_active">%1$s</label>', __( 'Update Emails Enabled?', 'update-control' ) ),
186 array( __CLASS__, 'update_control_email_active_cb' ),
187 'general',
188 'update-control'
189 );
190
191 add_settings_field(
192 'update_control_email_success',
193 sprintf( '<label for="update_control_email_success">%1$s</label>', __( 'Send Emails for Successful Updates?', 'update-control' ) ),
194 array( __CLASS__, 'update_control_email_success_cb' ),
195 'general',
196 'update-control'
197 );
198
199 add_settings_field(
200 'update_control_email_failure',
201 sprintf( '<label for="update_control_email_failure">%1$s</label>', __( 'Send Emails for Failed Updates?', 'update-control' ) ),
202 array( __CLASS__, 'update_control_email_failure_cb' ),
203 'general',
204 'update-control'
205 );
206
207 add_settings_field(
208 'update_control_email_critical',
209 sprintf( '<label for="update_control_email_critical">%1$s</label>', __( 'Send Emails for Critically Failed Updates?', 'update-control' ) ),
210 array( __CLASS__, 'update_control_email_critical_cb' ),
211 'general',
212 'update-control'
213 );
214
215 add_settings_field(
216 'update_control_email_debug',
217 sprintf( '<label for="update_control_email_debug">%1$s</label>', __( 'Send Update Debug Emails?', 'update-control' ) ),
218 array( __CLASS__, 'update_control_email_debug_cb' ),
219 'general',
220 'update-control'
221 );
222
223 register_setting( 'general', 'update_control_options', array( __CLASS__, 'sanitize_options' ) );
224 }
225
226 public static function update_control_settings_section() {
227 if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) : ?>
228 <p id="update-control-settings-section">
229 <?php _e( 'You have the <code>AUTOMATIC_UPDATER_DISABLED</code> constant set. Automatic updates are disabled.', 'update-control' ); ?>
230 </p>
231 <?php else : ?>
232 <p id="update-control-settings-section">
233 <?php _e( 'This section lets you specify what areas of your WordPress install will be permitted to auto-update.', 'update-control' ); ?>
234 </p>
235 <?php
236 $update_core_obj = get_site_transient( 'update_core' );
237 $last_checked = $update_core_obj->last_checked;
238 ?>
239 <script>
240 jQuery(document).ready(function($){
241 $('#update_control_active').change(function(){
242 if ( 'yes' != $(this).val() ) {
243 $('.update_control_dependency').attr( 'readonly', 'readonly' );
244 $('#update_control_toggleadvanced').val('no');
245 $('.update_control_advanced' ).parent().parent().css( 'display', 'none' );
246 } else {
247 $('.update_control_dependency' ).removeAttr( 'readonly' );
248 }
249 }).trigger('change');
250
251 $('#update_control_toggleadvanced').change(function(){
252 if ( 'hide' != $(this).val() ) {
253 $('.update_control_advanced').parent().parent().css( { 'display' : 'table-row' } );
254 $('.update_control_advanced').parent().siblings( 'th' ).css( { 'display' : 'block', 'padding-left' : '20px' } );
255 } else {
256 $('.update_control_advanced' ).parent().parent().css( 'display', 'none' );
257 }
258 }).trigger('change');
259
260 $('#update_control_email_active').change(function(){
261 if ( 'yes' != $(this).val() ) {
262 $('.update_control_email_dependency.update_control_advanced').attr( 'readonly', 'readonly' );
263 $('.update_control_email_dependency.update_control_advanced').parent().siblings( 'th' ).children().css( { 'padding-left' : '20px', 'display' : 'block' } );
264 } else {
265 $('.update_control_email_dependency.update_control_advanced' ).removeAttr( 'readonly' );
266 $('.update_control_email_dependency.update_control_advanced').parent().siblings( 'th' ).children().css( { 'padding-left' : '20px', 'display' : 'block' } );
267 }
268 }).trigger('change');
269 });
270 </script>
271 <style>
272 .update_control_dependency[readonly],
273 .update_control_email_dependency[readonly] {
274 opacity: 0.4;
275 }
276 </style>
277 <?php endif;
278 }
279
280 public static function update_control_active_cb() {
281 ?>
282 <select id="update_control_active" name="update_control_options[active]">
283 <option <?php selected( 'yes' == self::get_option( 'active' ) ); ?> value="yes"><?php _e( 'Yes', 'update-control' ); ?></option>
284 <option <?php selected( 'no' == self::get_option( 'active' ) ); ?> value="no"><?php _e( 'No', 'update-control' ); ?></option>
285 </select>
286 <?php
287 }
288
289 public static function update_control_core_cb() {
290 ?>
291 <select class="update_control_dependency" id="update_control_core" name="update_control_options[core]">
292 <option <?php selected( 'minor' == self::get_option( 'core' ) ); ?> value="minor"><?php _e( 'Minor Updates', 'update-control' ); ?></option>
293 <option <?php selected( 'major' == self::get_option( 'core' ) ); ?> value="major"><?php _e( 'Major Updates', 'update-control' ); ?></option>
294 <option <?php selected( 'dev' == self::get_option( 'core' ) ); ?> value="dev"><?php _e( 'Development Updates', 'update-control' ); ?></option>
295 </select>
296 <?php
297 }
298
299 public static function update_control_plugin_cb() {
300 ?>
301 <input type="checkbox" class="update_control_dependency" id="update_control_plugin" name="update_control_options[plugin]" <?php checked( self::get_option( 'plugin' ) ); ?> />
302 <?php
303 }
304
305 public static function update_control_theme_cb() {
306 ?>
307 <input type="checkbox" class="update_control_dependency" id="update_control_theme" name="update_control_options[theme]" <?php checked( self::get_option( 'theme' ) ); ?> />
308 <?php
309 }
310
311 public static function update_control_translation_cb() {
312 ?>
313 <input type="checkbox" class="update_control_dependency" id="update_control_translation" name="update_control_options[translation]" <?php checked( self::get_option( 'translation' ) ); ?> />
314 <?php
315 }
316
317 public static function update_control_toggleadvanced_cb() {
318 ?>
319 <select class="update_control_dependency" id="update_control_toggleadvanced" name="update_control_options[toggleadvanced]">
320 <option <?php selected( 'show' == self::get_option( 'toggleadvanced' ) ); ?> value="show"><?php _e( 'Show', 'update-control' ); ?></option>
321 <option <?php selected( 'hide' == self::get_option( 'toggleadvanced' ) ); ?> value="hide"><?php _e( 'Hide', 'update-control' ); ?></option>
322 </select>
323 <?php
324 }
325
326 public static function update_control_vcscheck_cb() {
327 ?>
328 <input type="checkbox" class="update_control_advanced" id="update_control_vcscheck" name="update_control_options[vcscheck]" <?php checked( self::get_option( 'vcscheck' ) ); ?> />
329 <?php
330 }
331
332 public static function update_control_email_active_cb() {
333 ?>
334 <select class="update_control_advanced" id="update_control_email_active" name="update_control_options[emailactive]">
335 <option <?php selected( 'yes' == self::get_option( 'emailactive' ) ); ?> value="yes"><?php _e( 'Yes', 'update-control' ); ?></option>
336 <option <?php selected( 'no' == self::get_option( 'emailactive' ) ); ?> value="no"><?php _e( 'No', 'update-control' ); ?></option>
337 </select>
338 <?php
339 }
340
341 public static function update_control_email_success_cb() {
342 ?>
343 <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' ) ); ?> />
344 <?php
345 }
346
347 public static function update_control_email_failure_cb() {
348 ?>
349 <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' ) ); ?> />
350 <?php
351 }
352
353 public static function update_control_email_critical_cb() {
354 ?>
355 <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' ) ); ?> />
356 <?php
357 }
358
359 public static function update_control_email_debug_cb() {
360 ?>
361 <input type="checkbox" class="update_control_advanced" id="update_control_email_debug" name="update_control_options[debugemail]" <?php checked( self::get_option( 'debugemail' ) ); ?> />
362 <?php
363 }
364
365 public static function sanitize_options( $options ) {
366 $options = (array) $options;
367
368 $options['active'] = ( in_array( $options['active'], array( 'yes', 'no' ) ) ? $options['active'] : 'yes' );
369 $options['core'] = ( in_array( $options['core'], array( 'minor', 'major', 'dev' ) ) ? $options['core'] : 'minor' );
370 $options['plugin'] = ! empty( $options['plugin'] );
371 $options['theme'] = ! empty( $options['theme'] );
372 $options['translation'] = ! empty( $options['translation'] );
373 $options['toggleadvanced'] = 'hide';
374 $options['vcscheck'] = ! empty( $options['vcscheck'] );
375 $options['emailactive'] = ( in_array( $options['emailactive'], array( 'yes', 'no' ) ) ? $options['emailactive'] : 'yes' );
376 $options['successemail'] = ! empty( $options['successemail'] );
377 $options['failureemail'] = ! empty( $options['failureemail'] );
378 $options['criticalemail'] = ! empty( $options['criticalemail'] );
379 $options['debugemail'] = ! empty( $options['debugemail'] );
380
381 return $options;
382 }
383
384 }
385 add_action( 'init', array( 'Stephanis_Update_Control', 'go' ), 0 );