class-extensions.php 3.0 KB

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