From 8d10484a4b1b45f874d7bbcbde4a6cc35fca5637 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Fri, 10 Jan 2025 13:56:48 -0500 Subject: [PATCH] Add capture helper, change defer helper --- CHANGELOG.md | 11 +++++ .../support/helpers/helpers-general.php | 47 +++++++++++++++---- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae49cbce..ad54bf1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## v1.3.3 - 2025-01-10 + +### Added + +- Added `Mantle\Support\Helpers\capture` helper to capture output from a callback using output buffering. + +### Changed + +- Updated the `Mantle\Support\Helpers\defer` helper to be able to used outside + of the Mantle Framework via the `shutdown` hook. + ## v1.3.2 - 2024-12-17 - Allow stray requests to be ignored and pass through when stray requests are being prevented. diff --git a/src/mantle/support/helpers/helpers-general.php b/src/mantle/support/helpers/helpers-general.php index 866ae8c1..e31b8a19 100644 --- a/src/mantle/support/helpers/helpers-general.php +++ b/src/mantle/support/helpers/helpers-general.php @@ -426,6 +426,18 @@ function the_classnames( ...$args ): void { echo esc_attr( classname( ...$args ) ); } +/** + * Capture the output of a callback. + * + * @param callable $callback + * @return string + */ +function capture( callable $callback ): string { + ob_start(); + $callback(); + return ob_get_clean(); +} + /** * Add a WordPress action with type-hint support. * @@ -532,13 +544,32 @@ function validate_file( $file, $allowed_files = [] ) { return 0; } -if ( ! function_exists( __NAMESPACE__ . '\defer' ) ) { - /** - * Defer the execution of a function until after the response is sent to the page. - * - * @param callable $callback Callback to defer. - */ - function defer( callable $callback ): void { - app()->terminating( $callback ); +/** + * Defer the execution of a function until after the response is sent to the + * page. + * + * When used outside of the Mantle Framework, the callback will be added to the + * 'shutdown' hook after sending the response to the client. + * + * @param callable $callback Callback to defer. + */ +function defer( callable $callback ): void { + if ( ! function_exists( 'app' ) ) { + \add_action( + 'shutdown', + function () use ( $callback ) { + if ( function_exists( 'fastcgi_finish_request' ) ) { + fastcgi_finish_request(); + } elseif ( function_exists( 'litespeed_finish_request' ) ) { + litespeed_finish_request(); + } + + $callback(); + }, + ); + + return; } + + app()->terminating( $callback ); }