wp_xmlrpc_server::mw_newMediaObject( array $args ): array|IXR_Error
- Since
- 1.5.0
- Source
wp-includes/class-wp-xmlrpc-server.php:6446
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: arguments must be ordered as documented.
$0intBlog ID (unused).$1stringUsername.$2stringPassword.$3arrayData.
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
- 30–118
- 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 181.
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 · 13 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 |
|---|---|---|
| always | 30 | ->escape(), ->escape(), sanitize_file_name(), ->login() |
!current_user_can() | 50 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), __() |
current_user_can() && !is_multisite() | 53 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), is_multisite(), apply_filters() |
current_user_can() && is_multisite() && !upload_is_user_over_quota() | 57 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), is_multisite(), upload_is_user_over_quota(), apply_filters() |
current_user_can() && is_multisite() && upload_is_user_over_quota() | 68 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), is_multisite(), upload_is_user_over_quota(), __(), get_space_allowed(), size_format(), sprintf() |
current_user_can() && !is_multisite() && !empty($upload) | 71 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), is_multisite(), apply_filters(), wp_upload_bits(), __(), sprintf() |
!is_multisite() && empty($upload) && !empty($data) | 74 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), is_multisite(), apply_filters(), wp_upload_bits(), current_user_can(), __() |
current_user_can() && is_multisite() && !upload_is_user_over_quota() && !empty($upload) | 75 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), is_multisite(), upload_is_user_over_quota(), apply_filters(), wp_upload_bits(), __(), sprintf() |
is_multisite() && !upload_is_user_over_quota() && empty($upload) && !empty($data) | 78 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), is_multisite(), upload_is_user_over_quota(), apply_filters(), wp_upload_bits(), current_user_can(), __() |
current_user_can() && !is_multisite() && empty($upload) && empty($data) | 107 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), 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() |
current_user_can() && is_multisite() && !upload_is_user_over_quota() && empty($upload) && empty($data) | 111 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), 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() |
current_user_can() && !is_multisite() && empty($upload) && !empty($data) | 114 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), 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() |
1 further outcome, up to 118 instructions
current_user_can() && is_multisite() && !upload_is_user_over_quota() && empty($upload) && !empty($data) | 118 | ->escape(), ->escape(), sanitize_file_name(), ->login(), do_action(), current_user_can(), 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: 181 instructions, 30–118 executed per call, 8 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 6461 (+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 6490 (+44 into the body)
Filters whether to preempt the XML-RPC media upload.
- do_action( xmlrpc_call_success_mw_newMediaObject )actionline 6533 (+87 into the body)
Fires after a new attachment has been added via the XML-RPC MovableType API.
Uses · 18
- sanitize_file_name()Sanitizes a filename, replacing whitespace with dashes.
- 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.
- 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 18
- 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::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 ) { $username = $this->escape( $args[1] ); $password = $this->escape( $args[2] ); $data = $args[3]; $name = sanitize_file_name( $data['name'] ); $type = $data['type']; $bits = $data['bits']; $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_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']; if ( ! current_user_can( 'edit_post', $post_id ) ) { return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) ); } } $attachment = array( 'post_title' => $name, 'post_content' => '', 'post_type' => 'attachment', 'post_parent' => $post_id, 'post_mime_type' => $type, 'guid' => $upload['url'], ); // Save the data. $attachment_id = wp_insert_attachment( $attachment, $upload['file'], $post_id ); wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) ); /**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 6.9.7 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.