| 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 |
|