class-support-logging.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | class Ga_SupportLogger { |
| 5 | |
| 6 | const EMAIL = 'support@googleanalytics.zendesk.com'; |
| 7 | const LOG_OPTION = 'googleanalytics_sherethis_error_log'; |
| 8 | |
| 9 | static $debug_info; |
| 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 | public static function send_email() { |
| 21 | $email = !empty( $_POST[ 'email' ] ) ? sanitize_text_field( $_POST[ 'email' ] ) : ''; |
| 22 | $description = !empty( $_POST[ 'description' ] ) ? __( 'Description of the issue:' ). PHP_EOL . PHP_EOL . sanitize_text_field( $_POST[ 'description' ] ) . PHP_EOL . PHP_EOL : ''; |
| 23 | |
| 24 | if ( !is_email( $email ) ) { |
| 25 | $response['error'] = 'Don\'t forget to provide your email address!'; |
| 26 | } |
| 27 | else if ( wp_mail( self::EMAIL, __( 'Debug Report' ), $description . self::$debug_info, 'From: ' . $email ) ) { |
| 28 | $response['success'] = 'Success! Thank you for sending your debug report, you should receive a confirmation email from us. If you don\'t, please email us directly at <a href="mailto:support@googleanalytics.zendesk.com">support@googleanalytics.zendesk.com</a>.'; |
| 29 | } else { |
| 30 | $response['error'] = 'Oops! Looks like we weren\'t able to send the email. Please copy and paste the "debug info" into your favorite email client to: <a href="mailto:support@googleanalytics.zendesk.com">support@googleanalytics.zendesk.com</a>. We hope to help soon!'; |
| 31 | } |
| 32 | |
| 33 | echo wp_json_encode( $response ); |
| 34 | wp_die(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Displays a button to email the debugging info. |
| 39 | * @return void |
| 40 | */ |
| 41 | public function display_button() { |
| 42 | printf( |
| 43 | '<a href="%s" class="button button-secondary" target="_blank">Send Debugging Info</a>', |
| 44 | esc_url( $this->get_mail_link() ) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Saves an error to the log. |
| 51 | * @param Exception $err Error to save. |
| 52 | * @return void |
| 53 | */ |
| 54 | public function save_error( Exception $err ) { |
| 55 | $cur_log = get_option( self::LOG_OPTION, array() ); |
| 56 | |
| 57 | // Creates the error object. |
| 58 | $new_log = array( |
| 59 | 'message' => $err->getMessage(), |
| 60 | 'stack' => $err->getTraceAsString(), |
| 61 | 'date' => current_time( 'r' ), |
| 62 | ); |
| 63 | |
| 64 | if ( method_exists( $err, 'get_google_error_response' ) ) { |
| 65 | $new_log['response'] = $err->get_google_error_response(); |
| 66 | } |
| 67 | |
| 68 | $cur_log[] = $new_log; |
| 69 | |
| 70 | // Cap the log at 20 entries for space purposes. |
| 71 | if ( count( $cur_log ) > 20 ) { |
| 72 | array_pop( $cur_log ); |
| 73 | } |
| 74 | |
| 75 | // Save. |
| 76 | update_option( self::LOG_OPTION, $cur_log ); |
| 77 | } |
| 78 | |
| 79 | public function get_debug_body(){ |
| 80 | $body = 'Debug Info:' . PHP_EOL . PHP_EOL; |
| 81 | $body .= implode( $this->get_debug_info(), PHP_EOL ); |
| 82 | $body .= PHP_EOL . PHP_EOL . 'Error Log:' . PHP_EOL . PHP_EOL; |
| 83 | $body .= $this->get_formatted_log(); |
| 84 | self::$debug_info = $body; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns a string used for providing an email link. |
| 89 | * @return string |
| 90 | */ |
| 91 | private function get_mail_link() { |
| 92 | |
| 93 | $body = 'DESCRIBE ISSUE HERE:' . str_repeat( '%0A', 10 ); |
| 94 | $body .= 'Debug Info:%0A%0A'; |
| 95 | $body .= implode( $this->get_debug_info(), '%0A' ); |
| 96 | $body .= '%0A%0AError Log:%0A%0A'; |
| 97 | $body .= str_replace( "\n", '%0A', $this->get_formatted_log() ); |
| 98 | |
| 99 | return add_query_arg( array( |
| 100 | 'subject' => __( 'Debug Report' ), |
| 101 | 'body' => $body, |
| 102 | ), 'mailto:' . self::EMAIL ); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Gets an array of debugging information about the current system. |
| 108 | * @return array |
| 109 | */ |
| 110 | private function get_debug_info() { |
| 111 | $theme = wp_get_theme(); |
| 112 | $plugins = wp_get_active_and_valid_plugins(); |
| 113 | |
| 114 | $data = array( |
| 115 | 'Plugin Version' => GOOGLEANALYTICS_VERSION, |
| 116 | 'WordPress Version' => get_bloginfo( 'version' ), |
| 117 | 'PHP Version' => phpversion(), |
| 118 | 'CURL Version' => $this->get_curl_version(), |
| 119 | 'Site URL' => get_bloginfo( 'wpurl' ), |
| 120 | 'Theme Name' => $theme->get( 'Name' ), |
| 121 | 'Theme URL' => $theme->get( 'ThemeURI' ), |
| 122 | 'Theme Version' => $theme->get( 'Version' ), |
| 123 | 'Active Plugins' => implode( $plugins, ', ' ), |
| 124 | 'Operating System' => $this->get_operating_system(), |
| 125 | 'Web Server' => $_SERVER['SERVER_SOFTWARE'], |
| 126 | 'Current Time' => current_time( 'r' ), |
| 127 | 'Browser' => !empty( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '', |
| 128 | 'Excluded roles' => get_option( 'googleanalytics_exclude_roles' ), |
| 129 | 'Manually Tracking ID enabled' => get_option( 'googleanalytics_web_property_id_manually' ), |
| 130 | 'Manually typed Tracking ID' => get_option( 'googleanalytics_web_property_id_manually_value' ), |
| 131 | 'Tracking ID' => get_option( 'googleanalytics_web_property_id' ), |
| 132 | ); |
| 133 | $formatted = array(); |
| 134 | foreach ( $data as $text => $value ) { |
| 135 | $formatted[] = sprintf( |
| 136 | __( $text ) . ': %s', |
| 137 | $value |
| 138 | ); |
| 139 | } |
| 140 | return $formatted; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Gets CURL version |
| 145 | * @return string |
| 146 | */ |
| 147 | private function get_curl_version(){ |
| 148 | $curl_version = curl_version(); |
| 149 | return !empty( $curl_version['version'] ) ? $curl_version['version'] : ''; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Gets operating system |
| 154 | * @return string |
| 155 | */ |
| 156 | private function get_operating_system(){ |
| 157 | if ( function_exists( 'ini_get' ) ) { |
| 158 | $disabled = explode( ',', ini_get( 'disable_functions' ) ); |
| 159 | return !in_array( 'php_uname', $disabled ) ? php_uname() : PHP_OS; |
| 160 | } |
| 161 | return PHP_OS; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Gets a string of formatted error log entries. |
| 166 | * @return string |
| 167 | */ |
| 168 | private function get_formatted_log() { |
| 169 | $log = get_option( self::LOG_OPTION ); |
| 170 | if ( ! $log ) { |
| 171 | return 'None'; |
| 172 | } |
| 173 | |
| 174 | $text = ''; |
| 175 | foreach ( $log as $error ) { |
| 176 | foreach ( $error as $key => $value ) { |
| 177 | $text .= ucwords( $key ) . ': ' . $value . "\n"; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return $text; |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | |
| 186 | new Ga_SupportLogger(); |
| 187 |