0; } ); if ( $pages && function_exists( '_prime_post_caches' ) ) { _prime_post_caches( $pages, false, false ); // Both pages in one query. } foreach ( array( 'cart', 'checkout' ) as $page ) { $id = (int) wc_get_page_id( $page ); $path = $id > 0 ? (string) wp_parse_url( (string) get_permalink( $id ), PHP_URL_PATH ) : ''; if ( '' !== $path && '/' !== $path ) { $lines[] = untrailingslashit( $path ) . '*'; } } $lines = array_values( array_unique( $lines ) ); } } return apply_filters( 'nodebook_intent_patterns', array_values( $lines ) ); } /** * Pages people see after they bought, booked or sent something: order received, view order, * thank-you and confirmation pages. Never a buying signal, even when a pattern like /checkout* matches. */ public static function is_after_conversion( $url, $title = '' ) { $path = strtolower( rawurldecode( (string) wp_parse_url( (string) $url, PHP_URL_PATH ) ) ); $qs = strtolower( (string) wp_parse_url( (string) $url, PHP_URL_QUERY ) ); $ends = array_filter( array( (string) get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ), (string) get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ), 'order-received', 'view-order', ) ); foreach ( $ends as $e ) { if ( false !== strpos( $path, '/' . strtolower( trim( $e, '/' ) ) ) ) { return true; } } if ( preg_match( '/(^|[?&])key=wc_order_/', $qs ) ) { return true; } // Whole path segments only, so "/thank-you-card-set/" (a product) or "/success-stories/" still count. $word = 'thank-?you|thanks|gracias|bedankt|dankjewel|danke|merci|obrigado|grazie|confirmation|confirmacion|confirmed|confirmada|confirmado|bevestiging|bevestigd|bestaetigung|bestatigung|success|exito|complete|completed|completada'; $pre = 'order|booking|reservation|reserva|pedido|payment|pago|purchase|compra|form|contact|bestelling|boeking|aanvraag|enquiry|inquiry|request|solicitud|checkout'; foreach ( array_filter( explode( '/', $path ) ) as $seg ) { if ( preg_match( '/^(?:' . $word . ')(?:-page|-pagina)?$|^(?:' . $pre . ')-(?:' . $word . ')$/', $seg ) ) { return true; } } $title = strtolower( wp_strip_all_tags( (string) $title ) ); return '' !== $title && (bool) preg_match( '/order received|order confirmed|booking (?:is )?confirmed|reservation (?:is )?confirmed|thank you for your (?:order|booking|reservation|purchase)|pedido recibido|reserva confirmada|bestelling ontvangen|boeking bevestigd/', $title ); } /** Bought or booked in the last 24 hours: they are a customer right now, not a lead to chase. */ public static function just_converted( $contact_id ) { global $wpdb; return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM %i WHERE contact_id = %d AND type IN ('order','booking') AND created_at >= %s LIMIT 1", Nodebook_Activities::table(), $contact_id, gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS ) ) ); } /** Does a URL match one of the high-intent path patterns ("*" = anything)? */ public static function is_intent( $url ) { if ( self::is_after_conversion( $url ) ) { return false; } $path = (string) wp_parse_url( (string) $url, PHP_URL_PATH ); $path = '/' . ltrim( $path, '/' ); foreach ( self::patterns() as $p ) { $re = '#^' . str_replace( '\*', '.*', preg_quote( $p, '#' ) ) . '$#i'; if ( preg_match( $re, $path ) || preg_match( $re, rtrim( $path, '/' ) ) ) { return true; } } return false; } /* ------------------------------------------------------------------ * * Instant alerts (called by the tracker for identified visitors) * ------------------------------------------------------------------ */ /** * @param int $contact_id Contact. * @param string $url Page URL. * @param string $title Page title. * @param bool $new_visit First page of a visit. * @param bool $intent Page matched a high-intent pattern. * @return bool Whether an alert was sent. */ public static function on_pageview( $contact_id, $url, $title, $new_visit, $intent, $event = '' ) { $c = Nodebook_Contacts::get( $contact_id ); if ( ! $c ) { return false; } $mode = Nodebook_Settings::get( 'alert_mode' ); $watched = (int) $c['watched'] === 1; $reason = ''; if ( $watched && ( $new_visit || $intent ) ) { $reason = $intent ? 'intent' : 'watched'; } elseif ( 'intent' === $mode && $intent ) { $reason = 'intent'; } elseif ( 'any' === $mode && ( $new_visit || $intent ) ) { $reason = $intent ? 'intent' : 'return'; } if ( '' === $reason || 'off' === $mode && ! $watched ) { return false; } if ( self::is_after_conversion( $url, $title ) || self::just_converted( $contact_id ) ) { return false; } if ( $c['last_alert_at'] && time() - strtotime( $c['last_alert_at'] . ' UTC' ) < self::THROTTLE ) { return false; } Nodebook_Contacts::update( $contact_id, array( 'last_alert_at' => current_time( 'mysql', true ) ) ); /** * Fires when a contact triggers an alert. Add-ons can send it to Slack, SMS, etc. * * @param array $c Contact. * @param string $reason intent|watched|return. * @param string $url Page URL. */ do_action( 'nodebook_alert', $c, $reason, $url ); if ( ! apply_filters( 'nodebook_send_alert_email', true, $c, $reason, $url ) ) { return true; } self::send_alert( $c, $reason, $url, $title, $event ); return true; } public static function recipients() { $to = trim( (string) Nodebook_Settings::get( 'alert_email' ) ); return '' !== $to ? $to : (string) get_option( 'admin_email' ); } private static function company_name( array $c ) { $co = $c['company_id'] ? Nodebook_Companies::get( $c['company_id'] ) : null; return $co ? $co['name'] : ''; } private static function send_alert( array $c, $reason, $url, $title, $event = '' ) { if ( Nodebook_Sample::$running || Nodebook_Sample::is_sample( (int) ( $c['id'] ?? 0 ) ) ) { return; } $name = Nodebook_Contacts::display_name( $c ); $company = self::company_name( $c ); // B2B thinks in accounts: lead with the company. $who = $company ? ( 'b2b' === Nodebook_Niche::current() ? sprintf( '%s (%s)', $company, $name ) : sprintf( '%s (%s)', $name, $company ) ) : $name; $page = $title ? wp_strip_all_tags( $title ) : (string) wp_parse_url( $url, PHP_URL_PATH ); $subject = $event /* translators: 1: person, 2: what they did, e.g. "Opened WhatsApp on Villa 26" */ ? sprintf( __( '%1$s: %2$s', 'nodebook' ), $who, wp_strip_all_tags( $event ) ) : ( 'intent' === $reason /* translators: 1: person, 2: page title */ ? sprintf( __( '%1$s is looking at %2$s', 'nodebook' ), $who, $page ) /* translators: %s: person */ : sprintf( __( '%s is back on your site', 'nodebook' ), $who ) ); $profile = admin_url( 'admin.php?page=nodebook&action=view&id=' . $c['id'] ); $rows = array( __( 'Page', 'nodebook' ) => $page . ' — ' . $url, __( 'Status', 'nodebook' ) => Nodebook_Fields::statuses()[ $c['status'] ] ?? $c['status'], __( 'Visits', 'nodebook' ) => number_format_i18n( (int) $c['visits_count'] ), __( 'First touch', 'nodebook' ) => Nodebook_Attribution::label( $c['first_source'], $c['first_medium'] ), ); if ( (int) $c['orders_count'] ) { $rows[ __( 'Orders', 'nodebook' ) ] = sprintf( '%d · %s', $c['orders_count'], Nodebook_Report::money( $c['total_spent'] ) ); } $html = '

' . esc_html( $subject ) . '

'; foreach ( $rows as $k => $v ) { $html .= ''; } $html .= '
' . esc_html( $k ) . '' . esc_html( $v ) . '
'; // One tap from the phone: reply while they are still on the site. $acts = Nodebook_Followup::actions( $c ); if ( $acts ) { $html .= '

'; foreach ( $acts as $act ) { $html .= '' . esc_html( $act['label'] ) . ''; } $html .= '

'; } $html .= '

' . esc_html__( 'Open the timeline', 'nodebook' ) . '

'; $html .= '

' . esc_html__( 'Sent by Nodebook. Change alerts under Nodebook → Settings.', 'nodebook' ) . '

'; wp_mail( Nodebook_Team::recipients_for( $c ), $subject, $html, array( 'Content-Type: text/html; charset=UTF-8' ) ); } /* ------------------------------------------------------------------ * * Daily digest * ------------------------------------------------------------------ */ /** * Build the digest for a window. Returns null when nothing happened. * * @return array|null [ subject, html, counts ] */ public static function build_digest( $since_gmt ) { global $wpdb; $a = Nodebook_Activities::table(); $ct = Nodebook_Contacts::table(); $returning = $wpdb->get_results( $wpdb->prepare( "SELECT c.id, c.first_name, c.last_name, c.email, c.phone, c.status, c.last_intent_at, COUNT(a.id) AS pages FROM %i a INNER JOIN %i c ON c.id = a.contact_id WHERE a.type = 'pageview' AND a.created_at > %s GROUP BY c.id, c.first_name, c.last_name, c.email, c.phone, c.status, c.last_intent_at ORDER BY pages DESC LIMIT 25", $a, $ct, $since_gmt ), ARRAY_A ); $new = $wpdb->get_results( $wpdb->prepare( 'SELECT id, first_name, last_name, email, phone, source, first_source, first_medium FROM %i WHERE created_at > %s ORDER BY created_at DESC LIMIT 25', $ct, $since_gmt ), ARRAY_A ); $orders = $wpdb->get_results( $wpdb->prepare( "SELECT a.title, c.id, c.first_name, c.last_name, c.email, c.phone FROM %i a INNER JOIN %i c ON c.id = a.contact_id WHERE a.type = 'order' AND a.created_at > %s ORDER BY a.created_at DESC LIMIT 25", $a, $ct, $since_gmt ), ARRAY_A ); $new_total = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at > %s', $ct, $since_gmt ) ); $eod = ( new DateTimeImmutable( 'tomorrow', wp_timezone() ) )->setTimezone( new DateTimeZone( 'UTC' ) )->format( 'Y-m-d H:i:s' ); $reminders = $wpdb->get_results( $wpdb->prepare( 'SELECT id, first_name, last_name, email, phone, remind_at, owner_id FROM %i WHERE remind_at IS NOT NULL AND remind_at < %s ORDER BY remind_at ASC LIMIT 25', $ct, $eod ), ARRAY_A ); if ( ! $returning && ! $new && ! $orders && ! $reminders ) { return null; } $link = static function ( $row ) { $row['id'] = (int) $row['id']; return '' . esc_html( Nodebook_Contacts::display_name( $row ) ) . ''; }; $html = '

' . esc_html__( 'Your Nodebook daily digest', 'nodebook' ) . '

'; if ( $reminders ) { $html .= '

' . esc_html__( 'Reminders due today', 'nodebook' ) . '

'; } if ( $returning ) { $html .= '

' . esc_html__( 'Known contacts on your site', 'nodebook' ) . '

'; } if ( $new ) { $html .= '

' . esc_html( sprintf( /* translators: %d: number of contacts */ _n( '%d new contact', '%d new contacts', $new_total, 'nodebook' ), $new_total ) ) . '

'; } if ( $orders ) { $html .= '

' . esc_html__( 'Orders', 'nodebook' ) . '

'; } $html .= '

' . esc_html__( 'Open the journey report', 'nodebook' ) . '

'; $subject = sprintf( /* translators: 1: site name, 2: returning contacts, 3: new contacts */ __( '[%1$s] %2$d known visitors, %3$d new contacts', 'nodebook' ), wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ), count( $returning ), $new_total ); return array( 'subject' => $subject, 'html' => $html, 'counts' => array( 'returning' => count( $returning ), 'new' => $new_total, 'orders' => count( $orders ), ), ); } public static function send_digest() { if ( ! Nodebook_Settings::get( 'digest' ) ) { return false; } $since = get_option( 'nodebook_last_digest' ); $since = $since ? $since : gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS ); $now = current_time( 'mysql', true ); $digest = self::build_digest( $since ); update_option( 'nodebook_last_digest', $now, false ); if ( ! $digest ) { return false; } return wp_mail( self::recipients(), $digest['subject'], $digest['html'], array( 'Content-Type: text/html; charset=UTF-8' ) ); } /* ------------------------------------------------------------------ * * Dashboard widget * ------------------------------------------------------------------ */ public static function dashboard() { if ( current_user_can( Nodebook_Admin::cap() ) ) { wp_add_dashboard_widget( 'nodebook_live', __( 'Nodebook — who’s on your site', 'nodebook' ), array( __CLASS__, 'render_widget' ) ); } } public static function render_widget() { global $wpdb; $since = gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS ); list( $restricted, $me, $unassigned ) = Nodebook_Team::scope_args(); $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE last_seen >= %s AND ( %d = 0 OR owner_id = %d OR ( %d = 1 AND owner_id = 0 ) ) ORDER BY last_seen DESC LIMIT 10', Nodebook_Contacts::table(), $since, $restricted, $me, $unassigned ), ARRAY_A ); $new = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at >= %s AND ( %d = 0 OR owner_id = %d OR ( %d = 1 AND owner_id = 0 ) )', Nodebook_Contacts::table(), $since, $restricted, $me, $unassigned ) ); echo '

' . esc_html( sprintf( /* translators: 1: known visitors, 2: new contacts */ __( 'Last 24 hours: %1$d known visitors · %2$d new contacts', 'nodebook' ), count( $rows ), $new ) ) . '

'; if ( ! $rows ) { echo '

' . esc_html__( 'No known contacts on the site in the last 24 hours.', 'nodebook' ) . '

'; return; } echo '

' . esc_html__( 'All contacts', 'nodebook' ) . ' · ' . esc_html__( 'Journey report', 'nodebook' ) . '

'; } /** Viewed a high-intent page in the last 7 days. */ public static function is_hot( array $c ) { return ! empty( $c['last_intent_at'] ) && strtotime( $c['last_intent_at'] . ' UTC' ) > time() - 7 * DAY_IN_SECONDS; } }