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

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