WP_REST_Font_Faces_Controller::create_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 6.5.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php:325
Compatibility
- WordPress
- since 6.5.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 WP_Error object on failure.
Performance profile
How much work a call to WP_REST_Font_Faces_Controller::create_item() 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
- Light
- Scaling
- Scales with input
- Instructions
- 12–78
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
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 125.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below WP_REST_Font_Faces_Controller::create_item()
Further down the call graph this can also reach serialize, cache, query, option 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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Font_Faces_Controller::create_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 12 | ->get_parent_font_family_post(), is_wp_error() |
!is_wp_error() && !empty($query) | 42 | ->get_parent_font_family_post(), is_wp_error(), ->get_param(), ->get_file_params(), ::WP_Font_Utils(), post_type(), __() |
empty($query) && !$srcs && isset($file_params[$src]) | 60–65 | ->get_parent_font_family_post(), is_wp_error(), ->get_param(), ->get_file_params(), ::WP_Font_Utils(), post_type(), function_exists(), ->handle_font_file_upload(), is_wp_error() |
!is_wp_error() && empty($query) | 65–78 | ->get_parent_font_family_post(), is_wp_error(), ->get_param(), ->get_file_params(), ::WP_Font_Utils(), post_type(), function_exists(), ->set_param(), ::create_item(), is_wp_error() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 125 | 12–78 | 12 | |
| 8.5 | 125 | 12–78 | 12 | |
| 8.4 | 125 | 12–78 | 12 | |
| 8.3 | 125 | 12–78 | 12 | |
| 8.2 | 125 | 12–78 | 12 | |
| 8.1 | 125 | 12–78 | 12 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 128 | 12–80 | 12 |
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.
Uses · 10
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- add_post_meta()Adds a meta field to the given post.
- WP_REST_Font_Faces_Controller::get_parent_font_family_post()Get the parent font family, if the ID is valid.
- WP_Query::__construct()Constructor.
- WP_Font_Utils::get_font_face_slug()Generates a slug from font face properties, e.g. `open sans;normal;400;100%;U+0-10FFFF`
- WP_Error::__construct()Initializes the error.
- WP_REST_Font_Faces_Controller::handle_font_file_upload()Handles the upload of a font file using wp_handle_upload().
- WP_REST_Font_Faces_Controller::relative_fonts_path()Returns relative path to an uploaded font file.
- WP_REST_Posts_Controller::create_item()Creates a single post.
Source code
public function create_item( $request ) { $font_family = $this->get_parent_font_family_post( $request['font_family_id'] ); if ( is_wp_error( $font_family ) ) { return $font_family; } // Settings have already been decoded by ::sanitize_font_face_settings(). $settings = $request->get_param( 'font_face_settings' ); $file_params = $request->get_file_params(); // Check that the necessary font face properties are unique. $query = new WP_Query( array( 'post_type' => $this->post_type, 'posts_per_page' => 1, 'title' => WP_Font_Utils::get_font_face_slug( $settings ), 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ) ); if ( ! empty( $query->posts ) ) { return new WP_Error( 'rest_duplicate_font_face', __( 'A font face matching those settings already exists.' ), array( 'status' => 400 ) ); } // Move the uploaded font asset from the temp folder to the fonts directory. if ( ! function_exists( 'wp_handle_upload' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $srcs = is_string( $settings['src'] ) ? array( $settings['src'] ) : $settings['src']; $processed_srcs = array(); $font_file_meta = array(); foreach ( $srcs as $src ) { // If src not a file reference, use it as is. if ( ! isset( $file_params[ $src ] ) ) { $processed_srcs[] = $src; continue; } $file = $file_params[ $src ]; $font_file = $this->handle_font_file_upload( $file ); if ( is_wp_error( $font_file ) ) { return $font_file; } $processed_srcs[] = $font_file['url']; $font_file_meta[] = $this->relative_fonts_path( $font_file['file'] ); } // Store the updated settings for prepare_item_for_database to use. $settings['src'] = count( $processed_srcs ) === 1 ? $processed_srcs[0] : $processed_srcs; $request->set_param( 'font_face_settings', $settings ); // Ensure that $settings data is slashed, so values with quotes are escaped. // WP_REST_Posts_Controller::create_item uses wp_slash() on the post_content. $font_face_post = parent::create_item( $request ); if ( is_wp_error( $font_face_post ) ) { return $font_face_post; } $font_face_id = $font_face_post->data['id']; foreach ( $font_file_meta as $font_file_path ) { add_post_meta( $font_face_id, '_wp_font_face_file', $font_file_path ); } return $font_face_post; }Changelog
Introduced in 6.5.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-font-faces-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.