' . esc_html__( 'Back to deals', 'nodebook' ) . '

'; Nodebook_Admin::footer(); return; } self::form( $d ); } elseif ( in_array( $view, array( 'won', 'lost' ), true ) ) { self::closed( $view ); } else { self::board(); } } /** Whose deals: the current user's (plus unassigned) on a team, unless "Everyone" is picked. */ private static function owner() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view toggle only. $all = isset( $_GET['who'] ) && 'all' === sanitize_key( wp_unslash( $_GET['who'] ) ); return ( ! $all || Nodebook_Team::restricted() ) && Nodebook_Team::is_team() ? get_current_user_id() : 0; } /** Deal-field filters from the URL: choice and yes/no fields only. */ private static function filters() { $out = array(); foreach ( Nodebook_Props::defs( 'deal' ) as $k => $def ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view filter only. if ( in_array( $def['type'], array( 'select', 'checkbox' ), true ) && isset( $_GET['f'][ $k ] ) && '' !== $_GET['f'][ $k ] ) { $out[ $k ] = sanitize_text_field( wp_unslash( $_GET['f'][ $k ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } } return $out; } /** Filter dropdowns for the deal fields people sort deals by (choice and yes/no). */ private static function filter_form( $view ) { $defs = array_filter( Nodebook_Props::defs( 'deal' ), static function ( $d ) { return in_array( $d['type'], array( 'select', 'checkbox' ), true ); } ); if ( ! $defs ) { return; } $cur = self::filters(); echo '
'; if ( '' !== $view ) { echo ''; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view toggle only. if ( isset( $_GET['who'] ) && 'all' === sanitize_key( wp_unslash( $_GET['who'] ) ) ) { echo ''; } foreach ( $defs as $k => $def ) { $opts = 'checkbox' === $def['type'] ? array( '1' => __( 'Yes', 'nodebook' ), '0' => __( 'No', 'nodebook' ) ) : array_combine( (array) $def['options'], (array) $def['options'] ); echo ' '; } echo ''; if ( $cur ) { echo ' ' . esc_html__( 'Clear', 'nodebook' ) . ''; } echo '
'; } /** Pipeline tabs, when there is more than one. */ private static function pipeline_switch( $current ) { $all = Nodebook_Deals::pipelines(); if ( count( $all ) < 2 ) { return; } $pipe = Nodebook_Deals::current(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view toggle only. $keep = isset( $_GET['who'] ) && 'all' === sanitize_key( wp_unslash( $_GET['who'] ) ) ? array( 'who' => 'all' ) : array(); echo ''; } private static function bar( $current ) { $team = Nodebook_Team::is_team() && ! Nodebook_Team::restricted(); $owner = self::owner(); $who = $owner ? array() : array( 'who' => 'all' ); echo '
'; self::pipeline_switch( $current ); if ( '' === $current ) { Nodebook_Admin::layout_switch( 'nodebook-deals', Nodebook_Admin::layout( 'deals' ), $team && ! $owner ? array( 'who' => 'all' ) : array() ); } if ( $team ) { $view = array_filter( array( 'view' => $current ) ); echo ''; } self::filter_form( $current ); if ( '' === $current ) { echo '

' . esc_html( 'list' === Nodebook_Admin::layout( 'deals' ) ? __( 'Drag a deal to another stage, or use the menu on the row. Open a deal to mark it won or lost.', 'nodebook' ) : __( 'Drag a deal to another stage, or use the menu on the card. Open a deal to mark it won or lost.', 'nodebook' ) ); if ( current_user_can( 'manage_options' ) ) { /** * Where "Edit stages" goes for the pipeline on screen. * * @param string $url Default: Settings. * @param int $pipeline Pipeline ID. */ $edit = (string) apply_filters( 'nodebook_stages_url', Nodebook_Admin_Pipelines::url( Nodebook_Deals::current() ), Nodebook_Deals::current() ); echo ' ' . esc_html__( 'Edit stages', 'nodebook' ) . ''; } echo '

'; } echo '
'; } /* ------------------------------------------------------------------ * * Board * ------------------------------------------------------------------ */ private static function board() { $owner = self::owner(); $pipe = Nodebook_Deals::current(); $stages = Nodebook_Deals::stages( $pipe ); $totals = Nodebook_Deals::stage_totals( $owner, $pipe ); $team = Nodebook_Team::is_team(); $sum = array( 'count' => 0, 'value' => 0.0, 'other' => array(), ); foreach ( $totals as $t ) { $sum['count'] += $t['count']; $sum['value'] += $t['value']; foreach ( $t['other'] as $cur => $v ) { $sum['other'][ $cur ] = ( $sum['other'][ $cur ] ?? 0.0 ) + $v; } } Nodebook_Admin::header( __( 'Deals', 'nodebook' ), array( __( 'Add deal', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'new', 'pipeline' => $pipe ) ) ) ); self::bar( '' ); list( $won_n, $won_v ) = Nodebook_Deals::won_since( gmdate( 'Y-m-d H:i:s', time() - 30 * DAY_IN_SECONDS ) ); echo '
' . esc_html( number_format_i18n( $sum['count'] ) ) . ' ' . esc_html__( 'open', 'nodebook' ) . '' . esc_html( Nodebook_Deals::total_label( $sum ) ) . ' ' . esc_html__( 'in the pipeline', 'nodebook' ) . '' . esc_html( number_format_i18n( $won_n ) ) . ' ' . esc_html( /* translators: %s: money */ sprintf( __( 'won in 30 days (%s)', 'nodebook' ), Nodebook_Admin::money( $won_v ) ) ) . '
'; $pipes = Nodebook_Deals::pipelines(); if ( ! $sum['count'] && count( $pipes ) > 1 ) { echo '

' . esc_html( sprintf( /* translators: %s: pipeline name */ __( 'Nothing in %s yet. Add a deal here, or move one over from its page.', 'nodebook' ), $pipes[ $pipe ]['name'] ) ) . '

'; } elseif ( ! $sum['count'] && ! $won_n && Nodebook_Node::on() ) { echo '
'; Nodebook_Admin_Node::says( 'calm' === Nodebook_Node::personality() ? __( 'No deals yet. A deal is a possible order, booking or project: add one when someone is seriously interested, and move it along the stages below.', 'nodebook' ) : __( 'No deals yet! When someone gets serious about buying, add a deal and drag it along the stages below. I’ll tell you when it needs a push 🎯', 'nodebook' ), 'hello', array( __( 'Add your first deal', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'new' ) ) ) ); echo '
'; } elseif ( ! $sum['count'] && ! $won_n ) { echo '

' . esc_html__( 'Track what could become a sale', 'nodebook' ) . '

' . esc_html__( 'A deal is a possible order, booking or project: who, how much, and how far along. Add one here or from a contact, move it as it progresses and mark it won or lost. A won deal makes the contact a customer.', 'nodebook' ) . '

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

'; } $filters = self::filters(); $ordered = array_map( 'intval', wp_list_pluck( Nodebook_Deals::ordered( $owner ), 'id' ) ); $cards = array_filter( Nodebook_Props::defs( 'deal' ), static function ( $def ) { return ! empty( $def['card'] ); } ); echo '
'; foreach ( $stages as $key => $label ) { $rows = Nodebook_Deals::query( array( 'stage' => $key, 'owner' => $owner, 'pipeline_id' => $pipe, 'limit' => self::PER_COLUMN, 'fields' => $filters, ) ); $t = $totals[ $key ]; if ( $filters ) { // Filtered: count and total what is shown. $t = array( 'count' => count( $rows ), 'value' => 0.0, 'other' => array(), ); $main = Nodebook_Conversions::currency(); foreach ( $rows as $r ) { $cur = strtoupper( (string) $r['currency'] ); if ( '' === $cur || '' === $main || $cur === $main ) { $t['value'] += (float) $r['value']; } else { $t['other'][ $cur ] = ( $t['other'][ $cur ] ?? 0.0 ) + (float) $r['value']; } } } $vals = $cards && $rows ? Nodebook_Props::values_for( 'deal', wp_list_pluck( $rows, 'id' ) ) : array(); echo '

' . esc_html( $label ) . '

' . esc_html( number_format_i18n( $t['count'] ) ) . '

' . esc_html( $t['count'] ? Nodebook_Deals::total_label( $t ) : '' ) . '

'; if ( $t['count'] > count( $rows ) ) { echo '

' . esc_html( /* translators: %s: number of deals */ sprintf( __( '%s more, oldest last', 'nodebook' ), number_format_i18n( $t['count'] - count( $rows ) ) ) ) . '

'; } echo '
'; } echo '
'; Nodebook_Admin::footer(); } private static function card( array $d, array $stages, $team, array $cards = array(), array $vals = array(), $ordered = false ) { $id = (int) $d['id']; $who = $d['contact_id'] && ( $d['first_name'] || $d['last_name'] || $d['email'] ) ? Nodebook_Contacts::display_name( $d ) : ''; $view = Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'view', 'id' => $id, ) ); echo '
  • '; echo '' . esc_html( $d['title'] ) . ''; $sub = array_filter( array( $who, $d['company'] ? Nodebook_Fields::name_case( $d['company'] ) : '' ) ); if ( $sub ) { echo '' . esc_html( implode( ' · ', array_unique( $sub ) ) ) . ''; } $shown = array(); foreach ( $cards as $k => $def ) { if ( isset( $vals[ $k ] ) && '' !== $vals[ $k ] ) { $shown[] = ( 'checkbox' === $def['type'] ? $def['label'] . ': ' : '' ) . Nodebook_Props::display( $def, $vals[ $k ] ); } } if ( $shown ) { echo '' . esc_html( implode( ' · ', $shown ) ) . ''; } echo '
    '; if ( $ordered ) { echo '' . esc_html__( 'Ordered', 'nodebook' ) . ''; } if ( (float) $d['value'] ) { echo '' . esc_html( Nodebook_Deals::money( $d ) ) . ''; } if ( $d['expected_close'] ) { $late = Nodebook_Deals::is_late( $d ); echo '' . esc_html( wp_date( 'j M', strtotime( $d['expected_close'] . ' 12:00:00' ) ) ) . ''; } if ( $team && $d['owner_id'] ) { echo '' . esc_html( Nodebook_Team::name( (int) $d['owner_id'] ) ) . ''; } /** * Extra pills on a deal card on the board (escaped HTML). * * @param string $html Default empty. * @param array $d Deal. */ echo wp_kses_post( (string) apply_filters( 'nodebook_deal_card_pills', '', $d ) ); echo '' . wp_kses( Nodebook_Admin::ago( $d['stage_at'] ? $d['stage_at'] : $d['created_at'] ), Nodebook_Admin::KSES ) . '
    '; echo '
    '; wp_nonce_field( 'nodebook_deal_' . $id ); echo '
  • '; } /* ------------------------------------------------------------------ * * Won / lost * ------------------------------------------------------------------ */ private static function closed( $outcome ) { $owner = self::owner(); $rows = Nodebook_Deals::query( array( 'outcome' => $outcome, 'owner' => $owner, 'pipeline_id' => count( Nodebook_Deals::pipelines() ) > 1 ? Nodebook_Deals::current() : 0, 'order' => 'closed', 'limit' => 200, 'fields' => self::filters(), ) ); Nodebook_Admin::header( 'won' === $outcome ? __( 'Won deals', 'nodebook' ) : __( 'Lost deals', 'nodebook' ), array( __( 'Add deal', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'new' ) ) ) ); self::bar( $outcome ); if ( 'lost' === $outcome && $rows ) { $reasons = array_count_values( array_filter( array_map( 'strval', wp_list_pluck( $rows, 'lost_reason' ) ) ) ); arsort( $reasons ); if ( $reasons ) { echo '

    ' . esc_html__( 'Why deals were lost', 'nodebook' ) . '

    '; } } if ( 'won' === $outcome && $rows ) { $by = array(); $main = Nodebook_Conversions::currency(); foreach ( $rows as $r ) { $src = '' !== $r['source'] ? $r['source'] : __( 'Unknown', 'nodebook' ); $by[ $src ] = $by[ $src ] ?? array( 0, 0.0 ); ++$by[ $src ][0]; $cur = strtoupper( (string) $r['currency'] ); if ( '' === $cur || '' === $main || $cur === $main ) { $by[ $src ][1] += (float) $r['value']; } } uasort( $by, static function ( $a, $b ) { return $b[1] <=> $a[1] ?: $b[0] <=> $a[0]; } ); echo '

    ' . esc_html__( 'Where won deals came from', 'nodebook' ) . '

    '; } if ( ! $rows ) { echo '

    ' . esc_html( 'won' === $outcome ? __( 'No won deals yet.', 'nodebook' ) : __( 'No lost deals.', 'nodebook' ) ) . '

    '; Nodebook_Admin::footer(); return; } $team = Nodebook_Team::is_team(); echo ''; echo 'lost' === $outcome ? '' : ''; echo ''; echo $team ? '' : ''; echo ''; foreach ( $rows as $d ) { echo ''; echo 'lost' === $outcome ? '' : ''; echo ''; echo $team ? '' : ''; echo ''; } echo '
    ' . esc_html__( 'Deal', 'nodebook' ) . '' . esc_html__( 'Contact', 'nodebook' ) . '' . esc_html__( 'Value', 'nodebook' ) . '' . esc_html( 'won' === $outcome ? __( 'Won', 'nodebook' ) : __( 'Lost', 'nodebook' ) ) . '' . esc_html__( 'Reason', 'nodebook' ) . '' . esc_html__( 'Source', 'nodebook' ) . '' . esc_html__( 'Owner', 'nodebook' ) . '
    ' . esc_html( $d['title'] ) . ''; if ( $d['contact_id'] && ( $d['first_name'] || $d['last_name'] || $d['email'] ) ) { echo '' . esc_html( Nodebook_Contacts::display_name( $d ) ) . ''; } echo '' . esc_html( (float) $d['value'] ? Nodebook_Deals::money( $d ) : '—' ) . '' . esc_html( $d['closed_at'] ? wp_date( get_option( 'date_format' ), strtotime( $d['closed_at'] . ' UTC' ) ) : '' ) . '' . esc_html( $d['lost_reason'] ) . '' . esc_html( $d['source'] ) . '' . esc_html( $d['owner_id'] ? Nodebook_Team::name( (int) $d['owner_id'] ) : '' ) . '
    '; Nodebook_Admin::footer(); } /* ------------------------------------------------------------------ * * One deal: details (also the "new deal" form), stage, won/lost, tasks, history * ------------------------------------------------------------------ */ private static function form( $d ) { $new = null === $d; if ( $new ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- prefill only. $cid = isset( $_GET['contact'] ) ? absint( $_GET['contact'] ) : 0; $co = isset( $_GET['company'] ) ? absint( $_GET['company'] ) : 0; // phpcs:enable WordPress.Security.NonceVerification.Recommended $c = $cid ? Nodebook_Contacts::get( $cid ) : null; // People limited to their own contacts can only start a deal for a contact they may see. if ( $c && ! Nodebook_Team::can_see( $c ) ) { $c = null; } $d = array( 'id' => 0, 'title' => '', 'contact_id' => $c ? (int) $c['id'] : 0, 'company_id' => $c ? (int) $c['company_id'] : $co, 'stage' => (string) array_key_first( Nodebook_Deals::stages( Nodebook_Deals::current() ) ), 'value' => '', 'currency' => Nodebook_Conversions::currency(), 'owner_id' => $c && $c['owner_id'] ? (int) $c['owner_id'] : get_current_user_id(), 'expected_close' => '', 'notes' => '', 'outcome' => '', 'lost_reason' => '', 'source' => $c ? Nodebook_Deals::source_of( $c ) : '', 'pipeline_id' => Nodebook_Deals::current(), ); } $id = (int) $d['id']; $c = $d['contact_id'] ? Nodebook_Contacts::get( $d['contact_id'] ) : null; $company = $d['company_id'] ? Nodebook_Companies::get( $d['company_id'] ) : null; $stages = Nodebook_Deals::stages( (int) $d['pipeline_id'] ); $team = Nodebook_Team::users(); Nodebook_Admin::header( $new ? __( 'Add deal', 'nodebook' ) : $d['title'], array( __( '← All deals', 'nodebook' ) => Nodebook_Admin::url( 'nodebook-deals' ) ) ); if ( ! $new ) { $open = '' === $d['outcome']; $state = $open ? ( $stages[ $d['stage'] ] ?? $d['stage'] ) : ( 'won' === $d['outcome'] ? __( 'Won', 'nodebook' ) : __( 'Lost', 'nodebook' ) ); $keys = array_keys( $stages ); $at = array_search( $d['stage'], $keys, true ); $at = false === $at ? 0 : (int) $at; echo '
    ' . esc_html( $state ) . ''; if ( $open && ! empty( $d['stage_at'] ) ) { $days = max( 0, (int) floor( ( time() - strtotime( $d['stage_at'] . ' UTC' ) ) / DAY_IN_SECONDS ) ); /* translators: %s: number of days */ echo '' . esc_html( 0 === $days ? __( 'in this stage since today', 'nodebook' ) : sprintf( _n( '%s day in this stage', '%s days in this stage', $days, 'nodebook' ), number_format_i18n( $days ) ) ) . ''; } elseif ( ! $open && ! empty( $d['closed_at'] ) ) { echo '' . esc_html( wp_date( get_option( 'date_format' ), strtotime( $d['closed_at'] . ' UTC' ) ) ) . ''; } if ( 'lost' === $d['outcome'] && '' !== $d['lost_reason'] ) { echo '' . esc_html( $d['lost_reason'] ) . ''; } echo '
    '; if ( (float) $d['value'] ) { echo '' . esc_html( Nodebook_Deals::money( $d ) ) . ''; } echo '
    '; if ( count( $keys ) > 1 ) { echo '
      '; foreach ( $keys as $i => $k ) { $cls = 'won' === $d['outcome'] ? 'is-done' : ( 'lost' === $d['outcome'] ? ( $i <= $at ? 'is-lost' : '' ) : ( $i < $at ? 'is-done' : ( $i === $at ? 'is-now' : '' ) ) ); echo '
    1. ' . esc_html( $stages[ $k ] ) . '
    2. '; } echo '
    '; } $bits = array(); if ( $c ) { $cn = Nodebook_Contacts::display_name( $c ); $bits[] = '' . Nodebook_Admin::avatar( $cn, 'c' . $c['id'], 'sm' ) . '' . esc_html( $cn ) . ''; } if ( $company ) { $bits[] = '' . str_replace( 'nb-avatar ', 'nb-avatar nb-avatar--co ', Nodebook_Admin::avatar( $company['name'], 'co' . $company['id'], 'sm' ) ) . '' . esc_html( Nodebook_Fields::name_case( $company['name'] ) ) . ''; } if ( $open && $d['expected_close'] ) { $late = Nodebook_Deals::is_late( $d ); $bits[] = '' . wp_kses( Nodebook_Icons::svg( 'calendar', 13 ), Nodebook_Icons::kses() ) . 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' ) ) ) ) . ''; } if ( $bits ) { echo '
    ' . implode( '', $bits ) . '
    '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each part escaped above. } echo '
    '; Nodebook_Admin_Node::tip( Nodebook_Node::on() ? Nodebook_Node::deal_tip( $d ) : null ); } echo '
    '; echo '
    '; wp_nonce_field( 'nodebook_deal_' . $id ); echo '

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

    '; echo '

    '; echo '
    '; self::contact_picker( 'contact', 'nb-deal-contact', __( 'Contact', 'nodebook' ), $c ); self::company_picker( 'nb-deal-company', $company ); echo '
    '; echo '

    '; echo '

    '; $pipes = Nodebook_Deals::pipelines(); if ( '' === $d['outcome'] && count( $pipes ) > 1 ) { // Several pipelines: one list, grouped by pipeline, so a deal can also move to another pipeline. echo '

    '; } elseif ( '' === $d['outcome'] ) { echo '

    '; } echo '

    '; if ( count( $team ) > 1 ) { echo '

    '; } echo '

    '; foreach ( self::sources() as $src ) { echo '
    '; // The deal's own fields (Fields → Deal fields). $defs = Nodebook_Props::defs( 'deal' ); if ( $defs ) { $vals = $id ? Nodebook_Props::values( 'deal', $id ) : array(); echo '
    '; foreach ( $defs as $key => $def ) { Nodebook_Admin::field( 'prop[' . $key . ']', 'deal-' . $key, array_merge( $def, array( 'custom' => true ) ), $vals[ $key ] ?? '' ); } echo '
    '; } elseif ( current_user_can( 'manage_options' ) ) { echo '

    ' . wp_kses( sprintf( /* translators: %s: link to the Fields screen */ __( 'Need more, like an event date or the number of guests? %s.', 'nodebook' ), '' . esc_html__( 'Add deal fields', 'nodebook' ) . '' ), array( 'a' => array( 'href' => array() ) ) ) . '

    '; } echo '

    '; echo '

    '; if ( ! $new ) { self::history( $d ); } echo '
    '; if ( ! $new ) { echo ''; } } if ( ! $new && Nodebook_Team::can_delete() ) { echo '
    ' . esc_html__( 'More', 'nodebook' ) . '
    '; wp_nonce_field( 'nodebook_deal_' . $id ); echo '

    ' . esc_html__( 'Its history stays on the contact timeline. Tasks stay with the contact.', 'nodebook' ) . '

    '; echo ''; } echo '
    '; Nodebook_Admin::footer(); } private static function outcome_card( array $d ) { $id = (int) $d['id']; $open = static function ( $op ) use ( $id ) { echo '
    '; wp_nonce_field( 'nodebook_deal_' . $id ); }; echo '

    ' . esc_html__( 'Outcome', 'nodebook' ) . '

    '; if ( '' === $d['outcome'] ) { // Orders and bookings since shortly before the deal was added: "won with this one". $since = gmdate( 'Y-m-d H:i:s', strtotime( $d['created_at'] . ' UTC' ) - 7 * DAY_IN_SECONDS ); $purchases = Nodebook_Deals::purchases( (int) $d['contact_id'], $since ); $open( 'won' ); if ( $purchases ) { echo '

    ' . esc_html__( 'Linking counts the money once: it is already in your revenue.', 'nodebook' ) . '

    '; } echo ''; $open( 'lost' ); echo '
    ' . esc_html__( 'Lost…', 'nodebook' ) . ''; foreach ( self::reasons() as $r ) { echo '
    '; $c = $d['contact_id'] ? Nodebook_Contacts::get( $d['contact_id'] ) : null; if ( $c && in_array( $c['status'], array( 'subscriber', 'lead', 'prospect', '' ), true ) ) { echo '

    ' . esc_html__( 'Won makes the contact a customer.', 'nodebook' ) . '

    '; } } else { echo '

    ' . esc_html( sprintf( /* translators: 1: Won or Lost, 2: date */ __( '%1$s on %2$s.', 'nodebook' ), 'won' === $d['outcome'] ? __( 'Won', 'nodebook' ) : __( 'Lost', 'nodebook' ), $d['closed_at'] ? wp_date( get_option( 'date_format' ), strtotime( $d['closed_at'] . ' UTC' ) ) : '' ) ) . '

    '; $paid = $d['activity_id'] ? Nodebook_Deals::purchase( (int) $d['activity_id'], (int) $d['contact_id'] ) : null; if ( $paid ) { echo '

    ' . esc_html( /* translators: %s: order or booking, e.g. "Order #133 · $89.00" */ sprintf( __( 'Paid with: %s', 'nodebook' ), $paid['title'] ) ) . '

    '; } $open( 'reopen' ); echo ''; } echo '
    '; } /** Reasons given before, most used first, plus a few common ones. */ private static function reasons() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table. $used = (array) $wpdb->get_col( $wpdb->prepare( "SELECT lost_reason FROM %i WHERE outcome = 'lost' AND lost_reason <> '' GROUP BY lost_reason ORDER BY COUNT(*) DESC LIMIT 10", Nodebook_Deals::table() ) ); return array_values( array_unique( array_merge( $used, array( __( 'Too expensive', 'nodebook' ), __( 'Chose someone else', 'nodebook' ), __( 'Date not available', 'nodebook' ), __( 'No reply', 'nodebook' ), __( 'Plans changed', 'nodebook' ) ) ) ) ); } private static function tasks( array $d, array $team ) { $id = (int) $d['id']; $tasks = Nodebook_Tasks::for_deal( $id ); echo '

    ' . esc_html__( 'Next steps', 'nodebook' ) . '

    '; if ( $tasks['open'] ) { echo ''; } elseif ( '' === $d['outcome'] ) { echo '

    ' . esc_html__( 'No next step planned. Deals without one tend to stall.', 'nodebook' ) . '

    '; } if ( '' === $d['outcome'] ) { echo '
    '; wp_nonce_field( 'nodebook_task' ); echo ' '; if ( count( $team ) > 1 ) { 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 '
    '; } private static function history( array $d ) { global $wpdb; if ( ! $d['contact_id'] ) { return; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table. $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT title, created_at, user_id FROM %i WHERE contact_id = %d AND ( ( type = 'deal' AND object_id = %d ) OR ( type IN ('touch','note') AND created_at >= %s ) ) ORDER BY created_at DESC, id DESC LIMIT 40", Nodebook_Activities::table(), $d['contact_id'], $d['id'], $d['created_at'] ), ARRAY_A ); if ( ! $rows ) { return; } echo '

    ' . esc_html__( 'History', 'nodebook' ) . '

      '; foreach ( $rows as $r ) { echo '
    1. ' . esc_html( $r['title'] ) . ' ' . wp_kses( Nodebook_Admin::ago( $r['created_at'] ), Nodebook_Admin::KSES ) . ( $r['user_id'] ? ' · ' . esc_html( Nodebook_Team::name( (int) $r['user_id'] ) ) : '' ) . '
    2. '; } echo '

    ' . esc_html__( 'Stage changes, plus calls and notes on the contact since the deal was added.', 'nodebook' ) . '

    '; } /* ------------------------------------------------------------------ * * Cards on contact and company pages * ------------------------------------------------------------------ */ /** Deals card on a contact profile. */ public static function contact_card( array $c ) { if ( ! Nodebook_Deals::on() ) { return; } $rows = Nodebook_Deals::query( array( 'contact_id' => (int) $c['id'], 'outcome' => 'all', 'order' => 'created', 'limit' => 20, ) ); self::list_card( $rows, Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'new', 'contact' => (int) $c['id'] ) ) ); } /** Deals card in a company sidebar. */ public static function company_card( $co ) { if ( empty( $co['id'] ) || ! Nodebook_Deals::on() ) { return; } $rows = Nodebook_Deals::query( array( 'company_id' => (int) $co['id'], 'outcome' => 'all', 'order' => 'created', 'limit' => 20, ) ); self::list_card( $rows, Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'new', 'company' => (int) $co['id'] ) ) ); } private static function list_card( array $rows, $add ) { $open = array_filter( $rows, static function ( $d ) { return '' === $d['outcome']; } ); echo '

    ' . esc_html__( 'Deals', 'nodebook' ) . ( $open ? ' ' . esc_html( number_format_i18n( count( $open ) ) ) . '' : '' ) . '

    ' . esc_html__( '+ Add deal', 'nodebook' ) . '
    '; if ( ! $rows ) { echo '

    ' . esc_html__( 'No deals yet.', 'nodebook' ) . '

    '; return; } // Open first, then closed (each newest first). usort( $rows, static function ( $a, $b ) { return ( '' === $a['outcome'] ? 0 : 1 ) <=> ( '' === $b['outcome'] ? 0 : 1 ); } ); echo ''; } /** Stage editor (on the Settings screen, as its own form). */ /** * Deal stages editor, part of the settings form (saved with "Save settings"). Rows keep their order on * screen; move them with the arrows, empty or remove a row to drop the stage. */ public static function stages_form() { $n = count( Nodebook_Deals::pipelines() ); echo '

    ' . esc_html__( 'Pipelines and stages', 'nodebook' ) . '

    ' . esc_html( sprintf( /* translators: %s: number of pipelines */ _n( '%s pipeline. Add more, rename them and edit their stages on the Pipelines tab.', '%s pipelines. Add more, rename them and edit their stages on the Pipelines tab.', $n, 'nodebook' ), number_format_i18n( $n ) ) ) . '

    ' . esc_html__( 'Edit pipelines and stages', 'nodebook' ) . '

    '; } private static function stage_row( $i, $key, $label ) { $name = 'nodebook_settings[deal_stages][' . $i . ']'; echo '
  • '; echo '
  • '; } /** From the settings form: rows in screen order → Nodebook_Deals::save_stages(). */ public static function stages_from_settings( $rows ) { if ( ! is_array( $rows ) || ! current_user_can( 'manage_options' ) ) { return; } $labels = array(); $n = 0; foreach ( $rows as $r ) { if ( ! is_array( $r ) ) { continue; } $key = sanitize_key( (string) ( $r['key'] ?? '' ) ); $label = sanitize_text_field( (string) ( $r['label'] ?? '' ) ); $labels[ '' !== $key ? $key : 'new_' . $n ] = $label; ++$n; } if ( $labels && array_filter( $labels, 'strlen' ) ) { Nodebook_Deals::save_stages( $labels ); } } public static function save_stages() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'nodebook' ), 403 ); } check_admin_referer( 'nodebook_deal_stages' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each value is sanitized below. $rows = isset( $_POST['stages'] ) && is_array( $_POST['stages'] ) ? wp_unslash( $_POST['stages'] ) : array(); $list = array(); foreach ( $rows as $i => $r ) { if ( ! is_array( $r ) ) { continue; } $list[] = array( 'key' => sanitize_key( (string) ( $r['key'] ?? '' ) ), 'label' => sanitize_text_field( (string) ( $r['label'] ?? '' ) ), 'pos' => (int) ( $r['pos'] ?? 0 ) * 1000 + (int) $i, ); } usort( $list, static function ( $a, $b ) { return $a['pos'] <=> $b['pos']; } ); $labels = array(); foreach ( $list as $n => $r ) { $labels[ '' !== $r['key'] ? $r['key'] : 'new_' . $n ] = $r['label']; } Nodebook_Deals::save_stages( $labels ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-settings' ) . '#nb-deal-stages', 'saved' ); } /* ------------------------------------------------------------------ * * Handlers * ------------------------------------------------------------------ */ private static function view_url( $id ) { return Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'view', 'id' => (int) $id, ) ); } /** * A contact search box: type a name, email, phone or company and pick. Without JavaScript it still * takes an email address or contact ID. * * @param string $name Field name. * @param string $id Element ID. * @param string $label Label. * @param array|null $c Current contact. * @param int $exclude Contact left out of the results (merging). */ public static function contact_picker( $name, $id, $label, $c, $exclude = 0 ) { $help = $id . '-help'; $sub = $c ? implode( ' · ', array_filter( array( $c['email'], $c['phone'] ) ) ) : ''; echo '
    ' . esc_html( $c ? Nodebook_Contacts::display_name( $c ) . ( '' !== $sub && Nodebook_Contacts::display_name( $c ) !== $sub ? ' · ' . $sub : '' ) : __( 'Or type a new email address to add someone.', 'nodebook' ) ) . '
    '; } /** A company search box; a name that is not a company yet adds it. */ public static function company_picker( $id, $company ) { $help = $id . '-help'; echo '
    ' . esc_html__( 'Empty: the contact’s company.', 'nodebook' ) . '
    '; } /** Sources used on deals before, most used first. */ private static function sources() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- plugin table. return (array) $wpdb->get_col( $wpdb->prepare( "SELECT source FROM %i WHERE source <> '' GROUP BY source ORDER BY COUNT(*) DESC LIMIT 20", Nodebook_Deals::table() ) ); } /** * Search for the pickers: contacts (only those this user may see) or companies. * Each result: value (what goes in the box), id, label, sub; plus "add new" when nothing matches exactly. */ public static function pick_ajax() { check_ajax_referer( 'nodebook_ajax', 'nonce' ); if ( ! current_user_can( Nodebook_Admin::cap() ) ) { wp_send_json_error( null, 403 ); } $kind = isset( $_POST['kind'] ) ? sanitize_key( wp_unslash( $_POST['kind'] ) ) : 'contact'; $q = isset( $_POST['q'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['q'] ) ) ) : ''; $exclude = isset( $_POST['exclude'] ) ? absint( $_POST['exclude'] ) : 0; $out = array(); if ( mb_strlen( $q ) < 2 ) { wp_send_json_success( $out ); } $exact = false; if ( 'company' === $kind ) { foreach ( Nodebook_Companies::query( array( 'search' => $q, 'per_page' => 8 ) )['rows'] as $co ) { $name = Nodebook_Fields::name_case( $co['name'] ); $exact = $exact || 0 === strcasecmp( $co['name'], $q ); $out[] = array( 'value' => $name, 'id' => (int) $co['id'], 'label' => $name, 'sub' => implode( ' · ', array_filter( array( (string) $co['domain'], (string) $co['city'] ) ) ), ); } if ( ! $exact ) { $out[] = array( 'value' => $q, 'id' => 0, /* translators: %s: company name */ 'label' => sprintf( __( 'Add company “%s”', 'nodebook' ), $q ), 'sub' => '', 'new' => true, ); } wp_send_json_success( $out ); } $rows = Nodebook_Contacts::query( array( 'search' => $q, 'per_page' => 9, 'orderby' => 'last_seen', 'order' => 'DESC', ) )['rows']; foreach ( $rows as $c ) { if ( (int) $c['id'] === $exclude || count( $out ) >= 8 ) { continue; } $exact = $exact || ( '' !== (string) $c['email'] && 0 === strcasecmp( $c['email'], $q ) ); $out[] = array( 'value' => '' !== (string) $c['email'] ? $c['email'] : (string) $c['id'], 'id' => (int) $c['id'], 'label' => Nodebook_Contacts::display_name( $c ), 'sub' => implode( ' · ', array_filter( array( (string) $c['email'], (string) ( $c['company'] ?? '' ) ) ) ), ); } if ( ! $exact && is_email( $q ) && ! Nodebook_Contacts::find_by_email( $q ) ) { $out[] = array( 'value' => strtolower( $q ), 'id' => 0, /* translators: %s: email address */ 'label' => sprintf( __( 'Add new contact %s', 'nodebook' ), strtolower( $q ) ), 'sub' => '', 'new' => true, ); } wp_send_json_success( $out ); } /** Company from the picker: the picked one, a known name, or a new company. 0 = none given. */ private static function resolve_company( $id, $name ) { $name = trim( (string) $name ); if ( '' === $name ) { return 0; } if ( $id ) { $co = Nodebook_Companies::get( $id ); if ( $co && 0 === strcasecmp( Nodebook_Fields::name_case( $co['name'] ), $name ) ) { return (int) $co['id']; } } $found = Nodebook_Companies::find_by_name( $name ); if ( $found ) { return (int) $found['id']; } return (int) Nodebook_Companies::insert( array( 'name' => mb_substr( $name, 0, 190 ) ) ); } /** Contact from "email or ID"; a new email creates a lead. */ public static function resolve_contact( $value ) { $value = trim( (string) $value ); if ( '' === $value ) { return 0; } if ( ctype_digit( $value ) ) { return Nodebook_Contacts::get( (int) $value ) && Nodebook_Team::can_see( (int) $value ) ? (int) $value : -1; } if ( ! is_email( $value ) ) { return -1; } $found = Nodebook_Contacts::find_by_email( $value ); if ( $found ) { return Nodebook_Team::can_see( $found ) ? (int) $found['id'] : -1; } $id = Nodebook_Contacts::insert( array( 'email' => $value, 'status' => 'lead', ) ); if ( ! $id ) { return -1; } Nodebook_Activities::add( $id, 'created', __( 'Contact added manually', 'nodebook' ) ); return (int) $id; } public static function save() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_deal_' . $id ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- verified just above. $cid = self::resolve_contact( isset( $_POST['contact'] ) ? sanitize_text_field( wp_unslash( $_POST['contact'] ) ) : '' ); if ( $cid < 0 ) { $back = $id ? self::view_url( $id ) : Nodebook_Admin::url( 'nodebook-deals', array( 'action' => 'new' ) ); Nodebook_Admin::redirect( $back, 'dealcontact' ); } $team = Nodebook_Team::users(); $in = array( 'title' => isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '', 'contact_id' => $cid, 'value' => isset( $_POST['value'] ) ? sanitize_text_field( wp_unslash( $_POST['value'] ) ) : '', 'currency' => isset( $_POST['currency'] ) ? sanitize_text_field( wp_unslash( $_POST['currency'] ) ) : '', 'expected_close' => isset( $_POST['expected_close'] ) ? sanitize_text_field( wp_unslash( $_POST['expected_close'] ) ) : '', 'notes' => isset( $_POST['notes'] ) ? sanitize_textarea_field( wp_unslash( $_POST['notes'] ) ) : '', 'source' => isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '', 'props' => isset( $_POST['prop'] ) && is_array( $_POST['prop'] ) ? array_intersect_key( map_deep( wp_unslash( $_POST['prop'] ), 'sanitize_textarea_field' ), Nodebook_Props::defs( 'deal' ) ) : array(), ); if ( isset( $_POST['pstage'] ) ) { $ps = explode( ':', sanitize_text_field( wp_unslash( $_POST['pstage'] ) ), 2 ); if ( 2 === count( $ps ) && isset( Nodebook_Deals::pipelines()[ absint( $ps[0] ) ] ) ) { $in['pipeline_id'] = absint( $ps[0] ); $in['stage'] = sanitize_key( $ps[1] ); } } elseif ( isset( $_POST['stage'] ) ) { $in['stage'] = sanitize_key( wp_unslash( $_POST['stage'] ) ); } if ( isset( $_POST['owner'] ) ) { $o = absint( $_POST['owner'] ); $in['owner_id'] = 0 === $o || isset( $team[ $o ] ) ? $o : get_current_user_id(); } // Company: what was picked or typed; empty = the contact's company. $co = self::resolve_company( isset( $_POST['company'] ) ? absint( $_POST['company'] ) : 0, isset( $_POST['company_name'] ) ? sanitize_text_field( wp_unslash( $_POST['company_name'] ) ) : '' ); if ( ! $co && $cid ) { $contact = Nodebook_Contacts::get( $cid ); $co = $contact ? (int) $contact['company_id'] : 0; } $in['company_id'] = $co; // phpcs:enable WordPress.Security.NonceVerification.Missing if ( $id ) { Nodebook_Deals::update( $id, $in ); Nodebook_Admin::redirect( self::view_url( $id ), 'saved' ); } $new = Nodebook_Deals::add( $in ); Nodebook_Admin::redirect( $new ? self::view_url( $new ) : Nodebook_Admin::url( 'nodebook-deals' ), $new ? 'dealadded' : '' ); } public static function close() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_deal_' . $id ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- verified just above. $op = isset( $_POST['op'] ) ? sanitize_key( wp_unslash( $_POST['op'] ) ) : ''; $reason = isset( $_POST['reason'] ) ? sanitize_text_field( wp_unslash( $_POST['reason'] ) ) : ''; $paid = isset( $_POST['purchase'] ) ? absint( $_POST['purchase'] ) : 0; // phpcs:enable WordPress.Security.NonceVerification.Missing $map = array( 'won' => 'won', 'lost' => 'lost', 'reopen' => '', ); if ( isset( $map[ $op ] ) ) { Nodebook_Deals::close( $id, $map[ $op ], $reason, $paid ); } Nodebook_Admin::redirect( self::view_url( $id ), 'won' === $op ? 'dealwon' : 'saved' ); } public static function delete() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_deal_' . $id ); Nodebook_Deals::delete( $id ); Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-deals' ), 'deleted' ); } /** Move to a stage without JavaScript (the card's menu). */ public static function move_post() { $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- ID is part of the nonce action verified below. Nodebook_Admin::require_cap(); check_admin_referer( 'nodebook_deal_' . $id ); $stage = isset( $_POST['stage'] ) ? sanitize_key( wp_unslash( $_POST['stage'] ) ) : ''; $d = Nodebook_Deals::get( $id ); if ( $d && isset( Nodebook_Deals::stages( (int) $d['pipeline_id'] )[ $stage ] ) ) { Nodebook_Deals::update( $id, array( 'stage' => $stage ) ); } Nodebook_Admin::redirect( Nodebook_Admin::url( 'nodebook-deals', $d ? array( 'pipeline' => (int) $d['pipeline_id'] ) : array() ), 'saved' ); } /** Board drag: move, then send back the label and fresh column totals. */ public static function move_ajax() { check_ajax_referer( 'nodebook_ajax', 'nonce' ); if ( ! current_user_can( Nodebook_Admin::cap() ) ) { wp_send_json_error( null, 403 ); } $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; $stage = isset( $_POST['status'] ) ? sanitize_key( wp_unslash( $_POST['status'] ) ) : ''; $d = Nodebook_Deals::get( $id ); $stages = $d ? Nodebook_Deals::stages( (int) $d['pipeline_id'] ) : array(); if ( ! $d || '' !== $d['outcome'] || ! isset( $stages[ $stage ] ) ) { wp_send_json_error( null, 400 ); } Nodebook_Deals::update( $id, array( 'stage' => $stage ) ); $who = isset( $_POST['who'] ) && 'all' === sanitize_key( wp_unslash( $_POST['who'] ) ); $owner = ! $who && Nodebook_Team::is_team() ? get_current_user_id() : 0; $totals = array(); foreach ( Nodebook_Deals::stage_totals( $owner, (int) $d['pipeline_id'] ) as $k => $t ) { $totals[ $k ] = $t['count'] ? Nodebook_Deals::total_label( $t ) : ''; } wp_send_json_success( array( 'label' => $stages[ $stage ], 'totals' => $totals, ) ); } }