class-support-logging.php
235 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | class Ga_SupportLogger { |
| 5 | const LOG_OPTION = 'googleanalytics_sherethis_error_log'; |
| 6 | |
| 7 | static $debug_info; |
| 8 | static $debug_help_message; |
| 9 | |
| 10 | /** |
| 11 | * Constructor. |
| 12 | * @return void |
| 13 | */ |
| 14 | public function __construct() { |
| 15 | add_action( 'st_support_show_button', array( $this, 'display_button' ) ); |
| 16 | add_action( 'st_support_save_error', array( $this, 'save_error' ) ); |
| 17 | $this->get_debug_body(); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Displays a button to email the debugging info. |
| 22 | * @return void |
| 23 | */ |
| 24 | public function display_button() { |
| 25 | printf( |
| 26 | '<a href="%s" class="button button-secondary" target="_blank">Get Debugging Info</a>', |
| 27 | esc_url( '' ) |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Saves an error to the log. |
| 34 | * @param Exception $err Error to save. |
| 35 | * @return void |
| 36 | */ |
| 37 | public function save_error( Exception $err ) { |
| 38 | $cur_log = get_option( self::LOG_OPTION, array() ); |
| 39 | |
| 40 | // Creates the error object. |
| 41 | $new_log = array( |
| 42 | 'message' => $err->getMessage(), |
| 43 | 'stack' => $err->getTraceAsString(), |
| 44 | 'date' => current_time( 'r' ), |
| 45 | ); |
| 46 | |
| 47 | if ( method_exists( $err, 'get_google_error_response' ) ) { |
| 48 | $new_log['response'] = $err->get_google_error_response(); |
| 49 | } |
| 50 | |
| 51 | $cur_log[] = $new_log; |
| 52 | |
| 53 | // Cap the log at 20 entries for space purposes. |
| 54 | if ( count( $cur_log ) > 20 ) { |
| 55 | array_pop( $cur_log ); |
| 56 | } |
| 57 | |
| 58 | // Save. |
| 59 | update_option( self::LOG_OPTION, $cur_log ); |
| 60 | } |
| 61 | |
| 62 | public function get_debug_body() { |
| 63 | $debug_error = $this->get_formatted_log(); |
| 64 | |
| 65 | if ( 'None' === $debug_error ) { |
| 66 | self::$debug_info = false; |
| 67 | |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $debug_message = $this->get_formatted_message(); |
| 72 | |
| 73 | $debug_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
| 74 | |
| 75 | $let_debug_message = '<br> If you are still experiencing the issue after that click <a href="' . $debug_link . '&sdb=true" id="debug-message">here</a>'; |
| 76 | |
| 77 | $debug_help_message = !empty($_GET['sdb']) ? false : $this->get_debug_help_message($debug_message); |
| 78 | |
| 79 | if (isset($debug_help_message['message'])) { |
| 80 | $body = $debug_help_message['message']; |
| 81 | $body .= $debug_help_message['let-debug'] ? $let_debug_message : ''; |
| 82 | |
| 83 | self::$debug_info = array('message' => $body, 'debug' => $debug_help_message['let-debug']); |
| 84 | } else { |
| 85 | $body = 'Debug Info:' . PHP_EOL . PHP_EOL; |
| 86 | $body .= implode( $this->get_debug_info(), PHP_EOL ); |
| 87 | $body .= PHP_EOL . PHP_EOL . 'Error Log:' . PHP_EOL . PHP_EOL; |
| 88 | $body .= esc_html( $debug_error ); |
| 89 | |
| 90 | self::$debug_info = $body; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public function get_debug_help_message($error) { |
| 95 | |
| 96 | switch ($error) { |
| 97 | case 'invalid_grant': |
| 98 | return array( |
| 99 | 'message' => 'Hi! It looks like you submitted the wrong authentication grant. Please try again by re-authenticating.', |
| 100 | 'let-debug' => true |
| 101 | ); |
| 102 | break; |
| 103 | case 'SSL certificate problem: unable to get local issuer certificate (60)': |
| 104 | return array( |
| 105 | 'message' => 'Hi! Please check your site\'s SSL certificate. A functioning SSL certificate is required', |
| 106 | 'let-debug' => false |
| 107 | ); |
| 108 | break; |
| 109 | case 'SSL certificate problem: unable to get local issuer certificate': |
| 110 | return array( |
| 111 | 'message' => 'Hi! Please check your site\'s SSL certificate. A functioning SSL certificate is required', |
| 112 | 'let-debug' => false |
| 113 | ); |
| 114 | break; |
| 115 | case 'User does not have any Google Analytics account.': |
| 116 | return array( |
| 117 | 'message' => 'Hi! Looks like we’re not able to find a Google Analytics account. Please double check to make sure the Google account you used to authenticate with has a working Google Analytics account setup.', |
| 118 | 'let-debug' => false |
| 119 | ); |
| 120 | break; |
| 121 | case 'SSL certificate problem: certificate has expired (60)': |
| 122 | return array( |
| 123 | 'message' => 'Hi! Please check your site\'s SSL certificate. A functioning SSL certificate is required', |
| 124 | 'let-debug' => false |
| 125 | ); |
| 126 | break; |
| 127 | case 'SSL certificate problem, verify that the CA cert is OK': |
| 128 | return array( |
| 129 | 'message' => 'Hi! Please check your site\'s SSL certificate. A functioning SSL certificate is required', |
| 130 | 'let-debug' => false |
| 131 | ); |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | return array( |
| 136 | 'message' => 'Hi! It appears something went wrong. We apologize for the inconvenience! Please try to re-authenticate your Google account and verify your site has a proper SSL certficiate.', |
| 137 | 'let-debug' => true |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Gets an array of debugging information about the current system. |
| 143 | * @return array |
| 144 | */ |
| 145 | private function get_debug_info() { |
| 146 | $theme = wp_get_theme(); |
| 147 | $plugins = wp_get_active_and_valid_plugins(); |
| 148 | |
| 149 | $data = array( |
| 150 | 'Plugin Version' => GOOGLEANALYTICS_VERSION, |
| 151 | 'WordPress Version' => get_bloginfo( 'version' ), |
| 152 | 'PHP Version' => phpversion(), |
| 153 | 'CURL Version' => $this->get_curl_version(), |
| 154 | 'Site URL' => get_bloginfo( 'wpurl' ), |
| 155 | 'Theme Name' => $theme->get( 'Name' ), |
| 156 | 'Theme URL' => $theme->get( 'ThemeURI' ), |
| 157 | 'Theme Version' => $theme->get( 'Version' ), |
| 158 | 'Active Plugins' => implode( $plugins, ', ' ), |
| 159 | 'Operating System' => $this->get_operating_system(), |
| 160 | 'Web Server' => $_SERVER['SERVER_SOFTWARE'], |
| 161 | 'Current Time' => current_time( 'r' ), |
| 162 | 'Browser' => !empty( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '', |
| 163 | 'Excluded roles' => get_option( 'googleanalytics_exclude_roles' ), |
| 164 | 'Manually Tracking ID enabled' => get_option( 'googleanalytics_web_property_id_manually' ), |
| 165 | 'Manually typed Tracking ID' => get_option( 'googleanalytics_web_property_id_manually_value' ), |
| 166 | 'Tracking ID' => get_option( 'googleanalytics_web_property_id' ), |
| 167 | ); |
| 168 | $formatted = array(); |
| 169 | foreach ( $data as $text => $value ) { |
| 170 | $formatted[] = sprintf( |
| 171 | __( $text ) . ': %s', |
| 172 | $value |
| 173 | ); |
| 174 | } |
| 175 | return $formatted; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets CURL version |
| 180 | * @return string |
| 181 | */ |
| 182 | private function get_curl_version(){ |
| 183 | $curl_version = curl_version(); |
| 184 | return !empty( $curl_version['version'] ) ? $curl_version['version'] : ''; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Gets operating system |
| 189 | * @return string |
| 190 | */ |
| 191 | private function get_operating_system(){ |
| 192 | if ( function_exists( 'ini_get' ) ) { |
| 193 | $disabled = explode( ',', ini_get( 'disable_functions' ) ); |
| 194 | return !in_array( 'php_uname', $disabled ) ? php_uname() : PHP_OS; |
| 195 | } |
| 196 | return PHP_OS; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Gets a string of formatted error log entries. |
| 201 | * @return string |
| 202 | */ |
| 203 | private function get_formatted_log() { |
| 204 | $log = get_option( self::LOG_OPTION ); |
| 205 | if ( ! $log ) { |
| 206 | return 'None'; |
| 207 | } |
| 208 | |
| 209 | $text = ''; |
| 210 | foreach ( $log as $error ) { |
| 211 | foreach ( $error as $key => $value ) { |
| 212 | $text .= ucwords( $key ) . ': ' . $value . "\n"; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return $text; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Gets a string of formatted of just the message |
| 221 | * @return string |
| 222 | */ |
| 223 | private function get_formatted_message() { |
| 224 | $log = get_option( self::LOG_OPTION ); |
| 225 | if ( ! $log ) { |
| 226 | return 'None'; |
| 227 | } |
| 228 | |
| 229 | return isset($log[0]['message']) ? $log[0]['message'] : ''; |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | |
| 234 | new Ga_SupportLogger(); |
| 235 |