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 +62 -92 3.9.9 → 4.0.3 View file →
@@ -1,5 +1,7 @@
1 1 <?php
2 +// Exit if accessed directly
3 +if ( ! defined( 'ABSPATH' ) ) exit;
2 4 /* the avatar field relies on the upload field */
3 5
4 6 /* handle field output */
5 7 function wppb_avatar_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){
@@ -6,10 +8,11 @@
6 8 if ( $field['field'] == 'Avatar' ){
7 9
8 10 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
9 11
10 - /* media upload add here, this should be added just once even if called multiple times */
11 - wp_enqueue_media();
12 + if ( ! wppb_use_simple_upload_field( $field ) ) {
13 + wp_enqueue_media();
14 + }
12 15 /* propper way to dequeue. add to functions file in theme or custom plugin
13 16 function wppb_dequeue_script() {
14 17 wp_script_is( 'wppb-upload-script', 'enqueued' ); //true
15 18 wp_dequeue_script( 'wppb-upload-script' );
@@ -33,40 +36,24 @@
33 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 ) );
34 37 $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
35 38
36 39 if( $form_location != 'register' ) {
37 - 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
38 45 $input_value = ( (wppb_user_meta_exists($user_id, $field['meta-name']) != null) ? get_user_meta($user_id, $field['meta-name'], true) : '');
39 - else
40 - $input_value = $request_data[wppb_handle_meta_name( $field['meta-name'] )];
41 46
42 - if( !empty( $input_value ) && !is_numeric( $input_value ) && apply_filters( 'wppb_avatar_field_transform_file_to_attachment', true, $field ) ){
43 - /* we have a file url and we need to change it into an attachment */
44 - // Check the type of file. We'll use this as the 'post_mime_type'.
45 - $wp_upload_dir = wp_upload_dir();
46 - $file_path = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir["basedir"], $input_value );
47 - //on windows os we might have \ instead of / so change them
48 - $file_path = str_replace( "\\", "/", $file_path );
49 - $file_type = wp_check_filetype( basename( $input_value ), null );
50 - $attachment = array(
51 - 'guid' => $input_value,
52 - 'post_mime_type' => $file_type['type'],
53 - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $input_value ) ),
54 - 'post_content' => '',
55 - 'post_status' => 'inherit'
56 - );
47 + if ( is_array( $input_value ) ) {
48 + $first_value = reset( $input_value );
49 + $input_value = is_scalar( $first_value ) ? $first_value : '';
50 + }
57 51
58 - // Insert the attachment.
59 - $input_value = wp_insert_attachment( $attachment, $input_value, 0 );
60 - if( !empty( $input_value ) ) {
61 - // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
62 - require_once(ABSPATH . 'wp-admin/includes/image.php');
63 - // Generate the metadata for the attachment, and update the database record.
64 - $attach_data = wp_generate_attachment_metadata($input_value, $file_path);
65 - wp_update_attachment_metadata($input_value, $attach_data);
66 - /* save the new attachment instead of the url */
67 - update_user_meta( $user_id, $field['meta-name'], $input_value );
68 - }
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 );
69 56 }
70 57 }
71 58 else
72 59 $input_value = !empty( $_POST[$field['meta-name']] ) ? sanitize_text_field( $_POST[$field['meta-name']] ) : '';
@@ -79,9 +66,9 @@
79 66
80 67 $extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location );
81 68
82 69 $output = '<label for="'.$field['meta-name'].'">'.$item_title.$error_mark.'</label>';
83 - $output .= wppb_make_upload_button( $field, $input_value, $extra_attr );
70 + $output .= wppb_default_fields_make_upload_button( $field, $input_value, $extra_attr );
84 71 if( !empty( $item_description ) )
85 72 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
86 73 }else{
87 74 $item_title = ( ( $field['required'] == 'Yes' ) ? $item_title .' <span class="description">('. __( 'required', 'profile-builder' ) .')</span>' : $item_title );
@@ -89,9 +76,9 @@
89 76 <table class="form-table">
90 77 <tr>
91 78 <th><label for="'.$field['meta-name'].'">'.$item_title.'</label></th>
92 79 <td>';
93 - $output .= wppb_make_upload_button( $field, $input_value );
80 + $output .= wppb_default_fields_make_upload_button( $field, $input_value );
94 81 $output .='<br/><span class="wppb-description-delimiter">'.$item_description;
95 82 $output .= '
96 83 </td>
97 84 </tr>
@@ -108,27 +95,21 @@
108 95 /* handle field save */
109 96 function wppb_save_avatar_value( $field, $user_id, $request_data, $form_location ){
110 97 if( $field['field'] == 'Avatar' ){
111 98 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
112 - 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 ) ) {
113 100 //Save data in the case the simple upload field is used
114 101 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] );
115 - if( isset( $_FILES[ $field_name ] ) ) {
102 + if ( wppb_simple_upload_was_submitted( $field, $request_data ) ) {
116 103 if ( !( isset( $field[ 'conditional-logic-enabled' ] ) && $field[ 'conditional-logic-enabled' ] == 'yes' && !isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ) ){
117 104 if ( isset( $_FILES[ $field_name ][ 'size' ] ) && $_FILES[ $field_name ][ 'size' ] == 0 ){
118 105 if ( isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ){
119 - 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 );
120 107 }
121 108 }
122 109 else{
123 110 $attachment_id = $request_data[ $field[ 'meta-name' ] ];
124 - update_user_meta( $user_id, $field[ 'meta-name' ], absint( $attachment_id ) );
125 - if ( $attachment_id !== '' ) {
126 - wp_update_post(array(
127 - 'ID' => absint(trim($attachment_id)),
128 - 'post_author' => $user_id
129 - ));
130 - }
111 + wppb_save_attachment_id( $attachment_id, $field, $user_id );
131 112 }
132 113 }
133 114 }
134 115 }
@@ -134,9 +115,10 @@
134 115 }
135 116 else{
136 117 //Save data in the case the WordPress upload is used
137 118 if ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ){
138 - update_user_meta( $user_id, $field['meta-name'], sanitize_text_field( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) );
119 + $attachment_id = sanitize_text_field( $request_data[wppb_handle_meta_name( $field['meta-name'] )] );
120 + wppb_save_attachment_id( $attachment_id, $field, $user_id );
139 121 }
140 122 }
141 123 }
142 124 }
@@ -142,45 +124,8 @@
142 124 }
143 125 add_action( 'wppb_save_form_field', 'wppb_save_avatar_value', 10, 4 );
144 126 add_action( 'wppb_backend_save_form_field', 'wppb_save_avatar_value', 10, 4 );
145 127
146 -/**
147 - * Function that saves an attachment from the simple upload version of the Avatar field
148 - * @param $field_name
149 - * @return string|WP_Error
150 - */
151 -function wppb_avatar_save_simple_upload_file ( $field_name ){
152 -
153 - require_once( ABSPATH . 'wp-admin/includes/file.php' );
154 - $upload_overrides = array( 'test_form' => false );
155 -
156 - if( isset( $_FILES[$field_name] ) )
157 - $file = wp_handle_upload( $_FILES[$field_name], $upload_overrides );
158 -
159 - if ( isset( $file[ 'error' ] ) ) {
160 - return new WP_Error( 'upload_error', $file[ 'error' ] );
161 - }
162 - $filename = isset( $_FILES[ $field_name ][ 'name' ] ) ? sanitize_text_field( $_FILES[ $field_name ][ 'name' ] ) : '';
163 - $wp_filetype = wp_check_filetype( $filename, null );
164 - $attachment = array(
165 - 'post_mime_type' => $wp_filetype[ 'type' ],
166 - 'post_title' => $filename,
167 - 'post_content' => '',
168 - 'post_status' => 'inherit'
169 - );
170 -
171 - $attachment_id = wp_insert_attachment( $attachment, $file[ 'file' ] );
172 -
173 - if (!is_wp_error($attachment_id) && is_numeric($attachment_id)) {
174 - require_once(ABSPATH . 'wp-admin/includes/image.php');
175 - $attachment_data = wp_generate_attachment_metadata($attachment_id, $file['file']);
176 - wp_update_attachment_metadata($attachment_id, $attachment_data);
177 - return trim($attachment_id);
178 - } else {
179 - return '';
180 - }
181 -}
182 -
183 128 /* save file when ec is enabled */
184 129 function wppb_avatar_add_upload_for_user_signup( $field_value, $field, $request_data ){
185 130
186 131 // Save the uploaded file
@@ -185,10 +130,11 @@
185 130
186 131 // Save the uploaded file
187 132 // It will have no author until the user's email is confirmed
188 133 if( $field['field'] == 'Avatar' ) {
189 - if( isset( $field[ 'simple-upload' ] ) && $field[ 'simple-upload' ] === 'yes' && ( !isset( $field[ 'woocommerce-checkout-field' ] ) || $field[ 'woocommerce-checkout-field' ] !== 'Yes' ) ) {
190 - $field_name = 'simple_upload_' . $field['meta-name'];
134 + if ( wppb_use_simple_upload_field( $field ) ) {
135 + $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
136 + $field_name = 'simple_upload_' . wppb_handle_meta_name( $field['meta-name'] );
191 137
192 138 if (isset($_FILES[$field_name]) &&
193 139 isset($_FILES[$field_name]['size']) && $_FILES[$field_name]['size'] !== 0 &&
194 140 !(wppb_belongs_to_repeater_with_conditional_logic($field) && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
@@ -193,16 +139,16 @@
193 139 isset($_FILES[$field_name]['size']) && $_FILES[$field_name]['size'] !== 0 &&
194 140 !(wppb_belongs_to_repeater_with_conditional_logic($field) && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
195 141 !(isset($field['conditional-logic-enabled']) && $field['conditional-logic-enabled'] == 'yes' && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
196 142 wppb_valid_simple_upload($field, $_FILES[$field_name])) { /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */ /* no need here */
197 - return wppb_save_simple_upload_file($field_name);
143 + return wppb_default_fields_save_simple_upload_file($field_name);
198 144 }
199 - } else {
200 - $attachment_id = $request_data[wppb_handle_meta_name( $field['meta-name'] )];
201 - if ( isset( $attachment_id ) ) {
202 - return absint( trim( $attachment_id ) );
203 - }
204 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 + }
205 151 }
206 152
207 153 return '';
208 154 }
@@ -210,11 +156,26 @@
210 156
211 157 /* handle simple upload at the WooCommerce Checkout */
212 158 function wppb_ajax_simple_avatar(){
213 159 check_ajax_referer( 'wppb_ajax_simple_upload', 'nonce' );
214 - if ( isset($_POST["name"]) ) {
215 - echo json_encode( wppb_avatar_save_simple_upload_file( sanitize_text_field( $_POST["name"] ) ) );
160 +
161 + if ( ! isset( $_POST['name'] ) ) {
162 + wp_die();
216 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 ) );
217 178 wp_die();
218 179 }
219 180 add_action( 'wp_ajax_nopriv_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' );
220 181 add_action( 'wp_ajax_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' );
@@ -223,9 +184,9 @@
223 184 function wppb_check_avatar_value( $message, $field, $request_data, $form_location ){
224 185 if( $field['field'] == 'Avatar' ){
225 186 if( $field['required'] == 'Yes' ){
226 187 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
227 - 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 ) ) {
228 189 //Check the required field in case simple upload is used
229 190 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] );
230 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() */
231 192 return wppb_required_field_error( $field[ 'field-title' ] );
@@ -260,4 +221,13 @@
260 221
261 222 wppb_userlisting_avatar();
262 223 }
263 224 }
225 +
226 +// include missing templates needed on Elementor Pages (Form inside an Elementor Popup)
227 +function wppb_avatar_field_scripts_and_styles() {
228 + if ( is_plugin_active('elementor-pro/elementor-pro.php') && function_exists( 'wp_print_media_templates' ) ) {
229 + wp_print_media_templates();
230 + }
231 +}
232 +add_action( 'elementor/frontend/after_enqueue_scripts', 'wppb_avatar_field_scripts_and_styles' );
233 +