1 <?php
  2 /**
  3  * Handle frontend forms
  4  *
  5  * @class       WC_Frontend_Scripts
  6  * @version     2.1.0
  7  * @package     WooCommerce/Classes/
  8  * @category    Class
  9  * @author      WooThemes
 10  */
 11 class WC_Frontend_Scripts {
 12 
 13     /**
 14      * Constructor
 15      */
 16     public function __construct () {
 17         add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
 18         add_action( 'wp_print_scripts', array( $this, 'check_jquery' ), 25 );
 19         add_filter( 'woocommerce_enqueue_styles', array( $this, 'backwards_compat' ) );
 20     }
 21 
 22     /**
 23      * Get styles for the frontend
 24      * @return array
 25      */
 26     public static function get_styles() {
 27         return apply_filters( 'woocommerce_enqueue_styles', array(
 28             'woocommerce-layout' => array(
 29                 'src'     => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce-layout.css',
 30                 'deps'    => '',
 31                 'version' => WC_VERSION,
 32                 'media'   => 'all'
 33             ),
 34             'woocommerce-smallscreen' => array(
 35                 'src'     => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce-smallscreen.css',
 36                 'deps'    => 'woocommerce-layout',
 37                 'version' => WC_VERSION,
 38                 'media'   => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', $breakpoint = '768px' ) . ')'
 39             ),
 40             'woocommerce-general' => array(
 41                 'src'     => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce.css',
 42                 'deps'    => '',
 43                 'version' => WC_VERSION,
 44                 'media'   => 'all'
 45             ),
 46         ) );
 47     }
 48 
 49     /**
 50      * Register/queue frontend scripts.
 51      *
 52      * @access public
 53      * @return void
 54      */
 55     public function load_scripts() {
 56         global $post, $wp;
 57 
 58         $suffix               = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
 59         $lightbox_en          = get_option( 'woocommerce_enable_lightbox' ) == 'yes' ? true : false;
 60         $ajax_cart_en         = get_option( 'woocommerce_enable_ajax_add_to_cart' ) == 'yes' ? true : false;
 61         $assets_path          = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/';
 62         $frontend_script_path = $assets_path . 'js/frontend/';
 63 
 64         // Register any scripts for later use, or used as dependencies
 65         wp_register_script( 'chosen', $assets_path . 'js/chosen/chosen.jquery' . $suffix . '.js', array( 'jquery' ), '1.0.0', true );
 66         wp_register_script( 'jquery-blockui', $assets_path . 'js/jquery-blockui/jquery.blockUI' . $suffix . '.js', array( 'jquery' ), '2.60', true );
 67         wp_register_script( 'jquery-payment', $assets_path . 'js/jquery-payment/jquery.payment' . $suffix . '.js', array( 'jquery' ), '1.0.2', true );
 68         wp_register_script( 'wc-credit-card-form', $assets_path . 'js/frontend/credit-card-form' . $suffix . '.js', array( 'jquery', 'jquery-payment' ), WC_VERSION, true );
 69 
 70         wp_register_script( 'wc-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
 71         wp_register_script( 'wc-single-product', $frontend_script_path . 'single-product' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
 72         wp_register_script( 'wc-country-select', $frontend_script_path . 'country-select' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
 73         wp_register_script( 'wc-address-i18n', $frontend_script_path . 'address-i18n' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
 74         wp_register_script( 'jquery-cookie', $assets_path . 'js/jquery-cookie/jquery.cookie' . $suffix . '.js', array( 'jquery' ), '1.3.1', true );
 75 
 76         // Queue frontend scripts conditionally
 77         if ( $ajax_cart_en )
 78             wp_enqueue_script( 'wc-add-to-cart', $frontend_script_path . 'add-to-cart' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
 79 
 80         if ( is_cart() )
 81             wp_enqueue_script( 'wc-cart', $frontend_script_path . 'cart' . $suffix . '.js', array( 'jquery', 'wc-country-select' ), WC_VERSION, true );
 82 
 83         if ( is_checkout() ) {
 84 
 85             if ( get_option( 'woocommerce_enable_chosen' ) == 'yes' ) {
 86                 wp_enqueue_script( 'wc-chosen', $frontend_script_path . 'chosen-frontend' . $suffix . '.js', array( 'chosen' ), WC_VERSION, true );
 87                 wp_enqueue_style( 'woocommerce_chosen_styles', $assets_path . 'css/chosen.css' );
 88             }
 89 
 90             wp_enqueue_script( 'wc-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ), WC_VERSION, true );
 91         }
 92 
 93         if ( is_page( get_option( 'woocommerce_myaccount_page_id' ) ) ) {
 94             if ( get_option( 'woocommerce_enable_chosen' ) == 'yes' ) {
 95                 wp_enqueue_script( 'wc-chosen', $frontend_script_path . 'chosen-frontend' . $suffix . '.js', array( 'chosen' ), WC_VERSION, true );
 96                 wp_enqueue_style( 'woocommerce_chosen_styles', $assets_path . 'css/chosen.css' );
 97             }
 98         }
 99 
100         if ( is_add_payment_method_page() )
101             wp_enqueue_script( 'wc-add-payment-method', $frontend_script_path . 'add-payment-method' . $suffix . '.js', array( 'jquery', 'woocommerce' ), WC_VERSION, true );
102 
103         if ( $lightbox_en && ( is_product() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) ) {
104             wp_enqueue_script( 'prettyPhoto', $assets_path . 'js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js', array( 'jquery' ), '3.1.5', true );
105             wp_enqueue_script( 'prettyPhoto-init', $assets_path . 'js/prettyPhoto/jquery.prettyPhoto.init' . $suffix . '.js', array( 'jquery','prettyPhoto' ), WC_VERSION, true );
106             wp_enqueue_style( 'woocommerce_prettyPhoto_css', $assets_path . 'css/prettyPhoto.css' );
107         }
108 
109         if ( is_product() )
110             wp_enqueue_script( 'wc-single-product' );
111 
112         // Global frontend scripts
113         wp_enqueue_script( 'woocommerce', $frontend_script_path . 'woocommerce' . $suffix . '.js', array( 'jquery', 'jquery-blockui' ), WC_VERSION, true );
114         wp_enqueue_script( 'wc-cart-fragments', $frontend_script_path . 'cart-fragments' . $suffix . '.js', array( 'jquery', 'jquery-cookie' ), WC_VERSION, true );
115 
116         // Variables for JS scripts
117         wp_localize_script( 'woocommerce', 'woocommerce_params', apply_filters( 'woocommerce_params', array(
118             'ajax_url'        => WC()->ajax_url(),
119             'ajax_loader_url' => apply_filters( 'woocommerce_ajax_loader_url', $assets_path . 'images/ajax-loader@2x.gif' ),
120         ) ) );
121 
122         wp_localize_script( 'wc-single-product', 'wc_single_product_params', apply_filters( 'wc_single_product_params', array(
123             'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
124             'review_rating_required'    => get_option( 'woocommerce_review_rating_required' ),
125         ) ) );
126 
127         wp_localize_script( 'wc-checkout', 'wc_checkout_params', apply_filters( 'wc_checkout_params', array(
128             'ajax_url'                  => WC()->ajax_url(),
129             'ajax_loader_url'           => apply_filters( 'woocommerce_ajax_loader_url', $assets_path . 'images/ajax-loader@2x.gif' ),
130             'update_order_review_nonce' => wp_create_nonce( "update-order-review" ),
131             'apply_coupon_nonce'        => wp_create_nonce( "apply-coupon" ),
132             'option_guest_checkout'     => get_option( 'woocommerce_enable_guest_checkout' ),
133             'checkout_url'              => add_query_arg( 'action', 'woocommerce_checkout', WC()->ajax_url() ),
134             'is_checkout'               => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0
135         ) ) );
136 
137         wp_localize_script( 'wc-address-i18n', 'wc_address_i18n_params', apply_filters( 'wc_address_i18n_params', array(
138             'locale'                    => json_encode( WC()->countries->get_country_locale() ),
139             'locale_fields'             => json_encode( WC()->countries->get_country_locale_field_selectors() ),
140             'i18n_required_text'        => esc_attr__( 'required', 'woocommerce' ),
141         ) ) );
142 
143         wp_localize_script( 'wc-cart', 'wc_cart_params', apply_filters( 'wc_cart_params', array(
144             'ajax_url'                     => WC()->ajax_url(),
145             'ajax_loader_url'              => apply_filters( 'woocommerce_ajax_loader_url', $assets_path . 'images/ajax-loader@2x.gif' ),
146             'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ),
147         ) ) );
148 
149         wp_localize_script( 'wc-cart-fragments', 'wc_cart_fragments_params', apply_filters( 'wc_cart_fragments_params', array(
150             'ajax_url'      => WC()->ajax_url(),
151             'fragment_name' => apply_filters( 'woocommerce_cart_fragment_name', 'wc_fragments' )
152         ) ) );
153 
154         wp_localize_script( 'wc-add-to-cart', 'wc_add_to_cart_params', apply_filters( 'wc_add_to_cart_params', array(
155             'ajax_url'                => WC()->ajax_url(),
156             'ajax_loader_url'         => apply_filters( 'woocommerce_ajax_loader_url', $assets_path . 'images/ajax-loader@2x.gif' ),
157             'i18n_view_cart'          => esc_attr__( 'View Cart', 'woocommerce' ),
158             'cart_url'                => get_permalink( wc_get_page_id( 'cart' ) ),
159             'is_cart'                 => is_cart(),
160             'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' )
161         ) ) );
162 
163         wp_localize_script( 'wc-add-to-cart-variation', 'wc_add_to_cart_variation_params', apply_filters( 'wc_add_to_cart_variation_params', array(
164             'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ),
165             'i18n_unavailable_text'            => esc_attr__( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ),
166         ) ) );
167 
168         wp_localize_script( 'wc-country-select', 'wc_country_select_params', apply_filters( 'wc_country_select_params', array(
169             'countries'              => json_encode( array_merge( WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states() ) ),
170             'i18n_select_state_text' => esc_attr__( 'Select an option&hellip;', 'woocommerce' ),
171         ) ) );
172 
173         // CSS Styles
174         $enqueue_styles = $this->get_styles();
175 
176         if ( $enqueue_styles )
177             foreach ( $enqueue_styles as $handle => $args )
178                 wp_enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'] );
179     }
180 
181     /**
182      * WC requires jQuery 1.8 since it uses functions like .on() for events and .parseHTML.
183      * If, by the time wp_print_scrips is called, jQuery is outdated (i.e not
184      * using the version in core) we need to deregister it and register the
185      * core version of the file.
186      *
187      * @access public
188      * @return void
189      */
190     public function check_jquery() {
191         global $wp_scripts;
192 
193         // Enforce minimum version of jQuery
194         if ( ! empty( $wp_scripts->registered['jquery']->ver ) && ! empty( $wp_scripts->registered['jquery']->src ) && 0 >= version_compare( $wp_scripts->registered['jquery']->ver, '1.8' ) ) {
195             wp_deregister_script( 'jquery' );
196             wp_register_script( 'jquery', '/wp-includes/js/jquery/jquery.js', array(), '1.8' );
197             wp_enqueue_script( 'jquery' );
198         }
199     }
200 
201     /**
202      * Provide backwards compat for old constant
203      * @param  array $styles
204      * @return array
205      */
206     public function backwards_compat( $styles ) {
207         if ( defined( 'WOOCOMMERCE_USE_CSS' ) ) {
208 
209             _deprecated_function( 'WOOCOMMERCE_USE_CSS', '2.1', 'Styles should be removed using wp_deregister_style or the woocommerce_enqueue_styles filter rather than the WOOCOMMERCE_USE_CSS constant.' );
210 
211             if ( ! WOOCOMMERCE_USE_CSS )
212                 return false;
213         }
214 
215         return $styles;
216     }
217 }
218 
219 new WC_Frontend_Scripts();
220 
WooCommerce API documentation generated by ApiGen 2.8.0