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