| 1 |
<?php |
| 2 |
/* the avatar field relies on the upload field */ |
| 3 |
|
| 4 |
/* handle field output */ |
| 5 |
function wppb_avatar_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 6 |
if ( $field['field'] == 'Avatar' ){ |
| 7 |
|
| 8 |
$field['meta-name'] = Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] ); |
| 9 |
|
| 10 |
/* media upload add here, this should be added just once even if called multiple times */ |
| 11 |
wp_enqueue_media(); |
| 12 |
/* propper way to dequeue. add to functions file in theme or custom plugin |
| 13 |
function wppb_dequeue_script() { |
| 14 |
wp_script_is( 'wppb-upload-script', 'enqueued' ); //true |
| 15 |
wp_dequeue_script( 'wppb-upload-script' ); |
| 16 |
} |
| 17 |
add_action( 'get_footer', 'wppb_dequeue_script' ); |
| 18 |
*/ |
| 19 |
$upload_script_vars = array( |
| 20 |
'nonce' => wp_create_nonce( 'wppb_ajax_simple_upload' ), |
| 21 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 22 |
'remove_link_text' => __( 'Remove', 'profile-builder' ) |
| 23 |
); |
| 24 |
|
| 25 |
wp_enqueue_script( 'wppb-upload-script', WPPB_PLUGIN_URL.'front-end/default-fields/upload/upload.js', array('jquery'), PROFILE_BUILDER_VERSION, true ); |
| 26 |
wp_localize_script( 'wppb-upload-script', 'wppb_upload_script_vars', $upload_script_vars ); |
| 27 |
|
| 28 |
$wppb_generalSettings = get_option( 'wppb_general_settings' ); |
| 29 |
|
| 30 |
if ( ( isset( $wppb_generalSettings['extraFieldsLayout'] ) && ( $wppb_generalSettings['extraFieldsLayout'] == 'default' ) ) ) |
| 31 |
wp_enqueue_style( 'profile-builder-upload-css', WPPB_PLUGIN_URL.'front-end/default-fields/upload/upload.css', false, PROFILE_BUILDER_VERSION ); |
| 32 |
|
| 33 |
$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 |
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 35 |
|
| 36 |
if( $form_location != 'register' ) { |
| 37 |
if( empty( $request_data[wppb_handle_meta_name( $field['meta-name'] )] ) ) |
| 38 |
$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 |
|
| 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 |
); |
| 57 |
|
| 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 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
else |
| 72 |
$input_value = !empty( $_POST[$field['meta-name']] ) ? sanitize_text_field( $_POST[$field['meta-name']] ) : ''; |
| 73 |
|
| 74 |
if ( $form_location != 'back_end' ){ |
| 75 |
$error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ); |
| 76 |
|
| 77 |
if ( array_key_exists( $field['id'], $field_check_errors ) ) |
| 78 |
$error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; |
| 79 |
|
| 80 |
$extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location ); |
| 81 |
|
| 82 |
$output = '<label for="'.$field['meta-name'].'">'.$item_title.$error_mark.'</label>'; |
| 83 |
$output .= wppb_make_upload_button( $field, $input_value, $extra_attr ); |
| 84 |
if( !empty( $item_description ) ) |
| 85 |
$output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>'; |
| 86 |
}else{ |
| 87 |
$item_title = ( ( $field['required'] == 'Yes' ) ? $item_title .' <span class="description">('. __( 'required', 'profile-builder' ) .')</span>' : $item_title ); |
| 88 |
$output = ' |
| 89 |
<table class="form-table"> |
| 90 |
<tr> |
| 91 |
<th><label for="'.$field['meta-name'].'">'.$item_title.'</label></th> |
| 92 |
<td>'; |
| 93 |
$output .= wppb_make_upload_button( $field, $input_value ); |
| 94 |
$output .='<br/><span class="wppb-description-delimiter">'.$item_description; |
| 95 |
$output .= ' |
| 96 |
</td> |
| 97 |
</tr> |
| 98 |
</table>'; |
| 99 |
} |
| 100 |
|
| 101 |
return apply_filters( 'wppb_'.$form_location.'_avatar_custom_field_'.$field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value ); |
| 102 |
} |
| 103 |
} |
| 104 |
add_filter( 'wppb_output_form_field_avatar', 'wppb_avatar_handler', 10, 6 ); |
| 105 |
add_filter( 'wppb_admin_output_form_field_avatar', 'wppb_avatar_handler', 10, 6 ); |
| 106 |
|
| 107 |
|
| 108 |
/* handle field save */ |
| 109 |
function wppb_save_avatar_value( $field, $user_id, $request_data, $form_location ){ |
| 110 |
if( $field['field'] == 'Avatar' ){ |
| 111 |
$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' ) ) { |
| 113 |
//Save data in the case the simple upload field is used |
| 114 |
$field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] ); |
| 115 |
if( isset( $_FILES[ $field_name ] ) ) { |
| 116 |
if ( !( isset( $field[ 'conditional-logic-enabled' ] ) && $field[ 'conditional-logic-enabled' ] == 'yes' && !isset( $request_data[ wppb_handle_meta_name( $field[ 'meta-name' ] ) ] ) ) ){ |
| 117 |
if ( isset( $_FILES[ $field_name ][ 'size' ] ) && $_FILES[ $field_name ][ 'size' ] == 0 ){ |
| 118 |
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' ] ) ] ) ); |
| 120 |
} |
| 121 |
} |
| 122 |
else{ |
| 123 |
$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 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
else{ |
| 136 |
//Save data in the case the WordPress upload is used |
| 137 |
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'] )] ) ); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
add_action( 'wppb_save_form_field', 'wppb_save_avatar_value', 10, 4 ); |
| 144 |
add_action( 'wppb_backend_save_form_field', 'wppb_save_avatar_value', 10, 4 ); |
| 145 |
|
| 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 |
/* save file when ec is enabled */ |
| 184 |
function wppb_avatar_add_upload_for_user_signup( $field_value, $field, $request_data ){ |
| 185 |
|
| 186 |
// Save the uploaded file |
| 187 |
// It will have no author until the user's email is confirmed |
| 188 |
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']; |
| 191 |
|
| 192 |
if (isset($_FILES[$field_name]) && |
| 193 |
isset($_FILES[$field_name]['size']) && $_FILES[$field_name]['size'] !== 0 && |
| 194 |
!(wppb_belongs_to_repeater_with_conditional_logic($field) && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) && |
| 195 |
!(isset($field['conditional-logic-enabled']) && $field['conditional-logic-enabled'] == 'yes' && !isset($request_data[wppb_handle_meta_name($field['meta-name'])])) && |
| 196 |
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); |
| 198 |
} |
| 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 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return ''; |
| 208 |
} |
| 209 |
add_filter( 'wppb_add_to_user_signup_form_field_avatar', 'wppb_avatar_add_upload_for_user_signup', 10, 3 ); |
| 210 |
|
| 211 |
/* handle simple upload at the WooCommerce Checkout */ |
| 212 |
function wppb_ajax_simple_avatar(){ |
| 213 |
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"] ) ) ); |
| 216 |
} |
| 217 |
wp_die(); |
| 218 |
} |
| 219 |
add_action( 'wp_ajax_nopriv_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' ); |
| 220 |
add_action( 'wp_ajax_wppb_ajax_simple_avatar', 'wppb_ajax_simple_avatar' ); |
| 221 |
|
| 222 |
/* handle field validation */ |
| 223 |
function wppb_check_avatar_value( $message, $field, $request_data, $form_location ){ |
| 224 |
if( $field['field'] == 'Avatar' ){ |
| 225 |
if( $field['required'] == 'Yes' ){ |
| 226 |
$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' ) ) { |
| 228 |
//Check the required field in case simple upload is used |
| 229 |
$field_name = 'simple_upload_' . wppb_handle_meta_name( $field[ 'meta-name' ] ); |
| 230 |
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 |
return wppb_required_field_error( $field[ 'field-title' ] ); |
| 232 |
} |
| 233 |
} |
| 234 |
else{ |
| 235 |
//Check the required field in case the WordPress upload is used |
| 236 |
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'] )] ) ){ |
| 237 |
return wppb_required_field_error($field["field-title"]); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
return $message; |
| 243 |
} |
| 244 |
add_filter( 'wppb_check_form_field_avatar', 'wppb_check_avatar_value', 10, 4 ); |
| 245 |
|
| 246 |
|
| 247 |
/* register image size defined in avatar field */ |
| 248 |
add_action( 'after_setup_theme', 'wppb_add_avatar_image_sizes' ); |
| 249 |
function wppb_add_avatar_image_sizes() { |
| 250 |
if ( isset($_REQUEST['action']) && ( ( 'upload-attachment' == $_REQUEST['action'] && isset($_REQUEST['wppb_upload']) && 'true' == $_REQUEST['wppb_upload'] ) || 'wppb_ajax_simple_avatar' == $_REQUEST['action'] ) ) { |
| 251 |
|
| 252 |
$all_fields = get_option('wppb_manage_fields'); |
| 253 |
if( !empty( $all_fields ) ) { |
| 254 |
foreach ($all_fields as $field) { |
| 255 |
if( $field['field'] == 'Avatar' ) { |
| 256 |
wppb_add_avatar_sizes( $field ); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
wppb_userlisting_avatar(); |
| 262 |
} |
| 263 |
} |
| 264 |
|