__( 'Nodebook CRM', 'nodebook' ), 'description' => __( 'Contacts, customer journeys and follow-ups from Nodebook.', 'nodebook' ), ) ); } /** Ability name => definition. Public so tests and add-ons can inspect them. */ public static function definitions() { $read = array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ); $limit = array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50, 'default' => 10, 'description' => __( 'How many to return (at most 50).', 'nodebook' ), ); return array( 'nodebook/search-contacts' => array( 'label' => __( 'Search contacts', 'nodebook' ), 'description' => __( 'Finds Nodebook contacts by name, email, phone or company, optionally filtered by status. Returns the most recently created first.', 'nodebook' ), 'input_schema' => array( 'type' => 'object', 'properties' => array( 'query' => array( 'type' => 'string', 'description' => __( 'Text to look for. Leave empty to list the newest contacts.', 'nodebook' ), ), 'status' => array( 'type' => 'string', 'enum' => array_merge( array( '' ), array_keys( Nodebook_Fields::statuses() ) ), ), 'limit' => $limit, ), 'additionalProperties' => false, 'default' => array(), ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'total' => array( 'type' => 'integer' ), 'contacts' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ), ), ), ), 'cb' => 'search', 'annotations' => $read, ), 'nodebook/get-contact' => array( 'label' => __( 'Get contact', 'nodebook' ), 'description' => __( 'Returns one contact by ID or email address: details, status, where they came from, what they bought, their own fields and the latest timeline events (visits, forms, orders, notes).', 'nodebook' ), 'input_schema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer', 'minimum' => 1, ), 'email' => array( 'type' => 'string', 'format' => 'email', ), 'timeline' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => 100, 'default' => 20, 'description' => __( 'How many timeline events to include.', 'nodebook' ), ), ), 'additionalProperties' => false, 'default' => array(), ), 'output_schema' => array( 'type' => 'object' ), 'cb' => 'get_contact', 'annotations' => $read, ), 'nodebook/follow-ups' => array( 'label' => __( 'Who to follow up with', 'nodebook' ), 'description' => __( 'Lists the contacts Nodebook suggests following up with now (new inquiries, people back on the site, reminders that are due, leads gone quiet), each with the reason.', 'nodebook' ), 'input_schema' => array( 'type' => 'object', 'properties' => array( 'mine' => array( 'type' => 'boolean', 'default' => false, 'description' => __( 'Only contacts assigned to the signed-in user (plus unassigned ones).', 'nodebook' ), ), 'limit' => $limit, ), 'additionalProperties' => false, 'default' => array(), ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'contacts' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ), ), ), ), 'cb' => 'follow_ups', 'annotations' => $read, ), 'nodebook/list-deals' => array( 'label' => __( 'List deals', 'nodebook' ), 'description' => __( 'Lists deals (possible sales or bookings) with their value, stage, contact, owner and expected close date. Open deals by default; also won or lost ones. Includes the open value per stage.', 'nodebook' ), 'input_schema' => array( 'type' => 'object', 'properties' => array( 'outcome' => array( 'type' => 'string', 'enum' => array( 'open', 'won', 'lost', 'all' ), 'default' => 'open', ), 'mine' => array( 'type' => 'boolean', 'default' => false, 'description' => __( 'Only deals of the signed-in user (plus unassigned ones).', 'nodebook' ), ), 'closing_within_days' => array( 'type' => 'integer', 'minimum' => 0, 'maximum' => 365, 'description' => __( 'Only open deals expected to close within this many days (or already past their date).', 'nodebook' ), ), 'limit' => $limit, ), 'additionalProperties' => false, 'default' => array(), ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'deals' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ), ), 'stages' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ), ), ), ), 'cb' => 'list_deals', 'annotations' => $read, ), 'nodebook/weekly-summary' => array( 'label' => __( 'Weekly numbers', 'nodebook' ), 'description' => __( 'The last seven days compared with the week before: new contacts, form submissions, orders and bookings, revenue, top sources and most-viewed items.', 'nodebook' ), 'input_schema' => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => false, 'default' => array(), ), 'output_schema' => array( 'type' => 'object' ), 'cb' => 'weekly', 'annotations' => $read, ), 'nodebook/track-event' => array( 'label' => __( 'Record an event', 'nodebook' ), 'description' => __( 'Records a custom event on a contact\'s timeline (for example "webinar_attended" or "trial_started"), optionally with a value. Events can be used in smart segments.', 'nodebook' ), 'input_schema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer', 'minimum' => 1, ), 'email' => array( 'type' => 'string', 'format' => 'email', ), 'event' => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 60, ), 'value' => array( 'type' => 'string', 'maxLength' => 190, ), ), 'required' => array( 'event' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'event_id' => array( 'type' => 'integer' ), ), ), 'cb' => 'track_event', 'annotations' => array( 'readonly' => false, 'destructive' => false, 'idempotent' => false, ), ), 'nodebook/add-note' => array( 'label' => __( 'Add a note to a contact', 'nodebook' ), 'description' => __( 'Adds a note to a contact\'s timeline, signed as the signed-in user. Optionally plans a follow-up task for a date.', 'nodebook' ), 'input_schema' => array( 'type' => 'object', 'properties' => array( 'id' => array( 'type' => 'integer', 'minimum' => 1, ), 'note' => array( 'type' => 'string', 'minLength' => 1, 'maxLength' => 5000, ), 'remind_date' => array( 'type' => 'string', 'pattern' => '^\d{4}-\d{2}-\d{2}$', 'description' => __( 'Optional follow-up date (YYYY-MM-DD, site time): creates a "Follow up" task.', 'nodebook' ), ), ), 'required' => array( 'id', 'note' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'note_id' => array( 'type' => 'integer' ), 'url' => array( 'type' => 'string' ), ), ), 'cb' => 'add_note', 'annotations' => array( 'readonly' => false, 'destructive' => false, 'idempotent' => false, ), ), ); } public function register() { foreach ( self::definitions() as $name => $d ) { wp_register_ability( $name, array( 'label' => $d['label'], 'description' => $d['description'], 'category' => self::CATEGORY, 'input_schema' => $d['input_schema'], 'output_schema' => $d['output_schema'], 'execute_callback' => array( __CLASS__, $d['cb'] ), 'permission_callback' => array( __CLASS__, 'can' ), 'meta' => array( 'annotations' => $d['annotations'], 'public' => true, ), ) ); } } /** Same rule as the Nodebook screens. */ public static function can() { return current_user_can( Nodebook_Admin::cap() ); } /* ------------------------------------------------------------------ * * Ability callbacks * ------------------------------------------------------------------ */ public static function search( $input = array() ) { $input = (array) $input; $status = isset( $input['status'] ) ? sanitize_key( $input['status'] ) : ''; $res = Nodebook_Contacts::query( array( 'search' => isset( $input['query'] ) ? sanitize_text_field( (string) $input['query'] ) : '', 'status' => isset( Nodebook_Fields::statuses()[ $status ] ) ? $status : '', 'per_page' => self::limit( $input ), ) ); return array( 'total' => (int) $res['total'], 'contacts' => array_map( array( __CLASS__, 'summary' ), $res['rows'] ), ); } public static function get_contact( $input = array() ) { $input = (array) $input; $c = null; if ( ! empty( $input['id'] ) ) { $c = Nodebook_Contacts::get( absint( $input['id'] ) ); } elseif ( ! empty( $input['email'] ) ) { $c = Nodebook_Contacts::find_by_email( (string) $input['email'] ); } // Someone else's contact (Settings → Team: own contacts only) answers like an unknown one. if ( ! $c || ! Nodebook_Team::can_see( $c ) ) { return new WP_Error( 'nodebook_not_found', __( 'No contact found.', 'nodebook' ), array( 'status' => 404 ) ); } $out = self::summary( $c ); $out['details'] = array_filter( array( 'job_title' => (string) $c['job_title'], 'website' => (string) $c['website'], 'address' => Nodebook_Admin::address_line( $c ), 'description' => (string) $c['description'], ), 'strlen' ); $out['emails'] = Nodebook_Contacts::emails( $c['id'] ); $journey = Nodebook_Report::journey( $c ); $out['journey'] = array( 'source' => $journey['has_source'] ? $journey['source'] : '', 'campaign' => (string) $journey['campaign'], 'landing' => (string) $journey['landing'], 'referrer' => (string) $journey['referrer'], 'first_seen' => self::date( $journey['start'] ), 'converted' => $journey['conversion'] ? array( 'type' => $journey['conversion']['type'], 'title' => $journey['conversion']['title'], 'date' => self::date( $journey['conversion']['created_at'] ), ) : null, 'first_order' => $journey['first_order'] ? array( 'title' => $journey['first_order']['title'], 'date' => self::date( $journey['first_order']['created_at'] ), ) : null, ); $fields = array(); $values = Nodebook_Props::values( 'contact', $c['id'] ); foreach ( Nodebook_Props::defs( 'contact' ) as $key => $def ) { if ( isset( $values[ $key ] ) && '' !== $values[ $key ] ) { $fields[ $def['label'] ] = Nodebook_Props::display( $def, $values[ $key ] ); } } $out['fields'] = (object) $fields; $out['reminder'] = $c['remind_at'] ? self::date( $c['remind_at'] ) : null; $out['tasks'] = array(); foreach ( Nodebook_Tasks::for_contact( $c['id'], 5 ) as $state => $list ) { foreach ( $list as $t ) { $out['tasks'][] = array_filter( array( 'title' => $t['title'], 'due' => self::date( $t['due_at'] ), 'done' => 'done' === $state, 'outcome' => (string) $t['outcome'], 'assignee' => $t['assignee_id'] ? Nodebook_Team::name( (int) $t['assignee_id'] ) : '', ), static function ( $v ) { return null !== $v && '' !== $v; } ); } } $out['deals'] = array_map( array( __CLASS__, 'deal' ), Nodebook_Deals::query( array( 'contact_id' => (int) $c['id'], 'outcome' => 'all', 'order' => 'created', 'limit' => 20 ) ) ); $out['follow_up'] = Nodebook_Followup::is_due( $c ) ? self::reason( $c['id'] ) : null; $types = Nodebook_Activities::types(); $out['timeline'] = array(); $n = isset( $input['timeline'] ) ? max( 0, min( 100, (int) $input['timeline'] ) ) : 20; foreach ( $n ? Nodebook_Activities::for_contact( $c['id'], $n ) : array() as $a ) { $data = is_array( $a['data'] ) ? $a['data'] : json_decode( (string) $a['data'], true ); $item = array( 'type' => $types[ $a['type'] ] ?? $a['type'], 'title' => (string) $a['title'], 'date' => self::date( $a['created_at'] ), ); if ( 'note' === $a['type'] && is_array( $data ) && ! empty( $data['text'] ) ) { $item['text'] = (string) $data['text']; } if ( $a['url'] ) { $item['url'] = (string) $a['url']; } $out['timeline'][] = $item; } return $out; } public static function list_deals( $input = array() ) { $input = (array) $input; $owner = ! empty( $input['mine'] ) ? get_current_user_id() : 0; $outcome = isset( $input['outcome'] ) && in_array( $input['outcome'], array( 'open', 'won', 'lost', 'all' ), true ) ? $input['outcome'] : 'open'; $args = array( 'outcome' => $outcome, 'owner' => $owner, 'limit' => self::limit( $input ), 'order' => 'open' === $outcome ? 'stage' : 'closed', ); if ( isset( $input['closing_within_days'] ) ) { $args['outcome'] = 'open'; $args['closing_before'] = wp_date( 'Y-m-d', time() + max( 0, (int) $input['closing_within_days'] ) * DAY_IN_SECONDS ); $args['order'] = 'close'; } $stages = array(); $pipes = Nodebook_Deals::pipelines(); foreach ( $pipes as $pid => $p ) { foreach ( Nodebook_Deals::stage_totals( $owner, (int) $pid ) as $k => $t ) { $stages[] = array( 'stage' => ( count( $pipes ) > 1 ? $p['name'] . ': ' : '' ) . $p['stages'][ $k ], 'count' => $t['count'], 'value' => $t['value'], 'other' => (object) $t['other'], ); } } return array( 'deals' => array_map( array( __CLASS__, 'deal' ), Nodebook_Deals::query( $args ) ), 'stages' => $stages, 'currency' => Nodebook_Conversions::currency(), ); } /** A deal's own fields as label => readable value (null when it has none). */ private static function deal_fields( $id ) { $out = array(); foreach ( Nodebook_Props::values( 'deal', $id ) as $k => $v ) { $def = Nodebook_Props::get( 'deal', $k ); if ( $def && '' !== $v ) { $out[ $def['label'] ] = Nodebook_Props::display( $def, $v ); } } return $out ? $out : null; } /** One deal for an assistant. */ public static function deal( array $d ) { $stages = Nodebook_Deals::stages( (int) $d['pipeline_id'] ); $c = isset( $d['first_name'] ) ? $d : ( $d['contact_id'] ? Nodebook_Contacts::get( (int) $d['contact_id'] ) : null ); return array_filter( array( 'id' => (int) $d['id'], 'title' => (string) $d['title'], 'value' => (float) $d['value'], 'currency' => (string) $d['currency'], 'stage' => '' === $d['outcome'] ? Nodebook_Deals::stage_name( $d ) : '', 'outcome' => '' === $d['outcome'] ? 'open' : $d['outcome'], 'lost_reason' => (string) $d['lost_reason'], 'contact_id' => (int) $d['contact_id'], 'contact' => $c && $d['contact_id'] ? Nodebook_Contacts::display_name( $c ) : '', 'company' => isset( $d['company'] ) ? (string) $d['company'] : '', 'owner' => $d['owner_id'] ? Nodebook_Team::name( (int) $d['owner_id'] ) : '', 'expected_close' => $d['expected_close'] ? (string) $d['expected_close'] : null, 'late' => Nodebook_Deals::is_late( $d ), 'closed' => self::date( $d['closed_at'] ), 'created' => self::date( $d['created_at'] ), 'notes' => (string) $d['notes'], 'source' => (string) $d['source'], 'fields' => self::deal_fields( (int) $d['id'] ), 'paid_with' => $d['activity_id'] ? ( Nodebook_Deals::purchase( (int) $d['activity_id'], (int) $d['contact_id'] )['title'] ?? '' ) : '', 'url' => admin_url( 'admin.php?page=nodebook-deals&action=view&id=' . (int) $d['id'] ), ), static function ( $v ) { return null !== $v && '' !== $v && false !== $v; } ); } public static function follow_ups( $input = array() ) { $input = (array) $input; $owner = ! empty( $input['mine'] ) ? get_current_user_id() : 0; $out = array(); foreach ( Nodebook_Followup::due( self::limit( $input ), $owner ) as $c ) { $row = self::summary( $c ); $row['reason'] = self::reason( $c['id'] ); $out[] = $row; } return array( 'contacts' => $out ); } public static function weekly( $input = array() ) { unset( $input ); $d = Nodebook_Weekly::build( time() ); $pair = static function ( $p ) { return array( 'this_week' => is_array( $p ) ? $p[0] : $p, 'last_week' => is_array( $p ) ? $p[1] : null, ); }; return array( 'new_contacts' => $pair( $d['new'] ), 'forms' => $pair( $d['forms'] ), 'orders_bookings' => $pair( $d['orders'] ), 'revenue' => $pair( $d['revenue'] ), 'status_changes' => (int) $d['moves'], 'contacted' => (int) $d['touches'], 'to_follow_up' => (int) $d['due'], 'deals_won' => array( 'count' => (int) $d['deals'][0], 'value' => (float) $d['deals'][1], ), 'deals_new' => (int) $d['deals_new'], 'pipeline_value' => (float) $d['deals_open'], 'top_sources' => array_values( (array) $d['sources'] ), 'top_items' => array_values( (array) $d['pages'] ), ); } public static function add_note( $input = array() ) { $input = (array) $input; $id = isset( $input['id'] ) ? absint( $input['id'] ) : 0; $note = isset( $input['note'] ) ? sanitize_textarea_field( (string) $input['note'] ) : ''; if ( ! $id || ! Nodebook_Contacts::get( $id ) || ! Nodebook_Team::can_see( $id ) ) { return new WP_Error( 'nodebook_not_found', __( 'No contact found.', 'nodebook' ), array( 'status' => 404 ) ); } if ( '' === trim( $note ) ) { return new WP_Error( 'nodebook_empty', __( 'The note is empty.', 'nodebook' ), array( 'status' => 400 ) ); } $user = get_current_user_id(); $note_id = Nodebook_Activities::add( $id, 'note', wp_trim_words( $note, 12, '…' ), array( 'user_id' => $user, 'data' => array( 'text' => $note, 'via' => 'abilities', ), ) ); $date = isset( $input['remind_date'] ) ? (string) $input['remind_date'] : ''; if ( preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date ) ) { $when = ( new DateTimeImmutable( $date . ' 09:00', wp_timezone() ) )->getTimestamp(); if ( $when > time() ) { Nodebook_Tasks::add( $id, __( 'Follow up', 'nodebook' ), array( 'due_at' => gmdate( 'Y-m-d H:i:s', $when ), 'assignee_id' => $user, ) ); } } Nodebook_Contacts::flush_caches(); return array( 'note_id' => (int) $note_id, 'url' => admin_url( 'admin.php?page=nodebook&action=view&id=' . $id ), ); } public static function track_event( $input = array() ) { $input = (array) $input; $who = ! empty( $input['id'] ) ? absint( $input['id'] ) : (string) ( $input['email'] ?? '' ); $known = is_int( $who ) ? Nodebook_Contacts::get( $who ) : ( '' !== $who ? Nodebook_Contacts::find_by_email( $who ) : null ); if ( $known && ! Nodebook_Team::can_see( $known ) ) { return new WP_Error( 'nodebook_not_found', __( 'No contact found, or the event name is empty.', 'nodebook' ), array( 'status' => 404 ) ); } $aid = $who ? Nodebook_Events::track( $who, (string) ( $input['event'] ?? '' ), array( 'value' => (string) ( $input['value'] ?? '' ) ) ) : 0; if ( ! $aid ) { return new WP_Error( 'nodebook_not_found', __( 'No contact found, or the event name is empty.', 'nodebook' ), array( 'status' => 404 ) ); } return array( 'event_id' => (int) $aid ); } /* ------------------------------------------------------------------ * * Helpers * ------------------------------------------------------------------ */ private static function limit( array $input ) { return isset( $input['limit'] ) ? max( 1, min( 50, (int) $input['limit'] ) ) : 10; } /** The fields an assistant needs to talk about a contact. */ private static function summary( array $c ) { $statuses = Nodebook_Fields::statuses(); $owner = (int) $c['owner_id'] ? get_userdata( (int) $c['owner_id'] ) : false; $company = isset( $c['company'] ) ? (string) $c['company'] : ''; if ( ! isset( $c['company'] ) && (int) $c['company_id'] ) { $co = Nodebook_Companies::get( (int) $c['company_id'] ); $company = $co ? (string) $co['name'] : ''; } return array( 'id' => (int) $c['id'], 'name' => Nodebook_Contacts::display_name( $c ), 'email' => (string) $c['email'], 'phone' => (string) $c['phone'], 'company' => $company, 'status' => $statuses[ $c['status'] ] ?? (string) $c['status'], 'email_consent' => (string) ( $c['email_status'] ?? '' ) !== '' ? (string) $c['email_status'] : 'not_asked', 'owner' => $owner ? $owner->display_name : '', 'source' => '' !== (string) $c['first_source'] ? Nodebook_Attribution::label( $c['first_source'], $c['first_medium'] ) : '', 'orders' => (int) $c['orders_count'], 'total_spent' => (float) $c['total_spent'], 'visits' => (int) $c['visits_count'], 'last_seen' => self::date( $c['last_seen'] ), 'created' => self::date( $c['created_at'] ), 'url' => admin_url( 'admin.php?page=nodebook&action=view&id=' . (int) $c['id'] ), ); } private static function reason( $contact_id ) { $r = Nodebook_Followup::reason( $contact_id ); return $r ? wp_strip_all_tags( (string) $r['title'] ) : ''; } /** GMT datetime from the database as ISO 8601, or null. */ private static function date( $gmt ) { if ( empty( $gmt ) || 0 === strpos( (string) $gmt, '0000' ) ) { return null; } $ts = strtotime( $gmt . ' UTC' ); return $ts ? gmdate( 'c', $ts ) : null; } }