$booking Booking. * @param bool $notify Tell the customer. */ public function created( array $booking, bool $notify = true ): void { $pending = Status::PENDING === $booking['status']; if ( $notify ) { $this->deliver( $pending ? 'booking_requested' : 'booking_confirmed', $booking ); } if ( 'online' === $booking['source'] || 'portal' === $booking['source'] ) { $this->deliver( $pending ? 'staff_new_request' : 'staff_new_booking', $booking ); } } /** * Staff approved a request. * * @param array $booking Booking. */ public function confirmed( array $booking ): void { $this->deliver( 'booking_confirmed', $booking ); } /** * A booking was cancelled. * * @param array $booking Booking with refund_note and cancel_reason. * @param string $by Who cancelled. */ public function cancelled( array $booking, string $by ): void { $notes = array_values( array_filter( array( (string) ( $booking['cancel_reason'] ?? '' ), (string) ( $booking['refund_note'] ?? '' ) ) ) ); $this->deliver( 'booking_cancelled', $booking, array(), $notes ); if ( 'customer' === $by ) { $this->deliver( 'staff_cancelled', $booking ); } } /** * A booking moved. * * @param array $booking Booking. * @param array $old Before. * @param string $by Who moved it. * @param bool $notify Tell the customer. */ public function rescheduled( array $booking, array $old, string $by, bool $notify ): void { /* translators: %s: old date and time. */ $was = array( sprintf( __( 'It was on %s.', 'schedulereed' ), Format::when( $old['start'], $this->zone( $old ) ) ) ); if ( $notify ) { $this->deliver( 'booking_rescheduled', $booking, array(), $was ); } if ( 'customer' === $by ) { $this->deliver( 'staff_rescheduled', $booking, array(), $was ); } } /** * A payment was recorded. Refunds are explained in the cancellation email. * * @param array $booking Booking. * @param array $payment Payment. */ public function payment_recorded( array $booking, array $payment ): void { if ( $payment['amount'] <= 0 || Status::AWAITING_PAYMENT === $booking['status'] ) { return; } /** * Whether to email the customer about a payment, e.g. false for a * gift card used while booking, which the booking email already shows. * * @param bool $send Send it. * @param array $booking Booking. * @param array $payment Payment. */ if ( ! apply_filters( 'schedulereed_notify_payment', true, $booking, $payment ) ) { return; } $this->deliver( 'payment_received', $booking, array( 'amount' => Format::money( $payment['amount'], $booking['currency'] ) ) ); } /** * Staff need to give money back by hand. * * @param array $booking Booking. * @param int $amount Minor units. */ public function refund_due( array $booking, int $amount ): void { $this->alert( 'staff_refund_due', $booking, /* translators: 1: amount; 2: booking reference. */ sprintf( __( 'Refund to make: %1$s for booking %2$s', 'schedulereed' ), Format::money( $amount, $booking['currency'] ), Format::reference( $booking ) ), sprintf( /* translators: %s: amount. */ __( 'This booking was cancelled and %s is due back to the customer, but it was not paid online so it could not be refunded automatically. Please send it, then record the refund on the booking.', 'schedulereed' ), Format::money( $amount, $booking['currency'] ) ) ); } /** * A customer paid after their time was given away; the money went back. * * @param array $booking Booking. */ public function paid_too_late( array $booking ): void { $this->alert( 'staff_paid_too_late', $booking, /* translators: %s: booking reference. */ sprintf( __( 'Late payment refunded for booking %s', 'schedulereed' ), Format::reference( $booking ) ), __( 'This customer paid after the time to pay ran out, and someone else had booked the time in the meantime. The payment was refunded. You may want to contact them to find another time.', 'schedulereed' ) ); } /** * An imported calendar shows an event over an existing booking. * * @param array $conflict calendar, booking, summary, start, end. */ public function calendar_conflict( array $conflict ): void { $booking = (array) $conflict['booking']; $this->alert( 'staff_calendar_conflict', $booking, /* translators: %s: calendar name. */ sprintf( __( 'Possible double booking from %s', 'schedulereed' ), $conflict['calendar'] ), sprintf( /* translators: 1: calendar name; 2: date and time. */ __( 'The calendar "%1$s" now has an event at %2$s, over the booking below. Please check both and move one of them.', 'schedulereed' ), $conflict['calendar'], Format::when( (int) $conflict['start'] ) ) ); } /** * Send reminders and other scheduled emails that are due; runs every 15 minutes. */ public function scheduled(): int { $now = Clock::now(); $sent = 0; foreach ( EmailTemplates::scheduled() as $type => $rule ) { $offset = $rule['hours'] * HOUR_IN_SECONDS; if ( EmailTemplates::BEFORE_START === $rule['when'] ) { $bookings = Services::bookings()->starting( array( Status::CONFIRMED, Status::PENDING ), $now, $now + $offset ); } else { $bookings = Services::bookings()->ended( array( Status::COMPLETED, Status::CONFIRMED ), $now - $offset - DAY_IN_SECONDS, $now - $offset ); } foreach ( $bookings as $booking ) { if ( EmailTemplates::BEFORE_START === $rule['when'] && $booking['created_at'] > $booking['start'] - $offset ) { // Booked inside the window: the confirmation is reminder enough. continue; } if ( $rule['unpaid'] && $booking['paid'] >= $booking['total'] ) { continue; } if ( ! Services::messages()->claim( $booking['id'], $type ) ) { continue; } if ( $this->deliver( $type, $booking ) ) { ++$sent; } } } return $sent; } /** * An editable email, ready to send. * * @param string $type Email type, a key of EmailTemplates::defaults(). * @param array $booking Booking. * @param array $extra More placeholder values. * @param array|null $template Unsaved wording to use instead, for previews. * @param string[] $notes Lines added below the message. * @return array{to: string[], subject: string, body: string, reply_to: string, attachments: string[], for_staff: bool}|null Null when turned off. */ public function compose( string $type, array $booking, array $extra = array(), ?array $template = null, array $notes = array() ): ?array { // Customers read their emails in the language they booked in. $customer = EmailTemplates::STAFF !== ( EmailTemplates::defaults()[ $type ]['to'] ?? '' ); $switched = $customer && Multilingual::switch_to( (string) ( $booking['locale'] ?? '' ) ); try { return $this->write( $type, $booking, $extra, $template, $notes ); } finally { if ( $switched ) { Multilingual::restore(); } } } /** * Compose in the current language. * * @param string $type Email type. * @param array $booking Booking. * @param array $extra More placeholder values. * @param array|null $template Wording to use instead of the saved one. * @param string[] $notes Lines added below the message. */ private function write( string $type, array $booking, array $extra, ?array $template, array $notes ): ?array { $default = EmailTemplates::defaults()[ $type ] ?? null; $saved = EmailTemplates::get( $type ); if ( null === $default || null === $saved || ( null === $template && ! $saved['enabled'] ) ) { return null; } $details = $this->details( $booking ); $values = $this->placeholder_values( $booking, $details ) + $extra; $subject = trim( (string) ( $template['subject'] ?? '' ) ); $message = trim( (string) ( $template['message'] ?? '' ) ); $subject = Placeholders::fill( '' !== $subject ? $subject : Multilingual::text( $saved['subject'], 'email_' . $type . '_subject' ), $values ); $message = Placeholders::fill( '' !== $message ? $message : Multilingual::text( $saved['message'], 'email_' . $type . '_message' ), $values ); $message = array() === $notes ? $message : $message . "\n\n" . implode( "\n", $notes ); $files = EmailTemplates::paths( isset( $template['attachments'] ) ? EmailTemplates::attachment_ids( (array) $template['attachments'] ) : $saved['attachments'] ); if ( EmailTemplates::STAFF === $default['to'] ) { return array( 'to' => $this->staff_recipients( $details ), 'subject' => $subject, 'body' => $this->staff_body( $booking, $details, $message ), 'reply_to' => $details['email'], 'attachments' => $files, 'for_staff' => true, ); } return array( 'to' => array_filter( array( $details['email'] ) ), 'subject' => $subject, 'body' => $this->customer_body( $booking, $details, $message ), 'reply_to' => Settings::notify_email(), 'attachments' => $files, 'for_staff' => false, ); } /** * Send an editable email unless it is turned off. * * @param string $type Email type. * @param array $booking Booking. * @param array $extra More placeholder values. * @param string[] $notes Lines added below the message. * @param bool $even_if_off Send although it is off, e.g. a resend staff asked for. * @return bool Whether at least one copy was accepted. */ public function deliver( string $type, array $booking, array $extra = array(), array $notes = array(), bool $even_if_off = false ): bool { $email = $this->compose( $type, $booking, $extra, $even_if_off ? EmailTemplates::get( $type ) : null, $notes ); /** * Fires for every booking message, sent or turned off, so other * channels (Pro's SMS and WhatsApp) can send their own version. * * @param string $type Message type. * @param array $booking Booking. * @param array $details Customer, service, staff and place. */ do_action( 'schedulereed_notification', $type, $booking, $this->details( $booking ) ); if ( null === $email || array() === $email['to'] ) { return false; } $ics = ''; if ( ! $email['for_staff'] && in_array( $type, array( 'booking_confirmed', 'booking_rescheduled', 'reminder' ), true ) ) { $ics = $this->ics_file( $booking ); if ( '' !== $ics ) { $email['attachments'][] = $ics; } } $ok = false; foreach ( $email['to'] as $to ) { $ok = $this->send( $type, $booking, $to, $email['subject'], $email['body'], $email['reply_to'], $email['attachments'] ) || $ok; } if ( '' !== $ics ) { wp_delete_file( $ics ); } return $ok; } /** * Send a written message about a booking, e.g. from Pro's automations. * * @param string $type Type for the filter and log. * @param array $booking Booking. * @param string $to Address. * @param string $subject Subject with {placeholders}. * @param string $message Message with {placeholders}. * @param bool $for_staff Staff layout. */ public function send_message( string $type, array $booking, string $to, string $subject, string $message, bool $for_staff = false ): bool { $details = $this->details( $booking ); $values = $this->placeholder_values( $booking, $details ); $message = Placeholders::fill( $message, $values ); return $this->send( $type, $booking, $to, Placeholders::fill( $subject, $values ), $for_staff ? $this->staff_body( $booking, $details, $message ) : $this->customer_body( $booking, $details, $message ), $for_staff ? $details['email'] : Settings::notify_email() ); } /** * Send an email that belongs to no booking, e.g. a sign-in link. * * @param string $type Type for the filter. * @param string $to Address. * @param string $subject Subject. * @param string $body Plain-text body. */ public function send_plain( string $type, string $to, string $subject, string $body ): bool { return $this->send( $type, array(), $to, $subject, $body, Settings::notify_email() ); } /** * The customer's sign-in link and code for the portal. * * @param string $email Address. * @param string $link One-time link. * @param string $code Six-digit code. */ public function sign_in( string $email, string $link, string $code ): bool { $site = self::site_name(); return $this->send_plain( 'customer_sign_in', $email, /* translators: %s: site name. */ sprintf( __( 'Your sign-in link for %s', 'schedulereed' ), $site ), implode( "\n", array( __( 'Hello,', 'schedulereed' ), '', __( 'Use this link to see and manage your bookings. It works once, for 30 minutes:', 'schedulereed' ), $link, '', /* translators: %s: six-digit code. */ sprintf( __( 'Or enter this code: %s', 'schedulereed' ), $code ), '', __( 'If you did not ask for this email, you can ignore it.', 'schedulereed' ), '', $site, ) ) ); } /** * Who and what a booking is about, for messages. * * @param array $booking Booking. * @return array name, first_name, email, phone, service, staff, staff_ids, location, address, meeting, people, tz. */ public function details( array $booking ): array { $customer = Services::customers()->find( (int) $booking['customer_id'] ); $services = array(); $staff = array(); $locations = array(); $people = 0; foreach ( (array) ( $booking['items'] ?? array() ) as $item ) { $bookable = Services::catalog()->bookable( $item['bookable_id'] ); if ( null !== $bookable ) { $services[] = Multilingual::text( $bookable->name, 'service_' . $bookable->id . '_name' ); } if ( $item['staff_id'] > 0 ) { $member = Services::staff()->find( $item['staff_id'] ); if ( null !== $member ) { $staff[ $member['id'] ] = $member; } } if ( $item['location_id'] > 0 ) { $location = Services::locations()->find( $item['location_id'] ); if ( null !== $location ) { $locations[ $location['id'] ] = $location; } } $people = max( $people, (int) $item['quantity'] ); } $location = reset( $locations ); $first = (string) ( $customer['first_name'] ?? '' ); return array( 'name' => trim( $first . ' ' . (string) ( $customer['last_name'] ?? '' ) ), 'first_name' => $first, 'email' => (string) ( $customer['email'] ?? '' ), 'phone' => (string) ( $customer['phone'] ?? '' ), 'service' => implode( ', ', array_unique( $services ) ), 'staff' => implode( ', ', array_column( $staff, 'name' ) ), 'staff_list' => array_values( $staff ), 'location' => $location ? (string) $location['name'] : '', 'address' => $location ? (string) $location['address'] : '', 'meeting' => (string) $booking['meeting_url'], 'people' => $people, 'tz' => $this->zone( $booking ), ); } /** * Values for the {placeholders} in an email. * * @param array $booking Booking. * @param array|null $details From details(). * @return array */ public function placeholder_values( array $booking, ?array $details = null ): array { $details = $details ?? $this->details( $booking ); $tz = $details['tz']; $currency = $booking['currency']; $page = Settings::booking_page(); $values = array( 'customer_name' => $details['name'], 'first_name' => '' !== $details['first_name'] ? $details['first_name'] : __( 'there', 'schedulereed' ), 'site_name' => self::site_name(), 'reference' => Format::reference( $booking ), 'service' => $details['service'], 'date' => Format::date( $booking['start'], $tz ), 'time' => Format::time( $booking['start'], $tz ), 'end_time' => Format::time( $booking['end'], $tz ), 'duration' => Format::duration( intdiv( $booking['end'] - $booking['start'], 60 ) ), 'staff' => $details['staff'], 'location' => trim( $details['location'] . ( '' !== $details['address'] ? ', ' . $details['address'] : '' ), ', ' ), 'meeting_link' => $details['meeting'], 'people' => (string) $details['people'], 'total' => Format::money( $booking['total'], $currency ), 'paid' => Format::money( $booking['paid'], $currency ), 'balance' => Format::money( max( 0, $booking['total'] - $booking['paid'] ), $currency ), 'manage_link' => Format::manage_url( $booking ), 'book_again_link' => $page > 0 ? (string) get_permalink( $page ) : home_url( '/' ), ); /** * Add or change {placeholder} values, e.g. Pro's {review_link}. * * @param array $values Name => value. * @param array $booking Booking. */ return array_map( 'strval', (array) apply_filters( 'schedulereed_placeholder_values', $values, $booking ) ); } /** * The time zone to show a booking in: the customer's, else the location's, else the site's. * * @param array $booking Booking. */ private function zone( array $booking ): \DateTimeZone { $item = $booking['items'][0] ?? null; if ( null !== $item && $item['location_id'] > 0 ) { $location = Services::locations()->find( $item['location_id'] ); if ( null !== $location && '' !== $location['timezone'] && ! $location['online'] ) { return Settings::zone( (string) $location['timezone'] ); } } return Settings::zone( (string) $booking['timezone'] ); } /** * The notification address and the booking's staff, without repeats. * * @param array $details Details. * @return string[] */ public function staff_recipients( array $details ): array { $to = array( Settings::notify_email() ); foreach ( $details['staff_list'] as $member ) { $settings = is_array( $member['settings'] ?? null ) ? $member['settings'] : array(); if ( is_email( (string) $member['email'] ) && false !== ( $settings['notify'] ?? true ) ) { $to[] = (string) $member['email']; } } return array_values( array_unique( array_filter( $to, 'is_email' ) ) ); } /** * The customer's email: message, booking, how to pay, links. * * @param array $booking Booking. * @param array $details Details. * @param string $message Opening message. */ private function customer_body( array $booking, array $details, string $message ): string { $lines = array( $message, '', $this->summary( $booking, $details, false ) ); $method = Services::methods()->find( $booking['payment_method'] ); $text = $method && Status::CANCELLED !== $booking['status'] ? $method->instructions( $booking ) : ''; if ( '' !== $text ) { $lines[] = ''; $lines[] = $text; } if ( Status::CANCELLED !== $booking['status'] ) { $policy = Format::booking_policy( $booking ); if ( array() !== $policy ) { $lines[] = ''; /* translators: %s: refund terms. */ $lines[] = sprintf( __( 'Cancellation: %s', 'schedulereed' ), implode( ' ', $policy ) ); } $title = $details['service'] . ' – ' . self::site_name(); $where = '' !== $details['meeting'] ? $details['meeting'] : $details['address']; $lines[] = ''; /* translators: %s: link. */ $lines[] = sprintf( __( 'Add to Google Calendar: %s', 'schedulereed' ), AddToCalendar::google( $title, $booking['start'], $booking['end'], Format::manage_url( $booking ), $where ) ); /* translators: %s: link. */ $lines[] = sprintf( __( 'Add to Outlook: %s', 'schedulereed' ), AddToCalendar::outlook( $title, $booking['start'], $booking['end'], Format::manage_url( $booking ), $where ) ); } $lines[] = ''; /* translators: %s: link to the booking. */ $lines[] = sprintf( __( 'View, move or cancel your booking: %s', 'schedulereed' ), Format::manage_url( $booking ) ); $lines[] = ''; $lines[] = self::site_name(); $business = Settings::business(); foreach ( array( 'phone', 'address' ) as $key ) { if ( '' !== (string) ( $business[ $key ] ?? '' ) ) { $lines[] = (string) $business[ $key ]; } } return implode( "\n", $lines ); } /** * Staff's email: message, booking with the customer's details, admin link. * * @param array $booking Booking. * @param array $details Details. * @param string $message Opening message. */ private function staff_body( array $booking, array $details, string $message ): string { return implode( "\n", array( $message, '', $this->summary( $booking, $details, true ), '', /* translators: %s: admin link. */ sprintf( __( 'Open it: %s', 'schedulereed' ), admin_url( 'admin.php?page=schedulereed#/bookings/' . $booking['id'] ) ), ) ); } /** * Email staff a fixed alert about a booking. * * @param string $type Type. * @param array $booking Booking. * @param string $subject Subject. * @param string $intro Opening line. */ private function alert( string $type, array $booking, string $subject, string $intro ): void { $details = $this->details( $booking ); $this->send( $type, $booking, Settings::notify_email(), $subject, $this->staff_body( $booking, $details, $intro ), $details['email'] ); } /** * The booking's details as plain text. * * @param array $booking Booking. * @param array $details Details. * @param bool $for_staff Include contact details and notes. */ private function summary( array $booking, array $details, bool $for_staff ): string { $tz = $details['tz']; $currency = $booking['currency']; $lines = array( /* translators: %s: booking reference. */ sprintf( __( 'Reference: %s', 'schedulereed' ), Format::reference( $booking ) ), /* translators: %s: service name. */ sprintf( __( 'What: %s', 'schedulereed' ), $details['service'] ), /* translators: 1: date and time; 2: end time. */ sprintf( __( 'When: %1$s – %2$s', 'schedulereed' ), Format::when( $booking['start'], $tz ), Format::time( $booking['end'], $tz ) ), ); if ( $tz->getName() !== Settings::timezone()->getName() ) { /* translators: %s: time zone name. */ $lines[] = sprintf( __( 'Time zone: %s', 'schedulereed' ), $tz->getName() ); } if ( '' !== $details['staff'] ) { /* translators: %s: staff names. */ $lines[] = sprintf( __( 'With: %s', 'schedulereed' ), $details['staff'] ); } if ( '' !== $details['location'] ) { /* translators: %s: place. */ $lines[] = sprintf( __( 'Where: %s', 'schedulereed' ), trim( $details['location'] . ', ' . $details['address'], ', ' ) ); } if ( '' !== $details['meeting'] && Status::CANCELLED !== $booking['status'] ) { /* translators: %s: video call link. */ $lines[] = sprintf( __( 'Join online: %s', 'schedulereed' ), $details['meeting'] ); } if ( $details['people'] > 1 ) { /* translators: %d: number of people. */ $lines[] = sprintf( _n( 'For: %d person', 'For: %d people', $details['people'], 'schedulereed' ), $details['people'] ); } if ( count( (array) $booking['items'] ) > 1 ) { $lines[] = ''; foreach ( $booking['items'] as $item ) { $bookable = Services::catalog()->bookable( $item['bookable_id'] ); $lines[] = sprintf( '%1$s: %2$s', Format::when( $item['start'], $tz ), $bookable ? $bookable->name : '' ); } } $lines[] = ''; foreach ( (array) ( $booking['charges']['lines'] ?? array() ) as $line ) { foreach ( (array) ( $line['extras'] ?? array() ) as $extra ) { $name = $extra['quantity'] > 1 ? $extra['quantity'] . ' × ' . $extra['name'] : $extra['name']; $lines[] = $name . ': ' . Format::money( (int) $extra['amount'], $currency ); } } if ( ! empty( $booking['charges']['discount'] ) ) { $lines[] = sprintf( /* translators: 1: coupon code; 2: amount. */ __( 'Code %1$s: -%2$s', 'schedulereed' ), $booking['coupon'], Format::money( (int) $booking['charges']['discount'], $currency ) ); } foreach ( (array) ( $booking['charges']['taxes'] ?? array() ) as $tax ) { $lines[] = $tax['name'] . ': ' . Format::money( (int) $tax['amount'], $currency ); } /* translators: %s: total price. */ $lines[] = sprintf( __( 'Total: %s', 'schedulereed' ), Format::money( $booking['total'], $currency ) ); if ( $booking['paid'] > 0 ) { /* translators: %s: amount paid. */ $lines[] = sprintf( __( 'Paid: %s', 'schedulereed' ), Format::money( $booking['paid'], $currency ) ); } if ( $for_staff ) { $method = Services::methods()->find( $booking['payment_method'] ); if ( $method ) { /* translators: %s: payment method. */ $lines[] = sprintf( __( 'Payment: %s', 'schedulereed' ), $method->label() ); } $lines[] = ''; /* translators: %s: customer name. */ $lines[] = sprintf( __( 'Customer: %s', 'schedulereed' ), $details['name'] ); if ( '' !== $details['email'] ) { /* translators: %s: customer email. */ $lines[] = sprintf( __( 'Email: %s', 'schedulereed' ), $details['email'] ); } if ( '' !== $details['phone'] ) { /* translators: %s: customer phone. */ $lines[] = sprintf( __( 'Phone: %s', 'schedulereed' ), $details['phone'] ); } if ( '' !== $booking['notes'] ) { /* translators: %s: customer's notes. */ $lines[] = sprintf( __( 'Notes: %s', 'schedulereed' ), $booking['notes'] ); } } $answers = (array) ( $booking['fields'] ?? array() ); if ( array() !== $answers ) { $lines[] = ''; foreach ( $answers as $answer ) { if ( is_array( $answer ) && isset( $answer['label'], $answer['value'] ) ) { $lines[] = $answer['label'] . ': ' . ( is_array( $answer['value'] ) ? implode( ', ', $answer['value'] ) : $answer['value'] ); } } } return implode( "\n", $lines ); } /** * A calendar file for the booking in the temp folder, or ''. * * @param array $booking Booking. */ private function ics_file( array $booking ): string { $details = $this->details( $booking ); $ics = ICal::write( array( array( 'uid' => 'schedulereed-' . $booking['uid'] . '@' . wp_parse_url( home_url(), PHP_URL_HOST ), 'start' => $booking['start'], 'end' => $booking['end'], 'summary' => $details['service'] . ' – ' . self::site_name(), 'description' => Format::manage_url( $booking ), 'location' => '' !== $details['meeting'] ? $details['meeting'] : trim( $details['location'] . ', ' . $details['address'], ', ' ), ), ), self::site_name(), new DateTimeImmutable( '@' . Clock::now() ) ); $dir = get_temp_dir(); $path = trailingslashit( $dir ) . 'booking-' . sanitize_file_name( strtolower( $booking['uid'] ) ) . '-' . wp_generate_password( 6, false ) . '.ics'; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- A temporary attachment, deleted after sending. return false === file_put_contents( $path, $ics ) ? '' : $path; } /** * Hand the email to wp_mail() after the filter, and log it. * * @param string $type Email type. * @param array $booking Booking. * @param string $to Recipient. * @param string $subject Subject. * @param string $body Plain-text body. * @param string $reply_to Reply-To address. * @param string[] $attachments Files. */ private function send( string $type, array $booking, string $to, string $subject, string $body, string $reply_to, array $attachments = array() ): bool { $email = array( 'to' => $to, 'subject' => $subject, 'body' => $body, 'headers' => is_email( $reply_to ) ? array( 'Reply-To: ' . $reply_to ) : array(), 'attachments' => $attachments, ); /** * Change or skip a ScheduleReed email. * * @param array|false $email to, subject, body, headers and attachments; return false to skip. * @param string $type booking_confirmed, booking_requested, booking_rescheduled, * booking_cancelled, payment_received, reminder, staff_new_booking, * staff_new_request, staff_rescheduled, staff_cancelled, * staff_refund_due, staff_paid_too_late, staff_calendar_conflict, * customer_sign_in, or one added by schedulereed_email_templates. * @param array $booking The booking; empty for customer_sign_in. */ $email = apply_filters( 'schedulereed_email', $email, $type, $booking ); if ( ! is_array( $email ) || ! is_email( $email['to'] ?? '' ) ) { return false; } $error = ''; $catcher = static function ( \WP_Error $e ) use ( &$error ): void { $error = $e->get_error_message(); }; add_action( 'wp_mail_failed', $catcher ); $ok = wp_mail( $email['to'], $email['subject'], $email['body'], $email['headers'] ?? array(), $email['attachments'] ?? array() ); remove_action( 'wp_mail_failed', $catcher ); Services::messages()->log( (int) ( $booking['id'] ?? 0 ), 'email', $type, (string) $email['to'], $ok, $error ); return $ok; } private static function site_name(): string { $business = Settings::business(); $name = (string) ( $business['name'] ?? '' ); return '' !== $name ? $name : wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ); } }