PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.5
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.5
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 3.16.5, at front-end/default-fields/avatar/avatar.php

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