post_title . ' has a winner'; $subject = apply_filters( 'abst_winner_email_subject', $subject, $data, $experiment ); $html = abst_render_winner_email_html( $data ); $html = apply_filters( 'abst_winner_email_html', $html, $data, $experiment ); $text = abst_render_winner_email_text( $data ); $text = apply_filters( 'abst_winner_email_text', $text, $data, $experiment ); $headers = array( 'Content-Type: text/html; charset=UTF-8' ); $headers = apply_filters( 'abst_winner_email_headers', $headers, $data, $experiment ); // Attach plain-text alternative once PHPMailer is initialised. $attach_alt_body = function( $phpmailer ) use ( $text ) { $phpmailer->AltBody = $text; }; add_action( 'phpmailer_init', $attach_alt_body ); $sent = wp_mail( $notify_to, $subject, $html, $headers ); remove_action( 'phpmailer_init', $attach_alt_body ); return $sent; } /** * Pull everything the renderer needs out of the test + observations. * * Returns an array shaped for the renderer; null if the data is too * incomplete to email about. */ function abst_build_winner_email_data( $experiment, $observations ) { if ( ! is_object( $experiment ) || empty( $observations['bt_bb_ab_stats']['best'] ) ) { return null; } $test_id = (int) $experiment->ID; $winner_key = (string) $observations['bt_bb_ab_stats']['best']; $winner_conf = isset( $observations['bt_bb_ab_stats']['probability'] ) ? (float) $observations['bt_bb_ab_stats']['probability'] : 0.0; $test_age_days = max( 1, (int) ( ( time() - get_post_time( 'U', true, $experiment ) ) / 86400 ) ); // Build a per-variation list with the fields the renderer cares about. $variations = array(); foreach ( $observations as $key => $row ) { if ( $key === 'bt_bb_ab_stats' || ! is_array( $row ) ) { continue; } $visits = isset( $row['visit'] ) ? (int) $row['visit'] : 0; $conversions = isset( $row['conversion'] ) ? (float) $row['conversion'] : 0.0; $variations[ (string) $key ] = array( 'key' => (string) $key, 'label' => abst_get_variation_label( $key, $test_id ), 'visits' => $visits, 'conversions' => $conversions, 'rate' => ( $visits > 0 ) ? ( $conversions / $visits ) : 0.0, // fraction (0..1) 'confidence' => isset( $row['probability'] ) ? (float) $row['probability'] : 0.0, ); } if ( empty( $variations ) ) { return null; } // Identify the control via the same helper the report uses. global $abst_btab; $control_key = null; if ( is_object( $abst_btab ) && method_exists( $abst_btab, 'identify_control_variation' ) ) { $control_key = $abst_btab->identify_control_variation( $variations, $experiment ); } if ( ! $control_key || ! isset( $variations[ $control_key ] ) ) { // Fall back to the first variation. $control_key = array_key_first( $variations ); } $control_rate = $variations[ $control_key ]['rate']; // Uplift vs control per variation (as percentage points, e.g. +38.9 or -14.5). // Compare via the row's own 'key' field, not the array key: PHP coerces // numeric-string array keys to ints, breaking strict comparison against // $control_key (e.g. full_page tests where keys are page IDs). foreach ( $variations as &$v ) { if ( (string) $v['key'] === (string) $control_key || $control_rate <= 0 ) { $v['uplift_vs_control'] = null; } else { $v['uplift_vs_control'] = ( ( $v['rate'] - $control_rate ) / $control_rate ) * 100.0; } } unset( $v ); // Totals. $total_visits = 0; $total_conversions = 0.0; foreach ( $variations as $v ) { $total_visits += $v['visits']; $total_conversions += $v['conversions']; } $overall_rate = ( $total_visits > 0 ) ? ( $total_conversions / $total_visits ) : 0.0; // Winner uplift and projected annual impact. $winner_uplift = 0.0; $winner_is_control = ( (string) $winner_key === (string) $control_key ); if ( ! $winner_is_control && $control_rate > 0 && isset( $variations[ $winner_key ] ) ) { $winner_uplift = ( ( $variations[ $winner_key ]['rate'] - $control_rate ) / $control_rate ) * 100.0; } $impact_kind = 'none'; // 'extra' | 'avoided' | 'none' $impact_value = 0.0; $runner_up_key = null; // Same projection as the results screen: a year of the busier arm's daily traffic. $annual_visits = 0.0; if ( isset( $variations[ $winner_key ] ) ) { $annual_visits = max( $variations[ $winner_key ]['visits'], $variations[ $control_key ]['visits'] ) / $test_age_days * 365; } if ( ! $winner_is_control && $winner_uplift > 0 && $total_visits > 0 ) { $impact_kind = 'extra'; $impact_value = round( ( $variations[ $winner_key ]['rate'] - $control_rate ) * $annual_visits ); } elseif ( $winner_is_control && $total_visits > 0 ) { // Avoided loss: pick the runner-up (highest-rate non-control variant). $runner_up_rate = -INF; foreach ( $variations as $v ) { if ( (string) $v['key'] === (string) $control_key ) continue; if ( $v['rate'] > $runner_up_rate ) { $runner_up_rate = $v['rate']; $runner_up_key = $v['key']; } } if ( $runner_up_key !== null && $control_rate > 0 && $runner_up_rate < $control_rate ) { $impact_kind = 'avoided'; $impact_value = round( ( $control_rate - $runner_up_rate ) * $annual_visits ); } } return array( 'test_id' => $test_id, 'test_name' => $experiment->post_title, 'test_age_days' => $test_age_days, 'total_visits' => $total_visits, 'total_conversions' => $total_conversions, 'overall_rate' => $overall_rate, 'variations' => $variations, 'control_key' => $control_key, 'winner_key' => $winner_key, 'winner_label' => $variations[ $winner_key ]['label'] ?? abst_get_variation_label( $winner_key, $test_id ), 'winner_conf' => $winner_conf, 'winner_is_control' => $winner_is_control, 'winner_uplift' => $winner_uplift, 'impact_kind' => $impact_kind, 'impact_value' => $impact_value, 'runner_up_key' => $runner_up_key, 'runner_up_label' => $runner_up_key ? $variations[ $runner_up_key ]['label'] : null, 'edit_url' => admin_url( 'post.php?post=' . $test_id . '&action=edit' ), 'brand_name' => 'AB Split Test', ); } /** * Render the HTML body. Inline CSS only - email clients strip