|
7 | 7 | */ |
8 | 8 | class Optml_otter_blocks extends Optml_compatibility { |
9 | 9 |
|
10 | | - |
11 | 10 | /** |
12 | 11 | * Should we load the integration logic. |
13 | 12 | * |
@@ -39,5 +38,43 @@ function ( $all_watchers ) { |
39 | 38 |
|
40 | 39 | // Replace the image URL with the optimized one in Otter-generated CSS. |
41 | 40 | 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; |
42 | 79 | } |
43 | 80 | } |
0 commit comments