PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / trunk
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization vtrunk
4.2.14 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 All 135 releases
optimole-wp / inc / compatibilities / groovy_menu.php

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

69 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 * Class Optml_groovy_menu.
4 *
5 * @reason Groovy Menu's auto-integration opens its own output buffer on `init`
6 * and, on `shutdown` at priority 0, calls ob_get_clean() on whichever buffer is
7 * on top to insert the menu markup after <body>.
8 *
9 * This only matters with the `optml_capture_at_shutdown` opt-in, where we capture
10 * and process our buffer at `shutdown` (PHP_INT_MIN) and re-arm an empty one, so
11 * Groovy Menu would receive an empty string and drop the menu. In that mode we
12 * apply Groovy Menu's final-output filter to the page we capture, before image
13 * replacement, and unhook its own shutdown step at that moment. In the default
14 * in-handler mode our capture never runs and Groovy Menu keeps its own step.
15 */
16 class Optml_groovy_menu extends Optml_compatibility {
17 /**
18 * Groovy Menu's shutdown callback that grabs the top output buffer.
19 */
20 const GROOVY_SHUTDOWN_CALLBACK = 'groovy_menu_pre_shutdown';
21
22 /**
23 * Groovy Menu's filter that inserts the menu markup into the page HTML.
24 */
25 const GROOVY_OUTPUT_FILTER = 'groovy_menu_final_output';
26
27 /**
28 * Should we load the integration logic.
29 *
30 * @return bool Should we load.
31 */
32 public function should_load() {
33 return function_exists( self::GROOVY_SHUTDOWN_CALLBACK )
34 && has_action( 'shutdown', self::GROOVY_SHUTDOWN_CALLBACK ) !== false;
35 }
36
37 /**
38 * Register integration details.
39 *
40 * @return void
41 */
42 public function register() {
43 add_filter( 'optml_captured_page_html', [ $this, 'insert_menu' ] );
44 }
45
46 /**
47 * Insert the Groovy Menu markup into the captured page and take over its shutdown step.
48 *
49 * @param string $html The captured page HTML, before image replacement.
50 *
51 * @return string
52 */
53 public function insert_menu( $html ) {
54 $priority = has_action( 'shutdown', self::GROOVY_SHUTDOWN_CALLBACK );
55 if ( $priority === false ) {
56 return $html;
57 }
58 // Our capture ran, so Groovy Menu must not ob_get_clean() the re-armed empty buffer afterwards.
59 remove_action( 'shutdown', self::GROOVY_SHUTDOWN_CALLBACK, $priority );
60
61 // Same guard as Groovy Menu's own shutdown callback.
62 if ( ! defined( 'GROOVY_MENU_SCRIPTS_INIT' ) ) {
63 return $html;
64 }
65
66 return apply_filters( self::GROOVY_OUTPUT_FILTER, $html ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Groovy Menu's own filter.
67 }
68 }
69