用户中心减层:收敛本人入口与 panel 契约
This commit is contained in:
@@ -880,60 +880,20 @@ add_action('template_redirect', function () {
|
||||
// AJAX: 保存用户个人资料(显示名称、简介、联系方式)
|
||||
// ============================================================
|
||||
add_action('wp_ajax_aksrt_save_profile', function () {
|
||||
// 验证 nonce
|
||||
if ( ! isset( $_POST['_profile_nonce'] ) || ! wp_verify_nonce( $_POST['_profile_nonce'], 'aksrt_profile_save' ) ) {
|
||||
wp_send_json_error( array( 'message' => __( '安全验证失败,请刷新页面重试', 'aksrt' ) ) );
|
||||
if (!isset($_POST['_profile_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_profile_nonce'])), 'aksrt_profile_save')) {
|
||||
wp_send_json_error(['message' => __('安全验证失败,请刷新页面重试', 'aksrt')]);
|
||||
}
|
||||
|
||||
$user_id = isset( $_POST['user_id'] ) ? (int) $_POST['user_id'] : 0;
|
||||
if ( ! $user_id || $user_id !== get_current_user_id() ) {
|
||||
wp_send_json_error( array( 'message' => __( '无权操作', 'aksrt' ) ) );
|
||||
$user_id = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
|
||||
$result = function_exists('aksrt_save_user_center_profile')
|
||||
? aksrt_save_user_center_profile($user_id, $_POST)
|
||||
: new WP_Error('aksrt_profile_unavailable', __('保存失败', 'aksrt'));
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
wp_send_json_error(['message' => $result->get_error_message()]);
|
||||
}
|
||||
|
||||
$errors = array();
|
||||
|
||||
// === 更新昵称(display_name) ===
|
||||
if ( isset( $_POST['nickname'] ) ) {
|
||||
$name = sanitize_text_field( $_POST['nickname'] );
|
||||
if ( mb_strlen( $name ) < 1 || mb_strlen( $name ) > 50 ) {
|
||||
$errors[] = __( '昵称长度需在 1-50 字之间', 'aksrt' );
|
||||
} else {
|
||||
wp_update_user( array( 'ID' => $user_id, 'display_name' => $name ) );
|
||||
}
|
||||
}
|
||||
|
||||
// === 更新个人简介 ===
|
||||
if ( isset( $_POST['description'] ) ) {
|
||||
$desc = sanitize_textarea_field( $_POST['description'] );
|
||||
if ( mb_strlen( $desc ) > 500 ) {
|
||||
$errors[] = __( '个人简介不能超过 500 字', 'aksrt' );
|
||||
} else {
|
||||
wp_update_user( array( 'ID' => $user_id, 'description' => $desc ) );
|
||||
}
|
||||
}
|
||||
|
||||
// === 更新联系方式 ===
|
||||
$contacts = array();
|
||||
if ( isset( $_POST['contacts'] ) && is_array( $_POST['contacts'] ) ) {
|
||||
foreach ( $_POST['contacts'] as $c ) {
|
||||
$url = isset( $c['url'] ) ? esc_url_raw( $c['url'] ) : '';
|
||||
$icon = isset( $c['icon'] ) ? sanitize_key( $c['icon'] ) : '';
|
||||
$icon = preg_replace('/^(fab|fas|far)[\s-]*/', '', $icon);
|
||||
$icon = preg_replace('/^fa[\s-]*/', '', $icon);
|
||||
$icon = preg_replace('/^[\s-]+/', '', $icon);
|
||||
$icon = trim( $icon, '- ' );
|
||||
if ( ! $url || ! $icon ) continue;
|
||||
$name = aksrt_icon_key_to_name( $icon );
|
||||
$contacts[] = array( 'name' => $name, 'url' => $url, 'icon' => $icon );
|
||||
}
|
||||
}
|
||||
update_user_meta( $user_id, 'aksrt_user_contacts', $contacts );
|
||||
|
||||
if ( ! empty( $errors ) ) {
|
||||
wp_send_json_error( array( 'message' => implode( '<br>', $errors ) ) );
|
||||
}
|
||||
|
||||
wp_send_json_success( array( 'message' => __( '保存成功!', 'aksrt' ) ) );
|
||||
wp_send_json_success(['message' => __('保存成功!', 'aksrt')]);
|
||||
});
|
||||
|
||||
// ============================================================
|
||||
|
||||
Reference in New Issue
Block a user