Source of file MetaPreviewField.php
Size: 3,136 Bytes - Last Modified: 2021-12-24T06:43:33+00:00
/var/www/docs.ssmods.com/process/src/src/Forms/MetaPreviewField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 | <?php namespace CyberDuck\SEO\Forms; use SilverStripe\Blog\Model\BlogPost; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\LiteralField; use SilverStripe\ORM\DataObject; use SilverStripe\View\Requirements; /** * MetaPreviewField * * Gogole SERP preview field * * @package silverstripe-seo * @license MIT License https://github.com/cyber-duck/silverstripe-seo/blob/master/LICENSE * @author <andrewm@cyber-duck.co.uk> **/ class MetaPreviewField extends LiteralField { /** * Object instance used to populate Meta from * * @since version 2.0.0 **/ private $page; /** * Requires a DataObject to be passed * * @since version 1.0.0 * * @param DataObject $page * * @return void **/ public function __construct(DataObject $page) { $this->page = $page; Requirements::javascript('cyber-duck/silverstripe-seo:assets/js/serp.js'); parent::__construct('MetaPreviewField', $this->getMetaContent()); } /** * Get the required values to show in the SERP preview * * @since version 2.0.0 * * @return ViewableData **/ private function getMetaContent() { return Controller::curr()->customise([ 'SerpMetaTitle' => $this->getPageMetaTitle(), 'SerpMetaLink' => $this->getPageMetaLink(), 'SerpMetaDescription' => $this->getPageMetaDescription() ])->renderWith('MetaPreview'); } /** * Get the Meta title to show in the SERP preview * * @since version 2.0.0 * * @return string **/ private function getPageMetaTitle() { if($this->page->MetaTitle) { return $this->page->MetaTitle; } if(class_exists(BlogPost::class)) { if($this->page instanceof BlogPost) { if($this->page->Parent()->DefaultPostMetaTitle == 1) { return $this->page->Title; } } } return Config::inst()->get(MetaPreviewField::class, 'meta_title'); } /** * Get the page URL to show in the SERP preview * * @since version 2.0.0 * * @throws Exception * * @return string **/ private function getPageMetaLink() { return Director::absoluteBaseURL().substr($this->page->Link(), 1); } /** * Get the Meta description to show in the SERP preview * * @since version 2.0.0 * * @return string **/ private function getPageMetaDescription() { if($this->page->MetaDescripion) { return $this->page->MetaDescripion; } if(class_exists(BlogPost::class)) { if($this->page instanceof BlogPost) { if($this->page->Parent()->DefaultPostMetaDescription == 1) { return strip_tags($this->page->Summary); } } } return Config::inst()->get(MetaPreviewField::class, 'meta_description'); } } |