[wp-trac] [WordPress Trac] #39473: get_routes() called multiple times within single REST request causing the rest_endpoints() filter to also fire more than once

WordPress Trac noreply at wordpress.org
Wed Jan 4 21:59:39 UTC 2017


#39473: get_routes() called multiple times within single REST request causing the
rest_endpoints() filter to also fire more than once
-------------------------+-----------------------------
 Reporter:  ruud@…       |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  4.4
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 Hi all,

 Many thanks for creating the REST API, and also for getting it into core!
 :)

 When I had a closer look at how to integrate this in our projects I
 noticed something peculiar with the rest_endpoints() filter: it is called
 multiple times over; in some cases twice, in others three times.

 So I did a little digging around and found that the root cause seemed to
 be the use of get_routes() at multiple locations:
 - in the rest_pre_dispatch filter (rest_handle_options_request)
 - in the rest_post_dispatch filter (rest_send_allow_header)
 - in the dispatch() itself
 - in the get_index() method
 - in the get_namespace_index() method

 After looking how these locations interact with each other, I couldn't
 detect any code which altered the generated route map between consecutive
 calls to get_routes().

 I will add a patch in which I propose to store the generated route map in
 the class, and re-use that one instead of generating yet again the same
 array (and also re-filtering the same array).

 Since the name 'endpoints' is already taken, and being used in the
 initialization as well, I thought it would be prune to use another
 variable name: $route_map, which is also being used in the current doc-
 block.

 I did not profile this patch (not really sure how to do that), so I'm not
 sure if storing this rather large associative array is a good thing to do.
 However generating it multiple times (and re-filtering it also) may also
 be quite 'expensive'.

 Thanks,
 Ruud

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


More information about the wp-trac mailing list