[ label, detection callback ]. */ public static function integrations() { return array( 'motopress' => array( 'MotoPress Hotel Booking', static function () { return function_exists( 'MPHB' ) || class_exists( 'HotelBookingPlugin' ); } ), 'latepoint' => array( 'LatePoint', static function () { return class_exists( 'LatePoint' ) || defined( 'LATEPOINT_VERSION' ); } ), 'amelia' => array( 'Amelia', static function () { return defined( 'AMELIA_VERSION' ) || class_exists( 'AmeliaBooking\Plugin' ); } ), 'ssa' => array( 'Simply Schedule Appointments', static function () { return class_exists( 'Simply_Schedule_Appointments' ) || function_exists( 'ssa' ); } ), ); } public static function init() { if ( ! Nodebook_Settings::get( 'bookings' ) ) { return; } add_action( 'mphb_booking_status_changed', array( __CLASS__, 'motopress' ), 20, 1 ); add_action( 'latepoint_booking_created', array( __CLASS__, 'latepoint' ), 20, 1 ); add_action( 'amelia_after_booking_added', array( __CLASS__, 'amelia' ), 20, 1 ); add_action( 'ssa/appointment/booked', array( __CLASS__, 'ssa' ), 20, 2 ); } /** MotoPress Hotel Booking: record confirmed or pending bookings (one entry per booking, updated on change). */ public static function motopress( $booking ) { try { if ( ! is_object( $booking ) || ! method_exists( $booking, 'getId' ) || ! method_exists( $booking, 'getCustomer' ) ) { return; } $status = method_exists( $booking, 'getStatus' ) ? (string) $booking->getStatus() : ''; if ( $status && ! in_array( $status, array( 'confirmed', 'pending', 'pending-user', 'pending-payment' ), true ) ) { // Cancelled / abandoned: only update a booking we already recorded (so it stops counting), never add one. if ( ! Nodebook_Activities::find( 'booking', (int) $booking->getId() ) ) { return; } } $c = $booking->getCustomer(); $person = array( 'email' => self::call( $c, 'getEmail' ), 'first_name' => self::call( $c, 'getFirstName' ), 'last_name' => self::call( $c, 'getLastName' ), 'phone' => self::call( $c, 'getPhone' ), 'country' => self::call( $c, 'getCountry' ), 'city' => self::call( $c, 'getCity' ), ); $in = Nodebook_Conversions::ymd( method_exists( $booking, 'getCheckInDate' ) ? $booking->getCheckInDate() : '' ); $out = Nodebook_Conversions::ymd( method_exists( $booking, 'getCheckOutDate' ) ? $booking->getCheckOutDate() : '' ); $item = 0; if ( method_exists( $booking, 'getReservedRooms' ) ) { foreach ( (array) $booking->getReservedRooms() as $room ) { $item = (int) self::call( $room, 'getRoomTypeId' ); if ( $item ) { break; } } } self::record( $person, 'booking', $item ? get_the_title( $item ) : __( 'a stay', 'nodebook' ), array( 'source' => 'MotoPress Hotel Booking', 'external_id' => (int) $booking->getId(), 'value' => self::call( $booking, 'getTotalPrice' ), 'currency' => function_exists( 'MPHB' ) && is_object( MPHB()->settings() ) ? self::call( MPHB()->settings()->currency(), 'getCurrencyCode' ) : '', 'object_id' => $item, 'dates' => array( $in, $out ), 'status' => $status, ) ); } catch ( \Throwable $e ) { return; // Never interfere with a booking. } } /** Read a property that may be loaded on demand through __get (isset() would say it is not there). */ private static function prop( $obj, $name ) { if ( ! is_object( $obj ) ) { return null; } if ( ! property_exists( $obj, $name ) && ! method_exists( $obj, '__get' ) ) { return null; } try { return $obj->$name; } catch ( \Throwable $e ) { return null; } } /** LatePoint: OsBookingModel; customer and service are loaded on demand (no __isset), the amount sits on the order item. */ public static function latepoint( $booking ) { try { if ( ! is_object( $booking ) ) { return; } $c = self::prop( $booking, 'customer' ); $person = is_object( $c ) ? array_filter( array( 'email' => (string) self::prop( $c, 'email' ), 'first_name' => (string) self::prop( $c, 'first_name' ), 'last_name' => (string) self::prop( $c, 'last_name' ), 'phone' => (string) self::prop( $c, 'phone' ), ) ) : array(); $svc = self::prop( $booking, 'service' ); $service = is_object( $svc ) ? (string) self::prop( $svc, 'name' ) : ''; $date = Nodebook_Conversions::ymd( (string) self::prop( $booking, 'start_date' ) ); $price = ''; if ( method_exists( $booking, 'get_order_item' ) ) { $item = $booking->get_order_item(); foreach ( array( 'total', 'subtotal' ) as $k ) { $v = self::prop( $item, $k ); if ( is_numeric( $v ) && (float) $v > 0 ) { $price = (float) $v; break; } } } if ( '' === $price && method_exists( $booking, 'full_amount_to_charge' ) ) { $v = $booking->full_amount_to_charge(); $price = is_numeric( $v ) && (float) $v > 0 ? (float) $v : ''; } self::record( $person, 'appointment', $service, array( 'source' => 'LatePoint', 'external_id' => (int) self::prop( $booking, 'id' ), 'value' => $price, 'object_id' => self::item_by_title( $service ), 'dates' => array( $date ), 'status' => (string) self::prop( $booking, 'status' ), ) ); } catch ( \Throwable $e ) { return; } } /** * Amelia (2.x): { type, appointment|event: {…, bookingStart, service, provider, bookings}, booking: {id, price, persons}, * customer: {email, firstName, lastName, phone} }. Older versions passed the appointment itself. * The customer is read from the customer fields only, never from anywhere in the data: the appointment also * holds the staff member (provider) with their own email. */ public static function amelia( $data ) { try { $data = (array) json_decode( wp_json_encode( $data ), true ); $type = isset( $data['type'] ) && is_string( $data['type'] ) ? $data['type'] : 'appointment'; $res = isset( $data[ $type ] ) && is_array( $data[ $type ] ) ? $data[ $type ] : $data; $booking = isset( $data['booking'] ) && is_array( $data['booking'] ) ? $data['booking'] : ( isset( $res['bookings'][0] ) && is_array( $res['bookings'][0] ) ? $res['bookings'][0] : array() ); $cust = isset( $data['customer'] ) && is_array( $data['customer'] ) && ! empty( $data['customer']['email'] ) ? $data['customer'] : ( isset( $booking['customer'] ) && is_array( $booking['customer'] ) ? $booking['customer'] : array() ); $person = array_filter( array( 'email' => isset( $cust['email'] ) ? (string) $cust['email'] : '', 'first_name' => isset( $cust['firstName'] ) ? (string) $cust['firstName'] : '', 'last_name' => isset( $cust['lastName'] ) ? (string) $cust['lastName'] : '', 'phone' => isset( $cust['phone'] ) ? (string) $cust['phone'] : '', 'country' => isset( $cust['countryPhoneIso'] ) ? strtoupper( (string) $cust['countryPhoneIso'] ) : '', ) ); $service = ''; if ( isset( $res['service']['name'] ) && is_string( $res['service']['name'] ) ) { $service = $res['service']['name']; } elseif ( 'event' === $type && isset( $res['name'] ) && is_string( $res['name'] ) ) { $service = $res['name']; } elseif ( ! empty( $res['serviceId'] ) ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Amelia's own table, read once per booking. $service = (string) $wpdb->get_var( $wpdb->prepare( 'SELECT name FROM %i WHERE id = %d', $wpdb->prefix . 'amelia_services', (int) $res['serviceId'] ) ); } $start = isset( $res['bookingStart'] ) ? $res['bookingStart'] : ( isset( $res['periods'][0]['periodStart'] ) ? $res['periods'][0]['periodStart'] : '' ); $price = isset( $booking['price'] ) && is_numeric( $booking['price'] ) ? (float) $booking['price'] : null; if ( null !== $price && ( ! isset( $booking['aggregatedPrice'] ) || $booking['aggregatedPrice'] ) ) { $price *= max( 1, (int) ( $booking['persons'] ?? 1 ) ); } self::record( $person, 'event' === $type ? 'booking' : 'appointment', $service, array( 'source' => 'Amelia', 'external_id' => isset( $booking['id'] ) ? (int) $booking['id'] : ( isset( $res['id'] ) ? (int) $res['id'] : 0 ), 'value' => null === $price ? '' : $price, 'object_id' => self::item_by_title( $service ), 'dates' => array( Nodebook_Conversions::ymd( $start ) ), 'status' => isset( $booking['status'] ) ? (string) $booking['status'] : '', ) ); } catch ( \Throwable $e ) { return; } } /** Simply Schedule Appointments: ( $appointment_id, $data ). */ public static function ssa( $appointment_id, $data = array() ) { try { $data = (array) json_decode( wp_json_encode( $data ), true ); $info = isset( $data['customer_information'] ) ? (array) $data['customer_information'] : $data; $person = self::person( $info ); $type = ''; if ( ! empty( $data['appointment_type_id'] ) && function_exists( 'ssa' ) ) { $t = ssa()->appointment_type_model->get( (int) $data['appointment_type_id'] ); $type = is_array( $t ) && isset( $t['title'] ) ? (string) $t['title'] : ''; } self::record( $person, 'appointment', $type, array( 'source' => 'Simply Schedule Appointments', 'external_id' => (int) $appointment_id, 'object_id' => self::item_by_title( $type ), 'dates' => array( Nodebook_Conversions::ymd( isset( $data['start_date'] ) ? $data['start_date'] : '' ) ), ) ); } catch ( \Throwable $e ) { return; } } /** * Map any flat or nested array of customer data to contact fields by guessing keys * ("Email", "customer_email", "Phone Number", "firstName"…). */ public static function person( array $data ) { $person = array(); foreach ( self::flatten( $data ) as $key => $value ) { if ( ! is_scalar( $value ) || '' === trim( (string) $value ) ) { continue; } $label = trim( preg_replace( '/([a-z])([A-Z])/', '$1 $2', (string) $key ) ); $field = Nodebook_Fields::guess( $label ); if ( in_array( $field, array( '', 'skip', 'message' ), true ) || isset( $person[ $field ] ) ) { continue; } // "email_consent": true or "email_verified": 1 are not addresses. if ( 'email' === $field && ! is_email( trim( (string) $value ) ) ) { continue; } $person[ $field ] = (string) $value; } return $person; } /** [ 'customer' => [ 'email' => x ] ] → [ 'customer email' => x ] (last key segment kept separately too). */ public static function flatten( array $data, $prefix = '', $depth = 0 ) { $out = array(); foreach ( $data as $k => $v ) { if ( is_array( $v ) ) { if ( $depth < 3 ) { $out += self::flatten( $v, is_int( $k ) ? $prefix : $k, $depth + 1 ); } continue; } if ( ! isset( $out[ $k ] ) && ! is_int( $k ) ) { $out[ $k ] = $v; } elseif ( $prefix ) { $out[ $prefix . ' ' . $k ] = $v; } } return $out; } /** Booking title per niche/type, then record. */ private static function record( array $person, $type, $what, array $args ) { $range = isset( $args['dates'] ) ? Nodebook_Conversions::range_label( (string) ( $args['dates'][0] ?? '' ), (string) ( $args['dates'][1] ?? '' ) ) : ''; $what = '' !== (string) $what ? (string) $what : __( 'an appointment', 'nodebook' ); $title = 'booking' === $type /* translators: 1: what was booked, e.g. "Villa 26" */ ? sprintf( __( 'Booked %s', 'nodebook' ), $what ) /* translators: 1: service, e.g. "Botox consultation" */ : sprintf( __( 'Appointment: %s', 'nodebook' ), $what ); if ( $range ) { $title .= ' · ' . $range; } Nodebook_Conversions::record( $person, $type, $title, $args ); } /** A service/property page with exactly this title, so the booking ties to the pages they read. */ private static function item_by_title( $title ) { $title = trim( (string) $title ); if ( '' === $title ) { return 0; } $q = new WP_Query( array( 'post_type' => Nodebook_Interests::item_types(), 'title' => $title, 'posts_per_page' => 1, 'fields' => 'ids', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); return $q->posts ? (int) $q->posts[0] : 0; } private static function call( $obj, $method ) { return is_object( $obj ) && method_exists( $obj, $method ) ? $obj->$method() : ''; } }