From a38eeac4ef0c81e07854defddab271821f1fddf4 Mon Sep 17 00:00:00 2001 From: Miguel Peixe Date: Thu, 30 Jan 2025 15:04:26 -0300 Subject: [PATCH] chore: remove fivetran --- includes/class-newspack.php | 1 - includes/oauth/class-fivetran-connection.php | 193 ------------------- includes/oauth/class-oauth.php | 14 +- tests/unit-tests/oauth.php | 20 -- 4 files changed, 3 insertions(+), 225 deletions(-) delete mode 100644 includes/oauth/class-fivetran-connection.php diff --git a/includes/class-newspack.php b/includes/class-newspack.php index b750254537..dfd083b817 100644 --- a/includes/class-newspack.php +++ b/includes/class-newspack.php @@ -115,7 +115,6 @@ private function includes() { include_once NEWSPACK_ABSPATH . 'includes/oauth/class-google-oauth.php'; include_once NEWSPACK_ABSPATH . 'includes/oauth/class-google-services-connection.php'; include_once NEWSPACK_ABSPATH . 'includes/oauth/class-mailchimp-api.php'; - include_once NEWSPACK_ABSPATH . 'includes/oauth/class-fivetran-connection.php'; include_once NEWSPACK_ABSPATH . 'includes/oauth/class-google-login.php'; include_once NEWSPACK_ABSPATH . 'includes/class-blocks.php'; include_once NEWSPACK_ABSPATH . 'includes/tracking/class-pixel.php'; diff --git a/includes/oauth/class-fivetran-connection.php b/includes/oauth/class-fivetran-connection.php deleted file mode 100644 index c596751379..0000000000 --- a/includes/oauth/class-fivetran-connection.php +++ /dev/null @@ -1,193 +0,0 @@ - \WP_REST_Server::READABLE, - 'callback' => [ $this, 'api_get_fivetran_connection_status' ], - 'permission_callback' => [ $this, 'api_permissions_check' ], - ] - ); - register_rest_route( - NEWSPACK_API_NAMESPACE, - '/oauth/fivetran/(?P[\a-z]+)', - [ - 'methods' => \WP_REST_Server::EDITABLE, - 'callback' => [ $this, 'api_create_connection' ], - 'permission_callback' => [ $this, 'api_permissions_check' ], - 'args' => [ - 'service' => [ - 'required' => true, - 'sanitize_callback' => 'sanitize_text_field', - ], - ], - ] - ); - register_rest_route( - NEWSPACK_API_NAMESPACE, - '/oauth/fivetran-tos', - [ - 'methods' => \WP_REST_Server::EDITABLE, - 'callback' => [ $this, 'api_post_fivetran_tos' ], - 'permission_callback' => [ $this, 'api_permissions_check' ], - 'args' => [ - 'has_accepted' => [ - 'required' => true, - 'sanitize_callback' => 'rest_sanitize_boolean', - ], - ], - ] - ); - } - - /** - * Get Fivetran connections status. - */ - public static function api_get_fivetran_connection_status() { - $url = OAuth::authenticate_proxy_url( 'fivetran', '/wp-json/newspack-fivetran/v1/connections-status' ); - $connections_statuses = self::process_proxy_response( - \wp_safe_remote_get( - $url, - [ - 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout - ] - ) - ); - if ( is_wp_error( $connections_statuses ) ) { - return new WP_Error( - 'newspack_connections_fivetran', - $connections_statuses->get_error_message() - ); - } - $response = [ - 'connections_statuses' => $connections_statuses, - 'has_accepted_tos' => (bool) get_user_meta( get_current_user_id(), self::NEWSPACK_FIVETRAN_TOS_CONSENT_USER_META, true ), - ]; - return $response; - } - - /** - * Create a new connection. - * - * @param WP_REST_Request $request Request. - */ - public static function api_create_connection( $request ) { - $service = $request->get_param( 'service' ); - $service_data = []; - - $url = OAuth::authenticate_proxy_url( - 'fivetran', - '/wp-json/newspack-fivetran/v1/connect-card', - [ - 'service' => $service, - 'service_data' => $service_data, - 'redirect_after' => admin_url( 'admin.php?page=newspack-settings' ), - ] - ); - $response = self::process_proxy_response( \wp_safe_remote_post( $url, [ 'timeout' => 30 ] ) ); // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout - if ( is_wp_error( $response ) ) { - return $response; - } - return \rest_ensure_response( $response ); - } - - /** - * Update the user's consent for the TOS. - * - * @param WP_REST_Request $request Request. - */ - public static function api_post_fivetran_tos( $request ) { - update_user_meta( - get_current_user_id(), - self::NEWSPACK_FIVETRAN_TOS_CONSENT_USER_META, - sanitize_meta( - self::NEWSPACK_FIVETRAN_TOS_CONSENT_USER_META, - $request->get_param( 'has_accepted' ), - 'user' - ) - ); - return rest_ensure_response( [] ); - } - - /** - * Process the proxy response. - * - * @param object $result Result of a request. - * @return WP_Error|object Error or response data. - */ - private static function process_proxy_response( $result ) { - if ( is_wp_error( $result ) ) { - return $result; - } - if ( 400 <= $result['response']['code'] ) { - $error_body = json_decode( $result['body'] ); - $error_prefix = __( 'Fivetran proxy error', 'newspack' ); - if ( null !== $error_body && property_exists( $error_body, 'message' ) ) { - $error_message = $error_prefix . ': ' . $error_body->message; - } elseif ( null !== $error_body && property_exists( $error_body, 'data' ) ) { - $error_message = $error_prefix . ': ' . wp_json_encode( $error_body->data ); - } else { - $error_message = $error_prefix; - } - return new WP_Error( - 'newspack_connections_fivetran', - $error_message - ); - } - return json_decode( $result['body'] ); - } - - /** - * Check capabilities for using API. - * - * @codeCoverageIgnore - * @param WP_REST_Request $request API request object. - * @return bool|WP_Error - */ - public static function api_permissions_check( $request ) { - if ( ! current_user_can( 'manage_options' ) ) { - return new \WP_Error( - 'newspack_rest_forbidden', - esc_html__( 'You cannot use this resource.', 'newspack' ), - [ - 'status' => 403, - ] - ); - } - return true; - } -} -new Fivetran_Connection(); diff --git a/includes/oauth/class-oauth.php b/includes/oauth/class-oauth.php index 6fc37c8757..9b06822535 100644 --- a/includes/oauth/class-oauth.php +++ b/includes/oauth/class-oauth.php @@ -77,7 +77,7 @@ public static function retrieve_csrf_token( $namespace ) { /** * Process OAuth proxy URL. * - * @param string $type 'google' or 'fivetran' for now. + * @param string $type 'google' for now. * @param string $path Path to append to base URL. * @param array $query_args Query params. * @throws \Exception If trying to authenticate a non-existent proxy. @@ -103,7 +103,7 @@ public static function authenticate_proxy_url( string $type, string $path = '', /** * Is OAuth2 configured for this instance? * - * @param string $type 'google' or 'fivetran' for now. + * @param string $type 'google' for now. */ public static function is_proxy_configured( $type ) { return self::get_proxy_url( $type ) && self::get_proxy_api_key(); @@ -112,7 +112,7 @@ public static function is_proxy_configured( $type ) { /** * Get proxy URL by type. * - * @param string $type 'google' or 'fivetran' for now. + * @param string $type 'google' for now. */ private static function get_proxy_url( $type ) { switch ( $type ) { @@ -124,14 +124,6 @@ private static function get_proxy_url( $type ) { return NEWSPACK_GOOGLE_OAUTH_PROXY; } break; - case 'fivetran': - if ( defined( 'NEWSPACK_FIVETRAN_PROXY_OVERRIDE' ) ) { - return NEWSPACK_FIVETRAN_PROXY_OVERRIDE; - } - if ( defined( 'NEWSPACK_FIVETRAN_PROXY' ) ) { - return NEWSPACK_FIVETRAN_PROXY; - } - break; } return false; } diff --git a/tests/unit-tests/oauth.php b/tests/unit-tests/oauth.php index e9b657b934..341e320ef1 100644 --- a/tests/unit-tests/oauth.php +++ b/tests/unit-tests/oauth.php @@ -129,24 +129,4 @@ public function test_oauth_google() { 'OAuth2 object getter return false after credentials are removed.' ); } - - /** - * Fivetran OAuth flow. - */ - public function test_oauth_fivetran() { - self::expectException( Exception::class ); - self::assertFalse( - OAuth::authenticate_proxy_url( 'fivetran', '/wp-json/newspack-fivetran' ), - 'Proxy URL getting throws until configured.' - ); - self::set_api_key(); - if ( ! defined( 'NEWSPACK_FIVETRAN_PROXY' ) ) { - define( 'NEWSPACK_FIVETRAN_PROXY', 'http://dummy.proxy' ); - } - self::assertEquals( - 'http://dummy.proxy/wp-json/newspack-fivetran?api_key=123abc', - OAuth::authenticate_proxy_url( 'fivetran', '/wp-json/newspack-fivetran' ), - 'Proxy URL is as expected after proxy is configured.' - ); - } }