| @@ -1,0 +1,140 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * This file contains compatibility functions for WooCommerce to improve Jetpack feature support. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 11 | + | |
| 12 | +add_action( 'woocommerce_init', 'jetpack_woocommerce_integration' ); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Loads JP+WC integration. | |
| 16 | + * | |
| 17 | + * Fires on `woocommerce_init` hook | |
| 18 | + */ | |
| 19 | +function jetpack_woocommerce_integration() { | |
| 20 | + /** | |
| 21 | + * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry. | |
| 22 | + */ | |
| 23 | + if ( ! class_exists( 'WooCommerce' ) ) { | |
| 24 | + return; | |
| 25 | + } | |
| 26 | + | |
| 27 | + add_action( 'woocommerce_share', 'jetpack_woocommerce_social_share_icons', 10 ); | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Add product post type to Jetpack sitemap while skipping hidden products. | |
| 31 | + */ | |
| 32 | + add_filter( 'jetpack_sitemap_post_types', 'jetpack_woocommerce_add_to_sitemap' ); | |
| 33 | + add_filter( 'jetpack_sitemap_skip_post', 'jetpack_woocommerce_skip_hidden_products_in_sitemap', 10, 2 ); | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Wrap in function exists check since this requires WooCommerce 3.3+. | |
| 37 | + */ | |
| 38 | + if ( function_exists( 'wc_get_default_products_per_row' ) ) { | |
| 39 | + add_filter( 'infinite_scroll_render_callbacks', 'jetpack_woocommerce_infinite_scroll_render_callback', 10 ); | |
| 40 | + add_action( 'wp_enqueue_scripts', 'jetpack_woocommerce_infinite_scroll_style', 10 ); | |
| 41 | + } | |
| 42 | +} | |
| 43 | + | |
| 44 | +/** | |
| 45 | + * Add product post type to sitemap if Woocommerce is present. | |
| 46 | + * | |
| 47 | + * @param array $post_types Array of post types included in sitemap. | |
| 48 | + */ | |
| 49 | +function jetpack_woocommerce_add_to_sitemap( $post_types ) { | |
| 50 | + $post_types[] = 'product'; | |
| 51 | + | |
| 52 | + return $post_types; | |
| 53 | +} | |
| 54 | + | |
| 55 | +/** | |
| 56 | + * Skip hidden products when generating the sitemap. | |
| 57 | + * | |
| 58 | + * @param bool $skip Whether to skip the post. | |
| 59 | + * @param WP_Post $post The post object. | |
| 60 | + */ | |
| 61 | +function jetpack_woocommerce_skip_hidden_products_in_sitemap( $skip, $post ) { | |
| 62 | + if ( $post !== null && $post->post_type === 'product' ) { | |
| 63 | + $product = wc_get_product( $post->ID ); | |
| 64 | + if ( $product ) { | |
| 65 | + $skip = ! $product->is_visible(); | |
| 66 | + } | |
| 67 | + } | |
| 68 | + return $skip; | |
| 69 | +} | |
| 70 | + | |
| 71 | +/** | |
| 72 | + * Make sure the social sharing icons show up under the product's short description | |
| 73 | + */ | |
| 74 | +function jetpack_woocommerce_social_share_icons() { | |
| 75 | + if ( function_exists( 'sharing_display' ) ) { | |
| 76 | + remove_filter( 'the_content', 'sharing_display', 19 ); | |
| 77 | + remove_filter( 'the_excerpt', 'sharing_display', 19 ); | |
| 78 | + sharing_display( '', true ); | |
| 79 | + } | |
| 80 | +} | |
| 81 | + | |
| 82 | +/** | |
| 83 | + * Remove sharing display from account, cart, and checkout pages in WooCommerce. | |
| 84 | + */ | |
| 85 | +function jetpack_woocommerce_remove_share() { | |
| 86 | + /** | |
| 87 | + * Double check WooCommerce exists - unlikely to fail due to the hook being used but better safe than sorry. | |
| 88 | + */ | |
| 89 | + if ( ! class_exists( 'WooCommerce' ) ) { | |
| 90 | + return; | |
| 91 | + } | |
| 92 | + | |
| 93 | + if ( is_cart() || is_checkout() || is_account_page() ) { | |
| 94 | + remove_filter( 'the_content', 'sharing_display', 19 ); | |
| 95 | + if ( class_exists( 'Jetpack_Likes' ) ) { | |
| 96 | + remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30 ); | |
| 97 | + } | |
| 98 | + } | |
| 99 | +} | |
| 100 | +add_action( 'loop_start', 'jetpack_woocommerce_remove_share' ); | |
| 101 | + | |
| 102 | +/** | |
| 103 | + * Add a callback for WooCommerce product rendering in infinite scroll. | |
| 104 | + * | |
| 105 | + * @param array $callbacks Array of render callpacks for IS. | |
| 106 | + * @return array | |
| 107 | + */ | |
| 108 | +function jetpack_woocommerce_infinite_scroll_render_callback( $callbacks ) { | |
| 109 | + $callbacks[] = 'jetpack_woocommerce_infinite_scroll_render'; | |
| 110 | + return $callbacks; | |
| 111 | +} | |
| 112 | + | |
| 113 | +/** | |
| 114 | + * Add a default renderer for WooCommerce products within infinite scroll. | |
| 115 | + */ | |
| 116 | +function jetpack_woocommerce_infinite_scroll_render() { | |
| 117 | + if ( ! is_shop() && ! is_product_taxonomy() && ! is_product_category() && ! is_product_tag() ) { | |
| 118 | + return; | |
| 119 | + } | |
| 120 | + | |
| 121 | + woocommerce_product_loop_start(); | |
| 122 | + | |
| 123 | + while ( have_posts() ) { | |
| 124 | + the_post(); | |
| 125 | + wc_get_template_part( 'content', 'product' ); | |
| 126 | + } | |
| 127 | + | |
| 128 | + woocommerce_product_loop_end(); | |
| 129 | +} | |
| 130 | + | |
| 131 | +/** | |
| 132 | + * Basic styling when infinite scroll is active only. | |
| 133 | + */ | |
| 134 | +function jetpack_woocommerce_infinite_scroll_style() { | |
| 135 | + $custom_css = ' | |
| 136 | + .infinite-scroll .woocommerce-pagination { | |
| 137 | + display: none; | |
| 138 | + }'; | |
| 139 | + wp_add_inline_style( 'woocommerce-layout', $custom_css ); | |
| 140 | +} | |