[wp-trac] [WordPress Trac] #40568: Add an action in WP_Error::add()

WordPress Trac noreply at wordpress.org
Wed Nov 18 09:08:02 UTC 2020


#40568: Add an action in WP_Error::add()
------------------------------------+--------------------------
 Reporter:  Shelob9                 |       Owner:  johnbillion
     Type:  enhancement             |      Status:  closed
 Priority:  normal                  |   Milestone:  5.6
Component:  General                 |     Version:  2.1
 Severity:  normal                  |  Resolution:  fixed
 Keywords:  dev-feedback has-patch  |     Focuses:
------------------------------------+--------------------------

Comment (by giuseppe.mazzapica):

 I saw this reopened and wanted to comment, but I was too slow for
 @SergeyBiryukov

 I'll comment anyway.

 `is_wp_error` has always been a pure function, that has been used in pure
 logic without consequences.

 Since I saw this ticket, I changed all occurrences of `is_wp_error` with
 `$thing instanceof WP_Error` and we're discussing to add `is_wp_error` to
 the list of discouraged functions in our code style.

 Checking a type of a variable should not be impure, nor trigger actions.

 I understand the reasoning in the ticket, but maybe could you consider to
 add a parameter to `is_wp_error` that make the function trigger the hook?

 `shortcode_atts` do something similar. If you pass the third argument to
 it, it fires an action, otherwise it is a quite useful pure function
 (https://github.com/WordPress/WordPress/blob/master/wp-
 includes/shortcodes.php#L582-L597).

 Could you consider to do the same for `is_wp_error`?

 That would mean that in all places where WP itself uses it, and it makes
 sense, that parameter would need to be passed. That could be a lot of
 work, I checked and there are ~420 occurrences of `is_wp_error` in WP,
 even if I think most of them would not need to checked.

 For example, in core the same error can be checked multiple times and
 triggering that hook multiple times for same error seems overkill to me.

 Alternatively, could you add a parameter to opt-out from the hook
 triggering?

 So the code would become either the following or the same but `$do_action`
 default to true.


 {{{#!php
 <?php
 function is_wp_error( $thing, $do_action = false ) {

     $is_wp_error = ( $thing instanceof WP_Error );

     if ( $do_action && $is_wp_error ) {
         /**
         * Fires when `is_wp_error()` is called and its parameter is an
 instance of `WP_Error`.
         *
         * @since 5.6.0
         *
         * @param WP_Error $thing The error object passed to
 `is_wp_error()`.
         */
         do_action( 'is_wp_error_instance', $thing );
     }

     return $is_wp_error;
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/40568#comment:31>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list