WC_REST_Product_Categories_Controller::update_term_meta_fields()protectedWC 3.5.5

Update term meta fields.

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

Хуков нет.

Возвращает

true|false|WP_Error.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->update_term_meta_fields( $term, $request );
$term(WP_Term) (обязательный)
Term object.
$request(WP_REST_Request) (обязательный)
Request instance.

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

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

Код WC_REST_Product_Categories_Controller::update_term_meta_fields() WC 8.7.0

protected function update_term_meta_fields( $term, $request ) {
	$id = (int) $term->term_id;

	if ( isset( $request['display'] ) ) {
		update_term_meta( $id, 'display_type', 'default' === $request['display'] ? '' : $request['display'] );
	}

	if ( isset( $request['menu_order'] ) ) {
		update_term_meta( $id, 'order', $request['menu_order'] );
	}

	if ( isset( $request['image'] ) ) {
		if ( empty( $request['image']['id'] ) && ! empty( $request['image']['src'] ) ) {
			$upload = wc_rest_upload_image_from_url( esc_url_raw( $request['image']['src'] ) );

			if ( is_wp_error( $upload ) ) {
				return $upload;
			}

			$image_id = wc_rest_set_uploaded_image_as_attachment( $upload );
		} else {
			$image_id = isset( $request['image']['id'] ) ? absint( $request['image']['id'] ) : 0;
		}

		// Check if image_id is a valid image attachment before updating the term meta.
		if ( $image_id && wp_attachment_is_image( $image_id ) ) {
			update_term_meta( $id, 'thumbnail_id', $image_id );

			// Set the image alt.
			if ( ! empty( $request['image']['alt'] ) ) {
				update_post_meta( $image_id, '_wp_attachment_image_alt', wc_clean( $request['image']['alt'] ) );
			}

			// Set the image title.
			if ( ! empty( $request['image']['name'] ) ) {
				wp_update_post(
					array(
						'ID'         => $image_id,
						'post_title' => wc_clean( $request['image']['name'] ),
					)
				);
			}
		} else {
			delete_term_meta( $id, 'thumbnail_id' );
		}
	}

	return true;
}