|null $body JSON body. * @return array|\WP_Error */ public static function request( string $method, string $path, ?array $body = null, int $timeout = 20 ) { $token = Connection::access_token(); if ( is_wp_error( $token ) ) { return $token; } $args = array( 'method' => $method, 'timeout' => $timeout, 'headers' => array( 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), ); if ( null !== $body ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = Cast::string( wp_json_encode( $body ) ); } return self::decode( wp_remote_request( Connection::api_base_url() . '/api/v1/' . ltrim( $path, '/' ), $args ) ); } /** * Sends a multipart form, with one optional file read from disk. * * @param array $fields Form fields. * @return array|\WP_Error */ public static function upload( string $path, array $fields, ?string $file = null, string $file_name = '' ) { $request = self::upload_request( $path, $fields, $file, $file_name ); if ( is_wp_error( $request ) ) { return $request; } return self::decode( wp_remote_post( $request['url'], array( 'timeout' => 120, 'headers' => $request['headers'], 'body' => $request['data'], ) ) ); } /** * Sends several uploads at once; results keep the keys of $uploads. * * @param array, file: ?string, name: string}> $uploads * @return array|\WP_Error> */ public static function upload_many( array $uploads ): array { if ( count( $uploads ) < 2 || ! class_exists( '\WpOrg\Requests\Requests' ) ) { return array_map( function ( $upload ) { return self::upload( $upload['path'], $upload['fields'], $upload['file'], $upload['name'] ); }, $uploads ); } $results = array(); $requests = array(); foreach ( $uploads as $key => $upload ) { $request = self::upload_request( $upload['path'], $upload['fields'], $upload['file'], $upload['name'] ); if ( is_wp_error( $request ) ) { $results[ $key ] = $request; continue; } $requests[ $key ] = array_merge( $request, array( 'type' => 'POST' ) ); } $options = array( 'timeout' => 120, 'connect_timeout' => 10, 'verify' => ABSPATH . WPINC . '/certificates/ca-bundle.crt', ); foreach ( \WpOrg\Requests\Requests::request_multiple( $requests, $options ) as $key => $response ) { $results[ $key ] = $response instanceof \WpOrg\Requests\Response ? self::decode( array( 'response' => array( 'code' => Cast::int( $response->status_code ) ), 'body' => Cast::string( $response->body ), ) ) : new \WP_Error( 'awesomeforms_http', $response instanceof \Exception ? $response->getMessage() : __( 'The request failed.', 'awesomeforms' ) ); } ksort( $results ); return $results; } /** * @param array $fields * @return array{url: string, headers: array, data: string}|\WP_Error */ private static function upload_request( string $path, array $fields, ?string $file, string $file_name ) { $token = Connection::access_token(); if ( is_wp_error( $token ) ) { return $token; } $boundary = 'awesomeforms' . wp_generate_password( 24, false ); $body = ''; foreach ( $fields as $name => $value ) { $body .= "--$boundary\r\nContent-Disposition: form-data; name=\"$name\"\r\n\r\n$value\r\n"; } if ( null !== $file ) { $content = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( false === $content ) { return new \WP_Error( 'awesomeforms_file', __( 'The file could not be read.', 'awesomeforms' ) ); } $safe = str_replace( array( '"', "\r", "\n" ), '_', $file_name ); $body .= "--$boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"$safe\"\r\nContent-Type: application/octet-stream\r\n\r\n$content\r\n"; } $body .= "--$boundary--\r\n"; return array( 'url' => Connection::api_base_url() . '/api/v1/' . ltrim( $path, '/' ), 'headers' => array( 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, ), 'data' => $body, ); } /** * @param array|\WP_Error $response * @return array|\WP_Error */ private static function decode( $response ) { if ( is_wp_error( $response ) ) { return $response; } $status = Cast::int( wp_remote_retrieve_response_code( $response ) ); $decoded = json_decode( Cast::string( wp_remote_retrieve_body( $response ) ), true ); if ( 401 === $status ) { Connection::forget(); } if ( $status >= 400 || ! is_array( $decoded ) ) { $message = is_array( $decoded ) && isset( $decoded['error'] ) && is_string( $decoded['error'] ) ? $decoded['error'] /* translators: %d: HTTP status code. */ : sprintf( __( 'AwesomeFor.ms answered with an error (%d).', 'awesomeforms' ), $status ); return new \WP_Error( 'awesomeforms_api', $message, array( 'status' => $status, 'body' => $decoded, ) ); } return Cast::map( $decoded ); } /** * @return array|\WP_Error */ public static function account() { $cached = get_transient( 'awesomeforms_account' ); if ( is_array( $cached ) ) { return Cast::map( $cached ); } $account = self::request( 'GET', 'me' ); if ( ! is_wp_error( $account ) ) { set_transient( 'awesomeforms_account', $account, 10 * MINUTE_IN_SECONDS ); } return $account; } /** * @return list>|\WP_Error */ public static function forms() { $result = self::request( 'GET', 'forms' ); return is_wp_error( $result ) ? $result : Cast::rows( $result['forms'] ?? array() ); } public static function has_feature( string $feature ): bool { $account = self::account(); return ! is_wp_error( $account ) && ! empty( Cast::map( $account['features'] ?? array() )[ $feature ] ); } }