Exemplo n.º 1
0
 /**
  * Getter
  *
  * @param string $name
  * @return mixed
  */
 public function __get($name)
 {
     switch ($name) {
         case 'client':
             if (is_null($this->_client) && ($this->consumer_key && $this->consumer_secret)) {
                 try {
                     $this->_client = new \Google_Client();
                     $this->_client->setClientId($this->consumer_key);
                     $this->_client->setClientSecret($this->consumer_secret);
                     $this->_client->setRedirectUri(admin_url('options-general.php?page=gapiwp-analytics'));
                     $this->_client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
                     $this->_client->setAccessType('offline');
                 } catch (\Exception $e) {
                     // Do nothing
                 }
             }
             return $this->_client;
             break;
         case 'ga':
             try {
                 if ($this->token && $this->client && is_null($this->_ga)) {
                     $this->client->setAccessToken($this->token);
                     if ($this->client->isAccessTokenExpired()) {
                         // Refresh token if expired
                         $token = json_decode($this->token);
                         $this->client->refreshToken($token->refresh_token);
                         update_option('gapiwp_token', $this->client->getAccessToken());
                     }
                     $this->_ga = new \Google_Service_Analytics($this->client);
                 }
             } catch (\Exception $e) {
                 // Do nothing
             }
             return $this->_ga;
             break;
         case 'view_id':
             return get_option('gapiwp_view_id', 0);
             break;
         case 'consumer_key':
             return get_option('gapiwp_key', '');
             break;
         case 'consumer_secret':
             return get_option('gapiwp_secret', '');
             break;
         case 'token':
             return get_option('gapiwp_token', '');
             break;
         default:
             return parent::__get($name);
             break;
     }
 }