From 14d5b02fec9ff6eb438212188c87fcb3351db0a7 Mon Sep 17 00:00:00 2001 From: DenysTsymbal Date: Wed, 13 Jul 2022 10:55:50 +0300 Subject: [PATCH] Add cron for resaving posts --- Cron/ReSaveExistingPosts.php | 32 +++++++++++++++++++------------- etc/cron_groups.xml | 18 ++++++++++++++++++ etc/crontab.xml | 14 ++++++++++++++ 3 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 etc/cron_groups.xml create mode 100644 etc/crontab.xml diff --git a/Cron/ReSaveExistingPosts.php b/Cron/ReSaveExistingPosts.php index b3884ae5..b2380b14 100644 --- a/Cron/ReSaveExistingPosts.php +++ b/Cron/ReSaveExistingPosts.php @@ -10,22 +10,25 @@ use Magefan\Blog\Model\Config; use Magento\Framework\Stdlib\DateTime\DateTime; +/** + * ReSave Posts that have PublishTime <= CurrentTime In Order To They Be Visible - Need If FPC Is Enabled + */ class ReSaveExistingPosts { /** * @var Config */ - protected $config; + private $config; /** * @var PostCollectionFactory */ - protected $postCollectionFactory; + private $postCollectionFactory; /** * @var DateTime */ - protected $date; + private $date; /** * @param Config $config @@ -36,26 +39,29 @@ public function __construct( Config $config, PostCollectionFactory $postCollectionFactory, DateTime $date - ) - { + ) { $this->config = $config; $this->postCollectionFactory = $postCollectionFactory; $this->date = $date; } - + /** + * @return void + */ public function execute() { + if (!$this->config->isEnabled()) { + return; + } $postCollection = $this->postCollectionFactory->create() - ->addFieldToFilter('publish_time', array('gteq' => $this->date->gmtDate('Y-m-d H:i:s', strtotime('-2 minutes')))) - ->addFieldToFilter('publish_time', array('lteq' => $this->date->gmtDate())); - - var_dump($this->date->gmtDate()); - var_dump(count($postCollection)); + ->addActiveFilter() + ->addFieldToFilter('publish_time', ['gteq' => $this->date->gmtDate('Y-m-d H:i:s', strtotime('-2 minutes'))]) + ->addFieldToFilter('publish_time', ['lteq' => $this->date->gmtDate()]); foreach ($postCollection as $post) { - var_dump($post->getId());exit; + $post->setOrigData('is_active', 0); + $post->afterCommitCallback(); } } -} \ No newline at end of file +} diff --git a/etc/cron_groups.xml b/etc/cron_groups.xml new file mode 100644 index 00000000..97964ec6 --- /dev/null +++ b/etc/cron_groups.xml @@ -0,0 +1,18 @@ + + + + + 1 + 4 + 2 + 10 + 60 + 600 + 1 + + diff --git a/etc/crontab.xml b/etc/crontab.xml new file mode 100644 index 00000000..255451f4 --- /dev/null +++ b/etc/crontab.xml @@ -0,0 +1,14 @@ + + + + + + */1 * * * * + + +