Source of file GlobalAutoLinkSettings.php
Size: 3,670 Bytes - Last Modified: 2021-12-24T06:45:30+00:00
/var/www/docs.ssmods.com/process/src/code/dataobjects/GlobalAutoLinkSettings.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 | <?php /** * Plugin: SEOToolbox * Author: Dylan Grech * Copyright: 2016 * * Global Auto Link Settings is a dataobject that is auto-created * on dev/build and contains all the global settings for the * automated links * * @property int MaxLinksPerPage * @property string ExcludeTags * @property string IncludeIn * @property string AddTo * @property string CrawlID */ class GlobalAutoLinkSettings extends DataObject{ public static $enabled = true; public static $encoding = 'UTF-8'; private static $db = array( 'MaxLinksPerPage' => 'INT', 'ExcludeTags' => 'VARCHAR(255)', 'IncludeIn' => 'Text', 'AddTo' => 'Text', 'CrawlID' => 'VARCHAR(15)' ); private static $defaults = array( 'IncludeIn' => 'Content' ); public static $default_create_config = array( 'MaxLinksPerPage' => 0, 'ExcludeTags' => 'pre,h1,h2,h3,h4,h5,h6', 'IncludeIn' => 'Content' ); /** * Return an array of all html tags we'll exclude * * @return array */ public function ExcludeTags(){ return array_unique( explode( ',', str_replace( ' ', '', $this->ExcludeTags ).',a,img,iframe,video,object' ) ); } /** * Return an array of where automated links can be created * * @return array */ public function IncludeInFields(){ return explode( ',', str_replace( ' ', '', $this->IncludeIn ) ); } public function requireDefaultRecords() { $hasData = self::get()->first(); if(!$hasData) { $obj = self::create(self::$default_create_config); $obj->CrawlID = $this->createCrawlID(); $obj->write(); DB::alteration_message("Added default Global Auto Link Settings","created"); } else{ if(!$hasData->CrawlID){ $hasData->CrawlID = $this->createCrawlID(); $hasData->write(); } } parent::requireDefaultRecords(); } /** * Create a random CrawlID * * @return string */ private function createCrawlID() { $ret = ''; $alpha = 'abcdefghijklm[)0123456789(]nopqrstuvwxyz'; $alpha_len = strlen($alpha); while (strlen($ret) < 14) $ret .= (rand(0, 1) == 0) ? strtoupper($alpha[rand(0, $alpha_len-1)]) : $alpha[rand(0, $alpha_len-1)]; return $ret; } /** * Returns a list of ClassNames where * auto links are allowed in * * Note: This function tries to cleanup user input * * @return array */ public function AllowedIn(){ $classes = array_values( ClassInfo::subclassesFor( 'SiteTree' ) ); if( !$this->AddTo ) return $classes; $sanitized = explode( ',', str_replace( ' ', '', strtolower( $this->AddTo ) ) ); $len = count($sanitized); for( $x = 0; $x < $len; $x++ ){ $found = false; foreach( $classes as $class ){ if( strtolower( $class ) === $sanitized[$x] ){ $sanitized[$x] = $class; $found = true; break 1; } } if( !$found ) unset( $sanitized[$x] ); } return (array) $sanitized; } /** * Gets the current config * * @return \DataObject * @throws \ValidationException * @throws null */ public static function get_current() { $obj = self::get()->first(); if (!$obj) { self::create(self::$default_create_config)->write(); self::flush_and_destroy_cache(); } return $obj; } } |