-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTakePic.php
440 lines (312 loc) · 13.4 KB
/
TakePic.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/*
Plugin Name: Take Pic
Plugin URI: https://github.com/ujw0l/takePicWp
Description: WordPress plugin which enables user to take picture with their webcam apply effects and upload to the server
Version: 3.0.0
Author: Ujwol Bastakoti
text-domain : take-pic
Author URI:http://ujw0l.github.io/
License: GPLv2
*/
/**
* class to handle other plugin functionlities
*/
class takePicPlugin{
public function __construct(){
self::allRequiredWpActions();
self::allRequiredAjaxAction();
}
/**
* function to register required wp action
*/
public function allRequiredWpActions(){
add_action( 'wp_enqueue_scripts', array($this,'enequeFrontendJs' ));
add_action('wp_enqueue_scripts',array($this,'loadFrontEndStyle'));
add_action('admin_menu', array($this, 'takePicAdminMenu'));
add_action('admin_footer', array($this,'takePicRemoveUpload_javascript'));
add_action( 'admin_enqueue_scripts', array($this,'enequeAdminJs' ));
add_action('init', array($this,'takePicRegisterBlock'));
}
/**
* Function to load dashicons
*/
public function loadFrontEndStyle(){
wp_enqueue_style( 'dashicons' );
wp_enqueue_style( 'vanillaCtcOverlayCss', plugins_url('css/ctc_overlay_style.css',__FILE__ )); //register css;
wp_enqueue_style( 'takePicCss', plugins_url('css/take-pic.css',__FILE__ )); //register css;
}
/**
* function to register required ajax function
*/
public function allRequiredAjaxAction(){
add_action('wp_ajax_ajaxUploadImage', array($this ,'ajaxUploadImage'));
add_action('wp_ajax_nopriv_ajaxUploadImage', array($this ,'ajaxUploadImage'));
add_action('wp_ajax_ajaxUpdateProfilePic', array($this ,'ajaxUpdateProfilePic'));
add_action('wp_ajax_nopriv_ajaxUpdateProfilePic', array($this ,'ajaxUpdateProfilePic'));
add_action('wp_ajax_ajaxGetUploadedImages', array($this ,'ajaxGetUploadedImages'));
add_action('wp_ajax_nopriv_ajaxGetUploadedImages', array($this ,'ajaxGetUploadedImages'));
add_action('wp_ajax_ajaxDeleteImage', array($this ,'ajaxDeleteImage'));
add_action('wp_ajax_nopriv_ajaxDeleteImage', array($this ,'ajaxDeleteImage'));
add_action('wp_ajax_takePicAdminRemoveUploadAjax',array($this,'takePicAdminRemoveUploadAjax'));
}
/**
* Admin menu
*/
public function takePicAdminMenu(){
if ( is_admin()):
add_menu_page( 'Take Pic',
'Take Pic' ,
'administrator',
'takePicAdminPanel',
array($this, 'takePicAdminHtml'),
'dashicons-camera-alt ',
'10');
endif;
}
/**
* Admin menu html
*/
public function takePicAdminHtml(){
?>
<div class="takePicAdminPanel">
<h1 class="dashicons-before dashicons-camera-alt">Take Pic</h1>
<p><b>Users with Image uploads and size</b></p>
<ul>
<?php
array_map(function($userObj){
$user = get_user_by('login', $userObj->user_login);
if( !empty($user->first_name) && !empty($user->last_name)){
$name = $user->first_name." ".$user->last_name;
}else{
$name = $userObj->user_login;
}
$uploadDir = wp_upload_dir();
$userDirname = $uploadDir['basedir']. '/' . $userObj->user_login;
if(file_exists($userDirname)):
?>
<div id="takePicUpload-<?=$userObj->user_login?>" class='plugin-notice notice notice-info notice-alt'
onMouseOver="this.style.boxShadow='2px 2px 3px lightblue'"
onMouseOut="this.style.boxShadow=''" style='width:40%;height:30px;padding-top:10px;' >
<span><b><?=$name.'<i> ('.$userObj->user_login.')</i>'?> :- <i> Upload size : <?=floor(filesize($userDirname))/1000?> mb</i></b><span>
<span style="float:right; margin-right:1%">
<a title="Delete images uploaded by user"
data-user-login="<?=$userObj->user_login?>"
onclick= 'takePicRemoveUpload(this.getAttribute("data-user-login"));'
class="dashicons-before dashicons-trash takePicDeleteUpload"
style="text-decoration:none;"
href="JavaScrip:void(0);"
></a></span>
</div>
<?php
endif;
},get_users( array( 'fields' => array( 'user_login' ) ) ));
?>
</ul>
</div>
<?php
}
/**
* function to remove user upload with javscript
* */
public function takePicRemoveUpload_javascript(){
?>
<script type="text/javascript">
function takePicRemoveUpload(userLogin){
if(confirm("Are you sure you want delete all uploads by the the user?")){
var xhttp = new XMLHttpRequest();
xhttp.open("POST",ajaxurl, true);
xhttp.responseType = "text";
xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
xhttp.onload = function () {
if (this.status >= 200 && this.status < 400) {
if(this.response === 'deleted'){
alert("Uploads sucessfully deleted");
var rowDelete = document.getElementById("takePicUpload-"+userLogin);
rowDelete.parentNode.removeChild(rowDelete);
}
else{
alert("Uploads couldn't be deleted. \nProbably, file permission issue, consult with your network adminstrator ");
}
} else {
alert(this.response);
}
};
xhttp.onerror = function() {
alert("connection error");
};
xhttp.send("action=takePicAdminRemoveUploadAjax&userlogin="+userLogin);
}
}
</script>
<?php
}
/**
* ajax function function tp handle remove request
*/
public function takePicAdminRemoveUploadAjax(){
$uploadDir = wp_upload_dir();
$userDir = $uploadDir['basedir'].'/'. $_POST['userlogin'];
$old = umask(0);
chmod($uploadDir['basedir'], 0777);
umask($old);
if(rmdir($userDir)):
echo deleted;
else:
echo __("Not deleted.",'take-pic');
endif;
wp_die();
}
/*
*
* frontend coding
*
*/
/**
* function to upload image with ajax
*/
public function ajaxUploadImage(){
if(is_user_logged_in()):
$current_user = wp_get_current_user();
$uploadDir = wp_upload_dir();
$userDirname = $uploadDir['basedir']. '/' . $current_user->user_login;
if(!file_exists($userDirname)):
if(wp_mkdir_p($userDirname)):
if($this->base64ToPngSave($_POST['image'],$userDirname)):
echo __("Image sucessfully uploaded",'take-pic');
else:
echo __("Image couldn't be uploaded at this time",'take-pic');
endif;
else:
echo __("Image couldn't be uploaded at this time",'take-pic');
endif;
else:
if($this->base64ToPngSave($_POST['image'],$userDirname)):
echo __("Image sucessfully uploaded",'take-pic');
else:
$this->base64ToPngSave($_POST['image'],$userDirname);
echo __("Couldn't upload image, try again later",'take-pic');
endif;
endif;
else:
echo __("You need to login to upload image",'take-pic');
endif;
wp_die();
}
/**
* Convert blob to png and save
*/
private function base64ToPngSave( $base64String,$directory){
$outPutFile = $directory.'/'.time().'.png';
if(strpos($base64String,'data:image/png;base64,') === 0):
if(file_put_contents($outPutFile, base64_decode(str_replace(' ', '+',str_replace('data:image/png;base64,', '',$base64String))))):
return true;
else:
return false;
endif;
endif;
}
/**
* function to delete image
*/
public function ajaxDeleteImage(){
$uploadDir = wp_upload_dir();
if(unlink($uploadDir['basedir'].str_replace(content_url().'/uploads', '', $_POST['image']))):
echo "deleted";
else:
echo __("Delete failed.",'take-pic');
endif;
wp_die();
}
/**
* function to get all uploaded
*/
public function ajaxGetUploadedImages(){
if(is_user_logged_in()):
$current_user = wp_get_current_user();
$uploadDir = wp_upload_dir();
$userDirname = $uploadDir['basedir']. '/' . $current_user->user_login;
if(file_exists($userDirname)):
$imageArray = scandir($userDirname);
if(count($imageArray)>2):
echo json_encode(array('dir'=>content_url().'/uploads/'.$current_user->user_login,'images'=>$imageArray));
else:
echo json_encode(array("noImage"));
endif;
else:
echo json_encode(array("noImage"));
endif;
else:
echo json_encode(array('notLoggedIn'=>__('You need to login to view images','take-pic')));
endif;
wp_die();
}
/**
* Register block for plugin
*
*/
public function takePicRegisterBlock(){
// Block Editor Script.
wp_register_script(
'take-pic-block-editor',
plugins_url( 'js/take-pic-block.js',__FILE__ ),
array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n','jsMasonry' ),
);
wp_localize_script( 'take-pic-block-editor', 'takePic', $this->getUserUploadedImgs());
register_block_type(
'take-pic/take-pic-block',
array(
'style' => '',
'editor_style' => '',
'editor_script' => 'take-pic-block-editor',
)
);
}
/**
* Get uploaded images by logged in user
* */
public function getUserUploadedImgs(){
$current_user = wp_get_current_user();
$uploadDir = wp_upload_dir();
$userDirname = $uploadDir['basedir']. '/' . $current_user->user_login;
$imgArr = scandir($userDirname);
unset($imgArr[0]);
unset($imgArr[1]);
return array( 'dirUrl'=>content_url().'/uploads/'.$current_user->user_login,
'files'=> $imgArr
);
}
/**
* since 2.5.0
* Eneque Js masonry on admin section
*
* */
public function enequeAdminJs(){
wp_enqueue_script('jsMasonry', plugins_url( 'js/js-masonry.js',__FILE__ ),array());
}
/**
*
* since 2.5.0
*
* Eneque forntend js files
*/
public function enequeFrontendJs(){
wp_enqueue_script('jsMasonry', plugins_url( 'js/js-masonry.js',__FILE__ ),array());
wp_enqueue_script('takePicFrontEndJs', plugins_url( 'js/take-pic-frontend.js',__FILE__ ),array('jsMasonry'));
wp_enqueue_script('ctcOverlayJs', plugins_url('js/ctc_overlay.js',__FILE__ ));
wp_enqueue_script('takePicJs', plugins_url('js/take-pic.js',__FILE__ ),array('jsCrop'));
wp_enqueue_script('jsCrop', plugins_url('js/js-crop.js',__FILE__ ));
wp_localize_script( 'takePicJs', 'my_ajax_url', admin_url( 'admin-ajax.php' ) );
wp_localize_script( 'takePicJs', 'takePicMessage', array(
'mobile_browser' => __("Mobile devices do not support this feature yet",'take-pic'),
'no_ssl' => __("You browser doesn't support webcam feature.",'take-pic'),
'connection_error' => __("Connection error.",'take-pic'),
'delete_confirm' => __("Are you sure you want to delete this image?",'take-pic'),
'delete_success' => __("Image sucessfully deleted.",'take-pic'),
'delete_failed' => __("Could't delete image at this time.",'take-pic'),
'profile_pic' => __("will be profile pic.",'take-pic'),
'no_image' => __("You do not have any image to display.",'take-pic'),
)) ;
}
}
new takePicPlugin();