$owner, 'closing_before' => wp_date( 'Y-m-d', time() + 7 * DAY_IN_SECONDS ), 'order' => 'close', 'limit' => 30, ) ); // Open deals whose contact ordered or booked since: probably won. $ordered = array(); $have = array_map( 'intval', wp_list_pluck( $deals, 'id' ) ); foreach ( Nodebook_Deals::on() ? Nodebook_Deals::ordered( $owner ) : array() as $od ) { $ordered[ (int) $od['id'] ] = true; if ( ! in_array( (int) $od['id'], $have, true ) ) { array_unshift( $deals, $od ); } } $asked = array(); $others = array(); $tasked = array_flip( array_map( 'intval', wp_list_pluck( $now, 'contact_id' ) ) ); foreach ( $follow as $c ) { if ( Nodebook_Followup::unanswered( $c ) ) { $asked[] = $c; } elseif ( ! isset( $tasked[ (int) $c['id'] ] ) ) { // Someone with a task due today shows under Tasks: that is the next step. $others[] = $c; } } $ids = array_merge( wp_list_pluck( $follow, 'id' ), wp_list_pluck( $now, 'contact_id' ), wp_list_pluck( $soon, 'contact_id' ) ); $last = self::last_moments( $ids ); $next = Nodebook_Tasks::next_for( $ids ); Nodebook_Admin::header( __( 'Today', 'nodebook' ), array(), array( 'sub' => wp_date( 'l j F' ) ) ); Nodebook_Mentions::card(); self::widgets( $owner, $now ); if ( ! Nodebook_Sample::present() && current_user_can( 'manage_options' ) && Nodebook_Contacts::count_all() < 3 ) { echo '

' . esc_html__( 'See Nodebook with a business like yours', 'nodebook' ) . '

' . esc_html__( 'Fill it with two dozen sample people, deals, tasks and visits for your kind of business. Nothing is emailed, and one click removes it all again.', 'nodebook' ) . '

'; Nodebook_Sample::button( 'button-primary' ); echo '
'; } if ( Nodebook_Node::on() ) { Nodebook_Admin_Node::card(); } else { Nodebook_Admin_Contact::setup_card(); } echo '
'; if ( $team ) { echo ''; } if ( ! $asked && ! $now && ! $others && ! $deals && ! Nodebook_Node::on() ) { echo '

' . esc_html__( 'All clear', 'nodebook' ) . '

' . esc_html__( 'Nobody is waiting for an answer and no tasks are due. New inquiries, tasks and people showing interest appear here.', 'nodebook' ) . '

'; } if ( $asked ) { self::section_start( 'asked', __( 'Waiting for an answer', 'nodebook' ), count( $asked ), __( 'They asked something. Reply, then log it or press Done.', 'nodebook' ) ); foreach ( $asked as $c ) { Nodebook_Admin_Contact::followup_row( $c, true, array( 'last' => $last[ (int) $c['id'] ] ?? null, 'next' => $next[ (int) $c['id'] ] ?? null, ) ); } echo ''; } if ( $now ) { self::section_start( 'tasks', __( 'Tasks for today', 'nodebook' ), count( $now ), __( 'Overdue first. Add how it went when you tick one off.', 'nodebook' ) ); foreach ( $now as $t ) { self::task_row( $t, $last[ (int) $t['contact_id'] ] ?? null ); } echo ''; } if ( $others ) { self::section_start( 'follow', __( 'Follow up', 'nodebook' ), count( $others ), __( 'Buying signals, people coming back, snoozes that ended and good contacts gone quiet.', 'nodebook' ) ); foreach ( $others as $c ) { Nodebook_Admin_Contact::followup_row( $c, true, array( 'last' => $last[ (int) $c['id'] ] ?? null, 'next' => $next[ (int) $c['id'] ] ?? null, ) ); } echo ''; } $reorder = Nodebook_Insights::reorder_due( $owner, 10 ); if ( $reorder ) { self::section_start( 'reorder', __( 'Due to reorder', 'nodebook' ), count( $reorder ), __( 'Customers who buy at a steady rhythm and are past their usual moment. A short hello often brings the next order.', 'nodebook' ) ); foreach ( $reorder as $cid => $r ) { $c = $r['contact']; $name = Nodebook_Contacts::display_name( $c ); echo '
  • ' . Nodebook_Admin::avatar( $name, 'c' . (int) $cid ) . '
    ' . esc_html( $name ) . '' . esc_html( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- avatar() escapes. sprintf( /* translators: 1: "every 6 weeks", 2: "9 weeks", 3: number of orders */ __( 'Buys %1$s · last order %2$s ago · %3$s orders so far', 'nodebook' ), Nodebook_Insights::every( $r['every'] ), Nodebook_Insights::span( $r['since'] ), number_format_i18n( $r['count'] ) ) ) . '
    '; Nodebook_Admin_Contact::action_buttons( $c, 'button-small' ); echo '
  • '; } echo ''; } if ( $deals ) { self::section_start( 'deals', __( 'Deals to close', 'nodebook' ), count( $deals ), __( 'Past or near their expected close date, or the contact already ordered. Push them forward, move the date, or close them as won or lost.', 'nodebook' ) ); foreach ( $deals as $d ) { $late = Nodebook_Deals::is_late( $d ); echo '
  • ' . wp_kses( Nodebook_Icons::svg( 'chart', 18 ), Nodebook_Icons::kses() ) . '
    ' . esc_html( $d['title'] ) . '' . esc_html( implode( ' · ', array_filter( array( Nodebook_Deals::stage_name( $d ), (float) $d['value'] ? Nodebook_Deals::money( $d ) : '', $d['contact_id'] && ( $d['first_name'] || $d['last_name'] || $d['email'] ) ? Nodebook_Contacts::display_name( $d ) : '', ) ) ) ) . '
    ' . ( isset( $ordered[ (int) $d['id'] ] ) ? '' . esc_html__( 'Ordered: won?', 'nodebook' ) . ' ' : '' ) . ( $d['expected_close'] ? '' . esc_html( $late /* translators: %s: date */ ? sprintf( __( 'was due %s', 'nodebook' ), wp_date( 'j M', strtotime( $d['expected_close'] . ' 12:00:00' ) ) ) /* translators: %s: date */ : sprintf( __( 'closes %s', 'nodebook' ), wp_date( 'j M', strtotime( $d['expected_close'] . ' 12:00:00' ) ) ) ) . '' : '' ) . '
  • '; } echo ''; } if ( $soon ) { echo '

    ' . esc_html__( 'Coming up this week', 'nodebook' ) . ' ' . esc_html( number_format_i18n( count( $soon ) ) ) . '

    '; } echo '
    '; Nodebook_Admin::footer(); } /** * The four tiles at the top: new contacts, pipeline (or returning visitors), tasks due and the next gathering * (or returning visitors). Each opens the screen behind it. * * @param int $owner Whose work (0 = everyone). * @param array $now Tasks due today or earlier. */ private static function widgets( $owner, array $now ) { global $wpdb; $ct = Nodebook_Contacts::table(); $week = Nodebook_Report::since( 7 ); $prev = Nodebook_Report::since( 14 ); // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own tables. if ( $owner ) { $new = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at >= %s AND owner_id = %d', $ct, $week, $owner ) ); $old = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at >= %s AND created_at < %s AND owner_id = %d', $ct, $prev, $week, $owner ) ); } else { $new = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at >= %s', $ct, $week ) ); $old = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at >= %s AND created_at < %s', $ct, $prev, $week ) ); } $tiles = array(); $diff = $new - $old; $tiles[] = array( 'icon' => 'user', 'color' => '#0071E3', 'label' => __( 'New contacts', 'nodebook' ), 'num' => number_format_i18n( $new ), /* translators: %s: signed number, e.g. +6 */ 'foot' => 0 === $diff ? __( 'Same as last week', 'nodebook' ) : sprintf( __( '%s on last week', 'nodebook' ), ( $diff > 0 ? '+' : '−' ) . number_format_i18n( abs( $diff ) ) ), 'tone' => $diff > 0 ? 'up' : '', 'url' => Nodebook_Admin::url( 'nodebook', array( 'orderby' => 'created', 'order' => 'desc' ) ), ); if ( Nodebook_Deals::on() ) { $row = $owner ? $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS n, COALESCE(SUM(value), 0) AS v FROM %i WHERE outcome = '' AND owner_id = %d", Nodebook_Deals::table(), $owner ), ARRAY_A ) : $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS n, COALESCE(SUM(value), 0) AS v FROM %i WHERE outcome = ''", Nodebook_Deals::table() ), ARRAY_A ); $tiles[] = array( 'icon' => 'chart', 'color' => '#34C759', 'label' => __( 'Open pipeline', 'nodebook' ), 'num' => preg_replace( '/[.,]00(?=\D*$)/', '', Nodebook_Admin::money( (float) $row['v'] ) ), /* translators: %s: number of deals */ 'foot' => sprintf( _n( '%s open deal', '%s open deals', (int) $row['n'], 'nodebook' ), number_format_i18n( (int) $row['n'] ) ), 'tone' => '', 'url' => Nodebook_Admin::url( 'nodebook-deals' ), ); } // phpcs:enable $late = 0; $gmt = current_time( 'mysql', true ); foreach ( $now as $t ) { $late += ! empty( $t['due_at'] ) && $t['due_at'] < $gmt ? 1 : 0; } $tiles[] = array( 'icon' => 'check-square', 'color' => '#FF9500', 'label' => __( 'Tasks due', 'nodebook' ), 'num' => number_format_i18n( count( $now ) ), /* translators: %s: number of overdue tasks */ 'foot' => $late ? sprintf( __( '%s overdue', 'nodebook' ), number_format_i18n( $late ) ) : ( $now ? __( 'All on time', 'nodebook' ) : __( 'Nothing due', 'nodebook' ) ), 'tone' => $late ? 'bad' : '', 'url' => '#nb-today-tasks', ); $next = array(); if ( class_exists( 'Nodebook_Gatherings' ) && Nodebook_Gatherings::on() ) { $next = current( Nodebook_Gatherings::all( array( 'when' => 'upcoming' ) ) ); } if ( $next ) { $s = Nodebook_Gatherings::stats( $next ); $cap = (int) $next['capacity']; $tiles[] = array( 'icon' => 'calendar', 'color' => '#FF2D55', 'label' => $next['title'], 'num' => number_format_i18n( $s['taken'] ) . ( $cap ? ' / ' . number_format_i18n( $cap ) . '' : '' ), 'foot' => $next['starts_on'] ? wp_date( 'j M', strtotime( $next['starts_on'] . ' 12:00:00' ) ) : '', 'tone' => '', 'bar' => $cap ? min( 100, (int) round( 100 * $s['taken'] / $cap ) ) : null, 'url' => Nodebook_Gatherings::url( (int) $next['id'] ), ); } else { $tot = Nodebook_Report::totals( $week ); $tiles[] = array( 'icon' => 'eye', 'color' => '#5856D6', 'label' => __( 'Came back', 'nodebook' ), 'num' => number_format_i18n( $tot['returning'] ), 'foot' => __( 'Known people on your site this week', 'nodebook' ), 'tone' => '', 'url' => Nodebook_Admin::url( 'nodebook', array( 'orderby' => 'last_seen', 'order' => 'desc' ) ), ); } echo '
    '; foreach ( $tiles as $t ) { echo '' . wp_kses( Nodebook_Icons::svg( $t['icon'], 13 ), Nodebook_Icons::kses() ) . '' . esc_html( $t['label'] ) . '' . wp_kses( $t['num'], array( 'small' => array() ) ) . ''; if ( isset( $t['bar'] ) && null !== $t['bar'] ) { echo ''; } echo '' . esc_html( $t['foot'] ) . ''; } echo '
    '; } private static function section_start( $id, $title, $count, $hint ) { echo '

    ' . esc_html( $title ) . ' ' . esc_html( number_format_i18n( $count ) ) . '

    ' . esc_html( $hint ) . '