WC_REST_General_Settings_V4_Controller::transform_setting_to_fieldprivateWC 1.0

Transform a WooCommerce setting into REST API field format.

Method of the class: WC_REST_General_Settings_V4_Controller{}

No Hooks.

Returns

Array|null. Transformed field or null if should be skipped.

Usage

// private - for code of main (parent) class only
$result = $this->transform_setting_to_field( $setting );
$setting(array) (required)
WooCommerce setting array.

WC_REST_General_Settings_V4_Controller::transform_setting_to_field() code WC 10.3.6

private function transform_setting_to_field( $setting ) {
	$setting_id   = $setting['id'] ?? '';
	$setting_type = $setting['type'] ?? 'text';

	// Skip certain settings that shouldn't be exposed via REST API.
	// This is a temporary array until designs are finalized.
	$skip_settings = array(
		'woocommerce_address_autocomplete_enabled',
		'woocommerce_address_autocomplete_provider',
	);

	if ( in_array( $setting_id, $skip_settings, true ) ) {
		return null;
	}

	$field = array(
		'id'    => $setting_id,
		'label' => $setting['title'] ?? $setting_id,
		'type'  => $this->normalize_field_type( $setting_type ),
		'desc'  => $setting['desc'] ?? '',
	);

	// Add options for select fields.
	if ( isset( $setting['options'] ) && is_array( $setting['options'] ) ) {
		$field['options'] = $setting['options'];
	} else {
		// Generate options for special field types that don't have them in the setting definition.
		$field['options'] = $this->get_field_options( $setting_type, $setting_id );
	}

	return $field;
}