PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.0
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Util / WpFileHandler.php

WpFileHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.2.0, at includes/Core/Util/WpFileHandler.php

161 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Util;
4
5 class WpFileHandler
6 {
7 private $_formID;
8 private $_uploadDirInfo;
9 private $_wpUploadbaseDir;
10 private $_bitformsUploadBaseDir;
11
12 public function __construct($fromID)
13 {
14 $this->_formID = $fromID;
15 $this->_uploadDirInfo = wp_upload_dir();
16 $this->_wpUploadbaseDir = $this->_uploadDirInfo['basedir'];
17 $this->_bitformsUploadBaseDir = $this->_wpUploadbaseDir . DIRECTORY_SEPARATOR . 'bitforms';
18 }
19
20 public function uploadFeatureImg($file, $entryID, $postID)
21 {
22 $_upload_dir = FileHandler::getEntriesFileUploadDir($this->_formID, $entryID);
23 $files = is_string($file) ? json_decode($file) : $file;
24 $IMGFileName = $files[0];
25 $IMGFilePath = $_upload_dir . DIRECTORY_SEPARATOR . $IMGFileName;
26 if (file_exists($IMGFilePath)) {
27 //prepare upload image to WordPress Media Library
28 $upload = wp_upload_bits($IMGFileName, null, file_get_contents($IMGFilePath, FILE_USE_INCLUDE_PATH));
29 // check and return file type
30 $imageFile = $upload['file'];
31 $wpFileType = wp_check_filetype($imageFile, null);
32 // Attachment attributes for file
33 $attachment = [
34 'post_mime_type' => $wpFileType['type'],
35 'post_title' => sanitize_file_name($IMGFileName), // sanitize and use image name as file name
36 'post_content' => '',
37 'post_status' => 'inherit'
38 ];
39 // insert and return attachment id
40 $attachmentId = wp_insert_attachment($attachment, $imageFile, $postID);
41 require_once ABSPATH . 'wp-admin/includes/image.php';
42 // insert and return attachment metadata
43 $attachmentData = wp_generate_attachment_metadata($attachmentId, $imageFile);
44 // update and return attachment metadata
45 wp_update_attachment_metadata($attachmentId, $attachmentData);
46 // finally, associate attachment id to post id
47 set_post_thumbnail($postID, $attachmentId);
48 }
49 }
50
51 public function singleFileMoveWpMedia($entryID, $fileValues, $post_id)
52 {
53 $_upload_dir = FileHandler::getEntriesFileUploadDir($this->_formID, $entryID);
54 $files = is_string($fileValues) ? json_decode($fileValues) : $fileValues;
55
56 $IMGFileName = $files[0];
57 $IMGFilePath = $_upload_dir . DIRECTORY_SEPARATOR . $IMGFileName;
58 if (file_exists($IMGFilePath)) {
59 //prepare upload image to WordPress Media Library
60 $upload = wp_upload_bits($IMGFileName, null, file_get_contents($IMGFilePath, FILE_USE_INCLUDE_PATH));
61
62 $imageFile = $upload['file'];
63 // echo $imageFile;
64 $wpFileType = wp_check_filetype($imageFile, null);
65 // Attachment attributes for file
66 $attachment = [
67 'post_mime_type' => $wpFileType['type'],
68 'post_title' => sanitize_file_name($IMGFileName), // sanitize and use image name as file name
69 'post_content' => '',
70 'post_status' => 'inherit',
71 'post_parent' => $post_id
72 ];
73 // insert and return attachment id
74 $attachmentId = wp_insert_attachment($attachment, $imageFile, $post_id);
75 require_once ABSPATH . 'wp-admin/includes/image.php';
76 // insert and return attachment metadata
77 $attachmentData = wp_generate_attachment_metadata($attachmentId, $imageFile);
78 wp_update_attachment_metadata($attachmentId, $attachmentData);
79 return $attachmentId;
80 }
81 }
82
83 public function multiFileMoveWpMedia($entryID, $fileValues, $post_id)
84 {
85 $_upload_dir = FileHandler::getEntriesFileUploadDir($this->_formID, $entryID);
86 $attachMentId = [];
87 $files = is_string($fileValues) ? json_decode($fileValues) : $fileValues;
88 foreach ($files as $file) {
89 $IMGFileName = $file;
90 $IMGFilePath = $_upload_dir . DIRECTORY_SEPARATOR . $IMGFileName;
91 if (file_exists($IMGFilePath)) {
92 //prepare upload image to WordPress Media Library
93 $upload = wp_upload_bits($IMGFileName, null, file_get_contents($IMGFilePath, FILE_USE_INCLUDE_PATH));
94
95 $imageFile = $upload['file'];
96 // echo $imageFile;
97 $wpFileType = wp_check_filetype($imageFile, null);
98 // Attachment attributes for file
99 $attachment = [
100 'post_mime_type' => $wpFileType['type'],
101 'post_title' => sanitize_file_name($IMGFileName), // sanitize and use image name as file name
102 'post_content' => '',
103 'post_status' => 'inherit',
104 'post_parent' => $post_id
105 ];
106 // insert and return attachment id
107 $attachmentId = wp_insert_attachment($attachment, $imageFile, $post_id);
108 // $attachMentId[]=$attachmentId;
109 array_push($attachMentId, $attachmentId);
110
111 require_once ABSPATH . 'wp-admin/includes/image.php';
112 // insert and return attachment metadata
113 $attachmentData = wp_generate_attachment_metadata($attachmentId, $imageFile);
114 // update and return attachment metadata
115 wp_update_attachment_metadata($attachmentId, $attachmentData);
116 }
117 }
118 return $attachMentId;
119 }
120
121 public function taxonomyData($formFields, $fieldValues)
122 {
123 $taxanomyData = [];
124 foreach ($formFields as $fieldKey => $field) {
125 $customType = isset($field['customType']) ? $field['customType'] : null;
126 $customType = (!isset($customType) && 'select' === $field->type && isset($field->customTypeList)) ? $field->customTypeList[0] : $customType;
127 if (isset($customType)) {
128 if (isset($customType->isTaxonomy) && !empty($fieldValues[$field['key']]) && isset($customType->isHierarchical)) {
129 if (true === $customType->isTaxonomy) {
130 if (true === $customType->isHierarchical) {
131 $taxanomyData[$fieldKey]['value'] = $fieldValues[$field['key']];
132 } else {
133 $slug = '';
134
135 if (is_array($fieldValues[$field['key']])) {
136 $allSlug = [];
137 foreach ($fieldValues[$field['key']] as $key => $value) {
138 $exists = get_term_by('term_id', $value, $customType->filter->taxanomy);
139 if ($exists) {
140 $allSlug[$key] = $exists->slug;
141 }
142 }
143 $slug = implode(',', $allSlug);
144 } else {
145 $exists = get_term_by('term_id', $fieldValues[$field['key']], $customType->filter->taxanomy);
146 if ($exists) {
147 $slug = $exists->slug;
148 }
149 }
150
151 $taxanomyData[$fieldKey]['value'] = $slug;
152 }
153 $taxanomyData[$fieldKey]['term'] = $customType->filter->taxanomy;
154 }
155 }
156 }
157 }
158 return $taxanomyData;
159 }
160 }
161