first-seen timestamp map. */ public const OPTION = 'aipt_funnel_milestones'; /** Seconds a record waits for another request to finish writing the option. */ private const LOCK_WAIT_SECONDS = 2; /** Events the browser may record through the REST route. */ public const CLIENT_EVENTS = array( 'gap_map_shown', 'card_pressed', 'key_drawer_opened', 'proof_generated', 'bulk_run_started', 'upgrade_pressed', 'next_step_pressed', 'upsell_shown_mcp', 'upsell_shown_after_value', 'upgrade_pressed_mcp', 'upgrade_pressed_after_value', ); /** Events only server code records. */ public const SERVER_EVENTS = array( 'key_saved', 'first_generation', 'mcp_enabled', 'mcp_first_request', ); /** * Every whitelisted event, client events first. * * @return string[] */ public static function allEvents(): array { return array_merge( self::CLIENT_EVENTS, self::SERVER_EVENTS ); } /** * Whether the browser is allowed to record this event. * * @param string $event Event name. * @return bool */ public static function isClientEvent( string $event ): bool { return in_array( $event, self::CLIENT_EVENTS, true ); } /** * Record an event the first time it is seen. * * @param string $event Whitelisted event name. * @return bool True when this call stored the first-seen timestamp, false for an unknown event, a repeat or a failed write. */ public static function record( string $event ): bool { if ( ! in_array( $event, self::allEvents(), true ) ) { return false; } if ( isset( self::all()[ $event ] ) ) { return false; } $release = self::lock(); try { wp_cache_delete( self::OPTION, 'options' ); wp_cache_delete( 'notoptions', 'options' ); $milestones = self::all(); if ( isset( $milestones[ $event ] ) ) { return false; } $milestones[ $event ] = gmdate( 'Y-m-d\TH:i:s\Z' ); return (bool) update_option( self::OPTION, $milestones, false ); } finally { if ( is_callable( $release ) ) { $release(); } } } /** * Take the milestones lock (a MySQL advisory lock, the same kind * {@see \AIPT\Api\Helpers\ProductWriteLock} uses). * * @return callable|false|null Release callback, false when another request * holds it past the wait, or null when the * database cannot take advisory locks. */ private static function lock() { global $wpdb; if ( ! isset( $wpdb ) || ! is_object( $wpdb ) || ! is_callable( array( $wpdb, 'get_var' ) ) || ! is_callable( array( $wpdb, 'prepare' ) ) ) { return null; } $name = 'aipt_funnel_' . sha1( ( defined( 'DB_NAME' ) ? \DB_NAME : '' ) . ':' . $wpdb->prefix . ':' . ( function_exists( 'get_current_blog_id' ) ? (int) \get_current_blog_id() : 0 ) ); $result = $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, %d)', $name, self::LOCK_WAIT_SECONDS ) ); if ( '1' !== (string) $result ) { return null === $result ? null : false; } return static function () use ( $wpdb, $name ): void { $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $name ) ); }; } /** * Whether the event was seen at least once. * * @param string $event Event name. * @return bool */ public static function has( string $event ): bool { return null !== self::firstSeen( $event ); } /** * First-seen timestamp of an event. * * @param string $event Event name. * @return string|null ISO 8601 UTC timestamp, or null when never seen. */ public static function firstSeen( string $event ): ?string { $milestones = self::all(); return isset( $milestones[ $event ] ) ? $milestones[ $event ] : null; } /** * Every stored milestone, restricted to whitelisted events and string values. * * @return array */ public static function all(): array { $stored = get_option( self::OPTION, array() ); if ( ! is_array( $stored ) ) { return array(); } $clean = array(); foreach ( self::allEvents() as $event ) { if ( isset( $stored[ $event ] ) && is_string( $stored[ $event ] ) && '' !== $stored[ $event ] ) { $clean[ $event ] = $stored[ $event ]; } } return $clean; } }