| @@ -4,8 +4,11 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package WPSEO\Plugin_Availability |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | +use Yoast\WP\SEO\Conditionals\Conditional; | |
| 9 | +use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; | |
| 10 | + | |
| 8 | 11 | /** |
| 9 | 12 | * Class WPSEO_Plugin_Availability |
| 10 | 13 | */ |
| 11 | 14 | class WPSEO_Plugin_Availability { |
| @@ -18,8 +21,10 @@ | ||
| 18 | 21 | protected $plugins = []; |
| 19 | 22 | |
| 20 | 23 | /** |
| 21 | 24 | * Registers the plugins so we can access them. |
| 25 | + * | |
| 26 | + * @return void | |
| 22 | 27 | */ |
| 23 | 28 | public function register() { |
| 24 | 29 | $this->register_yoast_plugins(); |
| 25 | 30 | $this->register_yoast_plugins_status(); |
| @@ -26,8 +31,10 @@ | ||
| 26 | 31 | } |
| 27 | 32 | |
| 28 | 33 | /** |
| 29 | 34 | * Registers all the available Yoast SEO plugins. |
| 35 | + * | |
| 36 | + * @return void | |
| 30 | 37 | */ |
| 31 | 38 | protected function register_yoast_plugins() { |
| 32 | 39 | $this->plugins = [ |
| 33 | 40 | 'yoast-seo-premium' => [ |
| @@ -35,9 +42,9 @@ | ||
| 35 | 42 | 'title' => 'Yoast SEO Premium', |
| 36 | 43 | 'description' => sprintf( |
| 37 | 44 | /* translators: %1$s expands to Yoast SEO */ |
| 38 | 45 | __( 'The premium version of %1$s with more features & support.', 'wordpress-seo' ), |
| 39 | - 'Yoast SEO' | |
| 46 | + 'Yoast SEO', | |
| 40 | 47 | ), |
| 41 | 48 | 'installed' => false, |
| 42 | 49 | 'slug' => 'wordpress-seo-premium/wp-seo-premium.php', |
| 43 | 50 | 'version_sync' => true, |
| @@ -79,13 +86,14 @@ | ||
| 79 | 86 | 'title' => 'Yoast WooCommerce SEO', |
| 80 | 87 | 'description' => sprintf( |
| 81 | 88 | /* translators: %1$s expands to Yoast SEO */ |
| 82 | 89 | __( 'Seamlessly integrate WooCommerce with %1$s and get extra features!', 'wordpress-seo' ), |
| 83 | - 'Yoast SEO' | |
| 90 | + 'Yoast SEO', | |
| 84 | 91 | ), |
| 85 | 92 | '_dependencies' => [ |
| 86 | 93 | 'WooCommerce' => [ |
| 87 | - 'slug' => 'woocommerce/woocommerce.php', | |
| 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(), | |
| 88 | 96 | ], |
| 89 | 97 | ], |
| 90 | 98 | 'installed' => false, |
| 91 | 99 | 'slug' => 'wpseo-woocommerce/wpseo-woocommerce.php', |
| @@ -96,8 +104,10 @@ | ||
| 96 | 104 | } |
| 97 | 105 | |
| 98 | 106 | /** |
| 99 | 107 | * Sets certain plugin properties based on WordPress' status. |
| 108 | + * | |
| 109 | + * @return void | |
| 100 | 110 | */ |
| 101 | 111 | protected function register_yoast_plugins_status() { |
| 102 | 112 | |
| 103 | 113 | foreach ( $this->plugins as $name => $plugin ) { |
| @@ -114,63 +124,13 @@ | ||
| 114 | 124 | } |
| 115 | 125 | } |
| 116 | 126 | |
| 117 | 127 | /** |
| 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 | 128 | * Checks if there are dependencies available for the plugin. |
| 169 | 129 | * |
| 170 | 130 | * @param array $plugin The information available about the plugin. |
| 171 | 131 | * |
| 172 | - * @return bool Whether or not there is a dependency present. | |
| 132 | + * @return bool Whether there is a dependency present. | |
| 173 | 133 | */ |
| 174 | 134 | public function has_dependencies( $plugin ) { |
| 175 | 135 | return ( isset( $plugin['_dependencies'] ) && ! empty( $plugin['_dependencies'] ) ); |
| 176 | 136 | } |
| @@ -201,12 +161,12 @@ | ||
| 201 | 161 | if ( ! $this->has_dependencies( $plugin ) ) { |
| 202 | 162 | return true; |
| 203 | 163 | } |
| 204 | 164 | |
| 205 | - $dependencies = $this->get_dependencies( $plugin ); | |
| 206 | - $installed_dependencies = array_filter( $dependencies, [ $this, 'is_dependency_available' ] ); | |
| 165 | + $dependencies = $this->get_dependencies( $plugin ); | |
| 166 | + $active_dependencies = array_filter( $dependencies, [ $this, 'is_dependency_active' ] ); | |
| 207 | 167 | |
| 208 | - return count( $installed_dependencies ) === count( $dependencies ); | |
| 168 | + return count( $active_dependencies ) === count( $dependencies ); | |
| 209 | 169 | } |
| 210 | 170 | |
| 211 | 171 | /** |
| 212 | 172 | * Checks whether or not one of the plugins is properly installed and usable. |
| @@ -223,25 +183,8 @@ | ||
| 223 | 183 | return $this->is_available( $plugin ); |
| 224 | 184 | } |
| 225 | 185 | |
| 226 | 186 | /** |
| 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 | 187 | * Checks for the availability of the plugin. |
| 245 | 188 | * |
| 246 | 189 | * @param array $plugin The information available about the plugin. |
| 247 | 190 | * |
| @@ -251,34 +194,19 @@ | ||
| 251 | 194 | return isset( $plugin['installed'] ) && $plugin['installed'] === true; |
| 252 | 195 | } |
| 253 | 196 | |
| 254 | 197 | /** |
| 255 | - * Checks whether a dependency is available. | |
| 198 | + * Checks whether a dependency is active. | |
| 256 | 199 | * |
| 257 | - * @param array $dependency The information about the dependency to look for. | |
| 200 | + * @param array<string, Conditional> $dependency The information about the dependency to look for. | |
| 258 | 201 | * |
| 259 | - * @return bool Whether or not the dependency is available. | |
| 202 | + * @return bool Whether or not the dependency is active. | |
| 260 | 203 | */ |
| 261 | - public function is_dependency_available( $dependency ) { | |
| 262 | - return isset( get_plugins()[ $dependency['slug'] ] ); | |
| 204 | + public function is_dependency_active( $dependency ) { | |
| 205 | + return $dependency['conditional']->is_met(); | |
| 263 | 206 | } |
| 264 | 207 | |
| 265 | 208 | /** |
| 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 | 209 | * Gets an array of plugins that have defined dependencies. |
| 282 | 210 | * |
| 283 | 211 | * @return array Array of the plugins that have dependencies. |
| 284 | 212 | */ |
| @@ -283,28 +211,6 @@ | ||
| 283 | 211 | * @return array Array of the plugins that have dependencies. |
| 284 | 212 | */ |
| 285 | 213 | public function get_plugins_with_dependencies() { |
| 286 | 214 | 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 | 215 | } |
| 310 | 216 | } |