✏️ 正在编辑: wc-dashboard-saver.php
路径:
/home/h359620/public_html/wp-content/themes/datis/inc/includes/woocommerce/wc-dashboard-saver.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php if (! defined('ABSPATH')) { exit; } if (! class_exists('Datis_WC_MyAccount_Dashboard_Saver')) { class Datis_WC_MyAccount_Dashboard_Saver { private static $instance = null; private $text_domain = 'datis'; private function __construct() { add_action( 'template_redirect', array( $this, 'maybe_handle_submit' ), 20 ); } public static function get_instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } public function maybe_handle_submit() { if (! function_exists('is_user_logged_in') || ! is_user_logged_in()) { return; } if (! function_exists('is_account_page') || ! is_account_page()) { return; } // We only handle our form. if (empty($_POST['datis_myaccount_dashboard_action']) || $_POST['datis_myaccount_dashboard_action'] !== 'save') { return; } if (empty($_POST['datis_myaccount_dashboard_nonce']) || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['datis_myaccount_dashboard_nonce'])), 'datis_myaccount_dashboard_save')) { if (function_exists('wc_add_notice')) { wc_add_notice(esc_html__('Security check failed.', $this->text_domain), 'error'); } return; } $user_id = get_current_user_id(); /** * Allow disabling saver via filter. */ $enabled = apply_filters('datis_myaccount_dashboard_saver_enabled', true, $user_id); if (! $enabled) { return; } $result = $this->save_fields($user_id); /** * Fires after attempting save. */ do_action('datis_myaccount_dashboard_after_save', $user_id, $result); // Redirect to avoid resubmission. if (! headers_sent()) { $redirect = apply_filters( 'datis_myaccount_dashboard_after_save_redirect', (function_exists('wc_get_account_endpoint_url') ? wc_get_account_endpoint_url('dashboard') : wc_get_page_permalink('myaccount')), $user_id, $result ); wp_safe_redirect($redirect); exit; } } private function save_fields($user_id) { $errors = array(); $updated = false; // Field map: POST key => callback updater $map = apply_filters( 'datis_myaccount_dashboard_field_map', array( 'datis_full_name' => array($this, 'update_full_name'), 'datis_phone' => array($this, 'update_billing_phone'), 'datis_email' => array($this, 'update_email'), 'datis_state' => array($this, 'update_billing_state'), 'datis_city' => array($this, 'update_billing_city'), 'datis_postcode' => array($this, 'update_billing_postcode'), ), $user_id ); foreach ($map as $post_key => $updater) { if (! isset($_POST[$post_key])) { continue; } $raw = wp_unslash($_POST[$post_key]); $val = is_string($raw) ? trim($raw) : ''; // IMPORTANT: ignore empty. if ($val === '') { continue; } try { $ok = call_user_func($updater, $user_id, $val); if ($ok) { $updated = true; } } catch (\Throwable $e) { $errors[] = $e->getMessage(); } } // Password change (only if all three are provided) $pwd_current = isset($_POST['datis_current_password']) ? (string) wp_unslash($_POST['datis_current_password']) : ''; $pwd_new = isset($_POST['datis_new_password']) ? (string) wp_unslash($_POST['datis_new_password']) : ''; $pwd_confirm = isset($_POST['datis_confirm_password']) ? (string) wp_unslash($_POST['datis_confirm_password']) : ''; $pwd_current = trim($pwd_current); $pwd_new = trim($pwd_new); $pwd_confirm = trim($pwd_confirm); // Ignore password section if any is empty. if ($pwd_current !== '' || $pwd_new !== '' || $pwd_confirm !== '') { if ($pwd_current === '' || $pwd_new === '' || $pwd_confirm === '') { $errors[] = esc_html__('To change your password, fill in all password fields.', $this->text_domain); } elseif ($pwd_new !== $pwd_confirm) { $errors[] = esc_html__('New password and confirmation do not match.', $this->text_domain); } elseif (strlen($pwd_new) < 8) { $errors[] = esc_html__('Password must be at least 8 characters.', $this->text_domain); } else { $ok = $this->update_password($user_id, $pwd_current, $pwd_new); if ($ok) { $updated = true; } else { $errors[] = esc_html__('Current password is incorrect.', $this->text_domain); } } } // Notices if (function_exists('wc_add_notice')) { if (! empty($errors)) { foreach ($errors as $msg) { wc_add_notice(wp_strip_all_tags((string) $msg), 'error'); } } if ($updated && empty($errors)) { wc_add_notice(esc_html__('Your details have been updated.', $this->text_domain), 'success'); } elseif (! $updated && empty($errors)) { wc_add_notice(esc_html__('Nothing to update.', $this->text_domain), 'notice'); } } return array( 'updated' => $updated, 'errors' => $errors, ); } private function update_full_name($user_id, $full_name) { $full_name = sanitize_text_field($full_name); if ($full_name === '') { return false; } // Optional: split to first/last (simple split) $parts = preg_split('/\s+/', $full_name); $first = isset($parts[0]) ? $parts[0] : ''; $last = ''; if (count($parts) > 1) { $last = implode(' ', array_slice($parts, 1)); } $updates = array('ID' => (int) $user_id); // Allow control via filter. $use_wp_names = apply_filters('datis_myaccount_dashboard_update_wp_names', true, $user_id, $full_name); if ($use_wp_names) { $updates['first_name'] = $first; $updates['last_name'] = $last; $updates['display_name'] = $full_name; } $r = wp_update_user($updates); if (is_wp_error($r)) { throw new \Exception($r->get_error_message()); } // Keep billing names in sync too (optional). $sync_billing = apply_filters('datis_myaccount_dashboard_sync_billing_names', true, $user_id); if ($sync_billing) { update_user_meta($user_id, 'billing_first_name', $first); update_user_meta($user_id, 'billing_last_name', $last); } return true; } private function update_email($user_id, $email) { $email = sanitize_email($email); if ($email === '') { return false; } if (! is_email($email)) { throw new \Exception(esc_html__('Please enter a valid email address.', $this->text_domain)); } $r = wp_update_user( array( 'ID' => (int) $user_id, 'user_email' => $email, ) ); if (is_wp_error($r)) { throw new \Exception($r->get_error_message()); } return true; } private function update_billing_phone($user_id, $phone) { $phone = sanitize_text_field($phone); if ($phone === '') { return false; } update_user_meta($user_id, 'billing_phone', $phone); return true; } private function update_billing_state($user_id, $state) { $state = sanitize_text_field($state); if ($state === '') { return false; } update_user_meta($user_id, 'billing_state', $state); return true; } private function update_billing_city($user_id, $city) { $city = sanitize_text_field($city); if ($city === '') { return false; } update_user_meta($user_id, 'billing_city', $city); return true; } private function update_billing_postcode($user_id, $postcode) { $postcode = sanitize_text_field($postcode); if ($postcode === '') { return false; } update_user_meta($user_id, 'billing_postcode', $postcode); return true; } private function update_password($user_id, $current_password, $new_password) { $user = get_user_by('id', $user_id); if (! $user) { return false; } // Check current password if (! wp_check_password($current_password, $user->user_pass, $user_id)) { return false; } // Set new password wp_set_password($new_password, $user_id); // Keep user logged in after password change wp_set_current_user($user_id); wp_set_auth_cookie($user_id, true); return true; } } Datis_WC_MyAccount_Dashboard_Saver::get_instance(); }
💾 保存文件
← 返回文件管理器