WP_REST_Templates_Controller::prepare_item_for_database( WP_REST_Request $request ): stdClass|WP_Error
- Since
- 5.8.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php:575
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
$requestWP_REST_Request- Request object.
Return value
stdClass|WP_Error- Changes to pass to wp_update_post.
Performance profile
How much work a call to WP_REST_Templates_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
- Heavy
- Scaling
- Constant
- Instructions
- 49–107
- Plugin surface
- 1 hook
- Called by
- 2
Reaches the database via get_user_by().
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 209.
Third-party callbacks on 'rest_pre_insert_{$this->post_type}' 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 - optionoption read or write
get_option()one call below WP_REST_Templates_Controller::prepare_item_for_database() - querycontent query
get_user_by()one call below WP_REST_Templates_Controller::prepare_item_for_database()
Further down the call graph this can also reach 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 · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Templates_Controller::prepare_item_for_database() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$request && $template !== null && !isset($request) && empty($request) | 49–81 | apply_filters() |
!$request && $template !== null && !isset($request) && !empty($request) && $post_author === false | 57–89 | get_current_user_id(), apply_filters() |
!$request && $template !== null && !isset($request) && empty($request) | 60–84 | _filter_block_template_part_area(), apply_filters() |
!$request && $template !== null && empty($request) | 61–91 | is_wp_suggestion(), apply_filters() |
!$request && $template !== null && !isset($request) && !empty($request) && $post_author !== false | 61–93 | get_current_user_id(), get_userdata(), __(), wp_parse_args() |
!$request && $template !== null && !isset($request) && !empty($request) && $post_author !== false | 62–94 | get_current_user_id(), get_userdata(), apply_filters() |
!$request && $template !== null && !isset($request) && !empty($request) && $post_author === false | 68–92 | _filter_block_template_part_area(), get_current_user_id(), apply_filters() |
!$request && $template !== null && !empty($request) && $post_author === false | 69–99 | is_wp_suggestion(), get_current_user_id(), apply_filters() |
!$request && $template !== null && empty($request) | 72–94 | is_wp_suggestion(), _filter_block_template_part_area(), apply_filters() |
!$request && $template !== null && !isset($request) && !empty($request) && $post_author !== false | 72–96 | _filter_block_template_part_area(), get_current_user_id(), get_userdata(), __(), wp_parse_args() |
!$request && $template !== null && !isset($request) && !empty($request) && $post_author !== false | 73–97 | _filter_block_template_part_area(), get_current_user_id(), get_userdata(), apply_filters() |
!$request && $template !== null && !empty($request) && $post_author !== false | 73–103 | is_wp_suggestion(), get_current_user_id(), get_userdata(), __(), wp_parse_args() |
4 further outcomes, up to 107 instructions
!$request && $template !== null && !empty($request) && $post_author !== false | 74–104 | is_wp_suggestion(), get_current_user_id(), get_userdata(), apply_filters() |
!$request && $template !== null && !empty($request) && $post_author === false | 80–102 | is_wp_suggestion(), _filter_block_template_part_area(), get_current_user_id(), apply_filters() |
!$request && $template !== null && !empty($request) && $post_author !== false | 84–106 | is_wp_suggestion(), _filter_block_template_part_area(), get_current_user_id(), get_userdata(), __(), wp_parse_args() |
!$request && $template !== null && !empty($request) && $post_author !== false | 85–107 | is_wp_suggestion(), _filter_block_template_part_area(), get_current_user_id(), get_userdata(), 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 | 207 | 49–107 | 28 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 209 | 49–107 | 28 | |
| 8.4 | 209 | 49–107 | 28 | |
| 8.3 | 209 | 49–107 | 28 | |
| 8.2 | 209 | 49–107 | 28 | |
| 8.1 | 209 | 49–107 | 28 | |
| 7.4 | 209 | 49–107 | 28 |
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_Templates_Controller::prepare_item_for_database() runs, in this order:
- apply_filters( rest_pre_insert_{$this->post_type} )filterline 661 (+86 into the body)
Filters a post before it is inserted via the REST API.
Uses · 10
- get_block_template()Retrieves a single unified template object using its id.
- get_stylesheet()Retrieves name of the current stylesheet.
- wp_parse_args()Merges user defined arguments into defaults array.
- _filter_block_template_part_area()Checks whether the input 'area' is a supported value.
- get_current_user_id()Gets the current user's ID.
- get_userdata()Retrieves user info by user ID.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- stdClass::__construct()
- WP_Error::__construct()Initializes the error.
Used by · 2
- WP_REST_Templates_Controller::create_item()Creates a single template.
- WP_REST_Templates_Controller::update_item()Updates a single template.
Source code
protected function prepare_item_for_database( $request ) { $template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null; $changes = new stdClass(); if ( null === $template ) { $changes->post_type = $this->post_type; $changes->post_status = 'publish'; $changes->tax_input = array( 'wp_theme' => $request['theme'] ?? get_stylesheet(), ); } elseif ( 'custom' !== $template->source ) { $changes->post_name = $template->slug; $changes->post_type = $this->post_type; $changes->post_status = 'publish'; $changes->tax_input = array( 'wp_theme' => $template->theme, ); $changes->meta_input = array( 'origin' => $template->source, ); } else { $changes->post_name = $template->slug; $changes->ID = $template->wp_id; $changes->post_status = 'publish'; } if ( isset( $request['content'] ) ) { if ( is_string( $request['content'] ) ) { $changes->post_content = $request['content']; } elseif ( isset( $request['content']['raw'] ) ) { $changes->post_content = $request['content']['raw']; } } elseif ( null !== $template && 'custom' !== $template->source ) { $changes->post_content = $template->content; } if ( isset( $request['title'] ) ) { if ( is_string( $request['title'] ) ) { $changes->post_title = $request['title']; } elseif ( ! empty( $request['title']['raw'] ) ) { $changes->post_title = $request['title']['raw']; } } elseif ( null !== $template && 'custom' !== $template->source ) { $changes->post_title = $template->title; } if ( isset( $request['description'] ) ) { $changes->post_excerpt = $request['description']; } elseif ( null !== $template && 'custom' !== $template->source ) { $changes->post_excerpt = $template->description; } if ( 'wp_template' === $this->post_type && isset( $request['is_wp_suggestion'] ) ) { $changes->meta_input = wp_parse_args( array( 'is_wp_suggestion' => $request['is_wp_suggestion'], ), $changes->meta_input = array() ); } if ( 'wp_template_part' === $this->post_type ) { if ( isset( $request['area'] ) ) { $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $request['area'] ); } elseif ( null !== $template && 'custom' !== $template->source && $template->area ) { $changes->tax_input['wp_template_part_area'] = _filter_block_template_part_area( $template->area ); } elseif ( empty( $template->area ) ) { $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; } } if ( ! empty( $request['author'] ) ) { $post_author = (int) $request['author']; if ( get_current_user_id() !== $post_author ) { $user_obj = get_userdata( $post_author ); if ( ! $user_obj ) { return new WP_Error( 'rest_invalid_author', __( 'Invalid author ID.' ), array( 'status' => 400 ) ); }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 7.0.4 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-templates-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.