<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[8310] sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/0-error-handling.php: WordCamp: Additional error logging tweaks</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/8310">8310</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/8310","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>coreymckrill</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-02-21 21:21:54 +0000 (Thu, 21 Feb 2019)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WordCamp: Additional error logging tweaks

* Add a namespace.
* Split up the error handler. The actual handler callback now only records
  the occurrence of errors, and then if conditions are met it also calls a
  new separate function that sends the info to Slack.
* Abstract the functionality that records the error occurrences.
* Formatting tweaks to the Slack messages.
* Ensure that the cron job that clears out old error log files is only run
  on the main site.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentmuplugins0errorhandlingphp">sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/0-error-handling.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentmuplugins0errorhandlingphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/0-error-handling.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/0-error-handling.php   2019-02-21 18:15:15 UTC (rev 8309)
+++ sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/0-error-handling.php     2019-02-21 21:21:54 UTC (rev 8310)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,25 +1,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+namespace WordCamp\Error_Handling;
</ins><span class="cx" style="display: block; padding: 0 10px"> defined( 'WPINC' ) or die();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-define( 'ERROR_RATE_LIMITING_DIR', '/tmp/error_limiting' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+use DirectoryIterator;
+use Dotorg\Slack\Send;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-if ( is_readable( __DIR__ . '/includes/slack/send.php' ) ) {
-       require_once( __DIR__ . '/includes/slack/send.php' );
-}
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+const ERROR_RATE_LIMITING_DIR = '/tmp/error_limiting';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-set_error_handler( 'send_error_to_slack' );
-register_shutdown_function( 'send_fatal_to_slack' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+set_error_handler( __NAMESPACE__ . '\handle_error' );
+register_shutdown_function( __NAMESPACE__ . '\catch_fatal' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Error handler to send errors to Slack.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Error handler to track error frequency and conditionally send error messages to Slack.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * Note: This should always return false so that default error handling still occurs as well.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @todo We should consider splitting the functionality here into two or more separate functions.
- *       This way logging errors to files could be separate from sending messages to Slack. This would
- *       also potentially allow us to load the error logging part earlier, before WP functions
- *       are available.
- *
</del><span class="cx" style="display: block; padding: 0 10px">  * @param int    $err_no
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $err_msg
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $file
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,17 +22,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @return bool
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function send_error_to_slack( $err_no, $err_msg, $file, $line ) {
-       if ( ! defined( 'WORDCAMP_ENVIRONMENT' )
-            || ( 'production' !==  WORDCAMP_ENVIRONMENT && ! defined( 'SANDBOX_SLACK_USERNAME' ) )
-       ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function handle_error( $err_no, $err_msg, $file, $line ) {
+       if ( ! check_error_handling_dependencies() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! init_error_handling() ) {
-               return false;
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         // Checks to see if the error-throwing expression is prepended with the @ control operator.
</span><span class="cx" style="display: block; padding: 0 10px">        // See https://secure.php.net/manual/en/function.set-error-handler.php
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 0 === error_reporting() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -44,7 +33,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $error_safelist = [
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $accepted_error_types = [
</ins><span class="cx" style="display: block; padding: 0 10px">                 E_ERROR,
</span><span class="cx" style="display: block; padding: 0 10px">                E_CORE_ERROR,
</span><span class="cx" style="display: block; padding: 0 10px">                E_COMPILE_ERROR,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -61,7 +50,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                E_USER_DEPRECATED,
</span><span class="cx" style="display: block; padding: 0 10px">        ];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! in_array( $err_no, $error_safelist ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! in_array( $err_no, $accepted_error_types ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -75,42 +64,136 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $err_key    = substr( base64_encode("$file-$line-$err_no" ), -254 ); // Max file length for ubuntu is 255.
-       $error_file = ERROR_RATE_LIMITING_DIR . "/$err_key";
-       $data       = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $err_key      = substr( base64_encode("$file-$line-$err_no" ), -254 ); // Max file length for ubuntu is 255.
+       $data         = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                 'last_reported_at' => time(),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'error_count'      => 0, // since last reported.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'error_count'      => 0, // Since last reported.
</ins><span class="cx" style="display: block; padding: 0 10px">         );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $messages    = explode( 'Stack trace:', $err_msg, 2 );
-       $pretext     = $messages[0] ?: '';
-       $stack_trace = ( ! empty( $messages[1] ) ) ? trim( sanitize_text_field( $messages[1] ) ) : '';
-       $footer      = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $send_message = false;
+       $occurrences  = 0;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! file_exists( $error_file ) ) {
-               file_put_contents( $error_file, wp_json_encode( $data ) );
-       } else {
-               $data                 = json_decode( file_get_contents( $error_file ), true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( error_record_exists( $err_key ) ) {
+               $data                 = get_error_record( $err_key );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $data['error_count'] += 1;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $occurrences          = $data['error_count'];
</ins><span class="cx" style="display: block; padding: 0 10px">                 $time_elapsed         = time() - $data['last_reported_at'];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $time_elapsed > 600 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $footer .= "Occurred *${data['error_count']} time(s)* since last reported";
-
</del><span class="cx" style="display: block; padding: 0 10px">                         $data['last_reported_at'] = time();
</span><span class="cx" style="display: block; padding: 0 10px">                        $data['error_count']      = 0;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        file_put_contents( $error_file, wp_json_encode( $data ) );
-               } else {
-                       file_put_contents( $error_file, wp_json_encode( $data ) );
-                       
-                       return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $send_message             = true;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        } else {
+               $send_message = true;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $domain    = esc_url( get_site_url() );
-       $page_slug = sanitize_text_field( untrailingslashit( $_SERVER['REQUEST_URI'] ) ) ?: '/';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ update_error_record( $err_key, $data );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( $send_message ) {
+               send_error_to_slack( $err_no, $err_msg, $file, $line, $occurrences );
+       }
+
+       return false;
+}
+
+/**
+ * Shutdown handler for catching fatal errors and sending them to Slack.
+ *
+ * Some error types cannot be handled directly by a custom error handler. However, we can catch them during shutdown
+ * and redirect them to the custom handler callback.
+ *
+ * @return void
+ */
+function catch_fatal() {
+       $error = error_get_last();
+
+       // See https://secure.php.net/manual/en/function.set-error-handler.php
+       $unhandled_error_types = [ E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING ];
+
+       if ( ! empty( $error ) && in_array( $error['type'], $unhandled_error_types, true ) ) {
+               handle_error( $error['type'], $error['message'], $error['file'], $error['line'] );
+       }
+}
+
+/**
+ * Check if an error has previously been recorded.
+ *
+ * @param string $err_key
+ *
+ * @return bool
+ */
+function error_record_exists( $err_key ) {
+       $error_file = ERROR_RATE_LIMITING_DIR . "/$err_key";
+
+       return is_readable( $error_file );
+}
+
+/**
+ * Get the data recorded for an error.
+ *
+ * Includes the timestamp of the error's last occurrence and the number of times it has occurred since it was
+ * last reported/sent to Slack.
+ *
+ * @param string $err_key
+ *
+ * @return array|mixed|object
+ */
+function get_error_record( $err_key ) {
+       $error_file = ERROR_RATE_LIMITING_DIR . "/$err_key";
+
+       return json_decode( file_get_contents( $error_file ), true );
+}
+
+/**
+ * Update the recorded data for an error.
+ *
+ * @param string $err_key
+ * @param array  $data
+ *
+ * @return bool|int
+ */
+function update_error_record( $err_key, $data ) {
+       $error_file = ERROR_RATE_LIMITING_DIR . "/$err_key";
+
+       return file_put_contents( $error_file, wp_json_encode( $data ) );
+}
+
+/**
+ * Build and dispatch an error message to a channel or user on Slack.
+ *
+ * @param int    $err_no
+ * @param string $err_msg
+ * @param string $file
+ * @param int    $line
+ * @param int    $occurrences
+ *
+ * @return void
+ */
+function send_error_to_slack( $err_no, $err_msg, $file, $line, $occurrences = 0 ) {
+       if ( ! defined( 'WORDCAMP_ENVIRONMENT' )
+            || ( 'production' !== WORDCAMP_ENVIRONMENT && ! defined( 'SANDBOX_SLACK_USERNAME' ) )
+                || ! is_readable( __DIR__ . '/includes/slack/send.php' )
+       ) {
+               return;
+       }
+
+       require_once( __DIR__ . '/includes/slack/send.php' );
+
+       $error_name  = array_search( $err_no, get_defined_constants( true )['Core'] ) ?: '';
+       $messages    = explode( 'Stack trace:', $err_msg, 2 );
+       $text        = ( ! empty( $messages[0] ) ) ? trim( sanitize_text_field( $messages[0] ) ) : '';
+       $stack_trace = ( ! empty( $messages[1] ) ) ? trim( sanitize_text_field( $messages[1] ) ) : '';
+       $domain      = esc_url( get_site_url() );
+       $page_slug   = sanitize_text_field( untrailingslashit( $_SERVER['REQUEST_URI'] ) ) ?: '/';
+       $footer      = '';
+
+       if ( $occurrences > 0 ) {
+               $footer .= "Occurred *$occurrences time(s)* since last reported";
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         switch( $err_no ) {
</span><span class="cx" style="display: block; padding: 0 10px">                case E_ERROR:
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                case E_PARSE:
</ins><span class="cx" style="display: block; padding: 0 10px">                 case E_CORE_ERROR:
</span><span class="cx" style="display: block; padding: 0 10px">                case E_COMPILE_ERROR :
</span><span class="cx" style="display: block; padding: 0 10px">                case E_USER_ERROR:
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -118,7 +201,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $color = '#ff0000'; // Red.
</span><span class="cx" style="display: block; padding: 0 10px">                        break;
</span><span class="cx" style="display: block; padding: 0 10px">                case E_WARNING:
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                case E_PARSE:
</del><span class="cx" style="display: block; padding: 0 10px">                 case E_CORE_WARNING:
</span><span class="cx" style="display: block; padding: 0 10px">                case E_COMPILE_WARNING:
</span><span class="cx" style="display: block; padding: 0 10px">                case E_USER_WARNING:
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -160,51 +242,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $attachment = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'fallback'    => $pretext,
-               'pretext'     => $pretext,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'fallback'    => $text,
+               'text'        => $text,
+               'author_name' => $error_name,
</ins><span class="cx" style="display: block; padding: 0 10px">                 'color'       => $color,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'author_name' => 'WordCamp Logger',
</del><span class="cx" style="display: block; padding: 0 10px">                 'fields'      => $fields,
</span><span class="cx" style="display: block; padding: 0 10px">                'footer'      => $footer,
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $send = new \Dotorg\Slack\Send( SLACK_ERROR_REPORT_URL );
-       $send->add_attachment( $attachment );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $slack = new Send( SLACK_ERROR_REPORT_URL );
+       $slack->add_attachment( $attachment );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 'production' === WORDCAMP_ENVIRONMENT ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $send->send( WORDCAMP_LOGS_SLACK_CHANNEL );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $slack->send( WORDCAMP_LOGS_SLACK_CHANNEL );
</ins><span class="cx" style="display: block; padding: 0 10px">         } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $send->send( SANDBOX_SLACK_USERNAME );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $slack->send( SANDBOX_SLACK_USERNAME );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-       return false;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Shutdown handler for catching fatal errors and sending them to Slack.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Check and create the filesystem directory used to manage error rate limiting.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Some error types cannot be handled directly by a custom error handler. However, we can catch them during shutdown
- * and redirect them to the custom handler callback.
- */
-function send_fatal_to_slack() {
-       $error = error_get_last();
-
-       // See https://secure.php.net/manual/en/function.set-error-handler.php
-       $unhandled_error_types = [ E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING ];
-
-       if ( ! empty( $error ) && in_array( $error['type'], $unhandled_error_types, true ) ) {
-               send_error_to_slack( $error['type'], $error['message'], $error['file'], $error['line'] );
-       }
-}
-
-/**
- * Check and create filesystem dirs to manage rate limiting in error handling.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * For legacy bugs we are doing rate limiting via filesystem. We would be investigating to see if we can instead use
+ * memcache to rate limit sometime in the future.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * For legacy bugs we are doing rate limiting via filesystem. We would be investigating to see if we can instead use memcache to rate limit sometime in the future.
- *
- * @return bool Return true if file permissions etc are present
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return bool Return true if file permissions etc are present.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function init_error_handling() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function check_error_handling_dependencies() {
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! file_exists( ERROR_RATE_LIMITING_DIR ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                mkdir( ERROR_RATE_LIMITING_DIR );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -215,14 +279,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Remove temporary error rate limiting files.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Function `send_error_to_slack` above also creates a bunch of files in /tmp/error_limiting folder in order to rate limit the notification.
- * This function will be used as a cron to clear these error_limiting files periodically.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Function `record_error` above also creates a bunch of files in /tmp/error_limiting folder in order to rate limit
+ * the notification. This function will be used as a cron to clear these error_limiting files periodically.
+ *
+ * @return void
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function handle_clear_error_rate_limiting_files() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! init_error_handling() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // This only needs to run on one site.
+       if ( BLOG_ID_CURRENT_SITE !== get_current_blog_id() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( ! check_error_handling_dependencies() ) {
+               return;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         foreach ( new DirectoryIterator( ERROR_RATE_LIMITING_DIR ) as $file_info ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $file_info->isDot() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        unlink( $file_info->getPathname() );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -234,4 +305,4 @@
</span><span class="cx" style="display: block; padding: 0 10px">        wp_schedule_event( time(), 'daily', 'clear_error_rate_limiting_files' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_action( 'clear_error_rate_limiting_files', 'handle_clear_error_rate_limiting_files' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'clear_error_rate_limiting_files', __NAMESPACE__ . '\handle_clear_error_rate_limiting_files' );
</ins></span></pre>
</div>
</div>

</body>
</html>