class-extensions.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Represents the class that contains the list of possible extensions for Yoast SEO.
  9. */
  10. class WPSEO_Extensions {
  11. /** @var array Array with the Yoast extensions */
  12. protected $extensions = array(
  13. 'Yoast SEO Premium' => array(
  14. 'slug' => 'yoast-seo-premium',
  15. 'identifier' => 'wordpress-seo-premium',
  16. 'classname' => 'WPSEO_Premium',
  17. ),
  18. 'News SEO' => array(
  19. 'slug' => 'news-seo',
  20. 'identifier' => 'wpseo-news',
  21. 'classname' => 'WPSEO_News',
  22. ),
  23. 'Yoast WooCommerce SEO' => array(
  24. 'slug' => 'woocommerce-yoast-seo',
  25. 'identifier' => 'wpseo-woocommerce',
  26. 'classname' => 'Yoast_WooCommerce_SEO',
  27. ),
  28. 'Video SEO' => array(
  29. 'slug' => 'video-seo-for-wordpress',
  30. 'identifier' => 'wpseo-video',
  31. 'classname' => 'WPSEO_Video_Sitemap',
  32. ),
  33. 'Local SEO' => array(
  34. 'slug' => 'local-seo-for-wordpress',
  35. 'identifier' => 'wpseo-local',
  36. 'classname' => 'WPSEO_Local_Core',
  37. ),
  38. 'Local SEO for WooCommerce' => array(
  39. 'slug' => 'local-seo-for-woocommerce',
  40. 'identifier' => 'wpseo-local-woocommerce',
  41. 'classname' => 'WPSEO_Local_WooCommerce',
  42. ),
  43. );
  44. /**
  45. * Returns the set extensions.
  46. *
  47. * @return array All the extension names.
  48. */
  49. public function get() {
  50. return array_keys( $this->extensions );
  51. }
  52. /**
  53. * Checks if the extension is valid.
  54. *
  55. * @param string $extension The extension to get the name for.
  56. *
  57. * @return bool Returns true when valid.
  58. */
  59. public function is_valid( $extension ) {
  60. $extensions = new WPSEO_Extension_Manager();
  61. return $extensions->is_activated( $this->extensions[ $extension ]['identifier'] );
  62. }
  63. /**
  64. * Invalidates the extension by removing its option.
  65. *
  66. * @param string $extension The extension to invalidate.
  67. */
  68. public function invalidate( $extension ) {
  69. /*
  70. * Make sure we clear the current site and multisite options.
  71. *
  72. * Because plugins can be site-activated or multi-site activated we need to clear
  73. * all possible options.
  74. *
  75. * If we knew here that the extension in question was network activated
  76. * we could do this a lot more easily.
  77. */
  78. delete_option( $this->get_option_name( $extension ) );
  79. delete_site_option( $this->get_option_name( $extension ) );
  80. }
  81. /**
  82. * Checks if the plugin has been installed.
  83. *
  84. * @param string $extension The name of the plugin to check.
  85. *
  86. * @return bool Returns true when installed.
  87. */
  88. public function is_installed( $extension ) {
  89. return class_exists( $this->extensions[ $extension ]['classname'] );
  90. }
  91. /**
  92. * Converts the extension to the required option name.
  93. *
  94. * @param string $extension The extension name to convert.
  95. *
  96. * @return string Returns the option name.
  97. */
  98. protected function get_option_name( $extension ) {
  99. return sanitize_title_with_dashes( $this->extensions[ $extension ]['slug'] . '_', null, 'save' ) . 'license';
  100. }
  101. }