| 1 |
<?php |
| 2 |
/** |
| 3 |
* Imsanity utility functions. |
| 4 |
* |
| 5 |
* @package Imsanity |
| 6 |
*/ |
| 7 |
|
| 8 |
// Initialize global variable for debug information/outut. |
| 9 |
$imsanity_debug_data = ''; |
| 10 |
|
| 11 |
/** |
| 12 |
* Finds the current PHP memory limit or a reasonable default. |
| 13 |
* |
| 14 |
* @return int The memory limit in bytes. |
| 15 |
*/ |
| 16 |
function imsanity_memory_limit() { |
| 17 |
if ( \defined( 'IMSANITY_MEMORY_LIMIT' ) && IMSANITY_MEMORY_LIMIT ) { |
| 18 |
$memory_limit = IMSANITY_MEMORY_LIMIT; |
| 19 |
} elseif ( function_exists( 'ini_get' ) ) { |
| 20 |
$memory_limit = ini_get( 'memory_limit' ); |
| 21 |
} else { |
| 22 |
// Conservative default, current usage + 16M. |
| 23 |
$current_memory = memory_get_usage( true ); |
| 24 |
$memory_limit = round( $current_memory / ( 1024 * 1024 ) ) + 16; |
| 25 |
if ( ! defined( 'IMSANITY_MEMORY_LIMIT' ) ) { |
| 26 |
define( 'IMSANITY_MEMORY_LIMIT', $memory_limit ); |
| 27 |
} |
| 28 |
} |
| 29 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 30 |
WP_CLI::debug( "memory limit is set at $memory_limit" ); |
| 31 |
} |
| 32 |
if ( ! $memory_limit || -1 === intval( $memory_limit ) ) { |
| 33 |
// Unlimited, set to 32GB. |
| 34 |
$memory_limit = '32000M'; |
| 35 |
} |
| 36 |
if ( stripos( $memory_limit, 'g' ) ) { |
| 37 |
$memory_limit = intval( $memory_limit ) * 1024 * 1024 * 1024; |
| 38 |
} else { |
| 39 |
$memory_limit = intval( $memory_limit ) * 1024 * 1024; |
| 40 |
} |
| 41 |
return $memory_limit; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the path to the current debug log, if one exists. Otherwise, generate a new filename. |
| 46 |
* |
| 47 |
* @return string The full path to the debug log. |
| 48 |
*/ |
| 49 |
function imsanity_debug_log_path() { |
| 50 |
if ( is_dir( IMSANITY_PLUGIN_DIR ) ) { |
| 51 |
$potential_logs = \scandir( IMSANITY_PLUGIN_DIR ); |
| 52 |
if ( is_iterable( $potential_logs ) ) { |
| 53 |
foreach ( $potential_logs as $potential_log ) { |
| 54 |
if ( str_ends_with( $potential_log, '.log' ) && str_contains( $potential_log, 'imsanity-debug-' ) && is_file( IMSANITY_PLUGIN_DIR . $potential_log ) ) { |
| 55 |
return IMSANITY_PLUGIN_DIR . $potential_log; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
return IMSANITY_PLUGIN_DIR . 'imsanity-debug-' . uniqid() . '.log'; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Saves the in-memory debug data to a logfile in the plugin folder. This is called on shutdown if the IMSANITY_DEBUG constant is set to true. |
| 65 |
*/ |
| 66 |
function imsanity_debug_log() { |
| 67 |
if ( ! defined( 'IMSANITY_DEBUG' ) || ! IMSANITY_DEBUG ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
global $imsanity_debug_data; |
| 71 |
$debug_log = imsanity_debug_log_path(); |
| 72 |
if ( ! empty( $imsanity_debug_data ) && is_writable( IMSANITY_PLUGIN_DIR ) ) { |
| 73 |
$memory_limit = imsanity_memory_limit(); |
| 74 |
clearstatcache(); |
| 75 |
$timestamp = gmdate( 'Y-m-d H:i:s' ) . "\n"; |
| 76 |
if ( ! file_exists( $debug_log ) ) { |
| 77 |
\touch( $debug_log ); |
| 78 |
} else { |
| 79 |
if ( filesize( $debug_log ) + 4000000 + memory_get_usage( true ) > $memory_limit ) { |
| 80 |
unlink( $debug_log ); |
| 81 |
clearstatcache(); |
| 82 |
$debug_log = imsanity_debug_log_path(); |
| 83 |
touch( $debug_log ); |
| 84 |
} |
| 85 |
} |
| 86 |
if ( filesize( $debug_log ) + strlen( $imsanity_debug_data ) + 4000000 + memory_get_usage( true ) <= $memory_limit && is_writable( $debug_log ) ) { |
| 87 |
$imsanity_debug_data = str_replace( '<br>', "\n", $imsanity_debug_data ); |
| 88 |
file_put_contents( $debug_log, $timestamp . $imsanity_debug_data, FILE_APPEND ); |
| 89 |
} |
| 90 |
} |
| 91 |
$imsanity_debug_data = ''; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Add a message to the debug log (in memory). |
| 96 |
* |
| 97 |
* @param string $message A message to send to the debugger. |
| 98 |
*/ |
| 99 |
function imsanity_debug( $message ) { |
| 100 |
global $imsanity_debug_data; |
| 101 |
if ( ! is_string( $message ) && ! is_int( $message ) && ! is_float( $message ) ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
if ( \defined( 'IMSANITY_PHPUNIT' ) && IMSANITY_PHPUNIT ) { |
| 105 |
if ( |
| 106 |
! empty( $_SERVER['argv'] ) && |
| 107 |
( \in_array( '--debug', $_SERVER['argv'], true ) || \in_array( '--verbose', $_SERVER['argv'], true ) ) |
| 108 |
) { |
| 109 |
$message = str_replace( '<br>', "\n", $message ); |
| 110 |
$message = str_replace( '<b>', '+', $message ); |
| 111 |
$message = str_replace( '</b>', '+', $message ); |
| 112 |
echo esc_html( $message ) . "\n"; |
| 113 |
} |
| 114 |
} |
| 115 |
$message = "$message"; |
| 116 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 117 |
WP_CLI::debug( $message ); |
| 118 |
return; |
| 119 |
} |
| 120 |
if ( defined( 'IMSANITY_DEBUG' ) && IMSANITY_DEBUG ) { |
| 121 |
$memory_limit = imsanity_memory_limit(); |
| 122 |
// If the message size + 4 MB + current usage exceeds the limit, don't add any more to the debug log. |
| 123 |
if ( strlen( $message ) + 4000000 + memory_get_usage( true ) > $memory_limit ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
$message = \str_replace( "\n\n\n", '<br>', $message ); |
| 127 |
$message = \str_replace( "\n\n", '<br>', $message ); |
| 128 |
$message = \str_replace( "\n", '<br>', $message ); |
| 129 |
$imsanity_debug_data .= "$message<br>"; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* View the debug log file from the wp-admin. |
| 135 |
*/ |
| 136 |
function imsanity_view_debug_log() { |
| 137 |
check_admin_referer( 'imsanity-options' ); |
| 138 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 139 |
wp_die( esc_html__( 'Access denied.', 'imsanity' ) ); |
| 140 |
} |
| 141 |
$debug_log = imsanity_debug_log_path(); |
| 142 |
if ( is_file( $debug_log ) ) { |
| 143 |
header( 'Content-Type: text/plain;charset=UTF-8' ); |
| 144 |
readfile( $debug_log ); |
| 145 |
exit; |
| 146 |
} |
| 147 |
wp_die( esc_html__( 'The Debug Log is empty.', 'imsanity' ) ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Removes the debug log file. |
| 152 |
*/ |
| 153 |
function imsanity_delete_debug_log() { |
| 154 |
check_admin_referer( 'imsanity-options' ); |
| 155 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 156 |
wp_die( esc_html__( 'Access denied.', 'imsanity' ) ); |
| 157 |
} |
| 158 |
$debug_log = imsanity_debug_log_path(); |
| 159 |
if ( is_file( $debug_log ) && is_writable( $debug_log ) ) { |
| 160 |
unlink( $debug_log ); |
| 161 |
} |
| 162 |
$sendback = wp_get_referer(); |
| 163 |
if ( empty( $sendback ) ) { |
| 164 |
$sendback = imsanity_get_settings_link(); |
| 165 |
} |
| 166 |
wp_safe_redirect( $sendback ); |
| 167 |
exit; |
| 168 |
} |
| 169 |
|
| 170 |
// Flush the debug log on shutdown. |
| 171 |
add_action( 'shutdown', 'imsanity_debug_log' ); |
| 172 |
// Non-AJAX handler to view the debug log from the wp-admin. |
| 173 |
add_action( 'admin_action_imsanity_view_debug_log', 'imsanity_view_debug_log' ); |
| 174 |
// Non-AJAX handler to delete the debug log, and reroute back to the settings page. |
| 175 |
add_action( 'admin_action_imsanity_delete_debug_log', 'imsanity_delete_debug_log' ); |
| 176 |
|