-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto-activate-auto-updates.php
executable file
·64 lines (59 loc) · 2.29 KB
/
auto-activate-auto-updates.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
<?php
/*
Plugin Name: Auto Activate Auto Updates
Description: Switch to opt-out rather then opt-in for updates
Version: 1.2.0
Author: Tim Nash
Author URI: https://timnash.co.uk
*/
/**
* Triggers on any activation of a plugin
*
* @since 1.0.0
* @return bool
*/
add_action( 'activated_plugin', function($plugin, $network){
// Check our existing deactivation list of plugins to check if we shouldn't be activating the plugin
$deactivated_auto_updates = (array) get_site_option( 'auto_update_deactivated_plugins', array() );
if( in_array( $plugin, $deactivated_auto_updates ) ){
//remove it from the list as its now an active plugin
unset( $deactivated_auto_updates[ $plugin ] );
return update_site_option( 'auto_update_deactivated_plugins', $deactivated_auto_updates );
}
// If plugin hasn't been previously deactivatate carry on
$auto_updates = (array) get_site_option( 'auto_update_plugins', array() );
$auto_updates[] = $plugin;
$auto_updates = array_unique( $auto_updates );
$all_items = apply_filters( 'all_plugins', get_plugins() );
$auto_updates = array_intersect( $auto_updates, array_keys( $all_items ) );
// Update or set the auto update option
update_site_option( 'auto_update_plugins', $auto_updates );
}, 10,2);
/**
* Store plugins that should be auto updated on reactivation
* @since 1.2.0
* @return bool
*/
add_action( 'deactivate_plugin', function( $plugin, $network ){
$no_plugin_update = true;
$auto_updates = (array) get_site_option( 'auto_update_plugins', array() );
if( in_array( $plugin, $auto_updates ) ) $no_plugin_update = false;
if( isset($no_plugin_update) && true === $no_plugin_update ){
$deactivated_auto_updates = (array) get_site_option( 'auto_update_deactivated_plugins', array() );
$deactivated_auto_updates[] = $plugin;
update_site_option( 'auto_update_deactivated_plugins', $deactivated_auto_updates );
}
}, 10,2);
/**
* Triggers Activation Hook when plugin is first activated
* @since 1.1.0
* @return bool
*/
register_activation_hook( __FILE__, function(){
$auto_updates = (array) get_site_option( 'auto_update_plugins', array() );
if( !empty( $auto_updates ) ){
$all_items = apply_filters( 'all_plugins', get_plugins() );
return update_site_option( 'auto_update_plugins', array_keys( $all_items ) );
}
return true;
});