1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Seo;
13
14 use ICanBoogie\Event;
15 use ICanBoogie\Operation;
16
17 use Brickrouge\Element;
18 use Brickrouge\Group;
19 use Brickrouge\Text;
20
21 use Patron\Engine as Patron;
22
23 use Icybee\EditBlock\AlterChildrenEvent;
24 use Icybee\Modules\Contents\Content;
25 use Icybee\Modules\Pages\PageController;
26
27
28
29 class Hooks
30 {
31 32 33 34 35 36 37 38 39 40 41
42 static public function on_page_controller_render(PageController\RenderEvent $event, PageController $target)
43 {
44 global $core;
45
46 $page = $event->page;
47
48 if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false || $core->user_id == 1 || !$page->is_online || ($page->node && !$page->node->is_online))
49 {
50 return;
51 }
52
53 $ua = $page->site->metas['google_analytics_ua'];
54
55 if (!$ua)
56 {
57 return;
58 }
59
60
61
62
63
64
65 $insert = <<<EOT
66
67
68 <script type="text/javascript">
69
70 var _gaq = _gaq || [];
71 _gaq.push(['_setAccount', '$ua']);
72 _gaq.push(['_trackPageview']);
73
74 (function() {
75 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
76 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
77 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
78 })();
79
80 </script>
81
82
83 EOT;
84
85 $event->html = str_replace('</body>', $insert . '</body>', $event->html);
86 }
87
88 89 90 91 92
93 static public function before_document_render_title(\Icybee\Document\BeforeRenderTitleEvent $event)
94 {
95 global $core;
96
97 $page = $core->request->context->page;
98 $title = $page->document_title;
99 $site_title = $page->site->title;
100
101 $event->title = $title . $event->separator . $site_title;
102 }
103
104 105 106 107 108
109 static public function before_document_render_metas(\Icybee\Document\BeforeRenderMetasEvent $event)
110 {
111 global $core;
112
113 $page = $core->request->context->page;
114 $node = isset($page->node) ? $page->node : null;
115 $description = $page->description;
116
117 if ($node instanceof Content)
118 {
119 $description = $page->node->excerpt;
120 }
121
122 if ($description)
123 {
124 $description = html_entity_decode($description, ENT_QUOTES, \ICanBoogie\CHARSET);
125 $description = trim(strip_tags($description));
126 $description = preg_replace('#\s+#', ' ', $description);
127
128 $event->metas['Description'] = $description;
129 }
130
131 if ($page->is_home)
132 {
133 $value = $page->site->metas['google_site_verification'];
134
135 if ($value)
136 {
137 $event->metas['google-site-verification'] = $value;
138 }
139 }
140 }
141
142 143 144 145 146
147 static public function on_document_render_metas(\Icybee\Document\RenderMetasEvent $event)
148 {
149 global $core;
150
151 $page = $core->request->context->page;
152 $node = isset($page->node) ? $page->node : null;
153
154
155
156
157
158
159
160 if ($node && $node->has_property('absolute_url'))
161 {
162 $event->html .= '<link rel="canonical" href="' . $node->absolute_url . '" />' . PHP_EOL;
163 }
164 }
165
166 167 168 169 170 171 172
173 static public function on_site_editblock_alter_children(AlterChildrenEvent $event, \Icybee\Modules\Sites\EditBlock $block)
174 {
175 $event->attributes[Element::GROUPS]['seo'] = array
176 (
177 'title' => 'SEO',
178 'weight' => 40
179 );
180
181 $event->children = array_merge
182 (
183 $event->children, array
184 (
185 'metas[google_analytics_ua]' => new Text
186 (
187 array
188 (
189 Group::LABEL => 'Google Analytics UA',
190 Element::GROUP => 'seo'
191 )
192 ),
193
194 'metas[google_site_verification]' => new Text
195 (
196 array
197 (
198 Group::LABEL => 'Google Site Verification',
199 Element::GROUP => 'seo'
200 )
201 )
202 )
203 );
204 }
205
206 207 208 209 210 211
212 static public function on_page_editblock_alter_children(AlterChildrenEvent $event, \Icybee\Modules\Pages\EditBlock $block)
213 {
214 global $core;
215
216 $event->attributes[Element::GROUPS]['seo'] = array
217 (
218 'title' => 'SEO',
219 'weight' => 40
220 );
221
222
223
224
225
226
227
228 $event->children['metas[document_title]'] = new Text
229 (
230 array
231 (
232 Group::LABEL => 'document_title',
233 Element::GROUP => 'seo',
234 Element::DESCRIPTION => 'document_title'
235 )
236 );
237
238 $event->children['metas[description]'] = new Element
239 (
240 'textarea', array
241 (
242 Group::LABEL => 'description',
243 Element::GROUP => 'seo',
244 Element::DESCRIPTION => 'description',
245
246 'rows' => 3
247 )
248 );
249 }
250
251 252 253 254 255 256
257 static public function on_operation_export(Operation\ProcessEvent $event, Operation $operation)
258 {
259 global $core;
260
261 $records = &$event->rc;
262 $keys = array_keys($records);
263
264 $metas = $core->models['registry/node']->where(array('targetid' => $keys, 'name' => array('document_title', 'description')))->all(\PDO::FETCH_NUM);
265
266 foreach ($metas as $meta)
267 {
268 list($pageid, $property, $value) = $meta;
269
270 $records[$pageid]->seo[$property] = $value;
271 }
272 }
273 }