-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.vanillaseo.plugin.php
executable file
·316 lines (267 loc) · 8.32 KB
/
class.vanillaseo.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
$PluginInfo['VanillaSEO'] = array (
'Name' => 'Vanilla SEO',
'Description' => 'Vanilla SEO is your all in one plugin for optimizing your Vanilla forum for search engines.',
'Version' => '0.3.0',
'RequiredApplications' => array('Vanilla' => '2.1.0'),
'RequiredPlugins' => FALSE,
'HasLocale' => FALSE,
'SettingsUrl' => '/dashboard/plugin/seo',
'SettingsPermission' => 'Garden.Settings.Manage',
'Author' => 'Jamie Chung',
'AuthorEmail' => '[email protected]',
'AuthorUrl' => 'http://www.jamiechung.me'
);
class VanillaSEOPlugin extends Gdn_Plugin
{
// All available %tags%.
public $tags = array (
'title' => 'Discussion Title',
'category' => 'Category Name',
'garden' => 'Vanilla Banner Title',
'search' => 'Search Query',
'tag' => 'Tag'
);
// Default titles for each part of the vanilla rewrite scheme.
public $dynamic_titles = array (
// CATEGORIES
'categories_all' => array(
'default' => 'All Categories on %garden%',
'fields' => array('garden'),
'name' => 'All Categories',
'info' => 'List page of all the categories available for users to post discussions.',
'examples' => array('/categories/all')
),
'category_single' => array(
'default' => '%category% Discussions on %garden%',
'fields' => array('garden', 'category'),
'name' => 'Single Category Page',
'info' => 'Category view displaying relevent discussions.',
'examples' => array('/categories/general-forum', '/categories/general-forum/p2', '/categories/general-forum/feed.rss')
),
'category_discussions' => array(
'default' => 'View Discussions and Categories on %garden%',
'fields' => array('garden'),
'name' => 'Sample Categories',
'info' => 'Showing all categories and a few discussions from each category.',
'examples' => array('/categories')
),
// ACTIVITY
'activity' => array(
'default' => 'Recent Activity on %garden%',
'fields' => array('garden'),
'name' => 'Recent Activity',
'info' => 'Page listing recent activity on your vanilla forum.',
'examples' => array('/activity')
),
// DISCUSSIONS
'discussions' => array(
'default' => 'Recent Discussions on %garden%',
'fields' => array('garden'),
'name' => 'Discussions Home Page',
'info' => 'Page listing recent discussions on your vanilla forum.',
'examples' => array('/discussions')
),
'discussion_single' => array(
'default' => '%title% - %category% Discussions on %garden%',
'fields' => array('garden', 'title', 'category'),
'name' => 'Single Discussion Page',
'info' => 'Viewing a single discussion thread.',
'examples' => array('/discussions/23/this-is-a-post-title')
),
// TAGGED DISCUSSIONS
'tagged' => array(
'default' => 'Tagged with "%tag%" on %garden%',
'fields' => array('garden', 'tag'),
'name' => 'Tagged Discussions Page',
'info' => 'Discussions tagged with a specific tag.',
'examples' => array('/discussions/tagged/vanilla')
),
// SEARCH
'search_results' => array(
'default' => '%search% - Search Results on %garden%',
'fields' => array('garden', 'search'),
'name' => 'Search Results Page',
'info' => 'Adds the search query in the page title of search results pages.',
'examples' => array('/search?Search=hello+world')
),
);
private function GetTitle ( $type )
{
if ( C('Plugins.SEO.DynamicTitles.'.$type) )
{
return strip_tags(C('Plugins.SEO.DynamicTitles.'.$type));
}
else
{
return $this->dynamic_titles[$type]['default'];
}
}
public function Base_GetAppSettingsMenuItems_Handler ( $Sender )
{
$Menu = $Sender->EventArguments['SideMenu'];
$Menu->AddItem('Site Settings', T('Settings'));
$Menu->AddLink('Site Settings', T('Search Engine Optimization'), 'plugin/seo', 'Garden.Settings.Manage');
}
public function PluginController_SEO_Create ( $Sender )
{
$Sender->Permission('Garden.Settings.Manage');
$Sender->Title(T('Search Engine Optimization'));
$Sender->AddSideMenu('plugin/seo');
$this->Dispatch($Sender, $Sender->RequestArgs);
}
public function Controller_Index ( $Sender )
{
$Sender->Permission('Garden.Settings.Manage');
$Sender->DynamicTitles = $this->dynamic_titles;
$Sender->DynamicTitleTags = $this->tags;
if ( $this->Enabled() )
{
if ( $Sender->Form->AuthenticatedPostBack() === TRUE )
{
foreach ( $this->dynamic_titles as $field => $info )
{
SaveToConfig('Plugins.SEO.DynamicTitles.'.$field, $Sender->Form->GetValue($field));
}
$Sender->StatusMessage = T('Your settings have been saved.');
}
foreach ( $this->dynamic_titles as $field => $info )
{
$Sender->Form->SetFormValue($field, $this->GetTitle($field));
}
}
$Sender->Render($this->GetView('seo.php'));
}
public function Controller_Toggle ( $Sender )
{
if ( Gdn::Session()->ValidateTransientKey(GetValue(1, $Sender->RequestArgs)) )
{
if ( C('Plugins.SEO.Enabled') )
{
RemoveFromConfig('Plugins.SEO.Enabled');
}
else
{
SaveToConfig('Plugins.SEO.Enabled', TRUE);
}
}
redirect('plugin/seo');
}
public function CategoriesController_Render_Before ( $Sender )
{
if ( !$this->Enabled() )
return;
$data = array();
switch ( Gdn::Dispatcher()->ControllerMethod() )
{
case 'all':
$type = 'categories_all';
break;
default:
if ( isset($Sender->Data['Category']) )
{
$type = 'category_single';
$data['category'] = $Sender->Data('Category.Name');
// Add meta description if one is available
$CategoryDescription = $Sender->Data('Category.Description', NULL);
if ( !is_null($CategoryDescription) )
{
$Sender->Head->AddTag('meta', array('name' => 'description', 'content'=> htmlspecialchars($CategoryDescription)));
}
}
else
{
$type = 'category_discussions';
}
break;
}
$this->ParseTitle($Sender, $data, $type);
}
public function ActivityController_Render_Before ( $Sender )
{
if ( !$this->Enabled() )
return;
$this->ParseTitle($Sender, '', 'activity');
}
public function SearchController_Render_Before ( $Sender )
{
if ( !$this->Enabled() )
return;
$Search = Gdn_Format::Text($Sender->Form->GetFormValue('Search'));
// No search term? No page title.
if ( strlen($Search) == 0 ) return;
$data['search'] = $Search;
$this->ParseTitle($Sender, $data, 'search_results');
}
public function DiscussionsController_Render_Before ( $Sender )
{
if ( !$this->Enabled() )
return;
$data = array();
switch ( Gdn::Dispatcher()->ControllerMethod() )
{
case 'tagged':
$type = 'tagged';
$data['tag'] = $Sender->Data('Tag');
break;
case 'index':
default:
$type = 'discussions';
break;
}
$this->ParseTitle($Sender, $data, $type );
}
private function ParseTitle ( &$Sender, $data, $type )
{
if ( !isset($this->dynamic_titles[$type]) )
return;
$dynamic = $this->dynamic_titles[$type];
$title = $this->GetTitle($type);
if ( !isset($data['garden']) )
{
$data['garden'] = C('Garden.Title');
}
// Only parse the field allowed for this dynamic title.
foreach ( $dynamic['fields'] as $field )
{
$title = str_replace('%'.$field.'%', isset($data[$field]) ? $data[$field] : '', $title);
}
$Sender->Head->Title($title);
}
public function Enabled ()
{
return ( C('Plugins.SEO.Enabled') == TRUE );
}
public function DiscussionController_Render_Before ( $Sender )
{
if ( !$this->Enabled() )
return;
$tags = array();
// Check if we have tags from current discussion
$DiscussionTags = $Sender->Data('Discussion.Tags', NULL);
if ( C('EnabledPlugins.Tagging') )
{
$tags += explode(',', $DiscussionTags);
$tags += explode(' ', $DiscussionTags);
}
array_walk($tags, 'strip_tags');
array_walk($tags, 'trim');
array_walk($tags, 'htmlspecialchars');
$tags = array_unique($tags);
if ( count($tags) > 0 )
{
$Sender->Head->AddTag('meta', array('name' => 'keywords', 'content' => implode(', ', $tags)));
}
$Sender->Head->AddTag('meta', array('name' => 'description', 'content'=> $Sender->Data('Discussion.Name')));
$data = array (
'title' => $Sender->Data('Discussion.Name'),
'category' => $Sender->Data('Discussion.Category'),
);
$type = 'discussion_single';
$this->ParseTitle($Sender, $data, $type);
}
public function Setup ()
{
// We don't need to setup because we're awesome.
}
}