PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.1
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.1
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
profile-builder / front-end / default-fields / avatar / avatar.php

avatar.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.1, at front-end/default-fields/avatar/avatar.php

255 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) exit;
4 /* the avatar field relies on the upload field */
5
6 /* handle field output */
7 function wppb_avatar_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){
8 if ( $field['field'] == 'Avatar' ){
9
10 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
11
12 if ( ! wppb_use_simple_upload_field( $field ) ) {
13 wp_enqueue_media();
14 }
15 /* propper way to dequeue. add to functions file in theme or custom plugin
16 function wppb_dequeue_script() {
17 wp_script_is( 'wppb-upload-script', 'enqueued' ); //true
18 wp_dequeue_script( 'wppb-upload-script' );
19 }
20 add_action( 'get_footer', 'wppb_dequeue_script' );
21 */
22 $upload_script_vars = array(
23 'nonce' => wp_create_nonce( 'wppb_ajax_simple_upload' ),
24 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
25 'remove_link_text' => __( 'Remove', 'profile-builder' )
26 );
27
28 wp_enqueue_script( 'wppb-upload-script', WPPB_PLUGIN_URL.'front-end/default-fields/upload/upload.js', array('jquery'), PROFILE_BUILDER_VERSION, true );
29 wp_localize_script( 'wppb-upload-script', 'wppb_upload_script_vars', $upload_script_vars );
30
31 $wppb_generalSettings = get_option( 'wppb_general_settings' );
32
33 if ( ( isset( $wppb_generalSettings['extraFieldsLayout'] ) && ( $wppb_generalSettings['extraFieldsLayout'] == 'default' ) ) )
34 wp_enqueue_style( 'profile-builder-upload-css', WPPB_PLUGIN_URL.'front-end/default-fields/upload/upload.css', false, PROFILE_BUILDER_VERSION );
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 ) );
37 $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
38
39 if( $form_location != 'register' ) {
40 if( empty( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) )
41 $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
45 if ( is_array( $input_value ) ) {
46 $first_value = reset( $input_value );
47 $input_value = is_scalar( $first_value ) ? $first_value : '';
48 }
49
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 }
77 }
78 }
79 else
80 $input_value = !empty( $_POST[$field['meta-name']] ) ? sanitize_text_field( $_POST[$field['meta-name']] ) : '';
81
82 if ( $form_location != 'back_end' ){
83 $error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' );
84
85 if ( array_key_exists( $field['id'], $field_check_errors ) )
86 $error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>';
87
88 $extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location );
89
90 $output = '<label for="'.$field['meta-name'].'">'.$item_title.$error_mark.'</label>';
91 $output .= wppb_default_fields_make_upload_button( $field, $input_value, $extra_attr );
92 if( !empty( $item_description ) )
93 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
94 }else{
95 $item_title = ( ( $field['required'] == 'Yes' ) ? $item_title .' <span class="description">('. __( 'required', 'profile-builder' ) .')</span>' : $item_title );
96 $output = '
97 <table class="form-table">
98 <tr>
99 <th><label for="'.$field['meta-name'].'">'.$item_title.'</label></th>
100 <td>';
101 $output .= wppb_default_fields_make_upload_button( $field, $input_value );
102 $output .='<br/><span class="wppb-description-delimiter">'.$item_description;
103 $output .= '
104 </td>
105 </tr>
106 </table>';
107 }
108
109 return apply_filters( 'wppb_'.$form_location.'_avatar_custom_field_'.$field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value );
110 }
111 }
112 add_filter( 'wppb_output_form_field_avatar', 'wppb_avatar_handler', 10, 6 );
113 add_filter( 'wppb_admin_output_form_field_avatar', 'wppb_avatar_handler', 10, 6 );
114
115
116 /* handle field save */
117 function wppb_save_avatar_value( $field, $user_id, $request_data, $form_location ){
118 if( $field['field'] == 'Avatar' ){
119 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
120 if ( wppb_use_simple_upload_field( $field ) ) {
121 //Save data in the case the simple upload field is used
122 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] );
123 if ( wppb_simple_upload_was_submitted( $field, $request_data ) ) {
124 if ( !( isset( $field[ 'conditional-logic-enabled' ] ) && $field[ 'conditional-logic-enabled' ] == 'yes' && !isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ) ){
125 if ( isset( $_FILES[ $field_name ][ 'size' ] ) && $_FILES[ $field_name ][ 'size' ] == 0 ){
126 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' ] ) ] ) );
128 }
129 }
130 else{
131 $attachment_id = $request_data[ $field[ 'meta-name' ] ];
132 wppb_save_attachment_id( $attachment_id, $field, $user_id );
133 }
134 }
135 }
136 }
137 else{
138 //Save data in the case the WordPress upload is used
139 if ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ){
140 $attachment_id = sanitize_text_field( $request_data[wppb_handle_meta_name( $field['meta-name'] )] );
141 wppb_save_attachment_id( $attachment_id, $field, $user_id );
142 }
143 }
144 }
145 }
146 add_action( 'wppb_save_form_field', 'wppb_save_avatar_value', 10, 4 );
147 add_action( 'wppb_backend_save_form_field', 'wppb_save_avatar_value', 10, 4 );
148
149 /* save file when ec is enabled */
150 function wppb_avatar_add_upload_for_user_signup( $field_value, $field, $request_data ){
151
152 // Save the uploaded file
153 // It will have no author until the user's email is confirmed
154 if( $field['field'] == 'Avatar' ) {
155 if ( wppb_use_simple_upload_field( $field ) ) {
156 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
157 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field['meta-name'] );
158
159 if (isset($_FILES[$field_name]) &&
160 isset($_FILES[$field_name]['size']) && $_FILES[$field_name]['size'] !== 0 &&
161 !(wppb_belongs_to_repeater_with_conditional_logic($field) && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
162 !(isset($field['conditional-logic-enabled']) && $field['conditional-logic-enabled'] == 'yes' && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
163 wppb_valid_simple_upload($field, $_FILES[$field_name])) { /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */ /* no need here */
164 return wppb_default_fields_save_simple_upload_file($field_name);
165 }
166 }
167
168 $attachment_id = isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ? $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] : null;
169 if ( isset( $attachment_id ) && wppb_verify_attachment_id( $attachment_id ) ) {
170 return absint( trim( $attachment_id ) );
171 }
172 }
173
174 return '';
175 }
176 add_filter( 'wppb_add_to_user_signup_form_field_avatar', 'wppb_avatar_add_upload_for_user_signup', 10, 3 );
177
178 /* handle simple upload at the WooCommerce Checkout */
179 function wppb_ajax_simple_avatar(){
180 check_ajax_referer( 'wppb_ajax_simple_upload', 'nonce' );
181
182 if ( ! isset( $_POST['name'] ) ) {
183 wp_die();
184 }
185
186 $post_name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
187 $field = wppb_resolve_simple_upload_ajax_field( $post_name, 'Avatar' );
188
189 if (
190 false === $field
191 || ! isset( $_FILES[ $post_name ] )
192 || ! wppb_valid_simple_upload( $field, $_FILES[ $post_name ] ) /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
193 ) {
194 echo wp_json_encode( new WP_Error( 'upload_error', __( 'Sorry, you cannot upload this file type for this field.', 'profile-builder' ) ) );
195 wp_die();
196 }
197
198 echo wp_json_encode( wppb_default_fields_save_simple_upload_file( $post_name ) );
199 wp_die();
200 }
201 add_action( 'wp_ajax_nopriv_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' );
202 add_action( 'wp_ajax_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' );
203
204 /* handle field validation */
205 function wppb_check_avatar_value( $message, $field, $request_data, $form_location ){
206 if( $field['field'] == 'Avatar' ){
207 if( $field['required'] == 'Yes' ){
208 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
209 if ( wppb_use_simple_upload_field( $field ) ) {
210 //Check the required field in case simple upload is used
211 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] );
212 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() */
213 return wppb_required_field_error( $field[ 'field-title' ] );
214 }
215 }
216 else{
217 //Check the required field in case the WordPress upload is used
218 if ( ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) && ( trim( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) == '' ) ) || !isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ){
219 return wppb_required_field_error($field["field-title"]);
220 }
221 }
222 }
223 }
224 return $message;
225 }
226 add_filter( 'wppb_check_form_field_avatar', 'wppb_check_avatar_value', 10, 4 );
227
228
229 /* register image size defined in avatar field */
230 add_action( 'after_setup_theme', 'wppb_add_avatar_image_sizes' );
231 function wppb_add_avatar_image_sizes() {
232 if ( isset($_REQUEST['action']) && ( ( 'upload-attachment' == $_REQUEST['action'] && isset($_REQUEST['wppb_upload']) && 'true' == $_REQUEST['wppb_upload'] ) || 'wppb_ajax_simple_avatar' == $_REQUEST['action'] ) ) {
233
234 $all_fields = get_option('wppb_manage_fields');
235 if( !empty( $all_fields ) ) {
236 foreach ($all_fields as $field) {
237 if( $field['field'] == 'Avatar' ) {
238 wppb_add_avatar_sizes( $field );
239 }
240 }
241 }
242
243 wppb_userlisting_avatar();
244 }
245 }
246
247 // include missing templates needed on Elementor Pages (Form inside an Elementor Popup)
248 function wppb_avatar_field_scripts_and_styles() {
249 if ( is_plugin_active('elementor-pro/elementor-pro.php') && function_exists( 'wp_print_media_templates' ) ) {
250 wp_print_media_templates();
251 }
252 }
253 add_action( 'elementor/frontend/after_enqueue_scripts', 'wppb_avatar_field_scripts_and_styles' );
254
255