feat: Dynamic SPA slug, field label storage, and SPA frontpage support (WIP)
This commit is contained in:
@@ -86,25 +86,39 @@ class AddressController {
|
||||
// Generate new ID
|
||||
$new_id = empty($addresses) ? 1 : max(array_column($addresses, 'id')) + 1;
|
||||
|
||||
// Prepare address data
|
||||
// Standard address fields
|
||||
$standard_fields = ['first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country', 'email', 'phone'];
|
||||
$reserved_fields = ['id', 'label', 'type', 'is_default'];
|
||||
|
||||
// Prepare address data with standard fields
|
||||
$address = [
|
||||
'id' => $new_id,
|
||||
'label' => sanitize_text_field($request->get_param('label')),
|
||||
'type' => sanitize_text_field($request->get_param('type')), // 'billing', 'shipping', or 'both'
|
||||
'first_name' => sanitize_text_field($request->get_param('first_name')),
|
||||
'last_name' => sanitize_text_field($request->get_param('last_name')),
|
||||
'company' => sanitize_text_field($request->get_param('company')),
|
||||
'address_1' => sanitize_text_field($request->get_param('address_1')),
|
||||
'address_2' => sanitize_text_field($request->get_param('address_2')),
|
||||
'city' => sanitize_text_field($request->get_param('city')),
|
||||
'state' => sanitize_text_field($request->get_param('state')),
|
||||
'postcode' => sanitize_text_field($request->get_param('postcode')),
|
||||
'country' => sanitize_text_field($request->get_param('country')),
|
||||
'email' => sanitize_email($request->get_param('email')),
|
||||
'phone' => sanitize_text_field($request->get_param('phone')),
|
||||
'is_default' => (bool) $request->get_param('is_default'),
|
||||
];
|
||||
|
||||
// Add standard fields
|
||||
foreach ($standard_fields as $field) {
|
||||
$value = $request->get_param($field);
|
||||
if ($field === 'email') {
|
||||
$address[$field] = sanitize_email($value);
|
||||
} else {
|
||||
$address[$field] = sanitize_text_field($value);
|
||||
}
|
||||
}
|
||||
|
||||
// Add any custom fields (like destination_id from Rajaongkir)
|
||||
$all_params = $request->get_json_params();
|
||||
if (is_array($all_params)) {
|
||||
foreach ($all_params as $key => $value) {
|
||||
if (!in_array($key, $standard_fields) && !in_array($key, $reserved_fields)) {
|
||||
// Store custom field
|
||||
$address[$key] = is_string($value) ? sanitize_text_field($value) : $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If this is set as default, unset other defaults of the same type
|
||||
if ($address['is_default']) {
|
||||
foreach ($addresses as &$addr) {
|
||||
@@ -138,22 +152,36 @@ class AddressController {
|
||||
if ($addr['id'] === $address_id) {
|
||||
$found = true;
|
||||
|
||||
// Update fields
|
||||
$addr['label'] = sanitize_text_field($request->get_param('label'));
|
||||
$addr['type'] = sanitize_text_field($request->get_param('type'));
|
||||
$addr['first_name'] = sanitize_text_field($request->get_param('first_name'));
|
||||
$addr['last_name'] = sanitize_text_field($request->get_param('last_name'));
|
||||
$addr['company'] = sanitize_text_field($request->get_param('company'));
|
||||
$addr['address_1'] = sanitize_text_field($request->get_param('address_1'));
|
||||
$addr['address_2'] = sanitize_text_field($request->get_param('address_2'));
|
||||
$addr['city'] = sanitize_text_field($request->get_param('city'));
|
||||
$addr['state'] = sanitize_text_field($request->get_param('state'));
|
||||
$addr['postcode'] = sanitize_text_field($request->get_param('postcode'));
|
||||
$addr['country'] = sanitize_text_field($request->get_param('country'));
|
||||
$addr['email'] = sanitize_email($request->get_param('email'));
|
||||
$addr['phone'] = sanitize_text_field($request->get_param('phone'));
|
||||
// Standard address fields
|
||||
$standard_fields = ['first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country', 'email', 'phone'];
|
||||
$reserved_fields = ['id', 'label', 'type', 'is_default'];
|
||||
|
||||
// Update standard meta fields
|
||||
$addr['label'] = sanitize_text_field($request->get_param('label'));
|
||||
$addr['type'] = sanitize_text_field($request->get_param('type'));
|
||||
$addr['is_default'] = (bool) $request->get_param('is_default');
|
||||
|
||||
// Update standard fields
|
||||
foreach ($standard_fields as $field) {
|
||||
$value = $request->get_param($field);
|
||||
if ($field === 'email') {
|
||||
$addr[$field] = sanitize_email($value);
|
||||
} else {
|
||||
$addr[$field] = sanitize_text_field($value);
|
||||
}
|
||||
}
|
||||
|
||||
// Update any custom fields (like destination_id from Rajaongkir)
|
||||
$all_params = $request->get_json_params();
|
||||
if (is_array($all_params)) {
|
||||
foreach ($all_params as $key => $value) {
|
||||
if (!in_array($key, $standard_fields) && !in_array($key, $reserved_fields)) {
|
||||
// Store/update custom field
|
||||
$addr[$key] = is_string($value) ? sanitize_text_field($value) : $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If this is set as default, unset other defaults of the same type
|
||||
if ($addr['is_default']) {
|
||||
foreach ($addresses as &$other_addr) {
|
||||
|
||||
Reference in New Issue
Block a user