class-ad-type-adsense.php
8 years ago
class-gadsense-data.php
8 years ago
class-mapi.php
8 years ago
class-mapi.php
723 lines
| 1 | <?php |
| 2 | class Advanced_Ads_AdSense_MAPI |
| 3 | { |
| 4 | const OPTNAME = 'advanced-ads-adsense-mapi'; |
| 5 | |
| 6 | const CID = '400595147946-alk0j13qk563bg94rd4f3ip2t0b2tr5r.apps.googleusercontent.com'; |
| 7 | |
| 8 | const CS = '5jecyWgvCszB8UxSM0oS1W22'; |
| 9 | |
| 10 | const CALL_PER_24H = 20; |
| 11 | |
| 12 | private static $instance = null; |
| 13 | |
| 14 | private static $default_options = array(); |
| 15 | |
| 16 | private static $empty_account_data = array( |
| 17 | 'default_app' => array( |
| 18 | 'access_token' => '', |
| 19 | 'refresh_token' => '', |
| 20 | 'expires' => 0, |
| 21 | 'token_type' => '', |
| 22 | ), |
| 23 | 'user_app' => array( |
| 24 | 'access_token' => '', |
| 25 | 'refresh_token' => '', |
| 26 | 'expires' => 0, |
| 27 | 'token_type' => '', |
| 28 | ), |
| 29 | 'ad_units' => array(), |
| 30 | ); |
| 31 | |
| 32 | private function __construct() { |
| 33 | |
| 34 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); |
| 35 | |
| 36 | add_action( 'wp_ajax_advads_gadsense_mapi_confirm_code', array( $this, 'ajax_confirm_code' ) ); |
| 37 | add_action( 'wp_ajax_advads_gadsense_mapi_get_adUnits', array( $this, 'ajax_get_adUnits' ) ); |
| 38 | add_action( 'wp_ajax_advads_mapi_get_adCode', array( $this, 'ajax_get_adCode' ) ); |
| 39 | add_action( 'wp_ajax_advads-mapi-reconstructed-code', array( $this, 'ajax_save_reconstructed_code' ) ); |
| 40 | add_action( 'wp_ajax_advads-mapi-revoke-token', array( $this, 'ajax_revoke_tokken' ) ); |
| 41 | |
| 42 | self::$default_options = array( |
| 43 | 'accounts' => array(), |
| 44 | 'ad_codes' => array(), |
| 45 | 'quota' => array( |
| 46 | 'count' => self::CALL_PER_24H, |
| 47 | 'ts' => 0, |
| 48 | ), |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get available quota and eventual message about remaining call |
| 54 | */ |
| 55 | public function get_quota() { |
| 56 | $options = $this->get_option(); |
| 57 | $now = time(); |
| 58 | if ( self::use_user_app() ) { |
| 59 | return array( 'count' => PHP_INT_MAX ); |
| 60 | } else { |
| 61 | if ( $now > $options['quota']['ts'] + ( 24 * 3600 ) ) { |
| 62 | return array( |
| 63 | 'count' => self::CALL_PER_24H, |
| 64 | ); |
| 65 | } else { |
| 66 | $msg = $this->get_quota_msg(); |
| 67 | return array( |
| 68 | 'count' => $options['quota']['count'], |
| 69 | 'msg' => $msg, |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the readable quota |
| 77 | */ |
| 78 | public function get_quota_msg() { |
| 79 | |
| 80 | $options = $this->get_option(); |
| 81 | $now = time(); |
| 82 | $secs = $options['quota']['ts'] + ( 24 * 3600 ) - $now; |
| 83 | $hours = floor( $secs / 3600 ); |
| 84 | $mins = ceil( ( $secs - ( $hours * 3600 ) ) / 60 ); |
| 85 | |
| 86 | if ( 60 == $mins ) { |
| 87 | $hours += 1; |
| 88 | $mins = 0; |
| 89 | } |
| 90 | |
| 91 | if ( 0 == $options['quota']['count'] ) { |
| 92 | |
| 93 | $msg = sprintf( |
| 94 | /* commented out so that these unused strings don’t show up for translators; using fixed strings instead in case we forget this when we might add the check again later |
| 95 | _x( 'No API call left before %1$s %2$s.', 'No call left for the next X hours Y minutes.', 'advanced-ads' ), |
| 96 | sprintf( _n( '%s hour', '%s hours', $hours, 'advanced-ads' ), $hours ), |
| 97 | sprintf( _n( '%s minute', '%s minutes', $mins, 'advanced-ads' ), $mins ) |
| 98 | */ |
| 99 | 'No API call left before %1$s %2$s.', |
| 100 | sprintf( '%s hours', $hours ), |
| 101 | sprintf( '%s minutes', $mins ) |
| 102 | ); |
| 103 | |
| 104 | if ( 0 == $hours ) { |
| 105 | /* commented out so that these unused strings don’t show up for translators; using fixed strings instead in case we forget this when we might add the check again later |
| 106 | $msg = sprintf( |
| 107 | _x( 'No API call left before %s.', 'No call left for the next X time.', 'advanced-ads' ), |
| 108 | sprintf( _n( '%s minute', '%s minutes', $mins, 'advanced-ads' ), $mins ) |
| 109 | ); |
| 110 | */ |
| 111 | $msg = 'No API call left before.'; |
| 112 | } |
| 113 | |
| 114 | if ( 0 == $mins ) { |
| 115 | /* commented out so that these unused strings don’t show up for translators; using fixed strings instead in case we forget this when we might add the check again later |
| 116 | $msg = sprintf( |
| 117 | _x( 'No API call left before %s.', 'No call left for the next X time.', 'advanced-ads' ), |
| 118 | sprintf( _n( '%s hour', '%s hours', $hours, 'advanced-ads' ), $hours ) |
| 119 | ); |
| 120 | */ |
| 121 | $msg = 'No API call left.'; |
| 122 | } |
| 123 | |
| 124 | } else { |
| 125 | |
| 126 | $msg = sprintf( |
| 127 | /* commented out so that these unused strings don’t show up for translators; using fixed strings instead in case we forget this when we might add the check again later |
| 128 | _x( '%1$s for the next %2$s %3$s.', 'Calls remaining for the next X hours Y minutes.', 'advanced-ads' ), |
| 129 | sprintf( _n( '%s API call remaining.', '%s API calls remaining.', $options['quota']['count'], 'advanced-ads' ), $options['quota']['count'] ), |
| 130 | sprintf( _n( '%s hour', '%s hours', $hours, 'advanced-ads' ), $hours ), |
| 131 | sprintf( _n( '%s minute', '%s minutes', $mins, 'advanced-ads' ), $mins ) |
| 132 | */ |
| 133 | '%1$s for the next %2$s %3$s', |
| 134 | sprintf( '%s API calls remaining', $options['quota']['count'] ), |
| 135 | sprintf( '%s hours', $hours ), |
| 136 | sprintf( '%s minutes', $mins ) |
| 137 | ); |
| 138 | |
| 139 | if ( 0 == $hours ) { |
| 140 | /* commented out so that these unused strings don’t show up for translators; using fixed strings instead in case we forget this when we might add the check again later |
| 141 | $msg = sprintf( |
| 142 | _x( '%1$s for the next %2$s', 'Calls remaining for the next X time.', 'advanced-ads' ), |
| 143 | sprintf( _n( '%s API call remaining.', '%s API calls remaining.', $options['quota']['count'], 'advanced-ads' ), $options['quota']['count'] ), |
| 144 | sprintf( _n( '%s minute', '%s minutes', $mins, 'advanced-ads' ), $mins ) |
| 145 | ); |
| 146 | */ |
| 147 | $msg = sprintf( '%s API calls remaining.', $options['quota']['count'] ); |
| 148 | } |
| 149 | |
| 150 | if ( 0 == $mins ) { |
| 151 | /* commented out so that these unused strings don’t show up for translators; using fixed strings instead in case we forget this when we might add the check again later |
| 152 | $msg = sprintf( |
| 153 | _x( '%1$s for the next %2$s', 'calls remaining for the next X time', 'advanced-ads' ), |
| 154 | sprintf( _n( '%s API call remaining', '%s API calls remaining', $options['quota']['count'], 'advanced-ads' ), $options['quota']['count'] ), |
| 155 | sprintf( _n( '%s hour', '%s hours', $hours, 'advanced-ads' ), $hours ) |
| 156 | ); |
| 157 | */ |
| 158 | $msg = sprintf( |
| 159 | '%1$s for the next %2$s', |
| 160 | sprintf( '%s API calls remaining', $options['quota']['count'] ), |
| 161 | sprintf( '%s hours', $hours ) |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | return $msg; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Decrement quota by 1, and return message about remaining call |
| 171 | */ |
| 172 | public function decrement_quota() { |
| 173 | $options = $this->get_option(); |
| 174 | if ( 0 < $options['quota']['count'] ) { |
| 175 | $options['quota']['count']--; |
| 176 | $now = time(); |
| 177 | if ( $now > $options['quota']['ts'] + ( 24 * 3600 ) ) { |
| 178 | $options['quota']['ts'] = $now; |
| 179 | } |
| 180 | update_option( self::OPTNAME, $options ); |
| 181 | return $this->get_quota_msg(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Return the ad code for a given client and unit |
| 187 | * |
| 188 | * @return [str]|[arr] the ad code or info on the error. |
| 189 | */ |
| 190 | public function get_ad_code( $adUnit ) { |
| 191 | $gadsense_data = Advanced_Ads_AdSense_Data::get_instance(); |
| 192 | $adsense_id = $gadsense_data->get_adsense_id(); |
| 193 | $options = self::get_option(); |
| 194 | |
| 195 | $gadsense_data = Advanced_Ads_AdSense_Data::get_instance(); |
| 196 | $adsense_id = $gadsense_data->get_adsense_id(); |
| 197 | |
| 198 | $url = 'https://www.googleapis.com/adsense/v1.4/accounts/' . $adsense_id . '/adclients/ca-' . $adsense_id . '/adunits/' . $adUnit . '/adcode'; |
| 199 | $access_token = $this->get_access_token( $adsense_id ); |
| 200 | |
| 201 | if ( !isset( $access_token['msg'] ) ) { |
| 202 | $headers = array( |
| 203 | 'Authorization' => 'Bearer ' . $access_token, |
| 204 | ); |
| 205 | $response = wp_remote_get( $url, array( 'headers' => $headers ) ); |
| 206 | if ( is_wp_error( $response ) ) { |
| 207 | return array( 'status' => false, 'msg' => 'error while retrieving adUnits list', 'raw' => $response->get_error_message() ); |
| 208 | } else { |
| 209 | $adCode = json_decode( $response['body'], true ); |
| 210 | if ( null === $adCode || !isset( $adCode['adCode'] ) ) { |
| 211 | if ( |
| 212 | $adCode['error'] && |
| 213 | $adCode['error']['errors'] && |
| 214 | isset( $adCode['error']['errors'][0] ) && |
| 215 | isset( $adCode['error']['errors'][0]['reason'] ) && |
| 216 | 'doesNotSupportAdUnitType' == $adCode['error']['errors'][0]['reason'] |
| 217 | ) { |
| 218 | return array( 'status' => false, 'msg' => 'doesNotSupportAdUnitType' ); |
| 219 | } else { |
| 220 | return array( 'status' => false, 'msg' => 'invalid response while retrieving adCode for ' . $adUnit, 'raw' => $response['body'] ); |
| 221 | } |
| 222 | } else { |
| 223 | $options['ad_codes'][$adUnit] = $adCode['adCode']; |
| 224 | update_option( self::OPTNAME, $options ); |
| 225 | return $adCode['adCode']; |
| 226 | } |
| 227 | } |
| 228 | } else { |
| 229 | // return the original error info |
| 230 | return $access_token; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get/Update ad unit list for a given client |
| 236 | * |
| 237 | * @return [bool]|[array] TRUE on success, error info (as array) if an error occurred. |
| 238 | */ |
| 239 | public function get_ad_units( $account ) { |
| 240 | $gadsense_data = Advanced_Ads_AdSense_Data::get_instance(); |
| 241 | $url = 'https://www.googleapis.com/adsense/v1.4/accounts/' . $account . '/adclients/ca-' . $account . '/adunits?includeInactive=true'; |
| 242 | $access_token = $this->get_access_token( $account ); |
| 243 | |
| 244 | $options = self::get_option(); |
| 245 | |
| 246 | if ( !isset( $access_token['msg'] ) ) { |
| 247 | $headers = array( |
| 248 | 'Authorization' => 'Bearer ' . $access_token, |
| 249 | ); |
| 250 | $response = wp_remote_get( $url, array( 'headers' => $headers ) ); |
| 251 | if ( is_wp_error( $response ) ) { |
| 252 | return array( 'status' => false, 'msg' => 'error while retrieving adUnits list for "' . $account . '"', 'raw' => $response->get_error_message() ); |
| 253 | } else { |
| 254 | $adUnits = json_decode( $response['body'], true ); |
| 255 | if ( null === $adUnits || !isset( $adUnits['items'] ) ) { |
| 256 | return array( 'status' => false, 'msg' => 'invalid response while retrieving adUnits list for "' . $account . '"', 'raw' => $response['body'] ); |
| 257 | } else { |
| 258 | $new_adUnits = array(); |
| 259 | foreach( $adUnits['items'] as $item ) { |
| 260 | $new_adUnits[$item['id']] = $item; |
| 261 | } |
| 262 | $options['accounts'][$account]['ad_units'] = $new_adUnits; |
| 263 | update_option( self::OPTNAME, $options ); |
| 264 | return true; |
| 265 | } |
| 266 | } |
| 267 | } else { |
| 268 | // return the original error info |
| 269 | return $access_token; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Get the appropriate access token (default one or from user's Google app). Update it if needed. |
| 275 | * |
| 276 | * @return [str]|[array] the token on success, error info (as array) if an error occurred. |
| 277 | */ |
| 278 | public function get_access_token( $account ) { |
| 279 | $options = self::get_option(); |
| 280 | if ( self::use_user_app() ) { |
| 281 | if ( time() > $options['accounts'][$account]['user_app']['expires'] ) { |
| 282 | $new_tokens = $this->renew_access_token( $account ); |
| 283 | if ( $new_tokens['status'] ) { |
| 284 | return $new_tokens['access_token']; |
| 285 | } else { |
| 286 | // return all error info [arr] |
| 287 | return $new_tokens; |
| 288 | } |
| 289 | } else { |
| 290 | return $options['accounts'][$account]['user_app']['access_token']; |
| 291 | } |
| 292 | } else { |
| 293 | if ( time() > $options['accounts'][$account]['default_app']['expires'] ) { |
| 294 | $new_tokens = $this->renew_access_token( $account ); |
| 295 | if ( $new_tokens['status'] ) { |
| 296 | return $new_tokens['access_token']; |
| 297 | } else { |
| 298 | // return all error info [arr] |
| 299 | return $new_tokens; |
| 300 | } |
| 301 | } else { |
| 302 | return $options['accounts'][$account]['default_app']['access_token']; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Renew the current access token. |
| 309 | */ |
| 310 | public function renew_access_token( $account ) { |
| 311 | $cid = self::CID; |
| 312 | $cs = self::CS; |
| 313 | $options = self::get_option(); |
| 314 | $access_token = $options['accounts'][$account]['default_app']['access_token']; |
| 315 | $refresh_token = $options['accounts'][$account]['default_app']['refresh_token']; |
| 316 | |
| 317 | if ( self::use_user_app() ) { |
| 318 | $gadsense_data = Advanced_Ads_AdSense_Data::get_instance(); |
| 319 | $_options = $gadsense_data->get_options(); |
| 320 | $cid = ADVANCED_ADS_MAPI_CID; |
| 321 | $cs = ADVANCED_ADS_MAPI_CIS; |
| 322 | $access_token = $options['accounts'][$account]['user_app']['access_token']; |
| 323 | $refresh_token = $options['accounts'][$account]['user_app']['refresh_token']; |
| 324 | } |
| 325 | |
| 326 | $url = 'https://www.googleapis.com/oauth2/v4/token'; |
| 327 | $args = array( |
| 328 | 'body' => array( |
| 329 | 'refresh_token' => $refresh_token, |
| 330 | 'client_id' => $cid, |
| 331 | 'client_secret' => $cs, |
| 332 | 'grant_type' => 'refresh_token', |
| 333 | ), |
| 334 | ); |
| 335 | |
| 336 | $response = wp_remote_post( $url, $args ); |
| 337 | if ( is_wp_error( $response ) ) { |
| 338 | return array( 'status' => false, 'msg' => 'error while renewing access token for "' . $account . '"', 'raw' => $response->get_error_message() ); |
| 339 | } else { |
| 340 | $tokens = json_decode( $response['body'], true ); |
| 341 | if ( null !== $tokens ) { |
| 342 | $expires = time() + absint( $tokens['expires_in'] ); |
| 343 | if ( self::use_user_app() ) { |
| 344 | $options['accounts'][$account]['user_app']['access_token'] = $tokens['access_token']; |
| 345 | $options['accounts'][$account]['user_app']['expires'] = $expires; |
| 346 | } else { |
| 347 | $options['accounts'][$account]['default_app']['access_token'] = $tokens['access_token']; |
| 348 | $options['accounts'][$account]['default_app']['expires'] = $expires; |
| 349 | } |
| 350 | update_option( self::OPTNAME, $options ); |
| 351 | return array( 'status' => true, 'access_token' => $tokens['access_token'] ); |
| 352 | } else { |
| 353 | return array( 'status' => false, 'msg' => 'invalid response received while renewing access token for "' . $account . '"', 'raw' => $response['body'] ); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Recoke a refresh token |
| 360 | */ |
| 361 | public function ajax_revoke_tokken() { |
| 362 | |
| 363 | $nonce = isset( $_POST['nonce'] )? $_POST['nonce'] : ''; |
| 364 | if ( false !== wp_verify_nonce( $nonce, 'advads-mapi' ) ) { |
| 365 | $adsense_id = stripslashes( $_POST['adsenseId'] ); |
| 366 | $options = self::get_option(); |
| 367 | if ( self::use_user_app() ) { |
| 368 | $token = $options['accounts'][$adsense_id]['user_app']['refresh_token']; |
| 369 | } else { |
| 370 | $token = $options['accounts'][$adsense_id]['default_app']['refresh_token']; |
| 371 | } |
| 372 | $url = 'https://accounts.google.com/o/oauth2/revoke?token=' . $token; |
| 373 | $args = array( |
| 374 | 'timeout' => 5, |
| 375 | 'header' => array( 'Content-type' => 'application/x-www-form-urlencoded' ), |
| 376 | ); |
| 377 | |
| 378 | $response = wp_remote_post( $url, $args ); |
| 379 | if ( is_wp_error( $response ) ) { |
| 380 | echo json_encode( array( 'status' => false ) ); |
| 381 | } else { |
| 382 | header( 'Content-Type: application/json' ); |
| 383 | unset( $options['accounts'][$adsense_id] ); |
| 384 | update_option( self::OPTNAME, $options ); |
| 385 | echo json_encode( array( 'status' => true ) ); |
| 386 | } |
| 387 | } |
| 388 | die; |
| 389 | |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Save ad code reconstructed from ad parameters |
| 394 | */ |
| 395 | public function ajax_save_reconstructed_code() { |
| 396 | $nonce = isset( $_POST['nonce'] )? $_POST['nonce'] : ''; |
| 397 | if ( false !== wp_verify_nonce( $nonce, 'advads-mapi' ) ) { |
| 398 | $code = stripslashes( $_POST['code'] ); |
| 399 | $slot = stripslashes( $_POST['slot'] ); |
| 400 | $gadsense_data = Advanced_Ads_AdSense_Data::get_instance(); |
| 401 | $adsense_id = $gadsense_data->get_adsense_id(); |
| 402 | $options = self::get_option(); |
| 403 | $options['ad_codes']['ca-' . $adsense_id . ':' . $slot] = $code; |
| 404 | update_option( self::OPTNAME, $options ); |
| 405 | header( 'Content-Type: application/json' ); |
| 406 | echo json_encode( array( 'status' => true ) ); |
| 407 | } |
| 408 | die; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Get ad code for a given unit |
| 413 | */ |
| 414 | public function ajax_get_adCode() { |
| 415 | $nonce = isset( $_POST['nonce'] )? $_POST['nonce'] : ''; |
| 416 | if ( false !== wp_verify_nonce( $nonce, 'advads-mapi' ) ) { |
| 417 | $unit = stripslashes( $_POST['unit'] ); |
| 418 | |
| 419 | if ( !self::use_user_app() ) { |
| 420 | $quota = $this->get_quota(); |
| 421 | |
| 422 | // No more quota left |
| 423 | if ( $quota['count'] < 1 ) { |
| 424 | $quota_msg = $this->get_quota_msg(); |
| 425 | header( 'Content-Type: application/json' ); |
| 426 | $quota_msg = $this->get_quota_msg(); |
| 427 | echo wp_json_encode( |
| 428 | array( |
| 429 | 'quota' => 0, |
| 430 | 'quotaMsg' => $quota_msg, |
| 431 | ) |
| 432 | ); |
| 433 | die; |
| 434 | } |
| 435 | |
| 436 | } |
| 437 | |
| 438 | $code = $this->get_ad_code( $unit ); |
| 439 | |
| 440 | /** |
| 441 | * Ad code is returned as string. Otherwise it's an error |
| 442 | */ |
| 443 | if ( is_string( $code ) ) { |
| 444 | |
| 445 | $response = array( 'code' => $code ); |
| 446 | |
| 447 | /** |
| 448 | * Add quota info for default API creds |
| 449 | */ |
| 450 | if ( !self::use_user_app() ) { |
| 451 | $quota = $this->get_quota(); |
| 452 | $quota_msg = $this->get_quota_msg(); |
| 453 | $response['quota'] = $quota['count']; |
| 454 | $response['quotaMsg'] = $quota_msg; |
| 455 | } |
| 456 | |
| 457 | header( 'Content-Type: application/json' ); |
| 458 | echo wp_json_encode( $response ); |
| 459 | |
| 460 | } else { |
| 461 | |
| 462 | // return info about the error |
| 463 | header( 'Content-Type: application/json' ); |
| 464 | echo wp_json_encode( $code ); |
| 465 | |
| 466 | } |
| 467 | |
| 468 | } |
| 469 | die; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Get / Update the ad unit list for a given ad client. The corresponding <select /> input used in the ad selector is passed as a fied of an array |
| 474 | */ |
| 475 | public function ajax_get_adUnits() { |
| 476 | $nonce = isset( $_POST['nonce'] )? $_POST['nonce'] : ''; |
| 477 | if ( false !== wp_verify_nonce( $nonce, 'advads-mapi' ) ) { |
| 478 | $account = stripslashes( $_POST['account'] ); |
| 479 | $units = $this->get_ad_units( $account ); |
| 480 | |
| 481 | if ( true == $units ) { |
| 482 | $options = self::get_option(); |
| 483 | $ad_units = $options['accounts'][$account]['ad_units']; |
| 484 | ob_start(); |
| 485 | |
| 486 | echo '<select id="mapi-adunit-select">'; |
| 487 | if ( empty( $ad_units ) ) { |
| 488 | echo '<option value="">' . __( 'No ad unit found', 'advanced-ads' ) . '</option>'; |
| 489 | } else { |
| 490 | echo '<option value="">' . __( '--select ad--', 'advanced-ads' ) . '</option>'; |
| 491 | $sorted_adunits = self::get_sorted_adunits( $ad_units ); |
| 492 | foreach ( $sorted_adunits as $key => $value ) { |
| 493 | echo '<option value="' . esc_attr( $key ) . '">' . $value . '</option>'; |
| 494 | } |
| 495 | } |
| 496 | echo '</select>'; |
| 497 | |
| 498 | $ad_selector = ob_get_clean(); |
| 499 | |
| 500 | $response = array( |
| 501 | 'status' => true, |
| 502 | 'html' => $ad_selector, |
| 503 | ); |
| 504 | |
| 505 | /** |
| 506 | * Add quota info for default API creds |
| 507 | */ |
| 508 | if ( !self::use_user_app() ) { |
| 509 | $quota = $this->get_quota(); |
| 510 | $quota_msg = $this->get_quota_msg(); |
| 511 | $response['quota'] = $quota['count']; |
| 512 | $response['quotaMsg'] = $quota_msg; |
| 513 | } |
| 514 | |
| 515 | } else { |
| 516 | /** |
| 517 | * return the error info [arr] |
| 518 | */ |
| 519 | $response = $units; |
| 520 | } |
| 521 | header( 'Content-Type: application/json' ); |
| 522 | echo wp_json_encode( $response ); |
| 523 | } |
| 524 | die; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Submit Google API confirmation code. Save the token and update ad client list. |
| 529 | */ |
| 530 | public function ajax_confirm_code() { |
| 531 | $nonce = isset( $_POST['nonce'] )? $_POST['nonce'] : ''; |
| 532 | if ( false !== wp_verify_nonce( $nonce, 'advads-mapi' ) ) { |
| 533 | $code = urldecode( $_POST['code'] ); |
| 534 | $cid = self::CID; |
| 535 | $cs = self::CS; |
| 536 | |
| 537 | $use_user_app = self::use_user_app(); |
| 538 | |
| 539 | $gadsense_data = Advanced_Ads_AdSense_Data::get_instance(); |
| 540 | |
| 541 | if ( $use_user_app ) { |
| 542 | $_options = $gadsense_data->get_options(); |
| 543 | $cid = ADVANCED_ADS_MAPI_CID; |
| 544 | $cs = ADVANCED_ADS_MAPI_CIS; |
| 545 | } |
| 546 | |
| 547 | $code_url = 'https://www.googleapis.com/oauth2/v4/token'; |
| 548 | $redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'; |
| 549 | $grant_type = 'authorization_code'; |
| 550 | |
| 551 | $args = array( |
| 552 | 'timeout' => 10, |
| 553 | 'body' => array( |
| 554 | 'code' => $code, |
| 555 | 'client_id' => $cid, |
| 556 | 'client_secret' => $cs, |
| 557 | 'redirect_uri' => $redirect_uri, |
| 558 | 'grant_type' => $grant_type, |
| 559 | ), |
| 560 | ); |
| 561 | |
| 562 | $response = wp_remote_post( $code_url, $args ); |
| 563 | |
| 564 | if ( is_wp_error( $response ) ) { |
| 565 | return json_encode( array( 'status' => false, 'msg' => 'error while submitting code', 'raw' => $response->get_error_message() ) ); |
| 566 | } else { |
| 567 | $token = json_decode( $response['body'], true ); |
| 568 | $adsense_id = $gadsense_data->get_adsense_id(); |
| 569 | |
| 570 | if ( null !== $token && isset( $token['refresh_token'] ) ) { |
| 571 | |
| 572 | if ( !empty( $adsense_id ) ) { |
| 573 | self::save_token_from_code( $token, $adsense_id ); |
| 574 | |
| 575 | $gadsense_options = $gadsense_data->get_options(); |
| 576 | $gadsense_options['page-level-enabled'] = isset( $_POST['autoads'] ); |
| 577 | update_option( GADSENSE_OPT_NAME, $gadsense_options ); |
| 578 | |
| 579 | header( 'Content-Type: application/json' ); |
| 580 | echo json_encode( array( 'status' => true ) ); |
| 581 | |
| 582 | } else { |
| 583 | /** |
| 584 | * get AdSense ID first |
| 585 | */ |
| 586 | $url = 'https://www.googleapis.com/adsense/v1.4/accounts'; |
| 587 | |
| 588 | $headers = array( 'Authorization' => 'Bearer ' . $token['access_token'] ); |
| 589 | $response = wp_remote_get( $url, array( 'headers' => $headers ) ); |
| 590 | |
| 591 | if ( is_wp_error( $response ) ) { |
| 592 | |
| 593 | header( 'Content-Type: application/json' ); |
| 594 | echo json_encode( array( 'status' => false, 'error_msg' => $response->get_error_message() ) ); |
| 595 | |
| 596 | } else { |
| 597 | |
| 598 | $accounts = json_decode( $response['body'], true ); |
| 599 | $adsense_id = $accounts['items'][0]['id']; |
| 600 | |
| 601 | $gadsense_options = $gadsense_data->get_options(); |
| 602 | $gadsense_options['adsense-id'] = $adsense_id; |
| 603 | |
| 604 | $gadsense_options['page-level-enabled'] = isset( $_POST['autoads'] ); |
| 605 | |
| 606 | update_option( GADSENSE_OPT_NAME, $gadsense_options ); |
| 607 | self::save_token_from_code( $token, $adsense_id ); |
| 608 | |
| 609 | header( 'Content-Type: application/json' ); |
| 610 | echo json_encode( array( 'status' => true ) ); |
| 611 | |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | |
| 616 | } else { |
| 617 | header( 'Content-Type: application/json' ); |
| 618 | echo json_encode( array( 'status' => false, 'response_body' => $response['body'] ) ); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | die; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Enqueue admin scripts |
| 627 | */ |
| 628 | public function admin_scripts( $hook ) { |
| 629 | if ( 'advanced-ads_page_advanced-ads-settings' == $hook ) { |
| 630 | wp_enqueue_script( 'gasense/mapi/settings', GADSENSE_BASE_URL . 'admin/assets/js/mapi-settings.js', array( 'jquery' ), ADVADS_VERSION ); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Sort ad units list alphabetically |
| 636 | */ |
| 637 | public static function get_sorted_adunits( $adunits ) { |
| 638 | $results = array(); |
| 639 | foreach ( $adunits as $unit ) { |
| 640 | $results[$unit['name']] = $unit['id']; |
| 641 | } |
| 642 | ksort( $results ); |
| 643 | $results = array_flip( $results ); |
| 644 | return $results; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Check if the credential are the default ones or from user's app |
| 649 | */ |
| 650 | public static function use_user_app() { |
| 651 | if ( ( defined( 'ADVANCED_ADS_MAPI_CID' ) && '' != ADVANCED_ADS_MAPI_CID ) && ( defined( 'ADVANCED_ADS_MAPI_CIS' ) && '' != ADVANCED_ADS_MAPI_CIS ) ) { |
| 652 | return true; |
| 653 | } else { |
| 654 | return false; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | public static function has_token( $adsense_id = '' ) { |
| 659 | if ( empty( $adsense_id ) ) { |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | $has_token = false; |
| 664 | $options = self::get_option(); |
| 665 | if ( self::use_user_app() ) { |
| 666 | if ( isset( $options['accounts'][$adsense_id] ) && !empty( $options['accounts'][$adsense_id]['user_app']['refresh_token'] ) ) { |
| 667 | $has_token = true; |
| 668 | } |
| 669 | } else { |
| 670 | if ( isset( $options['accounts'][$adsense_id] ) && !empty( $options['accounts'][$adsense_id]['default_app']['refresh_token'] ) ) { |
| 671 | $has_token = true; |
| 672 | } |
| 673 | } |
| 674 | return $has_token; |
| 675 | |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * save token obtained from confirmation code |
| 680 | */ |
| 681 | public static function save_token_from_code( $token, $adsense_id ) { |
| 682 | |
| 683 | $options = self::get_option(); |
| 684 | $expires = time() + absint( $token['expires_in'] ); |
| 685 | if ( !isset( $options['accounts'][$adsense_id] ) ) { |
| 686 | $options['accounts'][$adsense_id] = self::$empty_account_data; |
| 687 | } |
| 688 | if ( self::use_user_app() ) { |
| 689 | $options['accounts'][$adsense_id]['user_app'] = array( |
| 690 | 'access_token' => $token['access_token'], |
| 691 | 'refresh_token' => $token['refresh_token'], |
| 692 | 'expires' => $expires, |
| 693 | 'token_type' => $token['token_type'], |
| 694 | ); |
| 695 | } else { |
| 696 | $options['accounts'][$adsense_id]['default_app'] = array( |
| 697 | 'access_token' => $token['access_token'], |
| 698 | 'refresh_token' => $token['refresh_token'], |
| 699 | 'expires' => $expires, |
| 700 | 'token_type' => $token['token_type'], |
| 701 | ); |
| 702 | } |
| 703 | update_option( self::OPTNAME, $options ); |
| 704 | |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Get the class's option |
| 709 | */ |
| 710 | public static function get_option() { |
| 711 | $options = get_option( self::OPTNAME, array() ); |
| 712 | if ( !is_array( $options ) ) $options = array(); |
| 713 | return $options + self::$default_options; |
| 714 | } |
| 715 | |
| 716 | public static function get_instance() { |
| 717 | if (null == self::$instance) { |
| 718 | self::$instance = new self; |
| 719 | } |
| 720 | return self::$instance; |
| 721 | } |
| 722 | } |
| 723 |