Step 3. Creating an Installer/Uninstaller

Installer

The installer needs to live in the root directory of your plugin and allows you to register hooks, create options, create tables in the database, and register new routes.

Consider the following sample installer file, or for a more extensive example, please take a look at some working examples:

The installer should simply create a series of arrays that will be used by Zenbership to properly set up the plugin when a user installs it. It can further run any code you wish, and since it is loaded within the Zenbership environment, you can use any Zenbership class within the installer.

<?php

/**
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * PRIMARY PLUGIN METADATA
 */
$plugin = array(
    'id'                => 'member_directory',
    'name'              => 'Member Directory',
    'description'       => 'Creates a member directory on your membership website.',
    'author'            => 'Castlamp',
    'author_url'        => 'https://www.castlamp.com/',
    'author_twitter'    => 'castlamp',
    'version'           => '1.0',
    'app_creator'       => '',
    'app_creator_url'   => '',
);

/**
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * REGISTER A HOOK
 * 
 * trigger:
 * specific_trigger:
 * type: 1 = PHP Include, 2 = email, 3 = MySQL Query, 4 = Function name
 * data:    PHP: path to file. 
 *          Email: E-mail array.
 *          MySQL Query: Array of commands to run.
 *          Function: Array of function names to run.
 * when: 1 = before action, 2 = after action
 *
 * Use %path% in the "data" field to let Zenbership
 * determine the correct path.
 */

$hooks = array();

$hooks[] = array(
    'trigger'           => 'login',
    'specific_trigger'  => '',
    'type'              => '1',
    'data'              => '%path%/login.php',
    'when'              => '2',
);

/**
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * CREATE AN OPTION
 * 
 * display: Display name
 * value: Value
 * description: Description
 * type: 'text','select','radio','checkbox','timeframe','special','file_size','textarea'
 * width: Width of field
 * maxlength: 
 * options: array of options for select or radio, separated by vertical bar "|", or for special, "list:[list_type]"
 */

$options = array();

$options[] = array(
    'id'            => 'searchable',
    'name'          => 'Searchable Fields',
    'value'         => 'username,last_name,city,state',
    'description'   => 'Comma separated list. Names must match the field ID listed on the Integration > Fields page.',
    'type'          => 'text',
    'width'         => '300',
    'maxlength'     => '',
    'options'       => '',
);

/**
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Creating Routes
 *
 * Routes allow you to map a URL to a template.
 */

$routes = array();

$routes[] = array(
    'path'          => '/directory/(username)',
    'resolve'       => 'listing.php',
);


/**
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Run SQL Against the Zenbership Database
 */

$tables = array();

$tables['zen_plugin_docusign'] = "
    CREATE TABLE `zen_plugin_docusign` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `envelop_id` varchar(50) DEFAULT NULL,
      `date` datetime DEFAULT NULL,
      `user_id` varchar(40) DEFAULT NULL,
      `user_type` varchar(10) DEFAULT NULL,
      `confirmed` tinyint(1) DEFAULT NULL,
      `date_confirmed` datetime DEFAULT NULL,
      `template_id` varchar(50) DEFAULT NULL,
      `due_date` datetime DEFAULT NULL,
      `url` varchar(255) DEFAULT NULL,
      `status` varchar(20) DEFAULT NULL,
      `storage_folder` varchar(150) DEFAULT NULL,
      PRIMARY KEY (`id`),
      KEY `envelop_id` (`envelop_id`),
      KEY `user_id` (`user_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";

$tables['zen_plugin_docusign_triggers'] = "
    CREATE TABLE `zen_plugin_docusign_triggers` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `trigger` varchar(30) DEFAULT NULL,
      `trigger_id` varchar(30) DEFAULT NULL,
      `template_id` varchar(50) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";

/**
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * Everything Else
 *
 * This can be pretty much anything you want, for example you
 * could update some Zenbership options:
 */

// $db->update_option('sms_plugin', 'twilio');

Uninstaller

An uninstaller is a simple PHP file names uninstall.php which sits in the root directory of your plugin. There are no special rules to how an uninstaller needs to work, so you can place any code oyu wish within the uninstall.php file.

Note that an uninstaller is only required if your installer somehow changed the Zenbership files or database somehow. For example, in the Docusign plugin installer, we generated two new MySQL tables:

So perhaps we would want to remove those tables when the plugin is uninstalled.

Another example would be the Twilio plugin, which sets the sms_plugin option to twilio. Since Twilio won't work anymore, we could run code to reset this during the uninstall process.

Last modified 3 weeks ago.