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