WC_Frontend_Scripts::enqueue_style()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
eloc 4
nc 2
nop 5
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
	}
47
48
	/**
49
	 * Get styles for the frontend.
50
	 * @access private
51
	 * @return array
52
	 */
53
	public static function get_styles() {
54
		return apply_filters( 'woocommerce_enqueue_styles', array(
55
			'woocommerce-layout' => array(
56
				'src'     => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce-layout.css',
57
				'deps'    => '',
58
				'version' => WC_VERSION,
59
				'media'   => 'all'
60
			),
61
			'woocommerce-smallscreen' => array(
62
				'src'     => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce-smallscreen.css',
63
				'deps'    => 'woocommerce-layout',
64
				'version' => WC_VERSION,
65
				'media'   => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', $breakpoint = '768px' ) . ')'
66
			),
67
			'woocommerce-general' => array(
68
				'src'     => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce.css',
69
				'deps'    => '',
70
				'version' => WC_VERSION,
71
				'media'   => 'all'
72
			),
73
		) );
74
	}
75
76
	/**
77
	 * Register a script for use.
78
	 *
79
	 * @uses   wp_register_script()
80
	 * @access private
81
	 * @param  string   $handle
82
	 * @param  string   $path
83
	 * @param  string[] $deps
84
	 * @param  string   $version
85
	 * @param  boolean  $in_footer
86
	 */
87
	private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
88
		self::$scripts[] = $handle;
89
		wp_register_script( $handle, $path, $deps, $version, $in_footer );
90
	}
91
92
	/**
93
	 * Register and enqueue a script for use.
94
	 *
95
	 * @uses   wp_enqueue_script()
96
	 * @access private
97
	 * @param  string   $handle
98
	 * @param  string   $path
99
	 * @param  string[] $deps
100
	 * @param  string   $version
101
	 * @param  boolean  $in_footer
102
	 */
103
	private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
104
		if ( ! in_array( $handle, self::$scripts ) && $path ) {
105
			self::register_script( $handle, $path, $deps, $version, $in_footer );
106
		}
107
		wp_enqueue_script( $handle );
108
	}
109
110
	/**
111
	 * Register a style for use.
112
	 *
113
	 * @uses   wp_register_style()
114
	 * @access private
115
	 * @param  string   $handle
116
	 * @param  string   $path
117
	 * @param  string[] $deps
118
	 * @param  string   $version
119
	 * @param  string   $media
120
	 */
121
	private static function register_style( $handle, $path, $deps = array(), $version = WC_VERSION, $media = 'all' ) {
122
		self::$styles[] = $handle;
123
		wp_register_style( $handle, $path, $deps, $version, $media );
124
	}
125
126
	/**
127
	 * Register and enqueue a styles for use.
128
	 *
129
	 * @uses   wp_enqueue_style()
130
	 * @access private
131
	 * @param  string   $handle
132
	 * @param  string   $path
133
	 * @param  string[] $deps
134
	 * @param  string   $version
135
	 * @param  string   $media
136
	 */
137
	private static function enqueue_style( $handle, $path = '', $deps = array(), $version = WC_VERSION, $media = 'all' ) {
138
		if ( ! in_array( $handle, self::$styles ) && $path ) {
139
			self::register_style( $handle, $path, $deps, $version, $media );
140
		}
141
		wp_enqueue_style( $handle );
142
	}
143
144
	/**
145
	 * Register/queue frontend scripts.
146
	 */
147
	public static function load_scripts() {
148
		global $post;
149
150
		if ( ! did_action( 'before_woocommerce_init' ) ) {
151
			return;
152
		}
153
154
		$suffix               = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
155
		$lightbox_en          = 'yes' === get_option( 'woocommerce_enable_lightbox' );
156
		$ajax_cart_en         = 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' );
157
		$assets_path          = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/';
158
		$frontend_script_path = $assets_path . 'js/frontend/';
159
160
		// Register any scripts for later use, or used as dependencies
161
		self::register_script( 'select2', $assets_path . 'js/select2/select2' . $suffix . '.js', array( 'jquery' ), '3.5.4' );
162
		self::register_script( 'jquery-blockui', $assets_path . 'js/jquery-blockui/jquery.blockUI' . $suffix . '.js', array( 'jquery' ), '2.70' );
163
		self::register_script( 'jquery-payment', $assets_path . 'js/jquery-payment/jquery.payment' . $suffix . '.js', array( 'jquery' ), '1.4.1' );
164
		self::register_script( 'jquery-cookie', $assets_path . 'js/jquery-cookie/jquery.cookie' . $suffix . '.js', array( 'jquery' ), '1.4.1' );
165
		self::register_script( 'wc-credit-card-form', $frontend_script_path . 'credit-card-form' . $suffix . '.js', array( 'jquery', 'jquery-payment' ) );
166
		self::register_script( 'wc-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js', array( 'jquery', 'wp-util' ) );
167
		self::register_script( 'wc-single-product', $frontend_script_path . 'single-product' . $suffix . '.js', array( 'jquery' ) );
168
		self::register_script( 'wc-country-select', $frontend_script_path . 'country-select' . $suffix . '.js', array( 'jquery' ) );
169
		self::register_script( 'wc-address-i18n', $frontend_script_path . 'address-i18n' . $suffix . '.js', array( 'jquery' ) );
170
		self::register_script( 'wc-password-strength-meter', $frontend_script_path . 'password-strength-meter' . $suffix . '.js', array( 'jquery', 'password-strength-meter' ) );
171
172
		// Register frontend scripts conditionally
173
		if ( $ajax_cart_en ) {
174
			self::enqueue_script( 'wc-add-to-cart', $frontend_script_path . 'add-to-cart' . $suffix . '.js' );
175
		}
176
		if ( is_cart() ) {
177
			self::enqueue_script( 'wc-cart', $frontend_script_path . 'cart' . $suffix . '.js', array( 'jquery', 'wc-country-select', 'wc-address-i18n' ) );
178
		}
179
		if ( is_checkout() || is_account_page() ) {
180
			self::enqueue_script( 'select2' );
181
			self::enqueue_style( 'select2', $assets_path . 'css/select2.css' );
182
183
			// Password strength meter.
184
			// Load in checkout, account login and edit account page.
185
			if ( ( 'no' === get_option( 'woocommerce_registration_generate_password' ) && ! is_user_logged_in() ) || is_edit_account_page() ) {
186
				self::enqueue_script( 'wc-password-strength-meter' );
187
			}
188
		}
189
		if ( is_checkout() ) {
190
			self::enqueue_script( 'wc-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ) );
191
		}
192
		if ( is_add_payment_method_page() ) {
193
			self::enqueue_script( 'wc-add-payment-method', $frontend_script_path . 'add-payment-method' . $suffix . '.js', array( 'jquery', 'woocommerce' ) );
194
		}
195
		if ( is_lost_password_page() ) {
196
			self::enqueue_script( 'wc-lost-password', $frontend_script_path . 'lost-password' . $suffix . '.js', array( 'jquery', 'woocommerce' ) );
197
		}
198
		if ( $lightbox_en && ( is_product() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) ) {
199
			self::enqueue_script( 'prettyPhoto', $assets_path . 'js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js', array( 'jquery' ), '3.1.6', true );
200
			self::enqueue_script( 'prettyPhoto-init', $assets_path . 'js/prettyPhoto/jquery.prettyPhoto.init' . $suffix . '.js', array( 'jquery','prettyPhoto' ) );
201
			self::enqueue_style( 'woocommerce_prettyPhoto_css', $assets_path . 'css/prettyPhoto.css' );
202
		}
203
		if ( is_product() ) {
204
			self::enqueue_script( 'wc-single-product' );
205
		}
206
		if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) ) {
207
			// Exclude common bots from geolocation by user agent.
208
			$ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : '';
209
210
			if ( ! strstr( $ua, 'bot' ) && ! strstr( $ua, 'spider' ) && ! strstr( $ua, 'crawl' ) ) {
211
				self::enqueue_script( 'wc-geolocation', $frontend_script_path . 'geolocation' . $suffix . '.js', array( 'jquery' ) );
212
			}
213
		}
214
215
		// Global frontend scripts
216
		self::enqueue_script( 'woocommerce', $frontend_script_path . 'woocommerce' . $suffix . '.js', array( 'jquery', 'jquery-blockui' ) );
217
		self::enqueue_script( 'wc-cart-fragments', $frontend_script_path . 'cart-fragments' . $suffix . '.js', array( 'jquery', 'jquery-cookie' ) );
218
219
		// CSS Styles
220
		if ( $enqueue_styles = self::get_styles() ) {
221
			foreach ( $enqueue_styles as $handle => $args ) {
222
				self::enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'] );
223
			}
224
		}
225
	}
226
227
	/**
228
	 * Localize a WC script once.
229
	 * @access private
230
	 * @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.
231
	 * @param  string $handle
232
	 */
233
	private static function localize_script( $handle ) {
234
		if ( ! in_array( $handle, self::$wp_localize_scripts ) && wp_script_is( $handle ) && ( $data = self::get_script_data( $handle ) ) ) {
235
			$name                        = str_replace( '-', '_', $handle ) . '_params';
236
			self::$wp_localize_scripts[] = $handle;
237
			wp_localize_script( $handle, $name, apply_filters( $name, $data ) );
238
		}
239
	}
240
241
	/**
242
	 * Return data for script handles.
243
	 * @access private
244
	 * @param  string $handle
245
	 * @return array|bool
246
	 */
247
	private static function get_script_data( $handle ) {
248
		global $wp;
249
250
		switch ( $handle ) {
251
			case 'woocommerce' :
252
				return array(
253
					'ajax_url'    => WC()->ajax_url(),
254
					'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" )
255
				);
256
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
257
			case 'wc-geolocation' :
258
				return array(
259
					'wc_ajax_url'  => WC_AJAX::get_endpoint( "%%endpoint%%" ),
260
					'home_url'     => home_url(),
261
					'is_available' => ! ( is_cart() || is_account_page() || is_checkout() || is_customize_preview() ) ? '1' : '0',
262
					'hash'         => isset( $_GET['v'] ) ? wc_clean( $_GET['v'] ) : ''
263
				);
264
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
265
			case 'wc-single-product' :
266
				return array(
267
					'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
268
					'review_rating_required'    => get_option( 'woocommerce_review_rating_required' ),
269
				);
270
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
271
			case 'wc-checkout' :
272
				return array(
273
					'ajax_url'                  => WC()->ajax_url(),
274
					'wc_ajax_url'               => WC_AJAX::get_endpoint( "%%endpoint%%" ),
275
					'update_order_review_nonce' => wp_create_nonce( 'update-order-review' ),
276
					'apply_coupon_nonce'        => wp_create_nonce( 'apply-coupon' ),
277
					'remove_coupon_nonce'       => wp_create_nonce( 'remove-coupon' ),
278
					'option_guest_checkout'     => get_option( 'woocommerce_enable_guest_checkout' ),
279
					'checkout_url'              => WC_AJAX::get_endpoint( "checkout" ),
280
					'is_checkout'               => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0,
281
					'debug_mode'                => defined('WP_DEBUG') && WP_DEBUG,
282
					'i18n_checkout_error'       => esc_attr__( 'Error processing checkout. Please try again.', 'woocommerce' ),
283
				);
284
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
285
			case 'wc-address-i18n' :
286
				return array(
287
					'locale'             => json_encode( WC()->countries->get_country_locale() ),
288
					'locale_fields'      => json_encode( WC()->countries->get_country_locale_field_selectors() ),
289
					'i18n_required_text' => esc_attr__( 'required', 'woocommerce' ),
290
				);
291
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
292
			case 'wc-cart' :
293
				return array(
294
					'ajax_url'                     => WC()->ajax_url(),
295
					'wc_ajax_url'                  => WC_AJAX::get_endpoint( "%%endpoint%%" ),
296
					'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ),
297
					'apply_coupon_nonce'           => wp_create_nonce( "apply-coupon" ),
298
					'remove_coupon_nonce'          => wp_create_nonce( "remove-coupon" ),
299
				);
300
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
301
			case 'wc-cart-fragments' :
302
				return array(
303
					'ajax_url'      => WC()->ajax_url(),
304
					'wc_ajax_url'   => WC_AJAX::get_endpoint( "%%endpoint%%" ),
305
					'fragment_name' => apply_filters( 'woocommerce_cart_fragment_name', 'wc_fragments' )
306
				);
307
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
308
			case 'wc-add-to-cart' :
309
				return array(
310
					'ajax_url'                => WC()->ajax_url(),
311
					'wc_ajax_url'             => WC_AJAX::get_endpoint( "%%endpoint%%" ),
312
					'i18n_view_cart'          => esc_attr__( 'View Cart', 'woocommerce' ),
313
					'cart_url'                => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url() ),
314
					'is_cart'                 => is_cart(),
315
					'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' )
316
				);
317
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
318
			case 'wc-add-to-cart-variation' :
319
				// We also need the wp.template for this script :)
320
				wc_get_template( 'single-product/add-to-cart/variation.php' );
321
322
				return array(
323
					'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ),
324
					'i18n_make_a_selection_text'       => esc_attr__( 'Please select some product options before adding this product to your cart.', 'woocommerce' ),
325
					'i18n_unavailable_text'            => esc_attr__( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' )
326
				);
327
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
328
			case 'wc-country-select' :
329
				return array(
330
					'countries'                 => json_encode( array_merge( WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states() ) ),
331
					'i18n_select_state_text'    => esc_attr__( 'Select an option&hellip;', 'woocommerce' ),
332
					'i18n_matches_1'            => _x( 'One result is available, press enter to select it.', 'enhanced select', 'woocommerce' ),
333
					'i18n_matches_n'            => _x( '%qty% results are available, use up and down arrow keys to navigate.', 'enhanced select', 'woocommerce' ),
334
					'i18n_no_matches'           => _x( 'No matches found', 'enhanced select', 'woocommerce' ),
335
					'i18n_ajax_error'           => _x( 'Loading failed', 'enhanced select', 'woocommerce' ),
336
					'i18n_input_too_short_1'    => _x( 'Please enter 1 or more characters', 'enhanced select', 'woocommerce' ),
337
					'i18n_input_too_short_n'    => _x( 'Please enter %qty% or more characters', 'enhanced select', 'woocommerce' ),
338
					'i18n_input_too_long_1'     => _x( 'Please delete 1 character', 'enhanced select', 'woocommerce' ),
339
					'i18n_input_too_long_n'     => _x( 'Please delete %qty% characters', 'enhanced select', 'woocommerce' ),
340
					'i18n_selection_too_long_1' => _x( 'You can only select 1 item', 'enhanced select', 'woocommerce' ),
341
					'i18n_selection_too_long_n' => _x( 'You can only select %qty% items', 'enhanced select', 'woocommerce' ),
342
					'i18n_load_more'            => _x( 'Loading more results&hellip;', 'enhanced select', 'woocommerce' ),
343
					'i18n_searching'            => _x( 'Searching&hellip;', 'enhanced select', 'woocommerce' ),
344
				);
345
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
346
			case 'wc-password-strength-meter' :
347
				return array(
348
					'min_password_strength' => apply_filters( 'woocommerce_min_password_strength', 3 ),
349
					'i18n_password_error'   => esc_attr__( 'Please enter a stronger password.', 'woocommerce' ),
350
					'i18n_password_hint'    => esc_attr__( 'The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ &amp; ).', 'woocommerce' )
351
				);
352
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
353
		}
354
		return false;
355
	}
356
357
	/**
358
	 * Localize scripts only when enqueued.
359
	 */
360
	public static function localize_printed_scripts() {
361
		foreach ( self::$scripts as $handle ) {
362
			self::localize_script( $handle );
363
		}
364
	}
365
}
366
367
WC_Frontend_Scripts::init();
368