Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • Hooks
  • Module
  1 <?php
  2 
  3 /*
  4  * This file is part of the Icybee package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 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 // http://www.google.com/webmasters/docs/search-engine-optimization-starter-guide.pdf
 28 
 29 class Hooks
 30 {
 31     /**
 32      * Adds the Google Analytics script at the end of the body, unless one of the following
 33      * conditions is met:
 34      *
 35      * - "localhost" is in the server name.
 36      * - The user is the admin.
 37      * - The page or the displayed record is offline.
 38      *
 39      * @param PageController\RenderEvent $event
 40      * @param PageController $target
 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         // http://googlecode.blogspot.com/2009/12/google-analytics-launches-asynchronous.html
 61         // http://code.google.com/intl/fr/apis/analytics/docs/tracking/asyncUsageGuide.html
 62         // http://www.google.com/support/googleanalytics/bin/answer.py?answer=174090&cbid=-yb2wwt7lxo0o&src=cb&lev=%20index
 63         // http://developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_5/
 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      * Replaces the title of the document with the SEO title before the title is rendered.
 90      *
 91      * @param \Icybee\Document\BeforeRenderTitleEvent $event
 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      * Adds the `Description` and `google-site-verification` metas.
106      *
107      * @param \Icybee\Document\BeforeRenderMetasEvent $event
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      * Adds the canonical address of the document.
144      *
145      * @param \Icybee\Document\RenderMetasEvent $event
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         # canonical
156         #
157 
158         // http://yoast.com/articles/duplicate-content/
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      * Extends the site edit block with a `SEO` group and controls for the Google Analytics UA
168      * and the Google Site Verification key.
169      *
170      * @param AlterChildrenEvent $event
171      * @param \Icybee\Modules\Sites\EditBlock $block
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      * Adds controls to edit the SEO title and description of the page.
208      *
209      * @param AlterChildrenEvent $event
210      * @param \Icybee\Modules\Pages\EditBlock $block
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         # http://www.google.com/support/webmasters/bin/answer.py?answer=35264&hl=fr
224         # http://googlewebmastercentral.blogspot.com/2009/09/google-does-not-use-keywords-meta-tag.html
225         # http://www.google.com/support/webmasters/bin/answer.py?answer=79812
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      * Adds SEO properties to exported pages.
253      *
254      * @param Operation\ProcessEvent $event
255      * @param Operation $operation
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 }
Autodoc API documentation generated by ApiGen 2.8.0