Skip to content

Commit ba9863a

Browse files
committed
additions to update log
1 parent acac750 commit ba9863a

File tree

2 files changed

+154
-13
lines changed

2 files changed

+154
-13
lines changed

admin/class-boldgrid-backup-admin-auto-updates-logger.php

Lines changed: 153 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,180 @@ class Boldgrid_Backup_Admin_Auto_Updates_Logger {
3333
*/
3434
public $auto_update_log;
3535

36+
/**
37+
* Settings.
38+
*
39+
* @var array
40+
*/
41+
public $settings;
42+
3643
/**
3744
* Constructor.
3845
*
3946
* @since 1.8.0
4047
*/
4148
public function __construct() {
42-
add_action( 'upgrader_process_complete', array( $this, 'log_update' ), 10, 2 );
49+
$this->core = apply_filters( 'boldgrid_backup_get_core', null );
50+
$this->settings = $this->core->settings->get_setting( 'auto_update' );
51+
52+
// Hook into WordPress initialization to detect auto-updates.
53+
add_action( 'init', array( $this, 'detect_auto_update' ) );
54+
55+
// Hook into WordPress update process to log update information.
56+
add_action( 'upgrader_process_complete', array( $this, 'upgrade_complete' ), 10, 2 );
4357
}
4458

59+
/**
60+
* Get Days.
61+
*
62+
* @return int
63+
*/
64+
public function get_days() {
65+
if ( empty( $this->settings['timely-updates-enabled'] ) || empty( $this->settings['days'] ) ) {
66+
return 0;
67+
} else {
68+
return $this->settings['days'];
69+
}
70+
}
71+
72+
/**
73+
* Callback function to detect when WordPress auto-update process starts.
74+
*/
75+
public function detect_auto_update() {
76+
if ( defined( 'DOING_CRON' ) && DOING_CRON && false !== strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) ) {
77+
// Timely auto updates enabled.
78+
$timely_auto_updates = empty( $this->settings['timely-updates-enabled'] ) ? 'No' : 'Yes';
79+
// Today's date.
80+
$today = date( 'Y-m-d' );
81+
// Days to delay the update.
82+
$days_delay = $this->get_days();
83+
84+
$this->auto_update_log = new Boldgrid_Backup_Admin_Log( $this->core );
85+
$this->auto_update_log->init( 'auto-update.log' );
86+
$this->auto_update_log->add( '--- Running Auto Updates ---' );
87+
$this->auto_update_log->add( 'Timely Updates Enabled: ' . $timely_auto_updates );
88+
89+
$core_enabled = false;
90+
91+
foreach ( $this->settings['wpcore'] as $core ) {
92+
if ( ! empty( $core ) ) {
93+
$core_enabled = true;
94+
}
95+
}
96+
97+
// Check for core updates.
98+
if ( $core_enabled ) {
99+
$core_update = get_site_transient( 'update_core' );
100+
101+
if ( isset( $core_update->updates ) && is_array( $core_update->updates ) ) {
102+
foreach ( $core_update->updates as $update ) {
103+
if ( $update->current !== $update->version ) {
104+
$core_msg = sprintf(
105+
'WordPress Core Update Available from version %s to %s.',
106+
$update->current,
107+
$update->version,
108+
);
109+
110+
$this->auto_update_log->add( $core_msg );
111+
} else {
112+
$this->auto_update_log->add( 'WordPress Core is up to date.' );
113+
}
114+
}
115+
}
116+
}
117+
118+
// Check for plugin updates.
119+
$plugin_updates = get_site_transient( 'update_plugins' );
120+
121+
if ( isset( $plugin_updates->response ) && is_array( $plugin_updates->response ) ) {
122+
foreach ( $plugin_updates->response as $plugin_file => $update ) {
123+
if ( ! empty( $this->settings['plugins'][ $plugin_file ] ) ) {
124+
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file );
125+
$plugin_slug = dirname( plugin_basename( WP_PLUGIN_DIR . '/' . $plugin_file ) );
126+
$plugin_name = $plugin_data['Name'];
127+
$plugin_current_version = $plugin_data['Version'];
128+
$remote_plugin_data = wp_remote_get( 'https://api.wordpress.org/plugins/info/1.0/' . $plugin_slug . '.json' );
129+
130+
if ( ! is_wp_error( $remote_plugin_data ) ) {
131+
$plugin_json = json_decode( $remote_plugin_data['body'] );
132+
$plugin_new_version = $plugin_json->version;
133+
$plugin_release_date = date( 'Y-m-d', strtotime( $plugin_json->last_updated ) );
134+
$days_since_plugin_release = abs( strtotime( $today ) - strtotime( $plugin_release_date ) ) / 86400;
135+
$allow_update_msg = 'Yes' === $timely_auto_updates && $days_since_plugin_release < $days_delay ? 'This plugin will be updated.' : 'This plugin will not be updated.';
136+
137+
$plugin_msg = sprintf(
138+
'Plugin Update Available: %s from version %s to %s released on %s. Days since release: %s. Timely auto updates set to %s days after release. %s',
139+
$plugin_name,
140+
$plugin_current_version,
141+
$plugin_new_version,
142+
$plugin_release_date,
143+
$days_since_plugin_release,
144+
$days_delay,
145+
$allow_update_msg
146+
);
147+
148+
$this->auto_update_log->add( $plugin_msg );
149+
}
150+
}
151+
}
152+
} else {
153+
$this->auto_update_log->add( 'All plugins are up to date.' );
154+
}
155+
156+
// Check for theme updates.
157+
$theme_updates = get_site_transient( 'update_themes' );
158+
159+
if ( isset( $theme_updates->response ) && is_array( $theme_updates->response ) ) {
160+
foreach ( $theme_updates->response as $theme_slug => $update ) {
161+
if ( ! empty( $this->settings['themes'][ $theme_slug ] ) ) {
162+
$theme_data = wp_get_theme( $theme_slug );
163+
$theme_name = $theme_data->get( 'Name' );
164+
$theme_current_version = $theme_data->get( 'Version' );
165+
$remote_theme_data = wp_remote_get( 'https://api.wordpress.org/themes/info/1.2/?action=theme_information&slug=' . $theme_slug );
166+
167+
if ( ! is_wp_error( $remote_theme_data ) ) {
168+
$theme_json = json_decode( $remote_theme_data['body'] );
169+
$theme_new_version = $theme_json->version;
170+
$theme_release_date = date( 'Y-m-d', strtotime( $theme_json->last_updated ) );
171+
$days_since_theme_release = abs( strtotime( $today ) - strtotime( $theme_release_date ) ) / 86400;
172+
$allow_update_msg = 'Yes' === $timely_auto_updates && $days_since_theme_release < $days_delay ? 'This theme will be updated.' : 'This theme will not be updated.';
173+
$theme_msg = sprintf(
174+
'Theme Update Available: %s from version %s to %s released on %s. Days since release: %s. Timely auto updates set to %s days after release. %s',
175+
$theme_name,
176+
$theme_current_version,
177+
$theme_new_version,
178+
$theme_release_date,
179+
$days_since_theme_release,
180+
$days_delay,
181+
$allow_update_msg
182+
);
183+
184+
$this->auto_update_log->add( $theme_msg );
185+
}
186+
}
187+
}
188+
} else {
189+
$this->auto_update_log->add( 'All themes are up to date.' );
190+
}
191+
}
192+
}
193+
45194
/**
46195
* Log update information.
47196
*
48197
* @param WP_Upgrader $upgrader_object
49198
* @param array $options
50199
*/
51-
public function log_update( $upgrader_object, $options ) {
52-
$this->core = apply_filters( 'boldgrid_backup_get_core', null );
200+
public function upgrade_complete( $upgrader_object, $options ) {
53201
$this->auto_update_log = new Boldgrid_Backup_Admin_Log( $this->core );
54202
$this->auto_update_log->init( 'auto-update.log' );
55203

56-
$this->auto_update_log->add( 'Upgrader Object: ' . wp_json_encode( $upgrader_object ) );
57-
$this->auto_update_log->add( 'Options: ' . wp_json_encode( $options ) );
58-
59204
if ( isset( $options['action'] ) && 'update' === $options['action'] ) {
60-
$type = isset( $options['type'] ) ? $options['type'] : 'WP Core';
61-
205+
$type = isset( $options['type'] ) ? $options['type'] : 'WP Core';
62206
$update_item = ! empty( $options['temp_backup'] ) ? implode( ', ', $options['temp_backup'] ) : '';
63-
if ( ! empty( $options['temp_backup'] ) ) {
64-
$update_item = implode( ', ', $options['temp_backup'] );
65-
}
66207

67208
$log_message = sprintf(
68-
'Automatic %s update for %s: complete.',
209+
'Automatic %s update for %s complete.',
69210
$type,
70211
$update_item
71212
);

admin/class-boldgrid-backup-admin-auto-updates.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function wordpress_option_updated( $old_value, $new_value, $option ) {
122122
$this->auto_update_log->add( 'Setting WP Core Dev Updates to ' . $dev . '.' );
123123
$major = ( ! empty( $wpcs['major'] ) ) ? 'true' : 'false';
124124
$this->auto_update_log->add( 'Setting WP Core Major Updates to ' . $major . '.' );
125-
$minor = ( ! empty( $wpcs['minor']) ) ? 'true' : 'false';
125+
$minor = ( ! empty( $wpcs['minor'] ) ) ? 'true' : 'false';
126126
$this->auto_update_log->add( 'Setting WP Core Minor Updates to ' . $minor . '.' );
127127
$translation = ( ! empty( $wpcs['translation'] ) ) ? 'true' : 'false';
128128
$this->auto_update_log->add( 'Setting WP Core Translation Updates to ' . $translation . '.' );

0 commit comments

Comments
 (0)