notice() : $this->render( $parsed['kind'], $parsed['attributes'] );
}
public function notice(): string {
return '
' . esc_html( Services::error()->get_error_message() ) . '
';
}
public function render( string $kind, array $input ): string {
$attributes = Attributes::validate( $kind, $input );
if ( is_wp_error( $attributes ) ) {
return $this->notice();
}
$api = new Services();
if ( $attributes['connectionAccountId'] <= 0 || $api->account_id() !== $attributes['connectionAccountId'] ) {
return $this->notice();
}
wp_enqueue_style( 'waitlistize-embeds' );
if ( 'booking' === $kind ) {
$service = isset( $attributes['serviceSlug'] ) ? $api->by_slug( $attributes['connectionAccountId'], $attributes['serviceSlug'], $attributes['locale'] ) : $api->detail( $attributes['connectionAccountId'], $attributes['serviceId'], $attributes['locale'] );
if ( is_wp_error( $service ) ) {
return $this->notice();
}
$html = $this->card( $service, $attributes );
} else {
$items = array();
$item_count = 0;
for ( $page = 1; $page <= 20 && $item_count < $attributes['pageSize']; ++$page ) {
$catalog = $api->catalog( $attributes['connectionAccountId'], $page, 100, $attributes['locale'] );
if ( is_wp_error( $catalog ) ) {
return $this->notice();
}
foreach ( $catalog['data'] as $service ) {
if ( 'all' !== $attributes['typeFilter'] && $attributes['typeFilter'] !== $service['type'] ) {
continue;
}
if ( 'service' === $attributes['styleSource'] ) {
$service = $api->detail( $attributes['connectionAccountId'], $service['id'], $attributes['locale'] );
if ( is_wp_error( $service ) ) {
return $this->notice();
}
}
$items[] = $this->card( $service, $attributes );
++$item_count;
if ( count( $items ) >= $attributes['pageSize'] ) {
break;
}
}
if ( $page >= $catalog['meta']['last_page'] ) {
break;
}
}
if ( $page > 20 && count( $items ) < $attributes['pageSize'] ) {
return $this->notice();
}
$html = '' . implode( '', $items ) . '
';
if ( ! $items ) {
$html .= '' . esc_html__( 'No services to display.', 'waitlistize' ) . '
';
}
}
if ( ! str_contains( $html, 'data-embed=' ) ) {
$html .= '' . esc_html__( 'Service information only. Booking is not available in this embed yet.', 'waitlistize' ) . '
';
}
return '';
}
private function card( array $service, array $attributes ): string {
$color = 'service' === $attributes['styleSource'] ? sanitize_hex_color( $service['accent_color'] ) : '';
$html = '';
if ( $attributes['showTitle'] ?? true ) {
$html .= '' . esc_html( $service['name'] ) . '
';
}
if ( $attributes['showDescription'] && '' !== $service['description'] ) {
$html .= '' . esc_html( $service['description'] ) . '
';
}
if ( $attributes['showPrice'] && '' !== $service['public_price_amount'] ) {
$html .= '' . esc_html( trim( $service['public_price_amount'] . ' ' . $service['public_price_currency'] ) ) . '
';
}
return $html . ( new BookingWidget() )->render( $service, $attributes ) . '';
}
}