/
update-patrons-circle
executable file
·99 lines (84 loc) · 2.86 KB
/
update-patrons-circle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/php
<?
require_once('/standardebooks.org/web/lib/Core.php');
use Safe\DateTimeImmutable;
// Get a list of payments that are within 1 year / 45 days of today, and deactivate Patrons Circle members
// who aren't in that list.
// We give a 15 day grace period to Patrons Circle members because sometimes FA can be delayed in charging.
$now = new DateTimeImmutable();
$lastYear = new DateTimeImmutable('-1 year');
$expiredPatrons = Db::Query('
SELECT * from Patrons
where
Ended is null and
UserId not in
(
select distinct UserId from Payments where
UserId is not null
and
(
(IsRecurring = true and Amount >= 10 and Created > ? - interval 45 day)
or
(IsRecurring = false and Amount >= 100 and Created > ? - interval 1 year)
)
)
', [$now, $now], Patron::class);
if(sizeof($expiredPatrons) > 0){
$ebooksThisYear = 0;
// We can't use the Library class to get ebooks because this script is typically run via cron or CLI,
// which doesn't have access PHP-FMP's APCu cache.
foreach(explode("\n", trim(shell_exec('find ' . EBOOKS_DIST_PATH . ' -name "content.opf"') ?? '')) as $filename){
$metadata = file_get_contents($filename);
// Don't create a new Ebook object because that's very slow. Just do a regex match for speed.
preg_match_all('/<dc:date>(.+?)<\/dc:date>/iu', $metadata, $matches);
if(sizeof($matches) > 0){
$created = new DateTimeImmutable($matches[1][0]);
if($created >= $lastYear){
$ebooksThisYear++;
}
}
}
foreach($expiredPatrons as $patron){
Db::Query('
UPDATE Patrons
set Ended = ?
where UserId = ?
', [$now, $patron->UserId]);
Db::Query('
UPDATE Benefits
set CanAccessFeeds = false,
CanVote = false,
CanBulkDownload = false
where UserId = ?
', [$patron->UserId]);
// Email the patron to notify them their term has ended
// Is the patron a recurring subscriber?
$lastPayment = Db::Query('
SELECT *
from Payments
where UserId = ?
order by Created desc
limit 1
', [$patron->UserId], Payment::class);
if(sizeof($lastPayment) > 0 && $patron->User->Email !== null){
$em = new Email();
$em->From = EDITOR_IN_CHIEF_EMAIL_ADDRESS;
$em->FromName = EDITOR_IN_CHIEF_NAME;
$em->To = $patron->User->Email;
$em->ToName = $patron->Name;
$em->Subject = 'Will you still help us make free, beautiful digital literature?';
if($lastPayment[0]->IsRecurring){
// Email recurring donors who have lapsed
$em->Body = Template::EmailPatronsCircleRecurringCompleted();
$em->TextBody = Template::EmailPatronsCircleRecurringCompletedText();
}
else{
// Email one time donors who have expired after one year
$em->Body = Template::EmailPatronsCircleCompleted(['ebooksThisYear' => $ebooksThisYear]);
$em->TextBody = Template::EmailPatronsCircleCompletedText(['ebooksThisYear' => $ebooksThisYear]);
}
$em->Send();
}
}
}
?>