PluginProbe
Datafeedr API / 1.0.75
Datafeedr API v1.0.75
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 1.0.33 All 181 releases
datafeedr-api / classes / class-datafeedr-plugin-dependency.php

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

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