Skip to content

Commit

Permalink
Added reading time to the blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Feb 19, 2024
1 parent 7a0706d commit e212302
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Block/Post/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,16 @@ public function viewsCountEnabled()
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}

/**
* Retrieve 1 if display reading time is enabled
* @return int
*/
public function readingTimeEnabled()
{
return (int) $this->_scopeConfig->getValue(
'mfblog/post_view/reading_time/enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
12 changes: 12 additions & 0 deletions Block/Post/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,16 @@ public function getTemplate()
}
return parent::getTemplate();
}

/**
* Retrieve 1 if display reading time is enabled
* @return int
*/
public function readingTimeEnabled()
{
return (int) $this->_scopeConfig->getValue(
'mfblog/post_view/reading_time/enabled',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
26 changes: 26 additions & 0 deletions Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1098,4 +1098,30 @@ public function getShortContentExtractor()

return $this->shortContentExtractor;
}

/**
* Retrieve reading time
* @return int
*/
public function getReadingTime()
{
if (!$this->getData('reading_time')) {
$wpm = 250;
$contentHtml = $this->getFilteredContent();
$numberOfImages = substr_count( strtolower( $contentHtml ), '<img ' );
$additionalWordsForImages = (int)($numberOfImages * 12) / $wpm;
$wordCount = count(preg_split( '/\s+/', strip_tags($contentHtml)));

$readingTime = 1;

if (!$wordCount && !$additionalWordsForImages){
return $readingTime;
}
$readingTime = ceil(($wordCount + $additionalWordsForImages) / $wpm);

$this->setData('reading_time', $readingTime);
}

return (int)$this->getData('reading_time');
}
}
13 changes: 13 additions & 0 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,19 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
'after' => 'meta_description'
]
);

/* Add reading time to posts table */
$connection->addColumn(
$setup->getTable('magefan_blog_post'),
'reading_time',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
'length' => 20,
'nullable' => true,
'comment' => 'Post Reading Time',
'after' => 'views_count'
]
);
}

$setup->endSetup();
Expand Down
8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>

<group id="reading_time" translate="label" type="text" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Reading Time</label>
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Display Reading Time</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</group>

<group id="post_list" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
3 changes: 3 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<views_count>
<enabled>0</enabled>
</views_count>
<reading_time>
<enabled>0</enabled>
</reading_time>
</post_view>
<post_list>
<template>list-modern</template>
Expand Down
12 changes: 12 additions & 0 deletions view/adminhtml/ui_component/blog_post_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,18 @@
</item>
</argument>
</field>
<field name="reading_time">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">integer</item>
<item name="label" xsi:type="string" translate="true">Reading time (In minutes)</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">post</item>
<item name="sortOrder" xsi:type="number">90</item>
<item name="dataScope" xsi:type="string">reading_time</item>
</item>
</argument>
</field>
</fieldset>
<fieldset name="gallery">
<argument name="data" xsi:type="array">
Expand Down
8 changes: 8 additions & 0 deletions view/frontend/templates/post/info.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
<?php } ?>
<?php } ?>

<!-- reading time -->
<?php if ($block->readingTimeEnabled() && $_post->getReadingTime()) { ?>
<div class="item post-reading-time">
<i class="mf-blog-icon mfbi-reading-time"></i>
<span class="value"><?= $block->escapeHtml($_post->getReadingTime()) . ' ' . __('min read') ?></span>
</div>
<?php } ?>

<?php if ($block->viewsCountEnabled()) { ?>
<?php if ($viewsCount = $_post->getViewsCount()) { ?>
<div class="item post-views">
Expand Down
5 changes: 5 additions & 0 deletions view/frontend/templates/post/view-modern.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
<span class="post-date"><?= $block->escapeHtml($_post->getPublishDate()) ?></span>
<?php } ?>

<!-- reading time -->
<?php if ($block->readingTimeEnabled() && $_post->getReadingTime()) { ?>
<span class="reading-time"><?= ' - ' . $block->escapeHtml($_post->getReadingTime()) . ' ' . __('min read') ?></span>
<?php } ?>

<!-- post views -->
<?php if ($block->viewsCountEnabled()) { ?>
<?php if ($viewsCount = $_post->getViewsCount()) { ?>
Expand Down
1 change: 1 addition & 0 deletions view/frontend/web/css/blog-m.css
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
.mf-blog-icon.mfbi-tags {background-image: url('../images/tags-icon.svg')}
.mf-blog-icon.mfbi-user {background-image: url('../images/user-icon.svg')}
.mf-blog-icon.mfbi-views {background-image: url('../images/views-icon.svg')}
.mf-blog-icon.mfbi-reading-time {background-image: url('../images/views-icon.svg')}
/* END Fix Related Products */


Expand Down

0 comments on commit e212302

Please sign in to comment.