*/ public function start(): array { $user = wp_get_current_user(); return array( 'done' => self::is_done(), 'templates' => Templates::all(), 'countries' => Holidays::countries(), 'defaults' => array( 'business' => Settings::business(), 'currency' => self::guess_currency(), 'country' => self::guess_country(), 'timezone' => Settings::timezone()->getName(), 'notify_email' => (string) get_option( 'admin_email' ), 'staff' => array( array( 'name' => (string) $user->display_name, 'email' => (string) $user->user_email, 'user_id' => (int) $user->ID, ), ), ), ); } /** * Apply the answers. * * @param array $answers template, business, currency, country, timezone, notify_email, * approval, hours {from, to, days}, staff [{name, email, user_id}], * services [{name, type, mode, duration, price, basis, capacity, buffer, timetable}], * payments [ids], bank_details, holidays (bool), pages (bool). * @return array{services: int[], staff: int[], booking_url: string, portal_url: string} * @throws InvalidArgumentException When an answer is invalid; nothing is saved then. */ public function apply( array $answers ): array { $templates = Templates::all(); $template = (string) ( $answers['template'] ?? 'other' ); if ( ! isset( $templates[ $template ] ) ) { throw new InvalidArgumentException( __( 'Choose what kind of business you run.', 'schedulereed' ) ); } $t = $templates[ $template ]; $currency = strtoupper( (string) ( $answers['currency'] ?? '' ) ); if ( 1 !== preg_match( '/^[A-Z]{3}$/', $currency ) ) { throw new InvalidArgumentException( __( 'Choose a currency.', 'schedulereed' ) ); } $email = trim( (string) ( $answers['notify_email'] ?? '' ) ); if ( '' !== $email && ! is_email( $email ) ) { throw new InvalidArgumentException( __( 'Enter a valid email address for new bookings.', 'schedulereed' ) ); } $country = strtoupper( (string) ( $answers['country'] ?? '' ) ); $zone = (string) ( $answers['timezone'] ?? '' ); if ( '' !== $zone && ! in_array( $zone, timezone_identifiers_list(), true ) ) { throw new InvalidArgumentException( __( 'Choose your time zone.', 'schedulereed' ) ); } $h = (array) ( $answers['hours'] ?? $t['hours'] ); $days = array_values( array_intersect( array_map( 'intval', (array) ( $h['days'] ?? array() ) ), range( 1, 7 ) ) ); if ( array() === $days ) { throw new InvalidArgumentException( __( 'Choose at least one day you are open.', 'schedulereed' ) ); } $weekly = array(); foreach ( $days as $day ) { $weekly[ (string) $day ] = array( array( (string) ( $h['from'] ?? '09:00' ), (string) ( $h['to'] ?? '17:00' ) ) ); } $hours = WeeklyHours::from_array( $weekly ); $staff = array(); foreach ( array_slice( (array) ( $answers['staff'] ?? array() ), 0, self::MAX_STAFF ) as $member ) { $name = trim( sanitize_text_field( (string) ( $member['name'] ?? '' ) ) ); if ( '' === $name ) { continue; } $member_email = sanitize_email( (string) ( $member['email'] ?? '' ) ); $staff[] = array( 'name' => mb_substr( $name, 0, 191 ), 'email' => is_email( $member_email ) ? $member_email : '', 'user_id' => max( 0, (int) ( $member['user_id'] ?? 0 ) ), 'status' => 'active', 'color' => self::color( count( $staff ) ), ); } $rows = array(); foreach ( array_slice( (array) ( $answers['services'] ?? $t['services'] ), 0, self::MAX_SERVICES ) as $service ) { if ( '' === trim( (string) ( $service['name'] ?? '' ) ) ) { continue; } $type = (string) ( $service['type'] ?? Bookable::APPOINTMENT ); $mode = (string) ( $service['mode'] ?? ( Bookable::RENTAL === $type ? Bookable::DAYS : Bookable::FIXED ) ); $type = in_array( $mode, array( Bookable::DAYS, Bookable::NIGHTS ), true ) ? Bookable::RENTAL : $type; $price = Money::from_decimal( (string) ( $service['price'] ?? '0' ), $currency ); $rows[] = array( 'row' => Bookable::validate( array( 'type' => $type, 'name' => (string) $service['name'], 'duration_mode' => $mode, 'duration' => max( 5, (int) ( $service['duration'] ?? 60 ) ), 'price' => max( 0, (int) $price ), 'price_basis' => (string) ( $service['basis'] ?? ( Bookable::DAYS === $mode ? 'day' : ( Bookable::NIGHTS === $mode ? 'night' : 'booking' ) ) ), 'capacity' => max( 1, (int) ( $service['capacity'] ?? 1 ) ), 'buffer_after' => max( 0, (int) ( $service['buffer'] ?? 0 ) ), 'color' => self::color( count( $rows ) + 3 ), 'settings' => array( 'staff_mode' => (string) ( $t['staff_mode'] ?? Bookable::STAFF_ANY ), 'timetable' => (array) ( $service['timetable'] ?? array() ), 'min_units' => 1, 'max_units' => Bookable::RANGE === $mode ? 8 : ( Bookable::FIXED === $mode ? 1 : 14 ), 'max_people' => max( 1, (int) ( $service['capacity'] ?? 1 ) ), ), ) ), 'uses_staff' => Bookable::RENTAL !== $type, ); } if ( array() === $rows ) { throw new InvalidArgumentException( __( 'Add at least one service.', 'schedulereed' ) ); } $known = array_keys( Services::methods()->all() ); $payments = array_values( array_intersect( array_map( 'strval', (array) ( $answers['payments'] ?? array( 'onsite' ) ) ), $known ) ); // Everything is valid: save. update_option( Settings::BUSINESS, array( 'name' => mb_substr( sanitize_text_field( (string) ( $answers['business']['name'] ?? get_option( 'blogname' ) ) ), 0, 191 ), 'template' => $template, 'phone' => mb_substr( sanitize_text_field( (string) ( $answers['business']['phone'] ?? '' ) ), 0, 40 ), 'address' => mb_substr( sanitize_textarea_field( (string) ( $answers['business']['address'] ?? '' ) ), 0, 500 ), 'logo_id' => 0, ) ); update_option( Settings::CURRENCY, $currency ); if ( '' !== $zone ) { update_option( 'timezone_string', $zone ); } update_option( Settings::NOTIFY_EMAIL, $email ); update_option( Settings::APPROVAL, 'manual' === ( $answers['approval'] ?? ( $t['approval'] ?? 'auto' ) ) ? 'manual' : 'auto' ); update_option( Settings::SCHEDULING, array_merge( (array) get_option( Settings::SCHEDULING, array() ), (array) $t['scheduling'] ) ); update_option( Settings::PAYMENT_METHODS, array() === $payments ? array( 'onsite' ) : $payments ); if ( isset( $answers['bank_details'] ) ) { update_option( Settings::BANK_DETAILS, mb_substr( sanitize_textarea_field( (string) $answers['bank_details'] ), 0, 2000 ) ); } Services::schedules()->set_weekly( 'site', 0, $hours ); $staff_ids = array(); foreach ( $staff as $member ) { $existing = $member['user_id'] > 0 ? Services::staff()->for_user( $member['user_id'] ) : null; $staff_ids[] = null !== $existing ? (int) $existing['id'] : Services::staff()->create( $member ); } $service_ids = array(); foreach ( $rows as $entry ) { $id = Services::bookables()->create( $entry['row'] ); $service_ids[] = $id; if ( $entry['uses_staff'] ) { Services::links()->set( 'bookable', $id, 'staff', $staff_ids ); } } if ( 1 === preg_match( '/^[A-Z]{2}$/', $country ) ) { update_option( Settings::COUNTRY, $country ); if ( ! empty( $answers['holidays'] ) && in_array( $country, Holidays::countries(), true ) ) { Services::schedules()->clear_holidays(); $year = (int) wp_date( 'Y' ); $today = wp_date( 'Y-m-d' ); foreach ( array( $year, $year + 1 ) as $y ) { foreach ( Holidays::for_year( $country, $y ) as $date => $name ) { if ( $date >= $today ) { Services::schedules()->add_override( 'site', 0, DateOverride::closed_on( $date, $name ), ScheduleRepository::HOLIDAY ); } } } } } if ( ! isset( $answers['pages'] ) || ! empty( $answers['pages'] ) ) { $this->pages(); } update_option( Settings::SETUP_DONE, true ); Cache::bump(); Services::catalog()->flush(); foreach ( $service_ids as $id ) { $bookable = Services::catalog()->bookable( $id ); if ( null !== $bookable && Bookable::CLASS_TYPE === $bookable->type ) { Services::sessions()->generate( $bookable ); } } /** * Fires after the setup wizard is applied. * * @param array $answers The answers. */ do_action( 'schedulereed_setup_done', $answers ); return array( 'services' => $service_ids, 'staff' => $staff_ids, 'booking_url' => Settings::booking_page() > 0 ? (string) get_permalink( Settings::booking_page() ) : '', 'portal_url' => Settings::portal_url(), ); } /** * Make the booking and "My bookings" pages, unless they exist. */ public function pages(): void { $pages = array( Settings::BOOKING_PAGE => array( 'title' => __( 'Book online', 'schedulereed' ), 'content' => '', ), Settings::PORTAL_PAGE => array( 'title' => __( 'My bookings', 'schedulereed' ), 'content' => '', ), ); foreach ( $pages as $option => $page ) { $id = (int) get_option( $option, 0 ); if ( $id > 0 && 'trash' !== get_post_status( $id ) && false !== get_post_status( $id ) ) { continue; } $id = wp_insert_post( array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => $page['title'], 'post_content' => $page['content'], ) ); if ( is_int( $id ) && $id > 0 ) { update_option( $option, $id ); } } } /** * Steps to a working booking page, for the dashboard. * * @return array */ public function checklist(): array { $active = Services::bookables()->all( true ); $page = Settings::booking_page(); $methods = Settings::payment_methods(); $steps = array( array( 'id' => 'services', 'label' => __( 'Add what customers can book', 'schedulereed' ), 'done' => array() !== $active, 'link' => '#/services', ), array( 'id' => 'hours', 'label' => __( 'Set your opening hours', 'schedulereed' ), 'done' => null !== Services::schedules()->weekly( 'site', 0 ), 'link' => '#/settings/hours', ), array( 'id' => 'page', 'label' => __( 'Put the booking form on your site', 'schedulereed' ), 'done' => $page > 0 && 'publish' === get_post_status( $page ), 'link' => '#/settings/general', ), array( 'id' => 'payments', 'label' => __( 'Choose how customers pay', 'schedulereed' ), 'done' => array( 'onsite' ) !== $methods || (bool) get_option( 'schedulereed_payments_reviewed', false ), 'link' => '#/settings/payments', ), array( 'id' => 'email', 'label' => __( 'Send yourself a test email', 'schedulereed' ), 'done' => (bool) get_option( 'schedulereed_test_email_sent', false ), 'link' => '#/emails', ), ); return $steps; } /** * A guess at the site's currency from its locale. */ public static function guess_currency(): string { $stored = (string) get_option( Settings::CURRENCY, '' ); if ( '' !== $stored ) { return $stored; } $map = array( 'US' => 'USD', 'CA' => 'CAD', 'GB' => 'GBP', 'AU' => 'AUD', 'NZ' => 'NZD', 'IN' => 'INR', 'ZA' => 'ZAR', 'SG' => 'SGD', 'MX' => 'MXN', 'BR' => 'BRL', 'CH' => 'CHF', 'SE' => 'SEK', 'NO' => 'NOK', 'DK' => 'DKK', 'PL' => 'PLN', 'JP' => 'JPY', 'AE' => 'AED', 'PK' => 'PKR', ); $country = self::guess_country(); if ( isset( $map[ $country ] ) ) { return $map[ $country ]; } $eurozone = array( 'DE', 'AT', 'FR', 'BE', 'NL', 'ES', 'PT', 'IT', 'IE', 'FI', 'GR', 'LU', 'SK', 'SI', 'EE', 'LV', 'LT', 'MT', 'CY', 'HR' ); return in_array( $country, $eurozone, true ) ? 'EUR' : 'USD'; } /** * A guess at the site's country from its locale, e.g. en_GB → GB. */ public static function guess_country(): string { $stored = Settings::country(); if ( '' !== $stored ) { return $stored; } $locale = get_locale(); if ( 1 === preg_match( '/^[a-z]{2,3}_([A-Z]{2})/', $locale, $m ) ) { return $m[1]; } return 'US'; } /** * A pleasant colour by position. * * @param int $i Position. */ private static function color( int $i ): string { $colors = array( '#2f5bea', '#14967f', '#d9480f', '#7048e8', '#c2255c', '#1c7ed6', '#2b8a3e', '#e67700' ); return $colors[ $i % count( $colors ) ]; } }