WP_REST_Attachments_Controller::create_item_permissions_check( WP_REST_Request $request ): true|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:126
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
true|WP_Error- Boolean true if the attachment may be created, or a WP_Error if not.
Performance profile
How much work a call to WP_REST_Attachments_Controller::create_item_permissions_check() 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
- 7–64
- Plugin surface
- 1 hook
- Called by
- 0
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 87.
Third-party callbacks on 'wp_prevent_unsupported_mime_type_uploads' 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
Further down the call graph this can also reach cache, query, option, 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 · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Attachments_Controller::create_item_permissions_check() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7 | ::create_item_permissions_check() |
is_wp_error() | 11 | ::create_item_permissions_check(), is_wp_error() |
!is_wp_error() && !current_user_can() | 23 | ::create_item_permissions_check(), is_wp_error(), current_user_can(), __() |
!is_wp_error() && current_user_can() && empty($request) | 30–42 | ::create_item_permissions_check(), is_wp_error(), current_user_can(), ->get_file_params(), apply_filters() |
!is_wp_error() && !empty($request) | 35 | ::create_item_permissions_check(), is_wp_error(), current_user_can(), current_user_can(), __(), rest_authorization_required_code(), status() |
!is_wp_error() && current_user_can() && !empty($request) | 37–49 | ::create_item_permissions_check(), is_wp_error(), current_user_can(), current_user_can(), ->get_file_params(), apply_filters() |
!is_wp_error() && current_user_can() && empty($request) && isset($value) && mime_type() | 48–49 | ::create_item_permissions_check(), is_wp_error(), current_user_can(), ->get_file_params(), apply_filters(), mime_type() |
!is_wp_error() && current_user_can() && !empty($request) && isset($value) && mime_type() | 55–56 | ::create_item_permissions_check(), is_wp_error(), current_user_can(), current_user_can(), ->get_file_params(), apply_filters(), mime_type() |
!is_wp_error() && current_user_can() && empty($request) && isset($value) && !mime_type() | 56–57 | ::create_item_permissions_check(), is_wp_error(), current_user_can(), ->get_file_params(), apply_filters(), mime_type(), __(), wp_image_editor_supports() |
!is_wp_error() && current_user_can() && !empty($request) && isset($value) && !mime_type() | 63–64 | ::create_item_permissions_check(), is_wp_error(), current_user_can(), current_user_can(), ->get_file_params(), apply_filters(), mime_type(), __(), wp_image_editor_supports() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 87 | 7–64 | 11 | |
| 8.5 | 87 | 7–64 | 11 | |
| 8.4 | 87 | 7–64 | 11 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 90 | 7–67 | 11 | |
| 8.2 | 90 | 7–67 | 11 | |
| 8.1 | 90 | 7–67 | 11 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 91 | 7–68 | 11 |
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_Attachments_Controller::create_item_permissions_check() runs, in this order:
- apply_filters( wp_prevent_unsupported_mime_type_uploads )filterline 162 (+36 into the body)
Filter whether the server should prevent uploads for image types it doesn't support. Default true.
Uses · 9
- is_wp_error()Checks whether the given variable is a WordPress Error.
- current_user_can()Returns whether the current user has the specified capability.
- __()Retrieves the translation of $text.
- rest_authorization_required_code()Returns a contextual HTTP error code for authorization failure.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_image_editor_supports()Tests whether there is an editor that supports a given mime type or methods.
- WP_REST_Posts_Controller::create_item_permissions_check()Checks if a given request has access to create a post.
- WP_Error::__construct()Initializes the error.
Source code
public function create_item_permissions_check( $request ) { $ret = parent::create_item_permissions_check( $request ); if ( ! $ret || is_wp_error( $ret ) ) { return $ret; } if ( ! current_user_can( 'upload_files' ) ) { return new WP_Error( 'rest_cannot_create', __( 'Sorry, you are not allowed to upload media on this site.' ), array( 'status' => 400 ) ); } // Attaching media to a post requires ability to edit said post. if ( ! empty( $request['post'] ) && ! current_user_can( 'edit_post', (int) $request['post'] ) ) { return new WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to upload media to this post.' ), array( 'status' => rest_authorization_required_code() ) ); } $files = $request->get_file_params(); /** * Filter whether the server should prevent uploads for image types it doesn't support. Default true. * * Developers can use this filter to enable uploads of certain image types. By default image types that are not * supported by the server are prevented from being uploaded. * * @since 6.8.0 * * @param bool $check_mime Whether to prevent uploads of unsupported image types. * @param string|null $mime_type The mime type of the file being uploaded (if available). */ $prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, $files['file']['type'] ?? null ); // If the upload is an image, check if the server can handle the mime type. if ( $prevent_unsupported_uploads && isset( $files['file']['type'] ) && str_starts_with( $files['file']['type'], 'image/' ) ) { // List of non-resizable image formats. $editor_non_resizable_formats = array( 'image/svg+xml', ); // Check if the image editor supports the type or ignore if it isn't a format resizable by an editor. if ( ! in_array( $files['file']['type'], $editor_non_resizable_formats, true ) && ! wp_image_editor_supports( array( 'mime_type' => $files['file']['type'] ) ) ) { return new WP_Error( 'rest_upload_image_type_not_supported', __( 'The web server cannot generate responsive image sizes for this image. Convert it to JPEG or PNG before uploading.' ), array( 'status' => 400 ) ); } } return true; }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.0.4 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-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.