/wp-content/plugins/wordpress-seo/admin/class-plugin-availability.php

https://bitbucket.org/carloskikea/helpet · PHP · 344 lines · 183 code · 38 blank · 123 comment · 11 complexity · 00392a56ba82802e482174a88c044bfc MD5 · raw file

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