lang $this->setupLocale(); } public function register(Doku_Event_Handler $controller) { $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'metaTwitterProcessing', array()); } /** * * @param $event */ function metaTwitterProcessing($event) { global $ID; if (empty($ID)) { // $ID is null for media return; } $page = Page::createPageFromId($ID); if (!$page->exists()) { return; } /** * No social for bars */ if ($page->isSecondarySlot()) { return; } // https://datacadamia.com/marketing/twitter#html_meta // https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/markup // https://cards-dev.twitter.com/validator $twitterMeta = array( self::META_CARD => "summary", self::META_TITLE => StringUtility::truncateString($page->getTitleOrDefault(), 70), self::META_CREATOR => self::COMBO_STRAP_TWITTER_HANDLE, self::META_CREATOR_ID => self::COMBO_STRAP_TWITTER_ID ); $description = $page->getDescriptionOrElseDokuWiki(); if (!empty($description)) { // happens in test with document without content $twitterMeta[self::META_DESCRIPTION] = StringUtility::truncateString($description, 200); } /** * Twitter site */ $siteTwitterHandle = PluginUtility::getConfValue(self::CONF_TWITTER_SITE_HANDLE); $siteTwitterId = PluginUtility::getConfValue(self::CONF_TWITTER_SITE_ID); if (!empty($siteTwitterHandle)) { $twitterMeta[self::META_SITE] = $siteTwitterHandle; // Identify the Twitter profile of the page that populates the via property // https://developer.twitter.com/en/docs/twitter-for-websites/webpage-properties $name = str_replace("@", "", $siteTwitterHandle); $event->data['link'][] = array("rel" => "me", "href" => "https://twitter.com/$name"); } if (!empty($siteTwitterId)) { $twitterMeta[self::META_SITE_ID] = $siteTwitterId; } /** * Card image */ $twitterImages = $page->getImagesOrDefaultForTheFollowingUsages([PageImageUsage::TWITTER, PageImageUsage::ALL, PageImageUsage::SOCIAL]); if (empty($twitterImages)) { $defaultImageIdConf = PluginUtility::getConfValue(self::CONF_DEFAULT_TWITTER_IMAGE); if (!empty($defaultImageIdConf)) { $twitterImage = Image::createImageFromId($defaultImageIdConf); if ($twitterImage->exists()) { $twitterImages[] = $twitterImage; } else { if ($defaultImageIdConf != ":apple-touch-icon.png") { LogUtility::msg("The default twitter image ($defaultImageIdConf) does not exist", LogUtility::LVL_MSG_ERROR, self::CANONICAL); } } } } if (!empty($twitterImages)) { foreach ($twitterImages as $twitterImage) { if ($twitterImage->exists()) { $twitterMeta[self::META_IMAGE] = $twitterImage->getAbsoluteUrl(); $title = $twitterImage->getAltNotEmpty(); if (!empty($title)) { $twitterMeta[self::META_IMAGE_ALT] = $title; } // One image only break; } } } /** * https://developer.twitter.com/en/docs/twitter-for-websites/webpage-properties */ // don't track $twitterMeta[self::META_DNT] = PluginUtility::getConfValue(self::CONF_TWITTER_DONT_NOT_TRACK); // turn off csp warning $twitterMeta[self::META_WIDGET_CSP] = "on"; /** * Embedded Tweet Theme */ $twitterMeta[self::META_WIDGETS_THEME] = PluginUtility::getConfValue(syntax_plugin_combo_blockquote::CONF_TWEET_WIDGETS_THEME); $twitterMeta[self::META_WIDGETS_BORDER_COLOR] = PluginUtility::getConfValue(syntax_plugin_combo_blockquote::CONF_TWEET_WIDGETS_BORDER); /** * Add the properties */ foreach ($twitterMeta as $key => $content) { $event->data['meta'][] = array("name" => $key, "content" => $content); } } }