Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dev:extensions:plugins [2014/02/20 10:51]
mistic100 [Plugin architecture]
— (current)
Line 1: Line 1:
-====== Plugin developpement ====== 
  
-Plugins are designed to extend the Piwigo features. 
- 
-If you want to create a plugin, first read [[plugin:plugin_tutorial1]] 
- 
-===== Plugin architecture ===== 
- 
-A plugin can hook into the core of Piwigo by registering **event handlers** with Piwigo. An **event handler** is nothing more than a function that Piwigo will call. 
-<code php>register_event_handler('delete_user', 'my_action_on_delete_user');</code> 
-In this case, 'delete_user' is the name of the event to which the plugin will hook. Piwigo will automatically call your function when a user is deleted. 
- 
-Piwigo will trigger **notices** and **changes**. The only difference from a coding perspective is that you are required to return a value in the case of an event (this return value will be used later by Piwigo).  
- 
-An example of **trigger_notify** is 'delete_user' or 'delete_elements'. Your plugin can take any required actions when a user is deleted by registering an event handler for 'delete_user'. While handling actions you are not expected to directly return a value that will be used by Piwigo, but you can modify any global variable in order to change the general behaviour. 
- 
-An example of **trigger_change** is 'render_user_comment_content'. Piwigo will give in input the raw content of a comment, you can modify it at your will (for example allow bbcode or add smilies) and return the value: 
-<code php> 
-register_event_handler('render_user_comment_content', 'my_function'); 
-function my_function($content) 
-{ 
-  $content = str_replace(':)', '<img src="http://example.com/icon_smile.gif" alt=":)" />', $content); 
-  return $content; //<-- NOTE: this is how you return the value to Piwigo  
-} 
-</code> 
- 
-A complete reference of core triggers is available at **tool/triggers_list.php** into your Piwigo installation, or [[http://piwigo.us/dev/mistic100/triggers|here]] (not always up to date). 
- 
- 
- 
-===== Taxonomy of a plugin ===== 
-The minimal plugin is a file called main.inc.php (in the directory plugins/my_plugin if your plugin is called my_plugin). It MUST have the header below: 
-<code php> 
-<?php /* 
-Version: 1.0 
-Plugin Name: Short Name (shown on the plugins management page) 
-Plugin URI: http://www.somesite.com/where_i_keep_the_plugin_documentation_and_download_links 
-Author: My Name 
-Description: This a little bit longer description is also shown on the plugins management page 
-*/ 
- 
-// REAL PLUGIN CODE GOES HERE 
-?> 
-</code> 
-Note that the version number is saved into database, so keep it short and in a good style ( 1.1 or 1.a.b ...) 
- 
-===== DO and DONT ===== 
-While nobody will impose a coding standard here are some things you should consider: 
- 
- 
-==== Name collisions ==== 
-PHP does not allow several function names to be defined at the same time. Keep in mind that your plugin will need to work with other plugins activated, as well as the core of Piwigo, so please avoid function names such as 'send', 'delete', 'delete_user'... You have two options: 
- 
-A. Prefix function names with something unique: 'download_multi_delete_user', 'download_multi_delete_elements', etc... 
- 
-B. Use objects and classes 
-<code php> 
-// class EXAMPLE 
-  class MyClass 
-  { 
-    function on_delete_user() { ... } 
-  } 
-  add_event_handler('delete_user', array('MyClass', 'on_delete_user') ); 
-// object EXAMPLE 
-  class MyClass 
-  { 
-    function on_delete_user() { ... } 
-  } 
-//  New  object creation 
-  $myObj = new MyClass(); 
-// I call the methode 'on_delete_user' in the new object  
-  add_event_handler('delete_user', array(&$myObj, 'on_delete_user') ); 
-</code> 
- 
-==== Extensions compatibility ==== 
-If your plugin adds or load custom tpl, to the public part, you should let to the theme the ability to define their own tpl file. 
-If your plugin defines a new index page, like index.php?/xxxx, $page['body_id'] and $page['section'] needs to be defined with custom values by the plugin -only recommended for $page['title']-, using the loc_end_section_init trigger. This is useful for other plugins and themes, in order to declare what type of page is currently running. And for the title -which should be translated-, it's only for SEO. 
- 
- 
-==== Code size ==== 
-When Piwigo loads the plugins, it will include every main.inc.php. Don't put in your main.inc.php 3000 lines of code just to add a page on the administration menu. Split you main.inc.php in several files and feel free to add include_once inside functions defined in main.inc.php as much as you want. 
- 
- 
-==== Do not 'die()' ==== 
-If you call functions such as die or exit within your plugin, keep in mind that the final user will have no other choice than modify his config file so that no plugin is loaded or modify the #plugins table (if he knows that the cause is your plugin). So please whenever something is wrong, the plugins should display a message for the administrators and silently do nothing. 
- 
- 
- 
- 
- 
- 
- 
-==== What can be done when main.inc.php is executed ?  ==== 
-Piwigo will include main.inc.php sometime during the initialization. At this time, do not make any assumption that the user is logged in, the language is defined, the template is created, etc. The only safe assumptions are:  
-  * the connection to the database has been established  
-  * the configuration has been loaded 
-  * you can safely call add_event_handler, remove_event_handler or set_plugin_data. For all other cases, you must hook the event 'init'. The 'init'  event is triggered only once and at that point the common initialization of the current page is finished 
-  * the variable $plugin['id'] is defined and contains the identifier of your plugin. You will need this id when calling set_plugin_data 
- 
-===== Quick Tips & tricks ===== 
-==== Add content to html <head> element output ==== 
-<code php> 
-function my_add() 
-{ 
-  global $template; 
-  $url_to_me = get_root_url().'plugins/my_plugin/'; 
-  // add a stylesheet 
-  $template->assign_block_vars('head_element', array ( 
-    'CONTENT' => '<link rel="stylesheet" type="text/css" href="'.$url_to_me.'style.css">' 
-    )); 
-  // add a javascript 
-  $template->assign_block_vars('head_element', array ( 
-    'CONTENT' => '<script type="text/javascript" src="'.$url_to_me.'my_script.js"></script>' 
-    )); 
-} 
- 
-add_event_handler('loc_end_page_header', 'my_add' ); 
-</code> 
- 
- 
- 
-==== Add content to the administration page ==== 
-In your file plugins/my_plugin/main.inc.php 
-<code php> 
-add_event_handler('get_admin_plugin_menu_links', 'pwg_wants_menu' ); 
- 
-function pwg_wants_menu($menu) 
-{ 
-  array_push($menu, 
-      array( 
-        'NAME' => 'The title to be displayed', 
-        'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/the_super_admin_page.php') 
-      ) 
-    ); 
-  return $menu; 
-} 
-</code> 
- 
-In your file plugins/my_plugin/the_super_admin_page.php 
-<code php> 
-if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); 
-global $template; 
-$template->set_filenames( array( 
-  'something_here' => dirname(__FILE__).'/admin.tpl' //<- some template in plugin directory 
-) ); 
-... DO WHATEVER YOU WANT AND CALL $template->assign_... 
-$template->assign_var_from_handle( 'ADMIN_CONTENT', 'something_here'); 
-} 
-</code> 
- 
-In the same way you can add links to the advanced features page (event name is 'get_admin_advanced_features_links') or maintenance page (event name is 'get_admin_maintenance_links'). 
- 
- 
-==== Plugin maintenance ==== 
- 
-When a plugin is installed, activated, deactivated or uninstalled, Piwigo will allow you to take some special steps by executing some php code. If you have a php file named **maintain.inc.php** in your plugin directory, Piwigo will include this file and will call some maintenance functions. 
- 
-=== Piwigo 2.6 and above === 
-Maintenances functions have to be declared in a class extending **PluginMaintain** : 
-<code php> 
-/** 
- * PLUGINID must be replaced by the directory name of your plugin 
- */ 
-class PLUGINID_maintain extends PluginMaintain 
-{ 
-  function install($plugin_version, &$errors=array()) { ... } 
-  function activate($plugin_version, &$errors=array()) { ... } 
-  function deactivate() { ... } 
-  function uninstall() { ... } 
-} 
-</code> 
- 
-=== Piwigo before 2.6 === 
-Maintenance functions are declared inline and are all optional : 
-<code php> 
-function plugin_install($plugin_id, $plugin_version, &$errors) { ... } 
-function plugin_activate($plugin_id, $plugin_version, &$errors) { ... } 
-function plugin_deactivate($plugin_id) { ... } 
-function plugin_uninstall($plugin_id) { ... } 
-</code> 
- 
-**Note:** For Piwigo 2.6 both architecture are possible, in Piwigo 2.7 only the class architecture will be valid. 
 
Back to top
dev/extensions/plugins.1392893496.txt.gz · Last modified: 2014/02/20 10:51 by mistic100
 
 
github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact