From 1142fc36bc8653e02bcb42ba253bbfc703758b78 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lucas=20Garc=C3=ADa?= <lucas@codeccoop.org>
Date: Thu, 21 Sep 2023 17:56:26 +0200
Subject: [PATCH] feat: attachments module

---
 includes/attachments.php | 65 ++++++++++++++++++++++++++++++++++++++++
 includes/submissions.php | 64 +--------------------------------------
 wpct-crm-forms.php       |  1 +
 3 files changed, 67 insertions(+), 63 deletions(-)
 create mode 100644 includes/attachments.php

diff --git a/includes/attachments.php b/includes/attachments.php
new file mode 100644
index 0000000..7541f33
--- /dev/null
+++ b/includes/attachments.php
@@ -0,0 +1,65 @@
+<?php
+
+/**
+ * Store uploads on a custom folder under a custom endpoint
+ */
+add_filter('gform_upload_path', 'wpct_crm_forms_upload_path', 90, 2);
+function wpct_crm_forms_upload_path($path_info, $form_id)
+{
+    $upload_dir = wp_upload_dir();
+    $base_path = apply_filters('wpct_crm_forms_upload_path', $upload_dir['basedir'] . '/crm-forms');
+    if (!($base_path && is_string($base_path))) throw new Exception('WPCT CRM Forms: Invalid upload path');
+    $base_path = preg_replace('/\/$/', '', $base_path);
+
+    $path = $base_path . '/' . implode('/', [$form_id, date('Y'), date('m')]);
+    if (!is_dir($path)) mkdir($path, 0700, true);
+    $path_info['path'] = $path;
+
+    $url = get_site_url() . '/index.php?';
+    $url .= 'crm-forms-attachment=' . urlencode(str_replace($base_path, '', $path) . '/');
+    $path_info['url'] = $url;
+
+    return $path_info;
+};
+
+/*
+ * Attachments delivery handler
+ */
+add_action('init', 'wpct_crm_forms_download_file');
+function wpct_crm_forms_download_file()
+{
+    if (!isset($_GET['crm-forms-attachment'])) return;
+
+    $upload_dir = wp_upload_dir();
+    $base_path = apply_filters('wpct_crm_forms_upload_path', $upload_dir['basedir'] . '/crm-forms');
+    if (!($base_path && is_string($base_path))) throw new Exception('WPCT CRM Forms: Invalid upload path');
+    $base_path = preg_replace('/\/$/', '', $base_path);
+    $path = $base_path . urldecode($_GET['crm-forms-attachment']);
+
+    if (!(is_user_logged_in() && file_exists($path))) {
+        global $wp_query;
+        status_header(404);
+        $wp_query->set_404();
+        $template_path = get_404_template();
+        if (file_exists($template_path)) require_once($template_path);
+        die();
+    }
+
+    $filetype = wp_check_filetype($path);
+    if (!$filetype['type']) {
+        $filetype['type'] = mime_content_type($path);
+    }
+
+    nocache_headers();
+    header('X-Robots-Tag: noindex', true);
+    header('Content-Type: ' . $filetype['type']);
+    header('Content-Description: File Transfer');
+    header('Content-Disposition: inline; filename="' . wp_basename($path) . '"');
+    header('Content-Transfer-Encoding: binary');
+    header('Content-Length: ' . filesize($path));
+
+    if (ob_get_contents()) ob_end_clean();
+
+    readfile($path);
+    die();
+}
diff --git a/includes/submissions.php b/includes/submissions.php
index e8260da..6994976 100644
--- a/includes/submissions.php
+++ b/includes/submissions.php
@@ -57,7 +57,7 @@ function wpct_crm_forms_parse_form_entry($entry, $form)
 function wpct_crm_forms_format_value($value, $field, $input = null)
 {
     if ($field->type === 'fileupload') {
-        if (!is_array($field->get_entry_inputs())) return json_decode($value)[0];
+        return implode(',', json_decode($value));
     }
 
     return $value;
@@ -126,65 +126,3 @@ function wpct_crm_forms_prepare_submission($form_vals)
     $form_vals = wpct_crm_forms_cleanup_empties($form_vals);
     return wpct_crm_forms_get_submission_payload($form_vals);
 }
-
-
-/**
- * Store uploads on a custom folder
- */
-add_filter('gform_upload_path', 'wpct_crm_forms_upload_path', 90, 2);
-function wpct_crm_forms_upload_path($path_info, $form_id)
-{
-    $upload_dir = wp_upload_dir();
-    $base_path = apply_filters('wpct_crm_forms_upload_path', $upload_dir['basedir'] . '/crm-forms');
-    if (!($base_path && is_string($base_path))) throw new Exception('WPCT CRM Forms: Invalid upload path');
-    $base_path = preg_replace('/\/$/', '', $base_path);
-
-    $path = $base_path . '/' . implode('/', [$form_id, date('Y'), date('m')]);
-    if (!is_dir($path)) mkdir($path, 0700, true);
-    $path_info['path'] = $path;
-
-    $url = get_site_url() . '/index.php?';
-    $url .= 'crm-forms-attachment=' . urlencode(str_replace($base_path, '', $path) . '/');
-    $path_info['url'] = $url;
-
-    return $path_info;
-};
-
-add_action('init', 'wpct_crm_forms_download_file');
-function wpct_crm_forms_download_file()
-{
-    if (!isset($_GET['crm-forms-attachment'])) return;
-
-    $upload_dir = wp_upload_dir();
-    $base_path = apply_filters('wpct_crm_forms_upload_path', $upload_dir['basedir'] . '/crm-forms');
-    if (!($base_path && is_string($base_path))) throw new Exception('WPCT CRM Forms: Invalid upload path');
-    $base_path = preg_replace('/\/$/', '', $base_path);
-    $path = $base_path . urldecode($_GET['crm-forms-attachment']);
-
-    if (!(is_user_logged_in() && file_exists($path))) {
-        global $wp_query;
-        status_header(404);
-        $wp_query->set_404();
-        $template_path = get_404_template();
-        if (file_exists($template_path)) require_once($template_path);
-        die();
-    }
-
-    $filetype = wp_check_filetype($path);
-    if (!$filetype['type']) {
-        $filetype['type'] = mime_content_type($path);
-    }
-
-    nocache_headers();
-    header('X-Robots-Tag: noindex', true);
-    header('Content-Type: ' . $filetype['type']);
-    header('Content-Description: File Transfer');
-    header('Content-Disposition: inline; filename="' . wp_basename($path) . '"');
-    header('Content-Transfer-Encoding: binary');
-    header('Content-Length: ' . filesize($path));
-
-    if (ob_get_contents()) ob_end_clean();
-
-    readfile($path);
-    die();
-}
diff --git a/wpct-crm-forms.php b/wpct-crm-forms.php
index 796c318..fb45909 100755
--- a/wpct-crm-forms.php
+++ b/wpct-crm-forms.php
@@ -19,6 +19,7 @@ require_once "includes/options-page.php";
 /* Webhooks */
 require_once "includes/webhooks.php";
 require_once "includes/submissions.php";
+require_once "includes/attachments.php";
 
 /* Fields population */
 require_once "includes/fields-population.php";
-- 
GitLab