400 ) ); } if ( ( $data['type'] ?? '' ) !== 'service_account' ) { return new WP_Error( 'airanksuite_indexing_wrong_type', __( 'This key is not of type "service_account".', 'ai-rank-suite' ), array( 'status' => 400 ) ); } update_option( self::OPT_ACCOUNT, Secret::encrypt( $json ), false ); delete_option( self::OPT_TOKEN ); return sanitize_email( (string) $data['client_email'] ); } public function clear_account(): void { delete_option( self::OPT_ACCOUNT ); delete_option( self::OPT_TOKEN ); } /** @return array|null */ private function account(): ?array { $stored = get_option( self::OPT_ACCOUNT ); if ( ! is_string( $stored ) || '' === $stored ) { return null; } $json = Secret::decrypt( $stored ); if ( null === $json ) { return null; } $data = json_decode( $json, true ); return is_array( $data ) ? $data : null; } public function is_configured(): bool { return null !== $this->account(); } public function client_email(): string { $account = $this->account(); return $account ? (string) ( $account['client_email'] ?? '' ) : ''; } /** * Fetch (and cache) an access token via a signed JWT assertion. */ private function token(): string|WP_Error { $cached = get_option( self::OPT_TOKEN ); if ( is_array( $cached ) && ( $cached['expires'] ?? 0 ) > time() + 60 ) { $token = Secret::decrypt( (string) $cached['token'] ); if ( null !== $token ) { return $token; } } $account = $this->account(); if ( null === $account ) { return new WP_Error( 'airanksuite_indexing_not_configured', __( 'Upload a Google service account key first.', 'ai-rank-suite' ), array( 'status' => 400 ) ); } if ( ! function_exists( 'openssl_sign' ) ) { return new WP_Error( 'airanksuite_indexing_no_openssl', __( 'The OpenSSL PHP extension is required to sign Google requests. Ask your host to enable it.', 'ai-rank-suite' ), array( 'status' => 500 ) ); } $now = time(); $header = self::b64( (string) wp_json_encode( array( 'alg' => 'RS256', 'typ' => 'JWT', ) ) ); $claims = self::b64( (string) wp_json_encode( array( 'iss' => $account['client_email'], 'scope' => self::SCOPE, 'aud' => 'https://oauth2.googleapis.com/token', 'exp' => $now + 3600, 'iat' => $now, ) ) ); $signature = ''; $signed = openssl_sign( $header . '.' . $claims, $signature, (string) $account['private_key'], 'sha256WithRSAEncryption' ); if ( ! $signed ) { return new WP_Error( 'airanksuite_indexing_sign_failed', __( 'Could not sign the request — the private key in the key file appears to be invalid.', 'ai-rank-suite' ), array( 'status' => 500 ) ); } $assertion = $header . '.' . $claims . '.' . self::b64( $signature ); $response = wp_remote_post( 'https://oauth2.googleapis.com/token', array( 'timeout' => 20, 'body' => array( 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $assertion, ), ) ); if ( $response instanceof WP_Error ) { return $response; } $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $body ) || empty( $body['access_token'] ) ) { $msg = is_array( $body ) ? (string) ( $body['error_description'] ?? $body['error'] ?? '' ) : ''; return new WP_Error( 'airanksuite_indexing_token_failed', '' !== $msg ? $msg : __( 'Google did not return an access token.', 'ai-rank-suite' ), array( 'status' => 502 ) ); } update_option( self::OPT_TOKEN, array( 'token' => Secret::encrypt( (string) $body['access_token'] ), 'expires' => $now + (int) ( $body['expires_in'] ?? 3600 ), ), false ); return (string) $body['access_token']; } private static function b64( string $value ): string { return rtrim( strtr( base64_encode( $value ), '+/', '-_' ), '=' ); } /** * Notify Google about a URL. * * @param string $url Absolute URL on this site. * @param string $type URL_UPDATED | URL_DELETED. */ public function publish( string $url, string $type = 'URL_UPDATED' ): bool|WP_Error { $type = 'URL_DELETED' === $type ? 'URL_DELETED' : 'URL_UPDATED'; // Only ever submit URLs belonging to this site. $host = wp_parse_url( $url, PHP_URL_HOST ); if ( $host !== wp_parse_url( home_url(), PHP_URL_HOST ) ) { return new WP_Error( 'airanksuite_indexing_foreign_url', __( 'Only URLs on this site can be submitted.', 'ai-rank-suite' ), array( 'status' => 400 ) ); } if ( $this->used_today() >= self::DAILY_QUOTA ) { return new WP_Error( 'airanksuite_indexing_quota', sprintf( /* translators: %d: daily quota */ __( 'Google\'s daily quota of %d URLs is used up. It resets at midnight Pacific time.', 'ai-rank-suite' ), self::DAILY_QUOTA ), array( 'status' => 429 ) ); } $token = $this->token(); if ( $token instanceof WP_Error ) { return $token; } $response = wp_remote_post( 'https://indexing.googleapis.com/v3/urlNotifications:publish', array( 'timeout' => 20, 'headers' => array( 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ), 'body' => (string) wp_json_encode( array( 'url' => $url, 'type' => $type, ) ), ) ); if ( $response instanceof WP_Error ) { $this->log( $url, $type, false, $response->get_error_message() ); return $response; } $code = (int) wp_remote_retrieve_response_code( $response ); $body = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( $code >= 400 ) { $msg = is_array( $body ) ? (string) ( $body['error']['message'] ?? '' ) : ''; $this->log( $url, $type, false, $msg ); return new WP_Error( 'airanksuite_indexing_failed', '' !== $msg ? $msg : sprintf( 'Google Indexing API error (HTTP %d).', $code ), array( 'status' => 502 ) ); } $this->log( $url, $type, true, '' ); return true; } /** * Append to the ring-buffer log. */ private function log( string $url, string $type, bool $ok, string $error ): void { $log = get_option( self::OPT_LOG ); $log = is_array( $log ) ? $log : array(); array_unshift( $log, array( 'url' => esc_url_raw( $url ), 'type' => $type, 'ok' => $ok, 'error' => mb_substr( $error, 0, 200 ), 'time' => time(), ) ); update_option( self::OPT_LOG, array_slice( $log, 0, self::LOG_MAX ), false ); } /** * Successful submissions in the last 24 hours. */ public function used_today(): int { $log = get_option( self::OPT_LOG ); $cutoff = time() - DAY_IN_SECONDS; $count = 0; foreach ( (array) ( is_array( $log ) ? $log : array() ) as $entry ) { if ( ! empty( $entry['ok'] ) && (int) ( $entry['time'] ?? 0 ) >= $cutoff ) { ++$count; } } return $count; } public function quota(): int { return self::DAILY_QUOTA; } /** * @return array> */ public function recent(): array { $log = get_option( self::OPT_LOG ); return array_slice( is_array( $log ) ? $log : array(), 0, 50 ); } }