Skip to content

Commit 60169b2

Browse files
committed
fix: compatibility with YITH WooCommerce Quick View plugin, replacing images returned by the ajax request, fix #87
1 parent 2b14e7c commit 60169b2

File tree

2 files changed

+85
-22
lines changed

2 files changed

+85
-22
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Class Optml_shortcode_ultimate.
5+
*
6+
* @reason We need to turn on the image replacement on quick view ajax response, even if the user is logged in.
7+
*/
8+
class Optml_yith_quick_view extends Optml_compatibility {
9+
10+
/**
11+
* Optml_yith_quick_view constructor.
12+
*/
13+
public function __construct() {
14+
add_filter( 'optml_force_replacement_on', [ $this, 'force_replacement' ] );
15+
}
16+
17+
/**
18+
* Allow image replacement even if we are logged in.
19+
*
20+
* @param bool $status Old status.
21+
*
22+
* @return bool Replacement status.
23+
*/
24+
public function force_replacement( $status ) {
25+
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'yith_load_product_quick_view' ) {
26+
return true;
27+
}
28+
29+
return false;
30+
}
31+
32+
/**
33+
* Should we load the integration logic.
34+
*
35+
* @return bool Should we load.
36+
*/
37+
function should_load() {
38+
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
39+
40+
return ( is_plugin_active( 'yith-woocommerce-quick-view/init.php' ) );
41+
}
42+
43+
/**
44+
* Register integration details.
45+
*/
46+
public function register() {
47+
add_action(
48+
'wp_ajax_yith_load_product_quick_view',
49+
[
50+
Optml_Main::instance()->manager,
51+
'process_template_redirect_content',
52+
],
53+
PHP_INT_MIN
54+
);
55+
}
56+
57+
}

inc/manager.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
* @author Optimole <friends@optimole.com>
88
*/
99
final class Optml_Manager {
10-
10+
/**
11+
* Holds allowed compatibilities objects.
12+
*
13+
* @var Optml_compatibility[] Compatibilities objects.
14+
*/
15+
public static $loaded_compatibilities = [];
1116
/**
1217
* Cached object instance.
1318
*
1419
* @var Optml_Manager
1520
*/
1621
protected static $instance = null;
17-
1822
/**
1923
* Holds the url replacer class.
2024
*
@@ -23,7 +27,6 @@ final class Optml_Manager {
2327
* @var Optml_Url_Replacer Replacer instance.
2428
*/
2529
public $url_replacer;
26-
2730
/**
2831
* Holds the tag replacer class.
2932
*
@@ -32,7 +35,6 @@ final class Optml_Manager {
3235
* @var Optml_Tag_Replacer Replacer instance.
3336
*/
3437
public $tag_replacer;
35-
3638
/**
3739
* Holds the lazyload replacer class.
3840
*
@@ -47,13 +49,12 @@ final class Optml_Manager {
4749
* @var Optml_Settings WordPress settings.
4850
*/
4951
protected $settings;
50-
5152
/**
5253
* Possible integrations with different plugins.
5354
*
5455
* @var array Integrations classes.
5556
*/
56-
private $compatibilities = array(
57+
private $possible_compatibilities = array(
5758
'shortcode_ultimate',
5859
'foogallery',
5960
'envira',
@@ -62,6 +63,7 @@ final class Optml_Manager {
6263
'revslider',
6364
'metaslider',
6465
'woocommerce',
66+
'yith_quick_view',
6567
);
6668

6769
/**
@@ -92,6 +94,20 @@ public function init() {
9294

9395
$this->settings = new Optml_Settings();
9496

97+
foreach ( $this->possible_compatibilities as $compatibility_class ) {
98+
$compatibility_class = 'Optml_' . $compatibility_class;
99+
$compatibility = new $compatibility_class;
100+
101+
/**
102+
* Check if we should load compatibility.
103+
*
104+
* @var Optml_compatibility $compatibility Class to register.
105+
*/
106+
if ( $compatibility->should_load() ) {
107+
self::$loaded_compatibilities[ $compatibility_class ] = $compatibility;
108+
}
109+
}
110+
95111
if ( ! $this->should_replace() ) {
96112
return;
97113
}
@@ -112,7 +128,6 @@ public function should_replace() {
112128
if ( ( is_admin() && ! self::is_ajax_request() ) || ! $this->settings->is_connected() || ! $this->settings->is_enabled() || is_customize_preview() ) {
113129
return false; // @codeCoverageIgnore
114130
}
115-
116131
if ( array_key_exists( 'preview', $_GET ) && 'true' == $_GET['preview'] ) {
117132
return false; // @codeCoverageIgnore
118133
}
@@ -140,7 +155,10 @@ public function should_replace() {
140155
* @return bool Is ajax request?
141156
*/
142157
public static function is_ajax_request() {
158+
if ( apply_filters( 'optml_force_replacement_on', false ) === true ) {
143159

160+
return true;
161+
}
144162
if ( ! function_exists( 'is_user_logged_in' ) ) {
145163
return false;
146164
}
@@ -163,9 +181,7 @@ public static function is_ajax_request() {
163181
* Register frontend replacer hooks.
164182
*/
165183
public function register_hooks() {
166-
167184
do_action( 'optml_replacer_setup' );
168-
169185
add_filter( 'the_content', array( $this, 'process_images_from_content' ), PHP_INT_MAX );
170186
/**
171187
* When we have to process cdn images, i.e MIRROR is defined,
@@ -180,23 +196,12 @@ public function register_hooks() {
180196
),
181197
defined( 'OPTML_SITE_MIRROR' ) ? PHP_INT_MAX : PHP_INT_MIN
182198
);
183-
184199
add_action( 'rest_api_init', array( $this, 'process_template_redirect_content' ), PHP_INT_MIN );
185200

186201
add_action( 'get_post_metadata', array( $this, 'replace_meta' ), PHP_INT_MAX, 4 );
187202

188-
foreach ( $this->compatibilities as $compatibility_class ) {
189-
$compatibility_class = 'Optml_' . $compatibility_class;
190-
$compatibility = new $compatibility_class;
191-
192-
/**
193-
* Check if we should load compatibility.
194-
*
195-
* @var Optml_compatibility $compatibility Class to register.
196-
*/
197-
if ( $compatibility->should_load() ) {
198-
$compatibility->register();
199-
}
203+
foreach ( self::$loaded_compatibilities as $registered_compatibility ) {
204+
$registered_compatibility->register();
200205
}
201206
}
202207

@@ -324,6 +329,7 @@ function ( $url ) {
324329
* @return mixed Filtered content.
325330
*/
326331
public function replace_content( $html ) {
332+
327333
$html = $this->process_images_from_content( $html );
328334

329335
$html = $this->process_urls_from_content( $html );

0 commit comments

Comments
 (0)