xref: /plugin/mikioplugin/action.php (revision f773e921a37561b421f9ab02166288708becbf39)
1<?php
2
3/**
4 * Mikio Plugin
5 *
6 * @version    1.0
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     James Collins <james.collins@outlook.com.au>
9 */
10
11if (!defined('DOKU_INC')) die();
12
13require_once('icons/icons.php');
14
15if (!function_exists('glob_recursive')) {
16  function glob_recursive($pattern, $flags = 0)
17  {
18    $files = glob($pattern, $flags);
19    foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
20      $files = array_merge($files, glob_recursive($dir . '/' . basename($pattern), $flags));
21    }
22    return $files;
23  }
24}
25
26class action_plugin_mikioplugin extends DokuWiki_Action_Plugin
27{
28  public function register(Doku_Event_Handler $controller)
29  {
30    $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_load');
31  }
32
33  public function _load(Doku_Event $event, $param)
34  {
35    global $conf;
36    global $MIKIO_ICONS;
37
38    $baseDir = str_replace('\\', '/', DOKU_BASE . 'lib/plugins' . str_replace(dirname(dirname(__FILE__)), '', dirname(__FILE__)) . '/');
39    $stylesheets = [];
40    $less = [];
41    $scripts = [];
42
43    if (is_array($MIKIO_ICONS)) {
44      $icons = array();
45      foreach ($MIKIO_ICONS as $icon) {
46        if (isset($icon['name']) && isset($icon['css']) && isset($icon['insert'])) {
47          $icons[] = $icon;
48
49          if ($icon['css'] != '') {
50            if (strpos($icon['css'], '//') === FALSE) {
51              $stylesheets[] = $baseDir . 'icons/' . $icon['css'];
52            } else {
53              $stylesheets[] = $icon['css'];
54            }
55          }
56        }
57      }
58      $MIKIO_ICONS = $icons;
59    } else {
60      $MIKIO_ICONS = [];
61    }
62
63    $stylesList = glob_recursive(str_replace('\\', '/', 'lib/plugins' . str_replace(dirname(dirname(__FILE__)), '', dirname(__FILE__)) . '/styles/*'));
64    if ($stylesList !== FALSE) {
65      foreach ($stylesList as $value) {
66        $filename = strtolower($value);
67        if (substr($filename, -5) == '.less' || substr($filename, -5) == '.css') {
68          $stylesheets[] = '/' . $filename;
69        }
70      }
71    }
72
73    $stylesheets = array_unique($stylesheets);
74
75    // css
76    foreach ($stylesheets as $style) {
77      if (strtolower(substr($style, -5)) == '.less') {
78        $less[] = $style;
79      } else {
80        array_unshift($event->data['link'], array(
81          'type' => 'text/css',
82          'rel'  => 'stylesheet',
83          'href' => $style
84        ));
85      }
86    }
87
88    // less
89    array_unshift($less, '/assets/variables.less', '/assets/styles.less');
90
91    $lessSorted = [];
92    foreach ($less as $key => $value) {
93      if (substr(strtolower($value), -14) == 'variables.less') {
94        $lessSorted[] = $value;
95        unset($less[$key]);
96      }
97    }
98
99    $lessSorted = array_merge($lessSorted, $less);
100    $lessPath = implode(',', $lessSorted);
101
102    array_unshift($event->data['link'], array(
103      'type' => 'text/css',
104      'rel'  => 'stylesheet',
105      'href' => $baseDir . 'css.php?css=' . str_replace($baseDir, '', $lessPath)
106    ));
107
108    // js
109    foreach ($scripts as $script) {
110      $event->data['script'][] = array(
111        'type'  => 'text/javascript',
112        '_data' => '',
113        'src'   => $script
114      );
115    }
116  }
117}
118