PluginProbe
Datafeedr API / 1.4.3
Datafeedr API v1.4.3
1.4.3 1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 All 182 releases
datafeedr-api / classes / class-datafeedr-plugin-dependency.php

class-datafeedr-plugin-dependency.php in Datafeedr API 1.4.3, at classes/class-datafeedr-plugin-dependency.php

471 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5
6
7 /**
8 * Class Dfrapi_Plugin_Dependency
9 *
10 * USAGE:
11 * ++++++++++++++++++++++++++++++++++++++++
12 * $dependency = new Dfrapi_Plugin_Dependency( 'WooCommerce', 'woocommerce/woocommerce.php', '3.2.0' );
13 * $action = $dependency->action_required();
14 * if ( $action ) {
15 * echo '<div class="notice notice-error"><p>';
16 * echo $dependency->msg( 'Datafeedr API' );
17 * echo $dependency->link();
18 * echo '</p></div>';
19 * }
20 */
21 class Dfrapi_Plugin_Dependency {
22
23 /**
24 * Name of required plugin.
25 *
26 * Example: WooCommerce
27 *
28 * @since 1.0.0
29 * @access protected
30 * @var string $name
31 */
32 protected $name;
33
34 /**
35 * File name of plugin.
36 *
37 * Example: woocommerce/woocommerce.php
38 *
39 * @since 1.0.0
40 * @access protected
41 * @var string $file
42 */
43 protected $file;
44
45 /**
46 * The minimum version of the plugin required.
47 *
48 * Example: 3.2.6
49 *
50 * @since 1.0.0
51 * @access protected
52 * @var string $version
53 */
54 protected $version;
55
56 /**
57 * Whether the plugin is required.
58 *
59 * If the plugin is not required, only a version check will be performed.
60 *
61 * @since 1.0.0
62 * @access protected
63 * @var bool $required
64 */
65 protected $required;
66
67 /**
68 * Data returned by get_plugin_data() about the required plugin.
69 *
70 * @since 1.0.0
71 * @access protected
72 * @var array $data
73 */
74 protected $data;
75
76 /**
77 * Datafeedr_Plugin_Dependency constructor.
78 *
79 * @since 1.0.0
80 *
81 * @param string $name
82 * @param string $file
83 * @param string $version
84 * @param bool $required
85 */
86 public function __construct( $name, $file, $version, $required = true ) {
87 $this->name = $name;
88 $this->file = $file;
89 $this->version = $version;
90 $this->required = $required;
91 $this->set_data();
92 }
93
94 /**
95 * Sets the $this->data field.
96 *
97 * @since 1.0.0
98 */
99 protected function set_data() {
100 $this->data = ( $this->is_installed() ) ? get_plugin_data( $this->plugin_path() ) : array();
101 }
102
103 /**
104 * Returns "install", "activate" or "update" if an action is required for the required plugin.
105 *
106 * If no action is required, returns false.
107 *
108 * @since 1.0.0
109 *
110 * @return bool|string Returns "install", "activate" or "update" or false.
111 */
112 public function action_required() {
113
114 if ( ! $this->is_installed() && $this->required ) {
115 return 'install';
116 }
117
118 if ( ! $this->is_active() && $this->required ) {
119 return 'activate';
120 }
121
122 if ( ! $this->version_is_compatible() && $this->is_active() ) {
123 return 'update';
124 }
125
126 return false;
127 }
128
129 /**
130 * Returns the required plugin's name.
131 *
132 * @since 1.0.0
133 *
134 * @return string
135 */
136 public function plugin_name() {
137 return $this->name;
138 }
139
140 /**
141 * Returns true if the required plugin is installed otherwise returns false.
142 *
143 * @since 1.0.0
144 *
145 * @return bool
146 */
147 public function is_installed() {
148 return ( file_exists( $this->plugin_path() ) );
149 }
150
151 /**
152 * Returns true if the required plugin is activated otherwise returns false.
153 *
154 * @since 1.0.0
155 *
156 * @return bool
157 */
158 public function is_active() {
159 return ( is_plugin_active( $this->file ) );
160 }
161
162 /**
163 * Returns true if the required plugin meets the minimum version required otherwise returns false.
164 *
165 * @since 1.0.0
166 *
167 * @return bool
168 */
169 public function version_is_compatible() {
170 return ( version_compare( $this->current_version(), $this->required_version(), '>=' ) );
171 }
172
173 /**
174 * Returns the "installation" url if the user can install plugins. Otherwise returns an empty string.
175 *
176 * @since 1.0.0
177 *
178 * @return string
179 */
180 public function install_url() {
181
182 if ( ! current_user_can( 'install_plugins' ) ) {
183 return '';
184 }
185
186 $plugin_name = $this->plugin_action_name();
187
188 $url = add_query_arg( array(
189 'action' => 'install-plugin',
190 'plugin' => $plugin_name
191 ), wp_nonce_url( admin_url( 'update.php' ), 'install-plugin_' . $plugin_name ) );
192
193 return $url;
194 }
195
196 /**
197 * Returns the "activation" url if the user can activate this plugin. Otherwise returns an empty string.
198 *
199 * @since 1.0.0
200 *
201 * @return string
202 */
203 public function activate_url() {
204
205 if ( ! current_user_can( 'activate_plugin', $this->file ) ) {
206 return '';
207 }
208
209 $url = add_query_arg( array(
210 'action' => 'activate',
211 'plugin' => urlencode( $this->file ),
212 'paged' => '1',
213 's' => '',
214 ), wp_nonce_url( admin_url( 'plugins.php' ), 'activate-plugin_' . $this->file ) );
215
216 return $url;
217 }
218
219 /**
220 * Returns the "update" url if the user can update plugins. Otherwise returns an empty string.
221 *
222 * @since 1.0.0
223 *
224 * @return string
225 */
226 public function update_url() {
227
228 if ( ! current_user_can( 'update_plugins' ) ) {
229 return '';
230 }
231
232 $url = add_query_arg( array(
233 'action' => 'upgrade-plugin',
234 'plugin' => urlencode( $this->file )
235 ), wp_nonce_url( admin_url( 'update.php' ), 'upgrade-plugin_' . $this->file ) );
236
237 return $url;
238 }
239
240 /**
241 * Returns the current version of the installed plugin of 0 if plugin does not exist.
242 *
243 * @since 1.0.0
244 *
245 * @return string
246 */
247 public function current_version() {
248 $data = $this->plugin_data();
249
250 return ( isset( $data['Version'] ) ) ? $data['Version'] : '0';
251 }
252
253 /**
254 * Returns the minimum version of the required plugin.
255 *
256 * @since 1.0.0
257 *
258 * @return string
259 */
260 public function required_version() {
261 return $this->version;
262 }
263
264 /**
265 * Returns the message to display to the user if the required plugins needs to
266 * be installed, activated or updated.
267 *
268 * @since 1.0.0
269 *
270 * @see install_msg(), activate_msg(), update_msg()
271 *
272 * @param string $request_plugin Name of the requesting plugin.
273 *
274 * @return string
275 */
276 public function msg( $request_plugin ) {
277
278 $action = $this->action_required();
279
280 if ( ! $action ) {
281 return '';
282 }
283
284 $func = $action . '_msg';
285
286 return $this->$func( $request_plugin );
287 }
288
289 /**
290 * Returns the full <a href="..."></a> link for an install/activate/update action.
291 *
292 * @since 1.0.0
293 *
294 * @param bool $button Whether to display the link as a button.
295 * @param string $text Override the link's anchor text.
296 *
297 * @return string HTML.
298 */
299 public function link( $button = true, $text = '' ) {
300
301 $action = $this->action_required();
302
303 if ( ! $action ) {
304 return '';
305 }
306
307 // Calls install_url(), activate_url() or update_url().
308 $func = $action . '_url';
309 $url = $this->$func();
310
311 // Create the title attribute.
312 $title = sprintf(
313 __( '%1$s the %2$s plugin now.', 'datafeedr' ),
314 esc_attr( ucwords( strtolower( $action ) ) ),
315 esc_attr( $this->plugin_name() )
316 );
317
318 // Link CSS class.
319 $class = ( $button ) ? 'button button-small button-primary' : '';
320
321 // The anchor text for the link.
322 $text = ( ! empty( $text ) ) ? esc_html( $text ) : esc_html( ucwords( strtolower( $action ) ) );
323
324 // The HTML to return;
325 $html = ' <a href="%1$s" title="%2$s" class="%3$s">%4$s</a>';
326
327 return sprintf( $html, $url, $title, $class, $text );
328 }
329
330 /**
331 * Returns the message notifying the user that the required plugin must be installed.
332 *
333 * @since 1.0.0
334 *
335 * @param string $request_plugin Name of the requesting plugin.
336 *
337 * @return string Message to user.
338 */
339 protected function install_msg( $request_plugin ) {
340 $msg = __(
341 'The %1$s plugin requires the <strong>%2$s</strong> plugin to be installed.',
342 'datafeedr'
343 );
344
345 return sprintf( $msg, $request_plugin, $this->wporg_link(), $this->plugin_name() );
346 }
347
348 /**
349 * Returns the message notifying the user that the required plugin must be activated.
350 *
351 * @since 1.0.0
352 *
353 * @param string $request_plugin Name of the requesting plugin.
354 *
355 * @return string Message to user.
356 */
357 protected function activate_msg( $request_plugin ) {
358 $msg = __(
359 'The %1$s plugin requires the <strong>%2$s</strong> plugin to be activated.',
360 'datafeedr'
361 );
362
363 return sprintf( $msg, $request_plugin, $this->wporg_link() );
364 }
365
366 /**
367 * Returns the message notifying the user that the required plugin must be updated.
368 *
369 * @since 1.0.0
370 *
371 * @param string $request_plugin Name of the requesting plugin.
372 *
373 * @return string Message to user.
374 */
375 protected function update_msg( $request_plugin ) {
376 $msg = __(
377 'The %1$s plugin requires <strong>%2$s version %3$s</strong> or greater.',
378 'datafeedr'
379 );
380
381 return sprintf( $msg, $request_plugin, $this->wporg_link( $this->plugin_name() ),
382 $this->required_version() );
383 }
384
385 /**
386 * Returns an link <a href="..." target="_blank"></a> to the required plugin on wordpress.org's site.
387 *
388 * @since 1.0.0
389 *
390 * @param string $text Optional. Override anchor text.
391 *
392 * @return string HTML
393 */
394 public function wporg_link( $text = '' ) {
395 $url = $this->wporg_url();
396 $title = __( 'View this plugin on wordpress.org', 'datafeedr' );
397 $text = ( empty( $text ) ) ? esc_html( $this->plugin_name() ) : esc_html( $text );
398 $html = '<a href="%1$s" title="%2$s" target="_blank">%3$s</a>';
399
400 return sprintf( $html, $url, $title, $text );
401 }
402
403 /**
404 * Returns the URL to the required plugin's wordpress.org page.
405 *
406 * @since 1.0.0
407 *
408 * @return string URL
409 */
410 public function wporg_url() {
411 return sprintf( 'https://wordpress.org/plugins/%1$s/', $this->plugin_action_name() );
412 }
413
414 /**
415 * Returns the $this->data property.
416 *
417 * @since 1.0.0
418 *
419 * @return array
420 */
421 protected function plugin_data() {
422 return $this->data;
423 }
424
425 /**
426 * Returns the full path to the plugin file.
427 *
428 * Example: /home/username/public_html/wp-content/plugins/woocommerce/woocommerce.php
429 *
430 * @since 1.0.0
431 *
432 * @return string Full path
433 */
434 protected function plugin_path() {
435 return $this->plugins_dir() . $this->file;
436 }
437
438 /**
439 * Returns the full path to the WordPress plugin's directory WITH trailing slash.
440 *
441 * Example: /home/username/public_html/wp-content/plugins/
442 *
443 * @since 1.0.0
444 *
445 * @return string Full path with trailing slash.
446 */
447 protected function plugins_dir() {
448 return trailingslashit( WP_PLUGIN_DIR );
449 }
450
451 /**
452 * Returns the first part of the $this->file name.
453 *
454 * If the $this->file is "woocommerce/woocommerce.php" then this method will
455 * return "woocommerce".
456 *
457 * If the $this->file is "wordpress-seo/wp-seo.php", then this method will
458 * return "wordpress-seo".
459 *
460 * @since 1.0.0
461 *
462 * @return string
463 */
464 protected function plugin_action_name() {
465 $name = explode( '/', $this->file );
466 $name = str_replace( '.php', '', $name[0] );
467
468 return $name;
469 }
470 }
471