@@ -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