WP_REST_Comments_Controller::prepare_item_for_database( WP_REST_Request $request ): array|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:1397
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- Request object.
Return value
array|WP_Error- Prepared comment, otherwise WP_Error object.
Performance profile
How much work a call to WP_REST_Comments_Controller::prepare_item_for_database() 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
- 31–112
- Plugin surface
- 1 hook
- Called by
- 2
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 169.
Third-party callbacks on 'rest_preprocess_comment' run inside this call, and their cost is not bounded by anything here.
2 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
Further down the call graph this can also reach query, option, cache, serialize 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 · 49 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Comments_Controller::prepare_item_for_database() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($value) && !->exists() | 31–34 | ->exists(), __() |
!isset($request) && !isset($value) && empty($value) | 42–54 | apply_filters() |
!isset($request) && !isset($value) && empty($value) && empty($request) && !->get_header() | 42–54 | ->get_header(), apply_filters() |
!isset($value) | 45–58 | current_user_can(), apply_filters() |
!isset($value) && empty($request) && !->get_header() | 45–58 | current_user_can(), ->get_header(), apply_filters() |
!isset($request) && !isset($value) && empty($value) && empty($request) && ->get_header() | 47–59 | ->get_header(), ->get_header(), apply_filters() |
!isset($request) && !isset($value) && empty($value) && !empty($request) | 48–71 | rest_get_date_with_gmt(), apply_filters() |
!isset($request) && !isset($value) && empty($value) && !->get_header() | 48–71 | ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($request) && !isset($value) && !empty($value) | 49–64 | rest_is_ip_address(), apply_filters() |
!isset($request) && !isset($value) && !empty($value) && empty($request) && !->get_header() | 49–64 | rest_is_ip_address(), ->get_header(), apply_filters() |
!isset($value) && empty($request) && ->get_header() | 50–63 | current_user_can(), ->get_header(), ->get_header(), apply_filters() |
!isset($value) && !empty($request) | 51–75 | current_user_can(), rest_get_date_with_gmt(), apply_filters() |
37 further outcomes, up to 112 instructions
!isset($value) && !->get_header() | 51–75 | current_user_can(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && !current_user_can() && !empty($value) | 53–68 | current_user_can(), rest_is_ip_address(), apply_filters() |
!isset($request) && !isset($value) && empty($value) && ->get_header() | 53–76 | ->get_header(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && !current_user_can() && !empty($value) && empty($request) && !->get_header() | 53–68 | current_user_can(), rest_is_ip_address(), ->get_header(), apply_filters() |
!isset($request) && !isset($value) && !empty($value) && empty($request) && ->get_header() | 54–69 | rest_is_ip_address(), ->get_header(), ->get_header(), apply_filters() |
!isset($request) && !isset($value) && !empty($value) && !empty($request) | 55–81 | rest_is_ip_address(), rest_get_date_with_gmt(), apply_filters() |
!isset($request) && !isset($value) && !empty($value) && !->get_header() | 55–81 | rest_is_ip_address(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->get_header() | 56–80 | current_user_can(), ->get_header(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && !current_user_can() && !empty($value) && empty($request) && ->get_header() | 58–73 | current_user_can(), rest_is_ip_address(), ->get_header(), ->get_header(), apply_filters() |
!isset($value) && !current_user_can() && !empty($value) && !empty($request) | 59–85 | current_user_can(), rest_is_ip_address(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && !current_user_can() && !empty($value) && !->get_header() | 59–85 | current_user_can(), rest_is_ip_address(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($request) && !isset($value) && !empty($value) && ->get_header() | 60–86 | rest_is_ip_address(), ->get_header(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && empty($value) | 64–76 | ->exists(), apply_filters() |
!isset($value) && ->exists() && empty($value) && empty($request) && !->get_header() | 64–76 | ->exists(), ->get_header(), apply_filters() |
!isset($value) && !current_user_can() && !empty($value) && ->get_header() | 64–90 | current_user_can(), rest_is_ip_address(), ->get_header(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() | 67–80 | ->exists(), current_user_can(), apply_filters() |
!isset($value) && ->exists() && empty($request) && !->get_header() | 67–80 | ->exists(), current_user_can(), ->get_header(), apply_filters() |
!isset($value) && ->exists() && empty($value) && empty($request) && ->get_header() | 69–81 | ->exists(), ->get_header(), ->get_header(), apply_filters() |
!isset($value) && ->exists() && empty($value) && !empty($request) | 70–93 | ->exists(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && empty($value) && !->get_header() | 70–93 | ->exists(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !empty($value) | 71–86 | ->exists(), rest_is_ip_address(), apply_filters() |
!isset($value) && ->exists() && !empty($value) && empty($request) && !->get_header() | 71–86 | ->exists(), rest_is_ip_address(), ->get_header(), apply_filters() |
!isset($value) && ->exists() && empty($request) && ->get_header() | 72–85 | ->exists(), current_user_can(), ->get_header(), ->get_header(), apply_filters() |
!isset($value) && ->exists() && !empty($request) | 73–97 | ->exists(), current_user_can(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !->get_header() | 73–97 | ->exists(), current_user_can(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !current_user_can() && !empty($value) | 75–90 | ->exists(), current_user_can(), rest_is_ip_address(), apply_filters() |
!isset($value) && ->exists() && empty($value) && ->get_header() | 75–98 | ->exists(), ->get_header(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !current_user_can() && !empty($value) && empty($request) && !->get_header() | 75–90 | ->exists(), current_user_can(), rest_is_ip_address(), ->get_header(), apply_filters() |
!isset($value) && ->exists() && !empty($value) && empty($request) && ->get_header() | 76–91 | ->exists(), rest_is_ip_address(), ->get_header(), ->get_header(), apply_filters() |
!isset($value) && ->exists() && !empty($value) && !empty($request) | 77–103 | ->exists(), rest_is_ip_address(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !empty($value) && !->get_header() | 77–103 | ->exists(), rest_is_ip_address(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && ->get_header() | 78–102 | ->exists(), current_user_can(), ->get_header(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !current_user_can() && !empty($value) && empty($request) && ->get_header() | 80–95 | ->exists(), current_user_can(), rest_is_ip_address(), ->get_header(), ->get_header(), apply_filters() |
!isset($value) && ->exists() && !current_user_can() && !empty($value) && !empty($request) | 81–107 | ->exists(), current_user_can(), rest_is_ip_address(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !current_user_can() && !empty($value) && !->get_header() | 81–107 | ->exists(), current_user_can(), rest_is_ip_address(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !empty($value) && ->get_header() | 82–108 | ->exists(), rest_is_ip_address(), ->get_header(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
!isset($value) && ->exists() && !current_user_can() && !empty($value) && ->get_header() | 86–112 | ->exists(), current_user_can(), rest_is_ip_address(), ->get_header(), ->get_header(), rest_get_date_with_gmt(), apply_filters() |
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 | 169 | 31–112 | 21 | |
| 8.5 | 169 | 31–112 | 21 | |
| 8.4 | 169 | 31–112 | 21 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 173 | 31–112 | 21 | |
| 8.2 | 173 | 31–112 | 21 | |
| 8.1 | 173 | 31–112 | 21 | |
| 7.4 | 173 | 31–112 | 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::prepare_item_for_database() runs, in this order:
- apply_filters( rest_preprocess_comment )filterline 1485 (+88 into the body)
Filters a comment added via the REST API after it is prepared for insertion into the database.
Uses · 7
- __()Retrieves the translation of $text.
- current_user_can()Returns whether the current user has the specified capability.
- rest_is_ip_address()Determines if an IP address is valid.
- rest_get_date_with_gmt()Parses a date into both its local and UTC equivalent, in MySQL datetime format.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_User::__construct()Constructor.
- WP_Error::__construct()Initializes the error.
Used by · 2
- WP_REST_Comments_Controller::create_item()Creates a comment.
- WP_REST_Comments_Controller::update_item()Updates a comment.
Source code
protected function prepare_item_for_database( $request ) { $prepared_comment = array(); /* * Allow the comment_content to be set via the 'content' or * the 'content.raw' properties of the Request object. */ if ( isset( $request['content'] ) && is_string( $request['content'] ) ) { $prepared_comment['comment_content'] = trim( $request['content'] ); } elseif ( isset( $request['content']['raw'] ) && is_string( $request['content']['raw'] ) ) { $prepared_comment['comment_content'] = trim( $request['content']['raw'] ); } if ( isset( $request['post'] ) ) { $prepared_comment['comment_post_ID'] = (int) $request['post']; } if ( isset( $request['parent'] ) ) { $prepared_comment['comment_parent'] = $request['parent']; } if ( isset( $request['author'] ) ) { $user = new WP_User( $request['author'] ); if ( $user->exists() ) { $prepared_comment['user_id'] = $user->ID; $prepared_comment['comment_author'] = $user->display_name; $prepared_comment['comment_author_email'] = $user->user_email; $prepared_comment['comment_author_url'] = $user->user_url; } else { return new WP_Error( 'rest_comment_author_invalid', __( 'Invalid comment author ID.' ), array( 'status' => 400 ) ); } } if ( isset( $request['author_name'] ) ) { $prepared_comment['comment_author'] = $request['author_name']; } if ( isset( $request['author_email'] ) ) { $prepared_comment['comment_author_email'] = $request['author_email']; } if ( isset( $request['author_url'] ) ) { $prepared_comment['comment_author_url'] = $request['author_url']; } if ( isset( $request['author_ip'] ) && current_user_can( 'moderate_comments' ) ) { $prepared_comment['comment_author_IP'] = $request['author_ip']; } elseif ( ! empty( $_SERVER['REMOTE_ADDR'] ) && rest_is_ip_address( $_SERVER['REMOTE_ADDR'] ) ) { $prepared_comment['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; } else { $prepared_comment['comment_author_IP'] = '127.0.0.1'; } if ( ! empty( $request['author_user_agent'] ) ) { $prepared_comment['comment_agent'] = $request['author_user_agent']; } elseif ( $request->get_header( 'user_agent' ) ) { $prepared_comment['comment_agent'] = $request->get_header( 'user_agent' ); } if ( ! empty( $request['date'] ) ) { $date_data = rest_get_date_with_gmt( $request['date'] ); if ( ! empty( $date_data ) ) { list( $prepared_comment['comment_date'], $prepared_comment['comment_date_gmt'] ) = $date_data; } } elseif ( ! empty( $request['date_gmt'] ) ) { $date_data = rest_get_date_with_gmt( $request['date_gmt'], true ); if ( ! empty( $date_data ) ) { list( $prepared_comment['comment_date'], $prepared_comment['comment_date_gmt'] ) = $date_data; } } /** * Filters a comment added via the REST API after it is prepared for insertion into the database.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 7.1.0 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.