PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 4.2.6
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v4.2.6
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / compatibilities / facetwp.php

facetwp.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 4.2.6, at inc/compatibilities/facetwp.php

80 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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