WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

wp_xmlrpc_server::mw_newMediaObject() WP 1.5.0

Uploads a file, following your settings.

Adapted from a patch by Johann Richard.

Это метод класса: wp_xmlrpc_server

Возвращает

Массив/IXR_Error.

Использование

$wp_xmlrpc_server = new wp_xmlrpc_server();
$var = $wp_xmlrpc_server->mw_newMediaObject( $args );
$args(массив) (обязательный)

Method arguments. Note: arguments must be ordered as documented.

  • $blog_id (число)
    (unused)

  • $username (строка)

  • $password (строка)

  • $data (массив)

Заметки

  • Global. wpdb. $wpdb WordPress database abstraction object.

Список изменений

С версии 1.5.0 Введена.

Код wp xmlrpc server::mw newMediaObject: wp-includes/class-wp-xmlrpc-server.php WP 5.2.3

<?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,
			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.
	 *
	 * Passing 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.
	 */
	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 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;
}