| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Log Muter |
| 4 |
* |
| 5 |
* This class provides a simple way to mute specific error logs that clutter the error log. |
| 6 |
* To enable/disable muting, simply change the MUTE_LOGS constant to true/false. |
| 7 |
* |
| 8 |
* Muted logs: |
| 9 |
* - CHARITABLE: NEW VENDOR CALL (THROWN/RECEIVED) |
| 10 |
* - Charitable Progress Bar Debug |
| 11 |
* - Charitable Admin Splash |
| 12 |
* - [Charitable Square] logs |
| 13 |
* - Square Legacy Mode logs |
| 14 |
* - NPS Survey logs |
| 15 |
* - Admin notices saving logs |
| 16 |
* |
| 17 |
* @package Charitable |
| 18 |
* @since 1.8.8 |
| 19 |
*/ |
| 20 |
|
| 21 |
// Prevent direct access |
| 22 |
if ( ! defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
class Charitable_Log_Muter { |
| 27 |
|
| 28 |
/** |
| 29 |
* Whether to mute the specified logs |
| 30 |
* |
| 31 |
* Set to true to mute logs, false to show them |
| 32 |
* |
| 33 |
* @var boolean |
| 34 |
*/ |
| 35 |
const MUTE_LOGS = true; |
| 36 |
|
| 37 |
/** |
| 38 |
* Patterns to mute (case-insensitive) |
| 39 |
* |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
private static $mute_patterns = array( |
| 43 |
'CHARITABLE: NEW VENDOR', |
| 44 |
'Charitable Progress Bar Debug', |
| 45 |
'Charitable Admin Splash', |
| 46 |
'[Charitable Square]', |
| 47 |
'Square Legacy Mode', |
| 48 |
'Charitable: Loading NPS Survey', |
| 49 |
'Charitable NPS Survey:', |
| 50 |
'Saving admin notices to transient', |
| 51 |
'Notices to save:' |
| 52 |
); |
| 53 |
|
| 54 |
/** |
| 55 |
* Initialize the log muter |
| 56 |
*/ |
| 57 |
public static function init() { |
| 58 |
// The log muter is ready to use |
| 59 |
// Note: Direct error_log() calls in vendor files have been disabled |
| 60 |
// by commenting them out in the vendor files themselves |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Check if a message should be muted |
| 65 |
* |
| 66 |
* @param string $message The error message |
| 67 |
* @return boolean True if message should be muted, false otherwise |
| 68 |
*/ |
| 69 |
public static function should_mute( $message ) { |
| 70 |
if ( ! self::MUTE_LOGS ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
foreach ( self::$mute_patterns as $pattern ) { |
| 75 |
if ( stripos( $message, $pattern ) !== false ) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Conditional error logging function |
| 85 |
* |
| 86 |
* @param string $message The message to log |
| 87 |
* @param int $message_type The message type |
| 88 |
* @param string $destination The destination |
| 89 |
* @param string $extra_headers Extra headers |
| 90 |
* @return boolean True if logged, false if muted |
| 91 |
*/ |
| 92 |
public static function log( $message, $message_type = 0, $destination = null, $extra_headers = null ) { |
| 93 |
if ( self::should_mute( $message ) ) { |
| 94 |
return true; // Return true to indicate "success" but don't actually log |
| 95 |
} |
| 96 |
|
| 97 |
return error_log( $message, $message_type, $destination, $extra_headers ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Initialize the log muter |
| 102 |
Charitable_Log_Muter::init(); |
| 103 |
|