WPSEO_GSC_Service

Class WPSEO_GSC_Service.

Defined (1)

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

/admin/google_search_console/class-gsc-service.php  
  1. class WPSEO_GSC_Service { 
  2.  
  3. /** 
  4. * @var Yoast_Api_Google_Client 
  5. */ 
  6. private $client; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. private $profile; 
  12.  
  13. /** 
  14. * Search Console service constructor. 
  15. * @param string $profile Profile name. 
  16. */ 
  17. public function __construct( $profile = '' ) { 
  18. $this->profile = $profile; 
  19.  
  20. $this->set_client(); 
  21.  
  22. /** 
  23. * Returns the client 
  24. * @return Yoast_Api_Google_Client 
  25. */ 
  26. public function get_client() { 
  27. return $this->client; 
  28.  
  29. /** 
  30. * Removes the option and calls the clients clear_data method to clear that one as well 
  31. */ 
  32. public function clear_data() { 
  33. // Clear client data. 
  34. $this->client->clear_data(); 
  35.  
  36. /** 
  37. * Get all sites that are registered in the GSC panel 
  38. * @return array 
  39. */ 
  40. public function get_sites() { 
  41. $sites = array(); 
  42.  
  43. $response_json = $this->client->do_request( 'sites', true ); 
  44.  
  45. // Do list sites request. 
  46. if ( ! empty( $response_json->siteEntry ) ) { 
  47. foreach ( $response_json->siteEntry as $entry ) { 
  48. $sites[ str_ireplace( 'sites/', '', (string) $entry->siteUrl ) ] = (string) $entry->siteUrl; 
  49.  
  50. // Sorting the retrieved sites. 
  51. asort( $sites ); 
  52.  
  53. return $sites; 
  54.  
  55. /** 
  56. * Get crawl issues 
  57. * @return array 
  58. */ 
  59. public function get_crawl_issue_counts() { 
  60. // Setup crawl error list. 
  61. $crawl_error_counts = $this->get_crawl_error_counts( $this->profile ); 
  62.  
  63. $return = array(); 
  64. if ( ! empty( $crawl_error_counts->countPerTypes ) ) { 
  65. foreach ( $crawl_error_counts->countPerTypes as $category ) { 
  66. $return[ $category->platform ][ $category->category ] = array( 
  67. 'count' => $category->entries[0]->count,  
  68. 'last_fetch' => null,  
  69. ); 
  70.  
  71. return $return; 
  72.  
  73. /** 
  74. * Sending request to mark issue as fixed 
  75. * @param string $url Issue URL. 
  76. * @param string $platform Platform (desktop, mobile, feature phone). 
  77. * @param string $category Issue type. 
  78. * @return bool 
  79. */ 
  80. public function mark_as_fixed( $url, $platform, $category ) { 
  81. $response = $this->client->do_request( 'sites/' . urlencode( $this->profile ) . '/urlCrawlErrorsSamples/' . urlencode( ltrim( $url, '/' ) ) . '?category=' . WPSEO_GSC_Mapper::category_to_api( $category ) . '&platform=' . WPSEO_GSC_Mapper::platform_to_api( $platform ) . '', false, 'DELETE' ); 
  82. return ( $response->getResponseHttpCode() === 204 ); 
  83.  
  84. /** 
  85. * Fetching the issues from the GSC API 
  86. * @param string $platform Platform (desktop, mobile, feature phone). 
  87. * @param string $category Issue type. 
  88. * @return mixed 
  89. */ 
  90. public function fetch_category_issues( $platform, $category ) { 
  91. $issues = $this->client->do_request( 
  92. 'sites/' . urlencode( $this->profile ) . '/urlCrawlErrorsSamples?category=' . $category . '&platform=' . $platform,  
  93. true 
  94. ); 
  95.  
  96. if ( ! empty( $issues->urlCrawlErrorSample ) ) { 
  97. return $issues->urlCrawlErrorSample; 
  98.  
  99. /** 
  100. * Setting the GSC client 
  101. */ 
  102. private function set_client() { 
  103. try { 
  104. new Yoast_Api_Libs( '2.0' ); 
  105. catch ( Exception $exception ) { 
  106. if ( $exception->getMessage() === 'required_version' ) { 
  107. $this->incompatible_api_libs( 
  108. __( 'Yoast plugins share some code between them to make your site faster. As a result of that, we need all Yoast plugins to be up to date. We\'ve detected this isn\'t the case, so please update the Yoast plugins that aren\'t up to date yet.', 'wordpress-seo' ) 
  109. ); 
  110.  
  111. if ( class_exists( 'Yoast_Api_Google_Client' ) === false ) { 
  112. $this->incompatible_api_libs( 
  113. sprintf( 
  114. /** translators: %1$s expands to Yoast SEO, %2$s expands to Google Analytics by Yoast */ 
  115. __( 
  116. '%1$s detected you*re using a version of %2$s which is not compatible with %1$s. Please update %2$s to the latest version to use this feature.',  
  117. 'wordpress-seo' 
  118. ),  
  119. 'Yoast SEO',  
  120. 'Google Analytics by Yoast' 
  121. ); 
  122.  
  123. wp_redirect( admin_url( 'admin.php?page=' . WPSEO_Admin::PAGE_IDENTIFIER ) ); 
  124. exit; 
  125.  
  126. $this->client = new Yoast_Api_Google_Client( WPSEO_GSC_Config::$gsc, 'wpseo-gsc', 'https://www.googleapis.com/webmasters/v3/' ); 
  127.  
  128. /** 
  129. * Adding notice that the api libs has the wrong version 
  130. * @param string $notice Message string. 
  131. */ 
  132. private function incompatible_api_libs( $notice ) { 
  133. Yoast_Notification_Center::get()->add_notification( 
  134. new Yoast_Notification( $notice, array( 'type' => Yoast_Notification::ERROR ) ) 
  135. ); 
  136.  
  137. /** 
  138. * Getting the crawl error counts 
  139. * @param string $profile Profile name string. 
  140. * @return object|bool 
  141. */ 
  142. private function get_crawl_error_counts( $profile ) { 
  143. $crawl_error_counts = $this->client->do_request( 
  144. 'sites/' . urlencode( $profile ) . '/urlCrawlErrorsCounts/query',  
  145. true 
  146. ); 
  147.  
  148. if ( ! empty( $crawl_error_counts ) ) { 
  149. return $crawl_error_counts; 
  150.  
  151. return false;