| @@ -8,10 +8,11 @@ | ||
| 8 | 8 | if ( $field['field'] == 'Avatar' ){ |
| 9 | 9 | |
| 10 | 10 | $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] ); |
| 11 | 11 | |
| 12 | - /* media upload add here, this should be added just once even if called multiple times */ | |
| 13 | - wp_enqueue_media(); | |
| 12 | + if ( ! wppb_use_simple_upload_field( $field ) ) { | |
| 13 | + wp_enqueue_media(); | |
| 14 | + } | |
| 14 | 15 | /* propper way to dequeue. add to functions file in theme or custom plugin |
| 15 | 16 | function wppb_dequeue_script() { |
| 16 | 17 | wp_script_is( 'wppb-upload-script', 'enqueued' ); //true |
| 17 | 18 | wp_dequeue_script( 'wppb-upload-script' ); |
| @@ -35,12 +36,14 @@ | ||
| 35 | 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 ) ); |
| 36 | 37 | $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 37 | 38 | |
| 38 | 39 | if( $form_location != 'register' ) { |
| 39 | - 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 | |
| 40 | 45 | $input_value = ( (wppb_user_meta_exists($user_id, $field['meta-name']) != null) ? get_user_meta($user_id, $field['meta-name'], true) : ''); |
| 41 | - else | |
| 42 | - $input_value = $request_data[wppb_handle_meta_name( $field['meta-name'] )]; | |
| 43 | 46 | |
| 44 | 47 | if ( is_array( $input_value ) ) { |
| 45 | 48 | $first_value = reset( $input_value ); |
| 46 | 49 | $input_value = is_scalar( $first_value ) ? $first_value : ''; |
| @@ -45,35 +48,12 @@ | ||
| 45 | 48 | $first_value = reset( $input_value ); |
| 46 | 49 | $input_value = is_scalar( $first_value ) ? $first_value : ''; |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | - if( !empty( $input_value ) && is_string( $input_value ) && !is_numeric( $input_value ) && apply_filters( 'wppb_avatar_field_transform_file_to_attachment', true, $field ) ){ | |
| 50 | - /* we have a file url and we need to change it into an attachment */ | |
| 51 | - // Check the type of file. We'll use this as the 'post_mime_type'. | |
| 52 | - $wp_upload_dir = wp_upload_dir(); | |
| 53 | - $file_path = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir["basedir"], $input_value ); | |
| 54 | - //on windows os we might have \ instead of / so change them | |
| 55 | - $file_path = str_replace( "\\", "/", $file_path ); | |
| 56 | - $file_type = wp_check_filetype( basename( $input_value ), null ); | |
| 57 | - $attachment = array( | |
| 58 | - 'guid' => $input_value, | |
| 59 | - 'post_mime_type' => $file_type['type'], | |
| 60 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $input_value ) ), | |
| 61 | - 'post_content' => '', | |
| 62 | - 'post_status' => 'inherit' | |
| 63 | - ); | |
| 64 | - | |
| 65 | - // Insert the attachment. | |
| 66 | - $input_value = wp_insert_attachment( $attachment, $input_value, 0 ); | |
| 67 | - if( !empty( $input_value ) ) { | |
| 68 | - // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
| 69 | - require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
| 70 | - // Generate the metadata for the attachment, and update the database record. | |
| 71 | - $attach_data = wp_generate_attachment_metadata($input_value, $file_path); | |
| 72 | - wp_update_attachment_metadata($input_value, $attach_data); | |
| 73 | - /* save the new attachment instead of the url */ | |
| 74 | - update_user_meta( $user_id, $field['meta-name'], $input_value ); | |
| 75 | - } | |
| 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 ); | |
| 76 | 56 | } |
| 77 | 57 | } |
| 78 | 58 | else |
| 79 | 59 | $input_value = !empty( $_POST[$field['meta-name']] ) ? sanitize_text_field( $_POST[$field['meta-name']] ) : ''; |
| @@ -115,16 +95,16 @@ | ||
| 115 | 95 | /* handle field save */ |
| 116 | 96 | function wppb_save_avatar_value( $field, $user_id, $request_data, $form_location ){ |
| 117 | 97 | if( $field['field'] == 'Avatar' ){ |
| 118 | 98 | $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] ); |
| 119 | - if ( isset( $field[ 'simple-upload' ] ) && $field[ 'simple-upload' ] == 'yes' && ( !isset( $field[ 'woocommerce-checkout-field' ] ) || $field[ 'woocommerce-checkout-field' ] !== 'Yes' ) ) { | |
| 99 | + if ( wppb_use_simple_upload_field( $field ) ) { | |
| 120 | 100 | //Save data in the case the simple upload field is used |
| 121 | 101 | $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] ); |
| 122 | - if( isset( $_FILES[ $field_name ] ) || ( isset( $request_data[ 'pay_gate' ] ) && in_array( $request_data['pay_gate'], array( 'stripe_connect', 'paypal_connect' ) ) ) ){ | |
| 102 | + if ( wppb_simple_upload_was_submitted( $field, $request_data ) ) { | |
| 123 | 103 | if ( !( isset( $field[ 'conditional-logic-enabled' ] ) && $field[ 'conditional-logic-enabled' ] == 'yes' && !isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ) ){ |
| 124 | 104 | if ( isset( $_FILES[ $field_name ][ 'size' ] ) && $_FILES[ $field_name ][ 'size' ] == 0 ){ |
| 125 | 105 | if ( isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ){ |
| 126 | - 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 ); | |
| 127 | 107 | } |
| 128 | 108 | } |
| 129 | 109 | else{ |
| 130 | 110 | $attachment_id = $request_data[ $field[ 'meta-name' ] ]; |
| @@ -150,9 +130,9 @@ | ||
| 150 | 130 | |
| 151 | 131 | // Save the uploaded file |
| 152 | 132 | // It will have no author until the user's email is confirmed |
| 153 | 133 | if( $field['field'] == 'Avatar' ) { |
| 154 | - if( isset( $field[ 'simple-upload' ] ) && $field[ 'simple-upload' ] === 'yes' && ( !isset( $field[ 'woocommerce-checkout-field' ] ) || $field[ 'woocommerce-checkout-field' ] !== 'Yes' ) ) { | |
| 134 | + if ( wppb_use_simple_upload_field( $field ) ) { | |
| 155 | 135 | $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] ); |
| 156 | 136 | $field_name = 'simple_upload_' . wppb_handle_meta_name( $field['meta-name'] ); |
| 157 | 137 | |
| 158 | 138 | if (isset($_FILES[$field_name]) && |
| @@ -161,14 +141,14 @@ | ||
| 161 | 141 | !(isset($field['conditional-logic-enabled']) && $field['conditional-logic-enabled'] == 'yes' && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) && |
| 162 | 142 | wppb_valid_simple_upload($field, $_FILES[$field_name])) { /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */ /* no need here */ |
| 163 | 143 | return wppb_default_fields_save_simple_upload_file($field_name); |
| 164 | 144 | } |
| 165 | - } else { | |
| 166 | - $attachment_id = $request_data[wppb_handle_meta_name( $field['meta-name'] )]; | |
| 167 | - if ( isset( $attachment_id ) ) { | |
| 168 | - return absint( trim( $attachment_id ) ); | |
| 169 | - } | |
| 170 | 145 | } |
| 146 | + | |
| 147 | + $attachment_id = isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ? $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] : null; | |
| 148 | + if ( isset( $attachment_id ) && wppb_verify_attachment_id( $attachment_id ) ) { | |
| 149 | + return absint( trim( $attachment_id ) ); | |
| 150 | + } | |
| 171 | 151 | } |
| 172 | 152 | |
| 173 | 153 | return ''; |
| 174 | 154 | } |
| @@ -204,9 +184,9 @@ | ||
| 204 | 184 | function wppb_check_avatar_value( $message, $field, $request_data, $form_location ){ |
| 205 | 185 | if( $field['field'] == 'Avatar' ){ |
| 206 | 186 | if( $field['required'] == 'Yes' ){ |
| 207 | 187 | $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] ); |
| 208 | - if ( isset( $field[ 'simple-upload' ] ) && $field[ 'simple-upload' ] == 'yes' && ( !isset( $field[ 'woocommerce-checkout-field' ] ) || $field[ 'woocommerce-checkout-field' ] !== 'Yes' ) ) { | |
| 188 | + if ( wppb_use_simple_upload_field( $field ) ) { | |
| 209 | 189 | //Check the required field in case simple upload is used |
| 210 | 190 | $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] ); |
| 211 | 191 | if ( (!isset( $_FILES[ $field_name ] ) || ( isset( $_FILES[ $field_name ] ) && isset( $_FILES[ $field_name ][ 'size' ] ) && $_FILES[ $field_name ][ 'size' ] == 0 ) || !wppb_valid_simple_upload( $field, $_FILES[ $field_name ] ) ) && isset( $request_data[ $field[ 'meta-name' ] ] ) && empty( $request_data[ $field[ 'meta-name' ] ] ) ){ /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */ /* no need here for wppb_valid_simple_upload() */ |
| 212 | 192 | return wppb_required_field_error( $field[ 'field-title' ] ); |