Skip to content

Commit f346592

Browse files
committed
Changes for recent blog post widget after code review
1 parent 3c0763b commit f346592

File tree

6 files changed

+60
-111
lines changed

6 files changed

+60
-111
lines changed

Block/Widget/Recent.php

Lines changed: 21 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
44
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
55
*
66
* Glory to Ukraine! Glory to the heroes!
@@ -18,28 +18,7 @@ class Recent extends \Magefan\Blog\Block\Post\PostList\AbstractList implements \
1818
* @var \Magefan\Blog\Model\CategoryFactory
1919
*/
2020
protected $_categoryFactory;
21-
22-
/**
23-
* @var \Magefan\Blog\Model\TagFactory
24-
*/
25-
protected $tagFactory;
26-
27-
/**
28-
* @var \Magefan\Blog\Model\Tag
29-
*/
30-
protected $tag;
31-
32-
/**
33-
* @var \Magefan\Blog\Model\AuthorFactory
34-
*/
35-
protected $authorFactory;
3621

37-
/**
38-
* @var \Magefan\Blog\Model\Author
39-
*/
40-
protected $author;
41-
42-
4322
/**
4423
* @var \Magefan\Blog\Model\Category
4524
*/
@@ -54,8 +33,6 @@ class Recent extends \Magefan\Blog\Block\Post\PostList\AbstractList implements \
5433
* @param \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory
5534
* @param \Magefan\Blog\Model\Url $url
5635
* @param \Magefan\Blog\Model\CategoryFactory $categoryFactory
57-
* @param \Magefan\Blog\Model\TagFactory $tagFactory
58-
* @param \Magefan\Blog\Model\AuthorFactory $authorFactory
5936
* @param array $data
6037
*/
6138
public function __construct(
@@ -65,16 +42,10 @@ public function __construct(
6542
\Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
6643
\Magefan\Blog\Model\Url $url,
6744
\Magefan\Blog\Model\CategoryFactory $categoryFactory,
68-
$tagFactory = null,
69-
$authorFactory = null,
7045
array $data = []
7146
) {
7247
parent::__construct($context, $coreRegistry, $filterProvider, $postCollectionFactory, $url, $data);
7348
$this->_categoryFactory = $categoryFactory;
74-
75-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
76-
$this->tagFactory = $objectManager->create(\Magefan\Blog\Model\TagFactory::class);
77-
$this->authorFactory = $objectManager->create(\Magefan\Blog\Model\AuthorFactory::class);
7849
}
7950

8051
/**
@@ -123,7 +94,24 @@ protected function _preparePostCollection()
12394
if ($category = $this->getCategory()) {
12495
$this->_postCollection->addCategoryFilter($category);
12596
}
126-
97+
98+
if ($tagId = $this->getData('tag_id')) {
99+
$this->_postCollection->addTagFilter($tagId);
100+
}
101+
102+
if ($authorId = $this->getData('author_id')) {
103+
$this->_postCollection->addAuthorFilter($authorId);
104+
}
105+
106+
if ($from = $this->getData('from')) {
107+
$this->_postCollection
108+
->addFieldToFilter('publish_time', array('gteq' => $from . " 00:00:00"));
109+
}
110+
111+
if ($to = $this->getData('to')) {
112+
$this->_postCollection
113+
->addFieldToFilter('publish_time', array('lteq' => $to . " 00:00:00"));
114+
}
127115
}
128116

129117
/**
@@ -144,72 +132,11 @@ public function getCategory()
144132
return $this->_category = $category;
145133
}
146134
}
147-
}
148-
149-
if ($tag = $this->getTag()) {
150-
$this->_postCollection->addTagFilter($tag);
151-
}
152-
153-
if ($author = $this->getAuthor()) {
154-
$this->_postCollection->addAuthorFilter($author);
155-
}
156-
if ($this->getData('from')) {
157-
$this->_postCollection
158-
->addFieldToFilter('publish_time', array('gteq' => $this->getData('from') . " 00:00:00"));
159-
}
160-
161-
if ($this->getData('to')) {
162-
$this->_postCollection
163-
->addFieldToFilter('publish_time', array('lteq' => $this->getData('to') . " 00:00:00"));
164-
}
165-
}
166-
167-
168-
/**
169-
* Retrieve author instance
170-
*
171-
* @return \Magefan\Blog\Model\Author
172-
*/
173-
public function getAuthor()
174-
{
175-
if ($this->author === null) {
176-
if ($authotId = $this->getData('author_id')) {
177-
$author = $this->authorFactory->create();
178-
$author->load($authotId);
179-
180-
return $this->author = $author;
181-
182-
}
183-
184-
$this->author = false;
185-
}
186-
187-
return $this->author;
188-
189-
}
190-
191-
/**
192-
* Retrieve tag instance
193-
*
194-
* @return \Magefan\Blog\Model\Tag
195-
*/
196-
public function getTag()
197-
{
198-
if ($this->tag === null) {
199-
if ($tagId = $this->getData('tags_id')) {
200-
201-
$tag = $this->tagFactory->create();
202-
$tag->load($tagId);
203-
204-
return $this->tag = $tag;
205-
206-
}
207135

208-
$this->tag = false;
136+
$this->_category = false;
209137
}
210138

211-
return $this->tag;
212-
139+
return $this->_category;
213140
}
214141

215142
/**

Model/Config/Source/Tag.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Magefan\Blog\Model\Config\Source;
1010

1111
/**
12-
* Used in recent post widget
12+
* Used in recent post widget & post edit page
1313
*
1414
*/
1515
class Tag implements \Magento\Framework\Option\ArrayInterface
@@ -44,7 +44,7 @@ public function __construct(
4444
public function toOptionArray()
4545
{
4646
if ($this->options === null) {
47-
$this->options = [['label' => __('Please select'), 'value' => 0]];
47+
$this->options = [];
4848
$collection = $this->tagCollectionFactory->create();
4949
$collection->setOrder('title');
5050

Model/Config/Source/WidgetTag.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Model\Config\Source;
10+
11+
/**
12+
* Used in recent post widget
13+
*
14+
*/
15+
class WidgetTag extends Tag
16+
{
17+
/**
18+
* Options getter
19+
*
20+
* @return array
21+
*/
22+
public function toOptionArray()
23+
{
24+
if ($this->options === null) {
25+
parent::toOptionArray();
26+
array_unshift($this->options, ['label' => __('Please select'), 'value' => 0]);
27+
}
28+
29+
return $this->options;
30+
}
31+
32+
}

Model/Import/AbstractImport.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
/**
3-
* Copyright © 2017 Magefan (support@magefan.com). All rights reserved.
3+
* Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
44
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
57
*/
68

79
namespace Magefan\Blog\Model\Import;

Model/Tag.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,6 @@ public function checkIdentifier($identifier)
8787
{
8888
return $this->load($identifier)->getId();
8989
}
90-
91-
/**
92-
* Retrieve if is visible on store
93-
* @return bool
94-
*/
95-
public function isVisibleOnStore($storeId)
96-
{
97-
return $this->getIsActive()
98-
&& $this->getData('publish_time') <= $this->getResource()->getDate()->gmtDate()
99-
&& array_intersect([0, $storeId], $this->getStoreIds());
100-
}
10190

10291
/**
10392
* Retrieve catgegory url route path

etc/widget.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
33
/**
4-
* Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
4+
* Copyright © Magefan (support@magefan.com). All rights reserved.
55
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
66
*
77
* Glory to Ukraine! Glory to the heroes!
@@ -24,7 +24,7 @@
2424
<label translate="true">Blog Category</label>
2525
<description translate="true">Leave blank to display posts from all categories.</description>
2626
</parameter>
27-
<parameter name="tags_id" xsi:type="select" source_model="Magefan\Blog\Model\Config\Source\Tag" visible="true" sort_order="40" >
27+
<parameter name="tag_id" xsi:type="select" source_model="Magefan\Blog\Model\Config\Source\WidgetTag" visible="true" sort_order="40" >
2828
<label translate="true">Blog Tag</label>
2929
<description translate="true">Leave blank to display posts from all tags.</description>
3030
</parameter>
@@ -41,7 +41,6 @@
4141
<description translate="true">Please use date format YYYY-MM-DD.</description>
4242
</parameter>
4343
<parameter name="custom_template" xsi:type="text" visible="true" sort_order="80" >
44-
4544
<label translate="true">Custom Template</label>
4645
<description translate="true"><![CDATA[
4746
Leave blank to use default template <em>Magefan_Blog::widget/recent.phtml</em>.<br/>

0 commit comments

Comments
 (0)