abstracts
9 years ago
admin
9 years ago
api
9 years ago
cli
9 years ago
data-stores
9 years ago
emails
9 years ago
gateways
9 years ago
interfaces
9 years ago
legacy
9 years ago
libraries
9 years ago
log-handlers
9 years ago
payment-tokens
9 years ago
shipping
9 years ago
shortcodes
9 years ago
theme-support
9 years ago
vendor
9 years ago
walkers
9 years ago
widgets
9 years ago
class-wc-ajax.php
9 years ago
class-wc-api.php
9 years ago
class-wc-auth.php
9 years ago
class-wc-autoloader.php
9 years ago
class-wc-background-emailer.php
9 years ago
class-wc-background-updater.php
9 years ago
class-wc-breadcrumb.php
9 years ago
class-wc-cache-helper.php
9 years ago
class-wc-cart.php
9 years ago
class-wc-checkout.php
9 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
9 years ago
class-wc-countries.php
9 years ago
class-wc-coupon.php
9 years ago
class-wc-customer-download.php
9 years ago
class-wc-customer.php
9 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
9 years ago
class-wc-datetime.php
9 years ago
class-wc-deprecated-action-hooks.php
9 years ago
class-wc-deprecated-filter-hooks.php
9 years ago
class-wc-download-handler.php
9 years ago
class-wc-emails.php
9 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
9 years ago
class-wc-frontend-scripts.php
9 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
9 years ago
class-wc-https.php
9 years ago
class-wc-install.php
9 years ago
class-wc-integrations.php
9 years ago
class-wc-legacy-api.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
9 years ago
class-wc-order-factory.php
9 years ago
class-wc-order-item-coupon.php
9 years ago
class-wc-order-item-fee.php
9 years ago
class-wc-order-item-meta.php
9 years ago
class-wc-order-item-product.php
9 years ago
class-wc-order-item-shipping.php
9 years ago
class-wc-order-item-tax.php
9 years ago
class-wc-order-item.php
9 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
9 years ago
class-wc-payment-gateways.php
9 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
9 years ago
class-wc-post-types.php
9 years ago
class-wc-product-attribute.php
9 years ago
class-wc-product-download.php
9 years ago
class-wc-product-external.php
9 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
9 years ago
class-wc-product-simple.php
9 years ago
class-wc-product-variable.php
9 years ago
class-wc-product-variation.php
9 years ago
class-wc-query.php
9 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
9 years ago
class-wc-shipping-rate.php
9 years ago
class-wc-shipping-zone.php
9 years ago
class-wc-shipping-zones.php
9 years ago
class-wc-shipping.php
9 years ago
class-wc-shortcodes.php
9 years ago
class-wc-structured-data.php
9 years ago
class-wc-tax.php
9 years ago
class-wc-template-loader.php
9 years ago
class-wc-tracker.php
9 years ago
class-wc-validation.php
9 years ago
class-wc-webhook.php
9 years ago
wc-account-functions.php
9 years ago
wc-attribute-functions.php
9 years ago
wc-cart-functions.php
9 years ago
wc-conditional-functions.php
9 years ago
wc-core-functions.php
9 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
9 years ago
wc-formatting-functions.php
9 years ago
wc-notice-functions.php
9 years ago
wc-order-functions.php
9 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
9 years ago
wc-product-functions.php
9 years ago
wc-rest-functions.php
9 years ago
wc-stock-functions.php
9 years ago
wc-template-functions.php
9 years ago
wc-template-hooks.php
9 years ago
wc-term-functions.php
9 years ago
wc-update-functions.php
9 years ago
wc-user-functions.php
9 years ago
wc-webhook-functions.php
9 years ago
wc-widget-functions.php
9 years ago
class-wc-frontend-scripts.php
578 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handle frontend scripts |
| 4 | * |
| 5 | * @class WC_Frontend_Scripts |
| 6 | * @version 2.3.0 |
| 7 | * @package WooCommerce/Classes/ |
| 8 | * @category Class |
| 9 | * @author WooThemes |
| 10 | */ |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * WC_Frontend_Scripts Class. |
| 18 | */ |
| 19 | class WC_Frontend_Scripts { |
| 20 | |
| 21 | /** |
| 22 | * Contains an array of script handles registered by WC. |
| 23 | * @var array |
| 24 | */ |
| 25 | private static $scripts = array(); |
| 26 | |
| 27 | /** |
| 28 | * Contains an array of script handles registered by WC. |
| 29 | * @var array |
| 30 | */ |
| 31 | private static $styles = array(); |
| 32 | |
| 33 | /** |
| 34 | * Contains an array of script handles localized by WC. |
| 35 | * @var array |
| 36 | */ |
| 37 | private static $wp_localize_scripts = array(); |
| 38 | |
| 39 | /** |
| 40 | * Hook in methods. |
| 41 | */ |
| 42 | public static function init() { |
| 43 | add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) ); |
| 44 | add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 ); |
| 45 | add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 ); |
| 46 | add_action( 'setup_theme', array( __CLASS__, 'add_default_theme_support' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Add theme support for default WP themes. |
| 51 | * |
| 52 | * @since 3.0.0 |
| 53 | */ |
| 54 | public static function add_default_theme_support() { |
| 55 | if ( in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) { |
| 56 | add_theme_support( 'wc-product-gallery-zoom' ); |
| 57 | add_theme_support( 'wc-product-gallery-lightbox' ); |
| 58 | add_theme_support( 'wc-product-gallery-slider' ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get styles for the frontend. |
| 64 | * |
| 65 | * @return array |
| 66 | */ |
| 67 | public static function get_styles() { |
| 68 | return apply_filters( 'woocommerce_enqueue_styles', array( |
| 69 | 'woocommerce-layout' => array( |
| 70 | 'src' => self::get_asset_url( 'assets/css/woocommerce-layout.css' ), |
| 71 | 'deps' => '', |
| 72 | 'version' => WC_VERSION, |
| 73 | 'media' => 'all', |
| 74 | 'has_rtl' => true, |
| 75 | ), |
| 76 | 'woocommerce-smallscreen' => array( |
| 77 | 'src' => self::get_asset_url( 'assets/css/woocommerce-smallscreen.css' ), |
| 78 | 'deps' => 'woocommerce-layout', |
| 79 | 'version' => WC_VERSION, |
| 80 | 'media' => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', $breakpoint = '768px' ) . ')', |
| 81 | 'has_rtl' => true, |
| 82 | ), |
| 83 | 'woocommerce-general' => array( |
| 84 | 'src' => self::get_asset_url( 'assets/css/woocommerce.css' ), |
| 85 | 'deps' => '', |
| 86 | 'version' => WC_VERSION, |
| 87 | 'media' => 'all', |
| 88 | 'has_rtl' => true, |
| 89 | ), |
| 90 | ) ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return protocol relative asset URL. |
| 95 | * @param string $path |
| 96 | */ |
| 97 | private static function get_asset_url( $path ) { |
| 98 | return str_replace( array( 'http:', 'https:' ), '', plugins_url( $path, WC_PLUGIN_FILE ) ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Register a script for use. |
| 103 | * |
| 104 | * @uses wp_register_script() |
| 105 | * @access private |
| 106 | * @param string $handle |
| 107 | * @param string $path |
| 108 | * @param string[] $deps |
| 109 | * @param string $version |
| 110 | * @param boolean $in_footer |
| 111 | */ |
| 112 | private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) { |
| 113 | self::$scripts[] = $handle; |
| 114 | wp_register_script( $handle, $path, $deps, $version, $in_footer ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Register and enqueue a script for use. |
| 119 | * |
| 120 | * @uses wp_enqueue_script() |
| 121 | * @access private |
| 122 | * @param string $handle |
| 123 | * @param string $path |
| 124 | * @param string[] $deps |
| 125 | * @param string $version |
| 126 | * @param boolean $in_footer |
| 127 | */ |
| 128 | private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) { |
| 129 | if ( ! in_array( $handle, self::$scripts ) && $path ) { |
| 130 | self::register_script( $handle, $path, $deps, $version, $in_footer ); |
| 131 | } |
| 132 | wp_enqueue_script( $handle ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Register a style for use. |
| 137 | * |
| 138 | * @uses wp_register_style() |
| 139 | * @access private |
| 140 | * @param string $handle |
| 141 | * @param string $path |
| 142 | * @param string[] $deps |
| 143 | * @param string $version |
| 144 | * @param string $media |
| 145 | * @param boolean $has_rtl |
| 146 | */ |
| 147 | private static function register_style( $handle, $path, $deps = array(), $version = WC_VERSION, $media = 'all', $has_rtl = false ) { |
| 148 | self::$styles[] = $handle; |
| 149 | wp_register_style( $handle, $path, $deps, $version, $media ); |
| 150 | |
| 151 | if ( $has_rtl ) { |
| 152 | wp_style_add_data( $handle, 'rtl', 'replace' ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Register and enqueue a styles for use. |
| 158 | * |
| 159 | * @uses wp_enqueue_style() |
| 160 | * @access private |
| 161 | * @param string $handle |
| 162 | * @param string $path |
| 163 | * @param string[] $deps |
| 164 | * @param string $version |
| 165 | * @param string $media |
| 166 | * @param boolean $has_rtl |
| 167 | */ |
| 168 | private static function enqueue_style( $handle, $path = '', $deps = array(), $version = WC_VERSION, $media = 'all', $has_rtl = false ) { |
| 169 | if ( ! in_array( $handle, self::$styles ) && $path ) { |
| 170 | self::register_style( $handle, $path, $deps, $version, $media, $has_rtl ); |
| 171 | } |
| 172 | wp_enqueue_style( $handle ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Register all WC scripts. |
| 177 | */ |
| 178 | private static function register_scripts() { |
| 179 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 180 | $register_scripts = array( |
| 181 | 'flexslider' => array( |
| 182 | 'src' => self::get_asset_url( 'assets/js/flexslider/jquery.flexslider' . $suffix . '.js' ), |
| 183 | 'deps' => array( 'jquery' ), |
| 184 | 'version' => '2.6.1', |
| 185 | ), |
| 186 | 'js-cookie' => array( |
| 187 | 'src' => self::get_asset_url( 'assets/js/js-cookie/js.cookie' . $suffix . '.js' ), |
| 188 | 'deps' => array(), |
| 189 | 'version' => '2.1.3', |
| 190 | ), |
| 191 | 'jquery-blockui' => array( |
| 192 | 'src' => self::get_asset_url( 'assets/js/jquery-blockui/jquery.blockUI' . $suffix . '.js' ), |
| 193 | 'deps' => array( 'jquery' ), |
| 194 | 'version' => '2.70', |
| 195 | ), |
| 196 | 'jquery-cookie' => array( // deprecated. |
| 197 | 'src' => self::get_asset_url( 'assets/js/jquery-cookie/jquery.cookie' . $suffix . '.js' ), |
| 198 | 'deps' => array( 'jquery' ), |
| 199 | 'version' => '1.4.1', |
| 200 | ), |
| 201 | 'jquery-payment' => array( |
| 202 | 'src' => self::get_asset_url( 'assets/js/jquery-payment/jquery.payment' . $suffix . '.js' ), |
| 203 | 'deps' => array( 'jquery' ), |
| 204 | 'version' => '1.4.1', |
| 205 | ), |
| 206 | 'photoswipe' => array( |
| 207 | 'src' => self::get_asset_url( 'assets/js/photoswipe/photoswipe' . $suffix . '.js' ), |
| 208 | 'deps' => array(), |
| 209 | 'version' => '4.1.1', |
| 210 | ), |
| 211 | 'photoswipe-ui-default' => array( |
| 212 | 'src' => self::get_asset_url( 'assets/js/photoswipe/photoswipe-ui-default' . $suffix . '.js' ), |
| 213 | 'deps' => array( 'photoswipe' ), |
| 214 | 'version' => '4.1.1', |
| 215 | ), |
| 216 | 'prettyPhoto' => array( // deprecated. |
| 217 | 'src' => self::get_asset_url( 'assets/js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js' ), |
| 218 | 'deps' => array( 'jquery' ), |
| 219 | 'version' => '3.1.6', |
| 220 | ), |
| 221 | 'prettyPhoto-init' => array( // deprecated. |
| 222 | 'src' => self::get_asset_url( 'assets/js/prettyPhoto/jquery.prettyPhoto.init' . $suffix . '.js' ), |
| 223 | 'deps' => array( 'jquery', 'prettyPhoto' ), |
| 224 | 'version' => WC_VERSION, |
| 225 | ), |
| 226 | 'select2' => array( |
| 227 | 'src' => self::get_asset_url( 'assets/js/select2/select2.full' . $suffix . '.js' ), |
| 228 | 'deps' => array( 'jquery' ), |
| 229 | 'version' => '4.0.3', |
| 230 | ), |
| 231 | 'wc-address-i18n' => array( |
| 232 | 'src' => self::get_asset_url( 'assets/js/frontend/address-i18n' . $suffix . '.js' ), |
| 233 | 'deps' => array( 'jquery' ), |
| 234 | 'version' => WC_VERSION, |
| 235 | ), |
| 236 | 'wc-add-payment-method' => array( |
| 237 | 'src' => self::get_asset_url( 'assets/js/frontend/add-payment-method' . $suffix . '.js' ), |
| 238 | 'deps' => array( 'jquery', 'woocommerce' ), |
| 239 | 'version' => WC_VERSION, |
| 240 | ), |
| 241 | 'wc-cart' => array( |
| 242 | 'src' => self::get_asset_url( 'assets/js/frontend/cart' . $suffix . '.js' ), |
| 243 | 'deps' => array( 'jquery', 'wc-country-select', 'wc-address-i18n' ), |
| 244 | 'version' => WC_VERSION, |
| 245 | ), |
| 246 | 'wc-cart-fragments' => array( |
| 247 | 'src' => self::get_asset_url( 'assets/js/frontend/cart-fragments' . $suffix . '.js' ), |
| 248 | 'deps' => array( 'jquery', 'js-cookie' ), |
| 249 | 'version' => WC_VERSION, |
| 250 | ), |
| 251 | 'wc-checkout' => array( |
| 252 | 'src' => self::get_asset_url( 'assets/js/frontend/checkout' . $suffix . '.js' ), |
| 253 | 'deps' => array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ), |
| 254 | 'version' => WC_VERSION, |
| 255 | ), |
| 256 | 'wc-country-select' => array( |
| 257 | 'src' => self::get_asset_url( 'assets/js/frontend/country-select' . $suffix . '.js' ), |
| 258 | 'deps' => array( 'jquery' ), |
| 259 | 'version' => WC_VERSION, |
| 260 | ), |
| 261 | 'wc-credit-card-form' => array( |
| 262 | 'src' => self::get_asset_url( 'assets/js/frontend/credit-card-form' . $suffix . '.js' ), |
| 263 | 'deps' => array( 'jquery', 'jquery-payment' ), |
| 264 | 'version' => WC_VERSION, |
| 265 | ), |
| 266 | 'wc-add-to-cart' => array( |
| 267 | 'src' => self::get_asset_url( 'assets/js/frontend/add-to-cart' . $suffix . '.js' ), |
| 268 | 'deps' => array( 'jquery' ), |
| 269 | 'version' => WC_VERSION, |
| 270 | ), |
| 271 | 'wc-add-to-cart-variation' => array( |
| 272 | 'src' => self::get_asset_url( 'assets/js/frontend/add-to-cart-variation' . $suffix . '.js' ), |
| 273 | 'deps' => array( 'jquery', 'wp-util' ), |
| 274 | 'version' => WC_VERSION, |
| 275 | ), |
| 276 | 'wc-geolocation' => array( |
| 277 | 'src' => self::get_asset_url( 'assets/js/frontend/geolocation' . $suffix . '.js' ), |
| 278 | 'deps' => array( 'jquery' ), |
| 279 | 'version' => WC_VERSION, |
| 280 | ), |
| 281 | 'wc-lost-password' => array( |
| 282 | 'src' => self::get_asset_url( 'assets/js/frontend/lost-password' . $suffix . '.js' ), |
| 283 | 'deps' => array( 'jquery', 'woocommerce' ), |
| 284 | 'version' => WC_VERSION, |
| 285 | ), |
| 286 | 'wc-password-strength-meter' => array( |
| 287 | 'src' => self::get_asset_url( 'assets/js/frontend/password-strength-meter' . $suffix . '.js' ), |
| 288 | 'deps' => array( 'jquery', 'password-strength-meter' ), |
| 289 | 'version' => WC_VERSION, |
| 290 | ), |
| 291 | 'wc-single-product' => array( |
| 292 | 'src' => self::get_asset_url( 'assets/js/frontend/single-product' . $suffix . '.js' ), |
| 293 | 'deps' => array( 'jquery' ), |
| 294 | 'version' => WC_VERSION, |
| 295 | ), |
| 296 | 'woocommerce' => array( |
| 297 | 'src' => self::get_asset_url( 'assets/js/frontend/woocommerce' . $suffix . '.js' ), |
| 298 | 'deps' => array( 'jquery', 'jquery-blockui', 'js-cookie' ), |
| 299 | 'version' => WC_VERSION, |
| 300 | ), |
| 301 | 'zoom' => array( |
| 302 | 'src' => self::get_asset_url( 'assets/js/zoom/jquery.zoom' . $suffix . '.js' ), |
| 303 | 'deps' => array( 'jquery' ), |
| 304 | 'version' => '1.7.15', |
| 305 | ), |
| 306 | ); |
| 307 | foreach ( $register_scripts as $name => $props ) { |
| 308 | self::register_script( $name, $props['src'], $props['deps'], $props['version'] ); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Register all WC sty;es. |
| 314 | */ |
| 315 | private static function register_styles() { |
| 316 | $register_styles = array( |
| 317 | 'photoswipe' => array( |
| 318 | 'src' => self::get_asset_url( 'assets/css/photoswipe/photoswipe.css' ), |
| 319 | 'deps' => array(), |
| 320 | 'version' => WC_VERSION, |
| 321 | 'has_rtl' => false, |
| 322 | ), |
| 323 | 'photoswipe-default-skin' => array( |
| 324 | 'src' => self::get_asset_url( 'assets/css/photoswipe/default-skin/default-skin.css' ), |
| 325 | 'deps' => array( 'photoswipe' ), |
| 326 | 'version' => WC_VERSION, |
| 327 | 'has_rtl' => false, |
| 328 | ), |
| 329 | 'select2' => array( |
| 330 | 'src' => self::get_asset_url( 'assets/css/select2.css' ), |
| 331 | 'deps' => array(), |
| 332 | 'version' => WC_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' => WC_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_checkout() || is_account_page() ) { |
| 367 | self::enqueue_script( 'select2' ); |
| 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 = 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 | if ( $enqueue_styles = self::get_styles() ) { |
| 415 | foreach ( $enqueue_styles as $handle => $args ) { |
| 416 | if ( ! isset( $args['has_rtl'] ) ) { |
| 417 | $args['has_rtl'] = false; |
| 418 | } |
| 419 | |
| 420 | self::enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'], $args['has_rtl'] ); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Localize a WC script once. |
| 427 | * @access private |
| 428 | * @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. |
| 429 | * @param string $handle |
| 430 | */ |
| 431 | private static function localize_script( $handle ) { |
| 432 | if ( ! in_array( $handle, self::$wp_localize_scripts ) && wp_script_is( $handle ) && ( $data = self::get_script_data( $handle ) ) ) { |
| 433 | $name = str_replace( '-', '_', $handle ) . '_params'; |
| 434 | self::$wp_localize_scripts[] = $handle; |
| 435 | wp_localize_script( $handle, $name, apply_filters( $name, $data ) ); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Return data for script handles. |
| 441 | * @access private |
| 442 | * @param string $handle |
| 443 | * @return array|bool |
| 444 | */ |
| 445 | private static function get_script_data( $handle ) { |
| 446 | global $wp; |
| 447 | |
| 448 | switch ( $handle ) { |
| 449 | case 'woocommerce' : |
| 450 | return array( |
| 451 | 'ajax_url' => WC()->ajax_url(), |
| 452 | 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), |
| 453 | ); |
| 454 | break; |
| 455 | case 'wc-geolocation' : |
| 456 | return array( |
| 457 | 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), |
| 458 | 'home_url' => home_url(), |
| 459 | 'is_available' => ! ( is_cart() || is_account_page() || is_checkout() || is_customize_preview() ) ? '1' : '0', |
| 460 | 'hash' => isset( $_GET['v'] ) ? wc_clean( $_GET['v'] ) : '', |
| 461 | ); |
| 462 | break; |
| 463 | case 'wc-single-product' : |
| 464 | return array( |
| 465 | 'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ), |
| 466 | 'review_rating_required' => get_option( 'woocommerce_review_rating_required' ), |
| 467 | 'flexslider' => apply_filters( 'woocommerce_single_product_carousel_options', array( |
| 468 | 'rtl' => is_rtl(), |
| 469 | 'animation' => 'slide', |
| 470 | 'smoothHeight' => false, |
| 471 | 'directionNav' => false, |
| 472 | 'controlNav' => 'thumbnails', |
| 473 | 'slideshow' => false, |
| 474 | 'animationSpeed' => 500, |
| 475 | 'animationLoop' => false, // Breaks photoswipe pagination if true. |
| 476 | ) ), |
| 477 | 'zoom_enabled' => get_theme_support( 'wc-product-gallery-zoom' ), |
| 478 | 'photoswipe_enabled' => get_theme_support( 'wc-product-gallery-lightbox' ), |
| 479 | 'flexslider_enabled' => get_theme_support( 'wc-product-gallery-slider' ), |
| 480 | ); |
| 481 | break; |
| 482 | case 'wc-checkout' : |
| 483 | return array( |
| 484 | 'ajax_url' => WC()->ajax_url(), |
| 485 | 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), |
| 486 | 'update_order_review_nonce' => wp_create_nonce( 'update-order-review' ), |
| 487 | 'apply_coupon_nonce' => wp_create_nonce( 'apply-coupon' ), |
| 488 | 'remove_coupon_nonce' => wp_create_nonce( 'remove-coupon' ), |
| 489 | 'option_guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ), |
| 490 | 'checkout_url' => WC_AJAX::get_endpoint( "checkout" ), |
| 491 | 'is_checkout' => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0, |
| 492 | 'debug_mode' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
| 493 | 'i18n_checkout_error' => esc_attr__( 'Error processing checkout. Please try again.', 'woocommerce' ), |
| 494 | ); |
| 495 | break; |
| 496 | case 'wc-address-i18n' : |
| 497 | return array( |
| 498 | 'locale' => json_encode( WC()->countries->get_country_locale() ), |
| 499 | 'locale_fields' => json_encode( WC()->countries->get_country_locale_field_selectors() ), |
| 500 | 'i18n_required_text' => esc_attr__( 'required', 'woocommerce' ), |
| 501 | ); |
| 502 | break; |
| 503 | case 'wc-cart' : |
| 504 | return array( |
| 505 | 'ajax_url' => WC()->ajax_url(), |
| 506 | 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), |
| 507 | 'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ), |
| 508 | 'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ), |
| 509 | 'remove_coupon_nonce' => wp_create_nonce( "remove-coupon" ), |
| 510 | ); |
| 511 | break; |
| 512 | case 'wc-cart-fragments' : |
| 513 | return array( |
| 514 | 'ajax_url' => WC()->ajax_url(), |
| 515 | 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), |
| 516 | 'fragment_name' => apply_filters( 'woocommerce_cart_fragment_name', 'wc_fragments' ), |
| 517 | ); |
| 518 | break; |
| 519 | case 'wc-add-to-cart' : |
| 520 | return array( |
| 521 | 'ajax_url' => WC()->ajax_url(), |
| 522 | 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), |
| 523 | 'i18n_view_cart' => esc_attr__( 'View cart', 'woocommerce' ), |
| 524 | 'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url() ), |
| 525 | 'is_cart' => is_cart(), |
| 526 | 'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' ), |
| 527 | ); |
| 528 | break; |
| 529 | case 'wc-add-to-cart-variation' : |
| 530 | // We also need the wp.template for this script :) |
| 531 | wc_get_template( 'single-product/add-to-cart/variation.php' ); |
| 532 | |
| 533 | return array( |
| 534 | 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ), |
| 535 | 'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ), |
| 536 | 'i18n_make_a_selection_text' => esc_attr__( 'Please select some product options before adding this product to your cart.', 'woocommerce' ), |
| 537 | 'i18n_unavailable_text' => esc_attr__( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ), |
| 538 | ); |
| 539 | break; |
| 540 | case 'wc-country-select' : |
| 541 | return array( |
| 542 | 'countries' => json_encode( array_merge( WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states() ) ), |
| 543 | 'i18n_select_state_text' => esc_attr__( 'Select an option…', 'woocommerce' ), |
| 544 | 'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'woocommerce' ), |
| 545 | 'i18n_ajax_error' => _x( 'Loading failed', 'enhanced select', 'woocommerce' ), |
| 546 | 'i18n_input_too_short_1' => _x( 'Please enter 1 or more characters', 'enhanced select', 'woocommerce' ), |
| 547 | 'i18n_input_too_short_n' => _x( 'Please enter %qty% or more characters', 'enhanced select', 'woocommerce' ), |
| 548 | 'i18n_input_too_long_1' => _x( 'Please delete 1 character', 'enhanced select', 'woocommerce' ), |
| 549 | 'i18n_input_too_long_n' => _x( 'Please delete %qty% characters', 'enhanced select', 'woocommerce' ), |
| 550 | 'i18n_selection_too_long_1' => _x( 'You can only select 1 item', 'enhanced select', 'woocommerce' ), |
| 551 | 'i18n_selection_too_long_n' => _x( 'You can only select %qty% items', 'enhanced select', 'woocommerce' ), |
| 552 | 'i18n_load_more' => _x( 'Loading more results…', 'enhanced select', 'woocommerce' ), |
| 553 | 'i18n_searching' => _x( 'Searching…', 'enhanced select', 'woocommerce' ), |
| 554 | ); |
| 555 | break; |
| 556 | case 'wc-password-strength-meter' : |
| 557 | return array( |
| 558 | 'min_password_strength' => apply_filters( 'woocommerce_min_password_strength', 3 ), |
| 559 | 'i18n_password_error' => esc_attr__( 'Please enter a stronger password.', 'woocommerce' ), |
| 560 | 'i18n_password_hint' => esc_attr( wp_get_password_hint() ), |
| 561 | ); |
| 562 | break; |
| 563 | } |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Localize scripts only when enqueued. |
| 569 | */ |
| 570 | public static function localize_printed_scripts() { |
| 571 | foreach ( self::$scripts as $handle ) { |
| 572 | self::localize_script( $handle ); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | WC_Frontend_Scripts::init(); |
| 578 |