Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Email\Schema

EmailSettingsSchema::get_item_responsepublicWC 1.0

Get email settings data by transforming email settings into REST API format.

Method of the class: EmailSettingsSchema{}

No Hooks.

Returns

Array.

Usage

$EmailSettingsSchema = new EmailSettingsSchema();
$EmailSettingsSchema->get_item_response( $item, $request, $include_fields ): array;
$item(mixed) (required)
Settings array from WC_Settings_Emails.
$request(WP_REST_Request) (required)
Request object.
$include_fields(array)
Fields to include.
Default: array()

EmailSettingsSchema::get_item_response() code WC 10.4.3

public function get_item_response( $item, WP_REST_Request $request, array $include_fields = array() ): array {
	$settings = is_array( $item ) ? $item : array();

	// Transform settings into grouped format based on title/sectionend markers.
	$groups           = array();
	$values           = array();
	$current_group    = null;
	$current_group_id = null;

	foreach ( $settings as $setting ) {
		$setting_type = $setting['type'] ?? '';

		// Handle section titles and email_color_palette - start of a new group.
		if ( 'title' === $setting_type || 'email_color_palette' === $setting_type ) {
			$current_group_id = $setting['id'] ?? '';
			$current_group    = array(
				'title'       => $setting['title'] ?? '',
				'description' => $setting['desc'] ?? '',
				'order'       => isset( $setting['order'] ) ? (int) $setting['order'] : 999,
				'fields'      => array(),
			);
			continue;
		}

		// Handle section ends - save the current group.
		if ( 'sectionend' === $setting_type ) {
			if ( $current_group && $current_group_id ) {
				$groups[ $current_group_id ] = $current_group;
			}
			$current_group    = null;
			$current_group_id = null;
			continue;
		}

		// Skip non-editable field types.
		if ( in_array( $setting_type, self::NON_EDITABLE_TYPES, true ) ) {
			continue;
		}

		// Process field if we have a current group and the setting has an ID.
		if ( isset( $setting['id'] ) && $current_group ) {
			$setting_id   = $setting['id'];
			$setting_type = $setting['type'] ?? 'text';

			// Map WooCommerce field types to REST API types.
			$api_type = $this->map_setting_type_to_api_type( $setting_type );

			// Build field definition.
			$field = array(
				'id'    => $setting_id,
				'label' => $setting['title'] ?? $setting_id,
				'type'  => $api_type,
			);

			// Add description if available.
			if ( ! empty( $setting['desc'] ) ) {
				$field['desc'] = $setting['desc'];
			}

			// Add options if available.
			if ( isset( $setting['options'] ) && is_array( $setting['options'] ) ) {
				$field['options'] = $setting['options'];
			}

			$current_group['fields'][] = $field;

			// Get current value.
			$default_value = $setting['default'] ?? '';
			$current_value = get_option( $setting_id, $default_value );

			// Convert checkbox values to boolean for API.
			if ( 'checkbox' === $setting_type ) {
				$current_value = 'yes' === $current_value;
			}

			$values[ $setting_id ] = $current_value;
		}
	}

	// Filter groups without fields.
	$groups = array_filter(
		$groups,
		function ( $group ) {
			return ! empty( $group['fields'] );
		}
	);

	$response = array(
		'id'          => 'email',
		'title'       => __( 'Email design', 'woocommerce' ),
		'description' => __( 'Customize the look and feel of all you notification emails.', 'woocommerce' ),
		'values'      => $values,
		'groups'      => $groups,
	);

	if ( ! empty( $include_fields ) ) {
		$response = array_intersect_key( $response, array_flip( $include_fields ) );
	}

	return $response;
}