PluginProbe
Plugins Condition & Site Check / 2.01
Plugins Condition & Site Check v2.01
2.01 trunk 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 2.00
plugins-condition / lib / class-pluginscondition.php

class-pluginscondition.php in Plugins Condition & Site Check 2.01, at lib/class-pluginscondition.php

601 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugins Condition & Site Check
4 *
5 * @package Plugins Condition
6 * @subpackage PluginsCondition Main Functions
7 /*
8 Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 if ( ! defined( 'ABSPATH' ) ) {
24 exit;
25 }
26
27 $pluginscondition = new PluginsCondition();
28
29 /** ==================================================
30 * Main Functions
31 */
32 class PluginsCondition {
33
34 /** ==================================================
35 * Construct
36 *
37 * @since 1.00
38 */
39 public function __construct() {
40
41 add_filter( 'plugin_row_meta', array( $this, 'installed_plugins' ), 10, 2 );
42 add_action( 'wp_dashboard_setup', array( $this, 'plugins_condition_dashboard_widgets' ) );
43
44 add_action( 'plugins_condition_cron', array( $this, 'plugins_condition_wp_cron' ) );
45 register_activation_hook( plugin_dir_path( __DIR__ ) . 'pluginscondition.php', array( $this, 'cron_start' ) );
46 register_deactivation_hook( plugin_dir_path( __DIR__ ) . 'pluginscondition.php', array( $this, 'cron_stop' ) );
47
48 add_filter( 'cron_schedules', array( $this, 'plugins_condition_add_intervals' ) );
49 add_action( 'plugins_condition_notify_cron', array( $this, 'plugins_condition_notify_wp_cron' ) );
50 add_action( 'plugins_condition_notify_cron_start', array( $this, 'notify_cron_start' ) );
51 add_action( 'plugins_condition_notify_cron_stop', array( $this, 'notify_cron_stop' ) );
52 add_action( 'plugins_condition_notify_cron_now', array( $this, 'single_cron_run' ) );
53 register_activation_hook( plugin_dir_path( __DIR__ ) . 'pluginscondition.php', array( $this, 'notify_cron_start' ) );
54 register_deactivation_hook( plugin_dir_path( __DIR__ ) . 'pluginscondition.php', array( $this, 'notify_cron_stop' ) );
55 }
56
57 /** ==================================================
58 * View Installed Plugins
59 *
60 * @param array $links links.
61 * @param string $file file.
62 * @return array $links links.
63 * @since 1.00
64 */
65 public function installed_plugins( $links, $file ) {
66
67 list( $html_ver, $html_date ) = $this->main_func( $file, true );
68
69 $links[] .= $html_ver;
70 if ( ! empty( $html_date ) ) {
71 $links[] .= $html_date;
72 }
73
74 if ( ! wp_next_scheduled( 'plugins_condition_cron' ) ) {
75 wp_schedule_event( time() + 86400, 'daily', 'plugins_condition_cron' );
76 }
77
78 if ( ! wp_next_scheduled( 'plugins_condition_notify_cron' ) ) {
79 $notify_interval_int = get_option( 'plg_cond_notify_interval', 30 ) * 86400;
80 $notify_interval_str = 'plc_' . strval( get_option( 'plg_cond_notify_interval', 30 ) ) . 'days';
81 wp_schedule_event( time() + $notify_interval_int, $notify_interval_str, 'plugins_condition_notify_cron' );
82 }
83
84 $current = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
85
86 update_option( 'plg_cond_update_date_time', 'Plugins Condition & Site Check ' . __( 'Last updated', 'plugins-condition' ) . ' : ' . $current );
87
88 return $links;
89 }
90
91 /** ==================================================
92 * Plugins Condition wp cron for dashboard
93 *
94 * @since 1.03
95 */
96 public function plugins_condition_wp_cron() {
97
98 if ( ! function_exists( 'get_plugins' ) ) {
99 require_once ABSPATH . 'wp-admin/includes/plugin.php';
100 }
101
102 $plugins = get_plugins();
103 foreach ( $plugins as $file => $plugin ) {
104 list( $html_ver, $html_date ) = $this->main_func( $file, false );
105 }
106
107 $current = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
108
109 update_option( 'plg_cond_update_date_time', 'Plugins Condition & Site Check ' . __( 'Last updated', 'plugins-condition' ) . ' : ' . $current );
110 }
111
112 /** ==================================================
113 * Cron Start
114 *
115 * @return bool $result fail >> false | success >> void
116 * @since 1.03
117 */
118 public function cron_start() {
119
120 if ( ! wp_next_scheduled( 'plugins_condition_cron' ) ) {
121 $result = wp_schedule_event( time() + 86400, 'daily', 'plugins_condition_cron' );
122 } else {
123 wp_clear_scheduled_hook( 'plugins_condition_cron' );
124 $result = wp_schedule_event( time() + 86400, 'daily', 'plugins_condition_cron' );
125 }
126
127 return $result;
128 }
129
130 /** ==================================================
131 * Cron Stop
132 *
133 * @since 1.03
134 */
135 public function cron_stop() {
136
137 wp_clear_scheduled_hook( 'plugins_condition_cron' );
138 }
139
140 /** ==================================================
141 * Plugins Condition wp cron for notify mail
142 *
143 * @since 1.04
144 */
145 public function plugins_condition_notify_wp_cron() {
146
147 $adminmail = get_option( 'admin_email' );
148 $subject = 'Plugins Condition & Site Check - ' . get_option( 'blogname' );
149
150 $plcaution = $this->plugins_condition_caution();
151 /* translators: interval days */
152 $content = '<blockquote>' . sprintf( __( 'This email is delivered every %1$s days to the administrator by Plugins Condition & Site Check.', 'plugins-condition' ), get_option( 'plg_cond_notify_interval', 30 ) ) . '</blockquote>';
153 $content .= '<strong>' . get_option( 'plg_cond_update_date_time' ) . '</strong>' . $plcaution;
154
155 /* Mail Use HTML-Mails */
156 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
157
158 wp_mail( $adminmail, $subject, $content );
159
160 /* Mail default */
161 remove_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
162 }
163
164 /** ==================================================
165 * Mail content type
166 *
167 * @return string 'text/html'
168 * @since 1.04
169 */
170 public function set_html_content_type() {
171 return 'text/html';
172 }
173
174 /** ==================================================
175 * Custom Intervals
176 *
177 * @param string $schedules schedules.
178 * @return array $schedules
179 * @since 1.04
180 */
181 public function plugins_condition_add_intervals( $schedules ) {
182
183 $notify_interval_int = get_option( 'plg_cond_notify_interval', 30 ) * 86400;
184 $notify_interval_str = 'plc_' . strval( get_option( 'plg_cond_notify_interval', 30 ) ) . 'days';
185
186 $schedules[ $notify_interval_str ] = array(
187 'interval' => $notify_interval_int,
188 'display' => $notify_interval_str,
189 );
190
191 return $schedules;
192 }
193
194 /** ==================================================
195 * Notify Cron Start
196 *
197 * @return bool $result fail >> false | success >> void
198 * @since 1.04
199 */
200 public function notify_cron_start() {
201
202 $notify_interval_int = get_option( 'plg_cond_notify_interval', 30 ) * 86400;
203 $notify_interval_str = 'plc_' . strval( get_option( 'plg_cond_notify_interval', 30 ) ) . 'days';
204
205 if ( ! wp_next_scheduled( 'plugins_condition_notify_cron' ) ) {
206 $result = wp_schedule_event( time() + $notify_interval_int, $notify_interval_str, 'plugins_condition_notify_cron' );
207 } else {
208 wp_clear_scheduled_hook( 'plugins_condition_notify_cron' );
209 $result = wp_schedule_event( time() + $notify_interval_int, $notify_interval_str, 'plugins_condition_notify_cron' );
210 }
211
212 return $result;
213 }
214
215 /** ==================================================
216 * Notify Cron Stop
217 *
218 * @since 1.04
219 */
220 public function notify_cron_stop() {
221
222 wp_clear_scheduled_hook( 'plugins_condition_notify_cron' );
223 }
224
225 /** ==================================================
226 * Notify Single Cron Run
227 *
228 * @since 2.01
229 */
230 public function single_cron_run() {
231
232 wp_schedule_single_event(
233 time(),
234 'plugins_condition_notify_cron',
235 array( 'plc_now' ),
236 );
237
238 spawn_cron();
239 }
240
241 /** ==================================================
242 * Main
243 *
244 * @param string $file file.
245 * @param bool $installed_plugin installed_plugin.
246 * @return array $html_ver, $html_date
247 * @since 1.00
248 */
249 private function main_func( $file, $installed_plugin ) {
250
251 $plugin_name = wp_basename( $file );
252 $slug = untrailingslashit( str_replace( $plugin_name, '', $file ) );
253
254 $call_apis = array();
255 if ( ! empty( $slug ) ) {
256 if ( $installed_plugin && get_transient( 'plg_cond_datas_' . $slug . '_' . get_locale() ) ) {
257 /* Get cache */
258 $call_apis = get_transient( 'plg_cond_datas_' . $slug . '_' . get_locale() );
259 } else {
260 if ( ! function_exists( 'plugins_api' ) ) {
261 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
262 }
263 /* Call API */
264 $call_api = plugins_api(
265 'plugin_information',
266 array(
267 'slug' => $slug,
268 'fields' => array(
269 'short_description' => false,
270 'description' => false,
271 'sections' => false,
272 'tested' => true,
273 'requires' => false,
274 'rating' => false,
275 'ratings' => false,
276 'downloaded' => false,
277 'downloadlink' => false,
278 'last_updated' => true,
279 'added' => false,
280 'tags' => false,
281 'compatibility' => false,
282 'homepage' => false,
283 'versions' => false,
284 'donate_link' => false,
285 'reviews' => false,
286 'banners' => false,
287 'icons' => false,
288 'active_installs' => false,
289 'group' => false,
290 'contributors' => false,
291 ),
292 )
293 );
294 if ( is_wp_error( $call_api ) ) {
295 $dummy = 0; /* Skip */
296 } else {
297 $call_apis = array(
298 'tested' => $call_api->tested,
299 'last_updated' => $call_api->last_updated,
300 );
301
302 /* Set cache */
303 set_transient( 'plg_cond_datas_' . $slug . '_' . get_locale(), $call_apis, 86400 );
304 }
305 }
306 }
307
308 $html_ver = null;
309 $html_date = null;
310 $caution = null;
311 if ( ! empty( $call_apis ) ) {
312 $wp_ver = get_bloginfo( 'version' );
313 $pg_ver = $call_apis['tested'];
314 if ( $pg_ver === $wp_ver ) {
315 $html_ver = '<span style="color: green;">' . $pg_ver . '</span>';
316 } else {
317 $html_ver = '<span style="color: red;">' . $pg_ver . '</span>';
318 $caution .= '<span style="color: red;">' . $pg_ver . '</span>';
319 }
320 $pg_update_time = strtotime( $call_apis['last_updated'] );
321 $now = time();
322 $time_lag = $now - $pg_update_time;
323 $time_lag_date = $time_lag / 86400;
324 if ( 1 > $time_lag_date ) {
325 $color_date = 'green';
326 $pg_date = __( 'within 24 hours', 'plugins-condition' );
327 } else if ( 1 <= $time_lag_date && 7 > $time_lag_date ) {
328 $color_date = 'green';
329 $day = floor( $time_lag_date );
330 if ( 1 == $day ) {
331 /* translators: day */
332 $pg_date = sprintf( __( '%1$d day ago', 'plugins-condition' ), 1 );
333 } else {
334 /* translators: days */
335 $pg_date = sprintf( __( '%1$d days ago', 'plugins-condition' ), $day );
336 }
337 } else if ( 7 <= $time_lag_date && 30 > $time_lag_date ) {
338 $color_date = 'green';
339 $week = floor( $time_lag_date / 7 );
340 if ( 1 == $week ) {
341 /* translators: week */
342 $pg_date = sprintf( __( '%1$d week ago', 'plugins-condition' ), 1 );
343 } else {
344 /* translators: weeks */
345 $pg_date = sprintf( __( '%1$d weeks ago', 'plugins-condition' ), $week );
346 }
347 } else if ( 30 <= $time_lag_date && 365 > $time_lag_date ) {
348 $color_date = 'green';
349 $month = floor( $time_lag_date / 30 );
350 if ( 1 == $month ) {
351 /* translators: month */
352 $pg_date = sprintf( __( '%1$d month ago', 'plugins-condition' ), 1 );
353 } else {
354 /* translators: months */
355 $pg_date = sprintf( __( '%1$d months ago', 'plugins-condition' ), $month );
356 }
357 } else {
358 $color_date = 'red';
359 $year = floor( $time_lag_date / 365 );
360 if ( 1 == $year ) {
361 /* translators: year */
362 $pg_date = sprintf( __( '%1$d year ago', 'plugins-condition' ), 1 );
363 } else {
364 /* translators: years */
365 $pg_date = sprintf( __( '%1$d years ago', 'plugins-condition' ), $year );
366 }
367 $caution .= ' <span style="color: red;">' . $pg_date . '</span>';
368 }
369 $html_date = '<span style="color: ' . $color_date . ';">' . $pg_date . '</span>';
370 } else {
371 $html_ver = '<span style="color: red;">' . __( 'unofficial', 'plugins-condition' ) . '</span>';
372 $caution .= $html_ver;
373 }
374
375 $pg_name = $this->plugin_name( $file );
376 if ( $caution ) {
377 $text = '<div>' . $pg_name . ' : ' . $caution . '</div>';
378 update_option( 'plg_cond_text_' . $pg_name, $text );
379 } else {
380 delete_option( 'plg_cond_text_' . $pg_name );
381 }
382
383 return array( $html_ver, $html_date );
384 }
385
386 /** ==================================================
387 * Plugins Condition for Dashboard
388 *
389 * @since 1.00
390 */
391 public function plugins_condition_dashboard_widgets() {
392
393 if ( current_user_can( 'manage_options' ) ) {
394 global $wp_meta_boxes;
395 wp_add_dashboard_widget( 'custom_help_widget', get_option( 'plg_cond_update_date_time' ), array( $this, 'dashboard_text' ) );
396 }
397 }
398
399 /** ==================================================
400 * Dashboard text
401 *
402 * @since 1.00
403 */
404 public function dashboard_text() {
405
406 $screen = get_current_screen();
407 if ( 'dashboard' === $screen->id ) {
408
409 $plcaution = $this->plugins_condition_caution();
410
411 if ( is_multisite() ) {
412 $installed_plugin_url = network_admin_url( 'plugins.php' );
413 } else {
414 $installed_plugin_url = admin_url( 'plugins.php' );
415 }
416 $installed_plugin_html = '<a href="' . $installed_plugin_url . '" style="text-decoration: none; word-break: break-all;">' . __( 'Installed Plugins', 'plugins-condition' ) . '</a>';
417
418 ?>
419 <h3>
420 <?php
421 /* translators: %s: Installed Plugins */
422 echo wp_kses_post( sprintf( __( 'To update the "Plugin Information," please load your %s.', 'plugins-condition' ), $installed_plugin_html ) );
423 ?>
424 </h3>
425 <?php
426 echo wp_kses_post( $plcaution );
427 }
428 }
429
430 /** ==================================================
431 * Plugins Condition Caution
432 *
433 * @return string $plcaution plcaution.
434 * @since 1.04
435 */
436 private function plugins_condition_caution() {
437
438 global $wpdb;
439 $option_names = array();
440 $wp_options = $wpdb->get_results(
441 "
442 SELECT option_name
443 FROM {$wpdb->prefix}options
444 WHERE option_name LIKE '%%plg_cond_text_%%'
445 "
446 );
447
448 if ( ! function_exists( 'get_plugins' ) ) {
449 require_once ABSPATH . 'wp-admin/includes/plugin.php';
450 }
451 $plugins = get_plugins();
452 $plcaution = '<hr />';
453 $plcaution .= '<strong>' . __( 'Plugin Information', 'plugins-condition' ) . '</strong>';
454 ;
455 if ( ! empty( $wp_options ) ) {
456 foreach ( $wp_options as $wp_option ) {
457 $option_names[] = $wp_option->option_name;
458 }
459 foreach ( $option_names as $option_name ) {
460 $pl_name = str_replace( 'plg_cond_text_', '', $option_name );
461 $is_plg = false;
462 foreach ( $plugins as $file => $plugin ) {
463 if ( $pl_name === $plugin['Name'] ) {
464 $is_plg = true;
465 }
466 }
467 if ( $is_plg ) {
468 $plcaution .= get_option( $option_name );
469 } else {
470 delete_option( $option_name );
471 }
472 }
473 }
474
475 $plcaution .= $this->users_list();
476 $plcaution .= $this->wp_content_check();
477
478 return $plcaution;
479 }
480
481 /** ==================================================
482 * Plugin Name
483 *
484 * @param string $file file.
485 * @return string $plugin_name plugin_name.
486 * @since 1.00
487 */
488 private function plugin_name( $file ) {
489
490 $this_plugin_path = untrailingslashit( wp_normalize_path( plugin_dir_path( __DIR__ ) ) );
491 $this_plugin_name = wp_basename( $this_plugin_path );
492 $plugin_path = str_replace( $this_plugin_name, '', $this_plugin_path );
493
494 $plugin_datas = get_file_data(
495 $plugin_path . $file,
496 array(
497 'name' => 'Plugin Name',
498 'version' => 'Version',
499 )
500 );
501
502 $plugin_name = null;
503 $plugin_ver_num = null;
504 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) &&
505 array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
506 $plugin_name = $plugin_datas['name'];
507 $plugin_ver_num = $plugin_datas['version'];
508 $plugin_version = __( 'Version:', 'plugins-condition' ) . ' ' . $plugin_ver_num;
509 }
510
511 return $plugin_name;
512 }
513
514 /** ==================================================
515 * User's List
516 *
517 * @return string $html html for user's list.
518 * @since 2.00
519 */
520 private function users_list() {
521
522 $users = get_users();
523
524 global $wp_roles;
525
526 $html = '<hr />';
527 $html .= '<strong>' . __( 'User Information', 'plugins-condition' ) . '</strong>';
528 $html .= '<ul style="margin-top: 0;">';
529 foreach ( $users as $user ) {
530 /* Get roles */
531 $role_names = array();
532 if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
533 foreach ( $user->roles as $role_slug ) {
534 if ( isset( $wp_roles->roles[ $role_slug ] ) ) {
535 $role_names[] = translate_user_role( $wp_roles->roles[ $role_slug ]['name'] );
536 }
537 }
538 }
539 if ( ! empty( $role_names ) ) {
540 $display_roles = __( 'Role', 'plugins-condition' ) . ' : ' . implode( ', ', $role_names );
541 } else {
542 $display_roles = __( 'No role', 'plugins-condition' );
543 }
544
545 /* Get regiterd date time */
546 $registered_time = wp_date( 'Y-n-j H:i', strtotime( $user->user_registered ) );
547
548 $html.= '<li>' . esc_html( $user->user_login ) . ' (' . esc_html( $user->user_email ) . ')</li><ul style="list-style-type: disc; padding-left: 20px;"><li>' . esc_html( $display_roles ) . '</li><li>' . __( 'Date and Time of Registration', 'plugins-condition' ) . ' : ' . esc_html( $registered_time ) . '</li></ul>';
549 }
550 $html.= '</ul>';
551
552 return $html;
553 }
554
555 /** ==================================================
556 * Check list for wp-content
557 *
558 * @return string $html html for wp-content check list.
559 * @since 2.00
560 */
561 private function wp_content_check() {
562
563 $upload_dir = wp_upload_dir();
564
565 $filetype = wp_check_filetype( $wp_content_dir . '/index.php' );
566 $file_size = size_format( filesize( $file_path ) );
567 $modified_date = wp_date( 'Y-m-d H:i:s', filemtime( $file_path ) );
568
569 $content_php_files = glob( WP_CONTENT_DIR . '/*.php' );
570 $uploads_php_files = glob( $upload_dir['basedir'] . '/*.php');
571
572 $all_php_files = array_merge(
573 $content_php_files ?: [],
574 $uploads_php_files ?: []
575 );
576
577 $html = '<hr />';
578 $html .= '<strong>' . __( 'wp-content Information', 'plugins-condition' ) . '</strong>';
579 $html .= '<ul style="margin-top: 0;">';
580
581 if ( ! empty( $all_php_files ) ) {
582 foreach ( $all_php_files as $file_path ) {
583 if ( str_contains( $file_path, 'wp-content/uploads' ) ) {
584 $folder = 'wp-content/uploads';
585 } else {
586 $folder = 'wp-content';
587 }
588 $filename = wp_basename( $file_path );
589 $file_size = size_format( filesize( $file_path ) );
590 $modified_date = wp_date( 'Y-m-d H:i:s', filemtime( $file_path ) );
591 $html .= '<li>' . esc_html( $folder . '/' . $filename ) . '</li><ul style="list-style-type: disc; padding-left: 20px;"><li>' . esc_html__( 'Last updated', 'plugins-condition' ) . ' : ' . $modified_date . '</li><li>' . esc_html__( 'Size', 'plugins-condition' ) . ' : ' . $file_size . '</li></ul>';
592 }
593 } else {
594 $html .= esc_html__( 'No PHP files were found in the "wp-content" or "wp-content/uploads" directories.', 'plugins-condition' );
595 }
596 $html.= '</ul>';
597
598 return $html;
599 }
600 }
601