. */ if (! defined('ABSPATH')) exit; // Define plugin version and constants define('PPC3D_VERSION', '1.4.8.9'); define('PPC3D_PLUGIN_PATH', plugin_dir_path(__FILE__)); define('PPC3D_PLUGIN_URL', plugin_dir_url(__FILE__)); // Include necessary files require_once plugin_dir_path(__FILE__) . 'includes/admin-settings.php'; require_once plugin_dir_path(__FILE__) . 'includes/stl-parser-engine.php'; require_once plugin_dir_path(__FILE__) . 'includes/onboarding.php'; require_once plugin_dir_path(__FILE__) . 'includes/admin-file-manager.php'; // Register activation hook for onboarding register_activation_hook(__FILE__, 'ppc3d_plugin_activation'); /** * Function that runs when the plugin is activated */ function ppc3d_plugin_activation() { // Check if this is a new installation if (!get_option('ppc3d_plugin_version')) { // Set the flag for first time installation update_option('ppc3d_show_onboarding', 'yes'); // Set transient for redirect set_transient('ppc3d_activation_redirect', true, 30); } // Update the plugin version update_option('ppc3d_plugin_version', PPC3D_VERSION); } // Enqueue scripts and styles function ppc3d_stl_parser_enqueue_scripts() { wp_enqueue_script('babylon', 'https://cdn.babylonjs.com/babylon.js', array(), '7.25.1', true); wp_enqueue_script('babylon-loader', 'https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js', array(), '7.25.1', true); if (!is_admin()) { wp_enqueue_style('bootstrap', plugin_dir_url(__FILE__) . 'includes/bootstrap/css/bootstrap.min.css', array(), '5.3.3', false); wp_enqueue_style('fontawesome', plugin_dir_url(__FILE__) . 'includes/font-awesome/fontawesome.min.css', array(), '6.5.2', false); wp_enqueue_style('fontawesome-all', plugin_dir_url(__FILE__) . 'includes/font-awesome/all.min.css', array(), '6.5.2', false); wp_enqueue_script('bootstrap-js', plugin_dir_url(__FILE__) . 'includes/bootstrap/js/bootstrap.min.js', array(), '5.3.3', true); $style_path = plugin_dir_path(__FILE__) . 'css/style.css'; $style_version = file_exists($style_path) ? filemtime($style_path) : false; wp_enqueue_style('stl-parser-style', plugins_url('css/style.css', __FILE__), array(), $style_version); // Enqueue modal compatibility CSS with high priority (100) $modal_css_path = plugin_dir_path(__FILE__) . 'css/modal-compatibility.css'; $modal_css_version = file_exists($modal_css_path) ? filemtime($modal_css_path) : '1.0.0'; wp_enqueue_style('modal-compatibility', plugins_url('css/modal-compatibility.css', __FILE__), array('bootstrap'), $modal_css_version); // Ensure it loads with high priority wp_style_add_data('modal-compatibility', 'priority', 100); // Enqueue our new modal fixes CSS with even higher priority (110) $modal_fixes_css_path = plugin_dir_path(__FILE__) . 'css/modal-fixes.css'; $modal_fixes_css_version = file_exists($modal_fixes_css_path) ? filemtime($modal_fixes_css_path) : '1.0.0'; wp_enqueue_style('modal-fixes', plugins_url('css/modal-fixes.css', __FILE__), array('bootstrap', 'modal-compatibility'), $modal_fixes_css_version); // Ensure it loads with highest priority wp_style_add_data('modal-fixes', 'priority', 110); $script_path = plugin_dir_path(__FILE__) . 'js/script.js'; $script_version = file_exists($script_path) ? filemtime($script_path) : false; wp_enqueue_script('stl-parser-script', plugins_url('js/script.js', __FILE__), array('jquery', 'babylon', 'babylon-loader', 'bootstrap-js'), $script_version, true); // Enqueue view transitions script $view_transitions_path = plugin_dir_path(__FILE__) . 'js/view-transitions.js'; $view_transitions_version = file_exists($view_transitions_path) ? filemtime($view_transitions_path) : false; wp_enqueue_script('view-transitions', plugins_url('js/view-transitions.js', __FILE__), array('jquery', 'stl-parser-script'), $view_transitions_version, true); // Localize the script wp_localize_script('stl-parser-script', 'ppc3d_stl_parser_3d', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('ppc3d_upload_stl_nonce'), 'cost_per_cc' => floatval(get_option('ppc3d_stl_parser_cost_per_cc', 0.05)), 'admin_email' => sanitize_email(get_option('admin_email')), )); } } add_action('wp_enqueue_scripts', 'ppc3d_stl_parser_enqueue_scripts'); function ppc3d_enqueue_custom_admin_styles() { // Enqueue main admin styles $style_path_admin = plugin_dir_path(__FILE__) . 'css/admin-style.css'; $style_version_admin = file_exists($style_path_admin) ? filemtime($style_path_admin) : false; wp_enqueue_style('custom-admin-styles', plugins_url('/css/admin-styles.css', __FILE__), array(), $style_version_admin); // Enqueue main admin scripts $script_path_admin = plugin_dir_path(__FILE__) . 'js/admin-scripts.js'; $script_version_admin = file_exists($script_path_admin) ? filemtime($script_path_admin) : false; wp_enqueue_script('stl-parser-admin-script', plugins_url('js/admin-scripts.js', __FILE__), array('jquery'), $script_version_admin, true); // Localize the admin script wp_localize_script('stl-parser-admin-script', 'ppc3d_stl_parser_admin', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('ppc3d_upload_stl_nonce'), )); // Check if onboarding should be shown $show_onboarding = get_option('ppc3d_show_onboarding', 'no'); if ($show_onboarding === 'yes') { // Enqueue onboarding styles $onboarding_style_path = plugin_dir_path(__FILE__) . 'css/onboarding.css'; $onboarding_style_version = file_exists($onboarding_style_path) ? filemtime($onboarding_style_path) : false; wp_enqueue_style('ppc3d-onboarding-styles', plugins_url('/css/onboarding.css', __FILE__), array(), $onboarding_style_version); // Enqueue onboarding scripts $onboarding_script_path = plugin_dir_path(__FILE__) . 'js/onboarding.js'; $onboarding_script_version = file_exists($onboarding_script_path) ? filemtime($onboarding_script_path) : false; wp_enqueue_script('ppc3d-onboarding-script', plugins_url('js/onboarding.js', __FILE__), array('jquery'), $onboarding_script_version, true); // Enqueue dashicons if not already loaded wp_enqueue_style('dashicons'); // Localize the onboarding script wp_localize_script('ppc3d-onboarding-script', 'ppc3d_onboarding', array( 'show_onboarding' => $show_onboarding, 'nonce' => wp_create_nonce('ppc3d_onboarding_nonce'), )); } } add_action('admin_enqueue_scripts', 'ppc3d_enqueue_custom_admin_styles'); // Define the AJAX action hooks add_action('wp_ajax_ppc3d_upload_stl_file', 'ppc3d_upload_stl_file'); add_action('wp_ajax_nopriv_ppc3d_upload_stl_file', 'ppc3d_upload_stl_file'); add_action('wp_ajax_ppc3d_show_stl_file', 'ppc3d_show_stl_file'); add_action('wp_ajax_nopriv_ppc3d_show_stl_file', 'ppc3d_show_stl_file'); add_action('wp_ajax_ppc3d_send_purchase_emails', 'ppc3d_send_purchase_emails_callback'); add_action('wp_ajax_ppc3d_complete_onboarding', 'ppc3d_complete_onboarding_callback'); /** * AJAX callback to mark onboarding as completed */ function ppc3d_complete_onboarding_callback() { // Verify nonce if (!check_ajax_referer('ppc3d_onboarding_nonce', 'nonce', false)) { wp_send_json_error('Nonce verification failed.'); wp_die(); } // Update option to mark onboarding as completed update_option('ppc3d_show_onboarding', 'no'); // Send success response wp_send_json_success('Onboarding completed successfully.'); wp_die(); } function ppc3d_allow_stl_uploads($mimes) { // Allow STL file uploads $mimes['stl'] = 'application/sla'; return $mimes; } add_filter('upload_mimes', 'ppc3d_allow_stl_uploads'); function ppc3d_handle_file_upload($file_key) { // Verify nonce if (! check_ajax_referer('ppc3d_upload_stl_nonce', 'nonce', false)) { wp_send_json_error('Nonce verification failed.'); wp_die(); } // Sanitize the file key $file_key = sanitize_key($file_key); // Verify file upload exists if (!isset($_FILES[$file_key])) { wp_send_json_error('No file uploaded.'); wp_die(); } // Use wp_handle_upload for sanitizing and handling the file upload $upload_overrides = ['test_form' => false]; // Disable test for POST form field $upload_dir = wp_upload_dir(); $upload_path = trailingslashit($upload_dir['path']); // Directory path for uploads // Get original file name and sanitize it $file_name = sanitize_file_name($_FILES[$file_key]['name']); $file_path = $upload_path . $file_name; // Check if the file already exists and delete it if (file_exists($file_path)) { unlink($file_path); // Deletes the existing file } // Upload the new file $uploaded_file = wp_handle_upload($_FILES[$file_key], $upload_overrides); // Check if the file was uploaded successfully if (isset($uploaded_file['error'])) { wp_send_json_error('File upload error: ' . $uploaded_file['error']); wp_die(); } // Verify the file extension is STL $file_extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); if ($file_extension !== 'stl') { // Delete the file if it's not an STL wp_delete_file($uploaded_file['file']); wp_send_json_error('Invalid file type. Please upload an STL file.'); wp_die(); } // Return the file details return [ 'file_name' => $file_name, 'root_url' => trailingslashit($upload_dir['url']), 'site_upload_path' => esc_url_raw($uploaded_file['url']), 'upload_path' => $uploaded_file['file'], ]; } // Function to handle STL file upload function ppc3d_upload_stl_file() { // Verify nonce if (! check_ajax_referer('ppc3d_upload_stl_nonce', 'nonce', false)) { wp_send_json_error('Nonce verification failed.'); wp_die(); } $file_data = ppc3d_handle_file_upload('stl_file'); // Store file metadata for the file manager ppc3d_store_file_metadata($file_data); // Check if necessary $_POST variables are set $printing_technology = isset($_POST['printing_technology']) ? floatval($_POST['printing_technology']) : 0; $material = isset($_POST['material']) ? floatval($_POST['material']) : 0; $quality = isset($_POST['quality']) ? floatval($_POST['quality']) : 0; $quantity = isset($_POST['quantity']) ? intval($_POST['quantity']) : 0; $infill = isset($_POST['infill']) ? floatval($_POST['infill']) : 0; $infill_pct = isset($_POST['infill_percentage']) ? floatval($_POST['infill_percentage']) : floatval($infill); $color = isset($_POST['color']) ? floatval($_POST['color']) : 0; $unit = isset($_POST['unit']) ? sanitize_text_field(wp_unslash($_POST['unit'])) : ''; $scale = isset($_POST['scale']) ? floatval($_POST['scale']) : 0; $cost_per_cc = isset($_POST['cost_per_cc']) ? (floatval($_POST['cost_per_cc']) ?: 0.05) : (floatval(get_option('ppc3d_stl_parser_cost_per_cc')) ?: 0.05); if ($unit == 'inch') { $converted_scale = $scale / 200; } else { $converted_scale = $scale / 100; } $scale = $converted_scale; try { $parsed_data = ppc3d_parse_stl_file($file_data['upload_path'], $unit, $scale); } catch (Exception $e) { error_log('STL parse error: ' . $e->getMessage()); wp_send_json_error('Unable to parse STL file.'); return; } if (empty($parsed_data['vertices']) || empty($parsed_data['faces'])) { wp_send_json_error('Parsed STL file did not contain recognizable geometry.'); return; } if ($scale >= 0.50 && $scale < 1) { $concluder_price = 0.05; } elseif ($scale < 0.50) { $concluder_price = 0.5; } else { $concluder_price = 0.005; } $total_volume = ppc3d_calculate_stl_volume($parsed_data, $unit, $scale, PPC3D_LAYER_HEIGHT_MM, PPC3D_PRINTER_SPEED_MM_PER_S); $printing_time_hours = ppc3d_calculate_printing_time($parsed_data, PPC3D_LAYER_HEIGHT_MM, PPC3D_PRINTER_SPEED_MM_PER_S); $total_grams = ppc3d_calculate_total_grams($parsed_data); // Use infill percentage (sent as infill_percentage) to compute material volume $material_volume = $total_volume * ($infill_pct / 100); $support_material_volume = $total_volume * (PPC3D_SUPPORT_MATERIAL_PERCENTAGE / 100); $x_coordinates = array_column($parsed_data['vertices'], 1); $y_coordinates = array_column($parsed_data['vertices'], 2); $z_coordinates = array_column($parsed_data['vertices'], 3); $min_x = min($x_coordinates); $max_x = max($x_coordinates); $min_y = min($y_coordinates); $max_y = max($y_coordinates); $min_z = min($z_coordinates); $max_z = max($z_coordinates); $box_length = $max_x - $min_x; $box_width = $max_y - $min_y; $box_height = $max_z - $min_z; $box_volume_mm3 = $box_length * $box_width * $box_height; $box_volume = round($box_volume_mm3 / 1000, 2); $surface_area_total = 0; foreach ($parsed_data['faces'] as $faceVertices) { $v0 = $parsed_data['vertices'][$faceVertices[0]]; $v1 = $parsed_data['vertices'][$faceVertices[1]]; $v2 = $parsed_data['vertices'][$faceVertices[2]]; $surface_area_total += ppc3d_calculate_triangle_surface_area($v0, $v1, $v2); } $surface_area_cm2 = round($surface_area_total / 100, 2); $model_length = round(($max_x - $min_x) / 10, 2); $model_width = round(($max_y - $min_y) / 10, 2); $model_height = round(($max_z - $min_z) / 10, 2); if ($printing_time_hours <= 0.9) { $printing_time_output = ceil($printing_time_hours * 60) . ' minutes'; } elseif ($printing_time_hours == 1) { $printing_time_output = '1 hour'; } else { $printing_time_output = round($printing_time_hours) . ' hours'; } // Add infill and color as fees (admin UI labels them as 'Price Per CC') $production_cost = (($material_volume * $cost_per_cc) + $material + $printing_technology + $quality + $infill + $color) * $quantity; $data = [ 'data' => [ 'formated_data' => [ 'price' => '$' . number_format($production_cost, 2), 'total_grams' => number_format($total_grams, 2), 'material_volume' => number_format($material_volume, 2) . ' cm³', 'support_material' => number_format($support_material_volume, 2) . ' cm³', 'box_volume' => number_format($box_volume, 2) . ' cm³', 'total_surface_area' => number_format($surface_area_cm2, 2) . ' square cm', 'model_dimensions' => $model_length . ' cm x ' . $model_width . ' cm x ' . $model_height . ' cm', 'number_of_polygons' => count($parsed_data['faces']), 'print_time' => $printing_time_output, ], ], ]; // Generate a unique transient key using wp_generate_uuid4 $transient_key = sanitize_key('ppc3d_data_' . wp_generate_uuid4()); set_transient('ppc3d_transient_key', $transient_key, DAY_IN_SECONDS); $printing_technology_name = isset($_POST['printing_technology_name']) ? sanitize_text_field(wp_unslash($_POST['printing_technology_name'])) : ''; // Sanitize as string $material_name = isset($_POST['material_name']) ? sanitize_text_field(wp_unslash($_POST['material_name'])) : ''; // Sanitize as string $quality_name = isset($_POST['quality_name']) ? sanitize_text_field(wp_unslash($_POST['quality_name'])) : ''; // Sanitize as string $infill_name = isset($_POST['infill_name']) ? sanitize_text_field(wp_unslash($_POST['infill_name'])) : ''; // Sanitize as string $color_name = isset($_POST['color_name']) ? sanitize_text_field(wp_unslash($_POST['color_name'])) : ''; // Sanitize as string $data['data']['rootUrl'] = $file_data['root_url']; $data['data']['url'] = $file_data['site_upload_path']; $data['data']['fileName'] = $file_data['file_name']; // Based on the expected API response format: // { // "status": 200, // "data": { // "formated_data": { // "price": "$61.61", // "total_grams": "3.92", // "material_volume": "28.28 cm³", // ... // } // }, // "message": "Data Retrieved" // } // Make sure we have the expected structure if (!isset($data['data']['formated_data']) || !is_array($data['data']['formated_data'])) { error_log('Expected formated_data structure not found, creating it'); // Initialize the structure if it doesn't exist if (!isset($data['data'])) { $data['data'] = []; } if (!isset($data['data']['formated_data'])) { $data['data']['formated_data'] = []; } // Define the fields we expect to have $calculation_fields = [ 'price', 'material_volume', 'support_material', 'box_volume', 'total_surface_area', 'total_grams', 'model_dimensions', 'number_of_polygons', 'print_time' ]; // Try to find calculation data in various possible locations foreach ($calculation_fields as $field) { // Check in data.calculation if (isset($data['data']['calculation'][$field])) { $data['data']['formated_data'][$field] = $data['data']['calculation'][$field]; } // Check directly in data elseif (isset($data['data'][$field])) { $data['data']['formated_data'][$field] = $data['data'][$field]; } // Check in root calculation elseif (isset($data['calculation'][$field])) { $data['data']['formated_data'][$field] = $data['calculation'][$field]; } } // If we still don't have price, calculate it from material volume if (!isset($data['data']['formated_data']['price']) && isset($data['data']['formated_data']['material_volume'])) { $cost_per_cc = get_option('ppc3d_stl_parser_cost_per_cc', 0.05); $material_volume = floatval(preg_replace('/[^0-9.]/', '', $data['data']['formated_data']['material_volume'])); $price = $material_volume * $cost_per_cc; // Format with currency $currency_symbol = get_option('ppc3d_currency_symbol', '$'); $currency_position = get_option('ppc3d_currency_position', 'before'); if ($currency_position === 'before') { $data['data']['formated_data']['price'] = $currency_symbol . number_format($price, 2); } else { $data['data']['formated_data']['price'] = number_format($price, 2) . ' ' . $currency_symbol; } } } else { error_log('Found expected formated_data structure'); } // Ensure all values have proper formatting if (isset($data['data']['formated_data'])) { // Allow only valid currency symbols in the price string $currency_symbol = get_option('ppc3d_currency_symbol', '$'); $currency_position = get_option('ppc3d_currency_position', 'before'); $allowed_symbols = preg_quote($currency_symbol, '/') . '$£€¥₹₩'; // Make sure price has currency symbol if (isset($data['data']['formated_data']['price']) && !preg_match('/[^0-9.,\s' . $allowed_symbols . ']/u', $data['data']['formated_data']['price'])) { $price_value = floatval(preg_replace('/[^0-9.]/', '', $data['data']['formated_data']['price'])); if ($currency_position === 'before') { $data['data']['formated_data']['price'] = $currency_symbol . number_format($price_value, 2); } else { $data['data']['formated_data']['price'] = number_format($price_value, 2) . ' ' . $currency_symbol; } } // Make sure material volume has units if (isset($data['data']['formated_data']['material_volume']) && !strpos($data['data']['formated_data']['material_volume'], 'cm')) { $data['data']['formated_data']['material_volume'] = $data['data']['formated_data']['material_volume'] . ' cm³'; } // Make sure support material has units if (isset($data['data']['formated_data']['support_material']) && !strpos($data['data']['formated_data']['support_material'], 'cm')) { $data['data']['formated_data']['support_material'] = $data['data']['formated_data']['support_material'] . ' cm³'; } // Make sure box volume has units if (isset($data['data']['formated_data']['box_volume']) && !strpos($data['data']['formated_data']['box_volume'], 'cm')) { $data['data']['formated_data']['box_volume'] = $data['data']['formated_data']['box_volume'] . ' cm³'; } // Make sure total surface area has units if (isset($data['data']['formated_data']['total_surface_area']) && !strpos($data['data']['formated_data']['total_surface_area'], 'square')) { $data['data']['formated_data']['total_surface_area'] = $data['data']['formated_data']['total_surface_area'] . ' square cm'; } // Make sure total grams is formatted if (isset($data['data']['formated_data']['total_grams']) && is_numeric($data['data']['formated_data']['total_grams'])) { $data['data']['formated_data']['total_grams'] = number_format((float)$data['data']['formated_data']['total_grams'], 2); } } $data['data']['formated_data']['fileName'] = $file_data['file_name']; // Add the form data to the response (NAME) $data['data']['selectedFormDataNames'] = [ 'printing_technology_name' => $printing_technology_name, 'material_name' => $material_name, 'quality_name' => $quality_name, 'infill_name' => $infill_name, 'color_name' => $color_name, 'quantity' => $quantity, ]; $data['data']['formated_data']['transientKey'] = $transient_key; // Save formatted data in transient set_transient($transient_key, $data['data'], DAY_IN_SECONDS); // Return the transient key along with the data wp_send_json_success($data['data']); wp_die(); } // Add shortcode for the file upload form function ppc3d_stl_parser_upload_form_shortcode() { ob_start(); include(plugin_dir_path(__FILE__) . 'templates/upload-form.php'); return ob_get_clean(); } add_shortcode('ppc3d_stl_upload_form', 'ppc3d_stl_parser_upload_form_shortcode'); /** * Store metadata about uploaded STL files * * @param array $file_data File data from upload handler */ function ppc3d_store_file_metadata($file_data) { if (empty($file_data) || !isset($file_data['file_name']) || !isset($file_data['upload_path'])) { return; } // Get current user information $current_user = wp_get_current_user(); $user_id = $current_user->ID; $user_name = $current_user->display_name; if (empty($user_name)) { $user_name = 'Guest'; } // Get existing file records or create new array $uploaded_files = get_option('ppc3d_uploaded_files', array()); // Add new file record with metadata $uploaded_files[] = array( 'file_name' => $file_data['file_name'], 'file_path' => $file_data['upload_path'], 'file_url' => $file_data['site_upload_path'], 'upload_date' => current_time('mysql'), 'user_id' => $user_id, 'user_name' => $user_name, 'file_size' => filesize($file_data['upload_path']) ); // Store the updated records update_option('ppc3d_uploaded_files', $uploaded_files); } function ppc3d_dynamic_email_config($phpmailer) { // Check if WP Mail SMTP plugin is active and get its SMTP settings if (class_exists('WP_Mail_Smtp\Pro\SMTP') && method_exists('WP_Mail_Smtp\Pro\SMTP', 'get_smtp_options')) { $smtp_options = \WP_Mail_Smtp\Pro\SMTP::get_smtp_options(); // Use SMTP for sending emails $phpmailer->isSMTP(); $phpmailer->Host = $smtp_options['smtp_host']; $phpmailer->SMTPAuth = true; $phpmailer->Port = $smtp_options['smtp_port']; $phpmailer->SMTPSecure = $smtp_options['smtp_encryption']; $phpmailer->Username = $smtp_options['smtp_user']; $phpmailer->Password = $smtp_options['smtp_pass']; $phpmailer->setFrom($smtp_options['from_email'], $smtp_options['from_name']); } else { // Fallback to WordPress default email setup $phpmailer->isMail(); // Use PHP's mail() function $phpmailer->setFrom( get_option('admin_email'), // Use WordPress admin email get_bloginfo('name') // Use the site name as the "From" name ); } } add_action('phpmailer_init', 'ppc3d_dynamic_email_config'); // Register REST API routes function ppc3d_register_rest_routes() { register_rest_route('custom/v1', '/form_submission', array( 'methods' => 'POST', 'callback' => 'ppc3d_handle_custom_form_submission', 'permission_callback' => '__return_true', )); register_rest_route('custom/v1', '/get_transient_data', array( 'methods' => 'GET', 'callback' => 'ppc3d_get_transient_data', 'permission_callback' => '__return_true', )); } add_action('rest_api_init', 'ppc3d_register_rest_routes'); // Function to retrieve transient data function ppc3d_get_transient_data($request) { // Get the transient key from the request $transient_key = sanitize_text_field($request->get_param('transient_key')); if (empty($transient_key)) { return new WP_REST_Response(array( 'success' => false, 'message' => 'Transient key is required', ), 400); } // Get the transient data $transient_data = get_transient($transient_key); if (false === $transient_data) { return new WP_REST_Response(array( 'success' => false, 'message' => 'Transient data not found or expired', ), 404); } // Log the transient data for debugging error_log('Transient key: ' . $transient_key); error_log('Formatted data: ' . print_r($transient_data, true)); return new WP_REST_Response(array( 'success' => true, 'data' => $transient_data, ), 200); } function ppc3d_handle_custom_form_submission(WP_REST_Request $request) { try { // Extract and sanitize parameters $fullName = sanitize_text_field($request->get_param('fullName')); $customerEmail = sanitize_email($request->get_param('email')); $shippingAddress = sanitize_text_field($request->get_param('shippingAddress')); $transient_key = sanitize_text_field($request->get_param('transient_key_field')); $admin_email = get_option('admin_email'); // WordPress admin email // Retrieve formatted data from the transient using the transient key $formatted_data = get_transient($transient_key); // Debug log to check what's in the transient error_log('Transient key: ' . $transient_key); error_log('Formatted data: ' . print_r($formatted_data, true)); // Generate dynamic order number using timestamp and a random number $orderNumber = 'ORD-' . time() . '-' . rand(1000, 9999); // Customer email subject $subject_customer = "Order Confirmation - Order Code: $orderNumber"; // Admin email subject $subject_admin = "New Order Received - $fullName - Order $orderNumber"; // Load the email template from admin settings $use_default = get_option('use_default_email_template', 'no'); if ($use_default === 'yes') { $template = '
Order Code: {order_number}
Email: {email}
Shipping Address:
{shipping_address}
Contact us at support@example.com
') === false) { $template = wpautop($template); } } // Fallback template if nothing is saved if (empty($template)) { $template = '
Thank you for your order!
Name: {full_name}
Email: {email}
Shipping Address: {shipping_address}
Order Code: {order_number}
{order_details} {options_details}Need help? Contact us at support@example.com
'; } // Build order and option sections as HTML based on the expected API response format $orderDetails = "