PluginProbe ʕ •ᴥ•ʔ
SiteGuard WP Plugin / 1.7.7
SiteGuard WP Plugin v1.7.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 3 years ago siteguard-base.php 3 years ago siteguard-captcha.php 3 years ago siteguard-config.php 3 years ago siteguard-disable-author-query.php 3 years ago siteguard-disable-pingback.php 3 years ago siteguard-disable-xmlrpc.php 3 years ago siteguard-htaccess.php 3 years ago siteguard-login-alert.php 3 years ago siteguard-login-history.php 3 years ago siteguard-login-lock.php 3 years ago siteguard-rename-login.php 2 years ago siteguard-updates-notify.php 3 years ago siteguard-waf-exclude-rule.php 3 years ago
siteguard-updates-notify.php
283 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 defined true. This function can't 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__( 'Please solve the problem that can not be accessed wp-cron.php. Might be access control.', '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__( 'WP-Core: WordPress is out of date. 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 is out of date. 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 } else {
173 if ( 0 != count( $notified['plugin'] ) ) { // 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 }
179 return false; // No plugin updates so return false
180 }
181
182 private static function themes_update_check( &$message, $allOrActive ) {
183 global $siteguard_config;
184 $notified = $siteguard_config->get( 'notified' );
185 do_action( 'wp_update_themes' ); // force WP to check for theme updates
186 $update_themes = get_site_transient( 'update_themes' ); // get information of updates
187 if ( ! empty( $update_themes->response ) ) { // any theme updates available?
188 $themes_need_update = $update_themes->response; // themes that need updating
189 if ( 2 == $allOrActive ) { // are we to check just active themes?
190 $active_theme = array( get_option( 'template' ) => array() ); // find current theme that is active
191 $themes_need_update = array_intersect_key( $themes_need_update, $active_theme ); // only keep theme that is active
192 }
193 $themes_need_update = self::check_themes_against_notified( $themes_need_update ); // additional filtering of themes need update
194 if ( is_array( $themes_need_update ) && count( $themes_need_update ) >= 1 ) { // any themes need updating after all the filtering gone on above?
195 foreach ( $themes_need_update as $key => $data ) { // loop through the themes that need updating
196 $theme_info = wp_get_theme( $key ); // get theme info
197 $message .= "\n" . sprintf( esc_html__( 'Theme: %1$s is out of date. Please update from version %2$s to %3$s', 'siteguard' ), $theme_info['Name'], $theme_info['Version'], $data['new_version'] ) . "\n";
198 $notified['theme'][ $key ] = $data['new_version']; // set theme version we are notifying about
199 }
200 $siteguard_config->set( 'notified', $notified );
201 $siteguard_config->update();
202 return true; // we have theme updates return true
203 }
204 } else {
205 if ( 0 != count( $notified['theme'] ) ) { // is there any theme notifications?
206 $notified['theme'] = array(); // set theme notifications to empty as all themes up-to-date
207 $siteguard_config->set( 'notified', $notified );
208 $siteguard_config->update();
209 }
210 }
211 return false; // No theme updates so return false
212 }
213
214 public static function check_plugins_against_notified( $plugins_need_update ) {
215 global $siteguard_config;
216 $notified = $siteguard_config->get( 'notified' );
217 if ( is_array( $plugins_need_update ) ) {
218 foreach ( $plugins_need_update as $key => $data ) { // loop through plugins that need update
219 if ( isset( $notified['plugin'][ $key ] ) ) { // has this plugin been notified before?
220 if ( $data->new_version == $notified['plugin'][ $key ] ) { // does this plugin version match that of the one that's been notified?
221 unset( $plugins_need_update[ $key ] ); // don't notify this plugin as has already been notified
222 }
223 }
224 }
225 }
226 return $plugins_need_update;
227 }
228
229 public static function check_themes_against_notified( $themes_need_update ) {
230 global $siteguard_config;
231 $notified = $siteguard_config->get( 'notified' );
232 if ( is_array( $themes_need_update ) ) {
233 foreach ( $themes_need_update as $key => $data ) { // loop through themes that need update
234 if ( isset( $notified['theme'][ $key ] ) ) { // has this theme been notified before?
235 if ( $data['new_version'] == $notified['theme'][ $key ] ) { // does this theme version match that of the one that's been notified?
236 unset( $themes_need_update[ $key ] ); // don't notify this theme as has already been notified
237 }
238 }
239 }
240 }
241 return $themes_need_update;
242 }
243
244 public function send_notify( $message ) {
245 global $siteguard_config;
246 $subject = sprintf( esc_html__( 'WordPress: Updates Available @ %s', 'siteguard' ), home_url() );
247
248 $user_query = new WP_User_Query( array( 'role' => 'Administrator' ) );
249 if ( is_array( $user_query->results ) ) {
250 foreach ( $user_query->results as $user ) {
251 $user_email = $user->get( 'user_email' );
252 if ( true !== @wp_mail( $user_email, $subject, $message ) ) {
253 ;
254 siteguard_error_log( 'Failed send mail. To:' . $user_email . ' Subject:' . esc_html( $subject ) );
255 }
256 }
257 }
258 }
259
260 private function log_last_check_time() {
261 global $siteguard_config;
262 $siteguard_config->set( 'last_check_time', current_time( 'timestamp' ) );
263 $siteguard_config->update();
264 }
265
266 private static function get_schedules() {
267 $schedules = wp_get_schedules();
268 uasort( $schedules, array( __CLASS__, 'sort_by_interval' ) );
269 return $schedules;
270 }
271
272
273 private static function get_intervals() {
274 $intervals = array_keys( self::get_schedules() );
275 return $intervals;
276 }
277
278
279 private static function sort_by_interval( $a, $b ) {
280 return $a['interval'] - $b['interval'];
281 }
282 }
283