Under "Permissions" enable files.metadata.read and files.content.write, then click Submit.
Copy the App key and App secret from the Settings tab.
Open https://www.dropbox.com/oauth2/authorize?client_id=YOUR_APP_KEY&response_type=code&token_access_type=offline (replace YOUR_APP_KEY), allow access, and copy the code Dropbox shows.
Paste the App key, App secret and that code below and save. AFI exchanges the code for a refresh token, so the connection keeps working (pasted access tokens expire after a few hours).
',
'advanced-form-integration'
);
echo adfoin_platform_settings_template($title, $key, $arguments, $instructions);
}
add_action('wp_ajax_adfoin_get_dropbox_credentials', 'adfoin_get_dropbox_credentials', 10, 0);
function adfoin_get_dropbox_credentials() {
adfoin_verify_nonce();
$all_credentials = adfoin_read_credentials('dropbox');
wp_send_json_success($all_credentials);
}
add_action('wp_ajax_adfoin_save_dropbox_credentials', 'adfoin_save_dropbox_credentials', 10, 0);
function adfoin_save_dropbox_credentials() {
adfoin_verify_nonce();
$platform = sanitize_text_field( wp_unslash( $_POST['platform'] ) );
if ('dropbox' == $platform) {
$data = adfoin_array_map_recursive('sanitize_text_field', $_POST['data']);
// Exchange a one-time authorization code for a long-lived refresh token.
if (is_array($data)) {
foreach ($data as &$account) {
if (!is_array($account) || empty($account['authCode'])) {
continue;
}
if (!empty($account['appKey']) && !empty($account['appSecret'])) {
$token = adfoin_dropbox_oauth_token($account['appKey'], $account['appSecret'], [
'grant_type' => 'authorization_code',
'code' => $account['authCode'],
]);
if (!is_wp_error($token) && !empty($token['refresh_token'])) {
$account['refreshToken'] = $token['refresh_token'];
}
}
// The code is single-use; never keep it.
$account['authCode'] = '';
}
unset($account);
}
adfoin_save_credentials($platform, $data);
}
wp_send_json_success();
}
function adfoin_dropbox_credentials_list() {
$html = '';
$credentials = adfoin_read_credentials('dropbox');
foreach ($credentials as $option) {
$html .= '';
}
echo $html;
}
add_action('adfoin_action_fields', 'adfoin_dropbox_action_fields');
function adfoin_dropbox_action_fields()
{
?>
''], [], $cred_id);
$body = json_decode(wp_remote_retrieve_body($response), true);
if (!empty($body['entries'])) {
$folders = ['/' => __('Root', 'advanced-form-integration')];
foreach ($body['entries'] as $entry) {
if ($entry['.tag'] === 'folder') {
$folders[$entry['id']] = $entry['name'];
}
}
wp_send_json_success($folders);
} else {
wp_send_json_error();
}
}
add_action('adfoin_job_queue', 'adfoin_dropbox_job_queue', 10, 1);
function adfoin_dropbox_job_queue($data) {
adfoin_dropbox_send_data($data['record'], $data['posted_data']);
}
function adfoin_dropbox_send_data($record, $posted_data) {
$record_data = json_decode($record['data'], true);
if (adfoin_check_conditional_logic($record_data['action_data']['cl'] ?? [], $posted_data)) return;
$data = $record_data['field_data'];
$cred_id = empty($data['credId']) ? '' : $data['credId'];
$folder_id = empty($data['folderId']) ? '' : $data['folderId'];
$file_path = empty($data['fileField']) ? '' : adfoin_get_parsed_values($data['fileField'], $posted_data);
if (!$folder_id || !$file_path) {
return;
}
$file_content = file_get_contents($file_path);
$folder_id = $folder_id == '/' ? '' : $folder_id;
$response = adfoin_dropbox_file_request(
'/2/files/upload',
'POST',
['path' => "$folder_id/" . basename($file_path), 'mode' => 'add', 'autorename' => false, 'mute' => false, 'strict_conflict' => false],
$file_content,
$cred_id
);
if (is_wp_error($response)) {
return $response;
}
return json_decode(wp_remote_retrieve_body($response), true);
}
/**
* POST to Dropbox's OAuth token endpoint (authorization_code or
* refresh_token grant).
*
* @return array|WP_Error Decoded token response.
*/
function adfoin_dropbox_oauth_token($app_key, $app_secret, $params) {
$response = wp_remote_post('https://api.dropboxapi.com/oauth2/token', [
'timeout' => 30,
'body' => array_merge($params, [
'client_id' => $app_key,
'client_secret' => $app_secret,
]),
]);
if (is_wp_error($response)) {
return $response;
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (200 !== (int) wp_remote_retrieve_response_code($response) || empty($body['access_token'])) {
$message = is_array($body) ? ($body['error_description'] ?? ($body['error'] ?? '')) : '';
return new WP_Error('dropbox_token_failed', $message ? (string) $message : __('Dropbox token request failed.', 'advanced-form-integration'));
}
return $body;
}
/**
* Access token for a Dropbox account. Accounts with a refresh token get a
* short-lived token that is cached and refreshed on demand; older accounts
* fall back to their pasted access token.
*/
function adfoin_dropbox_get_access_token($cred_id, $force_refresh = false) {
$credentials = adfoin_get_credentials_by_id('dropbox', $cred_id);
if (!is_array($credentials)) {
return '';
}
$refresh_token = $credentials['refreshToken'] ?? '';
$app_key = $credentials['appKey'] ?? '';
$app_secret = $credentials['appSecret'] ?? '';
if (!$refresh_token || !$app_key || !$app_secret) {
return $credentials['accessToken'] ?? '';
}
$cache_key = 'adfoin_dropbox_token_' . md5((string) $cred_id);
if (!$force_refresh) {
$cached = get_transient($cache_key);
if (is_string($cached) && $cached) {
return $cached;
}
}
$token = adfoin_dropbox_oauth_token($app_key, $app_secret, [
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
]);
if (is_wp_error($token)) {
return '';
}
$ttl = isset($token['expires_in']) ? max(60, (int) $token['expires_in'] - 300) : 3 * HOUR_IN_SECONDS;
set_transient($cache_key, $token['access_token'], $ttl);
return $token['access_token'];
}
/**
* Send a Dropbox request with the account's token; on a 401, refresh the
* token once for this request and retry.
*/
function adfoin_dropbox_send($url, $args, $cred_id) {
$args['headers']['Authorization'] = 'Bearer ' . adfoin_dropbox_get_access_token($cred_id);
$result = wp_remote_request($url, $args);
if (401 === (int) wp_remote_retrieve_response_code($result)) {
$token = adfoin_dropbox_get_access_token($cred_id, true);
if ($token) {
$args['headers']['Authorization'] = 'Bearer ' . $token;
$result = wp_remote_request($url, $args);
}
}
return $result;
}
function adfoin_dropbox_file_request($endpoint, $method = 'POST', $data = [], $body = null, $cred_id = '') {
$url = 'https://content.dropboxapi.com' . $endpoint;
$args = [
'timeout' => 30,
'method' => $method,
'headers' => [
'Dropbox-API-Arg' => wp_json_encode($data),
'Content-Type' => 'application/octet-stream',
],
'body' => $body
];
$result = adfoin_dropbox_send($url, $args, $cred_id);
if (!is_wp_error($result)) {
return $result;
}
}
function adfoin_dropbox_request($endpoint, $method = 'POST', $data = [], $body = null, $cred_id = '') {
$url = 'https://api.dropboxapi.com' . $endpoint;
$args = [
'timeout' => 30,
'method' => $method,
'headers' => [
'Content-Type' => 'application/json',
],
'body' => wp_json_encode($data)
];
if ($body) {
$args['body'] = $body;
}
$result = adfoin_dropbox_send($url, $args, $cred_id);
if (!is_wp_error($result)) {
return $result;
}
}