Plugin.php Hints and Tips

From e107 Wiki
Jump to: navigation, search

e107 Wiki: English | Русский | Deutsch | Français | Magyar | Português | Български | Česky | Nederlands | Ελληνικά | Italiano | Norske | Polska | Slovenščina | Español | Svenska | Translate: Wiki | Page

logo_wiki.png

Current Release 1.0.4 | e107.org | Download | Changelog | Forum | Plugins | Themes | RSS Feed of Latest Changes | Atom Feed of Latest Changes


How to write a plugin
Main page > I : First Aid > II : Development > III : Administration > IV : How To's > V : Plugin Writing > VI : Theming & Shortcodes


Contents

Introduction

This article aims to provide some hints and tips that can be employed in a plugins plugin.php file.

Setting $eplug_table_names/$eplug_tables

Normally, these variables are set directly in the plugin.php file.

Using the code outlined below, the values required can be obtained from the myplugin_sql.php file. This file, when provided, also allows the website administrator to run a database check against the plugin tables to ensure they are set up correctly. So, creating an sql file serves two purposes and helps maintain the integrity of the plugin.

Code: Setting $eplug_table_names/$eplug_tables
// List of table names
$eplug_sql = file_get_contents(e_PLUGIN.$eplug_folder."/club_membership_sql.php");
$ret = preg_match_all("/CREATE TABLE (.*?)\(/i", $eplug_sql, $matches);
$eplug_table_names = $matches[1];

// List of sql requests to create tables
$eplug_tables = explode(";", 
                str_replace("CREATE TABLE ", "CREATE TABLE ".MPREFIX, $eplug_sql));
for ($i=0; $i<count($eplug_tables); $i++) {
   // Prevents adding last array line, which results in an "Unable to create database" 
   // error message when installing the plugin
   if($eplug_tables[$i] != "\r\n"){
       $eplug_tables[$i] .= ";";
   }else{
         unset($eplug_tables[$i]);
   }
}
Code: Sample plugin_sql.php
CREATE TABLE club_mpyplugin_table (
   mpyplugin_id                 int(11)        NOT NULL default 0,
   mpyplugin_status             tinyint(1)     NOT NULL default 0,
   mpyplugin_category           tinyint(1)     NOT NULL default 0,
   mpyplugin_notes              text           NOT NULL default '',
   mpyplugin_datestamp          int(10)        NOT NULL default 0,
   PRIMARY KEY (mpyplugin_id)
) TYPE=MyISAM;

Determining current action

This applies 2.0 release only

Whenever plugin.php is called from one of the core tasks (installer/upgrader/uninstaller/plugin scanner) the array variable $plug is set. Fields which are present depend on the circumstances. In most cases the relevant entries from the plugin_table database entry are included if the plugin has been installed.

In many cases $plug['plug_action'] is set to indicate the current action:

  • 'scan' - plugin scanner
  • 'install' - a fresh install is to be performed

This information is sometimes relevant to assist plugin.php in determining the values to return.

Determining if a plugin is installed

2.0 Version

To check whether a plugin is installed, call:

e107::isInstalled($plugname);

where $plugname is the plugin name (directory name).

The function returns TRUE if installed, FALSE otherwise.


0.7.9 to 1.0.x Versions

This applies to 0.7.9 and above

All installed plugins are entered into the $pref['plug_installed'] array - the array index is the path of the plugin (as a directory name) and the data is the current version.

So, the contents of a typical array might be:

Code: Sample $pref['plug_installed']
$pref['plug_installed'] = Array
(
    [calendar_menu] => 3.7b
    [content] => 1.24
    [featurebox] => 1.0
    [forum] => 1.2
    [gsitemap] => 1.0
    [links_page] => 1.12
    [linkwords] => 1.0
    [list_new] => 1.0
    [log] => 2.0
    [poll] => 2.0
 )

This information can be used in two ways:

  • To check whether a plugin is installed, use isset($pref['plug_installed']['calendar_menu']) for example.
  • To test the installed version of a plugin. Note that the version string text will be exactly as specified in plugin.php.

Writing safe plugins

Read Wiki article integrate with e107 for safety tips.