PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/app/code/community/Fishpig/Wordpress/Helper/Post.php

https://bitbucket.org/kiids/fishpig-wordpress-integration
PHP | 505 lines | 320 code | 70 blank | 115 comment | 105 complexity | b891ff348b7f0b85f1abcb96c4bae93f 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_Helper_Post extends Fishpig_Wordpress_Helper_Abstract
  9. {
  10. /**
  11. * Variable used for post ID's when using Guid links
  12. *
  13. * @var string
  14. */
  15. protected $_postIdVar = 'p';
  16. /**
  17. * Returns TRUE is ?p=id links are being used
  18. *
  19. * @return bool
  20. */
  21. public function useGuidLinks()
  22. {
  23. return !trim($this->getWpOption('permalink_structure'), '/ -');
  24. }
  25. /**
  26. * Returns the permalink structure stored in the WP database
  27. *
  28. * @return string
  29. */
  30. protected function _getPermalinkStructure()
  31. {
  32. if (!$this->useGuidLinks()) {
  33. $structure = trim($this->getWpOption('permalink_structure'), ' /');
  34. if (Mage::helper('wordpress')->isWordpressMU()) {
  35. if (strpos($structure, 'blog/') === 0) {
  36. $structure = substr($structure, 5);
  37. }
  38. }
  39. return $structure;
  40. }
  41. return false;
  42. }
  43. /**
  44. * Retrieve the permalink structure in array format
  45. * Each different section is separated
  46. *
  47. * @return array
  48. */
  49. protected function _getExplodedPermalinkStructure()
  50. {
  51. if ($this->useGuidLinks()) {
  52. return array();
  53. }
  54. else {
  55. $structure = $this->_getPermalinkStructure();
  56. $parts = preg_split("/(\/|-)/", $structure, -1, PREG_SPLIT_DELIM_CAPTURE);
  57. $structure = array();
  58. foreach($parts as $part) {
  59. if ($result = preg_split("/(%[a-zA-Z0-9_]{1,}%)/", $part, -1, PREG_SPLIT_DELIM_CAPTURE)) {
  60. $results = array_filter(array_unique($result));
  61. foreach($results as $result) {
  62. array_push($structure, $result);
  63. }
  64. }
  65. else {
  66. $structure[] = $part;
  67. }
  68. }
  69. return $structure;
  70. }
  71. }
  72. /**
  73. * Retrieve the pattern used to match the URL to the permalink structure
  74. *
  75. * @return string
  76. */
  77. protected function _getPermalinkPattern()
  78. {
  79. $routerHelper = Mage::helper('wordpress/router');
  80. if ($structure = $this->_getExplodedPermalinkStructure()) {
  81. foreach($structure as $i => $part) {
  82. if (preg_match('/^\%[a-zA-Z0-9_-]{1,}\%$/', $part)) {
  83. $part = trim($part, '%');
  84. if ($part === 'year') {
  85. $part = '[1-2]{1}[0-9]{3}';
  86. }
  87. else if ($part === 'monthnum') {
  88. $part = '[0-1]{1}[0-9]{1}';
  89. }
  90. else if ($part === 'day') {
  91. $part = '[0-3]{1}[0-9]{1}';
  92. }
  93. else if ($part === 'hour') {
  94. $part = '[0-2]{1}[0-9]{1}';
  95. }
  96. else if ($part === 'minute') {
  97. $part = '[0-5]{1}[0-9]{1}';
  98. }
  99. else if ($part === 'second') {
  100. $part = '[0-5]{1}[0-9]{1}';
  101. }
  102. else if ($part === 'post_id') {
  103. $part = '[0-9]{1,}';
  104. }
  105. else if ($part === 'postname') {
  106. $part = $routerHelper->getPermalinkStringRegex();
  107. }
  108. else if ($part === 'category') {
  109. $part = $routerHelper->getPermalinkStringRegex();
  110. }
  111. else if ($part === 'author') {
  112. $part = $routerHelper->getPermalinkStringRegex();
  113. }
  114. else {
  115. $response = new Varien_Object(array('value' => false));
  116. Mage::dispatchEvent('wordpress_permalink_segment_unknown_pattern', array('response' => $response, 'segment' => $part));
  117. if ($response->getValue() !== false) {
  118. $part = $response->getValue();
  119. }
  120. }
  121. $part = '(' . $part . ')';
  122. }
  123. else {
  124. $part = preg_replace('/([.|\\\|\/]{1})/i', '\\\$1', $part);
  125. }
  126. $structure[$i] = $part;
  127. }
  128. return '^' . implode('', $structure) . '$';
  129. }
  130. return false;
  131. }
  132. /**
  133. * Retrieve an array of the 'tokens' from the permalink structure
  134. * A token is part of the structure that relates to dynamic post informat
  135. * eg. %post_id% or %postname% etc
  136. *
  137. * @return false|array
  138. */
  139. protected function _getPermalinkTokens()
  140. {
  141. if ($format = $this->_getExplodedPermalinkStructure()) {
  142. foreach($format as $i => $part) {
  143. if (!preg_match("/^\%([a-zA-Z0-9_\-]{1,})\%$/", $part)) {
  144. unset($format[$i]);
  145. }
  146. else {
  147. $format[$i] = trim($part, '%');
  148. }
  149. }
  150. return array_values($format);
  151. }
  152. return false;
  153. }
  154. /**
  155. * Determine whether the URI is a post URI
  156. * This function accepts URI's generated by Fishpig_Wordpress_Helper_Router::getBlogUri
  157. *
  158. * @param string
  159. * @return bool
  160. */
  161. public function isPostUri($uri, $returnParts = false)
  162. {
  163. if ($uri === 'index.php') {
  164. return false;
  165. }
  166. if ($pattern = $this->_getPermalinkPattern()) {
  167. $results = array();
  168. if (preg_match("/" . $pattern . "/iu", $uri, $results)) {
  169. array_shift($results);
  170. return $returnParts ? $results : true;
  171. }
  172. }
  173. return $this->getPostId();
  174. }
  175. /**
  176. * Determine whether the URI is a post URI
  177. * This function accepts URI's generated by Fishpig_Wordpress_Helper_Router::getBlogUri
  178. *
  179. * @param string
  180. * @return bool
  181. */
  182. public function isPostAttachmentUri($uri)
  183. {
  184. if (strpos($uri, '/') !== false) {
  185. $postUri = substr($uri, 0, strrpos($uri, '/'));
  186. if ($this->isPostUri($postUri)) {
  187. $attachmentUri = trim(substr($uri, strrpos($uri, '/')), '/');
  188. return Mage::getResourceModel('wordpress/image')->isImagePostName($attachmentUri);
  189. }
  190. }
  191. return false;
  192. }
  193. /**
  194. * Retrieve an associative array of token => value for loading a post by it's permalink
  195. *
  196. * @param string $uri
  197. * @return array
  198. */
  199. protected function _getTokenValueArray($uri)
  200. {
  201. if (($tokens = $this->_getPermalinkTokens()) && ($values = $this->isPostUri($uri, true))) {
  202. if (count($tokens) == count($values)) {
  203. $loadValues = array();
  204. foreach($tokens as $i => $token) {
  205. $loadValues[$token] = $values[$i];
  206. }
  207. return $loadValues;
  208. }
  209. }
  210. return false;
  211. }
  212. /**
  213. * Loads a post model based on the URI (array)
  214. *
  215. * @param string|array $explodedUri
  216. * @return FIshpig_Wordpress_Model_Post
  217. */
  218. public function loadByPermalink($uri)
  219. {
  220. if ($this->useGuidLinks()) {
  221. return Mage::getModel('wordpress/post')->load($this->getPostId());
  222. }
  223. if ($loadTokens = $this->_getTokenValueArray($uri)) {
  224. $posts = Mage::getResourceModel('wordpress/post_collection')->addIsPublishedFilter();
  225. $date = array();
  226. $time = array();
  227. foreach($loadTokens as $token => $value) {
  228. if ($token === 'year') {
  229. $date[0] = $value;
  230. }
  231. else if ($token === 'monthnum') {
  232. $date[1] = $value;
  233. }
  234. else if ($token === 'day') {
  235. $date[2] = $value;
  236. }
  237. else if ($token === 'hour') {
  238. $time[0] = $value;
  239. }
  240. else if ($token === 'minute') {
  241. $time[1] = $value;
  242. }
  243. else if ($token === 'second') {
  244. $time[2] = $value;
  245. }
  246. else if ($token === 'post_id') {
  247. $posts->addFieldToFilter('ID', $value);
  248. }
  249. else if ($token === 'postname') {
  250. $posts->addFieldToFilter('post_name', strtolower(urlencode($value)));
  251. }
  252. else if ($token === 'category') {
  253. $posts->addCategorySlugFilter($value);
  254. }
  255. else if ($token === 'author') {
  256. }
  257. else {
  258. Mage::dispatchEvent('wordpress_permalink_segment_unknown_load', array('collection' => $posts, 'segment' => $token, 'value' => $value));
  259. }
  260. }
  261. if (($dateStr = $this->_craftDateString($date, $time)) !== false) {
  262. $posts->addPostDateFilter($dateStr);
  263. }
  264. $posts->setCurPage(1)->setPageSize(1)->load();
  265. if (count($posts) == 1) {
  266. return $posts->getFirstItem();
  267. }
  268. }
  269. return false;
  270. }
  271. /**
  272. * return the permalink based on permalink structure
  273. * which is defined in WP Admin
  274. *
  275. * @param Fishpig_Wordpress_Model_Post
  276. * @return string
  277. */
  278. public function getPermalink(Fishpig_Wordpress_Model_Post $post)
  279. {
  280. if ($this->useGuidLinks()) {
  281. return $this->getUrl('?p='.$post->getId());
  282. }
  283. else {
  284. $structure = $this->_getExplodedPermalinkStructure();
  285. if (count($structure) > 0) {
  286. $url = array();
  287. foreach($structure as $part) {
  288. if (preg_match('/^\%[a-zA-Z0-9_]{1,}\%$/', $part)) {
  289. $part = trim($part, '%');
  290. if ($part === 'year') {
  291. $url[] = $post->getPostDate('Y');
  292. }
  293. else if ($part === 'monthnum') {
  294. $url[] = $post->getPostDate('m');
  295. }
  296. else if ($part === 'day') {
  297. $url[] = $post->getPostDate('d');
  298. }
  299. else if ($part === 'hour') {
  300. $url[] = $post->getPostDate('H');
  301. }
  302. else if ($part === 'minute') {
  303. $url[] = $post->getPostDate('i');
  304. }
  305. else if ($part === 'second') {
  306. $url[] = $post->getPostDate('s');
  307. }
  308. else if ($part === 'post_id') {
  309. $url[] = $post->getId();
  310. }
  311. else if ($part === 'postname') {
  312. $url[] = urldecode($post->getPostName());
  313. }
  314. else if ($part === 'category') {
  315. $url[] = $this->_getPermalinkCategoryPortion($post);
  316. }
  317. else if ($part === 'author') {
  318. }
  319. else {
  320. $response = new Varien_Object(array('value' => false));
  321. Mage::dispatchEvent('wordpress_permalink_segment_unknown_getpermalink', array('response' => $response, 'post' => $post, 'segment' => $part));
  322. if ($response->getValue() !== false) {
  323. $url[] = $response->getValue();
  324. }
  325. }
  326. }
  327. else {
  328. if ($part === '/') {
  329. $partCount = count($url);
  330. if ($partCount > 0 && $url[$partCount-1]===$part) {
  331. continue;
  332. }
  333. }
  334. $url[] = $part;
  335. }
  336. }
  337. if ($this->permalinkHasTrainingSlash()) {
  338. $url[count($url)-1] .= '/';
  339. }
  340. return $this->getUrl(implode('', $url));
  341. }
  342. }
  343. }
  344. /**
  345. * Craft a date string to help load posts
  346. *
  347. * @param array $date = array
  348. * @param array $time = array
  349. * @return string
  350. */
  351. protected function _craftDateString(array $date = array(), array $time = array())
  352. {
  353. if (count($date) > 0 || count($time) > 0) {
  354. $dateStr = '';
  355. foreach(array('-' => $date, ':' => $time) as $sep => $values) {
  356. for($i = 0; $i <= 2; $i++ ) {
  357. $dateStr .= (isset($values[$i]) ? $values[$i] : '%') . $sep;
  358. }
  359. $dateStr = rtrim($dateStr, $sep) . ' ';
  360. }
  361. return rtrim($dateStr);
  362. }
  363. return false;
  364. }
  365. /**
  366. * Generates the category portion of the URL for a post
  367. *
  368. * @param Fishpig_Wordpress_Model_Post $post
  369. * @return string
  370. */
  371. protected function _getPermalinkCategoryPortion(Fishpig_Wordpress_Model_Post $post)
  372. {
  373. if ($category = $post->getParentCategory()) {
  374. return trim($category->getSlug(), '/');
  375. }
  376. }
  377. /**
  378. * Retrieve the post ID from the query string
  379. *
  380. * @return string
  381. */
  382. public function getPostId()
  383. {
  384. $postId = Mage::app()->getRequest()->getParam($this->getPostIdVar(), false);
  385. if ($postId === false && $this->isPreview()) {
  386. $postId = Mage::app()->getRequest()->getParam('preview_id', false);
  387. }
  388. return $postId;
  389. }
  390. /**
  391. * Determine whether we are previewing a post
  392. *
  393. * @return bool
  394. */
  395. public function isPreview()
  396. {
  397. return Mage::app()->getRequest()->getParam('preview', false);
  398. }
  399. /**
  400. * Retrieve the variable used for post ID's when using Guid links
  401. *
  402. * @return string
  403. */
  404. public function getPostIdVar()
  405. {
  406. return $this->_postIdVar;
  407. }
  408. /**
  409. * Retrieve the URL for the tags page
  410. *
  411. * @return string
  412. */
  413. public function getTagsUrl()
  414. {
  415. return $this->getUrl('tags');
  416. }
  417. /**
  418. * Retrieve the number of comments to display per page
  419. *
  420. * @return int
  421. */
  422. public function getCommentsPerPage()
  423. {
  424. return $this->getWpOption('page_comments') ? Mage::helper('wordpress')->getWpOption('comments_per_page', 50) : 0;
  425. }
  426. /**
  427. * Determine whether the permalink has a trailing slash
  428. *
  429. * @return bool
  430. */
  431. public function permalinkHasTrainingSlash()
  432. {
  433. return substr($this->getWpOption('permalink_structure'), -1) == '/';
  434. }
  435. }