| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Tracking; |
| 4 |
|
| 5 |
use Give\Tracking\Contracts\TrackData; |
| 6 |
use Give\Tracking\Enum\EventType; |
| 7 |
use Give\Tracking\Repositories\TrackEvents; |
| 8 |
use Give\Tracking\TrackingData\EditedDonationFormsData; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class TrackJob |
| 12 |
* |
| 13 |
* @package Give\Tracking |
| 14 |
* @since 2.10.0 |
| 15 |
*/ |
| 16 |
class TrackJob |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @var TrackClient |
| 20 |
*/ |
| 21 |
private $trackClient; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var TrackEvents |
| 25 |
*/ |
| 26 |
private $trackEvents; |
| 27 |
|
| 28 |
/** |
| 29 |
* TrackJob constructor. |
| 30 |
* |
| 31 |
* @param TrackClient $trackClient |
| 32 |
* @param TrackEvents $trackEvents |
| 33 |
*/ |
| 34 |
public function __construct(TrackClient $trackClient, TrackEvents $trackEvents) |
| 35 |
{ |
| 36 |
$this->trackClient = $trackClient; |
| 37 |
$this->trackEvents = $trackEvents; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Send tracks. |
| 42 |
* |
| 43 |
* @since 2.10.0 |
| 44 |
*/ |
| 45 |
public function send() |
| 46 |
{ |
| 47 |
$recordedTracks = $this->trackEvents->getTrackList(); |
| 48 |
|
| 49 |
if ( ! $recordedTracks) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
foreach ($recordedTracks as $trackId => $className) { |
| 54 |
/* @var TrackData $class */ |
| 55 |
$class = give($className); |
| 56 |
$eventType = new EventType($trackId); |
| 57 |
|
| 58 |
if ($class instanceof TrackData) { |
| 59 |
$this->trackClient->post($eventType, $class); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$this->trackEvents->saveRequestTime(); |
| 64 |
$this->trackEvents->removeTrackList(); |
| 65 |
|
| 66 |
if (in_array(EditedDonationFormsData::class, $recordedTracks, true)) { |
| 67 |
$this->trackEvents->removeRecentlyEditedDonationFormList(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Send tracked information immediately. |
| 73 |
* |
| 74 |
* @since 2.10.2 |
| 75 |
* |
| 76 |
* @param array $trackedEvents |
| 77 |
*/ |
| 78 |
public function sendNow($trackedEvents) |
| 79 |
{ |
| 80 |
/* @var TrackEvents $trackEvents */ |
| 81 |
$trackEvents = give(TrackEvents::class); |
| 82 |
|
| 83 |
foreach ($trackedEvents as $trackEvent) { |
| 84 |
give($trackEvent)->record(); |
| 85 |
} |
| 86 |
|
| 87 |
$trackEvents->saveTrackList(); |
| 88 |
$this->send(); |
| 89 |
|
| 90 |
// Do not setup cron job. |
| 91 |
$class = TrackJobScheduler::class; |
| 92 |
add_filter("give_disable_hook-shutdown:{$class}@schedule", '__return_true'); |
| 93 |
} |
| 94 |
} |
| 95 |
|