Ei kuvausta

class-wc-frontend-scripts.php 26KB

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