__( 'Softr', 'advanced-form-integration' ),
'tasks' => array(
'create_record' => __( 'Create Database Record', 'advanced-form-integration' ),
),
);
return $actions;
}
add_filter( 'adfoin_settings_tabs', 'adfoin_softr_settings_tab', 10, 1 );
function adfoin_softr_settings_tab( $providers ) {
$providers['softr'] = __( 'Softr', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_softr_settings_view', 10, 1 );
function adfoin_softr_settings_view( $current_tab ) {
if ( 'softr' !== $current_tab ) {
return;
}
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
$fields = array(
array(
'name' => 'apiKey',
'label' => __( 'API Key (PAT)', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'mask' => true,
'placeholder' => __( 'Softr Studio → Settings → API & Embed', 'advanced-form-integration' ),
'show_in_table' => true,
),
array(
'name' => 'domain',
'label' => __( 'Softr Domain (only required for AFI Pro user-management tasks)', 'advanced-form-integration' ),
'type' => 'text',
'required' => false,
'placeholder' => 'yourapp.softr.app',
'show_in_table' => false,
),
);
$instructions = sprintf(
'
- %s
- %s
- %s
',
esc_html__( 'In Softr Studio open Settings → API & Embed and generate a Personal Access Token.', 'advanced-form-integration' ),
esc_html__( 'Paste it as the API Key below. AFI calls tables-api.softr.io/api/v1/ with Softr-Api-Key for database tasks.', 'advanced-form-integration' ),
esc_html__( 'Domain is only needed for user-management actions in AFI Pro (Create User / Invite / Magic Link).', 'advanced-form-integration' )
);
ADFOIN_Account_Manager::render_settings_view( 'softr', __( 'Softr', 'advanced-form-integration' ), $fields, $instructions );
}
add_action( 'wp_ajax_adfoin_get_softr_credentials', 'adfoin_get_softr_credentials', 10, 0 );
function adfoin_get_softr_credentials() {
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
ADFOIN_Account_Manager::ajax_get_credentials_list( 'softr' );
}
add_action( 'wp_ajax_adfoin_save_softr_credentials', 'adfoin_save_softr_credentials', 10, 0 );
function adfoin_save_softr_credentials() {
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
ADFOIN_Account_Manager::ajax_save_credentials( 'softr', array( 'apiKey', 'domain' ) );
}
if ( ! function_exists( 'adfoin_softr_credentials_list' ) ) :
function adfoin_softr_credentials_list() {
foreach ( adfoin_read_credentials( 'softr' ) as $option ) {
printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) );
}
}
endif;
add_action( 'adfoin_action_fields', 'adfoin_softr_action_fields' );
function adfoin_softr_action_fields() {
?>
$fields ),
$field_data,
$posted_data
);
adfoin_softr_request(
'databases/' . rawurlencode( $database_id ) . '/tables/' . rawurlencode( $table_id ) . '/records',
'POST',
$payload,
$record,
$cred_id,
'tables'
);
}
if ( ! function_exists( 'adfoin_softr_request' ) ) :
/**
* Call a Softr REST API. Tables vs studio share the API key but use
* different host and version prefixes.
*
* @param string $endpoint Path under the version prefix.
* @param string $method HTTP verb.
* @param mixed $data Body (POST/PUT/PATCH) or query (GET).
* @param array $record Submission record for logging.
* @param string $cred_id Saved credential id.
* @param string $surface 'tables' (default) or 'studio' (user management).
*
* @return array|WP_Error
*/
function adfoin_softr_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '', $surface = 'tables' ) {
$api_key = '';
$domain = '';
if ( $cred_id && function_exists( 'adfoin_get_credentials_by_id' ) ) {
$credentials = adfoin_get_credentials_by_id( 'softr', $cred_id );
if ( is_array( $credentials ) ) {
$api_key = isset( $credentials['apiKey'] ) ? trim( (string) $credentials['apiKey'] ) : '';
$domain = isset( $credentials['domain'] ) ? trim( (string) $credentials['domain'] ) : '';
}
}
if ( ! $api_key ) {
return new WP_Error( 'softr_missing_key', __( 'Softr API key is missing.', 'advanced-form-integration' ) );
}
$base = ( 'studio' === $surface )
? 'https://studio-api.softr.io/v1/api/'
: 'https://tables-api.softr.io/api/v1/';
$url = $base . ltrim( $endpoint, '/' );
$method = strtoupper( $method );
$args = array(
'timeout' => 30,
'method' => $method,
'headers' => array(
'Softr-Api-Key' => $api_key,
'Accept' => 'application/json',
),
);
if ( 'studio' === $surface ) {
if ( ! $domain ) {
return new WP_Error( 'softr_missing_domain', __( 'Softr Domain is required for user-management tasks. Set it on the saved account.', 'advanced-form-integration' ) );
}
$args['headers']['Softr-Domain'] = $domain;
}
if ( 'GET' === $method ) {
if ( is_array( $data ) && ! empty( $data ) ) {
$url = add_query_arg( $data, $url );
}
} else {
$args['headers']['Content-Type'] = 'application/json';
$args['body'] = wp_json_encode( $data );
}
$response = wp_remote_request( $url, $args );
if ( $record ) {
adfoin_add_to_log( $response, $url, $args, $record );
}
return $response;
}
endif;