| @@ -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() ); |
| @@ -181,12 +181,13 @@ | ||
| 181 | 181 | * @param array $file File info to upload. |
| 182 | 182 | * @param array $args File upload helper args. |
| 183 | 183 | * @return object Uploaded file info |
| 184 | 184 | */ |
| 185 | - public function uwp_upload_file( $file, $args = array() ) { | |
| 185 | + public function upload_file( $file, $args = array() ) { | |
| 186 | 186 | |
| 187 | 187 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 188 | 188 | include_once ABSPATH . 'wp-admin/includes/media.php'; |
| 189 | + include_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 189 | 190 | |
| 190 | 191 | $args = wp_parse_args( $args, array( |
| 191 | 192 | 'file_key' => '', |
| 192 | 193 | 'file_label' => '', |
| @@ -202,11 +203,32 @@ | ||
| 202 | 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'] ) ) ) ); |
| 203 | 204 | } |
| 204 | 205 | } else { |
| 205 | 206 | $upload = wp_handle_upload( $file, apply_filters( 'uwp_handle_upload_overrides', array( 'test_form' => false ) ) ); |
| 207 | + | |
| 206 | 208 | if ( ! empty( $upload['error'] ) ) { |
| 207 | 209 | return new WP_Error( 'upload', $upload['error'] ); |
| 208 | 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 | + | |
| 209 | 231 | $uploaded_file->url = $upload['url']; |
| 210 | 232 | $uploaded_file->name = basename( $upload['file'] ); |
| 211 | 233 | $uploaded_file->path = $upload['file']; |
| 212 | 234 | $uploaded_file->type = $upload['type']; |
| @@ -214,9 +236,8 @@ | ||
| 214 | 236 | $uploaded_file->extension = substr( strrchr( $uploaded_file->name, '.' ), 1 ); |
| 215 | 237 | } |
| 216 | 238 | } |
| 217 | 239 | |
| 218 | - | |
| 219 | 240 | return $uploaded_file; |
| 220 | 241 | } |
| 221 | 242 | |
| 222 | 243 | /** |
| @@ -226,9 +247,9 @@ | ||
| 226 | 247 | * @package userswp |
| 227 | 248 | * @param array $file_data Files to upload |
| 228 | 249 | * @return array Prepared files. |
| 229 | 250 | */ |
| 230 | - public function uwp_prepare_files( $file_data ) { | |
| 251 | + public function prepare_files( $file_data ) { | |
| 231 | 252 | $files_to_upload = array(); |
| 232 | 253 | |
| 233 | 254 | if ( is_array( $file_data['name'] ) ) { |
| 234 | 255 | foreach ( $file_data['name'] as $file_data_key => $file_data_value ) { |
| @@ -260,9 +281,9 @@ | ||
| 260 | 281 | * @param bool $url_only Return only the url or whole file info? |
| 261 | 282 | * @param array|bool $fields Form fields. |
| 262 | 283 | * @return array Validated data. |
| 263 | 284 | */ |
| 264 | - public function uwp_validate_uploads($files, $type, $url_only = true, $fields = false) { | |
| 285 | + public function validate_uploads($files, $type, $url_only = true, $fields = false) { | |
| 265 | 286 | |
| 266 | 287 | $validated_data = array(); |
| 267 | 288 | |
| 268 | 289 | if (empty($files)) { |
| @@ -281,26 +302,23 @@ | ||
| 281 | 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))); |
| 282 | 303 | } |
| 283 | 304 | } |
| 284 | 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 ); | |
| 285 | 310 | |
| 286 | - if (!empty($fields)) { | |
| 287 | - foreach ($fields as $field) { | |
| 288 | - if(isset($files[$field->htmlvar_name])) { | |
| 289 | - | |
| 290 | - $file_urls = $this->handle_file_upload($field, $files); | |
| 291 | - | |
| 292 | - if (is_wp_error($file_urls)) { | |
| 311 | + if ( is_wp_error( $file_urls ) ) { | |
| 293 | 312 | return $file_urls; |
| 294 | 313 | } |
| 295 | 314 | |
| 296 | - if ($url_only) { | |
| 315 | + if ( $url_only ) { | |
| 297 | 316 | $validated_data[$field->htmlvar_name] = $file_urls['url']; |
| 298 | 317 | } else { |
| 299 | 318 | $validated_data[$field->htmlvar_name] = $file_urls; |
| 300 | 319 | } |
| 301 | 320 | } |
| 302 | - | |
| 303 | 321 | } |
| 304 | 322 | } |
| 305 | 323 | |
| 306 | 324 | return $validated_data; |
| @@ -315,17 +333,17 @@ | ||
| 315 | 333 | * @param string $value Value of the field. |
| 316 | 334 | * @param bool $removable Is this value removable by user? |
| 317 | 335 | * @return string HTML output. |
| 318 | 336 | */ |
| 319 | - public function uwp_file_upload_preview($field, $value, $removable = true) { | |
| 337 | + public function file_upload_preview($field, $value, $removable = true) { | |
| 320 | 338 | $output = ''; |
| 321 | 339 | |
| 322 | 340 | $value = esc_html($value); |
| 323 | 341 | |
| 324 | - if ($field->htmlvar_name == "uwp_banner_file") { | |
| 325 | - $htmlvar = "uwp_account_banner_thumb"; | |
| 326 | - } elseif ($field->htmlvar_name == "uwp_avatar_file") { | |
| 327 | - $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"; | |
| 328 | 346 | } else { |
| 329 | 347 | $htmlvar = $field->htmlvar_name; |
| 330 | 348 | } |
| 331 | 349 | |
| @@ -333,10 +351,9 @@ | ||
| 333 | 351 | if ( is_admin() && defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) { |
| 334 | 352 | $user_id = get_current_user_id(); |
| 335 | 353 | // If is another user's profile page |
| 336 | 354 | } elseif (is_admin() && ! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) { |
| 337 | - $user_id = $_GET['user_id']; | |
| 338 | - $user_id = (int) sanitize_text_field($user_id); | |
| 355 | + $user_id = absint( $_GET['user_id'] ); | |
| 339 | 356 | // Otherwise something is wrong. |
| 340 | 357 | } else { |
| 341 | 358 | $user_id = get_current_user_id(); |
| 342 | 359 | } |
| @@ -354,9 +371,9 @@ | ||
| 354 | 371 | if (in_array($filetype['ext'], $image_types)) { |
| 355 | 372 | $output .= '<div class="uwp_file_preview_wrap">'; |
| 356 | 373 | $output .= '<a href="'.$value.'" class="uwp_upload_file_preview"><img style="max-width:100px;" src="'.$value.'" /></a>'; |
| 357 | 374 | if ($removable) { |
| 358 | - $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>'; | |
| 359 | 376 | } |
| 360 | 377 | $output .= '</div>'; |
| 361 | 378 | ?> |
| 362 | 379 | <?php |
| @@ -363,9 +380,9 @@ | ||
| 363 | 380 | } else { |
| 364 | 381 | $output .= '<div class="uwp_file_preview_wrap">'; |
| 365 | 382 | $output .= '<a href="'.$value.'" class="uwp_upload_file_preview">'.$file.'</a>'; |
| 366 | 383 | if ($removable) { |
| 367 | - $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>'; | |
| 368 | 385 | } |
| 369 | 386 | $output .= '</div>'; |
| 370 | 387 | ?> |
| 371 | 388 | <?php |
| @@ -381,9 +398,9 @@ | ||
| 381 | 398 | * @package userswp |
| 382 | 399 | * @param array $file File info. |
| 383 | 400 | * @return array Modified file info. |
| 384 | 401 | */ |
| 385 | - public function uwp_wp_media_restrict_file_types($file) { | |
| 402 | + public function wp_media_restrict_file_types($file) { | |
| 386 | 403 | // This bit is for the flash uploader |
| 387 | 404 | if ($file['type']=='application/octet-stream' && isset($file['tmp_name'])) { |
| 388 | 405 | $file_size = getimagesize($file['tmp_name']); |
| 389 | 406 | if (isset($file_size['error']) && $file_size['error']!=0) { |
| @@ -395,9 +412,9 @@ | ||
| 395 | 412 | } |
| 396 | 413 | list($category,$type) = explode('/',$file['type']); |
| 397 | 414 | if ('image'!=$category || !in_array($type,array('jpg','jpeg','gif','png'))) { |
| 398 | 415 | $file['error'] = "Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file."; |
| 399 | - } 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)) { | |
| 400 | 417 | if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0) |
| 401 | 418 | $file['error'] = "Sorry, you cannot upload more than one (1) image."; |
| 402 | 419 | } |
| 403 | 420 | return $file; |
| @@ -409,9 +426,9 @@ | ||
| 409 | 426 | * @since 1.0.0 |
| 410 | 427 | * @package userswp |
| 411 | 428 | * @return bool |
| 412 | 429 | */ |
| 413 | - public function uwp_doing_upload(){ | |
| 430 | + public function doing_upload(){ | |
| 414 | 431 | return isset($_POST['uwp_profile_upload']) ? true : false; |
| 415 | 432 | } |
| 416 | 433 | |
| 417 | 434 | /** |
| @@ -480,9 +497,9 @@ | ||
| 480 | 497 | * @param string $ext Extension string. Ex: png, jpg |
| 481 | 498 | * |
| 482 | 499 | * @return string File type. |
| 483 | 500 | */ |
| 484 | - public function uwp_get_file_type($ext) { | |
| 501 | + public function get_file_type($ext) { | |
| 485 | 502 | $allowed_file_types = $this->allowed_mime_types(); |
| 486 | 503 | $file_types = array(); |
| 487 | 504 | foreach ( $allowed_file_types as $format => $types ) { |
| 488 | 505 | $file_types = array_merge($file_types, $types); |
| @@ -557,6 +574,52 @@ | ||
| 557 | 574 | ) |
| 558 | 575 | ) |
| 559 | 576 | ); |
| 560 | 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 | + } | |
| 561 | 624 | |
| 562 | 625 | } |