← Back
Editing: class-wcs-att-display.php
<?php /** * WCS_ATT_Display class * * @package WooCommerce All Products for Subscriptions * @since APFS 1.0.0 */ // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Front-end support and single-product template modifications. * * @class WCS_ATT_Display * @version 6.0.0 */ class WCS_ATT_Display { /** * Whether a 'wcsatt_enqueue_cart_script' callback turned the filter's historical default from true to false. * Written by frontend_scripts() on 'wp_enqueue_scripts'; readers must run later in the same request. * * @since 9.2.0 * * @var boolean */ private static $cart_script_vetoed = false; /** * Initialization. */ public static function init() { // Cart display hooks. WCS_ATT_Display_Cart::init(); // Single-product display hooks. WCS_ATT_Display_Product::init(); self::add_hooks(); } /** * Hook-in. */ private static function add_hooks() { // Enqueue scripts and styles. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'frontend_scripts' ) ); } /** * Enqueues the APFS frontend bundle. Call this from wherever the markup it drives is rendered. * * @since 9.2.0 * * @return void */ public static function enqueue_frontend_script() { wp_enqueue_script( 'wcsatt-frontend' ); } /** * Enqueues the bundle and the cart parameters it reads, unless 'wcsatt_enqueue_cart_script' vetoed them. * * Called from the cart and checkout renderer, and from frontend_scripts() when a callback forces the script onto a * page the renderer will not run on. * * @since 9.2.0 * * @return void */ public static function maybe_enqueue_cart_script() { if ( self::$cart_script_vetoed ) { return; } $cart_params = array( 'i18n_update_cart_sub_error' => __( 'Failed to update your cart. If this issue persists, please re-load the page and try again.', 'woocommerce-subscriptions' ), 'i18n_subs_load_error' => __( 'Failed to load matching subscriptions. If this issue persists, please re-load the page and try again.', 'woocommerce-subscriptions' ), 'update_cart_option_nonce' => wp_create_nonce( 'wcsatt_update_cart_option' ), 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), ); wp_localize_script( 'wcsatt-frontend', 'wcsatt_cart_params', $cart_params ); self::enqueue_frontend_script(); } /* |-------------------------------------------------------------------------- | Filters |-------------------------------------------------------------------------- */ /** * Front end styles and scripts. * * Registers the webpack-generated APFS frontend styles and consolidated JavaScript bundle, and enqueues the * stylesheet. The bundle is enqueued by whatever renders the markup it drives, through * WCS_ATT_Display::enqueue_frontend_script(). * RTL styles are automatically generated by webpack. * JavaScript bundle includes both single-add-to-cart.js and cart.js functionality. * * @return void */ public static function frontend_scripts() { // Load webpack-generated asset file with fallback. $asset_file_path = WCS_ATT()->plugin_path() . '/build/apfs-frontend.asset.php'; $asset_file = file_exists( $asset_file_path ) ? include $asset_file_path : array( 'dependencies' => array(), 'version' => WC_Subscriptions::$version, ); // Register the consolidated frontend CSS. wp_register_style( 'wcsatt-css', WCS_ATT()->plugin_url() . '/build/apfs-frontend.css', false, $asset_file['version'], 'all' ); wp_style_add_data( 'wcsatt-css', 'rtl', 'replace' ); // Not scoped like the bundle: this also styles the price detail lines of legacy subscription products and the // '.wcsatt-sub-discount' badge that 'woocommerce_get_price_html' injects into price HTML store-wide. wp_enqueue_style( 'wcsatt-css' ); // Merge webpack dependencies with WooCommerce-specific dependencies. // Webpack automatically detects jQuery and Backbone as externals, but we need to ensure // WooCommerce scripts are included as dependencies. $script_dependencies = array_merge( $asset_file['dependencies'], array( 'jquery', 'jquery-blockui', 'backbone', 'wc-country-select', 'wc-address-i18n' ) ); // Remove duplicates if webpack also added these. $script_dependencies = array_unique( $script_dependencies ); wp_register_script( 'wcsatt-frontend', WCS_ATT()->plugin_url() . '/build/apfs-frontend.js', $script_dependencies, $asset_file['version'], true ); wp_script_add_data( 'wcsatt-frontend', 'strategy', 'defer' ); // Localize script with single product parameters. Printed wherever the handle ends up enqueued. $single_product_params = array( 'i18n_subs_load_error' => __( 'Failed to load matching subscriptions. If this issue persists, please re-load the page and try again.', 'woocommerce-subscriptions' ), /* translators: %s: trial period string, e.g. "7 days" or "1 month" */ 'i18n_free_trial_label' => __( 'Free trial: %s', 'woocommerce-subscriptions' ), /* translators: %s: formatted signup fee amount */ 'i18n_signup_fee_label' => __( 'Sign-up fee: %s', 'woocommerce-subscriptions' ), 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), ); wp_localize_script( 'wcsatt-frontend', 'wcsatt_single_product_params', $single_product_params ); $default = is_cart() || ( is_checkout() && 'off' !== get_option( 'wcsatt_add_cart_to_subscription', 'off' ) ); /** * 'wcsatt_enqueue_cart_script' filter. * * Whether to load the APFS frontend bundle, and the 'wcsatt_cart_params' the cart module reads, for the "add * cart to an existing subscription" UI. Since 9.2.0, returning true where the default is already true no longer * enqueues by itself; the renderer decides. * * @since APFS 3.1.17 * * @param boolean $enqueue_script */ $filtered = (bool) apply_filters( 'wcsatt_enqueue_cart_script', $default ); // The bundle is normally enqueued by WCS_ATT_Manage_Add_Cart::options_template(), which runs too late to pass a // page condition to a filter that has always been dispatched from here. So dispatch it here regardless, with its // historical default, and hand the renderer the answer. Only a callback that turns the default's true into // false counts as a veto: where the default is already false the two are indistinguishable, and reading that as // a veto would break the cart shortcode on a page 'is_cart()' does not recognize. self::$cart_script_vetoed = $default && ! $filtered; // A callback said yes for a page the renderer will not run on. if ( $filtered && ! $default ) { self::maybe_enqueue_cart_script(); } } }
Save File
Cancel