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 |