wp_xmlrpc_server::wp_editTerm()publicWP 3.4.0

Edits a term.

Метод класса: wp_xmlrpc_server{}

Хуки из метода

Возвращает

true|IXR_Error. True on success, IXR_Error instance on failure.

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

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

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

  • 0(int)
    Blog ID (unused).

  • 1(строка)
    Username.

  • 2(строка)
    Password.

  • 3(int)
    Term ID.

  • 4(массив)
    Content struct for editing a term. The struct must contain the term 'taxonomy'. Optional accepted values include 'name', 'parent', 'description', and 'slug'.

Заметки

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

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

Код wp_xmlrpc_server::wp_editTerm() WP 6.6.2

public function wp_editTerm( $args ) {
	if ( ! $this->minimum_args( $args, 5 ) ) {
		return $this->error;
	}

	$this->escape( $args );

	$username       = $args[1];
	$password       = $args[2];
	$term_id        = (int) $args[3];
	$content_struct = $args[4];

	$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', 'wp.editTerm', $args, $this );

	if ( ! taxonomy_exists( $content_struct['taxonomy'] ) ) {
		return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
	}

	$taxonomy = get_taxonomy( $content_struct['taxonomy'] );

	$taxonomy = (array) $taxonomy;

	// Hold the data of the term.
	$term_data = array();

	$term = get_term( $term_id, $content_struct['taxonomy'] );

	if ( is_wp_error( $term ) ) {
		return new IXR_Error( 500, $term->get_error_message() );
	}

	if ( ! $term ) {
		return new IXR_Error( 404, __( 'Invalid term ID.' ) );
	}

	if ( ! current_user_can( 'edit_term', $term_id ) ) {
		return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this term.' ) );
	}

	if ( isset( $content_struct['name'] ) ) {
		$term_data['name'] = trim( $content_struct['name'] );

		if ( empty( $term_data['name'] ) ) {
			return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
		}
	}

	if ( ! empty( $content_struct['parent'] ) ) {
		if ( ! $taxonomy['hierarchical'] ) {
			return new IXR_Error( 403, __( 'Cannot set parent term, taxonomy is not hierarchical.' ) );
		}

		$parent_term_id = (int) $content_struct['parent'];
		$parent_term    = get_term( $parent_term_id, $taxonomy['name'] );

		if ( is_wp_error( $parent_term ) ) {
			return new IXR_Error( 500, $parent_term->get_error_message() );
		}

		if ( ! $parent_term ) {
			return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
		}

		$term_data['parent'] = $content_struct['parent'];
	}

	if ( isset( $content_struct['description'] ) ) {
		$term_data['description'] = $content_struct['description'];
	}

	if ( isset( $content_struct['slug'] ) ) {
		$term_data['slug'] = $content_struct['slug'];
	}

	$term = wp_update_term( $term_id, $taxonomy['name'], $term_data );

	if ( is_wp_error( $term ) ) {
		return new IXR_Error( 500, $term->get_error_message() );
	}

	if ( ! $term ) {
		return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
	}

	// Update term meta.
	if ( isset( $content_struct['custom_fields'] ) ) {
		$this->set_term_custom_fields( $term_id, $content_struct['custom_fields'] );
	}

	return true;
}