' . esc_html__( 'airtable.com/create/tokens → Create token with scopes data.records:write and schema.bases:read, and access to the bases you need.', 'fieldwren' ) . '
'; } public function credential_fields() { return array( 'token' => array( 'label' => __( 'Personal access token', 'fieldwren' ), 'type' => 'password', 'required' => true, 'placeholder' => 'pat…' ), ); } private function api( $method, $path, $body = null, $token = null ) { $args = array( 'headers' => array( 'Authorization' => 'Bearer ' . ( null === $token ? $this->cred( 'token' ) : $token ) ) ); if ( null !== $body ) { $args['body'] = $body; } return self::body( $this->http( $method, 'https://api.airtable.com/v0' . $path, $args ) ); } protected function error_message( $body, $code ) { if ( is_array( $body ) && isset( $body['error'] ) && is_array( $body['error'] ) ) { return (string) ( $body['error']['message'] ?? $body['error']['type'] ?? '' ); } return parent::error_message( $body, $code ); } public function verify( array $creds ) { $b = $this->api( 'GET', '/meta/whoami', null, (string) $creds['token'] ); return is_wp_error( $b ) ? $b : (string) ( $b['email'] ?? ( $b['id'] ?? '' ) ); } /** @return array|\WP_Error Tables of a base. */ private function tables( $base ) { $b = $this->api( 'GET', '/meta/bases/' . rawurlencode( $base ) . '/tables' ); return is_wp_error( $b ) ? $b : (array) ( $b['tables'] ?? array() ); } public function options( $source, array $settings ) { switch ( $source ) { case 'bases': $out = array(); $offset = ''; for ( $i = 0; $i < 10; $i++ ) { $b = $this->api( 'GET', '/meta/bases' . ( $offset ? '?offset=' . rawurlencode( $offset ) : '' ) ); if ( is_wp_error( $b ) ) { return $b; } $out += self::pluck( $b['bases'] ?? array(), 'id', 'name' ); $offset = (string) ( $b['offset'] ?? '' ); if ( ! $offset ) { break; } } return $out; case 'tables': $t = $this->tables( (string) ( $settings['base'] ?? '' ) ); return is_wp_error( $t ) ? $t : self::pluck( $t, 'id', 'name' ); case 'fields': $t = $this->tables( (string) ( $settings['base'] ?? '' ) ); if ( is_wp_error( $t ) ) { return $t; } $skip = array( 'formula', 'rollup', 'count', 'lookup', 'multipleLookupValues', 'autoNumber', 'createdTime', 'lastModifiedTime', 'createdBy', 'lastModifiedBy', 'button', 'multipleRecordLinks', 'aiText' ); $out = array(); foreach ( $t as $table ) { if ( (string) $table['id'] !== (string) ( $settings['table'] ?? '' ) ) { continue; } foreach ( (array) ( $table['fields'] ?? array() ) as $f ) { if ( in_array( $f['type'], $skip, true ) ) { continue; } $auto = 'email' === $f['type'] ? 'email' : ( 'phoneNumber' === $f['type'] ? 'phone' : ( 'url' === $f['type'] ? 'url' : ( preg_match( '/^(full )?name$/i', $f['name'] ) ? 'name' : '' ) ) ); $out[] = array( 'key' => $f['name'], 'label' => $f['name'] . ' [' . $f['type'] . ']', 'auto' => $auto, 'type' => $f['type'] ); } } return $out; } return parent::options( $source, $settings ); } public function feed_fields( $form_id ) { return array( array( 'key' => 'base', 'type' => 'select', 'source' => 'bases', 'label' => __( 'Base', 'fieldwren' ), 'required' => true ), array( 'key' => 'table', 'type' => 'select', 'source' => 'tables', 'depends' => array( 'base' ), 'label' => __( 'Table', 'fieldwren' ), 'required' => true ), array( 'key' => 'map', 'type' => 'map', 'source' => 'fields', 'depends' => array( 'base', 'table' ), 'label' => __( 'Fields', 'fieldwren' ), 'desc' => __( 'Values are converted by Airtable (typecast): numbers, dates, checkboxes ("true"), new select options are created; multiple selects take a comma list.', 'fieldwren' ) ), ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $fields = $this->mapped( 'map' ); if ( empty( $s['base'] ) || empty( $s['table'] ) ) { return new \WP_Error( 'fwren_airtable', __( 'Choose a base and a table.', 'fieldwren' ) ); } if ( ! $fields ) { return new \WP_Error( 'fwren_airtable', __( 'No mapped field has a value; nothing was sent.', 'fieldwren' ) ); } $defs = $this->cached_options( 'fields', $s ); $types = array(); foreach ( is_wp_error( $defs ) ? array() : $defs as $d ) { $types[ $d['key'] ] = $d['type'] ?? ''; } foreach ( $fields as $k => $v ) { switch ( $types[ $k ] ?? '' ) { case 'multipleSelects': $fields[ $k ] = array_values( array_filter( array_map( 'trim', explode( ',', $v ) ), 'strlen' ) ); break; case 'checkbox': $fields[ $k ] = $this->truthy( $v ); break; case 'number': case 'currency': case 'percent': case 'rating': case 'duration': $fields[ $k ] = is_numeric( $v ) ? $v + 0 : $v; break; } } $b = $this->api( 'POST', '/' . rawurlencode( (string) $s['base'] ) . '/' . rawurlencode( (string) $s['table'] ), array( 'records' => array( array( 'fields' => (object) $fields ) ), 'typecast' => true ) ); /* translators: %s record id */ return is_wp_error( $b ) ? $b : sprintf( __( 'Record %s created.', 'fieldwren' ), (string) ( $b['records'][0]['id'] ?? '' ) ); } }