| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Tag Groups |
| 5 |
* |
| 6 |
* @author Christoph Amthor |
| 7 |
* @copyright 2019 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 8 |
* @license GPL-3.0+ |
| 9 |
*/ |
| 10 |
|
| 11 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, PSR1.Methods.CamelCapsMethodName.NotCamelCaps, WordPress.PHP.DevelopmentFunctions, PublishPressStandards.Debug.DisallowDebugFunctions |
| 12 |
if (! class_exists('TagGroups_Error')) { |
| 13 |
/** |
| 14 |
* Error processing |
| 15 |
* |
| 16 |
*/ |
| 17 |
class TagGroups_Error |
| 18 |
{ |
| 19 |
/** |
| 20 |
* verbosity |
| 21 |
*/ |
| 22 |
public const NORMAL = 'normal'; |
| 23 |
public const VERBOSE = 'verbose'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Creates a HTML presentation of the backtrace |
| 27 |
* |
| 28 |
* based on http://php.net/manual/en/function.debug-backtrace.php#112238 |
| 29 |
* |
| 30 |
* @param void |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public static function get_backtrace() |
| 34 |
{ |
| 35 |
|
| 36 |
$path = ABSPATH; |
| 37 |
|
| 38 |
$e = new Exception(); |
| 39 |
$trace = explode("\n", $e->getTraceAsString()); |
| 40 |
// reverse array to make steps line up chronologically |
| 41 |
$trace = array_reverse($trace); |
| 42 |
array_shift($trace); |
| 43 |
|
| 44 |
array_pop($trace); |
| 45 |
|
| 46 |
$length = count($trace); |
| 47 |
$result = array(); |
| 48 |
|
| 49 |
for ($i = 0; $i < $length; $i++) { |
| 50 |
if (strpos($trace[$i], 'TagGroups_Error::') !== false) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
// remove home path |
| 55 |
$output = str_replace($path, '', substr($trace[$i], strpos($trace[$i], ' '))); |
| 56 |
$result[] = ( $i + 1 ) . ')' . $output; |
| 57 |
} |
| 58 |
|
| 59 |
return "\t" . implode("\n\t", $result); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Logs a message about a deprecated function and a backtrace |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public static function deprecated() |
| 69 |
{ |
| 70 |
|
| 71 |
if (! self::is_debug()) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
if (! self::is_verbose()) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
self::log('[Tag Groups] Called a deprecated function.'); |
| 81 |
error_log(TagGroups_Error::get_backtrace()); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Alias for debug() |
| 87 |
* |
| 88 |
* @param mixed ...$params |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
// phpcs:ignore PublishPressStandards.Debug.DisallowDebugFunctions.Found -- Legacy debug helper alias, only logs when debugging is enabled. |
| 92 |
public static function dump(...$params) |
| 93 |
{ |
| 94 |
|
| 95 |
return self::debug(...$params); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* Logs one or more variables, if debugging is on |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public static function debug() |
| 105 |
{ |
| 106 |
|
| 107 |
if (! self::is_debug()) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
$number_of_arguments = func_num_args(); |
| 112 |
if (0 == $number_of_arguments) { |
| 113 |
error_log(TagGroups_Error::get_backtrace()); |
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
$args = func_get_args(); |
| 118 |
foreach ($args as $arg) { |
| 119 |
error_log(var_export($arg, true)); |
| 120 |
} |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* wrapper for log(), additionally checks if verbose logging is on |
| 127 |
* |
| 128 |
* Optionally just a message, or a formatted message followed by the parameters |
| 129 |
* |
| 130 |
* @param mixed ...$params |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
public static function verbose_log(...$params) |
| 134 |
{ |
| 135 |
|
| 136 |
if (! self::is_verbose()) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
self::log(...$params); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* Logs a formatted message, if debugging is on |
| 146 |
* |
| 147 |
* Parameters can be: |
| 148 |
* - none: writes backtrace |
| 149 |
* - a string |
| 150 |
* - a formatted message for sprintf(), followed by the parameters |
| 151 |
* |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
public static function log() |
| 155 |
{ |
| 156 |
|
| 157 |
if (! self::is_debug()) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
$number_of_arguments = func_num_args(); |
| 162 |
if (0 == $number_of_arguments) { |
| 163 |
error_log(TagGroups_Error::get_backtrace()); |
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
$args = func_get_args(); |
| 168 |
if (1 == $number_of_arguments) { |
| 169 |
if (! is_integer($args[0]) && ! is_string($args[0])) { |
| 170 |
$args[0] = print_r($args[0], true); |
| 171 |
} |
| 172 |
|
| 173 |
error_log($args[0]); |
| 174 |
} else { |
| 175 |
$format = $args[0]; |
| 176 |
unset($args[0]); |
| 177 |
foreach ($args as &$arg) { |
| 178 |
if (! is_integer($arg) && ! is_string($arg)) { |
| 179 |
$arg = var_export($arg, true); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
error_log(vsprintf($format, $args)); |
| 184 |
} |
| 185 |
|
| 186 |
if (defined('CM_DEBUG')) { |
| 187 |
$debug_level = (int) CM_DEBUG; |
| 188 |
} else { |
| 189 |
$debug_level = 1; |
| 190 |
} |
| 191 |
|
| 192 |
if ($debug_level > 1) { |
| 193 |
error_log(TagGroups_Error::get_backtrace()); |
| 194 |
} |
| 195 |
|
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/** |
| 201 |
* Whether WP debug mode is on |
| 202 |
* |
| 203 |
* @return boolean |
| 204 |
*/ |
| 205 |
public static function is_debug() |
| 206 |
{ |
| 207 |
|
| 208 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
if (defined('CM_DEBUG')) { |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
/** |
| 221 |
* Whether verbose debugging is on |
| 222 |
* |
| 223 |
* @return boolean |
| 224 |
*/ |
| 225 |
public static function is_verbose() |
| 226 |
{ |
| 227 |
|
| 228 |
if (defined('CM_DEBUG') && strtolower(CM_DEBUG) == self::VERBOSE) { |
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
if (TagGroups_Options::get_option('tag_group_verbose_debug', 0)) { |
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
return false; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
} |
| 242 |
|