PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 2.1.2
ShareThis Dashboard for Google Analytics v2.1.2
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-support-logging.php
googleanalytics / tools Last commit date
Ga_Cache.php 9 years ago class-support-logging.php 9 years ago
class-support-logging.php
162 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 'Site URL' => get_bloginfo( 'wpurl' ),
119 'Theme Name' => $theme->get( 'Name' ),
120 'Theme URL' => $theme->get( 'ThemeURI' ),
121 'Theme Version' => $theme->get( 'Version' ),
122 'Active Plugins' => implode( $plugins, ', ' ),
123 'Operating System' => php_uname(),
124 'Web Server' => $_SERVER['SERVER_SOFTWARE'],
125 'Current Time' => current_time( 'r' ),
126 'Browser' => $_SERVER['HTTP_USER_AGENT'],
127 );
128 $formatted = array();
129 foreach ( $data as $text => $value ) {
130 $formatted[] = sprintf(
131 __( $text ) . ': %s',
132 $value
133 );
134 }
135 return $formatted;
136 }
137
138
139 /**
140 * Gets a string of formatted error log entries.
141 * @return string
142 */
143 private function get_formatted_log() {
144 $log = get_option( self::LOG_OPTION );
145 if ( ! $log ) {
146 return 'None';
147 }
148
149 $text = '';
150 foreach ( $log as $error ) {
151 foreach ( $error as $key => $value ) {
152 $text .= ucwords( $key ) . ': ' . $value . "\n";
153 }
154 }
155
156 return $text;
157 }
158
159 }
160
161 new Ga_SupportLogger();
162