xref: /dokuwiki/lib/plugins/styling/action.php (revision 26dfc2323f8f70cb69aac4c8c51bf7997809f2ca)
1<?php
2use dokuwiki\Extension\ActionPlugin;
3use dokuwiki\Extension\EventHandler;
4use dokuwiki\Extension\Event;
5/**
6 * DokuWiki Plugin styling (Action Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Andreas Gohr <andi@splitbrain.org>
10 */
11class action_plugin_styling extends ActionPlugin
12{
13
14    /**
15     * Registers a callback functions
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(EventHandler $controller)
21    {
22        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleHeader');
23    }
24
25    /**
26     * Adds the preview parameter to the stylesheet loading in non-js mode
27     *
28     * @param Doku_Event $event  event object by reference
29     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
30     *                           handler was registered]
31     * @return void
32     */
33    public function handleHeader(Event &$event, $param)
34    {
35        global $ACT;
36        global $INPUT;
37        if ($ACT != 'admin' || $INPUT->str('page') != 'styling') return;
38        /** @var admin_plugin_styling $admin */
39        $admin = plugin_load('admin', 'styling');
40        if (!$admin->isAccessibleByCurrentUser()) return;
41
42        // set preview
43        $len = count($event->data['link']);
44        for ($i = 0; $i < $len; $i++) {
45            if (
46                $event->data['link'][$i]['rel'] == 'stylesheet' &&
47                strpos($event->data['link'][$i]['href'], 'lib/exe/css.php') !== false
48            ) {
49                $event->data['link'][$i]['href'] .= '&preview=1&tseed='.time();
50            }
51        }
52    }
53}
54
55// vim:ts=4:sw=4:et:
56