"section:product_id" already printed on this page. */ private static $rendered = array(); /** Called by each module that uses the shared frontend; runs once. */ public static function boot() { if ( self::$booted ) { return; } self::$booted = true; add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register' ) ); add_action( 'wp_enqueue_scripts', array( __CLASS__, 'maybe_enqueue' ), 20 ); add_action( 'wc_ajax_cpod_pp_estimate', array( __CLASS__, 'ajax' ) ); } /** Button layout of General settings (and cPOD Kit Pro's Custom CSS), on every frontend page. */ public static function register_extra_css() { add_action( 'wp_enqueue_scripts', array( __CLASS__, 'extra_css' ), 99 ); add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'product_tabs' ), 99 ); } /** General settings → Hide WooCommerce's Additional information tab. */ public static function product_tabs( $tabs ) { if ( Settings::get( 'hide_additional' ) && is_array( $tabs ) ) { unset( $tabs['additional_information'] ); } return $tabs; } /** * Quantity box keeps its width; Add to cart (and Buy now, when the theme * prints one) share the rest of the row. Flatsome's sticky bar is left as is. */ const STRETCH_CSS = 'form.cart,form.cart .woocommerce-variation-add-to-cart{display:flex;flex-wrap:wrap;align-items:stretch;gap:8px}' . 'form.cart>*,form.cart .woocommerce-variation-add-to-cart>*{flex:1 1 100%}' . 'form.cart>.quantity,form.cart .woocommerce-variation-add-to-cart>.quantity{flex:0 0 auto;width:auto}' . 'form.cart>.single_add_to_cart_button,form.cart>.ux-buy-now-button,form.cart .woocommerce-variation-add-to-cart>.single_add_to_cart_button,form.cart .woocommerce-variation-add-to-cart>.ux-buy-now-button{flex:1 1 0;width:auto;min-width:0;padding-left:.6em;padding-right:.6em;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}' // The gap does the spacing: the repeated classes outrank theme margins (WooCommerce: .woocommerce div.product form.cart div.quantity). . 'body form.cart.cart>div.quantity.quantity.quantity,body form.cart .woocommerce-variation-add-to-cart.woocommerce-variation-add-to-cart>div.quantity.quantity.quantity,' . 'body form.cart.cart>.single_add_to_cart_button.single_add_to_cart_button.single_add_to_cart_button,body form.cart.cart>.ux-buy-now-button.ux-buy-now-button.ux-buy-now-button,' . 'body form.cart .woocommerce-variation-add-to-cart.woocommerce-variation-add-to-cart>.single_add_to_cart_button,body form.cart .woocommerce-variation-add-to-cart.woocommerce-variation-add-to-cart>.ux-buy-now-button{margin:0}' . '.sticky-add-to-cart--active form.cart,.sticky-add-to-cart--active .woocommerce-variation-add-to-cart{flex-wrap:nowrap}' . '.sticky-add-to-cart--active form.cart>*,.sticky-add-to-cart--active .woocommerce-variation-add-to-cart>*{flex:0 0 auto}' . '@media (max-width:549px){form.cart .single_add_to_cart_button,form.cart .ux-buy-now-button{flex-basis:auto;padding-left:.4em;padding-right:.4em;letter-spacing:0;font-size:.92em}}'; /** Product names on product cards cut to N lines with "…" (Flatsome and WooCommerce loops). */ public static function title_lines_css( $lines ) { return '.product-small .name.product-title a,ul.products li.product .woocommerce-loop-product__title{display:-webkit-box;-webkit-line-clamp:' . (int) $lines . ';line-clamp:' . (int) $lines . ';-webkit-box-orient:vertical;overflow:hidden}'; } public static function extra_css() { /** * Filters extra CSS printed after the plugin's styles (cPOD Kit Pro: Custom CSS). * * @param string $css CSS. */ $css = trim( wp_strip_all_tags( (string) apply_filters( 'cpod_custom_css', '' ) ) ); if ( Settings::get( 'stretch_buttons' ) ) { $css = self::STRETCH_CSS . ( '' === $css ? '' : "\n" . $css ); } $lines = (int) Settings::get( 'title_lines' ); if ( $lines ) { $css = self::title_lines_css( $lines ) . ( '' === $css ? '' : "\n" . $css ); } if ( '' === $css ) { return; } wp_register_style( 'cpod-kit-custom', false, array(), CPOD_VERSION ); wp_enqueue_style( 'cpod-kit-custom' ); wp_add_inline_style( 'cpod-kit-custom', $css ); } public static function is_rendered( $section, $product_id ) { return self::$in_quick_view || isset( self::$rendered[ $section . ':' . (int) $product_id ] ); } public static function mark_rendered( $section, $product_id ) { self::$rendered[ $section . ':' . (int) $product_id ] = true; } public static function register() { if ( wp_style_is( self::HANDLE, 'registered' ) ) { return; } wp_register_style( self::HANDLE, CPOD_URL . 'core/assets/frontend/cpod-pp.css', array(), CPOD_VERSION ); wp_register_script( self::HANDLE, CPOD_URL . 'core/assets/frontend/cpod-pp.js', array(), CPOD_VERSION, true ); wp_localize_script( self::HANDLE, 'cpodPp', array( 'endpoint' => class_exists( '\WC_AJAX' ) ? \WC_AJAX::get_endpoint( 'cpod_pp_estimate' ) : '', ) ); } /** Load CSS in on product pages to avoid a flash of unstyled content. */ public static function maybe_enqueue() { if ( function_exists( 'is_product' ) && is_product() ) { self::enqueue(); } } public static function enqueue() { self::register(); if ( wp_style_is( self::HANDLE, 'enqueued' ) ) { return; } wp_enqueue_style( self::HANDLE ); $vars = (array) apply_filters( 'cpod_frontend_css_vars', array() ); if ( $vars ) { $css = ''; foreach ( $vars as $name => $value ) { $css .= $name . ':' . $value . ';'; } wp_add_inline_style( self::HANDLE, ':root{' . $css . '}' ); } wp_enqueue_script( self::HANDLE ); } /** * Product id from shortcode attributes, or the current product. * * @param array $atts Shortcode attributes. * @param string $tag Shortcode name. * @return int 0 when not a product. */ public static function shortcode_product_id( $atts, $tag ) { $atts = shortcode_atts( array( 'product_id' => 0 ), $atts, $tag ); $product_id = absint( $atts['product_id'] ); if ( ! $product_id ) { global $product; $product_id = $product instanceof \WC_Product ? $product->get_id() : (int) get_the_ID(); } return 'product' === get_post_type( $product_id ) ? $product_id : 0; } public static function ajax() { nocache_headers(); $product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- read-only lookup, no state besides the visitor's own country preference. if ( ! $product_id || 'product' !== get_post_type( $product_id ) || 'publish' !== get_post_status( $product_id ) ) { wp_send_json_error( null, 400 ); } $chosen = isset( $_POST['country'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['country'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( $chosen && Country::is_allowed( $chosen ) ) { Country::remember( $chosen ); $country = $chosen; } else { $country = Country::resolve(); } $data = array( 'country' => $country, 'region' => Regions::of( $country ), 'html' => '', 'vars' => array(), ); $dynamic = array(); foreach ( Placeholders::definitions() as $name => $info ) { if ( $info['dynamic'] ) { $dynamic[ $name ] = true; } } if ( $dynamic ) { $data['vars'] = array_map( 'wp_strip_all_tags', array_intersect_key( Placeholders::values( $product_id, $country ), $dynamic ) ); } wp_send_json_success( apply_filters( 'cpod_ajax_data', $data, $product_id, $country ) ); } }