/src/app/code/community/Fishpig/Wordpress/Helper/Post.php
PHP | 505 lines | 320 code | 70 blank | 115 comment | 105 complexity | b891ff348b7f0b85f1abcb96c4bae93f MD5 | raw file
- <?php
- /**
- * @category Fishpig
- * @package Fishpig_Wordpress
- * @license http://fishpig.co.uk/license.txt
- * @author Ben Tideswell <help@fishpig.co.uk>
- */
- class Fishpig_Wordpress_Helper_Post extends Fishpig_Wordpress_Helper_Abstract
- {
- /**
- * Variable used for post ID's when using Guid links
- *
- * @var string
- */
- protected $_postIdVar = 'p';
- /**
- * Returns TRUE is ?p=id links are being used
- *
- * @return bool
- */
- public function useGuidLinks()
- {
- return !trim($this->getWpOption('permalink_structure'), '/ -');
- }
-
- /**
- * Returns the permalink structure stored in the WP database
- *
- * @return string
- */
- protected function _getPermalinkStructure()
- {
- if (!$this->useGuidLinks()) {
- $structure = trim($this->getWpOption('permalink_structure'), ' /');
- if (Mage::helper('wordpress')->isWordpressMU()) {
- if (strpos($structure, 'blog/') === 0) {
- $structure = substr($structure, 5);
- }
- }
-
- return $structure;
- }
-
- return false;
- }
-
- /**
- * Retrieve the permalink structure in array format
- * Each different section is separated
- *
- * @return array
- */
- protected function _getExplodedPermalinkStructure()
- {
- if ($this->useGuidLinks()) {
- return array();
- }
- else {
- $structure = $this->_getPermalinkStructure();
- $parts = preg_split("/(\/|-)/", $structure, -1, PREG_SPLIT_DELIM_CAPTURE);
- $structure = array();
- foreach($parts as $part) {
- if ($result = preg_split("/(%[a-zA-Z0-9_]{1,}%)/", $part, -1, PREG_SPLIT_DELIM_CAPTURE)) {
- $results = array_filter(array_unique($result));
- foreach($results as $result) {
- array_push($structure, $result);
- }
- }
- else {
- $structure[] = $part;
- }
- }
- return $structure;
- }
- }
-
- /**
- * Retrieve the pattern used to match the URL to the permalink structure
- *
- * @return string
- */
- protected function _getPermalinkPattern()
- {
- $routerHelper = Mage::helper('wordpress/router');
-
- if ($structure = $this->_getExplodedPermalinkStructure()) {
- foreach($structure as $i => $part) {
- if (preg_match('/^\%[a-zA-Z0-9_-]{1,}\%$/', $part)) {
- $part = trim($part, '%');
-
- if ($part === 'year') {
- $part = '[1-2]{1}[0-9]{3}';
- }
- else if ($part === 'monthnum') {
- $part = '[0-1]{1}[0-9]{1}';
- }
- else if ($part === 'day') {
- $part = '[0-3]{1}[0-9]{1}';
- }
- else if ($part === 'hour') {
- $part = '[0-2]{1}[0-9]{1}';
- }
- else if ($part === 'minute') {
- $part = '[0-5]{1}[0-9]{1}';
- }
- else if ($part === 'second') {
- $part = '[0-5]{1}[0-9]{1}';
- }
- else if ($part === 'post_id') {
- $part = '[0-9]{1,}';
- }
- else if ($part === 'postname') {
- $part = $routerHelper->getPermalinkStringRegex();
- }
- else if ($part === 'category') {
- $part = $routerHelper->getPermalinkStringRegex();
- }
- else if ($part === 'author') {
- $part = $routerHelper->getPermalinkStringRegex();
- }
- else {
- $response = new Varien_Object(array('value' => false));
-
- Mage::dispatchEvent('wordpress_permalink_segment_unknown_pattern', array('response' => $response, 'segment' => $part));
- if ($response->getValue() !== false) {
- $part = $response->getValue();
- }
- }
-
- $part = '(' . $part . ')';
- }
- else {
- $part = preg_replace('/([.|\\\|\/]{1})/i', '\\\$1', $part);
- }
- $structure[$i] = $part;
- }
- return '^' . implode('', $structure) . '$';
- }
-
- return false;
- }
-
- /**
- * Retrieve an array of the 'tokens' from the permalink structure
- * A token is part of the structure that relates to dynamic post informat
- * eg. %post_id% or %postname% etc
- *
- * @return false|array
- */
- protected function _getPermalinkTokens()
- {
- if ($format = $this->_getExplodedPermalinkStructure()) {
- foreach($format as $i => $part) {
- if (!preg_match("/^\%([a-zA-Z0-9_\-]{1,})\%$/", $part)) {
- unset($format[$i]);
- }
- else {
- $format[$i] = trim($part, '%');
- }
- }
-
- return array_values($format);
- }
-
- return false;
- }
-
- /**
- * Determine whether the URI is a post URI
- * This function accepts URI's generated by Fishpig_Wordpress_Helper_Router::getBlogUri
- *
- * @param string
- * @return bool
- */
- public function isPostUri($uri, $returnParts = false)
- {
- if ($uri === 'index.php') {
- return false;
- }
-
- if ($pattern = $this->_getPermalinkPattern()) {
- $results = array();
- if (preg_match("/" . $pattern . "/iu", $uri, $results)) {
- array_shift($results);
- return $returnParts ? $results : true;
- }
- }
- return $this->getPostId();
- }
- /**
- * Determine whether the URI is a post URI
- * This function accepts URI's generated by Fishpig_Wordpress_Helper_Router::getBlogUri
- *
- * @param string
- * @return bool
- */
- public function isPostAttachmentUri($uri)
- {
- if (strpos($uri, '/') !== false) {
- $postUri = substr($uri, 0, strrpos($uri, '/'));
-
- if ($this->isPostUri($postUri)) {
- $attachmentUri = trim(substr($uri, strrpos($uri, '/')), '/');
- return Mage::getResourceModel('wordpress/image')->isImagePostName($attachmentUri);
- }
- }
-
- return false;
- }
-
- /**
- * Retrieve an associative array of token => value for loading a post by it's permalink
- *
- * @param string $uri
- * @return array
- */
- protected function _getTokenValueArray($uri)
- {
- if (($tokens = $this->_getPermalinkTokens()) && ($values = $this->isPostUri($uri, true))) {
- if (count($tokens) == count($values)) {
- $loadValues = array();
-
- foreach($tokens as $i => $token) {
- $loadValues[$token] = $values[$i];
- }
-
- return $loadValues;
- }
- }
-
- return false;
- }
-
- /**
- * Loads a post model based on the URI (array)
- *
- * @param string|array $explodedUri
- * @return FIshpig_Wordpress_Model_Post
- */
- public function loadByPermalink($uri)
- {
- if ($this->useGuidLinks()) {
- return Mage::getModel('wordpress/post')->load($this->getPostId());
- }
- if ($loadTokens = $this->_getTokenValueArray($uri)) {
- $posts = Mage::getResourceModel('wordpress/post_collection')->addIsPublishedFilter();
- $date = array();
- $time = array();
-
- foreach($loadTokens as $token => $value) {
- if ($token === 'year') {
- $date[0] = $value;
- }
- else if ($token === 'monthnum') {
- $date[1] = $value;
- }
- else if ($token === 'day') {
- $date[2] = $value;
- }
- else if ($token === 'hour') {
- $time[0] = $value;
- }
- else if ($token === 'minute') {
- $time[1] = $value;
- }
- else if ($token === 'second') {
- $time[2] = $value;
- }
- else if ($token === 'post_id') {
- $posts->addFieldToFilter('ID', $value);
- }
- else if ($token === 'postname') {
- $posts->addFieldToFilter('post_name', strtolower(urlencode($value)));
- }
- else if ($token === 'category') {
- $posts->addCategorySlugFilter($value);
- }
- else if ($token === 'author') {
- }
- else {
- Mage::dispatchEvent('wordpress_permalink_segment_unknown_load', array('collection' => $posts, 'segment' => $token, 'value' => $value));
- }
- }
- if (($dateStr = $this->_craftDateString($date, $time)) !== false) {
- $posts->addPostDateFilter($dateStr);
- }
- $posts->setCurPage(1)->setPageSize(1)->load();
- if (count($posts) == 1) {
- return $posts->getFirstItem();
- }
- }
- return false;
- }
- /**
- * return the permalink based on permalink structure
- * which is defined in WP Admin
- *
- * @param Fishpig_Wordpress_Model_Post
- * @return string
- */
- public function getPermalink(Fishpig_Wordpress_Model_Post $post)
- {
- if ($this->useGuidLinks()) {
- return $this->getUrl('?p='.$post->getId());
- }
- else {
- $structure = $this->_getExplodedPermalinkStructure();
- if (count($structure) > 0) {
- $url = array();
- foreach($structure as $part) {
- if (preg_match('/^\%[a-zA-Z0-9_]{1,}\%$/', $part)) {
- $part = trim($part, '%');
-
- if ($part === 'year') {
- $url[] = $post->getPostDate('Y');
- }
- else if ($part === 'monthnum') {
- $url[] = $post->getPostDate('m');
- }
- else if ($part === 'day') {
- $url[] = $post->getPostDate('d');
- }
- else if ($part === 'hour') {
- $url[] = $post->getPostDate('H');
- }
- else if ($part === 'minute') {
- $url[] = $post->getPostDate('i');
- }
- else if ($part === 'second') {
- $url[] = $post->getPostDate('s');
- }
- else if ($part === 'post_id') {
- $url[] = $post->getId();
- }
- else if ($part === 'postname') {
- $url[] = urldecode($post->getPostName());
- }
- else if ($part === 'category') {
- $url[] = $this->_getPermalinkCategoryPortion($post);
- }
- else if ($part === 'author') {
- }
- else {
- $response = new Varien_Object(array('value' => false));
- Mage::dispatchEvent('wordpress_permalink_segment_unknown_getpermalink', array('response' => $response, 'post' => $post, 'segment' => $part));
-
- if ($response->getValue() !== false) {
- $url[] = $response->getValue();
- }
- }
- }
- else {
- if ($part === '/') {
- $partCount = count($url);
-
- if ($partCount > 0 && $url[$partCount-1]===$part) {
- continue;
- }
- }
-
- $url[] = $part;
- }
- }
-
- if ($this->permalinkHasTrainingSlash()) {
- $url[count($url)-1] .= '/';
- }
- return $this->getUrl(implode('', $url));
- }
- }
- }
-
- /**
- * Craft a date string to help load posts
- *
- * @param array $date = array
- * @param array $time = array
- * @return string
- */
- protected function _craftDateString(array $date = array(), array $time = array())
- {
- if (count($date) > 0 || count($time) > 0) {
- $dateStr = '';
-
- foreach(array('-' => $date, ':' => $time) as $sep => $values) {
- for($i = 0; $i <= 2; $i++ ) {
- $dateStr .= (isset($values[$i]) ? $values[$i] : '%') . $sep;
- }
-
- $dateStr = rtrim($dateStr, $sep) . ' ';
- }
-
- return rtrim($dateStr);
- }
-
- return false;
- }
- /**
- * Generates the category portion of the URL for a post
- *
- * @param Fishpig_Wordpress_Model_Post $post
- * @return string
- */
- protected function _getPermalinkCategoryPortion(Fishpig_Wordpress_Model_Post $post)
- {
- if ($category = $post->getParentCategory()) {
- return trim($category->getSlug(), '/');
- }
- }
-
- /**
- * Retrieve the post ID from the query string
- *
- * @return string
- */
- public function getPostId()
- {
- $postId = Mage::app()->getRequest()->getParam($this->getPostIdVar(), false);
-
- if ($postId === false && $this->isPreview()) {
- $postId = Mage::app()->getRequest()->getParam('preview_id', false);
- }
-
- return $postId;
- }
-
- /**
- * Determine whether we are previewing a post
- *
- * @return bool
- */
- public function isPreview()
- {
- return Mage::app()->getRequest()->getParam('preview', false);
- }
-
- /**
- * Retrieve the variable used for post ID's when using Guid links
- *
- * @return string
- */
- public function getPostIdVar()
- {
- return $this->_postIdVar;
- }
-
- /**
- * Retrieve the URL for the tags page
- *
- * @return string
- */
- public function getTagsUrl()
- {
- return $this->getUrl('tags');
- }
- /**
- * Retrieve the number of comments to display per page
- *
- * @return int
- */
- public function getCommentsPerPage()
- {
- return $this->getWpOption('page_comments') ? Mage::helper('wordpress')->getWpOption('comments_per_page', 50) : 0;
- }
-
- /**
- * Determine whether the permalink has a trailing slash
- *
- * @return bool
- */
- public function permalinkHasTrainingSlash()
- {
- return substr($this->getWpOption('permalink_structure'), -1) == '/';
- }
- }