本文整理汇总了PHP中_wp_json_sanity_check函数的典型用法代码示例。如果您正苦于以下问题:PHP _wp_json_sanity_check函数的具体用法?PHP _wp_json_sanity_check怎么用?PHP _wp_json_sanity_check使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_wp_json_sanity_check函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _wp_json_sanity_check
function _wp_json_sanity_check($data, $depth)
{
if ($depth < 0) {
throw new Exception('Reached depth limit');
}
if (is_array($data)) {
$output = array();
foreach ($data as $id => $el) {
// Don't forget to sanitize the ID!
if (is_string($id)) {
$clean_id = _wp_json_convert_string($id);
} else {
$clean_id = $id;
}
// Check the element type, so that we're only recursing if we really have to.
if (is_array($el) || is_object($el)) {
$output[$clean_id] = _wp_json_sanity_check($el, $depth - 1);
} elseif (is_string($el)) {
$output[$clean_id] = _wp_json_convert_string($el);
} else {
$output[$clean_id] = $el;
}
}
} elseif (is_object($data)) {
$output = new stdClass();
foreach ($data as $id => $el) {
if (is_string($id)) {
$clean_id = _wp_json_convert_string($id);
} else {
$clean_id = $id;
}
if (is_array($el) || is_object($el)) {
$output->{$clean_id} = _wp_json_sanity_check($el, $depth - 1);
} elseif (is_string($el)) {
$output->{$clean_id} = _wp_json_convert_string($el);
} else {
$output->{$clean_id} = $el;
}
}
} elseif (is_string($data)) {
return _wp_json_convert_string($data);
} else {
return $data;
}
return $output;
}
示例2: wp_json_encode
function wp_json_encode($data, $options = 0, $depth = 512)
{
/*
* json_encode() has had extra params added over the years.
* $options was added in 5.3, and $depth in 5.5.
* We need to make sure we call it with the correct arguments.
*/
if (version_compare(PHP_VERSION, '5.5', '>=')) {
$args = array($data, $options, $depth);
} elseif (version_compare(PHP_VERSION, '5.3', '>=')) {
$args = array($data, $options);
} else {
$args = array($data);
}
// Prepare the data for JSON serialization.
$data = _wp_json_prepare_data($data);
$json = @call_user_func_array('json_encode', $args);
// If json_encode() was successful, no need to do more sanity checking.
// ... unless we're in an old version of PHP, and json_encode() returned
// a string containing 'null'. Then we need to do more sanity checking.
if (false !== $json && (version_compare(PHP_VERSION, '5.5', '>=') || false === strpos($json, 'null'))) {
return $json;
}
try {
$args[0] = _wp_json_sanity_check($data, $depth);
} catch (Exception $e) {
return false;
}
return call_user_func_array('json_encode', $args);
}