Skip to content

Commit

Permalink
fix: compatibility with YITH WooCommerce Quick View plugin, replacing…
Browse files Browse the repository at this point in the history
… images returned by the ajax request, fix #87
  • Loading branch information
selul committed Mar 27, 2019
1 parent 2b14e7c commit 60169b2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 22 deletions.
57 changes: 57 additions & 0 deletions inc/compatibilities/yith_quick_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* Class Optml_shortcode_ultimate.
*
* @reason We need to turn on the image replacement on quick view ajax response, even if the user is logged in.
*/
class Optml_yith_quick_view extends Optml_compatibility {

/**
* Optml_yith_quick_view constructor.
*/
public function __construct() {
add_filter( 'optml_force_replacement_on', [ $this, 'force_replacement' ] );
}

/**
* Allow image replacement even if we are logged in.
*
* @param bool $status Old status.
*
* @return bool Replacement status.
*/
public function force_replacement( $status ) {
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'yith_load_product_quick_view' ) {
return true;
}

return false;
}

/**
* Should we load the integration logic.
*
* @return bool Should we load.
*/
function should_load() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

return ( is_plugin_active( 'yith-woocommerce-quick-view/init.php' ) );
}

/**
* Register integration details.
*/
public function register() {
add_action(
'wp_ajax_yith_load_product_quick_view',
[
Optml_Main::instance()->manager,
'process_template_redirect_content',
],
PHP_INT_MIN
);
}

}
50 changes: 28 additions & 22 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
* @author Optimole <[email protected]>
*/
final class Optml_Manager {

/**
* Holds allowed compatibilities objects.
*
* @var Optml_compatibility[] Compatibilities objects.
*/
public static $loaded_compatibilities = [];
/**
* Cached object instance.
*
* @var Optml_Manager
*/
protected static $instance = null;

/**
* Holds the url replacer class.
*
Expand All @@ -23,7 +27,6 @@ final class Optml_Manager {
* @var Optml_Url_Replacer Replacer instance.
*/
public $url_replacer;

/**
* Holds the tag replacer class.
*
Expand All @@ -32,7 +35,6 @@ final class Optml_Manager {
* @var Optml_Tag_Replacer Replacer instance.
*/
public $tag_replacer;

/**
* Holds the lazyload replacer class.
*
Expand All @@ -47,13 +49,12 @@ final class Optml_Manager {
* @var Optml_Settings WordPress settings.
*/
protected $settings;

/**
* Possible integrations with different plugins.
*
* @var array Integrations classes.
*/
private $compatibilities = array(
private $possible_compatibilities = array(
'shortcode_ultimate',
'foogallery',
'envira',
Expand All @@ -62,6 +63,7 @@ final class Optml_Manager {
'revslider',
'metaslider',
'woocommerce',
'yith_quick_view',
);

/**
Expand Down Expand Up @@ -92,6 +94,20 @@ public function init() {

$this->settings = new Optml_Settings();

foreach ( $this->possible_compatibilities as $compatibility_class ) {
$compatibility_class = 'Optml_' . $compatibility_class;
$compatibility = new $compatibility_class;

/**
* Check if we should load compatibility.
*
* @var Optml_compatibility $compatibility Class to register.
*/
if ( $compatibility->should_load() ) {
self::$loaded_compatibilities[ $compatibility_class ] = $compatibility;
}
}

if ( ! $this->should_replace() ) {
return;
}
Expand All @@ -112,7 +128,6 @@ public function should_replace() {
if ( ( is_admin() && ! self::is_ajax_request() ) || ! $this->settings->is_connected() || ! $this->settings->is_enabled() || is_customize_preview() ) {
return false; // @codeCoverageIgnore
}

if ( array_key_exists( 'preview', $_GET ) && 'true' == $_GET['preview'] ) {
return false; // @codeCoverageIgnore
}
Expand Down Expand Up @@ -140,7 +155,10 @@ public function should_replace() {
* @return bool Is ajax request?
*/
public static function is_ajax_request() {
if ( apply_filters( 'optml_force_replacement_on', false ) === true ) {

return true;
}
if ( ! function_exists( 'is_user_logged_in' ) ) {
return false;
}
Expand All @@ -163,9 +181,7 @@ public static function is_ajax_request() {
* Register frontend replacer hooks.
*/
public function register_hooks() {

do_action( 'optml_replacer_setup' );

add_filter( 'the_content', array( $this, 'process_images_from_content' ), PHP_INT_MAX );
/**
* When we have to process cdn images, i.e MIRROR is defined,
Expand All @@ -180,23 +196,12 @@ public function register_hooks() {
),
defined( 'OPTML_SITE_MIRROR' ) ? PHP_INT_MAX : PHP_INT_MIN
);

add_action( 'rest_api_init', array( $this, 'process_template_redirect_content' ), PHP_INT_MIN );

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

foreach ( $this->compatibilities as $compatibility_class ) {
$compatibility_class = 'Optml_' . $compatibility_class;
$compatibility = new $compatibility_class;

/**
* Check if we should load compatibility.
*
* @var Optml_compatibility $compatibility Class to register.
*/
if ( $compatibility->should_load() ) {
$compatibility->register();
}
foreach ( self::$loaded_compatibilities as $registered_compatibility ) {
$registered_compatibility->register();
}
}

Expand Down Expand Up @@ -324,6 +329,7 @@ function ( $url ) {
* @return mixed Filtered content.
*/
public function replace_content( $html ) {

$html = $this->process_images_from_content( $html );

$html = $this->process_urls_from_content( $html );
Expand Down

0 comments on commit 60169b2

Please sign in to comment.