PluginProbe
Plugins Condition & Site Check / 2.00
Plugins Condition & Site Check v2.00
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.00, at lib/class-pluginscondition.php

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