class-gsc-service.php 4.9 KB

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