_wp_die_process_input()
云策文档标注
概述
_wp_die_process_input() 是 WordPress 内部函数,用于处理传递给 wp_die() 的参数,确保其处理器能一致地处理错误消息、标题和行为参数。它标准化输入,包括处理 WP_Error 对象和设置默认值。
关键要点
- 函数处理三个参数:$message(必需,错误消息或 WP_Error 对象)、$title(可选,错误标题)、$args(可选,控制行为的参数数组)。
- 返回一个数组,包含处理后的消息、标题和参数,格式为 [消息, 标题, 参数数组]。
- 当 $message 是 WP_Error 对象时,函数会提取错误信息、代码和响应状态,并设置到 $args 中。
- 提供默认参数值,如 response 默认为 500,code 默认为 'wp_die',text_direction 基于语言方向设置。
- 与多个 wp_die 处理器(如 _default_wp_die_handler、_ajax_wp_die_handler)关联使用,确保错误处理的一致性。
代码示例
function _wp_die_process_input( $message, $title = '', $args = array() ) {
$defaults = array(
'response' => 0,
'code' => '',
'exit' => true,
'back_link' => false,
'link_url' => '',
'link_text' => '',
'text_direction' => '',
'charset' => 'utf-8',
'additional_errors' => array(),
);
$args = wp_parse_args( $args, $defaults );
if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
// 处理 WP_Error 对象的逻辑
}
// 设置默认值和其他处理
return array( $message, $title, $args );
}注意事项
- 此函数是内部函数,通常不应直接调用,而是通过 wp_die() 使用。
- 参数 $args 支持自定义行为,如设置响应代码、退出标志和附加错误信息。
- 在 WordPress 5.1.0 版本中引入,用于统一错误处理流程。
原文内容
Processes arguments passed to wp_die() consistently for its handlers.
Parameters
Source
function _wp_die_process_input( $message, $title = '', $args = array() ) {
$defaults = array(
'response' => 0,
'code' => '',
'exit' => true,
'back_link' => false,
'link_url' => '',
'link_text' => '',
'text_direction' => '',
'charset' => 'utf-8',
'additional_errors' => array(),
);
$args = wp_parse_args( $args, $defaults );
if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
if ( ! empty( $message->errors ) ) {
$errors = array();
foreach ( (array) $message->errors as $error_code => $error_messages ) {
foreach ( (array) $error_messages as $error_message ) {
$errors[] = array(
'code' => $error_code,
'message' => $error_message,
'data' => $message->get_error_data( $error_code ),
);
}
}
$message = $errors[0]['message'];
if ( empty( $args['code'] ) ) {
$args['code'] = $errors[0]['code'];
}
if ( empty( $args['response'] ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['status'] ) ) {
$args['response'] = $errors[0]['data']['status'];
}
if ( empty( $title ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['title'] ) ) {
$title = $errors[0]['data']['title'];
}
if ( WP_DEBUG_DISPLAY && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['error'] ) ) {
$args['error_data'] = $errors[0]['data']['error'];
}
unset( $errors[0] );
$args['additional_errors'] = array_values( $errors );
} else {
$message = '';
}
}
$have_gettext = function_exists( '__' );
// The $title and these specific $args must always have a non-empty value.
if ( empty( $args['code'] ) ) {
$args['code'] = 'wp_die';
}
if ( empty( $args['response'] ) ) {
$args['response'] = 500;
}
if ( empty( $title ) ) {
$title = $have_gettext ? __( 'WordPress › Error' ) : 'WordPress › Error';
}
if ( empty( $args['text_direction'] ) || ! in_array( $args['text_direction'], array( 'ltr', 'rtl' ), true ) ) {
$args['text_direction'] = 'ltr';
if ( function_exists( 'is_rtl' ) && is_rtl() ) {
$args['text_direction'] = 'rtl';
}
}
if ( ! empty( $args['charset'] ) ) {
$args['charset'] = _canonical_charset( $args['charset'] );
}
return array( $message, $title, $args );
}
Changelog
| Version | Description |
|---|---|
| 5.1.0 | Introduced. |