| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
if (!function_exists('wc_print_r')) { |
| 9 |
/** |
| 10 |
* Prints human-readable information about a variable. |
| 11 |
* |
| 12 |
* Some server environments blacklist some debugging functions. This function provides a safe way to |
| 13 |
* turn an expression into a printable, readable form without calling blacklisted functions. |
| 14 |
* |
| 15 |
* @since 3.0 |
| 16 |
* |
| 17 |
* @param mixed $expression The expression to be printed. |
| 18 |
* @param bool $return Optional. Default false. Set to true to return the human-readable string. |
| 19 |
* |
| 20 |
* @return string|bool False if expression could not be printed. True if the expression was printed. |
| 21 |
* If $return is true, a string representation will be returned. |
| 22 |
*/ |
| 23 |
function wc_print_r($expression, $return = false) |
| 24 |
{ |
| 25 |
$alternatives = [ |
| 26 |
[ |
| 27 |
'func' => 'print_r', |
| 28 |
'args' => [$expression, true], |
| 29 |
], |
| 30 |
[ |
| 31 |
'func' => 'var_export', |
| 32 |
'args' => [$expression, true], |
| 33 |
], |
| 34 |
[ |
| 35 |
'func' => 'json_encode', |
| 36 |
'args' => [$expression], |
| 37 |
], |
| 38 |
[ |
| 39 |
'func' => 'serialize', |
| 40 |
'args' => [$expression], |
| 41 |
], |
| 42 |
]; |
| 43 |
|
| 44 |
$alternatives = apply_filters('woocommerce_print_r_alternatives', $alternatives, $expression); |
| 45 |
|
| 46 |
foreach ($alternatives as $alternative) { |
| 47 |
if (function_exists($alternative['func'])) { |
| 48 |
$res = call_user_func_array($alternative['func'], $alternative['args']); |
| 49 |
if ($return) { |
| 50 |
return $res; |
| 51 |
} else { |
| 52 |
echo $res; // WPCS: XSS ok. |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return false; |
| 60 |
} |
| 61 |
} |
| 62 |
|