| 1 |
<?php |
| 2 |
namespace WeDevs\ERP; |
| 3 |
|
| 4 |
/** |
| 5 |
* People Class |
| 6 |
*/ |
| 7 |
class Uploader { |
| 8 |
function upload_file() { |
| 9 |
|
| 10 |
// check if guest post enabled for guests |
| 11 |
if ( ! is_user_logged_in() ) { |
| 12 |
die( 'error' ); |
| 13 |
} |
| 14 |
|
| 15 |
$upload = array( |
| 16 |
'name' => $_FILES['file']['name'], |
| 17 |
'type' => $_FILES['file']['type'], |
| 18 |
'tmp_name' => $_FILES['file']['tmp_name'], |
| 19 |
'error' => $_FILES['file']['error'], |
| 20 |
'size' => $_FILES['file']['size'] |
| 21 |
); |
| 22 |
|
| 23 |
header('Content-Type: text/html; charset=' . get_option('blog_charset')); |
| 24 |
|
| 25 |
$attach = $this->handle_upload( $upload ); |
| 26 |
|
| 27 |
if ( $attach['success'] ) { |
| 28 |
|
| 29 |
$response = array( 'success' => true ); |
| 30 |
return $this->attach_html( $attach['attach_id'] ); |
| 31 |
} else { |
| 32 |
return 'error'; |
| 33 |
} |
| 34 |
|
| 35 |
exit; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Generic function to upload a file |
| 40 |
* |
| 41 |
* @param string $field_name file input field name |
| 42 |
* @return bool|int attachment id on success, bool false instead |
| 43 |
*/ |
| 44 |
function handle_upload( $upload_data ) { |
| 45 |
|
| 46 |
$uploaded_file = wp_handle_upload( $upload_data, array('test_form' => false) ); |
| 47 |
|
| 48 |
// If the wp_handle_upload call returned a local path for the image |
| 49 |
if ( isset( $uploaded_file['file'] ) ) { |
| 50 |
|
| 51 |
$file_loc = $uploaded_file['file']; |
| 52 |
$file_name = basename( $upload_data['name'] ); |
| 53 |
$file_type = wp_check_filetype( $file_name ); |
| 54 |
|
| 55 |
$attachment = array( |
| 56 |
'post_mime_type' => $file_type['type'], |
| 57 |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ), |
| 58 |
'post_content' => '', |
| 59 |
'post_status' => 'inherit' |
| 60 |
); |
| 61 |
|
| 62 |
$attach_id = wp_insert_attachment( $attachment, $file_loc ); |
| 63 |
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_loc ); |
| 64 |
wp_update_attachment_metadata( $attach_id, $attach_data ); |
| 65 |
|
| 66 |
return array('success' => true, 'attach_id' => $attach_id); |
| 67 |
} |
| 68 |
|
| 69 |
return array('success' => false, 'error' => $uploaded_file['error']); |
| 70 |
} |
| 71 |
|
| 72 |
public static function attach_html( $attach_id, $custom_attr = [] ) { |
| 73 |
|
| 74 |
$attachment = get_post( $attach_id ); |
| 75 |
|
| 76 |
if ( ! $attachment ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if (wp_attachment_is_image( $attach_id)) { |
| 81 |
$image = wp_get_attachment_image_src( $attach_id, array( '80', '80' ) ); |
| 82 |
$image = $image[0]; |
| 83 |
} else { |
| 84 |
$image = wp_mime_type_icon( $attach_id ); |
| 85 |
} |
| 86 |
|
| 87 |
$html = '<li class="erp-image-wrap thumbnail">'; |
| 88 |
$html .= sprintf( '<div class="attachment-name"><img class="erp-file-mime" '.implode( ' data-', $custom_attr ).' height="80" width="80" src="%s" alt="%s" /></div>', $image, esc_attr( $attachment->post_title ) ); |
| 89 |
$html .= sprintf( '<input type="hidden" name="files[]" value="%d">', $attach_id ); |
| 90 |
$html .= sprintf( '<div class="caption"><a href="#" class="erp-del-attc-button btn-danger btn-small attachment-delete" data-attach_id="%d">X</a></div>', $attach_id ); |
| 91 |
$html .= '</li>'; |
| 92 |
|
| 93 |
return $html; |
| 94 |
} |
| 95 |
|
| 96 |
function delete_file( $attach_id ) { |
| 97 |
|
| 98 |
$attachment = get_post( $attach_id ); |
| 99 |
|
| 100 |
//post author or editor role |
| 101 |
if ( get_current_user_id() == $attachment->post_author || current_user_can( 'delete_private_pages' ) ) { |
| 102 |
wp_delete_attachment( $attach_id, true ); |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
exit; |
| 107 |
} |
| 108 |
} |