/var/www/html_fr/wp-content/plugins/fluent-smtp/app/Services/Reporting.php


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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace FluentMail\App\Services;

class 
Reporting
{
    protected static 
$daily 'P1D';
    protected static 
$weekly 'P1W';
    protected static 
$monthly 'P1M';

    public function 
getSendingStats($from$to)
    {
        
$period $this->makeDatePeriod(
            
$from $this->makeFromDate($from),
            
$to $this->makeToDate($to),
            
$frequency $this->getFrequency($from$to)
        );

        list(
$groupBy$orderBy) = $this->getGroupAndOrder($frequency);

        global 
$wpdb;

        
$tableName $wpdb->prefix.FLUENT_MAIL_DB_PREFIX.'email_logs';

        
$items $wpdb->get_results($wpdb->prepare(
            
'SELECT COUNT(id) AS count, DATE(created_at) AS date FROM `%1$s` WHERE `created_at` BETWEEN \'%2$s\' AND \'%3$s\' GROUP BY `%4$s` ORDER BY `%5$s` ASC',
            
$tableName,
            
$from->format('Y-m-d'),
            
$to->format('Y-m-d'),
            
$groupBy,
            
$orderBy
        
));

        return 
$this->getResult($period$items);
    }

    protected function 
makeDatePeriod($from$to$interval null)
    {
        
$interval $interval ?: static::$daily;

        return new 
\DatePeriod($from, new \DateInterval($interval), $to);
    }

    protected function 
makeFromDate($from)
    {
        
$from $from ?: '-7 days';

        return new 
\DateTime($from);
    }

    protected function 
makeToDate($to)
    {
        
$to $to gmdate('Y-m-d'strtotime$to " +1 days")) : '+1 days';
        return new 
\DateTime($to);
    }

    protected function 
getFrequency($from$to)
    {
        
$numDays $to->diff($from)->format("%a");

        if (
$numDays 62 && $numDays <= 181) {
            return static::
$weekly;
        } else if (
$numDays 181) {
            return static::
$monthly;
        }

        return static::
$daily;
    }

    protected function 
getGroupAndOrder($frequency)
    {
        
$orderBy $groupBy 'date';

        if (
$frequency == static::$weekly) {
            
$orderBy $groupBy 'week';
        } else if (
$frequency == static::$monthly) {
            
$orderBy $groupBy 'month';
        }

        return [
$groupBy$orderBy];
    }

    protected function 
prepareSelect($frequency$dateField 'created_at')
    {
        
$select = [
            
fluentMailDb()->raw('COUNT(id) AS count'),
            
fluentMailDb()->raw('DATE('.$dateField.') AS date')
        ];

        if (
$frequency == static::$weekly) {
            
$select[] = fluentMailDb()->raw('WEEK(created_at) week');
        } else if (
$frequency == static::$monthly) {
            
$select[] = fluentMailDb()->raw('MONTH(created_at) month');
        }

        return 
$select;
    }

    protected function 
getResult($period$items)
    {
        
$range $this->getDateRangeArray($period);

        
$formatter 'basicFormatter';

        if (
$this->isMonthly($period)) {
            
$formatter 'monYearFormatter';
        }

        foreach (
$items as $item) {
            
$date $this->{$formatter}($item->date);
            
$range[$date] = (int) $item->count;
        }

        return 
$range;
    }

    protected function 
getDateRangeArray($period)
    {
        
$range = [];

        
$formatter 'basicFormatter';

        if (
$this->isMonthly($period)) {
            
$formatter 'monYearFormatter';
        }

        foreach (
$period as $date) {
            
$date $this->{$formatter}($date);
            
$range[$date] = 0;
        }

        return 
$range;
    }

    protected function 
basicFormatter($date)
    {
        if (
is_string($date)) {
            
$date = new \DateTime($date);
        }

        return 
$date->format('Y-m-d');
    }

    protected function 
monYearFormatter($date)
    {
        if (
is_string($date)) {
            
$date = new \DateTime($date);
        }

        return 
$date->format('M Y');
    }

    protected function 
isMonthly($period)
    {
        return !!
$period->getDateInterval()->m;
    }
}