class-ga-supportlogger.php
291 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Support Logger class. |
| 4 | * |
| 5 | * @package GoogleAnalytics |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * GA Support Logger class. |
| 10 | */ |
| 11 | class Ga_SupportLogger { |
| 12 | const LOG_OPTION = 'googleanalytics_sherethis_error_log'; |
| 13 | |
| 14 | /** |
| 15 | * Debug info. |
| 16 | * |
| 17 | * @var mixed Info. |
| 18 | */ |
| 19 | public static $debug_info; |
| 20 | |
| 21 | /** |
| 22 | * Debug help message. |
| 23 | * |
| 24 | * @var string Message. |
| 25 | */ |
| 26 | public static $debug_help_message; |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function __construct() { |
| 34 | add_action( 'st_support_show_button', array( $this, 'display_button' ) ); |
| 35 | add_action( 'st_support_save_error', array( $this, 'save_error' ) ); |
| 36 | $this->get_debug_body(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Displays a button to email the debugging info. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public function display_button() { |
| 45 | printf( |
| 46 | '<a href="%s" class="button button-secondary" target="_blank">Get Debugging Info</a>', |
| 47 | esc_url( '' ) |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Saves an error to the log. |
| 54 | * |
| 55 | * @param Exception $err Error to save. |
| 56 | * @return void |
| 57 | */ |
| 58 | public function save_error( Exception $err ) { |
| 59 | $cur_log = get_option( self::LOG_OPTION, array() ); |
| 60 | |
| 61 | if ( false === is_array( $cur_log ) ) { |
| 62 | $cur_log = array(); |
| 63 | } |
| 64 | |
| 65 | // Creates the error object. |
| 66 | $new_log = array( |
| 67 | 'message' => $err->getMessage(), |
| 68 | 'stack' => $err->getTraceAsString(), |
| 69 | 'date' => current_time( 'r' ), |
| 70 | ); |
| 71 | |
| 72 | if ( method_exists( $err, 'get_google_error_response' ) ) { |
| 73 | $new_log['response'] = $err->get_google_error_response(); |
| 74 | } |
| 75 | |
| 76 | $cur_log[] = $new_log; |
| 77 | |
| 78 | // Cap the log at 20 entries for space purposes. |
| 79 | if ( count( $cur_log ) > 20 ) { |
| 80 | array_pop( $cur_log ); |
| 81 | } |
| 82 | |
| 83 | // Save. |
| 84 | update_option( self::LOG_OPTION, $cur_log ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get Current URL. |
| 89 | * |
| 90 | * @return string|void |
| 91 | */ |
| 92 | public static function get_current_url() { |
| 93 | $request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_UNSAFE_RAW ); |
| 94 | |
| 95 | return sanitize_text_field( wp_unslash( $request_uri ) ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get debug body. |
| 100 | * |
| 101 | * @return void |
| 102 | */ |
| 103 | public function get_debug_body() { |
| 104 | $debug_error = $this->get_formatted_log(); |
| 105 | |
| 106 | if ( 'None' === $debug_error ) { |
| 107 | self::$debug_info = false; |
| 108 | |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | $debug_message = $this->get_formatted_message(); |
| 113 | |
| 114 | $debug_link = self::get_current_url(); |
| 115 | |
| 116 | $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>'; |
| 117 | |
| 118 | $sdb = filter_input( INPUT_GET, 'sdb', FILTER_UNSAFE_RAW ); |
| 119 | |
| 120 | $debug_help_message = false === empty( $sdb ) ? false : $this->get_debug_help_message( $debug_message ); |
| 121 | |
| 122 | if ( isset( $debug_help_message['message'] ) ) { |
| 123 | $body = $debug_help_message['message']; |
| 124 | $body .= $debug_help_message['let-debug'] ? $let_debug_message : ''; |
| 125 | |
| 126 | self::$debug_info = array( |
| 127 | 'message' => $body, |
| 128 | 'debug' => $debug_help_message['let-debug'], |
| 129 | ); |
| 130 | } else { |
| 131 | $body = 'Debug Info:' . PHP_EOL . PHP_EOL; |
| 132 | $body .= implode( PHP_EOL, $this->get_debug_info() ); |
| 133 | $body .= PHP_EOL . PHP_EOL . 'Error Log:' . PHP_EOL . PHP_EOL; |
| 134 | $body .= esc_html( $debug_error ); |
| 135 | |
| 136 | self::$debug_info = $body; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get debug help message. |
| 142 | * |
| 143 | * @param string $error Error string. |
| 144 | * |
| 145 | * @return array Array with message and let-debug boolean. |
| 146 | */ |
| 147 | public function get_debug_help_message( $error ) { |
| 148 | |
| 149 | switch ( $error ) { |
| 150 | case 'invalid_grant': |
| 151 | return array( |
| 152 | 'message' => 'Hi! It looks like you submitted the wrong authentication grant. Please try again by re-authenticating.', |
| 153 | 'let-debug' => true, |
| 154 | ); |
| 155 | case 'SSL certificate problem: unable to get local issuer certificate (60)': |
| 156 | return array( |
| 157 | 'message' => 'Hi! Please check your site\'s SSL certificate. A functioning SSL certificate is required', |
| 158 | 'let-debug' => false, |
| 159 | ); |
| 160 | case 'SSL certificate problem: unable to get local issuer certificate': |
| 161 | return array( |
| 162 | 'message' => 'Hi! Please check your site\'s SSL certificate. A functioning SSL certificate is required', |
| 163 | 'let-debug' => false, |
| 164 | ); |
| 165 | case 'User does not have any Google Analytics account.': |
| 166 | return array( |
| 167 | '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.', |
| 168 | 'let-debug' => false, |
| 169 | ); |
| 170 | case 'SSL certificate problem: certificate has expired (60)': |
| 171 | return array( |
| 172 | 'message' => 'Hi! Please check your site\'s SSL certificate. A functioning SSL certificate is required', |
| 173 | 'let-debug' => false, |
| 174 | ); |
| 175 | case 'SSL certificate problem, verify that the CA cert is OK': |
| 176 | return array( |
| 177 | 'message' => 'Hi! Please check your site\'s SSL certificate. A functioning SSL certificate is required', |
| 178 | 'let-debug' => false, |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | return array( |
| 183 | '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.', |
| 184 | 'let-debug' => true, |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Gets an array of debugging information about the current system. |
| 190 | * |
| 191 | * @return array |
| 192 | */ |
| 193 | private function get_debug_info() { |
| 194 | $theme = wp_get_theme(); |
| 195 | $plugins = wp_get_active_and_valid_plugins(); |
| 196 | |
| 197 | $server_software = filter_input( INPUT_SERVER, 'SERVER_SOFTWARE', FILTER_SANITIZE_STRING ); |
| 198 | $http_user_agent = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_STRING ); |
| 199 | |
| 200 | $data = array( |
| 201 | 'Plugin Version' => GOOGLEANALYTICS_VERSION, |
| 202 | 'WordPress Version' => get_bloginfo( 'version' ), |
| 203 | 'PHP Version' => phpversion(), |
| 204 | 'CURL Version' => $this->get_curl_version(), |
| 205 | 'Site URL' => get_bloginfo( 'wpurl' ), |
| 206 | 'Theme Name' => $theme->get( 'Name' ), |
| 207 | 'Theme URL' => $theme->get( 'ThemeURI' ), |
| 208 | 'Theme Version' => $theme->get( 'Version' ), |
| 209 | 'Active Plugins' => implode( ', ', $plugins ), |
| 210 | 'Operating System' => $this->get_operating_system(), |
| 211 | 'Web Server' => $server_software, |
| 212 | 'Current Time' => current_time( 'r' ), |
| 213 | 'Browser' => false === empty( $http_user_agent ) ? $http_user_agent : '', |
| 214 | 'Excluded roles' => get_option( 'googleanalytics_exclude_roles' ), |
| 215 | 'Manually Tracking ID enabled' => get_option( 'googleanalytics_web_property_id_manually' ), |
| 216 | 'Manually typed Tracking ID' => get_option( 'googleanalytics_web_property_id_manually_value' ), |
| 217 | 'Tracking ID' => get_option( 'googleanalytics_web_property_id' ), |
| 218 | ); |
| 219 | $formatted = array(); |
| 220 | foreach ( $data as $text => $value ) { |
| 221 | /* translators: %s refers to the value of the message. */ |
| 222 | $formatted[] = esc_html__( |
| 223 | $text . sprintf( ': %s', $value ) // phpcs:ignore |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | return $formatted; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Gets CURL version |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | private function get_curl_version() { |
| 236 | $curl_version = curl_version(); |
| 237 | return ! empty( $curl_version['version'] ) ? $curl_version['version'] : ''; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Gets operating system |
| 242 | * |
| 243 | * @return string |
| 244 | */ |
| 245 | private function get_operating_system() { |
| 246 | if ( function_exists( 'ini_get' ) ) { |
| 247 | $disabled = explode( ',', ini_get( 'disable_functions' ) ); |
| 248 | return false === in_array( 'php_uname', $disabled, true ) ? php_uname() : PHP_OS; |
| 249 | } |
| 250 | return PHP_OS; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Gets a string of formatted error log entries. |
| 255 | * |
| 256 | * @return string |
| 257 | */ |
| 258 | private function get_formatted_log() { |
| 259 | $log = get_option( self::LOG_OPTION ); |
| 260 | if ( ! $log ) { |
| 261 | return 'None'; |
| 262 | } |
| 263 | |
| 264 | $text = ''; |
| 265 | foreach ( $log as $error ) { |
| 266 | foreach ( $error as $key => $value ) { |
| 267 | $text .= ucwords( $key ) . ': ' . $value . "\n"; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return $text; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Gets a string of formatted of just the message |
| 276 | * |
| 277 | * @return string |
| 278 | */ |
| 279 | private function get_formatted_message() { |
| 280 | $log = get_option( self::LOG_OPTION ); |
| 281 | if ( ! $log ) { |
| 282 | return 'None'; |
| 283 | } |
| 284 | |
| 285 | return isset( $log[0]['message'] ) ? $log[0]['message'] : ''; |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | |
| 290 | new Ga_SupportLogger(); |
| 291 |