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