PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
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 2.15.3, at includes/Core/Util/WpFileHandler.php

165 lines 6.9 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 require_once ABSPATH . 'wp-load.php';
23 $_upload_dir = $this->_bitformsUploadBaseDir . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $this->_formID . DIRECTORY_SEPARATOR . $entryID;
24 $files = is_string($file) ? json_decode($file) : $file;
25 $IMGFileName = $files[0];
26 $IMGFilePath = $_upload_dir . DIRECTORY_SEPARATOR . $IMGFileName;
27 if (file_exists($IMGFilePath)) {
28 //prepare upload image to WordPress Media Library
29 $upload = wp_upload_bits($IMGFileName, null, file_get_contents($IMGFilePath, FILE_USE_INCLUDE_PATH));
30 // check and return file type
31 $imageFile = $upload['file'];
32 $wpFileType = wp_check_filetype($imageFile, null);
33 // Attachment attributes for file
34 $attachment = [
35 'post_mime_type' => $wpFileType['type'],
36 'post_title' => sanitize_file_name($IMGFileName), // sanitize and use image name as file name
37 'post_content' => '',
38 'post_status' => 'inherit'
39 ];
40 // insert and return attachment id
41 $attachmentId = wp_insert_attachment($attachment, $imageFile, $postID);
42 require_once ABSPATH . 'wp-admin/includes/image.php';
43 // insert and return attachment metadata
44 $attachmentData = wp_generate_attachment_metadata($attachmentId, $imageFile);
45 // update and return attachment metadata
46 wp_update_attachment_metadata($attachmentId, $attachmentData);
47 // finally, associate attachment id to post id
48 set_post_thumbnail($postID, $attachmentId);
49 }
50 }
51
52 public function singleFileMoveWpMedia($entryID, $fileValues, $post_id)
53 {
54 $_upload_dir = $this->_bitformsUploadBaseDir . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $this->_formID . DIRECTORY_SEPARATOR . $entryID;
55 require_once ABSPATH . 'wp-load.php';
56 $files = is_string($fileValues) ? json_decode($fileValues) : $fileValues;
57
58 $IMGFileName = $files[0];
59 $IMGFilePath = $_upload_dir . DIRECTORY_SEPARATOR . $IMGFileName;
60 if (file_exists($IMGFilePath)) {
61 //prepare upload image to WordPress Media Library
62 $upload = wp_upload_bits($IMGFileName, null, file_get_contents($IMGFilePath, FILE_USE_INCLUDE_PATH));
63
64 $imageFile = $upload['file'];
65 // echo $imageFile;
66 $wpFileType = wp_check_filetype($imageFile, null);
67 // Attachment attributes for file
68 $attachment = [
69 'post_mime_type' => $wpFileType['type'],
70 'post_title' => sanitize_file_name($IMGFileName), // sanitize and use image name as file name
71 'post_content' => '',
72 'post_status' => 'inherit',
73 'post_parent' => $post_id
74 ];
75 // insert and return attachment id
76 $attachmentId = wp_insert_attachment($attachment, $imageFile, $post_id);
77 require_once ABSPATH . 'wp-admin/includes/image.php';
78 // insert and return attachment metadata
79 $attachmentData = wp_generate_attachment_metadata($attachmentId, $imageFile);
80 wp_update_attachment_metadata($attachmentId, $attachmentData);
81 return $attachmentId;
82 }
83 }
84
85 public function multiFileMoveWpMedia($entryID, $fileValues, $post_id)
86 {
87 $_upload_dir = $this->_bitformsUploadBaseDir . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $this->_formID . DIRECTORY_SEPARATOR . $entryID;
88 require_once ABSPATH . 'wp-load.php';
89 $attachMentId = [];
90 $files = is_string($fileValues) ? json_decode($fileValues) : $fileValues;
91 foreach ($files as $file) {
92 $IMGFileName = $file;
93 $IMGFilePath = $_upload_dir . DIRECTORY_SEPARATOR . $IMGFileName;
94 if (file_exists($IMGFilePath)) {
95 //prepare upload image to WordPress Media Library
96 $upload = wp_upload_bits($IMGFileName, null, file_get_contents($IMGFilePath, FILE_USE_INCLUDE_PATH));
97
98 $imageFile = $upload['file'];
99 // echo $imageFile;
100 $wpFileType = wp_check_filetype($imageFile, null);
101 // Attachment attributes for file
102 $attachment = [
103 'post_mime_type' => $wpFileType['type'],
104 'post_title' => sanitize_file_name($IMGFileName), // sanitize and use image name as file name
105 'post_content' => '',
106 'post_status' => 'inherit',
107 'post_parent' => $post_id
108 ];
109 // insert and return attachment id
110 $attachmentId = wp_insert_attachment($attachment, $imageFile, $post_id);
111 // $attachMentId[]=$attachmentId;
112 array_push($attachMentId, $attachmentId);
113
114 require_once ABSPATH . 'wp-admin/includes/image.php';
115 // insert and return attachment metadata
116 $attachmentData = wp_generate_attachment_metadata($attachmentId, $imageFile);
117 // update and return attachment metadata
118 wp_update_attachment_metadata($attachmentId, $attachmentData);
119 }
120 }
121 return $attachMentId;
122 }
123
124 public function getTaxonomies($formFields, $fieldValues)
125 {
126 $taxonomies = [];
127 foreach ($formFields as $fieldKey => $field) {
128 $field = 'object' === gettype($field) ? (array) $field : $field;
129 $customType = isset($field['customType']) ? $field['customType'] : null;
130 $customType = (!isset($customType) && 'select' === $field['type'] && isset($field['customTypeList'])) ? $field['customTypeList'][0] : $customType;
131 if (isset($customType)) {
132 if (isset($customType->isTaxonomy) && !empty($fieldValues[$field['key']]) && isset($customType->isHierarchical)) {
133 if (true === $customType->isTaxonomy) {
134 if (true === $customType->isHierarchical) {
135 $taxonomies[$fieldKey]['value'] = $fieldValues[$field['key']];
136 } else {
137 $slug = '';
138
139 if (is_array($fieldValues[$field['key']])) {
140 $allSlug = [];
141 foreach ($fieldValues[$field['key']] as $key => $value) {
142 $exists = get_term_by('term_id', $value, $customType->filter->taxanomy);
143 if ($exists) {
144 $allSlug[$key] = $exists->slug;
145 }
146 }
147 $slug = implode(',', $allSlug);
148 } else {
149 $exists = get_term_by('term_id', $fieldValues[$field['key']], $customType->filter->taxanomy);
150 if ($exists) {
151 $slug = $exists->slug;
152 }
153 }
154
155 $taxonomies[$fieldKey]['value'] = $slug;
156 }
157 $taxonomies[$fieldKey]['term'] = $customType->filter->taxanomy;
158 }
159 }
160 }
161 }
162 return $taxonomies;
163 }
164 }
165