WP_REST_Comments_Controller::get_items( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:194
Compatibility
- WordPress
- since 4.7.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
$requestWP_REST_Request- Full details about the request.
Return value
WP_REST_Response|WP_Error- Response object on success, or error object on failure.
Performance profile
How much work a call to WP_REST_Comments_Controller::get_items() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 82–163
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via ->query().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 194.
Third-party callbacks on 'rest_comment_query' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - sqldatabase query
->query()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 24 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Comments_Controller::get_items() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($registered) && !$total_comments && !$max_pages | 82–100 | ->get_collection_params(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg() |
isset($registered) && !$total_comments && !$max_pages | 89–107 | ->get_collection_params(), ->normalize_query_param(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg() |
empty($request) && !$total_comments && !$max_pages | 94–110 | ->get_collection_params(), absint(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg() |
!isset($registered) && !$total_comments | 94–115 | ->get_collection_params(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header() |
!isset($registered) && $total_comments && !$max_pages | 101–119 | ->get_collection_params(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg() |
isset($registered) && empty($request) && !$total_comments && !$max_pages | 101–117 | ->get_collection_params(), ->normalize_query_param(), absint(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg() |
isset($registered) && !$total_comments | 101–122 | ->get_collection_params(), ->normalize_query_param(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header() |
empty($request) && !$total_comments | 106–125 | ->get_collection_params(), absint(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header() |
isset($registered) && $total_comments && !$max_pages | 108–126 | ->get_collection_params(), ->normalize_query_param(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg() |
!isset($registered) && !$total_comments && $max_pages | 108–127 | ->get_collection_params(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header() |
empty($request) && $total_comments && !$max_pages | 113–129 | ->get_collection_params(), absint(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg() |
!isset($registered) && $total_comments | 113–134 | ->get_collection_params(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header() |
12 further outcomes, up to 163 instructions
isset($registered) && empty($request) && !$total_comments | 113–132 | ->get_collection_params(), ->normalize_query_param(), absint(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header() |
isset($registered) && !$total_comments && $max_pages | 115–134 | ->get_collection_params(), ->normalize_query_param(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header() |
isset($registered) && empty($request) && $total_comments && !$max_pages | 120–136 | ->get_collection_params(), ->normalize_query_param(), absint(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg() |
empty($request) && !$total_comments && $max_pages | 120–137 | ->get_collection_params(), absint(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header() |
isset($registered) && $total_comments | 120–141 | ->get_collection_params(), ->normalize_query_param(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header() |
empty($request) && $total_comments | 125–144 | ->get_collection_params(), absint(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header() |
!isset($registered) && $total_comments && $max_pages | 127–146 | ->get_collection_params(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header() |
isset($registered) && empty($request) && !$total_comments && $max_pages | 127–144 | ->get_collection_params(), ->normalize_query_param(), absint(), apply_filters(), ->query(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header() |
isset($registered) && empty($request) && $total_comments | 132–151 | ->get_collection_params(), ->normalize_query_param(), absint(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header() |
isset($registered) && $total_comments && $max_pages | 134–153 | ->get_collection_params(), ->normalize_query_param(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header() |
empty($request) && $total_comments && $max_pages | 139–156 | ->get_collection_params(), absint(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header() |
isset($registered) && empty($request) && $total_comments && $max_pages | 146–163 | ->get_collection_params(), ->normalize_query_param(), absint(), apply_filters(), ->query(), ->query(), ceil(), rest_ensure_response(), ->header(), ->header(), ->get_query_params(), urlencode_deep(), rest_url(), add_query_arg(), add_query_arg(), ->link_header(), add_query_arg(), ->link_header() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 194 | 82–163 | 21 | |
| 8.5 | 194 | 82–163 | 21 | |
| 8.4 | 194 | 82–163 | 21 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 196 | 84–165 | 21 | |
| 8.2 | 196 | 84–165 | 21 | |
| 8.1 | 196 | 84–165 | 21 | |
| 7.4 | 196 | 84–165 | 21 |
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 · 1
One hook fires while WP_REST_Comments_Controller::get_items() runs, in this order:
- apply_filters( rest_comment_query )filterline 275 (+81 into the body)
Filters WP_Comment_Query arguments when querying comments via the REST API.
Uses · 12
- absint()Converts a value to non-negative integer.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- add_query_arg()Retrieves a modified URL query string.
- urlencode_deep()Navigates through an array, object, or scalar, and encodes the values to be used in a URL.
- rest_url()Retrieves the URL to a REST endpoint.
- WP_REST_Comments_Controller::get_collection_params()Retrieves the query params for collections.
- WP_REST_Comments_Controller::normalize_query_param()Prepends internal property prefix to query parameters to match our response fields.
- WP_Comment_Query::__construct()Constructor.
- WP_REST_Comments_Controller::check_read_permission()Checks if the comment can be read.
- WP_REST_Comments_Controller::prepare_item_for_response()Prepares a single comment output for response.
- WP_REST_Comments_Controller::prepare_response_for_collection()
Source code
public function get_items( $request ) { // Retrieve the list of registered collection query parameters. $registered = $this->get_collection_params(); /* * This array defines mappings between public API query parameters whose * values are accepted as-passed, and their internal WP_Query parameter * name equivalents (some are the same). Only values which are also * present in $registered will be set. */ $parameter_mappings = array( 'author' => 'author__in', 'author_email' => 'author_email', 'author_exclude' => 'author__not_in', 'exclude' => 'comment__not_in', 'include' => 'comment__in', 'offset' => 'offset', 'order' => 'order', 'parent' => 'parent__in', 'parent_exclude' => 'parent__not_in', 'per_page' => 'number', 'post' => 'post__in', 'search' => 'search', 'status' => 'status', 'type' => 'type', ); $prepared_args = array(); /* * For each known parameter which is both registered and present in the request, * set the parameter's value on the query $prepared_args. */ foreach ( $parameter_mappings as $api_param => $wp_param ) { if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { $prepared_args[ $wp_param ] = $request[ $api_param ]; } } // Ensure certain parameter values default to empty strings. foreach ( array( 'author_email', 'search' ) as $param ) { if ( ! isset( $prepared_args[ $param ] ) ) { $prepared_args[ $param ] = ''; } } if ( isset( $registered['orderby'] ) ) { $prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] ); } $prepared_args['no_found_rows'] = false; $prepared_args['update_comment_post_cache'] = true; $prepared_args['date_query'] = array(); // Set before into date query. Date query must be specified as an array of an array. if ( isset( $registered['before'], $request['before'] ) ) { $prepared_args['date_query'][0]['before'] = $request['before']; } // Set after into date query. Date query must be specified as an array of an array. if ( isset( $registered['after'], $request['after'] ) ) { $prepared_args['date_query'][0]['after'] = $request['after']; } if ( isset( $registered['page'] ) && empty( $request['offset'] ) ) { $prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 ); } /** * Filters WP_Comment_Query arguments when querying comments via the REST API. * * @since 4.7.0 * * @link https://developer.wordpress.org/reference/classes/wp_comment_query/ * * @param array $prepared_args Array of arguments for WP_Comment_Query. * @param WP_REST_Request $request The REST API request.Changelog
Introduced in 4.7.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.7.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-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.