← All changes
|
includes/integrations/class-google-search-console-client.php
+16
-1
2.7.0
→
2.14.1
View file →
| @@ -61,10 +61,16 @@ | ||
| 61 | 61 | 'message' => 'Google Search Console API connection successful', |
| 62 | 62 | 'sites_count' => count($result['siteEntry'] ?? []) |
| 63 | 63 | ]; |
| 64 | 64 | } catch (\Exception $e) { |
| 65 | + // `message` as well as `error`: the success branch above reports | |
| 66 | + // its outcome as `message`, and every consumer reads that key, so | |
| 67 | + // a failure that only set `error` was reported to the admin as an | |
| 68 | + // error with no reason at all (#852). `error` is kept for any | |
| 69 | + // caller that already reads it. | |
| 65 | 70 | return [ |
| 66 | 71 | 'success' => false, |
| 72 | + 'message' => $e->getMessage(), | |
| 67 | 73 | 'error' => $e->getMessage() |
| 68 | 74 | ]; |
| 69 | 75 | } |
| 70 | 76 | } |
| @@ -242,12 +248,15 @@ | ||
| 242 | 248 | * @param string $site_url Site URL to get data for |
| 243 | 249 | * @param string $start_date Start date (Y-m-d) |
| 244 | 250 | * @param string $end_date End date (Y-m-d) |
| 245 | 251 | * @param int $row_limit Maximum rows to return |
| 252 | + * @param array $dimensions Dimensions to group by | |
| 253 | + * @param array $filters Optional dimension filters (ANDed), each | |
| 254 | + * {dimension, operator, expression}. @since 2.15.0 | |
| 246 | 255 | * @return array Raw rows from GSC API |
| 247 | 256 | * @throws \Exception If API request fails |
| 248 | 257 | */ |
| 249 | - public function get_search_performance_by_dates(string $site_url, string $start_date, string $end_date, int $row_limit = 500, array $dimensions = ['query']): array { | |
| 258 | + public function get_search_performance_by_dates(string $site_url, string $start_date, string $end_date, int $row_limit = 500, array $dimensions = ['query'], array $filters = []): array { | |
| 250 | 259 | $endpoint = '/sites/' . rawurlencode($site_url) . '/searchAnalytics/query'; |
| 251 | 260 | |
| 252 | 261 | $request_body = [ |
| 253 | 262 | 'startDate' => $start_date, |
| @@ -258,8 +267,14 @@ | ||
| 258 | 267 | // data — matches what the Search Console web UI displays, so the |
| 259 | 268 | // last 2-4 days aren't missing. |
| 260 | 269 | 'dataState' => 'all', |
| 261 | 270 | ]; |
| 271 | + | |
| 272 | + // Optional dimension filters, ANDed, e.g. one page's queries: | |
| 273 | + // [['dimension' => 'page', 'operator' => 'equals', 'expression' => $url]]. | |
| 274 | + if (!empty($filters)) { | |
| 275 | + $request_body['dimensionFilterGroups'] = [['filters' => array_values($filters)]]; | |
| 276 | + } | |
| 262 | 277 | |
| 263 | 278 | $full_url = self::API_BASE_URL . $endpoint; |
| 264 | 279 | $response = $this->make_request($full_url, $request_body, 'POST'); |
| 265 | 280 | |