[wp-trac] [WordPress Trac] #58532: Improve performance and simplify usage of `block_has_support()` by supporting a string feature and avoiding `_wp_array_get()` for them

WordPress Trac noreply at wordpress.org
Tue Jun 13 19:01:09 UTC 2023


#58532: Improve performance and simplify usage of `block_has_support()` by
supporting a string feature and avoiding `_wp_array_get()` for them
-------------------------+-----------------------------
 Reporter:  flixos90     |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:
  Focuses:  performance  |
-------------------------+-----------------------------
 Follow up to #58376: As mentioned in
 https://core.trac.wordpress.org/ticket/58376#comment:20,
 `block_has_support()` is one of the most notable functions that perform a
 ton of calls to `_wp_array_get()`. While that function can be useful,
 specifically for `block_has_support()` it is very much overkill, since
 almost every feature is just a simple string, i.e. uses a single-dimension
 array.

 Additionally, passing an array for the `$feature` parameter is a bit
 confusing. Even the initial documentation accidentally defined a string,
 which suggests that was simply assumed to be correct as it is more
 intuitive (see #56307).

 I am proposing the following:
 * Add support for the `$feature` parameter of `block_has_support()` to be
 an array ''or'' string.
 * If a string is provided, don't use `_wp_array_get(
 $block_type->supports, $feature, $default_value )`, but simply `isset(
 $block_type->supports[ $feature ] ) ? $block_type->supports[ $feature ] :
 $default_value`.
 * Update all usages of `block_has_support()` that provide an array with
 just one item to just pass that item as a string.

 Of course we could also count the items in the array and use `isset()` if
 it is just 1. But the approach of providing a string brings a similar
 performance improvement and is a more intuitive API.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58532>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list