$value) { $text = str_replace('{' . $key . '}', (string) $value, $text); } return $text; } private function emails($value) { $emails = array(); foreach (preg_split('/[,;\n]+/', (string) $value) as $email) { $email = sanitize_email(trim($email)); if (is_email($email)) { $emails[] = $email; } } return array_unique($emails); } public function submit() { check_admin_referer(NET360RECLAMACIONES_PUBLIC_PREFIX . '_claim_submit', NET360RECLAMACIONES_PUBLIC_PREFIX . '_claim_nonce'); $redirect = wp_get_referer() ? wp_get_referer() : home_url('/'); $full = $this->post_text('full_name'); $doc_type = $this->post_text('document_type'); $doc_num = $this->post_text('document_number'); $email = sanitize_email($this->post_text('email')); $goods_type = $this->post_text('goods_type'); $goods_desc = $this->post_textarea('goods_description'); $claim_type = $this->post_text('claim_type'); $claim_desc = $this->post_textarea('claim_description'); $request = $this->post_textarea('request_description'); if (!$full || !$doc_type || !$doc_num || !is_email($email) || !$goods_type || !$goods_desc || !$claim_type || !$claim_desc || !$request) { wp_safe_redirect(add_query_arg(NET360RECLAMACIONES_PUBLIC_PREFIX . '_error', '1', $redirect)); exit; } $minor = isset($_POST['is_minor']) ? 1 : 0; $amount_enabled = isset($_POST['claim_amount_enabled']) ? 1 : 0; $guardian_name = $this->post_text('guardian_name'); $guardian_document_type = $this->post_text('guardian_document_type'); $guardian_document_number = $this->post_text('guardian_document_number'); $currency = $this->post_text('currency'); $amount = $this->post_text('amount'); if ($minor && (!$guardian_name || !$guardian_document_type || !$guardian_document_number)) { wp_safe_redirect(add_query_arg(NET360RECLAMACIONES_PUBLIC_PREFIX . '_error', '1', $redirect)); exit; } if ($amount_enabled && (!$currency || (float) str_replace(',', '.', $amount) <= 0)) { wp_safe_redirect(add_query_arg(NET360RECLAMACIONES_PUBLIC_PREFIX . '_error', '1', $redirect)); exit; } global $wpdb; $code = NET360RECLAMACIONES_Database::next_code(); $data = array( 'claim_code' => $code, 'created_at' => current_time('mysql'), 'status' => 'new', 'full_name' => $full, 'is_minor' => $minor, 'guardian_name' => $minor ? $guardian_name : null, 'guardian_document_type' => $minor ? $guardian_document_type : null, 'guardian_document_number' => $minor ? $guardian_document_number : null, 'document_type' => $doc_type, 'document_number' => $doc_num, 'email' => $email, 'phone' => $this->post_text('phone'), 'address' => $this->post_textarea('address'), 'goods_type' => $goods_type, 'goods_description' => $goods_desc, 'claim_amount_enabled' => $amount_enabled, 'currency' => $amount_enabled ? $currency : null, 'amount' => $amount_enabled ? (float) str_replace(',', '.', $amount) : null, 'claim_type' => $claim_type, 'claim_description' => $claim_desc, 'request_description' => $request, ); if (false === $wpdb->insert(NET360RECLAMACIONES_Database::table(), $data)) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table insert via wpdb. wp_safe_redirect(add_query_arg(NET360RECLAMACIONES_PUBLIC_PREFIX . '_error', '1', $redirect)); exit; } NET360RECLAMACIONES_History::add((int) $wpdb->insert_id, 'created', 'Reclamación registrada desde el formulario público.', null); $settings = get_option(NET360RECLAMACIONES_PUBLIC_PREFIX . '_settings', array()); $tokens = array( 'claim_code' => $code, 'full_name' => $full, 'email' => $email, 'document_type' => $doc_type, 'document_number' => $doc_num, 'phone' => $data['phone'], 'goods_type' => $goods_type, 'goods_description' => $goods_desc, 'claim_type' => $claim_type, 'claim_description' => $claim_desc, 'request_description' => $request, 'amount' => $data['amount'], 'currency' => $data['currency'], 'site_name' => get_bloginfo('name'), ); $headers = array('Content-Type: text/plain; charset=UTF-8'); $from_name = !empty($settings['from_name']) ? sanitize_text_field($settings['from_name']) : ''; $from_email = !empty($settings['from_email']) ? sanitize_email($settings['from_email']) : ''; if ($from_name && is_email($from_email)) { $headers[] = 'From: ' . $from_name . ' <' . $from_email . '>'; } foreach ($this->emails($settings['admin_cc'] ?? '') as $cc) { $headers[] = 'Cc: ' . $cc; } foreach ($this->emails($settings['admin_bcc'] ?? '') as $bcc) { $headers[] = 'Bcc: ' . $bcc; } $admin = !empty($settings['admin_email']) ? sanitize_email($settings['admin_email']) : get_option('admin_email'); $subject = $this->tokens($settings['admin_subject'] ?? 'Nueva reclamación {claim_code}', $tokens); $body = "Se ha recibido una nueva reclamación.\n\nCódigo: {$code}\nConsumidor: {$full}\nCorreo: {$email}\nTipo: {$claim_type}\nEstado: Nueva\n\nReclamación:\n{$claim_desc}\n\nPedido:\n{$request}"; if (is_email($admin)) { wp_mail($admin, $subject, $body, $headers); } $customer_subject = $this->tokens($settings['customer_subject'] ?? 'Hemos recibido su reclamación {claim_code}', $tokens); $customer_body = $this->tokens($settings['customer_message'] ?? "Estimado/a {full_name},\n\nHemos recibido correctamente su reclamación.\nCódigo: {claim_code}", $tokens); wp_mail($email, $customer_subject, $customer_body, $headers); wp_safe_redirect(add_query_arg(array(NET360RECLAMACIONES_PUBLIC_PREFIX . '_success' => '1', NET360RECLAMACIONES_PUBLIC_PREFIX . '_code' => $code), $redirect)); exit; } }