class-extension-manager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Represents the class that contains the available extensions for Yoast SEO.
  9. */
  10. class WPSEO_Extension_Manager {
  11. /** The transient key to save the cache in */
  12. const TRANSIENT_CACHE_KEY = 'wpseo_license_active_extensions';
  13. /** @var WPSEO_Extension[] */
  14. protected $extensions = array();
  15. /** @var array List of active plugins */
  16. static protected $active_extensions;
  17. /**
  18. * Adds an extension to the manager.
  19. *
  20. * @param string $extension_name The extension name.
  21. * @param WPSEO_Extension $extension The extension value object.
  22. *
  23. * @return void
  24. */
  25. public function add( $extension_name, WPSEO_Extension $extension = null ) {
  26. $this->extensions[ $extension_name ] = $extension;
  27. }
  28. /**
  29. * Removes an extension from the manager.
  30. *
  31. * @param string $extension_name The name of the extension to remove.
  32. *
  33. * @return void
  34. */
  35. public function remove( $extension_name ) {
  36. if ( array_key_exists( $extension_name, $this->extensions ) ) {
  37. unset( $this->extensions[ $extension_name ] );
  38. }
  39. }
  40. /**
  41. * Returns the extension for the given extension name.
  42. *
  43. * @param string $extension_name The name of the extension to get.
  44. *
  45. * @return null|WPSEO_Extension The extension object or null when it doesn't exist.
  46. */
  47. public function get( $extension_name ) {
  48. if ( array_key_exists( $extension_name, $this->extensions ) ) {
  49. return $this->extensions[ $extension_name ];
  50. }
  51. return null;
  52. }
  53. /**
  54. * Returns all set extension.
  55. *
  56. * @return WPSEO_Extension[] Array with the extensions.
  57. */
  58. public function get_all() {
  59. return $this->extensions;
  60. }
  61. /**
  62. * Checks if the plugin is activated within My Yoast.
  63. *
  64. * @param string $extension_name The extension name to check.
  65. *
  66. * @return bool True when the plugin is activated.
  67. */
  68. public function is_activated( $extension_name ) {
  69. if ( self::$active_extensions === null ) {
  70. // Force re-check on license & dashboard pages.
  71. $current_page = $this->get_current_page();
  72. // Check whether the licenses are valid or whether we need to show notifications.
  73. $exclude_cache = ( $current_page === 'wpseo_licenses' || $current_page === 'wpseo_dashboard' );
  74. // Fetch transient data on any other page.
  75. if ( ! $exclude_cache ) {
  76. self::$active_extensions = $this->get_cached_extensions();
  77. }
  78. // If the active extensions is still NULL, we need to set it.
  79. if ( ! is_array( self::$active_extensions ) ) {
  80. self::$active_extensions = $this->retrieve_active_extensions();
  81. $this->set_cached_extensions( self::$active_extensions );
  82. }
  83. }
  84. return in_array( $extension_name, self::$active_extensions, true );
  85. }
  86. /**
  87. * Retrieves the active extensions via an external request.
  88. *
  89. * @return array Array containing the active extensions.
  90. */
  91. protected function retrieve_active_extensions() {
  92. return (array) apply_filters( 'yoast-active-extensions', array() );
  93. }
  94. /**
  95. * Returns the current page.
  96. *
  97. * @return string The current page.
  98. */
  99. protected function get_current_page() {
  100. return filter_input( INPUT_GET, 'page' );
  101. }
  102. /**
  103. * Gets a cached list of active extensions.
  104. *
  105. * @return boolean|array The cached extensions.
  106. */
  107. protected function get_cached_extensions() {
  108. return get_transient( self::TRANSIENT_CACHE_KEY );
  109. }
  110. /**
  111. * Sets the active extensions transient for the set duration.
  112. *
  113. * @param array $extensions The extensions to add.
  114. * @param int $duration The duration that the list of extensions needs to remain cached.
  115. *
  116. * @return void
  117. */
  118. protected function set_cached_extensions( $extensions, $duration = DAY_IN_SECONDS ) {
  119. set_transient( self::TRANSIENT_CACHE_KEY, $extensions, $duration );
  120. }
  121. }