";
if ($logo_url) {
echo "
";
} else {
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 "$store_name
";
}
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 "";
for ($i = 1; $i <= $stamps_needed; $i++) {
if ($i <= $current_stamps) {
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 "$i $stampicon
";
} else {
echo "$i $stampicon
";
}
}
echo "";
echo "";
for ($i = 1; $i <= $stamps_needed; $i++) {
if ($i <= $customer_data->stamps) {
echo "
";
// ✅ Show override form
echo '';
// ✅ Handle override if submitted
if (!empty($_POST['override_pin'])) {
if (password_verify($_POST['override_pin'], $settings->pin_hash)) {
$interval_met = true;
echo "$i $stampicon
";
} else {
echo "$i $stampicon
";
}
}
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 "";
for ($i = 1; $i <= $stamps_needed; $i++) {
if ($i <= $current_stamps) {
echo "
";
}
}
// Display the phone number form
?>
Store information not found.";
}
?>
$i $stampicon
";
} else {
echo "$i $stampicon
";
}
}
echo "