\WC_AJAX::get_endpoint( '%%endpoint%%' ), 'openOnAdd' => Settings::enabled( 'side_cart_open_on_add' ) && ! Settings::enabled( 'side_cart_shake' ), 'shake' => Settings::enabled( 'side_cart_shake' ), 'buttonEmpty' => Settings::enabled( 'side_cart_button_empty' ), 'ajaxSingle' => Settings::enabled( 'side_cart_ajax_single' ), 'triggers' => self::triggers(), 'cartUrl' => wc_get_cart_url(), 'couponParam' => Settings::enabled( 'coupon_links' ) ? \AiolosCheckout\Cart\CouponLink::PARAM : '', // A product added by a normal (non-AJAX) form post during this request. 'justAdded' => did_action( 'woocommerce_add_to_cart' ) > 0 && ! wp_doing_ajax(), 'i18n' => array( 'error' => __( 'Something went wrong. Please try again.', 'aiolos-checkout-for-woocommerce' ), /* translators: %s: product name */ 'removed' => __( '%s was removed.', 'aiolos-checkout-for-woocommerce' ), 'undo' => __( 'Undo', 'aiolos-checkout-for-woocommerce' ), 'coupon' => __( 'Enter a discount code.', 'aiolos-checkout-for-woocommerce' ), /* translators: %s: product name */ 'added' => __( 'Added: %s', 'aiolos-checkout-for-woocommerce' ), 'addedSome' => __( 'Added to your cart.', 'aiolos-checkout-for-woocommerce' ), 'openCart' => __( 'Open cart', 'aiolos-checkout-for-woocommerce' ), 'openCart0' => CartButton::label( 0 ), ), ) ); } /** * The selectors of links and buttons that open the side cart, each one on its own (the * script skips one a browser cannot read instead of all of them). * * @return string[] */ public static function triggers(): array { $selectors = array( '.acw-open-cart', '.acw-open-cart > a', '.wc-block-mini-cart__button', // Links to the cart page only (the script checks where they go). 'a.added_to_cart', 'a.wc-forward', ); $selectors = array_merge( $selectors, self::split_selectors( (string) Settings::get( 'side_cart_triggers' ) ) ); /** * Filters the CSS selectors of elements that open the side cart. A matching link to another * page than the cart (a "Checkout" link, for example) keeps working as a link. * * @since 1.6.0 * * @param string[] $selectors Selectors. */ $selectors = (array) apply_filters( 'aiolos_side_cart_triggers', $selectors ); $clean = array(); foreach ( $selectors as $selector ) { $selector = is_string( $selector ) ? trim( wp_strip_all_tags( $selector ) ) : ''; if ( '' !== $selector && strlen( $selector ) < 300 ) { $clean[] = $selector; } } return array_values( array_unique( $clean ) ); } /** * A selector list split at its top-level commas (`:is(a, b)` stays whole). * * @param string $selectors Selectors. * @return string[] */ private static function split_selectors( string $selectors ): array { $parts = array(); $depth = 0; $part = ''; $len = strlen( $selectors ); for ( $i = 0; $i < $len; $i++ ) { $char = $selectors[ $i ]; if ( '(' === $char || '[' === $char ) { ++$depth; } elseif ( ( ')' === $char || ']' === $char ) && $depth > 0 ) { --$depth; } elseif ( ',' === $char && 0 === $depth ) { $parts[] = trim( $part ); $part = ''; continue; } $part .= $char; } $parts[] = trim( $part ); return array_values( array_filter( $parts, 'strlen' ) ); } /** * The floating button's colors, size and place. */ private static function button_css(): string { $color = sanitize_hex_color( (string) Settings::get( 'side_cart_button_color' ) ); $bg = sanitize_hex_color( (string) Settings::get( 'side_cart_button_bg' ) ); $size = min( 64, max( 18, absint( Settings::get( 'side_cart_button_size', 34 ) ) ) ); $x = min( 200, absint( Settings::get( 'side_cart_button_x', 20 ) ) ); $y = min( 300, absint( Settings::get( 'side_cart_button_y', 20 ) ) ); return ':root{--acw-fab-color:' . ( $color ? $color : '#111827' ) . ';--acw-fab-bg:' . ( $bg ? $bg : '#ffffff' ) . ';--acw-fab-icon:' . $size . 'px;--acw-fab-x:' . $x . 'px;--acw-fab-y:' . $y . 'px;}'; } /** * The empty shell (filled by fragments) and the floating button. */ public static function render_shell(): void { if ( ! self::visible() ) { return; } // Payment plugins load their mini cart buttons' scripts on this hook. Fired once, out of // sight, unless the theme printed a mini cart already. if ( Settings::enabled( 'side_cart_express' ) && ! did_action( 'woocommerce_before_mini_cart' ) ) { echo ''; } $devices = (string) Settings::get( 'side_cart_button_devices', 'all' ); Template::render( 'side-cart/shell', array( 'button' => (string) Settings::get( 'side_cart_button' ), 'icon' => CartButton::icon( '', 0 ), 'devices' => in_array( $devices, array( 'all', 'desktop', 'mobile' ), true ) ? $devices : 'all', 'empty' => Settings::enabled( 'side_cart_button_empty' ), ) ); } /** * The buttons payment plugins add to WooCommerce's mini cart (PayPal, Apple Pay, Google Pay), * without WooCommerce's own "View cart" and "Checkout" (the side cart has its own). */ public static function express_buttons(): void { if ( ! Settings::enabled( 'side_cart_express' ) ) { return; } $hook = 'woocommerce_widget_shopping_cart_buttons'; $removed = array(); foreach ( array( 'woocommerce_widget_shopping_cart_button_view_cart', 'woocommerce_widget_shopping_cart_proceed_to_checkout' ) as $callback ) { $priority = has_action( $hook, $callback ); if ( false !== $priority ) { remove_action( $hook, $callback, $priority ); $removed[ $callback ] = $priority; } } /** This action is documented in woocommerce/templates/cart/mini-cart.php */ do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); /** This action is documented in woocommerce/templates/cart/mini-cart.php */ do_action( 'woocommerce_widget_shopping_cart_buttons' ); /** This action is documented in woocommerce/templates/cart/mini-cart.php */ do_action( 'woocommerce_widget_shopping_cart_after_buttons' ); foreach ( $removed as $callback => $priority ) { add_action( $hook, $callback, $priority ); } } /** * The side cart's fragments, added to every WooCommerce cart fragments answer. * * @param array $fragments Fragments. */ public static function fragments( $fragments ) { $fragments = (array) $fragments; // The count (span.acw-sc-count) comes from CartButton::fragments(). $fragments['div.acw-sc-content'] = Template::capture( 'side-cart/content', array( 'nonce' => wp_create_nonce( self::NONCE ) ) ); return $fragments; } /** * Answers with WooCommerce's cart fragments (and ends the request). * * @param array $extra Extra keys for the answer. */ private static function send_fragments( array $extra = array() ): void { // WooCommerce fires woocommerce_cart_updated when it saves the cart after the totals (as on // the cart page); where a store turned that saving off, it is fired here, once. $updated = did_action( 'woocommerce_cart_updated' ); WC()->cart->calculate_totals(); if ( ! empty( $extra['cart_changed'] ) && did_action( 'woocommerce_cart_updated' ) === $updated ) { /** This action is documented in woocommerce/includes/class-wc-cart-session.php */ do_action( 'woocommerce_cart_updated' ); } unset( $extra['cart_changed'] ); ob_start(); woocommerce_mini_cart(); $mini_cart = ob_get_clean(); $fragments = apply_filters( 'woocommerce_add_to_cart_fragments', array( 'div.widget_shopping_cart_content' => '
' . $mini_cart . '
' ) ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment wp_send_json( array_merge( array( 'fragments' => $fragments, 'cart_hash' => WC()->cart->get_cart_hash(), 'count' => (int) WC()->cart->get_cart_contents_count(), ), $extra ) ); } /** * The notices WooCommerce collected during the request, as text, and clears them. * * @param string $type error, success or notice. * @return string[] */ private static function take_notices( string $type ): array { $messages = array(); foreach ( wc_get_notices( $type ) as $notice ) { $text = is_array( $notice ) ? ( $notice['notice'] ?? '' ) : $notice; $text = trim( html_entity_decode( wp_strip_all_tags( (string) $text ), ENT_QUOTES, 'UTF-8' ) ); if ( '' !== $text ) { $messages[] = $text; } } return $messages; } /** * Checks the side cart nonce, or ends the request. */ private static function verify(): void { if ( ! check_ajax_referer( self::NONCE, 'security', false ) ) { wp_send_json_error( array( 'message' => __( 'Your session expired. Please refresh the page.', 'aiolos-checkout-for-woocommerce' ) ), 403 ); } } /** * Cart fragments (read-only: the panel's first load, cached pages). */ public static function ajax_get(): void { self::send_fragments(); } /** * Changes the quantity of a cart item (0 removes it). */ public static function ajax_update(): void { self::verify(); $key = isset( $_POST['key'] ) ? sanitize_key( wp_unslash( $_POST['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified in verify(). $raw = isset( $_POST['quantity'] ) && is_scalar( $_POST['quantity'] ) ? sanitize_text_field( wp_unslash( $_POST['quantity'] ) ) : '0'; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified in verify(). $qty = is_numeric( $raw ) ? max( 0.0, (float) $raw ) : 0.0; $result = CartItem::set_quantity( $key, $qty ); self::send_fragments( array( 'errors' => $result['errors'], 'removed' => $result['removed'], 'cart_changed' => true, ) ); } /** * Applies a discount code from the side cart. */ public static function ajax_coupon(): void { self::verify(); $code = isset( $_POST['code'] ) ? wc_format_coupon_code( sanitize_text_field( wp_unslash( $_POST['code'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified in verify(). wc_clear_notices(); if ( '' === $code ) { self::send_fragments( array( 'errors' => array( __( 'Enter a discount code.', 'aiolos-checkout-for-woocommerce' ) ) ) ); } $applied = WC()->cart->apply_coupon( $code ); $errors = self::take_notices( 'error' ); wc_clear_notices(); self::send_fragments( array( 'errors' => $errors, 'success' => $applied && ! $errors ? __( 'Discount applied.', 'aiolos-checkout-for-woocommerce' ) : '', ) ); } /** * Removes a discount code from the side cart. */ public static function ajax_uncoupon(): void { self::verify(); $code = isset( $_POST['code'] ) ? wc_format_coupon_code( sanitize_text_field( wp_unslash( $_POST['code'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified in verify(). if ( '' !== $code && self::coupon_removable( $code ) ) { WC()->cart->remove_coupon( $code ); } wc_clear_notices(); self::send_fragments(); } /** * Whether the customer may remove a code from the side cart (automatic discounts stay). * * @param string $code Coupon code. */ public static function coupon_removable( string $code ): bool { /** * Filters whether a discount code can be removed from the side cart. * * @since 1.1.0 * * @param bool $removable Whether it can. * @param string $code Coupon code. */ return (bool) apply_filters( 'aiolos_coupon_removable', true, $code ); } /** * Puts back an item removed a moment ago (Undo). */ public static function ajax_restore(): void { self::verify(); $key = isset( $_POST['key'] ) ? sanitize_key( wp_unslash( $_POST['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified in verify(). if ( '' !== $key ) { WC()->cart->restore_cart_item( $key ); } wc_clear_notices(); self::send_fragments( array( 'cart_changed' => '' !== $key ) ); } /** * A product page's add-to-cart form, already handled by WooCommerce's form handler. */ public static function ajax_add(): void { $errors = self::take_notices( 'error' ); wc_clear_notices(); if ( $errors ) { wp_send_json( array( 'error' => true, 'errors' => $errors, ) ); } self::send_fragments( array( 'added' => true ) ); } }