1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Adds Open Graph metadata to Pages. |
5
|
|
|
* |
6
|
|
|
* @namespace silverstripe-seo |
7
|
|
|
* @package open-graph |
8
|
|
|
* @author Andrew Gerber <[email protected]> |
9
|
|
|
* @version 1.0.0 |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class SEO_OpenGraph_SiteTree_DataExtension extends DataExtension { |
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/* Static Variables |
17
|
|
|
------------------------------------------------------------------------------*/ |
18
|
|
|
|
19
|
|
|
// |
20
|
|
|
private static $SEOOpenGraphUpload = 'SEO/OpenGraph/'; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/* Overload Model |
24
|
|
|
------------------------------------------------------------------------------*/ |
25
|
|
|
|
26
|
|
|
private static $db = array( |
|
|
|
|
27
|
|
|
'OpenGraphData' => 'Text' |
28
|
|
|
); |
29
|
|
|
private static $has_one = array( |
|
|
|
|
30
|
|
|
'OpenGraphImage' => 'Image', |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/* Variables |
35
|
|
|
------------------------------------------------------------------------------*/ |
36
|
|
|
|
37
|
|
|
// |
38
|
|
|
protected static $types = array( |
39
|
|
|
'website' => 'Website (default)', |
40
|
|
|
'off' => 'Disabled for this page' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
// |
44
|
|
|
protected static $OpenGraphProtocol = array( |
45
|
|
|
'og:type' => 'website', |
46
|
|
|
'og:url' => null, |
47
|
|
|
'og:site_name' => null, |
48
|
|
|
'og:title' => null, |
49
|
|
|
'og:description' => null, |
50
|
|
|
'og:image' => null |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/* Overload Methods |
55
|
|
|
------------------------------------------------------------------------------*/ |
56
|
|
|
|
57
|
|
|
// CMS Fields |
58
|
|
|
public function updateCMSFields(FieldList $fields) { |
59
|
|
|
|
60
|
|
|
// vars |
61
|
|
|
$config = SiteConfig::current_site_config(); |
62
|
|
|
$owner = $this->owner; |
63
|
|
|
|
64
|
|
|
// decode data into array |
65
|
|
|
$data = json_decode($owner->OpenGraphData, true); |
66
|
|
|
|
67
|
|
|
// @todo Add repair method if data is missing / corrupt ~ for fringe cases |
68
|
|
|
|
69
|
|
|
// tab |
70
|
|
|
$tab = new Tab('OpenGraph'); |
71
|
|
|
|
72
|
|
|
// add disabled/error state if `off` |
73
|
|
|
if ($data['og:type'] === 'off') { |
74
|
|
|
$tab->addExtraClass('error'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// add the tab |
78
|
|
|
$fields->addFieldToTab('Root.Metadata', $tab, 'FullOutput'); |
79
|
|
|
|
80
|
|
|
// new identity |
81
|
|
|
$tab = 'Root.Metadata.OpenGraph'; |
82
|
|
|
|
83
|
|
|
// add description |
84
|
|
|
// type always visible |
85
|
|
|
$fields->addFieldsToTab($tab, array( |
86
|
|
|
// header |
87
|
|
|
LabelField::create('OpenGraphHeader', '@todo Information</a>') |
88
|
|
|
->addExtraClass('information'), |
89
|
|
|
// type |
90
|
|
|
DropdownField::create('OpenGraphType', '<a href="http://ogp.me/#types">og:type</a>', self::$types, $data['og:type']), |
91
|
|
|
)); |
92
|
|
|
|
93
|
|
|
if ($data['og:type'] !== 'off') { |
94
|
|
|
$fields->addFieldsToTab($tab, array( |
95
|
|
|
// url |
96
|
|
|
ReadonlyField::create('OpenGraphURL', 'Canonical URL', $owner->AbsoluteLink()), |
97
|
|
|
// site name |
98
|
|
|
TextField::create('OpenGraphSiteName', 'Site Name', $data['og:site_name']) |
99
|
|
|
->setAttribute('placeholder', $config->Title), |
100
|
|
|
// title |
101
|
|
|
TextField::create('OpenGraphTitle', 'Page Title', $data['og:title']) |
102
|
|
|
->setAttribute('placeholder', $owner->Title), |
103
|
|
|
// description |
104
|
|
|
TextareaField::create('OpenGraphDescription', 'Description', $data['og:description']) |
105
|
|
|
->setAttribute('placeholder', $owner->GenerateDescription()), |
106
|
|
|
// image |
107
|
|
|
UploadField::create('OpenGraphImage', 'Image<pre>type: png/jpg/gif</pre><pre>size: variable *</pre>', $owner->OpenGraphImage) |
108
|
|
|
->setAllowedExtensions(array('png', 'jpg', 'jpeg', 'gif')) |
109
|
|
|
->setFolderName(self::$SEOOpenGraphUpload . $owner->Title) |
110
|
|
|
->setDescription('* <a href="https://developers.facebook.com/docs/sharing/best-practices#images" target="_blank">Facebook image best practices</a>, or use any preferred Open Graph guide.'), |
111
|
|
|
)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Require Default Records |
117
|
|
|
public function requireDefaultRecords() { |
118
|
|
|
|
119
|
|
|
// init parent |
120
|
|
|
parent::requireDefaultRecords(); |
121
|
|
|
|
122
|
|
|
// |
123
|
|
|
$pages = SiteTree::get(); |
124
|
|
|
if ($pages->count() > 0) { |
125
|
|
|
$count = 0; |
126
|
|
|
foreach ($pages as $page) { |
127
|
|
|
if ($page->OpenGraphData == null) { |
128
|
|
|
$page->OpenGraphData = json_encode(self::$OpenGraphProtocol); |
129
|
|
|
$page->write(); |
130
|
|
|
$count++; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
if ($count > 0) { |
134
|
|
|
DB::alteration_message('Open Graph Data added to ' . $count . ' page(s)', 'created'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// |
141
|
|
|
public function onBeforeWrite() { |
142
|
|
|
|
143
|
|
|
// init parent |
144
|
|
|
parent::onBeforeWrite(); |
145
|
|
|
|
146
|
|
|
// owner |
147
|
|
|
$owner = $this->owner; |
148
|
|
|
|
149
|
|
|
// this will NOT trigger during requireDefaultRecords(), to prevent setting blank values |
150
|
|
|
// @todo Is this the optimal solution ?? |
151
|
|
|
if ($owner->OpenGraphType != null) { |
152
|
|
|
|
153
|
|
|
// |
154
|
|
|
$data = json_decode($owner->OpenGraphData, true); |
155
|
|
|
|
156
|
|
|
// |
157
|
|
|
$data['og:type'] = $owner->OpenGraphType; |
158
|
|
|
|
159
|
|
|
// prevent clearing of existing values |
160
|
|
|
if ($data['og:type'] !== 'off') { |
161
|
|
|
// URL |
162
|
|
|
$data['og:url'] = $owner->OpenGraphURL; |
163
|
|
|
// site name |
164
|
|
|
$data['og:site_name'] = $owner->OpenGraphSiteName; |
165
|
|
|
// title |
166
|
|
|
$data['og:title'] = $owner->OpenGraphTitle; |
167
|
|
|
// description |
168
|
|
|
$data['og:description'] = $owner->OpenGraphDescription; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// |
172
|
|
|
$owner->OpenGraphData = json_encode($data); |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/* Template Methods |
180
|
|
|
------------------------------------------------------------------------------*/ |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @name updateMetadata |
184
|
|
|
* |
185
|
|
|
* Updates metadata with icons. |
186
|
|
|
* |
187
|
|
|
* @param SiteConfig $config |
188
|
|
|
* @param SiteTree $owner |
189
|
|
|
* @param $metadata |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function updateMetadata(SiteConfig $config, SiteTree $owner, &$metadata) { |
193
|
|
|
|
194
|
|
|
// decode into array |
195
|
|
|
$data = json_decode($owner->OpenGraphData, true); |
196
|
|
|
|
197
|
|
|
// check for data or throw an error |
198
|
|
|
if ($data) { |
199
|
|
|
|
200
|
|
|
// check if off |
201
|
|
|
if ($data['og:type'] !== 'off') { |
202
|
|
|
|
203
|
|
|
// Header |
204
|
|
|
$metadata .= $owner->MarkupComment('Open Graph'); |
205
|
|
|
|
206
|
|
|
// Type |
207
|
|
|
$metadata .= $owner->MarkupOpenGraph('og:type', $data['og:type']); |
208
|
|
|
|
209
|
|
|
// URL |
210
|
|
|
$metadata .= $owner->MarkupOpenGraph('og:url', $owner->AbsoluteLink()); |
211
|
|
|
|
212
|
|
|
// Site Name |
213
|
|
|
$siteName = ($data['og:site_name']) ? $data['og:site_name'] : $config->Title; |
214
|
|
|
$metadata .= $owner->MarkupOpenGraph('og:site_name', $siteName, true); |
215
|
|
|
|
216
|
|
|
// Title |
217
|
|
|
$title = ($data['og:title']) ? $data['og:title'] : $owner->Title; |
218
|
|
|
$metadata .= $owner->MarkupOpenGraph('og:title', $title, true); |
219
|
|
|
|
220
|
|
|
// Description |
221
|
|
|
$description = ($data['og:description']) ? $data['og:description'] : $owner->GenerateDescription(); |
222
|
|
|
$metadata .= $owner->MarkupOpenGraph('og:description', $description, true); |
223
|
|
|
|
224
|
|
|
// Image |
225
|
|
|
if ($owner->OpenGraphImage()->exists()) { |
226
|
|
|
$metadata .= $owner->MarkupOpenGraph('og:image', $owner->OpenGraphImage()->getAbsoluteURL()); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
} else { |
230
|
|
|
|
231
|
|
|
// OFF |
232
|
|
|
$metadata .= $owner->MarkupComment('Open Graph [ off ]'); |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} else { |
237
|
|
|
|
238
|
|
|
// ERROR |
239
|
|
|
$metadata .= $owner->MarkupComment('Open Graph [ error ]'); |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/* Class Methods |
247
|
|
|
------------------------------------------------------------------------------*/ |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Returns markup for an Open Graph meta element. |
251
|
|
|
* |
252
|
|
|
* @var $property |
253
|
|
|
* @var $content |
254
|
|
|
* @var $encode |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
public function MarkupOpenGraph($property, $content, $encode = false) { |
259
|
|
|
// encode content |
260
|
|
|
if ($encode) $content = htmlentities($content, ENT_QUOTES, $this->owner->Charset); |
261
|
|
|
// format & return |
262
|
|
|
return '<meta property="' . $property . '" content="' . $content . '" />' . PHP_EOL; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.