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