3 months ago (excludes never-ordered) * 6m → last order > 6 months ago (excludes never-ordered) * 1y → last order > 12 months ago (excludes never-ordered) * 1y+ → last order > 24 months ago (excludes never-ordered) * never → no qualifying order at all * * @since 1.1.0 */ class AISP_Customer_Activity_Dormant { const TRANSIENT_PREFIX = 'aisp_ca_dormant_'; const TRANSIENT_TTL = 600; public static function periods(): array { return [ '3m' => [ 'label' => __( '3 months +', 'aisp-customer-activity' ), 'months' => 3 ], '6m' => [ 'label' => __( '6 months +', 'aisp-customer-activity' ), 'months' => 6 ], '1y' => [ 'label' => __( '1 year +', 'aisp-customer-activity' ), 'months' => 12 ], 'never' => [ 'label' => __( 'Never ordered', 'aisp-customer-activity' ), 'months' => null ], ]; } // ------------------------------------------------------------------------- // Cache // ------------------------------------------------------------------------- public static function get_dormant_customers( string $period_key ): array { if ( ! array_key_exists( $period_key, self::periods() ) ) { return []; } $cache_key = self::TRANSIENT_PREFIX . sanitize_key( $period_key ); $cached = get_transient( $cache_key ); if ( false !== $cached ) { return $cached; } $results = self::query_dormant( $period_key ); set_transient( $cache_key, $results, self::TRANSIENT_TTL ); return $results; } public static function invalidate_cache() { foreach ( array_keys( self::periods() ) as $key ) { delete_transient( self::TRANSIENT_PREFIX . sanitize_key( $key ) ); } } // ------------------------------------------------------------------------- // Query // ------------------------------------------------------------------------- private static function query_dormant( string $period_key ): array { $period = self::periods()[ $period_key ]; $is_never = ( $period_key === 'never' ); $months = $period['months']; // null for never $cutoff_ts = ( ! $is_never && $months ) ? strtotime( "-{$months} months" ) : null; $tracked_roles = AISP_Customer_Activity_Settings::get_tracked_roles(); if ( empty( $tracked_roles ) ) { return []; } $users = get_users( [ 'role__in' => $tracked_roles, 'fields' => [ 'ID', 'display_name', 'user_email' ], 'number' => 2000, ] ); if ( empty( $users ) ) { return []; } $results = []; foreach ( $users as $user ) { $orders = wc_get_orders( [ 'customer_id' => (int) $user->ID, 'status' => [ 'completed', 'processing', 'on-hold' ], 'limit' => 1, 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', ] ); $last_order = ! empty( $orders ) ? $orders[0] : null; $last_order_ts = $last_order ? $last_order->get_date_created()?->getTimestamp() : null; if ( $is_never ) { // Keep only customers with no qualifying order ever. if ( $last_order_ts !== null ) { continue; } } else { // Keep only customers who HAVE ordered but not within the threshold. // Customers who never ordered go to "never" bucket only. if ( $last_order_ts === null ) { continue; } // Skip if last order is within the threshold (too recent). if ( $last_order_ts > $cutoff_ts ) { continue; } } $row = new \stdClass(); $row->ID = $user->ID; $row->display_name = $user->display_name; $row->user_email = $user->user_email; $row->first_name = get_user_meta( $user->ID, 'billing_first_name', true ); $row->last_name = get_user_meta( $user->ID, 'billing_last_name', true ); $row->company = get_user_meta( $user->ID, 'billing_company', true ); $row->phone = get_user_meta( $user->ID, 'billing_phone', true ); $row->last_order_date = $last_order_ts ? gmdate( 'Y-m-d H:i:s', $last_order_ts ) : null; $results[] = $row; } // Sort: most recent order first → oldest. Never-ordered: alphabetical. usort( $results, function ( $a, $b ) { $ta = $a->last_order_date ? strtotime( $a->last_order_date ) : 0; $tb = $b->last_order_date ? strtotime( $b->last_order_date ) : 0; if ( $ta !== $tb ) { return $tb - $ta; // DESC. } return strcmp( (string) $a->display_name, (string) $b->display_name ); } ); return $results; } // ------------------------------------------------------------------------- // Render // ------------------------------------------------------------------------- public static function render_tab() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $current = isset( $_GET['dormant_period'] ) ? sanitize_key( $_GET['dormant_period'] ) : '3m'; $periods = self::periods(); if ( ! array_key_exists( $current, $periods ) ) { $current = '3m'; } $customers = self::get_dormant_customers( $current ); $period = $periods[ $current ]; $base_url = admin_url( 'admin.php?page=aisp-customer-activity&tab=dormant&dormant_period=' ); // Description. echo '

'; if ( $current === 'never' ) { esc_html_e( 'Customers who have never placed a qualifying order.', 'aisp-customer-activity' ); } else { echo esc_html( sprintf( /* translators: %s: period label e.g. "3 months +" */ __( 'Customers whose last qualifying order was more than %s ago.', 'aisp-customer-activity' ), $period['label'] ) ); } echo '

'; // Buttons. echo '
'; echo '' . esc_html__( 'No orders since:', 'aisp-customer-activity' ) . ''; foreach ( $periods as $key => $def ) { $active = ( $key === $current ) ? ' button-primary' : ''; echo '' . esc_html( $def['label'] ) . ''; } echo '
'; // Table. if ( empty( $customers ) ) { echo '

' . esc_html__( 'No customers found for this period.', 'aisp-customer-activity' ) . '

'; return; } echo '

' . esc_html( sprintf( /* translators: %d: number of customers */ _n( '%d customer found.', '%d customers found.', count( $customers ), 'aisp-customer-activity' ), count( $customers ) ) ) . '

'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ( $customers as $c ) { self::render_row( $c ); } echo '
' . esc_html__( 'Name', 'aisp-customer-activity' ) . '' . esc_html__( 'Company', 'aisp-customer-activity' ) . '' . esc_html__( 'Phone', 'aisp-customer-activity' ) . '' . esc_html__( 'Email', 'aisp-customer-activity' ) . '' . esc_html__( 'Last Order', 'aisp-customer-activity' ) . '
'; } private static function render_row( object $c ) { $first = trim( (string) $c->first_name ); $last = trim( (string) $c->last_name ); $full_name = trim( "$first $last" ) ?: $c->display_name; $company = trim( (string) $c->company ); $phone = trim( (string) $c->phone ); $email = trim( (string) $c->user_email ); $edit_url = get_edit_user_link( (int) $c->ID ); $last_order = $c->last_order_date ? date_i18n( get_option( 'date_format' ), strtotime( $c->last_order_date ) ) : '' . esc_html__( 'Never ordered', 'aisp-customer-activity' ) . ''; echo ''; echo '' . esc_html( $full_name ) . ''; echo '' . ( $company ? esc_html( $company ) : '—' ) . ''; echo '' . ( $phone ? '📞 ' . esc_html( $phone ) . '' : '—' ) . ''; echo '' . ( $email ? '✉ ' . esc_html( $email ) . '' : '—' ) . ''; echo '' . wp_kses_post( $last_order ) . ''; echo ''; } }