class-plugin-availability.php 9.0 KB

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