__( 'Describe a Customer', 'ai-eshop-optimizer' ), 'description' => __( 'Enter a name or email and get a plain-language dossier: what they buy, their affinities and needs, lifecycle and reactivation potential — grounded in your own data, with an honest account of what is not known.', 'ai-eshop-optimizer' ), 'bullets' => array( __( '40+ behavioural + CDP signals resolved into a legible profile', 'ai-eshop-optimizer' ), __( 'Behavioural recommendations, lookalikes and in-market intent', 'ai-eshop-optimizer' ), __( 'Pseudonymised, capability-gated, GDPR-hygienic', 'ai-eshop-optimizer' ), ), ); } public function register_hooks() { add_action( 'wp_ajax_aieo_dmm_customer_profile_describe', array( $this, 'ajax_describe' ) ); add_action( 'wp_ajax_aieo_dmm_customer_profile_narrate', array( $this, 'ajax_narrate' ) ); } public function render_subtab() { if ( ! class_exists( 'AIEO_Customer_Profile' ) ) { list( $aieo_cp_open, $aieo_cp_close ) = AIEO_DMM_UI::card( array( 'icon' => 'circle-user', 'title' => $this->get_label() ) ); echo $aieo_cp_open; echo \Rooster\Admin\Ui::notice( esc_html__( 'Customer Profile service is not loaded.', 'ai-eshop-optimizer' ), 'destructive', array( 'inline' => true ) ); echo $aieo_cp_close; return; } $this->render_template( 'customer-profile.php', array( 'nonce' => wp_create_nonce( 'aieo_dmm_nonce' ), ) ); } public function ajax_describe() { AIEO_DMM::verify_ajax_request(); $q = isset( $_POST['query'] ) ? sanitize_text_field( wp_unslash( $_POST['query'] ) ) : ''; $ids = AIEO_Customer_Profile::resolve( $q ); if ( empty( $ids['found'] ) ) { wp_send_json_success( array( 'found' => false, 'matches' => $ids['matches'] ) ); } $bundle = AIEO_Customer_Profile::assemble( $ids ); $persona = AIEO_Customer_Profile::persona( $bundle ); wp_send_json_success( array( 'found' => true, 'facts' => self::facts_html( $bundle, $persona ), 'identity' => array( 'name' => $ids['name'], 'email' => $ids['email'], 'eponymous_id' => $ids['eponymous_id'], 'wp_user_id' => $ids['wp_user_id'], 'subject_id' => $ids['subject_id'], ), 'bundle' => $bundle, 'persona' => $persona, ) ); } private static function facts_html( $bundle, $persona ) { if ( ! class_exists( '\Rooster\Admin\Ui' ) && ( ! function_exists( 'rooster_admin_builders' ) || ! rooster_admin_builders() ) ) { return array( 'persona' => '', 'facts' => '' ); } $b = json_decode( (string) wp_json_encode( $bundle ), true ); $p = json_decode( (string) wp_json_encode( $persona ), true ); $b = is_array( $b ) ? $b : array(); $ps = is_array( $p['signals'] ?? null ) ? $p['signals'] : array(); $c = is_array( $b['cdp'] ?? null ) ? $b['cdp'] : array(); $vit = is_array( $b['vitals'] ?? null ) ? $b['vitals'] : array(); $set = static function ( $v ) { return null !== $v && '' !== $v; }; $e = static function ( $v ) { return esc_html( is_scalar( $v ) ? (string) $v : '' ); }; $q = static function ( $v ) use ( $e ) { return ' ' . $e( $v ); }; $chips = static function ( $list ) use ( $e ) { if ( ! is_array( $list ) || ! $list ) { return ''; } return implode( ' ', array_map( static function ( $x ) { return \Rooster\Admin\Ui::badge( is_scalar( $x ) ? (string) $x : '', 'mono' ); }, $list ) ); }; $cur = function_exists( 'get_woocommerce_currency_symbol' ) ? html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' ) : ''; $pos = (string) get_option( 'woocommerce_currency_pos', 'left' ); $money = static function ( $v ) use ( $cur, $pos, $set ) { if ( ! $set( $v ) ) { return ''; } if ( function_exists( 'wc_price' ) ) { return trim( html_entity_decode( wp_strip_all_tags( wc_price( (float) $v ) ), ENT_QUOTES, 'UTF-8' ) ); } $n = number_format( (float) $v, 0, '.', '' ); switch ( $pos ) { case 'right': return $n . $cur; case 'left_space': return $cur . ' ' . $n; case 'right_space': return $n . ' ' . $cur; default: return $cur . $n; } }; $pair = static function ( $label, $html ) { return ( null === $html || '' === $html ) ? array( 'label' => $label, 'value' => '' ) : array( 'label' => $label, 'html' => $html ); }; $persona_facts = array(); if ( ! empty( $ps['affluence'] ) ) { $persona_facts[] = $pair( __( 'Value tier', 'ai-eshop-optimizer' ), $e( $ps['affluence']['label'] ?? '' ) . $q( '(' . ( $ps['affluence']['confidence'] ?? '' ) . ')' ) ); } if ( ! empty( $ps['profession'] ) ) { $persona_facts[] = $pair( __( 'Profession', 'ai-eshop-optimizer' ), $e( $ps['profession']['label'] ?? '' ) . $q( '(' . ( $ps['profession']['confidence'] ?? '' ) . ')' ) ); } if ( ! empty( $ps['affinity'] ) ) { $persona_facts[] = $pair( __( 'Into', 'ai-eshop-optimizer' ), $e( $ps['affinity']['top_category'] ?? '' ) . ( ! empty( $ps['affinity']['top_brand'] ) ? ' · ' . $e( $ps['affinity']['top_brand'] ) : '' ) ); if ( ! empty( $ps['affinity']['needs'] ) ) { $persona_facts[] = $pair( __( 'Needs', 'ai-eshop-optimizer' ), $chips( $ps['affinity']['needs'] ) ); } } if ( ! empty( $ps['priorities'] ) ) { $persona_facts[] = $pair( __( 'Top priority', 'ai-eshop-optimizer' ), $e( $ps['priorities']['top'] ?? '' ) . ( isset( $ps['priorities']['confidence'] ) ? $q( '(' . $ps['priorities']['confidence'] . ')' ) : '' ) ); } if ( ! empty( $ps['lifecycle'] ) ) { $persona_facts[] = $pair( __( 'Lifecycle', 'ai-eshop-optimizer' ), $chips( $ps['lifecycle']['segments'] ?? array() ) . ( ! empty( $ps['lifecycle']['dormancy'] ) ? $q( $ps['lifecycle']['dormancy'] ) : '' ) ); } $dob = is_array( $b['birthday'] ?? null ) ? $b['birthday'] : array(); $day = ''; if ( preg_match( '/^(\d{2})-(\d{2})$/', (string) ( $dob['md'] ?? '' ), $md ) ) { $day = wp_date( 'j F', (int) gmmktime( 12, 0, 0, (int) $md[1], (int) $md[2], 2000 ), new DateTimeZone( 'UTC' ) ); } if ( ! empty( $ps['age'] ) ) { $born = '' !== $day ? sprintf( __( 'birthday %s', 'ai-eshop-optimizer' ), $day ) : ''; $persona_facts[] = $pair( _x( 'Age', 'a person\'s age in years', 'ai-eshop-optimizer' ), $e( $ps['age']['years'] ?? '' ) . ( '' !== $born ? $q( '(' . $born . ')' ) : '' ) ); } elseif ( '' !== $day ) { $persona_facts[] = $pair( __( 'Birthday', 'ai-eshop-optimizer' ), $e( $day ) ); } $facts = array(); $facts[] = $pair( __( 'Orders / LTV / AOV', 'ai-eshop-optimizer' ), $e( $c['orders_count'] ?? '' ) . ' · ' . $e( $money( $c['ltv_total'] ?? null ) ) . ' · ' . $e( $money( $c['aov'] ?? null ) ) ); $facts[] = $pair( __( 'Recency', 'ai-eshop-optimizer' ), isset( $c['recency_days'] ) ? $e( $c['recency_days'] ) . ' ' . esc_html__( 'days', 'ai-eshop-optimizer' ) : null ); $facts[] = $pair( __( 'RFM cell', 'ai-eshop-optimizer' ), $e( $c['rfm_cell'] ?? '' ) ); $facts[] = $pair( __( 'Discount share', 'ai-eshop-optimizer' ), isset( $c['discount_share'] ) ? round( (float) $c['discount_share'] * 100 ) . '%' : null ); $facts[] = $pair( __( 'Replenishment cadence', 'ai-eshop-optimizer' ), ! empty( $c['replenishment_json'] ) ? $e( $c['replenishment_json'] ) : null ); $facts[] = $pair( __( 'Email (TNP)', 'ai-eshop-optimizer' ), $e( $c['tnp_status'] ?? '' ) . ( isset( $c['open_rate_lifetime'] ) ? ' · ' . esc_html__( 'open', 'ai-eshop-optimizer' ) . ' ' . round( (float) $c['open_rate_lifetime'] * 100 ) . '%' : '' ) ); if ( ! empty( $vit['soft1_recorded_speciality'] ) ) { $facts[] = $pair( __( 'ERP speciality', 'ai-eshop-optimizer' ), $e( $vit['soft1_recorded_speciality'] ) ); } if ( ! empty( $vit['soft1_turnover'] ) ) { $facts[] = $pair( __( 'ERP turnover', 'ai-eshop-optimizer' ), $e( $money( $vit['soft1_turnover'] ) ) ); } if ( isset( $b['behaviour']['return_risk_band'] ) ) { $facts[] = $pair( __( 'Return-risk band', 'ai-eshop-optimizer' ), $e( $b['behaviour']['return_risk_band'] ) . ' / 2' ); } return array( 'persona' => $persona_facts ? \Rooster\Admin\Ui::facts( $persona_facts ) : '
' . esc_html__( 'No inferred signals for this customer yet.', 'ai-eshop-optimizer' ) . '
', 'facts' => \Rooster\Admin\Ui::facts( $facts ), ); } public function ajax_narrate() { AIEO_DMM::verify_ajax_request(); if ( ! self::is_aieo_pro_user() ) { wp_send_json_error( array( 'message' => __( 'This feature requires the Pro version.', 'ai-eshop-optimizer' ) ) ); } $q = isset( $_POST['query'] ) ? sanitize_text_field( wp_unslash( $_POST['query'] ) ) : ''; $ids = AIEO_Customer_Profile::resolve( $q ); if ( empty( $ids['found'] ) ) { wp_send_json_error( array( 'message' => __( 'Customer not found.', 'ai-eshop-optimizer' ) ) ); } $bundle = AIEO_Customer_Profile::assemble( $ids ); $persona = AIEO_Customer_Profile::persona( $bundle ); $res = AIEO_Customer_Profile::narrate( $bundle, $persona ); if ( ! empty( $res['ok'] ) ) { wp_send_json_success( array( 'text' => $res['text'] ) ); } wp_send_json_error( array( 'message' => (string) $res['error'] ) ); } }