| 1 |
<?php |
| 2 |
|
| 3 |
namespace ContentEgg\application; |
| 4 |
|
| 5 |
defined('\ABSPATH') || exit; |
| 6 |
|
| 7 |
use ContentEgg\application\components\ModuleManager; |
| 8 |
use ContentEgg\application\components\ContentManager; |
| 9 |
use ContentEgg\application\admin\GeneralConfig; |
| 10 |
use ContentEgg\application\vendor\CrawlerDetect\CrawlerDetect; |
| 11 |
|
| 12 |
/** |
| 13 |
* ModuleUpdateVisit class file |
| 14 |
* |
| 15 |
* @author keywordrush.com <support@keywordrush.com> |
| 16 |
* @link https://www.keywordrush.com |
| 17 |
* @copyright Copyright © 2026 keywordrush.com |
| 18 |
*/ |
| 19 |
class ModuleUpdateVisit |
| 20 |
{ |
| 21 |
|
| 22 |
private static $instance = null; |
| 23 |
|
| 24 |
public static function getInstance() |
| 25 |
{ |
| 26 |
if (self::$instance == null) |
| 27 |
self::$instance = new self; |
| 28 |
|
| 29 |
return self::$instance; |
| 30 |
} |
| 31 |
|
| 32 |
private function __construct() |
| 33 |
{ |
| 34 |
} |
| 35 |
|
| 36 |
public function init() |
| 37 |
{ |
| 38 |
if (defined('\WP_CLI') && \WP_CLI) |
| 39 |
return; |
| 40 |
|
| 41 |
if (GeneralConfig::getInstance()->option('filter_bots')) |
| 42 |
{ |
| 43 |
$cd = new CrawlerDetect(); |
| 44 |
if ($cd->isCrawler()) |
| 45 |
{ |
| 46 |
return; |
| 47 |
} |
| 48 |
} |
| 49 |
// exec before post layout |
| 50 |
\add_filter('template_redirect', array($this, 'update'), 10); |
| 51 |
} |
| 52 |
|
| 53 |
public function update() |
| 54 |
{ |
| 55 |
if (!is_single() && !is_page()) |
| 56 |
return; |
| 57 |
|
| 58 |
$this->updateByKeyword(); |
| 59 |
$this->updateItems(); |
| 60 |
} |
| 61 |
|
| 62 |
private function updateByKeyword() |
| 63 |
{ |
| 64 |
global $post; |
| 65 |
|
| 66 |
if (empty($post)) |
| 67 |
return; |
| 68 |
|
| 69 |
foreach (ModuleManager::getInstance()->getParsers(true) as $module) |
| 70 |
{ |
| 71 |
$is_visit_update = in_array($module->config('update_mode'), array('visit', 'visit_cron')); |
| 72 |
$is_data_exists = ContentManager::isDataExists($post->ID, $module->getId()); |
| 73 |
|
| 74 |
// parse data if not exists in any case |
| 75 |
if (!$is_visit_update && $is_data_exists) |
| 76 |
continue; |
| 77 |
|
| 78 |
$ttl = $module->config('ttl'); |
| 79 |
if (!$ttl && $is_data_exists) |
| 80 |
continue; |
| 81 |
|
| 82 |
if (!ContentManager::getAutoupdateKeyword($post->ID, $module->getId())) |
| 83 |
continue; |
| 84 |
|
| 85 |
$last_update = (int) \get_post_meta($post->ID, ContentManager::META_PREFIX_LAST_BYKEYWORD_UPDATE . $module->getId(), true); |
| 86 |
if ($last_update && time() - $last_update < $ttl) |
| 87 |
continue; |
| 88 |
|
| 89 |
ContentManager::updateByKeyword($post->ID, $module->getId()); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private function updateItems() |
| 94 |
{ |
| 95 |
global $post; |
| 96 |
|
| 97 |
if (empty($post)) |
| 98 |
return; |
| 99 |
|
| 100 |
foreach (ModuleManager::getInstance()->getAffiliateParsers(true) as $module) |
| 101 |
{ |
| 102 |
if (!in_array($module->config('update_mode'), array('visit', 'visit_cron'))) |
| 103 |
continue; |
| 104 |
|
| 105 |
if (!$module->isItemsUpdateAvailable()) |
| 106 |
continue; |
| 107 |
|
| 108 |
if (!$ttl_items = $module->config('ttl_items')) |
| 109 |
continue; |
| 110 |
|
| 111 |
$last_items_update = (int) \get_post_meta($post->ID, ContentManager::META_PREFIX_LAST_ITEMS_UPDATE . $module->getId(), true); |
| 112 |
if (!$last_items_update || time() - $last_items_update < $ttl_items) |
| 113 |
continue; |
| 114 |
ContentManager::updateItems($post->ID, $module->getId()); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|