/wp-content/plugins/wordpress-seo/admin/config-ui/components/class-component-connect-google-search-console.php

https://bitbucket.org/carloskikea/helpet · PHP · 151 lines · 61 code · 24 blank · 66 comment · 1 complexity · 2d276e7ededee8784b20b38096f3fc8e MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Class WPSEO_Config_Component_Connect_Google_Search_Console
  9. */
  10. class WPSEO_Config_Component_Connect_Google_Search_Console implements WPSEO_Config_Component {
  11. const OPTION_ACCESS_TOKEN = 'wpseo-gsc-access_token';
  12. const OPTION_REFRESH_TOKEN = 'wpseo-gsc-refresh_token';
  13. /** @var WPSEO_GSC_Service Service to use */
  14. protected $gsc_service;
  15. /**
  16. * WPSEO_Config_Component_Connect_Google_Search_Console constructor.
  17. */
  18. public function __construct() {
  19. $this->gsc_service = new WPSEO_GSC_Service( $this->get_profile() );
  20. }
  21. /**
  22. * Set the Google Search Console service.
  23. *
  24. * @param WPSEO_GSC_Service $service Set service to use.
  25. */
  26. public function set_gsc_service( WPSEO_GSC_Service $service ) {
  27. $this->gsc_service = $service;
  28. }
  29. /**
  30. * Gets the component identifier.
  31. *
  32. * @return string
  33. */
  34. public function get_identifier() {
  35. return 'ConnectGoogleSearchConsole';
  36. }
  37. /**
  38. * Gets the field.
  39. *
  40. * @return WPSEO_Config_Field
  41. */
  42. public function get_field() {
  43. return new WPSEO_Config_Field_Connect_Google_Search_Console();
  44. }
  45. /**
  46. * Get the data for the field.
  47. *
  48. * @return mixed
  49. */
  50. public function get_data() {
  51. $data = array(
  52. 'profileList' => $this->get_profilelist(),
  53. 'profile' => $this->get_profile(),
  54. 'hasAccessToken' => $this->hasAccessToken(),
  55. );
  56. return $data;
  57. }
  58. /**
  59. * Save data
  60. *
  61. * @param array $data Data containing changes.
  62. *
  63. * @return mixed
  64. */
  65. public function set_data( $data ) {
  66. $current_data = $this->get_data();
  67. $this->handle_profile_change( $current_data, $data );
  68. // Save profile.
  69. $has_saved = update_option(
  70. WPSEO_GSC::OPTION_WPSEO_GSC,
  71. array( 'profile' => $data['profile'] )
  72. );
  73. // Collect results to return to the configurator.
  74. $results = array(
  75. 'profile' => $has_saved,
  76. );
  77. return $results;
  78. }
  79. /**
  80. * Remove issues when the profile has changed
  81. *
  82. * @param array $current_data Saved data before changes.
  83. * @param array $data Data after changes.
  84. */
  85. protected function handle_profile_change( $current_data, $data ) {
  86. // If the profile has been changed, remove issues.
  87. if ( $current_data['profile'] === $data['profile'] ) {
  88. return;
  89. }
  90. $this->reload_issues();
  91. }
  92. /**
  93. * Get the current GSC profile
  94. *
  95. * @return string
  96. */
  97. protected function get_profile() {
  98. return WPSEO_GSC_Settings::get_profile();
  99. }
  100. /**
  101. * Reload GSC issues
  102. */
  103. protected function reload_issues() {
  104. WPSEO_GSC_Settings::reload_issues();
  105. }
  106. /**
  107. * Gets a list with the profiles.
  108. *
  109. * @return array
  110. */
  111. protected function get_profilelist() {
  112. $profiles = array();
  113. $sites = $this->gsc_service->get_sites();
  114. foreach ( $sites as $site_key => $site_value ) {
  115. $profiles[ untrailingslashit( $site_key ) ] = untrailingslashit( $site_value );
  116. }
  117. return $profiles;
  118. }
  119. /**
  120. * Checks if there is an access token. If so, there is a connection.
  121. *
  122. * @return bool
  123. */
  124. private function hasAccessToken() {
  125. return ( null !== $this->gsc_service->get_client()->getAccessToken() );
  126. }
  127. }