PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.74
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.74
1.2.74 1.2.73 1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 All 174 releases
← All changes | includes/class-files.php +97 -33 1.0.191.2.74 View file →
@@ -28,12 +28,12 @@
28 28 if (isset($extra_fields['uwp_file_types']) && !in_array("*", $extra_fields['uwp_file_types'])) {
29 29 $allowed_mime_types = $extra_fields['uwp_file_types'];
30 30 }
31 31
32 - $allowed_mime_types = apply_filters('uwp_allowed_mime_types', $allowed_mime_types, $field->htmlvar_name);
32 + $allowed_mime_types = apply_filters('uwp_fields_allowed_mime_types', $allowed_mime_types, $field->htmlvar_name);
33 33
34 34 $file_urls = array();
35 - $files_to_upload = $this->uwp_prepare_files( $files[ $field->htmlvar_name ] );
35 + $files_to_upload = $this->prepare_files( $files[ $field->htmlvar_name ] );
36 36
37 37 $max_upload_size = $this->uwp_get_max_upload_size($field->form_type, $field->htmlvar_name);
38 38
39 39 if ( ! $max_upload_size ) {
@@ -42,9 +42,9 @@
42 42
43 43 foreach ( $files_to_upload as $file_key => $file_to_upload ) {
44 44
45 45 if (!empty($allowed_mime_types)) {
46 - $ext = $this->uwp_get_file_type($file_to_upload['type']);
46 + $ext = $this->get_file_type($file_to_upload['type']);
47 47
48 48 $allowed_error_text = implode(', ', $allowed_mime_types);
49 49 if ( !in_array( $ext , $allowed_mime_types ) )
50 50 return new WP_Error( 'validation-error', sprintf( __( 'Allowed files types are: %s', 'userswp' ), $allowed_error_text) );
@@ -60,14 +60,14 @@
60 60 if (is_wp_error($error_result)) {
61 61 return $error_result;
62 62 }
63 63
64 - remove_filter( 'wp_handle_upload_prefilter', array($this, 'uwp_wp_media_restrict_file_types') );
65 - if(in_array($field->htmlvar_name, array('uwp_banner_file','uwp_avatar_file'))){
64 + remove_filter( 'wp_handle_upload_prefilter', array($this, 'wp_media_restrict_file_types') );
65 + if(in_array($field->htmlvar_name, array('avatar', 'banner'))){
66 66 add_filter( 'upload_dir', 'uwp_handle_multisite_profile_image', 10, 1 );
67 67 }
68 - $uploaded_file = $this->uwp_upload_file( $file_to_upload, array( 'file_key' => $file_key ) );
69 - add_filter( 'wp_handle_upload_prefilter', array($this, 'uwp_wp_media_restrict_file_types') );
68 + $uploaded_file = $this->upload_file( $file_to_upload, array( 'file_key' => $file_key ) );
69 + add_filter( 'wp_handle_upload_prefilter', array($this, 'wp_media_restrict_file_types') );
70 70
71 71 if ( is_wp_error( $uploaded_file ) ) {
72 72
73 73 return new WP_Error( 'validation-error', $uploaded_file->get_error_message() );
@@ -155,8 +155,9 @@
155 155 */
156 156 public function uwp_get_size_in_bytes($val) {
157 157 $val = trim($val);
158 158 $last = strtolower($val[strlen($val)-1]);
159 + $val = substr($val, 0, -1);
159 160 switch($last) {
160 161 // The 'G' modifier is available since PHP 5.1.0
161 162 case 'g':
162 163 $val *= (1024 * 1024 * 1024); //1073741824
@@ -180,12 +181,13 @@
180 181 * @param array $file File info to upload.
181 182 * @param array $args File upload helper args.
182 183 * @return object Uploaded file info
183 184 */
184 - public function uwp_upload_file( $file, $args = array() ) {
185 + public function upload_file( $file, $args = array() ) {
185 186
186 187 include_once ABSPATH . 'wp-admin/includes/file.php';
187 188 include_once ABSPATH . 'wp-admin/includes/media.php';
189 + include_once ABSPATH . 'wp-admin/includes/image.php';
188 190
189 191 $args = wp_parse_args( $args, array(
190 192 'file_key' => '',
191 193 'file_label' => '',
@@ -201,11 +203,32 @@
201 203 return new WP_Error( 'upload', sprintf( __( 'Uploaded files need to be one of the following file types: %s', 'userswp' ), implode( ', ', array_keys( $args['allowed_mime_types'] ) ) ) );
202 204 }
203 205 } else {
204 206 $upload = wp_handle_upload( $file, apply_filters( 'uwp_handle_upload_overrides', array( 'test_form' => false ) ) );
207 +
205 208 if ( ! empty( $upload['error'] ) ) {
206 209 return new WP_Error( 'upload', $upload['error'] );
207 210 } else {
211 + if ( ! empty( $upload['type'] ) && $upload['type'] != 'image/png' && strpos( $upload['type'], 'image/' ) === 0 ) {
212 + // Fetch additional metadata from EXIF/IPTC.
213 + $exif_meta = wp_read_image_metadata( $upload['file'] );
214 +
215 + if ( ! empty( $exif_meta ) && is_array( $exif_meta ) && ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
216 + $editor = wp_get_image_editor( $upload['file'] );
217 +
218 + if ( ! empty( $editor ) && ! is_wp_error( $editor ) ) {
219 + // Rotate the whole original image if there is EXIF data and "orientation" is not 1.
220 + $rotated = $editor->maybe_exif_rotate();
221 + $rotated = $rotated === true ? $editor->save( $editor->generate_filename( 'rotated' ) ) : false;
222 +
223 + if ( ! empty( $rotated ) && ! is_wp_error( $rotated ) && ! empty( $rotated['path'] ) ) {
224 + $upload['url'] = str_replace( basename( $upload['url'] ), basename( $rotated['path'] ), $upload['url'] );
225 + $upload['file'] = $rotated['path'];
226 + }
227 + }
228 + }
229 + }
230 +
208 231 $uploaded_file->url = $upload['url'];
209 232 $uploaded_file->name = basename( $upload['file'] );
210 233 $uploaded_file->path = $upload['file'];
211 234 $uploaded_file->type = $upload['type'];
@@ -213,9 +236,8 @@
213 236 $uploaded_file->extension = substr( strrchr( $uploaded_file->name, '.' ), 1 );
214 237 }
215 238 }
216 239
217 -
218 240 return $uploaded_file;
219 241 }
220 242
221 243 /**
@@ -225,9 +247,9 @@
225 247 * @package userswp
226 248 * @param array $file_data Files to upload
227 249 * @return array Prepared files.
228 250 */
229 - public function uwp_prepare_files( $file_data ) {
251 + public function prepare_files( $file_data ) {
230 252 $files_to_upload = array();
231 253
232 254 if ( is_array( $file_data['name'] ) ) {
233 255 foreach ( $file_data['name'] as $file_data_key => $file_data_value ) {
@@ -259,9 +281,9 @@
259 281 * @param bool $url_only Return only the url or whole file info?
260 282 * @param array|bool $fields Form fields.
261 283 * @return array Validated data.
262 284 */
263 - public function uwp_validate_uploads($files, $type, $url_only = true, $fields = false) {
285 + public function validate_uploads($files, $type, $url_only = true, $fields = false) {
264 286
265 287 $validated_data = array();
266 288
267 289 if (empty($files)) {
@@ -280,26 +302,23 @@
280 302 $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type = 'file' AND is_active = '1' ORDER BY sort_order ASC", array($type)));
281 303 }
282 304 }
283 305
306 + if ( ! empty( $fields ) ) {
307 + foreach ( $fields as $field ) {
308 + if ( isset( $files[ $field->htmlvar_name ] ) && ! empty( $files[ $field->htmlvar_name ]['name'] ) ) {
309 + $file_urls = $this->handle_file_upload( $field, $files );
284 310
285 - if (!empty($fields)) {
286 - foreach ($fields as $field) {
287 - if(isset($files[$field->htmlvar_name])) {
288 -
289 - $file_urls = $this->handle_file_upload($field, $files);
290 -
291 - if (is_wp_error($file_urls)) {
311 + if ( is_wp_error( $file_urls ) ) {
292 312 return $file_urls;
293 313 }
294 314
295 - if ($url_only) {
315 + if ( $url_only ) {
296 316 $validated_data[$field->htmlvar_name] = $file_urls['url'];
297 317 } else {
298 318 $validated_data[$field->htmlvar_name] = $file_urls;
299 319 }
300 320 }
301 -
302 321 }
303 322 }
304 323
305 324 return $validated_data;
@@ -314,17 +333,17 @@
314 333 * @param string $value Value of the field.
315 334 * @param bool $removable Is this value removable by user?
316 335 * @return string HTML output.
317 336 */
318 - public function uwp_file_upload_preview($field, $value, $removable = true) {
337 + public function file_upload_preview($field, $value, $removable = true) {
319 338 $output = '';
320 339
321 340 $value = esc_html($value);
322 341
323 - if ($field->htmlvar_name == "uwp_banner_file") {
324 - $htmlvar = "uwp_account_banner_thumb";
325 - } elseif ($field->htmlvar_name == "uwp_avatar_file") {
326 - $htmlvar = "uwp_account_avatar_thumb";
342 + if ($field->htmlvar_name == "banner") {
343 + $htmlvar = "banner_thumb";
344 + } elseif ($field->htmlvar_name == "avatar") {
345 + $htmlvar = "avatar_thumb";
327 346 } else {
328 347 $htmlvar = $field->htmlvar_name;
329 348 }
330 349
@@ -332,10 +351,9 @@
332 351 if ( is_admin() && defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
333 352 $user_id = get_current_user_id();
334 353 // If is another user's profile page
335 354 } elseif (is_admin() && ! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
336 - $user_id = $_GET['user_id'];
337 - $user_id = (int) sanitize_text_field($user_id);
355 + $user_id = absint( $_GET['user_id'] );
338 356 // Otherwise something is wrong.
339 357 } else {
340 358 $user_id = get_current_user_id();
341 359 }
@@ -353,9 +371,9 @@
353 371 if (in_array($filetype['ext'], $image_types)) {
354 372 $output .= '<div class="uwp_file_preview_wrap">';
355 373 $output .= '<a href="'.$value.'" class="uwp_upload_file_preview"><img style="max-width:100px;" src="'.$value.'" /></a>';
356 374 if ($removable) {
357 - $output .= '<a onclick="return confirm(\'are you sure?\')" style="display: block;margin: 5px 0;" href="#" id="'.$htmlvar.'" data-htmlvar="'.$htmlvar.'" data-uid="'.$user_id.'" class="uwp_upload_file_remove">'. __( 'Remove Image' , 'userswp' ).'</a>';
375 + $output .= '<a onclick="return confirm(\'Are you sure?\')" style="display: block;margin: 5px 0;" href="#" id="'.$htmlvar.'" data-htmlvar="'.$htmlvar.'" data-uid="'.$user_id.'" class="uwp_upload_file_remove">'. __( 'Remove Image' , 'userswp' ).'</a>';
358 376 }
359 377 $output .= '</div>';
360 378 ?>
361 379 <?php
@@ -362,9 +380,9 @@
362 380 } else {
363 381 $output .= '<div class="uwp_file_preview_wrap">';
364 382 $output .= '<a href="'.$value.'" class="uwp_upload_file_preview">'.$file.'</a>';
365 383 if ($removable) {
366 - $output .= '<a onclick="return confirm(\'are you sure?\')" style="display: block;margin: 5px 0;" href="#" id="'.$htmlvar.'" data-htmlvar="'.$htmlvar.'" data-uid="'.$user_id.'" class="uwp_upload_file_remove">'. __( 'Remove File' , 'userswp' ).'</a>';
384 + $output .= '<a onclick="return confirm(\'Are you sure?\')" style="display: block;margin: 5px 0;" href="#" id="'.$htmlvar.'" data-htmlvar="'.$htmlvar.'" data-uid="'.$user_id.'" class="uwp_upload_file_remove">'. __( 'Remove File' , 'userswp' ).'</a>';
367 385 }
368 386 $output .= '</div>';
369 387 ?>
370 388 <?php
@@ -380,9 +398,9 @@
380 398 * @package userswp
381 399 * @param array $file File info.
382 400 * @return array Modified file info.
383 401 */
384 - public function uwp_wp_media_restrict_file_types($file) {
402 + public function wp_media_restrict_file_types($file) {
385 403 // This bit is for the flash uploader
386 404 if ($file['type']=='application/octet-stream' && isset($file['tmp_name'])) {
387 405 $file_size = getimagesize($file['tmp_name']);
388 406 if (isset($file_size['error']) && $file_size['error']!=0) {
@@ -394,9 +412,9 @@
394 412 }
395 413 list($category,$type) = explode('/',$file['type']);
396 414 if ('image'!=$category || !in_array($type,array('jpg','jpeg','gif','png'))) {
397 415 $file['error'] = "Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file.";
398 - } else if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
416 + } else if ($post_id = (isset($_REQUEST['post_id']) ? absint($_REQUEST['post_id']) : false)) {
399 417 if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0)
400 418 $file['error'] = "Sorry, you cannot upload more than one (1) image.";
401 419 }
402 420 return $file;
@@ -408,9 +426,9 @@
408 426 * @since 1.0.0
409 427 * @package userswp
410 428 * @return bool
411 429 */
412 - public function uwp_doing_upload(){
430 + public function doing_upload(){
413 431 return isset($_POST['uwp_profile_upload']) ? true : false;
414 432 }
415 433
416 434 /**
@@ -479,9 +497,9 @@
479 497 * @param string $ext Extension string. Ex: png, jpg
480 498 *
481 499 * @return string File type.
482 500 */
483 - public function uwp_get_file_type($ext) {
501 + public function get_file_type($ext) {
484 502 $allowed_file_types = $this->allowed_mime_types();
485 503 $file_types = array();
486 504 foreach ( $allowed_file_types as $format => $types ) {
487 505 $file_types = array_merge($file_types, $types);
@@ -556,6 +574,52 @@
556 574 )
557 575 )
558 576 );
559 577 }
578 +
579 + /**
580 + * Initiate the WordPress file system and provide fallback if needed.
581 + *
582 + * @since 1.2.2
583 + * @package userswp
584 + * @return bool|string Returns the file system class on success. False on failure.
585 + */
586 + public static function uwp_init_filesystem() {
587 +
588 + if ( ! function_exists( 'get_filesystem_method' ) ) {
589 + require_once( ABSPATH . "/wp-admin/includes/file.php" );
590 + }
591 + $access_type = get_filesystem_method();
592 + if ( $access_type === 'direct' ) {
593 + /* you can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */
594 + $creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() );
595 +
596 + /* initialize the API */
597 + if ( ! WP_Filesystem( $creds ) ) {
598 + /* any problems and we exit */
599 + return false;
600 + }
601 +
602 + global $wp_filesystem;
603 +
604 + return $wp_filesystem;
605 + /* do our file manipulations below */
606 + } elseif ( defined( 'FTP_USER' ) ) {
607 + $creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() );
608 +
609 + /* initialize the API */
610 + if ( ! WP_Filesystem( $creds ) ) {
611 + /* any problems and we exit */
612 + return false;
613 + }
614 +
615 + global $wp_filesystem;
616 +
617 + return $wp_filesystem;
618 +
619 + } else {
620 + return false;
621 + }
622 +
623 + }
560 624
561 625 }