AdSenseAuth

The Google Adsense Dashboard AdSenseAuth class.

Defined (1)

The class is defined in the following location(s).

/functions.php  
  1. class AdSenseAuth 
  2.  
  3. protected $client; 
  4.  
  5. protected $adSenseService; 
  6.  
  7. private $user, $authUrl; 
  8.  
  9. public function __construct() 
  10. require 'autoload.php'; 
  11.  
  12. $this->client = new Google_Client(); 
  13. $this->client->setAccessType('offline'); 
  14. $this->client->setApplicationName('Google Adsense Dashboard for WP'); 
  15. $this->client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); 
  16.  
  17. if (get_option('gads_dash_userapi')) { 
  18. $this->client->setClientId(get_option('gads_dash_clientid')); 
  19. $this->client->setClientSecret(get_option('gads_dash_clientsecret')); 
  20. $this->client->setDeveloperKey(get_option('gads_dash_apikey')); 
  21. } else { 
  22. $this->client->setClientId('265189663307.apps.googleusercontent.com'); 
  23. $this->client->setClientSecret('B-LxlsVehit2CCzF5ke-SK6T'); 
  24. $this->client->setDeveloperKey('AIzaSyDH3q3w33uLpH4GN25CZqoWE_Nkcpk2UmY'); 
  25.  
  26. $this->adSenseService = new Google_Service_AdSense($this->client); 
  27.  
  28. function gads_dash_store_token($user, $token) 
  29. update_option('gads_dash_user', $user); 
  30. update_option('gads_dash_token', $token); 
  31.  
  32. function gads_dash_get_token() 
  33. if (get_option('gads_dash_token')) { 
  34. return get_option('gads_dash_token'); 
  35. } else { 
  36. return; 
  37.  
  38. public function gads_dash_reset_token() 
  39. update_option('gads_dash_token', ""); 
  40.  
  41. function gads_dash_clear_cache() 
  42. global $wpdb; 
  43. $sqlquery = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_gadsdash%%'"); 
  44. $sqlquery = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_gadsdash%%'"); 
  45.  
  46. function authenticate($user) 
  47. $this->user = $user; 
  48. $token = $this->gads_dash_get_token(); 
  49.  
  50. if (isset($token)) { 
  51. $this->client->setAccessToken($token); 
  52. } else { 
  53. $this->client->setScopes(array( 
  54. "https://www.googleapis.com/auth/adsense.readonly" 
  55. )); 
  56. $this->authUrl = $this->client->createAuthUrl(); 
  57. if (! isset($_REQUEST['gads_dash_authorize'])) { 
  58. if (! current_user_can('manage_options')) { 
  59. _e("Ask an admin to authorize this Application", 'gads-dash'); 
  60. return; 
  61.  
  62. echo '<div style="padding:20px;">' . __("Use this link to get your access code:", 'gads-dash') . ' <a href="' . $this->authUrl . '" target="_blank">' . __("Get Access Code", 'gads-dash') . '</a>'; 
  63. echo '<form name="input" action="#" method="POST"> 
  64. <p><b>' . __("Access Code:", 'gads-dash') . ' </b><input type="text" name="gads_dash_code" value="" size="35"></p> 
  65. <input type="submit" class="button button-primary" name="gads_dash_authorize" value="' . __("Save Access Code", 'gads-dash') . '"/> 
  66. </form> 
  67. </div>'; 
  68. return; 
  69. } else  
  70. if (isset($_REQUEST['gads_dash_code'])) { 
  71. $this->client->authenticate($_REQUEST['gads_dash_code']); 
  72. $this->gads_dash_store_token($this->user, $this->client->getAccessToken()); 
  73. } else { 
  74. $adminurl = admin_url("#gads-dash-widget"); 
  75. echo '<script> window.location="' . $adminurl . '"; </script> '; 
  76.  
  77. function getAdSenseService() 
  78. return $this->adSenseService; 
  79.  
  80. function gads_dash_refreshToken() 
  81. if ($this->client->getAccessToken() != null) { 
  82. $this->gads_dash_store_token('default', $this->client->getAccessToken());