false ) ); if ( ! $cid ) { return 0; } $data = array_filter( array( 'source' => $source, 'value' => isset( $args['value'] ) && '' !== $args['value'] ? round( (float) $args['value'], 2 ) : null, 'currency' => isset( $args['currency'] ) ? strtoupper( substr( sanitize_key( (string) $args['currency'] ), 0, 3 ) ) : null, 'dates' => isset( $args['dates'] ) ? array_filter( (array) $args['dates'] ) : null, 'status' => isset( $args['status'] ) ? sanitize_text_field( (string) $args['status'] ) : null, 'item' => isset( $args['object_id'] ) ? absint( $args['object_id'] ) : null, ), static function ( $v ) { return null !== $v && array() !== $v && 0 !== $v; } ); $ext = isset( $args['external_id'] ) ? absint( $args['external_id'] ) : 0; $existing = $ext ? Nodebook_Activities::find( $type, $ext ) : null; if ( $existing ) { Nodebook_Activities::update( $existing['id'], array( 'contact_id' => $cid, 'title' => $title, 'data' => $data, ) ); } else { Nodebook_Activities::add( $cid, $type, $title, array( 'object_id' => $ext, 'user_id' => 0, 'visitor_id' => Nodebook_Tracker::current_visitor_id(), 'url' => isset( $args['url'] ) ? $args['url'] : '', 'data' => $data, ) ); do_action( 'nodebook_conversion', $cid, $type, $title, $data ); } $c = Nodebook_Contacts::get( $cid ); $settled = ! isset( $data['status'] ) || self::is_settled( $data['status'] ); if ( $c && $settled && in_array( $type, array( 'booking', 'appointment' ), true ) && in_array( $c['status'], array( 'subscriber', 'lead', 'prospect', '' ), true ) ) { Nodebook_Contacts::update( $cid, array( 'status' => 'customer' ) ); Nodebook_Activities::add( $cid, 'status', __( 'Became a customer', 'nodebook' ), array( 'user_id' => 0 ) ); } if ( in_array( $type, array( 'booking', 'appointment' ), true ) ) { self::recalculate( $cid ); } return $cid; } /** Statuses that mean the booking did not happen. */ public static function is_cancelled( $status ) { return (bool) preg_match( '/cancel|reject|abandon|refund|fail|declin|trash|no.?show|expired/i', (string) $status ); } /** Confirmed (or no status given): not cancelled and not still waiting for approval or payment, like an unpaid order. */ public static function is_settled( $status ) { return ! self::is_cancelled( $status ) && ! preg_match( '/pending|on.?hold|awaiting|unconfirmed|tentative|request/i', (string) $status ); } /** * The currency totals are kept in: WooCommerce's, otherwise the one under Settings, otherwise '' * (then each contact's totals use the currency of their first priced booking). */ public static function currency() { $cur = function_exists( 'get_woocommerce_currency' ) ? (string) get_woocommerce_currency() : ''; if ( '' === $cur ) { $cur = (string) Nodebook_Settings::get( 'currency' ); } return (string) apply_filters( 'nodebook_currency', strtoupper( $cur ) ); } /** * Settled bookings and appointments of a contact: how many, their value in the main currency, * and the value of those in other currencies (never added together: no conversion is guessed). * * @return array{0:int,1:float,2:array} count, value in $main, other currencies. */ public static function bookings_total( $contact_id, $main = null ) { global $wpdb; $main = null === $main ? self::currency() : strtoupper( (string) $main ); $count = 0; $value = 0.0; $others = array(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table. foreach ( (array) $wpdb->get_col( $wpdb->prepare( "SELECT data FROM %i WHERE contact_id = %d AND type IN ('booking','appointment') ORDER BY created_at ASC, id ASC", Nodebook_Activities::table(), $contact_id ) ) as $json ) { $d = json_decode( (string) $json, true ); if ( ! is_array( $d ) || empty( $d['value'] ) || ( isset( $d['status'] ) && ! self::is_settled( $d['status'] ) ) ) { continue; } ++$count; $cur = strtoupper( (string) ( $d['currency'] ?? '' ) ); if ( '' === $main && '' !== $cur ) { $main = $cur; } if ( '' === $cur || $cur === $main ) { $value += (float) $d['value']; } else { $others[ $cur ] = ( $others[ $cur ] ?? 0.0 ) + (float) $d['value']; } } return array( $count, $value, $others ); } /** "EUR 1,200 · GBP 300" for amounts in other currencies, or ''. */ public static function others_label( $json ) { $o = is_array( $json ) ? $json : json_decode( (string) $json, true ); if ( ! is_array( $o ) ) { return ''; } $out = array(); foreach ( $o as $cur => $amount ) { if ( (float) $amount ) { $out[] = Nodebook_Admin::money( (float) $amount, (string) $cur ); } } return implode( ' · ', $out ); } public static function recalculate( $contact_id ) { $c = Nodebook_Contacts::get( $contact_id ); if ( ! $c ) { return; } if ( class_exists( 'Nodebook_Woocommerce' ) && Nodebook_Woocommerce::active() && 'off' !== Nodebook_Woocommerce::mode() && $c['email'] ) { Nodebook_Woocommerce::recalculate( $contact_id, $c['email'] ); // Adds the bookings itself. return; } list( $count, $value, $others ) = self::bookings_total( $contact_id ); Nodebook_Contacts::update( $contact_id, array( 'orders_count' => $count, 'total_spent' => $value, 'other_spent' => $others ? $others : '', ) ); } /** Pull a date-ish value into Y-m-d, or ''. */ public static function ymd( $value ) { if ( $value instanceof DateTimeInterface ) { return $value->format( 'Y-m-d' ); } $value = trim( (string) $value ); if ( '' === $value ) { return ''; } $ts = strtotime( $value ); return $ts ? gmdate( 'Y-m-d', $ts ) : ''; } /** "12–19 Feb 2027" style range. */ public static function range_label( $in, $out = '' ) { $a = $in ? strtotime( $in ) : 0; $b = $out ? strtotime( $out ) : 0; if ( ! $a ) { return ''; } if ( ! $b || $b === $a ) { return wp_date( 'j M Y', $a ); } return gmdate( 'Y-m', $a ) === gmdate( 'Y-m', $b ) ? wp_date( 'j', $a ) . '–' . wp_date( 'j M Y', $b ) : wp_date( 'j M', $a ) . ' – ' . wp_date( 'j M Y', $b ); } }