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

234 lines 13.4 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( !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 );
59
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 }
71 }
72 }
73 else
74 $input_value = !empty( $_POST[$field['meta-name']] ) ? sanitize_text_field( $_POST[$field['meta-name']] ) : '';
75
76 if ( $form_location != 'back_end' ){
77 $error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' );
78
79 if ( array_key_exists( $field['id'], $field_check_errors ) )
80 $error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>';
81
82 $extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location );
83
84 $output = '<label for="'.$field['meta-name'].'">'.$item_title.$error_mark.'</label>';
85 $output .= wppb_default_fields_make_upload_button( $field, $input_value, $extra_attr );
86 if( !empty( $item_description ) )
87 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
88 }else{
89 $item_title = ( ( $field['required'] == 'Yes' ) ? $item_title .' <span class="description">('. __( 'required', 'profile-builder' ) .')</span>' : $item_title );
90 $output = '
91 <table class="form-table">
92 <tr>
93 <th><label for="'.$field['meta-name'].'">'.$item_title.'</label></th>
94 <td>';
95 $output .= wppb_default_fields_make_upload_button( $field, $input_value );
96 $output .='<br/><span class="wppb-description-delimiter">'.$item_description;
97 $output .= '
98 </td>
99 </tr>
100 </table>';
101 }
102
103 return apply_filters( 'wppb_'.$form_location.'_avatar_custom_field_'.$field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value );
104 }
105 }
106 add_filter( 'wppb_output_form_field_avatar', 'wppb_avatar_handler', 10, 6 );
107 add_filter( 'wppb_admin_output_form_field_avatar', 'wppb_avatar_handler', 10, 6 );
108
109
110 /* handle field save */
111 function wppb_save_avatar_value( $field, $user_id, $request_data, $form_location ){
112 if( $field['field'] == 'Avatar' ){
113 $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' ) ) {
115 //Save data in the case the simple upload field is used
116 $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' ) ) ) ){
118 if ( !( isset( $field[ 'conditional-logic-enabled' ] ) && $field[ 'conditional-logic-enabled' ] == 'yes' && !isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ) ){
119 if ( isset( $_FILES[ $field_name ][ 'size' ] ) && $_FILES[ $field_name ][ 'size' ] == 0 ){
120 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' ] ) ] ) );
122 }
123 }
124 else{
125 $attachment_id = $request_data[ $field[ 'meta-name' ] ];
126 wppb_save_attachment_id( $attachment_id, $field, $user_id );
127 }
128 }
129 }
130 }
131 else{
132 //Save data in the case the WordPress upload is used
133 if ( isset( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ){
134 $attachment_id = sanitize_text_field( $request_data[wppb_handle_meta_name( $field['meta-name'] )] );
135 wppb_save_attachment_id( $attachment_id, $field, $user_id );
136 }
137 }
138 }
139 }
140 add_action( 'wppb_save_form_field', 'wppb_save_avatar_value', 10, 4 );
141 add_action( 'wppb_backend_save_form_field', 'wppb_save_avatar_value', 10, 4 );
142
143 /* save file when ec is enabled */
144 function wppb_avatar_add_upload_for_user_signup( $field_value, $field, $request_data ){
145
146 // Save the uploaded file
147 // It will have no author until the user's email is confirmed
148 if( $field['field'] == 'Avatar' ) {
149 if( isset( $field[ 'simple-upload' ] ) && $field[ 'simple-upload' ] === 'yes' && ( !isset( $field[ 'woocommerce-checkout-field' ] ) || $field[ 'woocommerce-checkout-field' ] !== 'Yes' ) ) {
150 $field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] );
151 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field['meta-name'] );
152
153 if (isset($_FILES[$field_name]) &&
154 isset($_FILES[$field_name]['size']) && $_FILES[$field_name]['size'] !== 0 &&
155 !(wppb_belongs_to_repeater_with_conditional_logic($field) && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
156 !(isset($field['conditional-logic-enabled']) && $field['conditional-logic-enabled'] == 'yes' && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) &&
157 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);
159 }
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 }
166 }
167
168 return '';
169 }
170 add_filter( 'wppb_add_to_user_signup_form_field_avatar', 'wppb_avatar_add_upload_for_user_signup', 10, 3 );
171
172 /* handle simple upload at the WooCommerce Checkout */
173 function wppb_ajax_simple_avatar(){
174 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"] ) ) );
177 }
178 wp_die();
179 }
180 add_action( 'wp_ajax_nopriv_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' );
181 add_action( 'wp_ajax_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' );
182
183 /* handle field validation */
184 function wppb_check_avatar_value( $message, $field, $request_data, $form_location ){
185 if( $field['field'] == 'Avatar' ){
186 if( $field['required'] == 'Yes' ){
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' ) ) {
189 //Check the required field in case simple upload is used
190 $field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] );
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 return wppb_required_field_error( $field[ 'field-title' ] );
193 }
194 }
195 else{
196 //Check the required field in case the WordPress upload is used
197 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'] )] ) ){
198 return wppb_required_field_error($field["field-title"]);
199 }
200 }
201 }
202 }
203 return $message;
204 }
205 add_filter( 'wppb_check_form_field_avatar', 'wppb_check_avatar_value', 10, 4 );
206
207
208 /* register image size defined in avatar field */
209 add_action( 'after_setup_theme', 'wppb_add_avatar_image_sizes' );
210 function wppb_add_avatar_image_sizes() {
211 if ( isset($_REQUEST['action']) && ( ( 'upload-attachment' == $_REQUEST['action'] && isset($_REQUEST['wppb_upload']) && 'true' == $_REQUEST['wppb_upload'] ) || 'wppb_ajax_simple_avatar' == $_REQUEST['action'] ) ) {
212
213 $all_fields = get_option('wppb_manage_fields');
214 if( !empty( $all_fields ) ) {
215 foreach ($all_fields as $field) {
216 if( $field['field'] == 'Avatar' ) {
217 wppb_add_avatar_sizes( $field );
218 }
219 }
220 }
221
222 wppb_userlisting_avatar();
223 }
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
234