DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:plugin_programming_tips

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
devel:plugin_programming_tips [2023-09-20 23:01] – [User lists and info] Klap-indevel:plugin_programming_tips [2023-09-20 23:34] (current) Klap-in
Line 195: Line 195:
 Create an [[Action Plugin]] which should contain: Create an [[Action Plugin]] which should contain:
 <code php lib/plugin/example/action.php> <code php lib/plugin/example/action.php>
-/* +use dokuwiki\Extension\ActionPlugin
- * plugin should use this method to register its handlers  +use dokuwiki\Extension\EventHandler
- * with the dokuwiki's event controller +use dokuwiki\Extension\Event;
- */ +
-function register(Doku_Event_Handler $controller) { +
-    $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,'_ajax_call')+
-+
-     +
-/** +
- * handle ajax requests +
- */ +
-function _ajax_call(Doku_Event $event, $param) { +
-    if ($event->data !== 'plugin_example') { +
-        return+
-    } +
-    //no other ajax call handlers needed +
-    $event->stopPropagation(); +
-    $event->preventDefault();+
  
-    //e.g. access additional request variables +class action_plugin_example extends ActionPlugin { 
-    global $INPUT; //available since release 2012-10-13 "Adora Belle" + 
-    $name = $INPUT->str('name');+    /** 
 +     * plugin should use this method to register its handlers  
 +     * with the dokuwiki's event controller 
 +     *
 +    public function register(EventHandler $controller) { 
 +        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajaxCall'); 
 +    }
          
-    //data +    /** 
-    $data = array();+     * handle ajax requests 
 +     *
 +    public function ajaxCall(Event $event) { 
 +        if ($event->data !== 'plugin_example') { 
 +            return; 
 +        } 
 +        //no other ajax call handlers needed 
 +        $event->stopPropagation(); 
 +        $event->preventDefault();
  
-    //json library of DokuWiki +        //e.g. access additional request variables 
-    $json new JSON();+        global $INPUT; 
 +        $name $INPUT->str('name');
          
-    //set content type +        //data 
-    header('Content-Type: application/json'); +        $data = []; 
-    echo $json->encode($data);+ 
 +        //set content type 
 +        header('Content-Type: application/json'); 
 +        echo json_encode($data); 
 +    }
 } }
 </code> </code>
Line 276: Line 280:
 Example: Example:
 <code php> <code php>
 +global $INPUT;
 +
 // we are parsing a submitted comment... // we are parsing a submitted comment...
-if (isset($_REQUEST['comment'])) {+if ($INPUT->has('comment')) {
     return false;     return false;
 } }
Line 289: Line 295:
 The [[plugin:popularity]] plugin already gather the number of time a plugin is installed on an instance of Dokuwiki. The [[plugin:popularity]] plugin already gather the number of time a plugin is installed on an instance of Dokuwiki.
  
-It also let the possibility to developers, to send more data about usage. It can be used by plugins developers to know if a given obsolete feature is still used. To do it, you need to subscribe to the [[devel:event:PLUGIN_POPULARITY_DATA_SETUP|PLUGIN_POPULARITY_DATA_SETUP]] event. This event contains a key-value array. You should add a key which is the name of your plugin. The value should be either a string, or a key-value array itself (in this latter case, the data will be sent with the key ''<plugin-name>_<key>'')+It also let the possibility to developers, to send more data about usage. It can be used by plugins developers to know if a given obsolete feature is still used. To do it, you need to subscribe to the [[devel:event:PLUGIN_POPULARITY_DATA_SETUP]] event. This event contains a key-value array. You should add a key which is the name of your plugin. The value should be either a string, or a key-value array itself (in this latter case, the data will be sent with the key ''<plugin-name>_<key>'')
  
 Example: Example:
  
 <code php action.php> <code php action.php>
-public function register(Doku_Event_Handler $controller) { +use dokuwiki\Extension\ActionPlugin; 
-  $controller->register_hook('PLUGIN_POPULARITY_DATA_SETUP', 'AFTER', $this, 'usage_data'); +use dokuwiki\Extension\EventHandler; 
-}+use dokuwiki\Extension\Event; 
 + 
 +class action_plugin_example extends ActionPlugin { 
 +    public function register(EventHandler $controller) { 
 +        $controller->register_hook('PLUGIN_POPULARITY_DATA_SETUP', 'AFTER', $this, 'usageData'); 
 +    }
          
-public function usage_data(Doku_Event $event){ +    public function usageData(Event $event){ 
-  $event->data['my_plugin_name'] = 'my usage data';+        $event->data['my_plugin_name'] = 'my usage data';
    
- /* or:  +    /* or:  
-    $event->data['my_plugin_name'] = array ( +        $event->data['my_plugin_name'] = [ 
-                                        'k1' => 'v1',  +            'k1' => 'v1',  
-                                        'k2' => 'v2' +            'k2' => 'v2' 
-                                     )+        ]
-  */+    */ 
 +    }
 } }
 </code> </code>
  
 A plugin which uses this feature is the [[plugin:nspages]] Plugin. A plugin which uses this feature is the [[plugin:nspages]] Plugin.
devel/plugin_programming_tips.txt · Last modified: 2023-09-20 23:34 by Klap-in

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki