Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/worker/src/MWP/Extensio...
File: HitCounter.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* This file is part of the ManageWP Worker plugin.
[2] Fix | Delete
*
[3] Fix | Delete
* (c) ManageWP LLC <contact@managewp.com>
[4] Fix | Delete
*
[5] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[6] Fix | Delete
* file that was distributed with this source code.
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
class MWP_Extension_HitCounter
[10] Fix | Delete
{
[11] Fix | Delete
[12] Fix | Delete
private $context;
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* @var int
[16] Fix | Delete
*/
[17] Fix | Delete
private $numberOfDays;
[18] Fix | Delete
[19] Fix | Delete
const OPTION_NAME = 'user_hit_count';
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* @param MWP_WordPress_Context $context
[23] Fix | Delete
* @param int $numberOfDays Number of days to keep the log.
[24] Fix | Delete
*/
[25] Fix | Delete
public function __construct(MWP_WordPress_Context $context, $numberOfDays = 1)
[26] Fix | Delete
{
[27] Fix | Delete
$this->context = $context;
[28] Fix | Delete
$this->numberOfDays = $numberOfDays;
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* @param int $incrementBy
[33] Fix | Delete
* @param DateTime $dateTime
[34] Fix | Delete
*/
[35] Fix | Delete
public function increment($incrementBy = 1, DateTime $dateTime = null)
[36] Fix | Delete
{
[37] Fix | Delete
if ($dateTime === null) {
[38] Fix | Delete
$dateTime = new DateTime('now', new DateTimeZone('UTC'));
[39] Fix | Delete
}
[40] Fix | Delete
$date = $dateTime->format('Y-m-d');
[41] Fix | Delete
[42] Fix | Delete
$hitCount = (array)$this->getHitCount();
[43] Fix | Delete
[44] Fix | Delete
if (!isset($hitCount[$date])) {
[45] Fix | Delete
$hitCount[$date] = 0;
[46] Fix | Delete
[47] Fix | Delete
ksort($hitCount);
[48] Fix | Delete
[49] Fix | Delete
$logSince = clone $dateTime;
[50] Fix | Delete
$logSince->modify(sprintf('-%d day', $this->numberOfDays));
[51] Fix | Delete
$logSinceDate = $logSince->format('Y-m-d');
[52] Fix | Delete
foreach ($hitCount as $hitDate => $hitTotal) {
[53] Fix | Delete
// The old functionality had a bug where keys were invalid dates, hence the date length check.
[54] Fix | Delete
if ($hitDate <= $logSinceDate || strlen($hitDate) !== 10) {
[55] Fix | Delete
unset($hitCount[$hitDate]);
[56] Fix | Delete
}
[57] Fix | Delete
}
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
$hitCount[$date] += $incrementBy;
[61] Fix | Delete
[62] Fix | Delete
$this->context->optionSet(self::OPTION_NAME, $hitCount);
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* @return array
[67] Fix | Delete
*/
[68] Fix | Delete
public function getHitCount()
[69] Fix | Delete
{
[70] Fix | Delete
$hitCount = $this->context->optionGet(self::OPTION_NAME, array());
[71] Fix | Delete
[72] Fix | Delete
return $hitCount;
[73] Fix | Delete
}
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function