class-plugin-compatibility.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Plugin_Compatibility
  6. */
  7. /**
  8. * Class WPSEO_Plugin_Compatibility
  9. */
  10. class WPSEO_Plugin_Compatibility {
  11. /**
  12. * @var string
  13. */
  14. protected $current_wpseo_version;
  15. /**
  16. * @var WPSEO_Plugin_Availability
  17. */
  18. protected $availability_checker;
  19. /**
  20. * @var array
  21. */
  22. protected $installed_plugins;
  23. /**
  24. * WPSEO_Plugin_Compatibility constructor.
  25. *
  26. * @param string $version The version to check against.
  27. * @param null|class $availability_checker The checker to use.
  28. */
  29. public function __construct( $version, $availability_checker = null ) {
  30. // We trim off the patch version, as this shouldn't break the comparison.
  31. $this->current_wpseo_version = $this->get_major_minor_version( $version );
  32. $this->availability_checker = $this->retrieve_availability_checker( $availability_checker );
  33. $this->installed_plugins = $this->availability_checker->get_installed_plugins();
  34. }
  35. /**
  36. * Retrieves the availability checker.
  37. *
  38. * @param null|object $checker The checker to set.
  39. *
  40. * @return WPSEO_Plugin_Availability The checker to use.
  41. */
  42. private function retrieve_availability_checker( $checker ) {
  43. if ( is_null( $checker ) || ! is_object( $checker ) ) {
  44. $checker = new WPSEO_Plugin_Availability();
  45. $checker->register();
  46. }
  47. return $checker;
  48. }
  49. /**
  50. * Wraps the availability checker's get_installed_plugins method.
  51. *
  52. * @return array Array containing all the installed plugins.
  53. */
  54. public function get_installed_plugins() {
  55. return $this->installed_plugins;
  56. }
  57. /**
  58. * Creates a list of installed plugins and whether or not they are compatible.
  59. *
  60. * @return array Array containing the installed plugins and compatibility.
  61. */
  62. public function get_installed_plugins_compatibility() {
  63. foreach ( $this->installed_plugins as $key => $plugin ) {
  64. $this->installed_plugins[ $key ]['compatible'] = $this->is_compatible( $key );
  65. }
  66. return $this->installed_plugins;
  67. }
  68. /**
  69. * Checks whether or not a plugin is compatible.
  70. *
  71. * @param string $plugin The plugin to look for and match.
  72. *
  73. * @return bool Whether or not the plugin is compatible.
  74. */
  75. public function is_compatible( $plugin ) {
  76. $plugin = $this->availability_checker->get_plugin( $plugin );
  77. // If we are not syncing versions, we are always compatible.
  78. if ( ! isset( $plugin['version_sync'] ) || $plugin['version_sync'] !== true ) {
  79. return true;
  80. }
  81. $plugin_version = $this->availability_checker->get_version( $plugin );
  82. return $this->get_major_minor_version( $plugin_version ) === $this->current_wpseo_version;
  83. }
  84. /**
  85. * Gets the major/minor version of the plugin for easier comparing.
  86. *
  87. * @param string $version The version to trim.
  88. *
  89. * @return string The major/minor version of the plugin.
  90. */
  91. protected function get_major_minor_version( $version ) {
  92. return substr( $version, 0, 3 );
  93. }
  94. }