Source of file OpenGraphObjectExtension.php
Size: 6,866 Bytes - Last Modified: 2021-12-23T10:55:43+00:00
/var/www/docs.ssmods.com/process/src/src/Extensions/OpenGraphObjectExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 | <?php namespace TractorCow\OpenGraph\Extensions; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\i18n\i18n; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBText; use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\View\SSViewer; use TractorCow\OpenGraph\Constants\OGDeterminers; use TractorCow\OpenGraph\Constants\OGTypes; use TractorCow\OpenGraph\InspectionTrait; use TractorCow\OpenGraph\Interfaces\IOpenGraphObjectBuilder; use TractorCow\OpenGraph\Interfaces\ObjectTypes\IOGObjectExplicit; use TractorCow\OpenGraph\Interfaces\ObjectTypes\Music\IOGMusic; use TractorCow\OpenGraph\Interfaces\ObjectTypes\Other\IOGArticle; use TractorCow\OpenGraph\Interfaces\ObjectTypes\Other\IOGBook; use TractorCow\OpenGraph\Interfaces\ObjectTypes\Other\IOGProfile; use TractorCow\OpenGraph\Interfaces\ObjectTypes\Other\IOGWebsite; use TractorCow\OpenGraph\Interfaces\ObjectTypes\Video\IOGVideo; use TractorCow\OpenGraph\OpenGraph; /** * Adds open graph functionality to a page or data object * * @author Damian Mooyman * @property DataObject|OpenGraphObjectExtension $owner */ class OpenGraphObjectExtension extends DataExtension implements IOGObjectExplicit { use Configurable; use InspectionTrait; /** * The default image to use * * @config * @var string */ private static $default_image = 'tractorcow/silverstripe-opengraph: images/logo.gif'; /** * Do not escpae HTML * * @var string */ private static $casting = [ 'OGNS' => 'HTMLFragment', ]; /** * Property for retrieving the opengraph namespace html tag(s). * This should be inserted into your Page.SS template as: "<html $OGNS>" * @return string The HTML tag to use for the opengraph namespace(s) */ public function getOGNS() { // todo : Should custom namespace be injected here, or left up to user code? $ns = ' prefix="og: http://ogp.me/ns# fb: http://www.facebook.com/2008/fbml'; if ($this->implementsType($this->owner, IOGMusic::class)) { $ns .= ' music: http://ogp.me/ns/music#'; } if ($this->implementsType($this->owner, IOGVideo::class)) { $ns .= ' video: http://ogp.me/ns/video#'; } if ($this->implementsType($this->owner, IOGArticle::class)) { $ns .= ' article: http://ogp.me/ns/article#'; } if ($this->implementsType($this->owner, IOGBook::class)) { $ns .= ' book: http://ogp.me/ns/book#'; } if ($this->implementsType($this->owner, IOGProfile::class)) { $ns .= ' profile: http://ogp.me/ns/profile#'; } if ($this->implementsType($this->owner, IOGWebsite::class) || $this->owner->getOGType() == OGTypes::DEFAULT_TYPE ) { $ns .= ' website: http://ogp.me/ns/website#'; } $ns .= '"'; return $ns; } /** * Determines the tag builder to use for this object * @return IOpenGraphObjectBuilder */ public function getTagBuilder() { // Determine type $type = $this->owner->getOGType(); // Case for non-types if (empty($type)) { return null; } // Determine type, if configured $prototype = OpenGraph::get_prototype($type); if (!empty($prototype['tagbuilder'])) { $class = $prototype['tagbuilder']; } else { $class = OpenGraph::get_default_tagbuilder(); } // Construct instance from type return new $class(); } public function MetaTags(&$tags) { // Generate tag builder $builder = $this->owner->getTagBuilder(); if (!$builder) { return; } $config = SiteConfig::current_site_config(); // Default tags $builder->BuildTags($tags, $this->owner, $config); } /** * Determines the opengraph type identifier for this object * @return string */ public function getOGType() { if ($type = OpenGraph::get_object_type($this->owner)) { return $type; } return OGTypes::DEFAULT_TYPE; } public function getOGTitle() { /** * @see DataObject::getTitle() */ return $this->owner->Title; } public function getOGSiteName() { $config = SiteConfig::current_site_config(); return $config->Title; } public function getOGImage() { // If a theme is in use, check if a default image is provided (theme_name_default_image) // This is useful to have a different default image on sub sites if (SSViewer::config()->uninherited('theme_enabled') === true) { $themes = SSViewer::get_themes(); if (isset($themes[0])) { $themeName = preg_replace('/[^\w ]+/ ', '_', strtolower($themes[0])); $config = $themeName . '_default_image'; if ($image = self::config()->{$config}) { return Director::absoluteURL(ModuleResourceLoader::resourceURL($image)); } } } // Since og:image is a required property, provide a reasonable default if ($image = self::config()->default_image) { return Director::absoluteURL(ModuleResourceLoader::resourceURL($image)); } return ''; } public function AbsoluteLink() { // Left blank by default. Implement this in the decorated class to determine correct value } public function getOGAudio() { // No audio by default } public function getOGVideo() { // No video by default } public function getOGDescription() { // Check MetaDescription has given content if ($this->owner->hasField('MetaDescription')) { $description = trim($this->owner->MetaDescription); if (!empty($description)) { return $description; } } // Intelligent fallback for SiteTree instances $contentField = $this->owner->dbObject('Content'); if ($contentField instanceof DBText) { return $contentField->Summary(100); } return $contentField; } public function getOGDeterminer() { return OGDeterminers::DEFAULT_VALUE; } public function getOGLocales() { // Use current locale $locale = i18n::get_locale(); // Check locale is valid if (OpenGraph::is_locale_valid($locale)) { return $locale; } // Return default return OpenGraph::get_default_locale(); } } |