types
3 weeks ago
adsense-report-api.php
3 weeks ago
class-ad-type-adsense.php
3 weeks ago
class-adsense-report-data.php
3 weeks ago
class-adsense-report.php
3 weeks ago
class-gadsense-data.php
3 weeks ago
class-mapi.php
3 weeks ago
class-network-adsense.php
3 weeks ago
class-adsense-report.php
184 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName |
| 2 | |
| 3 | /** |
| 4 | * AdSense earnings report display. |
| 5 | * |
| 6 | * @package AdvancedAds |
| 7 | * @author Advanced Ads <info@wpadvancedads.com> |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Class Advanced_Ads_AdSense_Report |
| 12 | * |
| 13 | * Displays AdSense earnings on the ad overview page or the ad edit page. |
| 14 | */ |
| 15 | class Advanced_Ads_AdSense_Report { |
| 16 | |
| 17 | /** |
| 18 | * Domain name or ad unit to filter data with before display. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | private $filter; |
| 23 | |
| 24 | /** |
| 25 | * Report type. 'unit' or 'domain'. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | private $type; |
| 30 | |
| 31 | /** |
| 32 | * Object representing the current report data. |
| 33 | * |
| 34 | * @var AdSense_Report_Data |
| 35 | */ |
| 36 | private $data_object; |
| 37 | |
| 38 | /** |
| 39 | * Error from the last attempt to call Google. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | private $last_api_error_message; |
| 44 | |
| 45 | /** |
| 46 | * Instance constructor. |
| 47 | * |
| 48 | * @param string $type report type. |
| 49 | * @param string $filter report filter. |
| 50 | */ |
| 51 | public function __construct( $type = 'unit', $filter = '' ) { |
| 52 | $this->type = $type; |
| 53 | |
| 54 | if ( 'domain' === $type && ! empty( $filter ) ) { |
| 55 | update_option( 'advanced-ads-adsense-dashboard-filter', $filter ); |
| 56 | // Backward compatibility: "*" was used to display data for all domains if API version prior to 2.0. |
| 57 | if ( '*' === $filter ) { |
| 58 | $filter = ''; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | $this->filter = $filter; |
| 63 | $this->data_object = AdSense_Report_Data::get_data_from_options( $type ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Tries to get fresh data from Google. |
| 68 | * |
| 69 | * @return bool true if we got fresh data. |
| 70 | */ |
| 71 | public function refresh_report() { |
| 72 | $api_helper = new Advanced_Ads_AdSense_Report_Api( $this->type ); |
| 73 | $error = []; |
| 74 | |
| 75 | if ( $api_helper->has_token() ) { |
| 76 | $response = $api_helper->call_google(); |
| 77 | if ( true === $response['status'] ) { |
| 78 | $this->data_object->update_data_from_response( $response['response_body'] ); |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | if ( isset( $response['msg'] ) ) { |
| 83 | $this->last_api_error_message = $response['msg']; |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if ( $api_helper->has_token_error() ) { |
| 90 | $error = $api_helper->get_token_error(); |
| 91 | } |
| 92 | |
| 93 | if ( isset( $error['msg'] ) ) { |
| 94 | $this->last_api_error_message = $error['msg']; |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | if ( isset( $error['raw'] ) ) { |
| 100 | $this->last_api_error_message = $error['raw']; |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | if ( empty( $this->last_api_error_message ) ) { |
| 106 | $this->last_api_error_message = __( 'No valid tokens', 'advanced-ads' ); |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Retrieve the error message from the last API call. |
| 114 | * |
| 115 | * @return string Error message from the last API call. |
| 116 | */ |
| 117 | public function get_last_api_error() { |
| 118 | if ( empty( $this->last_api_error_message ) ) { |
| 119 | return ''; |
| 120 | } |
| 121 | |
| 122 | return $this->last_api_error_message; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the report data object. |
| 127 | * |
| 128 | * @return AdSense_Report_Data |
| 129 | */ |
| 130 | public function get_data() { |
| 131 | return $this->data_object; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Build an return the HTML markup for display. |
| 136 | * |
| 137 | * @return string the final markup. |
| 138 | */ |
| 139 | public function get_markup() { |
| 140 | if ( ! $this->get_data()->is_valid() ) { |
| 141 | return '<p style="text-align:center;"><span class="report-need-refresh spinner advads-ad-parameters-spinner advads-spinner"></span></p>'; |
| 142 | } |
| 143 | ob_start(); |
| 144 | $report_filter = $this->filter; |
| 145 | $report_domains = $this->data_object->get_domains(); |
| 146 | $sums = $this->data_object->get_sums( $this->filter ); |
| 147 | $earning_cells = ''; |
| 148 | foreach ( $sums as $index => $sum ) { |
| 149 | $earning_cells .= $this->get_earning_cell( $sum, $index ); |
| 150 | } |
| 151 | |
| 152 | require_once GADSENSE_BASE_PATH . '/admin/views/adsense-report.php'; |
| 153 | return ob_get_clean(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Build and return the HTML markup for a given period. |
| 158 | * |
| 159 | * @param float $sum the earning for that period. |
| 160 | * @param string $index the period identifier. |
| 161 | * |
| 162 | * @return string HTML of the individual cell. |
| 163 | */ |
| 164 | private function get_earning_cell( $sum, $index ) { |
| 165 | $period_strings = [ |
| 166 | 'today' => esc_html__( 'Today', 'advanced-ads' ), |
| 167 | 'yesterday' => esc_html__( 'Yesterday', 'advanced-ads' ), |
| 168 | /* translators: 1: The number of days. */ |
| 169 | '7days' => sprintf( esc_html__( 'Last %1$d days', 'advanced-ads' ), 7 ), |
| 170 | 'this_month' => esc_html__( 'This Month', 'advanced-ads' ), |
| 171 | /* translators: 1: The number of days. */ |
| 172 | '28days' => sprintf( esc_html__( 'Last %1$d days', 'advanced-ads' ), 28 ), |
| 173 | ]; |
| 174 | |
| 175 | $markup = '<div class="advads-flex1 advads-stats-box"><div>' . $period_strings[ $index ] . '</div>'; |
| 176 | $markup .= '<div class="advads-stats-box-main">'; |
| 177 | $markup .= number_format_i18n( ceil( 100 * $sum ) / 100, 2 ); |
| 178 | $markup .= ' ' . $this->get_data()->get_currency(); |
| 179 | $markup .= '</div></div>'; |
| 180 | |
| 181 | return $markup; |
| 182 | } |
| 183 | } |
| 184 |