'Attended the webinar', 'value' => 'Villa investing 101' ) ); * * @package Nodebook */ // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom tables owned by this plugin. defined( 'ABSPATH' ) || exit; final class Nodebook_Events { const OPTION = 'nodebook_events'; const MAX = 200; // Distinct event names per site. /** "Webinar attended!" → "webinar_attended". */ public static function key( $name ) { $k = strtolower( remove_accents( trim( (string) $name ) ) ); $k = preg_replace( '/[^a-z0-9]+/', '_', $k ); return substr( trim( (string) $k, '_' ), 0, 60 ); } /** Known events: key => [ id, label, count, last ]. */ public static function all() { $all = get_option( self::OPTION, array() ); return is_array( $all ) ? $all : array(); } /** Key => label, for pickers. */ public static function labels() { $out = array(); foreach ( self::all() as $k => $e ) { $out[ $k ] = (string) $e['label']; } asort( $out ); return $out; } /** Numeric ID of an event name (stored as the activity's object_id), or 0 when unknown. */ public static function id( $name ) { $all = self::all(); $k = self::key( $name ); return isset( $all[ $k ] ) ? (int) $all[ $k ]['id'] : 0; } /** Register an event name; returns its ID (0 when the site already has the maximum). */ public static function register( $name, $label = '' ) { $k = self::key( $name ); if ( '' === $k ) { return 0; } $all = self::all(); if ( isset( $all[ $k ] ) ) { if ( '' !== (string) $label && $label !== $all[ $k ]['label'] && $all[ $k ]['label'] === self::humanize( $k ) ) { $all[ $k ]['label'] = mb_substr( sanitize_text_field( (string) $label ), 0, 120 ); update_option( self::OPTION, $all, false ); } return (int) $all[ $k ]['id']; } if ( count( $all ) >= self::MAX ) { return 0; } $id = 1; foreach ( $all as $e ) { $id = max( $id, (int) $e['id'] + 1 ); } $all[ $k ] = array( 'id' => $id, 'label' => '' !== (string) $label ? mb_substr( sanitize_text_field( (string) $label ), 0, 120 ) : self::humanize( $k ), 'count' => 0, 'last' => '', ); update_option( self::OPTION, $all, false ); return $id; } /** "webinar_attended" → "Webinar attended". */ public static function humanize( $key ) { $s = trim( str_replace( array( '_', '-', '.' ), ' ', (string) $key ) ); return '' === $s ? '' : mb_strtoupper( mb_substr( $s, 0, 1 ) ) . mb_substr( $s, 1 ); } /** * Record an event for a contact. * * @param int|string|array $who Contact ID, email address, or contact data ( [ 'email' => …, 'first_name' => … ] ). * @param string $name Event name ("webinar_attended"). * @param array $args { * @type string|float $value Optional value ("Villa investing 101", 49.95). * @type string $label Readable name for the event ("Attended the webinar"). * @type string $url Related page. * @type array $data Extra details (kept small). * @type string $at When it happened (GMT). Default now. * @type bool $once Only record it the first time for this contact. * @type bool $create Create the contact when the email address is new. Default false. * @type string $source Source label when a contact is created. * } * @return int Activity ID, or 0 when nothing was recorded. */ public static function track( $who, $name, array $args = array() ) { $key = self::key( $name ); if ( '' === $key ) { return 0; } $cid = self::resolve( $who, ! empty( $args['create'] ), (string) ( $args['source'] ?? '' ) ); if ( ! $cid ) { return 0; } $id = self::register( $key, (string) ( $args['label'] ?? '' ) ); if ( ! $id ) { return 0; } if ( ! empty( $args['once'] ) && self::has( $cid, $key ) ) { return 0; } $all = self::all(); $label = $all[ $key ]['label']; $value = isset( $args['value'] ) && is_scalar( $args['value'] ) ? mb_substr( sanitize_text_field( (string) $args['value'] ), 0, 190 ) : ''; $extra = isset( $args['data'] ) && is_array( $args['data'] ) ? Nodebook_Contacts::clean_meta( $args['data'] ) : array(); $at = ! empty( $args['at'] ) && strtotime( (string) $args['at'] ) ? gmdate( 'Y-m-d H:i:s', strtotime( (string) $args['at'] ) ) : current_time( 'mysql', true ); $aid = Nodebook_Activities::add( $cid, 'event', '' !== $value ? $label . ' · ' . $value : $label, array( 'object_id' => $id, 'url' => isset( $args['url'] ) ? (string) $args['url'] : '', 'user_id' => 0, 'created_at' => $at, 'data' => array_filter( array( 'event' => $key, 'value' => $value, 'details' => $extra, ) ), ) ); if ( $aid ) { $all[ $key ]['count'] = (int) $all[ $key ]['count'] + 1; $all[ $key ]['last'] = $at; update_option( self::OPTION, $all, false ); /** * Fires after a custom event is recorded. * * @param int $contact_id Contact. * @param string $key Event key. * @param string $value Value. * @param array $args Arguments passed to nodebook_track(). */ do_action( 'nodebook_event_tracked', $cid, $key, $value, $args ); } return $aid; } /** Whether a contact has this event. */ public static function has( $contact_id, $name ) { global $wpdb; $id = self::id( $name ); return $id && (bool) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM %i WHERE contact_id = %d AND type = 'event' AND object_id = %d LIMIT 1", Nodebook_Activities::table(), $contact_id, $id ) ); } /** Contact ID for an ID, email or data array. */ private static function resolve( $who, $create, $source ) { if ( is_numeric( $who ) ) { return Nodebook_Contacts::get( (int) $who ) ? (int) $who : 0; } $data = is_array( $who ) ? $who : array( 'email' => (string) $who ); if ( ! empty( $data['email'] ) ) { $c = Nodebook_Contacts::find_by_email( (string) $data['email'] ); if ( $c ) { if ( is_array( $who ) && count( $data ) > 1 ) { Nodebook_Capture::capture( $data, $source, array( 'activity' => false ) ); } return (int) $c['id']; } } if ( ! $create ) { return 0; } return Nodebook_Capture::capture( $data, '' !== $source ? $source : __( 'Event', 'nodebook' ), array( 'activity' => false ) ); } }