WP_REST_Widgets_Controller::prepare_item_for_response( array $item, WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 5.8.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php:671
Compatibility
- WordPress
- since 5.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$itemarray- An array containing a widget_id and sidebar_id.
$requestWP_REST_Request- Request object.
Return value
WP_REST_Response|WP_Error- Response object on success, or WP_Error object on failure.
Performance profile
How much work a call to WP_REST_Widgets_Controller::prepare_item_for_response() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 17–146
- Plugin surface
- 1 hook
- Called by
- 5
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 167.
Third-party callbacks on 'rest_prepare_widget' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - serializeserialisation
serialize()called directly
Further down the call graph this can also reach option, cache, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 50 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Widgets_Controller::prepare_item_for_response() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($wp_registered_widgets[$widget_id]) | 17 | __() |
isset($wp_registered_widgets[$widget_id]) && ->is_method() | 23 | ->is_method(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && !rest_is_field_included() | 79–82 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 80–83 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 85–91 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 85–88 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 86–91 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 86–92 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 87–92 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 88–89 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 89–90 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 91–97 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
38 further outcomes, up to 146 instructions
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 92–100 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 92–97 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && rest_is_field_included() | 93–101 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 94–98 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 94–95 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 95–98 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 95–99 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 96–99 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() | 98–106 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 100–104 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 101–107 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 101–104 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && rest_is_field_included() && $sidebar_id !== "wp_inactive_widgets" | 102–108 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" | 107–113 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) | 112–121 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) | 113–122 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) | 118–130 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) | 118–127 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && rest_is_field_included() && isset($parsed_id) | 119–131 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) && !empty($value) && empty($instance) | 121–124 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) | 121–128 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) && !empty($value) && empty($instance) | 122–125 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) | 122–129 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) | 124–136 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) && !empty($value) && empty($instance) | 127–133 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) | 127–137 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) && !empty($value) && empty($instance) | 127–130 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) | 127–134 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && rest_is_field_included() && isset($parsed_id) && !empty($value) && empty($instance) | 128–134 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && rest_is_field_included() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) | 128–138 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) && !empty($value) && empty($instance) | 130–131 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) && !empty($value) && empty($instance) | 131–132 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && isset($parsed_id) && !empty($value) && empty($instance) | 133–139 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) | 133–143 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) && !empty($value) && empty($instance) | 136–140 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) && !empty($value) && empty($instance) | 136–137 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && rest_is_field_included() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) && !empty($value) && empty($instance) | 137–141 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
isset($wp_registered_widgets[$widget_id]) && !->is_method() && $sidebar_id !== "wp_inactive_widgets" && isset($parsed_id) && !empty($value) && empty($instance) | 142–146 | ->is_method(), wp_parse_widget_id(), ->get_fields_for_response(), rest_is_field_included(), wp_render_widget(), rest_is_field_included(), wp_render_widget_control(), rest_is_field_included(), ->get_widget_object(), ->get_settings(), serialize(), base64_encode(), wp_hash(), id(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 167 | 17–146 | 14 | |
| 8.5 | 167 | 17–146 | 14 | |
| 8.4 | 167 | 17–146 | 14 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 171 | 17–150 | 14 | |
| 8.2 | 171 | 17–150 | 14 | |
| 8.1 | 171 | 17–150 | 14 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 172 | 17–151 | 14 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while WP_REST_Widgets_Controller::prepare_item_for_response() runs, in this order:
- apply_filters( rest_prepare_widget )filterline 689 (+18 into the body)
Filters the REST API response for a widget.
- apply_filters( rest_prepare_widget )filterline 753 (+82 into the body)
Filters the REST API response for a widget.
Uses · 15
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_parse_widget_id()Converts a widget ID into its id_base and number components.
- rest_is_field_included()Given an array of fields to include in a response, some of which may be `nested.fields`, determine whether the provided field should be included in the response body.
- wp_render_widget()Calls the render callback of a widget and returns the output.
- wp_render_widget_control()Calls the control callback of a widget and returns the output.
- wp_hash()Gets the hash of the given string.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- WP_Error::__construct()Initializes the error.
- WP_REST_Response::__construct()
- WP_REST_Widgets_Controller::get_fields_for_response()
- stdClass::__construct()
Show all 15
- WP_REST_Widgets_Controller::add_additional_fields_to_object()
- WP_REST_Widgets_Controller::filter_response_by_context()
- WP_REST_Widgets_Controller::prepare_links()Prepares links for the widget.
Used by · 5
- WP_REST_Widgets_Controller::create_item()Creates a widget.
- WP_REST_Widgets_Controller::delete_item()Deletes a widget.
- WP_REST_Widgets_Controller::get_item()Gets an individual widget.
- WP_REST_Widgets_Controller::get_items()Retrieves a collection of widgets.
- WP_REST_Widgets_Controller::update_item()Updates an existing widget.
Source code
public function prepare_item_for_response( $item, $request ) { global $wp_widget_factory, $wp_registered_widgets; $widget_id = $item['widget_id']; $sidebar_id = $item['sidebar_id']; if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) { return new WP_Error( 'rest_invalid_widget', __( 'The requested widget is invalid.' ), array( 'status' => 500 ) ); } $widget = $wp_registered_widgets[ $widget_id ]; // Don't prepare the response body for HEAD requests. if ( $request->is_method( 'HEAD' ) ) { /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php */ return apply_filters( 'rest_prepare_widget', new WP_REST_Response( array() ), $widget, $request ); } $parsed_id = wp_parse_widget_id( $widget_id ); $fields = $this->get_fields_for_response( $request ); $prepared = array( 'id' => $widget_id, 'id_base' => $parsed_id['id_base'], 'sidebar' => $sidebar_id, 'rendered' => '', 'rendered_form' => null, 'instance' => null, ); if ( rest_is_field_included( 'rendered', $fields ) && 'wp_inactive_widgets' !== $sidebar_id ) { $prepared['rendered'] = trim( wp_render_widget( $widget_id, $sidebar_id ) ); } if ( rest_is_field_included( 'rendered_form', $fields ) ) { $rendered_form = wp_render_widget_control( $widget_id ); if ( ! is_null( $rendered_form ) ) { $prepared['rendered_form'] = trim( $rendered_form ); } } if ( rest_is_field_included( 'instance', $fields ) ) { $widget_object = $wp_widget_factory->get_widget_object( $parsed_id['id_base'] ); if ( $widget_object && isset( $parsed_id['number'] ) ) { $all_instances = $widget_object->get_settings(); $instance = $all_instances[ $parsed_id['number'] ]; $serialized_instance = serialize( $instance ); $prepared['instance']['encoded'] = base64_encode( $serialized_instance ); $prepared['instance']['hash'] = wp_hash( $serialized_instance ); if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) { // Use new stdClass so that JSON result is {} and not []. $prepared['instance']['raw'] = empty( $instance ) ? new stdClass() : $instance; } } } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $prepared = $this->add_additional_fields_to_object( $prepared, $request ); $prepared = $this->filter_response_by_context( $prepared, $context ); $response = rest_ensure_response( $prepared ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { $response->add_links( $this->prepare_links( $prepared ) ); } /** * Filters the REST API response for a widget. * * @since 5.8.0 * * @param WP_REST_Response|WP_Error $response The response object, or WP_Error object on failure. * @param array $widget The registered widget data.Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.