SiteDrive Provider API

This article explains how to use the SiteDrive Provider API. A provider is an integration that provides a way for SiteDrive to source data or files.


sitedrive_provider_register

This function is used to register a custom provider type to SiteDrive.

sitedrive_provider_register('yourType', [
  'name' => __('Your Type')
]);
name string Name or title of your provider
icon string Icon to show in the integrations page
file_process callable Set the image of an individual file or set a default image if this is not valid
download callable Process file into base64 data
import callable The process to import a file into another site using SiteDrive
finder callable Grab results of your provider and add them to the SiteDrive file finder
import_status callable takes 3 arguments. The default status, the drive, and the file in question.
upload callable similar to import. This sends an array with data as a key with the file upload data. This is not a sync request and does not contain information about the last updated time
is_file_type callable a detection function prior to running upload . Returning true here will result in the file type being regarded as a file for your provider, then calling the upload function.
init callable a function to run if your provider is enabled. This is mostly used to hook into actions and delete files if the file has been deleted outside of SiteDrive.

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 Attachment handler
    add_action('gform_after_delete_form', function($form_id) {
      sitedrive_file_delete_by([
        'type' => 'gravityforms',
        'type_id' => $form_id,
      ]);
    });
  },
]);

For website builders we primarily use our custom post import however you might wish to have nicer file images when there is no featured image set on a post. We do this on various builders like Bricks.


sitedrive_providers

This function will grab all providers registered to SiteDrive in an array.

Arguments

None

Returns

Array of providers which is an associative array.

$providers = sitedrive_providers();

foreach ($providers as $provider) {
  echo $provider['id'] . "\n";
}

sitedrive_provider

Grab a singular provider by an ID.

Arguments

$id The ID of provider

Returns

Associative Array of the Provider. Or throws an error if trying to grab a provider that does not exist.


sitedrive_provider_enabled

To enable a provider. Pass in an ID and get the status.

Arguments

$id The ID of provider

Returns

bool if the provider is enabled. Will return false if the provider does not exist.

Still need help? Contact Us Contact Us