PluginProbe
Update Control / 1.5
Update Control v1.5
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.5, at update-control.php

388 lines 14.6 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.5
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_false', 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 global $wp_version;
216 if ( false !== strpos( $wp_version, '-' ) ) {
217 add_settings_field(
218 'update_control_email_debug',
219 sprintf( '<label for="update_control_email_debug">%1$s</label>', __( 'Disable Debug Emails for Development Versions?', 'update-control' ) ),
220 array( __CLASS__, 'update_control_email_debug_cb' ),
221 'general',
222 'update-control'
223 );
224 }
225
226 register_setting( 'general', 'update_control_options', array( __CLASS__, 'sanitize_options' ) );
227 }
228
229 public static function update_control_settings_section() {
230 if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) : ?>
231 <p id="update-control-settings-section">
232 <?php _e( 'You have the <code>AUTOMATIC_UPDATER_DISABLED</code> constant set. Automatic updates are disabled.', 'update-control' ); ?>
233 </p>
234 <?php else : ?>
235 <p id="update-control-settings-section">
236 <?php _e( 'This section lets you specify what areas of your WordPress install will be permitted to auto-update.', 'update-control' ); ?>
237 </p>
238 <?php
239 $update_core_obj = get_site_transient( 'update_core' );
240 $last_checked = $update_core_obj->last_checked;
241 ?>
242 <script>
243 jQuery(document).ready(function($){
244 $('#update_control_active').change(function(){
245 if ( 'yes' != $(this).val() ) {
246 $('.update_control_dependency').attr( 'readonly', 'readonly' );
247 $('#update_control_toggleadvanced').val('no');
248 $('.update_control_advanced' ).parent().parent().css( 'display', 'none' );
249 } else {
250 $('.update_control_dependency' ).removeAttr( 'readonly' );
251 }
252 }).trigger('change');
253
254 $('#update_control_toggleadvanced').change(function(){
255 if ( 'hide' != $(this).val() ) {
256 $('.update_control_advanced').parent().parent().css( { 'display' : 'table-row' } );
257 $('.update_control_advanced').parent().siblings( 'th' ).css( { 'display' : 'block', 'padding-left' : '20px' } );
258 } else {
259 $('.update_control_advanced' ).parent().parent().css( 'display', 'none' );
260 }
261 }).trigger('change');
262
263 $('#update_control_email_active').change(function(){
264 if ( 'yes' != $(this).val() ) {
265 $('.update_control_email_dependency.update_control_advanced').attr( 'readonly', 'readonly' );
266 $('.update_control_email_dependency.update_control_advanced').parent().siblings( 'th' ).children().css( { 'padding-left' : '20px', 'display' : 'block' } );
267 } else {
268 $('.update_control_email_dependency.update_control_advanced' ).removeAttr( 'readonly' );
269 $('.update_control_email_dependency.update_control_advanced').parent().siblings( 'th' ).children().css( { 'padding-left' : '20px', 'display' : 'block' } );
270 }
271 }).trigger('change');
272 });
273 </script>
274 <style>
275 .update_control_dependency[readonly],
276 .update_control_email_dependency[readonly] {
277 opacity: 0.4;
278 }
279 </style>
280 <?php endif;
281 }
282
283 public static function update_control_active_cb() {
284 ?>
285 <select id="update_control_active" name="update_control_options[active]">
286 <option <?php selected( 'yes' == self::get_option( 'active' ) ); ?> value="yes"><?php _e( 'Yes', 'update-control' ); ?></option>
287 <option <?php selected( 'no' == self::get_option( 'active' ) ); ?> value="no"><?php _e( 'No', 'update-control' ); ?></option>
288 </select>
289 <?php
290 }
291
292 public static function update_control_core_cb() {
293 ?>
294 <select class="update_control_dependency" id="update_control_core" name="update_control_options[core]">
295 <option <?php selected( 'minor' == self::get_option( 'core' ) ); ?> value="minor"><?php _e( 'Minor Updates', 'update-control' ); ?></option>
296 <option <?php selected( 'major' == self::get_option( 'core' ) ); ?> value="major"><?php _e( 'Major Updates', 'update-control' ); ?></option>
297 <option <?php selected( 'dev' == self::get_option( 'core' ) ); ?> value="dev"><?php _e( 'Development Updates', 'update-control' ); ?></option>
298 </select>
299 <?php
300 }
301
302 public static function update_control_plugin_cb() {
303 ?>
304 <input type="checkbox" class="update_control_dependency" id="update_control_plugin" name="update_control_options[plugin]" <?php checked( self::get_option( 'plugin' ) ); ?> />
305 <?php
306 }
307
308 public static function update_control_theme_cb() {
309 ?>
310 <input type="checkbox" class="update_control_dependency" id="update_control_theme" name="update_control_options[theme]" <?php checked( self::get_option( 'theme' ) ); ?> />
311 <?php
312 }
313
314 public static function update_control_translation_cb() {
315 ?>
316 <input type="checkbox" class="update_control_dependency" id="update_control_translation" name="update_control_options[translation]" <?php checked( self::get_option( 'translation' ) ); ?> />
317 <?php
318 }
319
320 public static function update_control_toggleadvanced_cb() {
321 ?>
322 <select class="update_control_dependency" id="update_control_toggleadvanced" name="update_control_options[toggleadvanced]">
323 <option <?php selected( 'show' == self::get_option( 'toggleadvanced' ) ); ?> value="show"><?php _e( 'Show', 'update-control' ); ?></option>
324 <option <?php selected( 'hide' == self::get_option( 'toggleadvanced' ) ); ?> value="hide"><?php _e( 'Hide', 'update-control' ); ?></option>
325 </select>
326 <?php
327 }
328
329 public static function update_control_vcscheck_cb() {
330 ?>
331 <input type="checkbox" class="update_control_advanced" id="update_control_vcscheck" name="update_control_options[vcscheck]" <?php checked( self::get_option( 'vcscheck' ) ); ?> />
332 <?php
333 }
334
335 public static function update_control_email_active_cb() {
336 ?>
337 <select class="update_control_advanced" id="update_control_email_active" name="update_control_options[emailactive]">
338 <option <?php selected( 'yes' == self::get_option( 'emailactive' ) ); ?> value="yes"><?php _e( 'Yes', 'update-control' ); ?></option>
339 <option <?php selected( 'no' == self::get_option( 'emailactive' ) ); ?> value="no"><?php _e( 'No', 'update-control' ); ?></option>
340 </select>
341 <?php
342 }
343
344 public static function update_control_email_success_cb() {
345 ?>
346 <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' ) ); ?> />
347 <?php
348 }
349
350 public static function update_control_email_failure_cb() {
351 ?>
352 <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' ) ); ?> />
353 <?php
354 }
355
356 public static function update_control_email_critical_cb() {
357 ?>
358 <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' ) ); ?> />
359 <?php
360 }
361
362 public static function update_control_email_debug_cb() {
363 ?>
364 <input type="checkbox" class="update_control_advanced" id="update_control_email_debug" name="update_control_options[debugemail]" <?php checked( self::get_option( 'debugemail' ) ); ?> />
365 <?php
366 }
367
368 public static function sanitize_options( $options ) {
369 $options = (array) $options;
370
371 $options['active'] = ( in_array( $options['active'], array( 'yes', 'no' ) ) ? $options['active'] : 'yes' );
372 $options['core'] = ( in_array( $options['core'], array( 'minor', 'major', 'dev' ) ) ? $options['core'] : 'minor' );
373 $options['plugin'] = ! empty( $options['plugin'] );
374 $options['theme'] = ! empty( $options['theme'] );
375 $options['translation'] = ! empty( $options['translation'] );
376 $options['toggleadvanced'] = 'hide';
377 $options['vcscheck'] = ! empty( $options['vcscheck'] );
378 $options['emailactive'] = ( in_array( $options['emailactive'], array( 'yes', 'no' ) ) ? $options['emailactive'] : 'yes' );
379 $options['successemail'] = ! empty( $options['successemail'] );
380 $options['failureemail'] = ! empty( $options['failureemail'] );
381 $options['criticalemail'] = ! empty( $options['criticalemail'] );
382 $options['debugemail'] = ! empty( $options['debugemail'] );
383
384 return $options;
385 }
386
387 }
388 add_action( 'init', array( 'Stephanis_Update_Control', 'go' ), 0 );