Skip to content

Commit 37e4271

Browse files
committed
Abilities API: Catch exceptions thrown by ability callbacks and return WP_Error.
Wraps `invoke_callback()` in a try/catch so that exceptions thrown by execute or permission callbacks are converted to a `WP_Error` with the `ability_callback_exception` code instead of propagating as uncaught throwables. Developed in: WordPress/wordpress-develop#11544 Reviewed by adamsilverstein, justlevine, jorbin. Merges [62238] to the 7.0 branch. Props priyankagusani, jamesgiroux, jeffpaul, dkotter, adamsilverstein, justlevine, jorbin, pavanpatil1. Fixes #65058. Built from https://develop.svn.wordpress.org/branches/7.0@62240 git-svn-id: http://core.svn.wordpress.org/branches/7.0@61520 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 00db7bf commit 37e4271

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

wp-includes/abilities-api/class-wp-ability.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,15 +502,27 @@ public function validate_input( $input = null ) {
502502
*
503503
* @param callable $callback The callable to invoke.
504504
* @param mixed $input Optional. The input data for the ability. Default `null`.
505-
* @return mixed The result of the callable execution.
505+
* @return mixed The result of the callable execution, or a `WP_Error` if the callback threw.
506506
*/
507507
protected function invoke_callback( callable $callback, $input = null ) {
508508
$args = array();
509509
if ( ! empty( $this->get_input_schema() ) ) {
510510
$args[] = $input;
511511
}
512512

513-
return $callback( ...$args );
513+
try {
514+
return $callback( ...$args );
515+
} catch ( Throwable $e ) {
516+
return new WP_Error(
517+
'ability_callback_exception',
518+
sprintf(
519+
/* translators: 1: Ability name, 2: Exception message. */
520+
__( 'Ability "%1$s" callback threw an exception: %2$s' ),
521+
esc_html( $this->name ),
522+
esc_html( $e->getMessage() )
523+
)
524+
);
525+
}
514526
}
515527

516528
/**

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '7.0-RC2-62236';
19+
$wp_version = '7.0-RC2-62240';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)