Completed
Push — add/admin-color-schemes-gutenb... ( 149d9d...8ce7f6 )
by
unknown
08:51
created

3rd-party/woocommerce.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file contains compatibility functions for WooCommerce to improve Jetpack feature support.
4
 *
5
 * @package Jetpack.
6
 */
7
8
add_action( 'woocommerce_init', 'jetpack_woocommerce_integration' );
9
10
/**
11
 * Loads JP+WC integration.
12
 *
13
 * Fires on `woocommerce_init` hook
14
 */
15
function jetpack_woocommerce_integration() {
16
	/**
17
	 * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry.
18
	 */
19
	if ( ! class_exists( 'WooCommerce' ) ) {
20
		return;
21
	}
22
23
	add_action( 'woocommerce_share', 'jetpack_woocommerce_social_share_icons', 10 );
24
25
	/**
26
	 * Wrap in function exists check since this requires WooCommerce 3.3+.
27
	 */
28
	if ( function_exists( 'wc_get_default_products_per_row' ) ) {
29
		add_filter( 'infinite_scroll_render_callbacks', 'jetpack_woocommerce_infinite_scroll_render_callback', 10 );
30
		add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_infinite_scroll_style', 10 );
31
	}
32
}
33
34
/**
35
 * Make sure the social sharing icons show up under the product's short description
36
 */
37
function jetpack_woocommerce_social_share_icons() {
38
	if ( function_exists( 'sharing_display' ) ) {
39
		remove_filter( 'the_content', 'sharing_display', 19 );
40
		remove_filter( 'the_excerpt', 'sharing_display', 19 );
41
		sharing_display( '', true );
42
	}
43
}
44
45
/**
46
 * Remove sharing display from account, cart, and checkout pages in WooCommerce.
47
 */
48
function jetpack_woocommerce_remove_share() {
49
	/**
50
	 * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry.
51
	 */
52
	if ( ! class_exists( 'WooCommerce' ) ) {
53
		return;
54
	}
55
56
	if ( is_cart() || is_checkout() || is_account_page() ) {
57
		remove_filter( 'the_content', 'sharing_display', 19 );
58
		if ( class_exists( 'Jetpack_Likes' ) ) {
59
			remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
0 ignored issues
show
The call to remove_filter() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
		}
61
	}
62
}
63
add_action( 'loop_start', 'jetpack_woocommerce_remove_share' );
64
65
/**
66
 * Add a callback for WooCommerce product rendering in infinite scroll.
67
 *
68
 * @param array $callbacks Array of render callpacks for IS.
69
 * @return array
70
 */
71
function jetpack_woocommerce_infinite_scroll_render_callback( $callbacks ) {
72
	$callbacks[] = 'jetpack_woocommerce_infinite_scroll_render';
73
	return $callbacks;
74
}
75
76
/**
77
 * Add a default renderer for WooCommerce products within infinite scroll.
78
 */
79
function jetpack_woocommerce_infinite_scroll_render() {
80
	if ( ! is_shop() && ! is_product_taxonomy() && ! is_product_category() && ! is_product_tag() ) {
81
		return;
82
	}
83
84
	woocommerce_product_loop_start();
85
86
	while ( have_posts() ) {
87
		the_post();
88
		wc_get_template_part( 'content', 'product' );
89
	}
90
91
	woocommerce_product_loop_end();
92
}
93
94
/**
95
 * Basic styling when infinite scroll is active only.
96
 */
97
function jetpack_woocommerce_infinite_scroll_style() {
98
	$custom_css = '
99
	.infinite-scroll .woocommerce-pagination {
100
		display: none;
101
	}';
102
	wp_add_inline_style( 'woocommerce-layout', $custom_css );
103
}
104
105
/**
106
 * Adds compat for WooCommerce and Lazy Loading.
107
 */
108
function jetpack_woocommerce_lazy_images_compat() {
109
	wp_add_inline_script(
110
		'wc-cart-fragments',
111
		"
112
		jQuery( 'body' ).bind( 'wc_fragments_refreshed', function() {
113
			var jetpackLazyImagesLoadEvent;
114
			try {
115
				jetpackLazyImagesLoadEvent = new Event( 'jetpack-lazy-images-load', {
116
					bubbles: true,
117
					cancelable: true
118
				} );
119
			} catch ( e ) {
120
				jetpackLazyImagesLoadEvent = document.createEvent( 'Event' )
121
				jetpackLazyImagesLoadEvent.initEvent( 'jetpack-lazy-images-load', true, true );
122
			}
123
			jQuery( 'body' ).get( 0 ).dispatchEvent( jetpackLazyImagesLoadEvent );
124
		} );
125
		"
126
	);
127
}
128
129
add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_lazy_images_compat', 11 );
130