PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php

https://gitlab.com/haque.mdmanzurul/soundkreationsfinal
PHP | 301 lines | 208 code | 20 blank | 73 comment | 25 complexity | 91346e0f34da118fc7d7d009276e780b MD5 | raw file
  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. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. /**
  15. * WC_Frontend_Scripts Class
  16. */
  17. class WC_Frontend_Scripts {
  18. /**
  19. * Contains an array of script handles registered by WC
  20. * @var array
  21. */
  22. private static $scripts = array();
  23. /**
  24. * Contains an array of script handles localized by WC
  25. * @var array
  26. */
  27. private static $wp_localize_scripts = array();
  28. /**
  29. * Hook in methods.
  30. */
  31. public static function init() {
  32. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) );
  33. add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
  34. add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
  35. }
  36. /**
  37. * Get styles for the frontend.
  38. * @access private
  39. * @return array
  40. */
  41. public static function get_styles() {
  42. return apply_filters( 'woocommerce_enqueue_styles', array(
  43. 'woocommerce-layout' => array(
  44. 'src' => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce-layout.css',
  45. 'deps' => '',
  46. 'version' => WC_VERSION,
  47. 'media' => 'all'
  48. ),
  49. 'woocommerce-smallscreen' => array(
  50. 'src' => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce-smallscreen.css',
  51. 'deps' => 'woocommerce-layout',
  52. 'version' => WC_VERSION,
  53. 'media' => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', $breakpoint = '768px' ) . ')'
  54. ),
  55. 'woocommerce-general' => array(
  56. 'src' => str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/css/woocommerce.css',
  57. 'deps' => '',
  58. 'version' => WC_VERSION,
  59. 'media' => 'all'
  60. ),
  61. ) );
  62. }
  63. /**
  64. * Register a script for use.
  65. *
  66. * @uses wp_register_script()
  67. * @access private
  68. * @param string $handle [description]
  69. * @param string $path [description]
  70. * @param string[] $deps [description]
  71. * @param string $version [description]
  72. * @param boolean $in_footer [description]
  73. */
  74. private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
  75. self::$scripts[] = $handle;
  76. wp_register_script( $handle, $path, $deps, $version, $in_footer );
  77. }
  78. /**
  79. * Register and enqueue a script for use.
  80. *
  81. * @uses wp_enqueue_script()
  82. * @access private
  83. * @param string $handle [description]
  84. * @param string $path [description]
  85. * @param string[] $deps [description]
  86. * @param string $version [description]
  87. * @param boolean $in_footer [description]
  88. */
  89. private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
  90. if ( ! in_array( $handle, self::$scripts ) && $path ) {
  91. self::register_script( $handle, $path, $deps, $version, $in_footer );
  92. }
  93. wp_enqueue_script( $handle );
  94. }
  95. /**
  96. * Register/queue frontend scripts.
  97. */
  98. public static function load_scripts() {
  99. global $post;
  100. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  101. $lightbox_en = 'yes' === get_option( 'woocommerce_enable_lightbox' );
  102. $ajax_cart_en = 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' );
  103. $assets_path = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/';
  104. $frontend_script_path = $assets_path . 'js/frontend/';
  105. // Chosen is @deprecated as of 2.3 in favour of 2.3. Here for backwards compatibility.
  106. self::register_script( 'chosen', $assets_path . 'js/chosen/chosen.jquery' . $suffix . '.js', array( 'jquery' ), '1.0.0' );
  107. self::register_script( 'select2', $assets_path . 'js/select2/select2' . $suffix . '.js', array( 'jquery' ), '3.5.2' );
  108. // Register any scripts for later use, or used as dependencies
  109. self::register_script( 'jquery-blockui', $assets_path . 'js/jquery-blockui/jquery.blockUI' . $suffix . '.js', array( 'jquery' ), '2.70' );
  110. self::register_script( 'jquery-payment', $assets_path . 'js/jquery-payment/jquery.payment' . $suffix . '.js', array( 'jquery' ), '1.2.4' );
  111. self::register_script( 'jquery-cookie', $assets_path . 'js/jquery-cookie/jquery.cookie' . $suffix . '.js', array( 'jquery' ), '1.4.1' );
  112. self::register_script( 'wc-credit-card-form', $frontend_script_path . 'credit-card-form' . $suffix . '.js', array( 'jquery', 'jquery-payment' ) );
  113. self::register_script( 'wc-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js' );
  114. self::register_script( 'wc-single-product', $frontend_script_path . 'single-product' . $suffix . '.js' );
  115. self::register_script( 'wc-country-select', $frontend_script_path . 'country-select' . $suffix . '.js' );
  116. self::register_script( 'wc-address-i18n', $frontend_script_path . 'address-i18n' . $suffix . '.js' );
  117. // Register frontend scripts conditionally
  118. if ( $ajax_cart_en ) {
  119. self::enqueue_script( 'wc-add-to-cart', $frontend_script_path . 'add-to-cart' . $suffix . '.js' );
  120. }
  121. if ( is_cart() ) {
  122. self::enqueue_script( 'wc-cart', $frontend_script_path . 'cart' . $suffix . '.js', array( 'jquery', 'wc-country-select', 'wc-address-i18n' ) );
  123. }
  124. if ( is_checkout() || is_page( get_option( 'woocommerce_myaccount_page_id' ) ) ) {
  125. self::enqueue_script( 'select2' );
  126. wp_enqueue_style( 'select2', $assets_path . 'css/select2.css' );
  127. }
  128. if ( is_checkout() ) {
  129. self::enqueue_script( 'wc-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ) );
  130. }
  131. if ( is_add_payment_method_page() ) {
  132. self::enqueue_script( 'wc-add-payment-method', $frontend_script_path . 'add-payment-method' . $suffix . '.js', array( 'jquery', 'woocommerce' ) );
  133. }
  134. if ( is_lost_password_page() ) {
  135. self::enqueue_script( 'wc-lost-password', $frontend_script_path . 'lost-password' . $suffix . '.js', array( 'jquery', 'woocommerce' ) );
  136. }
  137. if ( $lightbox_en && ( is_product() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) ) {
  138. self::enqueue_script( 'prettyPhoto', $assets_path . 'js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js', array( 'jquery' ), '3.1.6', true );
  139. self::enqueue_script( 'prettyPhoto-init', $assets_path . 'js/prettyPhoto/jquery.prettyPhoto.init' . $suffix . '.js', array( 'jquery','prettyPhoto' ) );
  140. wp_enqueue_style( 'woocommerce_prettyPhoto_css', $assets_path . 'css/prettyPhoto.css' );
  141. }
  142. if ( is_product() ) {
  143. self::enqueue_script( 'wc-single-product' );
  144. }
  145. if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) ) {
  146. self::enqueue_script( 'wc-geolocation', $frontend_script_path . 'geolocation' . $suffix . '.js', array( 'jquery' ) );
  147. }
  148. // Global frontend scripts
  149. self::enqueue_script( 'woocommerce', $frontend_script_path . 'woocommerce' . $suffix . '.js', array( 'jquery', 'jquery-blockui' ) );
  150. self::enqueue_script( 'wc-cart-fragments', $frontend_script_path . 'cart-fragments' . $suffix . '.js', array( 'jquery', 'jquery-cookie' ) );
  151. // CSS Styles
  152. if ( $enqueue_styles = self::get_styles() ) {
  153. foreach ( $enqueue_styles as $handle => $args ) {
  154. wp_enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'] );
  155. }
  156. }
  157. }
  158. /**
  159. * Localize a WC script once.
  160. * @access private
  161. * @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.
  162. * @param string $handle
  163. */
  164. private static function localize_script( $handle ) {
  165. if ( ! in_array( $handle, self::$wp_localize_scripts ) && wp_script_is( $handle ) && ( $data = self::get_script_data( $handle ) ) ) {
  166. $name = str_replace( '-', '_', $handle ) . '_params';
  167. self::$wp_localize_scripts[] = $handle;
  168. wp_localize_script( $handle, $name, apply_filters( $name, $data ) );
  169. }
  170. }
  171. /**
  172. * Return data for script handles.
  173. * @access private
  174. * @param string $handle
  175. * @return array|bool
  176. */
  177. private static function get_script_data( $handle ) {
  178. global $wp;
  179. switch ( $handle ) {
  180. case 'woocommerce' :
  181. return array(
  182. 'ajax_url' => WC()->ajax_url(),
  183. 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" )
  184. );
  185. break;
  186. case 'wc-geolocation' :
  187. return array(
  188. 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
  189. 'home_url' => home_url(),
  190. 'is_checkout' => is_checkout() ? '1' : '0',
  191. 'hash' => isset( $_GET['v'] ) ? wc_clean( $_GET['v'] ) : ''
  192. );
  193. break;
  194. case 'wc-single-product' :
  195. return array(
  196. 'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
  197. 'review_rating_required' => get_option( 'woocommerce_review_rating_required' ),
  198. );
  199. break;
  200. case 'wc-checkout' :
  201. return array(
  202. 'ajax_url' => WC()->ajax_url(),
  203. 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
  204. 'update_order_review_nonce' => wp_create_nonce( 'update-order-review' ),
  205. 'apply_coupon_nonce' => wp_create_nonce( 'apply-coupon' ),
  206. 'remove_coupon_nonce' => wp_create_nonce( 'remove-coupon' ),
  207. 'option_guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ),
  208. 'checkout_url' => WC_AJAX::get_endpoint( "checkout" ),
  209. 'is_checkout' => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0,
  210. 'debug_mode' => defined('WP_DEBUG') && WP_DEBUG,
  211. 'i18n_checkout_error' => esc_attr__( 'Error processing checkout. Please try again.', 'woocommerce' ),
  212. );
  213. break;
  214. case 'wc-address-i18n' :
  215. return array(
  216. 'locale' => json_encode( WC()->countries->get_country_locale() ),
  217. 'locale_fields' => json_encode( WC()->countries->get_country_locale_field_selectors() ),
  218. 'i18n_required_text' => esc_attr__( 'required', 'woocommerce' ),
  219. );
  220. break;
  221. case 'wc-cart' :
  222. return array(
  223. 'ajax_url' => WC()->ajax_url(),
  224. 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
  225. 'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ),
  226. );
  227. break;
  228. case 'wc-cart-fragments' :
  229. return array(
  230. 'ajax_url' => WC()->ajax_url(),
  231. 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
  232. 'fragment_name' => apply_filters( 'woocommerce_cart_fragment_name', 'wc_fragments' )
  233. );
  234. break;
  235. case 'wc-add-to-cart' :
  236. return array(
  237. 'ajax_url' => WC()->ajax_url(),
  238. 'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
  239. 'i18n_view_cart' => esc_attr__( 'View Cart', 'woocommerce' ),
  240. 'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', WC()->cart->get_cart_url() ),
  241. 'is_cart' => is_cart(),
  242. 'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' )
  243. );
  244. break;
  245. case 'wc-add-to-cart-variation' :
  246. return array(
  247. 'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ),
  248. 'i18n_unavailable_text' => esc_attr__( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ),
  249. );
  250. break;
  251. case 'wc-country-select' :
  252. return array(
  253. 'countries' => json_encode( array_merge( WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states() ) ),
  254. 'i18n_select_state_text' => esc_attr__( 'Select an option&hellip;', 'woocommerce' ),
  255. 'i18n_matches_1' => _x( 'One result is available, press enter to select it.', 'enhanced select', 'woocommerce' ),
  256. 'i18n_matches_n' => _x( '%qty% results are available, use up and down arrow keys to navigate.', 'enhanced select', 'woocommerce' ),
  257. 'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'woocommerce' ),
  258. 'i18n_ajax_error' => _x( 'Loading failed', 'enhanced select', 'woocommerce' ),
  259. 'i18n_input_too_short_1' => _x( 'Please enter 1 or more characters', 'enhanced select', 'woocommerce' ),
  260. 'i18n_input_too_short_n' => _x( 'Please enter %qty% or more characters', 'enhanced select', 'woocommerce' ),
  261. 'i18n_input_too_long_1' => _x( 'Please delete 1 character', 'enhanced select', 'woocommerce' ),
  262. 'i18n_input_too_long_n' => _x( 'Please delete %qty% characters', 'enhanced select', 'woocommerce' ),
  263. 'i18n_selection_too_long_1' => _x( 'You can only select 1 item', 'enhanced select', 'woocommerce' ),
  264. 'i18n_selection_too_long_n' => _x( 'You can only select %qty% items', 'enhanced select', 'woocommerce' ),
  265. 'i18n_load_more' => _x( 'Loading more results&hellip;', 'enhanced select', 'woocommerce' ),
  266. 'i18n_searching' => _x( 'Searching&hellip;', 'enhanced select', 'woocommerce' ),
  267. );
  268. break;
  269. }
  270. return false;
  271. }
  272. /**
  273. * Localize scripts only when enqueued.
  274. */
  275. public static function localize_printed_scripts() {
  276. foreach ( self::$scripts as $handle ) {
  277. self::localize_script( $handle );
  278. }
  279. }
  280. }
  281. WC_Frontend_Scripts::init();