class-extension-manager.php 3.7 KB

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