diff --git a/inc/app_replacer.php b/inc/app_replacer.php index d00ef10c..74eb0537 100644 --- a/inc/app_replacer.php +++ b/inc/app_replacer.php @@ -164,7 +164,9 @@ public function init() { * @return bool If we can replace the image. */ public function should_replace() { - + if ( Optml_Manager::is_ajax_request() ) { + return true; + } if ( is_admin() || ! $this->settings->is_connected() || ! $this->settings->is_enabled() || is_customize_preview() ) { return false; // @codeCoverageIgnore } diff --git a/inc/manager.php b/inc/manager.php index c26dc79a..2a81f9f1 100644 --- a/inc/manager.php +++ b/inc/manager.php @@ -45,7 +45,7 @@ public function init() { * Otherwise, we can hook first to avoid any other plugins to take care of replacement. */ add_action( - 'template_redirect', + self::is_ajax_request() ? 'init' : 'template_redirect', array( $this, 'process_template_redirect_content', @@ -55,6 +55,31 @@ public function init() { add_action( 'get_post_metadata', array( $this, 'replace_meta' ), PHP_INT_MAX, 4 ); } + /** + * Check if we are in a ajax contex where we should enable replacement. + * + * @return bool Is ajax request? + */ + public static function is_ajax_request() { + + if ( ! function_exists( 'is_user_logged_in' ) ) { + return false; + } + // Disable for logged in users to avoid unexpected results. + if ( is_user_logged_in() ) { + return false; + } + + if ( ! function_exists( 'wp_doing_ajax' ) ) { + return false; + } + if ( ! wp_doing_ajax() ) { + return false; + } + + return true; + } + /** * Handles the url replacement in options and theme mods. */