octicon octicon-file-text"> woocommerce-th-a65b4ddf263e8c410e9bcbfd6e6d72e4.json d01d7cf85d first commit 4 lat temu woocommerce-th-a8b73b087444470c8f4396b7142908f6.json d01d7cf85d first commit 4 lat temu woocommerce-th-abb904b047b7be3be9d638e2c6144691.json d01d7cf85d first commit 4 lat temu woocommerce-th-ae1c49c8c0597593a9c36e106a04bd60.json d01d7cf85d first commit 4 lat temu woocommerce-th-aed4ce3ede1d44a8fecfe0edfbf8421c.json d01d7cf85d first commit 4 lat temu woocommerce-th-af52ab8cb2e7713cf3037751dd593965.json d01d7cf85d first commit 4 lat temu woocommerce-th-afbede9cda22f594a80ae8b178e0e98c.json d01d7cf85d first commit 4 lat temu woocommerce-th-afd1e270efc2e8283acfd50ccab18d2b.json d01d7cf85d first commit 4 lat temu woocommerce-th-b007baff76144318d6c33cd26f4bfdb9.json d01d7cf85d first commit 4 lat temu woocommerce-th-b0a592b6fe83588c5d1b8ed6850b6a74.json d01d7cf85d first commit 4 lat temu woocommerce-th-b155b7bf28890099b74a84417cbaafe9.json d01d7cf85d first commit 4 lat temu woocommerce-th-b1bb8fbd24d68a9c893faf1a6f7b493b.json d01d7cf85d first commit 4 lat temu woocommerce-th-b1bcbab1d6a7ca6b0433d67776f347f1.json d01d7cf85d first commit 4 lat temu woocommerce-th-b239ff15197a133498a4b025f002dee5.json d01d7cf85d first commit 4 lat temu woocommerce-th-b2e956ee35340f6e47f27f8a54309ac3.json d01d7cf85d first commit 4 lat temu woocommerce-th-b31f51fa0bb40ceb060aaa9f77ca1771.json d01d7cf85d first commit 4 lat temu woocommerce-th-b3f83696a2cb49f571043ce14a384a76.json d01d7cf85d first commit 4 lat temu woocommerce-th-b4bf1e55ca6145f38f1b2f104201da21.json d01d7cf85d first commit 4 lat temu woocommerce-th-b826befd7acca93e8e4c12b7cd2fc7f1.json d01d7cf85d first commit 4 lat temu woocommerce-th-b9bf1ca91d393a30e2461ef709f8fa10.json d01d7cf85d first commit 4 lat temu woocommerce-th-ba8d2fb7c85928310d4f3a907b7d9bdd.json d01d7cf85d first commit 4 lat temu woocommerce-th-bb0f027faa1257de4b730158475093de.json d01d7cf85d first commit 4 lat temu woocommerce-th-bb5c5935ec0d64e8880b1c7794470460.json d01d7cf85d first commit 4 lat temu woocommerce-th-bc1f4f7aec77a0a6dd229cc910f601f1.json d01d7cf85d first commit 4 lat temu woocommerce-th-bc9c3f9eda462d6d9968c1dca51e7c62.json d01d7cf85d first commit 4 lat temu woocommerce-th-bcb06ab6e8931a862dec9c07c66df45b.json d01d7cf85d first commit 4 lat temu woocommerce-th-c06829f63f73ae430c10ae700fb1eb6d.json d01d7cf85d first commit 4 lat temu woocommerce-th-c0faba84c7842505f8893c2c327a9f78.json d01d7cf85d first commit 4 lat temu woocommerce-th-c13acd243559529a685813d9871026a1.json d01d7cf85d first commit 4 lat temu woocommerce-th-c1bc39bcdee2da3c0b62bcd5372120f4.json d01d7cf85d first commit 4 lat temu woocommerce-th-c22945301f466e2d78d6e80404a22664.json d01d7cf85d first commit 4 lat temu woocommerce-th-c2435bc8dfd186b147aba658416d115e.json d01d7cf85d first commit 4 lat temu woocommerce-th-c257c024e3db4e010644d2c7be3c7090.json d01d7cf85d first commit 4 lat temu woocommerce-th-c2d9a34cf502bd5b5a80ca646a1420b3.json d01d7cf85d first commit 4 lat temu woocommerce-th-c5702ace609dd30ccabd4c999de43096.json d01d7cf85d first commit 4 lat temu woocommerce-th-c608fe5deda8db7667c8431ca6fd222a.json d01d7cf85d first commit 4 lat temu woocommerce-th-c6c5331d3afe1391f2ca83e991ccc086.json d01d7cf85d first commit 4 lat temu woocommerce-th-c70671124b0e00661f88fe1616dfc9f0.json d01d7cf85d first commit 4 lat temu woocommerce-th-c8cbe8ef95155db152b270d441fc8528.json d01d7cf85d first commit 4 lat temu woocommerce-th-c8ddf2164475a213274d06873bfbb73a.json d01d7cf85d first commit 4 lat temu woocommerce-th-c93706e596cbd46fb0d621f4fe3efb80.json d01d7cf85d first commit 4 lat temu woocommerce-th-ca3cd4394cb082bde7555ebd3bb5f143.json d01d7cf85d first commit 4 lat temu woocommerce-th-cb71ce5ddcc7c808172e80a0dc9833d5.json d01d7cf85d first commit 4 lat temu woocommerce-th-cc16013df9d4b0a0ba45297b90b06f20.json d01d7cf85d first commit 4 lat temu woocommerce-th-ccc500b0b722a0c96712f1634cbb7289.json d01d7cf85d first commit 4 lat temu woocommerce-th-cd9fc1d9fc46b55bb32d0fd471457690.json d01d7cf85d first commit 4 lat temu woocommerce-th-ce3f8f2f3e0f3c58f8aea5b7aa5cc6f4.json d01d7cf85d first commit 4 lat temu woocommerce-th-d05ee634b22d05f0055b724443f05a68.json d01d7cf85d first commit 4 lat temu woocommerce-th-d0d05e0dda872b433f260969b4d53b30.json d01d7cf85d first commit 4 lat temu woocommerce-th-d0fa7cd1cd624e53cc7b4fde655c20af.json d01d7cf85d first commit 4 lat temu woocommerce-th-d2b4400b6de0e69404e0f7f61d16bc55.json d01d7cf85d first commit 4 lat temu woocommerce-th-d3e57c518feb58ab8ea4f9d2d8e66160.json d01d7cf85d first commit 4 lat temu woocommerce-th-d4c505e96199e8c818fc6d24151d9c4a.json d01d7cf85d first commit 4 lat temu woocommerce-th-d5ea33028f13b50f40bbcce807e37fc0.json d01d7cf85d first commit 4 lat temu woocommerce-th-d6f4faa954a31ef3d5b74c2dfe13f141.json d01d7cf85d first commit 4 lat temu woocommerce-th-d7bbe90eb59636eb8a6f31c7979ab6f5.json d01d7cf85d first commit 4 lat temu woocommerce-th-d80020dab74131d7f0c108526c0fbebe.json d01d7cf85d first commit 4 lat temu woocommerce-th-da92a9b2d4c08762c60d7c4c569eb5db.json d01d7cf85d first commit 4 lat temu woocommerce-th-de12a4697822740a2fbaeb47c5062af1.json d01d7cf85d first commit 4 lat temu woocommerce-th-deed2aed3392b43c83c7cea296b154af.json d01d7cf85d first commit 4 lat temu woocommerce-th-df51042cba56a4e9fcac0e5db643906a.json d01d7cf85d first commit 4 lat temu woocommerce-th-dfb09f9953676bbc1de4eb2b9f232c6d.json d01d7cf85d first commit 4 lat temu woocommerce-th-dfbfe3d556f60ebbd62c2b5c8e0fa466.json d01d7cf85d first commit 4 lat temu woocommerce-th-e03ec35386c2705c11cebd6288b33cf8.json d01d7cf85d first commit 4 lat temu woocommerce-th-e0bde0ac58f55fc6b1426844e6697db3.json d01d7cf85d first commit 4 lat temu woocommerce-th-e0f9c7d89b1b130a9023c5c2fde2815e.json d01d7cf85d first commit 4 lat temu woocommerce-th-e41c58d5fb39f750efcbf65a2b55460b.json d01d7cf85d first commit 4 lat temu woocommerce-th-e44671e4528b0da3a0fd6feaf8abadca.json d01d7cf85d first commit 4 lat temu woocommerce-th-e4fbc8d7fc7a16bd8cf2e0522a199de0.json d01d7cf85d first commit 4 lat temu woocommerce-th-e690486b33a7bb1944a37f6e734d734f.json d01d7cf85d first commit 4 lat temu woocommerce-th-e80539b9960e930bf0dcd334d99eb128.json d01d7cf85d first commit 4 lat temu woocommerce-th-e885442559cb44e8b827f0e0ca0a14c9.json d01d7cf85d first commit 4 lat temu woocommerce-th-e88dc35c02f6fa832e90fac5a122323c.json d01d7cf85d first commit 4 lat temu woocommerce-th-ea6fb371f81c4d3584bbc795fa4ac09c.json d01d7cf85d first commit 4 lat temu woocommerce-th-ed9b16000a6d37c51bf23c61f4df878e.json d01d7cf85d first commit 4 lat temu woocommerce-th-ef5798395d7601ab6fea0f1efd60f40a.json d01d7cf85d first commit 4 lat temu woocommerce-th-f31b0aa0c71dd74b7103120610ca6253.json d01d7cf85d first commit 4 lat temu woocommerce-th-f34924aeff974b0bdbfac124c23fbfe3.json d01d7cf85d first commit 4 lat temu woocommerce-th-f39008456fd28b2f79cdb0964ff294e1.json d01d7cf85d first commit 4 lat temu woocommerce-th-f3f1cc4f5811d4bf8a04a58feca06858.json d01d7cf85d first commit 4 lat temu woocommerce-th-f56e66fd08c07abc78cd844f7e83af8c.json d01d7cf85d first commit 4 lat temu woocommerce-th-f60049653c419cfb1e3dc8876e7a8bdb.json d01d7cf85d first commit 4 lat temu woocommerce-th-f686a272add740773cdcb730afdb5454.json d01d7cf85d first commit 4 lat temu woocommerce-th-f976da155dfdd080588c50ccda5af747.json d01d7cf85d first commit 4 lat temu woocommerce-th-f9c56a0cbb6710505047e6c835953c82.json d01d7cf85d first commit 4 lat temu woocommerce-th-fb2ef023bdd44902b5a309e25e0b7792.json d01d7cf85d first commit 4 lat temu woocommerce-th-fc1fe56fadfdeeb25d1bc441581d92b6.json d01d7cf85d first commit 4 lat temu woocommerce-th-fc4895b6508f32c16c4d8bc69f8162b6.json d01d7cf85d first commit 4 lat temu woocommerce-th-fd20fc8ea1484781160e8ea0d229d5e9.json d01d7cf85d first commit 4 lat temu woocommerce-th-fffdec5db595ec0ca7d043c87bae79b8.json d01d7cf85d first commit 4 lat temu woocommerce-th-wc-admin-app.json d01d7cf85d first commit 4 lat temu woocommerce-th.mo d01d7cf85d first commit 4 lat temu woocommerce-th.po d01d7cf85d first commit 4 lat temu tum/whitesports - Gogs: Simplico Git Service

Nessuna descrizione

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();