0 && self::table_exists( $wpdb->prefix . 'aieo_subjects' ) ) { $uid = (int) $wpdb->get_var( $wpdb->prepare( "SELECT wp_user_id FROM `{$wpdb->prefix}aieo_subjects` WHERE subject_id = %d", (int) $subject_id ) ); } return AIEO_Automation_Test_Mode::tester_address( $uid ); } $ci = $wpdb->prefix . 'aieo_customer_intelligence'; $has_ci = self::table_exists( $ci ); $where = ''; $val = 0; $account = (int) $wp_user_id; if ( $subject_id > 0 ) { $subjects = $wpdb->prefix . 'aieo_subjects'; if ( self::table_exists( $subjects ) ) { $s = $wpdb->get_row( $wpdb->prepare( "SELECT wp_user_id, eponymous_id FROM `{$subjects}` WHERE subject_id = %d", (int) $subject_id ), ARRAY_A ); if ( $s ) { if ( (int) $s['wp_user_id'] > 0 ) { $account = (int) $s['wp_user_id']; } if ( (int) $s['eponymous_id'] > 0 ) { $where = 'eponymous_id'; $val = (int) $s['eponymous_id']; } elseif ( (int) $s['wp_user_id'] > 0 ) { $where = 'wp_user_id'; $val = (int) $s['wp_user_id']; } } } } if ( '' === $where && $wp_user_id > 0 ) { $where = 'wp_user_id'; $val = (int) $wp_user_id; } if ( '' === $where ) { return ''; } $email = ''; if ( $has_ci ) { $consent = class_exists( 'AIEO_Contact_Consent' ) ? AIEO_Contact_Consent::consent_sql( 'email' ) : '0'; $email = (string) $wpdb->get_var( $wpdb->prepare( "SELECT email FROM `{$ci}` WHERE {$where} = %d AND email <> '' AND ( tnp_status = 'C' OR {$consent} ) LIMIT 1", $val ) ); } if ( ! is_email( $email ) ) { if ( $account <= 0 || ( $has_ci && self::has_customer_row( $where, $val, $account ) ) ) { return ''; } $user = get_userdata( $account ); $email = $user ? trim( (string) $user->user_email ) : ''; if ( ! is_email( $email ) || ! self::has_marketing_consent( $email ) ) { return ''; } } if ( class_exists( 'AIEO_AW_Decommission' ) && AIEO_AW_Decommission::is_suppressed( $email ) ) { return ''; } return $email; } public static function footer_kind( array $args ) { $named = isset( $args['footer'] ) ? (string) $args['footer'] : ''; if ( 'marketing' === $named || 'transactional' === $named ) { return $named; } return ( empty( $args['marketing'] ) && (int) ( $args['user_id'] ?? 0 ) <= 0 && ! empty( $args['skip_dedup'] ) ) ? 'transactional' : 'marketing'; } public static function dispatch( array $args ) { $trigger_id = (string) ( $args['trigger_id'] ?? '' ); $user_id = (int) ( $args['user_id'] ?? 0 ); $product_id = (int) ( $args['target_product_id'] ?? 0 ); $email = (string) ( $args['email'] ?? '' ); $marketing = ! empty( $args['marketing'] ); $eponymous_id = ( $marketing && $user_id <= 0 ) ? self::eponymous_for_email( $email ) : 0; $test = class_exists( 'AIEO_Automation_Test_Mode' ) && AIEO_Automation_Test_Mode::opens( 'email' ); if ( $test && ! AIEO_Automation_Test_Mode::is_tester( $user_id, $email ) ) { return self::suppressed( $args, 'not_tester' ); } if ( ! $test && ( ! class_exists( 'AIEO_Email_Engine' ) || ! AIEO_Email_Engine::is_live() ) ) { return array( 'sent' => false, 'reason' => 'engine_paused', 'dispatch_id' => 0 ); } if ( ! $test && self::is_quiet_now() ) { return self::suppressed( $args, 'quiet_hours' ); } $cap_key = $user_id; if ( $user_id <= 0 && $marketing ) { $cap_key = ( $eponymous_id > 0 ) ? ( self::ANON_CAP_OFFSET + $eponymous_id ) : self::address_cap_key( $email ); } if ( $test ) { $cap_key = 0; } if ( $cap_key > 0 && ( self::over_cap( $cap_key, 'day' ) || self::over_cap( $cap_key, 'week' ) ) ) { return self::suppressed( $args, 'capped' ); } if ( ! $test && empty( $args['skip_dedup'] ) && self::recently_sent( $trigger_id, $product_id, $user_id, $email, self::dedup_scope( $args ) ) ) { return self::suppressed( $args, 'deduped' ); } if ( '' === $email || ! is_email( $email ) ) { return self::suppressed( $args, 'no_consented_email' ); } if ( ! $test && empty( $args['confirms_opt_out'] ) && class_exists( 'AIEO_AW_Decommission' ) && AIEO_AW_Decommission::is_suppressed( $email ) ) { return self::suppressed( $args, 'aw_unsub' ); } if ( ! $test && $marketing && $user_id <= 0 && ! self::has_marketing_consent( $email ) ) { return self::suppressed( $args, 'no_consent' ); } $dispatch_id = self::log_row( $args, 'queued', '' ); $html = (string) ( $args['html'] ?? '' ); if ( $dispatch_id > 0 && class_exists( 'AIEO_Email_Tracking' ) ) { $html = AIEO_Email_Tracking::instrument( $html, $dispatch_id ); } $send_args = array(); if ( class_exists( 'AIEO_Email_Unsubscribe' ) ) { $kind = self::footer_kind( $args ); $html = class_exists( 'AIEO_I18n' ) ? AIEO_I18n::with_locale( AIEO_I18n::recipient_locale( $user_id, $email ), function () use ( $html, $email, $kind ) { return AIEO_Email_Unsubscribe::append_footer( $html, $email, $kind ); } ) : AIEO_Email_Unsubscribe::append_footer( $html, $email, $kind ); $send_args['headers'] = array( 'List-Unsubscribe: ' . AIEO_Email_Unsubscribe::header_value( $email ), 'List-Unsubscribe-Post: List-Unsubscribe=One-Click', ); } $res = AIEO_Email_Sender::instance()->send( $email, (string) ( $args['subject'] ?? '' ), $html, $send_args ); if ( ! empty( $res['ok'] ) ) { self::mark_state( $dispatch_id, 'sent', '', (int) $res['status'] ); if ( $cap_key > 0 ) { self::bump_caps( $cap_key ); } return array( 'sent' => true, 'reason' => 'sent', 'dispatch_id' => $dispatch_id ); } self::mark_state( $dispatch_id, 'failed', (string) $res['error'], (int) $res['status'] ); return array( 'sent' => false, 'reason' => (string) $res['error'], 'dispatch_id' => $dispatch_id ); } private static function is_quiet_now() { $start = 22 * 60; $end = 9 * 60; if ( class_exists( 'AIEO_DMM_Settings' ) ) { $start = self::hhmm_to_min( (string) AIEO_DMM_Settings::get( 'contact_quiet_start', '22:00' ), $start ); $end = self::hhmm_to_min( (string) AIEO_DMM_Settings::get( 'contact_quiet_end', '09:00' ), $end ); } if ( $start === $end ) { return false; } try { $now = new \DateTime( 'now', new \DateTimeZone( 'Europe/Athens' ) ); $m = (int) $now->format( 'G' ) * 60 + (int) $now->format( 'i' ); } catch ( \Throwable $e ) { return true; } return ( $start < $end ) ? ( $m >= $start && $m < $end ) : ( $m >= $start || $m < $end ); } private static function hhmm_to_min( $hhmm, $fallback ) { if ( preg_match( '/^(\d{1,2}):(\d{2})$/', trim( (string) $hhmm ), $mm ) ) { $h = (int) $mm[1]; $i = (int) $mm[2]; if ( $h >= 0 && $h <= 23 && $i >= 0 && $i <= 59 ) { return $h * 60 + $i; } } return (int) $fallback; } private static function eponymous_for_email( $email ) { global $wpdb; $email = strtolower( trim( (string) $email ) ); if ( '' === $email || ! is_email( $email ) ) { return 0; } $ci = $wpdb->prefix . 'aieo_customer_intelligence'; if ( ! self::table_exists( $ci ) ) { return 0; } return (int) $wpdb->get_var( $wpdb->prepare( "SELECT eponymous_id FROM `{$ci}` WHERE LOWER(TRIM(email)) = %s LIMIT 1", $email ) ); } private static function address_cap_key( $email ) { $email = strtolower( trim( (string) $email ) ); if ( '' === $email || ! is_email( $email ) ) { return 0; } return self::ADDRESS_CAP_OFFSET + (int) hexdec( substr( sha1( $email ), 0, 15 ) ); } private static function has_customer_row( $where, $val, $account ) { global $wpdb; $ci = $wpdb->prefix . 'aieo_customer_intelligence'; $sql = "SELECT 1 FROM `{$ci}` WHERE {$where} = %d"; $params = array( (int) $val ); if ( $account > 0 ) { $sql .= ' OR wp_user_id = %d'; $params[] = (int) $account; } return 1 === (int) $wpdb->get_var( $wpdb->prepare( $sql . ' LIMIT 1', $params ) ); } private static function has_marketing_consent( $email ) { global $wpdb; $email = strtolower( trim( (string) $email ) ); if ( '' === $email || ! is_email( $email ) ) { return false; } if ( class_exists( 'AIEO_Contact_Consent' ) && AIEO_Contact_Consent::has_record( $email ) ) { return true; } $ci = $wpdb->prefix . 'aieo_customer_intelligence'; if ( self::table_exists( $ci ) ) { $row = (array) $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS n, COALESCE( SUM( tnp_status = 'C' ), 0 ) AS c FROM `{$ci}` WHERE LOWER(TRIM(email)) = %s", $email ), ARRAY_A ); if ( (int) ( $row['n'] ?? 0 ) > 0 ) { return (int) ( $row['c'] ?? 0 ) > 0; } } return false; } private static function cap_window( $kind ) { return 'week' === $kind ? 'e_week' : 'e_day'; } private static function over_cap( $user_id, $kind ) { global $wpdb; $tbl = $wpdb->prefix . 'aieo_push_caps'; if ( ! self::table_exists( $tbl ) ) { return false; } $start = ( 'week' === $kind ) ? self::week_start() : self::today(); $limit = ( 'week' === $kind ) ? (int) apply_filters( 'aieo_email_cap_per_week', 3 ) : (int) apply_filters( 'aieo_email_cap_per_day', 1 ); if ( $limit <= 0 ) { return false; } $sent = (int) $wpdb->get_var( $wpdb->prepare( "SELECT sent_count FROM `{$tbl}` WHERE user_id = %d AND window_kind = %s AND window_start = %s", (int) $user_id, self::cap_window( $kind ), $start ) ); return $sent >= $limit; } private static function bump_caps( $user_id ) { global $wpdb; $tbl = $wpdb->prefix . 'aieo_push_caps'; if ( ! self::table_exists( $tbl ) ) { return; } $now = gmdate( 'Y-m-d H:i:s' ); $sql = "INSERT INTO `{$tbl}` (user_id, window_kind, window_start, sent_count, updated_at) VALUES (%d, %s, %s, 1, %s) ON DUPLICATE KEY UPDATE sent_count = sent_count + 1, updated_at = VALUES(updated_at)"; $wpdb->query( $wpdb->prepare( $sql, (int) $user_id, self::cap_window( 'day' ), self::today(), $now ) ); $wpdb->query( $wpdb->prepare( $sql, (int) $user_id, self::cap_window( 'week' ), self::week_start(), $now ) ); } public static function dedup_scope( array $args ) { $secs = (int) ( $args['dedup_secs'] ?? 0 ); return array( 'step' => isset( $args['dedup_step'] ) ? max( 0, (int) $args['dedup_step'] ) : null, 'secs' => $secs > 0 ? min( $secs, self::DEDUP_DAYS * DAY_IN_SECONDS ) : self::DEDUP_DAYS * DAY_IN_SECONDS, ); } public static function offer_marks( array $args ) { $placement = (int) ( $args['placement_id'] ?? 0 ); if ( $placement <= 0 ) { return array(); } return array( 'placement_id' => $placement ) + ( empty( $args['code'] ) ? array() : array( 'code' => 1 ) ); } public static function payload_prefix( $step ) { return null === $step ? '' : '{"wf_step":' . (int) $step . ','; } private static function recently_sent( $trigger_id, $product_id, $user_id, $email = '', array $scope = array() ) { global $wpdb; if ( '' === $trigger_id ) { return false; } $tbl = $wpdb->prefix . 'aieo_push_dispatch'; if ( ! self::table_exists( $tbl ) ) { return false; } if ( $user_id > 0 ) { $key_sql = 'user_id = %d'; $key_val = (int) $user_id; } elseif ( '' !== $email ) { $key_sql = 'recipient_email = %s'; $key_val = $email; } else { return false; } $since = gmdate( 'Y-m-d H:i:s', time() - (int) ( $scope['secs'] ?? self::DEDUP_DAYS * DAY_IN_SECONDS ) ); $prefix = self::payload_prefix( $scope['step'] ?? null ); $params = array( self::CHANNEL, $trigger_id, (int) $product_id, $key_val, $since ); $step = ''; if ( '' !== $prefix ) { $step = ' AND payload_json LIKE %s'; $params[] = $wpdb->esc_like( $prefix ) . '%'; } $hit = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM `{$tbl}` WHERE channel = %s AND trigger_id = %s AND target_product_id = %d AND {$key_sql} AND state = 'sent' AND sent_at >= %s{$step}", $params ) ); return $hit > 0; } private static function log_row( array $args, $state, $suppress_reason ) { global $wpdb; $tbl = $wpdb->prefix . 'aieo_push_dispatch'; if ( ! self::table_exists( $tbl ) ) { return 0; } $now = gmdate( 'Y-m-d H:i:s' ); $payload = array( 'subject' => (string) ( $args['subject'] ?? '' ), ); $step = self::dedup_scope( $args )['step']; if ( null !== $step ) { $payload = array( 'wf_step' => $step ) + $payload; } $payload += self::offer_marks( $args ); $wpdb->insert( $tbl, array( 'channel' => self::CHANNEL, 'subscription_id' => 0, 'subject_id' => (int) ( $args['subject_id'] ?? 0 ), 'recipient_email' => (string) ( $args['email'] ?? '' ), 'user_id' => (int) ( $args['user_id'] ?? 0 ), 'trigger_id' => (string) ( $args['trigger_id'] ?? '' ), 'template_id' => (int) ( $args['template_id'] ?? 0 ), 'target_product_id' => (int) ( $args['target_product_id'] ?? 0 ), 'target_url' => (string) ( $args['target_url'] ?? '' ), 'payload_json' => wp_json_encode( $payload ), 'state' => (string) $state, 'suppress_reason' => (string) $suppress_reason, 'queued_at' => $now, 'created_at' => $now, ), array( '%s', '%d', '%d', '%s', '%d', '%s', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s' ) ); return (int) $wpdb->insert_id; } private static function mark_state( $dispatch_id, $state, $error = '', $provider_status = 0 ) { global $wpdb; $dispatch_id = (int) $dispatch_id; if ( $dispatch_id <= 0 ) { return; } $tbl = $wpdb->prefix . 'aieo_push_dispatch'; $data = array( 'state' => (string) $state, 'provider_status' => (int) $provider_status, 'provider_error' => (string) $error, ); $fmt = array( '%s', '%d', '%s' ); if ( 'sent' === $state ) { $data['sent_at'] = gmdate( 'Y-m-d H:i:s' ); $fmt[] = '%s'; } $wpdb->update( $tbl, $data, array( 'dispatch_id' => $dispatch_id ), $fmt, array( '%d' ) ); } private static function suppressed( array $args, $reason ) { $id = self::log_row( $args, 'suppressed', $reason ); return array( 'sent' => false, 'reason' => $reason, 'dispatch_id' => $id ); } private static function today() { return wp_date( 'Y-m-d' ); } private static function week_start() { $dow = (int) wp_date( 'N' ); return wp_date( 'Y-m-d', time() - ( ( $dow - 1 ) * DAY_IN_SECONDS ) ); } private static function table_exists( $table ) { global $wpdb; return (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table; } }