forked from alimuzzaman/author-or-user-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthor___Image.php
302 lines (269 loc) · 9.21 KB
/
Author___Image.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
<?php
/**
* @package Author_Image
* @version 1.0.1
*/
/*
Plugin Name:WordPress Author Image
Plugin URI: http://www.zm-tech.net/wp-plugins/wordpress-author-or-user-image/
Description: This plugin is for Author Image. You and your users will be able to set image. The image will be appears in comment and about author section.
Author: Alimuzzaman Alim
Version: 1.0.2
Author URI: http://www.zm-tech.net/wp-plugins/wordpress-author-or-user-image/
*/
function form_submit_work()
{
if(wp_verify_nonce( $_POST['___wpnonce'] ,'author-image-size'))
{
$user_ID = get_current_user_id(); //Get the current user ID
$dir = WP_CONTENT_DIR . "/uploads/author image/"; // Author Image upload DIR
$size=get_option("author_image_size",array('h'=>'150','width'=>'150'));
wp_mkdir_p($dir); // Creat AI DIR if not exists
if(isset($_POST['size']) and ($size!=$_POST['size']))
{
$size=$_POST['size'];
if(!add_option("author_image_size",$size,"",'no'))
{
update_option("author_image_size",$size);
$d = dir($dir);
while ($file = $d->read())
{
$image = wp_get_image_editor($dir. $file);
if(!is_wp_error($image))
{
$s=$image->resize($size['width'], $size[h]);
$image->save($dir. $file );
}
}
}
}
if(isset($_POST['author_default_image']) && is_file($dir."author_default.jpg"))
unlink($dir."author_default.jpg"); //Remove default imgae if "Default" check box is checked
if((isset($_POST['author_image']) or (!isset($_POST['ad']) and isset($_POST['rem']))) && is_file($dir.$user_ID .".jpg"))
unlink($dir.$user_ID .".jpg"); //Remove default imgae if check box is checked
if (!empty($_FILES['author_default']['tmp_name']))
up_a_image($_FILES['author_default'],$dir."author_default.jpg",$size);
if (!empty($_FILES['author_curr']['tmp_name']))
up_a_image($_FILES['author_curr'],$dir.$user_ID.".jpg",$size);
}
else
wp_die("Sory unothorized access.",'Error');
}
// Function up_a_image($uploaded_file,$path,$size)
// This function resize image and upload image from temp folder to upload folder
// $uploaded_file = The file what has been uploaded. Ex. $_FILSES
// $path = Destination path.
// $size = The size of new image
// return = nuthing
function up_a_image($uploaded_file,$path,$size)
{
$wp_filetype = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'], false );
if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) )
wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
$image = wp_get_image_editor( $uploaded_file["tmp_name"] );
if ( ! is_wp_error( $image ) )
{
$image->resize($size['width'], $size[heigth] );
$image->save( $path );
}
else
wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
}
function mainContent()
{
$dir = WP_CONTENT_DIR . '/uploads/author image/';
$nonce=wp_create_nonce( 'author-image-size' );
// Create an nonce for a link.
// We pass it as a GET parameter.
// The target page will perform some action based on the 'do_something' parameter.
?>
<div class="wrap">
<?php screen_icon();?>
<h2>Author Image</h2>
<form method="post" enctype="multipart/form-data" >
<?php
settings_fields('author_image_size');
?>
<input type="hidden" id="___wpnonce" name="___wpnonce" value="<?php echo $nonce;?>">
<table class="form-table">
<tbody>
<?php
if(current_user_can("administrator"))
{
?>
<tr valign="top">
<th scope="row"><label for="author_default">Default Image</label></th>
<td><input name="author_default" id="author_default" type="file">
<br>
<?php
if(file_exists($dir. 'author_default.jpg'))
{
$qq=get_option( 'siteurl' ). '/wp-content/uploads/author image/author_default.jpg';
echo "<br /><img width='200px' src='{$qq}' alt='' />";
}
?></td>
</tr>
<?php
}
?>
<tr valign="top">
<th scope="row"><label for="author_curr">Yours Image</label></th>
<td><input name="author_curr" id="author_curr" type="file">
<br>
<?php
if(file_exists($dir.get_current_user_id().'.jpg'))
{
$qq=get_option( 'siteurl' ). '/wp-content/uploads/author image/'.get_current_user_id().'.jpg';
echo "<img width='200px' src='$qq' alt='' />";
}
?>
<br></td>
</tr>
<?php
if(current_user_can("administrator"))
{
?>
<tr>
<td colspan="2"><h3 title="This is maintain accepts ratio">Resize Image</h3></td>
</tr>
<?php $size=get_option("author_image_size",array('h'=>'150','width'=>'150')); ?>
<tr valign="top">
<th> <label scope="row" for="size[h]">Height of image:</label>
</th>
<td><input type="text" name='size[h]' id='size[h]' value='<?php echo $size['h']; ?>' /></td>
</tr>
<tr>
<th> <label scope="row" for="size[width]"> Width of image: </label>
</th>
<td><input type="text" name='size[width]' id='size[width]' value='<?php echo $size['width']; ?>' /></td>
</tr>
<?php
}
?>
</tbody>
</table>
<p class="submit">
<input type="submit" name="submit" class="button-primary" value="Save Image" />
</p>
</form>
<br />
<br />
<br />
<?php screen_icon();?>
<h2>Remove Picture</h2>
<form method="post">
<?php
settings_fields('author_image_size');
?>
<input type="hidden" id="___wpnonce" name="___wpnonce" value="<?php echo $nonce;?>">
<input type="hidden" name="rem" value="true" />
<p style="margin-left:22px" >
<label>
<?php if(current_user_can("administrator"))
{
?>
<input type="hidden" name="ad" value="true" />
<input type="checkbox" name="author_default_image" value="true" id="CheckboxGroup1_0"
<?php
if(!file_exists($dir.'author_default.jpg'))
echo'disabled';
?>
/>
Default</label>
<br />
<br />
<br />
<label>
<input type="checkbox" name="author_image" value="true" id="CheckboxGroup1_1"
<?php
if(!file_exists($dir.get_current_user_id().'.jpg'))
echo'disabled';
?>
/>
Your</label>
<br />
<?php } ?>
</p>
<p class="submit">
<input type="hidden" id="___wpnonce" name="___wpnonce" value="<?php echo $nonce;?>">
<input type="submit" name="submit" class="button-primary" value="Remove Picture"
<?php
if(current_user_can("administrator"))
{
if(!file_exists($dir.get_current_user_id().'.jpg') and !file_exists($dir.'author_default.jpg'))
echo'disabled';
}
else
if(!file_exists($dir.get_current_user_id().'.jpg'))
echo'disabled';
?>
/>
</p>
</p>
</form>
</div>
<?php
}
function add_page()
{
add_users_page('Author Image', 'Author Image','read',__FILE__,'mainContent');
}
//add_action("load-$page", aimage::form_submit_work(""), 49);
add_action("admin_menu",'add_page');
add_action("admin_init",'adinit');
function adinit()
{
if(isset($_POST['_wp_http_referer']))
if(ereg('Author___Image',$_POST['_wp_http_referer']))
if(isset($_POST['rem']) or $_FILES)
form_submit_work();
$dir = WP_CONTENT_DIR . '/uploads/author image/';
if(!file_exists($dir.'author_default.jpg'))
add_action( 'admin_notices', '_site_admin_notice' );
}
function _site_admin_notice()
{?>
<div id="message1" class="updated below-h1"><p>Default Image is not set yet. <a href="users.php?page=Author_Image/Author___Image.php">Please set a default Image.</a></p></div>
<?php }
if ( !function_exists( 'get_avatar' ) ) :
function get_avatar( $id_or_email="1", $size = '96px', $default = '', $alt = 'Author Image' )
{
if ( is_numeric($id_or_email) )
$id=$id_or_email;
elseif ( is_object($id_or_email) )
{
// No avatar for pingbacks or trackbacks
$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
return false;
if ( !empty($id_or_email->user_id) )
$id = (int) $id_or_email->user_id;
else
{
$email = $id_or_email->comment_author_email;
$us=get_user_by('email', $email);
$id=$us->id;
}
}
else
{
$us=get_user_by('email', $id_or_email);
$id=$us->id;
}
$dir = WP_CONTENT_DIR . '/uploads/author image/';
if(file_exists($dir.$id.'.jpg'))
$op=get_option( 'siteurl' ). '/wp-content/uploads/author image/'.$id.'.jpg';
else
{
if(file_exists($dir.'author_default.jpg'))
$op=get_option( 'siteurl' ). '/wp-content/uploads/author image/author_default.jpg';
else
$op=false;
}
if ($op)
$avatar = "<img alt='{$alt}' title='Author Image' src='{$op}' class='avatar avatar-{$size} photo avatar-default' width='{$size}' />";
else
$avatar="<img alt='{$alt}' title='Author Image' src='{plugin_dir_path(__FILE__)}default-user-image.png' class='avatar avatar-{$size} photo avatar-default' width='{$size}' />";
return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
}
endif;