|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plugin: SEOToolbox |
|
4
|
|
|
* Author: Dylan Grech |
|
5
|
|
|
* Copyright: 2016 |
|
6
|
|
|
* |
|
7
|
|
|
* Global Auto Link Settings is a dataobject that is auto-created |
|
8
|
|
|
* on dev/build and contains all the global settings for the |
|
9
|
|
|
* automated links |
|
10
|
|
|
* |
|
11
|
|
|
* @property int MaxLinksPerPage |
|
12
|
|
|
* @property string ExcludeTags |
|
13
|
|
|
* @property string IncludeIn |
|
14
|
|
|
* @property string AddTo |
|
15
|
|
|
* @property string CrawlID |
|
16
|
|
|
*/ |
|
17
|
|
|
class GlobalAutoLinkSettings extends DataObject{ |
|
18
|
|
|
|
|
19
|
|
|
public static $enabled = true; |
|
20
|
|
|
public static $encoding = 'UTF-8'; |
|
21
|
|
|
|
|
22
|
|
|
private static $db = array( |
|
23
|
|
|
'MaxLinksPerPage' => 'INT', |
|
24
|
|
|
'ExcludeTags' => 'VARCHAR(255)', |
|
25
|
|
|
'IncludeIn' => 'Text', |
|
26
|
|
|
'AddTo' => 'Text', |
|
27
|
|
|
'CrawlID' => 'VARCHAR(15)' |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
private static $defaults = array( |
|
31
|
|
|
'IncludeIn' => 'Content' |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
public static $default_create_config = array( |
|
35
|
|
|
'MaxLinksPerPage' => 0, |
|
36
|
|
|
'ExcludeTags' => 'pre,h1,h2,h3,h4,h5,h6', |
|
37
|
|
|
'IncludeIn' => 'Content' |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return an array of all html tags we'll exclude |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public function ExcludeTags(){ |
|
46
|
|
|
return array_unique( explode( ',', str_replace( ' ', '', $this->ExcludeTags ).',a,img,iframe,video,object' ) ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Return an array of where automated links can be created |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function IncludeInFields(){ |
|
55
|
|
|
return explode( ',', str_replace( ' ', '', $this->IncludeIn ) ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function requireDefaultRecords() { |
|
59
|
|
|
$hasData = self::get()->first(); |
|
60
|
|
|
if(!$hasData) { |
|
61
|
|
|
$obj = self::create(self::$default_create_config); |
|
62
|
|
|
$obj->CrawlID = $this->createCrawlID(); |
|
63
|
|
|
$obj->write(); |
|
64
|
|
|
DB::alteration_message("Added default Global Auto Link Settings","created"); |
|
65
|
|
|
} else{ |
|
66
|
|
|
if(!$hasData->CrawlID){ |
|
67
|
|
|
$hasData->CrawlID = $this->createCrawlID(); |
|
68
|
|
|
$hasData->write(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
parent::requireDefaultRecords(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Create a random CrawlID |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
private function createCrawlID() { |
|
81
|
|
|
$ret = ''; |
|
82
|
|
|
$alpha = 'abcdefghijklm[)0123456789(]nopqrstuvwxyz'; |
|
83
|
|
|
$alpha_len = strlen($alpha); |
|
84
|
|
|
|
|
85
|
|
|
while (strlen($ret) < 14) |
|
86
|
|
|
$ret .= (rand(0, 1) == 0) ? strtoupper($alpha[rand(0, $alpha_len-1)]) : $alpha[rand(0, $alpha_len-1)]; |
|
87
|
|
|
|
|
88
|
|
|
return $ret; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns a list of ClassNames where |
|
93
|
|
|
* auto links are allowed in |
|
94
|
|
|
* |
|
95
|
|
|
* Note: This function tries to cleanup user input |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function AllowedIn(){ |
|
100
|
|
|
$classes = array_values( ClassInfo::subclassesFor( 'SiteTree' ) ); |
|
101
|
|
|
if( !$this->AddTo ) return $classes; |
|
102
|
|
|
|
|
103
|
|
|
$sanitized = explode( ',', str_replace( ' ', '', strtolower( $this->AddTo ) ) ); |
|
104
|
|
|
$len = count($sanitized); |
|
105
|
|
|
|
|
106
|
|
|
for( $x = 0; $x < $len; $x++ ){ |
|
107
|
|
|
$found = false; |
|
108
|
|
|
|
|
109
|
|
|
foreach( $classes as $class ){ |
|
110
|
|
|
if( strtolower( $class ) === $sanitized[$x] ){ |
|
111
|
|
|
$sanitized[$x] = $class; |
|
112
|
|
|
$found = true; |
|
113
|
|
|
break 1; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if( !$found ) unset( $sanitized[$x] ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return (array) $sanitized; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Gets the current config |
|
125
|
|
|
* |
|
126
|
|
|
* @return \DataObject |
|
127
|
|
|
* @throws \ValidationException |
|
128
|
|
|
* @throws null |
|
129
|
|
|
*/ |
|
130
|
|
|
public static function get_current() { |
|
131
|
|
|
$obj = self::get()->first(); |
|
132
|
|
|
if (!$obj) { |
|
133
|
|
|
self::create(self::$default_create_config)->write(); |
|
134
|
|
|
self::flush_and_destroy_cache(); |
|
135
|
|
|
} |
|
136
|
|
|
return $obj; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|