Skip to content

Commit

Permalink
Try considering PHP_OUTPUT_HANDLER_FINAL in output buffer callback
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Aug 2, 2024
1 parent 08eba80 commit b3ce59e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
23 changes: 13 additions & 10 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@
*/
function od_buffer_output( string $passthrough ): string {
ob_start(
static function ( string $output ): string {
/**
* Filters the template output buffer prior to sending to the client.
*
* @since 0.1.0
*
* @param string $output Output buffer.
* @return string Filtered output buffer.
*/
return (string) apply_filters( 'od_template_output_buffer', $output );
static function ( string $output, ?int $phase ): string {
if ( $phase & PHP_OUTPUT_HANDLER_FINAL ) {

Check failure on line 36 in plugins/optimization-detective/optimization.php

View workflow job for this annotation

GitHub Actions / PHP

Only booleans are allowed in an if condition, int<0, 8> given.
/**
* Filters the template output buffer prior to sending to the client.
*
* @since 0.1.0
*
* @param string $output Output buffer.
* @return string Filtered output buffer.
*/
$output = (string) apply_filters( 'od_template_output_buffer', $output );
}
return $output;
}
);
return $passthrough;
Expand Down
42 changes: 41 additions & 1 deletion plugins/optimization-detective/tests/test-optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public function test_od_buffer_output(): void {
// buffer callback. See <https://stackoverflow.com/a/61439514/93579>.
ob_start();

$filter_invoked = false;
add_filter(
'od_template_output_buffer',
function ( $buffer ) use ( $original, $expected ) {
function ( $buffer ) use ( $original, $expected, &$filter_invoked ) {
$this->assertSame( $original, $buffer );
$filter_invoked = true;
return $expected;
}
);
Expand All @@ -71,6 +73,44 @@ function ( $buffer ) use ( $original, $expected ) {

$buffer = ob_get_clean(); // Get the buffer from our wrapper output buffer.
$this->assertSame( $expected, $buffer );
$this->assertTrue( $filter_invoked );
}

/**
* Make output is buffered and that it is also filtered.
*
* @covers ::od_buffer_output
*/
public function test_od_buffer_output_not_finalized(): void {
$original = 'Hello My World!';
$override = 'Ciao mondo!';

// In order to test, a wrapping output buffer is required because ob_get_clean() does not invoke the output
// buffer callback. See <https://stackoverflow.com/a/61439514/93579>.
ob_start();

$filter_invoked = false;
add_filter(
'od_template_output_buffer',
function ( $buffer ) use ( $original, &$filter_invoked ) {
$this->assertSame( $original, $buffer );
$filter_invoked = true;
return '¡Hola Mi Mundo!';
}
);

$original_ob_level = ob_get_level();
od_buffer_output( '' );
$this->assertSame( $original_ob_level + 1, ob_get_level(), 'Expected call to ob_start().' );
echo $original;

ob_clean(); // Note the lack of flush here.
echo $override;

$buffer = ob_get_clean(); // Get the buffer from our wrapper output buffer.

$this->assertFalse( $filter_invoked );
$this->assertSame( $override, $buffer );
}

/**
Expand Down

0 comments on commit b3ce59e

Please sign in to comment.