*/ public static function defaults(): array { $emails = array( 'booking_requested' => array( 'label' => __( 'Request received', 'schedulereed' ), 'to' => self::CUSTOMER, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( 'We received your booking request ({reference})', 'schedulereed' ), /* translators: Keep the {placeholders} as they are. */ 'message' => __( 'Hello {first_name}, thank you for your request for {service} on {date} at {time}. We will email you again as soon as it is confirmed.', 'schedulereed' ), ), 'booking_confirmed' => array( 'label' => __( 'Booking confirmed', 'schedulereed' ), 'to' => self::CUSTOMER, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( 'Your booking is confirmed: {service} on {date}', 'schedulereed' ), /* translators: Keep the {placeholders} as they are. */ 'message' => __( 'Hello {first_name}, thank you for booking. See you on {date} at {time}.', 'schedulereed' ), ), 'booking_rescheduled' => array( 'label' => __( 'Booking moved', 'schedulereed' ), 'to' => self::CUSTOMER, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( 'Your booking has moved to {date} at {time}', 'schedulereed' ), /* translators: Keep the {placeholders} as they are. */ 'message' => __( 'Hello {first_name}, your booking for {service} is now on {date} at {time}.', 'schedulereed' ), ), 'booking_cancelled' => array( 'label' => __( 'Booking cancelled', 'schedulereed' ), 'to' => self::CUSTOMER, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( 'Your booking was cancelled ({reference})', 'schedulereed' ), /* translators: Keep the {placeholders} as they are. */ 'message' => __( 'Hello {first_name}, your booking for {service} on {date} has been cancelled. Reply to this email if you have any questions.', 'schedulereed' ), ), 'payment_received' => array( 'label' => __( 'Payment received', 'schedulereed' ), 'to' => self::CUSTOMER, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( 'Payment received ({reference})', 'schedulereed' ), /* translators: Keep the {placeholders} as they are. */ 'message' => __( 'Hello {first_name}, thank you. We have received your payment of {amount}.', 'schedulereed' ), ), 'reminder' => array( 'label' => __( 'Reminder', 'schedulereed' ), 'to' => self::CUSTOMER, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( 'Reminder: {service} on {date} at {time}', 'schedulereed' ), /* translators: Keep the {placeholders} as they are. */ 'message' => __( 'Hello {first_name}, this is a reminder of your booking. If you can no longer make it, please let us know using the link below.', 'schedulereed' ), 'schedule' => array( 'when' => self::BEFORE_START, 'hours' => 24, ), ), 'staff_new_booking' => array( 'label' => __( 'New booking', 'schedulereed' ), 'to' => self::STAFF, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( 'New booking: {service} on {date} at {time} with {customer_name}', 'schedulereed' ), 'message' => __( 'You have a new booking.', 'schedulereed' ), ), 'staff_new_request' => array( 'label' => __( 'New booking request', 'schedulereed' ), 'to' => self::STAFF, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( 'Please approve: {service} on {date} with {customer_name}', 'schedulereed' ), 'message' => __( 'You have a new booking request. The time is kept for this customer until you approve or decline it.', 'schedulereed' ), ), 'staff_rescheduled' => array( 'label' => __( 'Moved by the customer', 'schedulereed' ), 'to' => self::STAFF, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( '{customer_name} moved their booking to {date} at {time}', 'schedulereed' ), 'message' => __( 'The customer moved this booking online.', 'schedulereed' ), ), 'staff_cancelled' => array( 'label' => __( 'Cancelled by the customer', 'schedulereed' ), 'to' => self::STAFF, /* translators: Keep the {placeholders} as they are. */ 'subject' => __( '{customer_name} cancelled {service} on {date}', 'schedulereed' ), 'message' => __( 'The customer cancelled this booking online and the time is free again.', 'schedulereed' ), ), ); /** * Filters the emails owners can edit on ScheduleReed → Emails. * * @param array> $emails Email type to label, to, subject, message and optional enabled and schedule. */ $emails = (array) apply_filters( 'schedulereed_email_templates', $emails ); foreach ( $emails as $type => $email ) { $emails[ $type ]['placeholders'] = $email['placeholders'] ?? ( 'payment_received' === $type ? array_merge( self::PLACEHOLDERS, array( 'amount' ) ) : self::PLACEHOLDERS ); } return $emails; } /** * An email's current wording. * * @param string $type Email type, a key of defaults(). * @return array{enabled: bool, subject: string, message: string, hours: int|null, attachments: int[]}|null Null for unknown types. * Hours is null for emails that are not scheduled; attachments are media library IDs. */ public static function get( string $type ): ?array { $default = self::defaults()[ $type ] ?? null; if ( null === $default ) { return null; } $saved = self::saved()[ $type ] ?? array(); return array( 'enabled' => (bool) ( $saved['enabled'] ?? $default['enabled'] ?? true ), 'subject' => '' !== ( $saved['subject'] ?? '' ) ? $saved['subject'] : $default['subject'], 'message' => '' !== ( $saved['message'] ?? '' ) ? $saved['message'] : $default['message'], 'hours' => isset( $default['schedule'] ) ? (int) ( $saved['hours'] ?? $default['schedule']['hours'] ) : null, 'attachments' => self::attachment_ids( (array) ( $saved['attachments'] ?? array() ) ), ); } /** * Scheduled emails that are turned on. * * @return array Type to when, hours and whether only unpaid bookings get it. */ public static function scheduled(): array { $list = array(); foreach ( self::defaults() as $type => $default ) { $email = self::get( $type ); if ( isset( $default['schedule'] ) && $email['enabled'] ) { $list[ $type ] = array( 'when' => $default['schedule']['when'], 'hours' => $email['hours'], // Only for bookings with something left to pay. 'unpaid' => ! empty( $default['schedule']['unpaid'] ), ); } } return $list; } /** * Every email with its current and default wording, for the admin screen. * * @return array> */ public static function all(): array { $list = array(); foreach ( self::defaults() as $type => $default ) { $list[] = array( 'type' => $type, 'label' => $default['label'], 'to' => $default['to'], 'placeholders' => $default['placeholders'], 'default_subject' => $default['subject'], 'default_message' => $default['message'], 'schedule' => $default['schedule']['when'] ?? null, ) + self::get( $type ); $list[ array_key_last( $list ) ]['files'] = self::files( $list[ array_key_last( $list ) ]['attachments'] ); } return $list; } /** * Save the owner's wording. Text equal to the default, or empty, goes back * to the default; unknown types are ignored. Hours apply to scheduled emails * only, from 0 to MAX_HOURS. * * @param array $emails Type to {enabled, subject, message, hours, attachments}. */ public static function save( array $emails ): void { $defaults = self::defaults(); $saved = self::saved(); foreach ( $emails as $type => $email ) { if ( ! isset( $defaults[ $type ] ) || ! is_array( $email ) ) { continue; } $current = $saved[ $type ] ?? array(); if ( array_key_exists( 'enabled', $email ) ) { $current['enabled'] = (bool) $email['enabled']; if ( ( $defaults[ $type ]['enabled'] ?? true ) === $current['enabled'] ) { unset( $current['enabled'] ); } } if ( isset( $defaults[ $type ]['schedule'] ) && array_key_exists( 'hours', $email ) ) { $hours = max( 0, min( self::MAX_HOURS, (int) $email['hours'] ) ); if ( $hours === $defaults[ $type ]['schedule']['hours'] ) { unset( $current['hours'] ); } else { $current['hours'] = $hours; } } if ( array_key_exists( 'attachments', $email ) ) { $ids = self::attachment_ids( (array) $email['attachments'] ); if ( array() === $ids ) { unset( $current['attachments'] ); } else { $current['attachments'] = $ids; } } foreach ( array( 'subject' => self::MAX_SUBJECT, 'message' => self::MAX_MESSAGE, ) as $field => $max ) { if ( ! array_key_exists( $field, $email ) ) { continue; } $text = 'subject' === $field ? sanitize_text_field( (string) $email[ $field ] ) : sanitize_textarea_field( (string) $email[ $field ] ); $text = mb_substr( trim( $text ), 0, $max ); if ( '' === $text || $text === $defaults[ $type ][ $field ] ) { unset( $current[ $field ] ); } else { $current[ $field ] = $text; } } if ( array() === $current ) { unset( $saved[ $type ] ); } else { $saved[ $type ] = $current; } } update_option( self::OPTION, $saved, false ); } /** * Media library files that exist on disk, without repeats, at most MAX_ATTACHMENTS. * * @param array $ids Attachment IDs. * @return int[] */ public static function attachment_ids( array $ids ): array { $valid = array(); foreach ( $ids as $id ) { $id = (int) $id; if ( $id > 0 && ! in_array( $id, $valid, true ) && 'attachment' === get_post_type( $id ) && '' !== self::path( $id ) ) { $valid[] = $id; } } return array_slice( $valid, 0, self::MAX_ATTACHMENTS ); } /** * Paths of attachments to hand to wp_mail(). * * @param int[] $ids Attachment IDs, as attachment_ids() returns them. * @return string[] */ public static function paths( array $ids ): array { return array_values( array_filter( array_map( array( self::class, 'path' ), $ids ) ) ); } /** * Name, file name and size of attachments, for the admin screen. * * @param int[] $ids Attachment IDs. * @return array */ public static function files( array $ids ): array { return array_map( static function ( int $id ): array { $path = self::path( $id ); return array( 'id' => $id, 'title' => get_the_title( $id ), 'filename' => wp_basename( $path ), 'size' => (int) wp_filesize( $path ), ); }, $ids ); } /** * An attachment's file, or '' when it is missing. * * @param int $id Attachment ID. */ private static function path( int $id ): string { $path = get_attached_file( $id ); return is_string( $path ) && is_readable( $path ) ? $path : ''; } /** * Stored changes. * * @return array> */ private static function saved(): array { $saved = get_option( self::OPTION, array() ); return is_array( $saved ) ? $saved : array(); } }