get_user_id(); if ( ! $user_id ) { return $headers; } // Only add CC to emails sent to the customer (never to admin notifications // such as new_order, cancelled_order or failed_order). if ( ! self::is_customer_email( $email_id, $email ) ) { return $headers; } // Only send CC for users whose role is selected in the settings. $user = get_userdata( $user_id ); if ( ! AISP_CC_Email_User_Fields::user_has_allowed_role( $user ) ) { return $headers; } $cc1_raw = get_user_meta( $user_id, 'cc_email_1', true ); $statuses1_raw = get_user_meta( $user_id, 'cc_email_1_statuses', true ); $cc2_raw = get_user_meta( $user_id, 'cc_email_2', true ); $statuses2_raw = get_user_meta( $user_id, 'cc_email_2_statuses', true ); $cc1 = sanitize_email( $cc1_raw ); $cc2 = sanitize_email( $cc2_raw ); $statuses1 = is_array( $statuses1_raw ) ? array_map( 'sanitize_text_field', $statuses1_raw ) : array(); $statuses2 = is_array( $statuses2_raw ) ? array_map( 'sanitize_text_field', $statuses2_raw ) : array(); $current_status = 'wc-' . sanitize_text_field( $order->get_status() ); // ===================================================== // CC EMAIL 1 (indépendant de CC2) // ===================================================== if ( ! empty( $cc1 ) && filter_var( $cc1, FILTER_VALIDATE_EMAIL ) ) { if ( in_array( $current_status, $statuses1, true ) ) { $headers .= 'Cc: ' . $cc1 . "\r\n"; } } // ===================================================== // CC EMAIL 2 (indépendant de CC1 — NOUVELLE LOGIQUE) // ===================================================== if ( ! empty( $cc2 ) && filter_var( $cc2, FILTER_VALIDATE_EMAIL ) ) { if ( in_array( $current_status, $statuses2, true ) ) { $headers .= 'Cc: ' . $cc2 . "\r\n"; } } return $headers; } /** * Check whether a WooCommerce email is sent to the customer. * * @param string $email_id WooCommerce email ID. * @param WC_Email|null $email Email object, if available. * @return bool */ private static function is_customer_email( $email_id, $email = null ) { if ( $email instanceof WC_Email && method_exists( $email, 'is_customer_email' ) ) { return (bool) $email->is_customer_email(); } // Fallback: WooCommerce customer email IDs start with "customer_". return 0 === strpos( (string) $email_id, 'customer_' ); } }