Completed
Push — add/changelog-64 ( 49ba93...06347a )
by
unknown
169:34 queued 150:02
created

woocommerce.php ➔ jetpack_woocommerce_lazy_images_compat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains compatibility functions for WooCommerce to improve Jetpack feature support.
4
 */
5
add_action( 'woocommerce_init', 'jetpack_woocommerce_integration' );
6
7
function jetpack_woocommerce_integration() {
8
	/**
9
	 * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry.
10
	 */
11
	if ( ! class_exists( 'WooCommerce' ) ) {
12
		return;
13
	}
14
15
	add_action( 'woocommerce_share', 'jetpack_woocommerce_social_share_icons', 10 );
16
17
	/**
18
	 * Wrap in function exists check since this requires WooCommerce 3.3+.
19
	 */
20
	if ( function_exists( 'wc_get_default_products_per_row' ) ) {
21
		add_filter( 'infinite_scroll_render_callbacks', 'jetpack_woocommerce_infinite_scroll_render_callback', 10 );
22
		add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_infinite_scroll_style', 10 );
23
	}
24
}
25
26
/*
27
 * Make sure the social sharing icons show up under the product's short description
28
 */
29
function jetpack_woocommerce_social_share_icons() {
30
	if ( function_exists( 'sharing_display' ) ) {
31
		remove_filter( 'the_content', 'sharing_display', 19 );
32
		remove_filter( 'the_excerpt', 'sharing_display', 19 );
33
		echo sharing_display();
34
	}
35
}
36
37
/**
38
 * Remove sharing display from account, cart, and checkout pages in WooCommerce.
39
 */
40
function jetpack_woocommerce_remove_share() {
41
	/**
42
	 * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry.
43
	 */
44
	if ( ! class_exists( 'WooCommerce' ) ) {
45
		return;
46
	}
47
48
	if ( is_cart() || is_checkout() || is_account_page() ) {
49
		remove_filter( 'the_content', 'sharing_display', 19 );
50
		if ( class_exists( 'Jetpack_Likes' ) ) {
51
			remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
52
		}
53
	}
54
}
55
add_action( 'loop_start', 'jetpack_woocommerce_remove_share' );
56
57
/**
58
 * Add a callback for WooCommerce product rendering in infinite scroll.
59
 *
60
 * @param array $callbacks
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
/**
69
 * Add a default renderer for WooCommerce products within infinite scroll.
70
 */
71
function jetpack_woocommerce_infinite_scroll_render() {
72
	if ( ! is_shop() && ! is_product_taxonomy() && ! is_product_category() && ! is_product_tag() ) {
73
		return;
74
	}
75
76
	woocommerce_product_loop_start();
77
78
	while ( have_posts() ) {
79
		the_post();
80
		wc_get_template_part( 'content', 'product' );
81
	}
82
83
	woocommerce_product_loop_end();
84
}
85
86
/**
87
 * Basic styling when infinite scroll is active only.
88
 */
89
function jetpack_woocommerce_infinite_scroll_style() {
90
	$custom_css = "
91
	.infinite-scroll .woocommerce-pagination {
92
		display: none;
93
	}";
94
	wp_add_inline_style( 'woocommerce-layout', $custom_css );
95
}
96
97
function jetpack_woocommerce_lazy_images_compat() {
98
	wp_add_inline_script( 'wc-cart-fragments', "
99
		jQuery( 'body' ).bind( 'wc_fragments_refreshed', function() {
100
			jQuery( 'body' ).trigger( 'jetpack-lazy-images-load' );
101
		} );
102
	" );
103
}
104
105
add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_lazy_images_compat', 11 );
106