Skip to content

Commit f4bf311

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

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-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
}

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,12 @@ parameters:
10201020
count: 1
10211021
path: inc/compatibilities/metaslider.php
10221022

1023+
-
1024+
message: '#^Method Optml_otter_blocks\:\:maybe_init_replacer_for_rest\(\) has parameter \$request with generic class WP_REST_Request but does not specify its types\: T$#'
1025+
identifier: missingType.generics
1026+
count: 1
1027+
path: inc/compatibilities/otter_blocks.php
1028+
10231029
-
10241030
message: '#^Method Optml_otter_blocks\:\:register\(\) has no return type specified\.$#'
10251031
identifier: missingType.return

0 commit comments

Comments
 (0)