Bez popisu

woocommerce.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * This file contains compatibility functions for WooCommerce to improve Jetpack feature support.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. add_action( 'woocommerce_init', 'jetpack_woocommerce_integration' );
  8. /**
  9. * Loads JP+WC integration.
  10. *
  11. * Fires on `woocommerce_init` hook
  12. */
  13. function jetpack_woocommerce_integration() {
  14. /**
  15. * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry.
  16. */
  17. if ( ! class_exists( 'WooCommerce' ) ) {
  18. return;
  19. }
  20. add_action( 'woocommerce_share', 'jetpack_woocommerce_social_share_icons', 10 );
  21. /**
  22. * Wrap in function exists check since this requires WooCommerce 3.3+.
  23. */
  24. if ( function_exists( 'wc_get_default_products_per_row' ) ) {
  25. add_filter( 'infinite_scroll_render_callbacks', 'jetpack_woocommerce_infinite_scroll_render_callback', 10 );
  26. add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_infinite_scroll_style', 10 );
  27. }
  28. }
  29. /**
  30. * Make sure the social sharing icons show up under the product's short description
  31. */
  32. function jetpack_woocommerce_social_share_icons() {
  33. if ( function_exists( 'sharing_display' ) ) {
  34. remove_filter( 'the_content', 'sharing_display', 19 );
  35. remove_filter( 'the_excerpt', 'sharing_display', 19 );
  36. sharing_display( '', true );
  37. }
  38. }
  39. /**
  40. * Remove sharing display from account, cart, and checkout pages in WooCommerce.
  41. */
  42. function jetpack_woocommerce_remove_share() {
  43. /**
  44. * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry.
  45. */
  46. if ( ! class_exists( 'WooCommerce' ) ) {
  47. return;
  48. }
  49. if ( is_cart() || is_checkout() || is_account_page() ) {
  50. remove_filter( 'the_content', 'sharing_display', 19 );
  51. if ( class_exists( 'Jetpack_Likes' ) ) {
  52. remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
  53. }
  54. }
  55. }
  56. add_action( 'loop_start', 'jetpack_woocommerce_remove_share' );
  57. /**
  58. * Add a callback for WooCommerce product rendering in infinite scroll.
  59. *
  60. * @param array $callbacks Array of render callpacks for IS.
  61. * @return array
  62. */
  63. function jetpack_woocommerce_infinite_scroll_render_callback( $callbacks ) {
  64. $callbacks[] = 'jetpack_woocommerce_infinite_scroll_render';
  65. return $callbacks;
  66. }
  67. /**
  68. * Add a default renderer for WooCommerce products within infinite scroll.
  69. */
  70. function jetpack_woocommerce_infinite_scroll_render() {
  71. if ( ! is_shop() && ! is_product_taxonomy() && ! is_product_category() && ! is_product_tag() ) {
  72. return;
  73. }
  74. woocommerce_product_loop_start();
  75. while ( have_posts() ) {
  76. the_post();
  77. wc_get_template_part( 'content', 'product' );
  78. }
  79. woocommerce_product_loop_end();
  80. }
  81. /**
  82. * Basic styling when infinite scroll is active only.
  83. */
  84. function jetpack_woocommerce_infinite_scroll_style() {
  85. $custom_css = '
  86. .infinite-scroll .woocommerce-pagination {
  87. display: none;
  88. }';
  89. wp_add_inline_style( 'woocommerce-layout', $custom_css );
  90. }
  91. /**
  92. * Adds compat for WooCommerce and Lazy Loading.
  93. */
  94. function jetpack_woocommerce_lazy_images_compat() {
  95. wp_add_inline_script(
  96. 'wc-cart-fragments',
  97. "
  98. jQuery( 'body' ).bind( 'wc_fragments_refreshed', function() {
  99. var jetpackLazyImagesLoadEvent;
  100. try {
  101. jetpackLazyImagesLoadEvent = new Event( 'jetpack-lazy-images-load', {
  102. bubbles: true,
  103. cancelable: true
  104. } );
  105. } catch ( e ) {
  106. jetpackLazyImagesLoadEvent = document.createEvent( 'Event' )
  107. jetpackLazyImagesLoadEvent.initEvent( 'jetpack-lazy-images-load', true, true );
  108. }
  109. jQuery( 'body' ).get( 0 ).dispatchEvent( jetpackLazyImagesLoadEvent );
  110. } );
  111. "
  112. );
  113. }
  114. add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_lazy_images_compat', 11 );