PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.25
Advanced Ads – Ad Manager & AdSense v2.0.25
2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / modules / gadsense / includes / class-adsense-report-data.php
advanced-ads / modules / gadsense / includes Last commit date
types 1 year ago adsense-report-api.php 1 year ago class-ad-type-adsense.php 3 months ago class-adsense-report-data.php 1 year ago class-adsense-report.php 1 year ago class-gadsense-data.php 1 year ago class-mapi.php 1 year ago class-network-adsense.php 1 year ago
class-adsense-report-data.php
334 lines
1 <?php
2 /**
3 * Handle all report data received from Google
4 *
5 * @package Advanced_Ads
6 */
7
8 /**
9 * Main class
10 */
11 class AdSense_Report_Data implements Serializable {
12
13 /**
14 * Cached data life span.
15 *
16 * @var integer
17 */
18 const CACHE_DURATION = 3600;
19
20 /**
21 * DB option name for report by domain.
22 *
23 * @var string
24 */
25 const DOMAIN_OPTION = 'advanced_ads_adsense_report_domain';
26
27 /**
28 * DB option name for report by ad unit.
29 *
30 * @var string
31 */
32 const UNIT_OPTION = 'advanced_ads_adsense_report_unit';
33
34 /**
35 * Daily earnings.
36 *
37 * @var null|array
38 */
39 private $earnings;
40
41 /**
42 * Report type. 'unit' or 'domain'.
43 *
44 * @var string
45 */
46 private $type;
47
48 /**
49 * UNIX timestamp at which the data was obtained from Google.
50 *
51 * @var int
52 */
53 private $timestamp = 0;
54
55 /**
56 * Currency used in the report.
57 *
58 * @var string
59 */
60 private $currency = '';
61
62 /**
63 * Version of Google AdSense Management API used.
64 *
65 * @var string
66 */
67 private $version = '';
68
69 /**
70 * List of domain names found in the report data.
71 *
72 * @var array
73 */
74 private $domains = [];
75
76 /**
77 * Instance constructor.
78 *
79 * @param string $type report type.
80 */
81 public function __construct( $type = 'unit' ) {
82 $this->type = $type;
83 }
84
85 /**
86 * Get all domains.
87 *
88 * @return array the domain list.
89 */
90 public function get_domains() {
91 return $this->domains;
92 }
93
94 /**
95 * Get the report timestamp.
96 *
97 * @return int data timestamp.
98 */
99 public function get_timestamp() {
100 return $this->timestamp;
101 }
102
103 /**
104 * Get the currency used in the report.
105 *
106 * @return string the currency code.
107 */
108 public function get_currency() {
109 return $this->currency;
110 }
111
112 /**
113 * Serialize an instance of this class into a string. For PHP version >= 7.4
114 *
115 * @return array
116 */
117 public function __serialize() {
118 return [
119 'earnings' => $this->earnings,
120 'type' => $this->type,
121 'timestamp' => $this->timestamp,
122 'currency' => $this->currency,
123 'domains' => $this->domains,
124 ];
125 }
126
127 /**
128 * Recreate an instance of this class from a string. For PHP version >= 7.4
129 *
130 * @param array $data the array from __serialize.
131 *
132 * @return void
133 */
134 public function __unserialize( $data ) {
135 $this->earnings = $data['earnings'] ?? null;
136 $this->type = $data['type'] ?? null;
137 $this->timestamp = $data['timestamp'] ?? 0;
138 $this->currency = $data['currency'] ?? '';
139 $this->domains = $data['domains'] ?? [];
140 }
141
142 /**
143 * Returns serialized object properties. For PHP version < 7.4
144 *
145 * @return string the serialized data.
146 */
147 public function serialize() {
148 return serialize(
149 [
150 'earnings' => $this->earnings,
151 'type' => $this->type,
152 'timestamp' => $this->timestamp,
153 'currency' => $this->currency,
154 'domains' => $this->domains,
155 ]
156 );
157 }
158
159 /**
160 * Set object properties from serialized data string. For PHP version < 7.4
161 *
162 * @param string $data serilaized data from DB.
163 */
164 public function unserialize( $data ) {
165 try {
166 $unwrapped = unserialize( $data );
167 } catch ( Exception $ex ) {
168 $unwrapped = [];
169 }
170 $this->__unserialize( $unwrapped );
171 }
172
173 /**
174 * Update object properties and DB record from a API response.
175 *
176 * @param array $response API call response from Google.
177 */
178 public function update_data_from_response( $response ) {
179 $headers = [];
180 $this->version = $response['api_version'];
181 $this->timestamp = $response['timestamp'];
182 foreach ( $response['headers'] as $header ) {
183 if ( 'METRIC_CURRENCY' === $header['type'] ) {
184 $this->currency = $header['currencyCode'];
185 }
186 $headers[] = $header['name'];
187 }
188 $earnings = [];
189
190 if ( ! empty( $response['rows'] ) ) {
191 foreach ( $response['rows'] as $row ) {
192 $earning = new StdClass();
193 foreach ( $row['cells'] as $index => $cell ) {
194 switch ( $headers[ $index ] ) {
195 case 'DATE':
196 $earning->date = new DateTimeImmutable( $cell['value'] );
197 break;
198 case 'ESTIMATED_EARNINGS':
199 $earning->estimated_earning = (float) $cell['value'];
200 break;
201 default: // "DOMAIN_NAME" or "AD_UNIT_ID".
202 $earning->{strtolower( $headers[ $index ] )} = $cell['value'];
203 if ( 'DOMAIN_NAME' === $headers[ $index ] && ! in_array( $cell['value'], $this->domains, true ) ) {
204 $this->domains[] = $cell['value'];
205 }
206 }
207 }
208 $earnings[] = $earning;
209 }
210 }
211 $this->earnings = $earnings;
212 $option_name = 'unit' === $this->type ? self::UNIT_OPTION : self::DOMAIN_OPTION;
213
214 // Delete old options entries.
215 delete_option( 'advanced_ads_adsense_report_DATE_AD_UNIT_CODE_EARNINGS_dashboard' );
216 delete_option( 'advanced_ads_adsense_report_DATE_DOMAIN_NAME_EARNINGS_dashboard' );
217
218 // Save the data instance in DB.
219 update_option( $option_name, $this->serialize() );
220 }
221
222 /**
223 * Returns a data object constructed from saved data. Constructs a new one if there is no usable data.
224 *
225 * @param string $type report type.
226 *
227 * @return AdSense_Report_Data
228 */
229 public static function get_data_from_options( $type ) {
230 $option_name = 'unit' === $type ? self::UNIT_OPTION : self::DOMAIN_OPTION;
231 $option = get_option( $option_name, false );
232 if ( ! $option ) {
233 return new self( $type );
234 }
235
236 // PHP version < 7.4.
237 if ( $option instanceof self ) {
238 return $option;
239 }
240
241 try {
242 $unserialized = is_serialized( $option ) ? unserialize( $option ) : null;
243 if ( $unserialized instanceof self ) {
244 return $unserialized;
245 }
246
247 return new self( $type );
248 } catch ( Exception $ex ) {
249 return new self( $type );
250 }
251 }
252
253 /**
254 * Checks if cached data need to be updated.
255 *
256 * @return bool true if the stored data has not expired yet.
257 */
258 public function is_valid() {
259 return $this->timestamp + self::CACHE_DURATION > time();
260 }
261
262 /**
263 * Get the earnings sums for display.
264 *
265 * @param string $filter filter sums by a given domain name or ad unit.
266 *
267 * @return int[]
268 */
269 public function get_sums( $filter = '' ) {
270 $today = new DateTimeImmutable();
271 $yesterday = $today->sub( date_interval_create_from_date_string( '1 day' ) );
272 $prev7 = $today->sub( date_interval_create_from_date_string( '7 days' ) );
273 $prev28 = $today->sub( date_interval_create_from_date_string( '28 days' ) );
274 $sums = [
275 'today' => 0,
276 'yesterday' => 0,
277 '7days' => 0,
278 'this_month' => 0,
279 '28days' => 0,
280 ];
281
282 // Unit type reports should always have the ad unit id specified.
283 if ( '' === $filter && 'unit' === $this->type ) {
284 return $sums;
285 }
286
287 if ( ! is_array( $this->earnings ) || empty( $this->earnings ) ) {
288 return $sums;
289 }
290
291 foreach ( $this->earnings as $value ) {
292 if (
293 ( 'unit' === $this->type && false === strpos( $value->ad_unit_id, $filter ) )
294 || ( ! empty( $filter ) && 'domain' === $this->type && $filter !== $value->domain_name )
295 ) {
296 continue;
297 }
298
299 if ( $this->date_ymd( $value->date ) === $this->date_ymd( $today ) ) {
300 $sums['today'] += $value->estimated_earning;
301 }
302 if ( $this->date_ymd( $value->date ) === $this->date_ymd( $yesterday ) ) {
303 $sums['yesterday'] += $value->estimated_earning;
304 }
305 if ( $this->date_ymd( $value->date ) >= $this->date_ymd( $prev7 ) ) {
306 $sums['7days'] += $value->estimated_earning;
307 }
308 if ( $this->date_ymd( $value->date ) >= $this->date_ymd( $prev28 ) ) {
309 $sums['28days'] += $value->estimated_earning;
310 }
311 if ( $value->date->format( 'm' ) === $today->format( 'm' ) ) {
312 $sums['this_month'] += $value->estimated_earning;
313 }
314 }
315
316 return $sums;
317 }
318
319 /**
320 * Get an integer representation of a DateTime object to be used in date comparison.
321 *
322 * @param DateTimeInterface $date the date object.
323 *
324 * @return int
325 */
326 private function date_ymd( $date ) {
327 if ( $date instanceof DateTimeInterface ) {
328 return (int) $date->format( 'Ymd' );
329 }
330
331 return 0;
332 }
333 }
334