✏️ 正在编辑: request-ajax.php
路径:
/home/h359620/public_html/wp-content/themes/datis/inc/includes/consultation/request-ajax.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php if (!defined('ABSPATH')) exit; if (!class_exists('Datis_Consultation_Request_Ajax')) { class Datis_Consultation_Request_Ajax { /** @var self|null */ private static $instance = null; /** Nonce action used in wp_localize_script / widget */ private $nonce_action = 'datis_consult_request_nonce'; /** Consultation Request CPT (NEW - separate from booking/schedule) */ private $request_cpt = 'consultation_request'; /** Optional debug (set true temporarily for troubleshooting) */ private $debug = false; private function __construct() { $this->register_hooks(); } /** * Register AJAX hooks (logged-in only). */ private function register_hooks() { add_action('wp_ajax_datis_submit_consult_request', array($this, 'ajax_create_request')); // IMPORTANT: no nopriv hook, guests must not submit } /** * Verify nonce for all requests. */ private function verify_request() { if (!isset($_POST['nonce'])) { wp_send_json_error(array('message' => __('Missing nonce.', 'datis')), 403); } $nonce = sanitize_text_field(wp_unslash($_POST['nonce'])); if (!wp_verify_nonce($nonce, $this->nonce_action)) { wp_send_json_error(array('message' => __('Invalid nonce.', 'datis')), 403); } } /** * Ensure user is logged in. */ private function verify_logged_in() { if (!is_user_logged_in()) { wp_send_json_error(array( 'message' => __('You must be logged in to submit a consultation request.', 'datis'), ), 401); } } /** * Create consultation request (CPT) for current user. * Expected POST fields: * - full_name, email, phone, project_type, project_location, project_stage, message */ public function ajax_create_request() { $this->verify_request(); $this->verify_logged_in(); $current_user_id = get_current_user_id(); // Sanitize inputs $full_name = isset($_POST['full_name']) ? sanitize_text_field(wp_unslash($_POST['full_name'])) : ''; $email = isset($_POST['email']) ? sanitize_email(wp_unslash($_POST['email'])) : ''; $phone = isset($_POST['phone']) ? sanitize_text_field(wp_unslash($_POST['phone'])) : ''; $project_type = isset($_POST['project_type']) ? sanitize_text_field(wp_unslash($_POST['project_type'])) : ''; $project_location = isset($_POST['project_location']) ? sanitize_text_field(wp_unslash($_POST['project_location'])) : ''; $project_stage = isset($_POST['project_stage']) ? sanitize_text_field(wp_unslash($_POST['project_stage'])) : ''; $message = isset($_POST['message']) ? sanitize_textarea_field(wp_unslash($_POST['message'])) : ''; // Minimal validation (you can tighten later) if (empty($full_name) || empty($email) || empty($phone)) { wp_send_json_error(array( 'message' => __('Missing required fields.', 'datis'), ), 400); } if (!is_email($email)) { wp_send_json_error(array( 'message' => __('Invalid email address.', 'datis'), ), 400); } // Create post $title = $full_name; if (!empty($project_type)) { $title = $full_name . ' - ' . $project_type; } $post_id = wp_insert_post(array( 'post_type' => $this->request_cpt, 'post_status' => 'pending', 'post_title' => $title, 'post_content' => $message, 'post_author' => $current_user_id, ), true); if (is_wp_error($post_id) || !$post_id) { wp_send_json_error(array( 'message' => __('Could not create consultation request.', 'datis'), ), 500); } // Save meta (consistent naming) update_post_meta($post_id, '_datis_cr_full_name', $full_name); update_post_meta($post_id, '_datis_cr_email', $email); update_post_meta($post_id, '_datis_cr_phone', $phone); update_post_meta($post_id, '_datis_cr_project_type', $project_type); update_post_meta($post_id, '_datis_cr_project_location', $project_location); update_post_meta($post_id, '_datis_cr_project_stage', $project_stage); update_post_meta($post_id, '_datis_cr_message', $message); update_post_meta($post_id, '_datis_cr_user_id', $current_user_id); // optional (redundant but handy) wp_send_json_success(array( 'request_id' => (int) $post_id, 'message' => __('Request created.', 'datis'), )); } /** * Singleton instance. */ public static function get_instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } } // Boot Datis_Consultation_Request_Ajax::get_instance(); }
💾 保存文件
← 返回文件管理器