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

300 lines 8.8 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) 2016- 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 $pluginscondition = new PluginsCondition();
24
25 /** ==================================================
26 * Main Functions
27 */
28 class PluginsCondition {
29
30 /** ==================================================
31 * Construct
32 *
33 * @since 1.00
34 */
35 public function __construct() {
36
37 add_filter( 'plugin_row_meta', array( $this, 'main_func' ), 10, 2 );
38 add_action( 'wp_dashboard_setup', array( $this, 'plugins_condition_dashboard_widgets' ) );
39
40 }
41
42 /** ==================================================
43 * Main
44 *
45 * @param array $links links.
46 * @param string $file file.
47 * @return array $links links.
48 * @since 1.00
49 */
50 public function main_func( $links, $file ) {
51
52 $slugs = explode( DIRECTORY_SEPARATOR, $file );
53 $slug = $slugs[0];
54 $call_apis = array();
55 if ( ! empty( $slug ) ) {
56 if ( get_transient( 'plg_cond_datas_' . $slug . '_' . get_locale() ) ) {
57 /* Get cache */
58 $call_apis = get_transient( 'plg_cond_datas_' . $slug . '_' . get_locale() );
59 } else {
60 if ( ! function_exists( 'plugins_api' ) ) {
61 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
62 }
63 /* Call API */
64 $call_api = plugins_api(
65 'plugin_information',
66 array(
67 'slug' => $slug,
68 'fields' => array(
69 'short_description' => false,
70 'description' => false,
71 'sections' => false,
72 'tested' => true,
73 'requires' => false,
74 'rating' => false,
75 'ratings' => false,
76 'downloaded' => false,
77 'downloadlink' => false,
78 'last_updated' => true,
79 'added' => false,
80 'tags' => false,
81 'compatibility' => false,
82 'homepage' => false,
83 'versions' => false,
84 'donate_link' => false,
85 'reviews' => false,
86 'banners' => false,
87 'icons' => false,
88 'active_installs' => false,
89 'group' => false,
90 'contributors' => false,
91 ),
92 )
93 );
94 if ( is_wp_error( $call_api ) ) {
95 $dummy = 0; /* Skip */
96 } else {
97 $call_apis = array(
98 'tested' => $call_api->tested,
99 'last_updated' => $call_api->last_updated,
100 );
101
102 /* Set cache */
103 set_transient( 'plg_cond_datas_' . $slug . '_' . get_locale(), $call_apis, 86400 );
104 }
105 }
106 }
107
108 $caution = null;
109 if ( ! empty( $call_apis ) ) {
110 $wp_ver = get_bloginfo( 'version' );
111 $pg_ver = $call_apis['tested'];
112 if ( $pg_ver === $wp_ver ) {
113 $links[] .= '<span style="color: green;">' . $pg_ver . '</span>';
114 } else {
115 $links[] .= '<span style="color: red;">' . $pg_ver . '</span>';
116 $caution .= '<span style="color: red;">' . $pg_ver . '</span>';
117 }
118 $pg_update_time = strtotime( $call_apis['last_updated'] );
119 $now = time();
120 $time_lag = $now - $pg_update_time;
121 $time_lag_date = $time_lag / 86400;
122 if ( 1 > $time_lag_date ) {
123 $color_date = 'green';
124 $pg_date = __( 'within 24 hours', 'plugins-condition' );
125 } else if ( 1 <= $time_lag_date && 7 > $time_lag_date ) {
126 $color_date = 'green';
127 $day = floor( $time_lag_date );
128 if ( 1 == $day ) {
129 /* translators: day */
130 $pg_date = sprintf( __( '%1$d day ago', 'plugins-condition' ), 1 );
131 } else {
132 /* translators: days */
133 $pg_date = sprintf( __( '%1$d days ago', 'plugins-condition' ), $day );
134 }
135 } else if ( 7 <= $time_lag_date && 30 > $time_lag_date ) {
136 $color_date = 'green';
137 $week = floor( $time_lag_date / 7 );
138 if ( 1 == $week ) {
139 /* translators: week */
140 $pg_date = sprintf( __( '%1$d week ago', 'plugins-condition' ), 1 );
141 } else {
142 /* translators: weeks */
143 $pg_date = sprintf( __( '%1$d weeks ago', 'plugins-condition' ), $week );
144 }
145 } else if ( 30 <= $time_lag_date && 365 > $time_lag_date ) {
146 $color_date = 'green';
147 $month = floor( $time_lag_date / 30 );
148 if ( 1 == $month ) {
149 /* translators: month */
150 $pg_date = sprintf( __( '%1$d month ago', 'plugins-condition' ), 1 );
151 } else {
152 /* translators: months */
153 $pg_date = sprintf( __( '%1$d months ago', 'plugins-condition' ), $month );
154 }
155 } else {
156 $color_date = 'red';
157 $year = floor( $time_lag_date / 365 );
158 if ( 1 == $year ) {
159 /* translators: year */
160 $pg_date = sprintf( __( '%1$d year ago', 'plugins-condition' ), 1 );
161 } else {
162 /* translators: years */
163 $pg_date = sprintf( __( '%1$d years ago', 'plugins-condition' ), $year );
164 }
165 $caution .= ' <span style="color: red;">' . $pg_date . '</span>';
166 }
167 $links[] .= '<span style="color: ' . $color_date . ';">' . $pg_date . '</span>';
168 } else {
169 $links[] .= '<span style="color: red;">' . __( 'unofficial', 'plugins-condition' ) . '</span>';
170 $caution .= '<span style="color: red;">' . __( 'unofficial', 'plugins-condition' ) . '</span>';
171 }
172
173 $pg_name = $this->plugin_name( $file );
174 if ( $caution ) {
175 $text = '<div>' . $pg_name . ' : ' . $caution . '</div>';
176 update_option( 'plg_cond_text_' . $pg_name, $text );
177 } else {
178 delete_option( 'plg_cond_text_' . $pg_name );
179 }
180
181 return $links;
182
183 }
184
185 /** ==================================================
186 * Plugins Condition for Dashboard
187 *
188 * @since 1.00
189 */
190 public function plugins_condition_dashboard_widgets() {
191
192 if ( current_user_can( 'administrator' ) ) {
193 global $wp_meta_boxes;
194 wp_add_dashboard_widget( 'custom_help_widget', 'Plugins Condition ' . __( 'Status' ), array( $this, 'dashboard_text' ) );
195 }
196
197 }
198
199 /** ==================================================
200 * Dashboard text
201 *
202 * @since 1.00
203 */
204 public function dashboard_text() {
205
206 $screen = get_current_screen();
207 if ( 'dashboard' === $screen->id ) {
208 global $wpdb;
209 $option_names = array();
210 $wp_options = $wpdb->get_results(
211 "
212 SELECT option_name
213 FROM $wpdb->options
214 WHERE option_name LIKE '%%plg_cond_text_%%'
215 "
216 );
217
218 if ( ! function_exists( 'get_plugins' ) ) {
219 require_once ABSPATH . 'wp-admin/includes/plugin.php';
220 }
221 $plugins = get_plugins();
222 $plcaution = null;
223 if ( ! empty( $wp_options ) ) {
224 foreach ( $wp_options as $wp_option ) {
225 $option_names[] = $wp_option->option_name;
226 }
227 foreach ( $option_names as $option_name ) {
228 $pl_name = str_replace( 'plg_cond_text_', '', $option_name );
229 $is_plg = false;
230 foreach ( $plugins as $path => $plugin ) {
231 if ( $pl_name === $plugin['Name'] ) {
232 $is_plg = true;
233 }
234 }
235 if ( $is_plg ) {
236 $plcaution .= get_option( $option_name );
237 } else {
238 delete_option( $option_name );
239 }
240 }
241 }
242
243 if ( is_multisite() ) {
244 $installed_plugin_url = network_admin_url( 'plugins.php' );
245 } else {
246 $installed_plugin_url = admin_url( 'plugins.php' );
247 }
248 $installed_plugin_html = '<a href="' . $installed_plugin_url . '" style="text-decoration: none; word-break: break-all;">' . __( 'Installed Plugins' ) . '</a>';
249
250 ?>
251 <h3>
252 <?php
253 /* translators: %s: Installed Plugins */
254 echo wp_kses_post( sprintf( __( 'Please read %s for the latest information.', 'plugins-condition' ), $installed_plugin_html ) );
255 ?>
256 </h3>
257 <?php
258 echo wp_kses_post( $plcaution );
259 }
260
261 }
262
263 /** ==================================================
264 * Plugin Name
265 *
266 * @param string $file file.
267 * @return string $plugin_name plugin_name.
268 * @since 1.00
269 */
270 private function plugin_name( $file ) {
271
272 $plugin_path = untrailingslashit( plugin_dir_path( __DIR__ ) );
273 $plugin_paths = explode( DIRECTORY_SEPARATOR, $plugin_path );
274 $str_length = strlen( end( $plugin_paths ) );
275 $plugin_path = substr( $plugin_path, 0, -$str_length );
276
277 $plugin_datas = get_file_data(
278 $plugin_path . $file,
279 array(
280 'name' => 'Plugin Name',
281 'version' => 'Version',
282 )
283 );
284
285 $plugin_name = null;
286 $plugin_ver_num = null;
287 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
288 $plugin_name = $plugin_datas['name'];
289 $plugin_ver_num = $plugin_datas['version'];
290 $plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num;
291 }
292
293 return $plugin_name;
294
295 }
296
297 }
298
299
300