wp_xmlrpc_server::mw_newMediaObject( array $args )

Uploads a file, following your settings.


Description

Adapted from a patch by Johann Richard.


Parameters

$args

(array) (Required) Method arguments. Note: arguments must be ordered as documented.

  • 'blog_id'
    (int) (unused)
  • 'username'
    (string)
  • 'password'
    (string)
  • 'data'
    (array)


Return

(array|IXR_Error)


Source

File: wp-includes/class-wp-xmlrpc-server.php

	public function mw_newMediaObject( $args ) {
		global $wpdb;

		$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'];

		if ( !$user = $this->login($username, $password) )
			return $this->error;

		/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
		do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject' );

		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, __( 'Sorry, you have used your space allocation.' ) );
			return $this->error;
		}

		/**
		 * Filters whether to preempt the XML-RPC media upload.
		 *
		 * Passing a truthy value will effectively short-circuit the media upload,
		 * returning that value as a 500 error instead.
		 *
		 * @since WP-2.1.0
		 *
		 * @param bool $error Whether to pre-empt the media upload. Default false.
		 */
		if ( $upload_err = apply_filters( 'pre_upload_error', false ) ) {
			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 */
			$errorString = sprintf( __( 'Could not write file %1$s (%2$s).' ), $name, $upload['error'] );
			return new IXR_Error( 500, $errorString );
		}
		// 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
		$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );

		/**
		 * Fires after a new attachment has been added via the XML-RPC MovableType API.
		 *
		 * @since WP-3.4.0
		 *
		 * @param int   $id   ID of the new attachment.
		 * @param array $args An array of arguments to add the attachment.
		 */
		do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );

		$struct = $this->_prepare_media_item( get_post( $id ) );

		// Deprecated values
		$struct['id']   = $struct['attachment_id'];
		$struct['file'] = $struct['title'];
		$struct['url']  = $struct['link'];

		return $struct;
	}


Changelog

Changelog
Version Description
WP-1.5.0 Introduced.