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

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