if ( ! empty( $posts ) ) { wp_redirect( get_permalink( $posts[0] ), 302 ); exit; } } /** * Remember that this visitor has a discount so HollerBox discount popups stay hidden. * Set when they land on any ?discount= link (before EDD redirects it away) or when EDD applies a discount. */ add_action( 'init', 'spc_discount_link_cookie', 1 ); function spc_discount_link_cookie() { if ( ! empty( $_GET['discount'] ) ) { spc_set_has_discount_cookie(); } } add_action( 'edd_cart_discount_set', 'spc_set_has_discount_cookie' ); function spc_set_has_discount_cookie() { if ( headers_sent() || ! empty( $_COOKIE['spc_has_discount'] ) ) { return; } setcookie( 'spc_has_discount', '1', time() + 30 * DAY_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), false ); } /** * Hide any HollerBox popup whose button links to a ?discount URL when the visitor already has a discount. * Done in the browser (not PHP) because the popup list is baked into cached page HTML. */ add_action( 'wp_enqueue_scripts', 'spc_hollerbox_hide_discount_popups', 20 ); function spc_hollerbox_hide_discount_popups() { if ( ! wp_script_is( 'hollerbox-popups', 'enqueued' ) ) { return; } wp_add_inline_script( 'hollerbox-popups', " if ( document.cookie.indexOf( 'spc_has_discount=1' ) !== -1 && window.HollerBox && Array.isArray( HollerBox.active ) ) { HollerBox.active = HollerBox.active.filter( function ( p ) { return ! ( p.button_link || '' ).includes( '?discount' ); } ); }", 'before' ); }