> <?php bloginfo('name'); ?> >
"; // Get the user ID from the page title $user_id = get_query_var('customer_id'); // Fetch settings and store data global $wpdb; $settings = $wpdb->get_row($wpdb->prepare( "SELECT * FROM stampy_settings WHERE store_id = %d", $user_id )); $store_data = get_stampy_user_data($user_id); if ($store_data) { $store_name = $store_data->store_name; $logo_url = $settings->logo_path ?? null; $stamps_needed = $settings->stamps_needed ?? 10; // Default to 10 $promotion_text = $settings->promotion_text ?? 'Collect enough stamps to earn exciting rewards!'; // Fetch the store's timezone from settings $timezone = $settings->timezone ?? 'America/Denver'; // Default to Mountain Time // Get the current date and day in the store's timezone $date_time = new DateTime('now', new DateTimeZone($timezone)); $current_date = $date_time->format('Y-m-d'); $current_day = $date_time->format('l'); // e.g., "Monday" // Check for active special days $special_day = $wpdb->get_row($wpdb->prepare( "SELECT * FROM stampy_special_days WHERE store_id = %d AND ((day_type = 'weekly' AND day_value = %s) OR (day_type = 'annual' AND SUBSTR(day_value, 6) = SUBSTR(%s, 6)) OR (day_type = 'nonrepeating' AND day_value = %s))", $user_id, $current_day, $current_date, $current_date )); $multiplier = $special_day->multiplier ?? 1; // Default to single stamps echo "
"; if ($logo_url) { echo ""; } else { echo "
$store_name
"; } echo "
"; if ($special_day) { $day_message = ($special_day->day_type === 'weekly') ? "Every $special_day->day_value" : (($special_day->day_type === 'annual') ? "Every " . (new DateTime($special_day->day_value))->format('F j') : (new DateTime($special_day->day_value))->format('F j, Y') ); // Corrected the condition to ensure only double or triple stamps are displayed properly if ($multiplier == 2) { echo "
Double Stamps today!
"; } elseif ($multiplier == 3) { echo "
Triple Stamps today!
"; } } echo "
Get $stamps_needed stamps for"; echo "$promotion_text
"; // Handle stamp interval and form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['customer_phone'])) { global $wpdb; $customer_phone = sanitize_text_field($_POST['customer_phone']); $current_time = new DateTime(); // ✅ Check if this exact phone number exists and fetch last activity $customer_data = $wpdb->get_row($wpdb->prepare( "SELECT last_activity_date, stamps FROM stampy_customers WHERE store_id = %d AND phone = %s", $user_id, $customer_phone )); $stamp_interval = $settings->stamp_interval ?? 'Unlimited'; $interval_met = false; // ✅ If the phone number doesn't exist, allow the stamp immediately if (!$customer_data) { $interval_met = true; } else { // ✅ If phone exists, check last activity against the current time $last_activity_time = new DateTime($customer_data->last_activity_date); switch ($stamp_interval) { case 'Unlimited': $interval_met = true; break; case 'Hourly': $interval_met = $current_time->diff($last_activity_time)->h >= 1; break; case 'Daily': $interval_met = $current_time->diff($last_activity_time)->d >= 1; break; case 'Weekly': $interval_met = $current_time->diff($last_activity_time)->days >= 7; break; default: $interval_met = true; // Default to allow if invalid interval } } // ✅ Check if the customer exists in the database if ($customer_data) { // Existing customer: Increment stamps only if the interval is met if ($interval_met) { $stamps_earned = $multiplier; $current_stamps = $customer_data->stamps + $stamps_earned; // Check if the reward has been earned if ($current_stamps >= $stamps_needed) { $current_stamps = 0; $wpdb->query($wpdb->prepare( "UPDATE stampy_customers SET stamp_completions = stamp_completions + 1 WHERE store_id = %d AND phone = %s", $user_id, $customer_phone )); echo "
Congratulations! You get {$settings->promotion_text}
"; } else { echo "
"; echo "
You've earned $stamps_earned " . ($stamps_earned > 1 ? "stamps" : "stamp") . "!Only " . ($stamps_needed - $current_stamps) . " more to go!
"; for ($i = 1; $i <= $stamps_needed; $i++) { if ($i <= $current_stamps) { echo "
$i $stampicon
"; } else { echo "
$i $stampicon
"; } } echo "
"; } // ✅ Update the last_activity_date and stamps $wpdb->update( "stampy_customers", [ 'stamps' => $current_stamps, 'last_activity_date' => $current_time->format('Y-m-d H:i:s') ], ['store_id' => $user_id, 'phone' => $customer_phone] ); } else { // ✅ Interval not met, show message and stamp progress $interval_message = [ 'Hourly' => 'Try again in an hour.', 'Daily' => 'Try again tomorrow.', 'Weekly' => 'Try again next week.', ][$stamp_interval] ?? 'Stamps are currently unavailable.'; echo "
"; echo "
$interval_messageOnly " . ($stamps_needed - $customer_data->stamps) . " more to go!
"; for ($i = 1; $i <= $stamps_needed; $i++) { if ($i <= $customer_data->stamps) { echo "
$i $stampicon
"; } else { echo "
$i $stampicon
"; } } echo "
"; // ✅ Show override form echo '
'; echo ''; echo ''; echo ''; echo ''; echo '
'; // ✅ Handle override if submitted if (!empty($_POST['override_pin'])) { if (password_verify($_POST['override_pin'], $settings->pin_hash)) { $interval_met = true; echo "

Override accepted. Stamp has been authorized.

"; // ✅ Force page to reprocess submission echo ""; } else { echo "

Invalid PIN. Please try again.

"; } } } } else { // ✅ New customer: Add to the database and give the first stamp $stamps_earned = $multiplier; $current_stamps = $stamps_earned; $wpdb->insert( "stampy_customers", [ 'store_id' => $user_id, 'phone' => $customer_phone, 'stamps' => $current_stamps, 'last_activity_date' => $current_time->format('Y-m-d H:i:s') ] ); // ✅ Display the first stamp visually echo "
"; echo "
You've earned $stamps_earned " . ($stamps_earned > 1 ? "stamps" : "stamp") . ".Only " . ($stamps_needed - $current_stamps) . " more to go!
"; for ($i = 1; $i <= $stamps_needed; $i++) { if ($i <= $current_stamps) { echo "
$i $stampicon
"; } else { echo "
$i $stampicon
"; } } echo "
"; } } // Display the phone number form ?>
1
2
3
4
5
6
7
8
9
Clear
0
Delete
Store information not found.

"; } ?>