DN_Google_Analytics_Admin

The Google Analytics by DN DN Google Analytics Admin class.

Defined (1)

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

/class.dn_google_analytics_admin.php  
  1. class DN_Google_Analytics_Admin { 
  2.  
  3. private $options; 
  4.  
  5. function __construct() { 
  6. add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); 
  7. add_action( 'admin_init', array( $this, 'page_init' ) ); 
  8.  
  9. public function add_plugin_page() { 
  10. add_options_page( 'Settings Admin',  
  11. 'Google Analytics',  
  12. 'manage_options',  
  13. 'settings_section',  
  14. array( $this, 'create_admin_page' )  
  15. ); 
  16.  
  17. function create_admin_page() { 
  18. // set class property 
  19. $this->options = get_option( 'dn_google_analytics_settings_option' ); 
  20. ?> 
  21. <div class="wrap"> 
  22. <h2>Google Analytics by DN</h2> 
  23. <form method="post" action="options.php"> 
  24. <?php 
  25. settings_fields( 'dn_google_analytics_settings_option_group' ); 
  26. do_settings_sections( 'settings_section' ); 
  27.  
  28. submit_button(); 
  29. ?> 
  30. </form> 
  31. </div> 
  32. <?php 
  33.  
  34. public function page_init() { 
  35. register_setting( 'dn_google_analytics_settings_option_group', // Option group 
  36. 'dn_google_analytics_settings_option', // Option name 
  37. array( $this, 'sanitize' ) // Sanitize 
  38. ); 
  39.  
  40. // General Settings Section 
  41. add_settings_section( 'general_settings_section', // Section ID 
  42. 'General Settings', // Title 
  43. array( $this, 'general_settings_info' ), // Callback 
  44. 'settings_section' // Page 
  45. ); 
  46.  
  47. // Tracking ID field 
  48. add_settings_field( 'tracking_id', // ID 
  49. 'Tracking ID', // Title 
  50. array( $this, 'tracking_id_callback' ), // Callback 
  51. 'settings_section', // Page 
  52. 'general_settings_section' // Section ID 
  53. ); 
  54.  
  55. // Anonymize IP field 
  56. add_settings_field( 'anonymize_ip', // ID 
  57. 'Anonymize IP', // Title 
  58. array( $this, 'anonymize_ip_callback' ), // Callback 
  59. 'settings_section', // Page 
  60. 'general_settings_section' // Section ID 
  61. ); 
  62.  
  63. public function general_settings_info() { 
  64. $html = 'Note: Google Analytics will not be activated if Tracking ID is empty.'; 
  65. echo $html; 
  66.  
  67. public function tracking_id_callback() { 
  68. printf(' 
  69. <label for="tracking_id_input" style="display:none;"><b>Tracking ID: </b></label> 
  70. <input type="text" placeholder="Eg. UA-XXXXX-XX" id="tracking_id" name="dn_google_analytics_settings_option[tracking_id]" value="%s" />',  
  71. isset( $this->options['tracking_id'] ) ? esc_attr( $this->options['tracking_id'] ) : '' 
  72. ); 
  73.  
  74. public function anonymize_ip_callback() { 
  75. $this->options = get_option( 'dn_google_analytics_settings_option' ); 
  76. $html = '<input type="checkbox" id="anonymize_ip" name="dn_google_analytics_settings_option[anonymize_ip]" value="1" ' . checked( 1, $this->options['anonymize_ip'], false ) . '/>'; 
  77. $html .= '<label for="anonymize_ip"> Check to enable</label>'; 
  78. $html .= '<p class="description">Protect visitors\' privacy by anonymizing their IP address in Google Analytics.</p>'; 
  79. echo $html; 
  80.  
  81. public function sanitize( $input ) { 
  82. $output = array(); 
  83.  
  84. foreach( $input as $key => $value ) { 
  85. // Check to see if the current option has a value. If so, process it. 
  86. if( isset( $input[$key] ) ) { 
  87. $output[$key] = strip_tags( stripslashes( $input[ $key ] ) ); // Strip all HTML and PHP tags and properly handle quoted strings 
  88.  
  89. // Return the array processing any additional functions filtered by this action 
  90. return apply_filters( 'sanitize', $output, $input );