/** * Set the post this comment is associated to * * @param Fishpig_Wordpress_Model_Post $post * @return Fishpig_Wordpress_Model_Post_Comment */ public function setPost(Fishpig_Wordpress_Model_Post $post) { $this->setPostId($post->getId()); $this->setData('comment_post_ID', $post->getId()); return parent::setData('post', $post); }
/** * Retrieve a single parent category * This is the category associated with the post with the lowest term_id * * @param Fishpig_Wordpress_Model_Post $post * @retrun Fishpig_Wordpress_Model_Post_Category_Collection */ public function getParentCategory(Fishpig_Wordpress_Model_Post $post) { $collection = Mage::getResourceModel('wordpress/post_category_collection')->addPostIdFilter($post->getId()); $collection->getSelect()->limit(1)->reset(Zend_Db_Select::ORDER)->order('main_table.term_id ASC'); $collection->load(); return $collection->getFirstItem()->getId() ? $collection->getFirstItem() : false; }
/** * Retrieve an array of related post ID's * * @param Fishpig_Wordpress_Model_Post $post * @return array|false */ public function getRelatedPostIds(Fishpig_Wordpress_Model_Post $post) { $helper = Mage::helper('wordpress/database'); $select = $helper->getReadAdapter()->select()->from($helper->getTableName('yarpp_related_cache'), 'ID')->where('reference_ID=?', $post->getId())->where('score > ?', 0)->order($this->getOrder() ? $this->getOrder() : 'score DESC')->limit($this->getLimit() ? $this->getLimit() : 5); try { return $helper->getReadAdapter()->fetchCol($select); } catch (Exception $e) { Mage::helper('wordpress')->log($e->getMessage()); } return array(); }
/** * Retrieve a collection of products assocaited with the post * * @param Fishpig_Wordpress_Model_Post $post * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */ public function getAssociatedProducts($post) { if ($post instanceof Fishpig_Wordpress_Model_Post) { $productIds = $this->_getAssociatedWpEntityIds($post->getId(), 'product', 'post', 'post_id'); if (count($productIds) > 0) { $collection = Mage::getResourceModel('catalog/product_collection'); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); $collection->addAttributeToFilter('status', 1); $collection->addAttributeToFilter('entity_id', array('in' => $productIds)); return $collection; } } return false; }
/** * Retrieve a collection of products assocaited with the post * * @param Fishpig_Wordpress_Model_Post $post * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */ public function getAssociatedProducts($post) { if ($post instanceof Fishpig_Wordpress_Model_Post) { $productIds = $this->_getAssociatedWpEntityIds($post->getId(), 'product', 'post', 'post_id'); try { foreach ($post->getParentCategories() as $category) { $productIds = array_merge($productIds, $this->_getAssociatedWpEntityIds($category->getId(), 'product', 'category', 'category_id')); } } catch (Exception $e) { $this->log($e->getMessage()); } if (count($productIds) > 0) { $collection = Mage::getResourceModel('catalog/product_collection'); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); $collection->addAttributeToFilter('status', 1); $collection->addAttributeToFilter('entity_id', array('in' => $productIds)); return $collection; } } return false; }
/** * Retrieve a collection of products assocaited with the post * * @param Fishpig_Wordpress_Model_Post $post * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */ public function getAssociatedProductsByPost(Fishpig_Wordpress_Model_Post $post) { if (!$this->isConnected() || !$post instanceof Fishpig_Wordpress_Model_Post) { return false; } $associations = array_keys($this->getReverseAssociations('product/post', $post->getId())); foreach ($post->getParentCategories() as $category) { $associations = array_merge($associations, array_keys($this->getReverseAssociations('product/category', $category->getId()))); } $associations = array_unique($associations); if (count($associations) > 0) { $collection = Mage::getResourceModel('catalog/product_collection'); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); if (!Mage::getStoreConfigFlag('cataloginventory/options/show_out_of_stock')) { Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection); } $collection->addAttributeToFilter('status', 1); $collection->addAttributeToFilter('entity_id', array('in' => $associations)); return $collection; } return false; }
/** * Retrieve the featured image for the post * * @param Fishpig_Wordpress_Model_Post $post * @return Fishpig_Wordpress_Model_Image $image */ public function getFeaturedImage(Fishpig_Wordpress_Model_Post $post) { if ($images = $post->getImages()) { $select = $this->_getReadAdapter()->select()->from($this->getTable('wordpress/post_meta'), 'meta_value')->where('post_id=?', $post->getId())->where('meta_key=?', '_thumbnail_id')->limit(1); if (($imageId = $this->_getReadAdapter()->fetchOne($select)) !== false) { if (preg_match('/([a-z-]{1,})([0-9]{1,})/', $imageId, $matches)) { if (($prefix = trim($matches[1], '- ')) !== '') { $eventData = array('object' => $post, 'image_id' => $matches[2], 'original_image_id' => $imageId, 'result' => new Varien_Object()); Mage::dispatchEvent('wordpress_post_get_featured_image_' . $prefix, $eventData); if ($eventData['result']->getFeaturedImage()) { return $eventData['result']->getFeaturedImage(); } } } else { return Mage::getModel('wordpress/image')->load($imageId); } } } 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, '%'); switch($part) { case 'year': $url[] = $post->getPostDate('Y'); break; case 'monthnum': $url[] = $post->getPostDate('m'); break; case 'day': $url[] = $post->getPostDate('d'); break; case 'hour': $url[] = $post->getPostDate('H'); break; case 'minute': $url[] = $post->getPostDate('i'); break; case 'second': $url[] = $post->getPostDate('s'); break; case 'post_id': $url[] = $post->getId(); break; case 'postname': $url[] = urldecode($post->getPostName()); break; case 'category': $url[] = $this->_getPermalinkCategoryPortion($post); break; case 'author': break; default: $this->log("Unknown permalink token ({$segment})"); break; } } else { $url[] = $part; } } return $this->getUrl(implode('', $url)); } } }
/** * 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)); } } }
/** * Retrieve a collection of categories * * @param Fishpig_Wordpress_Model_Post $post * @retrun Fishpig_Wordpress_Model_Post_Category_Collection */ public function getParentCategories(Fishpig_Wordpress_Model_Post $post) { return Mage::getResourceModel('wordpress/post_category_collection')->addPostIdFilter($post->getId()); }
/** * Retrieve a collection of post tags * * @param Fishpig_Wordpress_Model_Post $post * @return Fishpig_Wordpress_Model_Resource_Post_Tag_Collection */ public function getPostTags(Fishpig_Wordpress_Model_Post $post) { return Mage::getResourceModel('wordpress/post_tag_collection')->addPostIdFilter($post->getId()); }
/** * Retrieve the position of a post in a product * * @param Fishpig_Wordpress_Model_Post $post * @param int $productId * @return int */ public function getPositionInProduct(Fishpig_Wordpress_Model_Post $post, $productId) { $read = Mage::getSingleton('core/resource')->getConnection('core_read'); $select = $read->select()->from(Mage::getSingleton('core/resource')->getTableName('wordpress_product_post'), 'position')->where('post_id=?', $post->getId())->where('product_id=?', $productId)->limit(1); return number_format($read->fetchOne($select), 0); }
/** * Filter the collection by a post model * This is just a wrapper for self::addPostIdFilter * * @param Fishpig_Wordpress_Model_Post $post */ public function addPostFilter(Fishpig_Wordpress_Model_Post $post) { return $this->addPostIdFilter($post->getId()); }
/** * Unset a post's comment data * * @param Fishpig_Wordpress_Model_Post $post */ public function removePostCommentData(Fishpig_Wordpress_Model_Post $post) { return $this->unsetData('post_comment_data_' . $post->getId()); }
/** * Get a collection of terms that belong this taxonomy and $post * * @param Fishpig_Wordpress_Model_Post $post * @return Fishpig_Wordpress_Model_Resource_Post_Collection */ public function getPostTermsCollection(Fishpig_Wordpress_Model_Post $post) { return Mage::getResourceModel('wordpress/term_collection')->addTaxonomyFilter($this->getTaxonomyType())->addPostIdFilter($post->getId()); }