From 47afbc7e4e6c9ab70075c496c0aa52cdc3573a82 Mon Sep 17 00:00:00 2001 From: Shea Bunge Date: Fri, 24 Jan 2025 15:30:32 +1100 Subject: [PATCH] Remove unused cloud API methods. --- src/php/cloud/class-cloud-api.php | 427 ------------------------------ 1 file changed, 427 deletions(-) diff --git a/src/php/cloud/class-cloud-api.php b/src/php/cloud/class-cloud-api.php index ae19c3fd..ab242069 100644 --- a/src/php/cloud/class-cloud-api.php +++ b/src/php/cloud/class-cloud-api.php @@ -148,15 +148,6 @@ private static function get_cloud_settings(): array { return $settings; } - /** - * Retrieve the current cloud connection state. - * - * @return string - */ - public static function get_current_state(): string { - return self::get_cloud_setting( 'state' ) ?? ''; - } - /** * Retrieve the cloud local token. * @@ -361,55 +352,6 @@ public function allow_cloud_redirects( array $allowed_hosts ): array { return $allowed_hosts; } - /** - * Check Cloud Connection is Available or Establish New Connection - * - * @return array - */ - public function ensure_cloud_connection_available(): array { - // Check if cloud connection is already available. - if ( $this->is_cloud_connection_available() ) { - return [ - 'success' => true, - 'redirect-slug' => 'success', - ]; - } - - if ( ! $this->is_cloud_key_verified() ) { - return [ - 'success' => false, - 'redirect-slug' => 'not-connected', - ]; - } - - // Establish new cloud connection. - $cloud_connection = $this->establish_new_cloud_connection(); - - if ( 'no_codevault' === $cloud_connection['message'] ) { - return [ - 'success' => false, - 'redirect-slug' => 'no-codevault', - ]; - } - - // Check if the connection was successful. - if ( ! $cloud_connection['success'] ) { - // If not successful return the error message. - return [ - 'success' => false, - 'redirect-slug' => 'invalid', - 'message' => $cloud_connection['message'], - ]; - } - - $this->update_cloud_settings( [ 'token_verified' => true ] ); - - return [ - 'success' => true, - 'redirect-slug' => 'success', - ]; - } - /** * Get ownership and Cloud ID of a snippet. * @@ -451,185 +393,6 @@ private static function unpack_request_json( $response ): ?array { return $body ? json_decode( $body, true ) : null; } - /** - * Establish new connection to the cloud platform. - * - * @return array - success, message, - */ - public function establish_new_cloud_connection(): array { - $local_token = $this->get_local_token(); - $cloud_key = $this->get_cloud_key(); - - // Send POST request to CLOUD_API_URL . 'private/syncandverify' with site_token and site_host as form data. - $response = wp_remote_post( - self::get_cloud_api_url() . 'private/syncandverify', - [ - 'method' => 'POST', - 'headers' => [ - 'Authorization' => 'Bearer ' . $cloud_key, - 'Local-Token' => $local_token, - 'Access-Control-Allow-Origin' => '*', - 'Accept' => 'application/json', - ], - 'body' => [ - 'site_token' => $local_token, - 'site_host' => wp_parse_url( get_site_url(), PHP_URL_HOST ), - ], - ] - ); - - // Check the response codes and return accordingly. - if ( 401 === wp_remote_retrieve_response_code( $response ) ) { - return [ - 'success' => false, - 'message' => 'That token is invalid - please check and try again.', - ]; - } - - if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { - return [ - 'success' => false, - 'message' => 'There was an error connecting to the cloud platform. Please try again later.', - ]; - } - - $data = self::unpack_request_json( $response ); - - if ( isset( $data['sync_status'] ) ) { - if ( 'error' === $data['sync_status'] ) { - return [ - 'success' => false, - 'message' => strpos( $data['message'], 'No Codevault!' ) !== false ? - 'no_codevault' : - $data['message'], - ]; - } elseif ( 'success' === $data['sync_status'] ) { - return [ - 'success' => true, - 'message' => $data['message'], - ]; - } - } - - return [ - 'success' => false, - 'message' => 'There was an unknown error, please try again later.', - ]; - } - - /** - * Generate the client ID from the current local token and site URL. - * - * @return string Client ID. - */ - private function get_client_id(): string { - $local_token = $this->get_cloud_setting( 'local_token' ); - $site_host = wp_parse_url( get_site_url(), PHP_URL_HOST ); - return "$site_host-$local_token"; - } - - /** - * Verify that a request to connect to or disconnect from cloud is genuine. - * - * @param string|null $nonce_value Value of nonce to verify, or null if nonce has already been verified. - * - * @return bool - */ - public function verify_action_nonce( string $nonce_value ): bool { - return wp_verify_nonce( $nonce_value, self::CLOUD_ACTION_NONCE ); - } - - /** - * Initialise the process for connecting to Cloud. - * - * Use verify_connection_nonce() or similar validation before calling this function. - * - * @return void - */ - public function init_cloud_connection() { - $callback_url = add_query_arg( - [ 'confirm-authorise-cloud' => true ], - code_snippets()->get_menu_url( 'settings' ) - ); - - $url = add_query_arg( - [ - 'response_type' => 'code', - 'client_id' => $this->get_client_id(), - 'code_challenge' => $this->get_cloud_setting( 'code_challenge' ), - 'state' => $this->get_current_state(), - 'callback_url' => esc_url_raw( $callback_url ), - ], - self::get_cloud_url() . 'oauth/login' - ); - - wp_safe_redirect( esc_url_raw( $url ) ); - exit; - } - - /** - * Exchange the auth code for a bearer token. - * - * @param string $state Received state. - * @param string $auth_code Authorisation code. - * - * @return WP_Error|null Error on failure, null on success. - */ - public function decode_auth_code( string $state, string $auth_code ): ?WP_Error { - if ( self::get_current_state() !== $state ) { - return new WP_Error( - 'snippets_cloud_invalid_state', - __( 'Did not receive a valid state from Code Snippets Cloud. Please try again.', 'code-snippets' ) - ); - } - - $response = wp_remote_post( - self::get_cloud_api_url() . 'auth/token', - [ - 'method' => 'POST', - 'headers' => [ - 'Accept' => 'application/json', - 'Local-Token' => $this->get_cloud_setting( 'local_token' ), - 'Access-Control-Allow-Origin' => '*', - ], - 'body' => [ - 'code' => $auth_code, - 'client_id' => $this->get_client_id(), - 'grant_type' => 'authorization_code', - 'code_verifier' => $this->get_cloud_setting( 'code_verifier' ), - ], - ] - ); - - $body = wp_remote_retrieve_body( $response ); - $data = json_decode( $body, true ); - - // Check the response codes and return accordingly. - if ( 401 === wp_remote_retrieve_response_code( $response ) ) { - return new WP_Error( - 'snippets_cloud_invalid_token', - esc_html__( 'That token is invalid – please check and try again.', 'code-snippets' ) - ); - } - - if ( empty( $data['token'] ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { - return new WP_Error( - 'snippets_cloud_connection_error', - esc_html__( 'There was an error connecting to the cloud platform. Please try again later.', 'code-snippets' ) - ); - } - - // Save the token in code snippets cloud settings. - $this->update_cloud_settings( - [ - 'cloud_token' => $data['token'], - 'token_verified' => true, - ] - ); - - return null; - } - /** * Retrieves a list of all snippets from the cloud API. * @@ -729,84 +492,6 @@ public function add_cloud_link( Cloud_Link $link ) { ); } - /** - * Upload a series of local snippets to the cloud platform. - * - * @param Snippet[] $snippets List of code snippets to store. - */ - public function store_snippets_in_cloud( array $snippets ) { - foreach ( $snippets as $snippet ) { - $snippet->desc = wp_strip_all_tags( $snippet->desc ); - - // Send post request to cs store api with snippet data. - $response = wp_remote_post( - self::get_cloud_api_url() . 'private/storesnippet', - [ - 'method' => 'POST', - 'headers' => $this->build_request_headers(), - 'body' => [ - 'name' => $snippet->name, - 'desc' => $snippet->desc, - 'code' => $snippet->code, - 'scope' => $snippet->scope, - 'revision' => $snippet->revision, - ], - ] - ); - - $data = $this->unpack_request_json( $response ); - $cloud_id = (string) $data['cloud_id']; - $revision = (int) $data['revision']; - - // Update the stored local snippet information. - update_snippet_fields( - $snippet->id, - array( - 'cloud_id' => $cloud_id, - 'revision' => $revision, - ) - ); - - $this->clear_caches(); - } - } - - /** - * Update the already-existing remote data for a series of snippets. - * - * @param Snippet[] $snippets_to_update List of snippets to update. - * - * @return void - */ - public function update_snippets_in_cloud( array $snippets_to_update ) { - foreach ( $snippets_to_update as $snippet ) { - $cloud_id_owner = $this->get_cloud_id_and_ownership( $snippet->cloud_id ); - $cloud_id = (int) $cloud_id_owner['cloud_id']; - - // Send post request to cs store api with snippet data. - $response = wp_remote_post( - self::get_cloud_api_url() . 'private/updatesnippet/' . $cloud_id, - [ - 'method' => 'POST', - 'headers' => $this->build_request_headers(), - 'body' => [ - 'name' => $snippet->name, - 'desc' => $snippet->desc, - 'code' => $snippet->code, - 'revision' => $snippet->revision, - 'local_id' => $snippet->id, - ], - ] - ); - - $updated = $this->unpack_request_json( $response ); - - if ( $updated && $updated['success'] ) { - $this->clear_caches(); - } - } - } - /** * Delete a snippet from local-to-cloud map. * @@ -869,67 +554,6 @@ public static function get_cloud_snippet_revision( string $cloud_id ): ?string { return $cloud_snippet_revision['snippet_revision'] ?? null; } - /** - * Get list of all bundles from the cloud API. - * - * @return array|null Bundle name and id, null otherwise. - */ - public static function get_bundles(): ?array { - $response = wp_remote_get( - self::get_cloud_api_url() . 'private/bundles', - [ 'headers' => self::build_request_headers() ] - ); - return self::unpack_request_json( $response ); - } - - /** - * Get List of Snippets from a Bundle from the cloud API. - * - * @param int $bundle_id Bundle ID. - * - * @return Cloud_Snippets - */ - public function get_snippets_from_bundle( int $bundle_id ): Cloud_Snippets { - $api_url = self::get_cloud_api_url() . sprintf( 'private/getbundle/%s', $bundle_id ); - $response = wp_remote_post( - $api_url, - [ - 'method' => 'POST', - 'headers' => $this->build_request_headers(), - ] - ); - - $results = self::unpack_request_json( $response ); - $results = new Cloud_Snippets( $results ); - $results->page = 1; - - return $results; - } - - /** - * Get List of Snippets from a Shared Bundle from the cloud API. - * - * @param string $bundle_share_name Bundle share name. - * - * @return Cloud_Snippets - */ - public function get_snippets_from_shared_bundle( string $bundle_share_name ): Cloud_Snippets { - $api_url = self::get_cloud_api_url() . sprintf( 'private/getsharedbundle?share_name=%s', $bundle_share_name ); - $response = wp_remote_post( - $api_url, - [ - 'method' => 'POST', - 'headers' => $this->build_request_headers(), - ] - ); - - $results = self::unpack_request_json( $response ); - $results = new Cloud_Snippets( $results ); - $results->page = 1; - - return $results; - } - /** * Download a snippet from the cloud. * @@ -1109,27 +733,6 @@ public function update_snippet_from_cloud( Cloud_Snippet $snippet_to_store ): ar ]; } - /** - * Find the cloud link for a given local snippet identifier. - * - * @param Snippet $snippet Local snippet. - * - * @return Cloud_Link|null - */ - public function get_link_for_snippet( Snippet $snippet ): ?Cloud_Link { - $cloud_links = $this->get_cloud_links(); - - if ( $cloud_links ) { - foreach ( $cloud_links as $cloud_link ) { - if ( $cloud_link->local_id === $snippet->id ) { - return $cloud_link; - } - } - } - - return null; - } - /** * Find the cloud link for a given cloud snippet identifier. * @@ -1227,23 +830,6 @@ public static function render_cloud_snippet_thickbox() { update_cloud_settings( - [ - 'cloud_token' => '', - 'token_verified' => false, - 'local_token' => '', - 'code_challenge' => '', - 'code_verifier' => '', - ] - ); - - $this->clear_caches(); - } - /** * Refresh the cached synced data. * @@ -1256,17 +842,4 @@ public function clear_caches() { delete_transient( self::CLOUD_MAP_TRANSIENT_KEY ); delete_transient( self::CODEVAULT_SNIPPETS_TRANSIENT_KEY ); } - - /** - * Unsync local snippets from the cloud - * - * @param Snippet[] $snippets List of code snippets to remove sync. - */ - public function remove_snippets_from_cloud( array $snippets ) { - foreach ( $snippets as $snippet ) { - update_snippet_fields( $snippet->id, [ 'cloud_id' => null ], $snippet->network ); - - $this->delete_snippet_from_transient_data( $snippet->id ); - } - } }