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