0, 'staff' => 0, 'customers' => 0, 'bookings' => 0, 'skipped' => 0, 'notes' => array(), ); protected bool $dry_run = false; /** * Old ID => new ID, by kind. * * @var array> */ private array $map = array(); abstract public function id(): string; abstract public function label(): string; /** * How much there is to import, or null when the plugin's data is not on this site. * * @return array{services: int, bookings: int}|null */ abstract public function found(): ?array; /** * Read and add everything. */ abstract protected function import(): void; /** * Run the import. * * @param bool $dry_run Only count. * @return array The report, with "more" when bookings are left for another run. * @throws InvalidArgumentException When there is nothing to import. */ public function run( bool $dry_run = false ): array { if ( null === $this->found() ) { /* translators: %s: plugin name. */ throw new InvalidArgumentException( esc_html( sprintf( __( 'No %s data was found on this site.', 'schedulereed' ), $this->label() ) ) ); } $this->dry_run = $dry_run; $this->map = (array) get_option( 'schedulereed_import_' . $this->id(), array() ); $this->import(); if ( ! $dry_run ) { update_option( 'schedulereed_import_' . $this->id(), $this->map, false ); Cache::bump(); Services::catalog()->flush(); } $this->report['notes'] = array_values( array_unique( $this->report['notes'] ) ); return $this->report + array( 'more' => $this->report['bookings'] + $this->report['skipped'] >= self::BATCH ); } /** * Our ID for an old one, or 0. * * @param string $kind Kind. * @param int|string $old Old ID. */ protected function mapped( string $kind, $old ): int { return (int) ( $this->map[ $kind ][ (string) $old ] ?? 0 ); } /** * Whether an old item was handled before (imported or left out). * * @param string $kind Kind. * @param int|string $old Old ID. */ protected function known( string $kind, $old ): bool { return isset( $this->map[ $kind ][ (string) $old ] ); } /** * Remember a new ID. * * @param string $kind Kind. * @param int|string $old Old ID. * @param int $new New ID. */ protected function remember( string $kind, $old, int $new ): void { $this->map[ $kind ][ (string) $old ] = $new; } /** * Add a service once. * * @param int|string $old Old ID. * @param array $data name, minutes, price (decimal string), capacity, days (bool), approval (bool), description. */ protected function service( $old, array $data ): int { $have = $this->mapped( 'service', $old ); if ( $have > 0 || ( $this->dry_run && isset( $this->map['service'][ (string) $old ] ) ) ) { return $have; } ++$this->report['services']; if ( $this->dry_run ) { $this->remember( 'service', $old, 0 ); return 0; } $days = ! empty( $data['days'] ); $capacity = max( 1, (int) ( $data['capacity'] ?? 1 ) ); $row = Bookable::validate( array( 'type' => $days ? Bookable::RENTAL : ( $capacity > 1 ? Bookable::CLASS_TYPE : Bookable::APPOINTMENT ), 'name' => mb_substr( trim( (string) $data['name'] ), 0, 191 ) ?: __( 'Service', 'schedulereed' ), // phpcs:ignore Universal.Operators.DisallowShortTernary.Found 'description' => wp_strip_all_tags( (string) ( $data['description'] ?? '' ) ), 'duration_mode' => $days ? Bookable::DAYS : Bookable::FIXED, 'duration' => max( 5, min( 1440, (int) ( $data['minutes'] ?? 60 ) ) ), 'price' => max( 0, (int) Money::from_decimal( (string) ( $data['price'] ?? '0' ), Settings::currency() ) ), 'price_basis' => $days ? 'day' : 'booking', 'capacity' => $capacity, 'status' => 'active', 'settings' => array( 'approval' => ! empty( $data['approval'] ) ? 'manual' : null, 'max_people' => $capacity, ), ) ); $id = Services::bookables()->create( $row ); $this->remember( 'service', $old, $id ); return $id; } /** * Add a staff member once. * * @param int|string $old Old ID. * @param string $name Name. * @param string $email Email. */ protected function staff( $old, string $name, string $email = '' ): int { $have = $this->mapped( 'staff', $old ); if ( $have > 0 || ( $this->dry_run && isset( $this->map['staff'][ (string) $old ] ) ) ) { return $have; } ++$this->report['staff']; if ( $this->dry_run ) { $this->remember( 'staff', $old, 0 ); return 0; } $id = Services::staff()->create( array( 'name' => mb_substr( '' !== trim( $name ) ? trim( $name ) : __( 'Staff member', 'schedulereed' ), 0, 191 ), 'email' => is_email( $email ) ? $email : '', 'status' => 'active', ) ); $this->remember( 'staff', $old, $id ); return $id; } /** * Let a staff member do a service. * * @param int $service Service. * @param int $staff Staff member. */ protected function link( int $service, int $staff ): void { if ( $this->dry_run || $service <= 0 || $staff <= 0 ) { return; } $current = Services::links()->targets( 'bookable', $service, 'staff' ); if ( ! isset( $current[ $staff ] ) ) { $current[ $staff ] = array( 'id' => $staff ); Services::links()->set( 'bookable', $service, 'staff', array_values( $current ) ); } } /** * Find or add a customer. * * @param string $email Email. * @param string $first First name. * @param string $last Last name. * @param string $phone Phone. * @param int $user WordPress user. */ protected function customer( string $email, string $first, string $last, string $phone = '', int $user = 0 ): int { $email = sanitize_email( $email ); if ( $this->dry_run ) { if ( '' === $email || null === Services::customers()->find_by_email( $email ) ) { $key = '' !== $email ? strtolower( $email ) : $first . ' ' . $last; if ( ! isset( $this->map['customer_dry'][ $key ] ) ) { $this->map['customer_dry'][ $key ] = 0; ++$this->report['customers']; } } return 0; } $known = '' !== $email ? Services::customers()->find_by_email( $email ) : null; if ( null === $known ) { ++$this->report['customers']; } return Services::customers()->upsert( array( 'email' => $email, 'first_name' => mb_substr( sanitize_text_field( $first ), 0, 100 ), 'last_name' => mb_substr( sanitize_text_field( $last ), 0, 100 ), 'phone' => mb_substr( sanitize_text_field( $phone ), 0, 40 ), 'user_id' => $user, ) ); } /** * Add a booking once. * * @param int|string $old Old ID. * @param array $booking status, customer_id, total (minor), paid (minor), notes, created. * @param array> $items bookable_id, staff_id, start, end, quantity, price (minor). */ protected function booking( $old, array $booking, array $items ): int { if ( isset( $this->map['booking'][ (string) $old ] ) ) { return $this->mapped( 'booking', $old ); } $items = array_values( array_filter( $items, static fn( array $i ): bool => $i['bookable_id'] > 0 || ! empty( $i['dry'] ) ) ); if ( array() === $items || ! in_array( $booking['status'], Status::ALL, true ) ) { ++$this->report['skipped']; $this->remember( 'booking', $old, 0 ); return 0; } ++$this->report['bookings']; if ( $this->dry_run ) { $this->remember( 'booking', $old, 0 ); return 0; } $total = max( 0, (int) $booking['total'] ); $id = Services::bookings()->create( array( 'uid' => \ScheduleReed\Booking\Checkout::reference(), 'token' => wp_generate_password( 32, false ), 'customer_id' => (int) $booking['customer_id'], 'status' => $booking['status'], 'source' => 'import', 'start' => (int) min( array_column( $items, 'start' ) ), 'end' => (int) max( array_column( $items, 'end' ) ), 'total' => $total, 'currency' => Settings::currency(), 'notes' => mb_substr( (string) ( $booking['notes'] ?? '' ), 0, 2000 ), ), $items ); $paid = min( $total, max( 0, (int) ( $booking['paid'] ?? 0 ) ) ); if ( $paid > 0 ) { Services::payments()->create( array( 'booking_id' => $id, 'gateway' => PaymentService::OTHER, 'amount' => $paid, 'currency' => Settings::currency(), /* translators: %s: plugin name. */ 'note' => sprintf( __( 'Paid in %s', 'schedulereed' ), $this->label() ), ) ); Services::bookings()->set_paid( $id, $paid ); } Services::activity()->record( $id, 'imported', array( 'from' => $this->label(), 'id' => (string) $old, ) ); $this->remember( 'booking', $old, $id ); return $id; } /** * A local date and time to Unix time. * * @param string $local e.g. 2026-05-01 14:30 or 20260501143000. * @param string $zone Time zone, '' for the site's. */ protected static function time( string $local, string $zone = '' ): ?int { $local = trim( $local ); if ( 1 === preg_match( '/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/', $local, $m ) ) { $local = "{$m[1]}-{$m[2]}-{$m[3]} {$m[4]}:{$m[5]}:{$m[6]}"; } try { return ( new \DateTimeImmutable( $local, '' !== $zone ? Settings::zone( $zone ) : Settings::timezone() ) )->getTimestamp(); } catch ( \Exception $e ) { return null; } } protected function note( string $note ): void { $this->report['notes'][] = $note; } /** * Posts of a type, oldest first, in any status but trash. Read from the * table, since the other plugin's statuses are not registered while it * is inactive. * * @param string $type Post type. * @param int $limit Most to read. * @param int $parent Only children of this post, or -1 for all. * @return \WP_Post[] */ protected static function posts( string $type, int $limit = 1000, int $parent = -1 ): array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_type = %s AND post_status NOT IN ('trash', 'auto-draft') AND ( %d < 0 OR post_parent = %d ) ORDER BY ID ASC LIMIT %d", $type, $parent, $parent, $limit ) ); return array_map( static fn( object $row ): \WP_Post => new \WP_Post( $row ), $rows ); } /** * How many posts of a type there are, whatever their status. * * @param string $type Post type. */ protected static function count_posts( string $type ): int { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s AND post_status NOT IN ('trash', 'auto-draft')", $type ) ); } }