Skip to content

Commit

Permalink
Add capture helper, change defer helper
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Jan 10, 2025
1 parent 13dc068 commit 8d10484
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 39 additions & 8 deletions src/mantle/support/helpers/helpers-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
}

0 comments on commit 8d10484

Please sign in to comment.