*/ private static $printed = array(); /** * Hooks. */ public static function init() { add_action( 'wp', array( __CLASS__, 'setup' ) ); add_action( 'wp_enqueue_scripts', array( __CLASS__, 'assets' ) ); add_action( 'wp_body_open', array( __CLASS__, 'top' ), 5 ); add_action( 'wp_footer', array( __CLASS__, 'top_fallback' ), 5 ); add_action( 'woocommerce_before_cart', array( __CLASS__, 'cart' ), 20 ); add_action( 'woocommerce_before_checkout_form', array( __CLASS__, 'checkout' ), 5 ); add_action( 'woocommerce_widget_shopping_cart_before_buttons', array( __CLASS__, 'mini' ) ); add_action( 'woocommerce_single_product_summary', array( __CLASS__, 'product' ), 31 ); add_filter( 'render_block', array( __CLASS__, 'render_block' ), 10, 2 ); add_shortcode( 'fspbar', array( __CLASS__, 'shortcode' ) ); add_action( 'wc_ajax_fspbar_refresh', array( __CLASS__, 'refresh' ) ); add_filter( 'woocommerce_shipping_chosen_method', array( __CLASS__, 'choose_free_shipping' ), 10, 3 ); // The cart changed during this request: count again. foreach ( array( 'woocommerce_add_to_cart', 'woocommerce_cart_item_removed', 'woocommerce_cart_emptied', 'woocommerce_after_calculate_totals', 'woocommerce_applied_coupon', 'woocommerce_removed_coupon' ) as $hook ) { add_action( $hook, array( Goal::class, 'flush' ) ); } } /** * Runs once WordPress knows which page is shown: picks the page's bar. */ public static function setup() { $s = Settings::get(); if ( is_wc_endpoint_url( 'order-received' ) || is_wc_endpoint_url( 'order-pay' ) ) { self::$page = 'none'; self::$show_top = false; return; } if ( $s['place_cart'] && self::is_cart_page() ) { self::$page = 'cart'; } elseif ( $s['place_checkout'] && self::is_checkout_page() ) { self::$page = 'checkout'; } elseif ( $s['place_product'] && is_product() ) { $product = wc_get_product( get_queried_object_id() ); if ( $product && $product->needs_shipping() ) { self::$page = 'product'; } } /** * Filters whether the top bar shows on this page. * * @param bool $show Whether it shows. * @param string $page The page's own bar ('cart', 'checkout', 'product') or ''. */ self::$show_top = (bool) apply_filters( 'fspbar_show_top', $s['place_top'] && '' === self::$page, self::$page ); } /** * Whether this is the cart page (also a page with the Cart block that is not set as the cart page). * * @return bool */ private static function is_cart_page() { return is_cart() || ( is_singular() && has_block( 'woocommerce/cart', get_queried_object_id() ) ); } /** * Whether this is the checkout page. * * @return bool */ private static function is_checkout_page() { return is_checkout() || ( is_singular() && has_block( 'woocommerce/checkout', get_queried_object_id() ) ); } /** * Whether any place is switched on. * * @return bool */ private static function any_place() { $s = Settings::get(); return $s['place_top'] || $s['place_cart'] || $s['place_checkout'] || $s['place_mini'] || $s['place_product']; } /** * Loads the bar's CSS and JS on shop pages. */ public static function assets() { if ( ! self::any_place() || 'none' === self::$page ) { return; } self::enqueue(); } /** * Queues the bar's files (also called by the shortcode). */ public static function enqueue() { if ( wp_script_is( 'fspbar', 'enqueued' ) ) { return; } wp_enqueue_style( 'fspbar', FSPBAR_URL . 'assets/css/bar.css', array(), FSPBAR_VERSION ); wp_add_inline_style( 'fspbar', self::inline_css() ); wp_enqueue_script( 'fspbar', FSPBAR_URL . 'assets/js/bar.js', array(), FSPBAR_VERSION, array( 'in_footer' => true, 'strategy' => 'defer', ) ); wp_add_inline_script( 'fspbar', 'window.fspbarConfig = ' . wp_json_encode( array( 'url' => \WC_AJAX::get_endpoint( 'fspbar_refresh' ) ) ) . ';', 'before' ); /** * Fires after the bar's files are queued. Add-ons queue theirs here. */ do_action( 'fspbar_enqueue_assets' ); } /** * CSS variables for the chosen color. * * @return string */ public static function inline_css() { $color = Settings::color(); $css = '.fspbar{--fspbar-color:' . $color . ';--fspbar-on-color:' . Settings::text_color_for( $color ) . ';}'; /** * Filters the bar's inline CSS (add-ons add their design here). * * @param string $css CSS. */ return (string) apply_filters( 'fspbar_inline_css', $css ); } // Places. /** * Top of every page (themes call wp_body_open right after
). */ public static function top() { if ( self::$show_top && empty( self::$printed['top'] ) ) { self::$printed['top'] = true; echo wp_kses( self::bar( 'top' ), self::allowed_html() ); } } /** * Themes without wp_body_open: print the top bar in the footer; the script moves it to the top. */ public static function top_fallback() { if ( self::$show_top && empty( self::$printed['top'] ) ) { self::$printed['top'] = true; echo wp_kses( self::bar( 'top', array( 'data-fspbar-move' => 'top' ) ), self::allowed_html() ); } } /** * Cart page (classic cart). */ public static function cart() { if ( 'cart' === self::$page && empty( self::$printed['cart'] ) ) { self::$printed['cart'] = true; echo wp_kses( self::bar( 'cart' ), self::allowed_html() ); } } /** * Checkout page (classic checkout). */ public static function checkout() { if ( 'checkout' === self::$page && empty( self::$printed['checkout'] ) ) { self::$printed['checkout'] = true; echo wp_kses( self::bar( 'checkout' ), self::allowed_html() ); } } /** * Mini cart (the cart widget, e.g. Storefront's header cart). Also printed when WooCommerce refreshes it. */ public static function mini() { if ( Settings::get()['place_mini'] ) { echo wp_kses( self::bar( 'mini' ), self::allowed_html() ); } } /** * Product page, under Add to cart (classic themes). */ public static function product() { if ( 'product' === self::$page && empty( self::$printed['product'] ) ) { self::$printed['product'] = true; echo wp_kses( self::bar( 'product' ), self::allowed_html() ); } } /** * Block themes: before the Cart and Checkout blocks, after the product's Add to cart block. * * @param string $content Block HTML. * @param array $block Block. * @return string */ public static function render_block( $content, $block ) { $name = isset( $block['blockName'] ) ? (string) $block['blockName'] : ''; if ( 'woocommerce/cart' === $name && 'cart' === self::$page && empty( self::$printed['cart'] ) ) { self::$printed['cart'] = true; return wp_kses( self::bar( 'cart' ), self::allowed_html() ) . $content; } if ( 'woocommerce/checkout' === $name && 'checkout' === self::$page && empty( self::$printed['checkout'] ) ) { self::$printed['checkout'] = true; return wp_kses( self::bar( 'checkout' ), self::allowed_html() ) . $content; } if ( in_array( $name, array( 'woocommerce/add-to-cart-form', 'woocommerce/add-to-cart-with-options' ), true ) && 'product' === self::$page && empty( self::$printed['product'] ) ) { self::$printed['product'] = true; return $content . wp_kses( self::bar( 'product' ), self::allowed_html() ); } // Mini-Cart block drawer: above its footer (subtotal and buttons). if ( 'woocommerce/mini-cart-footer-block' === $name && Settings::get()['place_mini'] && 'none' !== self::$page ) { return wp_kses( self::bar( 'mini' ), self::allowed_html() ) . $content; } return $content; } /** * [fspbar] anywhere. * * @return string */ public static function shortcode() { if ( is_admin() || wp_is_json_request() ) { return ''; } self::enqueue(); return wp_kses( self::bar( 'inline' ), self::allowed_html() ); } // HTML. /** * The whole bar for a place. * * @param string $place top, cart, checkout, mini, product or inline. * @param array $attrs Extra attributes. * @return string HTML. */ public static function bar( $place, array $attrs = array() ) { $s = Settings::get(); $state = Goal::state(); $classes = array( 'fspbar', 'fspbar--' . $place, 'fspbar--' . $s['style'] ); if ( 'top' === $place && ! $s['top_empty'] ) { $classes[] = 'fspbar--hide-empty'; } /** * Filters the bar's CSS classes. * * @param string[] $classes Classes. * @param string $place Place. * @param array $s Settings. */ $classes = array_map( 'sanitize_html_class', (array) apply_filters( 'fspbar_bar_classes', $classes, $place, $s ) ); $context = array(); if ( 'product' === $place && is_product() ) { $context['product'] = get_queried_object_id(); $attrs['data-product'] = $context['product']; } $attrs = array_merge( array( 'class' => implode( ' ', $classes ), 'data-fspbar' => $place, 'data-state' => $state['state'], 'data-hash' => WC()->cart ? WC()->cart->get_cart_hash() : '', 'aria-live' => 'polite', ), $attrs ); $html = '