wp_xmlrpc_server::mw_newMediaObject( array $args ): array|IXR_Error
- Since
- 1.5.0
- Source
wp-includes/class-wp-xmlrpc-server.php:6461
Description
Adapted from a patch by Johann Richard.
Compatibility
- WordPress
- since 1.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
$argsarray- Method arguments. Note: top-level arguments must be ordered as documented.
$0intBlog ID (unused).$1stringUsername.$2stringPassword.$3array{ Data for the file to upload.$namestringFile name. Sanitized with sanitize_file_name().$typestringdefault: empty stringOptional. File MIME type, stored as the attachment's post MIME type.$bitsstringdefault: empty stringOptional. File contents.$post_idintdefault: 0. }Optional. ID of the post to attach the file to.
Return value
array|IXR_Error
Performance profile
How much work a call to wp_xmlrpc_server::mw_newMediaObject() 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
- 8–148
- Plugin surface
- 3 hooks
- Called by
- 0
Reaches the database via get_post().
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 229.
Third-party callbacks on 'xmlrpc_call', 'pre_upload_error', 'xmlrpc_call_success_mw_newMediaObject' 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
do_action()called directly - querycontent query
get_post()called directly - optionnetwork option
get_site_option()one call below wp_xmlrpc_server::mw_newMediaObject()
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 · 15 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_xmlrpc_server::mw_newMediaObject() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!->minimum_args() | 8 | ->minimum_args() |
->minimum_args() | 27 | ->minimum_args(), ->escape(), ->escape(), ->login() |
->minimum_args() | 46–61 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), __() |
->minimum_args() && current_user_can() && is_array($data) && $name === "" | 66–69 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), __() |
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && !is_multisite() | 78–83 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), apply_filters() |
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && is_multisite() && !upload_is_user_over_quota() | 82–87 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), upload_is_user_over_quota(), apply_filters() |
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && is_multisite() && upload_is_user_over_quota() | 93–98 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), upload_is_user_over_quota(), __(), get_space_allowed(), size_format(), sprintf() |
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && !is_multisite() && !empty($upload) | 96–101 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), apply_filters(), wp_upload_bits(), __(), sprintf() |
->minimum_args() && is_array($data) && $name !== "" && !is_multisite() && empty($upload) && !empty($data) | 99–104 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), apply_filters(), wp_upload_bits(), current_user_can(), __() |
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && is_multisite() && !upload_is_user_over_quota() && !empty($upload) | 100–105 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), upload_is_user_over_quota(), apply_filters(), wp_upload_bits(), __(), sprintf() |
->minimum_args() && is_array($data) && $name !== "" && is_multisite() && !upload_is_user_over_quota() && empty($upload) && !empty($data) | 103–108 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), upload_is_user_over_quota(), apply_filters(), wp_upload_bits(), current_user_can(), __() |
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && !is_multisite() && empty($upload) && empty($data) | 132–137 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), apply_filters(), wp_upload_bits(), wp_insert_attachment(), wp_generate_attachment_metadata(), wp_update_attachment_metadata(), do_action(), get_post(), ->_prepare_media_item() |
3 further outcomes, up to 148 instructions
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && is_multisite() && !upload_is_user_over_quota() && empty($upload) && empty($data) | 136–141 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), upload_is_user_over_quota(), apply_filters(), wp_upload_bits(), wp_insert_attachment(), wp_generate_attachment_metadata(), wp_update_attachment_metadata(), do_action(), get_post(), ->_prepare_media_item() |
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && !is_multisite() && empty($upload) && !empty($data) | 139–144 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), apply_filters(), wp_upload_bits(), current_user_can(), wp_insert_attachment(), wp_generate_attachment_metadata(), wp_update_attachment_metadata(), do_action(), get_post(), ->_prepare_media_item() |
->minimum_args() && current_user_can() && is_array($data) && $name !== "" && is_multisite() && !upload_is_user_over_quota() && empty($upload) && !empty($data) | 143–148 | ->minimum_args(), ->escape(), ->escape(), ->login(), do_action(), current_user_can(), sanitize_file_name(), is_multisite(), upload_is_user_over_quota(), apply_filters(), wp_upload_bits(), current_user_can(), wp_insert_attachment(), wp_generate_attachment_metadata(), wp_update_attachment_metadata(), do_action(), get_post(), ->_prepare_media_item() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 229 instructions, 8–148 executed per call, 19 branches. The work does not change between versions.
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 · 3
3 hooks fire while wp_xmlrpc_server::mw_newMediaObject() runs, in this order:
- do_action( xmlrpc_call )actionline 6476 (+15 into the body)
Fires after the XML-RPC user has been authenticated but before the rest of the method logic begins.
- apply_filters( pre_upload_error )filterline 6524 (+63 into the body)
Filters whether to preempt the XML-RPC media upload.
- do_action( xmlrpc_call_success_mw_newMediaObject )actionline 6567 (+106 into the body)
Fires after a new attachment has been added via the XML-RPC MovableType API.
Uses · 19
- do_action()Calls the callback functions that have been added to an action hook.
- current_user_can()Returns whether the current user has the specified capability.
- __()Retrieves the translation of $text.
- sanitize_file_name()Sanitizes a filename, replacing whitespace with dashes.
- is_multisite()Determines whether Multisite is enabled.
- upload_is_user_over_quota()Checks whether a site has used its allotted upload space.
- size_format()Converts a number of bytes to the largest unit the bytes will fit into.
- get_space_allowed()Returns the upload quota for the current blog.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_upload_bits()Creates a file in the upload folder with given content.
- wp_insert_attachment()Inserts an attachment.
- wp_update_attachment_metadata()Updates metadata for an attachment.
Show all 19
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- get_post()Retrieves post data given a post ID or post object.
- wp_xmlrpc_server::minimum_args()Checks if the method received at least the minimum number of arguments.
- wp_xmlrpc_server::escape()Escapes string or array of strings for database.
- wp_xmlrpc_server::login()Logs user in.
- IXR_Error::__construct()PHP5 constructor.
- wp_xmlrpc_server::_prepare_media_item()Prepares media item data for return in an XML-RPC object.
Source code
public function mw_newMediaObject( $args ) { if ( ! $this->minimum_args( $args, 4 ) ) { return $this->error; } $username = $this->escape( $args[1] ); $password = $this->escape( $args[2] ); $data = $args[3]; $user = $this->login( $username, $password ); if ( ! $user ) { return $this->error; } /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */ do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject', $args, $this ); if ( ! current_user_can( 'upload_files' ) ) { $this->error = new IXR_Error( 401, __( 'Sorry, you are not allowed to upload files.' ) ); return $this->error; } if ( ! is_array( $data ) || ! is_string( $data['name'] ?? null ) || ! is_string( $data['type'] ?? '' ) || ! is_string( $data['bits'] ?? '' ) ) { return new IXR_Error( 400, __( 'Invalid attachment data.' ) ); } $name = sanitize_file_name( $data['name'] ); // A name consisting only of characters the sanitizer strips leaves nothing to write to. if ( '' === $name ) { return new IXR_Error( 400, __( 'Invalid attachment data.' ) ); } $type = $data['type'] ?? ''; $bits = $data['bits'] ?? ''; if ( is_multisite() && upload_is_user_over_quota( false ) ) { $this->error = new IXR_Error( 401, sprintf( /* translators: %s: Allowed space allocation. */ __( 'Sorry, you have used your space allocation of %s. Please delete some files to upload more files.' ), size_format( get_space_allowed() * MB_IN_BYTES ) ) ); return $this->error; } /** * Filters whether to preempt the XML-RPC media upload. * * Returning a truthy value will effectively short-circuit the media upload, * returning that value as a 500 error instead. * * @since 2.1.0 * * @param bool $error Whether to pre-empt the media upload. Default false. */ $upload_err = apply_filters( 'pre_upload_error', false ); if ( $upload_err ) { return new IXR_Error( 500, $upload_err ); } $upload = wp_upload_bits( $name, null, $bits ); if ( ! empty( $upload['error'] ) ) { /* translators: 1: File name, 2: Error message. */ $error_string = sprintf( __( 'Could not write file %1$s (%2$s).' ), $name, $upload['error'] ); return new IXR_Error( 500, $error_string ); } // Construct the attachment array. $post_id = 0; if ( ! empty( $data['post_id'] ) ) { $post_id = (int) $data['post_id'];Changelog
Introduced in 1.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/class-wp-xmlrpc-server.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.