255) { return new \WP_Error('name', 'יש למפות שדה שם מלא (עד 255 תווים).'); } if ($phone === '') { return new \WP_Error('phone', 'יש למפות מספר טלפון תקין עם קידומת מדינה.'); } $payload = ['name' => $name, 'phone' => $phone]; $email = self::value($fields, $s['email']); if ($email !== '') { if (!is_email($email) || strlen($email) > 255) { return new \WP_Error('email', 'כתובת האימייל אינה תקינה.'); } $payload['email'] = $email; } if ($s['stage_id']) { $payload['stage_id'] = $s['stage_id']; } foreach ($s['custom'] as $row) { if (!array_key_exists($row['field'], $fields)) { continue; } $value = self::value($fields, $row['field']); if ($row['type'] === 'boolean') { $value = !in_array(strtolower($value), ['', '0', 'false', 'no', 'off'], true); } elseif (mb_strlen($value) > 2000) { return new \WP_Error('custom_fields', 'ערך של שדה מותאם ארוך מ-2,000 תווים.'); } $payload['custom_fields'][$row['key']] = $value; } return $payload; } private static function value(array $fields, string $id): string { $v = $id !== '' ? ($fields[$id] ?? '') : ''; if (is_array($v)) { $v = implode(', ', array_filter($v, 'is_scalar')); } return is_scalar($v) ? trim(sanitize_textarea_field((string) $v)) : ''; } public static function phone(string $raw, string $country): string { // Reject extensions, letters and non-phone input instead of silently altering it. if (!preg_match('/^[+0-9\s().-]+$/D', $raw)) { return ''; } $phone = preg_replace('/[\s().-]/', '', $raw); if (str_starts_with($phone, '00')) { $phone = '+' . substr($phone, 2); } if (!str_starts_with($phone, '+')) { if ($country === '') { return ''; } $phone = '+' . (str_starts_with($phone, $country) ? $phone : $country . ltrim($phone, '0')); } return preg_match('/^\+[1-9][0-9]{7,14}$/D', $phone) ? $phone : ''; } }