PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.8.7
SiteGuard WP Plugin v1.8.7
1.8.7 1.8.6 1.8.6-beta1 1.8.6-beta2 1.8.4 1.8.5 1.8.3 1.8.2 1.8.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.4.3 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.11 1.7.12 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.0-beta1 1.8.0-beta2 1.8.0-beta3 1.8.0-beta4
siteguard / classes / siteguard-updates-notify.php
siteguard / classes Last commit date
siteguard-admin-filter.php 1 month ago siteguard-base.php 1 month ago siteguard-captcha.php 1 month ago siteguard-config.php 11 months ago siteguard-disable-author-query.php 2 weeks ago siteguard-disable-pingback.php 1 month ago siteguard-disable-xmlrpc.php 1 month ago siteguard-htaccess.php 2 weeks ago siteguard-login-alert.php 1 month ago siteguard-login-history.php 1 month ago siteguard-login-lock.php 1 month ago siteguard-rename-login.php 1 week ago siteguard-updates-notify.php 1 month ago siteguard-waf-exclude-rule.php 1 month ago
siteguard-updates-notify.php
280 lines
1 <?php
2 /*
3 This function based on WP Updates Notifier 1.4.1 by Scott Cariss.
4 */
5 class SiteGuard_UpdatesNotify extends SiteGuard_Base {
6 const CRON_NAME = 'siteguard_update_check';
7
8 function __construct() {
9 add_action( self::CRON_NAME, array( $this, 'do_update_check' ) ); // action to link cron task to actual task
10 }
11
12 public function init() {
13 global $siteguard_config;
14 $siteguard_config->set( 'notify_wpcore', '1' );
15 $siteguard_config->set( 'notify_plugins', '2' );
16 $siteguard_config->set( 'notify_themes', '2' );
17 $siteguard_config->set(
18 'notified',
19 array(
20 'core' => '',
21 'plugin' => array(),
22 'theme' => array(),
23 )
24 );
25 $siteguard_config->set( 'last_check_time', false );
26 // We need save the configuration before calling self::check_requirements.
27 $siteguard_config->update();
28 if ( true === self::check_requirements() ) {
29 $siteguard_config->set( 'updates_notify_enable', '1' );
30 $siteguard_config->update();
31 self::feature_on();
32 } else {
33 $siteguard_config->set( 'updates_notify_enable', '0' );
34 $siteguard_config->update();
35 }
36 }
37 public static function check_requirements() {
38 $error = siteguard_check_multisite();
39 if ( is_wp_error( $error ) ) {
40 return $error;
41 }
42 $error = self::check_disable_wp_cron();
43 if ( is_wp_error( $error ) ) {
44 return $error;
45 }
46 $error = self::check_wp_cron_access();
47 if ( is_wp_error( $error ) ) {
48 return $error;
49 }
50 return true;
51 }
52 static function check_disable_wp_cron() {
53 if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
54 $message = esc_html__( 'DISABLE_WP_CRON is set to true. This feature cannot be used.', 'siteguard' );
55 $error = new WP_Error( 'siteguard_updates_notify', $message );
56 return $error;
57 }
58 return true;
59 }
60 static function check_wp_cron_access() {
61 $result = wp_remote_post( site_url( '/wp-cron.php' ) );
62 if ( ! is_wp_error( $result ) && 200 === $result['response']['code'] ) {
63 return true;
64 }
65 $message = esc_html__( 'wp-cron.php cannot be accessed. This may be caused by access control settings.', 'siteguard' );
66 $error = new WP_Error( 'siteguard_updates_notify', $message );
67 return $error;
68 }
69 public static function feature_on() {
70 // Already scheduled
71 if ( false !== wp_get_schedule( self::CRON_NAME ) ) {
72 return;
73 }
74
75 // Schedule cron for this plugin.
76 wp_schedule_event( time(), 'daily', self::CRON_NAME );
77 }
78
79 public static function feature_off() {
80 wp_clear_scheduled_hook( self::CRON_NAME ); // clear cron
81 }
82
83 public function do_update_check() {
84 global $siteguard_config;
85 $message = ''; // start with a blank message //
86 if ( '0' != $siteguard_config->get( 'notify_wpcore' ) ) { // are we to check for WordPress core?
87 $core_updated = self::core_update_check( $message ); // check the WP core for updates
88 } else {
89 $core_updated = false; // no core updates
90 }
91 if ( '0' != $siteguard_config->get( 'notify_plugins' ) ) { // are we to check for plugin updates? //
92 $plugins_updated = self::plugins_update_check( $message, $siteguard_config->get( 'notify_plugins' ) ); // check for plugin updates
93 } else {
94 $plugins_updated = false; // no plugin updates
95 }
96 if ( '0' != $siteguard_config->get( 'notify_themes' ) ) { // are we to check for theme updates? //
97 $themes_updated = self::themes_update_check( $message, $siteguard_config->get( 'notify_themes' ) ); // check for theme updates //
98 } else {
99 $themes_updated = false; // no theme updates
100 }
101 if ( $core_updated || $plugins_updated || $themes_updated ) { // Did anything come back as need updating?
102 $message = esc_html__( 'There are updates available for your WordPress site:', 'siteguard' ) . "\n" . $message . "\n";
103 $message .= sprintf( esc_html__( 'Please visit %s to update.', 'siteguard' ), admin_url( 'update-core.php' ) ) . "\n\n--\nSiteGuard WP Plugin";
104 self::send_notify( $message ); // send our notification email.
105 }
106
107 self::log_last_check_time();
108 }
109
110 private static function core_update_check( &$message ) {
111 global $siteguard_config, $wp_version;
112 do_action( 'wp_version_check' ); // force WP to check its core for updates
113 $update_core = get_site_transient( 'update_core' ); // get information of updates
114 $notified = $siteguard_config->get( 'notified' );
115 if ( 'upgrade' == $update_core->updates[0]->response ) { // is WP core update available?
116 if ( $update_core->updates[0]->current != $notified['core'] ) { // have we already notified about this version?
117 require_once ABSPATH . WPINC . '/version.php'; // Including this because some plugins can mess with the real version stored in the DB.
118 $new_core_ver = $update_core->updates[0]->current; // The new WP core version
119 $old_core_ver = $wp_version; // the old WP core version
120 $message .= "\n" . sprintf( esc_html__( 'WordPress core: Update available. Please update from version %1$s to %2$s.', 'siteguard' ), $old_core_ver, $new_core_ver ) . "\n";
121 $notified['core'] = $new_core_ver; // set core version we are notifying about
122 $siteguard_config->set( 'notified', $notified );
123 $siteguard_config->update();
124 return true; // we have updates so return true
125 } else {
126 return false; // There are updates but we have already notified in the past.
127 }
128 }
129 $notified['core'] = ''; // no updates lets set this nothing
130 $siteguard_config->set( 'notified', $notified );
131 $siteguard_config->update();
132 return false; // no updates return false
133 }
134
135 private static function plugins_update_check( &$message, $allOrActive ) {
136 global $siteguard_config, $wp_version;
137 $cur_wp_version = preg_replace( '/-.*$/', '', $wp_version );
138 $notified = $siteguard_config->get( 'notified' );
139 do_action( 'wp_update_plugins' ); // force WP to check plugins for updates
140 $update_plugins = get_site_transient( 'update_plugins' ); // get information of updates
141 if ( ! empty( $update_plugins->response ) ) { // any plugin updates available?
142 $plugins_need_update = $update_plugins->response; // plugins that need updating
143 if ( 2 == $allOrActive ) { // are we to check just active plugins?
144 $active_plugins = array_flip( get_option( 'active_plugins' ) ); // find which plugins are active
145 $plugins_need_update = array_intersect_key( $plugins_need_update, $active_plugins ); // only keep plugins that are active
146 }
147 $plugins_need_update = self::check_plugins_against_notified( $plugins_need_update ); // additional filtering of plugins need update
148 if ( is_array( $plugins_need_update ) && count( $plugins_need_update ) >= 1 ) { // any plugins need updating after all the filtering gone on above?
149 require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // Required for plugin API
150 require_once ABSPATH . WPINC . '/version.php'; // Required for WP core version
151 foreach ( $plugins_need_update as $key => $data ) { // loop through the plugins that need updating
152 $plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $key ); // get local plugin info
153 $info = plugins_api( 'plugin_information', array( 'slug' => $data->slug ) ); // get repository plugin info
154 $message .= "\n" . sprintf( esc_html__( 'Plugin: %1$s has an update available. Please update from version %2$s to %3$s.', 'siteguard' ), $plugin_info['Name'], $plugin_info['Version'], $data->new_version ) . "\n";
155 $message .= "\t" . sprintf( esc_html__( 'Details: %s', 'siteguard' ), $data->url ) . "\n";
156 $message .= "\t" . sprintf( esc_html__( 'Changelog: %1$s%2$s', 'siteguard' ), $data->url, 'changelog/' ) . "\n";
157 if ( isset( $info->tested ) && version_compare( $info->tested, $wp_version, '>=' ) ) {
158 $compat = sprintf( esc_html__( 'Compatibility with WordPress %1$s: 100%% (according to its author)' ), $cur_wp_version );
159 } elseif ( isset( $info->compatibility[ $wp_version ][ $data->new_version ] ) ) {
160 $compat = $info->compatibility[ $wp_version ][ $data->new_version ];
161 $compat = sprintf( esc_html__( 'Compatibility with WordPress %1$s: %2$d%% (%3$d "works" votes out of %4$d total)' ), $wp_version, $compat[0], $compat[2], $compat[1] );
162 } else {
163 $compat = sprintf( esc_html__( 'Compatibility with WordPress %1$s: Unknown' ), $wp_version );
164 }
165 $message .= "\t" . sprintf( esc_html__( 'Compatibility: %s', 'siteguard' ), $compat ) . "\n";
166 $notified['plugin'][ $key ] = $data->new_version; // set plugin version we are notifying about
167 }
168 $siteguard_config->set( 'notified', $notified );
169 $siteguard_config->update();
170 return true; // we have plugin updates return true
171 }
172 } elseif ( 0 != count( $notified['plugin'] ) ) {
173 // is there any plugin notifications?
174 $notified['plugin'] = array(); // set plugin notifications to empty as all plugins up-to-date
175 $siteguard_config->set( 'notified', $notified );
176 $siteguard_config->update();
177 } //
178 return false; // No plugin updates so return false
179 }
180
181 private static function themes_update_check( &$message, $allOrActive ) {
182 global $siteguard_config;
183 $notified = $siteguard_config->get( 'notified' );
184 do_action( 'wp_update_themes' ); // force WP to check for theme updates
185 $update_themes = get_site_transient( 'update_themes' ); // get information of updates
186 if ( ! empty( $update_themes->response ) ) { // any theme updates available?
187 $themes_need_update = $update_themes->response; // themes that need updating
188 if ( 2 == $allOrActive ) { // are we to check just active themes?
189 $active_theme = array( get_option( 'template' ) => array() ); // find current theme that is active
190 $themes_need_update = array_intersect_key( $themes_need_update, $active_theme ); // only keep theme that is active
191 }
192 $themes_need_update = self::check_themes_against_notified( $themes_need_update ); // additional filtering of themes need update
193 if ( is_array( $themes_need_update ) && count( $themes_need_update ) >= 1 ) { // any themes need updating after all the filtering gone on above?
194 foreach ( $themes_need_update as $key => $data ) { // loop through the themes that need updating
195 $theme_info = wp_get_theme( $key ); // get theme info
196 $message .= "\n" . sprintf( esc_html__( 'Theme: %1$s has an update available. Please update from version %2$s to %3$s.', 'siteguard' ), $theme_info['Name'], $theme_info['Version'], $data['new_version'] ) . "\n";
197 $notified['theme'][ $key ] = $data['new_version']; // set theme version we are notifying about
198 }
199 $siteguard_config->set( 'notified', $notified );
200 $siteguard_config->update();
201 return true; // we have theme updates return true
202 }
203 } elseif ( 0 != count( $notified['theme'] ) ) {
204 // is there any theme notifications?
205 $notified['theme'] = array(); // set theme notifications to empty as all themes up-to-date
206 $siteguard_config->set( 'notified', $notified );
207 $siteguard_config->update();
208 } //
209 return false; // No theme updates so return false
210 }
211
212 public static function check_plugins_against_notified( $plugins_need_update ) {
213 global $siteguard_config;
214 $notified = $siteguard_config->get( 'notified' );
215 if ( is_array( $plugins_need_update ) ) {
216 foreach ( $plugins_need_update as $key => $data ) { // loop through plugins that need update
217 if ( isset( $notified['plugin'][ $key ] ) ) { // has this plugin been notified before?
218 if ( $data->new_version == $notified['plugin'][ $key ] ) { // does this plugin version match that of the one that's been notified?
219 unset( $plugins_need_update[ $key ] ); // don't notify this plugin as has already been notified
220 }
221 }
222 }
223 }
224 return $plugins_need_update;
225 }
226
227 public static function check_themes_against_notified( $themes_need_update ) {
228 global $siteguard_config;
229 $notified = $siteguard_config->get( 'notified' );
230 if ( is_array( $themes_need_update ) ) {
231 foreach ( $themes_need_update as $key => $data ) { // loop through themes that need update
232 if ( isset( $notified['theme'][ $key ] ) ) { // has this theme been notified before?
233 if ( $data['new_version'] == $notified['theme'][ $key ] ) { // does this theme version match that of the one that's been notified?
234 unset( $themes_need_update[ $key ] ); // don't notify this theme as has already been notified
235 }
236 }
237 }
238 }
239 return $themes_need_update;
240 }
241
242 public function send_notify( $message ) {
243 global $siteguard_config;
244 $subject = sprintf( esc_html__( 'WordPress: Updates available for %s', 'siteguard' ), home_url() );
245
246 $user_query = new WP_User_Query( array( 'role' => 'Administrator' ) );
247 if ( is_array( $user_query->results ) ) {
248 foreach ( $user_query->results as $user ) {
249 $user_email = $user->get( 'user_email' );
250 if ( true !== @wp_mail( $user_email, $subject, $message ) ) {
251 siteguard_error_log( 'Failed send mail. To:' . $user_email . ' Subject:' . esc_html( $subject ) );
252 }
253 }
254 }
255 }
256
257 private function log_last_check_time() {
258 global $siteguard_config;
259 $siteguard_config->set( 'last_check_time', current_time( 'timestamp' ) );
260 $siteguard_config->update();
261 }
262
263 private static function get_schedules() {
264 $schedules = wp_get_schedules();
265 uasort( $schedules, array( __CLASS__, 'sort_by_interval' ) );
266 return $schedules;
267 }
268
269
270 private static function get_intervals() {
271 $intervals = array_keys( self::get_schedules() );
272 return $intervals;
273 }
274
275
276 private static function sort_by_interval( $a, $b ) {
277 return $a['interval'] - $b['interval'];
278 }
279 }
280