'', 'header' => '{customer_name}', 'title' => '{service}', 'fields' => [ ['label' => __('Date', 'addtowallet-for-bookly'), 'value' => '{date}'], ['label' => __('Time', 'addtowallet-for-bookly'), 'value' => '{time}'], ['label' => __('Booking', 'addtowallet-for-bookly'), 'value' => '{booking_id}'], ], 'back' => [ ['type' => 'text', 'label' => __('With', 'addtowallet-for-bookly'), 'value' => '{employee}'], ['type' => 'address', 'label' => __('Address', 'addtowallet-for-bookly'), 'value' => '{business_address}'], ['type' => 'email', 'label' => __('Email us', 'addtowallet-for-bookly'), 'value' => '{business_email}'], ['type' => 'phone', 'label' => __('Call us', 'addtowallet-for-bookly'), 'value' => '{business_phone}'], ], 'hero_rules' => [], 'reminder' => 1, ]; } /** * The {data fields} a template can use, as token => label, in the order the * "+ Data Field" menu lists them. */ public static function tokens() { return [ 'customer_name' => __('Customer name', 'addtowallet-for-bookly'), 'first_name' => __('Customer first name', 'addtowallet-for-bookly'), 'customer_email' => __('Customer email', 'addtowallet-for-bookly'), 'customer_phone' => __('Customer phone', 'addtowallet-for-bookly'), 'service' => __('Service', 'addtowallet-for-bookly'), 'employee' => __('Staff member', 'addtowallet-for-bookly'), 'location' => __('Location (Locations add-on)', 'addtowallet-for-bookly'), 'date' => __('Date', 'addtowallet-for-bookly'), 'time' => __('Start time', 'addtowallet-for-bookly'), 'end_time' => __('End time', 'addtowallet-for-bookly'), 'duration' => __('Duration', 'addtowallet-for-bookly'), 'timezone' => __('Time zone', 'addtowallet-for-bookly'), 'booking_id' => __('Booking number', 'addtowallet-for-bookly'), 'status' => __('Booking status', 'addtowallet-for-bookly'), 'price' => __('Price', 'addtowallet-for-bookly'), 'persons' => __('Number of people', 'addtowallet-for-bookly'), 'meeting_url' => __('Online meeting link', 'addtowallet-for-bookly'), 'business_name' => __('Business name', 'addtowallet-for-bookly'), 'business_email' => __('Business email', 'addtowallet-for-bookly'), 'business_phone' => __('Business phone', 'addtowallet-for-bookly'), 'business_address' => __('Business address', 'addtowallet-for-bookly'), 'business_website' => __('Business website', 'addtowallet-for-bookly'), 'site_name' => __('Site name', 'addtowallet-for-bookly'), ]; } /** * The business details from Bookly's Settings › Company (name, email, phone, address, * website), which Bookly itself uses in its notifications. Falls back to the site * name and admin email. */ public static function business() { $co = ATW_Bookly_Provider_Bookly::company(); $email = $co['email']; if ($email === '' || !is_email($email)) { $email = (string) get_option('admin_email'); } return [ 'name' => $co['name'] !== '' ? $co['name'] : (string) get_bloginfo('name'), 'email' => $email, 'phone' => $co['phone'], 'address' => $co['address'], 'website' => $co['website'], ]; } /** * The saved template, with anything missing or malformed replaced by the default. * Settings imported from 1.x have no 'template' key at all. */ public static function normalize($saved) { $defaults = self::defaults(); if (!is_array($saved)) { return $defaults; } $out = $defaults; foreach (['business_name', 'header', 'title'] as $key) { if (isset($saved[$key]) && is_scalar($saved[$key])) { $out[$key] = (string) $saved[$key]; } } if (isset($saved['fields']) && is_array($saved['fields'])) { $fields = []; foreach (array_values($saved['fields']) as $field) { if (count($fields) >= self::MAX_FIELDS) { break; } $fields[] = [ 'label' => isset($field['label']) && is_scalar($field['label']) ? (string) $field['label'] : '', 'value' => isset($field['value']) && is_scalar($field['value']) ? (string) $field['value'] : '', ]; } while (count($fields) < self::MAX_FIELDS) { $fields[] = ['label' => '', 'value' => '']; } $out['fields'] = $fields; } if (isset($saved['back']) && is_array($saved['back'])) { $back = []; foreach (array_values($saved['back']) as $section) { if (count($back) >= self::MAX_BACK || !is_array($section)) { continue; } $type = isset($section['type']) ? (string) $section['type'] : 'text'; $back[] = [ 'type' => in_array($type, self::BACK_TYPES, true) ? $type : 'text', 'label' => isset($section['label']) && is_scalar($section['label']) ? (string) $section['label'] : '', 'value' => isset($section['value']) && is_scalar($section['value']) ? (string) $section['value'] : '', ]; } $out['back'] = $back; } if (isset($saved['hero_rules']) && is_array($saved['hero_rules'])) { $rules = []; foreach (array_values($saved['hero_rules']) as $rule) { $key = is_array($rule) && isset($rule['type_id']) ? self::target_key($rule['type_id']) : ''; if ($key === '' || empty($rule['url'])) { continue; } $rules[] = ['type_id' => $key, 'url' => (string) $rule['url']]; } $out['hero_rules'] = array_slice($rules, 0, self::MAX_HERO_RULES); } if (isset($saved['reminder'])) { $out['reminder'] = empty($saved['reminder']) ? 0 : 1; } return $out; } /** * A hero variation's target, "service:", or '' when it is not one. * (Stored under type_id, the name the editor script uses for it.) */ public static function target_key($value) { $value = strtolower(trim((string) $value)); return preg_match('/^service:([1-9][0-9]{0,9})$/', $value) ? $value : ''; } /** Cuts a string to a length without splitting a multibyte character. */ private static function cap($text, $max) { return function_exists('mb_substr') ? mb_substr($text, 0, $max) : substr($text, 0, $max); } /** * Sanitises the Pass design form's template part. Input is the raw, unslashed POST * array; every value is cleaned here, field by field. */ public static function sanitize(array $raw) { $text = function ($key, $max) use ($raw) { $value = isset($raw[$key]) && is_scalar($raw[$key]) ? sanitize_text_field((string) $raw[$key]) : ''; return self::cap($value, $max); }; $out = [ 'business_name' => $text('business_name', 60), 'header' => $text('header', 80), 'title' => $text('title', 100), 'fields' => [], 'back' => [], 'hero_rules' => [], 'reminder' => empty($raw['reminder']) ? 0 : 1, ]; $fields = isset($raw['fields']) && is_array($raw['fields']) ? array_values($raw['fields']) : []; for ($i = 0; $i < self::MAX_FIELDS; $i++) { $field = isset($fields[$i]) && is_array($fields[$i]) ? $fields[$i] : []; $out['fields'][] = [ 'label' => self::cap(sanitize_text_field(isset($field['label']) ? (string) $field['label'] : ''), 40), 'value' => self::cap(sanitize_text_field(isset($field['value']) ? (string) $field['value'] : ''), 100), ]; } $back = isset($raw['back']) && is_array($raw['back']) ? array_values($raw['back']) : []; foreach ($back as $section) { if (count($out['back']) >= self::MAX_BACK || !is_array($section)) { continue; } $type = isset($section['type']) ? sanitize_key((string) $section['type']) : 'text'; $type = in_array($type, self::BACK_TYPES, true) ? $type : 'text'; $label = self::cap(sanitize_text_field(isset($section['label']) ? (string) $section['label'] : ''), 60); // Browsers post textarea line breaks as CRLF; the pass wants plain newlines. $value = isset($section['value']) ? str_replace(["\r\n", "\r"], "\n", (string) $section['value']) : ''; // Text sections keep their line breaks; the others are one line. $value = $type === 'text' ? self::cap(sanitize_textarea_field($value), 1000) : self::cap(sanitize_text_field($value), 300); if ($label === '' && $value === '') { continue; } $out['back'][] = ['type' => $type, 'label' => $label, 'value' => $value]; } $rules = isset($raw['hero_rules']) && is_array($raw['hero_rules']) ? array_values($raw['hero_rules']) : []; $seen = []; foreach ($rules as $rule) { if (!is_array($rule) || count($out['hero_rules']) >= self::MAX_HERO_RULES) { continue; } $type_id = isset($rule['type_id']) ? self::target_key($rule['type_id']) : ''; $url = isset($rule['url']) ? esc_url_raw(trim((string) $rule['url']), ['http', 'https']) : ''; // One image per service: a second rule for the same one could never win. if (!$type_id || $url === '' || isset($seen[$type_id])) { continue; } $seen[$type_id] = true; $out['hero_rules'][] = ['type_id' => $type_id, 'url' => $url]; } return $out; } /** Bookly's raw status as words, for {status}. */ public static function status_label($status) { $labels = [ 'approved' => __('Approved', 'addtowallet-for-bookly'), 'pending' => __('Pending', 'addtowallet-for-bookly'), 'waitlisted' => __('Waiting list', 'addtowallet-for-bookly'), 'cancelled' => __('Cancelled', 'addtowallet-for-bookly'), 'rejected' => __('Rejected', 'addtowallet-for-bookly'), 'done' => __('Done', 'addtowallet-for-bookly'), ]; $status = (string) $status; return isset($labels[$status]) ? $labels[$status] : ucfirst(str_replace(['_', '-'], ' ', $status)); } /** * A price in Bookly's own format (Settings › Payments: currency and price format). * '' for free bookings and unknown prices. */ public static function price_label($price) { if ($price === null || $price === '' || (float) $price <= 0) { return ''; } return ATW_Bookly_Provider_Bookly::format_price($price); } /** "45 min", "1 h", "1 h 30 min". */ private static function duration_label($minutes) { $minutes = (int) $minutes; if ($minutes <= 0) { return ''; } $hours = intdiv($minutes, 60); $rest = $minutes % 60; if (!$hours) { /* translators: %d: minutes */ return sprintf(__('%d min', 'addtowallet-for-bookly'), $rest); } if (!$rest) { /* translators: %d: hours */ return sprintf(__('%d h', 'addtowallet-for-bookly'), $hours); } /* translators: 1: hours, 2: minutes */ return sprintf(__('%1$d h %2$d min', 'addtowallet-for-bookly'), $hours, $rest); } /** * Every token's value for one booking. Dates use the booking's own zone (the one * Bookly showed the customer). * * @param array $booking Normalised booking (see ATW_Bookly_Provider::find_booking()). * @return array token => string */ public static function values(array $booking) { $zone = self::zone($booking); $start = self::to_datetime(isset($booking['booking_start']) ? $booking['booking_start'] : null, $zone); $end = self::to_datetime(isset($booking['booking_end']) ? $booking['booking_end'] : null, $zone); $biz = self::business(); $name = trim((string) (isset($booking['customer_name']) ? $booking['customer_name'] : '')); $parts = preg_split('/\s+/', $name); $get = function ($key) use ($booking) { return isset($booking[$key]) && is_scalar($booking[$key]) ? trim((string) $booking[$key]) : ''; }; $minutes = ($start && $end) ? (int) round(($end->getTimestamp() - $start->getTimestamp()) / 60) : 0; $values = [ 'customer_name' => $name, 'first_name' => $name !== '' && $parts ? (string) $parts[0] : '', 'customer_email' => $get('customer_email'), 'customer_phone' => $get('customer_phone'), 'service' => $get('service_name'), 'employee' => $get('provider_name'), 'location' => $get('location_name'), 'date' => $start ? wp_date('D, j M Y', $start->getTimestamp(), $start->getTimezone()) : '', 'time' => $start ? $start->format('H:i') : '', 'end_time' => $end ? $end->format('H:i') : '', 'duration' => self::duration_label($minutes), 'timezone' => $start ? $zone->getName() : '', 'booking_id' => isset($booking['booking_id']) ? (string) $booking['booking_id'] : '', 'status' => isset($booking['status']) ? self::status_label($booking['status']) : '', 'price' => self::price_label(isset($booking['price']) ? $booking['price'] : null), 'persons' => !empty($booking['persons']) ? (string) (int) $booking['persons'] : '', 'meeting_url' => $get('meeting_url'), 'business_name' => $biz['name'], 'business_email' => $biz['email'], 'business_phone' => $biz['phone'], 'business_address' => $biz['address'], 'business_website' => $biz['website'], 'site_name' => (string) get_bloginfo('name'), ]; /** * Add or change the values a pass template's {data fields} are filled with. * * @param array $values token => string * @param array $booking Normalised booking. */ $values = apply_filters('atw_bookly_template_values', $values, $booking); return array_map(function ($value) { return is_scalar($value) ? trim((string) $value) : ''; }, (array) $values); } /** * Replaces each {token} with its value. Unknown tokens, and known ones with no value * for this booking, become empty; spaces left doubled by that are closed up. */ public static function fill($text, array $values) { $text = preg_replace_callback( '/\{([a-z_]+)\}/', function ($m) use ($values) { return isset($values[$m[1]]) ? (string) $values[$m[1]] : ''; }, (string) $text ); $text = preg_replace('/[ \t]{2,}/', ' ', $text); return trim($text); } /** A booking for the preview when the site has none: plausible, clearly made up. */ public static function sample_booking() { $start = new DateTimeImmutable('tomorrow 14:30', wp_timezone()); return [ 'booking_id' => 1042, 'type' => 'appointment', 'status' => 'approved', 'customer_name' => __('Jane Smith', 'addtowallet-for-bookly'), 'customer_email' => 'jane.smith@example.com', 'customer_phone' => '+1 555 010 2030', 'service_name' => __('Initial Consultation', 'addtowallet-for-bookly'), 'service_id' => 0, 'hero_key' => '', 'provider_name' => __('John Doe', 'addtowallet-for-bookly'), 'location_name' => '', 'location_address' => '', 'meeting_url' => '', 'price' => null, 'persons' => 1, 'booking_start' => $start->format('Y-m-d H:i:s'), 'booking_end' => $start->modify('+30 minutes')->format('Y-m-d H:i:s'), 'timezone' => wp_timezone()->getName(), ]; } /** * The zone the booking's times are in: the booking's own when it names one, otherwise * the site's. */ public static function zone(array $booking) { if (!empty($booking['timezone'])) { try { return new DateTimeZone((string) $booking['timezone']); } catch (Exception $e) { // Fall through to the site zone. } } return wp_timezone(); } /** 'Y-m-d H:i:s' in $zone to a date, or null. */ public static function to_datetime($value, DateTimeZone $zone) { if (empty($value)) { return null; } try { return new DateTimeImmutable((string) $value, $zone); } catch (Exception $e) { return null; } } }