protected static function _processUpload() { header('content-type: application/json'); require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; $attachments = array(); if (CMA_Settings::areAttachmentsAllowed() and !empty($_FILES['cma-file'])) { $maxFileSize = CMA_Settings::getOption(CMA_Settings::OPTION_ATTACHMENTS_MAX_SIZE); foreach ($_FILES['cma-file']['name'] as $i => $name) { $attachment = array('name' => $name); if ($_FILES['cma-file']['size'][$i] > $maxFileSize) { $attachments[] = array_merge($attachment, array('status' => 'ERROR', 'msg' => CMA::__('File is too large.'))); } else { if (!CMA_Thread::checkAttachmentAllowed($name)) { $msg = CMA::__('Filetype is not allowed. Allowed extensions:'); $msg .= ' ' . implode(', ', CMA_Settings::getOption(CMA_Settings::OPTION_ATTACHMENTS_FILE_EXTENSIONS)); $attachments[] = array_merge($attachment, array('status' => 'ERROR', 'msg' => $msg)); } else { $newName = floor(microtime(true) * 1000) . '_' . sanitize_file_name($name); $target = CMA_Attachment::getUploadPath() . $newName; if (move_uploaded_file($_FILES['cma-file']['tmp_name'][$i], $target)) { $data = array('guid' => $target, 'post_mime_type' => $_FILES['cma-file']['type'][$i], 'post_title' => $name, 'post_content' => '', 'post_status' => 'draft'); $attach_id = wp_insert_attachment($data, $target); $attach_data = wp_generate_attachment_metadata($attach_id, $target); wp_update_attachment_metadata($attach_id, $attach_data); $attachments[] = array_merge($attachment, array('status' => 'OK', 'id' => $attach_id)); } else { $attachments[] = array_merge($attachment, array('status' => 'ERROR')); } } } } } echo json_encode($attachments); exit; }