Skip to content

Commit d175dc0

Browse files
committed
fix: compatibility with otter blocks background images defined in CSS files [closes Codeinwp/optimole-service#1594]
1 parent 4514c15 commit d175dc0

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

inc/compatibilities/otter_blocks.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
class Optml_otter_blocks extends Optml_compatibility {
99

10-
1110
/**
1211
* Should we load the integration logic.
1312
*
@@ -39,5 +38,43 @@ function ( $all_watchers ) {
3938

4039
// Replace the image URL with the optimized one in Otter-generated CSS.
4140
add_filter( 'otter_apply_dynamic_image', [ Optml_Main::instance()->manager->url_replacer, 'build_url' ], 99 );
41+
42+
// Ensure replacer is initialized for Otter REST API routes (where register_hooks isn't called).
43+
add_filter( 'rest_pre_dispatch', [ $this, 'maybe_init_replacer_for_rest' ], 10, 3 );
44+
}
45+
46+
/**
47+
* Initialize replacer for Otter REST API routes.
48+
*
49+
* @param mixed $result Response to replace the requested version with.
50+
* @param \WP_REST_Server $server Server instance.
51+
* @param \WP_REST_Request $request Request used to generate the response.
52+
* @return mixed Unmodified result.
53+
*/
54+
public function maybe_init_replacer_for_rest( $result, \WP_REST_Server $server, \WP_REST_Request $request ) {
55+
$route = $request->get_route();
56+
57+
// Only initialize for Otter styles REST routes.
58+
if ( strpos( $route, '/otter/v1/post_styles' ) === false
59+
&& strpos( $route, '/otter/v1/widget_styles' ) === false
60+
&& strpos( $route, '/otter/v1/block_styles' ) === false
61+
) {
62+
return $result;
63+
}
64+
65+
if ( ! did_action( 'optml_replacer_setup' ) ) {
66+
do_action( 'optml_replacer_setup' );
67+
}
68+
69+
return $result;
70+
}
71+
72+
/**
73+
* Should we early load the compatibility?
74+
*
75+
* @return bool Whether to load the compatibility early.
76+
*/
77+
public function should_load_early() {
78+
return true;
4279
}
4380
}

0 commit comments

Comments
 (0)