xref: /plugin/combo/action/metacanonical.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3
4use ComboStrap\Page;
5
6/**
7 * Class action_plugin_combo_metacanonical
8 * Add all canonical HTML meta
9 */
10class action_plugin_combo_metacanonical
11{
12
13
14    public function register(Doku_Event_Handler $controller)
15    {
16        /**
17         * https://www.dokuwiki.org/devel:event:tpl_metaheader_output
18         */
19        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'metaCanonicalProcessing', array());
20
21
22    }
23
24    /**
25     * Dokuwiki has already a canonical methodology
26     * https://www.dokuwiki.org/canonical
27     *
28     * @param $event
29     */
30    function metaCanonicalProcessing($event)
31    {
32
33        global $ID;
34        if (empty($ID)) {
35            // $_SERVER['SCRIPT_NAME']== "/lib/exe/mediamanager.php"
36            // $ID is null
37            return;
38        }
39
40        $page = Page::createPageFromId($ID);
41
42        /**
43         * No canonical for slot page
44         */
45        if ($page->isSecondarySlot()) {
46            return;
47        }
48
49        /**
50         * Where do we pick the canonical URL
51         */
52        /**
53         * Canonical from meta
54         *
55         * FYI: The creation of the link was extracted from
56         * {@link wl()} that call {@link idfilter()} that performs just a replacement
57         * Calling the wl function will not work because
58         * {@link wl()} use the constant DOKU_URL that is set before any test via getBaseURL(true)
59         */
60        $canonicalUrl = $page->getAbsoluteCanonicalUrl();
61
62        /**
63         * Replace the meta entry
64         *
65         * First search the key of the meta array
66         */
67        $canonicalKey = "";
68        $canonicalRelArray = array("rel" => "canonical", "href" => $canonicalUrl);
69        foreach ($event->data['link'] as $key => $link) {
70            if ($link["rel"] == "canonical") {
71                $canonicalKey = $key;
72            }
73        }
74        if ($canonicalKey != "") {
75            // Update
76            $event->data['link'][$canonicalKey] = $canonicalRelArray;
77        } else {
78            // Add
79            $event->data['link'][] = $canonicalRelArray;
80        }
81
82        /**
83         * Add the Og canonical meta
84         * https://developers.facebook.com/docs/sharing/webmasters/getting-started/versioned-link/
85         */
86        $canonicalOgKeyKey = "";
87        $canonicalPropertyKey = "og:url";
88        $canonicalOgArray = array("property" => $canonicalPropertyKey, "content" => $canonicalUrl);
89        // Search if the canonical property is already present
90        foreach ($event->data['meta'] as $key => $meta) {
91            if (array_key_exists("property", $meta)) {
92                /**
93                 * We may have several properties
94                 */
95                if ($meta["property"] == $canonicalPropertyKey) {
96                    $canonicalOgKeyKey = $key;
97                }
98            }
99        }
100        if ($canonicalOgKeyKey != "") {
101            // Update
102            $event->data['meta'][$canonicalOgKeyKey] = $canonicalOgArray;
103        } else {
104            // Add
105            $event->data['meta'][] = $canonicalOgArray;
106        }
107
108    }
109
110}
111