Source of file MetaConfigPageExtension.php
Size: 9,745 Bytes - Last Modified: 2021-12-23T10:04:04+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/MetaConfigPageExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 | <?php /** * Extension for Page that adds a lot of meta functionality * * @Author Martijn Schenk * @Alias Chibby * @Email martijnschenk@loyals.nl */ class MetaConfigPageExtension extends DataExtension { /** * Cache for the page image * * @var Image|null */ protected $image; /** * Cache for the site image * * @var SiteConfig|null */ protected $config; /** * Database fields * * @var array */ private static $db = array( 'NoIndex' => 'Boolean', 'NoFollow' => 'Boolean', ); /** * Retrieve the site config * * @return \DataObject */ protected function getSiteConfig() { if (!$this->config) { $this->config = SiteConfig::current_site_config(); } return $this->config; } /** * Update the settings tab * * @param \FieldList */ public function updateSettingsFields(FieldList &$fields) { $fields->addFieldToTab('Root.Settings', CompositeField::create($robots = FieldGroup::create(checkBoxField::create('NoIndex', _t('Admin.NoIndex', 'No index')), checkBoxField::create('NoFollow', _t('Admin.NoFollow', 'No follow'))))); $robots->setTitle(_t('Admin.Robots', 'Robots')); } /** * Update the meta tags * * @param $metatags */ public function updateMetaTags(&$metatags) { if (Director::isLive()) { $index = $this->owner->NoIndex ? 'noindex' : 'index'; $follow = $this->owner->NoFollow ? 'nofollow' : 'follow'; $metatags['robots'] = sprintf("<meta name=\"robots\" content=\"%1\$s,%2\$s\" />", $index, $follow); } if (method_exists(Controller::curr(), 'getProduct') && Controller::curr() ->getProduct() && Controller::curr() ->getProduct() ->Introduction) { $metatags['MetaDescription'] = "<meta name=\"description\" content=\"" . Convert::raw2att(Controller::curr() ->getProduct() ->MetaDescription) . "\" />"; } } /** * Generate a Google Rich Snippet for Local Business * * @return \HTMLText */ public function GoogleRichSnippetLocalBusiness() { $siteConfig = $this->getSiteConfig(); $image = $this->getPageImage(); $addressString = urlencode(sprintf( '%1$s, %2$s, %3$s, NL', $siteConfig->Address, $siteConfig->City, $siteConfig->Postcode )); $geocoding = GoogleGeocoding::getOrCreateGeocode($addressString); $snippet = [ '@context' => 'http://schema.org', '@type' => $siteConfig->BusinessType, 'image' => $image ? Director::absoluteBaseURL() . $image->Link() : null, // we'll need to fix this '@id' => Director::absoluteBaseURL(), 'name' => $siteConfig->Title, 'address' => [ '@type' => 'PostalAddress', 'streetAddress' => $siteConfig->Address, 'addressLocality' => $siteConfig->City, 'postalCode' => $siteConfig->Postcode, 'addressCountry' => 'NL', ], ]; if ($geocoding) { $snippet['geo'] = [ '@type' => 'GeoCoordinates', 'latitude' => $geocoding->Latitude, 'longitude' => $geocoding->Longitude, ]; } $snippet['url'] = Director::absoluteBaseURL(); $snippet['telephone'] = $siteConfig->Phonenumber; $template = new SSViewer('GoogleRichSnippetLocalBusiness'); return $template->process($this->owner->customise(new ArrayData([ "JSON" => json_encode($snippet), ]))); } /** * Generate OpenGraph meta data * * @return \HTMLText */ public function OpenGraph() { $siteConfig = $this->getSiteConfig(); $image = $this->getPageImage(); /** @var SimpleProduct $product */ $product = method_exists(Controller::curr(), 'getProduct') && Controller::curr() ->getProduct(); $template = new SSViewer('OpenGraph'); return $template->process($this->owner->customise(new ArrayData([ 'Title' => $this->owner->Title, 'Type' => 'website', 'Image' => ($image ? Director::absoluteBaseURL() . $image->CropHeight(249) ->Link() : null), 'Url' => $this->owner->AbsoluteLink(), 'SiteName' => $siteConfig->Title, 'ThemeDir' => $this->owner->ThemeDir(), 'Description' => $product ? $product->MetaDescription : ($this->owner->MetaDescription ?: ''), ]))); } /** * Generate a Twitter Summary Card * * @return \HTMLText */ public function TwitterSummaryCard() { $siteConfig = $this->getSiteConfig(); $image = $this->getPageImage(); /** @var SimpleProduct $product */ $product = method_exists(Controller::curr(), 'getProduct') && Controller::curr() ->getProduct(); $template = new SSViewer('TwitterSummaryCard'); return $template->process($this->owner->customise(new ArrayData([ 'Type' => $product ? 'summary_large_image' : 'summary', 'Title' => $product ? $product->Title : $this->owner->Title, 'Image' => Director::absoluteBaseURL() . $image->CropHeight($product ? '281' : '125') ->Link(), 'TwitterUser' => $siteConfig->TwitterUser, 'Description' => $product ? $product->MetaDescription : ($this->owner->MetaDescription ?: ''), ]))); } /** * Retrieve the image for this page * * @return null|Image */ protected function getPageImage() { $siteConfig = $this->getSiteConfig(); if (!$this->image) { if (method_exists($this->owner, 'getPageImage')) { $this->image = $this->owner->getPageImage(); } else { $this->image = $siteConfig->Image(); } } return $this->image; } /** * Retrieve the Google scripts for this page * * @return \HTMLText */ public function GoogleScripts() { $siteConfig = $this->getSiteConfig(); $template = new SSViewer('GoogleScripts'); return $template->process($this->owner->customise(new ArrayData([ 'AnalyticsID' => $siteConfig->GoogleAnalyticsID, 'TagManagerID' => $siteConfig->GoogleTagManagerID, ]))); } /** * Retrieve the Google breadcrumbs for this page * * @return \HTMLText */ public function GoogleBreadcrumbs() { $template = new SSViewer('GoogleBreadcrumbs'); $breadcrumbs = $this->getGoogleBreadcrumbs(); return $template->process($this->owner->customise(new ArrayData([ 'JSON' => $breadcrumbs ? json_encode($breadcrumbs) : false, ]))); } /** * Retrieve the Breadcrumbs information in a schema.org defined array * * @return array|bool */ protected function getGoogleBreadcrumbs() { /** @var ArrayList $crumbs */ $crumbs = $this->owner->getBreadcrumbItems(20, false, true); if (!$crumbs) { return false; } // fetch the first page in this list $home = $crumbs->shift(); // if it's not the homepage, just return it, no worries if ($home->Link() != '/home/') { $crumbs->unshift($home); } if (!$crumbs->Count()) { return false; } $return = [ "@context" => "http://schema.org", "@type" => "BreadcrumbList", ]; /** * @var int $idx * @var Page $crumb */ foreach ($crumbs AS $idx => $crumb) { if (!isset($return["itemListElement"])) { $return["itemListElement"] = []; } $return["itemListElement"][] = [ "@type" => "ListItem", "position" => $idx + 1, "item" => [ "@id" => $crumb->AbsoluteLink(), "name" => $crumb->dbObject('MenuTitle')->XML(), ], ]; } return $return; } /** * Retrieve the canonical, next and previous link tags * * @return \HTMLText */ public function Canonical() { $template = new SSViewer('Canonical'); return $template->process($this->owner->customise(new ArrayData([ 'Canonical' => $this->owner->AbsoluteLink(), 'Next' => $this->NextLink(), 'Previous' => $this->PrevLink(), ]))); } /** * Attempt to retrieve a next link * * @return string|bool */ protected function NextLink() { if (method_exists($this->owner, 'NextLink')) { return $this->owner->NextLink(); } return false; } /** * Attempt to retrieve a previous link * * @return string|bool */ protected function PrevLink() { if (method_exists($this->owner, 'PrevLink')) { return $this->owner->PrevLink(); } return false; } } |