until (unix). const HOOK = 'nodebook_node_morning'; const LAST = 'nodebook_node_last_note'; const PUT_OFF = 3; // Days. const MET = 'nodebook_node_met'; // User meta: when Node first introduced himself. public static function init() { add_action( self::HOOK, array( __CLASS__, 'morning' ) ); add_action( 'update_option_' . Nodebook_Settings::OPTION, array( __CLASS__, 'settings_saved' ), 10, 2 ); if ( ! wp_next_scheduled( self::HOOK ) ) { self::schedule(); } } /** Every day at 07:30 site time. */ /** Every day at the chosen time (site time zone); days that are not picked are skipped when it runs. */ public static function schedule() { $next = new DateTimeImmutable( 'today ' . self::send_time(), wp_timezone() ); if ( $next->getTimestamp() <= time() ) { $next = $next->modify( '+1 day' ); } wp_schedule_event( $next->getTimestamp(), 'daily', self::HOOK ); } /** "07:30": when the morning note goes out. */ public static function send_time() { $t = (string) Nodebook_Settings::get( 'node_email_time' ); return preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', $t ) ? $t : '07:30'; } /** Days of the week the note may go out: 0 = Sunday … 6 = Saturday. */ public static function send_days() { $d = Nodebook_Settings::get( 'node_email_days' ); $d = is_array( $d ) ? array_values( array_intersect( array_map( 'intval', $d ), range( 0, 6 ) ) ) : range( 0, 6 ); return $d ? $d : range( 0, 6 ); } /** Settings saved: move the morning note to its new time. */ public static function settings_saved( $old, $new ) { $a = is_array( $old ) ? (string) ( $old['node_email_time'] ?? '' ) : ''; $b = is_array( $new ) ? (string) ( $new['node_email_time'] ?? '' ) : ''; if ( $a !== $b ) { self::unschedule(); self::schedule(); } } /** When the next note can go out (unix), given the chosen days. */ public static function next_send() { $days = self::send_days(); $at = new DateTimeImmutable( 'today ' . self::send_time(), wp_timezone() ); for ( $i = 0; $i < 8; $i++ ) { $day = $at->modify( '+' . $i . ' day' ); if ( $day->getTimestamp() > time() && in_array( (int) $day->format( 'w' ), $days, true ) ) { return $day->getTimestamp(); } } return 0; } public static function unschedule() { wp_clear_scheduled_hook( self::HOOK ); } /* ------------------------------------------------------------------ * * Personality * ------------------------------------------------------------------ */ /** playful | calm | off. */ public static function personality() { $p = (string) Nodebook_Settings::get( 'node_personality' ); return in_array( $p, array( 'playful', 'calm', 'off' ), true ) ? $p : 'playful'; } public static function on() { return 'off' !== self::personality(); } private static function playful() { return 'playful' === self::personality(); } /** Pick the line for the current personality. */ public static function say( $playful, $calm ) { return self::playful() ? $playful : $calm; } /** The person Node works for: Settings → Node, else the first administrator. */ public static function owner() { $id = (int) Nodebook_Settings::get( 'node_owner' ); if ( $id && user_can( $id, 'manage_options' ) ) { return $id; } $admins = get_users( array( 'role' => 'administrator', 'orderby' => 'ID', 'order' => 'ASC', 'number' => 1, 'fields' => 'ID', ) ); return $admins ? (int) ( is_object( $admins[0] ) ? $admins[0]->ID : $admins[0] ) : 0; } /** "Good morning" by the site's clock. */ public static function greeting( $name = '' ) { $h = (int) wp_date( 'G' ); if ( $h < 12 ) { $part = self::say( __( 'Morning', 'nodebook' ), __( 'Good morning', 'nodebook' ) ); } elseif ( $h < 18 ) { $part = self::say( __( 'Hey', 'nodebook' ), __( 'Good afternoon', 'nodebook' ) ); } else { $part = self::say( __( 'Evening', 'nodebook' ), __( 'Good evening', 'nodebook' ) ); } return '' !== $name ? $part . ', ' . $name . ( self::playful() ? '!' : '.' ) : $part . ( self::playful() ? '!' : '.' ); } /* ------------------------------------------------------------------ * * Focus: the three things that matter most * ------------------------------------------------------------------ */ /** * What deserves attention now, most important first. * * @param int $user_id Whose view (scope, "put off" list). 0 = current user. * @param bool $all Every candidate, not just the top three (for tests and the email). * @return array[] key, kind, score, text, action [label, url], who (contact id) */ public static function focus( $user_id = 0, $all = false ) { $user_id = $user_id ? (int) $user_id : get_current_user_id(); $owner = Nodebook_Team::is_team() ? $user_id : 0; $items = array_merge( self::from_followups( $owner ), self::from_tasks( $owner ? $user_id : 0 ), self::from_deals( $owner ), self::from_insights( $owner, $user_id ), self::from_health( $user_id ), self::from_growth( $user_id ) ); /** * What Node points out, before sorting. Modules add their own items here. * * @param array $items Each: key (stable, for "Not now"), kind, score (0–100), text, action [ label, url ], who (contact ID or 0). * @param int $user_id Whose focus this is. */ $items = (array) apply_filters( 'nodebook_node_items', $items, $user_id ); $seen = self::seen( $user_id ); $items = array_values( array_filter( $items, static function ( $i ) use ( $seen ) { return empty( $seen[ $i['key'] ] ) || $seen[ $i['key'] ] < time(); } ) ); usort( $items, static function ( $a, $b ) { return $b['score'] <=> $a['score']; } ); return $all ? $items : array_slice( $items, 0, self::MAX ); } /** Cached count for the corner bubble (2 minutes per user, like the menu badge). */ public static function count( $user_id = 0 ) { $user_id = $user_id ? (int) $user_id : get_current_user_id(); $key = 'nodebook_node_n_' . $user_id; $n = get_transient( $key ); if ( false === $n ) { $n = count( self::focus( $user_id ) ); set_transient( $key, $n, 2 * MINUTE_IN_SECONDS ); } return (int) $n; } public static function flush( $user_id = 0 ) { delete_transient( 'nodebook_node_n_' . ( $user_id ? (int) $user_id : get_current_user_id() ) ); } /** happy (nothing to do), alert (someone is waiting), calm. */ public static function mood( array $items ) { if ( ! $items ) { return 'happy'; } return $items[0]['score'] >= 90 ? 'alert' : 'calm'; } /** The line above the list. */ public static function headline( array $items, $name = '' ) { $n = count( $items ); if ( ! $n ) { return self::greeting( $name ) . ' ' . self::say( __( 'All caught up. Go grab a coffee ☕', 'nodebook' ), __( 'Nothing needs you right now.', 'nodebook' ) ); } if ( self::playful() ) { /* translators: %d: number of things */ $line = 1 === $n ? __( 'One thing deserves your attention 👇', 'nodebook' ) : sprintf( _n( '%d thing deserves your attention 👇', '%d things deserve your attention 👇', $n, 'nodebook' ), $n ); } else { /* translators: %d: number of things */ $line = sprintf( _n( '%d thing for today.', '%d things for today.', $n, 'nodebook' ), $n ); } return self::greeting( $name ) . ' ' . $line; } private static function first( array $c ) { $f = trim( (string) ( $c['first_name'] ?? '' ) ); if ( '' === $f ) { return Nodebook_Contacts::display_name( $c ); } // "MARIA" and "maria" both become "Maria"; "McKenzie" stays as written. return mb_strtolower( $f, 'UTF-8' ) === $f ? mb_convert_case( $f, MB_CASE_TITLE, 'UTF-8' ) : Nodebook_Fields::name_case( $f ); } private static function profile( $id ) { return Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => (int) $id, ) ); } private static function ago( $gmt ) { return human_time_diff( strtotime( $gmt . ' UTC' ), time() ); } /** Unanswered inquiries, buying signals, reminders and contacts gone quiet. */ private static function from_followups( $owner ) { $asked = array(); $hot = array(); $quiet = array(); $remind = array(); foreach ( Nodebook_Followup::due( 40, $owner ) as $c ) { if ( Nodebook_Followup::unanswered( $c ) ) { $asked[] = $c; continue; } if ( $hot && $remind && $quiet ) { continue; // One of each is enough: skip the queries behind reason(). } $r = Nodebook_Followup::reason( (int) $c['id'] ); $t = $r['type'] ?? ''; if ( 'reminder' === $t ) { $remind[] = array( $c, $r ); } elseif ( 'quiet' === $t ) { $quiet[] = array( $c, $r ); } elseif ( $r ) { $hot[] = array( $c, $r ); } } $out = array(); if ( $asked ) { $c = $asked[0]; $age = self::ago( (string) $c['last_inquiry_at'] ); $more = count( $asked ) - 1; if ( ! $more ) { $text = self::say( /* translators: 1: first name, 2: time ago */ sprintf( __( '%1$s asked something %2$s ago and is still waiting. Shall we reply? ✉️', 'nodebook' ), self::first( $c ), $age ), /* translators: 1: first name, 2: time ago */ sprintf( __( '%1$s is waiting for an answer (asked %2$s ago).', 'nodebook' ), self::first( $c ), $age ) ); } else { $text = self::say( /* translators: 1: first name, 2: number of other people */ sprintf( _n( '%1$s and %2$d other are waiting for an answer. Let’s not keep them hanging ⏳', '%1$s and %2$d others are waiting for an answer. Let’s not keep them hanging ⏳', $more, 'nodebook' ), self::first( $c ), $more ), /* translators: 1: first name, 2: number of other people */ sprintf( _n( '%1$s and %2$d other are waiting for an answer.', '%1$s and %2$d others are waiting for an answer.', $more, 'nodebook' ), self::first( $c ), $more ) ); } $out[] = array( 'key' => 'asked:' . (int) $c['id'] . ':' . strtotime( (string) $c['last_inquiry_at'] . ' UTC' ), // A new question brings it back. 'kind' => 'asked', 'score' => 100 + min( 20, count( $asked ) ), 'text' => $text, 'action' => array( $more ? __( 'See who', 'nodebook' ) : __( 'Reply', 'nodebook' ), $more ? Nodebook_Admin::url( 'nodebook-today' ) . '#nb-today-asked' : self::profile( $c['id'] ) ), 'who' => (int) $c['id'], ); } if ( $hot ) { list( $c, $r ) = $hot[0]; $topic = Nodebook_Followup::topic( (int) $c['id'] ); $what = $topic ? $topic['title'] : ''; $out[] = array( 'key' => 'hot:' . (int) $c['id'] . ':' . substr( (string) $c['last_intent_at'], 0, 10 ), 'kind' => 'hot', 'score' => 80, 'text' => '' !== $what ? self::say( /* translators: 1: first name, 2: what they looked at */ sprintf( __( '%1$s is looking at %2$s again 👀 A good moment to get in touch.', 'nodebook' ), self::first( $c ), $what ), /* translators: 1: first name, 2: what they looked at */ sprintf( __( '%1$s is looking at %2$s again. A good moment to get in touch.', 'nodebook' ), self::first( $c ), $what ) ) : self::say( /* translators: %s: first name */ sprintf( __( '%s is back on your site and showing interest 👀', 'nodebook' ), self::first( $c ) ), /* translators: %s: first name */ sprintf( __( '%s is back on your site and showing interest.', 'nodebook' ), self::first( $c ) ) ), 'action' => array( __( 'Open', 'nodebook' ), self::profile( $c['id'] ) ), 'who' => (int) $c['id'], ); } if ( $remind ) { list( $c, $r ) = $remind[0]; $out[] = array( 'key' => 'remind:' . (int) $c['id'] . ':' . substr( (string) $c['remind_at'], 0, 10 ), 'kind' => 'remind', 'score' => 75, 'text' => self::say( /* translators: 1: first name, 2: reminder */ sprintf( __( 'You asked me to remind you about %1$s: %2$s 🔔', 'nodebook' ), self::first( $c ), $r['title'] ), /* translators: 1: first name, 2: reminder */ sprintf( __( 'Reminder about %1$s: %2$s', 'nodebook' ), self::first( $c ), $r['title'] ) ), 'action' => array( __( 'Open', 'nodebook' ), self::profile( $c['id'] ) ), 'who' => (int) $c['id'], ); } if ( $quiet ) { list( $c, $r ) = $quiet[0]; $out[] = array( 'key' => 'quiet:' . (int) $c['id'], 'kind' => 'quiet', 'score' => 40, 'text' => self::say( /* translators: 1: first name, 2: why (e.g. "Customer, last seen 7 months ago") */ sprintf( __( 'It’s been a while since we heard from %1$s. %2$s. Say hi? 👋', 'nodebook' ), self::first( $c ), rtrim( $r['title'], '.' ) ), /* translators: 1: first name, 2: why */ sprintf( __( '%1$s has gone quiet. %2$s.', 'nodebook' ), self::first( $c ), rtrim( $r['title'], '.' ) ) ), 'action' => array( __( 'Open', 'nodebook' ), self::profile( $c['id'] ) ), 'who' => (int) $c['id'], ); } return $out; } /** Tasks that are overdue or due today. */ private static function from_tasks( $assignee ) { $rows = Nodebook_Tasks::due( $assignee, '', Nodebook_Tasks::end_of_today() ); $late = array_values( array_filter( $rows, array( 'Nodebook_Tasks', 'is_overdue' ) ) ); $today = count( $rows ) - count( $late ); $out = array(); $tasks_u = Nodebook_Admin::url( 'nodebook-today' ) . '#nb-today-tasks'; if ( $late ) { $t = $late[0]; $out[] = array( 'key' => 'late:' . (int) $t['id'], 'kind' => 'late', 'score' => 90, 'text' => 1 === count( $late ) ? self::say( /* translators: %s: task */ sprintf( __( '“%s” is overdue. Five minutes and it’s off your list ✅', 'nodebook' ), $t['title'] ), /* translators: %s: task */ sprintf( __( 'Overdue: “%s”.', 'nodebook' ), $t['title'] ) ) : self::say( /* translators: %d: number */ sprintf( __( '%d tasks are overdue. Let’s clear them out ✅', 'nodebook' ), count( $late ) ), /* translators: %d: number */ sprintf( __( '%d tasks are overdue.', 'nodebook' ), count( $late ) ) ), 'action' => array( __( 'Tasks', 'nodebook' ), $tasks_u ), 'who' => (int) $t['contact_id'], ); } elseif ( $today ) { $out[] = array( 'key' => 'today:' . wp_date( 'Y-m-d' ), 'kind' => 'tasks', 'score' => 60, 'text' => self::say( /* translators: %d: number */ sprintf( _n( '%d task on today’s list. You’ve got this 💪', '%d tasks on today’s list. You’ve got this 💪', $today, 'nodebook' ), $today ), /* translators: %d: number */ sprintf( _n( '%d task due today.', '%d tasks due today.', $today, 'nodebook' ), $today ) ), 'action' => array( __( 'Tasks', 'nodebook' ), $tasks_u ), 'who' => 0, ); } return $out; } /** Deals whose contact already ordered, and deals past their close date. */ private static function from_deals( $owner ) { if ( ! Nodebook_Deals::on() ) { return array(); } $out = array(); $ordered = Nodebook_Deals::ordered( $owner ); if ( $ordered ) { $d = $ordered[0]; $out[] = array( 'key' => 'ordered:' . (int) $d['id'], 'kind' => 'ordered', 'score' => 85, 'text' => self::say( /* translators: 1: first name, 2: deal */ sprintf( __( '%1$s just ordered! Is that “%2$s”? Then it’s won 🎉', 'nodebook' ), self::first( $d ), $d['title'] ), /* translators: 1: first name, 2: deal */ sprintf( __( '%1$s ordered since “%2$s” was added. Mark it won?', 'nodebook' ), self::first( $d ), $d['title'] ) ), 'action' => array( __( 'Open deal', 'nodebook' ), Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'view', 'id' => (int) $d['id'], ) ), ), 'who' => (int) $d['contact_id'], ); } $late = array_values( array_filter( Nodebook_Deals::query( array( 'owner' => $owner, 'closing_before' => wp_date( 'Y-m-d', time() - DAY_IN_SECONDS ), 'order' => 'close', 'limit' => 20, ) ), static function ( $d ) { return Nodebook_Deals::is_late( $d ); } ) ); if ( $late ) { $sum = array_sum( wp_list_pluck( $late, 'value' ) ); $out[] = array( 'key' => 'latedeals:' . wp_date( 'Y-W' ), 'kind' => 'deals', 'score' => 70, 'text' => self::say( /* translators: 1: number of deals, 2: money */ sprintf( _n( '%1$d deal (%2$s) is past its close date. Nudge it or close it 🎯', '%1$d deals (%2$s) are past their close date. Nudge them or close them 🎯', count( $late ), 'nodebook' ), count( $late ), Nodebook_Admin::money( $sum ) ), /* translators: 1: number of deals, 2: money */ sprintf( _n( '%1$d deal (%2$s) is past its close date.', '%1$d deals (%2$s) are past their close date.', count( $late ), 'nodebook' ), count( $late ), Nodebook_Admin::money( $sum ) ) ), 'action' => array( __( 'Deals', 'nodebook' ), Nodebook_Admin::url( 'nodebook-today' ) . '#nb-today-deals' ), 'who' => 0, ); } return $out; } /** From the numbers: the best open lead nobody is working on, and a regular customer due to reorder. */ private static function from_insights( $owner, $user_id ) { global $wpdb; $out = array(); $wk = gmdate( 'Y-m-d H:i:s', time() - 7 * DAY_IN_SECONDS ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own tables. $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT c.* FROM %i c WHERE c.score >= 75 AND c.status NOT IN ('customer','lost','archived') AND ( c.followed_up_at IS NULL OR c.followed_up_at < %s ) AND NOT EXISTS ( SELECT 1 FROM %i t WHERE t.contact_id = c.id AND t.done_at IS NULL ) AND ( %d = 0 OR c.owner_id = %d OR c.owner_id = 0 ) ORDER BY c.score DESC, c.last_seen DESC LIMIT 5", Nodebook_Contacts::table(), $wk, Nodebook_Tasks::table(), (int) $owner, (int) $owner ), ARRAY_A ); foreach ( $rows as $c ) { $c = Nodebook_Contacts::hydrate( $c ); if ( ! Nodebook_Team::can_see( $c ) || Nodebook_Followup::unanswered( $c ) ) { continue; } $x = Nodebook_Insights::explain( $c ); $why = array_slice( $x['reasons'], 0, 2 ); $out[] = array( 'key' => 'likely:' . (int) $c['id'] . ':' . wp_date( 'Y-W' ), 'kind' => 'likely', 'score' => 66 + (int) floor( ( (int) $c['score'] - 75 ) / 3 ), 'text' => self::say( /* translators: 1: first name, 2: reasons, like "came from Google · paid, read pricing pages 3 times" */ sprintf( __( '%1$s looks a lot like the people who buy from you (%2$s). Worth a call? 🎯', 'nodebook' ), self::first( $c ), implode( ', ', $why ) ), /* translators: 1: first name, 2: reasons */ sprintf( __( '%1$s is likely to buy: %2$s.', 'nodebook' ), self::first( $c ), implode( ', ', $why ) ) ), 'action' => array( __( 'Open', 'nodebook' ), self::profile( $c['id'] ) ), 'who' => (int) $c['id'], ); break; } $due = Nodebook_Insights::reorder_due( $owner, 5 ); if ( $due ) { $r = reset( $due ); $cid = (int) key( $due ); $more = count( $due ) - 1; $line = self::say( /* translators: 1: first name, 2: "every 6 weeks", 3: "9 weeks" */ sprintf( __( '%1$s usually buys %2$s, and it’s been %3$s. A friendly hello might bring the next order 🛍️', 'nodebook' ), self::first( $r['contact'] ), Nodebook_Insights::every( $r['every'] ), Nodebook_Insights::span( $r['since'] ) ), /* translators: 1: first name, 2: "every 6 weeks", 3: "9 weeks" */ sprintf( __( '%1$s usually buys %2$s; the last order was %3$s ago.', 'nodebook' ), self::first( $r['contact'] ), Nodebook_Insights::every( $r['every'] ), Nodebook_Insights::span( $r['since'] ) ) ); if ( $more ) { /* translators: %d: number of other customers */ $line .= ' ' . sprintf( _n( '(%d more customer is due too.)', '(%d more customers are due too.)', $more, 'nodebook' ), $more ); } $out[] = array( 'key' => 'reorder:' . $cid . ':' . substr( (string) $r['last'], 0, 10 ), 'kind' => 'reorder', 'score' => 72, 'text' => $line, 'action' => array( $more ? __( 'See who', 'nodebook' ) : __( 'Open', 'nodebook' ), $more ? Nodebook_Admin::url( 'nodebook-today' ) . '#nb-today-reorder' : self::profile( $cid ) ), 'who' => $more ? 0 : $cid, ); } return $out; } /** Things that stop Nodebook from working well (administrators only). */ private static function from_health( $user_id ) { if ( ! user_can( $user_id, 'manage_options' ) ) { return array(); } $out = array(); $checks = Nodebook_Setup::checks(); foreach ( array( 'consent', 'endpoint', 'cron', 'mail' ) as $k ) { if ( ! isset( $checks[ $k ] ) || 'ok' === $checks[ $k ][1] ) { continue; } $out[] = array( 'key' => 'health:' . $k, 'kind' => 'health', 'score' => 'bad' === $checks[ $k ][1] ? 95 : 30, 'text' => self::say( /* translators: %s: what needs a look, e.g. "Email alerts" */ sprintf( __( 'Psst, something on my side needs a look: %s 🔧', 'nodebook' ), $checks[ $k ][0] ), /* translators: %s: what needs a look */ sprintf( __( 'Needs a look: %s.', 'nodebook' ), $checks[ $k ][0] ) ), 'action' => array( __( 'Check', 'nodebook' ), Nodebook_Admin::url( 'nodebook-settings' ) . '#nb-status' ), 'who' => 0, ); break; // One at a time. } return $out; } /* ------------------------------------------------------------------ * * Tips on the screen you are on * ------------------------------------------------------------------ */ /** * The one thing to know or do about this contact right now, or null. * * @return array|null text, action [label, url or #anchor], mood */ public static function contact_tip( array $c ) { $id = (int) $c['id']; $first = self::first( $c ); if ( Nodebook_Followup::unanswered( $c ) ) { $r = Nodebook_Followup::reason( $id ); $what = $r && ! empty( $r['title'] ) ? preg_replace( '/^[^:]+:\s*/u', '', (string) $r['title'] ) : ''; $ago = self::ago( (string) $c['last_inquiry_at'] ); return array( 'text' => self::say( /* translators: 1: first name, 2: what they asked about, 3: time ago */ sprintf( __( '%1$s asked about %2$s %3$s ago and is still waiting. Reply, then log it here ✉️', 'nodebook' ), $first, '' !== $what ? ( false !== strpos( $what, '“' ) ? $what : '“' . $what . '”' ) : __( 'something', 'nodebook' ), $ago ), /* translators: 1: first name, 2: time ago */ sprintf( __( '%1$s is waiting for an answer (asked %2$s ago). Reply, then log it here.', 'nodebook' ), $first, $ago ) ), 'action' => array( __( 'Log a reply', 'nodebook' ), '#nb-log' ), 'mood' => 'alert', ); } foreach ( Nodebook_Deals::on() ? Nodebook_Deals::ordered( 0 ) : array() as $d ) { if ( (int) $d['contact_id'] === $id ) { return array( 'text' => self::say( /* translators: %s: deal */ sprintf( __( 'They ordered after “%s” was added. Won? 🎉', 'nodebook' ), $d['title'] ), /* translators: %s: deal */ sprintf( __( 'They ordered after “%s” was added. Mark it won?', 'nodebook' ), $d['title'] ) ), 'action' => array( __( 'Open deal', 'nodebook' ), Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'view', 'id' => (int) $d['id'] ) ) . '#nb-outcome' ), 'mood' => 'happy', ); } } $tasks = Nodebook_Tasks::for_contact( $id, 0 ); foreach ( $tasks['open'] as $t ) { if ( Nodebook_Tasks::is_overdue( $t ) ) { return array( 'text' => self::say( /* translators: %s: task */ sprintf( __( '“%s” is overdue. Done, or move it? ⏰', 'nodebook' ), $t['title'] ), /* translators: %s: task */ sprintf( __( 'Overdue: “%s”.', 'nodebook' ), $t['title'] ) ), 'action' => array( __( 'Tasks', 'nodebook' ), '#nb-tasks' ), 'mood' => 'alert', ); } } if ( ! empty( $c['remind_at'] ) && strtotime( $c['remind_at'] . ' UTC' ) <= time() ) { $r = Nodebook_Followup::reason( $id ); return array( 'text' => self::say( ( $r['title'] ?? __( 'Reminder', 'nodebook' ) ) . ' 🔔', (string) ( $r['title'] ?? __( 'Reminder', 'nodebook' ) ) ), 'action' => array( __( 'Log what you did', 'nodebook' ), '#nb-log' ), 'mood' => 'calm', ); } $hot = ! empty( $c['last_intent_at'] ) ? strtotime( $c['last_intent_at'] . ' UTC' ) : 0; $done = ! empty( $c['followed_up_at'] ) ? strtotime( $c['followed_up_at'] . ' UTC' ) : 0; if ( $hot > time() - 7 * DAY_IN_SECONDS && $hot > $done ) { $topic = Nodebook_Followup::topic( $id ); $ago = self::ago( (string) $c['last_intent_at'] ); return array( 'text' => $topic ? self::say( /* translators: 1: first name, 2: what they looked at, 3: time ago */ sprintf( __( '%1$s looked at %2$s %3$s ago. Strike while it’s warm 🔥', 'nodebook' ), $first, $topic['title'], $ago ), /* translators: 1: first name, 2: what they looked at, 3: time ago */ sprintf( __( '%1$s looked at %2$s %3$s ago. A good moment to get in touch.', 'nodebook' ), $first, $topic['title'], $ago ) ) : self::say( /* translators: 1: first name, 2: time ago */ sprintf( __( '%1$s was on your site %2$s ago, showing interest 🔥', 'nodebook' ), $first, $ago ), /* translators: 1: first name, 2: time ago */ sprintf( __( '%1$s showed interest %2$s ago. A good moment to get in touch.', 'nodebook' ), $first, $ago ) ), 'action' => array( __( 'Log contact', 'nodebook' ), '#nb-log' ), 'mood' => 'alert', ); } if ( ! $tasks['open'] && Nodebook_Deals::on() ) { $open = Nodebook_Deals::query( array( 'contact_id' => $id, 'limit' => 1 ) ); if ( $open ) { return array( 'text' => self::say( /* translators: %s: deal */ sprintf( __( '“%s” has no next step yet. Plan one so it doesn’t go cold 🧊', 'nodebook' ), $open[0]['title'] ), /* translators: %s: deal */ sprintf( __( '“%s” has no next step planned.', 'nodebook' ), $open[0]['title'] ) ), 'action' => array( __( 'Add a task', 'nodebook' ), '#nb-tasks' ), 'mood' => 'calm', ); } } $r = Nodebook_Followup::is_due( $c ) ? Nodebook_Followup::reason( $id ) : null; if ( $r && 'quiet' === ( $r['type'] ?? '' ) ) { return array( 'text' => self::say( /* translators: %s: why, e.g. "Customer, last seen 7 months ago" */ sprintf( __( '%s. Time for a friendly hello? 👋', 'nodebook' ), rtrim( $r['title'], '.' ) ), rtrim( $r['title'], '.' ) . '.' ), 'action' => array( __( 'Log contact', 'nodebook' ), '#nb-log' ), 'mood' => 'calm', ); } $rhythm = Nodebook_Insights::reorder_of( $id ); if ( $rhythm && ( empty( $c['followed_up_at'] ) || $c['followed_up_at'] < $rhythm['last'] ) ) { return array( 'text' => self::say( /* translators: 1: first name, 2: "every 6 weeks", 3: "9 weeks" */ sprintf( __( '%1$s usually buys %2$s and the last order was %3$s ago. A good moment to check in 🛍️', 'nodebook' ), $first, Nodebook_Insights::every( $rhythm['every'] ), Nodebook_Insights::span( (int) floor( ( time() - strtotime( $rhythm['last'] . ' UTC' ) ) / DAY_IN_SECONDS ) ) ), /* translators: 1: first name, 2: "every 6 weeks", 3: "9 weeks" */ sprintf( __( '%1$s usually buys %2$s; the last order was %3$s ago.', 'nodebook' ), $first, Nodebook_Insights::every( $rhythm['every'] ), Nodebook_Insights::span( (int) floor( ( time() - strtotime( $rhythm['last'] . ' UTC' ) ) / DAY_IN_SECONDS ) ) ) ), 'action' => array( __( 'Log contact', 'nodebook' ), '#nb-log' ), 'mood' => 'calm', ); } return null; } /** The one thing to do about this deal now, or null. */ public static function deal_tip( array $d ) { if ( '' !== (string) $d['outcome'] ) { return null; } foreach ( Nodebook_Deals::ordered( 0 ) as $o ) { if ( (int) $o['id'] === (int) $d['id'] ) { return array( 'text' => self::say( __( 'They ordered since this deal was added. Is that the one? Then it’s won 🎉', 'nodebook' ), __( 'The contact ordered since this deal was added. Mark it won?', 'nodebook' ) ), 'action' => array( __( 'Close it', 'nodebook' ), '#nb-outcome' ), 'mood' => 'happy', ); } } if ( Nodebook_Deals::is_late( $d ) ) { $days = max( 1, (int) floor( ( time() - strtotime( $d['expected_close'] . ' 00:00:00 UTC' ) ) / DAY_IN_SECONDS ) ); return array( 'text' => self::say( /* translators: %d: days */ sprintf( _n( 'The close date passed %d day ago. Move it, or close the deal 🎯', 'The close date passed %d days ago. Move it, or close the deal 🎯', $days, 'nodebook' ), $days ), /* translators: %d: days */ sprintf( _n( 'The close date passed %d day ago.', 'The close date passed %d days ago.', $days, 'nodebook' ), $days ) ), 'action' => array( __( 'Close it', 'nodebook' ), '#nb-outcome' ), 'mood' => 'alert', ); } $tasks = Nodebook_Tasks::for_deal( (int) $d['id'] ); if ( ! $tasks['open'] ) { return array( 'text' => self::say( __( 'No next step planned. Add one so this deal doesn’t go cold 🧊', 'nodebook' ), __( 'No next step planned for this deal.', 'nodebook' ) ), 'action' => array( __( 'Add a step', 'nodebook' ), '#nb-tasks' ), 'mood' => 'calm', ); } return null; } /** The most useful growth tip (administrators only), well below anything about people. */ private static function from_growth( $user_id ) { if ( ! user_can( $user_id, 'manage_options' ) || ! Nodebook_Growth::on() ) { return array(); } $todo = Nodebook_Growth::todo(); if ( ! $todo ) { return array(); } $t = $todo[0]; return array( array( 'key' => 'growth:' . $t['key'], 'kind' => 'growth', 'score' => 14 + 2 * (int) $t['weight'], // 16–34: after people waiting, tasks and deals. 'text' => self::say( /* translators: %s: the tip, e.g. "Connect Google Search Console" */ sprintf( __( 'Quick win: %s 💡', 'nodebook' ), $t['title'] ), /* translators: %s: the tip */ sprintf( __( 'Tip: %s.', 'nodebook' ), $t['title'] ) ), 'action' => array( __( 'Show me', 'nodebook' ), Nodebook_Admin::url( 'nodebook-growth' ) . '#g-' . $t['key'] ), 'who' => 0, ), ); } /* ------------------------------------------------------------------ * * "Not now" * ------------------------------------------------------------------ */ public static function seen( $user_id ) { $s = get_user_meta( (int) $user_id, self::SEEN, true ); return is_array( $s ) ? $s : array(); } /** Put a thing off for a few days. */ public static function dismiss( $key, $user_id = 0, $days = self::PUT_OFF ) { $user_id = $user_id ? (int) $user_id : get_current_user_id(); $seen = array_filter( self::seen( $user_id ), static function ( $until ) { return $until > time(); } ); $seen[ mb_substr( sanitize_text_field( (string) $key ), 0, 80 ) ] = time() + (int) $days * DAY_IN_SECONDS; update_user_meta( $user_id, self::SEEN, array_slice( $seen, -200, null, true ) ); self::flush( $user_id ); } public static function dismiss_post() { Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_node' ); $key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; if ( '' !== $key ) { self::dismiss( $key ); } Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-today' ) . '#nb-node' ); } /* ------------------------------------------------------------------ * * Look * ------------------------------------------------------------------ */ /** * Node himself: a blob with eyes and a mouth, in a mood. Plain HTML and CSS; still for people who * prefer less motion. * * @param string $mood happy | calm | alert | hello. * @param int $size Pixels. */ public static function blob( $mood = 'calm', $size = 64 ) { $mood = in_array( $mood, array( 'happy', 'calm', 'alert', 'hello' ), true ) ? $mood : 'calm'; return ''; } /** First name of a WordPress user, for greetings. */ public static function user_first( $user_id ) { $u = get_userdata( (int) $user_id ); if ( ! $u ) { return ''; } $f = trim( (string) get_user_meta( $u->ID, 'first_name', true ) ); return '' !== $f ? $f : (string) $u->display_name; } /* ------------------------------------------------------------------ * * Onboarding: Node walks you through setup * ------------------------------------------------------------------ */ /** What Node says about the current setup step. */ public static function setup_line( $key, $done, $total, $intro = false, $name = '' ) { if ( $intro ) { $hi = '' !== $name /* translators: %s: first name */ ? self::say( sprintf( __( 'Hi %s,', 'nodebook' ), $name ), sprintf( __( 'Hello %s,', 'nodebook' ), $name ) ) : self::say( __( 'Hi,', 'nodebook' ), __( 'Hello,', 'nodebook' ) ); $left = $total - $done; return $hi . ' ' . self::say( /* translators: %d: number of steps left */ sprintf( _n( 'I’m Node 👋 I keep Nodebook running and tell you what deserves your attention. Let’s get you set up: %d quick step.', 'I’m Node 👋 I keep Nodebook running and tell you what deserves your attention. Let’s get you set up: %d quick steps.', $left, 'nodebook' ), $left ), /* translators: %d: number of steps left */ sprintf( _n( 'I’m Node. I keep Nodebook running and point out what needs your attention. Setup takes %d step.', 'I’m Node. I keep Nodebook running and point out what needs your attention. Setup takes %d steps.', $left, 'nodebook' ), $left ) ); } $lines = array( 'capture' => array( __( 'Nice! Now let’s catch your first contact: send a form on your site with your own email 📨', 'nodebook' ), __( 'Next: capture your first contact by sending a form on your site.', 'nodebook' ) ), 'tracking' => array( __( 'Got you! Now browse a few pages in that same browser and watch them show up 👀', 'nodebook' ), __( 'Next: browse a few pages in the same browser to see a return visit.', 'nodebook' ) ), 'import' => array( __( 'Almost there 🙌 Bring in the people you already know and I’ll keep an eye on them too.', 'nodebook' ), __( 'Last step: import the contacts you already have.', 'nodebook' ) ), 'team' => array( __( 'Quick one: is it just you, or do you work with a team? 👥', 'nodebook' ), __( 'First: do you use Nodebook alone or with a team?', 'nodebook' ) ), 'shop' => array( __( 'You have a shop! 🛍️ When should buyers show up here?', 'nodebook' ), __( 'Next: choose when shop customers become contacts.', 'nodebook' ) ), 'business' => array( __( 'Quick one: tell me what you sell, so I talk your language 🏷️', 'nodebook' ), __( 'Next: confirm your business type.', 'nodebook' ) ), ); $l = $lines[ $key ] ?? array( __( 'One more step and we’re ready 🚀', 'nodebook' ), __( 'One more step.', 'nodebook' ) ); return self::say( $l[0], $l[1] ); } /** Node introduces himself during the first day someone sees him. */ public static function introducing( $user_id = 0 ) { $user_id = $user_id ? (int) $user_id : get_current_user_id(); $met = (int) get_user_meta( $user_id, self::MET, true ); if ( ! $met ) { update_user_meta( $user_id, self::MET, time() ); return true; } return $met > time() - DAY_IN_SECONDS; } /* ------------------------------------------------------------------ * * Morning note * ------------------------------------------------------------------ */ /** Build the morning email for the owner, or null when there is nothing to say. */ public static function note( $user_id ) { $items = self::focus( $user_id ); if ( ! $items ) { return null; } $name = self::user_first( $user_id ); $subject = self::say( /* translators: %d: number of things */ sprintf( _n( '☀️ Node: %d thing for today', '☀️ Node: %d things for today', count( $items ), 'nodebook' ), count( $items ) ), /* translators: %d: number of things */ sprintf( _n( 'Today in Nodebook: %d thing', 'Today in Nodebook: %d things', count( $items ), 'nodebook' ), count( $items ) ) ); $rows = ''; foreach ( $items as $i ) { $rows .= '' . esc_html( $i['text'] ) . '
' . esc_html( $i['action'][0] ) . ''; } $img = NODEBOOK_URL . 'assets/img/node.png'; $html = '
' . '
Node' . esc_html( self::headline( $items, $name ) ) . '
' . '' . $rows . '
' . '

' . esc_html( self::say( __( 'That’s it from me. Have a good one! — Node', 'nodebook' ), __( 'Node, Nodebook', 'nodebook' ) ) ) . ' · ' . esc_html__( 'Change or stop these emails', 'nodebook' ) . '

' . '
'; return array( 'subject' => $subject, 'html' => $html, 'items' => $items, ); } /** Daily: send the owner today's focus (once a day, only when there is something to say). */ public static function morning() { if ( ! self::on() || ! Nodebook_Settings::get( 'node_email' ) ) { return false; } if ( ! in_array( (int) wp_date( 'w' ), self::send_days(), true ) ) { return false; } $owner = self::owner(); $user = $owner ? get_userdata( $owner ) : null; if ( ! $user || get_option( self::LAST ) === wp_date( 'Y-m-d' ) ) { return false; } $was = get_current_user_id(); wp_set_current_user( $owner ); $note = self::note( $owner ); wp_set_current_user( $was ); update_option( self::LAST, wp_date( 'Y-m-d' ), false ); if ( ! $note ) { return false; } return wp_mail( $user->user_email, $note['subject'], $note['html'], array( 'Content-Type: text/html; charset=UTF-8' ) ); } }