*/ private static $cache = array(); /** * Hooks. */ public static function init(): void { add_action( 'init', array( self::class, 'maybe_switch' ), 20 ); add_action( 'admin_bar_menu', array( self::class, 'admin_bar' ), 90 ); // The checkout and the side cart ask these filters; the bypass wins over everything. add_filter( 'aiolos_load_checkout', array( self::class, 'filter_off' ), PHP_INT_MAX ); add_filter( 'aiolos_side_cart_visible', array( self::class, 'filter_off' ), PHP_INT_MAX ); // The redesigned thank-you page too: "without it" means the whole front end. add_action( 'wp', array( self::class, 'thankyou_off' ), 0 ); add_action( 'wp_logout', array( self::class, 'forget' ) ); } /** * Whether the current visitor is a shop manager who chose to see the shop without the plugin. */ public static function active(): bool { if ( empty( $_COOKIE[ self::COOKIE ] ) || ! is_string( $_COOKIE[ self::COOKIE ] ) ) { return false; } // Too early to know who is visiting (before plugins_loaded): never bypass. if ( ! function_exists( 'wp_get_current_user' ) || ! function_exists( 'wp_hash' ) || ! did_action( 'plugins_loaded' ) ) { return false; } $user_id = (int) get_current_user_id(); if ( isset( self::$cache[ $user_id ] ) ) { return self::$cache[ $user_id ]; } $cookie = sanitize_text_field( wp_unslash( $_COOKIE[ self::COOKIE ] ) ); $active = $user_id > 0 && hash_equals( self::token( $user_id ), $cookie ) && user_can( $user_id, Admin::CAPABILITY ); self::$cache[ $user_id ] = $active; return $active; } /** * The cookie value for a user: it only works for that user on this site. * * @param int $user_id User id. */ private static function token( int $user_id ): string { return $user_id . '.' . wp_hash( 'aiolos-bypass|' . $user_id, 'auth' ); } /** * `?aiolos-bypass=1` turns it on, `=0` off (with the nonce from the admin bar link), then the * same page loads again without the parameters. */ public static function maybe_switch(): void { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- checked below before anything changes. if ( ! isset( $_GET[ self::PARAM ] ) || ! is_string( $_GET[ self::PARAM ] ) || is_admin() || wp_doing_ajax() ) { return; } $on = '1' === sanitize_key( wp_unslash( $_GET[ self::PARAM ] ) ); $nonce = isset( $_GET['_wpnonce'] ) && is_string( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; // phpcs:enable if ( ! is_user_logged_in() || ! current_user_can( Admin::CAPABILITY ) || ! wp_verify_nonce( $nonce, self::NONCE ) ) { return; } self::set( $on ); Log::info( $on ? 'Bypass on: this shop manager sees the shop without Aiolos Checkout.' : 'Bypass off.', array( 'user' => get_current_user_id() ) ); nocache_headers(); wp_safe_redirect( remove_query_arg( array( self::PARAM, '_wpnonce' ) ) ); exit; } /** * Sets or clears the cookie for the current user. * * @param bool $on On. */ public static function set( bool $on ): void { $user_id = get_current_user_id(); if ( $user_id <= 0 ) { return; } $value = $on ? self::token( $user_id ) : ''; $expiry = $on ? time() + DAY_IN_SECONDS : time() - YEAR_IN_SECONDS; if ( ! headers_sent() ) { setcookie( self::COOKIE, $value, array( 'expires' => $expiry, 'path' => COOKIEPATH ? COOKIEPATH : '/', 'domain' => (string) COOKIE_DOMAIN, 'secure' => is_ssl(), 'httponly' => true, 'samesite' => 'Lax', ) ); } if ( $on ) { $_COOKIE[ self::COOKIE ] = $value; } else { unset( $_COOKIE[ self::COOKIE ] ); } self::$cache = array(); } /** * Clears the cookie when the manager logs out. */ public static function forget(): void { if ( ! empty( $_COOKIE[ self::COOKIE ] ) && ! headers_sent() ) { setcookie( self::COOKIE, '', time() - YEAR_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', (string) COOKIE_DOMAIN, is_ssl(), true ); } self::$cache = array(); } /** * Filter callback: false while the bypass is on. * * @param mixed $value Value. * @return mixed */ public static function filter_off( $value ) { return self::active() ? false : $value; } /** * Without the redesigned thank-you page while the bypass is on. */ public static function thankyou_off(): void { if ( ! self::active() ) { return; } remove_filter( 'woocommerce_locate_template', array( \AiolosCheckout\Checkout\ThankYou::class, 'locate_template' ), 20 ); remove_action( 'wp_enqueue_scripts', array( \AiolosCheckout\Checkout\ThankYou::class, 'enqueue' ), 20 ); } /** * The admin bar switch, on the front end for shop managers. * * @param \WP_Admin_Bar $bar Admin bar. */ public static function admin_bar( $bar ): void { if ( is_admin() || ! is_object( $bar ) || ! is_user_logged_in() || ! current_user_can( Admin::CAPABILITY ) ) { return; } if ( ! Settings::enabled( 'checkout_enabled' ) && ! Settings::enabled( 'side_cart_enabled' ) ) { return; } $active = self::active(); $url = wp_nonce_url( add_query_arg( self::PARAM, $active ? '0' : '1', self::current_url() ), self::NONCE ); $bar->add_node( array( 'id' => 'aiolos-bypass', 'title' => $active ? esc_html__( 'Aiolos Checkout: off for you – View with it', 'aiolos-checkout-for-woocommerce' ) : esc_html__( 'Aiolos Checkout: View without it', 'aiolos-checkout-for-woocommerce' ), 'href' => $url, 'meta' => array( 'title' => $active ? __( 'Show this shop with Aiolos Checkout again.', 'aiolos-checkout-for-woocommerce' ) : __( 'See WooCommerce\'s own checkout and no side cart, for you only. Customers keep seeing Aiolos Checkout.', 'aiolos-checkout-for-woocommerce' ), ), ) ); $bar->add_node( array( 'parent' => 'aiolos-bypass', 'id' => 'aiolos-bypass-settings', 'title' => esc_html__( 'Settings', 'aiolos-checkout-for-woocommerce' ), 'href' => Admin::url(), ) ); } /** * The address of the page being viewed. */ private static function current_url(): string { // Relative to the request (add_query_arg() reads REQUEST_URI), so a site in a sub-folder works. return remove_query_arg( array( self::PARAM, '_wpnonce' ) ); } }