| 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 taxonomyData($formFields, $fieldValues) |
| 125 |
{ |
| 126 |
$taxanomyData = []; |
| 127 |
foreach ($formFields as $fieldKey => $field) { |
| 128 |
if (isset($field['customType'])) { |
| 129 |
if (isset($field['customType']->isTaxonomy) && !empty($fieldValues[$field['key']]) && isset($field['customType']->isHierarchical)) { |
| 130 |
if (true === $field['customType']->isTaxonomy) { |
| 131 |
if (true === $field['customType']->isHierarchical) { |
| 132 |
$taxanomyData[$fieldKey]['value'] = $fieldValues[$field['key']]; |
| 133 |
} else { |
| 134 |
$slug = ''; |
| 135 |
|
| 136 |
if (is_array($fieldValues[$field['key']])) { |
| 137 |
$allSlug = []; |
| 138 |
foreach ($fieldValues[$field['key']] as $key => $value) { |
| 139 |
$exists = get_term_by('term_id', $value, $field['customType']->filter->taxanomy); |
| 140 |
if ($exists) { |
| 141 |
$allSlug[$key] = $exists->slug; |
| 142 |
} |
| 143 |
} |
| 144 |
$slug = implode(',', $allSlug); |
| 145 |
} else { |
| 146 |
$exists = get_term_by('term_id', $fieldValues[$field['key']], $field['customType']->filter->taxanomy); |
| 147 |
if ($exists) { |
| 148 |
$slug = $exists->slug; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
$taxanomyData[$fieldKey]['value'] = $slug; |
| 153 |
} |
| 154 |
$taxanomyData[$fieldKey]['term'] = $field['customType']->filter->taxanomy; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
return $taxanomyData; |
| 160 |
} |
| 161 |
} |
| 162 |
|