/www/shop/engine/Shopware/Plugins/Default/Frontend/Seo/Bootstrap.php

https://bitbucket.org/weberlars/sot-shopware · PHP · 167 lines · 87 code · 17 blank · 63 comment · 15 complexity · 1bab64ee6ef31b98b5ea262762714717 MD5 · raw file

  1. <?php
  2. /**
  3. * Shopware 4.0
  4. * Copyright Š 2012 shopware AG
  5. *
  6. * According to our dual licensing model, this program can be used either
  7. * under the terms of the GNU Affero General Public License, version 3,
  8. * or under a proprietary license.
  9. *
  10. * The texts of the GNU Affero General Public License with an additional
  11. * permission and of our proprietary license can be found at and
  12. * in the LICENSE file you have received along with this program.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * "Shopware" is a registered trademark of shopware AG.
  20. * The licensing of the program under the AGPLv3 does not imply a
  21. * trademark license. Therefore any rights, title and interest in
  22. * our trademarks remain entirely with us.
  23. *
  24. * @category Shopware
  25. * @package Shopware_Plugins
  26. * @subpackage Seo
  27. * @copyright Copyright (c) 2012, shopware AG (http://www.shopware.de)
  28. * @version $Id$
  29. * @author Heiner Lohaus
  30. * @author $Author$
  31. */
  32. /**
  33. * Shopware SEO Plugin
  34. *
  35. * todo@all: Documentation
  36. */
  37. class Shopware_Plugins_Frontend_Seo_Bootstrap extends Shopware_Components_Plugin_Bootstrap
  38. {
  39. /**
  40. * Install SEO-Plugin
  41. * @return bool
  42. */
  43. public function install()
  44. {
  45. $this->subscribeEvent(
  46. 'Enlight_Plugins_ViewRenderer_FilterRender',
  47. 'onFilterRender'
  48. );
  49. $this->subscribeEvent(
  50. 'Enlight_Controller_Action_PostDispatch',
  51. 'onPostDispatch'
  52. );
  53. return true;
  54. }
  55. /**
  56. * Optimize Sourcecode / Apply seo rules
  57. *
  58. * @param Enlight_Event_EventArgs $args
  59. * @return void
  60. */
  61. public function onPostDispatch(Enlight_Event_EventArgs $args)
  62. {
  63. $request = $args->getSubject()->Request();
  64. $response = $args->getSubject()->Response();
  65. $view = $args->getSubject()->View();
  66. if (!$request->isDispatched() || $response->isException()
  67. || $request->getModuleName() != 'frontend'
  68. || !$view->hasTemplate()
  69. ) {
  70. return;
  71. }
  72. $config = Shopware()->Config();
  73. $controllerBlacklist = preg_replace('#\s#', '', $config['sSEOVIEWPORTBLACKLIST']);
  74. $controllerBlacklist = explode(',', $controllerBlacklist);
  75. $queryBlacklist = preg_replace('#\s#', '', $config['sSEOQUERYBLACKLIST']);
  76. $queryBlacklist = explode(',', $queryBlacklist);
  77. if (!empty($config['sSEOMETADESCRIPTION'])) {
  78. if (!empty($view->sArticle['metaDescription'])) {
  79. $metaDescription = $view->sArticle['metaDescription'];
  80. } elseif (!empty($view->sArticle['description'])) {
  81. $metaDescription = $view->sArticle['description'];
  82. } elseif (!empty($view->sArticle['description_long'])) {
  83. $metaDescription = $view->sArticle['description_long'];
  84. } elseif (!empty($view->sCategoryContent['metaDescription'])) {
  85. $metaDescription = $view->sCategoryContent['metaDescription'];
  86. } elseif (!empty($view->sCategoryContent['cmstext'])) {
  87. $metaDescription = $view->sCategoryContent['cmstext'];
  88. }
  89. if (!empty($metaDescription)) {
  90. $metaDescription = html_entity_decode($metaDescription, ENT_COMPAT, 'UTF-8');
  91. $metaDescription = trim(preg_replace('/\s\s+/', ' ', strip_tags($metaDescription)));
  92. $metaDescription = htmlspecialchars($metaDescription);
  93. }
  94. }
  95. $controller = $request->getControllerName();
  96. if (!empty($controllerBlacklist) && in_array($controller, $controllerBlacklist)) {
  97. $metaRobots = 'noindex,follow';
  98. } elseif (!empty($queryBlacklist)) {
  99. foreach ($queryBlacklist as $queryKey) {
  100. if ($request->getQuery($queryKey) !== null) {
  101. $metaRobots = 'noindex,follow';
  102. }
  103. }
  104. }
  105. $view->extendsTemplate('frontend/plugins/seo/index.tpl');
  106. if (!empty($metaRobots)) {
  107. $view->SeoMetaRobots = $metaRobots;
  108. }
  109. if (!empty($metaDescription)) {
  110. $view->SeoMetaDescription = $metaDescription;
  111. }
  112. }
  113. /**
  114. * Remove html-comments / whitespaces
  115. *
  116. * @param Enlight_Event_EventArgs $args
  117. * @return mixed|string
  118. */
  119. public function onFilterRender(Enlight_Event_EventArgs $args)
  120. {
  121. $source = $args->getReturn();
  122. if (strpos($source, '<html') === false) {
  123. return $source;
  124. }
  125. $config = Shopware()->Config();
  126. // Remove comments
  127. // && empty($template->_tpl_vars['debug_output'] todo@all property tpl_vars seems to not exist in smarty 3.1
  128. if (!empty($config['sSEOREMOVECOMMENTS'])) {
  129. $source = str_replace(array("\r\n", "\r"), "\n", $source);
  130. $expressions = array(
  131. // Remove comments
  132. '#(<(?:script|pre|textarea)[^>]*?>.*?</(?:script|pre|textarea)>)|(<style[^>]*?>.*?</style>)|(<!--\[.*?\]-->)|<!--.*?-->#msiS' => '$1$2$3',
  133. // remove spaces between attributes (but not in attribute values!)
  134. '#(([a-z0-9]\s*=\s*(["\'])[^\3]*?\3)|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \4',
  135. // note: for some very weird reason trim() seems to remove spaces inside attributes.
  136. // maybe a \0 byte or something is interfering?
  137. '#^\s+#ms' => '',
  138. '#\s+$#ms' => '',
  139. );
  140. $source = preg_replace(array_keys($expressions), array_values($expressions), $source);
  141. }
  142. // Trim whitespace
  143. // $template = Shopware()->Template();
  144. // if(!empty($config['sSEOREMOVEWHITESPACES'])&&empty($template->_tpl_vars['debug_output'])) {
  145. // require_once(SMARTY_DIR.'plugins/outputfilter.trimwhitespace.php');
  146. // $source = smarty_outputfilter_trimwhitespace($source, $template);
  147. // }
  148. return $source;
  149. }
  150. }