| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @since 1.0.74 |
| 5 |
*/ |
| 6 |
class Datafeedr_Plugin_Dependency { |
| 7 |
|
| 8 |
protected $name; |
| 9 |
protected $file; |
| 10 |
protected $version; |
| 11 |
protected $data; |
| 12 |
|
| 13 |
public function __construct( $name, $file, $version ) { |
| 14 |
$this->name = $name; |
| 15 |
$this->file = $file; |
| 16 |
$this->version = $version; |
| 17 |
$this->set_data(); |
| 18 |
} |
| 19 |
|
| 20 |
protected function set_data() { |
| 21 |
$this->data = ( $this->is_installed() ) ? get_plugin_data( $this->plugin_path() ) : array(); |
| 22 |
} |
| 23 |
|
| 24 |
// returns install, activate, update, false |
| 25 |
public function action_required() { |
| 26 |
|
| 27 |
if ( ! $this->is_installed() ) { |
| 28 |
return 'install'; |
| 29 |
} |
| 30 |
|
| 31 |
if ( ! $this->is_active() ) { |
| 32 |
return 'activate'; |
| 33 |
} |
| 34 |
|
| 35 |
if ( ! $this->version_is_compatible() ) { |
| 36 |
return 'update'; |
| 37 |
} |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
public function plugin_name() { |
| 43 |
return $this->name; |
| 44 |
} |
| 45 |
|
| 46 |
public function is_installed() { |
| 47 |
return ( file_exists( $this->plugin_path() ) ); |
| 48 |
} |
| 49 |
|
| 50 |
public function is_active() { |
| 51 |
return ( is_plugin_active( $this->file ) ); |
| 52 |
} |
| 53 |
|
| 54 |
public function version_is_compatible() { |
| 55 |
return ( version_compare( $this->current_version(), $this->required_version(), '>=' ) ); |
| 56 |
} |
| 57 |
|
| 58 |
public function installation_url() { |
| 59 |
|
| 60 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 61 |
return ''; |
| 62 |
} |
| 63 |
|
| 64 |
$plugin_name = $this->plugin_action_name(); |
| 65 |
|
| 66 |
$url = add_query_arg( array( |
| 67 |
'action' => 'install-plugin', |
| 68 |
'plugin' => $plugin_name |
| 69 |
), wp_nonce_url( admin_url( 'update.php' ), 'install-plugin_' . $plugin_name ) ); |
| 70 |
|
| 71 |
return $url; |
| 72 |
} |
| 73 |
|
| 74 |
public function activation_url() { |
| 75 |
|
| 76 |
if ( ! current_user_can( 'activate_plugin', $this->file ) ) { |
| 77 |
return ''; |
| 78 |
} |
| 79 |
|
| 80 |
$url = add_query_arg( array( |
| 81 |
'action' => 'activate', |
| 82 |
'plugin' => urlencode( $this->file ), |
| 83 |
'paged' => '1', |
| 84 |
's' => '', |
| 85 |
), wp_nonce_url( admin_url( 'plugins.php' ), 'activate-plugin_' . $this->file ) ); |
| 86 |
|
| 87 |
return $url; |
| 88 |
} |
| 89 |
|
| 90 |
public function update_url() { |
| 91 |
|
| 92 |
if ( ! current_user_can( 'update_plugins' ) ) { |
| 93 |
return ''; |
| 94 |
} |
| 95 |
|
| 96 |
$url = add_query_arg( array( |
| 97 |
'action' => 'upgrade-plugin', |
| 98 |
'plugin' => urlencode( $this->file ) |
| 99 |
), wp_nonce_url( admin_url( 'update.php' ), 'upgrade-plugin_' . $this->file ) ); |
| 100 |
|
| 101 |
return $url; |
| 102 |
} |
| 103 |
|
| 104 |
public function current_version() { |
| 105 |
$data = $this->plugin_data(); |
| 106 |
|
| 107 |
return ( isset( $data['Version'] ) ) ? $data['Version'] : '0'; |
| 108 |
} |
| 109 |
|
| 110 |
public function required_version() { |
| 111 |
return $this->version; |
| 112 |
} |
| 113 |
|
| 114 |
protected function plugin_data() { |
| 115 |
return $this->data; |
| 116 |
} |
| 117 |
|
| 118 |
protected function plugin_path() { |
| 119 |
return $this->plugins_dir() . $this->file; |
| 120 |
} |
| 121 |
|
| 122 |
protected function plugins_dir() { |
| 123 |
return trailingslashit( WP_PLUGIN_DIR ); |
| 124 |
} |
| 125 |
|
| 126 |
protected function plugin_action_name() { |
| 127 |
$name = explode( '/', $this->file ); |
| 128 |
$name = str_replace( '.php', '', $name[1] ); |
| 129 |
|
| 130 |
return $name; |
| 131 |
} |
| 132 |
} |