| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_otter_blocks. |
| 5 |
* |
| 6 |
* @reason Adding selectors for background lazyload |
| 7 |
*/ |
| 8 |
class Optml_otter_blocks extends Optml_compatibility { |
| 9 |
|
| 10 |
/** |
| 11 |
* Should we load the integration logic. |
| 12 |
* |
| 13 |
* @return bool Should we load. |
| 14 |
*/ |
| 15 |
public function should_load() { |
| 16 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 17 |
|
| 18 |
return is_plugin_active( 'otter-blocks/otter-blocks.php' ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Register integration details. |
| 23 |
*/ |
| 24 |
public function register() { |
| 25 |
add_filter( |
| 26 |
'optml_lazyload_bg_selectors', |
| 27 |
function ( $all_watchers ) { |
| 28 |
$all_watchers[] = '.o-flip-front'; |
| 29 |
$all_watchers[] = '.o-flip-back'; |
| 30 |
$all_watchers[] = '.wp-block-themeisle-blocks-advanced-columns'; |
| 31 |
$all_watchers[] = '.wp-block-themeisle-blocks-advanced-columns-overlay'; |
| 32 |
$all_watchers[] = '.wp-block-themeisle-blocks-advanced-column'; |
| 33 |
$all_watchers[] = '.wp-block-themeisle-blocks-advanced-column-overlay'; |
| 34 |
|
| 35 |
return $all_watchers; |
| 36 |
} |
| 37 |
); |
| 38 |
|
| 39 |
// Replace the image URL with the optimized one in Otter-generated CSS. |
| 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; |
| 79 |
} |
| 80 |
} |
| 81 |
|