__( 'Visit', 'nodebook' ), 'pageview' => __( 'Page view', 'nodebook' ), 'form' => __( 'Form submission', 'nodebook' ), 'order' => __( 'Order', 'nodebook' ), 'refund' => __( 'Refund', 'nodebook' ), 'note' => __( 'Note', 'nodebook' ), 'signup' => __( 'Registration', 'nodebook' ), 'created' => __( 'Contact created', 'nodebook' ), 'import' => __( 'Imported', 'nodebook' ), 'segment' => __( 'Segment', 'nodebook' ), 'status' => __( 'Status change', 'nodebook' ), 'merge' => __( 'Merge', 'nodebook' ), 'booking' => __( 'Booking', 'nodebook' ), 'appointment' => __( 'Appointment', 'nodebook' ), 'click' => __( 'Click', 'nodebook' ), 'followup' => __( 'Follow-up', 'nodebook' ), 'touch' => __( 'Contacted', 'nodebook' ), 'subscription' => __( 'Email consent', 'nodebook' ), 'event' => __( 'Event', 'nodebook' ), 'task' => __( 'Task', 'nodebook' ), 'deal' => __( 'Deal', 'nodebook' ), 'gathering' => __( 'Gathering', 'nodebook' ), ) ); } /** * Add an activity. * * @param int $contact_id Contact ID (0 for an anonymous visitor). * @param string $type Activity type. * @param string $title Short human title. * @param array $args url, data (array), object_id, visitor_id, created_at (GMT 'Y-m-d H:i:s'), user_id. * @return int Activity ID. */ public static function add( $contact_id, $type, $title, array $args = array() ) { global $wpdb; $row = array( 'contact_id' => absint( $contact_id ), 'visitor_id' => isset( $args['visitor_id'] ) ? (string) $args['visitor_id'] : '', 'type' => sanitize_key( $type ), 'object_id' => isset( $args['object_id'] ) ? absint( $args['object_id'] ) : 0, 'title' => mb_substr( sanitize_text_field( $title ), 0, 255 ), 'url' => isset( $args['url'] ) ? esc_url_raw( $args['url'] ) : '', 'data' => isset( $args['data'] ) ? wp_json_encode( $args['data'] ) : '', 'user_id' => isset( $args['user_id'] ) ? absint( $args['user_id'] ) : get_current_user_id(), 'created_at' => ! empty( $args['created_at'] ) ? $args['created_at'] : current_time( 'mysql', true ), ); if ( false === $wpdb->insert( self::table(), $row ) ) { return 0; } $id = (int) $wpdb->insert_id; /** * Fires after an activity is recorded. */ do_action( 'nodebook_activity_added', $id, $row ); return $id; } public static function count_type( $contact_id, $type ) { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE contact_id = %d AND type = %s', self::table(), $contact_id, $type ) ); } /** A contact's most recent activity of one type, or null. */ public static function latest( $contact_id, $type ) { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE contact_id = %d AND type = %s ORDER BY id DESC LIMIT 1', self::table(), $contact_id, $type ), ARRAY_A ); return $row ? $row : null; } public static function find( $type, $object_id ) { global $wpdb; return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE type = %s AND object_id = %d ORDER BY id ASC LIMIT 1', self::table(), $type, $object_id ), ARRAY_A ); } public static function update( $id, array $fields ) { global $wpdb; if ( isset( $fields['data'] ) && is_array( $fields['data'] ) ) { $fields['data'] = wp_json_encode( $fields['data'] ); } if ( isset( $fields['title'] ) ) { $fields['title'] = mb_substr( sanitize_text_field( $fields['title'] ), 0, 255 ); } $fields = array_intersect_key( $fields, array_flip( array( 'contact_id', 'title', 'data', 'url', 'created_at' ) ) ); return false !== $wpdb->update( self::table(), $fields, array( 'id' => (int) $id ) ); } public static function delete( $id ) { global $wpdb; return (bool) $wpdb->delete( self::table(), array( 'id' => (int) $id ) ); } public static function for_contact( $contact_id, $limit = 200 ) { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE contact_id = %d ORDER BY created_at DESC, id DESC LIMIT %d', self::table(), $contact_id, $limit ), ARRAY_A ); foreach ( $rows as &$r ) { $d = json_decode( (string) $r['data'], true ); $r['data'] = is_array( $d ) ? $d : array(); } return $rows; } public static function count_for_contact( $contact_id ) { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE contact_id = %d', self::table(), $contact_id ) ); } public static function recent( $limit = 15 ) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( "SELECT a.*, c.first_name, c.last_name, c.email FROM %i a INNER JOIN %i c ON c.id = a.contact_id WHERE a.type NOT IN ('pageview') ORDER BY a.created_at DESC, a.id DESC LIMIT %d", self::table(), Nodebook_Contacts::table(), $limit ), ARRAY_A ); } /** * Group activities into timeline items: a visit carries its page views. * Input is newest-first; output is newest-first. */ public static function group( array $rows ) { $items = array(); $cv = null; // Index of the visit currently collecting page views. foreach ( array_reverse( $rows ) as $r ) { if ( 'visit' === $r['type'] ) { $items[] = array( 'row' => $r, 'pages' => array(), ); $cv = count( $items ) - 1; continue; } if ( 'pageview' === $r['type'] ) { if ( null === $cv ) { $items[] = array( 'row' => array_merge( $r, array( 'type' => 'visit', 'title' => __( 'Visited the website', 'nodebook' ) ) ), 'pages' => array(), ); $cv = count( $items ) - 1; } $items[ $cv ]['pages'][] = $r; continue; } $items[] = array( 'row' => $r, 'pages' => array(), ); } return array_reverse( $items ); } }