20, 'user-agent' => 'Medivot-WordPress/' . MEDIVOT_VERSION, 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Accept' => 'application/json', ), ) ); if ( is_wp_error( $response ) ) { return $response; } $status = (int) wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $status < 200 || $status >= 300 ) { return new WP_Error( 'medivot_api_' . $status, self::error_message( $status, $body ) ); } if ( ! is_array( $body ) ) { return new WP_Error( 'medivot_api_invalid', __( 'Medivot sent a response the plugin could not read.', 'medivot' ) ); } return $body; } /** * Check an API key against GET /api/v1/me. * * @param string $api_key Medivot API key. * @return array Connection record: ok, workspace, slug, message, checked_at. */ public static function check_connection( $api_key ) { $record = array( 'ok' => false, 'workspace' => '', 'slug' => '', 'message' => '', 'checked_at' => time(), ); $body = self::get( 'me', $api_key ); if ( is_wp_error( $body ) ) { $record['message'] = $body->get_error_message(); return $record; } $tenant = isset( $body['data']['tenant'] ) && is_array( $body['data']['tenant'] ) ? $body['data']['tenant'] : array(); $record['ok'] = true; $record['workspace'] = isset( $tenant['name'] ) ? sanitize_text_field( (string) $tenant['name'] ) : ''; $record['slug'] = isset( $tenant['slug'] ) ? Medivot_Plugin::clean_slug( $tenant['slug'] ) : ''; return $record; } /** * Active, public event types of the connected workspace. * * @param string $api_key Medivot API key. * @return array|WP_Error List of array( slug, name, duration ). */ public static function event_types( $api_key ) { $body = self::get( 'calendar/event-types', $api_key, array( 'active_only' => '1', 'public_only' => '1', 'per_page' => '100', ) ); if ( is_wp_error( $body ) ) { return $body; } $items = array(); $rows = isset( $body['data'] ) && is_array( $body['data'] ) ? $body['data'] : array(); foreach ( $rows as $row ) { if ( ! is_array( $row ) || empty( $row['slug'] ) ) { continue; } $items[] = array( 'slug' => Medivot_Plugin::clean_slug( $row['slug'] ), 'name' => isset( $row['name'] ) ? sanitize_text_field( (string) $row['name'] ) : '', 'duration' => isset( $row['duration_minutes'] ) ? absint( $row['duration_minutes'] ) : 0, ); } return $items; } /** * Human-readable message for a failed API call. * * @param int $status HTTP status. * @param mixed $body Decoded body. * @return string */ private static function error_message( $status, $body ) { if ( 401 === $status ) { return __( 'Medivot did not accept this API key. Check that it was copied in full and has not been revoked.', 'medivot' ); } if ( is_array( $body ) ) { foreach ( array( 'detail', 'message' ) as $field ) { if ( ! empty( $body[ $field ] ) && is_string( $body[ $field ] ) ) { return sanitize_text_field( $body[ $field ] ); } } } /* translators: %d: HTTP status code returned by Medivot. */ return sprintf( __( 'Medivot returned an error (HTTP %d). Please try again later.', 'medivot' ), $status ); } }