120, // Days ahead customers can book. 'horizon' => 60, // Minutes a slot is held while the customer checks out. 'hold_minutes' => 15, // Minutes an unpaid online booking keeps its time. 'pay_minutes' => 30, // 12 or 24 hour clock on the booking form. 'clock' => 'auto', // First day of the week on calendars, 0 = Sunday ... 6 = Saturday, or site setting. 'week_starts' => 'auto', ); /** Which booking form fields show; required, optional or hidden. */ public const FIELD_DEFAULTS = array( 'phone' => 'optional', 'last_name' => 'required', 'notes' => 'optional', 'address' => 'hidden', 'terms' => '', 'terms_url' => '', 'marketing' => false, ); public static function currency(): string { $currency = strtoupper( (string) get_option( self::CURRENCY, 'USD' ) ); return 1 === preg_match( '/^[A-Z]{3}$/', $currency ) ? $currency : 'USD'; } /** * ISO 3166-1 country of the business, '' until the wizard sets it. */ public static function country(): string { $country = strtoupper( (string) get_option( self::COUNTRY, '' ) ); return 1 === preg_match( '/^[A-Z]{2}$/', $country ) ? $country : ''; } /** * Business details from the setup wizard. * * @return array{name: string, template: string, phone: string, address: string, logo_id: int} */ public static function business(): array { $stored = (array) get_option( self::BUSINESS, array() ); return array( 'name' => (string) ( $stored['name'] ?? wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ) ), 'template' => (string) ( $stored['template'] ?? '' ), 'phone' => (string) ( $stored['phone'] ?? '' ), 'address' => (string) ( $stored['address'] ?? '' ), 'logo_id' => (int) ( $stored['logo_id'] ?? 0 ), ); } /** * The site time zone, used for hours unless a location or staff member has its own. */ public static function timezone(): DateTimeZone { return wp_timezone(); } /** * A time zone by name, falling back to the site's. * * @param string $name IANA name or ''. */ public static function zone( string $name ): DateTimeZone { if ( '' !== $name ) { try { return new DateTimeZone( $name ); } catch ( Exception ) { return self::timezone(); } } return self::timezone(); } /** * Whether new bookings need the owner's approval: "auto" or "manual". */ public static function approval(): string { return 'manual' === get_option( self::APPROVAL ) ? 'manual' : 'auto'; } public static function notify_email(): string { $email = (string) get_option( self::NOTIFY_EMAIL, '' ); return is_email( $email ) ? $email : (string) get_option( 'admin_email' ); } /** * IDs of the payment methods offered; pay on site until the owner chooses. * * @return string[] */ public static function payment_methods(): array { $ids = (array) get_option( self::PAYMENT_METHODS, array( 'onsite' ) ); return array_values( array_filter( array_map( 'strval', $ids ), static fn( string $id ): bool => 1 === preg_match( '/^[a-z0-9_]{1,40}$/', $id ) ) ); } public static function deposit(): DepositPolicy { $stored = get_option( self::DEPOSIT, array() ); try { return DepositPolicy::from_array( is_array( $stored ) ? $stored : array() ); } catch ( InvalidArgumentException ) { return DepositPolicy::full(); } } public static function bank_details(): string { return (string) get_option( self::BANK_DETAILS, '' ); } /** * Stripe keys. Kept out of /wp/v2/settings so secrets never reach the browser. * * @return array{publishable_key: string, secret_key: string, webhook_secret: string} */ public static function stripe(): array { $stored = (array) get_option( self::STRIPE, array() ); return array( 'publishable_key' => (string) ( $stored['publishable_key'] ?? '' ), 'secret_key' => (string) ( $stored['secret_key'] ?? '' ), 'webhook_secret' => (string) ( $stored['webhook_secret'] ?? '' ), ); } /** * PayPal REST credentials; sandbox unless set to live. * * @return array{client_id: string, secret: string, mode: string} */ public static function paypal(): array { $stored = (array) get_option( self::PAYPAL, array() ); return array( 'client_id' => (string) ( $stored['client_id'] ?? '' ), 'secret' => (string) ( $stored['secret'] ?? '' ), 'mode' => 'live' === ( $stored['mode'] ?? '' ) ? 'live' : 'sandbox', ); } /** * Taxes and fees. * * @return TaxRule[] */ public static function taxes(): array { try { return TaxRule::list_from( get_option( self::TAXES, array() ) ); } catch ( InvalidArgumentException ) { return array(); } } public static function prices_include_tax(): bool { return (bool) get_option( self::PRICES_INC_TAX, false ); } /** * The site's cancellation terms; free until 24 hours before by default. */ public static function cancellation(): ?CancellationPolicy { $stored = get_option( self::CANCELLATION, false ); if ( false === $stored ) { return CancellationPolicy::free_until( 24 ); } try { return CancellationPolicy::from_array( '' === $stored ? null : $stored ); } catch ( InvalidArgumentException ) { return null; } } public static function changes(): ChangeRules { return ChangeRules::from_array( get_option( self::CHANGES, array() ) ); } /** * Scheduling defaults. * * @return array */ public static function scheduling(): array { $stored = (array) get_option( self::SCHEDULING, array() ); $out = array_merge( self::SCHEDULING_DEFAULTS, array_intersect_key( $stored, self::SCHEDULING_DEFAULTS ) ); foreach ( array( 'lead_time', 'horizon', 'hold_minutes', 'pay_minutes' ) as $key ) { $out[ $key ] = max( 0, (int) $out[ $key ] ); } $out['hold_minutes'] = max( 5, min( 60, $out['hold_minutes'] ) ); $out['pay_minutes'] = max( 10, min( 1440, $out['pay_minutes'] ) ); $out['horizon'] = max( 1, min( 1095, $out['horizon'] ) ); return $out; } /** * Booking form fields. * * @return array */ public static function fields(): array { $stored = (array) get_option( self::FIELDS, array() ); $out = array_merge( self::FIELD_DEFAULTS, array_intersect_key( $stored, self::FIELD_DEFAULTS ) ); foreach ( array( 'phone', 'last_name', 'notes', 'address' ) as $field ) { if ( ! in_array( $out[ $field ], array( 'required', 'optional', 'hidden' ), true ) ) { $out[ $field ] = self::FIELD_DEFAULTS[ $field ]; } } $out['terms'] = (string) $out['terms']; $out['terms_url'] = esc_url_raw( (string) $out['terms_url'] ); $out['marketing'] = (bool) $out['marketing']; /** * Change the booking form's fields, e.g. for Pro's form builder. * * @param array $fields Field settings. */ return (array) apply_filters( 'schedulereed_fields', $out ); } /** * Look of the booking form. * * @return array{color: string, radius: int, style: string} */ public static function appearance(): array { $stored = (array) get_option( self::APPEARANCE, array() ); $color = (string) ( $stored['color'] ?? '#2f5bea' ); return array( 'color' => 1 === preg_match( '/^#[0-9a-fA-F]{6}$/', $color ) ? $color : '#2f5bea', 'radius' => max( 0, min( 24, (int) ( $stored['radius'] ?? 8 ) ) ), 'style' => in_array( $stored['style'] ?? '', array( 'inline', 'popup' ), true ) ? $stored['style'] : 'inline', ); } /** * Spam protection: Turnstile or reCAPTCHA keys and the request limit. * * @return array{captcha: string, site_key: string, secret_key: string, limit: int} */ public static function security(): array { $stored = (array) get_option( self::SECURITY, array() ); return array( 'captcha' => in_array( $stored['captcha'] ?? '', array( 'turnstile', 'recaptcha' ), true ) ? $stored['captcha'] : '', 'site_key' => (string) ( $stored['site_key'] ?? '' ), 'secret_key' => (string) ( $stored['secret_key'] ?? '' ), 'limit' => max( 5, min( 1000, (int) ( $stored['limit'] ?? 30 ) ) ), ); } /** * Data retention: months after which old customers without upcoming * bookings are anonymised; 0 keeps them. * * @return array{retention_months: int, consent_text: string} */ public static function privacy(): array { $stored = (array) get_option( self::PRIVACY, array() ); return array( 'retention_months' => max( 0, min( 240, (int) ( $stored['retention_months'] ?? 0 ) ) ), 'consent_text' => (string) ( $stored['consent_text'] ?? '' ), ); } /** * Hours before the start that reminders go out, e.g. [24, 2]. * * @return int[] */ public static function reminders(): array { $stored = get_option( self::REMINDERS, array( 24 ) ); $hours = array_values( array_unique( array_filter( array_map( 'intval', (array) $stored ), static fn( int $h ): bool => $h > 0 && $h <= 720 ) ) ); rsort( $hours ); return array_slice( $hours, 0, 3 ); } /** * Secret used in iCal feed URLs, made on first use. */ public static function ical_key(): string { $key = (string) get_option( self::ICAL_KEY, '' ); if ( '' === $key ) { $key = wp_generate_password( 32, false ); update_option( self::ICAL_KEY, $key, false ); } return $key; } /** * The page with the booking form, 0 when none. */ public static function booking_page(): int { return (int) get_option( self::BOOKING_PAGE, 0 ); } /** * Where customers manage bookings: the portal page, or the booking page. */ public static function portal_url(): string { $page = (int) get_option( self::PORTAL_PAGE, 0 ); $page = \ScheduleReed\Integrations\Multilingual::page( $page > 0 ? $page : self::booking_page() ); $url = $page > 0 ? get_permalink( $page ) : false; return $url ? (string) $url : home_url( '/' ); } /** * Options for /wp/v2/settings, so the admin app can read and save them. */ public static function register(): void { register_setting( 'schedulereed', self::CURRENCY, array( 'type' => 'string', 'show_in_rest' => true, 'default' => 'USD', 'sanitize_callback' => array( self::class, 'sanitize_currency' ), ) ); register_setting( 'schedulereed', self::COUNTRY, array( 'type' => 'string', 'show_in_rest' => true, 'default' => '', 'sanitize_callback' => array( self::class, 'sanitize_country' ), ) ); register_setting( 'schedulereed', self::APPROVAL, array( 'type' => 'string', 'show_in_rest' => true, 'default' => 'auto', 'sanitize_callback' => array( self::class, 'sanitize_approval' ), ) ); register_setting( 'schedulereed', self::NOTIFY_EMAIL, array( 'type' => 'string', 'show_in_rest' => true, 'default' => '', 'sanitize_callback' => 'sanitize_email', ) ); register_setting( 'schedulereed', self::BANK_DETAILS, array( 'type' => 'string', 'show_in_rest' => true, 'default' => '', 'sanitize_callback' => 'sanitize_textarea_field', ) ); register_setting( 'schedulereed', self::PRICES_INC_TAX, array( 'type' => 'boolean', 'show_in_rest' => true, 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ) ); register_setting( 'schedulereed', self::REMOVE_DATA, array( 'type' => 'boolean', 'show_in_rest' => true, 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ) ); register_setting( 'schedulereed', self::BOOKING_PAGE, array( 'type' => 'integer', 'show_in_rest' => true, 'default' => 0, 'sanitize_callback' => 'absint', ) ); register_setting( 'schedulereed', self::PORTAL_PAGE, array( 'type' => 'integer', 'show_in_rest' => true, 'default' => 0, 'sanitize_callback' => 'absint', ) ); } /** * A three-letter ISO 4217 currency code, or USD. * * @param mixed $value Submitted value. */ public static function sanitize_currency( $value ): string { $code = strtoupper( sanitize_text_field( (string) $value ) ); return 1 === preg_match( '/^[A-Z]{3}$/', $code ) ? $code : 'USD'; } /** * A two-letter ISO 3166 country code, or ''. * * @param mixed $value Submitted value. */ public static function sanitize_country( $value ): string { $code = strtoupper( sanitize_text_field( (string) $value ) ); return 1 === preg_match( '/^[A-Z]{2}$/', $code ) ? $code : ''; } /** * Whether new bookings need approval: 'manual' or 'auto'. * * @param mixed $value Submitted value. */ public static function sanitize_approval( $value ): string { return 'manual' === $value ? 'manual' : 'auto'; } }