WPSEO_Plugin_Availability

Class WPSEO_Plugin_Availability.

Defined (1)

The class is defined in the following location(s).

/admin/class-plugin-availability.php  
  1. class WPSEO_Plugin_Availability { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. protected $plugins = array(); 
  7.  
  8. /** 
  9. * WPSEO_Plugin_Availability constructor. 
  10. */ 
  11. public function __construct() { 
  12. $this->register_yoast_plugins(); 
  13. $this->register_yoast_plugins_status(); 
  14.  
  15. /** 
  16. * Registers all the available Yoast SEO plugins. 
  17. */ 
  18. protected function register_yoast_plugins() { 
  19. $this->plugins = array( 
  20. 'yoast-seo-premium' => array( 
  21. 'url' => 'https://yoast.com/wordpress/plugins/seo-premium/',  
  22. 'title' => 'Yoast SEO Premium',  
  23. /** translators: %1$s expands to Yoast SEO */ 
  24. 'description' => sprintf( __( 'The premium version of %1$s with more features & support.', 'wordpress-seo' ), 'Yoast SEO' ),  
  25. 'installed' => false,  
  26. 'slug' => 'wordpress-seo-premium/wp-seo-premium.php',  
  27. ),  
  28.  
  29. 'video-seo-for-wordpress-seo-by-yoast' => array( 
  30. 'url' => 'https://yoast.com/wordpress/plugins/video-seo/',  
  31. 'title' => 'Video SEO',  
  32. 'description' => __( 'Optimize your videos to show them off in search results and get more clicks!', 'wordpress-seo' ),  
  33. 'installed' => false,  
  34. 'slug' => 'wpseo-video/video-seo.php',  
  35. ),  
  36.  
  37. 'yoast-news-seo' => array( 
  38. 'url' => 'https://yoast.com/wordpress/plugins/news-seo/',  
  39. 'title' => 'News SEO',  
  40. 'description' => __( 'Are you in Google News? Increase your traffic from Google News by optimizing for it!', 'wordpress-seo' ),  
  41. 'installed' => false,  
  42. 'slug' => 'wpseo-news/wpseo-news.php',  
  43. ),  
  44.  
  45. 'local-seo-for-yoast-seo' => array( 
  46. 'url' => 'https://yoast.com/wordpress/plugins/local-seo/',  
  47. 'title' => 'Local SEO',  
  48. 'description' => __( 'Rank better locally and in Google Maps, without breaking a sweat!', 'wordpress-seo' ),  
  49. 'installed' => false,  
  50. 'slug' => 'wordpress-seo-local/local-seo.php',  
  51. ),  
  52.  
  53. 'yoast-woocommerce-seo' => array( 
  54. 'url' => 'https://yoast.com/wordpress/plugins/yoast-woocommerce-seo/',  
  55. 'title' => 'Yoast WooCommerce SEO',  
  56. /** translators: %1$s expands to Yoast SEO */ 
  57. 'description' => sprintf( __( 'Seamlessly integrate WooCommerce with %1$s and get extra features!', 'wordpress-seo' ), 'Yoast SEO' ),  
  58. 'installed' => false,  
  59. 'slug' => 'wpseo-woocommerce/wpseo-woocommerce.php',  
  60. ),  
  61. ); 
  62.  
  63. /** 
  64. * Sets certain plugin properties based on WordPress' status. 
  65. */ 
  66. protected function register_yoast_plugins_status() { 
  67.  
  68. foreach ( $this->plugins as $name => $plugin ) { 
  69.  
  70. $plugin_slug = $plugin['slug']; 
  71. $plugin_path = WP_PLUGIN_DIR . '/' . $plugin_slug; 
  72.  
  73. if ( file_exists( $plugin_path ) ) { 
  74. $plugin_data = get_plugin_data( $plugin_path, false, false ); 
  75. $this->plugins[ $name ]['installed'] = true; 
  76. $this->plugins[ $name ]['version'] = $plugin_data['Version']; 
  77. $this->plugins[ $name ]['active'] = is_plugin_active( $plugin_slug ); 
  78.  
  79. /** 
  80. * Checks whether or not a plugin is known within the Yoast SEO collection. 
  81. * @param {string} $plugin The plugin to search for. 
  82. * @return bool Whether or not the plugin is exists. 
  83. */ 
  84. protected function plugin_exists( $plugin ) { 
  85. return isset( $this->plugins[ $plugin ] ); 
  86.  
  87. /** 
  88. * Gets all the possibly available plugins. 
  89. * @return array Array containing the information about the plugins. 
  90. */ 
  91. public function get_plugins() { 
  92. return $this->plugins; 
  93.  
  94. /** 
  95. * Gets a specific plugin. Returns an empty array if it cannot be found. 
  96. * @param {string} $plugin The plugin to search for. 
  97. * @return array The plugin properties. 
  98. */ 
  99. public function get_plugin( $plugin ) { 
  100. if ( ! $this->plugin_exists( $plugin ) ) { 
  101. return array(); 
  102.  
  103. return $this->plugins[ $plugin ]; 
  104.  
  105. /** 
  106. * Gets the version of the plugin. 
  107. * @param {string} $plugin The plugin to search for. 
  108. * @return string The version associated with the plugin. 
  109. */ 
  110. public function get_version( $plugin ) { 
  111. if ( ! isset( $plugin['version'] ) ) { 
  112. return ''; 
  113.  
  114. return $plugin['version']; 
  115.  
  116. /** 
  117. * Checks if there are dependencies available for the plugin. 
  118. * @param {string} $plugin The plugin to search for. 
  119. * @return bool Whether or not there is a dependency present. 
  120. */ 
  121. public function has_dependencies( $plugin ) { 
  122. return ( isset( $plugin['_dependencies'] ) && ! empty( $plugin['_dependencies'] ) ); 
  123.  
  124. /** 
  125. * Gets the dependencies for the plugin. 
  126. * @param {string} $plugin The plugin to search for. 
  127. * @return array Array containing all the dependencies associated with the plugin. 
  128. */ 
  129. public function get_dependencies( $plugin ) { 
  130. if ( ! $this->has_dependencies( $plugin ) ) { 
  131. return array(); 
  132.  
  133. return $plugin['_dependencies']; 
  134.  
  135. /** 
  136. * Checks if all dependencies are satisfied. 
  137. * @param {string} $plugin The plugin to search for. 
  138. * @return bool Whether or not the dependencies are satisfied. 
  139. */ 
  140. public function dependencies_are_satisfied( $plugin ) { 
  141. if ( ! $this->has_dependencies( $plugin ) ) { 
  142. return true; 
  143.  
  144. $dependencies = $this->get_dependencies( $plugin ); 
  145. $installed_dependencies = array_filter( $dependencies, array( $this, 'is_dependency_available' ) ); 
  146.  
  147. return count( $installed_dependencies ) === count( $dependencies ); 
  148.  
  149. /** 
  150. * Checks whether or not one of the plugins is properly installed and usable. 
  151. * @param {string} $plugin The plugin to search for. 
  152. * @return bool Whether or not the plugin is properly installed. 
  153. */ 
  154. public function is_installed( $plugin ) { 
  155. if ( empty( $plugin ) ) { 
  156. return false; 
  157.  
  158. $dependencies_are_satisfied = $this->dependencies_are_satisfied( $plugin ); 
  159.  
  160. return $dependencies_are_satisfied && $this->is_available( $plugin ); 
  161.  
  162. /** 
  163. * Gets all installed plugins. 
  164. * @return array 
  165. */ 
  166. public function get_installed_plugins() { 
  167. $installed = array(); 
  168.  
  169. foreach ( $this->plugins as $plugin_key => $plugin ) { 
  170. if ( $this->is_installed( $plugin ) ) { 
  171. $installed[ $plugin_key ] = $plugin; 
  172.  
  173. return $installed; 
  174.  
  175. /** 
  176. * Checks for the availability of the plugin. 
  177. * @param {string} $plugin The plugin to search for. 
  178. * @return bool Whether or not the plugin is available. 
  179. */ 
  180. public function is_available( $plugin ) { 
  181. return isset( $plugin['installed'] ) && $plugin['installed'] === true; 
  182.  
  183. /** 
  184. * Checks whether a dependency is available. 
  185. * @param {string} $dependency The dependency to look for. 
  186. * @return bool Whether or not the dependency is available. 
  187. */ 
  188. public function is_dependency_available( $dependency ) { 
  189. return class_exists( $dependency );