is_in_stock()) { $button_text = isset($options['button_text']) ? $options['button_text'] : __('Notify When Available', 'alertify'); ?>
exists() ? $current_user->user_email : ''; $user_name = $current_user->exists() ? $current_user->display_name : ''; ?> prefix . 'alertify_subscriptions'; // Try to get from cache first $cache_key = 'alertify_subscription_' . $product_id . '_' . md5($email); $existing = wp_cache_get($cache_key, 'alertify'); if (false === $existing) { // Cache miss - get from database // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery $existing = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}alertify_subscriptions WHERE product_id = %d AND email = %s AND status = 'pending'", $product_id, $email ) ); // Cache the result for 5 minutes wp_cache_set($cache_key, $existing, 'alertify', 5 * MINUTE_IN_SECONDS); } if ($existing) { wp_send_json_error(esc_html__('You are already subscribed to this product.', 'alertify')); return; } // Insert new subscription // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery $result = $wpdb->insert( $table_name, array( 'product_id' => $product_id, 'email' => $email, 'name' => $name, 'phone' => $phone, 'status' => 'pending', 'created_at' => current_time('mysql') ), array( '%d', // product_id '%s', // email '%s', // name '%s', // phone '%s', // status '%s' // created_at ) ); if ($result === false) { wp_send_json_error(array( 'message' => esc_html__('Unable to process your request. Please try again.', 'alertify'), 'code' => 'db_error' )); return; } wp_send_json_success(esc_html__('You will be notified when this product becomes available.', 'alertify')); } public function enqueue_dashicons() { wp_enqueue_style('dashicons'); } public function add_endpoints() { add_rewrite_endpoint('waitlist', EP_ROOT | EP_PAGES); } public function add_account_menu_item($items) { // Add new item after orders $new_items = array(); foreach ($items as $key => $item) { $new_items[$key] = $item; if ($key === 'orders') { $new_items['waitlist'] = esc_html__('Waitlist', 'alertify'); } } return $new_items; } public function endpoint_content() { global $wpdb; // Handle unsubscribe action if (isset($_POST['unsubscribe']) && isset($_POST['subscription_id'])) { check_admin_referer('alertify_unsubscribe'); $subscription_id = absint($_POST['subscription_id']); $current_user_email = wp_get_current_user()->user_email; // Try to get subscription from cache first $cache_key = 'alertify_subscription_' . $subscription_id . '_' . md5($current_user_email); $subscription = wp_cache_get($cache_key, 'alertify'); if (false === $subscription) { // Cache miss - get from database // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery $subscription = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}alertify_subscriptions WHERE id = %d AND email = %s", $subscription_id, $current_user_email )); // Cache the subscription for 5 minutes if ($subscription) { wp_cache_set($cache_key, $subscription, 'alertify', 5 * MINUTE_IN_SECONDS); } } if ($subscription) { // Delete from database // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery $wpdb->delete( $wpdb->prefix . 'alertify_subscriptions', array( 'id' => $subscription_id, 'email' => $current_user_email ), array('%d', '%s') ); // Delete all related caches wp_cache_delete('alertify_subscription_' . $subscription_id, 'alertify'); wp_cache_delete('alertify_all_subscriptions', 'alertify'); wp_cache_delete('alertify_pending_notifications', 'alertify'); wc_add_notice(__('Successfully unsubscribed from notification.', 'alertify'), 'success'); } } // Get current user's subscriptions $current_user_email = wp_get_current_user()->user_email; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery $subscriptions = $wpdb->get_results($wpdb->prepare(" SELECT s.*, p.post_title as product_name FROM {$wpdb->prefix}alertify_subscriptions s JOIN {$wpdb->posts} p ON s.product_id = p.ID WHERE s.email = %s ORDER BY s.created_at DESC ", $current_user_email)); ?>

product_name); ?> created_at))); ?> esc_html__('Waiting for stock', 'alertify'), 'notified' => esc_html__('Notified', 'alertify') ); echo esc_html($status_labels[$subscription->status] ?? $subscription->status); ?>

update( $wpdb->prefix . 'alertify_subscriptions', array('status' => 'unsubscribed'), array('email' => $email), array('%s'), array('%s') ); // Delete relevant caches wp_cache_delete('alertify_all_subscriptions', 'alertify'); wp_cache_delete('alertify_pending_notifications', 'alertify'); wp_die( esc_html__('You have been successfully unsubscribed from all notifications.', 'alertify'), esc_html__('Unsubscribed', 'alertify') ); } } } } new ALERTIFY_Frontend();