__( 'New contacts', 'nodebook' ), 'buyers' => __( 'Became customers', 'nodebook' ), 'back' => __( 'Came back', 'nodebook' ), 'source' => Nodebook_Attribution::label( $src[0], $src[1] ), ); return array( 'kind' => $kind, 'days' => $days, 'source' => $src[0], 'medium' => $src[1], 'raw' => $raw, 'label' => $labels[ $kind ], ); } public function get_columns() { $cols = array( 'cb' => '', 'name' => __( 'Name', 'nodebook' ), 'email' => __( 'Email', 'nodebook' ), 'company' => __( 'Company', 'nodebook' ), 'status' => __( 'Status', 'nodebook' ), 'score' => __( 'Likely to buy', 'nodebook' ), 'touch' => __( 'First touch', 'nodebook' ), 'visits' => __( 'Visits', 'nodebook' ), 'last_seen' => __( 'Last seen', 'nodebook' ), 'created' => __( 'Created', 'nodebook' ), 'email_status' => __( 'Email consent', 'nodebook' ), ); if ( Nodebook_Team::is_team() ) { $cols['owner'] = __( 'Owner', 'nodebook' ); } if ( $this->woo ) { $cols = array_slice( $cols, 0, 6, true ) + array( 'orders' => __( 'Orders', 'nodebook' ), 'spent' => __( 'Spent', 'nodebook' ), ) + array_slice( $cols, 6, null, true ); } foreach ( Nodebook_Props::defs( 'contact' ) as $key => $def ) { $cols[ $key ] = $def['label']; } $builtin = Nodebook_Fields::contact(); if ( ! empty( $builtin['email']['default_label'] ) ) { $cols['email'] = $builtin['email']['label']; } return apply_filters( 'nodebook_contacts_columns', $cols ); } protected function get_sortable_columns() { return array( 'name' => array( 'first_name', false ), 'email' => array( 'email', false ), 'company' => array( 'company', false ), 'status' => array( 'status', false ), 'score' => array( 'score', true ), 'visits' => array( 'visits_count', true ), 'orders' => array( 'orders_count', true ), 'spent' => array( 'total_spent', true ), 'last_seen' => array( 'last_seen', true ), 'touch' => array( 'first_source', false ), 'created' => array( 'created_at', true ), ); } protected function get_bulk_actions() { $actions = array(); if ( Nodebook_Team::can_delete() ) { $actions['delete'] = __( 'Delete', 'nodebook' ); $actions['merge'] = __( 'Merge selected into the oldest', 'nodebook' ); } foreach ( Nodebook_Fields::statuses() as $slug => $label ) { /* translators: %s: status */ $actions[ 'status-' . $slug ] = sprintf( __( 'Set status: %s', 'nodebook' ), $label ); } foreach ( Nodebook_Segments::all( 'static' ) as $s ) { /* translators: %s: segment name */ $actions[ 'add-' . $s['id'] ] = sprintf( __( 'Add to: %s', 'nodebook' ), $s['name'] ); /* translators: %s: segment name */ $actions[ 'remove-' . $s['id'] ] = sprintf( __( 'Remove from: %s', 'nodebook' ), $s['name'] ); } foreach ( Nodebook_Team::users() as $uid => $uname ) { /* translators: %s: team member */ $actions[ 'owner-' . $uid ] = sprintf( __( 'Assign to: %s', 'nodebook' ), $uname ); } $actions['owner-0'] = __( 'Unassign', 'nodebook' ); $actions['email-subscribed'] = __( 'Email consent: subscribed (they asked for it)', 'nodebook' ); $actions['email-unsubscribed'] = __( 'Email consent: unsubscribed', 'nodebook' ); if ( Nodebook_Props::defs( 'contact' ) ) { $actions['setfield'] = __( 'Update a field…', 'nodebook' ); } if ( Nodebook_Team::can_export() ) { $actions['export'] = __( 'Export selected to CSV', 'nodebook' ); } return $actions; } /** "Update a field…": which field, how, and the value (shown next to the bulk action menu when picked). */ protected function bulk_actions( $which = '' ) { parent::bulk_actions( $which ); $defs = Nodebook_Props::defs( 'contact' ); if ( 'top' !== $which || ! $defs ) { return; } echo ''; } /** * Set, add to, subtract from, fill or clear one of your fields on many contacts. * * @return int Contacts changed. */ public static function bulk_field( array $ids, $key, $mode, $value ) { $def = Nodebook_Props::get( 'contact', $key ); if ( ! $def || ! $ids ) { return 0; } $numeric = in_array( $def['type'], array( 'number', 'money' ), true ); if ( in_array( $mode, array( 'add', 'subtract' ), true ) ) { $delta = Nodebook_Props::to_number( $value ); if ( ! $numeric || null === $delta ) { return 0; } $have = Nodebook_Props::values_for( 'contact', $ids ); $n = 0; foreach ( $ids as $id ) { $cur = Nodebook_Props::to_number( $have[ $id ][ $key ] ?? '0' ); $new = (float) $cur + ( 'add' === $mode ? $delta : -$delta ); $n += Nodebook_Props::set( 'contact', $id, array( $key => (string) round( $new, 4 ) ) ); } return $n; } $n = 0; foreach ( $ids as $id ) { $n += Nodebook_Props::set( 'contact', $id, array( $key => 'clear' === $mode ? '' : (string) $value ), 'fill' === $mode ? 'fill' : 'overwrite' ); } return $n; } public static function process_bulk() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- nonce verified below once an action is present. $action = ''; foreach ( array( 'action', 'action2' ) as $k ) { if ( ! empty( $_REQUEST[ $k ] ) && '-1' !== $_REQUEST[ $k ] ) { $action = sanitize_key( wp_unslash( $_REQUEST[ $k ] ) ); break; } } $ids = isset( $_REQUEST['contact'] ) ? array_map( 'absint', (array) wp_unslash( $_REQUEST['contact'] ) ) : array(); // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( ! $action || ! $ids || in_array( $action, array( 'view', 'edit', 'new' ), true ) ) { return; } check_admin_referer( 'bulk-contacts' ); if ( ! current_user_can( Nodebook_Admin::cap() ) || ( in_array( $action, array( 'delete', 'merge' ), true ) && ! Nodebook_Team::can_delete() ) || ( 'export' === $action && ! Nodebook_Team::can_export() ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'nodebook' ), 403 ); } // Only contacts this user can see (Settings → Team). if ( Nodebook_Team::restricted() ) { $ids = array_map( 'intval', wp_list_pluck( Nodebook_Contacts::query( array( 'ids' => $ids, 'per_page' => 0 ) )['rows'], 'id' ) ); if ( ! $ids ) { Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook' ) ); } } if ( 'export' === $action ) { Nodebook_Csv::export_contacts( array( 'ids' => $ids ) ); } elseif ( 'merge' === $action ) { $keep = Nodebook_Merge::merge_many( $ids ); delete_transient( 'nodebook_dupe_count' ); Nodebook_Admin::redirect( $keep ? Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $keep ) ) : Nodebook_Admin::url( 'nodebook' ), $keep ? 'merged' : 'mergefail' ); } elseif ( 'delete' === $action ) { Nodebook_Contacts::delete( $ids ); } elseif ( 0 === strpos( $action, 'status-' ) ) { $status = substr( $action, 7 ); if ( isset( Nodebook_Fields::statuses()[ $status ] ) ) { foreach ( $ids as $id ) { Nodebook_Contacts::update( $id, array( 'status' => $status ) ); } } } elseif ( 'setfield' === $action ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- verified above (bulk-contacts). self::bulk_field( $ids, isset( $_REQUEST['nb_bf_field'] ) ? sanitize_key( wp_unslash( $_REQUEST['nb_bf_field'] ) ) : '', isset( $_REQUEST['nb_bf_mode'] ) ? sanitize_key( wp_unslash( $_REQUEST['nb_bf_mode'] ) ) : 'set', isset( $_REQUEST['nb_bf_value'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['nb_bf_value'] ) ) : '' ); // phpcs:enable WordPress.Security.NonceVerification.Recommended } elseif ( in_array( $action, array( 'email-subscribed', 'email-unsubscribed' ), true ) ) { foreach ( $ids as $id ) { Nodebook_Subscription::set( $id, substr( $action, 6 ), array( 'source' => __( 'Changed by hand (bulk)', 'nodebook' ), 'force' => true, ) ); } } elseif ( preg_match( '/^owner-(\d+)$/', $action, $m ) ) { $owner = (int) $m[1]; if ( 0 === $owner || isset( Nodebook_Team::users()[ $owner ] ) ) { foreach ( $ids as $id ) { Nodebook_Contacts::update( $id, array( 'owner_id' => $owner ) ); } } } elseif ( preg_match( '/^(add|remove)-(\d+)$/', $action, $m ) ) { if ( 'add' === $m[1] ) { Nodebook_Segments::add_contacts( (int) $m[2], $ids ); } else { Nodebook_Segments::remove_contacts( (int) $m[2], $ids ); } } $back = remove_query_arg( array( 'action', 'action2', 'contact', '_wpnonce', '_wp_http_referer' ), wp_get_referer() ? wp_get_referer() : Nodebook_Admin::url( 'nodebook' ) ); Nodebook_Admin::redirect( $back, 'bulk' ); } public function prepare_items() { $per_page = $this->get_items_per_page( 'nodebook_per_page', 25 ); // phpcs:disable WordPress.Security.NonceVerification.Recommended -- sorting only. $orderby = isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : 'created_at'; $order = isset( $_GET['order'] ) ? sanitize_key( wp_unslash( $_GET['order'] ) ) : 'desc'; // phpcs:enable WordPress.Security.NonceVerification.Recommended $r = Nodebook_Contacts::query( array_merge( self::filters(), array( 'orderby' => $orderby, 'order' => $order, 'per_page' => $per_page, 'page' => $this->get_pagenum(), ) ) ); $this->items = $r['rows']; // Your own fields, for the columns switched on under Screen Options (one query per page). $screen = isset( $this->screen ) ? $this->screen : ( function_exists( 'get_current_screen' ) ? get_current_screen() : null ); $hidden = $screen ? get_hidden_columns( $screen ) : array(); $shown = array_diff( array_keys( Nodebook_Props::defs( 'contact' ) ), (array) $hidden ); if ( $shown && $this->items ) { $vals = Nodebook_Props::values_for( 'contact', wp_list_pluck( $this->items, 'id' ) ); foreach ( $this->items as &$item ) { $item['_props'] = $vals[ (int) $item['id'] ] ?? array(); } unset( $item ); } $this->_column_headers = array( $this->get_columns(), (array) $hidden, $this->get_sortable_columns(), 'name' ); $this->set_pagination_args( array( 'total_items' => $r['total'], 'per_page' => $per_page, ) ); } protected function get_views() { $f = self::filters(); $base = Nodebook_Admin::url( 'nodebook', $f['segment'] ? array( 'segment' => $f['segment'] ) : array() ); $views = array(); $counts = Nodebook_Contacts::status_counts( array( 'segment' => $f['segment'] ) ); $views['all'] = '' . esc_html__( 'All', 'nodebook' ) . ' (' . esc_html( number_format_i18n( $counts['all'] ) ) . ')'; foreach ( Nodebook_Fields::statuses() as $slug => $label ) { $n = $counts['by'][ $slug ] ?? 0; if ( ! $n ) { continue; } $views[ $slug ] = '' . esc_html( $label ) . ' (' . esc_html( number_format_i18n( $n ) ) . ')'; } return $views; } protected function extra_tablenav( $which ) { if ( 'top' !== $which ) { return; } $f = self::filters(); echo '
'; echo ''; $team = Nodebook_Team::users(); if ( Nodebook_Team::is_team() ) { echo ''; } echo ''; submit_button( __( 'Filter', 'nodebook' ), '', 'filter_action', false ); if ( '' !== $f['search'] || '' !== $f['status'] || $f['segment'] || $f['owner'] || '' !== $f['email_status'] ) { echo ' ' . esc_html__( 'Clear all filters', 'nodebook' ) . ''; } echo '
'; } protected function column_owner( $item ) { $name = $item['owner_id'] ? Nodebook_Team::name( $item['owner_id'] ) : ''; return '' !== $name ? esc_html( $name ) : '—'; } public function no_items() { esc_html_e( 'No contacts yet. They appear here automatically when someone submits a form or places an order, or you can add or import them.', 'nodebook' ); } protected function column_cb( $item ) { return ''; } protected function column_name( $item ) { $view = Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $item['id'] ) ); $delete = wp_nonce_url( admin_url( 'admin-post.php?action=nodebook_delete_contact&id=' . $item['id'] ), 'nodebook_delete_contact_' . $item['id'] ); $actions = array( 'view' => '' . esc_html__( 'View', 'nodebook' ) . '', 'edit' => '' . esc_html__( 'Edit', 'nodebook' ) . '', 'delete' => '' . esc_html__( 'Delete', 'nodebook' ) . '', ); if ( ! Nodebook_Team::can_delete() ) { unset( $actions['delete'] ); } // Quick contact links (no extra queries: the message is generic here). foreach ( Nodebook_Followup::actions( $item, true ) as $a ) { $actions[ 'nb-' . $a['kind'] ] = '' . esc_html( $a['label'] ) . ''; } $name = Nodebook_Contacts::display_name( $item ); $flags = ( $item['watched'] ? ' ★' : '' ) . ( Nodebook_Alerts::is_hot( $item ) ? ' ' . esc_html__( 'hot', 'nodebook' ) . '' : '' ); $sub = array_filter( array( $item['job_title'], ! empty( $item['remind_at'] ) ? '⏰ ' . wp_date( 'j M', strtotime( $item['remind_at'] . ' UTC' ) ) : '' ) ); return '
' . Nodebook_Admin::avatar( $name, 'c' . $item['id'] ) . '
' . esc_html( $name ) . '' . $flags . ( $sub ? '' . esc_html( implode( ' · ', $sub ) ) . '' : '' ) . $this->row_actions( $actions ) . '
'; } protected function column_default( $item, $col ) { if ( Nodebook_Props::is_key( $col ) ) { $def = Nodebook_Props::get( 'contact', $col ); $v = $item['_props'][ $col ] ?? ''; return $def && '' !== $v ? esc_html( Nodebook_Props::display( $def, $v ) ) : '—'; } switch ( $col ) { case 'email': return $item['email'] ? '' . esc_html( $item['email'] ) . '' : '—'; case 'company': return $item['company'] ? '' . esc_html( $item['company'] ) . '' : '—'; case 'status': $label = Nodebook_Fields::statuses()[ $item['status'] ] ?? $item['status']; return '' . esc_html( $label ) . ''; case 'touch': return $item['first_source'] ? esc_html( Nodebook_Attribution::label( $item['first_source'], $item['first_medium'] ) ) . ( $item['first_campaign'] ? '
' . esc_html( $item['first_campaign'] ) . '' : '' ) : '—'; case 'score': return null !== ( $item['score'] ?? null ) ? Nodebook_Insights::pill( $item['score'] ) : '—'; case 'visits': return esc_html( number_format_i18n( (int) $item['visits_count'] ) ); case 'orders': return esc_html( number_format_i18n( (int) $item['orders_count'] ) ); case 'spent': return (float) $item['total_spent'] ? esc_html( Nodebook_Admin::money( $item['total_spent'] ) ) : '—'; case 'last_seen': return wp_kses( Nodebook_Admin::ago( $item['last_seen'] ), Nodebook_Admin::KSES ); case 'created': return wp_kses( Nodebook_Admin::ago( $item['created_at'] ), Nodebook_Admin::KSES ); case 'email_status': $st = (string) ( $item['email_status'] ?? '' ); return '' . esc_html( Nodebook_Subscription::statuses()[ $st ] ?? $st ) . ''; } return esc_html( (string) apply_filters( 'nodebook_contacts_column_' . $col, '', $item ) ); } }