-3 Removals
+3 Additions
1<?php 1<?php
2 2
3/** 3/**
4 * Attachment Uploader class 4 * Attachment Uploader class
5 * 5 *
6 * @since 1.0 6 * @since 1.0
7 * @package wpuf 7 * @package wpuf
8 */ 8 */
9class WPUF_Upload { 9class WPUF_Upload {
10 10
11 function __construct() { 11 function __construct() {
12 12
13 add_action( 'wp_ajax_wpuf_file_upload', array($this, 'upload_file') ); 13 add_action( 'wp_ajax_wpuf_file_upload', array($this, 'upload_file') );
14 add_action( 'wp_ajax_nopriv_wpuf_file_upload', array($this, 'upload_file') ); 14 add_action( 'wp_ajax_nopriv_wpuf_file_upload', array($this, 'upload_file') );
15 15
16 add_action( 'wp_ajax_wpuf_file_del', array($this, 'delete_file') ); 16 add_action( 'wp_ajax_wpuf_file_del', array($this, 'delete_file') );
17 add_action( 'wp_ajax_nopriv_wpuf_file_del', array($this, 'delete_file') ); 17 add_action( 'wp_ajax_nopriv_wpuf_file_del', array($this, 'delete_file') );
18 18
19 add_action( 'wp_ajax_wpuf_insert_image', array( $this, 'insert_image' ) ); 19 add_action( 'wp_ajax_wpuf_insert_image', array( $this, 'insert_image' ) );
20 add_action( 'wp_ajax_nopriv_wpuf_insert_image', array( $this, 'insert_image' ) ); 20 add_action( 'wp_ajax_nopriv_wpuf_insert_image', array( $this, 'insert_image' ) );
21 } 21 }
22 22
23 function upload_file( $image_only = false ) { 23 function upload_file( $image_only = false ) {
24 $upload = array( 24 $upload = array(
25 'name' => $_FILES['wpuf_file']['name'], 25 'name' => $_FILES['wpuf_file']['name'],
26 'type' => $_FILES['wpuf_file']['type'], 26 'type' => $_FILES['wpuf_file']['type'],
27 'tmp_name' => $_FILES['wpuf_file']['tmp_name'], 27 'tmp_name' => $_FILES['wpuf_file']['tmp_name'],
28 'error' => $_FILES['wpuf_file']['error'], 28 'error' => $_FILES['wpuf_file']['error'],
29 'size' => $_FILES['wpuf_file']['size'] 29 'size' => $_FILES['wpuf_file']['size']
30 ); 30 );
31 31
32 header('Content-Type: text/html; charset=' . get_option('blog_charset')); 32 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
33 33
34 $attach = $this->handle_upload( $upload ); 34 $attach = $this->handle_upload( $upload );
35 35
36 if ( $attach['success'] ) { 36 if ( $attach['success'] ) {
37 37
38 $response = array( 'success' => true ); 38 $response = array( 'success' => true );
39 39
40 if ($image_only) { 40 if ($image_only) {
41 $response['html'] = wp_get_attachment_image( $attach['attach_id'], 'full' ); 41 $response['html'] = wp_get_attachment_image( $attach['attach_id'], 'full' );
42 } else { 42 } else {
43 $response['html'] = $this->attach_html( $attach['attach_id'] ); 43 $response['html'] = $this->attach_html( $attach['attach_id'] );
44 // $response['html'] = wp_get_attachment_image( $attach['attach_id'], 'thumbnail' ); 44 // $response['html'] = wp_get_attachment_image( $attach['attach_id'], 'thumbnail' );
45 } 45 }
46 46
47 echo $response['html']; 47 echo $response['html'];
48 } else { 48 } else {
49 echo 'error'; 49 echo 'error';
50 } 50 }
51 51
52 52
53 // $response = array('success' => false, 'message' => $attach['error']); 53 // $response = array('success' => false, 'message' => $attach['error']);
54 // echo json_encode( $response ); 54 // echo json_encode( $response );
55 exit; 55 exit;
56 } 56 }
57 57
58 /** 58 /**
59 * Generic function to upload a file 59 * Generic function to upload a file
60 * 60 *
61 * @param string $field_name file input field name 61 * @param string $field_name file input field name
62 * @return bool|int attachment id on success, bool false instead 62 * @return bool|int attachment id on success, bool false instead
63 */ 63 */
64 function handle_upload( $upload_data ) { 64 function handle_upload( $upload_data ) {
65 65
66 $uploaded_file = wp_handle_upload( $upload_data, array('test_form' => false) ); 66 $uploaded_file = wp_handle_upload( $upload_data, array('test_form' => false) );
67 67
68 // If the wp_handle_upload call returned a local path for the image 68 // If the wp_handle_upload call returned a local path for the image
69 if ( isset( $uploaded_file['file'] ) ) { 69 if ( isset( $uploaded_file['file'] ) ) {
70 $file_loc = $uploaded_file['file']; 70 $file_loc = $uploaded_file['file'];
71 $file_name = basename( $upload_data['name'] ); 71 $file_name = basename( $upload_data['name'] );
72 $file_type = wp_check_filetype( $file_name ); 72 $file_type = wp_check_filetype( $file_name );
73 73
74 $attachment = array( 74 $attachment = array(
75 'post_mime_type' => $file_type['type'], 75 'post_mime_type' => $file_type['type'],
76 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ), 76 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
77 'post_content' => '', 77 'post_content' => '',
78 'post_status' => 'inherit' 78 'post_status' => 'inherit'
79 ); 79 );
80 80
81 $attach_id = wp_insert_attachment( $attachment, $file_loc ); 81 $attach_id = wp_insert_attachment( $attachment, $file_loc );
82 $attach_data = wp_generate_attachment_metadata( $attach_id, $file_loc ); 82 $attach_data = wp_generate_attachment_metadata( $attach_id, $file_loc );
83 wp_update_attachment_metadata( $attach_id, $attach_data ); 83 wp_update_attachment_metadata( $attach_id, $attach_data );
84 84
85 return array('success' => true, 'attach_id' => $attach_id); 85 return array('success' => true, 'attach_id' => $attach_id);
86 } 86 }
87 87
88 return array('success' => false, 'error' => $uploaded_file['error']); 88 return array('success' => false, 'error' => $uploaded_file['error']);
89 } 89 }
90 90
91 public static function attach_html( $attach_id, $type = NULL ) { 91 public static function attach_html( $attach_id, $type = NULL ) {
92 if ( !$type ) { 92 if ( !$type ) {
93 $type = isset( $_GET['type'] ) ? $_GET['type'] : 'image'; 93 $type = isset( $_GET['type'] ) ? $_GET['type'] : 'image';
94 } 94 }
95 95
96 $attachment = get_post( $attach_id ); 96 $attachment = get_post( $attach_id );
97 97
98 if (!$attachment) { 98 if (!$attachment) {
99 return; 99 return;
100 } 100 }
101 101
102 if (wp_attachment_is_image( $attach_id)) { 102 if (wp_attachment_is_image( $attach_id)) {
103 $image = wp_get_attachment_image_src( $attach_id, 'thumbnail' ); 103 $image = wp_get_attachment_image_src( $attach_id, 'full' );
104 $image = $image[0]; 104 $image = $image[0];
105 } else { 105 } else {
106 $image = wp_mime_type_icon( $attach_id ); 106 $image = wp_mime_type_icon( $attach_id );
107 } 107 }
108 108
109 $html = '<li class="image-wrap thumbnail" style="width: 150px">'; 109 $html = '<li class="image-wrap thumbnail">';
110 $html .= sprintf( '<div class="attachment-name"><img src="%s" alt="%s" /></div>', $image, esc_attr( $attachment->post_title ) ); 110 $html .= sprintf( '<div class="attachment-name"><img src="%s" alt="%s" /></div>', $image, esc_attr( $attachment->post_title ) );
111 $html .= sprintf( '<div class="caption"><a href="#" class="btn btn-danger btn-small attachment-delete" data-attach_id="%d">%s</a></div>', $attach_id, __( 'Delete', 'wpuf' ) ); 111 $html .= sprintf( '<div class="caption"><a href="#" class="btn btn-danger btn-small attachment-delete" data-attach_id="%d">%s</a></div>', $attach_id, __( '<span>[X]</span> Delete Image', 'wpuf' ) );
112 $html .= sprintf( '<input type="hidden" name="wpuf_files[%s][]" value="%d">', $type, $attach_id ); 112 $html .= sprintf( '<input type="hidden" name="wpuf_files[%s][]" value="%d">', $type, $attach_id );
113 $html .= '</li>'; 113 $html .= '</li>';
114 114
115 return $html; 115 return $html;
116 } 116 }
117 117
118 function delete_file() { 118 function delete_file() {
119 check_ajax_referer( 'wpuf_nonce', 'nonce' ); 119 check_ajax_referer( 'wpuf_nonce', 'nonce' );
120 120
121 $attach_id = isset( $_POST['attach_id'] ) ? intval( $_POST['attach_id'] ) : 0; 121 $attach_id = isset( $_POST['attach_id'] ) ? intval( $_POST['attach_id'] ) : 0;
122 $attachment = get_post( $attach_id ); 122 $attachment = get_post( $attach_id );
123 123
124 //post author or editor role 124 //post author or editor role
125 if ( get_current_user_id() == $attachment->post_author || current_user_can( 'delete_private_pages' ) ) { 125 if ( get_current_user_id() == $attachment->post_author || current_user_can( 'delete_private_pages' ) ) {
126 wp_delete_attachment( $attach_id, true ); 126 wp_delete_attachment( $attach_id, true );
127 echo 'success'; 127 echo 'success';
128 } 128 }
129 129
130 exit; 130 exit;
131 } 131 }
132 132
133 function associate_file( $attach_id, $post_id ) { 133 function associate_file( $attach_id, $post_id ) {
134 wp_update_post( array( 134 wp_update_post( array(
135 'ID' => $attach_id, 135 'ID' => $attach_id,
136 'post_parent' => $post_id 136 'post_parent' => $post_id
137 ) ); 137 ) );
138 } 138 }
139 139
140 function insert_image() { 140 function insert_image() {
141 $this->upload_file( true ); 141 $this->upload_file( true );
142 } 142 }
143 143
144}144}
Editor
Original Text
Changed Text
Recommended videos