]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - plugins/demo_plugin/demo_plugin.php
Plugin system: allow plugins to provide custom routes
[github/shaarli/Shaarli.git] / plugins / demo_plugin / demo_plugin.php
index 71ba7495ce9695bd948b52f741dfe2bb5f53da18..15cfc2c51cd4889cc0d38f821e0ab856806a4167 100644 (file)
@@ -1,11 +1,14 @@
 <?php
+
 /**
  * Demo Plugin.
  *
- * This plugin try to cover Shaarli's plugin API entirely.
- * Can be used by plugin developper to make their own.
+ * This plugin tries to completely cover Shaarli's plugin API.
+ * Can be used by plugin developers to make their own plugin.
  */
 
+require_once __DIR__ . '/DemoPluginController.php';
+
 /*
  * RENDER HEADER, INCLUDES, FOOTER
  *
@@ -16,7 +19,7 @@
 
 use Shaarli\Config\ConfigManager;
 use Shaarli\Plugin\PluginManager;
-use Shaarli\Router;
+use Shaarli\Render\TemplatePage;
 
 /**
  * In the footer hook, there is a working example of a translation extension for Shaarli.
@@ -59,9 +62,20 @@ function demo_plugin_init($conf)
     return $errors;
 }
 
+function demo_plugin_register_routes(): array
+{
+    return [
+        [
+            'method' => 'GET',
+            'route' => '/custom',
+            'callable' => 'Shaarli\DemoPlugin\DemoPluginController:index',
+        ],
+    ];
+}
+
 /**
  * Hook render_header.
- * Executed on every page redering.
+ * Executed on every page render.
  *
  * Template placeholders:
  *   - buttons_toolbar
@@ -74,7 +88,7 @@ function demo_plugin_init($conf)
 function hook_demo_plugin_render_header($data)
 {
     // Only execute when linklist is rendered.
-    if ($data['_PAGE_'] == Router::$PAGE_LINKLIST) {
+    if ($data['_PAGE_'] == TemplatePage::LINKLIST) {
         // If loggedin
         if ($data['_LOGGEDIN_'] === true) {
             /*
@@ -82,14 +96,14 @@ function hook_demo_plugin_render_header($data)
              * A link is an array of its attributes (key="value"),
              * and a mandatory `html` key, which contains its value.
              */
-            $button = array(
-                'attr' => array (
+            $button = [
+                'attr' =>  [
                     'href' => '#',
                     'class' => 'mybutton',
                     'title' => 'hover me',
-                ),
+                ],
                 'html' => 'DEMO buttons toolbar',
-            );
+            ];
             $data['buttons_toolbar'][] = $button;
         }
 
@@ -115,29 +129,29 @@ function hook_demo_plugin_render_header($data)
          *   <input input-2-attribute-1="input 2 attribute 1 value">
          * </form>
          */
-        $form = array(
-            'attr' => array(
+        $form = [
+            'attr' => [
                 'method' => 'GET',
-                'action' => '?',
+                'action' => $data['_BASE_PATH_'] . '/',
                 'class' => 'addform',
-            ),
-            'inputs' => array(
-                array(
+            ],
+            'inputs' => [
+                [
                     'type' => 'text',
                     'name' => 'demo',
                     'placeholder' => 'demo',
-                )
-            )
-        );
+                ]
+            ]
+        ];
         $data['fields_toolbar'][] = $form;
     }
     // Another button always displayed
-    $button = array(
-        'attr' => array(
+    $button = [
+        'attr' => [
             'href' => '#',
-        ),
+        ],
         'html' => 'Demo',
-    );
+    ];
     $data['buttons_toolbar'][] = $button;
 
     return $data;
@@ -145,7 +159,7 @@ function hook_demo_plugin_render_header($data)
 
 /**
  * Hook render_includes.
- * Executed on every page redering.
+ * Executed on every page render.
  *
  * Template placeholders:
  *   - css_files
@@ -169,7 +183,7 @@ function hook_demo_plugin_render_includes($data)
 
 /**
  * Hook render_footer.
- * Executed on every page redering.
+ * Executed on every page render.
  *
  * Template placeholders:
  *   - text
@@ -186,8 +200,8 @@ function hook_demo_plugin_render_includes($data)
  */
 function hook_demo_plugin_render_footer($data)
 {
-    // footer text
-    $data['text'][] = '<br>'. demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.');
+    // Footer text
+    $data['text'][] = '<br>' . demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.');
 
     // Free elements at the end of the page.
     $data['endofpage'][] = '<marquee id="demo_marquee">' .
@@ -229,13 +243,13 @@ function hook_demo_plugin_render_linklist($data)
      * and a mandatory `html` key, which contains its value.
      * It's also recommended to add key 'on' or 'off' for theme rendering.
      */
-    $action = array(
-        'attr' => array(
+    $action = [
+        'attr' => [
             'href' => '?up',
             'title' => 'Uppercase!',
-        ),
+        ],
         'html' => '←',
-    );
+    ];
 
     if (isset($_GET['up'])) {
         // Manipulate link data
@@ -275,9 +289,9 @@ function hook_demo_plugin_render_linklist($data)
 function hook_demo_plugin_render_editlink($data)
 {
     // Load HTML into a string
-    $html = file_get_contents(PluginManager::$PLUGINS_PATH .'/demo_plugin/field.html');
+    $html = file_get_contents(PluginManager::$PLUGINS_PATH . '/demo_plugin/field.html');
 
-    // replace value in HTML if it exists in $data
+    // Replace value in HTML if it exists in $data
     if (!empty($data['link']['stuff'])) {
         $html = sprintf($html, $data['link']['stuff']);
     } else {
@@ -303,7 +317,11 @@ function hook_demo_plugin_render_editlink($data)
 function hook_demo_plugin_render_tools($data)
 {
     // field_plugin
-    $data['tools_plugin'][] = 'tools_plugin';
+    $data['tools_plugin'][] = '<div class="tools-item">
+        <a href="' . $data['_BASE_PATH_'] . '/plugin/demo_plugin/custom">
+          <span class="pure-button pure-u-lg-2-3 pure-u-3-4">Demo Plugin Custom Route</span>
+        </a>
+      </div>';
 
     return $data;
 }
@@ -324,9 +342,7 @@ function hook_demo_plugin_render_tools($data)
  */
 function hook_demo_plugin_render_picwall($data)
 {
-    // plugin_start_zone
     $data['plugin_start_zone'][] = '<center>BEFORE</center>';
-    // plugin_end_zone
     $data['plugin_end_zone'][] = '<center>AFTER</center>';
 
     return $data;
@@ -348,9 +364,7 @@ function hook_demo_plugin_render_picwall($data)
  */
 function hook_demo_plugin_render_tagcloud($data)
 {
-    // plugin_start_zone
     $data['plugin_start_zone'][] = '<center>BEFORE</center>';
-    // plugin_end_zone
     $data['plugin_end_zone'][] = '<center>AFTER</center>';
 
     return $data;
@@ -372,9 +386,7 @@ function hook_demo_plugin_render_tagcloud($data)
  */
 function hook_demo_plugin_render_daily($data)
 {
-    // plugin_start_zone
     $data['plugin_start_zone'][] = '<center>BEFORE</center>';
-    // plugin_end_zone
     $data['plugin_end_zone'][] = '<center>AFTER</center>';
 
 
@@ -447,9 +459,9 @@ function hook_demo_plugin_delete_link($data)
 function hook_demo_plugin_render_feed($data)
 {
     foreach ($data['links'] as &$link) {
-        if ($data['_PAGE_'] == Router::$PAGE_FEED_ATOM) {
+        if ($data['_PAGE_'] == TemplatePage::FEED_ATOM) {
             $link['description'] .= ' - ATOM Feed' ;
-        } elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) {
+        } elseif ($data['_PAGE_'] == TemplatePage::FEED_RSS) {
             $link['description'] .= ' - RSS Feed';
         }
     }
@@ -465,7 +477,8 @@ function hook_demo_plugin_render_feed($data)
  */
 function hook_demo_plugin_save_plugin_parameters($data)
 {
-    // Here we edit the provided value, but we can use this to generate config files, etc.
+    // Here we edit the provided value.
+    // This hook can also be used to generate config files, etc.
     if (! empty($data['DEMO_PLUGIN_PARAMETER']) && ! endsWith($data['DEMO_PLUGIN_PARAMETER'], '_SUFFIX')) {
         $data['DEMO_PLUGIN_PARAMETER'] .= '_SUFFIX';
     }