class-wc-frontend-scripts.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. /**
  3. * Handle frontend scripts
  4. *
  5. * @package WooCommerce/Classes
  6. * @version 2.3.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * Frontend scripts class.
  13. */
  14. class WC_Frontend_Scripts {
  15. /**
  16. * Contains an array of script handles registered by WC.
  17. *
  18. * @var array
  19. */
  20. private static $scripts = array();
  21. /**
  22. * Contains an array of script handles registered by WC.
  23. *
  24. * @var array
  25. */
  26. private static $styles = array();
  27. /**
  28. * Contains an array of script handles localized by WC.
  29. *
  30. * @var array
  31. */
  32. private static $wp_localize_scripts = array();
  33. /**
  34. * Hook in methods.
  35. */
  36. public static function init() {
  37. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) );
  38. add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
  39. add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
  40. }
  41. /**
  42. * Get styles for the frontend.
  43. *
  44. * @return array
  45. */
  46. public static function get_styles() {
  47. return apply_filters(
  48. 'woocommerce_enqueue_styles', array(
  49. 'woocommerce-layout' => array(
  50. 'src' => self::get_asset_url( 'assets/css/woocommerce-layout.css' ),
  51. 'deps' => '',
  52. 'version' => WC_VERSION,
  53. 'media' => 'all',
  54. 'has_rtl' => true,
  55. ),
  56. 'woocommerce-smallscreen' => array(
  57. 'src' => self::get_asset_url( 'assets/css/woocommerce-smallscreen.css' ),
  58. 'deps' => 'woocommerce-layout',
  59. 'version' => WC_VERSION,
  60. 'media' => 'only screen and (max-width: ' . apply_filters( 'woocommerce_style_smallscreen_breakpoint', '768px' ) . ')',
  61. 'has_rtl' => true,
  62. ),
  63. 'woocommerce-general' => array(
  64. 'src' => self::get_asset_url( 'assets/css/woocommerce.css' ),
  65. 'deps' => '',
  66. 'version' => WC_VERSION,
  67. 'media' => 'all',
  68. 'has_rtl' => true,
  69. ),
  70. )
  71. );
  72. }
  73. /**
  74. * Return asset URL.
  75. *
  76. * @param string $path Assets path.
  77. * @return string
  78. */
  79. private static function get_asset_url( $path ) {
  80. return apply_filters( 'woocommerce_get_asset_url', plugins_url( $path, WC_PLUGIN_FILE ), $path );
  81. }
  82. /**
  83. * Register a script for use.
  84. *
  85. * @uses wp_register_script()
  86. * @param string $handle Name of the script. Should be unique.
  87. * @param string $path Full URL of the script, or path of the script relative to the WordPress root directory.
  88. * @param string[] $deps An array of registered script handles this script depends on.
  89. * @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
  90. * @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.
  91. */
  92. private static function register_script( $handle, $path, $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
  93. self::$scripts[] = $handle;
  94. wp_register_script( $handle, $path, $deps, $version, $in_footer );
  95. }
  96. /**
  97. * Register and enqueue a script for use.
  98. *
  99. * @uses wp_enqueue_script()
  100. * @param string $handle Name of the script. Should be unique.
  101. * @param string $path Full URL of the script, or path of the script relative to the WordPress root directory.
  102. * @param string[] $deps An array of registered script handles this script depends on.
  103. * @param string $version String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
  104. * @param boolean $in_footer Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.
  105. */
  106. private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
  107. if ( ! in_array( $handle, self::$scripts, true ) && $path ) {
  108. self::register_script( $handle, $path, $deps, $version, $in_footer );
  109. }
  110. wp_enqueue_script( $handle );
  111. }
  112. /**
  113. * Register a style for use.
  114. *
  115. * @uses wp_register_style()
  116. * @param string $handle Name of the stylesheet. Should be unique.
  117. * @param string $path Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
  118. * @param string[] $deps An array of registered stylesheet handles this stylesheet depends on.
  119. * @param string $version String specifying stylesheet version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
  120. * @param string $media The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.
  121. * @param boolean $has_rtl If has RTL version to load too.
  122. */
  123. private static function register_style( $handle, $path, $deps = array(), $version = WC_VERSION, $media = 'all', $has_rtl = false ) {
  124. self::$styles[] = $handle;
  125. wp_register_style( $handle, $path, $deps, $version, $media );
  126. if ( $has_rtl ) {
  127. wp_style_add_data( $handle, 'rtl', 'replace' );
  128. }
  129. }
  130. /**
  131. * Register and enqueue a styles for use.
  132. *
  133. * @uses wp_enqueue_style()
  134. * @param string $handle Name of the stylesheet. Should be unique.
  135. * @param string $path Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
  136. * @param string[] $deps An array of registered stylesheet handles this stylesheet depends on.
  137. * @param string $version String specifying stylesheet version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
  138. * @param string $media The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.
  139. * @param boolean $has_rtl If has RTL version to load too.
  140. */
  141. private static function enqueue_style( $handle, $path = '', $deps = array(), $version = WC_VERSION, $media = 'all', $has_rtl = false ) {
  142. if ( ! in_array( $handle, self::$styles, true ) && $path ) {
  143. self::register_style( $handle, $path, $deps, $version, $media, $has_rtl );
  144. }
  145. wp_enqueue_style( $handle );
  146. }
  147. /**
  148. * Register all WC scripts.
  149. */
  150. private static function register_scripts() {
  151. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  152. $register_scripts = array(
  153. 'flexslider' => array(
  154. 'src' => self::get_asset_url( 'assets/js/flexslider/jquery.flexslider' . $suffix . '.js' ),
  155. 'deps' => array( 'jquery' ),
  156. 'version' => '2.7.1',
  157. ),
  158. 'js-cookie' => array(
  159. 'src' => self::get_asset_url( 'assets/js/js-cookie/js.cookie' . $suffix . '.js' ),
  160. 'deps' => array(),
  161. 'version' => '2.1.4',
  162. ),
  163. 'jquery-blockui' => array(
  164. 'src' => self::get_asset_url( 'assets/js/jquery-blockui/jquery.blockUI' . $suffix . '.js' ),
  165. 'deps' => array( 'jquery' ),
  166. 'version' => '2.70',
  167. ),
  168. 'jquery-cookie' => array( // deprecated.
  169. 'src' => self::get_asset_url( 'assets/js/jquery-cookie/jquery.cookie' . $suffix . '.js' ),
  170. 'deps' => array( 'jquery' ),
  171. 'version' => '1.4.1',
  172. ),
  173. 'jquery-payment' => array(
  174. 'src' => self::get_asset_url( 'assets/js/jquery-payment/jquery.payment' . $suffix . '.js' ),
  175. 'deps' => array( 'jquery' ),
  176. 'version' => '3.0.0',
  177. ),
  178. 'photoswipe' => array(
  179. 'src' => self::get_asset_url( 'assets/js/photoswipe/photoswipe' . $suffix . '.js' ),
  180. 'deps' => array(),
  181. 'version' => '4.1.1',
  182. ),
  183. 'photoswipe-ui-default' => array(
  184. 'src' => self::get_asset_url( 'assets/js/photoswipe/photoswipe-ui-default' . $suffix . '.js' ),
  185. 'deps' => array( 'photoswipe' ),
  186. 'version' => '4.1.1',
  187. ),
  188. 'prettyPhoto' => array( // deprecated.
  189. 'src' => self::get_asset_url( 'assets/js/prettyPhoto/jquery.prettyPhoto' . $suffix . '.js' ),
  190. 'deps' => array( 'jquery' ),
  191. 'version' => '3.1.6',
  192. ),
  193. 'prettyPhoto-init' => array( // deprecated.
  194. 'src' => self::get_asset_url( 'assets/js/prettyPhoto/jquery.prettyPhoto.init' . $suffix . '.js' ),
  195. 'deps' => array( 'jquery', 'prettyPhoto' ),
  196. 'version' => WC_VERSION,
  197. ),
  198. 'select2' => array(
  199. 'src' => self::get_asset_url( 'assets/js/select2/select2.full' . $suffix . '.js' ),
  200. 'deps' => array( 'jquery' ),
  201. 'version' => '4.0.3',
  202. ),
  203. 'selectWoo' => array(
  204. 'src' => self::get_asset_url( 'assets/js/selectWoo/selectWoo.full' . $suffix . '.js' ),
  205. 'deps' => array( 'jquery' ),
  206. 'version' => '1.0.4',
  207. ),
  208. 'wc-address-i18n' => array(
  209. 'src' => self::get_asset_url( 'assets/js/frontend/address-i18n' . $suffix . '.js' ),
  210. 'deps' => array( 'jquery' ),
  211. 'version' => WC_VERSION,
  212. ),
  213. 'wc-add-payment-method' => array(
  214. 'src' => self::get_asset_url( 'assets/js/frontend/add-payment-method' . $suffix . '.js' ),
  215. 'deps' => array( 'jquery', 'woocommerce' ),
  216. 'version' => WC_VERSION,
  217. ),
  218. 'wc-cart' => array(
  219. 'src' => self::get_asset_url( 'assets/js/frontend/cart' . $suffix . '.js' ),
  220. 'deps' => array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ),
  221. 'version' => WC_VERSION,
  222. ),
  223. 'wc-cart-fragments' => array(
  224. 'src' => self::get_asset_url( 'assets/js/frontend/cart-fragments' . $suffix . '.js' ),
  225. 'deps' => array( 'jquery', 'js-cookie' ),
  226. 'version' => WC_VERSION,
  227. ),
  228. 'wc-checkout' => array(
  229. 'src' => self::get_asset_url( 'assets/js/frontend/checkout' . $suffix . '.js' ),
  230. 'deps' => array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ),
  231. 'version' => WC_VERSION,
  232. ),
  233. 'wc-country-select' => array(
  234. 'src' => self::get_asset_url( 'assets/js/frontend/country-select' . $suffix . '.js' ),
  235. 'deps' => array( 'jquery' ),
  236. 'version' => WC_VERSION,
  237. ),
  238. 'wc-credit-card-form' => array(
  239. 'src' => self::get_asset_url( 'assets/js/frontend/credit-card-form' . $suffix . '.js' ),
  240. 'deps' => array( 'jquery', 'jquery-payment' ),
  241. 'version' => WC_VERSION,
  242. ),
  243. 'wc-add-to-cart' => array(
  244. 'src' => self::get_asset_url( 'assets/js/frontend/add-to-cart' . $suffix . '.js' ),
  245. 'deps' => array( 'jquery' ),
  246. 'version' => WC_VERSION,
  247. ),
  248. 'wc-add-to-cart-variation' => array(
  249. 'src' => self::get_asset_url( 'assets/js/frontend/add-to-cart-variation' . $suffix . '.js' ),
  250. 'deps' => array( 'jquery', 'wp-util' ),
  251. 'version' => WC_VERSION,
  252. ),
  253. 'wc-geolocation' => array(
  254. 'src' => self::get_asset_url( 'assets/js/frontend/geolocation' . $suffix . '.js' ),
  255. 'deps' => array( 'jquery' ),
  256. 'version' => WC_VERSION,
  257. ),
  258. 'wc-lost-password' => array(
  259. 'src' => self::get_asset_url( 'assets/js/frontend/lost-password' . $suffix . '.js' ),
  260. 'deps' => array( 'jquery', 'woocommerce' ),
  261. 'version' => WC_VERSION,
  262. ),
  263. 'wc-password-strength-meter' => array(
  264. 'src' => self::get_asset_url( 'assets/js/frontend/password-strength-meter' . $suffix . '.js' ),
  265. 'deps' => array( 'jquery', 'password-strength-meter' ),
  266. 'version' => WC_VERSION,
  267. ),
  268. 'wc-single-product' => array(
  269. 'src' => self::get_asset_url( 'assets/js/frontend/single-product' . $suffix . '.js' ),
  270. 'deps' => array( 'jquery' ),
  271. 'version' => WC_VERSION,
  272. ),
  273. 'woocommerce' => array(
  274. 'src' => self::get_asset_url( 'assets/js/frontend/woocommerce' . $suffix . '.js' ),
  275. 'deps' => array( 'jquery', 'jquery-blockui', 'js-cookie' ),
  276. 'version' => WC_VERSION,
  277. ),
  278. 'zoom' => array(
  279. 'src' => self::get_asset_url( 'assets/js/zoom/jquery.zoom' . $suffix . '.js' ),
  280. 'deps' => array( 'jquery' ),
  281. 'version' => '1.7.21',
  282. ),
  283. );
  284. foreach ( $register_scripts as $name => $props ) {
  285. self::register_script( $name, $props['src'], $props['deps'], $props['version'] );
  286. }
  287. }
  288. /**
  289. * Register all WC sty;es.
  290. */
  291. private static function register_styles() {
  292. $register_styles = array(
  293. 'photoswipe' => array(
  294. 'src' => self::get_asset_url( 'assets/css/photoswipe/photoswipe.css' ),
  295. 'deps' => array(),
  296. 'version' => WC_VERSION,
  297. 'has_rtl' => false,
  298. ),
  299. 'photoswipe-default-skin' => array(
  300. 'src' => self::get_asset_url( 'assets/css/photoswipe/default-skin/default-skin.css' ),
  301. 'deps' => array( 'photoswipe' ),
  302. 'version' => WC_VERSION,
  303. 'has_rtl' => false,
  304. ),
  305. 'select2' => array(
  306. 'src' => self::get_asset_url( 'assets/css/select2.css' ),
  307. 'deps' => array(),
  308. 'version' => WC_VERSION,
  309. 'has_rtl' => false,
  310. ),
  311. 'woocommerce_prettyPhoto_css' => array( // deprecated.
  312. 'src' => self::get_asset_url( 'assets/css/prettyPhoto.css' ),
  313. 'deps' => array(),
  314. 'version' => WC_VERSION,
  315. 'has_rtl' => true,
  316. ),
  317. );
  318. foreach ( $register_styles as $name => $props ) {
  319. self::register_style( $name, $props['src'], $props['deps'], $props['version'], 'all', $props['has_rtl'] );
  320. }
  321. }
  322. /**
  323. * Register/queue frontend scripts.
  324. */
  325. public static function load_scripts() {
  326. global $post;
  327. if ( ! did_action( 'before_woocommerce_init' ) ) {
  328. return;
  329. }
  330. self::register_scripts();
  331. self::register_styles();
  332. if ( 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' ) ) {
  333. self::enqueue_script( 'wc-add-to-cart' );
  334. }
  335. if ( is_cart() ) {
  336. self::enqueue_script( 'wc-cart' );
  337. }
  338. if ( is_cart() || is_checkout() || is_account_page() ) {
  339. self::enqueue_script( 'selectWoo' );
  340. self::enqueue_style( 'select2' );
  341. // Password strength meter. Load in checkout, account login and edit account page.
  342. if ( ( 'no' === get_option( 'woocommerce_registration_generate_password' ) && ! is_user_logged_in() ) || is_edit_account_page() || is_lost_password_page() ) {
  343. self::enqueue_script( 'wc-password-strength-meter' );
  344. }
  345. }
  346. if ( is_checkout() ) {
  347. self::enqueue_script( 'wc-checkout' );
  348. }
  349. if ( is_add_payment_method_page() ) {
  350. self::enqueue_script( 'wc-add-payment-method' );
  351. }
  352. if ( is_lost_password_page() ) {
  353. self::enqueue_script( 'wc-lost-password' );
  354. }
  355. // Load gallery scripts on product pages only if supported.
  356. if ( is_product() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) {
  357. if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
  358. self::enqueue_script( 'zoom' );
  359. }
  360. if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
  361. self::enqueue_script( 'flexslider' );
  362. }
  363. if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
  364. self::enqueue_script( 'photoswipe-ui-default' );
  365. self::enqueue_style( 'photoswipe-default-skin' );
  366. add_action( 'wp_footer', 'woocommerce_photoswipe' );
  367. }
  368. self::enqueue_script( 'wc-single-product' );
  369. }
  370. if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) ) {
  371. $ua = wc_get_user_agent(); // Exclude common bots from geolocation by user agent.
  372. if ( ! strstr( $ua, 'bot' ) && ! strstr( $ua, 'spider' ) && ! strstr( $ua, 'crawl' ) ) {
  373. self::enqueue_script( 'wc-geolocation' );
  374. }
  375. }
  376. // Global frontend scripts.
  377. self::enqueue_script( 'woocommerce' );
  378. self::enqueue_script( 'wc-cart-fragments' );
  379. // CSS Styles.
  380. $enqueue_styles = self::get_styles();
  381. if ( $enqueue_styles ) {
  382. foreach ( $enqueue_styles as $handle => $args ) {
  383. if ( ! isset( $args['has_rtl'] ) ) {
  384. $args['has_rtl'] = false;
  385. }
  386. self::enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'], $args['has_rtl'] );
  387. }
  388. }
  389. // Placeholder style.
  390. wp_register_style( 'woocommerce-inline', false );
  391. wp_enqueue_style( 'woocommerce-inline' );
  392. if ( true === wc_string_to_bool( get_option( 'woocommerce_checkout_highlight_required_fields', 'yes' ) ) ) {
  393. wp_add_inline_style( 'woocommerce-inline', '.woocommerce form .form-row .required { visibility: visible; }' );
  394. } else {
  395. wp_add_inline_style( 'woocommerce-inline', '.woocommerce form .form-row .required { visibility: hidden; }' );
  396. }
  397. }
  398. /**
  399. * Localize a WC script once.
  400. *
  401. * @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.
  402. * @param string $handle Script handle the data will be attached to.
  403. */
  404. private static function localize_script( $handle ) {
  405. if ( ! in_array( $handle, self::$wp_localize_scripts, true ) && wp_script_is( $handle ) ) {
  406. $data = self::get_script_data( $handle );
  407. if ( ! $data ) {
  408. return;
  409. }
  410. $name = str_replace( '-', '_', $handle ) . '_params';
  411. self::$wp_localize_scripts[] = $handle;
  412. wp_localize_script( $handle, $name, apply_filters( $name, $data ) );
  413. }
  414. }
  415. /**
  416. * Return data for script handles.
  417. *
  418. * @param string $handle Script handle the data will be attached to.
  419. * @return array|bool
  420. */
  421. private static function get_script_data( $handle ) {
  422. global $wp;
  423. switch ( $handle ) {
  424. case 'woocommerce':
  425. $params = array(
  426. 'ajax_url' => WC()->ajax_url(),
  427. 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
  428. );
  429. break;
  430. case 'wc-geolocation':
  431. $params = array(
  432. 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
  433. 'home_url' => home_url(),
  434. 'is_available' => ! ( is_cart() || is_account_page() || is_checkout() || is_customize_preview() ) ? '1' : '0',
  435. 'hash' => isset( $_GET['v'] ) ? wc_clean( wp_unslash( $_GET['v'] ) ) : '', // WPCS: input var ok, CSRF ok.
  436. );
  437. break;
  438. case 'wc-single-product':
  439. $params = array(
  440. 'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
  441. 'review_rating_required' => get_option( 'woocommerce_review_rating_required' ),
  442. 'flexslider' => apply_filters(
  443. 'woocommerce_single_product_carousel_options', array(
  444. 'rtl' => is_rtl(),
  445. 'animation' => 'slide',
  446. 'smoothHeight' => true,
  447. 'directionNav' => false,
  448. 'controlNav' => 'thumbnails',
  449. 'slideshow' => false,
  450. 'animationSpeed' => 500,
  451. 'animationLoop' => false, // Breaks photoswipe pagination if true.
  452. 'allowOneSlide' => false,
  453. )
  454. ),
  455. 'zoom_enabled' => apply_filters( 'woocommerce_single_product_zoom_enabled', get_theme_support( 'wc-product-gallery-zoom' ) ),
  456. 'zoom_options' => apply_filters( 'woocommerce_single_product_zoom_options', array() ),
  457. 'photoswipe_enabled' => apply_filters( 'woocommerce_single_product_photoswipe_enabled', get_theme_support( 'wc-product-gallery-lightbox' ) ),
  458. 'photoswipe_options' => apply_filters(
  459. 'woocommerce_single_product_photoswipe_options', array(
  460. 'shareEl' => false,
  461. 'closeOnScroll' => false,
  462. 'history' => false,
  463. 'hideAnimationDuration' => 0,
  464. 'showAnimationDuration' => 0,
  465. )
  466. ),
  467. 'flexslider_enabled' => apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) ),
  468. );
  469. break;
  470. case 'wc-checkout':
  471. $params = array(
  472. 'ajax_url' => WC()->ajax_url(),
  473. 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
  474. 'update_order_review_nonce' => wp_create_nonce( 'update-order-review' ),
  475. 'apply_coupon_nonce' => wp_create_nonce( 'apply-coupon' ),
  476. 'remove_coupon_nonce' => wp_create_nonce( 'remove-coupon' ),
  477. 'option_guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ),
  478. 'checkout_url' => WC_AJAX::get_endpoint( 'checkout' ),
  479. 'is_checkout' => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0,
  480. 'debug_mode' => defined( 'WP_DEBUG' ) && WP_DEBUG,
  481. 'i18n_checkout_error' => esc_attr__( 'Error processing checkout. Please try again.', 'woocommerce' ),
  482. );
  483. break;
  484. case 'wc-address-i18n':
  485. $params = array(
  486. 'locale' => wp_json_encode( WC()->countries->get_country_locale() ),
  487. 'locale_fields' => wp_json_encode( WC()->countries->get_country_locale_field_selectors() ),
  488. 'i18n_required_text' => esc_attr__( 'required', 'woocommerce' ),
  489. 'i18n_optional_text' => esc_html__( 'optional', 'woocommerce' ),
  490. );
  491. break;
  492. case 'wc-cart':
  493. $params = array(
  494. 'ajax_url' => WC()->ajax_url(),
  495. 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
  496. 'update_shipping_method_nonce' => wp_create_nonce( 'update-shipping-method' ),
  497. 'apply_coupon_nonce' => wp_create_nonce( 'apply-coupon' ),
  498. 'remove_coupon_nonce' => wp_create_nonce( 'remove-coupon' ),
  499. );
  500. break;
  501. case 'wc-cart-fragments':
  502. $params = array(
  503. 'ajax_url' => WC()->ajax_url(),
  504. 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
  505. 'cart_hash_key' => apply_filters( 'woocommerce_cart_hash_key', 'wc_cart_hash_' . md5( get_current_blog_id() . '_' . get_site_url( get_current_blog_id(), '/' ) . get_template() ) ),
  506. 'fragment_name' => apply_filters( 'woocommerce_cart_fragment_name', 'wc_fragments_' . md5( get_current_blog_id() . '_' . get_site_url( get_current_blog_id(), '/' ) . get_template() ) ),
  507. );
  508. break;
  509. case 'wc-add-to-cart':
  510. $params = array(
  511. 'ajax_url' => WC()->ajax_url(),
  512. 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
  513. 'i18n_view_cart' => esc_attr__( 'View cart', 'woocommerce' ),
  514. 'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url() ),
  515. 'is_cart' => is_cart(),
  516. 'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' ),
  517. );
  518. break;
  519. case 'wc-add-to-cart-variation':
  520. // We also need the wp.template for this script :).
  521. wc_get_template( 'single-product/add-to-cart/variation.php' );
  522. $params = array(
  523. 'wc_ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ),
  524. 'i18n_no_matching_variations_text' => esc_attr__( 'Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce' ),
  525. 'i18n_make_a_selection_text' => esc_attr__( 'Please select some product options before adding this product to your cart.', 'woocommerce' ),
  526. 'i18n_unavailable_text' => esc_attr__( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ),
  527. );
  528. break;
  529. case 'wc-country-select':
  530. $params = array(
  531. 'countries' => wp_json_encode( array_merge( WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states() ) ),
  532. 'i18n_select_state_text' => esc_attr__( 'Select an option&hellip;', 'woocommerce' ),
  533. 'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'woocommerce' ),
  534. 'i18n_ajax_error' => _x( 'Loading failed', 'enhanced select', 'woocommerce' ),
  535. 'i18n_input_too_short_1' => _x( 'Please enter 1 or more characters', 'enhanced select', 'woocommerce' ),
  536. 'i18n_input_too_short_n' => _x( 'Please enter %qty% or more characters', 'enhanced select', 'woocommerce' ),
  537. 'i18n_input_too_long_1' => _x( 'Please delete 1 character', 'enhanced select', 'woocommerce' ),
  538. 'i18n_input_too_long_n' => _x( 'Please delete %qty% characters', 'enhanced select', 'woocommerce' ),
  539. 'i18n_selection_too_long_1' => _x( 'You can only select 1 item', 'enhanced select', 'woocommerce' ),
  540. 'i18n_selection_too_long_n' => _x( 'You can only select %qty% items', 'enhanced select', 'woocommerce' ),
  541. 'i18n_load_more' => _x( 'Loading more results&hellip;', 'enhanced select', 'woocommerce' ),
  542. 'i18n_searching' => _x( 'Searching&hellip;', 'enhanced select', 'woocommerce' ),
  543. );
  544. break;
  545. case 'wc-password-strength-meter':
  546. $params = array(
  547. 'min_password_strength' => apply_filters( 'woocommerce_min_password_strength', 3 ),
  548. 'i18n_password_error' => esc_attr__( 'Please enter a stronger password.', 'woocommerce' ),
  549. 'i18n_password_hint' => esc_attr( wp_get_password_hint() ),
  550. );
  551. break;
  552. default:
  553. $params = false;
  554. }
  555. return apply_filters( 'woocommerce_get_script_data', $params, $handle );
  556. }
  557. /**
  558. * Localize scripts only when enqueued.
  559. */
  560. public static function localize_printed_scripts() {
  561. foreach ( self::$scripts as $handle ) {
  562. self::localize_script( $handle );
  563. }
  564. }
  565. }
  566. WC_Frontend_Scripts::init();