prepare_items(); $f = Nodebook_List_Contacts::filters(); Nodebook_Admin::header( __( 'Contacts', 'nodebook' ), array( __( 'Add contact', 'nodebook' ) => Nodebook_Admin::url( 'nodebook', array( 'action' => 'new' ) ), __( 'Import', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-tools' ), ), array( 'count' => (int) $table->get_pagination_arg( 'total_items' ) ) ); if ( $f['segment'] ) { $seg = Nodebook_Segments::get( $f['segment'] ); if ( $seg ) { echo '

' . esc_html( sprintf( /* translators: 1: segment name, 2: smart or static */ __( 'Showing segment “%1$s” (%2$s).', 'nodebook' ), $seg['name'], 'smart' === $seg['type'] ? __( 'smart', 'nodebook' ) : __( 'static', 'nodebook' ) ) ) . ' ' . esc_html__( 'Edit segment', 'nodebook' ) . '

'; } } if ( $f['journey'] ) { $periods = array( 7 => __( 'last 7 days', 'nodebook' ), 30 => __( 'last 30 days', 'nodebook' ), 90 => __( 'last 90 days', 'nodebook' ), 365 => __( 'last 12 months', 'nodebook' ), 0 => __( 'all time', 'nodebook' ) ); echo '

' . wp_kses( Nodebook_Icons::svg( 'chevron-l', 14 ), Nodebook_Icons::kses() ) . esc_html__( 'Journeys', 'nodebook' ) . '' . esc_html( $f['journey']['label'] . ' · ' . $periods[ $f['journey']['days'] ] ) . '×' . esc_html__( 'Clear filter', 'nodebook' ) . '

'; } if ( ! $f['segment'] && ! $f['status'] && '' === $f['search'] && ! $f['owner'] && '' === $f['email_status'] && ! $f['journey'] && ! Nodebook_Node::on() ) { $waiting = Nodebook_Admin_Today::badge(); if ( $waiting ) { echo '

' . esc_html( /* translators: %s: number */ sprintf( _n( '%s thing to do today', '%s things to do today', $waiting, 'nodebook' ), number_format_i18n( $waiting ) ) ) . ' →

'; } } $unfiltered = ! $f['segment'] && ! $f['status'] && '' === $f['search'] && ! $f['owner'] && '' === $f['email_status'] && ! $f['journey']; if ( $unfiltered && ! $table->get_pagination_arg( 'total_items' ) ) { self::empty_state(); Nodebook_Admin::footer(); return; } Nodebook_Admin_Segments::pinned_bar( $f['segment'] ); $table->views(); echo '
'; if ( $f['journey'] ) { echo ''; } if ( $f['status'] ) { echo ''; } $table->search_box( __( 'Search contacts', 'nodebook' ), 'nb-contact' ); $table->display(); echo '
'; Nodebook_Admin::footer(); } /** No contacts at all yet: three ways to get them. */ private static function empty_state() { $cards = array(); if ( ! Nodebook_Team::restricted() ) { $cards[] = array( 'upload', __( 'Import a CSV', 'nodebook' ), __( 'Upload a spreadsheet of contacts. Columns are matched for you.', 'nodebook' ), Nodebook_Admin::url( 'nodebook-tools' ) . '#nb-import' ); } $cards[] = array( 'user-plus', __( 'Add a contact', 'nodebook' ), __( 'Create one contact by hand.', 'nodebook' ), Nodebook_Admin::url( 'nodebook', array( 'action' => 'new' ) ) ); $cards[] = array( 'file-text', __( 'Use your forms', 'nodebook' ), __( 'Forms on your site already send people here. Submit one to try it.', 'nodebook' ), current_user_can( 'manage_options' ) ? Nodebook_Admin::url( 'nodebook-settings' ) . '#nb-capture' : home_url( '/' ) ); echo '
'; $said = Nodebook_Admin_Node::says( Nodebook_Node::on() && 'calm' === Nodebook_Node::personality() ? __( 'No contacts yet. Import the people you already know, or let your forms and shop fill this list.', 'nodebook' ) : __( 'It’s a bit empty in here 👀 Bring in the people you already know, or let your forms and shop fill this list on their own.', 'nodebook' ), 'hello' ); if ( ! $said ) { echo '

' . esc_html__( 'Add your first contacts', 'nodebook' ) . '

' . esc_html__( 'Import the people you already know, or let your forms and shop fill this list on their own.', 'nodebook' ) . '

'; } echo '
'; foreach ( $cards as $c ) { echo '' . wp_kses( Nodebook_Icons::svg( $c[0], 18 ), Nodebook_Icons::kses() ) . '' . esc_html( $c[1] ) . '' . esc_html( $c[2] ) . ''; } echo '
'; if ( ! Nodebook_Team::restricted() && Nodebook_Import::available() ) { echo '

' . esc_html__( 'Moving from another CRM?', 'nodebook' ) . ' ' . esc_html__( 'Import from FluentCRM, Groundhogg or Jetpack CRM', 'nodebook' ) . '

'; } echo '
'; } /** Getting started: four auto-detected steps, hidden once done or dismissed. */ public static function setup_card() { if ( ! Nodebook_Setup::visible() ) { return; } $steps = Nodebook_Setup::steps(); $done = count( array_filter( wp_list_pluck( $steps, 'done' ) ) ); echo '

' . esc_html__( 'Get Nodebook working for you', 'nodebook' ) . '

'; echo '' . esc_html( /* translators: 1: steps done, 2: total steps */ sprintf( __( '%1$d of %2$d done', 'nodebook' ), $done, count( $steps ) ) ) . '
    '; $next = true; foreach ( $steps as $key => $s ) { $open = ! $s['done'] && $next; if ( $open ) { $next = false; } echo '
  1. ' . esc_html( $s['label'] ) . ''; if ( $s['done'] ) { echo ' ' . esc_html__( '(done)', 'nodebook' ) . ''; } else { echo '

    ' . esc_html( $s['help'] ) . '

    '; if ( ! empty( $s['choices'] ) ) { echo '
    ' . Nodebook_Setup::choices( $s, $open ) . '
    '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- choices() escapes its own output. } else { echo '' . esc_html( $s['cta'] ) . ''; } } echo '
  2. '; } echo '
'; wp_nonce_field( 'nodebook_hide_setup' ); echo '
'; } /** Follow up today: who is showing buying intent right now, with one-tap actions. */ private static function followup_card() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view toggle only. $all = isset( $_GET['fu'] ) && 'all' === sanitize_key( wp_unslash( $_GET['fu'] ) ); $team = Nodebook_Team::is_team(); $owner = $team && ! $all ? get_current_user_id() : 0; $due = Nodebook_Followup::due( 8, $owner ); if ( ! $due && ! ( $team && ! $all && Nodebook_Followup::count_due() ) ) { return; } $total = Nodebook_Followup::count_due( $owner ); echo '

' . esc_html__( 'Follow up today', 'nodebook' ) . ' ' . esc_html( number_format_i18n( $total ) ) . '

'; if ( $team ) { echo ''; } echo '
'; if ( ! $due ) { echo '

' . esc_html__( 'Nothing for you right now. Your teammates have follow-ups waiting under “Everyone”.', 'nodebook' ) . '

'; return; } echo '

' . esc_html__( 'Inquiries nobody answered yet (last 30 days); reminders that came due; known contacts who viewed a high-intent page, tapped call or WhatsApp, or (if you watch them) came back in the last 7 days; and good contacts who have gone quiet. Log what you did, snooze, or mark them done.', 'nodebook' ) . '

'; if ( $total > count( $due ) ) { echo '

' . esc_html( /* translators: %s: number of more contacts */ sprintf( __( 'and %s more, sorted by latest intent', 'nodebook' ), number_format_i18n( $total - count( $due ) ) ) ) . '

'; } echo ''; } /** * One follow-up row (list) or bar (profile). * * @param array $c Contact. * @param bool $with_name Row in a list (true) or bar on the profile (false). * @param array $extra last (latest logged contact moment row), next (next open task row). */ public static function followup_row( array $c, $with_name, array $extra = array() ) { $reason = Nodebook_Followup::reason( $c['id'] ); $topic = Nodebook_Followup::topic( $c['id'] ); $view = Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $c['id'] ) ); echo $with_name ? '
  • ' . Nodebook_Admin::avatar( Nodebook_Contacts::display_name( $c ), 'c' . $c['id'] ) : '
    '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- avatar() escapes. echo '
    '; if ( $with_name ) { $company = $c['company_id'] ? Nodebook_Companies::get( $c['company_id'] ) : null; echo '' . esc_html( Nodebook_Contacts::display_name( $c ) ) . '' . ( isset( $c['score'] ) && null !== $c['score'] ? Nodebook_Insights::pill( $c['score'] ) : '' ) . '' . ( $company ? ' ' . esc_html( $company['name'] ) . '' : '' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pill() escapes. if ( $c['watched'] ) { echo ' ★'; } if ( $c['owner_id'] && Nodebook_Team::is_team() ) { echo ' ' . esc_html( Nodebook_Team::name( $c['owner_id'] ) ) . ''; } } else { echo '' . esc_html__( 'Follow up', 'nodebook' ) . ''; } if ( $reason ) { $quiet = 'quiet' === $reason['type']; echo '' . esc_html( $reason['title'] ) . ( $reason['created_at'] ? ' · ' . ( $quiet ? esc_html__( 'last activity', 'nodebook' ) . ' ' : '' ) . wp_kses( Nodebook_Admin::ago( $reason['created_at'] ), Nodebook_Admin::KSES ) : '' ) . ''; } self::context_lines( $extra ); if ( $with_name ) { $best = Nodebook_Insights::best_time( (int) $c['id'] ); if ( $best ) { echo '' . wp_kses( Nodebook_Icons::svg( 'clock', 12 ), Nodebook_Icons::kses() ) . ' ' . esc_html( $best['text'] ) . ( $best['now'] ? ' · ' . esc_html__( 'good moment now', 'nodebook' ) . '' : '' ) . ''; } } if ( $with_name && '' !== trim( (string) ( $c['pinned'] ?? '' ) ) ) { echo '' . esc_html( wp_trim_words( (string) $c['pinned'], 18, '…' ) ) . ''; } if ( $topic && ( ! $reason || false === strpos( (string) $reason['title'], $topic['title'] ) ) ) { echo '' . esc_html( /* translators: 1: item title, 2: number of views */ sprintf( _n( 'Most interested in %1$s (%2$s view)', 'Most interested in %1$s (%2$s views)', $topic['views'], 'nodebook' ), $topic['title'], number_format_i18n( $topic['views'] ) ) ) . ''; } echo '
    '; self::action_buttons( $c, 'button-small' ); echo '
    '; wp_nonce_field( 'nodebook_followed_up_' . $c['id'] ); echo '
    '; echo '
    '; wp_nonce_field( 'nodebook_snooze_' . $c['id'] ); echo '
    '; echo $with_name ? '
  • ' : ''; } /** "Last contact: Called · No answer, 3 days ago" and "Next: Send the quote · Tomorrow". */ public static function context_lines( array $extra ) { if ( ! empty( $extra['last'] ) ) { $who = ! empty( $extra['last']['user_id'] ) && Nodebook_Team::is_team() ? Nodebook_Team::name( (int) $extra['last']['user_id'] ) : ''; echo '' . esc_html__( 'Last contact:', 'nodebook' ) . ' ' . esc_html( $extra['last']['title'] ) . ( '' !== $who ? ' · ' . esc_html( $who ) : '' ) . ' · ' . wp_kses( Nodebook_Admin::ago( $extra['last']['created_at'] ), Nodebook_Admin::KSES ) . ''; } if ( ! empty( $extra['next'] ) ) { $t = $extra['next']; echo '' . esc_html__( 'Next:', 'nodebook' ) . ' ' . esc_html( $t['title'] ) . ' · ' . esc_html( Nodebook_Tasks::due_label( $t['due_at'] ) ) . ( Nodebook_Team::is_team() && $t['assignee_id'] ? ' · ' . esc_html( Nodebook_Team::name( (int) $t['assignee_id'] ) ) : '' ) . ''; } } /** * Call / WhatsApp / Email buttons, plus a reply-template picker that rewrites the WhatsApp and email text. * * @param array $c Contact. * @param string $size Extra button class. * @param bool $log Add data-nb-log (profile: tapping a button readies the log form). */ public static function action_buttons( array $c, $size = '', $log = false ) { $actions = Nodebook_Followup::actions( $c ); foreach ( $actions as $a ) { $external = 0 === strpos( $a['url'], 'http' ); $compose = array(); if ( 'email' === $a['kind'] ) { // With JavaScript the email button opens Nodebook's own compose sheet; without, the mail app. $topic = Nodebook_Followup::topic( (int) $c['id'] ); $compose = array( 'data-nb-compose' => (string) (int) $c['id'], 'data-to' => (string) $c['email'], 'data-name' => Nodebook_Contacts::display_name( $c ), 'data-first-name' => trim( (string) $c['first_name'] ), 'data-item' => $topic ? $topic['title'] : '', 'data-subject' => $topic ? $topic['title'] : get_bloginfo( 'name' ), 'data-body' => Nodebook_Followup::message( $c ), 'data-warn' => Nodebook_Email::warning( $c ), ); $log = false; } $attrs = ''; foreach ( array_filter( $compose, 'strlen' ) as $k => $v ) { $attrs .= ' ' . sanitize_key( $k ) . '="' . esc_attr( $v ) . '"'; } $icon = array( 'call' => 'phone', 'whatsapp' => 'message', 'email' => 'mail', )[ $a['kind'] ] ?? ''; if ( 'tile' === $size ) { echo '' . wp_kses( Nodebook_Icons::svg( $icon ? $icon : 'external', 22 ), Nodebook_Icons::kses() ) . '' . esc_html( $a['label'] ) . ''; continue; } $round = 'button-small' === $size && $icon; echo '' . ( $round ? wp_kses( Nodebook_Icons::svg( $icon, 17 ), Nodebook_Icons::kses() ) . '' . esc_html( $a['label'] ) . '' : esc_html( $a['label'] ) ) . ''; } $templates = Nodebook_Followup::templates(); $kinds = wp_list_pluck( $actions, 'kind' ); if ( $templates && ( in_array( 'whatsapp', $kinds, true ) || in_array( 'email', $kinds, true ) ) ) { $topic = Nodebook_Followup::topic( (int) $c['id'] ); $uid = 'nb-tpl-' . (int) $c['id'] . '-' . wp_rand( 100, 999 ); echo ''; } } public static function followed_up() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_followed_up_' . $id ); Nodebook_Followup::mark_done( $id, get_current_user_id() ); $back = isset( $_POST['back'] ) && 'view' === sanitize_key( wp_unslash( $_POST['back'] ) ) ? Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ) : Nodebook_Admin::url( 'nodebook-today' ); Nodebook_Admin::redirect( $back, 'followed' ); } public static function snooze() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_snooze_' . $id ); $days = isset( $_POST['days'] ) ? absint( $_POST['days'] ) : 0; $ok = Nodebook_Followup::snooze( $id, $days, get_current_user_id() ); $back = isset( $_POST['back'] ) && 'view' === sanitize_key( wp_unslash( $_POST['back'] ) ) ? Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ) : Nodebook_Admin::url( 'nodebook-today' ); Nodebook_Admin::redirect( $back, $ok ? 'snoozed' : '' ); } /** Pinned facts: what you should know before you call (kept apart from the timeline). */ public static function pin() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_pin_' . $id ); if ( Nodebook_Contacts::get( $id ) ) { $text = isset( $_POST['pinned'] ) ? sanitize_textarea_field( wp_unslash( $_POST['pinned'] ) ) : ''; Nodebook_Contacts::update( $id, array( 'pinned' => $text ) ); } Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ), 'saved' ); } private static function pin_box( array $c ) { $id = (int) $c['id']; $text = trim( (string) ( $c['pinned'] ?? '' ) ); echo '
    '; if ( '' === $text ) { echo '' . esc_html__( '+ Pin a fact', 'nodebook' ) . ''; } else { echo '' . nl2br( esc_html( $text ) ) . ' ' . esc_html__( 'Edit', 'nodebook' ) . ''; } echo '
    '; wp_nonce_field( 'nodebook_pin_' . $id ); echo '

    '; } public static function owner() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_owner_' . $id ); $owner = isset( $_POST['owner'] ) ? absint( $_POST['owner'] ) : 0; if ( 0 === $owner || isset( Nodebook_Team::users()[ $owner ] ) ) { Nodebook_Contacts::update( $id, array( 'owner_id' => $owner ) ); } Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ), 'saved' ); } public static function clear_reminder() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_clear_reminder_' . $id ); Nodebook_Followup::remind( $id, '' ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ), 'saved' ); } /** Log what happened (note, call, WhatsApp, email, meeting) and when to follow up next. */ /** Log what happened (with how it went) and plan the next step in one go. */ private static function log_card( array $c ) { $id = (int) $c['id']; $options = array( 'note' => __( 'Note', 'nodebook' ) ) + Nodebook_Followup::channels(); $team = Nodebook_Team::users(); echo '
    '; wp_nonce_field( 'nodebook_add_note_' . $id ); echo '
    ' . esc_html__( 'What happened', 'nodebook' ) . ''; foreach ( $options as $k => $label ) { echo ''; } echo '
    '; echo ''; echo '
    '; echo ' '; if ( Nodebook_Team::is_team() ) { echo ' '; } echo '
    '; if ( ! empty( $c['remind_at'] ) ) { $ts = strtotime( $c['remind_at'] . ' UTC' ); $due = $ts <= time(); echo '
    '; wp_nonce_field( 'nodebook_clear_reminder_' . $id ); echo ' ' . esc_html( $due /* translators: %s: date */ ? sprintf( __( 'Back on the list since %s', 'nodebook' ), wp_date( get_option( 'date_format' ), $ts ) ) /* translators: %s: date */ : sprintf( __( 'Snoozed until %s', 'nodebook' ), wp_date( get_option( 'date_format' ), $ts ) ) ) . '
    '; } echo '
    '; } /** Open tasks (with Done and an outcome), a quick "add task", and what was done. */ private static function tasks_card( array $c ) { $id = (int) $c['id']; $tasks = Nodebook_Tasks::for_contact( $id ); $team = Nodebook_Team::users(); echo '

    ' . esc_html__( 'Tasks', 'nodebook' ) . ( $tasks['open'] ? ' ' . esc_html( number_format_i18n( count( $tasks['open'] ) ) ) . '' : '' ) . '

    '; if ( $tasks['open'] ) { echo ''; } else { echo '

    ' . esc_html__( 'No open tasks. Plan the next step below or when you log a call.', 'nodebook' ) . '

    '; } echo '
    ' . esc_html__( 'Add a task', 'nodebook' ) . '
    '; wp_nonce_field( 'nodebook_task' ); echo ' '; echo ' '; if ( Nodebook_Team::is_team() ) { echo ' '; } echo '
    '; if ( $tasks['done'] ) { echo '
    ' . esc_html( /* translators: %s: number */ sprintf( _n( '%s done', '%s done', count( $tasks['done'] ), 'nodebook' ), number_format_i18n( count( $tasks['done'] ) ) ) ) . '
    '; } echo '
    '; } /** * One task: title, due, assignee; Done (with outcome), reschedule, delete. On Today the contact name leads. * * @param array $t Task row (with first_name/last_name/email when $with_who). * @param bool $with_who Show whose task it is (Today). */ public static function task_item( array $t, $with_who ) { $id = (int) $t['id']; $done = ! empty( $t['done_at'] ); $team = Nodebook_Team::is_team(); echo '
  • ' . esc_html( $t['title'] ) . ''; $meta = array(); if ( $done ) { /* translators: %s: date */ $meta[] = sprintf( __( 'Done %s', 'nodebook' ), wp_date( 'j M', strtotime( $t['done_at'] . ' UTC' ) ) ) . ( '' !== (string) $t['outcome'] ? ' · ' . $t['outcome'] : '' ); } else { $meta[] = Nodebook_Tasks::due_label( $t['due_at'] ); } if ( $team && $t['assignee_id'] ) { $meta[] = Nodebook_Team::name( (int) $t['assignee_id'] ); } echo '' . esc_html( implode( ' · ', $meta ) ) . '
    '; $form = static function ( $op, $inner, $class = '' ) use ( $id ) { echo '
    '; wp_nonce_field( 'nodebook_task' ); echo $inner; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built from escaped parts by the callers below. echo '
    '; }; if ( $done ) { $form( 'reopen', '' ); } else { $form( 'done', ' ', 'nb-task__done' ); $opts = ''; foreach ( array( 'tomorrow', '3days', 'week', '2weeks' ) as $k ) { $opts .= ''; } $form( 'move', '' ); } $form( 'delete', '' ); echo '
  • '; } /** Task actions: add, done, reopen, move, delete. */ public static function task_action() { Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_task' ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- verified just above. $op = isset( $_POST['op'] ) ? sanitize_key( wp_unslash( $_POST['op'] ) ) : ''; $id = isset( $_POST['task'] ) ? absint( $_POST['task'] ) : 0; $back = wp_get_referer() ? wp_get_referer() : Nodebook_Admin::url( 'nodebook-today' ); // Only tasks and contacts this person may see (people limited to their own contacts). if ( 'add' !== $op && ! self::can_touch_task( $id ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'nodebook' ), 403 ); } switch ( $op ) { case 'add': $cid = isset( $_POST['contact'] ) ? absint( $_POST['contact'] ) : 0; $deal = isset( $_POST['deal'] ) ? Nodebook_Deals::get( absint( $_POST['deal'] ) ) : null; if ( ( $cid && ! Nodebook_Team::can_see( $cid ) ) || ( $deal && ! Nodebook_Deals::can_see( $deal ) ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'nodebook' ), 403 ); } $team = Nodebook_Team::users(); $who = isset( $_POST['assignee'] ) ? absint( $_POST['assignee'] ) : get_current_user_id(); Nodebook_Tasks::add( $cid, isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '', array( 'due_at' => Nodebook_Tasks::due_from( isset( $_POST['due'] ) ? sanitize_key( wp_unslash( $_POST['due'] ) ) : '', isset( $_POST['date'] ) ? sanitize_text_field( wp_unslash( $_POST['date'] ) ) : '' ), 'assignee_id' => isset( $team[ $who ] ) ? $who : get_current_user_id(), 'deal_id' => isset( $_POST['deal'] ) ? absint( $_POST['deal'] ) : 0, ) ); break; case 'done': Nodebook_Tasks::complete( $id, isset( $_POST['outcome'] ) ? sanitize_text_field( wp_unslash( $_POST['outcome'] ) ) : '' ); break; case 'reopen': Nodebook_Tasks::reopen( $id ); break; case 'move': $due = Nodebook_Tasks::due_from( isset( $_POST['due'] ) ? sanitize_key( wp_unslash( $_POST['due'] ) ) : '' ); if ( $due ) { Nodebook_Tasks::update( $id, array( 'due_at' => $due ) ); } break; case 'delete': Nodebook_Tasks::delete( $id ); break; } // phpcs:enable WordPress.Security.NonceVerification.Missing Nodebook_Admin::redirect( remove_query_arg( 'nb_notice', $back ), 'saved' ); } /** May the current person change this task? */ private static function can_touch_task( $id ) { $task = Nodebook_Tasks::get( (int) $id ); if ( ! $task ) { return false; } if ( ! Nodebook_Team::restricted() ) { return true; } return $task['contact_id'] ? Nodebook_Team::can_see( (int) $task['contact_id'] ) : (int) $task['assignee_id'] === get_current_user_id(); } public static function hide_setup() { Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_hide_setup' ); update_user_meta( get_current_user_id(), Nodebook_Setup::HIDE, 1 ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-today' ) ); } /* ------------------------------------------------------------------ * * Profile * ------------------------------------------------------------------ */ private static function view( $id ) { $c = Nodebook_Contacts::get( $id ); if ( ! $c ) { wp_die( esc_html__( 'Contact not found.', 'nodebook' ) ); } // Orders counted but missing from the timeline (customer existed before Nodebook)? Import them once a day. if ( (int) $c['orders_count'] && $c['email'] && Nodebook_Woocommerce::active() && ! get_transient( 'nodebook_hist_' . $id ) ) { set_transient( 'nodebook_hist_' . $id, 1, DAY_IN_SECONDS ); if ( Nodebook_Activities::count_type( $id, 'order' ) < (int) $c['orders_count'] && Nodebook_Woocommerce::history( $id, $c['email'] ) ) { $c = Nodebook_Contacts::get( $id ); } } $company = $c['company_id'] ? Nodebook_Companies::get( $c['company_id'] ) : null; $name = Nodebook_Contacts::display_name( $c ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. $limit = isset( $_GET['tl'] ) ? min( 5000, absint( $_GET['tl'] ) ) : 150; $rows = Nodebook_Activities::for_contact( $id, $limit ); $total = Nodebook_Activities::count_for_contact( $id ); $items = Nodebook_Activities::group( $rows ); Nodebook_Admin::header( Nodebook_Contacts::display_name( $c ), array( __( '← All contacts', 'nodebook' ) => Nodebook_Admin::url( 'nodebook' ), __( 'Edit', 'nodebook' ) => Nodebook_Admin::url( 'nodebook', array( 'action' => 'edit', 'id' => $id ) ), ), array( 'hide_title' => true ) ); $journey = Nodebook_Report::journey( $c ); $status = Nodebook_Fields::statuses()[ $c['status'] ] ?? $c['status']; ?>

      ' . esc_html( $label ) . ''; } self::timeline_item( $item, $id ); } ?>
    count( $rows ) ) : ?>

    __( 'First inquiry', 'nodebook' ), 'order' => __( 'First order', 'nodebook' ), 'booking' => __( 'First booking', 'nodebook' ), 'appointment' => __( 'First appointment', 'nodebook' ), ); $steps = array(); if ( $j['has_source'] ) { $meta = array_filter( array( $j['campaign'] ? sprintf( /* translators: %s: campaign */ __( 'Campaign: %s', 'nodebook' ), $j['campaign'] ) : '', $j['landing'] ? sprintf( /* translators: %s: landing page */ __( 'Landed on %s', 'nodebook' ), $j['landing'] ) : '', $fmt( $j['start'] ), ) ); $steps[] = array( __( 'Came from', 'nodebook' ), $j['source'], implode( ' · ', $meta ) ); } if ( $j['conversion'] ) { $steps[] = array( $label[ $j['conversion']['type'] ] ?? __( 'Converted', 'nodebook' ), $j['conversion']['title'], $fmt( $j['conversion']['created_at'] ) ); if ( $j['has_source'] ) { $days = (float) $j['days']; $steps[] = array( __( 'Took', 'nodebook' ), $days < 1 ? __( 'Same day', 'nodebook' ) /* translators: %s: number of days */ : sprintf( _n( '%s day', '%s days', (int) ceil( $days ), 'nodebook' ), number_format_i18n( $days, $days < 10 && floor( $days ) !== $days ? 1 : 0 ) ), /* translators: %s: number of visits */ sprintf( _n( '%s visit before converting', '%s visits before converting', (int) $j['visits_before'], 'nodebook' ), number_format_i18n( (int) $j['visits_before'] ) ), ); } } if ( $j['first_order'] && ( ! $j['conversion'] || 'order' !== $j['conversion']['type'] ) ) { $steps[] = array( __( 'First order', 'nodebook' ), $j['first_order']['title'], $fmt( $j['first_order']['created_at'] ) ); } $now = array(); if ( (int) $c['orders_count'] ) { /* translators: 1: number of orders, 2: amount */ $now[] = sprintf( _n( '%1$s order · %2$s', '%1$s orders · %2$s', (int) $c['orders_count'], 'nodebook' ), number_format_i18n( (int) $c['orders_count'] ), Nodebook_Report::money( $c['total_spent'] ) ); } if ( (int) $c['visits_count'] ) { /* translators: %s: number of visits */ $now[] = sprintf( _n( '%s visit', '%s visits', (int) $c['visits_count'], 'nodebook' ), number_format_i18n( (int) $c['visits_count'] ) ); } if ( $now && count( $steps ) ) { $steps[] = array( __( 'So far', 'nodebook' ), implode( ' · ', $now ), '' ); } echo '

    ' . esc_html__( 'Journey', 'nodebook' ) . '

    '; if ( ! $steps ) { echo '

    ' . esc_html__( 'The journey fills in as they visit, inquire, book or buy.', 'nodebook' ) . '

    '; return; } echo '
      '; foreach ( $steps as $st ) { echo '
    1. ' . esc_html( $st[0] ) . '' . esc_html( $st[1] ) . '' . ( '' !== $st[2] ? '' . esc_html( $st[2] ) . '' : '' ) . '
    2. '; } echo '
    '; if ( ! $j['has_source'] ) { echo '

    ' . esc_html__( 'Where they first came from is unknown: they arrived before Nodebook was installed or with cookies blocked.', 'nodebook' ) . '

    '; } elseif ( $j['referrer'] ) { echo '

    ' . esc_html__( 'Referring page:', 'nodebook' ) . ' ' . esc_html( $j['referrer'] ) . '

    '; } echo ''; } /** Who this is, how they are doing, and how to reach them — in one glance. */ private static function hero( array $c, $company, $name, $status ) { $id = (int) $c['id']; $sub = array( Nodebook_Props::hidden( 'contact', 'job_title' ) ? '' : $c['job_title'] ); if ( '' !== (string) $c['first_source'] ) { /* translators: %s: where they first came from, e.g. "Google · paid" */ $sub[] = sprintf( __( 'First came via %s', 'nodebook' ), Nodebook_Attribution::label( $c['first_source'], $c['first_medium'] ) ); } $sub = array_filter( $sub ); echo '
    '; echo Nodebook_Admin::avatar( $name, 'c' . $id, 'xl' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- avatar() escapes. echo '

    ' . esc_html( $name ) . ( Nodebook_Alerts::is_hot( $c ) ? ' ' . esc_html__( 'hot', 'nodebook' ) . '' : '' ) . '

    '; if ( $company ) { echo '' . esc_html( $company['name'] ) . ''; } foreach ( $sub as $bit ) { echo '' . esc_html( $bit ) . ''; } echo '

    '; self::pin_box( $c ); echo '
    '; wp_nonce_field( 'nodebook_status_' . $id ); echo '
    '; $team = Nodebook_Team::users(); if ( Nodebook_Team::is_team() ) { echo '
    '; wp_nonce_field( 'nodebook_owner_' . $id ); echo '
    '; } echo '
    '; self::action_buttons( $c, 'tile', true ); Nodebook_Reviews::buttons( $c, 'tile' ); echo '
    '; wp_nonce_field( 'nodebook_watch_' . $id ); echo '
    '; echo '
    '; $stats = array( __( 'Visits', 'nodebook' ) => number_format_i18n( (int) $c['visits_count'] ), __( 'Last seen', 'nodebook' ) => $c['last_seen'] ? wp_kses( Nodebook_Admin::ago( $c['last_seen'] ), Nodebook_Admin::KSES ) : '' . esc_html__( 'not yet', 'nodebook' ) . '', ); if ( Nodebook_Woocommerce::active() || (int) $c['orders_count'] ) { $stats[ __( 'Orders', 'nodebook' ) ] = number_format_i18n( (int) $c['orders_count'] ); $other = Nodebook_Conversions::others_label( $c['other_spent'] ?? '' ); $stats[ __( 'Spent', 'nodebook' ) ] = esc_html( Nodebook_Admin::money( $c['total_spent'] ) ) . ( '' !== $other ? ' + ' . esc_html( $other ) . '' : '' ); } $stats[ __( 'Contact since', 'nodebook' ) ] = esc_html( wp_date( get_option( 'date_format' ), strtotime( $c['created_at'] . ' UTC' ) ) ); echo '
    '; foreach ( $stats as $label => $value ) { echo '
    ' . esc_html( $label ) . '
    ' . wp_kses( $value, Nodebook_Admin::KSES ) . '
    '; } echo '
    '; self::insight_strip( $c ); echo '
    '; } /** Node's read on this person: how likely they are to buy (and why), when to reach them, their buying rhythm. */ private static function insight_strip( array $c ) { $cells = array(); $x = Nodebook_Insights::explain( $c ); if ( null !== $x['score'] ) { $why = $x['reasons'] ? '' : ''; $note = $x['learned'] ? __( 'Compared with the people who bought from you before.', 'nodebook' ) : __( 'Based on how small businesses usually sell. It learns from your own customers as sales come in.', 'nodebook' ); $cells[] = '
    ' . esc_html__( 'Likely to buy', 'nodebook' ) . '' . Nodebook_Insights::pill( $x['score'] ) . ' ' . esc_html( $x['label'] ) . '
    ' . esc_html__( 'Why', 'nodebook' ) . '' . $why . '

    ' . esc_html( $note ) . '

    '; } $best = Nodebook_Insights::best_time( (int) $c['id'] ); if ( $best ) { $cells[] = '
    ' . esc_html__( 'Best time to reach', 'nodebook' ) . '' . esc_html( $best['text'] ) . ( $best['now'] ? ' ' . esc_html__( 'now', 'nodebook' ) . '' : '' ) . '
    '; } $rhythm = Nodebook_Insights::reorder_of( (int) $c['id'] ); if ( $rhythm ) { $cells[] = '
    ' . esc_html__( 'Due to reorder', 'nodebook' ) . '' . esc_html( sprintf( /* translators: 1: "every 6 weeks", 2: "9 weeks" */ __( 'Buys %1$s · last %2$s ago', 'nodebook' ), Nodebook_Insights::every( $rhythm['every'] ), Nodebook_Insights::span( (int) floor( ( time() - strtotime( $rhythm['last'] . ' UTC' ) ) / DAY_IN_SECONDS ) ) ) ) . '
    '; } if ( $cells ) { echo '
    ' . implode( '', $cells ) . '
    '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built from escaped parts above. } } /** Contact details: only what is filled in, address as one line, codes as names. */ private static function details_card( array $c ) { $rows = array(); if ( $c['email'] ) { $rows[ __( 'Email', 'nodebook' ) ] = '' . esc_html( $c['email'] ) . ''; } if ( $c['phone'] ) { $intl = Nodebook_Phone::international( $c['phone'], $c['country'] ); $rows[ __( 'Phone', 'nodebook' ) ] = '' . esc_html( Nodebook_Phone::display( $c['phone'], $c['country'] ) ) . ''; } $f = Nodebook_Fields::contact(); if ( $c['website'] && empty( $f['website']['hidden'] ) ) { $rows[ $f['website']['label'] ] = '' . esc_html( preg_replace( '#^https?://(www\.)?#', '', untrailingslashit( $c['website'] ) ) ) . ''; } $addr = Nodebook_Admin::address_line( $c ); if ( '' !== $addr && empty( $f['address']['hidden'] ) ) { $rows[ $f['address']['label'] ] = esc_html( $addr ); } $vals = Nodebook_Props::values( 'contact', (int) $c['id'] ); foreach ( Nodebook_Props::defs( 'contact' ) as $key => $def ) { if ( isset( $vals[ $key ] ) && '' !== $vals[ $key ] ) { $shown = Nodebook_Props::display( $def, $vals[ $key ] ); $rows[ $def['label'] ] = 'url' === $def['type'] ? '' . esc_html( preg_replace( '#^https?://(www\.)?#', '', $shown ) ) . '' : nl2br( esc_html( $shown ) ); } } if ( $c['description'] && empty( $f['description']['hidden'] ) ) { $rows[ ! empty( $f['description']['default_label'] ) ? $f['description']['label'] : __( 'Notes', 'nodebook' ) ] = nl2br( esc_html( $c['description'] ) ); } if ( $c['source'] && empty( $f['source']['hidden'] ) ) { $rows[ $f['source']['label'] ] = esc_html( $c['source'] ); } if ( $c['wp_user_id'] && get_userdata( $c['wp_user_id'] ) ) { $rows[ __( 'WordPress user', 'nodebook' ) ] = '' . esc_html( get_userdata( $c['wp_user_id'] )->user_login ) . ''; } echo '

    ' . esc_html__( 'Details', 'nodebook' ) . '

    ' . esc_html__( 'Edit', 'nodebook' ) . '
    '; foreach ( $rows as $label => $html ) { echo '
    ' . esc_html( $label ) . '
    ' . wp_kses_post( $html ) . '
    '; } echo '
    '; } /** Email consent: status, proof, and a manual change for when the person asks. */ private static function consent_card( array $c ) { if ( empty( $c['email'] ) ) { return; } $id = (int) $c['id']; $statuses = Nodebook_Subscription::statuses(); $st = (string) ( $c['email_status'] ?? '' ); $pill = '' . esc_html( $statuses[ $st ] ?? $st ) . ''; echo ''; } public static function email_status() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_email_status_' . $id ); $st = isset( $_POST['email_status'] ) ? sanitize_key( wp_unslash( $_POST['email_status'] ) ) : ''; $st = 'none' === $st ? '' : $st; $why = isset( $_POST['why'] ) ? sanitize_text_field( wp_unslash( $_POST['why'] ) ) : ''; Nodebook_Subscription::set( $id, $st, array( 'source' => '' !== $why ? $why : __( 'Changed by hand', 'nodebook' ), 'force' => true, ) ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ), 'saved' ); } /** What they keep looking at: properties, listings, treatments, products. */ private static function interests_card( $id ) { $items = Nodebook_Interests::for_contact( $id, 8 ); $dates = Nodebook_Interests::dates_for_contact( $id ); if ( ! $items && ! $dates ) { return; } echo '

    ' . esc_html( Nodebook_Niche::get( 'interests' ) ) . '

    '; if ( $items ) { echo ''; } if ( $dates ) { echo '

    ' . esc_html__( 'Dates searched:', 'nodebook' ) . ' '; $bits = array(); foreach ( $dates as $d ) { $bits[] = '' . esc_html( Nodebook_Conversions::range_label( $d['in'], $d['out'] ) ) . ( $d['times'] > 1 ? ' ×' . esc_html( number_format_i18n( $d['times'] ) ) . '' : '' ) . ''; } echo wp_kses_post( implode( ' ', $bits ) ) . '

    '; } echo '
    '; } /** "Today", "Yesterday", "Monday", or a date. */ private static function day_label( $gmt ) { $ts = strtotime( $gmt . ' UTC' ); $day = wp_date( 'Y-m-d', $ts ); $today = wp_date( 'Y-m-d' ); if ( $day === $today ) { return __( 'Today', 'nodebook' ); } if ( wp_date( 'Y-m-d', strtotime( '-1 day' ) ) === $day ) { return __( 'Yesterday', 'nodebook' ); } if ( time() - $ts < 6 * DAY_IN_SECONDS ) { return wp_date( 'l', $ts ); } return wp_date( get_option( 'date_format' ), $ts ); } private static function timeline_item( array $item, $contact_id ) { $r = $item['row']; $types = Nodebook_Activities::types(); $d = isset( $r['data'] ) && is_array( $r['data'] ) ? $r['data'] : array(); $time = wp_date( get_option( 'time_format' ), strtotime( $r['created_at'] . ' UTC' ) ); echo '
  • ' . Nodebook_Admin::icon( 'touch' === $r['type'] && ! empty( $d['channel'] ) ? 'touch-' . $d['channel'] : $r['type'] ) . '
    '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- icon() escapes its class. echo '
    ' . esc_html( $r['title'] ) . '' . esc_html( ( $types[ $r['type'] ] ?? $r['type'] ) . ' · ' . $time ) . '
    '; switch ( $r['type'] ) { case 'visit': if ( ! empty( $d['referrer'] ) ) { echo '

    ' . esc_html__( 'Referrer:', 'nodebook' ) . ' ' . esc_html( $d['referrer'] ) . '

    '; } if ( $item['pages'] ) { echo ''; } break; case 'form': if ( ! empty( $d['message'] ) ) { echo '
    ' . nl2br( esc_html( $d['message'] ) ) . '
    '; } if ( ! empty( $d['fields'] ) ) { echo '
    ' . esc_html__( 'All submitted fields', 'nodebook' ) . '
    '; foreach ( (array) $d['fields'] as $k => $v ) { echo '
    ' . esc_html( $k ) . '
    ' . nl2br( esc_html( (string) $v ) ) . '
    '; } echo '
    '; } if ( $r['url'] ) { echo '

    ' . esc_html__( 'Sent from', 'nodebook' ) . ' ' . esc_html( '' !== (string) wp_parse_url( $r['url'], PHP_URL_PATH ) ? (string) wp_parse_url( $r['url'], PHP_URL_PATH ) : $r['url'] ) . '

    '; } break; case 'order': if ( ! empty( $d['items'] ) ) { echo ''; } if ( function_exists( 'wc_get_order' ) && $r['object_id'] ) { $order = wc_get_order( $r['object_id'] ); if ( $order ) { echo '

    ' . esc_html__( 'Open order', 'nodebook' ) . '

    '; } } break; case 'booking': case 'appointment': $bits = array(); if ( ! empty( $d['source'] ) ) { $bits[] = esc_html( $d['source'] ); } if ( isset( $d['value'] ) ) { $bits[] = esc_html( Nodebook_Admin::money( $d['value'], $d['currency'] ?? '' ) ); } if ( ! empty( $d['status'] ) ) { $bits[] = esc_html( $d['status'] ); } if ( $bits ) { echo '

    ' . implode( ' · ', $bits ) . '

    '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each part escaped above. } if ( ! empty( $d['item'] ) && get_post( (int) $d['item'] ) ) { echo '

    ' . esc_html( get_the_title( (int) $d['item'] ) ) . '

    '; } break; case 'click': if ( ! empty( $d['target'] ) ) { echo '

    ' . esc_html( $d['target'] ) . '' . ( $r['url'] ? ' ' . esc_html__( 'on', 'nodebook' ) . ' ' . esc_html( (string) wp_parse_url( $r['url'], PHP_URL_PATH ) ) . '' : '' ) . '

    '; } break; case 'touch': if ( ! empty( $d['text'] ) && wp_trim_words( $d['text'], 10, '…' ) !== $d['text'] ) { echo '

    ' . nl2br( esc_html( $d['text'] ) ) . '

    '; } $author = $r['user_id'] ? get_userdata( $r['user_id'] ) : null; if ( $author ) { echo '

    ' . esc_html( $author->display_name ) . '

    '; } break; case 'auto': if ( ! empty( $d['subject'] ) ) { echo '

    ' . esc_html( $d['subject'] ) . '

    '; } if ( ! empty( $d['text'] ) ) { echo '

    ' . nl2br( esc_html( wp_trim_words( $d['text'], 60, '…' ) ) ) . '

    '; } break; case 'email_out': if ( ! empty( $d['source'] ) ) { /* translators: %s: plugin name (WooCommerce, Amelia…) */ echo '

    ' . esc_html( sprintf( __( 'Sent by %s', 'nodebook' ), $d['source'] ) ) . '

    '; } break; case 'subscription': if ( ! empty( $d['text'] ) ) { /* translators: %s: the wording the person agreed to */ echo '
    ' . esc_html( sprintf( __( 'Agreed to: “%s”', 'nodebook' ), $d['text'] ) ) . '
    '; } $author = $r['user_id'] ? get_userdata( $r['user_id'] ) : null; if ( $author ) { /* translators: %s: user name */ echo '

    ' . esc_html( sprintf( __( 'Changed by %s', 'nodebook' ), $author->display_name ) ) . '

    '; } if ( $r['url'] ) { echo '

    ' . esc_html__( 'Sent from', 'nodebook' ) . ' ' . esc_html( '' !== (string) wp_parse_url( $r['url'], PHP_URL_PATH ) ? (string) wp_parse_url( $r['url'], PHP_URL_PATH ) : $r['url'] ) . '

    '; } break; case 'event': if ( ! empty( $d['details'] ) && is_array( $d['details'] ) ) { echo '
    '; foreach ( $d['details'] as $k => $v ) { echo '
    ' . esc_html( (string) $k ) . '
    ' . esc_html( (string) $v ) . '
    '; } echo '
    '; } break; case 'note': echo '

    ' . nl2br( esc_html( $d['text'] ?? '' ) ) . '

    '; $author = $r['user_id'] ? get_userdata( $r['user_id'] ) : null; $del = wp_nonce_url( admin_url( 'admin-post.php?action=nodebook_delete_note¬e=' . $r['id'] . '&id=' . $contact_id ), 'nodebook_delete_note_' . $r['id'] ); echo '

    ' . ( $author ? esc_html( $author->display_name ) . ' · ' : '' ) . '' . esc_html__( 'Delete note', 'nodebook' ) . '

    '; break; } do_action( 'nodebook_timeline_item', $r, $item ); echo '
  • '; } /* ------------------------------------------------------------------ * * Add / edit * ------------------------------------------------------------------ */ private static function edit( $id ) { $c = $id ? Nodebook_Contacts::get( $id ) : null; if ( $id && ! $c ) { wp_die( esc_html__( 'Contact not found.', 'nodebook' ) ); } $company = ( $c && $c['company_id'] ) ? Nodebook_Companies::get( $c['company_id'] ) : null; Nodebook_Admin::header( $c ? __( 'Edit contact', 'nodebook' ) : __( 'Add contact', 'nodebook' ), $c ? array( __( '← Back to profile', 'nodebook' ) => Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ) ) : array() ); echo '
    '; echo ''; wp_nonce_field( 'nodebook_save_contact' ); echo '
    '; $journey_fields = array(); foreach ( Nodebook_Fields::contact() as $key => $def ) { if ( ! empty( $def['readonly'] ) || ! empty( $def['hidden'] ) ) { continue; } if ( isset( $def['group'] ) && 'journey' === $def['group'] ) { $journey_fields[ $key ] = $def; continue; } if ( 'company' === $key ) { $value = $company ? $company['name'] : ''; } elseif ( 'status' === $key && ! $c ) { $value = Nodebook_Settings::get( 'default_status' ); } elseif ( 'source' === $key && ! $c ) { $value = __( 'Manual', 'nodebook' ); } else { $value = $c ? $c[ $key ] : ''; } Nodebook_Admin::field( 'contact[' . $key . ']', $key, $def, $value ); } $vals = $c ? Nodebook_Props::values( 'contact', $id ) : array(); foreach ( Nodebook_Props::defs( 'contact' ) as $key => $def ) { Nodebook_Admin::field( 'prop[' . $key . ']', $key, array_merge( $def, array( 'custom' => true ) ), $vals[ $key ] ?? '' ); } echo '
    '; if ( current_user_can( 'manage_options' ) ) { echo ''; } if ( $journey_fields ) { echo '

    ' . esc_html__( 'First touch', 'nodebook' ) . '

    ' . esc_html__( 'Filled in automatically. Edit only to correct or for contacts you add by hand.', 'nodebook' ) . '

    '; foreach ( $journey_fields as $key => $def ) { Nodebook_Admin::field( 'contact[' . $key . ']', $key, $def, $c ? $c[ $key ] : '' ); } echo '
    '; } echo ''; foreach ( Nodebook_Companies::all_names() as $co ) { echo ''; echo '

    ' . esc_html__( 'Other form fields', 'nodebook' ) . '

    ' . esc_html__( 'Anything a form sent that is not one of your fields. Free-form name and value.', 'nodebook' ) . '

    '; $meta = $c ? $c['meta'] : array(); $meta = array_merge( $meta, array( '' => '' ) ); $i = 0; foreach ( $meta as $k => $v ) { echo '
    '; ++$i; } echo '

    '; do_action( 'nodebook_contact_form', $c ); submit_button( $c ? __( 'Save contact', 'nodebook' ) : __( 'Create contact', 'nodebook' ) ); echo '
    '; Nodebook_Admin::footer(); } /* ------------------------------------------------------------------ * * Handlers * ------------------------------------------------------------------ */ public static function save() { Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_save_contact' ); $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; $in = isset( $_POST['contact'] ) && is_array( $_POST['contact'] ) ? map_deep( wp_unslash( $_POST['contact'] ), 'sanitize_textarea_field' ) : array(); $keys = isset( $_POST['meta_key'] ) ? array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['meta_key'] ) ) : array(); $vals = isset( $_POST['meta_value'] ) ? array_map( 'sanitize_textarea_field', (array) wp_unslash( $_POST['meta_value'] ) ) : array(); $meta = array(); foreach ( $keys as $i => $k ) { if ( '' !== trim( $k ) && isset( $vals[ $i ] ) && '' !== trim( $vals[ $i ] ) ) { $meta[ trim( $k ) ] = $vals[ $i ]; } } $data = array(); foreach ( Nodebook_Fields::contact() as $key => $def ) { if ( empty( $def['readonly'] ) && 'company' !== $key && array_key_exists( $key, $in ) ) { $data[ $key ] = $in[ $key ]; } } $data['meta'] = $meta; $props = isset( $_POST['prop'] ) && is_array( $_POST['prop'] ) ? map_deep( wp_unslash( $_POST['prop'] ), 'sanitize_textarea_field' ) : array(); $email = isset( $data['email'] ) ? strtolower( sanitize_email( $data['email'] ) ) : ''; if ( '' !== $email && ! is_email( $email ) ) { Nodebook_Admin::redirect( wp_get_referer(), 'invalid' ); } if ( '' === $email && empty( $data['phone'] ) ) { Nodebook_Admin::redirect( wp_get_referer(), 'invalid' ); } if ( $email ) { $other = Nodebook_Contacts::find_by_email( $email ); if ( $other && (int) $other['id'] !== $id ) { Nodebook_Admin::redirect( wp_get_referer(), 'duplicate' ); } } $company = isset( $in['company'] ) ? trim( $in['company'] ) : ''; $data['company_id'] = '' === $company ? 0 : Nodebook_Companies::resolve( $company, '' ); if ( $id ) { $old = Nodebook_Contacts::get( $id ); Nodebook_Contacts::update( $id, $data ); if ( $old && isset( $data['status'] ) && $old['status'] !== $data['status'] ) { $labels = Nodebook_Fields::statuses(); /* translators: 1: old status, 2: new status */ Nodebook_Activities::add( $id, 'status', sprintf( __( 'Status changed from %1$s to %2$s', 'nodebook' ), $labels[ $old['status'] ] ?? $old['status'], $labels[ $data['status'] ] ?? $data['status'] ) ); } } else { $id = Nodebook_Contacts::insert( $data ); if ( $id ) { Nodebook_Activities::add( $id, 'created', __( 'Contact added manually', 'nodebook' ) ); } } if ( $id ) { Nodebook_Props::set( 'contact', $id, array_intersect_key( $props, Nodebook_Props::defs( 'contact' ) ), 'overwrite' ); } Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ), 'saved' ); } public static function delete() { $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_delete_contact_' . $id ); Nodebook_Contacts::delete( array( $id ) ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook' ), 'deleted' ); } public static function add_note() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_add_note_' . $id ); $note = isset( $_POST['note'] ) ? sanitize_textarea_field( wp_unslash( $_POST['note'] ) ) : ''; $channel = isset( $_POST['channel'] ) ? sanitize_key( wp_unslash( $_POST['channel'] ) ) : 'note'; $remind = isset( $_POST['remind'] ) ? sanitize_key( wp_unslash( $_POST['remind'] ) ) : ''; $date = isset( $_POST['remind_date'] ) ? sanitize_text_field( wp_unslash( $_POST['remind_date'] ) ) : ''; $user = get_current_user_id(); $done = false; if ( Nodebook_Contacts::get( $id ) ) { if ( isset( Nodebook_Followup::channels()[ $channel ] ) ) { $outcome = isset( $_POST['outcome'] ) ? sanitize_key( wp_unslash( $_POST['outcome'] ) ) : ''; $done = Nodebook_Followup::log_touch( $id, $channel, $note, $user, $outcome ); } elseif ( '' !== trim( $note ) ) { $done = (bool) Nodebook_Activities::add( $id, 'note', wp_trim_words( $note, 12, '…' ), array( 'user_id' => $user, 'data' => array( 'text' => $note ), ) ); } // The next step becomes a task (a date alone means "follow up"). $title = isset( $_POST['task_title'] ) ? sanitize_text_field( wp_unslash( $_POST['task_title'] ) ) : ''; $due = Nodebook_Tasks::due_from( $remind, $date ); if ( '' !== trim( $title ) || $due ) { $team = Nodebook_Team::users(); $who = isset( $_POST['assignee'] ) ? absint( $_POST['assignee'] ) : $user; $done = (bool) Nodebook_Tasks::add( $id, '' !== trim( $title ) ? $title : __( 'Follow up', 'nodebook' ), array( 'due_at' => $due, 'assignee_id' => isset( $team[ $who ] ) ? $who : $user, ) ) || $done; } } Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ), $done ? 'note' : '' ); } public static function delete_note() { $note = isset( $_GET['note'] ) ? absint( $_GET['note'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_delete_note_' . $note ); global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table; only notes can be deleted here. $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE id = %d AND contact_id = %d AND type = 'note'", Nodebook_Activities::table(), $note, $id ) ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ), 'deleted' ); } public static function watch() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_watch_' . $id ); $c = Nodebook_Contacts::get( $id ); if ( $c ) { Nodebook_Contacts::update( $id, array( 'watched' => $c['watched'] ? 0 : 1 ) ); } Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ), 'saved' ); } public static function segments() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_contact_segs_' . $id ); $want = isset( $_POST['segments'] ) ? array_map( 'absint', (array) wp_unslash( $_POST['segments'] ) ) : array(); $current = array_map( 'intval', wp_list_pluck( Nodebook_Segments::for_contact( $id ), 'id' ) ); foreach ( Nodebook_Segments::all( 'static' ) as $s ) { $sid = (int) $s['id']; if ( in_array( $sid, $want, true ) && ! in_array( $sid, $current, true ) ) { Nodebook_Segments::add_contacts( $sid, array( $id ) ); } elseif ( ! in_array( $sid, $want, true ) && in_array( $sid, $current, true ) ) { Nodebook_Segments::remove_contacts( $sid, array( $id ) ); } } $name = isset( $_POST['new_list'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['new_list'] ) ) ) : ''; if ( '' !== $name ) { Nodebook_Segments::add_contacts( self::static_list( $name ), array( $id ) ); } Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $id ) ) . '#nb-lists', 'saved' ); } /** A static list by name: the existing one with that name, or a new one. */ private static function static_list( $name ) { foreach ( Nodebook_Segments::all( 'static' ) as $s ) { if ( 0 === strcasecmp( trim( $s['name'] ), $name ) ) { return (int) $s['id']; } } return Nodebook_Segments::save( array( 'name' => $name, 'type' => 'static', ) ); } /** * Lists on the contact page, without a page load: tick a list to add the contact, untick to remove, * or type a new name to make a list and add them to it. */ public static function list_ajax() { check_ajax_referer( 'nodebook_ajax', 'nonce' ); Nodebook_Admin::require_cap(); $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; if ( ! $id || ! Nodebook_Contacts::get( $id ) || ! Nodebook_Team::can_see( $id ) ) { wp_send_json_error( array( 'message' => __( 'Contact not found.', 'nodebook' ) ), 404 ); } $name = isset( $_POST['name'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['name'] ) ) ) : ''; $sid = isset( $_POST['segment'] ) ? absint( $_POST['segment'] ) : 0; if ( '' !== $name ) { $sid = self::static_list( $name ); Nodebook_Segments::add_contacts( $sid, array( $id ) ); $seg = Nodebook_Segments::get( $sid ); wp_send_json_success( array( 'id' => $sid, 'name' => $seg ? $seg['name'] : $name, 'on' => true, ) ); } $seg = $sid ? Nodebook_Segments::get( $sid ) : null; if ( ! $seg || 'static' !== $seg['type'] ) { wp_send_json_error( array( 'message' => __( 'List not found.', 'nodebook' ) ), 404 ); } $on = ! empty( $_POST['on'] ); if ( $on ) { Nodebook_Segments::add_contacts( $sid, array( $id ) ); } else { Nodebook_Segments::remove_contacts( $sid, array( $id ) ); } wp_send_json_success( array( 'id' => $sid, 'name' => $seg['name'], 'on' => $on, ) ); } }