-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-upload-ajax-with-process.php
246 lines (185 loc) · 7.48 KB
/
wp-upload-ajax-with-process.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/*
Plugin Name: Thạch Phạm Upload File
Plugin URI: http://hoidap.thachpham.com/
Description: Upload file ngoài frontend bằng ajax và process.
Author: Vũ Đức Nam
Version: 0.1
Author URI: http://vuducnam.com/
*/
define('TPU_FOLDER', basename(dirname(__FILE__)));
define('TPU_URL', plugin_dir_url(TPU_FOLDER).TPU_FOLDER.'/');
define('TPU_CSS', TPU_URL.'assets/css/');
define('TPU_JS', TPU_URL.'assets/js/');
/**
*
*/
class TP_Upload_File
{
function __construct()
{
/**
* Đây là action thêm style vào header
*/
add_action( 'wp_enqueue_scripts', array($this, 'tp_enqueue_styles') );
/**
* Đây là action thêm script vào header vs footer
*/
add_action( 'wp_enqueue_scripts', array($this, 'tp_enqueue_scripts') );
/**
* Đây là action để thêm shortcode [tp_upload]
*/
add_shortcode('tp_upload', array($this, 'tp_shortcode'));
/**
* Đây là action đăng ký ajax upload
*/
add_action( 'wp_ajax_tp_upload', array($this, 'tp_upload') );
add_action( 'wp_ajax_nopriv_tp_upload', array($this, 'tp_upload') );
/**
* Đây là action đăng ký ajax delete upload
*/
add_action( 'wp_ajax_tp_delete_upload', array($this, 'tp_delete_upload') );
add_action( 'wp_ajax_nopriv_tp_delete_upload', array($this, 'tp_delete_upload') );
}
/**
* Đây là function thêm style
*/
public function tp_enqueue_styles(){
wp_enqueue_style( 'tp-style-icon', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css', array(), '0.1', 'all' );
wp_enqueue_style( 'tp-style-uikit', TPU_CSS . 'uikit.docs.min.css', array(), '0.1', 'all' );
wp_enqueue_style( 'tp-style-main', TPU_CSS . 'main.css', array(), '0.1', 'all' );
}
/**
* Đây là function thêm script
*/
public function tp_enqueue_scripts(){
wp_enqueue_script( 'tpu-script-uikit', TPU_JS . 'uikit.min.js', array( 'jquery' ), '0.1', false );
wp_enqueue_script( 'tpu-script-upload', TPU_JS . 'upload.js', array( 'jquery' ), '0.1', false );
wp_enqueue_script( 'tpu-script-main', TPU_JS . 'main.js', array( 'jquery' ), '0.1', true );
wp_localize_script( 'tpu-script-main', 'tp_ajax_url', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
/**
* Đây là function shortcode
*/
public function tp_shortcode(){
ob_start();
?>
<div id="upload-drop" class="uk-placeholder uk-text-center">
<i class="fa fa-cloud-upload uk-icon-medium uk-text-muted uk-margin-small-right"></i> Attach binaries by dropping them here or <a class="uk-form-file">selecting one<input id="upload-select" name="uploadfile" type="file"></a>.
<div id="progressbar" class="uk-progress uk-hidden">
<div class="uk-progress-bar" style="width: 0%;">0%</div>
</div>
<div id="upload-results" style="display:none;">
<div class="uk-alert">
<ul id="files" class="uk-list"><li class="clearfix" style="clear: both;"></li></ul>
</div>
</div>
</div>
<?php $upload_nonce = wp_create_nonce( 'tp_upload_nonce' );?>
<script>
(function($) {
"use strict";
var progressbar = $("#progressbar"),
bar = progressbar.find('.uk-progress-bar'),
settings = {
action: "<?php echo esc_url( admin_url('admin-ajax.php'));?>?action=tp_upload&_wpnonce=<?php echo $upload_nonce; ?>", // ajax upload url
param: 'uploadfile',//gửi data input file để xử lý trong php
allow : "*.(jpg|png|jpeg|gif)",//file cho phép upload
type: 'json',//kiểu trả về - json, html...
loadstart: function() {
bar.css("width", "0%").text("0%");
progressbar.removeClass("uk-hidden");
},
progress: function(percent) {
percent = Math.ceil(percent);
bar.css("width", percent+"%").text(percent+"%");
},
allcomplete: function(data) {
bar.css("width", "100%").text("100%");
setTimeout(function(){ progressbar.addClass("uk-hidden");}, 250);
$("#upload-results").show();
if( data.status == "1" ){
$('#files .clearfix').before('<li class="success"><img src="'+ data.url +'" alt="" data-name="'+ data.name_tmp +'" data-id="'+ data.id +'" style="max-width:35px;"/><br />'+ data.name_tmp +' <a href="" class="remove_img uk-alert-close uk-close" data-delete="'+ data.id +'"></a></li>');
new tp_remove_data_upload("#files .success .remove_img");//hàm xóa file upload
} else{
$("#upload-results .uk-alert").addClass('uk-alert-danger');
$('#files .clearfix').before('<li class="error">'+ data.message +'</li>');
}
}
};
var select = UIkit.uploadSelect($("#upload-select"), settings),
drop = UIkit.uploadDrop($("#upload-drop"), settings);
})(jQuery);
</script>
<?php
$out = ob_get_contents();
ob_end_clean();
return $out;
}
/**
* Đây là function để chạy action upload file ajax
*/
public function tp_upload(){
$status = array('status'=> '0', 'name'=> '', 'name_tmp' => '', 'url' => '', 'id' => '', 'message'=> 'No file sent.');
if( isset($_FILES['uploadfile']['name']) && !empty($_FILES['uploadfile']['name']) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'tp_upload_nonce' ) ){
$mimes = array( 'image/jpeg', 'image/png', 'image/gif', 'image/jpg' );
$attach_id = false;
$file_name = basename( $_FILES['uploadfile']['name'] );
$file_type = wp_check_filetype( $file_name );
$file_renamed = mt_rand( 1000, 1000000 ) . '.' . $file_type['ext'];
$upload = array(
'name' => $file_renamed,
'type' => $file_type['type'],
'tmp_name' => $_FILES['uploadfile']['tmp_name'],
'error' => $_FILES['uploadfile']['error'],
'size' => $_FILES['uploadfile']['size']
);
if( in_array( $file_type['type'], $mimes) ){
$file = wp_handle_upload( $upload, array( 'test_form' => false ) );
if ( $file && !isset( $file['error'] ) ) {
$status['status'] = '1';
$status['name'] = $file_renamed;
$status['name_tmp'] = $_FILES['uploadfile']['name'];
$status['url'] = esc_url($file['url']);
$status['message'] = 'File is uploaded successfully.';
if ( isset( $file['file'] ) ) {
$file_loc = $file['file'];
$attachment = array(
'post_mime_type' => $file_type['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => esc_url($file['url'])
);
$post_id = 0;
$attach_id = wp_insert_attachment( $attachment, $file_loc, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_loc );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
if ( $attach_id ) {
$status['id'] = $attach_id;
}
}else{
$status['message'] = $file['error'];
}
}else{
$status['message'] = 'Invalid file format.';
}
}
echo json_encode($status);
die();
}
/**
* Đây là function để chạy action delete file ajax
*/
public function tp_delete_upload(){
$status = array('status'=> '0');
if( isset($_REQUEST['attach_id']) && !empty($_REQUEST['attach_id']) ){
wp_delete_attachment( $_REQUEST['attach_id'] );
}
echo json_encode($status);
die();
}
}
new TP_Upload_File();
?>