PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.8
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.8
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / 3rd-party / WooCommerce / class-woocommerce.php

class-woocommerce.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.8, at inc/3rd-party/WooCommerce/class-woocommerce.php

96 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\ThirdParty\WooCommerce;
3
4 /**
5 * Compatibility for WooCommerce.
6 *
7 * @since 1.10.0
8 */
9 class WooCommerce {
10 /**
11 * Initialize compatibility functionality.
12 *
13 * @since 1.10.0
14 *
15 * @return void
16 */
17 public function init() {
18 add_action( 'woocommerce_single_product_summary', [ $this, 'variable_products_nextgen_compat' ] );
19 }
20
21 /**
22 * Add Variable Products Next-gen images Compatibility.
23 *
24 * @since 1.10.0
25 *
26 * @return void
27 */
28 public function variable_products_nextgen_compat() {
29 global $product;
30
31 if ( ! isset( $product ) || ! $product->is_type( 'variable' ) ) {
32 return;
33 }
34
35 add_filter( 'imagify_picture_attributes', [ $this, 'remove_wp_post_image_class' ], 10, 2 );
36 add_filter(
37 'imagify_picture_source_attributes',
38 [ $this, 'maybe_add_wp_post_image_class_on_picture_internal_tags' ],
39 10,
40 2
41 );
42 add_filter(
43 'imagify_picture_img_attributes',
44 [ $this, 'maybe_add_wp_post_image_class_on_picture_internal_tags' ],
45 10,
46 2
47 );
48 }
49
50 /**
51 * Remove wp-post-image class from picture tags.
52 *
53 * @since 1.10.0
54 *
55 * @param array $attributes The picture tag attributes.
56 *
57 * @return array The picture tage attributes with modified or removed 'class'.
58 */
59 public function remove_wp_post_image_class( $attributes ) {
60 if ( isset( $attributes['class'] ) ) {
61 $attributes['class'] = str_replace( 'wp-post-image', '', $attributes['class'] );
62 }
63
64 if ( empty( $attributes['class'] ) ) {
65 unset( $attributes['class'] );
66 }
67
68 return $attributes;
69 }
70
71 /**
72 * Add wp-post-image class to source and image tags internal to a picture tag.
73 *
74 * @since 1.10.0
75 *
76 * @param array $attributes The source or img tag attributes.
77 * @param array $image The original image tag data.
78 *
79 * @return array Source or image tag attributes with modified 'class'.
80 */
81 public function maybe_add_wp_post_image_class_on_picture_internal_tags( $attributes, $image ) {
82 if (
83 ! empty( $image['attributes']['class'] )
84 && strpos( $image['attributes']['class'], 'wp-post-image' ) !== false
85 ) {
86 $attributes['class'] = isset( $attributes['class'] )
87 ? $attributes['class'] . ' wp-post-image'
88 : 'wp-post-image';
89 }
90
91 return $attributes;
92 }
93 }
94
95 ( new WooCommerce() )->init();
96