1<?php
2/**
3 * DokuWiki Plugin inlinedcss (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  i-net software / Gerry Weißbach <tools@inetsoftware.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_zerolinecss extends DokuWiki_Action_Plugin {
13
14    private $httpClient = null;
15
16    /**
17     * Registers a callback function for a given event
18     *
19     * @param Doku_Event_Handler $controller DokuWiki's event controller object
20     * @return void
21     */
22    public function register(Doku_Event_Handler $controller) {
23       $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_tpl_metaheader_output', null, 10000);
24    }
25
26    /**
27     * [Custom event handler which performs action]
28     *
29     * @param Doku_Event $event  event object by reference
30     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
31     *                           handler was registered]
32     * @return void
33     */
34    public function handle_tpl_metaheader_output(Doku_Event &$event, $param) {
35        foreach( $event->data['link'] as &$link ) {
36            if ( $link['rel'] != 'zerolinecss' || empty($link['href']) ) continue;
37            $this->_init_http_client();
38            $data = $this->httpClient->get( DOKU_URL . $link['href'] );
39
40            if ( !empty($data) ) {
41
42                // Print instantly
43                _tpl_metaheaders_action(array(
44                    'style' => array(array(
45                        'rel' => 'stylesheet',
46                        'type' => $link['type'],
47                        '_data' => '/* ZEROLINECSS */ ' . $data
48                    ))
49                ));
50
51                // empty original array
52                $link = array();
53            }
54        }
55    }
56
57    private function _init_http_client() {
58        if ( $this->httpClient != null ) {
59            return;
60        }
61
62        $this->httpClient = new dokuwiki\HTTP\DokuHTTPClient();
63    }
64}
65
66// vim:ts=4:sw=4:et:
67