xref: /plugin/combo/action/metatwitter.php (revision 5f891b7e09648e05e78f5882f3fdde1e9df9b0f1)
1<?php
2
3use ComboStrap\Image;
4use ComboStrap\LogUtility;
5use ComboStrap\MetadataUtility;
6use ComboStrap\PluginUtility;
7use ComboStrap\Page;
8use ComboStrap\Site;
9use ComboStrap\StringUtility;
10
11if (!defined('DOKU_INC')) die();
12
13require_once(__DIR__ . '/../class/Site.php');
14
15/**
16 *
17 * For the canonical meta, see {@link action_plugin_combo_metacanonical}
18 * https://github.com/twbs/bootstrap/blob/v4-dev/site/layouts/partials/social.html
19 *
20 * TODO: https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/overview
21 */
22class action_plugin_combo_metatwitter extends DokuWiki_Action_Plugin
23{
24
25
26    /**
27     * The handle name
28     */
29    const CONF_TWITTER_SITE_HANDLE = "twitterSiteHandle";
30    /**
31     * The handle id
32     */
33    const CONF_TWITTER_SITE_ID = "twitterSiteId";
34    /**
35     * The image
36     */
37    const CONF_DEFAULT_TWITTER_IMAGE = "defaultTwitterImage";
38
39    /**
40     * Don't track
41     */
42    const CONF_TWITTER_DONT_NOT_TRACK = "twitter:dnt";
43    const CONF_DONT_NOT_TRACK = "twitter:dnt";
44    const CONF_ON = "on";
45    const CONF_OFF = "off";
46
47    /**
48     * The creation ie (combostrap)
49     */
50    const COMBO_STRAP_TWITTER_HANDLE = "@combostrapweb";
51    const COMBO_STRAP_TWITTER_ID = "1283330969332842497";
52    const CANONICAL = "twitter";
53
54
55
56    function __construct()
57    {
58        // enable direct access to language strings
59        // ie $this->lang
60        $this->setupLocale();
61    }
62
63    public function register(Doku_Event_Handler $controller)
64    {
65        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'metaTwitterProcessing', array());
66    }
67
68    /**
69     *
70     * @param $event
71     */
72    function metaTwitterProcessing($event)
73    {
74
75        global $ID;
76        if (empty($ID)) {
77            // $ID is null for media
78            return;
79        }
80
81
82        $page = new Page($ID);
83
84        /**
85         * No social for bars
86         */
87        if ($page->isBar()) {
88            return;
89        }
90
91
92        // https://datacadamia.com/marketing/twitter#html_meta
93        // https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/markup
94        // https://cards-dev.twitter.com/validator
95
96        $twitterMeta = array(
97            "twitter:card" => "summary",
98            "twitter:title" => StringUtility::truncateString($page->getTitleNotEmpty(), 70),
99            "twitter:description" => StringUtility::truncateString($page->getDescription(), 200),
100            "twitter:creator" => self::COMBO_STRAP_TWITTER_HANDLE,
101            "twitter:creator:id" => self::COMBO_STRAP_TWITTER_ID
102        );
103
104
105        /**
106         * Twitter site
107         */
108        $siteTwitterHandle = PluginUtility::getConfValue(self::CONF_TWITTER_SITE_HANDLE);
109        $siteTwitterId = PluginUtility::getConfValue(self::CONF_TWITTER_SITE_ID);
110        if (!empty($siteTwitterHandle)) {
111            $twitterMeta["twitter:site"] = $siteTwitterHandle;
112
113            // Identify the Twitter profile of the page that populates the via property
114            // https://developer.twitter.com/en/docs/twitter-for-websites/webpage-properties
115            $name = str_replace("@","",$siteTwitterHandle);
116            $event->data['link'][] = array("rel" => "me", "href" => "https://twitter.com/$name");
117        }
118        if (!empty($siteTwitterId)) {
119            $twitterMeta["twitter:site:id"] = $siteTwitterId;
120        }
121
122        /**
123         * Card image
124         */
125        $twitterImages = $page->getImageSet();
126        if (empty($twitterImages)) {
127            $defaultImageIdConf = cleanID(PluginUtility::getConfValue(self::CONF_DEFAULT_TWITTER_IMAGE));
128            if (!empty($defaultImageIdConf)) {
129                $twitterImage = new Image($defaultImageIdConf);
130                if ($twitterImage->exists()) {
131                    $twitterImages[] = $twitterImage;
132                } else {
133                    if ($defaultImageIdConf != "apple-touch-icon.png") {
134                        LogUtility::msg("The default twitter image ($defaultImageIdConf) does not exist", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
135                    }
136                }
137            }
138
139        }
140        if (!empty($twitterImages)) {
141            foreach ($twitterImages as $twitterImage) {
142                if ($twitterImage->exists()) {
143                    $twitterMeta["twitter:image"] = $twitterImage->getUrl();
144                    if (!empty($twitterImage->getAlt())) {
145                        $twitterMeta["twitter:image:alt"] = $twitterImage->getAlt();
146                    }
147                    // One image only
148                    break;
149                }
150            }
151        }
152
153        /**
154         * https://developer.twitter.com/en/docs/twitter-for-websites/webpage-properties
155         */
156        // don;t track
157        $twitterMeta["twitter:dnt"]=PluginUtility::getConfValue(self::CONF_TWITTER_DONT_NOT_TRACK);
158        // turn off csp warning
159        $twitterMeta["twitter:widgets:csp"]="on";
160
161        /**
162         * Embedded Tweet Theme
163         */
164
165        $twitterMeta["twitter:widgets:theme"]=PluginUtility::getConfValue(syntax_plugin_combo_blockquote::CONF_TWEET_WIDGETS_THEME);
166        $twitterMeta["twitter:widgets:border-color"]=PluginUtility::getConfValue(syntax_plugin_combo_blockquote::CONF_TWEET_WIDGETS_BORDER);
167
168        /**
169         * Add the properties
170         */
171        foreach ($twitterMeta as $key => $content) {
172            $event->data['meta'][] = array("name" => $key, "content" => $content);
173        }
174
175
176
177    }
178
179}
180