Creating a SiteDrive Integration
We are always adding new integrations, however you can also add your own custom provider or custom post types. The following is a sample from our Gravity Forms integration.
<?php
namespace SiteDrive\GravityForms;
use GFAPI;
use SiteDrive\Data\Files;
const LOGO = SITEDRIVE_URL . 'assets/img/gravityforms-default.png';
// Grab a gravity form by our meta key and id we use
function getByMetaID($metaID, $sendID = true) {
$forms = GFAPI::get_forms();
foreach ($forms as $form) {
if ($form[Files::META_KEY] !== $metaID) {
continue;
}
return $sendID
? $form['id']
: $form;
}
return null;
}
function getByID($id) {
$forms = GFAPI::get_forms();
foreach ($forms as $form) {
if ($form['id'] !== (int)$id) {
continue;
}
return $form;
}
return null;
}
// Checks if keys in export are that of a gravity forms export
function isExport($json) {
return !empty($json['fields']) || !empty($json['button']);
}
// Registry as a provider
sitedrive_provider_register('gravityforms', [
'name' => __('Gravity Forms', SITEDRIVE_LOCALIZE),
'icon' => SITEDRIVE_URL . 'assets/img/gravity-forms-icon.jpg',
'file_process' => function($file) {
$file['image'] = LOGO;
$file['last_updated'] = get_the_modified_date('U', $file['type_id']);
return $file;
},
'import_status' => function($default, $drive, $file) {
$metaId = sitedrive_generate_meta_id($drive, $file);
// Check if imported at all
$imported = getByMetaID($metaId, false);
if (empty($imported)) {
return Files::NOT_IMPORTED;
}
// Send status based on updated time
$importedUpdated = $imported[Files::META_UPDATED_KEY];
return (int)$importedUpdated === (int)$file['last_updated']
? Files::SYNCED
: FILES::OUT_OF_SYNC;
},
'download' => function($file) {
$form = GFAPI::get_form($file['type_id']);
return sitedrive_base64_json_encode($form);
},
'import' => function($drive, $file, $contents) {
$metaId = sitedrive_generate_meta_id($drive, $file);
// Grab json data
$json = sitedrive_base64_json_decode($contents);
unset($json['id']);
$foundID = getByMetaID($metaId);
// Set meta key
$json[Files::META_KEY] = $metaId;
$json[Files::META_UPDATED_KEY] = $file['last_updated'];
// Update or add form
if ($foundID) {
$json['id'] = $foundID;
GFAPI::update_form($json);
} else {
$foundID = GFAPI::add_form($json);
}
return $foundID;
},
'finder' => function($args = []) {
if (!empty($args['offset'])) {
return [];
}
$search = $args['search'];
$forms = GFAPI::get_forms();
$results = [];
foreach ($forms as $form) {
if (!empty($search) && stripos($form['title'], $search) === false) {
continue;
}
$results[] = [
'name' => $form['title'],
'id' => $form['id'],
'image' => LOGO,
'type' => 'gravityforms',
];
}
return [
'results' => $results,
];
},
// File uploaded is a valid attachment type
'is_file_type' => function($file) {
$json = sitedrive_safe_json_decode($file['data']);
// Directly from Gravity Forms export
if (!empty($json['0'])) {
return isExport($json['0']);
}
// Singular our download
return isExport($json);
},
'upload' => function($file) {
$json = sitedrive_safe_json_decode($file['data']);
// Singular import
if (isExport($json)) {
$out = [];
$out['type_id'] = GFAPI::add_form($json);
$form = GFAPI::get_form($out['type_id']);
$out['name'] = $form['title'];
return $out;
}
// Multi export from Gravity Forms itself
$ids = [];
foreach ($json as $form) {
$ids[] = GFAPI::add_form($form);
}
return $ids;
},
'init' => function() {
// Delete form handler
add_action('gform_after_delete_form', function($form_id) {
sitedrive_file_delete_by([
'type' => 'gravityforms',
'type_id' => $form_id,
]);
});
},
]);