Uploads a file, following your settings.
Description
Adapted from a patch by Johann Richard.
Parameters
$argsarrayrequired- Method arguments. Note: top-level arguments must be ordered as documented.
0intBlog ID (unused).1stringUsername.2stringPassword.3arrayData for the file to upload.namestringFile name. Sanitized with sanitize_file_name() .typestringOptional. File MIME type, stored as the attachment’s post MIME type. Default empty string.bitsstringOptional. File contents. Default empty string.post_idintOptional. ID of the post to attach the file to.
Default 0.
Source
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']; 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'] ) ); /** * Fires after a new attachment has been added via the XML-RPC MovableType API. * * @since 3.4.0 * * @param int $attachment_id ID of the new attachment. * @param array $args An array of arguments to add the attachment. */ do_action( 'xmlrpc_call_success_mw_newMediaObject', $attachment_id, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase $struct = $this->_prepare_media_item( get_post( $attachment_id ) ); // Deprecated values. $struct['id'] = $struct['attachment_id']; $struct['file'] = $struct['title']; $struct['url'] = $struct['link']; return $struct; }Hooks
- apply_filters( ‘pre_upload_error’,
bool $error ) Filters whether to preempt the XML-RPC media upload.
- do_action( ‘xmlrpc_call’,
string $name ,array|string $args ,wp_xmlrpc_server $server ) Fires after the XML-RPC user has been authenticated but before the rest of the method logic begins.
- do_action( ‘xmlrpc_call_success_mw_newMediaObject’,
int $attachment_id ,array $args ) Fires after a new attachment has been added via the XML-RPC MovableType API.
Changelog
Version Description 1.5.0 Introduced.
User Contributed Notes
You must log in before being able to contribute a note or feedback.