get( 'hyperdrop_skip' ) ) { return; } // The single product page itself is handled by the gate (redirect), not a 404. if ( $q->is_main_query() && $q->is_singular() ) { return; } $types = (array) $q->get( 'post_type' ); $applies = array_intersect( $types, array( 'product', 'any' ) ) || ( ( ! $q->get( 'post_type' ) || 'any' === $q->get( 'post_type' ) ) && $q->is_search() ) || $q->get( 'product_cat' ); if ( ! $applies ) { return; } // Single-product drops still in test mode: invisible to non-admins. if ( ! HyperDrop_Drop::can_test() && HyperDrop_Drop::testing_products() ) { $q->set( 'post__not_in', array_merge( (array) $q->get( 'post__not_in' ), HyperDrop_Drop::testing_products() ) ); } $hidden = HyperDrop_Drop::hidden_terms(); if ( ! $hidden ) { return; } $ours = array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $hidden, 'operator' => 'NOT IN', 'include_children' => true, ); $existing = $q->get( 'tax_query' ); $q->set( 'tax_query', $existing ? array( 'relation' => 'AND', $existing, $ours ) : array( $ours ) ); } public static function is_visible( $visible, $product_id ) { if ( ! $visible || ! self::is_frontend() ) { return $visible; } return HyperDrop_Drop::can_view( $product_id ); } public static function filter_ids( $ids ) { if ( ! self::is_frontend() || ! is_array( $ids ) || ! HyperDrop_Drop::hidden_terms() ) { return $ids; } return array_values( array_filter( $ids, array( 'HyperDrop_Drop', 'can_view' ) ) ); } /** * Block direct single-product REST lookups (Store API and wp/v2) for locked products. */ /** * Embeds (oEmbed / share previews) must not reveal locked or test-mode products. */ public static function oembed_guard( $post_id ) { if ( $post_id && 'product' === get_post_type( $post_id ) ) { $p = wc_get_product( $post_id ); if ( $p && ( ! HyperDrop_Drop::can_view( $p ) || HyperDrop_Drop::is_testing_product( $p ) ) ) { return 0; } } return $post_id; } /** * Keep hidden and test-mode categories out of the WordPress sitemap. */ public static function sitemap_terms( $args, $taxonomy ) { if ( 'product_cat' !== $taxonomy ) { return $args; } $ids = array_unique( array_merge( HyperDrop_Drop::hidden_terms(), HyperDrop_Drop::testing_terms() ) ); foreach ( $ids as $tid ) { $kids = get_term_children( $tid, 'product_cat' ); if ( is_array( $kids ) ) { $ids = array_merge( $ids, $kids ); } } if ( $ids ) { // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- a handful of term IDs in a term query. $args['exclude'] = array_unique( array_merge( wp_parse_id_list( isset( $args['exclude'] ) ? $args['exclude'] : array() ), array_map( 'intval', $ids ) ) ); } return $args; } public static function rest_guard( $result, $server, $request ) { if ( null !== $result || ! HyperDrop_Drop::hidden_terms() ) { return $result; } if ( preg_match( '#^/(?:wc/store(?:/v\d+)?/products|wp/v2/product|wc/v\d+/products)/(\d+)$#', $request->get_route(), $m ) ) { $p = wc_get_product( (int) $m[1] ); if ( $p && ! HyperDrop_Drop::can_view( $p ) ) { return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'hyperdrop' ), array( 'status' => 404 ) ); } } return $result; } /* ------------------------------------------------------------------ * Page gate * ------------------------------------------------------------------ */ public static function detect() { // Test mode: to anyone but admins the drop doesn't exist (404). if ( ! HyperDrop_Drop::can_test() ) { $missing = ( is_product_category() && HyperDrop_Drop::testing_term_for( get_queried_object_id() ) ) || ( is_product() && HyperDrop_Drop::is_testing_product( get_queried_object_id() ) ); if ( $missing ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); return; } } if ( HyperDrop_Drop::is_staff() ) { return; } if ( is_product_category() ) { $g = HyperDrop_Drop::gating_term_for( get_queried_object_id() ); if ( $g && ! HyperDrop_Drop::term_code( $g ) && ! HyperDrop_Drop::is_hidden_mode( $g ) ) { // Preview mode: the category page stays public; show an unlock banner above the products. self::$preview = $g; nocache_headers(); } elseif ( $g && ! HyperDrop_Drop::term_code( $g ) ) { self::$term = $g; self::$return = HyperDrop_Frontend::from_product_url(); } } elseif ( is_product() ) { $product = wc_get_product( get_queried_object_id() ); if ( $product && ! HyperDrop_Drop::can_view( $product ) ) { // Send them to the category's gate; the product name/content never renders. $terms = HyperDrop_Drop::gated_terms_for_product( $product ); $link = get_term_link( (int) $terms[0], 'product_cat' ); if ( ! is_wp_error( $link ) ) { nocache_headers(); wp_safe_redirect( HyperDrop_Frontend::with_from( $link, $product->get_id() ) ); exit; } } } if ( self::$term ) { nocache_headers(); add_filter( 'wp_robots', 'wp_robots_no_robots' ); add_filter( 'body_class', array( __CLASS__, 'body_class' ) ); } } public static function preview_banner() { if ( ! self::$preview || self::$banner_done ) { return; } self::$banner_done = true; wp_enqueue_style( 'hyperdrop' ); echo wp_kses( HyperDrop_Frontend::term_box( self::$preview, '', true ), hyperdrop_allowed_html() ); } /** * Block themes: put the banner right after the category description block. */ public static function preview_banner_block( $content, $block ) { if ( ! self::$preview || self::$banner_done || ! is_product_category() ) { return $content; } ob_start(); self::preview_banner(); return $content . ob_get_clean(); } /** * Preview collections with "hide prices": show a lock label instead of the price. */ public static function price_html( $html, $product ) { if ( ! self::is_frontend() || ! $product ) { return $html; } $t = HyperDrop_Drop::preview_lock_term( $product ); if ( $t && HyperDrop_Drop::term_settings( $t )['hide_price'] ) { return '' . esc_html__( '🔒 Price revealed with access code', 'hyperdrop' ) . ''; } return $html; } /** * Test mode: leave the collection and its sub-categories out of every category * query (lists, tiles, widgets, Store API) for everyone but admins. */ public static function exclude_testing_terms( $args, $taxonomies ) { if ( ! self::is_frontend() || ! empty( $args['hyperdrop_skip'] ) || ! in_array( 'product_cat', (array) $taxonomies, true ) ) { return $args; } // A product's own category lookup (wp_get_object_terms) must stay intact; only lists are filtered. if ( ! empty( $args['object_ids'] ) ) { return $args; } // 'get' => 'all' is WordPress asking for every term on purpose — e.g. building the // cached category tree (_get_term_hierarchy). Filtering it would save a broken tree. if ( isset( $args['get'] ) && 'all' === $args['get'] ) { return $args; } if ( HyperDrop_Drop::can_test() ) { return $args; } // Work out the hidden IDs once per request. Everything inside can run its own // get_terms() (testing_terms(), and get_term_children() when the category // hierarchy cache is cold), which comes back into this filter: the guard stays // on until the whole calculation is finished, so it can never recurse. static $busy = false; static $ids = null; if ( $busy ) { return $args; } if ( null === $ids ) { $busy = true; try { $ids = array(); foreach ( HyperDrop_Drop::testing_terms() as $tid ) { $ids[] = (int) $tid; $kids = get_term_children( (int) $tid, 'product_cat' ); if ( is_array( $kids ) ) { $ids = array_merge( $ids, array_map( 'intval', $kids ) ); } } $ids = array_values( array_unique( $ids ) ); } finally { $busy = false; } } if ( $ids ) { // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- a handful of term IDs in a term query, not a post query. $args['exclude'] = array_unique( array_merge( wp_parse_id_list( isset( $args['exclude'] ) ? $args['exclude'] : array() ), $ids ) ); } return $args; } /** * Test mode (fallback for term lists built another way): remove the collection (and its sub-categories) from category lists, * tiles and widgets for everyone but admins. */ public static function hide_testing_terms( $terms, $taxonomies, $args ) { if ( ! self::is_frontend() || ! empty( $args['hyperdrop_skip'] ) || ! empty( $args['object_ids'] ) || ! is_array( $terms ) || ! in_array( 'product_cat', (array) $taxonomies, true ) ) { return $terms; } // WordPress asking for every term on purpose (e.g. the cached category tree): leave it whole. if ( isset( $args['get'] ) && 'all' === $args['get'] ) { return $terms; } if ( HyperDrop_Drop::can_test() || ! HyperDrop_Drop::testing_terms() ) { return $terms; } // With fields like 'id=>parent' or 'id=>name' the term ID is the array key, not the value. $id_keys = isset( $args['fields'] ) && 0 === strpos( (string) $args['fields'], 'id=>' ); $out = array(); foreach ( $terms as $k => $t ) { $id = $t instanceof WP_Term ? $t->term_id : ( $id_keys ? (int) $k : ( is_numeric( $t ) ? (int) $t : 0 ) ); if ( $id && HyperDrop_Drop::testing_term_for( $id ) ) { continue; } $out[ $k ] = $t; } return is_array( $terms ) && array_values( $terms ) === $terms ? array_values( $out ) : $out; } /** * Test mode: hide menu links to the collection or its products. */ public static function hide_testing_menu_items( $items ) { if ( is_admin() || HyperDrop_Drop::can_test() || ! is_array( $items ) ) { return $items; } foreach ( $items as $k => $item ) { $hide = ( 'taxonomy' === $item->type && 'product_cat' === $item->object && HyperDrop_Drop::testing_term_for( $item->object_id ) ) || ( 'post_type' === $item->type && 'product' === $item->object && HyperDrop_Drop::is_testing_product( $item->object_id ) ); if ( $hide ) { unset( $items[ $k ] ); } } return $items; } public static function body_class( $c ) { $c[] = 'hyperdrop-gated'; return $c; } public static function template( $template ) { if ( ! self::$term ) { return $template; } wp_enqueue_style( 'hyperdrop' ); return HYPERDROP_DIR . 'templates/gate.php'; } }