settings for categories currently locked. */ protected static $gated_terms = null; /* ------------------------------------------------------------------ * Products * ------------------------------------------------------------------ */ /** * Resolve variations to their parent product. */ public static function root_id( $product ) { if ( is_numeric( $product ) ) { $product = wc_get_product( $product ); } if ( ! $product ) { return 0; } return $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(); } /** * How shoppers without a code can get one. Add-ons can register more modes. * * @return array mode => label */ public static function signup_modes() { return (array) apply_filters( 'hyperdrop_signup_modes', array( 'auto' => __( 'Instant — email a code automatically', 'hyperdrop' ), 'off' => __( 'Off — codes only from me', 'hyperdrop' ), ) ); } /** * A saved mode that is no longer available falls back to "off" — never to * sending codes automatically to people the store meant to review. */ public static function clean_signup( $mode ) { if ( '' === (string) $mode ) { return 'auto'; } return array_key_exists( $mode, self::signup_modes() ) ? $mode : 'off'; } public static function settings( $product_id ) { $product_id = self::root_id( $product_id ); return array( 'enabled' => 'yes' === get_post_meta( $product_id, '_hyperdrop_enabled', true ), 'release_at' => (int) get_post_meta( $product_id, '_hyperdrop_release_at', true ), 'signup' => self::clean_signup( get_post_meta( $product_id, '_hyperdrop_signup', true ) ), 'max_qty' => (int) get_post_meta( $product_id, '_hyperdrop_max_qty', true ), 'message' => (string) get_post_meta( $product_id, '_hyperdrop_message', true ), ); } /** * Is this product itself set up as a drop (buy-lock) right now? */ public static function is_product_gated( $product ) { $id = self::root_id( $product ); if ( ! $id ) { return false; } $s = self::settings( $id ); if ( ! $s['enabled'] ) { return false; } if ( 'public' === HyperDrop_Schedule::phase( 'p:' . $id ) ) { return false; // Public release time reached — open to everyone. } return (bool) apply_filters( 'hyperdrop_is_gated', true, $id ); } /** * Locked for any reason (product drop or protected category)? */ public static function is_gated( $product ) { return self::is_product_gated( $product ) || (bool) self::gated_terms_for_product( $product ); } /* ------------------------------------------------------------------ * Categories * ------------------------------------------------------------------ */ public static function term_settings( $term_id ) { $term_id = (int) $term_id; $act = get_term_meta( $term_id, '_hyperdrop_activations', true ); return array( 'enabled' => 'yes' === get_term_meta( $term_id, '_hyperdrop_protected', true ), 'release_at' => (int) get_term_meta( $term_id, '_hyperdrop_release_at', true ), 'signup' => self::clean_signup( get_term_meta( $term_id, '_hyperdrop_signup', true ) ), 'activations' => '' === $act ? 3 : (int) $act, 'message' => (string) get_term_meta( $term_id, '_hyperdrop_message', true ), // hidden = page + products invisible until unlocked; preview = visible, buying (and optionally prices) locked. 'visibility' => 'preview' === get_term_meta( $term_id, '_hyperdrop_visibility', true ) ? 'preview' : 'hidden', 'hide_price' => 'yes' === get_term_meta( $term_id, '_hyperdrop_hide_price', true ), ); } /** * Categories that are locked right now: [ term_id => settings ]. */ public static function gated_terms() { if ( null !== self::$gated_terms ) { return self::$gated_terms; } self::$gated_terms = array(); $ids = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'fields' => 'ids', 'hyperdrop_skip' => true, 'meta_query' => array( array( 'key' => '_hyperdrop_protected', 'value' => 'yes' ) ), // phpcs:ignore ) ); if ( is_wp_error( $ids ) ) { return self::$gated_terms; } foreach ( $ids as $id ) { $s = self::term_settings( $id ); if ( 'public' === HyperDrop_Schedule::phase( 't:' . $id ) ) { continue; } if ( apply_filters( 'hyperdrop_is_term_gated', true, $id ) ) { self::$gated_terms[ (int) $id ] = $s; } } return self::$gated_terms; } public static function flush_cache() { self::$testing_terms = null; self::$testing_products = null; self::$gated_terms = null; } /** * The locked category that controls this category (itself or an ancestor), or 0. */ public static function gating_term_for( $term_id ) { $gated = self::gated_terms(); if ( ! $gated ) { return 0; } $chain = array_merge( array( (int) $term_id ), array_map( 'intval', get_ancestors( $term_id, 'product_cat', 'taxonomy' ) ) ); foreach ( $chain as $id ) { if ( isset( $gated[ $id ] ) ) { return $id; } } return 0; } public static function is_term_gated( $term_id ) { return (bool) self::gating_term_for( $term_id ); } /** * Locked categories that apply to a product: [ gating_term_id, ... ]. */ public static function gated_terms_for_product( $product ) { $id = self::root_id( $product ); if ( ! $id || ! self::gated_terms() ) { return array(); } $cats = wc_get_product_cat_ids( $id ); $out = array(); foreach ( $cats as $cat ) { $g = self::gating_term_for( $cat ); if ( $g ) { $out[ $g ] = $g; } } return array_values( $out ); } /* ------------------------------------------------------------------ * Access state * ------------------------------------------------------------------ */ protected static function session_get( $key ) { if ( ! function_exists( 'WC' ) ) { return array(); } // REST requests (Store API, wp/v2) don't load the shopper session until later — load it now. if ( ! WC()->session && did_action( 'woocommerce_init' ) && ( defined( 'REST_REQUEST' ) && REST_REQUEST ) && method_exists( WC(), 'initialize_session' ) ) { WC()->initialize_session(); } if ( ! WC()->session ) { return array(); } $u = WC()->session->get( $key ); return is_array( $u ) ? $u : array(); } protected static function session_set( $key, $value ) { if ( ! function_exists( 'WC' ) || ! WC()->session ) { return; } if ( ! WC()->session->has_session() ) { WC()->session->set_customer_session_cookie( true ); } WC()->session->set( $key, $value ); } /** [ product_id => code_id ] */ public static function unlocked() { return self::session_get( self::SESSION_PRODUCTS ); } /** [ term_id => code_id ] — session, plus saved access for logged-in customers. */ public static function unlocked_terms() { $u = self::session_get( self::SESSION_TERMS ); if ( is_user_logged_in() ) { $saved = get_user_meta( get_current_user_id(), self::USER_META_TERMS, true ); if ( is_array( $saved ) ) { $u = $u + $saved; } } return $u; } public static function code_for( $product ) { $id = self::root_id( $product ); $u = self::unlocked(); return isset( $u[ $id ] ) ? (int) $u[ $id ] : 0; } /** * The code that gives access to a locked category, re-validated. 0 if none. */ public static function term_code( $term_id ) { // Coming soon / ended: nobody gets in yet (saved codes are kept for when it opens). if ( 'live' !== HyperDrop_Schedule::phase( 't:' . (int) $term_id ) ) { return 0; } $u = self::unlocked_terms(); if ( empty( $u[ $term_id ] ) ) { return 0; } $row = HyperDrop_Codes::get( $u[ $term_id ] ); if ( ! $row || is_wp_error( HyperDrop_Codes::check_term( $row, $term_id, false ) ) ) { self::lock_term( $term_id ); return 0; } return (int) $row->id; } /** * Saved code for a category even if the drop isn't live yet (no phase check). */ public static function saved_term_code( $term_id ) { $u = self::unlocked_terms(); return isset( $u[ $term_id ] ) ? (int) $u[ $term_id ] : 0; } public static function has_term_access( $term_id ) { $g = self::gating_term_for( $term_id ); return ! $g || (bool) self::term_code( $g ); } /** * Can the current visitor see / buy this product? */ public static function shopper_has_access( $product ) { if ( self::is_staff() ) { return true; } if ( self::blocked_by_test_mode( $product ) ) { return false; } $id = self::root_id( $product ); $terms = self::gated_terms_for_product( $id ); if ( $terms ) { $ok = false; foreach ( $terms as $t ) { if ( self::term_code( $t ) ) { $ok = true; break; } } if ( ! $ok ) { return false; } } if ( self::is_product_gated( $id ) ) { if ( 'live' !== HyperDrop_Schedule::phase( 'p:' . $id ) ) { return false; } $code_id = self::code_for( $id ); $row = $code_id ? HyperDrop_Codes::get( $code_id ) : null; if ( ! $row || is_wp_error( HyperDrop_Codes::check( $row, $id ) ) ) { self::lock( $id ); return false; } } return true; } /** * Can the visitor see the product at all? (Product drops stay visible; only * protected categories hide things.) */ public static function can_view( $product ) { if ( self::is_staff() ) { return true; } if ( self::blocked_by_test_mode( $product ) ) { return false; } $terms = self::gated_terms_for_product( $product ); $hidden = array_filter( $terms, array( __CLASS__, 'is_hidden_mode' ) ); if ( ! $hidden ) { return true; // Only "preview" collections (or none) — visible to everyone. } foreach ( $terms as $t ) { if ( self::term_code( $t ) ) { return true; } } return false; } public static function is_hidden_mode( $term_id ) { return 'hidden' === self::term_settings( $term_id )['visibility']; } /** * For a visible product that is buy-locked by a "preview" collection: that collection's id. Else 0. */ public static function preview_lock_term( $product ) { if ( self::is_staff() ) { return 0; } $terms = self::gated_terms_for_product( $product ); if ( ! $terms ) { return 0; } foreach ( $terms as $t ) { if ( self::term_code( $t ) ) { return 0; } } foreach ( $terms as $t ) { if ( ! self::is_hidden_mode( $t ) ) { return $t; } } return 0; } /** * Shop managers and admins always see everything (so they can preview). */ /** * Admins / shop managers bypass every lock — unless they switched on "View as shopper". */ public static function is_staff() { return (bool) apply_filters( 'hyperdrop_is_staff', self::can_test() && ! self::shopper_view() ); } /* ------------------------------------------------------------------ * Test mode * ------------------------------------------------------------------ */ /** @var array|null Collections in test mode. */ protected static $testing_terms = null; /** @var array|null Product drops in test mode. */ protected static $testing_products = null; /** * Can this visitor see drops that are in test mode? (Admins / shop managers.) */ public static function can_test() { return current_user_can( 'manage_woocommerce' ); } /** * Admin switched on "View as shopper" (admin bar). */ public static function shopper_view() { return is_user_logged_in() && current_user_can( 'manage_woocommerce' ) && 'yes' === get_user_meta( get_current_user_id(), 'hyperdrop_shopper_view', true ); } public static function is_testing_scope( $scope ) { list( $type, $id ) = HyperDrop_Limits::split( $scope ); $v = 't' === $type ? get_term_meta( $id, '_hyperdrop_testing', true ) : get_post_meta( $id, '_hyperdrop_testing', true ); return 'yes' === $v; } /** * Protected collections in test mode: [ term_id, ... ]. */ public static function testing_terms() { if ( null === self::$testing_terms ) { $ids = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'fields' => 'ids', 'hyperdrop_skip' => true, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery array( 'key' => '_hyperdrop_protected', 'value' => 'yes' ), array( 'key' => '_hyperdrop_testing', 'value' => 'yes' ), ), ) ); self::$testing_terms = is_wp_error( $ids ) ? array() : array_map( 'intval', $ids ); } return self::$testing_terms; } /** * Product drops in test mode: [ product_id, ... ]. */ public static function testing_products() { if ( null === self::$testing_products ) { self::$testing_products = array_map( 'intval', get_posts( array( 'post_type' => 'product', 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', 'hyperdrop_skip' => true, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery array( 'key' => '_hyperdrop_enabled', 'value' => 'yes' ), array( 'key' => '_hyperdrop_testing', 'value' => 'yes' ), ), ) ) ); } return self::$testing_products; } /** * The test-mode collection controlling this category (itself or a parent), or 0. */ public static function testing_term_for( $term_id ) { $testing = self::testing_terms(); if ( ! $testing ) { return 0; } $chain = array_merge( array( (int) $term_id ), array_map( 'intval', get_ancestors( $term_id, 'product_cat', 'taxonomy' ) ) ); foreach ( $chain as $id ) { if ( in_array( $id, $testing, true ) ) { return $id; } } return 0; } /** * Is this product part of a drop that is in test mode? */ public static function is_testing_product( $product ) { $id = self::root_id( $product ); if ( ! $id ) { return false; } if ( in_array( $id, self::testing_products(), true ) ) { return true; } if ( ! self::testing_terms() ) { return false; } foreach ( wc_get_product_cat_ids( $id ) as $cat ) { if ( self::testing_term_for( $cat ) ) { return true; } } return false; } /** * Hidden from this visitor because its drop is still in test mode? */ public static function blocked_by_test_mode( $product ) { return ! self::can_test() && self::is_testing_product( $product ); } /** * Locked categories the visitor cannot see. */ public static function hidden_terms() { if ( self::is_staff() ) { return array(); } // Test-mode collections are invisible to everyone who isn't an admin. $out = self::can_test() ? array() : self::testing_terms(); foreach ( array_keys( self::gated_terms() ) as $t ) { if ( self::is_hidden_mode( $t ) && ! self::term_code( $t ) ) { $out[] = $t; } } return array_values( array_unique( array_map( 'intval', $out ) ) ); } public static function unlock( $product_id, $code_id ) { $u = self::unlocked(); $u[ $product_id ] = (int) $code_id; self::session_set( self::SESSION_PRODUCTS, $u ); } public static function lock( $product_id ) { $u = self::unlocked(); if ( isset( $u[ $product_id ] ) ) { unset( $u[ $product_id ] ); self::session_set( self::SESSION_PRODUCTS, $u ); } } public static function unlock_term( $term_id, $code_id ) { $u = self::session_get( self::SESSION_TERMS ); $u[ $term_id ] = (int) $code_id; self::session_set( self::SESSION_TERMS, $u ); if ( is_user_logged_in() ) { $saved = get_user_meta( get_current_user_id(), self::USER_META_TERMS, true ); $saved = is_array( $saved ) ? $saved : array(); $saved[ $term_id ] = (int) $code_id; update_user_meta( get_current_user_id(), self::USER_META_TERMS, $saved ); } } public static function lock_term( $term_id ) { $u = self::session_get( self::SESSION_TERMS ); if ( isset( $u[ $term_id ] ) ) { unset( $u[ $term_id ] ); self::session_set( self::SESSION_TERMS, $u ); } if ( is_user_logged_in() ) { $saved = get_user_meta( get_current_user_id(), self::USER_META_TERMS, true ); if ( is_array( $saved ) && isset( $saved[ $term_id ] ) ) { unset( $saved[ $term_id ] ); update_user_meta( get_current_user_id(), self::USER_META_TERMS, $saved ); } } } }