PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
← All changes | front-end/default-fields/avatar/avatar.php +44 -44 3.16.0 → 4.0.3 View file →
@@ -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,40 +36,24 @@
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 - if( !empty( $input_value ) && !is_numeric( $input_value ) && apply_filters( 'wppb_avatar_field_transform_file_to_attachment', true, $field ) ){
45 - /* we have a file url and we need to change it into an attachment */
46 - // Check the type of file. We'll use this as the 'post_mime_type'.
47 - $wp_upload_dir = wp_upload_dir();
48 - $file_path = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir["basedir"], $input_value );
49 - //on windows os we might have \ instead of / so change them
50 - $file_path = str_replace( "\\", "/", $file_path );
51 - $file_type = wp_check_filetype( basename( $input_value ), null );
52 - $attachment = array(
53 - 'guid' => $input_value,
54 - 'post_mime_type' => $file_type['type'],
55 - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $input_value ) ),
56 - 'post_content' => '',
57 - 'post_status' => 'inherit'
58 - );
47 + if ( is_array( $input_value ) ) {
48 + $first_value = reset( $input_value );
49 + $input_value = is_scalar( $first_value ) ? $first_value : '';
50 + }
59 51
60 - // Insert the attachment.
61 - $input_value = wp_insert_attachment( $attachment, $input_value, 0 );
62 - if( !empty( $input_value ) ) {
63 - // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
64 - require_once(ABSPATH . 'wp-admin/includes/image.php');
65 - // Generate the metadata for the attachment, and update the database record.
66 - $attach_data = wp_generate_attachment_metadata($input_value, $file_path);
67 - wp_update_attachment_metadata($input_value, $attach_data);
68 - /* save the new attachment instead of the url */
69 - update_user_meta( $user_id, $field['meta-name'], $input_value );
70 - }
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 );
71 56 }
72 57 }
73 58 else
74 59 $input_value = !empty( $_POST[$field['meta-name']] ) ? sanitize_text_field( $_POST[$field['meta-name']] ) : '';
@@ -110,16 +95,16 @@
110 95 /* handle field save */
111 96 function wppb_save_avatar_value( $field, $user_id, $request_data, $form_location ){
112 97 if( $field['field'] == 'Avatar' ){
113 98 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
114 - 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 ) ) {
115 100 //Save data in the case the simple upload field is used
116 101 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] );
117 - 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 ) ) {
118 103 if ( !( isset( $field[ 'conditional-logic-enabled' ] ) && $field[ 'conditional-logic-enabled' ] == 'yes' && !isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ) ){
119 104 if ( isset( $_FILES[ $field_name ][ 'size' ] ) && $_FILES[ $field_name ][ 'size' ] == 0 ){
120 105 if ( isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ){
121 - 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 );
122 107 }
123 108 }
124 109 else{
125 110 $attachment_id = $request_data[ $field[ 'meta-name' ] ];
@@ -145,9 +130,9 @@
145 130
146 131 // Save the uploaded file
147 132 // It will have no author until the user's email is confirmed
148 133 if( $field['field'] == 'Avatar' ) {
149 - 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 ) ) {
150 135 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
151 136 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field['meta-name'] );
152 137
153 138 if (isset($_FILES[$field_name]) &&
@@ -154,16 +139,16 @@
154 139 isset($_FILES[$field_name]['size']) && $_FILES[$field_name]['size'] !== 0 &&
155 140 !(wppb_belongs_to_repeater_with_conditional_logic($field) && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
156 141 !(isset($field['conditional-logic-enabled']) && $field['conditional-logic-enabled'] == 'yes' && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
157 142 wppb_valid_simple_upload($field, $_FILES[$field_name])) { /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */ /* no need here */
158 - return wppb_avatar_save_simple_upload_file($field_name);
143 + return wppb_default_fields_save_simple_upload_file($field_name);
159 144 }
160 - } else {
161 - $attachment_id = $request_data[wppb_handle_meta_name( $field['meta-name'] )];
162 - if ( isset( $attachment_id ) ) {
163 - return absint( trim( $attachment_id ) );
164 - }
165 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 + }
166 151 }
167 152
168 153 return '';
169 154 }
@@ -171,11 +156,26 @@
171 156
172 157 /* handle simple upload at the WooCommerce Checkout */
173 158 function wppb_ajax_simple_avatar(){
174 159 check_ajax_referer( 'wppb_ajax_simple_upload', 'nonce' );
175 - if ( isset($_POST["name"]) ) {
176 - echo json_encode( wppb_default_fields_save_simple_upload_file( sanitize_text_field( $_POST["name"] ) ) );
160 +
161 + if ( ! isset( $_POST['name'] ) ) {
162 + wp_die();
177 163 }
164 +
165 + $post_name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
166 + $field = wppb_resolve_simple_upload_ajax_field( $post_name, 'Avatar' );
167 +
168 + if (
169 + false === $field
170 + || ! isset( $_FILES[ $post_name ] )
171 + || ! wppb_valid_simple_upload( $field, $_FILES[ $post_name ] ) /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
172 + ) {
173 + echo wp_json_encode( new WP_Error( 'upload_error', __( 'Sorry, you cannot upload this file type for this field.', 'profile-builder' ) ) );
174 + wp_die();
175 + }
176 +
177 + echo wp_json_encode( wppb_default_fields_save_simple_upload_file( $post_name ) );
178 178 wp_die();
179 179 }
180 180 add_action( 'wp_ajax_nopriv_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' );
181 181 add_action( 'wp_ajax_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' );
@@ -184,9 +184,9 @@
184 184 function wppb_check_avatar_value( $message, $field, $request_data, $form_location ){
185 185 if( $field['field'] == 'Avatar' ){
186 186 if( $field['required'] == 'Yes' ){
187 187 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
188 - 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 ) ) {
189 189 //Check the required field in case simple upload is used
190 190 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] );
191 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() */
192 192 return wppb_required_field_error( $field[ 'field-title' ] );