gglnltcs_settings_page

The BestWebSoft Google Analytics gglnltcs settings page function.

Description

gglnltcs_settings_page(); 

Usage

  1. if ( !function_exists( 'gglnltcs_settings_page' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bestwebsoft-google-analytics/bws-google-analytics.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = gglnltcs_settings_page(); 
  7.  

Defined (1)

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

/bws-google-analytics.php  
  1. function gglnltcs_settings_page() { 
  2. global $gglnltcs_options; 
  3. /** Settings Page Main Header */ 
  4. if ( ! isset( $_POST['code'] ) ) { ?> 
  5. <div class="icon32 icon32-bws" id="icon-options-general"></div> 
  6. <h2 id="gglnltcs-main-header"><?php _e( 'BestWebSoft Google Analytics Settings', 'gglnltcs' ); ?></h2><?php 
  7. require_once 'google-api-php-client/api-code/Google_Client.php'; 
  8. require_once 'google-api-php-client/api-code/contrib/Google_AnalyticsService.php'; 
  9. $client = new Google_Client(); 
  10. $client->setApplicationName( 'BestWebSoft Google Analytics' ); 
  11. $client->setClientId( '714548546682-ai821bsdfn2th170q8ofprgfmh5ch7cn.apps.googleusercontent.com' ); 
  12. $client->setClientSecret( 'pyBXulcOqPhQGzKiW4kehZZB' ); 
  13. $client->setRedirectUri( 'urn:ietf:wg:oauth:2.0:oob' ); 
  14. $client->setDeveloperKey( 'AIzaSyDA7L2CZgY4ud4vv6rw0Yu4GUDyfbRw0f0' ); 
  15. $client->setScopes( array( 'https://www.googleapis.com/auth/analytics.readonly' ) ); 
  16. $client->setUseObjects( true ); 
  17. /** If user pressed log out button delete his Access Token from database. */ 
  18. if ( isset( $_POST['gglnltcs_log_out'] ) && check_admin_referer( plugin_basename( __FILE__ ), 'gglnltcs_nonce_name' ) ) { 
  19. unset( $gglnltcs_options['token'] ); 
  20. unset( $gglnltcs_options['settings'] ); 
  21. update_option( 'gglnltcs_options', $gglnltcs_options ); 
  22. /** Set Access Token if it is stored in database */ 
  23. if ( isset( $gglnltcs_options['token'] ) ) { 
  24. try { 
  25. $client->setAccessToken( $gglnltcs_options['token'] ); 
  26. } catch ( Google_AuthException $e ) { 
  27. /** The Access Token stored in the database was damaged or invalid for some reason.  
  28. * So do nothing, this is normal. We will get a new one in the code below. */ 
  29. /** If setAccessToken() has been failed. We don't have an Authentication Token. 
  30. * The code below will be used to get it, so the user can access his website stats. */ 
  31. if ( ! $client->getAccessToken() ) { 
  32. /** This will be executed if user get on the page in the first time. */ 
  33. if ( ! isset( $_POST['code'] ) ) { 
  34. /** The post['code'] has not been passed yet, so let us offer the user to enter the Google Authentication Code. 
  35. * First we need to redirect user to the Google Authorization page. 
  36. * For this reason we create an URL to obtain user authorization. */ 
  37. $authUrl = $client->createAuthUrl(); ?> 
  38. <div class="gglnltcs-text-information"> 
  39. <p><?php _e( 'In order to use BestWebSoft Google Analytics plugin, you must be signed in with a registered Google Account email address and password. If you don\'t have Google Account you can create it', 'gglnltcs' ); ?> <a href="https://www.google.com/accounts/NewAccount" target="_blank"><?php _e( 'here', 'gglnltcs' ); ?>.</a></p>  
  40. <input id="gglnltcs-google-sign-in" type="button" class="button-primary" onclick="window.open('<?php echo $authUrl; ?>', 'activate', 'width=640, height=480, menubar=0, status=0, location=0, toolbar=0')" value="<?php _e( 'Sign In To Google Account', 'gglnltcs' ); ?>"> 
  41. <noscript> 
  42. <div class="button-primary gglnltcs-google-sign-in"> 
  43. <a href="<?php echo $authUrl; ?>" target="_blanket"><?php _e( 'Or Click Here If You Have Disabled Javascript', 'gglnltcs' ); ?></a> 
  44. </div> 
  45. </noscript> 
  46. <p class="gglnltcs-authentication-instructions"><?php _e( 'When you finish authorization process you will get Google Authentication Code. You must enter this code in the field below and press "Start Plugin" button. This code will be used to get an Authentication Token so you can access your website stats.', 'gglnltcs' ); ?></p> 
  47. <form id="gglnltcs-authentication-form" method="post" action="<?php echo $_SERVER["PHP_SELF"] . '?page=bws-google-analytics.php&noheader=true'; ?>"> 
  48. <?php wp_nonce_field( plugin_basename( __FILE__ ), 'gglnltcs_nonce_name' ); ?> 
  49. <p><input id="gglnltcs-authentication-code-input" type="text" name="code"><input type="submit" class="button-primary" value="<?php _e( 'Start Plugin', 'gglnltcs' ); ?>"></p> 
  50. </form> 
  51. </div><?php  
  52. /** This message will appear if user enter invalid Google Authentication Code. 
  53. * Invalid code will cause exception in the $client->authenticate method. */ 
  54. if ( isset( $_POST['invalid_code'] ) && check_admin_referer( plugin_basename( __FILE__ ), 'gglnltcs_nonce_name' ) ) { ?> 
  55. <p><span class="gglnltcs-unsuccess-message"><?php _e( 'Invalid code. Please, try again.', 'gglnltcs' ); ?></span></p><?php 
  56. /** Enter your Google Authentication Code in this box. */ 
  57. } else { 
  58. /** This will be executed after user has submitted the form. The post['code'] is set.*/ 
  59. try { 
  60. /** We got here from the redirect from a successful authorization grant,  
  61. * try to fetch the access token. */ 
  62. $client->authenticate( stripslashes( esc_html( $_POST['code'] ) ) ); 
  63. $redirect = true; 
  64. } catch ( Google_AuthException $e ) { 
  65. /** If user passes invalid Google Authentication Code. */ ?> 
  66. <form id="gglnltcs-invalid-code" method="post" action="<?php echo $_SERVER["PHP_SELF"] . '?page=bws-google-analytics.php'; ?>"> 
  67. <?php wp_nonce_field( plugin_basename( __FILE__ ), 'gglnltcs_nonce_name' ); ?> 
  68. <input type="hidden" name="invalid_code" value="1"> 
  69. <noscript> 
  70. <p><span class="gglnltcs-unsuccess-message"><?php _e( 'Invalid code. Please, try again.', 'gglnltcs' ); ?></span></p> 
  71. <input type="submit" value="<?php _e( 'Back', 'gglnltcs' ); ?>"> 
  72. </noscript> 
  73. </form> 
  74. <script type="text/javascript"> 
  75. document.forms['invalid-code'].submit(); 
  76. </script><?php 
  77. $redirect = false; 
  78. /** Save Access Token to the database and reload the page. */ 
  79. if ( $redirect && check_admin_referer( plugin_basename( __FILE__ ), 'gglnltcs_nonce_name' ) ) { 
  80. $gglnltcs_options[ 'token' ] = $client->getAccessToken(); 
  81. update_option( 'gglnltcs_options', $gglnltcs_options ); 
  82. $redirect = $_SERVER['PHP_SELF'] . '?page=bws-google-analytics.php'; 
  83. wp_redirect( filter_var( $redirect, FILTER_SANITIZE_URL ) ); 
  84. exit; 
  85. } /** close else isset ! post['code'].*/ 
  86. } else {  
  87. /** If setAccessToken() was successful */ 
  88. /** We have an authorized user and can display his website stats.*/ 
  89. /** Run Main Plugin Function */ 
  90. $analytics = new Google_AnalyticsService( $client ); 
  91. gglnltcs_main_func( $analytics, $client ); 
  92. } /** close gglnltcs_settings_page function.*/