/app/code/community/Fishpig/Wordpress/Helper/Router.php

https://github.com/seloshow/Magento-Pruebas · PHP · 340 lines · 161 code · 45 blank · 134 comment · 25 complexity · cf0b5b1a4337cde6988747567a5d51f2 MD5 · raw file

  1. <?php
  2. class Fishpig_Wordpress_Helper_Router extends Fishpig_Wordpress_Helper_Abstract
  3. {
  4. /**
  5. * The variable used for pages
  6. *
  7. * @var string
  8. */
  9. protected $_postPagerVar = 'page';
  10. /**
  11. * The variable format used for comment pages
  12. *
  13. * @var string
  14. */
  15. protected $_commentPagerVarFormat = '^comment-page-%s$';
  16. /**
  17. * The variable used to indicate this is a feed page
  18. *
  19. * @var string
  20. */
  21. protected $_feedVar = 'feed';
  22. /**
  23. * The variable used to indicate a trackback page
  24. *
  25. * @var string
  26. */
  27. protected $_trackbackVar = 'trackback';
  28. /**
  29. * Retrieve the blog URI
  30. * This is the whole URI after blog route
  31. *
  32. * @return string
  33. */
  34. public function getBlogUri()
  35. {
  36. $pathInfo = explode('/', strtolower(trim($this->getRequest()->getPathInfo(), '/')));
  37. if (count($pathInfo) == 0) {
  38. return null;
  39. }
  40. if ($pathInfo[0] != $this->getBlogRoute()) {
  41. return null;
  42. }
  43. // Remove blog route
  44. array_shift($pathInfo);
  45. // Clean off pager and feed parts
  46. if (($key = array_search($this->getPostPagerVar(), $pathInfo)) !== false) {
  47. if (isset($pathInfo[($key+1)]) && preg_match("/[0-9]{1,}/", $pathInfo[($key+1)])) {
  48. $this->getRequest()->setParam($this->getPostPagerVar(), $pathInfo[($key+1)]);
  49. unset($pathInfo[($key+1)]);
  50. unset($pathInfo[$key]);
  51. $pathInfo = array_values($pathInfo);
  52. }
  53. }
  54. // Clean off feed and trackback variable
  55. foreach(array($this->getFeedVar(), $this->getTrackbackVar()) as $var) {
  56. if (($key = array_search($var, $pathInfo)) !== false) {
  57. unset($pathInfo[$key]);
  58. $pathInfo = array_values($pathInfo);
  59. $this->getRequest()->setParam($var, 1);
  60. }
  61. }
  62. // Remove comments pager variable
  63. foreach($pathInfo as $i => $part) {
  64. $results = array();
  65. if (preg_match("/" . sprintf($this->getCommentPagerVarFormat(), '([0-9]{1,})') . "/", $part, $results)) {
  66. if (isset($results[1])) {
  67. unset($pathInfo[$i]);
  68. }
  69. }
  70. }
  71. if (count($pathInfo) == 1 && preg_match("/^[0-9]{1,8}$/", $pathInfo[0])) {
  72. $this->getRequest()->setParam(Mage::helper('wordpress/post')->getPostIdVar(), $pathInfo[0]);
  73. array_shift($pathInfo);
  74. }
  75. return urldecode(implode('/', $pathInfo));
  76. }
  77. /**
  78. * Determines whether the uri is a blog archive URI
  79. *
  80. * @param string $uri
  81. * @return bool
  82. */
  83. public function isArchiveUri($uri)
  84. {
  85. $pattern['year'] = '[1-2]{1}[0-9]{3}';
  86. $pattern['month'] = '[0-1]{1}[0-9]{1}';
  87. $pattern['day'] = '[0-3]{1}[0-9]{1}';
  88. return preg_match("/^" . implode('\/', $pattern) . "$/", $uri)
  89. || preg_match("/^" . $pattern['year'] . '\/' . $pattern['month'] . '$/', $uri);
  90. }
  91. /**
  92. * Determine whether the URI is a blog author uri
  93. *
  94. * @param string $uri
  95. * @return bool
  96. */
  97. public function isAuthorUri($uri)
  98. {
  99. return preg_match('/^author\/' . $this->getPermalinkStringRegex() . '$/i', $uri);
  100. }
  101. /**
  102. * Determine whether the URI is a blog category URI
  103. *
  104. * @param string
  105. * @return bool
  106. */
  107. public function isCategoryUri($uri)
  108. {
  109. if (Mage::helper('wordpress')->isPluginEnabled('No Category Base')) {
  110. $category = Mage::getModel('wordpress/post_category')->loadBySlug($uri);
  111. return $category && $category->getId();
  112. }
  113. return preg_match('/^' . str_replace('/', '\/', trim(Mage::helper('wordpress/router')->getCategoryBase(), '/')) . '\/' . $this->getPermalinkStringRegex() . '$/i', $uri);
  114. }
  115. /**
  116. * Determine whether the URI is a blog tag URI
  117. *
  118. * @param string $uri
  119. * @return bool
  120. */
  121. public function isTagUri($uri)
  122. {
  123. return preg_match('/^' . Mage::helper('wordpress/router')->getTagBase() . '\/' . $this->getPermalinkStringRegex() . '$/', $uri);
  124. }
  125. /**
  126. * Determine whether the URI is a blog post URI
  127. *
  128. * @param string $uri
  129. * @return bool
  130. */
  131. public function isPostUri($uri)
  132. {
  133. return Mage::helper('wordpress/post')->isPostUri($uri);
  134. }
  135. /**
  136. * Determine whether the URI is a blog post attachment URI
  137. *
  138. * @param string $uri
  139. * @return bool
  140. */
  141. public function isPostAttachmentUri($uri)
  142. {
  143. return Mage::helper('wordpress/post')->isPostAttachmentUri($uri);
  144. }
  145. /**
  146. * Determine whether the URL is a page URI
  147. *
  148. * @param string $uri
  149. * @param bool $registerPage = false
  150. * @return bool
  151. */
  152. public function isPageUri($uri, $registerPage = false)
  153. {
  154. $uris = explode('/', $uri);
  155. $pages = array();
  156. $count = 0;
  157. foreach($uris as $uri) {
  158. $page = Mage::getModel('wordpress/page')->loadBySlug($uri);
  159. if (!$page->getId()) {
  160. return false;
  161. }
  162. if ($count++ > 0) {
  163. $lastPage = end($pages);
  164. $page->setParentPage($lastPage);
  165. reset($pages);
  166. }
  167. else {
  168. if ($page->getPostParent() > 0) {
  169. return false;
  170. }
  171. }
  172. $pages[] = $page;
  173. }
  174. if ($registerPage) {
  175. $page = array_pop($pages);
  176. Mage::register('wordpress_page', $page, true);
  177. }
  178. return true;
  179. }
  180. /**
  181. * Trim the base from the URI
  182. *
  183. * @param string $uri
  184. * @param string $base
  185. * @param string $ltrim
  186. * @return string
  187. */
  188. public function trimUriBase($uri, $base, $ltrim = '/')
  189. {
  190. if (substr($uri, 0, strlen($base)) == $base) {
  191. $uri = substr($uri, strlen($base));
  192. if (!is_null($ltrim)) {
  193. $uri = ltrim($uri, $ltrim);
  194. }
  195. }
  196. return $uri;
  197. }
  198. /**
  199. * Retrieve the URI with the base portion trimmed off
  200. *
  201. * @param string $base
  202. * @param string $ltrim
  203. * @return string
  204. */
  205. public function getTrimmedUri($base, $ltrim = '/')
  206. {
  207. return $this->trimUriBase($this->getBlogUri(), $base, $ltrim);
  208. }
  209. /**
  210. * Retrieve the category base
  211. *
  212. * @return string
  213. */
  214. public function getCategoryBase()
  215. {
  216. return Mage::helper('wordpress')->getWpOption('category_base', 'category');
  217. }
  218. /**
  219. * Retrieve the tag base
  220. *
  221. * @return string
  222. */
  223. public function getTagBase()
  224. {
  225. return Mage::helper('wordpress')->getWpOption('tag_base', 'tag');
  226. }
  227. /**
  228. * Retrieve the Regex pattern used to identify a permalink string
  229. * Allows for inclusion of other locale characters
  230. *
  231. * @return string
  232. */
  233. public function getPermalinkStringRegex()
  234. {
  235. return '[a-z0-9' . $this->getSpecialUriChars() . '_\-\.]{1,}';
  236. }
  237. /**
  238. * Retrieve an array of special chars that can be used in a URI
  239. *
  240. * @return array
  241. */
  242. public function getSpecialUriChars()
  243. {
  244. $chars = array('‘', '’','“', '”', '–', '—', '`');
  245. if (Mage::helper('wordpress')->isCryllicLocaleEnabled()) {
  246. $chars[] = '\p{Cyrillic}';
  247. }
  248. return implode('', $chars);
  249. }
  250. /**
  251. * Retrieve the format variable for the comment pager
  252. *
  253. * @return string
  254. */
  255. public function getCommentPagerVarFormat()
  256. {
  257. return $this->_commentPagerVarFormat;
  258. }
  259. /**
  260. * Retrieve the post pager variable
  261. *
  262. * @return string
  263. */
  264. public function getPostPagerVar()
  265. {
  266. return $this->_postPagerVar;
  267. }
  268. /**
  269. * Retrieve the feed variable
  270. *
  271. * @return string
  272. */
  273. public function getFeedVar()
  274. {
  275. return $this->_feedVar;
  276. }
  277. /**
  278. * Retrieve the trackback variable
  279. *
  280. * @return string
  281. */
  282. public function getTrackbackVar()
  283. {
  284. return $this->_trackbackVar;
  285. }
  286. /**
  287. * Retrieve the request object
  288. *
  289. * @return
  290. */
  291. public function getRequest()
  292. {
  293. return Mage::app()->getRequest();
  294. }
  295. }