ID) { return '

' . esc_html__('Sign in to see your Bonus card.', 'ai-eshop-optimizer') . '

'; } $role = self::loyalty_role(); if (in_array($role, (array) $user->roles, true)) { return $this->render_card((int) $user->ID); } if (!class_exists('AIEO_Safe_Roles') || !AIEO_Safe_Roles::is_safe($role)) { return '

' . esc_html__('The Bonus card is not available at the moment.', 'ai-eshop-optimizer') . '

'; } $enrolled = $this->maybe_handle_enroll((int) $user->ID, $role); if ($enrolled === 'enrolled') { return $this->render_card((int) $user->ID); } $result = $this->maybe_handle_submit((int) $user->ID, $role); if ($result === 'matched') { return $this->render_card((int) $user->ID); } return (is_string($enrolled) ? $enrolled : '') . (is_string($result) ? $result : '') . $this->render_form() . $this->render_enroll_cta(); } private function vitals_field($user_id, $col) { global $wpdb; $tbl = $wpdb->prefix . 'aieo_user_vitals'; return $wpdb->get_var($wpdb->prepare("SELECT `{$col}` FROM {$tbl} WHERE user_id = %d", (int) $user_id)); } private function fresh_points($loyalty_id) { $loyalty_id = trim((string) $loyalty_id); if ($loyalty_id === '') { return null; } global $wpdb; $table = self::customers_table(); if ($table === '' || !$wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table))) { return null; } return $wpdb->get_row($wpdb->prepare("SELECT BonusPoints, BonusPointsTotal FROM `{$table}` WHERE LoyaltyId = %s LIMIT 1", $loyalty_id)); } private function render_card($user_id) { $name = (string) $this->vitals_field($user_id, 'soft1_customer_name'); $lid = (string) $this->vitals_field($user_id, 'soft1_loyalty_id'); $points = (string) $this->vitals_field($user_id, 'soft1_loyalty_points'); $total = (string) $this->vitals_field($user_id, 'soft1_loyalty_points_total'); $since = (string) $this->vitals_field($user_id, 'soft1_first_transaction_date'); $fresh = $this->fresh_points($lid); if ($fresh) { if (isset($fresh->BonusPoints) && $fresh->BonusPoints !== '') { $points = (string) $fresh->BonusPoints; } if (isset($fresh->BonusPointsTotal) && $fresh->BonusPointsTotal !== '') { $total = (string) $fresh->BonusPointsTotal; } } $since_fmt = ''; if ($since !== '' && $since !== '0000-00-00 00:00:00') { $d = date_create($since); if ($d) { $since_fmt = $d->format('d-m-Y'); } } $out = '
'; $out .= '

' . esc_html__('Full name', 'ai-eshop-optimizer') . ': ' . esc_html($name) . '

'; $out .= '
' . esc_html__('Bonus Card no.', 'ai-eshop-optimizer') . ': ' . esc_html($lid) . '
'; $out .= '
' . esc_html__('Member since', 'ai-eshop-optimizer') . ': ' . esc_html($since_fmt) . '
'; $out .= '
' . esc_html__('Points', 'ai-eshop-optimizer') . ': ' . esc_html($points) . '
'; $out .= '

' . esc_html__('Total points', 'ai-eshop-optimizer') . ': ' . esc_html($total) . '

'; $out .= '
'; return $out; } private function render_form() { $out = '

' . esc_html__('Already have a Bonus card?', 'ai-eshop-optimizer') . '

'; $out .= esc_html__('Unfortunately we could not identify you from your eshop account details.', 'ai-eshop-optimizer') . '
'; $out .= esc_html__('Enter the Bonus card number and the mobile (or email) you gave when you registered in store. If they match, you will be identified straight away!', 'ai-eshop-optimizer') . '

'; $out .= '
'; $out .= wp_nonce_field('aieo_loyalty_match', 'aieo_loyalty_nonce', true, false); $out .= ''; $out .= ''; $out .= ''; $out .= '
'; $out .= '
'; return $out; } private function maybe_handle_submit($user_id, $role) { if (!isset($_POST['aieo_loyalty_id'], $_POST['aieo_loyalty_email'], $_POST['aieo_loyalty_phone'])) { return ''; } if (!isset($_POST['aieo_loyalty_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['aieo_loyalty_nonce'])), 'aieo_loyalty_match')) { return '
' . esc_html__('Session expired — please try again.', 'ai-eshop-optimizer') . '
'; } global $wpdb; $table = self::customers_table(); if ($table === '' || !$wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table))) { return '
' . esc_html__('The service is unavailable at the moment.', 'ai-eshop-optimizer') . '
'; } $lid = sanitize_text_field(wp_unslash($_POST['aieo_loyalty_id'])); $email = sanitize_email(wp_unslash($_POST['aieo_loyalty_email'])); $phone = sanitize_text_field(wp_unslash($_POST['aieo_loyalty_phone'])); $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$table}` WHERE LoyaltyId = %s AND (CustomerEmail = %s OR CustomerPhone = %s) LIMIT 1", $lid, $email, $phone)); if (!$row) { return '
' . esc_html__('No member was found with the details you gave!', 'ai-eshop-optimizer') . '
'; } $this->upsert_vitals($user_id, $row); AIEO_Safe_Roles::grant($user_id, $role, 'loyalty'); return 'matched'; } private function maybe_handle_enroll($user_id, $role) { if (!isset($_POST['aieo_loyalty_enroll'])) { return ''; } if (!isset($_POST['aieo_loyalty_enroll_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['aieo_loyalty_enroll_nonce'])), 'aieo_loyalty_enroll')) { return '
' . esc_html__('Session expired — please try again.', 'ai-eshop-optimizer') . '
'; } $card_id = $this->generate_enroll_card_id($user_id); global $wpdb; $tbl = $wpdb->prefix . 'aieo_user_vitals'; $sv = static function ($v) { return ($v !== null && $v !== '') ? (string) $v : null; }; $u = new WP_User($user_id); $name = trim((string) $u->display_name); if ($name === '') { $name = trim(trim((string) $u->first_name) . ' ' . trim((string) $u->last_name)); } $data = array( 'soft1_customer_name' => $sv($name), 'soft1_loyalty_id' => (int) $card_id, 'soft1_loyalty_points' => 0, 'soft1_loyalty_points_total' => 0, 'soft1_first_transaction_date' => current_time('mysql', true), ); if ($wpdb->get_var($wpdb->prepare("SELECT 1 FROM {$tbl} WHERE user_id = %d", (int) $user_id))) { $wpdb->update($tbl, $data, array('user_id' => (int) $user_id)); } else { $data['user_id'] = (int) $user_id; $wpdb->insert($tbl, $data); } AIEO_Safe_Roles::grant($user_id, $role, 'loyalty'); do_action('aieo_loyalty_enrolled', $user_id, $card_id, array('source' => 'self_service')); return 'enrolled'; } private function generate_enroll_card_id($user_id) { return (int) apply_filters('aieo_loyalty_enroll_card_id', (int) $user_id, $user_id); } private function render_enroll_cta() { $out = '
'; $out .= '

' . esc_html__('Not a member yet?', 'ai-eshop-optimizer') . '

'; $out .= '

' . esc_html__('Join the Bonus programme now and start collecting points with every purchase.', 'ai-eshop-optimizer') . '

'; $out .= '
'; $out .= wp_nonce_field('aieo_loyalty_enroll', 'aieo_loyalty_enroll_nonce', true, false); $out .= '
'; $out .= '
'; return $out; } private function norm_date($v) { $v = trim((string) $v); if ($v === '' || strpos($v, '0000-00-00') === 0) { return null; } $d = date_create($v); return $d ? $d->format('Y-m-d H:i:s') : null; } private function upsert_vitals($user_id, $row) { global $wpdb; $tbl = $wpdb->prefix . 'aieo_user_vitals'; $iv = static function ($v) { return ($v !== null && $v !== '') ? (int) $v : null; }; $sv = static function ($v) { return ($v !== null && $v !== '') ? (string) $v : null; }; $data = array( 'soft1_customer_name' => $sv($row->CustomerName ?? null), 'soft1_customer_id' => $iv($row->CustomerId ?? null), 'soft1_loyalty_id' => $iv($row->LoyaltyId ?? null), 'soft1_loyalty_points' => $iv($row->BonusPoints ?? null), 'soft1_loyalty_points_total' => $iv($row->BonusPointsTotal ?? null), 'soft1_latest_location' => $sv($row->CustomerLatestLocation ?? null), 'soft1_recorded_speciality' => $sv($row->CustomerRecordedSpeciality ?? null), 'soft1_latest_transaction_date' => $this->norm_date($row->CustomerLatestTransaction ?? ''), 'soft1_first_transaction_date' => $this->norm_date($row->CustomerFirstTransaction ?? ''), ); if ($wpdb->get_var($wpdb->prepare("SELECT 1 FROM {$tbl} WHERE user_id = %d", (int) $user_id))) { $wpdb->update($tbl, $data, array('user_id' => (int) $user_id)); } else { $data['user_id'] = (int) $user_id; $wpdb->insert($tbl, $data); } } } }