[wp-trac] [WordPress Trac] #62067: Remove Unnecessary array_flip Function in VALID_TOP_LEVEL_KEYS Filter

WordPress Trac noreply at wordpress.org
Wed Sep 18 04:33:21 UTC 2024


#62067: Remove Unnecessary array_flip Function in VALID_TOP_LEVEL_KEYS Filter
-------------------------+-----------------------------
 Reporter:  wpdebuglog   |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 The current implementation of the `array_intersect_key` function uses an
 `array_flip` on the `VALID_TOP_LEVEL_KEYS` array to filter input.

 This adds unnecessary overhead. We can optimize the process by converting
 `VALID_TOP_LEVEL_KEYS` into an associative array and passing it directly
 to `array_intersect_key`, eliminating the need for `array_flip`.

 Sample code:
 {{{#!php

 <?php
 function test(){
     $a = array(
         'blockTypes' => 0,
         'customTemplates' => 1,
         'description' => 2,
         'patterns' => 3,
         'settings' => 4,
         'slug' => 5,
         'styles' => 6,
         'templateParts' => 7,
         'title' => 8,
         'version' => 9,
     );
     return $a;
 }
 function flip(){
     $a = array(
                 'blockTypes',
                 'customTemplates',
                 'description',
                 'patterns',
                 'settings',
                 'slug',
                 'styles',
                 'templateParts',
                 'title',
                 'version',
         );
     return array_flip( $a );
 }

 test();
 flip();
 }}}


 [[Image(https://i.ibb.co/3RP2qp2/src5.png)]]


 File: `wp-includes/class-wp-theme-json.php`
 WP_Theme_JSON::sanitize is calling more than 50 times.

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


More information about the wp-trac mailing list