芝麻web文件管理V1.00
编辑当前文件:/home/ezdajrnh/public_html/wp-content/plugins/google-listings-and-ads/src/API/WP/OAuthService.php
get_data_from_google(); $store_url = urlencode_deep( admin_url( "admin.php?page=wc-admin&path={$path}" ) ); $state = $this->base64url_encode( build_query( [ 'nonce' => $google_data['nonce'], 'store_url' => $store_url, ] ) ); $auth_url = esc_url_raw( add_query_arg( [ 'blog' => Jetpack_Options::get_option( 'id' ), 'client_id' => $google_data['client_id'], 'redirect_uri' => $google_data['redirect_uri'], 'response_type' => self::RESPONSE_TYPE, 'scope' => self::SCOPE, 'state' => $state, ], $this->get_wpcom_api_url( self::AUTH_URL ) ) ); return $auth_url; } /** * Get a WPCOM REST API URl concatenating the endpoint with the API Domain * * @param string $endpoint The endpoint to get the URL for * * @return string The WPCOM endpoint with the domain. */ protected function get_wpcom_api_url( string $endpoint ): string { return self::WPCOM_API_URL . $endpoint; } /** * Calls an API by Google via WCS to get required information in order to form an auth URL. * * @return array{client_id: string, redirect_uri: string, nonce: string} An associative array contains required information that is retrieved from Google. * client_id: Google's WPCOM app client ID, will be used to form the authorization URL. * redirect_uri: A Google's URL that will be redirected to when the merchant approve the app access. Note that it needs to be matched with the Google WPCOM app client settings. * nonce: A string returned by Google that we will put it in the auth URL and the redirect_uri. Google will use it to verify the call. * @throws ContainerExceptionInterface When get_sdi_auth_params throws an exception. */ protected function get_data_from_google(): array { /** @var Middleware $middleware */ $middleware = $this->container->get( Middleware::class ); $response = $middleware->get_sdi_auth_params(); $nonce = $response['nonce']; $this->options->update( OptionsInterface::GOOGLE_WPCOM_AUTH_NONCE, $nonce ); return [ 'client_id' => $response['clientId'], 'redirect_uri' => $response['redirectUri'], 'nonce' => $nonce, ]; } /** * Perform a remote request for revoking OAuth access for the current user. * * @return string The body of the response * @throws Exception If the remote request fails. */ public function revoke_wpcom_api_auth(): string { $args = [ 'method' => 'DELETE', 'timeout' => 30, 'url' => $this->get_wpcom_api_url( '/wpcom/v2/sites/' . Jetpack_Options::get_option( 'id' ) . '/wc/partners/google/revoke-token' ), 'user_id' => get_current_user_id(), ]; $request = $this->container->get( Jetpack::class )->remote_request( $args ); if ( is_wp_error( $request ) ) { /** * When the WPCOM token has been revoked with errors. * * @event revoke_wpcom_api_authorization * @property int status The status of the request. * @property string error The error message. * @property int|null blog_id The blog ID. */ do_action( 'woocommerce_gla_track_event', 'revoke_wpcom_api_authorization', [ 'status' => 400, 'error' => $request->get_error_message(), 'blog_id' => Jetpack_Options::get_option( 'id' ), ] ); throw new Exception( $request->get_error_message(), 400 ); } else { $body = wp_remote_retrieve_body( $request ); $status = wp_remote_retrieve_response_code( $request ); if ( ! $status || $status !== 200 ) { $data = json_decode( $body, true ); $message = $data['message'] ?? 'Error revoking access to WPCOM.'; /** * * When the WPCOM token has been revoked with errors. * * @event revoke_wpcom_api_authorization * @property int status The status of the request. * @property string error The error message. * @property int|null blog_id The blog ID. */ do_action( 'woocommerce_gla_track_event', 'revoke_wpcom_api_authorization', [ 'status' => $status, 'error' => $message, 'blog_id' => Jetpack_Options::get_option( 'id' ), ] ); throw new Exception( $message, $status ); } /** * When the WPCOM token has been revoked successfully. * * @event revoke_wpcom_api_authorization * @property int status The status of the request. * @property int|null blog_id The blog ID. */ do_action( 'woocommerce_gla_track_event', 'revoke_wpcom_api_authorization', [ 'status' => 200, 'blog_id' => Jetpack_Options::get_option( 'id' ), ] ); $this->container->get( AccountService::class )->reset_wpcom_api_authorization_data(); return $body; } } /** * Deactivate the service. * * Revoke token on deactivation. */ public function deactivate(): void { // Try to revoke the token on deactivation. If no token is available, it will throw an exception which we can ignore. try { // Attempt to revoke the WPCOM token if setup is complete. if ( $this->is_jetpack_connected() ) { $this->revoke_wpcom_api_auth(); } } catch ( Exception $e ) { do_action( 'woocommerce_gla_error', sprintf( 'Error revoking the WPCOM token: %s', $e->getMessage() ), __METHOD__ ); } } }