= $options['tapp_expires']) { $options = tapp_refresh_token(); } $qs = '?access_token='.urlencode($options['tapp_token']); foreach ($params as $k => $v) { $qs .= '&'.$k.'='.($urlEncode ? urlencode($v) : $v); } $request = new WP_Http; $result = $request->request($url.$qs); $json = new stdClass(); $options['tapp_error'] = null; if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) { $json = json_decode($result['body']); update_option('tapp', $options); return $json; } else { if ( is_array( $result ) && isset( $result['response']['code'] ) && 403 === $result['response']['code'] ) { $json = json_decode($result['body'], true); $options['tapp_error'] = $json['error']['errors'][0]['message']; $options['tapp_token'] = null; $options['tapp_token_refresh'] = null; $options['tapp_expires'] = null; $options['tapp_gid'] = null; update_option('tapp', $options); } return new stdClass(); } } function tapp_refresh_token() { $options = tapp_options(); /* If the token has expired, we create it again */ if (!empty($options['tapp_token_refresh'])) { $request = new WP_Http; $result = $request->request('https://accounts.google.com/o/oauth2/token', array( 'method' => 'POST', 'body' => array( 'client_id' => $options['tapp_clientid'], 'client_secret' => $options['tapp_psecret'], 'refresh_token' => $options['tapp_token_refresh'], 'grant_type' => 'refresh_token', ), )); $options['tapp_error'] = null; if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) { $tjson = json_decode($result['body']); $request = new WP_Http; $result = $request->request('https://www.googleapis.com/oauth2/v1/userinfo?access_token='.urlencode($tjson->access_token)); if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) { $ijson = json_decode($result['body']); $options['tapp_token'] = $tjson->access_token; if (isset($tjson->refresh_token) && !empty($tjson->refresh_token)) { $options['tapp_token_refresh'] = $tjson->refresh_token; } $options['tapp_expires'] = time() + $tjson->expires_in; $options['tapp_gid'] = $ijson->id; update_option('tapp', $options); } elseif ( is_array( $result ) && isset( $result['response']['code'] ) && 403 === $result['response']['code'] ) { $json = json_decode($result['body'], true); $options['tapp_error'] = $json['error']['errors'][0]['message']; $options['tapp_token'] = null; $options['tapp_token_refresh'] = null; $options['tapp_expires'] = null; $options['tapp_gid'] = null; update_option('tapp', $options); } } } return $options; } function tapp_options() { $options = get_option('tapp'); if (!isset($options['tapp_clientid'])) { if (isset($options['tapp_pnumber'])) { if (isset($options['tapp_clientid'])){ $options['tapp_clientid'] = $options['tapp_pnumber'] . '.apps.googleusercontent.com'; } } else { if (isset($options['tapp_clientid'])) { $options = ['tapp_clientid' => null]; } } } if (isset($options['tapp_pnumber'])) unset($options['tapp_pnumber']); if (!isset($options['tapp_psecret'])) $options = ['tapp_psecret' => null]; if (!isset($options['tapp_gid'])) $options =['tapp_gid' => null] ; if (!isset($options['tapp_gmail'])) $options = ['tapp_gmail' => null]; if (!isset($options['tapp_token'])) $options = ['tapp_token' => null]; if (!isset($options['tapp_defaultval'])) $options['tapp_defaultval'] = 0; if (!isset($options['tapp_totalpost'])) $options['tapp_totalpost'] = 1; if (!isset($options['tapp_totalpost_grid'])) $options['tapp_totalpost_grid'] = 1; if (!isset($options['tapp_btn_color'])) $options['tapp_btn_color'] = null; if (!isset($options['tapp_sliderwidth'])) $options['tapp_sliderwidth'] = '100%'; if (!isset($options['tapp_sliderheight'])) $options['tapp_sliderheight'] = '600px'; if (!isset($options['tapp_analytics_code'])) $options['tapp_analytics_code'] = ''; if (!isset($options['tapp_token_refresh'])) $options['tapp_token_refresh'] = null; if (!isset($options['tapp_expires'])) $options['tapp_expires'] = null; if (!isset($options['tapp_wid'])) $options['tapp_wid'] = null; if (!isset($options['tapp_column'])) $options['tapp_column'] = true; if (!isset($options['tapp_onoffswitch'])) $options['tapp_onoffswitch'] = true; if (!isset($options['tapp_onoffswitch_grid'])) $options['tapp_onoffswitch_grid'] = true; if (!isset($options['tapp_trailing'])) $options['tapp_trailing'] = true; if (!isset($options['tapp_cache'])) $options['tapp_cache'] = 60; if (!isset($options['tapp_metric'])) $options['tapp_metric'] = 'ga:pageviews'; if(isset($options['tapp_startdate'])) { if (!preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $options['tapp_startdate'])) $options['tapp_startdate'] = '2007-09-29'; } return $options; } function tapp_conf() { /** @var $wpdb WPDB */ global $wpdb; $options = tapp_options(); $updated = false; if (isset($_GET['state']) && $_GET['state'] == 'init' && $_GET['code']) { $request = new WP_Http; $result = $request->request('https://accounts.google.com/o/oauth2/token', array( 'method' => 'POST', 'body' => array( 'code' => sanitize_text_field($_GET['code']), 'client_id' => $options['tapp_clientid'], 'client_secret' => $options['tapp_psecret'], 'redirect_uri' => admin_url('options-general.php?page=' . TAPP_SLUG), 'grant_type' => 'authorization_code', ) )); if ( !is_array( $result ) || !isset( $result['response']['code'] ) && 200 !== $result['response']['code'] ) { echo '
'; _e('There was something wrong with Authentication.', 'addweb-traffic-post-page-views'); echo "
'; _e('Configuration updated.', 'addweb-traffic-post-page-views'); echo '
'.__('In order to connect to your G-Analytics Account, you need to create a new project in the G-API Console and activate the Analytics API in "APIs & auth > APIs".', 'addweb-traffic-post-page-views').'
'; echo '