Make WordPress Core


Ignore:
Timestamp:
04/16/2026 07:22:23 AM (2 months ago)
Author:
gziolo
Message:

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: https://github.com/WordPress/wordpress-develop/pull/11544

Props priyankagusani, jamesgiroux, jeffpaul, dkotter, adamsilverstein, justlevine, jorbin, pavanpatil1.
Fixes #65058.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/abilities-api/class-wp-ability.php

    r62094 r62238  
    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 ) {
     
    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
Note: See TracChangeset for help on using the changeset viewer.