Skip to content

Commit

Permalink
Consolidate all hooks to hooks.php
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Mar 19, 2024
1 parent f6e0683 commit b74f7eb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 73 deletions.
38 changes: 2 additions & 36 deletions plugins/optimization-detective/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,6 @@
exit; // Exit if accessed directly.
}

/**
* Starts output buffering at the end of the 'template_include' filter.
*
* This is to implement #43258 in core.
*
* This is a hack which would eventually be replaced with something like this in wp-includes/template-loader.php:
*
* $template = apply_filters( 'template_include', $template );
* + ob_start( 'wp_template_output_buffer_callback' );
* if ( $template ) {
* include $template;
* } elseif ( current_user_can( 'switch_themes' ) ) {
*
* @since 0.1.0
* @access private
* @link https://core.trac.wordpress.org/ticket/43258
*
* @param string $passthrough Optional. Filter value. Default null.
* @return string Unmodified value of $passthrough.
*/
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 );
}
);
return $passthrough;
}
add_filter( 'template_include', 'od_buffer_output', PHP_INT_MAX );
OD_URL_Metrics_Post_Type::add_hooks();
add_action( 'wp', 'od_maybe_add_template_output_buffer_filter' );
8 changes: 5 additions & 3 deletions plugins/optimization-detective/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

define( 'OPTIMIZATION_DETECTIVE_VERSION', '0.1.0' );

require_once __DIR__ . '/hooks.php';

// Storage logic.
require_once __DIR__ . '/class-od-data-validation-exception.php';
require_once __DIR__ . '/class-od-url-metric.php';
Expand All @@ -38,9 +36,13 @@
require_once __DIR__ . '/storage/class-od-url-metrics-post-type.php';
require_once __DIR__ . '/storage/data.php';
require_once __DIR__ . '/storage/rest-api.php';
OD_URL_Metrics_Post_Type::add_hooks();

// Detection logic.
require_once __DIR__ . '/detection.php';

// Optimization logic.
require_once __DIR__ . '/class-od-html-tag-processor.php';
require_once __DIR__ . '/optimization.php';

// Add hooks for the above requires.
require_once __DIR__ . '/hooks.php';
38 changes: 37 additions & 1 deletion plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,43 @@
exit; // Exit if accessed directly.
}

/**
* Starts output buffering at the end of the 'template_include' filter.
*
* This is to implement #43258 in core.
*
* This is a hack which would eventually be replaced with something like this in wp-includes/template-loader.php:
*
* $template = apply_filters( 'template_include', $template );
* + ob_start( 'wp_template_output_buffer_callback' );
* if ( $template ) {
* include $template;
* } elseif ( current_user_can( 'switch_themes' ) ) {
*
* @since 0.1.0
* @access private
* @link https://core.trac.wordpress.org/ticket/43258
*
* @param string $passthrough Optional. Filter value. Default null.
* @return string Unmodified value of $passthrough.
*/
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 );
}
);
return $passthrough;
}

/**
* Adds template output buffer filter for optimization if eligible.
*
Expand All @@ -26,7 +63,6 @@ function od_maybe_add_template_output_buffer_filter() {
}
add_filter( 'od_template_output_buffer', $callback );
}
add_action( 'wp', 'od_maybe_add_template_output_buffer_filter' );

/**
* Determines whether the current response can be optimized.
Expand Down
47 changes: 14 additions & 33 deletions tests/plugins/optimization-detective/hooks-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,22 @@
class OD_Hooks_Tests extends WP_UnitTestCase {

/**
* Make sure the hook is added.
*/
public function test_hooking_output_buffering_at_template_include() {
$this->assertEquals( PHP_INT_MAX, has_filter( 'template_include', 'od_buffer_output' ) );
}

/**
* Make output is buffered and that it is also filtered.
* Make sure the hooks are added in hooks.php.
*
* @covers ::od_buffer_output
* @see OD_Storage_Post_Type_Tests::test_add_hooks()
*/
public function test_buffering_and_filtering_output() {
$original = 'Hello World!';
$expected = '¡Hola Mundo!';

// 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();

add_filter(
'od_template_output_buffer',
function ( $buffer ) use ( $original, $expected ) {
$this->assertSame( $original, $buffer );
return $expected;
}
public function test_hooks_added() {
$this->assertEquals( PHP_INT_MAX, has_filter( 'template_include', 'od_buffer_output' ) );
$this->assertEquals( 10, has_filter( 'wp', 'od_maybe_add_template_output_buffer_filter' ) );
$this->assertSame(
10,
has_action(
'init',
array(
OD_URL_Metrics_Post_Type::class,
'register_post_type',
)
)
);

$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_end_flush(); // Flushing invokes the output buffer callback.

$buffer = ob_get_clean(); // Get the buffer from our wrapper output buffer.
$this->assertSame( $expected, $buffer );
}
}
32 changes: 32 additions & 0 deletions tests/plugins/optimization-detective/optimization-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ public function tear_down() {
parent::tear_down();
}

/**
* Make output is buffered and that it is also filtered.
*
* @covers ::od_buffer_output
*/
public function test_od_buffer_output() {
$original = 'Hello World!';
$expected = '¡Hola Mundo!';

// 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();

add_filter(
'od_template_output_buffer',
function ( $buffer ) use ( $original, $expected ) {
$this->assertSame( $original, $buffer );
return $expected;
}
);

$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_end_flush(); // Flushing invokes the output buffer callback.

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

/**
* Test od_maybe_add_template_output_buffer_filter().
*
Expand Down

0 comments on commit b74f7eb

Please sign in to comment.