1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\Event;
5use dokuwiki\Extension\EventHandler;
6
7/**
8 * Invite Code Plugin
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Herbert Schiller <hbschiller@schillerapp.com>
12 */
13class action_plugin_gautoads extends ActionPlugin
14{
15
16    /**
17     * Register the event handlers
18     */
19    public function register(EventHandler $controller)
20    {
21        // inject in user registration
22		$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'hookGoogleAutoAdsScript');
23    }
24
25	/**
26     * Inject the invitation code field in the registration form
27     */
28    public function hookGoogleAutoAdsScript(Event $event)
29    {
30		$adsenseId = $this->getAdsenseId();
31
32		// Add Google AdSense script
33        $event->data['script'][] = [
34            'type' => 'text/javascript',
35            'src'  => "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-$adsenseId",
36            '_data' => '',
37            'async' => 'async',
38            'crossorigin' => 'anonymous'
39        ];
40
41        // Add inline script to push AdSense ads
42        $event->data['script'][] = [
43            'type' => 'text/javascript',
44            '_data' => "(adsbygoogle = window.adsbygoogle || []).push({});"
45        ];
46
47    }
48
49	private function getAdsenseId()
50    {
51        return $this->getConf('adsense_id');
52    }
53}