3 * @package WPSEO\Admin|Google_Search_Console
7 * Class WPSEO_GSC_Service
9 class WPSEO_GSC_Service {
12 * @var Yoast_Api_Google_Client
24 * @param string $profile
26 public function __construct( $profile = '' ) {
27 $this->profile = $profile;
35 * @return Yoast_Api_Google_Client
37 public function get_client() {
42 * Removes the option and calls the clients clear_data method to clear that one as well
44 public function clear_data() {
46 $this->client->clear_data();
50 * Get all sites that are registered in the GSC panel
54 public function get_sites() {
57 $response_json = $this->client->do_request( 'sites', true );
59 // Do list sites request.
60 if ( ! empty( $response_json->siteEntry ) ) {
61 foreach ( $response_json->siteEntry as $entry ) {
62 $sites[ str_ireplace( 'sites/', '', (string) $entry->siteUrl ) ] = (string) $entry->siteUrl;
65 // Sorting the retrieved sites.
77 public function get_crawl_issue_counts() {
78 // Setup crawl error list.
79 $crawl_error_counts = $this->get_crawl_error_counts( $this->profile );
82 if ( ! empty( $crawl_error_counts->countPerTypes ) ) {
83 foreach ( $crawl_error_counts->countPerTypes as $category ) {
84 $return[ $category->platform ][ $category->category ] = array(
85 'count' => $category->entries[0]->count,
95 * Sending request to mark issue as fixed
98 * @param string $platform
99 * @param string $category
103 public function mark_as_fixed( $url, $platform, $category ) {
104 $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' );
105 return ( $response->getResponseHttpCode() === 204 );
109 * Fetching the issues from the GSC API
111 * @param string $platform
112 * @param string $category
116 public function fetch_category_issues( $platform, $category ) {
117 $issues = $this->client->do_request(
118 'sites/' . urlencode( $this->profile ) . '/urlCrawlErrorsSamples?category=' . $category . '&platform=' . $platform,
122 if ( ! empty( $issues->urlCrawlErrorSample ) ) {
123 return $issues->urlCrawlErrorSample;
128 * Setting the GSC client
130 private function set_client() {
132 new Yoast_Api_Libs( '2.0' );
134 catch ( Exception $exception ) {
135 if ( $exception->getMessage() === 'required_version' ) {
136 $this->incompatible_api_libs(
137 __( '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' )
142 if ( class_exists( 'Yoast_Api_Google_Client' ) === false ) {
143 $this->incompatible_api_libs(
144 /* translators: %1$s expands to Yoast SEO, %2$s expands to Google Analytics by Yoast */
147 '%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.',
151 'Google Analytics by Yoast'
155 wp_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) );
159 $this->client = new Yoast_Api_Google_Client( WPSEO_GSC_Config::$gsc, 'wpseo-gsc', 'https://www.googleapis.com/webmasters/v3/' );
163 * Adding notice that the api libs has the wrong version
165 * @param string $notice
167 private function incompatible_api_libs( $notice ) {
168 Yoast_Notification_Center::get()->add_notification(
169 new Yoast_Notification( $notice, array( 'type' => 'error' ) )
174 * Getting the crawl error counts
176 * @param string $profile
178 * @return object|bool
180 private function get_crawl_error_counts( $profile ) {
181 $crawl_error_counts = $this->client->do_request(
182 'sites/' . urlencode( $profile ) . '/urlCrawlErrorsCounts/query',
186 if ( ! empty( $crawl_error_counts ) ) {
187 return $crawl_error_counts;