WP_REST_Posts_Controller::check_assign_terms_permission()protectedWP 4.7.0

Checks whether current user can assign all terms sent with the current request.

Method of the class: WP_REST_Posts_Controller{}

No Hooks.

Return

true|false. Whether the current user can assign the provided terms.

Usage

// protected - for code of main (parent) or child class
$result = $this->check_assign_terms_permission( $request );
$request(WP_REST_Request) (required)
The request object with post and terms data.

Changelog

Since 4.7.0 Introduced.

WP_REST_Posts_Controller::check_assign_terms_permission() code WP 6.4.3

protected function check_assign_terms_permission( $request ) {
	$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
	foreach ( $taxonomies as $taxonomy ) {
		$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;

		if ( ! isset( $request[ $base ] ) ) {
			continue;
		}

		foreach ( (array) $request[ $base ] as $term_id ) {
			// Invalid terms will be rejected later.
			if ( ! get_term( $term_id, $taxonomy->name ) ) {
				continue;
			}

			if ( ! current_user_can( 'assign_term', (int) $term_id ) ) {
				return false;
			}
		}
	}

	return true;
}