2013-06-06 13:20:47 +00:00
< ? php
2013-06-11 12:31:41 +00:00
/**
* WC_Frontend_Scripts
*/
class WC_Frontend_Scripts {
2013-06-06 13:20:47 +00:00
2013-06-11 12:31:41 +00:00
/**
* Constructor
*/
public function __construct () {
add_action ( 'wp_enqueue_scripts' , array ( $this , 'load_scripts' ) );
add_action ( 'wp_print_scripts' , array ( $this , 'check_jquery' ), 25 );
2013-08-02 11:06:32 +00:00
add_filter ( 'woocommerce_enqueue_styles' , array ( $this , 'backwards_compat' ) );
2013-06-11 12:31:41 +00:00
}
2013-06-06 13:20:47 +00:00
2013-08-02 11:27:31 +00:00
/**
* Get styles for the frontend
* @ return array
*/
public static function get_styles () {
return apply_filters ( 'woocommerce_enqueue_styles' , array (
'woocommerce-layout' => array (
'src' => str_replace ( array ( 'http:' , 'https:' ), '' , WC () -> plugin_url () ) . '/assets/css/woocommerce-layout.css' ,
'deps' => '' ,
2013-10-24 18:36:22 +00:00
'version' => WC_VERSION ,
2013-08-02 11:27:31 +00:00
'media' => ''
),
'woocommerce-smallscreen' => array (
'src' => str_replace ( array ( 'http:' , 'https:' ), '' , WC () -> plugin_url () ) . '/assets/css/woocommerce-smallscreen.css' ,
'deps' => 'woocommerce-layout' ,
2013-10-24 18:36:22 +00:00
'version' => WC_VERSION ,
2013-08-02 11:27:31 +00:00
'media' => 'only screen and (max-width: ' . apply_filters ( 'woocommerce_style_smallscreen_breakpoint' , $breakpoint = '768px' ) . ')'
),
'woocommerce-general' => array (
'src' => str_replace ( array ( 'http:' , 'https:' ), '' , WC () -> plugin_url () ) . '/assets/css/woocommerce.css' ,
'deps' => '' ,
2013-10-24 18:36:22 +00:00
'version' => WC_VERSION ,
2013-08-02 11:27:31 +00:00
'media' => ''
),
) );
}
2013-06-06 13:20:47 +00:00
/**
* Register / queue frontend scripts .
*
* @ access public
* @ return void
*/
public function load_scripts () {
2013-08-02 11:06:32 +00:00
global $post , $wp ;
2013-06-06 13:20:47 +00:00
$suffix = defined ( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ;
$lightbox_en = get_option ( 'woocommerce_enable_lightbox' ) == 'yes' ? true : false ;
$ajax_cart_en = get_option ( 'woocommerce_enable_ajax_add_to_cart' ) == 'yes' ? true : false ;
2013-08-02 11:06:32 +00:00
$assets_path = str_replace ( array ( 'http:' , 'https:' ), '' , WC () -> plugin_url () ) . '/assets/' ;
2013-06-06 13:20:47 +00:00
$frontend_script_path = $assets_path . 'js/frontend/' ;
// Register any scripts for later use, or used as dependencies
wp_register_script ( 'chosen' , $assets_path . 'js/chosen/chosen.jquery' . $suffix . '.js' , array ( 'jquery' ), '0.9.14' , true );
wp_register_script ( 'jquery-blockui' , $assets_path . 'js/jquery-blockui/jquery.blockUI' . $suffix . '.js' , array ( 'jquery' ), '2.60' , true );
2013-09-27 12:22:04 +00:00
wp_register_script ( 'jquery-payment' , $assets_path . 'js/jquery-payment/jquery.payment' . $suffix . '.js' , array ( 'jquery' ), '1.0.2' , true );
2013-10-24 18:36:22 +00:00
wp_register_script ( 'wc-credit-card-form' , $assets_path . 'js/frontend/credit-card-form' . $suffix . '.js' , array ( 'jquery' , 'jquery-payment' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
2013-10-24 18:36:22 +00:00
wp_register_script ( 'wc-add-to-cart-variation' , $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js' , array ( 'jquery' ), WC_VERSION , true );
wp_register_script ( 'wc-single-product' , $frontend_script_path . 'single-product' . $suffix . '.js' , array ( 'jquery' ), WC_VERSION , true );
wp_register_script ( 'wc-country-select' , $frontend_script_path . 'country-select' . $suffix . '.js' , array ( 'jquery' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
wp_register_script ( 'jquery-cookie' , $assets_path . 'js/jquery-cookie/jquery.cookie' . $suffix . '.js' , array ( 'jquery' ), '1.3.1' , true );
// Queue frontend scripts conditionally
if ( $ajax_cart_en )
2013-10-24 18:36:22 +00:00
wp_enqueue_script ( 'wc-add-to-cart' , $frontend_script_path . 'add-to-cart' . $suffix . '.js' , array ( 'jquery' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
if ( is_cart () )
2013-10-24 18:36:22 +00:00
wp_enqueue_script ( 'wc-cart' , $frontend_script_path . 'cart' . $suffix . '.js' , array ( 'jquery' , 'wc-country-select' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
if ( is_checkout () ) {
2013-06-11 12:31:41 +00:00
2013-06-11 10:27:56 +00:00
if ( get_option ( 'woocommerce_enable_chosen' ) == 'yes' ) {
2013-10-24 18:36:22 +00:00
wp_enqueue_script ( 'wc-chosen' , $frontend_script_path . 'chosen-frontend' . $suffix . '.js' , array ( 'chosen' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
wp_enqueue_style ( 'woocommerce_chosen_styles' , $assets_path . 'css/chosen.css' );
}
2013-10-24 18:36:22 +00:00
wp_enqueue_script ( 'wc-checkout' , $frontend_script_path . 'checkout' . $suffix . '.js' , array ( 'jquery' , 'woocommerce' , 'wc-country-select' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
}
2013-11-05 22:41:21 +00:00
if ( is_add_payment_method_page () )
wp_enqueue_script ( 'wc-add-payment-method' , $frontend_script_path . 'add-payment-method' . $suffix . '.js' , array ( 'jquery' , 'woocommerce' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
if ( $lightbox_en && ( is_product () || ( ! empty ( $post -> post_content ) && strstr ( $post -> post_content , '[product_page' ) ) ) ) {
wp_enqueue_script ( 'prettyPhoto' , $assets_path . 'js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js' , array ( 'jquery' ), '3.1.5' , true );
2013-10-24 18:36:22 +00:00
wp_enqueue_script ( 'prettyPhoto-init' , $assets_path . 'js/prettyPhoto/jquery.prettyPhoto.init' . $suffix . '.js' , array ( 'jquery' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
wp_enqueue_style ( 'woocommerce_prettyPhoto_css' , $assets_path . 'css/prettyPhoto.css' );
}
if ( is_product () )
wp_enqueue_script ( 'wc-single-product' );
// Global frontend scripts
2013-10-28 09:03:03 +00:00
wp_enqueue_script ( 'woocommerce' , $frontend_script_path . 'woocommerce' . $suffix . '.js' , array ( 'jquery' , 'jquery-blockui' ), WC_VERSION , true );
2013-10-24 18:36:22 +00:00
wp_enqueue_script ( 'wc-cart-fragments' , $frontend_script_path . 'cart-fragments' . $suffix . '.js' , array ( 'jquery' , 'jquery-cookie' ), WC_VERSION , true );
2013-06-06 13:20:47 +00:00
// Variables for JS scripts
2013-06-13 16:01:42 +00:00
wp_localize_script ( 'woocommerce' , 'woocommerce_params' , apply_filters ( 'woocommerce_params' , array (
2013-08-02 11:06:32 +00:00
'ajax_url' => WC () -> ajax_url (),
2013-06-13 16:01:42 +00:00
'ajax_loader_url' => apply_filters ( 'woocommerce_ajax_loader_url' , $assets_path . 'images/ajax-loader@2x.gif' ),
) ) );
2013-06-11 12:31:41 +00:00
wp_localize_script ( 'wc-single-product' , 'wc_single_product_params' , apply_filters ( 'wc_single_product_params' , array (
'i18n_required_rating_text' => esc_attr__ ( 'Please select a rating' , 'woocommerce' ),
'review_rating_required' => get_option ( 'woocommerce_review_rating_required' ),
) ) );
wp_localize_script ( 'wc-checkout' , 'wc_checkout_params' , apply_filters ( 'wc_checkout_params' , array (
2013-08-02 11:06:32 +00:00
'ajax_url' => WC () -> ajax_url (),
2013-06-06 13:20:47 +00:00
'ajax_loader_url' => apply_filters ( 'woocommerce_ajax_loader_url' , $assets_path . 'images/ajax-loader@2x.gif' ),
'i18n_required_text' => esc_attr__ ( 'required' , 'woocommerce' ),
'update_order_review_nonce' => wp_create_nonce ( " update-order-review " ),
'apply_coupon_nonce' => wp_create_nonce ( " apply-coupon " ),
'option_guest_checkout' => get_option ( 'woocommerce_enable_guest_checkout' ),
2013-11-14 12:13:34 +00:00
'checkout_url' => add_query_arg ( 'action' , 'woocommerce_checkout' , WC () -> ajax_url () ),
2013-06-06 13:20:47 +00:00
'is_checkout' => is_page ( woocommerce_get_page_id ( 'checkout' ) ) && empty ( $wp -> query_vars [ 'order-pay' ] ) && ! isset ( $wp -> query_vars [ 'order-received' ] ) ? 1 : 0 ,
2013-08-02 11:06:32 +00:00
'locale' => json_encode ( WC () -> countries -> get_country_locale () )
2013-06-11 12:31:41 +00:00
) ) );
wp_localize_script ( 'wc-cart' , 'wc_cart_params' , apply_filters ( 'wc_cart_params' , array (
2013-08-02 11:06:32 +00:00
'ajax_url' => WC () -> ajax_url (),
2013-06-11 12:31:41 +00:00
'ajax_loader_url' => apply_filters ( 'woocommerce_ajax_loader_url' , $assets_path . 'images/ajax-loader@2x.gif' ),
2013-06-06 13:20:47 +00:00
'update_shipping_method_nonce' => wp_create_nonce ( " update-shipping-method " ),
2013-06-11 12:31:41 +00:00
) ) );
wp_localize_script ( 'wc-cart-fragments' , 'wc_cart_fragments_params' , apply_filters ( 'wc_cart_fragments_params' , array (
2013-08-02 11:06:32 +00:00
'ajax_url' => WC () -> ajax_url ()
2013-06-11 12:31:41 +00:00
) ) );
wp_localize_script ( 'wc-add-to-cart' , 'wc_add_to_cart_params' , apply_filters ( 'wc_add_to_cart_params' , array (
2013-10-04 11:21:22 +00:00
'ajax_url' => WC () -> ajax_url (),
'ajax_loader_url' => apply_filters ( 'woocommerce_ajax_loader_url' , $assets_path . 'images/ajax-loader@2x.gif' ),
2013-10-28 09:45:59 +00:00
'i18n_view_cart' => esc_attr__ ( 'View Cart' , 'woocommerce' ),
2013-10-04 11:21:22 +00:00
'cart_url' => get_permalink ( woocommerce_get_page_id ( 'cart' ) ),
'is_cart' => is_cart (),
'cart_redirect_after_add' => get_option ( 'woocommerce_cart_redirect_after_add' )
2013-06-11 12:31:41 +00:00
) ) );
2013-06-06 13:20:47 +00:00
2013-06-11 12:31:41 +00:00
wp_localize_script ( 'wc-add-to-cart-variation' , 'wc_add_to_cart_variation_params' , apply_filters ( 'wc_add_to_cart_variation_params' , array (
'i18n_no_matching_variations_text' => esc_attr__ ( 'Sorry, no products matched your selection. Please choose a different combination.' , 'woocommerce' ),
2013-10-10 15:34:44 +00:00
'i18n_unavailable_text' => esc_attr__ ( 'Sorry, this product is unavailable. Please choose a different combination.' , 'woocommerce' ),
2013-06-11 12:31:41 +00:00
) ) );
2013-06-06 13:20:47 +00:00
2013-06-11 12:31:41 +00:00
wp_localize_script ( 'wc-country-select' , 'wc_country_select_params' , apply_filters ( 'wc_country_select_params' , array (
2013-08-02 15:54:28 +00:00
'countries' => json_encode ( array_merge ( WC () -> countries -> get_allowed_country_states (), WC () -> countries -> get_shipping_country_states () ) ),
2013-06-11 12:31:41 +00:00
'i18n_select_state_text' => esc_attr__ ( 'Select an option…' , 'woocommerce' ),
) ) );
2013-06-06 13:20:47 +00:00
// CSS Styles
2013-08-02 11:27:31 +00:00
$enqueue_styles = $this -> get_styles ();
2013-08-02 11:06:32 +00:00
if ( $enqueue_styles )
foreach ( $enqueue_styles as $handle => $args )
wp_enqueue_style ( $handle , $args [ 'src' ], $args [ 'deps' ], $args [ 'version' ], $args [ 'media' ] );
2013-06-06 13:20:47 +00:00
}
/**
* WC requires jQuery 1.7 since it uses functions like . on () for events .
* If , by the time wp_print_scrips is called , jQuery is outdated ( i . e not
* using the version in core ) we need to deregister it and register the
* core version of the file .
*
* @ access public
* @ return void
*/
public function check_jquery () {
global $wp_scripts ;
// Enforce minimum version of jQuery
2013-08-06 11:27:36 +00:00
if ( ! empty ( $wp_scripts -> registered [ 'jquery' ] -> ver ) && ! empty ( $wp_scripts -> registered [ 'jquery' ] -> src ) && 0 >= version_compare ( $wp_scripts -> registered [ 'jquery' ] -> ver , '1.7' ) ) {
2013-06-06 13:20:47 +00:00
wp_deregister_script ( 'jquery' );
wp_register_script ( 'jquery' , '/wp-includes/js/jquery/jquery.js' , array (), '1.7' );
wp_enqueue_script ( 'jquery' );
}
}
2013-08-02 11:06:32 +00:00
/**
* Provide backwards compat for old constant
* @ param array $styles
* @ return array
*/
public function backwards_compat ( $styles ) {
if ( defined ( 'WOOCOMMERCE_USE_CSS' ) ) {
_deprecated_function ( 'WOOCOMMERCE_USE_CSS' , '2.1' , 'Styles should be removed using wp_deregister_style or the woocommerce_enqueue_frontend_styles filter rather than the WOOCOMMERCE_USE_CSS constant.' );
if ( ! WOOCOMMERCE_USE_CSS )
return false ;
}
return $styles ;
}
2013-06-11 12:31:41 +00:00
}
new WC_Frontend_Scripts ();