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) { $executionContext = ExecutionContext::getActualOrCreateFromEnv(); try { $templateForWebPage = $executionContext->getExecutingPageTemplate(); if(!$templateForWebPage->isSocial()){ return; } $page = MarkupPath::createPageFromPathObject($templateForWebPage->getRequestedContextPath()); } catch (ExceptionNotFound $e) { return; } if (!FileSystems::exists($page)) { return; } /** * No social for bars */ if ($page->isSlot()) { 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 = $this->getConf(self::CONF_TWITTER_SITE_HANDLE); $siteTwitterId = $this->getConf(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 */ try { $twitterImagePath = TwitterImage::createFromResource($page)->getValueOrDefault(); } catch (ExceptionNotFound $e) { // no twitter image return; } if (!FileSystems::exists($twitterImagePath)) { LogUtility::error("The twitter image ($twitterImagePath) does not exists.", self::CANONICAL); return; } try { $twitterMeta[self::META_IMAGE] = FetcherRaster::createImageFetchFromPath($twitterImagePath)->getFetchUrl()->toAbsoluteUrlString(); } catch (ExceptionBadArgument|ExceptionBadSyntax|ExceptionNotExists $e) { LogUtility::error("Error with the twitter image url. " . $e->getMessage(), self::CANONICAL, $e); return; } $title = ResourceName::getFromPath($twitterImagePath); if (!empty($title)) { $twitterMeta[self::META_IMAGE_ALT] = $title; } /** * https://developer.twitter.com/en/docs/twitter-for-websites/webpage-properties */ // don't track $twitterMeta[self::META_DNT] = $this->getConf(self::CONF_TWITTER_DONT_NOT_TRACK, self::CONF_ON); // turn off csp warning $twitterMeta[self::META_WIDGET_CSP] = "on"; /** * Embedded Tweet Theme */ $twitterMeta[self::META_WIDGETS_THEME] = $this->getConf(BlockquoteTag::CONF_TWEET_WIDGETS_THEME, BlockquoteTag::CONF_TWEET_WIDGETS_THEME_DEFAULT); $twitterMeta[self::META_WIDGETS_BORDER_COLOR] = $this->getConf(BlockquoteTag::CONF_TWEET_WIDGETS_BORDER, BlockquoteTag::CONF_TWEET_WIDGETS_BORDER_DEFAULT); /** * Add the properties */ foreach ($twitterMeta as $key => $content) { $event->data['meta'][] = array("name" => $key, "content" => $content); } } }