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