' . esc_html__( '92 Notify requires WooCommerce to be installed and active. Its features are currently disabled.', '92-notify-for-woocommerce' ) . '

'; } /** * The data layer (subscriber table, unsubscribe links, privacy exporter/ * eraser, CSV export, Settings/Subscribers admin screens) has no WooCommerce * dependency of its own, so it stays available even if WooCommerce is * temporarily deactivated — an admin can still view, export, or delete * subscriber data without it. */ require_once NINETYTWO_NOTIFY_PLUGIN_DIR . 'inc/db.php'; require_once NINETYTWO_NOTIFY_PLUGIN_DIR . 'inc/unsubscribe.php'; require_once NINETYTWO_NOTIFY_PLUGIN_DIR . 'inc/emails.php'; require_once NINETYTWO_NOTIFY_PLUGIN_DIR . 'inc/privacy.php'; require_once NINETYTWO_NOTIFY_PLUGIN_DIR . 'inc/csv-export.php'; require_once NINETYTWO_NOTIFY_PLUGIN_DIR . 'inc/settings.php'; /** * Loads the WooCommerce-dependent parts of the plugin (frontend form, * product/stock hooks) once we're certain WooCommerce is actually active. * * This intentionally runs on `plugins_loaded` rather than checking * `class_exists( 'WooCommerce' )` at the top level of this file: WordPress's * "Requires Plugins" header does not reliably guarantee load order for * plugins outside WordPress.org, so checking too early can see WooCommerce * as "not yet loaded" even when it's active, silently skipping these hooks * for the entire request. `plugins_loaded` fires only after every active * plugin's main file has finished loading, so this check is reliable. */ function ninetytwo_notify_init_woocommerce_dependent_features() { if ( ! class_exists( 'WooCommerce' ) ) { add_action( 'admin_notices', 'ninetytwo_notify_woocommerce_missing_notice' ); return; } require_once NINETYTWO_NOTIFY_PLUGIN_DIR . 'inc/notifier-form.php'; require_once NINETYTWO_NOTIFY_PLUGIN_DIR . 'inc/admin/admin.php'; } add_action( 'plugins_loaded', 'ninetytwo_notify_init_woocommerce_dependent_features' ); /** * Declares compatibility with WooCommerce's High-Performance Order Storage. * This plugin never reads or writes order data, so HPOS's storage location * for orders has no effect on it either way. Registered unconditionally (not * inside the plugins_loaded check above) because WooCommerce can fire * `before_woocommerce_init` before `plugins_loaded`. */ add_action( 'before_woocommerce_init', function () { if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', NINETYTWO_NOTIFY_PLUGIN_FILE, true ); } } ); /** * Enqueues the frontend form's assets only on WooCommerce pages (single * product, shop, category/tag archives) — the only templates where the * notification form can render — instead of on every page of the site. */ function ninetytwo_notify_plugin_scripts() { if ( ! function_exists( 'is_woocommerce' ) || ! is_woocommerce() ) { return; } $style_path = NINETYTWO_NOTIFY_PLUGIN_DIR . 'assets/css/style.css'; $script_path = NINETYTWO_NOTIFY_PLUGIN_DIR . 'assets/js/submit.js'; wp_enqueue_style( 'ninetytwo-notify-css', NINETYTWO_NOTIFY_PLUGIN_URL . 'assets/css/style.css', array(), file_exists( $style_path ) ? filemtime( $style_path ) : false ); wp_enqueue_script( 'ninetytwo-notify-submit-js', NINETYTWO_NOTIFY_PLUGIN_URL . 'assets/js/submit.js', array( 'jquery' ), file_exists( $script_path ) ? filemtime( $script_path ) : false, true ); wp_localize_script( 'ninetytwo-notify-submit-js', 'ninetyTwoNotifyForm', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'ninetytwo_notify_subscribe' ), 'i18n' => array( 'genericError' => __( 'Something went wrong. Please try again.', '92-notify-for-woocommerce' ), 'consentError' => __( 'Please agree to receive availability emails before subscribing.', '92-notify-for-woocommerce' ), 'emailError' => __( 'Please enter a valid email address.', '92-notify-for-woocommerce' ), 'requestError' => __( 'An error occurred during the request. Please try again.', '92-notify-for-woocommerce' ), 'sending' => __( 'Sending…', '92-notify-for-woocommerce' ), ), ) ); } add_action( 'wp_enqueue_scripts', 'ninetytwo_notify_plugin_scripts' ); /** * Handles the public subscribe form submission (guests and logged-in users). */ function ninetytwo_notify_submit_form() { check_ajax_referer( 'ninetytwo_notify_subscribe', 'nonce' ); global $wpdb; $table_name = $wpdb->prefix . 'ninetytwo_notify_subscribers'; // Honeypot: a hidden field real visitors never fill in. Bots that fill // every field get a generic success response, but nothing is stored. if ( ! empty( $_POST['ninetytwo_notify_hp_field'] ) ) { wp_send_json_success( ninetytwo_notify_get_subscribe_success_message() ); } if ( ninetytwo_notify_is_subscribe_rate_limited() ) { wp_send_json_error( __( 'Too many requests. Please try again shortly.', '92-notify-for-woocommerce' ) ); } $email = sanitize_email( wp_unslash( $_POST['email'] ?? '' ) ); $product_id = absint( $_POST['product_id'] ?? 0 ); $variation_id = absint( $_POST['variation_id'] ?? 0 ); $product_name = sanitize_text_field( wp_unslash( $_POST['product_name'] ?? '' ) ); $product_url = esc_url_raw( wp_unslash( $_POST['product_url'] ?? '' ) ); $tags = mb_substr( sanitize_text_field( wp_unslash( $_POST['tags'] ?? '' ) ), 0, 255 ); $varieties = mb_substr( sanitize_text_field( wp_unslash( $_POST['varieties'] ?? '' ) ), 0, 100 ); $is_agree = ! empty( $_POST['isAgree'] ); if ( ! $is_agree ) { wp_send_json_error( __( 'You must agree to receive availability emails to subscribe.', '92-notify-for-woocommerce' ) ); } if ( empty( $email ) || ! is_email( $email ) ) { wp_send_json_error( __( 'Please enter a valid email address.', '92-notify-for-woocommerce' ) ); } if ( ! $product_id ) { wp_send_json_error( __( 'Something went wrong. Please refresh the page and try again.', '92-notify-for-woocommerce' ) ); } $existing = $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE email = %s AND product_id = %d AND variation_id = %d', $table_name, $email, $product_id, $variation_id ) ); if ( $existing ) { wp_send_json_error( __( 'This email address is already subscribed for this product.', '92-notify-for-woocommerce' ) ); } $result = $wpdb->insert( $table_name, array( 'email' => $email, 'product_id' => $product_id, 'variation_id' => $variation_id, 'product_name' => $product_name, 'product_url' => $product_url, 'tags' => $tags, 'varieties' => $varieties, 'unsubscribe_token' => ninetytwo_notify_generate_unsubscribe_token(), 'created_at' => current_time( 'mysql' ), ), array( '%s', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s' ) ); if ( false === $result ) { wp_send_json_error( __( 'We could not save your subscription. Please try again.', '92-notify-for-woocommerce' ) ); } wp_send_json_success( ninetytwo_notify_get_subscribe_success_message() ); } add_action( 'wp_ajax_ninetytwo_notify_submit_form', 'ninetytwo_notify_submit_form' ); add_action( 'wp_ajax_nopriv_ninetytwo_notify_submit_form', 'ninetytwo_notify_submit_form' ); /** * Builds the success message shown after a valid subscription. * * @return string */ function ninetytwo_notify_get_subscribe_success_message() { /* translators: %s: store name */ return sprintf( __( 'You have successfully subscribed to receive availability alerts from %s.', '92-notify-for-woocommerce' ), get_bloginfo( 'name' ) ); } /** * Lightweight, non-invasive abuse protection for the public subscribe form: * a per-IP submission cap held only in a short-lived transient (never * written to the database or logged), generous enough that legitimate users * behind a shared/office IP are not blocked. * * @return bool */ function ninetytwo_notify_is_subscribe_rate_limited() { $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; if ( empty( $ip ) ) { return false; } $key = 'ninetytwo_notify_rl_' . md5( $ip . wp_salt() ); $count = (int) get_transient( $key ); if ( $count >= 20 ) { return true; } set_transient( $key, $count + 1, 10 * MINUTE_IN_SECONDS ); return false; }