WPSEO_Config_Component_Connect_Google_Search_Console

Class WPSEO_Config_Component_Connect_Google_Search_Console.

Defined (1)

The class is defined in the following location(s).

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