post_content, 'hyperdrop' ) || has_shortcode( $post->post_content, 'zxd_drop_form' ) ) ) { nocache_headers(); } } } /* ------------------------------------------------------------------ * Form nonces * ------------------------------------------------------------------ */ /** * Nonce (and return address) for the shopper forms. */ public static function nonce_field() { wp_nonce_field( 'hyperdrop_front', 'hyperdrop_nonce' ); } /** * The form nonce was missing or expired: tell the shopper and send them back * to the page they came from. No form data is read. */ protected static function nonce_failed() { if ( function_exists( 'wc_add_notice' ) ) { // Guests need a WooCommerce session for the message to survive the redirect. if ( WC()->session && ! WC()->session->has_session() ) { WC()->session->set_customer_session_cookie( true ); } wc_add_notice( __( 'Your session expired. Please try again.', 'hyperdrop' ), 'error' ); } $back = wp_get_referer(); wp_safe_redirect( $back ? $back : home_url( '/' ) ); exit; } /* ------------------------------------------------------------------ * "Back to the product" after unlocking a collection * ------------------------------------------------------------------ */ /** * Add the product a shopper came from to a collection link, with a nonce. * * @param string $url Collection (gate) URL. * @param int $product_id Product ID. * @return string */ public static function with_from( $url, $product_id ) { $product_id = absint( $product_id ); if ( ! $product_id ) { return $url; } return add_query_arg( array( 'hyperdrop_from' => $product_id, '_hdfrom' => wp_create_nonce( 'hyperdrop_from_' . $product_id ), ), $url ); } /** * The product page to return to, from a nonce-checked ?hyperdrop_from=ID, or ''. * * @return string */ public static function from_product_url() { if ( ! isset( $_GET['hyperdrop_from'], $_GET['_hdfrom'] ) ) { return ''; } $from = absint( $_GET['hyperdrop_from'] ); if ( ! $from || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_hdfrom'] ) ), 'hyperdrop_from_' . $from ) ) { return ''; } return 'product' === get_post_type( $from ) ? (string) get_permalink( $from ) : ''; } /* ------------------------------------------------------------------ * Request handling (unlock, code request, magic link) * ------------------------------------------------------------------ */ /** * Shopper forms: enter a code (unlock) or ask for one by email (request). * Every form posts a nonce, checked before any other input is read. * Emailed unlock links fill in and submit the same form (see hyperdrop.js). */ public static function handle_requests() { if ( ! isset( $_POST['hyperdrop_action'], $_POST['hyperdrop_nonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['hyperdrop_nonce'] ) ), 'hyperdrop_front' ) ) { self::nonce_failed(); } $action = sanitize_key( wp_unslash( $_POST['hyperdrop_action'] ) ); $term_id = isset( $_POST['hyperdrop_term'] ) ? absint( $_POST['hyperdrop_term'] ) : 0; $product = isset( $_POST['hyperdrop_product'] ) ? absint( $_POST['hyperdrop_product'] ) : 0; $from = isset( $_POST['hyperdrop_from'] ) ? absint( $_POST['hyperdrop_from'] ) : 0; $code = isset( $_POST['hyperdrop_code'] ) ? sanitize_text_field( wp_unslash( $_POST['hyperdrop_code'] ) ) : ''; $email = isset( $_POST['hyperdrop_email'] ) ? sanitize_email( wp_unslash( $_POST['hyperdrop_email'] ) ) : ''; $bot = ! empty( $_POST['hyperdrop_website'] ); // Honeypot field, hidden from people. // Return address after unlocking a collection: the product the shopper came from, built here, never taken from a URL. $return = ''; if ( $from && 'product' === get_post_type( $from ) ) { $return = (string) get_permalink( $from ); } if ( $term_id && in_array( $action, array( 'unlock_term', 'request_term' ), true ) ) { if ( 'unlock_term' === $action ) { self::try_unlock_term( $term_id, $code, $return ); } else { if ( $bot ) { self::back_to_term( $term_id, $return ); } self::request_term_code( $term_id, $email, $return ); } return; } if ( ! $product ) { return; } if ( 'unlock' === $action ) { self::try_unlock( $product, $code ); } elseif ( 'request' === $action ) { if ( $bot ) { self::back( $product ); } self::request_code( $product, $email ); } } protected static function try_unlock( $product_id, $code ) { $product_id = HyperDrop_Drop::root_id( $product_id ); if ( ! $product_id ) { return; } if ( HyperDrop_Drop::blocked_by_test_mode( $product_id ) ) { wc_add_notice( __( 'That code is not valid.', 'hyperdrop' ), 'error' ); wp_safe_redirect( home_url( '/' ) ); exit; } if ( ! self::rate_ok( 'unlock', HyperDrop_Limits::global_value( 'unlock_attempts' ) ) ) { wc_add_notice( __( 'Too many attempts. Please wait a few minutes and try again.', 'hyperdrop' ), 'error' ); self::back( $product_id ); } $phase = HyperDrop_Schedule::phase( 'p:' . $product_id ); if ( 'ended' === $phase ) { wc_add_notice( __( 'This drop has ended.', 'hyperdrop' ), 'error' ); self::back( $product_id ); } $row = HyperDrop_Codes::find( $code ); $check = HyperDrop_Codes::check( $row, $product_id ); if ( is_wp_error( $check ) ) { wc_add_notice( $check->get_error_message(), 'error' ); self::back( $product_id ); } HyperDrop_Drop::unlock( $product_id, $row->id ); do_action( 'hyperdrop_unlocked', $product_id, $row ); if ( 'upcoming' === $phase ) { /* translators: %s: date and time */ wc_add_notice( sprintf( __( "Code saved! You'll get in the moment the drop opens on %s.", 'hyperdrop' ), HyperDrop_Schedule::fmt( HyperDrop_Schedule::get( 'p:' . $product_id )['start'] ) ), 'success' ); self::back( $product_id ); } wc_add_notice( apply_filters( 'hyperdrop_unlocked_message', __( 'Unlocked! You now have access to this drop.', 'hyperdrop' ), $row, 'p:' . $product_id ), 'success' ); self::back( $product_id, true ); } protected static function request_code( $product_id, $email ) { $product_id = HyperDrop_Drop::root_id( $product_id ); if ( HyperDrop_Drop::blocked_by_test_mode( $product_id ) ) { wp_safe_redirect( home_url( '/' ) ); exit; } $s = HyperDrop_Drop::settings( $product_id ); if ( ! $product_id || ! HyperDrop_Drop::is_product_gated( $product_id ) || 'off' === $s['signup'] ) { self::back( $product_id ); } if ( ! is_email( $email ) ) { wc_add_notice( __( 'Please enter a valid email address.', 'hyperdrop' ), 'error' ); self::back( $product_id ); } if ( ! self::rate_ok( 'request', HyperDrop_Limits::global_value( 'requests' ) ) ) { wc_add_notice( __( 'Too many requests. Please wait a few minutes and try again.', 'hyperdrop' ), 'error' ); self::back( $product_id ); } if ( 'ended' === HyperDrop_Schedule::phase( 'p:' . $product_id ) ) { wc_add_notice( __( 'This drop has ended.', 'hyperdrop' ), 'error' ); self::back( $product_id ); } $existing = HyperDrop_Codes::find_for_email( $email, $product_id ); $limit = HyperDrop_Limits::email_error( 'p:' . $product_id, $email, $existing ); if ( $limit ) { wc_add_notice( $limit, 'error' ); self::back( $product_id ); } // Other sign-up modes (added by add-ons, e.g. a waitlist) handle the request themselves. if ( 'auto' !== $s['signup'] ) { do_action( 'hyperdrop_signup_request', $s['signup'], 'p:' . $product_id, $email, $existing ); self::back( $product_id ); } // Instant mode. $row = $existing; if ( ! $row || 'disabled' === $row->status ) { $row = HyperDrop_Codes::create( array( 'product_id' => $product_id, 'email' => $email, 'status' => 'active', 'source' => 'signup', 'max_uses' => HyperDrop_Limits::get( 'p:' . $product_id )['code_uses'], 'expires_at' => HyperDrop_Limits::signup_expiry(), ) ); } if ( is_wp_error( $row ) || ! $row ) { wc_add_notice( __( 'Something went wrong. Please try again.', 'hyperdrop' ), 'error' ); self::back( $product_id ); } if ( (int) $row->max_uses > 0 && (int) $row->uses >= (int) $row->max_uses ) { wc_add_notice( __( 'The code for this email has already been used.', 'hyperdrop' ), 'error' ); self::back( $product_id ); } HyperDrop_Mailer::send_code( $row ); wc_add_notice( __( 'Check your inbox — we just emailed your access code.', 'hyperdrop' ), 'success' ); self::back( $product_id ); } /* ------------------------------------------------------------------ * Protected categories * ------------------------------------------------------------------ */ protected static function try_unlock_term( $term_id, $code, $return ) { if ( ! HyperDrop_Drop::can_test() && HyperDrop_Drop::testing_term_for( $term_id ) ) { wc_add_notice( __( 'That code is not valid.', 'hyperdrop' ), 'error' ); wp_safe_redirect( home_url( '/' ) ); exit; } $g = HyperDrop_Drop::gating_term_for( $term_id ); if ( ! $g ) { self::back_to_term( $term_id, $return ); // Not locked (anymore) — just go there. } if ( ! self::rate_ok( 'unlock', HyperDrop_Limits::global_value( 'unlock_attempts' ) ) ) { wc_add_notice( __( 'Too many attempts. Please wait a few minutes and try again.', 'hyperdrop' ), 'error' ); self::back_to_term( $g, $return, true ); } $phase = HyperDrop_Schedule::phase( 't:' . $g ); if ( 'ended' === $phase ) { wc_add_notice( __( 'This drop has ended.', 'hyperdrop' ), 'error' ); self::back_to_term( $g, $return, true ); } $row = HyperDrop_Codes::find( $code ); // Re-entering a code this visitor already activated doesn't use up another activation. $already = HyperDrop_Drop::unlocked_terms(); $repeat = $row && isset( $already[ $g ] ) && (int) $already[ $g ] === (int) $row->id; $check = HyperDrop_Codes::check_term( $row, $g, ! $repeat ); if ( is_wp_error( $check ) ) { wc_add_notice( $check->get_error_message(), 'error' ); self::back_to_term( $g, $return, true ); } if ( ! $repeat ) { HyperDrop_Codes::add_activation( $row->id ); } HyperDrop_Drop::unlock_term( $g, $row->id ); do_action( 'hyperdrop_term_unlocked', $g, $row ); if ( 'upcoming' === $phase ) { /* translators: %s: date and time */ wc_add_notice( sprintf( __( "Code saved! You'll get in the moment the drop opens on %s.", 'hyperdrop' ), HyperDrop_Schedule::fmt( HyperDrop_Schedule::get( 't:' . $g )['start'] ) ), 'success' ); self::back_to_term( $g, $return, true ); } wc_add_notice( apply_filters( 'hyperdrop_unlocked_message', __( 'Unlocked! Welcome to the exclusive collection.', 'hyperdrop' ), $row, 't:' . $g ), 'success' ); self::back_to_term( $g, $return ); } protected static function request_term_code( $term_id, $email, $return ) { if ( ! HyperDrop_Drop::can_test() && HyperDrop_Drop::testing_term_for( $term_id ) ) { wp_safe_redirect( home_url( '/' ) ); exit; } $g = HyperDrop_Drop::gating_term_for( $term_id ); if ( ! $g ) { self::back_to_term( $term_id, $return ); } $s = HyperDrop_Drop::term_settings( $g ); if ( 'off' === $s['signup'] ) { self::back_to_term( $g, $return, true ); } if ( ! is_email( $email ) ) { wc_add_notice( __( 'Please enter a valid email address.', 'hyperdrop' ), 'error' ); self::back_to_term( $g, $return, true ); } if ( ! self::rate_ok( 'request', HyperDrop_Limits::global_value( 'requests' ) ) ) { wc_add_notice( __( 'Too many requests. Please wait a few minutes and try again.', 'hyperdrop' ), 'error' ); self::back_to_term( $g, $return, true ); } if ( 'ended' === HyperDrop_Schedule::phase( 't:' . $g ) ) { wc_add_notice( __( 'This drop has ended.', 'hyperdrop' ), 'error' ); self::back_to_term( $g, $return, true ); } $existing = HyperDrop_Codes::find_for_email( $email, 0, $g ); $limit = HyperDrop_Limits::email_error( 't:' . $g, $email, $existing ); if ( $limit ) { wc_add_notice( $limit, 'error' ); self::back_to_term( $g, $return, true ); } // Other sign-up modes (added by add-ons, e.g. a waitlist) handle the request themselves. if ( 'auto' !== $s['signup'] ) { do_action( 'hyperdrop_signup_request', $s['signup'], 't:' . $g, $email, $existing ); self::back_to_term( $g, $return, true ); } $row = $existing; if ( ! $row || 'disabled' === $row->status ) { $row = HyperDrop_Codes::create( array( 'term_id' => $g, 'email' => $email, 'status' => 'active', 'source' => 'signup', 'max_uses' => HyperDrop_Limits::get( 't:' . $g )['activations'], 'expires_at' => HyperDrop_Limits::signup_expiry(), ) ); } if ( ! $row || is_wp_error( $row ) ) { wc_add_notice( __( 'Something went wrong. Please try again.', 'hyperdrop' ), 'error' ); self::back_to_term( $g, $return, true ); } HyperDrop_Mailer::send_code( $row ); wc_add_notice( __( 'Check your inbox — we just emailed your access code.', 'hyperdrop' ), 'success' ); self::back_to_term( $g, $return, true ); } /** * Redirect to the category (or the product they came from). * * @param bool $stay_on_gate Keep the return URL attached so the gate can send them on after unlocking. */ protected static function back_to_term( $term_id, $return = '', $stay_on_gate = false ) { if ( WC()->session && ! WC()->session->has_session() ) { WC()->session->set_customer_session_cookie( true ); } $return = $return ? wp_validate_redirect( $return, '' ) : ''; $link = get_term_link( (int) $term_id, 'product_cat' ); $link = is_wp_error( $link ) ? home_url( '/' ) : $link; // Preview collections: the product page is public, so stay on it (errors show there). if ( $stay_on_gate && $return && ! HyperDrop_Drop::is_hidden_mode( $term_id ) ) { $stay_on_gate = false; } if ( $stay_on_gate ) { $url = $return ? self::with_from( $link, url_to_postid( $return ) ) : $link; } else { $url = $return ? $return : $link; } wp_safe_redirect( add_query_arg( 'hyperdrop', time(), $url ) ); exit; } /** * Schedule line(s) for a locked box: coming-soon countdown, "ends in", public release, or ended. */ public static function schedule_html( $scope, $saved = false ) { $phase = HyperDrop_Schedule::phase( $scope ); $sch = HyperDrop_Schedule::get( $scope ); $out = ''; if ( 'ended' === $phase ) { /* translators: %s: date */ return '

' . esc_html( sprintf( __( 'This drop ended on %s. Thanks to everyone who joined!', 'hyperdrop' ), HyperDrop_Schedule::fmt( $sch['end'] ) ) ) . '

'; } if ( 'upcoming' === $phase ) { /* translators: %s: date and time */ $out .= '

' . esc_html( sprintf( __( 'Drops %s', 'hyperdrop' ), HyperDrop_Schedule::fmt( $sch['start'] ) ) ) . '

'; $out .= HyperDrop_Schedule::countdown( $sch['start'], __( 'Starts in', 'hyperdrop' ) ); if ( $saved ) { $out .= '

✓ ' . esc_html__( "Your code is saved — you'll get in the moment it opens. Keep this page handy!", 'hyperdrop' ) . '

'; } else { $out .= '

' . esc_html__( 'Have a code already? Enter it now and you\'re in as soon as it opens.', 'hyperdrop' ) . '

'; } return $out; } if ( $sch['end'] ) { $out .= HyperDrop_Schedule::countdown( $sch['end'], __( 'Ends in', 'hyperdrop' ) ); } if ( $sch['release'] ) { /* translators: %s: date and time */ $out .= '

' . esc_html( sprintf( __( 'Opens to everyone on %s', 'hyperdrop' ), HyperDrop_Schedule::fmt( $sch['release'] ) ) ) . '

'; if ( ! $sch['end'] ) { $out .= HyperDrop_Schedule::countdown( $sch['release'], '' ); } } return $out; } /** * Gate box for a protected category. */ public static function term_box( $term_id, $return = '', $compact = false ) { $term = get_term( $term_id, 'product_cat' ); if ( ! $term || is_wp_error( $term ) ) { return ''; } $testing = HyperDrop_Drop::testing_term_for( $term_id ); if ( $testing && ! HyperDrop_Drop::can_test() ) { return ''; // Test mode: nothing public, not even the unlock box. } $s = HyperDrop_Drop::term_settings( $term_id ); $preview = 'preview' === $s['visibility']; $default = $preview ? __( 'This collection is exclusive. Enter your access code to shop it.', 'hyperdrop' ) : __( 'This collection is exclusive. Enter your access code to view it.', 'hyperdrop' ); $msg = $s['message'] ? $s['message'] : $default; $url = get_term_link( $term ); $hyperdrop_look = HyperDrop_Design::attrs( 't:' . $term_id, ! $compact ); wp_enqueue_style( 'hyperdrop' ); ob_start(); ?>
>

session ) { wc_print_notices(); } ?>
>

name ) ) : esc_html__( '🔒 Exclusive access', 'hyperdrop' ); ?>

name ); ?>

0 && $n >= $limit ) { return false; } set_transient( $key, $n + 1, 10 * MINUTE_IN_SECONDS ); return true; } protected static function back( $product_id, $unlocked = false ) { // Guests have no session cookie yet; without one, notices would be lost on redirect. if ( WC()->session && ! WC()->session->has_session() ) { WC()->session->set_customer_session_cookie( true ); } $url = $product_id ? get_permalink( $product_id ) : home_url( '/' ); // Query arg busts page caches that ignore the session cookie. $url = add_query_arg( 'hyperdrop', $unlocked ? 'unlocked' : time(), $url ); wp_safe_redirect( $url ); exit; } /* ------------------------------------------------------------------ * Rendering * ------------------------------------------------------------------ */ public static function swap_add_to_cart() { global $product; $pt = $product ? HyperDrop_Drop::preview_lock_term( $product ) : 0; if ( $pt && ! HyperDrop_Drop::is_product_gated( $product ) ) { remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); add_action( 'woocommerce_single_product_summary', array( __CLASS__, 'echo_preview_box' ), 30 ); return; } if ( ! $product || ! HyperDrop_Drop::is_product_gated( $product ) || HyperDrop_Drop::shopper_has_access( $product ) ) { if ( $product && HyperDrop_Drop::is_gated( $product ) && ! HyperDrop_Drop::is_staff() ) { add_action( 'woocommerce_single_product_summary', array( __CLASS__, 'unlocked_badge' ), 29 ); } return; } remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); add_action( 'woocommerce_single_product_summary', array( __CLASS__, 'echo_box' ), 30 ); } /** * Product IDs whose locked box was already printed this request. Block themes * can trigger both the classic hook (via WooCommerce's compatibility layer) * and the add-to-cart block, so print it once. */ protected static $printed = array(); public static function echo_preview_box() { global $product; $id = HyperDrop_Drop::root_id( $product ); if ( isset( self::$printed[ $id ] ) ) { return; } self::$printed[ $id ] = true; echo wp_kses( self::term_box( HyperDrop_Drop::preview_lock_term( $product ), get_permalink( $id ), true ), hyperdrop_allowed_html() ); } public static function filter_block( $content, $block ) { global $product; $p = $product; if ( ! $p && is_product() ) { $p = wc_get_product( get_queried_object_id() ); } if ( $p && ! HyperDrop_Drop::is_product_gated( $p ) && HyperDrop_Drop::preview_lock_term( $p ) ) { $id = HyperDrop_Drop::root_id( $p ); if ( isset( self::$printed[ $id ] ) ) { return ''; } self::$printed[ $id ] = true; wp_enqueue_style( 'hyperdrop' ); wp_enqueue_script( 'hyperdrop' ); return self::term_box( HyperDrop_Drop::preview_lock_term( $p ), get_permalink( $id ), true ); } if ( ! $p || ! HyperDrop_Drop::is_product_gated( $p ) || HyperDrop_Drop::shopper_has_access( $p ) ) { return $content; } $id = HyperDrop_Drop::root_id( $p ); if ( isset( self::$printed[ $id ] ) ) { return ''; // Already shown — and the add-to-cart form stays hidden. } self::$printed[ $id ] = true; wp_enqueue_style( 'hyperdrop' ); wp_enqueue_script( 'hyperdrop' ); return self::box( $p ); } public static function echo_box() { global $product; $id = HyperDrop_Drop::root_id( $product ); if ( isset( self::$printed[ $id ] ) ) { return; } self::$printed[ $id ] = true; echo wp_kses( self::box( $product ), hyperdrop_allowed_html() ); } public static function unlocked_badge() { global $product; echo '

' . esc_html__( '🔓 Exclusive access unlocked', 'hyperdrop' ) . '

'; if ( $product ) { do_action( 'hyperdrop_unlocked_badge', $product ); } if ( $product ) { foreach ( HyperDrop_Limits::scopes_for( $product ) as $sc ) { $end = HyperDrop_Schedule::get( $sc )['end']; if ( $end && 'live' === HyperDrop_Schedule::phase( $sc ) ) { echo wp_kses( HyperDrop_Schedule::countdown( $end, __( 'Drop ends in', 'hyperdrop' ) ), hyperdrop_allowed_html() ); break; } } } $note = $product ? HyperDrop_Limits::product_note( $product ) : ''; if ( $note ) { echo '

' . esc_html( $note ) . '

'; } } public static function loop_badge() { global $product; if ( $product && HyperDrop_Drop::is_gated( $product ) ) { echo '' . esc_html__( 'Exclusive', 'hyperdrop' ) . ''; } } /** * [hyperdrop product="123"] – product drop unlock box. * [hyperdrop category="exclusive"] – category unlock box (e.g. on the home page). * Shows nothing once the visitor has access, unless unlocked_text="..." is set. */ public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'product' => 0, 'category' => '', 'unlocked_text' => '', ), $atts ); if ( $atts['category'] ) { $term = is_numeric( $atts['category'] ) ? get_term( (int) $atts['category'], 'product_cat' ) : get_term_by( 'slug', sanitize_title( $atts['category'] ), 'product_cat' ); if ( ! $term || is_wp_error( $term ) ) { return ''; } wp_enqueue_style( 'hyperdrop' ); $g = HyperDrop_Drop::gating_term_for( $term->term_id ); if ( ! $g || HyperDrop_Drop::is_staff() || HyperDrop_Drop::term_code( $g ) ) { if ( ! $atts['unlocked_text'] ) { return ''; } return '

' . esc_html( $atts['unlocked_text'] ) . '

'; } return self::term_box( $g, '' ); } $p = wc_get_product( absint( $atts['product'] ) ); if ( ! $p ) { global $product; $p = $product; } if ( ! $p || HyperDrop_Drop::shopper_has_access( $p ) ) { return ''; } wp_enqueue_style( 'hyperdrop' ); wp_enqueue_script( 'hyperdrop' ); return self::box( $p ); } /** * The locked box HTML. * * @param WC_Product $product */ public static function box( $product ) { $id = HyperDrop_Drop::root_id( $product ); $s = HyperDrop_Drop::settings( $id ); $msg = $s['message'] ? $s['message'] : __( 'This is an exclusive drop. Enter your access code to shop.', 'hyperdrop' ); $url = get_permalink( $id ); ob_start(); ?>

>