init
This commit is contained in:
+187
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
/**
|
||||
* Mageplaza
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Mageplaza.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.mageplaza.com/LICENSE.txt
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Mageplaza
|
||||
* @package Mageplaza_Blog
|
||||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
|
||||
* @license https://www.mageplaza.com/LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Mageplaza\Blog\Block\Post;
|
||||
|
||||
use Exception;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Theme\Block\Html\Pager;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\Post;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Post\Collection;
|
||||
|
||||
/**
|
||||
* Class AuthorPost
|
||||
* @package Mageplaza\Blog\Block\Post
|
||||
*/
|
||||
class AuthorPost extends \Mageplaza\Blog\Block\Listpost
|
||||
{
|
||||
/**
|
||||
* @return AbstractCollection|Collection|null
|
||||
*/
|
||||
public function getPostCollection()
|
||||
{
|
||||
try {
|
||||
$collection = $this->helperData->getFactoryByType()->create()->getCollection();
|
||||
$this->helperData->addStoreFilter($collection, $this->store->getStore()->getId());
|
||||
|
||||
$userId = $this->getAuthor()->getId();
|
||||
|
||||
$collection->addFieldToFilter('author_id', $userId);
|
||||
|
||||
if ($collection && $collection->getSize()) {
|
||||
$pager = $this->getLayout()->createBlock(Pager::class, 'mpblog.post.pager');
|
||||
$perPageValues = (string) $this->helperData->getDisplayConfig('pagination');
|
||||
$perPageValues = explode(',', $perPageValues ?? '');
|
||||
$perPageValues = array_combine($perPageValues, $perPageValues);
|
||||
$pager->setAvailableLimit($perPageValues)->setCollection($collection);
|
||||
$this->setChild('pager', $pager);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$collection = null;
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $statusId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStatusHtmlById($statusId)
|
||||
{
|
||||
$statusText = $this->authorStatusType->toArray()[$statusId]->getText();
|
||||
|
||||
switch ($statusId) {
|
||||
case '2':
|
||||
$html = '<div class="mp-post-status mp-post-disapproved">' . __($statusText) . '</div>';
|
||||
break;
|
||||
case '1':
|
||||
$html = '<div class="mp-post-status mp-post-approved">' . __($statusText) . '</div>';
|
||||
break;
|
||||
case '0':
|
||||
default:
|
||||
$html = '<div class="mp-post-status mp-post-pending">' . __($statusText) . '</div>';
|
||||
break;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$array = explode('/', $this->helperData->getConfigValue('cms/wysiwyg/editor') ?? '');
|
||||
if ($array[count($array) - 1] === 'tinymce4Adapter') {
|
||||
return 4;
|
||||
}
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getMagentoVersion()
|
||||
{
|
||||
return (int) $this->helperData->versionCompare('2.4.0', '>=') ? '' : '4';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractCollection|Collection|null $postCollection
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPostDatas($postCollection)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
if ($postCollection) {
|
||||
try {
|
||||
/** @var Post $post */
|
||||
foreach ($postCollection->getItems() as $post) {
|
||||
$post->getCategoryIds();
|
||||
$post->getTopicIds();
|
||||
$post->getTagIds();
|
||||
if ($post->getPostContent()) {
|
||||
$post->setData('post_content', $this->getPageFilter($post->getPostContent()));
|
||||
}
|
||||
$post = $this->helperData->formatPublishDate($post);
|
||||
$result[$post->getId()] = $post->getData();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$result = [];
|
||||
}
|
||||
}
|
||||
|
||||
return Data::jsonEncode($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAuthorName()
|
||||
{
|
||||
return $this->getAuthor()->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getAuthorStatus()
|
||||
{
|
||||
$author = $this->getAuthor();
|
||||
|
||||
return $author->getStatus() === '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAuthor()
|
||||
{
|
||||
return $this->coreRegistry->registry('mp_author');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $meta
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBlogTitle($meta = false)
|
||||
{
|
||||
return $meta ? [$this->getAuthor()->getName()] : $this->getAuthor()->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getBaseMediaUrl()
|
||||
{
|
||||
return $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* Mageplaza
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Mageplaza.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.mageplaza.com/LICENSE.txt
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Mageplaza
|
||||
* @package Mageplaza_Blog
|
||||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
|
||||
* @license https://www.mageplaza.com/LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Mageplaza\Blog\Block\Post;
|
||||
|
||||
/**
|
||||
* Class Listpost
|
||||
* @package Mageplaza\Blog\Block\Post
|
||||
*/
|
||||
class Listpost extends \Mageplaza\Blog\Block\Listpost
|
||||
{
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Mageplaza
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Mageplaza.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.mageplaza.com/LICENSE.txt
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Mageplaza
|
||||
* @package Mageplaza_Blog
|
||||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
|
||||
* @license https://www.mageplaza.com/LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Mageplaza\Blog\Block\Post;
|
||||
|
||||
use Mageplaza\Blog\Block\Frontend;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
|
||||
/**
|
||||
* Class ManagePost
|
||||
* @package Mageplaza\Blog\Block\Post
|
||||
*/
|
||||
class ManagePost extends Frontend
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCategoriesTree()
|
||||
{
|
||||
return Data::jsonEncode($this->categoryOptions->getCategoriesTree());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTopicTree()
|
||||
{
|
||||
return Data::jsonEncode($this->topicOptions->getTopicsCollection());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTagTree()
|
||||
{
|
||||
return Data::jsonEncode($this->tagOptions->getTagsCollection());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function checkTheme()
|
||||
{
|
||||
return $this->themeProvider->getThemeById($this->helperData->getCurrentThemeId())
|
||||
->getCode() === 'Smartwave/porto';
|
||||
}
|
||||
}
|
||||
+192
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
/**
|
||||
* Mageplaza
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Mageplaza.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.mageplaza.com/LICENSE.txt
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Mageplaza
|
||||
* @package Mageplaza_Blog
|
||||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
|
||||
* @license https://www.mageplaza.com/LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Mageplaza\Blog\Block\Post;
|
||||
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\Framework\View\Element\Template\Context;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Helper\Image;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Post\Collection;
|
||||
|
||||
/**
|
||||
* Class RelatedPost
|
||||
* @package Mageplaza\Blog\Block\Post
|
||||
*/
|
||||
class RelatedPost extends Template
|
||||
{
|
||||
/**
|
||||
* Core registry
|
||||
*
|
||||
* @var Registry
|
||||
*/
|
||||
protected $_coreRegistry;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
protected $helperData;
|
||||
|
||||
/**
|
||||
* @var Collection
|
||||
*/
|
||||
protected $_relatedPosts;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $_limitPost;
|
||||
|
||||
/**
|
||||
* RelatedPost constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param Registry $registry
|
||||
* @param Data $helperData
|
||||
* @param array $data
|
||||
*
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Registry $registry,
|
||||
Data $helperData,
|
||||
array $data = []
|
||||
) {
|
||||
$this->_coreRegistry = $registry;
|
||||
$this->helperData = $helperData;
|
||||
|
||||
parent::__construct($context, $data);
|
||||
|
||||
$this->setTabTitle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current product id
|
||||
*
|
||||
* @return null|int
|
||||
*/
|
||||
public function getProductId()
|
||||
{
|
||||
$product = $this->_coreRegistry->registry('product');
|
||||
|
||||
return $product ? $product->getId() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getRelatedPostList()
|
||||
{
|
||||
if ($this->_relatedPosts == null) {
|
||||
/** @var Collection $collection */
|
||||
$collection = $this->helperData->getPostList();
|
||||
$collection->getSelect()
|
||||
->join(
|
||||
[
|
||||
'related' => $collection->getTable('mageplaza_blog_post_product')
|
||||
],
|
||||
'related.post_id=main_table.post_id AND related.entity_id=' . $this->getProductId()
|
||||
)
|
||||
->limit($this->getLimitPosts());
|
||||
|
||||
$this->_relatedPosts = $collection;
|
||||
}
|
||||
|
||||
return $this->_relatedPosts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|mixed
|
||||
*/
|
||||
public function getLimitPosts()
|
||||
{
|
||||
if (!$this->_limitPost) {
|
||||
$this->_limitPost = (int) $this->helperData->getBlogConfig('product_post/product_detail/post_limit') ?: 1;
|
||||
}
|
||||
|
||||
return $this->_limitPost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tab title
|
||||
*
|
||||
* @return void
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function setTabTitle()
|
||||
{
|
||||
$relatedSize = min($this->getRelatedPostList()->getSize(), $this->getLimitPosts());
|
||||
|
||||
$title = $relatedSize
|
||||
? __('Related Posts %1', '(' . $relatedSize . ')')
|
||||
: __('Related Posts');
|
||||
if ($this->helperData->isEnabled()) {
|
||||
$this->setTitle($title);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnabledBlog()
|
||||
{
|
||||
return $this->helperData->isEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getRelatedMode()
|
||||
{
|
||||
return (int) $this->helperData->getPostViewPageConfig('related_mode') === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize Image Function
|
||||
*
|
||||
* @param $image
|
||||
* @param null $size
|
||||
* @param string $type
|
||||
*
|
||||
* @return string
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function resizeImage($image, $size = null, $type = Image::TEMPLATE_MEDIA_TYPE_POST)
|
||||
{
|
||||
if (!$image) {
|
||||
return $this->getDefaultImageUrl();
|
||||
}
|
||||
|
||||
return $this->helperData->getImageHelper()->resizeImage($image, $size, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* get default image url
|
||||
*/
|
||||
public function getDefaultImageUrl()
|
||||
{
|
||||
return $this->getViewFileUrl('Mageplaza_Blog::media/images/mageplaza-logo-default.png');
|
||||
}
|
||||
}
|
||||
+222
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
/**
|
||||
* Mageplaza
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Mageplaza.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.mageplaza.com/LICENSE.txt
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Mageplaza
|
||||
* @package Mageplaza_Blog
|
||||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
|
||||
* @license https://www.mageplaza.com/LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Mageplaza\Blog\Block\Post;
|
||||
|
||||
use Exception;
|
||||
use Magento\Catalog\Api\CategoryRepositoryInterface;
|
||||
use Magento\Catalog\Block\Product\Context;
|
||||
use Magento\Catalog\Block\Product\ListProduct;
|
||||
use Magento\Catalog\Helper\Output;
|
||||
use Magento\Catalog\Model\Layer\Resolver;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\ProductRepository;
|
||||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
|
||||
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;
|
||||
use Magento\Framework\Data\Helper\PostHelper;
|
||||
use Magento\Framework\Url\Helper\Data;
|
||||
use Mageplaza\Blog\Helper\Data as HelperData;
|
||||
|
||||
/**
|
||||
* Class RelatedProduct
|
||||
* @package Mageplaza\Blog\Block\Post
|
||||
*/
|
||||
class RelatedProduct extends ListProduct
|
||||
{
|
||||
/**
|
||||
* Default related product page title
|
||||
*/
|
||||
const TITLE = 'Related Products';
|
||||
|
||||
/**
|
||||
* Default limit related products
|
||||
*/
|
||||
const LIMIT = '12';
|
||||
|
||||
/**
|
||||
* @var CollectionFactory
|
||||
*/
|
||||
protected $_productCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var HelperData
|
||||
*/
|
||||
protected $helper;
|
||||
|
||||
/**
|
||||
* @var Output
|
||||
*/
|
||||
protected $outputHelper;
|
||||
|
||||
/**
|
||||
* @var Configurable
|
||||
*/
|
||||
protected $catalogProductTypeConfigurable;
|
||||
|
||||
/**
|
||||
* @var ProductRepository
|
||||
*/
|
||||
protected $productRepository;
|
||||
|
||||
/**
|
||||
* RelatedProduct constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param PostHelper $postDataHelper
|
||||
* @param Resolver $layerResolver
|
||||
* @param CategoryRepositoryInterface $categoryRepository
|
||||
* @param CollectionFactory $productCollectionFactory
|
||||
* @param Configurable $catalogProductTypeConfigurable
|
||||
* @param ProductRepository $productRepository
|
||||
* @param HelperData $helperData
|
||||
* @param Output $output
|
||||
* @param Data $urlHelper
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PostHelper $postDataHelper,
|
||||
Resolver $layerResolver,
|
||||
CategoryRepositoryInterface $categoryRepository,
|
||||
CollectionFactory $productCollectionFactory,
|
||||
Configurable $catalogProductTypeConfigurable,
|
||||
ProductRepository $productRepository,
|
||||
HelperData $helperData,
|
||||
Output $output,
|
||||
Data $urlHelper,
|
||||
array $data = []
|
||||
) {
|
||||
$this->_productCollectionFactory = $productCollectionFactory;
|
||||
$this->helper = $helperData;
|
||||
$this->outputHelper = $output;
|
||||
$this->catalogProductTypeConfigurable = $catalogProductTypeConfigurable;
|
||||
$this->productRepository = $productRepository;
|
||||
|
||||
parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Output
|
||||
*/
|
||||
public function getHelper()
|
||||
{
|
||||
return $this->outputHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function relatedMode()
|
||||
{
|
||||
return $this->helper->getModuleConfig('product_post/post_detail/related_mode');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function hasProduct()
|
||||
{
|
||||
$collection = $this->_getProductCollection();
|
||||
|
||||
return $collection->getSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ProductCollection in same brand (filter by Attribute Option_Id)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function _getProductCollection()
|
||||
{
|
||||
if ($this->_productCollection === null) {
|
||||
$postId = $this->getRequest()->getParam('id');
|
||||
$collection = $this->_productCollectionFactory->create()
|
||||
->addAttributeToSelect('*')
|
||||
->addStoreFilter();
|
||||
|
||||
$collection->getSelect()
|
||||
->join(
|
||||
['product_post' => $collection->getTable('mageplaza_blog_post_product')],
|
||||
'e.entity_id = product_post.entity_id'
|
||||
)
|
||||
->where('product_post.post_id = ' . $postId)
|
||||
->order('product_post.position ASC')
|
||||
->limit((int) $this->helper->getBlogConfig('product_post/post_detail/product_limit') ?: self::LIMIT);
|
||||
|
||||
$this->_productCollection = $collection;
|
||||
}
|
||||
|
||||
return $this->_productCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getMode()
|
||||
{
|
||||
return 'grid';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getToolbarHtml()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getAdditionalHtml()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _beforeToHtml()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getParentProductUrl($product)
|
||||
{
|
||||
$parentByChild = $this->catalogProductTypeConfigurable->getParentIdsByChild($product->getId());
|
||||
if (isset($parentByChild[0])) {
|
||||
try {
|
||||
$parentProduct = $this->productRepository->getById($parentByChild[0]);
|
||||
|
||||
return $parentProduct->getProductUrl();
|
||||
} catch (Exception $e) {
|
||||
return $product->getProductUrl();
|
||||
}
|
||||
}
|
||||
|
||||
return $product->getProductUrl();
|
||||
}
|
||||
}
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
/**
|
||||
* Mageplaza
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Mageplaza.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.mageplaza.com/LICENSE.txt
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Mageplaza
|
||||
* @package Mageplaza_Blog
|
||||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
|
||||
* @license https://www.mageplaza.com/LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Mageplaza\Blog\Block\Post\Rss;
|
||||
|
||||
use Magento\Framework\App\Rss\DataProviderInterface;
|
||||
use Magento\Framework\App\Rss\UrlBuilderInterface;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Element\AbstractBlock;
|
||||
use Magento\Framework\View\Element\Template\Context;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\Post;
|
||||
|
||||
/**
|
||||
* Class NewProducts
|
||||
* @package Magento\Catalog\Block\Rss\Product
|
||||
*/
|
||||
class Lists extends AbstractBlock implements DataProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var UrlBuilderInterface
|
||||
*/
|
||||
public $rssUrlBuilder;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
public $storeManager;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
public $helper;
|
||||
|
||||
/**
|
||||
* Lists constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param Data $helper
|
||||
* @param UrlBuilderInterface $rssUrlBuilder
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
UrlBuilderInterface $rssUrlBuilder,
|
||||
Data $helper,
|
||||
array $data = []
|
||||
) {
|
||||
$this->rssUrlBuilder = $rssUrlBuilder;
|
||||
$this->helper = $helper;
|
||||
$this->storeManager = $context->getStoreManager();
|
||||
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->setCacheKey('rss_blog_posts_store_' . $this->getStoreId());
|
||||
|
||||
parent::_construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isAllowed()
|
||||
{
|
||||
return $this->helper->isEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRssData()
|
||||
{
|
||||
$storeModel = $this->storeManager->getStore($this->getStoreId());
|
||||
$title = __('List Posts from %1', $storeModel->getFrontendName())->render();
|
||||
$storeUrl = $this->storeManager->getStore($this->getStoreId())->getBaseUrl(UrlInterface::URL_TYPE_WEB);
|
||||
$data = [
|
||||
'title' => $title,
|
||||
'description' => $title,
|
||||
'link' => $storeUrl . 'blog/post/rss.xml',
|
||||
'charset' => 'UTF-8',
|
||||
'language' => $this->helper->getConfigValue('general/locale/code', $storeModel),
|
||||
];
|
||||
|
||||
$posts = $this->helper->getPostList($this->getStoreId())
|
||||
->addFieldToFilter('in_rss', 1)
|
||||
->setOrder('post_id', 'DESC');
|
||||
$posts->getSelect()->limit(10);
|
||||
/** @var Post $item */
|
||||
foreach ($posts->getItems() as $item) {
|
||||
$item->setAllowedInRss(true);
|
||||
$item->setAllowedPriceInRss(true);
|
||||
|
||||
$description = $item->getShortDescription();
|
||||
$data['entries'][] = [
|
||||
'title' => $item->getName(),
|
||||
'link' => $item->getUrl(),
|
||||
'description' => $description ?: 'no content',
|
||||
'lastUpdate' => strtotime($item->getPublishDate())
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getStoreId()
|
||||
{
|
||||
$storeId = $this->getRequest()->getParam('store_id');
|
||||
if ($storeId === null) {
|
||||
$storeId = $this->storeManager->getStore()->getId();
|
||||
}
|
||||
|
||||
return (int)$storeId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCacheLifetime()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFeeds()
|
||||
{
|
||||
$data = [];
|
||||
if ($this->isAllowed()) {
|
||||
$url = $this->rssUrlBuilder->getUrl(['type' => 'blog_posts']);
|
||||
$data = ['label' => __('Posts'), 'link' => $url];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isAuthRequired()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Executable
+449
@@ -0,0 +1,449 @@
|
||||
<?php
|
||||
/**
|
||||
* Mageplaza
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Mageplaza.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.mageplaza.com/LICENSE.txt
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Mageplaza
|
||||
* @package Mageplaza_Blog
|
||||
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
|
||||
* @license https://www.mageplaza.com/LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Mageplaza\Blog\Block\Post;
|
||||
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Phrase;
|
||||
use Magento\Framework\View\Element\Messages;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\Post;
|
||||
use Mageplaza\Blog\Model\PostLike;
|
||||
|
||||
/**
|
||||
* Class View
|
||||
* @package Mageplaza\Blog\Block\Post
|
||||
* @method Post getPost()
|
||||
* @method void setPost($post)
|
||||
*/
|
||||
class View extends \Mageplaza\Blog\Block\Listpost
|
||||
{
|
||||
/**
|
||||
* config logo blog path
|
||||
*/
|
||||
const LOGO = 'mageplaza/blog/logo/';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
parent::_construct();
|
||||
|
||||
$post = $this->postFactory->create();
|
||||
$id = $this->getRequest()->getParam('id');
|
||||
$historyId = $this->getRequest()->getParam('historyId');
|
||||
|
||||
if ($historyId) {
|
||||
$history = $this->helperData->getFactoryByType(Data::TYPE_HISTORY)->create()->load($historyId);
|
||||
$post = $this->helperData->getFactoryByType(Data::TYPE_POST)->create()->load($history->getPostId());
|
||||
$data = $history->getData();
|
||||
$post->addData($data);
|
||||
} elseif ($id) {
|
||||
$post->load($id);
|
||||
}
|
||||
$this->setPost($post);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getRelatedMode()
|
||||
{
|
||||
return (int) $this->helperData->getPostViewPageConfig('related_mode') === 1 ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDecrypt($value)
|
||||
{
|
||||
return $this->enc->decrypt($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getBlogObject()
|
||||
{
|
||||
return $this->getPost();
|
||||
}
|
||||
|
||||
/**
|
||||
* check customer is logged in or not
|
||||
*/
|
||||
public function isLoggedIn()
|
||||
{
|
||||
return $this->helperData->isLogin();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function checkRss()
|
||||
{
|
||||
return $this->helperData->getBlogUrl('post/rss');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $topic
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTopicUrl($topic)
|
||||
{
|
||||
return $this->helperData->getBlogUrl($topic, Data::TYPE_TOPIC);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $tag
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTagUrl($tag)
|
||||
{
|
||||
return $this->helperData->getBlogUrl($tag, Data::TYPE_TAG);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $category
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCategoryUrl($category)
|
||||
{
|
||||
return $this->helperData->getBlogUrl($category, Data::TYPE_CATEGORY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the store-specific SEO title with the post name as fallback.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPostMetaTitle()
|
||||
{
|
||||
return (string) ($this->helperData->getMetaTitleByStoreId($this->getPost()->getMetaTitle())
|
||||
?: $this->getPost()->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the store-specific SEO description with the short description as fallback.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPostMetaDescription()
|
||||
{
|
||||
$storeId = (int) $this->store->getStore()->getId();
|
||||
|
||||
return (string) ($this->getMetaFieldByStoreId($this->getPost()->getMetaDescription(), $storeId)
|
||||
?: $this->getPost()->getShortDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $code
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function helperComment($code)
|
||||
{
|
||||
return $this->helperData->getBlogConfig('comment/' . $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* get comments tree html
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCommentsHtml()
|
||||
{
|
||||
return $this->commentTree;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $userId
|
||||
*
|
||||
* @return CustomerInterface
|
||||
* @throws LocalizedException
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getUserComment($userId)
|
||||
{
|
||||
return $this->customerRepository->getById($userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $cmtId
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
public function getCommentLikes($cmtId)
|
||||
{
|
||||
$likes = $this->likeFactory->create()
|
||||
->getCollection()
|
||||
->addFieldToFilter('comment_id', $cmtId)
|
||||
->getSize();
|
||||
|
||||
return $likes ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $cmtId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLiked($cmtId)
|
||||
{
|
||||
if ($this->helperData->isLogin()) {
|
||||
$customerId = $this->helperData->getCustomerIdByContext();
|
||||
$likes = $this->likeFactory->create()->getCollection();
|
||||
foreach ($likes as $like) {
|
||||
if ($like->getEntityId() == $customerId && $like->getCommentId() == $cmtId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $postId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPostComments($postId)
|
||||
{
|
||||
$result = [];
|
||||
$comments = $this->cmtFactory->create()->getCollection()
|
||||
->addFieldToFilter('main_table.post_id', $postId);
|
||||
foreach ($comments as $comment) {
|
||||
$result[] = $comment->getData();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $postId
|
||||
* @param $action
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPostLike($postId, $action)
|
||||
{
|
||||
/** @var PostLike $postLike */
|
||||
$postLike = $this->postLikeFactory->create();
|
||||
|
||||
return $postLike->getCollection()->addFieldToFilter('post_id', $postId)
|
||||
->addFieldToFilter('action', $action)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $comment
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function commentHtml($comment)
|
||||
{
|
||||
$html = '';
|
||||
foreach (explode("\n", trim($comment)) as $value) {
|
||||
$html .= '<p>' . $this->escapeHtml($value) . '</p>';
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $comments
|
||||
* @param $cmtId
|
||||
*
|
||||
* @throws LocalizedException
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getCommentsTree($comments, $cmtId)
|
||||
{
|
||||
$this->commentTree .= '<ul class="default-cmt__content__cmt-content row">';
|
||||
foreach ($comments as $comment) {
|
||||
if ($comment['reply_id'] == $cmtId && $comment['status'] == 1) {
|
||||
$isReply = (bool) $comment['is_reply'];
|
||||
$replyId = $isReply ? $comment['reply_id'] : '';
|
||||
if ($comment['entity_id'] == 0) {
|
||||
$userName = $comment['user_name'];
|
||||
} else {
|
||||
$userCmt = $this->getUserComment($comment['entity_id']);
|
||||
$userName = $userCmt->getFirstName() . ' '
|
||||
. $userCmt->getLastName();
|
||||
}
|
||||
$countLikes = $this->getCommentLikes($comment['comment_id']);
|
||||
$isLiked = ($this->isLiked($comment['comment_id'])) ? "mpblog-liked" : "mpblog-like";
|
||||
$this->commentTree .= '<li id="cmt-id-' . $comment['comment_id']
|
||||
. '" class="default-cmt__content__cmt-content__cmt-row cmt-row-'
|
||||
. $comment['comment_id'] . ' cmt-row col-md-12'
|
||||
. ($isReply ? ' reply-row' : '') . '" data-cmt-id="'
|
||||
. $comment['comment_id'] . '" ' . ($replyId
|
||||
? 'data-reply-id="' . $replyId . '"' : '') . '>
|
||||
<div class="cmt-row__cmt-username">
|
||||
<span class="cmt-row__cmt-username username username__'
|
||||
. $comment['comment_id'] . '">'
|
||||
. $userName . '</span>
|
||||
</div>
|
||||
<div class="cmt-row__cmt-content">
|
||||
' . $this->commentHtml($comment['content']) . '
|
||||
</div>
|
||||
<div class="cmt-row__cmt-interactions interactions">
|
||||
<div class="interactions__btn-actions">
|
||||
<a class="interactions__btn-actions action btn-like '
|
||||
. $isLiked . '" data-cmt-id="'
|
||||
. $comment['comment_id'] . '" click="1">
|
||||
<i class="fa fa-thumbs-up" aria-hidden="true"></i>
|
||||
<span class="count-like__like-text">'
|
||||
. $countLikes . '</span></a>
|
||||
<a class="interactions__btn-actions action btn-reply" data-cmt-id="'
|
||||
. $comment['comment_id'] . '">' . __('Reply') . '</a>
|
||||
</div>
|
||||
<div class="interactions__cmt-createdat">
|
||||
<span>' . $this->getDateFormat($comment['created_at']) . '</span>
|
||||
</div>
|
||||
</div>';
|
||||
if ($comment['has_reply']) {
|
||||
$this->commentTree .= $this->getCommentsTree(
|
||||
$comments,
|
||||
$comment['comment_id']
|
||||
);
|
||||
}
|
||||
$this->commentTree .= '</li>';
|
||||
}
|
||||
}
|
||||
$this->commentTree .= '</ul>';
|
||||
}
|
||||
|
||||
/**
|
||||
* get tag list
|
||||
*
|
||||
* @param Post $post
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTagList($post)
|
||||
{
|
||||
$tagCollection = $post->getSelectedTagsCollection();
|
||||
$result = '';
|
||||
if (!empty($tagCollection)) {
|
||||
$listTags = [];
|
||||
foreach ($tagCollection as $tag) {
|
||||
$listTags[] = '<a class="mp-info" href="' . $this->getTagUrl($tag) . '">' . $tag->getName() . '</a>';
|
||||
}
|
||||
$result = implode(', ', $listTags);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLoginUrl()
|
||||
{
|
||||
return $this->customerUrl->getLoginUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRegisterUrl()
|
||||
{
|
||||
return $this->customerUrl->getRegisterUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _prepareLayout()
|
||||
{
|
||||
parent::_prepareLayout();
|
||||
|
||||
if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) {
|
||||
if ($catId = $this->getRequest()->getParam('cat')) {
|
||||
$category = $this->categoryFactory->create()
|
||||
->load($catId);
|
||||
if ($category->getId()) {
|
||||
$breadcrumbs->addCrumb($category->getUrlKey(), [
|
||||
'label' => $category->getName(),
|
||||
'title' => $category->getName(),
|
||||
'link' => $this->helperData->getBlogUrl($category, Data::TYPE_CATEGORY)
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$post = $this->getPost();
|
||||
$breadcrumbs->addCrumb($post->getUrlKey(), [
|
||||
'label' => $post->getName(),
|
||||
'title' => $post->getName()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $meta
|
||||
*
|
||||
* @return array|Phrase|string
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getBlogTitle($meta = false)
|
||||
{
|
||||
$blogTitle = parent::getBlogTitle($meta);
|
||||
$post = $this->getBlogObject();
|
||||
if (!$post) {
|
||||
return $blogTitle;
|
||||
}
|
||||
|
||||
if ($meta) {
|
||||
if ($this->helperData->getMetaTitleByStoreId($post->getMetaTitle())) {
|
||||
$blogTitle[] = $this->helperData->getMetaTitleByStoreId($post->getMetaTitle());
|
||||
} else {
|
||||
$blogTitle[] = ucfirst($post->getName());
|
||||
}
|
||||
|
||||
return $blogTitle;
|
||||
}
|
||||
|
||||
return ucfirst($post->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $priority
|
||||
* @param $message
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessagesHtml($priority, $message)
|
||||
{
|
||||
/** @var $messagesBlock Messages */
|
||||
$messagesBlock = $this->_layout->createBlock(Messages::class);
|
||||
$messagesBlock->{$priority}(__($message));
|
||||
|
||||
return $messagesBlock->toHtml();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user