1<?php
2/**
3 * DokuWiki Plugin jquerymigrate (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Henry Pan <dokuwiki@phy25.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class action_plugin_jquerymigrate extends DokuWiki_Action_Plugin
15{
16
17    /**
18     * Registers a callback function for a given event
19     *
20     * @param Doku_Event_Handler $controller DokuWiki's event controller object
21     *
22     * @return void
23     */
24    public function register(Doku_Event_Handler $controller)
25    {
26        $controller->register_hook('CONFUTIL_CDN_SELECT', 'AFTER', $this, 'handle_confutil_cdn_select');
27
28    }
29
30    /**
31     * [Custom event handler which performs action]
32     *
33     * Called for event:
34     *
35     * @param Doku_Event $event  event object by reference
36     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
37     *                           handler was registered]
38     *
39     * @return void
40     */
41    public function handle_confutil_cdn_select(Doku_Event $event, $param)
42    {
43        global $conf;
44        $dev_string = $conf['compress'] ? '.min' : '';
45        $version = $this->getConf('version');
46
47        // remove bundled jqm so prevent conflict
48        foreach ($event->data['src'] as $key => $value) {
49            if (strstr($value, 'jquery-migrate') !== false) {
50                unset($event->data['src'][$key]);
51            }
52        }
53
54        // add our jqm
55        if(!$conf['jquerycdn']) {
56            $event->data['src'][] = sprintf(DOKU_BASE . 'lib/plugins/jquerymigrate/jquery-migrate-3.1.0%s.js', $dev_string);
57        } elseif($conf['jquerycdn'] == 'jquery') {
58            $event->data['src'][] = sprintf('https://code.jquery.com/jquery-migrate-%s%s.js', $version, $dev_string);
59        } elseif($conf['jquerycdn'] == 'cdnjs') {
60            $event->data['src'][] = sprintf(
61                'https://cdnjs.cloudflare.com/ajax/libs/jquery-migrate/%s/jquery-migrate%s.js',
62                $version, $dev_string
63            );
64        }
65    }
66}
67