| @@ -36,12 +36,14 @@ | ||
| 36 | 36 | $item_title = apply_filters( 'wppb_'.$form_location.'_avatar_custom_field_'.$field['id'].'_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_title_translation', $field['field-title'], true ) ); |
| 37 | 37 | $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 38 | 38 | |
| 39 | 39 | if( $form_location != 'register' ) { |
| 40 | - if( empty( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ) | |
| 40 | + $from_request = ! empty( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ); | |
| 41 | + | |
| 42 | + if( $from_request ) | |
| 43 | + $input_value = $request_data[wppb_handle_meta_name( $field['meta-name'] )]; | |
| 44 | + else | |
| 41 | 45 | $input_value = ( (wppb_user_meta_exists($user_id, $field['meta-name']) != null) ? get_user_meta($user_id, $field['meta-name'], true) : ''); |
| 42 | - else | |
| 43 | - $input_value = $request_data[wppb_handle_meta_name( $field['meta-name'] )]; | |
| 44 | 46 | |
| 45 | 47 | if ( is_array( $input_value ) ) { |
| 46 | 48 | $first_value = reset( $input_value ); |
| 47 | 49 | $input_value = is_scalar( $first_value ) ? $first_value : ''; |
| @@ -46,35 +48,12 @@ | ||
| 46 | 48 | $first_value = reset( $input_value ); |
| 47 | 49 | $input_value = is_scalar( $first_value ) ? $first_value : ''; |
| 48 | 50 | } |
| 49 | 51 | |
| 50 | - if( !empty( $input_value ) && is_string( $input_value ) && !is_numeric( $input_value ) && apply_filters( 'wppb_avatar_field_transform_file_to_attachment', true, $field ) ){ | |
| 51 | - /* we have a file url and we need to change it into an attachment */ | |
| 52 | - // Check the type of file. We'll use this as the 'post_mime_type'. | |
| 53 | - $wp_upload_dir = wp_upload_dir(); | |
| 54 | - $file_path = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir["basedir"], $input_value ); | |
| 55 | - //on windows os we might have \ instead of / so change them | |
| 56 | - $file_path = str_replace( "\\", "/", $file_path ); | |
| 57 | - $file_type = wp_check_filetype( basename( $input_value ), null ); | |
| 58 | - $attachment = array( | |
| 59 | - 'guid' => $input_value, | |
| 60 | - 'post_mime_type' => $file_type['type'], | |
| 61 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $input_value ) ), | |
| 62 | - 'post_content' => '', | |
| 63 | - 'post_status' => 'inherit' | |
| 64 | - ); | |
| 65 | - | |
| 66 | - // Insert the attachment. | |
| 67 | - $input_value = wp_insert_attachment( $attachment, $input_value, 0 ); | |
| 68 | - if( !empty( $input_value ) ) { | |
| 69 | - // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
| 70 | - require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
| 71 | - // Generate the metadata for the attachment, and update the database record. | |
| 72 | - $attach_data = wp_generate_attachment_metadata($input_value, $file_path); | |
| 73 | - wp_update_attachment_metadata($input_value, $attach_data); | |
| 74 | - /* save the new attachment instead of the url */ | |
| 75 | - update_user_meta( $user_id, $field['meta-name'], $input_value ); | |
| 76 | - } | |
| 52 | + /* user meta from old versions holds a file url; convert it into an attachment once. | |
| 53 | + Request data is never converted, so rendering the field cannot persist request input. */ | |
| 54 | + if( ! $from_request && !empty( $input_value ) && is_string( $input_value ) && !is_numeric( $input_value ) && apply_filters( 'wppb_avatar_field_transform_file_to_attachment', true, $field ) ){ | |
| 55 | + $input_value = wppb_legacy_file_url_to_attachment( $input_value, $field, $user_id ); | |
| 77 | 56 | } |
| 78 | 57 | } |
| 79 | 58 | else |
| 80 | 59 | $input_value = !empty( $_POST[$field['meta-name']] ) ? sanitize_text_field( $_POST[$field['meta-name']] ) : ''; |
| @@ -123,9 +102,9 @@ | ||
| 123 | 102 | if ( wppb_simple_upload_was_submitted( $field, $request_data ) ) { |
| 124 | 103 | if ( !( isset( $field[ 'conditional-logic-enabled' ] ) && $field[ 'conditional-logic-enabled' ] == 'yes' && !isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ) ){ |
| 125 | 104 | if ( isset( $_FILES[ $field_name ][ 'size' ] ) && $_FILES[ $field_name ][ 'size' ] == 0 ){ |
| 126 | 105 | if ( isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ){ |
| 127 | - update_user_meta( $user_id, $field[ 'meta-name' ], sanitize_text_field( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ); | |
| 106 | + wppb_save_attachment_id( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ], $field, $user_id ); | |
| 128 | 107 | } |
| 129 | 108 | } |
| 130 | 109 | else{ |
| 131 | 110 | $attachment_id = $request_data[ $field[ 'meta-name' ] ]; |