PluginProbe
Plugins Condition & Site Check / 1.09
Plugins Condition & Site Check v1.09
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 1.09, at lib/class-pluginscondition.php

502 lines 15.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
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 if ( function_exists( 'wp_date' ) ) {
84 $current = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
85 } else {
86 $current = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
87 }
88
89 update_option( 'plg_cond_update_date_time', 'Plugins Condition ' . __( 'Last updated', 'plugins-condition' ) . ' : ' . $current );
90
91 return $links;
92 }
93
94 /** ==================================================
95 * Plugins Condition wp cron for dashboard
96 *
97 * @since 1.03
98 */
99 public function plugins_condition_wp_cron() {
100
101 if ( ! function_exists( 'get_plugins' ) ) {
102 require_once ABSPATH . 'wp-admin/includes/plugin.php';
103 }
104
105 $plugins = get_plugins();
106 foreach ( $plugins as $file => $plugin ) {
107 list( $html_ver, $html_date ) = $this->main_func( $file, false );
108 }
109
110 if ( function_exists( 'wp_date' ) ) {
111 $current = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
112 } else {
113 $current = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
114 }
115
116 update_option( 'plg_cond_update_date_time', 'Plugins Condition ' . __( 'Last updated', 'plugins-condition' ) . ' : ' . $current );
117 }
118
119 /** ==================================================
120 * Cron Start
121 *
122 * @return bool $result fail >> false | success >> void
123 * @since 1.03
124 */
125 public function cron_start() {
126
127 if ( ! wp_next_scheduled( 'plugins_condition_cron' ) ) {
128 $result = wp_schedule_event( time() + 86400, 'daily', 'plugins_condition_cron' );
129 } else {
130 wp_clear_scheduled_hook( 'plugins_condition_cron' );
131 $result = wp_schedule_event( time() + 86400, 'daily', 'plugins_condition_cron' );
132 }
133
134 return $result;
135 }
136
137 /** ==================================================
138 * Cron Stop
139 *
140 * @since 1.03
141 */
142 public function cron_stop() {
143
144 wp_clear_scheduled_hook( 'plugins_condition_cron' );
145 }
146
147 /** ==================================================
148 * Plugins Condition wp cron for notify mail
149 *
150 * @since 1.04
151 */
152 public function plugins_condition_notify_wp_cron() {
153
154 $adminmail = get_option( 'admin_email' );
155 $subject = 'Plugins Condition - ' . get_option( 'blogname' );
156
157 $plcaution = $this->plugins_condition_caution();
158 /* translators: interval days */
159 $content = '<blockquote>' . sprintf( __( 'This email is delivered every %1$s days to the administrator by Plugins Condition.', 'plugins-condition' ), get_option( 'plg_cond_notify_interval', 30 ) ) . '</blockquote>';
160 $content .= '<strong>' . get_option( 'plg_cond_update_date_time' ) . '</strong>' . $plcaution;
161
162 /* Mail Use HTML-Mails */
163 add_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
164
165 wp_mail( $adminmail, $subject, $content );
166
167 /* Mail default */
168 remove_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );
169 }
170
171 /** ==================================================
172 * Mail content type
173 *
174 * @return string 'text/html'
175 * @since 1.04
176 */
177 public function set_html_content_type() {
178 return 'text/html';
179 }
180
181 /** ==================================================
182 * Custom Intervals
183 *
184 * @param string $schedules schedules.
185 * @return array $schedules
186 * @since 1.04
187 */
188 public function plugins_condition_add_intervals( $schedules ) {
189
190 $notify_interval_int = get_option( 'plg_cond_notify_interval', 30 ) * 86400;
191 $notify_interval_str = 'plc_' . strval( get_option( 'plg_cond_notify_interval', 30 ) ) . 'days';
192
193 $schedules[ $notify_interval_str ] = array(
194 'interval' => $notify_interval_int,
195 'display' => $notify_interval_str,
196 );
197
198 return $schedules;
199 }
200
201 /** ==================================================
202 * Notify Cron Start
203 *
204 * @return bool $result fail >> false | success >> void
205 * @since 1.04
206 */
207 public function notify_cron_start() {
208
209 $notify_interval_int = get_option( 'plg_cond_notify_interval', 30 ) * 86400;
210 $notify_interval_str = 'plc_' . strval( get_option( 'plg_cond_notify_interval', 30 ) ) . 'days';
211
212 if ( ! wp_next_scheduled( 'plugins_condition_notify_cron' ) ) {
213 $result = wp_schedule_event( time() + $notify_interval_int, $notify_interval_str, 'plugins_condition_notify_cron' );
214 } else {
215 wp_clear_scheduled_hook( 'plugins_condition_notify_cron' );
216 $result = wp_schedule_event( time() + $notify_interval_int, $notify_interval_str, 'plugins_condition_notify_cron' );
217 }
218
219 return $result;
220 }
221
222 /** ==================================================
223 * Notify Cron Stop
224 *
225 * @since 1.04
226 */
227 public function notify_cron_stop() {
228
229 wp_clear_scheduled_hook( 'plugins_condition_notify_cron' );
230 }
231
232 /** ==================================================
233 * Main
234 *
235 * @param string $file file.
236 * @param bool $installed_plugin installed_plugin.
237 * @return array $html_ver, $html_date
238 * @since 1.00
239 */
240 private function main_func( $file, $installed_plugin ) {
241
242 $plugin_name = wp_basename( $file );
243 $slug = untrailingslashit( str_replace( $plugin_name, '', $file ) );
244
245 $call_apis = array();
246 if ( ! empty( $slug ) ) {
247 if ( $installed_plugin && get_transient( 'plg_cond_datas_' . $slug . '_' . get_locale() ) ) {
248 /* Get cache */
249 $call_apis = get_transient( 'plg_cond_datas_' . $slug . '_' . get_locale() );
250 } else {
251 if ( ! function_exists( 'plugins_api' ) ) {
252 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
253 }
254 /* Call API */
255 $call_api = plugins_api(
256 'plugin_information',
257 array(
258 'slug' => $slug,
259 'fields' => array(
260 'short_description' => false,
261 'description' => false,
262 'sections' => false,
263 'tested' => true,
264 'requires' => false,
265 'rating' => false,
266 'ratings' => false,
267 'downloaded' => false,
268 'downloadlink' => false,
269 'last_updated' => true,
270 'added' => false,
271 'tags' => false,
272 'compatibility' => false,
273 'homepage' => false,
274 'versions' => false,
275 'donate_link' => false,
276 'reviews' => false,
277 'banners' => false,
278 'icons' => false,
279 'active_installs' => false,
280 'group' => false,
281 'contributors' => false,
282 ),
283 )
284 );
285 if ( is_wp_error( $call_api ) ) {
286 $dummy = 0; /* Skip */
287 } else {
288 $call_apis = array(
289 'tested' => $call_api->tested,
290 'last_updated' => $call_api->last_updated,
291 );
292
293 /* Set cache */
294 set_transient( 'plg_cond_datas_' . $slug . '_' . get_locale(), $call_apis, 86400 );
295 }
296 }
297 }
298
299 $html_ver = null;
300 $html_date = null;
301 $caution = null;
302 if ( ! empty( $call_apis ) ) {
303 $wp_ver = get_bloginfo( 'version' );
304 $pg_ver = $call_apis['tested'];
305 if ( $pg_ver === $wp_ver ) {
306 $html_ver = '<span style="color: green;">' . $pg_ver . '</span>';
307 } else {
308 $html_ver = '<span style="color: red;">' . $pg_ver . '</span>';
309 $caution .= '<span style="color: red;">' . $pg_ver . '</span>';
310 }
311 $pg_update_time = strtotime( $call_apis['last_updated'] );
312 $now = time();
313 $time_lag = $now - $pg_update_time;
314 $time_lag_date = $time_lag / 86400;
315 if ( 1 > $time_lag_date ) {
316 $color_date = 'green';
317 $pg_date = __( 'within 24 hours', 'plugins-condition' );
318 } else if ( 1 <= $time_lag_date && 7 > $time_lag_date ) {
319 $color_date = 'green';
320 $day = floor( $time_lag_date );
321 if ( 1 == $day ) {
322 /* translators: day */
323 $pg_date = sprintf( __( '%1$d day ago', 'plugins-condition' ), 1 );
324 } else {
325 /* translators: days */
326 $pg_date = sprintf( __( '%1$d days ago', 'plugins-condition' ), $day );
327 }
328 } else if ( 7 <= $time_lag_date && 30 > $time_lag_date ) {
329 $color_date = 'green';
330 $week = floor( $time_lag_date / 7 );
331 if ( 1 == $week ) {
332 /* translators: week */
333 $pg_date = sprintf( __( '%1$d week ago', 'plugins-condition' ), 1 );
334 } else {
335 /* translators: weeks */
336 $pg_date = sprintf( __( '%1$d weeks ago', 'plugins-condition' ), $week );
337 }
338 } else if ( 30 <= $time_lag_date && 365 > $time_lag_date ) {
339 $color_date = 'green';
340 $month = floor( $time_lag_date / 30 );
341 if ( 1 == $month ) {
342 /* translators: month */
343 $pg_date = sprintf( __( '%1$d month ago', 'plugins-condition' ), 1 );
344 } else {
345 /* translators: months */
346 $pg_date = sprintf( __( '%1$d months ago', 'plugins-condition' ), $month );
347 }
348 } else {
349 $color_date = 'red';
350 $year = floor( $time_lag_date / 365 );
351 if ( 1 == $year ) {
352 /* translators: year */
353 $pg_date = sprintf( __( '%1$d year ago', 'plugins-condition' ), 1 );
354 } else {
355 /* translators: years */
356 $pg_date = sprintf( __( '%1$d years ago', 'plugins-condition' ), $year );
357 }
358 $caution .= ' <span style="color: red;">' . $pg_date . '</span>';
359 }
360 $html_date = '<span style="color: ' . $color_date . ';">' . $pg_date . '</span>';
361 } else {
362 $html_ver = '<span style="color: red;">' . __( 'unofficial', 'plugins-condition' ) . '</span>';
363 $caution .= $html_ver;
364 }
365
366 $pg_name = $this->plugin_name( $file );
367 if ( $caution ) {
368 $text = '<div>' . $pg_name . ' : ' . $caution . '</div>';
369 update_option( 'plg_cond_text_' . $pg_name, $text );
370 } else {
371 delete_option( 'plg_cond_text_' . $pg_name );
372 }
373
374 return array( $html_ver, $html_date );
375 }
376
377 /** ==================================================
378 * Plugins Condition for Dashboard
379 *
380 * @since 1.00
381 */
382 public function plugins_condition_dashboard_widgets() {
383
384 if ( current_user_can( 'manage_options' ) ) {
385 global $wp_meta_boxes;
386 wp_add_dashboard_widget( 'custom_help_widget', get_option( 'plg_cond_update_date_time' ), array( $this, 'dashboard_text' ) );
387 }
388 }
389
390 /** ==================================================
391 * Dashboard text
392 *
393 * @since 1.00
394 */
395 public function dashboard_text() {
396
397 $screen = get_current_screen();
398 if ( 'dashboard' === $screen->id ) {
399
400 $plcaution = $this->plugins_condition_caution();
401
402 if ( is_multisite() ) {
403 $installed_plugin_url = network_admin_url( 'plugins.php' );
404 } else {
405 $installed_plugin_url = admin_url( 'plugins.php' );
406 }
407 $installed_plugin_html = '<a href="' . $installed_plugin_url . '" style="text-decoration: none; word-break: break-all;">' . __( 'Installed Plugins', 'plugins-condition' ) . '</a>';
408
409 ?>
410 <h3>
411 <?php
412 /* translators: %s: Installed Plugins */
413 echo wp_kses_post( sprintf( __( 'Please read %s for the latest information.', 'plugins-condition' ), $installed_plugin_html ) );
414 ?>
415 </h3>
416 <?php
417 echo wp_kses_post( $plcaution );
418 }
419 }
420
421 /** ==================================================
422 * Plugins Condition Caution
423 *
424 * @return string $plcaution plcaution.
425 * @since 1.04
426 */
427 private function plugins_condition_caution() {
428
429 global $wpdb;
430 $option_names = array();
431 $wp_options = $wpdb->get_results(
432 "
433 SELECT option_name
434 FROM {$wpdb->prefix}options
435 WHERE option_name LIKE '%%plg_cond_text_%%'
436 "
437 );
438
439 if ( ! function_exists( 'get_plugins' ) ) {
440 require_once ABSPATH . 'wp-admin/includes/plugin.php';
441 }
442 $plugins = get_plugins();
443 $plcaution = null;
444 if ( ! empty( $wp_options ) ) {
445 foreach ( $wp_options as $wp_option ) {
446 $option_names[] = $wp_option->option_name;
447 }
448 foreach ( $option_names as $option_name ) {
449 $pl_name = str_replace( 'plg_cond_text_', '', $option_name );
450 $is_plg = false;
451 foreach ( $plugins as $file => $plugin ) {
452 if ( $pl_name === $plugin['Name'] ) {
453 $is_plg = true;
454 }
455 }
456 if ( $is_plg ) {
457 $plcaution .= get_option( $option_name );
458 } else {
459 delete_option( $option_name );
460 }
461 }
462 }
463
464 return $plcaution;
465 }
466
467 /** ==================================================
468 * Plugin Name
469 *
470 * @param string $file file.
471 * @return string $plugin_name plugin_name.
472 * @since 1.00
473 */
474 private function plugin_name( $file ) {
475
476 $this_plugin_path = untrailingslashit( wp_normalize_path( plugin_dir_path( __DIR__ ) ) );
477 $this_plugin_name = wp_basename( $this_plugin_path );
478 $plugin_path = str_replace( $this_plugin_name, '', $this_plugin_path );
479
480 $plugin_datas = get_file_data(
481 $plugin_path . $file,
482 array(
483 'name' => 'Plugin Name',
484 'version' => 'Version',
485 )
486 );
487
488 $plugin_name = null;
489 $plugin_ver_num = null;
490 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) &&
491 array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
492 $plugin_name = $plugin_datas['name'];
493 $plugin_ver_num = $plugin_datas['version'];
494 $plugin_version = __( 'Version:', 'plugins-condition' ) . ' ' . $plugin_ver_num;
495 }
496
497 return $plugin_name;
498 }
499 }
500
501
502