-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass-boldgrid-backup-admin-auto-updates.php
More file actions
302 lines (279 loc) · 8.97 KB
/
class-boldgrid-backup-admin-auto-updates.php
File metadata and controls
302 lines (279 loc) · 8.97 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* File: class-boldgrid-backup-admin-auto-updates.php
*
* @link https://www.boldgrid.com
* @since 1.14.0
*
* @package Boldgrid_Backup
* @subpackage Boldgrid_Backup/admin
* @copyright BoldGrid
* @version 1.14.0
* @author BoldGrid <support@boldgrid.com>
*/
use Boldgrid\Library\Library\Plugin\Plugins;
/**
* Class: Boldgrid_Backup_Admin_Auto_Updates.
*
* @since 1.14.0
*/
class Boldgrid_Backup_Admin_Auto_Updates {
/**
* Settings.
*
* @since 1.14.0
* @var array
*/
public $settings;
/**
* Active Plugins.
*
* @since 1.14.0
* @var array
*/
public $plugins = array();
/**
* Themes.
*
* @since 1.14.0
* @var array
*/
public $themes = array();
/**
* Active Plugins.
*
* @since 1.14.0
* @var Boldgrid_Backup_Admin_Core
*/
public $core;
/**
* Constructor.
*
* @since 1.14.0
*/
public function __construct() {
$this->set_settings();
$plugins = new \Boldgrid\Library\Library\Plugin\Plugins();
$this->plugins = $plugins->getAllPlugins();
$this->themes = new \Boldgrid\Library\Library\Theme\Themes();
add_filter( 'automatic_updater_disabled', '__return_false' );
}
/**
* Updates the WordPress Options table.
*
* This is fired by the "update_option_{$option}" hook when the option is
* either auto_update_plugins or auto_update_themes.
*
* Prior to WordPress 5.5, Total Upkeep managed auto updates per plugin / theme and saved the
* settings in $settings['auto_update']. As of WordPress 5.5, WordPress handles auto updates
* per plugin / theme and saves this info to auto_update_plugins and auto_update_themes options.
* This method is here to ensure the two record systems stay in sync. Whenever the official options
* are updated, this method will apply the same settings to the Total Upkeep settings.
*
* @see Boldgrid_Backup::define_admin_hooks() add_action definition.
*
* @see https://developer.wordpress.org/reference/hooks/update_option_option/ WP Hook Documentation
*
* @since 1.14.3
*
* @param array $old_value Value of the option before updating.
* @param array $new_value Value of the option after updating.
* @param string $option Name of option being updated.
*/
public function wordpress_option_updated( $old_value, $new_value, $option ) {
// Determines if this is being fired for a theme or plugin update
$update_type = 'auto_update_plugins' === $option ? 'plugins' : 'themes';
$core = apply_filters( 'boldgrid_backup_get_core', null );
$settings = $core->settings->get_settings();
// The plugins / themes listed in $new_value will only be those that have auto updates enabled.
$enabled_offers = $new_value;
foreach ( $settings['auto_update'][ $update_type ] as $offer => $enabled ) {
// Do not modify the 'default' setting. This is used to define the default for new themes / plugins.
if ( 'default' === $offer ) {
continue;
}
// If the theme / plugin is found in the $enabled_offers array, enable in our settings, otherwise disable.
$settings['auto_update'][ $update_type ][ $offer ] = in_array( $offer, $enabled_offers, true ) ? '1' : '0';
}
/*
* The above loop cannot account for the presence of a plugin or theme in the $enabled_offers
* array, that does not yet exist in the auto update settings. This will be the case if a new plugin or theme is installed
* before they settings for that theme or plugin have been set in the Total Upkeep settings page. This will ensure that
* scenario is covered.
*/
foreach ( $enabled_offers as $enabled_offer ) {
$settings['auto_update'][ $update_type ][ $enabled_offer ] = '1';
}
// Save the settings.
$core->settings->save( $settings );
}
/**
* Set Settings.
*
* @since 1.14.0
*/
public function set_settings() {
$core = apply_filters( 'boldgrid_backup_get_core', null );
$this->settings = $core->settings->get_setting( 'auto_update' );
}
/**
* Get Days.
*
* @since 1.14.0
*
* @return int
*/
public function get_days() {
if ( empty( $this->settings['timely-updates-enabled'] ) || empty( $this->settings['days'] ) ) {
return 0;
} else {
return $this->settings['days'];
}
}
/**
* Maybe Update Plugin.
*
* @since 1.14.0
*
* @param string $slug Plugin Slug.
* @return bool
*/
public function maybe_update_plugin( $slug ) {
$days_to_wait = $this->get_days();
$plugin = \Boldgrid\Library\Library\Plugin\Plugins::getBySlug( $this->plugins, $slug );
$plugin->setUpdateData();
$days_since_release = $plugin->updateData->days; //phpcs:ignore WordPress.NamingConventions.ValidVariableName
$plugin_update_enabled = array_key_exists( $plugin->getFile(), $this->settings['plugins'] ) ? (bool) $this->settings['plugins'][ $plugin->getFile() ] : (bool) $this->settings['plugins']['default'];
$is_update_time = ( $days_since_release >= $days_to_wait );
if ( $is_update_time && true === $plugin_update_enabled ) {
return true;
} else {
return false;
}
}
/**
* Maybe Update Theme.
*
* @since 1.14.0
*
* @param string $stylesheet Theme's Stylesheet.
* @return bool
*/
public function maybe_update_theme( $stylesheet ) {
$days_to_wait = $this->get_days();
$theme = $this->themes->getFromStylesheet( $stylesheet );
$theme->setUpdateData();
$days_since_release = $theme->updateData->days; //phpcs:ignore WordPress.NamingConventions.ValidVariableName
$theme_update_enabled = isset( $this->settings['themes'][ $stylesheet ] ) ? (bool) $this->settings['themes'][ $stylesheet ] : (bool) $this->settings['themes']['default'];
$is_update_time = ( $days_since_release >= $days_to_wait );
if ( $is_update_time && true === $theme_update_enabled ) {
return true;
} else {
return false;
}
}
/**
* Auto Update Plugins.
*
* This method is the callback for the 'auto_update_plugin' action hook.
*
* @since 1.14.0
*
* @param mixed $update Whether or not to update.
* @param stdClass $item The item class passed to callback.
*
* @return bool
*/
public function auto_update_plugins( $update, $item ) {
/*
* In WP5.5 , WordPress uses the auto_update_${type} hook to
* determine if auto updates is forced or not, in order to disable the
* Enable / Disable action links on the plugin / theme pages. When it checks the filter in those cases
* it provides null as the $update parameter. If we return $null in those cases
* WP will not think that the auto updates are 'forced'.
*/
if ( is_null( $update ) ) {
return null;
}
// Array of plugin slugs to always auto-update.
$plugins = array();
foreach ( $this->plugins as $plugin ) {
if ( $this->maybe_update_plugin( $plugin->getSlug() ) ) {
$plugins[] = $plugin->getSlug();
}
}
if ( in_array( $item->slug, $plugins, true ) ) {
// Always update plugins in this array.
return true;
} else {
// Else, Do Not Update Plugin.
return false;
}
}
/**
* Auto Update Themes.
*
* This method is the callback for the 'auto_update_theme' action hook.
*
* @since 1.14.0
*
* @param mixed $update Whether or not to update.
* @param stdClass $item The item class passed to callback.
*
* @return bool
*/
public function auto_update_themes( $update, $item ) {
/*
* In WP5.5 , WordPress uses the auto_update_${type} hook to
* determine if auto updates is forced or not, in order to disable the
* Enable / Disable action links on the plugin / theme pages. When it checks the filter in those cases
* it provides null as the $update parameter. If we return $null in those cases
* WP will not think that the auto updates are 'forced'.
*/
if ( is_null( $update ) ) {
return null;
}
// Array of theme stylesheets to always auto-update.
$themes = array();
foreach ( $this->themes->get() as $theme ) {
if ( $this->maybe_update_theme( $theme->stylesheet ) ) {
$themes[] = $theme->stylesheet;
}
}
if ( in_array( $item->theme, $themes, true ) ) {
// Always update themes in this array.
return true;
} else {
// Else, Do Not Update theme.
return false;
}
}
/**
* Auto Update Core.
*
* Sets the type of updates to perform for wpcore.
*
* @since 1.14.0
*/
public function auto_update_core() {
$wpcs_default = array(
'all' => false,
'minor' => true,
'major' => false,
'translation' => false,
'dev' => false,
);
$wpcs = isset( $this->settings['wpcore'] ) ? $this->settings['wpcore'] : $wpcs_default;
if ( $wpcs['all'] ) {
add_filter( 'auto_update_core', '__return_true' );
}
$dev = ( $wpcs['dev'] ) ? 'true' : 'false';
$major = ( $wpcs['major'] ) ? 'true' : 'false';
$minor = ( $wpcs['minor'] ) ? 'true' : 'false';
$translation = ( $wpcs['translation'] ) ? 'true' : 'false';
add_filter( 'allow_major_auto_core_updates', '__return_' . $major );
add_filter( 'allow_minor_auto_core_updates', '__return_' . $minor );
add_filter( 'auto_update_translation', '__return_' . $translation );
add_filter( 'allow_dev_auto_core_updates', '__return_' . $dev );
}
}