PluginProbe ʕ •ᴥ•ʔ
CloudSecure WP Security / 1.4.1
CloudSecure WP Security v1.4.1
1.4.10 1.4.9 trunk 0.9.0 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.19 1.3.2 1.3.20 1.3.21 1.3.22 1.3.23 1.3.24 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8
cloudsecure-wp-security / modules / update-notice.php
cloudsecure-wp-security / modules Last commit date
admin 4 months ago cli 8 months ago lib 4 months ago captcha.php 2 years ago cloudsecure-wp.php 4 months ago common.php 4 months ago config.php 2 years ago disable-access-system-file.php 4 months ago disable-author-query.php 2 years ago disable-login.php 1 year ago disable-restapi.php 7 months ago disable-xmlrpc.php 1 year ago htaccess.php 5 months ago login-log.php 4 months ago login-notification.php 1 year ago rename-login-page.php 1 year ago restrict-admin-page.php 10 months ago server-error-notification.php 1 year ago two-factor-authentication.php 4 months ago unify-messages.php 2 years ago update-notice.php 7 months ago waf-engine.php 4 months ago waf.php 4 months ago
update-notice.php
437 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class CloudSecureWP_Update_Notice extends CloudSecureWP_Common {
8 private const KEY_FEATURE = 'update_notice';
9 private const KEY_WP = self::KEY_FEATURE . '_wp';
10 private const KEY_PLUGIN = self::KEY_FEATURE . '_plugin';
11 private const KEY_THEME = self::KEY_FEATURE . '_theme';
12 private const WP_VALUES = array( 1, 2 ); // 無効、有効 .
13 private const PLUGIN_VALUES = array( 1, 2, 3 ); // 無効、有効(�
14 �て)、有効(有効化されたもの) .
15 private const THEME_VALUES = array( 1, 2, 3 ); // 無効、有効(�
16 �て)、有効(有効化されたもの) .
17 private const KEY_CRON = 'cloudsecurewp_' . self::KEY_FEATURE . '_cron';
18 private const KEY_LAST_NOTICE = self::KEY_FEATURE . '_last_notice';
19 private const LAST_NOTICE_WP = 'wp';
20 private const LAST_NOTICE_PLUGINS = 'plugins';
21 private const LAST_NOTICE_THEMES = 'themes';
22 private $config;
23
24 function __construct( array $info, CloudSecureWP_Config $config ) {
25 parent::__construct( $info );
26 $this->config = $config;
27 }
28
29 /**
30 * 機能毎のKEY取得
31 *
32 * @return string
33 */
34 public function get_feature_key(): string {
35 return self::KEY_FEATURE;
36 }
37
38 /**
39 * 有効無効判定
40 *
41 * @return bool
42 */
43 public function is_enabled(): bool {
44 return $this->config->get( $this->get_feature_key() ) === 't' ? true : false;
45 }
46
47 /**
48 * 初期設定値取得
49 *
50 * @return array
51 */
52 public function get_default(): array {
53 $flag = false;
54
55 if ( $this->check_environment() && '' === $this->check_cron_error() ) {
56 $flag = true;
57 }
58
59 $ret = array(
60 self::KEY_FEATURE => $flag ? 't' : 'f',
61 self::KEY_WP => self::WP_VALUES[1],
62 self::KEY_PLUGIN => self::PLUGIN_VALUES[2],
63 self::KEY_THEME => self::THEME_VALUES[2],
64 self::KEY_LAST_NOTICE => $this->get_last_notice_default(),
65 );
66 return $ret;
67 }
68
69 /**
70 * 設定値key取得
71 */
72 public function get_keys(): array {
73 $ret = array(
74 self::KEY_FEATURE,
75 self::KEY_WP,
76 self::KEY_PLUGIN,
77 self::KEY_THEME,
78 self::KEY_LAST_NOTICE,
79 );
80 return $ret;
81 }
82
83 /**
84 * LAST_NOTICE 初期値取得
85 */
86 public function get_last_notice_default(): array {
87 $ret = array(
88 self::LAST_NOTICE_WP => '',
89 self::LAST_NOTICE_PLUGINS => array(),
90 self::LAST_NOTICE_THEMES => array(),
91 );
92 return $ret;
93 }
94
95 /**
96 * 設定値取得
97 */
98 public function get_settings(): array {
99 $settings = array();
100 $keys = $this->get_keys();
101
102 foreach ( $keys as $key ) {
103 $settings[ $key ] = $this->config->get( $key );
104 }
105
106 return $settings;
107 }
108
109 /**
110 * 設定値保存
111 *
112 * @param array $settings
113 * @return void
114 */
115 public function save_settings( $settings ): void {
116 $keys = $this->get_keys();
117
118 foreach ( $keys as $key ) {
119 $this->config->set( $key, $settings[ $key ] ?? '' );
120 }
121 $this->config->save();
122 }
123
124 /**
125 * 設定定義値取得
126 *
127 * @return array
128 */
129 public function get_constant_settings(): array {
130 $ret = array(
131 self::KEY_WP => self::WP_VALUES,
132 self::KEY_PLUGIN => self::PLUGIN_VALUES,
133 self::KEY_THEME => self::THEME_VALUES,
134 );
135 return $ret;
136 }
137
138 /**
139 * 実行環境チェック( cron )
140 *
141 * @return string
142 */
143 public function check_cron_error(): string {
144 if ( false === wp_next_scheduled( 'wp_version_check' ) ) {
145 return 'WP_CRON が無効化されています';
146 }
147
148 $result = wp_remote_post( site_url( '/wp-cron.php' ) );
149
150 if ( is_wp_error( $result ) || 200 !== (int) $result['response']['code'] ) {
151 return 'wp-cron.php へのアクセスに失敗しました';
152 }
153
154 return '';
155 }
156
157 /**
158 * cron名取得
159 *
160 * @return string
161 */
162 public function get_cron_key(): string {
163 return self::KEY_CRON;
164 }
165
166 public function update_notice(): void {
167 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
168 require_once ABSPATH . WPINC . '/version.php';
169
170 $settings = $this->get_settings();
171
172 $wp = '';
173 if ( self::WP_VALUES[0] !== (int) $settings[ self::KEY_WP ] ) {
174 $wp = $this->get_update_wp_version();
175 }
176
177 $body_update = '';
178 if ( ! empty( $wp ) ) {
179 $body_update .= "【WordPressのアップデート】\n";
180 $body_update .= "・WordPressの新しいバージョンは{$wp}です。\n\n";
181 $this->save_last_notice_wp_version( $wp );
182 }
183
184 $plugins = array();
185 if ( (int) $settings[ self::KEY_PLUGIN ] !== self::PLUGIN_VALUES[0] ) {
186 $plugins = $this->get_update_plugin_versions();
187 }
188
189 if ( ! empty( $plugins ) ) {
190 $body_update .= "【プラグインのアップデート】\n";
191
192 foreach ( $plugins as $plugin ) {
193 $body_update .= "・プラグイン{$plugin['name']}の新しいバージョンは{$plugin['new_version']}です。\n";
194 }
195 $body_update .= "\n";
196
197 $this->save_last_notice_plugin_versions( $plugins );
198 }
199
200 $themes = array();
201 if ( self::THEME_VALUES[0] !== (int) $settings[ self::KEY_THEME ] ) {
202 $themes = $this->get_update_theme_versions();
203 }
204
205 if ( ! empty( $themes ) ) {
206 $body_update .= "【テーマのアップデート】\n";
207
208 foreach ( $themes as $theme ) {
209 $body_update .= "・テーマ{$theme['name']}の新しいバージョンは{$theme['new_version']}です。\n";
210 }
211 $body_update .= "\n";
212
213 $this->save_last_notice_theme_versions( $themes );
214 }
215
216 if ( ! empty( $body_update ) ) {
217 $update_page_url = admin_url( 'update-core.php' );
218
219 $subject = 'アップデート通知';
220
221 $body = get_option( 'blogname' ) . "に新たな更新があります。\n\n";
222 $body .= $body_update;
223 $body .= "以下のページを確認し、更新してください。\n\n";
224 $body .= "{$update_page_url}\n\n";
225 $body .= "--\nCloudSecure WP Security\n";
226
227 $admins = $this->get_admin_users();
228
229 foreach ( $admins as $admin ) {
230 $this->wp_send_mail( $admin->user_email, esc_html( $subject ), esc_html( $body ) );
231 }
232 }
233 }
234
235 /**
236 * WP 通知済 バージョン保存
237 *
238 * @param string $version
239 * @return void
240 */
241 private function save_last_notice_wp_version( string $version ): void {
242 $settings = $this->get_settings();
243 if ( ! empty( $version ) ) {
244 $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_WP ] = $version;
245 $this->save_settings( $settings );
246 }
247 }
248
249 /**
250 * wp 更新バージョン取得
251 *
252 * @return string
253 */
254 private function get_update_wp_version(): string {
255 do_action( 'wp_version_check' );
256 $info = get_site_transient( 'update_core' );
257
258 if ( 'upgrade' === ( $info->updates[0]->response ?? '' ) ) {
259 $update_version = $info->updates[0]->current ?? '';
260 $settings = $this->get_settings();
261 $last_notice_version = $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_WP ] ?? '';
262
263 if ( $update_version != $last_notice_version ) {
264 return $update_version;
265 }
266 }
267
268 return '';
269 }
270
271 /**
272 * プラグイン 通知済 バージョン保存
273 *
274 * @param array $versions
275 * @return void
276 */
277 private function save_last_notice_plugin_versions( $versions ): void {
278 $settings = $this->get_settings();
279 $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_PLUGINS ] = array_merge( $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_PLUGINS ], $versions );
280 $this->save_settings( $settings );
281 }
282
283 /**
284 * プラグイン 更新バージョン取得
285 *
286 * @return array
287 */
288 private function get_update_plugin_versions(): array {
289 do_action( 'wp_update_plugins' );
290
291 $settings = $this->get_settings();
292 $res = get_site_transient( 'update_plugins' );
293 $update_plugins = $res->response ?? array();
294 $notice_plugins = array();
295
296 if ( empty( $update_plugins ) ) {
297 $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_PLUGINS ] = $notice_plugins;
298 $this->save_settings( $settings );
299 return $notice_plugins;
300 }
301
302 $last_notice_plugins = $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_PLUGINS ] ?? array();
303 $plugins = get_plugins();
304
305 foreach ( $update_plugins as $plugin_path => $plugin ) {
306 $new_version = '';
307
308 if ( $plugin->new_version !== ( $last_notice_plugins[ $plugin_path ]['new_version'] ?? '' ) ) {
309 if ( self::PLUGIN_VALUES[2] === (int) $settings[ self::KEY_PLUGIN ] ) {
310 if ( is_plugin_active( $plugin_path ) ) {
311 $new_version = $plugin->new_version;
312 }
313 } else {
314 $new_version = $plugin->new_version;
315 }
316
317 if ( '' !== $new_version && array_key_exists( $plugin_path, $plugins ) ) {
318 $notice_plugins[ $plugin_path ] = array(
319 'name' => $plugins[ $plugin_path ]['Name'],
320 'new_version' => $new_version,
321 );
322 }
323 }
324 }
325
326 return $notice_plugins;
327 }
328
329 /**
330 * テーマ 通知済 バージョン保存
331 *
332 * @param array $versions
333 * @return void
334 */
335 private function save_last_notice_theme_versions( array $versions ): void {
336 $settings = $this->get_settings();
337 $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_THEMES ] = array_merge( $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_THEMES ], $versions );
338 $this->save_settings( $settings );
339 }
340
341 /**
342 * テーマ 更新バージョン取得
343 *
344 * @return array
345 */
346 private function get_update_theme_versions(): array {
347 do_action( 'wp_update_themes' );
348
349 $settings = $this->get_settings();
350 $res = get_site_transient( 'update_themes' );
351 $update_themes = $res->response ?? array();
352 $notice_themes = array();
353
354 if ( empty( $update_themes ) ) {
355 $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_THEMES ] = $notice_themes;
356 $this->save_settings( $settings );
357 return $notice_themes;
358 }
359
360 $last_notice_themes = $settings[ self::KEY_LAST_NOTICE ][ self::LAST_NOTICE_THEMES ] ?? array();
361 $active_theme = wp_get_theme();
362 $active_text_domain = $active_theme->get( 'TextDomain' );
363 $themes = wp_get_themes();
364
365 foreach ( $update_themes as $text_domain => $theme ) {
366 $new_version = '';
367
368 if ( $theme['new_version'] !== ( $last_notice_themes[ $text_domain ]['new_version'] ?? '' ) ) {
369
370 if ( self::THEME_VALUES[2] === (int) $settings[ self::KEY_THEME ] ) {
371 if ( $text_domain === $active_text_domain ) {
372 $new_version = $theme['new_version'];
373 }
374 } else {
375 $new_version = $theme['new_version'];
376 }
377
378 if ( '' !== $new_version && array_key_exists( $text_domain, $themes ) ) {
379 $notice_themes[ $text_domain ] = array(
380 'name' => $themes[ $text_domain ]->get( 'Name' ),
381 'new_version' => $new_version,
382 );
383 }
384 }
385 }
386
387 return $notice_themes;
388 }
389
390 /**
391 * cron 設定
392 */
393 public function set_cron(): void {
394 if ( false === wp_get_scheduled_event( self::KEY_CRON ) ) {
395 wp_schedule_event( time(), 'daily', self::KEY_CRON );
396 }
397 }
398
399 /**
400 * cron 削除
401 *
402 * @return void
403 */
404 public function remove_cron(): void {
405 wp_clear_scheduled_hook( self::KEY_CRON );
406 }
407
408 /**
409 * 有効化
410 *
411 * @return void
412 */
413 public function activate(): void {
414 $settings = $this->get_default();
415 $this->save_settings( $settings );
416
417 if ( 't' === $settings[ self::KEY_FEATURE ] ) {
418 $this->set_cron();
419 } else {
420 $this->remove_cron();
421 }
422 }
423
424 /**
425 * 無効化
426 *
427 * @return void
428 */
429 public function deactivate(): void {
430 $this->remove_cron();
431 $settings = $this->get_settings();
432 $settings[ self::KEY_FEATURE ] = 'f';
433 $settings[ self::KEY_LAST_NOTICE ] = $this->get_last_notice_default();
434 $this->save_settings( $settings );
435 }
436 }
437