wppaste
WordPress

wp_xmlrpc_server::mw_newMediaObject( array $args ): array|IXR_Error

Since
1.5.0
Source
wp-includes/class-wp-xmlrpc-server.php:6446
Uploads a file, following your settings.

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.
  • $0int

    Blog ID (unused).
  • $1string

    Username.
  • $2string

    Password.
  • $3array

    Data.

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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
30–118

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 181.

Plugin surface
3 hooks

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.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • querycontent queryget_post()called directly
  • optionnetwork optionget_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.

WhenInstructionsCalls it makes
always30->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:

  1. 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.

  2. apply_filters( pre_upload_error )filterline 6490 (+44 into the body)

    Filters whether to preempt the XML-RPC media upload.

  3. 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

Show all 18

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.