| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Plugin_Availability |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class WPSEO_Plugin_Availability |
| 10 |
*/ |
| 11 |
class WPSEO_Plugin_Availability { |
| 12 |
|
| 13 |
/** |
| 14 |
* Holds the plugins. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $plugins = []; |
| 19 |
|
| 20 |
/** |
| 21 |
* Registers the plugins so we can access them. |
| 22 |
*/ |
| 23 |
public function register() { |
| 24 |
$this->register_yoast_plugins(); |
| 25 |
$this->register_yoast_plugins_status(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Registers all the available Yoast SEO plugins. |
| 30 |
*/ |
| 31 |
protected function register_yoast_plugins() { |
| 32 |
$this->plugins = [ |
| 33 |
'yoast-seo-premium' => [ |
| 34 |
'url' => WPSEO_Shortlinker::get( 'https://yoa.st/1y7' ), |
| 35 |
'title' => 'Yoast SEO Premium', |
| 36 |
'description' => sprintf( |
| 37 |
/* translators: %1$s expands to Yoast SEO */ |
| 38 |
__( 'The premium version of %1$s with more features & support.', 'wordpress-seo' ), |
| 39 |
'Yoast SEO' |
| 40 |
), |
| 41 |
'installed' => false, |
| 42 |
'slug' => 'wordpress-seo-premium/wp-seo-premium.php', |
| 43 |
'version_sync' => true, |
| 44 |
'premium' => true, |
| 45 |
], |
| 46 |
|
| 47 |
'video-seo-for-wordpress-seo-by-yoast' => [ |
| 48 |
'url' => WPSEO_Shortlinker::get( 'https://yoa.st/1y8' ), |
| 49 |
'title' => 'Video SEO', |
| 50 |
'description' => __( 'Optimize your videos to show them off in search results and get more clicks!', 'wordpress-seo' ), |
| 51 |
'installed' => false, |
| 52 |
'slug' => 'wpseo-video/video-seo.php', |
| 53 |
'version_sync' => true, |
| 54 |
'premium' => true, |
| 55 |
], |
| 56 |
|
| 57 |
'yoast-news-seo' => [ |
| 58 |
'url' => WPSEO_Shortlinker::get( 'https://yoa.st/1y9' ), |
| 59 |
'title' => 'News SEO', |
| 60 |
'description' => __( 'Are you in Google News? Increase your traffic from Google News by optimizing for it!', 'wordpress-seo' ), |
| 61 |
'installed' => false, |
| 62 |
'slug' => 'wpseo-news/wpseo-news.php', |
| 63 |
'version_sync' => true, |
| 64 |
'premium' => true, |
| 65 |
], |
| 66 |
|
| 67 |
'local-seo-for-yoast-seo' => [ |
| 68 |
'url' => WPSEO_Shortlinker::get( 'https://yoa.st/1ya' ), |
| 69 |
'title' => 'Local SEO', |
| 70 |
'description' => __( 'Rank better locally and in Google Maps, without breaking a sweat!', 'wordpress-seo' ), |
| 71 |
'installed' => false, |
| 72 |
'slug' => 'wordpress-seo-local/local-seo.php', |
| 73 |
'version_sync' => true, |
| 74 |
'premium' => true, |
| 75 |
], |
| 76 |
|
| 77 |
'yoast-woocommerce-seo' => [ |
| 78 |
'url' => WPSEO_Shortlinker::get( 'https://yoa.st/1o0' ), |
| 79 |
'title' => 'Yoast WooCommerce SEO', |
| 80 |
'description' => sprintf( |
| 81 |
/* translators: %1$s expands to Yoast SEO */ |
| 82 |
__( 'Seamlessly integrate WooCommerce with %1$s and get extra features!', 'wordpress-seo' ), |
| 83 |
'Yoast SEO' |
| 84 |
), |
| 85 |
'_dependencies' => [ |
| 86 |
'WooCommerce' => [ |
| 87 |
'slug' => 'woocommerce/woocommerce.php', |
| 88 |
], |
| 89 |
], |
| 90 |
'installed' => false, |
| 91 |
'slug' => 'wpseo-woocommerce/wpseo-woocommerce.php', |
| 92 |
'version_sync' => true, |
| 93 |
'premium' => true, |
| 94 |
], |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Sets certain plugin properties based on WordPress' status. |
| 100 |
*/ |
| 101 |
protected function register_yoast_plugins_status() { |
| 102 |
|
| 103 |
foreach ( $this->plugins as $name => $plugin ) { |
| 104 |
|
| 105 |
$plugin_slug = $plugin['slug']; |
| 106 |
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin_slug; |
| 107 |
|
| 108 |
if ( file_exists( $plugin_path ) ) { |
| 109 |
$plugin_data = get_plugin_data( $plugin_path, false, false ); |
| 110 |
$this->plugins[ $name ]['installed'] = true; |
| 111 |
$this->plugins[ $name ]['version'] = $plugin_data['Version']; |
| 112 |
$this->plugins[ $name ]['active'] = is_plugin_active( $plugin_slug ); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Checks whether or not a plugin is known within the Yoast SEO collection. |
| 119 |
* |
| 120 |
* @param string $plugin The plugin to search for. |
| 121 |
* |
| 122 |
* @return bool Whether or not the plugin is exists. |
| 123 |
*/ |
| 124 |
protected function plugin_exists( $plugin ) { |
| 125 |
return isset( $this->plugins[ $plugin ] ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Gets all the possibly available plugins. |
| 130 |
* |
| 131 |
* @return array Array containing the information about the plugins. |
| 132 |
*/ |
| 133 |
public function get_plugins() { |
| 134 |
return $this->plugins; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Gets a specific plugin. Returns an empty array if it cannot be found. |
| 139 |
* |
| 140 |
* @param string $plugin The plugin to search for. |
| 141 |
* |
| 142 |
* @return array The plugin properties. |
| 143 |
*/ |
| 144 |
public function get_plugin( $plugin ) { |
| 145 |
if ( ! $this->plugin_exists( $plugin ) ) { |
| 146 |
return []; |
| 147 |
} |
| 148 |
|
| 149 |
return $this->plugins[ $plugin ]; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Gets the version of the plugin. |
| 154 |
* |
| 155 |
* @param array $plugin The information available about the plugin. |
| 156 |
* |
| 157 |
* @return string The version associated with the plugin. |
| 158 |
*/ |
| 159 |
public function get_version( $plugin ) { |
| 160 |
if ( ! isset( $plugin['version'] ) ) { |
| 161 |
return ''; |
| 162 |
} |
| 163 |
|
| 164 |
return $plugin['version']; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Checks if there are dependencies available for the plugin. |
| 169 |
* |
| 170 |
* @param array $plugin The information available about the plugin. |
| 171 |
* |
| 172 |
* @return bool Whether or not there is a dependency present. |
| 173 |
*/ |
| 174 |
public function has_dependencies( $plugin ) { |
| 175 |
return ( isset( $plugin['_dependencies'] ) && ! empty( $plugin['_dependencies'] ) ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Gets the dependencies for the plugin. |
| 180 |
* |
| 181 |
* @param array $plugin The information available about the plugin. |
| 182 |
* |
| 183 |
* @return array Array containing all the dependencies associated with the plugin. |
| 184 |
*/ |
| 185 |
public function get_dependencies( $plugin ) { |
| 186 |
if ( ! $this->has_dependencies( $plugin ) ) { |
| 187 |
return []; |
| 188 |
} |
| 189 |
|
| 190 |
return $plugin['_dependencies']; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Checks if all dependencies are satisfied. |
| 195 |
* |
| 196 |
* @param array $plugin The information available about the plugin. |
| 197 |
* |
| 198 |
* @return bool Whether or not the dependencies are satisfied. |
| 199 |
*/ |
| 200 |
public function dependencies_are_satisfied( $plugin ) { |
| 201 |
if ( ! $this->has_dependencies( $plugin ) ) { |
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
$dependencies = $this->get_dependencies( $plugin ); |
| 206 |
$installed_dependencies = array_filter( $dependencies, [ $this, 'is_dependency_available' ] ); |
| 207 |
|
| 208 |
return count( $installed_dependencies ) === count( $dependencies ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Checks whether or not one of the plugins is properly installed and usable. |
| 213 |
* |
| 214 |
* @param array $plugin The information available about the plugin. |
| 215 |
* |
| 216 |
* @return bool Whether or not the plugin is properly installed. |
| 217 |
*/ |
| 218 |
public function is_installed( $plugin ) { |
| 219 |
if ( empty( $plugin ) ) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
return $this->is_available( $plugin ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Gets all installed plugins. |
| 228 |
* |
| 229 |
* @return array The installed plugins. |
| 230 |
*/ |
| 231 |
public function get_installed_plugins() { |
| 232 |
$installed = []; |
| 233 |
|
| 234 |
foreach ( $this->plugins as $plugin_key => $plugin ) { |
| 235 |
if ( $this->is_installed( $plugin ) ) { |
| 236 |
$installed[ $plugin_key ] = $plugin; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return $installed; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Checks for the availability of the plugin. |
| 245 |
* |
| 246 |
* @param array $plugin The information available about the plugin. |
| 247 |
* |
| 248 |
* @return bool Whether or not the plugin is available. |
| 249 |
*/ |
| 250 |
public function is_available( $plugin ) { |
| 251 |
return isset( $plugin['installed'] ) && $plugin['installed'] === true; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Checks whether a dependency is available. |
| 256 |
* |
| 257 |
* @param array $dependency The information about the dependency to look for. |
| 258 |
* |
| 259 |
* @return bool Whether or not the dependency is available. |
| 260 |
*/ |
| 261 |
public function is_dependency_available( $dependency ) { |
| 262 |
return isset( get_plugins()[ $dependency['slug'] ] ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Gets the names of the dependencies. |
| 267 |
* |
| 268 |
* @param array $plugin The plugin to get the dependency names from. |
| 269 |
* |
| 270 |
* @return array Array containing the names of the associated dependencies. |
| 271 |
*/ |
| 272 |
public function get_dependency_names( $plugin ) { |
| 273 |
if ( ! $this->has_dependencies( $plugin ) ) { |
| 274 |
return []; |
| 275 |
} |
| 276 |
|
| 277 |
return array_keys( $plugin['_dependencies'] ); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Gets an array of plugins that have defined dependencies. |
| 282 |
* |
| 283 |
* @return array Array of the plugins that have dependencies. |
| 284 |
*/ |
| 285 |
public function get_plugins_with_dependencies() { |
| 286 |
return array_filter( $this->plugins, [ $this, 'has_dependencies' ] ); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Determines whether or not a plugin is active. |
| 291 |
* |
| 292 |
* @param string $plugin The plugin slug to check. |
| 293 |
* |
| 294 |
* @return bool Whether or not the plugin is active. |
| 295 |
*/ |
| 296 |
public function is_active( $plugin ) { |
| 297 |
return is_plugin_active( $plugin ); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Determines whether or not a plugin is a Premium product. |
| 302 |
* |
| 303 |
* @param array $plugin The plugin to check. |
| 304 |
* |
| 305 |
* @return bool Whether or not the plugin is a Premium product. |
| 306 |
*/ |
| 307 |
public function is_premium( $plugin ) { |
| 308 |
return isset( $plugin['premium'] ) && $plugin['premium'] === true; |
| 309 |
} |
| 310 |
} |
| 311 |
|