client = new Adsforwp_Google_Client(); $this->client->setApprovalPrompt( 'force' ); $this->client->setAccessType( 'offline' ); $this->client->setClientId( $this->ClientID ); $this->client->setClientSecret( $this->ClientSecret ); $this->client->setRedirectUri( $this->redirect ); $this->client->setScopes( $this->scope ); try { $this->service = new \Adsforwp_Google_Service_Analytics( $this->client ); if( get_option( 'adsforwp_google_token' ) ){ $this->pa_connect(); } // This function refresh token and use for debugging //$this->client->refreshToken( $this->token->refresh_token ); } catch ( \Adsforwp_Google_Service_Exception $e ) { // Show error message only for logged in users. if ( current_user_can( 'manage_options' ) ) { echo sprintf( esc_html__( '%1$s Oops, Something went wrong. %2$s %5$s %2$s %3$s Don\'t worry, This error message is only visible to Administrators. %4$s %2$s ', 'ads-for-wp' ), '

', '
', '', '', esc_textarea( $e->getMessage() ) ); } } catch ( \Adsforwp_Google_Auth_Exception $e ) { // Show error message only for logged in users. if ( current_user_can( 'manage_options' ) ) { echo sprintf( esc_html__( '%1$s Oops, Try to %2$s Reset %3$s Authentication. %4$s %7$s %4$s %5$s Don\'t worry, This error message is only visible to Administrators. %6$s %4$s', 'ads-for-wp' ), '

', '', '', '
', '', '', esc_textarea( $e->getMessage() ) ); } } $this->postCalls(); } function postCalls(){ if($_SERVER['REQUEST_METHOD']=='POST'){ if(isset($_POST['saveKey'])){ //Need to remove it $savedOpt = get_option('adsforwp_analytics'); $adsforwp_google_token = esc_html($_POST['adsforwp_google_token']); $savedOpt['google_token'] = $adsforwp_google_token; update_option( 'adsforwp_analytics', $savedOpt ); } if(isset($_POST['adsforwp_profile_entry'])){ $savedOpt = get_option('adsforwp_analytics'); $profile_for_dashboard = esc_html($_POST['profile_for_dashboard']); $savedOpt['profile_for_post'] = $profile_for_dashboard; $savedOpt['profile_for_dashboard'] = $profile_for_dashboard; update_option( 'adsforwp_analytics', $savedOpt ); wp_redirect(admin_url('edit.php?post_type=adsforwp&page=analytics')); } if(isset($_POST['wp_adsforwp_analytics_log_out'])){ delete_option( 'adsforwp_google_token' ); delete_option( 'adsforwp_analytics' ); } } } function authentication(){ ?>

siteSettings(); } else { ?>

fetch_profiles_list_summary(); $profilePostOpts = ''; $profileDashboardOpts = ''; $analyticsSettings = get_option('adsforwp_analytics'); $profile_for_post_value = $analyticsSettings['profile_for_dashboard']; $profile_for_dashboard_value = $analyticsSettings['profile_for_dashboard']; if(isset($_profile_otions) && $_profile_otions){ foreach ($_profile_otions->getItems() as $account) { foreach ( $account->getWebProperties() as $property ) { foreach ( $property->getProfiles() as $profile ) { $profilePostOpts .= sprintf( '', $profile->getId(), selected( $profile_for_post_value, $profile->getId(), false ), $profile->getName() , $property->getId() ); $profileDashboardOpts .= sprintf( '', $profile->getId(), selected( $profile_for_dashboard_value, $profile->getId(), false ), $profile->getName() , $property->getId() ); // Update the UA code in option on setting save for profile_for_posts. if ( $profile_for_post_value === $profile->getId()) { update_option( 'adsforwp_ua_code', $property->getId() ); } } } } } ?>
redirect; $client_id = $this->ClientID; $url = http_build_query( array( 'next' => admin_url( 'edit.php?post_type=adsforwp&page=adsforwp-analytics' ), 'scope' => $this->scope, 'response_type' => 'code', 'state' => get_admin_url() . 'edit.php?post_type=adsforwp&page=adsforwp-analytics', 'redirect_uri' => $redirect_url, 'client_id' => $client_id, 'access_type' => 'offline', 'approval_prompt' => 'force', ) ); return $url; } function fetch_profiles_list_summary() { $profiles = get_option( 'ampforwp_profiles_list_summary' ); if ( ! $profiles && get_option( 'adsforwp_google_token' ) ) { try { if ( get_option( 'adsforwp_profile_exception' ) ) { $this->adsforwp_handle_exceptions( get_option( 'adsforwp_profile_exception' ) ); } else if ( get_option( 'adsforwp_google_token' ) != '' ) { $profiles = $this->service->management_accountSummaries->listManagementAccountSummaries(); } else { echo '

' . esc_html__( 'Notice: You must authenticate to access your web profiles.', 'ads-for-wp' ) . '

'; } } catch (Exception $e) { // Show admin notice if some exception occurs. $this->adsforwp_handle_exceptions( $e->getErrors() ); update_option( 'adsforwp_profile_exception', $e->getErrors() ); } update_option( 'ampforwp_profiles_list_summary' , $profiles ); } return $profiles; } public function adsforwp_google_authentication(){ if ( isset( $_GET['code'] ) && 'adsforwp-analytics' === $_GET['page'] ) { $key_google_token = sanitize_text_field( wp_unslash( $_GET['code'] ) ); try { update_option( 'adsforwp_post_analytics_token', $key_google_token ); if ( $this->pa_connect() ) { wp_redirect( admin_url( 'edit.php?post_type=adsforwp&page=adsforwp-analytics' )); } } catch (Exception $e) { echo $e->getMessage(); } wp_redirect( admin_url( 'edit.php?post_type=adsforwp&page=adsforwp-analytics' )); exit; } } public function pa_connect() { $ga_google_authtoken = get_option( 'adsforwp_google_token' ); if ( ! empty( $ga_google_authtoken ) ) { $this->client->setAccessToken( $ga_google_authtoken ); } else { $auth_code = get_option( 'adsforwp_post_analytics_token' ); if ( empty( $auth_code ) ) { return false; } try { $access_token = $this->client->authenticate( $auth_code ); } catch ( Exception $e ) { echo 'Adsforwp (Bug): ' . esc_textarea( $e->getMessage() ); return false; } if ( $access_token ) { $this->client->setAccessToken( $access_token ); update_option( 'adsforwp_google_token', $access_token ); return true; } else { return false; } } $this->token = json_decode( $this->client->getAccessToken() ); return true; } function adsforwp_handle_exceptions( $_exception_errors ) { if ( $_exception_errors[0]['reason'] == 'dailyLimitExceeded' ) { add_action( 'admin_notices', array( $this,'daily_limit_exceed_error' ), 9 ); } else if ( $_exception_errors[0]['reason'] == 'insufficientPermissions' ) { add_action( 'admin_notices', array( $this,'insufficent_permissions_error' ), 9 ); } else if ( $_exception_errors[0]['reason'] == 'usageLimits.userRateLimitExceededUnreg' ) { add_action( 'admin_notices', array( $this,'user_rate_limit_unreg_error' ), 9 ); } else if ( $_exception_errors[0]['reason'] == 'userRateLimitExceeded' ) { add_action( 'admin_notices', array( $this,'user_rate_limit_error' ), 9 ); } else if ( $_exception_errors[0]['reason'] == 'rateLimitExceeded' ) { add_action( 'admin_notices', array( $this,'rate_limit_exceeded_error' ), 9 ); } else if ( $_exception_errors[0]['reason'] == 'quotaExceeded' ) { add_action( 'admin_notices', array( $this,'quota_exceeded_error' ), 9 ); } else if ( $_exception_errors[0]['reason'] == 'accessNotConfigured' ) { add_action( 'admin_notices', array( $this,'accessNotConfigured' ), 9 ); } } function show_notice_messages(){ $this->admin_notice_messages; } } add_action( 'plugins_loaded', 'adsforwp_instantiate_analytics_class' ); function adsforwp_instantiate_analytics_class(){ if(is_admin() && current_user_can( 'manage_options' ) ){ $GLOBALS['ADSFORWP'] = new Adsforwp_analyticsSettings(); } }