'GET', 'callback' => array( $this, 'status' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/gsc/credentials', array( 'methods' => 'PUT', 'callback' => array( $this, 'credentials' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'client_id' => array( 'type' => 'string', 'required' => true, ), 'client_secret' => array( 'type' => 'string', 'required' => true, ), ), ) ); register_rest_route( self::NAMESPACE, '/gsc/connect-url', array( 'methods' => 'GET', 'callback' => fn() => $this->wrap( $this->client->auth_url(), static fn( $url ) => array( 'url' => $url ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); // Sign in with Google through the vendor's connect service — no Cloud // project on the customer's side. Answers 400 on builds without one. register_rest_route( self::NAMESPACE, '/gsc/signin-url', array( 'methods' => 'GET', 'callback' => fn() => $this->wrap( $this->client->proxy_start_url(), static fn( $url ) => array( 'url' => $url ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); // One call after connecting: match this site's Search Console // property and GA4 account automatically. The wizard uses it so a // person never has to know what a "property" is. register_rest_route( self::NAMESPACE, '/gsc/auto-configure', array( 'methods' => 'POST', 'callback' => fn() => new \WP_REST_Response( $this->client->auto_configure() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/gsc/sites', array( 'methods' => 'GET', 'callback' => fn() => $this->wrap( $this->client->sites(), static fn( $sites ) => array( 'sites' => $sites ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/gsc/property', array( 'methods' => 'PUT', 'callback' => array( $this, 'property' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'site' => array( 'type' => 'string', 'required' => true, ), ), ) ); register_rest_route( self::NAMESPACE, '/gsc/performance', array( 'methods' => 'GET', 'callback' => array( $this, 'performance' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'days' => array( 'type' => 'integer', 'default' => 28, ), ), ) ); register_rest_route( self::NAMESPACE, '/gsc/disconnect', array( 'methods' => 'POST', 'callback' => array( $this, 'disconnect' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); } public function status(): WP_REST_Response { return new WP_REST_Response( array( 'has_credentials' => $this->client->has_credentials(), 'connected' => $this->client->connected(), 'mode' => $this->client->mode(), 'signin_available' => GoogleClient::proxy_available(), 'property' => $this->client->property(), 'redirect_uri' => $this->client->redirect_uri(), ) ); } public function credentials( WP_REST_Request $request ): WP_REST_Response { $this->client->save_credentials( (string) $request['client_id'], (string) $request['client_secret'] ); return $this->status(); } public function property( WP_REST_Request $request ): WP_REST_Response { $this->client->set_property( (string) $request['site'] ); return $this->status(); } public function performance( WP_REST_Request $request ): WP_REST_Response|WP_Error { $days = in_array( (int) $request['days'], array( 7, 28, 90 ), true ) ? (int) $request['days'] : 28; $data = $this->client->performance( $days ); return $data instanceof WP_Error ? $data : new WP_REST_Response( $data ); } public function disconnect(): WP_REST_Response { $this->client->disconnect(); return $this->status(); } /** * @param mixed $result Value or WP_Error. * @param callable $shape Shapes the success payload. */ private function wrap( mixed $result, callable $shape ): WP_REST_Response|WP_Error { return $result instanceof WP_Error ? $result : new WP_REST_Response( $shape( $result ) ); } }