Skip to content

Commit

Permalink
fix: image replacement on admin ajax requests
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Feb 4, 2019
1 parent 8aca6dc commit 924cc49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 3 additions & 1 deletion inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 26 additions & 1 deletion inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.
*/
Expand Down

0 comments on commit 924cc49

Please sign in to comment.