__( 'The Zip AI Endpoint was not declared', 'zip-ai' ), ); } // Get the Auth Token from the Zip AI Settings. $auth_token = self::get_decrypted_auth_token(); // If the Zip Auth Token is not set, then abandon ship. if ( empty( $auth_token ) || ! is_string( $auth_token ) ) { return array( 'error' => __( 'The Zip AI Auth Token is not set.', 'zip-ai' ), ); } // Set the API URL. $api_url = ZIP_AI_CREDIT_SERVER_API . $endpoint; // Get the response from the endpoint. $response = wp_remote_post( $api_url, array( 'headers' => array( 'Authorization' => 'Bearer ' . $auth_token, ), 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- 30 seconds is required sometime for open ai responses ) ); // If the response was an error, or not a 200 status code, then abandon ship. if ( is_wp_error( $response ) || empty( $response['response'] ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { return array( 'error' => __( 'The Zip AI Middleware is not responding.', 'zip-ai' ), ); } // Get the response body. $response_body = wp_remote_retrieve_body( $response ); // If the response body is not a JSON, then abandon ship. if ( empty( $response_body ) || ! json_decode( $response_body ) ) { return array( 'error' => __( 'The Zip AI Middleware encountered an error.', 'zip-ai' ), ); } // Return the response body. return json_decode( $response_body, true ); } /** * Get the decrypted auth token. * * @since 1.0.0 * @return string The decrypted auth token. */ public static function get_decrypted_auth_token() { // Get the Zip AI Settings. $auth_token = self::get_setting( 'auth_token' ); // Return early if the auth token is not set. if ( empty( $auth_token ) || ! is_string( $auth_token ) ) { return ''; } // Return the decrypted auth token. return ! empty( trim( $auth_token ) ) ? Utils::decrypt( $auth_token ) : ''; } /** * This helper function returns credit details. * * @since 1.0.0 * @return array */ public static function get_credit_details() { // Set the default credit details. $credit_details = array( 'used' => 0, 'total' => 0, 'threshold' => array( 'medium' => ZIP_AI_CREDIT_THRESHOLD_MEDIUM, 'high' => ZIP_AI_CREDIT_THRESHOLD_HIGH, ), 'percentage' => 0, 'status' => 'success', ); // Get the response from the endpoint. $response = self::get_credit_server_response( 'usage' ); // If the response is not an error, then update the credit details. if ( empty( $response['error'] ) && ! empty( $response['total_credits'] ) ) { $credit_details['used'] = ! empty( $response['total_used_credits'] ) ? $response['total_used_credits'] : 0; $credit_details['total'] = $response['total_credits']; $credit_details['percentage'] = intval( ( $credit_details['used'] / $credit_details['total'] ) * 100 ); } else { $credit_details['status'] = 'error'; } return $credit_details; } /** * Get the authorization middleware url. * * @since 1.0.0 * @return string The authorization middleware url. */ public static function get_auth_middleware_url() { $auth_url = add_query_arg( apply_filters( 'zip_ai_auth_middleware_args', array( 'type' => 'token', 'redirect_url' => add_query_arg( array( 'nonce' => wp_create_nonce( 'zip_ai_auth_nonce' ), 'scs-authorize' => 'true', ), admin_url() ), ) ), ZIP_AI_MIDDLEWARE ); return $auth_url; } /** * Get the revoke url for the auth token. * * @since 1.0.0 * @return string The authorization revoke url. */ public static function get_auth_revoke_url() { $revoke_url = add_query_arg( apply_filters( 'zip_ai_auth_revoke_args', array( array( 'nonce' => wp_create_nonce( 'zip_ai_auth_nonce' ), 'revoke_zip_ai_authorization_token' => 'definitely', ), admin_url(), ) ) ); return $revoke_url; } }