Skip to content

Commit bac66d1

Browse files
committed
feat: standarize airtable fields serialization
1 parent d867387 commit bac66d1

4 files changed

Lines changed: 151 additions & 154 deletions

File tree

forms-bridge/addons/airtable/class-airtable-addon.php

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,16 @@ class Airtable_Addon extends Addon {
5151
* @return boolean
5252
*/
5353
public function ping( $backend ) {
54-
$bridge = new Airtable_Form_Bridge(
55-
array(
56-
'name' => '__airtable-' . time(),
57-
'backend' => $backend,
58-
'endpoint' => '/v0/meta/bases',
59-
'method' => 'GET',
60-
)
61-
);
54+
$backend = FBAPI::get_backend( $backend );
6255

63-
$response = $bridge->submit();
56+
if ( ! $backend ) {
57+
Logger::log( 'Airtable backend ping error: Backend is unkown or invalid', Logger::ERROR );
58+
return false;
59+
}
60+
61+
$response = $backend->get( '/v0/meta/bases' );
6462
if ( is_wp_error( $response ) ) {
65-
Logger::log( 'Airtable backend ping error: Unable to recover the credential access token', Logger::ERROR );
63+
Logger::log( 'Airtable backend ping error: Unable to list airtable bases', Logger::ERROR );
6664
return false;
6765
}
6866

@@ -78,25 +76,24 @@ public function ping( $backend ) {
7876
* @return array|WP_Error
7977
*/
8078
public function fetch( $endpoint, $backend ) {
81-
$bridge = new Airtable_Form_Bridge(
82-
array(
83-
'name' => '__airtable-meta-bases',
84-
'backend' => $backend,
85-
'endpoint' => '/v0/meta/bases',
86-
'method' => 'GET',
87-
),
88-
);
79+
$backend = FBAPI::get_backend( $backend );
80+
if ( ! $backend ) {
81+
return new WP_Error( 'invalid_backend', 'Backend is unkown or invalid', array( 'backend' => $backend ) );
82+
}
8983

90-
$response = $bridge->submit();
84+
if ( $endpoint && '/v0/meta/tables' !== $endpoint ) {
85+
return $backend->get( $endpoint );
86+
}
87+
88+
$response = $backend->get( '/v0/meta/bases' );
9189

9290
if ( is_wp_error( $response ) ) {
9391
return $response;
9492
}
9593

9694
$tables = array();
9795
foreach ( $response['data']['bases'] as $base ) {
98-
$schema_response = $bridge->patch( array( 'endpoint' => "/v0/meta/bases/{$base['id']}/tables" ) )
99-
->submit();
96+
$schema_response = $backend->get( "/v0/meta/bases/{$base['id']}/tables" );
10097

10198
if ( is_wp_error( $schema_response ) ) {
10299
return $schema_response;
@@ -172,45 +169,20 @@ public function get_endpoint_schema( $endpoint, $backend, $method = null ) {
172169

173170
$schema = array();
174171
foreach ( $fields as $field ) {
175-
if (
176-
in_array(
177-
$field['type'],
178-
array(
179-
'aiText',
180-
'formula',
181-
'autoNumber',
182-
'button',
183-
'count',
184-
'createdBy',
185-
'createdTime',
186-
'lastModifiedBy',
187-
'lastModifiedTime',
188-
'rollup',
189-
'externalSyncSource',
190-
'multipleCollaborators',
191-
'multipleLookupValues',
192-
'multipleRecordLinks',
193-
),
194-
true,
195-
)
196-
) {
197-
continue;
198-
}
199-
200172
switch ( $field['type'] ) {
201-
case 'rating':
202173
case 'number':
203174
$type = 'number';
204175
break;
205176
case 'checkbox':
206177
$type = 'boolean';
207178
break;
208-
case 'multipleSelects':
209-
$type = 'array';
179+
case 'select':
180+
$type = $field['is_multi'] ? 'array' : 'string';
210181
break;
211-
case 'multipleAttachments':
182+
case 'file':
212183
$type = 'file';
213184
break;
185+
case 'textarea':
214186
default:
215187
$type = 'string';
216188
break;

forms-bridge/addons/airtable/class-airtable-form-bridge.php

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,20 @@ public function get_fields() {
6868
return new WP_Error( 'invalid_bridge', 'The bridge is invalid', $this->data );
6969
}
7070

71+
$backend = $this->backend;
72+
if ( ! $backend ) {
73+
return new WP_Error( 'invalid_backend', 'The bridge backend is unkown or invalid', $this->data );
74+
}
75+
7176
$base_id = $this->base_id();
7277
$table_id = $this->table_id();
7378

7479
if ( ! $base_id || ! $table_id ) {
75-
return new WP_Error( 'invalid_endpoint', 'The bridge has an invalid endpoint', $this->data );
80+
return new WP_Error( 'invalid_endpoint', 'The bridge has an invalid endpoint', $this->data );
7681
}
7782

78-
$response = $this->patch(
79-
array(
80-
'method' => 'GET',
81-
'endpoint' => "/v0/meta/bases/{$base_id}/tables",
82-
)
83-
)->submit();
83+
$endpoint = "/v0/meta/bases/{$base_id}/tables";
84+
$response = $backend->get( $endpoint );
8485

8586
if ( is_wp_error( $response ) ) {
8687
return $response;
@@ -97,7 +98,81 @@ public function get_fields() {
9798
return new WP_Error( 'not_found', 'Table not found', $this->data );
9899
}
99100

100-
return $table['fields'];
101+
$fields = array();
102+
foreach ( $table['fields'] as $air_field ) {
103+
if (
104+
in_array(
105+
$air_field['type'],
106+
array(
107+
'aiText',
108+
'formula',
109+
'autoNumber',
110+
'button',
111+
'count',
112+
'createdBy',
113+
'createdTime',
114+
'lastModifiedBy',
115+
'lastModifiedTime',
116+
'rollup',
117+
'externalSyncSource',
118+
'multipleCollaborators',
119+
'multipleLookupValues',
120+
'multipleRecordLinks',
121+
),
122+
true,
123+
)
124+
) {
125+
continue;
126+
}
127+
128+
$field = array(
129+
'id' => $air_field['id'],
130+
'name' => $air_field['name'],
131+
'label' => $air_field['name'],
132+
);
133+
134+
switch ( $air_field['type'] ) {
135+
case 'multipleAttachments':
136+
$field['type'] = 'file';
137+
$field['is_multi'] = true;
138+
break;
139+
case 'rating':
140+
case 'number':
141+
$field['type'] = 'number';
142+
break;
143+
case 'checkbox':
144+
$field['type'] = 'checkbox';
145+
break;
146+
case 'multipleSelects':
147+
case 'singleSelect':
148+
$field['type'] = 'select';
149+
$field['options'] = array_map(
150+
function ( $choice ) {
151+
return array(
152+
'value' => $choice['name'],
153+
'label' => $choice['name'],
154+
);
155+
},
156+
$air_field['options']['choices'],
157+
);
158+
159+
$field['is_multi'] = 'multipleSelects' === $air_field['type'];
160+
break;
161+
case 'date':
162+
$field['type'] = 'date';
163+
break;
164+
case 'multilineText':
165+
$field['type'] = 'textarea';
166+
break;
167+
default:
168+
$field['type'] = 'text';
169+
break;
170+
}
171+
172+
$fields[] = $field;
173+
}
174+
175+
return $fields;
101176
}
102177

103178
/**
@@ -136,7 +211,7 @@ public function submit( $payload = array(), $attachments = array() ) {
136211

137212
$l = count( $fields );
138213
for ( $i = 0; $i < $l; ++$i ) {
139-
if ( 'multipleAttachments' === $fields[ $i ]['type'] ) {
214+
if ( 'file' === $fields[ $i ]['type'] ) {
140215
$attachment_field = $fields[ $i ];
141216
$attachment_name = $attachment_field['name'];
142217

@@ -170,7 +245,11 @@ function ( $name ) use ( $attachment_name ) {
170245
$field_name = $data_field['name'];
171246

172247
if ( isset( $payload[ $field_name ] ) ) {
173-
if ( 'multipleSelects' === $data_field['type'] && ! is_array( $payload[ $field_name ] ) ) {
248+
if (
249+
'select' === $data_field['type']
250+
&& $data_field['is_multi']
251+
&& ! is_array( $payload[ $field_name ] )
252+
) {
174253
$payload[ $field_name ] = array( $payload[ $field_name ] );
175254
}
176255

@@ -204,7 +283,7 @@ function ( $name ) use ( $attachment_name ) {
204283
$filename = basename( $path );
205284
$filetype = wp_check_filetype( $path );
206285
if ( empty( $filetype['type'] ) ) {
207-
$filetype['type'] = mime_content_type( $path );
286+
$filetype['type'] = mime_content_type( $path ) ?: 'octet/stream';
208287
}
209288
}
210289
}

0 commit comments

Comments
 (0)