| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Plugin Name: Update Control |
| 5 |
* Plugin URI: http://github.com/georgestephanis/update-control/ |
| 6 |
* Description: Adds a manual toggle to the WordPress Admin Interface for managing auto-updates. |
| 7 |
* Author: George Stephanis |
| 8 |
* Version: 1.0 |
| 9 |
* Author URI: http://stephanis.info/ |
| 10 |
*/ |
| 11 |
|
| 12 |
class Stephanis_Update_Control { |
| 13 |
|
| 14 |
function go() { |
| 15 |
add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); |
| 16 |
add_action( 'init', array( __CLASS__, 'setup_upgrade_filters' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
function setup_upgrade_filters() { |
| 20 |
$options = self::get_options(); |
| 21 |
|
| 22 |
// Do these at priority 1, so other folks can easily override it. |
| 23 |
|
| 24 |
if ( ! $options['active'] ) { |
| 25 |
add_filter( 'auto_upgrader_disabled', '__return_true', 1 ); |
| 26 |
return; // It's all disabled, no need to check the others. |
| 27 |
} |
| 28 |
|
| 29 |
if ( $options['core'] ) { |
| 30 |
add_filter( 'auto_upgrade_core', '__return_true', 1 ); |
| 31 |
} |
| 32 |
|
| 33 |
if ( $options['plugin'] ) { |
| 34 |
add_filter( 'auto_upgrade_plugin', '__return_true', 1 ); |
| 35 |
} |
| 36 |
|
| 37 |
if ( $options['theme'] ) { |
| 38 |
add_filter( 'auto_upgrade_theme', '__return_true', 1 ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
function get_options() { |
| 43 |
$defaults = array( |
| 44 |
'active' => true, |
| 45 |
'core' => false, |
| 46 |
'plugin' => false, |
| 47 |
'theme' => false, |
| 48 |
); |
| 49 |
$args = get_option( 'update_control_options', array() ); |
| 50 |
return wp_parse_args( $args, $defaults ); |
| 51 |
} |
| 52 |
|
| 53 |
function get_option( $key ) { |
| 54 |
$options = self::get_options(); |
| 55 |
if ( isset( $options[ $key ] ) ) { |
| 56 |
return $options[ $key ]; |
| 57 |
} |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
function register_settings() { |
| 62 |
add_settings_section( |
| 63 |
'update-control', |
| 64 |
esc_html__( 'Automatic Updates', 'update-control' ), |
| 65 |
array( __CLASS__, 'update_control_settings_section' ), |
| 66 |
'general' |
| 67 |
); |
| 68 |
|
| 69 |
if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
add_settings_field( |
| 74 |
'update_control_active', |
| 75 |
sprintf( '<label for="update_control_active">%1$s</label>', __( 'Permit Automatic Updates?', 'update-control' ) ), |
| 76 |
array( __CLASS__, 'update_control_active_cb' ), |
| 77 |
'general', |
| 78 |
'update-control' |
| 79 |
); |
| 80 |
|
| 81 |
add_settings_field( |
| 82 |
'update_control_core', |
| 83 |
sprintf( '<label for="update_control_core">%1$s</label>', __( 'Permit Automatic Major Core Updates?', 'update-control' ) ), |
| 84 |
array( __CLASS__, 'update_control_core_cb' ), |
| 85 |
'general', |
| 86 |
'update-control' |
| 87 |
); |
| 88 |
|
| 89 |
add_settings_field( |
| 90 |
'update_control_plugin', |
| 91 |
sprintf( '<label for="update_control_plugin">%1$s</label>', __( 'Permit Automatic Plugin Updates?', 'update-control' ) ), |
| 92 |
array( __CLASS__, 'update_control_plugin_cb' ), |
| 93 |
'general', |
| 94 |
'update-control' |
| 95 |
); |
| 96 |
|
| 97 |
add_settings_field( |
| 98 |
'update_control_theme', |
| 99 |
sprintf( '<label for="update_control_theme">%1$s</label>', __( 'Permit Automatic Theme Updates?', 'update-control' ) ), |
| 100 |
array( __CLASS__, 'update_control_theme_cb' ), |
| 101 |
'general', |
| 102 |
'update-control' |
| 103 |
); |
| 104 |
|
| 105 |
register_setting( 'general', 'update_control_options', array( __CLASS__, 'sanitize_options' ) ); |
| 106 |
} |
| 107 |
|
| 108 |
function update_control_settings_section() { |
| 109 |
if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) : ?> |
| 110 |
<p id="update-control-settings-section"> |
| 111 |
<?php _e( 'You have the <code>AUTOMATIC_UPDATER_DISABLED</code> constant set. Automatic updates are disabled.', 'update-control' ); ?> |
| 112 |
</p> |
| 113 |
<?php else : ?> |
| 114 |
<p id="update-control-settings-section"> |
| 115 |
<?php _e( 'This section lets you specify what areas of your WordPress install will be permitted to auto-update.', 'update-control' ); ?> |
| 116 |
</p> |
| 117 |
<script> |
| 118 |
jQuery(document).ready(function($){ |
| 119 |
$('#update_control_active').click(function(){ |
| 120 |
$('.update_control_dependency').attr( 'disabled', ! this.checked ); |
| 121 |
}).trigger('click'); |
| 122 |
}); |
| 123 |
</script> |
| 124 |
<style> |
| 125 |
.update_control_dependency[disabled] { |
| 126 |
opacity: 0.4; |
| 127 |
} |
| 128 |
</style> |
| 129 |
<?php endif; |
| 130 |
} |
| 131 |
|
| 132 |
function update_control_active_cb() { |
| 133 |
?> |
| 134 |
<input type="checkbox" id="update_control_active" name="update_control_options[active]" <?php checked( self::get_option( 'active' ) ); ?> /> |
| 135 |
<?php |
| 136 |
} |
| 137 |
|
| 138 |
function update_control_core_cb() { |
| 139 |
?> |
| 140 |
<input type="checkbox" class="update_control_dependency" id="update_control_core" name="update_control_options[core]" <?php checked( self::get_option( 'core' ) ); ?> /> |
| 141 |
<?php |
| 142 |
} |
| 143 |
|
| 144 |
function update_control_plugin_cb() { |
| 145 |
?> |
| 146 |
<input type="checkbox" class="update_control_dependency" id="update_control_plugin" name="update_control_options[plugin]" <?php checked( self::get_option( 'plugin' ) ); ?> /> |
| 147 |
<?php |
| 148 |
} |
| 149 |
|
| 150 |
function update_control_theme_cb() { |
| 151 |
?> |
| 152 |
<input type="checkbox" class="update_control_dependency" id="update_control_theme" name="update_control_options[theme]" <?php checked( self::get_option( 'theme' ) ); ?> /> |
| 153 |
<?php |
| 154 |
} |
| 155 |
|
| 156 |
function sanitize_options( $options ) { |
| 157 |
$options = (array) $options; |
| 158 |
|
| 159 |
$options['active'] = ! empty( $options['active'] ); |
| 160 |
$options['core'] = ! empty( $options['core'] ); |
| 161 |
$options['plugin'] = ! empty( $options['plugin'] ); |
| 162 |
$options['theme'] = ! empty( $options['theme'] ); |
| 163 |
|
| 164 |
return $options; |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
Stephanis_Update_Control::go(); |