1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7/**
8 * DokuWiki Plugin googleconsentmananger (Action Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author i-net /// software <tools@inetsoftware.de>
12 */
13class action_plugin_googleconsentmananger extends ActionPlugin
14{
15    const GTMID = 'GTMID';
16
17    /** @inheritDoc */
18    public function register(EventHandler $controller)
19    {
20        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleTplMetaheaderOutput');
21        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handleDokuwikiStarted');
22    }
23
24    /**
25     * Event handler for DOKUWIKI_STARTED
26     *
27     * @see https://www.dokuwiki.org/devel:events:DOKUWIKI_STARTED
28     * @param Event $event Event object
29     * @param mixed $param optional parameter passed when event was registered
30     * @return void
31     */
32    public function handleDokuwikiStarted(Event $event, $param)
33    {
34        global $JSINFO;
35        global $conf;
36        $JSINFO['plugins']['googleconsent'] = array (
37            'template' => $conf['template'],
38        );
39        $this->addConfig( 'acceptBody' );
40        $this->addConfig( 'acceptButton' );
41        $this->addConfig( 'declineButton' );
42        $this->addConfig( 'policyButton' );
43        $this->addConfig( 'policyURL' );
44        $this->addConfig( 'acceptOnContinue' );
45        $this->addConfig( 'acceptOnScroll' );
46        $this->addConfig( 'acceptAnyClick' );
47        $this->addConfig( 'expireDays' );
48        $this->addConfig( 'renewOnVisit' );
49        $this->addConfig( 'forceShow' );
50    }
51
52    private function addConfig( $valueName ) {
53        global $JSINFO;
54        $value = $this->getConf($valueName);
55        if ( !empty( $value ) ) {
56            $JSINFO['plugins']['googleconsent'][$valueName] = $value;
57        }
58    }
59
60    /**
61     * Event handler for TPL_METAHEADER_OUTPUT
62     *
63     * @see https://www.dokuwiki.org/devel:events:TPL_METAHEADER_OUTPUT
64     * @param Event $event Event object
65     * @param mixed $param optional parameter passed when event was registered
66     * @return void
67     */
68    public function handleTplMetaheaderOutput(Event $event, $param)
69    {
70        $GTMID = $this->getConf(self::GTMID);
71        if(!$GTMID) {
72            return;
73        }
74
75        $event->data['noscript'][] = array (
76            '_data' => '<iframe src="//www.googletagmanager.com/ns.html?id='.$this->getConf(self::GTMID).'" height="0" width="0" style="display:none;visibility:hidden"></iframe>',
77        );
78        $event->data['script'][] = array (
79            'type' => 'text/javascript',
80            '_data' => "
81            window.dataLayer = window.dataLayer || [];
82            function gtag() { dataLayer.push(arguments); }
83            gtag('config', '${GTMID}');
84            gtag('consent', 'default', {
85                'ad_user_data': 'denied',
86                'ad_personalization': 'denied',
87                'ad_storage': 'denied',
88                'analytics_storage': 'denied',
89                'wait_for_update': 500,
90            });
91            gtag('js', new Date());
92            ",
93        );
94    }
95}
96