/wp-content/plugins/wordpress-seo/admin/class-license-page-manager.php

https://bitbucket.org/carloskikea/helpet · PHP · 211 lines · 93 code · 32 blank · 86 comment · 10 complexity · c17924b3cb541a9312f7f439dc043daa MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Represents the values for a single Yoast Premium extension plugin.
  9. */
  10. class WPSEO_License_Page_Manager implements WPSEO_WordPress_Integration {
  11. /**
  12. * @var string Version number for License Page Manager.
  13. */
  14. const VERSION_LEGACY = '1';
  15. /**
  16. * @var string Version number for License Page Manager.
  17. */
  18. const VERSION_BACKWARDS_COMPATIBILITY = '2';
  19. /**
  20. * Registers all hooks to WordPress.
  21. */
  22. public function register_hooks() {
  23. add_filter( 'http_response', array( $this, 'handle_response' ), 10, 3 );
  24. if ( $this->get_version() === self::VERSION_BACKWARDS_COMPATIBILITY ) {
  25. add_filter( 'yoast-license-valid', '__return_true' );
  26. add_filter( 'yoast-show-license-notice', '__return_false' );
  27. add_action( 'admin_init', array( $this, 'validate_extensions' ), 15 );
  28. }
  29. else {
  30. add_action( 'admin_init', array( $this, 'remove_faulty_notifications' ), 15 );
  31. }
  32. }
  33. /**
  34. * Validates the extensions and show a notice for the invalid extensions.
  35. */
  36. public function validate_extensions() {
  37. if ( filter_input( INPUT_GET, 'page' ) === WPSEO_Admin::PAGE_IDENTIFIER ) {
  38. /**
  39. * Filter: 'yoast-active-extensions' - Collects all active extensions. This hook is implemented in the
  40. * license manager.
  41. *
  42. * @api array $extensions The array with extensions.
  43. */
  44. apply_filters( 'yoast-active-extensions', array() );
  45. }
  46. $extension_list = new WPSEO_Extensions();
  47. $extensions = $extension_list->get();
  48. $notification_center = Yoast_Notification_Center::get();
  49. foreach ( $extensions as $product_name ) {
  50. $notification = $this->create_notification( $product_name );
  51. // Add a notification when the installed plugin isn't activated in My Yoast.
  52. if ( $extension_list->is_installed( $product_name ) && ! $extension_list->is_valid( $product_name ) ) {
  53. $notification_center->add_notification( $notification );
  54. continue;
  55. }
  56. $notification_center->remove_notification( $notification );
  57. }
  58. }
  59. /**
  60. * Removes the faulty set notifications.
  61. */
  62. public function remove_faulty_notifications() {
  63. $extension_list = new WPSEO_Extensions();
  64. $extensions = $extension_list->get();
  65. $notification_center = Yoast_Notification_Center::get();
  66. foreach ( $extensions as $product_name ) {
  67. $notification = $this->create_notification( $product_name );
  68. $notification_center->remove_notification( $notification );
  69. }
  70. }
  71. /**
  72. * Handles the response.
  73. *
  74. * @param array $response HTTP response.
  75. * @param array $request_arguments HTTP request arguments. Unused.
  76. * @param string $url The request URL.
  77. *
  78. * @return array The response array.
  79. */
  80. public function handle_response( array $response, $request_arguments, $url ) {
  81. $response_code = wp_remote_retrieve_response_code( $response );
  82. if ( $response_code === 200 && $this->is_expected_endpoint( $url ) ) {
  83. $response_data = $this->parse_response( $response );
  84. $this->detect_version( $response_data );
  85. }
  86. return $response;
  87. }
  88. /**
  89. * Returns the license page to use based on the version number.
  90. *
  91. * @return string The page to use.
  92. */
  93. public function get_license_page() {
  94. return 'licenses';
  95. }
  96. /**
  97. * Returns the version number of the license server.
  98. *
  99. * @return int The version number
  100. */
  101. protected function get_version() {
  102. return get_option( $this->get_option_name(), self::VERSION_BACKWARDS_COMPATIBILITY );
  103. }
  104. /**
  105. * Returns the option name.
  106. *
  107. * @return string The option name.
  108. */
  109. protected function get_option_name() {
  110. return 'wpseo_license_server_version';
  111. }
  112. /**
  113. * Sets the version when there is a value in the response.
  114. *
  115. * @param array $response The response to extract the version from.
  116. */
  117. protected function detect_version( $response ) {
  118. if ( ! empty( $response['serverVersion'] ) ) {
  119. $this->set_version( $response['serverVersion'] );
  120. }
  121. }
  122. /**
  123. * Sets the version.
  124. *
  125. * @param string $server_version The version number to save.
  126. */
  127. protected function set_version( $server_version ) {
  128. update_option( $this->get_option_name(), $server_version );
  129. }
  130. /**
  131. * Parses the response by getting its body and do a unserialize of it.
  132. *
  133. * @param array $response The response to parse.
  134. *
  135. * @return mixed|string|false The parsed response.
  136. */
  137. protected function parse_response( $response ) {
  138. $response = json_decode( wp_remote_retrieve_body( $response ), true );
  139. $response = maybe_unserialize( $response );
  140. return $response;
  141. }
  142. /**
  143. * Checks if the given url matches the expected endpoint.
  144. *
  145. * @param string $url The url to check.
  146. *
  147. * @return bool True when url matches the endpoint.
  148. */
  149. protected function is_expected_endpoint( $url ) {
  150. $url_parts = wp_parse_url( $url );
  151. $is_yoast_com = ( in_array( $url_parts['host'], array( 'yoast.com', 'my.yoast.com' ), true ) );
  152. $is_edd_api = ( isset( $url_parts['path'] ) && $url_parts['path'] === '/edd-sl-api' );
  153. return $is_yoast_com && $is_edd_api;
  154. }
  155. /**
  156. * Creates an instance of Yoast_Notification
  157. *
  158. * @param string $product_name The product to create the notification for.
  159. *
  160. * @return Yoast_Notification The created notification.
  161. */
  162. protected function create_notification( $product_name ) {
  163. $notification_options = array(
  164. 'type' => Yoast_Notification::ERROR,
  165. 'id' => 'wpseo-dismiss-' . sanitize_title_with_dashes( $product_name, null, 'save' ),
  166. 'capabilities' => 'wpseo_manage_options',
  167. );
  168. $notification = new Yoast_Notification(
  169. sprintf(
  170. /* translators: %1$s expands to the product name. %2$s expands to a link to My Yoast */
  171. __( 'You are not receiving updates or support! Fix this problem by adding this site and enabling %1$s for it in %2$s.', 'wordpress-seo' ),
  172. $product_name,
  173. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/13j' ) . '" target="_blank">My Yoast</a>'
  174. ),
  175. $notification_options
  176. );
  177. return $notification;
  178. }
  179. }