PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/app/code/community/Fishpig/Wordpress/Controller/Router.php

https://bitbucket.org/kiids/fishpig-wordpress-integration
PHP | 139 lines | 80 code | 27 blank | 32 comment | 13 complexity | fe49473fe3cbf77213eac46e41d4141d MD5 | raw file
  1. <?php
  2. /**
  3. * @category Fishpig
  4. * @package Fishpig_Wordpress
  5. * @license http://fishpig.co.uk/license.txt
  6. * @author Ben Tideswell <help@fishpig.co.uk>
  7. */
  8. class Fishpig_Wordpress_Controller_Router extends Fishpig_Wordpress_Controller_Router_Abstract
  9. {
  10. /**
  11. * Create an instance of the router and add it to the queue
  12. *
  13. * @param Varien_Event_Observer $observer
  14. * @return bool
  15. */
  16. public function initControllerObserver(Varien_Event_Observer $observer)
  17. {
  18. if (Mage::helper('wordpress')->isEnabled()) {
  19. return parent::initControllerObserver($observer);
  20. }
  21. return false;
  22. }
  23. /**
  24. * Performs the logic for self::match
  25. *
  26. * @param string $uri
  27. * @return bool
  28. */
  29. protected function _match($uri)
  30. {
  31. $helper = Mage::helper('wordpress/router');
  32. Mage::dispatchEvent('wordpress_match_routes_before', array('router' => $this, 'helper' => $helper, 'uri' => $uri));
  33. if ($helper->isPostUri($uri)) {
  34. if (($post = Mage::helper('wordpress/post')->loadByPermalink($uri)) !== false) {
  35. Mage::register('wordpress_post_temp', $post);
  36. return $this->setRoutePath('*/post/view');
  37. }
  38. else if (($postId = Mage::helper('wordpress/post')->getPostId()) !== false) {
  39. return $this->setRoutePath('*/post/view');
  40. }
  41. }
  42. if ($helper->isNoBaseCategoryUri($uri)) {
  43. return $this->setRoutePath('*/post_category/view');
  44. }
  45. if ($helper->isPageUri($uri)) {
  46. return $this->setRoutePath('*/page/view');
  47. }
  48. if ($helper->isTermUri($uri)) {
  49. return $this->setRoutePath('*/term/view');
  50. }
  51. if ($helper->isPostAttachmentUri($uri)) {
  52. return $this->_redirectFromAttachmentUriToPost($uri);
  53. }
  54. Mage::dispatchEvent('wordpress_match_routes_after', array('router' => $this, 'helper' => $helper, 'uri' => $uri));
  55. return $this->_hasValidRouteDetails();
  56. }
  57. /**
  58. * Adds redirects from attachment pages to parent post
  59. * This stops 404 errors showing up in Google Analytics
  60. *
  61. * @param string $uri
  62. * @return bool
  63. */
  64. protected function _redirectFromAttachmentUriToPost($uri)
  65. {
  66. if (strpos($uri, '/') !== false) {
  67. $postUri = substr($uri, 0, strrpos($uri, '/'));
  68. header("HTTP/1.1 301 Moved Permanently");
  69. header('Location: ' . Mage::helper('wordpress')->getUrl($postUri));
  70. exit;
  71. }
  72. return false;
  73. }
  74. /**
  75. * Initialize the static routes used by WordPress
  76. *
  77. * @return $this
  78. */
  79. protected function _initStaticRoutes()
  80. {
  81. if (!Mage::helper('wordpress/post')->getPostId()) {
  82. $this->addStaticRoute('/^$/');
  83. }
  84. $helper = Mage::helper('wordpress/router');
  85. if ($helper->categoryUrlHasBase()) {
  86. $this->addStaticRoute('/^' . $helper->getCategoryBase() . '\/.*$/i', 'view', 'post_category');
  87. }
  88. $tagBase = $helper->getTagBase();
  89. $this ->addStaticRoute('/^' . $tagBase . '\/.*$/', 'view', 'post_tag');
  90. $this->addStaticRoute('/^author\/.*$/', 'view', 'author');
  91. // Archive static routes
  92. $year = '[1-2]{1}[0-9]{3}';
  93. $month = '[0-1]{1}[0-9]{1}';
  94. $day = '[0-3]{1}[0-9]{1}';
  95. $this->addStaticRoute('/^' . $year . '\/' . $month . '[\/]{0,1}$/', 'view', 'archive');
  96. $this->addStaticRoute('/^' . $year . '\/' . $month . '\/' . $day . '[\/]{0,1}$/', 'view', 'archive');
  97. if (Mage::helper('wordpress/search')->isEnabled()) {
  98. $searchBase = Mage::helper('wordpress/search')->getSearchRoute();
  99. $this->addStaticRoute('/^' . $searchBase . '/', 'index', 'search');
  100. }
  101. // Forward certain request directly to WP
  102. $this->addStaticRoute('/^index.php/i', 'forward');
  103. $this->addStaticRoute('/^wp-content\/(.*)/i', 'forwardFile');
  104. $this->addStaticRoute('/^wp-includes\/(.*)/i', 'forwardFile');
  105. $this->addStaticRoute('/^wp-cron.php.*/', 'forwardFile');
  106. $this->addStaticRoute('/^wp-admin[\/]{0,1}$/', 'wpAdmin');
  107. $this->addStaticRoute('/^wp-pass.php.*/', 'applyPostPassword');
  108. $this->addStaticRoute('/^robots.txt$/i', 'robots');
  109. $this->addStaticRoute('/^[^\/]{0,}sitemap[^\/]{0,}.xml$/', 'sitemap');
  110. return parent::_initStaticRoutes();
  111. }
  112. }