Source of file PageHealthExtension.php
Size: 3,697 Bytes - Last Modified: 2021-12-23T10:11:23+00:00
/var/www/docs.ssmods.com/process/src/src/Extensions/PageHealthExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 | <?php namespace QuinnInteractive\Seo\Extensions; use KubAT\PhpSimple\HtmlDomParser; use QuinnInteractive\Seo\Forms\GoogleSearchPreview; use QuinnInteractive\Seo\Forms\HealthAnalysisField; use SilverStripe\Control\Controller; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\TextField; use SilverStripe\Forms\ToggleCompositeField; use SilverStripe\ORM\DataExtension; use SilverStripe\VersionedAdmin\Controllers\CMSPageHistoryViewerController; use SilverStripe\VersionedAdmin\Controllers\HistoryViewerController; use SilverStripe\View\Requirements; /** * Class PageHealthExtension * @package QuinnInteractive\Seo\Extensions * * @property string FocusKeyword */ class PageHealthExtension extends DataExtension { const EMPTY_HTML = '<p></p>'; /** * @var string|null */ protected $renderedHtml; private static $db = [ 'FocusKeyword' => 'Varchar(50)' ]; /** * @return \Page|static */ public function getOwner() { /** @var \Page $owner */ $owner = parent::getOwner(); return $owner; } /** * Gets the rendered html (current version, either draft or live) * * @return string|null */ public function getRenderedHtml() { if (!$this->renderedHtml) { $controllerName = $this->owner->getControllerName(); if ('SilverStripe\UserForms\Control\UserDefinedFormController' == $controllerName) { // remove the Form since it crashes $this->owner->Form = false; } Requirements::clear(); // we only want the HTML, not any of the js or css $this->renderedHtml = $controllerName::singleton()->render($this->owner); Requirements::restore(); // put the js/css requirements back when we're done } if ($this->renderedHtml === false) { $this->renderedHtml = self::EMPTY_HTML; } return $this->renderedHtml; } /** * Gets the DOM parser for the rendered html * * @return \simple_html_dom\simple_html_dom */ public function getRenderedHtmlDomParser() { return HtmlDomParser::str_get_html($this->getRenderedHtml()); } /** * Override this if you have more than just `Content` (or don't have `Content` at all). Fields should * be in the order for which they appear for a frontend user * * @return array */ public function seoContentFields() { return [ 'Content' ]; } /** * @param FieldList $fields */ public function updateCMSFields(FieldList $fields) { if ( Controller::curr() instanceof HistoryViewerController || Controller::curr() instanceof CMSPageHistoryViewerController ) { // avoid breaking the history comparison UI return; } if ($this->owner instanceof \SilverStripe\ErrorPage\ErrorPage) { return; } $dom = $this->getRenderedHtmlDomParser(); if ($dom) { $fields->addFieldsToTab('Root.Main', [ ToggleCompositeField::create('SEOHealthAnalysis', 'SEO Health Analysis', [ GoogleSearchPreview::create( 'GoogleSearchPreview', 'Search Preview', $this->getOwner(), $dom ), TextField::create('FocusKeyword', 'Set focus keyword'), HealthAnalysisField::create('ContentAnalysis', 'Content Analysis', $this->getOwner()), ]) ], 'Metadata'); } } } |