abstracts
5 years ago
admin
5 years ago
cli
6 years ago
customizer
6 years ago
data-stores
5 years ago
emails
6 years ago
export
5 years ago
gateways
6 years ago
import
5 years ago
integrations
6 years ago
interfaces
6 years ago
legacy
6 years ago
libraries
6 years ago
log-handlers
6 years ago
payment-tokens
6 years ago
queue
7 years ago
shipping
6 years ago
shortcodes
5 years ago
theme-support
6 years ago
tracks
6 years ago
traits
6 years ago
walkers
8 years ago
wccom-site
6 years ago
widgets
6 years ago
class-wc-ajax.php
6 years ago
class-wc-api.php
6 years ago
class-wc-auth.php
7 years ago
class-wc-autoloader.php
6 years ago
class-wc-background-emailer.php
6 years ago
class-wc-background-updater.php
7 years ago
class-wc-breadcrumb.php
7 years ago
class-wc-cache-helper.php
6 years ago
class-wc-cart-fees.php
6 years ago
class-wc-cart-session.php
6 years ago
class-wc-cart-totals.php
6 years ago
class-wc-cart.php
5 years ago
class-wc-checkout.php
5 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
5 years ago
class-wc-countries.php
5 years ago
class-wc-coupon.php
6 years ago
class-wc-customer-download-log.php
8 years ago
class-wc-customer-download.php
6 years ago
class-wc-customer.php
7 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
7 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
6 years ago
class-wc-download-handler.php
6 years ago
class-wc-emails.php
6 years ago
class-wc-embed.php
8 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
6 years ago
class-wc-geo-ip.php
8 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
6 years ago
class-wc-https.php
8 years ago
class-wc-install.php
5 years ago
class-wc-integrations.php
6 years ago
class-wc-log-levels.php
7 years ago
class-wc-logger.php
6 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
6 years ago
class-wc-order-item-coupon.php
7 years ago
class-wc-order-item-fee.php
7 years ago
class-wc-order-item-meta.php
7 years ago
class-wc-order-item-product.php
6 years ago
class-wc-order-item-shipping.php
7 years ago
class-wc-order-item-tax.php
6 years ago
class-wc-order-item.php
6 years ago
class-wc-order-query.php
7 years ago
class-wc-order-refund.php
8 years ago
class-wc-order.php
6 years ago
class-wc-payment-gateways.php
6 years ago
class-wc-payment-tokens.php
6 years ago
class-wc-post-data.php
6 years ago
class-wc-post-types.php
7 years ago
class-wc-privacy-background-process.php
8 years ago
class-wc-privacy-erasers.php
6 years ago
class-wc-privacy-exporters.php
6 years ago
class-wc-privacy.php
6 years ago
class-wc-product-attribute.php
7 years ago
class-wc-product-download.php
6 years ago
class-wc-product-external.php
8 years ago
class-wc-product-factory.php
7 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
7 years ago
class-wc-product-simple.php
6 years ago
class-wc-product-variable.php
6 years ago
class-wc-product-variation.php
7 years ago
class-wc-query.php
6 years ago
class-wc-rate-limiter.php
6 years ago
class-wc-regenerate-images-request.php
7 years ago
class-wc-regenerate-images.php
7 years ago
class-wc-register-wp-admin-settings.php
8 years ago
class-wc-rest-authentication.php
6 years ago
class-wc-rest-exception.php
6 years ago
class-wc-session-handler.php
6 years ago
class-wc-shipping-rate.php
8 years ago
class-wc-shipping-zone.php
6 years ago
class-wc-shipping-zones.php
6 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
6 years ago
class-wc-structured-data.php
6 years ago
class-wc-tax.php
6 years ago
class-wc-template-loader.php
6 years ago
class-wc-tracker.php
6 years ago
class-wc-validation.php
6 years ago
class-wc-webhook.php
6 years ago
class-woocommerce.php
5 years ago
wc-account-functions.php
6 years ago
wc-attribute-functions.php
5 years ago
wc-cart-functions.php
5 years ago
wc-conditional-functions.php
6 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
7 years ago
wc-deprecated-functions.php
6 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
6 years ago
wc-order-functions.php
6 years ago
wc-order-item-functions.php
6 years ago
wc-page-functions.php
6 years ago
wc-product-functions.php
6 years ago
wc-rest-functions.php
6 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
6 years ago
wc-term-functions.php
6 years ago
wc-update-functions.php
6 years ago
wc-user-functions.php
6 years ago
wc-webhook-functions.php
7 years ago
wc-widget-functions.php
8 years ago
class-wc-frontend-scripts.php
619 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handle frontend scripts |
| 4 | * |
| 5 | * @package WooCommerce/Classes |
| 6 | * @version 2.3.0 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Constants; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Frontend scripts class. |
| 17 | */ |
| 18 | class WC_Frontend_Scripts { |
| 19 | |
| 20 | /** |
| 21 | * Contains an array of script handles registered by WC. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | private static $scripts = array(); |
| 26 | |
| 27 | /** |
| 28 | * Contains an array of script handles registered by WC. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | private static $styles = array(); |
| 33 | |
| 34 | /** |
| 35 | * Contains an array of script handles localized by WC. |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | private static $wp_localize_scripts = array(); |
| 40 | |
| 41 | /** |
| 42 | * Hook in methods. |
| 43 | */ |
| 44 | public static function init() { |
| 45 | add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) ); |
| 46 | add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 ); |
| 47 | add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get styles for the frontend. |
| 52 | * |
| 53 | * @return array |
| 54 | */ |
| 55 | public static function get_styles() { |
| 56 | $version = Constants::get_constant( 'WC_VERSION' ); |
| 57 | |
| 58 | return apply_filters( |
| 59 | 'woocommerce_enqueue_styles', |
| 60 | array( |
| 61 | 'woocommerce-layout' => array( |
| 62 | 'src' => self::get_asset_url( 'assets/css/woocommerce-layout.css' ), |
| 63 | 'deps' => '', |
| 64 | 'version' => $version, |
| 65 | 'media' => 'all', |
| 66 | 'has_rtl' => true, |
| 67 | ), |
| 68 | 'woocommerce-smallscreen' => array( |
| 69 | 'src' => self::get_asset_url( 'assets/css/woocommerce-smallscreen.css' ), |
| 70 | 'deps' => 'woocommerce-layout', |
| 71 | 'version' => $version, |
| 72 | 'media' => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', '768px' ) . ')', |
| 73 | 'has_rtl' => true, |
| 74 | ), |
| 75 | 'woocommerce-general' => array( |
| 76 | 'src' => self::get_asset_url( 'assets/css/woocommerce.css' ), |
| 77 | 'deps' => '', |
| 78 | 'version' => $version, |
| 79 | 'media' => 'all', |
| 80 | 'has_rtl' => true, |
| 81 | ), |
| 82 | ) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Return asset URL. |
| 88 | * |
| 89 | * @param string $path Assets path. |
| 90 | * @return string |
| 91 | */ |
| 92 | private static function get_asset_url( $path ) { |
| 93 | return apply_filters( 'woocommerce_get_asset_url', plugins_url( $path, WC_PLUGIN_FILE ), $path ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Register a script for use. |
| 98 | * |
| 99 | * @uses wp_register_script() |
| 100 | * @param string $handle Name of the script. Should be unique. |
| 101 | * @param string $path Full URL of the script, or path of the script relative to the WordPress root directory. |
| 102 | * @param string[] $deps An array of registered script handles this script depends on. |
| 103 | * @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. |
| 104 | * @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'. |
| 105 | */ |
| 106 | private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) { |
| 107 | self::$scripts[] = $handle; |
| 108 | wp_register_script( $handle, $path, $deps, $version, $in_footer ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Register and enqueue a script for use. |
| 113 | * |
| 114 | * @uses wp_enqueue_script() |
| 115 | * @param string $handle Name of the script. Should be unique. |
| 116 | * @param string $path Full URL of the script, or path of the script relative to the WordPress root directory. |
| 117 | * @param string[] $deps An array of registered script handles this script depends on. |
| 118 | * @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. |
| 119 | * @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'. |
| 120 | */ |
| 121 | private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) { |
| 122 | if ( ! in_array( $handle, self::$scripts, true ) && $path ) { |
| 123 | self::register_script( $handle, $path, $deps, $version, $in_footer ); |
| 124 | } |
| 125 | wp_enqueue_script( $handle ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Register a style for use. |
| 130 | * |
| 131 | * @uses wp_register_style() |
| 132 | * @param string $handle Name of the stylesheet. Should be unique. |
| 133 | * @param string $path Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
| 134 | * @param string[] $deps An array of registered stylesheet handles this stylesheet depends on. |
| 135 | * @param string $version String specifying stylesheet version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. |
| 136 | * @param string $media The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
| 137 | * @param boolean $has_rtl If has RTL version to load too. |
| 138 | */ |
| 139 | private static function register_style( $handle, $path, $deps = array(), $version = WC_VERSION, $media = 'all', $has_rtl = false ) { |
| 140 | self::$styles[] = $handle; |
| 141 | wp_register_style( $handle, $path, $deps, $version, $media ); |
| 142 | |
| 143 | if ( $has_rtl ) { |
| 144 | wp_style_add_data( $handle, 'rtl', 'replace' ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Register and enqueue a styles for use. |
| 150 | * |
| 151 | * @uses wp_enqueue_style() |
| 152 | * @param string $handle Name of the stylesheet. Should be unique. |
| 153 | * @param string $path Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
| 154 | * @param string[] $deps An array of registered stylesheet handles this stylesheet depends on. |
| 155 | * @param string $version String specifying stylesheet version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. |
| 156 | * @param string $media The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'. |
| 157 | * @param boolean $has_rtl If has RTL version to load too. |
| 158 | */ |
| 159 | private static function enqueue_style( $handle, $path = '', $deps = array(), $version = WC_VERSION, $media = 'all', $has_rtl = false ) { |
| 160 | if ( ! in_array( $handle, self::$styles, true ) && $path ) { |
| 161 | self::register_style( $handle, $path, $deps, $version, $media, $has_rtl ); |
| 162 | } |
| 163 | wp_enqueue_style( $handle ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Register all WC scripts. |
| 168 | */ |
| 169 | private static function register_scripts() { |
| 170 | $suffix = Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min'; |
| 171 | $version = Constants::get_constant( 'WC_VERSION' ); |
| 172 | |
| 173 | $register_scripts = array( |
| 174 | 'flexslider' => array( |
| 175 | 'src' => self::get_asset_url( 'assets/js/flexslider/jquery.flexslider' . $suffix . '.js' ), |
| 176 | 'deps' => array( 'jquery' ), |
| 177 | 'version' => '2.7.2', |
| 178 | ), |
| 179 | 'js-cookie' => array( |
| 180 | 'src' => self::get_asset_url( 'assets/js/js-cookie/js.cookie' . $suffix . '.js' ), |
| 181 | 'deps' => array(), |
| 182 | 'version' => '2.1.4', |
| 183 | ), |
| 184 | 'jquery-blockui' => array( |
| 185 | 'src' => self::get_asset_url( 'assets/js/jquery-blockui/jquery.blockUI' . $suffix . '.js' ), |
| 186 | 'deps' => array( 'jquery' ), |
| 187 | 'version' => '2.70', |
| 188 | ), |
| 189 | 'jquery-cookie' => array( // deprecated. |
| 190 | 'src' => self::get_asset_url( 'assets/js/jquery-cookie/jquery.cookie' . $suffix . '.js' ), |
| 191 | 'deps' => array( 'jquery' ), |
| 192 | 'version' => '1.4.1', |
| 193 | ), |
| 194 | 'jquery-payment' => array( |
| 195 | 'src' => self::get_asset_url( 'assets/js/jquery-payment/jquery.payment' . $suffix . '.js' ), |
| 196 | 'deps' => array( 'jquery' ), |
| 197 | 'version' => '3.0.0', |
| 198 | ), |
| 199 | 'photoswipe' => array( |
| 200 | 'src' => self::get_asset_url( 'assets/js/photoswipe/photoswipe' . $suffix . '.js' ), |
| 201 | 'deps' => array(), |
| 202 | 'version' => '4.1.1', |
| 203 | ), |
| 204 | 'photoswipe-ui-default' => array( |
| 205 | 'src' => self::get_asset_url( 'assets/js/photoswipe/photoswipe-ui-default' . $suffix . '.js' ), |
| 206 | 'deps' => array( 'photoswipe' ), |
| 207 | 'version' => '4.1.1', |
| 208 | ), |
| 209 | 'prettyPhoto' => array( // deprecated. |
| 210 | 'src' => self::get_asset_url( 'assets/js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js' ), |
| 211 | 'deps' => array( 'jquery' ), |
| 212 | 'version' => '3.1.6', |
| 213 | ), |
| 214 | 'prettyPhoto-init' => array( // deprecated. |
| 215 | 'src' => self::get_asset_url( 'assets/js/prettyPhoto/jquery.prettyPhoto.init' . $suffix . '.js' ), |
| 216 | 'deps' => array( 'jquery', 'prettyPhoto' ), |
| 217 | 'version' => $version, |
| 218 | ), |
| 219 | 'select2' => array( |
| 220 | 'src' => self::get_asset_url( 'assets/js/select2/select2.full' . $suffix . '.js' ), |
| 221 | 'deps' => array( 'jquery' ), |
| 222 | 'version' => '4.0.3', |
| 223 | ), |
| 224 | 'selectWoo' => array( |
| 225 | 'src' => self::get_asset_url( 'assets/js/selectWoo/selectWoo.full' . $suffix . '.js' ), |
| 226 | 'deps' => array( 'jquery' ), |
| 227 | 'version' => '1.0.6', |
| 228 | ), |
| 229 | 'wc-address-i18n' => array( |
| 230 | 'src' => self::get_asset_url( 'assets/js/frontend/address-i18n' . $suffix . '.js' ), |
| 231 | 'deps' => array( 'jquery', 'wc-country-select' ), |
| 232 | 'version' => $version, |
| 233 | ), |
| 234 | 'wc-add-payment-method' => array( |
| 235 | 'src' => self::get_asset_url( 'assets/js/frontend/add-payment-method' . $suffix . '.js' ), |
| 236 | 'deps' => array( 'jquery', 'woocommerce' ), |
| 237 | 'version' => $version, |
| 238 | ), |
| 239 | 'wc-cart' => array( |
| 240 | 'src' => self::get_asset_url( 'assets/js/frontend/cart' . $suffix . '.js' ), |
| 241 | 'deps' => array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ), |
| 242 | 'version' => $version, |
| 243 | ), |
| 244 | 'wc-cart-fragments' => array( |
| 245 | 'src' => self::get_asset_url( 'assets/js/frontend/cart-fragments' . $suffix . '.js' ), |
| 246 | 'deps' => array( 'jquery', 'js-cookie' ), |
| 247 | 'version' => $version, |
| 248 | ), |
| 249 | 'wc-checkout' => array( |
| 250 | 'src' => self::get_asset_url( 'assets/js/frontend/checkout' . $suffix . '.js' ), |
| 251 | 'deps' => array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ), |
| 252 | 'version' => $version, |
| 253 | ), |
| 254 | 'wc-country-select' => array( |
| 255 | 'src' => self::get_asset_url( 'assets/js/frontend/country-select' . $suffix . '.js' ), |
| 256 | 'deps' => array( 'jquery' ), |
| 257 | 'version' => $version, |
| 258 | ), |
| 259 | 'wc-credit-card-form' => array( |
| 260 | 'src' => self::get_asset_url( 'assets/js/frontend/credit-card-form' . $suffix . '.js' ), |
| 261 | 'deps' => array( 'jquery', 'jquery-payment' ), |
| 262 | 'version' => $version, |
| 263 | ), |
| 264 | 'wc-add-to-cart' => array( |
| 265 | 'src' => self::get_asset_url( 'assets/js/frontend/add-to-cart' . $suffix . '.js' ), |
| 266 | 'deps' => array( 'jquery', 'jquery-blockui' ), |
| 267 | 'version' => $version, |
| 268 | ), |
| 269 | 'wc-add-to-cart-variation' => array( |
| 270 | 'src' => self::get_asset_url( 'assets/js/frontend/add-to-cart-variation' . $suffix . '.js' ), |
| 271 | 'deps' => array( 'jquery', 'wp-util', 'jquery-blockui' ), |
| 272 | 'version' => $version, |
| 273 | ), |
| 274 | 'wc-geolocation' => array( |
| 275 | 'src' => self::get_asset_url( 'assets/js/frontend/geolocation' . $suffix . '.js' ), |
| 276 | 'deps' => array( 'jquery' ), |
| 277 | 'version' => $version, |
| 278 | ), |
| 279 | 'wc-lost-password' => array( |
| 280 | 'src' => self::get_asset_url( 'assets/js/frontend/lost-password' . $suffix . '.js' ), |
| 281 | 'deps' => array( 'jquery', 'woocommerce' ), |
| 282 | 'version' => $version, |
| 283 | ), |
| 284 | 'wc-password-strength-meter' => array( |
| 285 | 'src' => self::get_asset_url( 'assets/js/frontend/password-strength-meter' . $suffix . '.js' ), |
| 286 | 'deps' => array( 'jquery', 'password-strength-meter' ), |
| 287 | 'version' => $version, |
| 288 | ), |
| 289 | 'wc-single-product' => array( |
| 290 | 'src' => self::get_asset_url( 'assets/js/frontend/single-product' . $suffix . '.js' ), |
| 291 | 'deps' => array( 'jquery' ), |
| 292 | 'version' => $version, |
| 293 | ), |
| 294 | 'woocommerce' => array( |
| 295 | 'src' => self::get_asset_url( 'assets/js/frontend/woocommerce' . $suffix . '.js' ), |
| 296 | 'deps' => array( 'jquery', 'jquery-blockui', 'js-cookie' ), |
| 297 | 'version' => $version, |
| 298 | ), |
| 299 | 'zoom' => array( |
| 300 | 'src' => self::get_asset_url( 'assets/js/zoom/jquery.zoom' . $suffix . '.js' ), |
| 301 | 'deps' => array( 'jquery' ), |
| 302 | 'version' => '1.7.21', |
| 303 | ), |
| 304 | ); |
| 305 | foreach ( $register_scripts as $name => $props ) { |
| 306 | self::register_script( $name, $props['src'], $props['deps'], $props['version'] ); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Register all WC sty;es. |
| 312 | */ |
| 313 | private static function register_styles() { |
| 314 | $version = Constants::get_constant( 'WC_VERSION' ); |
| 315 | |
| 316 | $register_styles = array( |
| 317 | 'photoswipe' => array( |
| 318 | 'src' => self::get_asset_url( 'assets/css/photoswipe/photoswipe.min.css' ), |
| 319 | 'deps' => array(), |
| 320 | 'version' => $version, |
| 321 | 'has_rtl' => false, |
| 322 | ), |
| 323 | 'photoswipe-default-skin' => array( |
| 324 | 'src' => self::get_asset_url( 'assets/css/photoswipe/default-skin/default-skin.min.css' ), |
| 325 | 'deps' => array( 'photoswipe' ), |
| 326 | 'version' => $version, |
| 327 | 'has_rtl' => false, |
| 328 | ), |
| 329 | 'select2' => array( |
| 330 | 'src' => self::get_asset_url( 'assets/css/select2.css' ), |
| 331 | 'deps' => array(), |
| 332 | 'version' => $version, |
| 333 | 'has_rtl' => false, |
| 334 | ), |
| 335 | 'woocommerce_prettyPhoto_css' => array( // deprecated. |
| 336 | 'src' => self::get_asset_url( 'assets/css/prettyPhoto.css' ), |
| 337 | 'deps' => array(), |
| 338 | 'version' => $version, |
| 339 | 'has_rtl' => true, |
| 340 | ), |
| 341 | ); |
| 342 | foreach ( $register_styles as $name => $props ) { |
| 343 | self::register_style( $name, $props['src'], $props['deps'], $props['version'], 'all', $props['has_rtl'] ); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Register/queue frontend scripts. |
| 349 | */ |
| 350 | public static function load_scripts() { |
| 351 | global $post; |
| 352 | |
| 353 | if ( ! did_action( 'before_woocommerce_init' ) ) { |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | self::register_scripts(); |
| 358 | self::register_styles(); |
| 359 | |
| 360 | if ( 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' ) ) { |
| 361 | self::enqueue_script( 'wc-add-to-cart' ); |
| 362 | } |
| 363 | if ( is_cart() ) { |
| 364 | self::enqueue_script( 'wc-cart' ); |
| 365 | } |
| 366 | if ( is_cart() || is_checkout() || is_account_page() ) { |
| 367 | self::enqueue_script( 'selectWoo' ); |
| 368 | self::enqueue_style( 'select2' ); |
| 369 | |
| 370 | // Password strength meter. Load in checkout, account login and edit account page. |
| 371 | if ( ( 'no' === get_option( 'woocommerce_registration_generate_password' ) && ! is_user_logged_in() ) || is_edit_account_page() || is_lost_password_page() ) { |
| 372 | self::enqueue_script( 'wc-password-strength-meter' ); |
| 373 | } |
| 374 | } |
| 375 | if ( is_checkout() ) { |
| 376 | self::enqueue_script( 'wc-checkout' ); |
| 377 | } |
| 378 | if ( is_add_payment_method_page() ) { |
| 379 | self::enqueue_script( 'wc-add-payment-method' ); |
| 380 | } |
| 381 | if ( is_lost_password_page() ) { |
| 382 | self::enqueue_script( 'wc-lost-password' ); |
| 383 | } |
| 384 | |
| 385 | // Load gallery scripts on product pages only if supported. |
| 386 | if ( is_product() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) { |
| 387 | if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) { |
| 388 | self::enqueue_script( 'zoom' ); |
| 389 | } |
| 390 | if ( current_theme_supports( 'wc-product-gallery-slider' ) ) { |
| 391 | self::enqueue_script( 'flexslider' ); |
| 392 | } |
| 393 | if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) { |
| 394 | self::enqueue_script( 'photoswipe-ui-default' ); |
| 395 | self::enqueue_style( 'photoswipe-default-skin' ); |
| 396 | add_action( 'wp_footer', 'woocommerce_photoswipe' ); |
| 397 | } |
| 398 | self::enqueue_script( 'wc-single-product' ); |
| 399 | } |
| 400 | |
| 401 | if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) ) { |
| 402 | $ua = strtolower( wc_get_user_agent() ); // Exclude common bots from geolocation by user agent. |
| 403 | |
| 404 | if ( ! strstr( $ua, 'bot' ) && ! strstr( $ua, 'spider' ) && ! strstr( $ua, 'crawl' ) ) { |
| 405 | self::enqueue_script( 'wc-geolocation' ); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // Global frontend scripts. |
| 410 | self::enqueue_script( 'woocommerce' ); |
| 411 | self::enqueue_script( 'wc-cart-fragments' ); |
| 412 | |
| 413 | // CSS Styles. |
| 414 | $enqueue_styles = self::get_styles(); |
| 415 | if ( $enqueue_styles ) { |
| 416 | foreach ( $enqueue_styles as $handle => $args ) { |
| 417 | if ( ! isset( $args['has_rtl'] ) ) { |
| 418 | $args['has_rtl'] = false; |
| 419 | } |
| 420 | |
| 421 | self::enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'], $args['has_rtl'] ); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Placeholder style. |
| 426 | wp_register_style( 'woocommerce-inline', false ); // phpcs:ignore |
| 427 | wp_enqueue_style( 'woocommerce-inline' ); |
| 428 | |
| 429 | if ( true === wc_string_to_bool( get_option( 'woocommerce_checkout_highlight_required_fields', 'yes' ) ) ) { |
| 430 | wp_add_inline_style( 'woocommerce-inline', '.woocommerce form .form-row .required { visibility: visible; }' ); |
| 431 | } else { |
| 432 | wp_add_inline_style( 'woocommerce-inline', '.woocommerce form .form-row .required { visibility: hidden; }' ); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Localize a WC script once. |
| 438 | * |
| 439 | * @since 2.3.0 this needs less wp_script_is() calls due to https://core.trac.wordpress.org/ticket/28404 being added in WP 4.0. |
| 440 | * @param string $handle Script handle the data will be attached to. |
| 441 | */ |
| 442 | private static function localize_script( $handle ) { |
| 443 | if ( ! in_array( $handle, self::$wp_localize_scripts, true ) && wp_script_is( $handle ) ) { |
| 444 | $data = self::get_script_data( $handle ); |
| 445 | |
| 446 | if ( ! $data ) { |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | $name = str_replace( '-', '_', $handle ) . '_params'; |
| 451 | self::$wp_localize_scripts[] = $handle; |
| 452 | wp_localize_script( $handle, $name, apply_filters( $name, $data ) ); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Return data for script handles. |
| 458 | * |
| 459 | * @param string $handle Script handle the data will be attached to. |
| 460 | * @return array|bool |
| 461 | */ |
| 462 | private static function get_script_data( $handle ) { |
| 463 | global $wp; |
| 464 | |
| 465 | switch ( $handle ) { |
| 466 | case 'woocommerce': |
| 467 | $params = array( |
| 468 | 'ajax_url' => WC()->ajax_url(), |
| 469 | 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 470 | ); |
| 471 | break; |
| 472 | case 'wc-geolocation': |
| 473 | $params = array( |
| 474 | 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 475 | 'home_url' => remove_query_arg( 'lang', home_url() ), // FIX for WPML compatibility. |
| 476 | 'is_available' => ! ( is_cart() || is_account_page() || is_checkout() || is_customize_preview() ) ? '1' : '0', |
| 477 | 'hash' => isset( $_GET['v'] ) ? wc_clean( wp_unslash( $_GET['v'] ) ) : '', // WPCS: input var ok, CSRF ok. |
| 478 | ); |
| 479 | break; |
| 480 | case 'wc-single-product': |
| 481 | $params = array( |
| 482 | 'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ), |
| 483 | 'review_rating_required' => wc_review_ratings_required() ? 'yes' : 'no', |
| 484 | 'flexslider' => apply_filters( |
| 485 | 'woocommerce_single_product_carousel_options', |
| 486 | array( |
| 487 | 'rtl' => is_rtl(), |
| 488 | 'animation' => 'slide', |
| 489 | 'smoothHeight' => true, |
| 490 | 'directionNav' => false, |
| 491 | 'controlNav' => 'thumbnails', |
| 492 | 'slideshow' => false, |
| 493 | 'animationSpeed' => 500, |
| 494 | 'animationLoop' => false, // Breaks photoswipe pagination if true. |
| 495 | 'allowOneSlide' => false, |
| 496 | ) |
| 497 | ), |
| 498 | 'zoom_enabled' => apply_filters( 'woocommerce_single_product_zoom_enabled', get_theme_support( 'wc-product-gallery-zoom' ) ), |
| 499 | 'zoom_options' => apply_filters( 'woocommerce_single_product_zoom_options', array() ), |
| 500 | 'photoswipe_enabled' => apply_filters( 'woocommerce_single_product_photoswipe_enabled', get_theme_support( 'wc-product-gallery-lightbox' ) ), |
| 501 | 'photoswipe_options' => apply_filters( |
| 502 | 'woocommerce_single_product_photoswipe_options', |
| 503 | array( |
| 504 | 'shareEl' => false, |
| 505 | 'closeOnScroll' => false, |
| 506 | 'history' => false, |
| 507 | 'hideAnimationDuration' => 0, |
| 508 | 'showAnimationDuration' => 0, |
| 509 | ) |
| 510 | ), |
| 511 | 'flexslider_enabled' => apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) ), |
| 512 | ); |
| 513 | break; |
| 514 | case 'wc-checkout': |
| 515 | $params = array( |
| 516 | 'ajax_url' => WC()->ajax_url(), |
| 517 | 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 518 | 'update_order_review_nonce' => wp_create_nonce( 'update-order-review' ), |
| 519 | 'apply_coupon_nonce' => wp_create_nonce( 'apply-coupon' ), |
| 520 | 'remove_coupon_nonce' => wp_create_nonce( 'remove-coupon' ), |
| 521 | 'option_guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ), |
| 522 | 'checkout_url' => WC_AJAX::get_endpoint( 'checkout' ), |
| 523 | 'is_checkout' => is_checkout() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0, |
| 524 | 'debug_mode' => Constants::is_true( 'WP_DEBUG' ), |
| 525 | 'i18n_checkout_error' => esc_attr__( 'Error processing checkout. Please try again.', 'woocommerce' ), |
| 526 | ); |
| 527 | break; |
| 528 | case 'wc-address-i18n': |
| 529 | $params = array( |
| 530 | 'locale' => wp_json_encode( WC()->countries->get_country_locale() ), |
| 531 | 'locale_fields' => wp_json_encode( WC()->countries->get_country_locale_field_selectors() ), |
| 532 | 'i18n_required_text' => esc_attr__( 'required', 'woocommerce' ), |
| 533 | 'i18n_optional_text' => esc_html__( 'optional', 'woocommerce' ), |
| 534 | ); |
| 535 | break; |
| 536 | case 'wc-cart': |
| 537 | $params = array( |
| 538 | 'ajax_url' => WC()->ajax_url(), |
| 539 | 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 540 | 'update_shipping_method_nonce' => wp_create_nonce( 'update-shipping-method' ), |
| 541 | 'apply_coupon_nonce' => wp_create_nonce( 'apply-coupon' ), |
| 542 | 'remove_coupon_nonce' => wp_create_nonce( 'remove-coupon' ), |
| 543 | ); |
| 544 | break; |
| 545 | case 'wc-cart-fragments': |
| 546 | $params = array( |
| 547 | 'ajax_url' => WC()->ajax_url(), |
| 548 | 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 549 | 'cart_hash_key' => apply_filters( 'woocommerce_cart_hash_key', 'wc_cart_hash_' . md5( get_current_blog_id() . '_' . get_site_url( get_current_blog_id(), '/' ) . get_template() ) ), |
| 550 | 'fragment_name' => apply_filters( 'woocommerce_cart_fragment_name', 'wc_fragments_' . md5( get_current_blog_id() . '_' . get_site_url( get_current_blog_id(), '/' ) . get_template() ) ), |
| 551 | 'request_timeout' => 5000, |
| 552 | ); |
| 553 | break; |
| 554 | case 'wc-add-to-cart': |
| 555 | $params = array( |
| 556 | 'ajax_url' => WC()->ajax_url(), |
| 557 | 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 558 | 'i18n_view_cart' => esc_attr__( 'View cart', 'woocommerce' ), |
| 559 | 'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url(), null ), |
| 560 | 'is_cart' => is_cart(), |
| 561 | 'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' ), |
| 562 | ); |
| 563 | break; |
| 564 | case 'wc-add-to-cart-variation': |
| 565 | // We also need the wp.template for this script :). |
| 566 | wc_get_template( 'single-product/add-to-cart/variation.php' ); |
| 567 | |
| 568 | $params = array( |
| 569 | 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 570 | 'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ), |
| 571 | 'i18n_make_a_selection_text' => esc_attr__( 'Please select some product options before adding this product to your cart.', 'woocommerce' ), |
| 572 | 'i18n_unavailable_text' => esc_attr__( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ), |
| 573 | ); |
| 574 | break; |
| 575 | case 'wc-country-select': |
| 576 | $params = array( |
| 577 | 'countries' => wp_json_encode( array_merge( WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states() ) ), |
| 578 | 'i18n_select_state_text' => esc_attr__( 'Select an option…', 'woocommerce' ), |
| 579 | 'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'woocommerce' ), |
| 580 | 'i18n_ajax_error' => _x( 'Loading failed', 'enhanced select', 'woocommerce' ), |
| 581 | 'i18n_input_too_short_1' => _x( 'Please enter 1 or more characters', 'enhanced select', 'woocommerce' ), |
| 582 | 'i18n_input_too_short_n' => _x( 'Please enter %qty% or more characters', 'enhanced select', 'woocommerce' ), |
| 583 | 'i18n_input_too_long_1' => _x( 'Please delete 1 character', 'enhanced select', 'woocommerce' ), |
| 584 | 'i18n_input_too_long_n' => _x( 'Please delete %qty% characters', 'enhanced select', 'woocommerce' ), |
| 585 | 'i18n_selection_too_long_1' => _x( 'You can only select 1 item', 'enhanced select', 'woocommerce' ), |
| 586 | 'i18n_selection_too_long_n' => _x( 'You can only select %qty% items', 'enhanced select', 'woocommerce' ), |
| 587 | 'i18n_load_more' => _x( 'Loading more results…', 'enhanced select', 'woocommerce' ), |
| 588 | 'i18n_searching' => _x( 'Searching…', 'enhanced select', 'woocommerce' ), |
| 589 | ); |
| 590 | break; |
| 591 | case 'wc-password-strength-meter': |
| 592 | $params = array( |
| 593 | 'min_password_strength' => apply_filters( 'woocommerce_min_password_strength', 3 ), |
| 594 | 'stop_checkout' => apply_filters( 'woocommerce_enforce_password_strength_meter_on_checkout', false ), |
| 595 | 'i18n_password_error' => esc_attr__( 'Please enter a stronger password.', 'woocommerce' ), |
| 596 | 'i18n_password_hint' => esc_attr( wp_get_password_hint() ), |
| 597 | ); |
| 598 | break; |
| 599 | default: |
| 600 | $params = false; |
| 601 | } |
| 602 | |
| 603 | $params = apply_filters_deprecated( $handle . '_params', array( $params ), '3.0.0', 'woocommerce_get_script_data' ); |
| 604 | |
| 605 | return apply_filters( 'woocommerce_get_script_data', $params, $handle ); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Localize scripts only when enqueued. |
| 610 | */ |
| 611 | public static function localize_printed_scripts() { |
| 612 | foreach ( self::$scripts as $handle ) { |
| 613 | self::localize_script( $handle ); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | WC_Frontend_Scripts::init(); |
| 619 |