| 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_webp_compat' ] ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Add Variable Products Webp Compatibility. |
| 23 |
* |
| 24 |
* @since 1.10.0 |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function variable_products_webp_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 |
|