| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_facetwp |
| 5 |
* |
| 6 |
* @reason Adding filter to force replacement on api request. |
| 7 |
*/ |
| 8 |
class Optml_facetwp 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 |
return is_plugin_active( 'facetwp/index.php' ); |
| 18 |
} |
| 19 |
/** |
| 20 |
* Register integration details. |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
add_filter( 'facetwp_ajax_response', [ $this, 'api_replacement_filter' ], 1, 2 ); |
| 24 |
add_action( 'facetwp_inject_template', [ $this, 'api_replacement_action' ], 1 ); |
| 25 |
} |
| 26 |
/** |
| 27 |
* Add filter to replace images from api requests. |
| 28 |
* |
| 29 |
* Modifies template for a facetwp template. |
| 30 |
* NOTE: $output needs to be json decoded before modification and re encoded before returning. |
| 31 |
* |
| 32 |
* @param string $output The output before optimization. |
| 33 |
* @param array $data More data about the request. |
| 34 |
* @return string The output with the optimized images. |
| 35 |
*/ |
| 36 |
public function api_replacement_filter( $output, $data ) { |
| 37 |
do_action( 'optml_replacer_setup' ); |
| 38 |
|
| 39 |
$output = json_decode( $output ); |
| 40 |
|
| 41 |
if ( isset( $output->template ) ) { |
| 42 |
$output->template = Optml_Main::instance()->manager->replace_content( $output->template, true ); |
| 43 |
} |
| 44 |
|
| 45 |
// Ignore invalid UTF-8 characters in PHP 7.2+ |
| 46 |
if ( version_compare( phpversion(), '7.2', '<' ) ) { |
| 47 |
$output = json_encode( $output ); |
| 48 |
} else { |
| 49 |
$output = json_encode( $output, JSON_INVALID_UTF8_IGNORE ); |
| 50 |
} |
| 51 |
$output = Optml_Main::instance()->manager->replace_content( $output, true ); |
| 52 |
return $output; |
| 53 |
} |
| 54 |
/** |
| 55 |
* Add action to replace images from facetwp api requests. |
| 56 |
* |
| 57 |
* Modifies template for a non-facetwp template. |
| 58 |
* |
| 59 |
* @param array $output The output before optimization. |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function api_replacement_action( $output ) { |
| 63 |
do_action( 'optml_replacer_setup' ); |
| 64 |
|
| 65 |
if ( ! isset( $output['template'] ) || ! function_exists( 'FWP' ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
$output['template'] = Optml_Main::instance()->manager->replace_content( $output['template'], true ); |
| 69 |
FWP()->request->output = $output; |
| 70 |
} |
| 71 |
/** |
| 72 |
* Should we early load the compatibility? |
| 73 |
* |
| 74 |
* @return bool Whether to load the compatibility or not. |
| 75 |
*/ |
| 76 |
public function should_load_early() { |
| 77 |
return true; |
| 78 |
} |
| 79 |
} |
| 80 |
|