| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
/* Set up upload field for frontend */ |
| 6 |
/* overwrite the two functions for when an upload is made from the frontend so they don't check for a logged in user */ |
| 7 |
if( strpos( wp_get_referer(), 'wp-admin' ) === false && isset( $_REQUEST['action'] ) && 'upload-attachment' == $_REQUEST['action'] ){ |
| 8 |
|
| 9 |
if( isset( $_REQUEST['wppb_upload'] ) && 'true' == $_REQUEST['wppb_upload'] && |
| 10 |
isset( $_REQUEST['meta_name'] ) && wppb_check_that_field_is_defined( sanitize_text_field( $_REQUEST['meta_name'] ), array( 'Avatar', 'Upload' ) ) ){ |
| 11 |
|
| 12 |
if( !function_exists( 'check_ajax_referer' ) ){ |
| 13 |
function check_ajax_referer( ) { |
| 14 |
return true; |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
if( !function_exists( 'auth_redirect' ) ){ |
| 19 |
function auth_redirect() { |
| 20 |
return true; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
/* create a fake user with the "upload_posts" capability and assign him to the global $current_user. this is used to bypass the checks for current_user_can('upload_files') in async-upload.php */ |
| 29 |
add_action( 'current_screen', 'wppb_create_fake_user_when_uploading_and_not_logged_in' ); |
| 30 |
if( !function_exists( 'wppb_create_fake_user_when_uploading_and_not_logged_in' ) ) { |
| 31 |
function wppb_create_fake_user_when_uploading_and_not_logged_in() { |
| 32 |
// don't do anything if this request is coming from the back-end |
| 33 |
if( !( strpos( wp_get_referer(), 'wp-admin' ) === false ) ) |
| 34 |
return; |
| 35 |
|
| 36 |
if ( isset($_REQUEST['action']) && 'upload-attachment' == $_REQUEST['action'] && |
| 37 |
isset($_REQUEST['wppb_upload']) && 'true' == $_REQUEST['wppb_upload'] && |
| 38 |
isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( $_REQUEST['_wpnonce'] ), 'media-form' ) && |
| 39 |
isset( $_REQUEST['meta_name'] ) && wppb_check_that_field_is_defined( sanitize_text_field( $_REQUEST['meta_name'] ), array( 'Avatar', 'Upload' ) ) ) { |
| 40 |
|
| 41 |
if ( !is_user_logged_in() || !current_user_can( 'upload_files' ) || !current_user_can( 'edit_posts' ) ) { |
| 42 |
global $current_user; |
| 43 |
$current_user = new WP_User( 0, 'frontend_uploader' ); |
| 44 |
$current_user->allcaps = array( "upload_files" => true, "edit_posts" => true, "edit_others_posts" => true, "edit_pages" => true, "edit_others_pages" => true ); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/* for a request of a upload from the frontend and no user is logged in don't query for attachments */ |
| 51 |
add_action( 'after_setup_theme', 'wppb_modify_query_attachements_when_not_logged_in' ); |
| 52 |
if( !function_exists( 'wppb_modify_query_attachements_when_not_logged_in' ) ) { |
| 53 |
function wppb_modify_query_attachements_when_not_logged_in() |
| 54 |
{ |
| 55 |
if ( strpos(wp_get_referer(), 'wp-admin') === false && !is_user_logged_in() ) { |
| 56 |
add_action('wp_ajax_query-attachments', 'wppb_wp_ajax_not_loggedin_query_attachments', 0); |
| 57 |
add_action('wp_ajax_nopriv_query-attachments', 'wppb_wp_ajax_not_loggedin_query_attachments', 0); |
| 58 |
function wppb_wp_ajax_not_loggedin_query_attachments() |
| 59 |
{ |
| 60 |
wp_send_json_success(); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/* restrict file types of the upload field functionality */ |
| 67 |
add_filter('wp_handle_upload_prefilter', 'wppb_upload_file_type'); |
| 68 |
if( !function_exists( 'wppb_upload_file_type' ) ) { |
| 69 |
function wppb_upload_file_type($file) |
| 70 |
{ |
| 71 |
if( isset( $_POST['wppb_upload'] ) && $_POST['wppb_upload'] == 'true' && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['_wpnonce'] ), 'media-form' ) ) { |
| 72 |
|
| 73 |
// file size limits. |
| 74 |
$size = $file['size']; |
| 75 |
$limit = apply_filters('wppb_server_max_upload_size_byte_constant', wppb_return_bytes(ini_get('upload_max_filesize'))); |
| 76 |
if ( $size > $limit ) { |
| 77 |
$limit = $limit / ( 1024 * 1024 ) ; |
| 78 |
$file['error'] = __("Files must be smaller than ", "profile-builder") . $limit . 'MB'; |
| 79 |
} |
| 80 |
|
| 81 |
if (isset($_POST['meta_name']) && !empty($_POST['meta_name'])) { |
| 82 |
$meta_name = sanitize_text_field( $_POST['meta_name'] ); |
| 83 |
/*let's get the field details so we can see if we have any file restrictions */ |
| 84 |
$all_fields = apply_filters( 'wppb_form_fields', get_option('wppb_manage_fields'), array( 'context' => 'upload_helper', 'upload_meta_name' => $meta_name ) ); |
| 85 |
if (!empty($all_fields)) { |
| 86 |
foreach ($all_fields as $field) { |
| 87 |
if ($field['meta-name'] == $meta_name) { |
| 88 |
|
| 89 |
// per-field file size limit |
| 90 |
if ( !empty( $field['max-file-size'] ) && is_numeric( $field['max-file-size'] ) && floatval( $field['max-file-size'] ) > 0 ) { |
| 91 |
$field_limit = floatval( $field['max-file-size'] ) * 1024 * 1024; |
| 92 |
$effective_limit = min( $field_limit, $limit ); |
| 93 |
if ( $size > $effective_limit ) { |
| 94 |
$file['error'] = __( "Files must be smaller than ", "profile-builder" ) . floatval( $field['max-file-size'] ) . 'MB'; |
| 95 |
return $file; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$allowed_upload_extensions = ''; |
| 100 |
|
| 101 |
if ($field['field'] == 'Upload' && !empty($field['allowed-upload-extensions'])) |
| 102 |
$allowed_upload_extensions = $field['allowed-upload-extensions']; |
| 103 |
if ($field['field'] == 'Avatar' && !empty($field['allowed-image-extensions'])) { |
| 104 |
if (trim($field['allowed-image-extensions']) == '.*') |
| 105 |
$allowed_upload_extensions = '.jpg,.jpeg,.gif,.png,.ico'; |
| 106 |
else |
| 107 |
$allowed_upload_extensions = $field['allowed-image-extensions']; |
| 108 |
} |
| 109 |
|
| 110 |
$ext = strtolower( substr(strrchr($file['name'], '.'), 1) ); |
| 111 |
|
| 112 |
if (!empty($allowed_upload_extensions) && $allowed_upload_extensions != '.*') { |
| 113 |
$allowed = str_replace('.', '', array_map('trim', explode(",", strtolower( $allowed_upload_extensions)))); |
| 114 |
//first check if the user uploaded the right type |
| 115 |
if (!in_array($ext, (array)$allowed)) { |
| 116 |
$file['error'] = __("Sorry, you cannot upload this file type for this field.", 'profile-builder'); |
| 117 |
return $file; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
//check if the type is allowed at all by WordPress |
| 122 |
foreach (get_allowed_mime_types() as $key => $value) { |
| 123 |
if (strpos($key, $ext) !== false || $key == $ext) |
| 124 |
return $file; |
| 125 |
} |
| 126 |
|
| 127 |
$file['error'] = __("Sorry, you cannot upload this file type for this field.", 'profile-builder'); |
| 128 |
|
| 129 |
break; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if (empty($_POST['meta_name'])) |
| 136 |
$file['error'] = __("An error occurred, please try again later.", 'profile-builder'); |
| 137 |
} |
| 138 |
|
| 139 |
return $file; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Function that performs validation for the simple upload field |
| 145 |
* |
| 146 |
* @param $field - simple upload field |
| 147 |
* @param $upload - data to be uploaded |
| 148 |
* |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
function wppb_valid_simple_upload( $field, $upload ){ |
| 152 |
$limit = apply_filters( 'wppb_server_max_upload_size_byte_constant', wppb_return_bytes( ini_get( 'upload_max_filesize' ) ) ); |
| 153 |
$allowed_mime_types = get_allowed_mime_types(); |
| 154 |
$all_fields = apply_filters( 'wppb_form_fields', get_option( 'wppb_manage_fields' ), array( 'context' => 'upload_helper', 'upload_meta_name' => $field[ 'meta-name' ] ) ); |
| 155 |
if ( !empty( $all_fields ) ) { |
| 156 |
foreach ( $all_fields as $form_field ) { |
| 157 |
if ($form_field[ 'meta-name' ] == $field[ 'meta-name' ] ) { |
| 158 |
// apply per-field size limit if set |
| 159 |
if ( !empty( $form_field['max-file-size'] ) && is_numeric( $form_field['max-file-size'] ) && floatval( $form_field['max-file-size'] ) > 0 ) { |
| 160 |
$field_limit = floatval( $form_field['max-file-size'] ) * 1024 * 1024; |
| 161 |
$limit = min( $field_limit, $limit ); |
| 162 |
} |
| 163 |
$allowed_upload_extensions = ''; |
| 164 |
if ( $form_field[ 'field' ] == 'Upload' && !empty( $form_field[ 'allowed-upload-extensions' ] ) ) { |
| 165 |
$allowed_upload_extensions = $form_field[ 'allowed-upload-extensions' ]; |
| 166 |
} |
| 167 |
if ( $form_field[ 'field' ] == 'Avatar' ) { |
| 168 |
if ( trim( $field[ 'allowed-image-extensions' ] ) == '.*' || trim( $field[ 'allowed-image-extensions' ] ) == '' ) { |
| 169 |
$allowed_upload_extensions = '.jpg,.jpeg,.gif,.png'; |
| 170 |
} |
| 171 |
else { |
| 172 |
$allowed_upload_extensions = $form_field[ 'allowed-image-extensions' ]; |
| 173 |
} |
| 174 |
} |
| 175 |
if ( !empty( $allowed_upload_extensions ) && $allowed_upload_extensions != '.*' ) { |
| 176 |
$allowed_upload_extensions = str_replace( '.', '', array_map( 'trim', explode( ",", strtolower( $allowed_upload_extensions ) ) ) ); |
| 177 |
} else { |
| 178 |
$allowed = true; |
| 179 |
} |
| 180 |
$allowed_by_wordpress = false; |
| 181 |
foreach ( $allowed_mime_types as $key => $val ){ |
| 182 |
if ( $val == $upload[ 'type' ] ){ |
| 183 |
$possible_extensions = explode( '|', $key ); |
| 184 |
$allowed_by_wordpress = true; |
| 185 |
} |
| 186 |
} |
| 187 |
if ( isset( $possible_extensions ) && $allowed_by_wordpress == true ){ |
| 188 |
if ( !isset( $allowed ) ){ |
| 189 |
$allowed = false; |
| 190 |
foreach ( $allowed_upload_extensions as $extension ){ |
| 191 |
if ( in_array( $extension, $possible_extensions ) ){ |
| 192 |
$allowed = true; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
if ( $upload[ 'size' ] > $limit ){ |
| 197 |
$allowed = false; |
| 198 |
} |
| 199 |
return $allowed; |
| 200 |
} |
| 201 |
else{ |
| 202 |
return false; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Function that registers intermediate avatar sizes |
| 211 |
* |
| 212 |
* @param $field - avatar field |
| 213 |
* |
| 214 |
*/ |
| 215 |
function wppb_add_avatar_sizes( $field ){ |
| 216 |
if( !empty( $field['avatar-size'] ) ) |
| 217 |
add_image_size( 'wppb-avatar-size-'.$field['avatar-size'], $field['avatar-size'], $field['avatar-size'], true ); |
| 218 |
else |
| 219 |
add_image_size( 'wppb-avatar-size-100', 100, 100, true ); |
| 220 |
|
| 221 |
add_image_size( 'wppb-avatar-size-64', 64, 64, true ); |
| 222 |
add_image_size( 'wppb-avatar-size-26', 26, 26, true ); |
| 223 |
} |
| 224 |
|
| 225 |
//Function that registers avatar sizes for userlisting |
| 226 |
function wppb_userlisting_avatar(){ |
| 227 |
$userlisting_posts = get_posts( array( 'posts_per_page' => -1, 'post_status' =>'publish', 'post_type' => 'wppb-ul-cpt', 'orderby' => 'post_date', 'order' => 'ASC' ) ); |
| 228 |
if( !empty( $userlisting_posts ) ){ |
| 229 |
foreach ( $userlisting_posts as $post ){ |
| 230 |
$this_form_settings = get_post_meta( $post->ID, 'wppb_ul_page_settings', true ); |
| 231 |
$all_userlisting_avatar_size = apply_filters( 'all_userlisting_avatar_size', ( isset( $this_form_settings[0]['avatar-size-all-userlisting'] ) ? (int)$this_form_settings[0]['avatar-size-all-userlisting'] : 100 ) ); |
| 232 |
$single_userlisting_avatar_size = apply_filters( 'single_userlisting_avatar_size', ( isset( $this_form_settings[0]['avatar-size-single-userlisting'] ) ? (int)$this_form_settings[0]['avatar-size-single-userlisting'] : 100 ) ); |
| 233 |
|
| 234 |
add_image_size( 'wppb-avatar-size-'.$all_userlisting_avatar_size, $all_userlisting_avatar_size, $all_userlisting_avatar_size, true ); |
| 235 |
add_image_size( 'wppb-avatar-size-'.$single_userlisting_avatar_size, $single_userlisting_avatar_size, $single_userlisting_avatar_size, true ); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Function that checks if the simple upload field belongs to a repeater field with conditional logic enabled |
| 242 |
* |
| 243 |
* @param $field - simple upload field |
| 244 |
* |
| 245 |
* @return bool |
| 246 |
*/ |
| 247 |
function wppb_belongs_to_repeater_with_conditional_logic( $field ){ |
| 248 |
$all_fields = apply_filters( 'wppb_form_fields', get_option( 'wppb_manage_fields' ), array( 'context' => 'upload_helper', 'upload_meta_name' => $field[ 'meta-name' ] ) ); |
| 249 |
if ( !empty( $all_fields ) ) { |
| 250 |
foreach ( $all_fields as $form_field ) { |
| 251 |
if ( $form_field[ 'field' ] == 'Repeater' && isset( $form_field[ 'conditional-logic-enabled' ] ) && $form_field[ 'conditional-logic-enabled' ] == 'yes' ) { |
| 252 |
$repeater_group = get_option( $form_field[ 'meta-name' ], 'not_set' ); |
| 253 |
if ( $repeater_group == 'not_set' ) { |
| 254 |
continue; |
| 255 |
} |
| 256 |
else{ |
| 257 |
$repeater_count = count( $repeater_group ); |
| 258 |
for ( $i = 0; $i < $repeater_count; $i++ ){ |
| 259 |
if ( $repeater_group[ $i ][ 'field' ] == 'Upload' && isset( $repeater_group[ $i ][ 'simple-upload' ] ) && $repeater_group[ $i ][ 'simple-upload' ] == 'yes' && isset( $_REQUEST[ $form_field[ 'meta-name' ] . '_extra_groups_count' ] ) ){ |
| 260 |
$groups = absint( $_REQUEST[ $form_field[ 'meta-name' ] . '_extra_groups_count' ] ); |
| 261 |
for ( $j = 0; $j <= $groups; $j++ ){ |
| 262 |
$name = $repeater_group[ $i ][ 'meta-name' ]; |
| 263 |
if ( $j != 0 ){ |
| 264 |
$name .= '_' . $j; |
| 265 |
} |
| 266 |
if ( $field[ 'meta-name' ] == $name ){ |
| 267 |
return true; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
function wppb_default_fields_make_upload_button( $field, $input_value, $extra_attr = '' ){ |
| 280 |
// change the upload limit displayed in the upload window (per-field aware) |
| 281 |
$per_field_max = $field; |
| 282 |
add_filter('upload_size_limit', function($wp_limit) use ($per_field_max) { |
| 283 |
$server_limit = apply_filters('wppb_server_max_upload_size_byte_constant', wppb_return_bytes(ini_get('upload_max_filesize'))); |
| 284 |
if ( !empty( $per_field_max['max-file-size'] ) && is_numeric( $per_field_max['max-file-size'] ) && floatval( $per_field_max['max-file-size'] ) > 0 ) { |
| 285 |
$field_limit = floatval( $per_field_max['max-file-size'] ) * 1024 * 1024; |
| 286 |
return min( $field_limit, $server_limit ); |
| 287 |
} |
| 288 |
return $server_limit; |
| 289 |
}, 10, 1); |
| 290 |
|
| 291 |
$upload_button = ''; |
| 292 |
$upload_input_id = str_replace( '-', '_', Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] ) ); |
| 293 |
|
| 294 |
/* container for the image preview (or file ico) and name and file type */ |
| 295 |
if( !empty( $input_value ) ){ |
| 296 |
/* it can hold multiple attachments separated by comma */ |
| 297 |
$values = explode( ',', $input_value ); |
| 298 |
foreach( $values as $value ) { |
| 299 |
if( !empty( $value ) && is_numeric( $value ) ){ |
| 300 |
$thumbnail = wp_get_attachment_image($value, array(80, 80), true); |
| 301 |
$file_name = get_the_title($value); |
| 302 |
$file_type = get_post_mime_type($value); |
| 303 |
$attachment_url = wp_get_attachment_url($value); |
| 304 |
$upload_button .= '<div id="' . esc_attr($upload_input_id) . '_info_container" class="upload-field-details" data-attachment_id="' . $value . '">'; |
| 305 |
$upload_button .= '<div class="file-thumb">'; |
| 306 |
$upload_button .= "<a href='{$attachment_url}' target='_blank' class='wppb-attachment-link'>" . $thumbnail . "</a>"; |
| 307 |
$upload_button .= '</div>'; |
| 308 |
$upload_button .= '<p><span class="file-name">'; |
| 309 |
$upload_button .= $file_name; |
| 310 |
$upload_button .= '</span><span class="file-type">'; |
| 311 |
$upload_button .= $file_type; |
| 312 |
$upload_button .= '</span>'; |
| 313 |
$upload_button .= '<span class="wppb-remove-upload" tabindex="0">' . apply_filters( 'wppb_upload_button_remove_label', __( 'Remove', 'profile-builder' ) ) . '</span>'; |
| 314 |
$upload_button .= '</p></div>'; |
| 315 |
} |
| 316 |
} |
| 317 |
$hide_upload_button = ' style="display:none;"'; |
| 318 |
} |
| 319 |
else{ |
| 320 |
$hide_upload_button = ''; |
| 321 |
} |
| 322 |
|
| 323 |
if ( isset( $field[ 'simple-upload' ] ) && $field[ 'simple-upload' ] == 'yes' ){ |
| 324 |
//If selected accordingly in form fields, generate a simple upload button |
| 325 |
$upload_button .= '<input type="file" id="upload_' . esc_attr(Wordpress_Creation_Kit_PB::wck_generate_slug($field['meta-name'], $field)) . '_button" class="wppb_simple_upload" name="simple_upload_'. esc_attr( Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'], $field ) ) .'"'; |
| 326 |
$upload_button .= $hide_upload_button . '>'; |
| 327 |
$upload_button .= '<p id="p_simple_upload_'. esc_attr(Wordpress_Creation_Kit_PB::wck_generate_slug($field['meta-name'], $field)) .'"></p>'; |
| 328 |
$limit = apply_filters( 'wppb_server_max_upload_size_byte_constant', wppb_return_bytes( ini_get( 'upload_max_filesize' ) ) ); |
| 329 |
$all_fields = apply_filters( 'wppb_form_fields', get_option( 'wppb_manage_fields' ), array( 'context' => 'upload_helper', 'upload_meta_name' => $field[ 'meta-name' ] ) ); |
| 330 |
if ( !empty( $all_fields ) ) { |
| 331 |
foreach ( $all_fields as $form_field ) { |
| 332 |
if ($form_field[ 'meta-name' ] == $field[ 'meta-name' ] ) { |
| 333 |
// apply per-field size limit if set |
| 334 |
if ( !empty( $form_field['max-file-size'] ) && is_numeric( $form_field['max-file-size'] ) && floatval( $form_field['max-file-size'] ) > 0 ) { |
| 335 |
$field_limit = floatval( $form_field['max-file-size'] ) * 1024 * 1024; |
| 336 |
$limit = min( $field_limit, $limit ); |
| 337 |
} |
| 338 |
$allowed_upload_extensions = ''; |
| 339 |
if ( $form_field[ 'field' ] == 'Upload' && !empty( $form_field[ 'allowed-upload-extensions' ] ) ) { |
| 340 |
$allowed_upload_extensions = $form_field[ 'allowed-upload-extensions' ]; |
| 341 |
} |
| 342 |
if ( $form_field[ 'field' ] == 'Avatar' ) { |
| 343 |
if ( trim( $field[ 'allowed-image-extensions' ] ) == '.*' || trim( $field[ 'allowed-image-extensions' ] ) == '' ) { |
| 344 |
$allowed_upload_extensions = '.jpg,.jpeg,.gif,.png'; |
| 345 |
} |
| 346 |
else { |
| 347 |
$allowed_upload_extensions = $form_field[ 'allowed-image-extensions' ]; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
if ( !empty( $allowed_upload_extensions ) && $allowed_upload_extensions != '.*' ) { |
| 352 |
$allowed_extensions = str_replace( '.', '', array_map( 'trim', explode( ",", strtolower( $allowed_upload_extensions ) ) ) ); |
| 353 |
$allowed_extensions = implode( ',', $allowed_extensions ); |
| 354 |
} else { |
| 355 |
$allowed_extensions = ''; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
$upload_button .= '<input id="allowed_extensions_simple_upload_'. esc_attr( $upload_input_id ) .'" type="hidden" size="36" name="allowed_extensions_simple_upload_'. esc_attr( Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'], $field ) ) .'" value="'. $allowed_extensions .'"/>'; |
| 360 |
$upload_button .= '<input id="size_limit_simple_upload_'. esc_attr( $upload_input_id ) .'" type="hidden" name="size_limit_simple_upload_'. esc_attr( Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'], $field ) ) .'" value="'. esc_attr( $limit ) .'"/>'; |
| 361 |
$allowed_mime_types = get_allowed_mime_types(); |
| 362 |
$allowed_types = ''; |
| 363 |
if ( !empty( $allowed_mime_types ) ) { |
| 364 |
foreach ($allowed_mime_types as $key => $val){ |
| 365 |
$allowed_types .= $key . '=>' . $val . ','; |
| 366 |
} |
| 367 |
} |
| 368 |
$error_messages = array( |
| 369 |
'limit_error_message' => __( 'Files must be smaller than ', 'profile-builder' ), |
| 370 |
'upload_type_error_message' => __( 'Sorry, you cannot upload this file type for this field.', 'profile-builder' ), |
| 371 |
); |
| 372 |
$size_limit = array( |
| 373 |
'size_limit' => $limit |
| 374 |
); |
| 375 |
$allowed_wordpress_formats = array( |
| 376 |
'allowed_wordpress_formats' => $allowed_mime_types |
| 377 |
); |
| 378 |
wp_localize_script( 'wppb-upload-script', 'wppb_error_messages', $error_messages ); |
| 379 |
wp_localize_script( 'wppb-upload-script', 'wppb_limit', $size_limit ); |
| 380 |
wp_localize_script( 'wppb-upload-script', 'wppb_allowed_wordpress_formats', $allowed_wordpress_formats ); |
| 381 |
} |
| 382 |
else{ |
| 383 |
//Otherwise, generate the WordPress upload button |
| 384 |
$upload_button .= '<a href="#" class="button wppb_upload_button" id="upload_' . esc_attr(Wordpress_Creation_Kit_PB::wck_generate_slug($field['meta-name'], $field)) . '_button" '.$hide_upload_button.' data-uploader_title="' . $field["field-title"] . '" data-uploader_button_text="'. __( 'Select File', 'profile-builder' ) .'" data-upload_mn="'. $field['meta-name'] .'" data-upload_input="' . esc_attr($upload_input_id) . '"'; |
| 385 |
|
| 386 |
if (is_user_logged_in()) |
| 387 |
$upload_button .= ' data-uploader_logged_in="true"'; |
| 388 |
$upload_button .= ' data-multiple_upload="false"'; |
| 389 |
|
| 390 |
$upload_button .= '>' . apply_filters( 'wppb_upload_button_select_label', __( 'Upload ', 'profile-builder' ) ) . '</a>'; |
| 391 |
} |
| 392 |
|
| 393 |
$upload_button .= '<input id="'. esc_attr( $upload_input_id ) .'" type="hidden" size="36" name="'. esc_attr( Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'], $field ) ) .'" value="'. $input_value .'"/>'; |
| 394 |
return $upload_button; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Function to save an attachment from the simple upload field |
| 399 |
* @param $field_name |
| 400 |
* @return string|WP_Error |
| 401 |
*/ |
| 402 |
function wppb_default_fields_save_simple_upload_file( $field_name ) { |
| 403 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 404 |
$upload_overrides = array('test_form' => false); |
| 405 |
|
| 406 |
if( isset( $_FILES[$field_name] ) ) |
| 407 |
$file = wp_handle_upload($_FILES[$field_name], $upload_overrides); |
| 408 |
|
| 409 |
if (isset($file['error'])) { |
| 410 |
return new WP_Error('upload_error', $file['error']); |
| 411 |
} |
| 412 |
$filename = isset( $_FILES[$field_name]['name'] ) ? sanitize_text_field( $_FILES[$field_name]['name'] ) : ''; |
| 413 |
$wp_filetype = wp_check_filetype($filename, null); |
| 414 |
$attachment = array( |
| 415 |
'post_mime_type' => $wp_filetype['type'], |
| 416 |
'post_title' => $filename, |
| 417 |
'post_content' => '', |
| 418 |
'post_status' => 'inherit' |
| 419 |
); |
| 420 |
$attachment_id = wp_insert_attachment($attachment, $file['file']); |
| 421 |
if (!is_wp_error($attachment_id) && is_numeric($attachment_id)) { |
| 422 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 423 |
$attachment_data = wp_generate_attachment_metadata($attachment_id, $file['file']); |
| 424 |
wp_update_attachment_metadata($attachment_id, $attachment_data); |
| 425 |
return trim($attachment_id); |
| 426 |
} else { |
| 427 |
return ''; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
// Deferred to plugins_loaded so older Profile Builder Pro versions (which declare |
| 432 |
// wppb_verify_attachment_id unconditionally during their own file load) win the |
| 433 |
// declaration race and our function_exists guard then skips — avoiding a fatal. |
| 434 |
add_action( 'plugins_loaded', 'wppb_register_attachment_ownership_helpers', 20 ); |
| 435 |
function wppb_register_attachment_ownership_helpers() { |
| 436 |
|
| 437 |
/** |
| 438 |
* Verifies if an attachment either doesn't exist or already belongs to the user. |
| 439 |
* Used for IDOR protection on both Upload and Avatar fields. |
| 440 |
* |
| 441 |
* @param string|int $attachment_id The attachment post ID to verify. |
| 442 |
* @param int|null $user_id The user ID to check ownership against. |
| 443 |
* |
| 444 |
* @return bool True if the attachment is valid for this user, false otherwise. |
| 445 |
*/ |
| 446 |
if ( !function_exists( 'wppb_verify_attachment_id' ) ) { |
| 447 |
function wppb_verify_attachment_id( $attachment_id, $user_id = null ) { |
| 448 |
if ( $attachment_id !== '' && is_numeric( $attachment_id ) ) { |
| 449 |
$attachment = get_post( absint( trim( $attachment_id ) ) ); |
| 450 |
if ( $attachment && $attachment->post_type === 'attachment' ) { |
| 451 |
|
| 452 |
// Get current user info for admin bypass checks |
| 453 |
$current_user_id = get_current_user_id(); |
| 454 |
$current_user = $current_user_id ? get_userdata( $current_user_id ) : null; |
| 455 |
$is_admin = $current_user && current_user_can( 'manage_options' ); |
| 456 |
|
| 457 |
if ( $user_id ) { |
| 458 |
// Allow admins to upload files for users |
| 459 |
if ( $is_admin ) { |
| 460 |
return true; |
| 461 |
} |
| 462 |
// Only update if the attachment belongs to the user or has no author (post_author = 0) |
| 463 |
if ( $attachment->post_author == $user_id || $attachment->post_author == $current_user_id || $attachment->post_author == 0 ) { |
| 464 |
return true; |
| 465 |
} |
| 466 |
} else { |
| 467 |
// If no user ID is provided, check if current user is admin |
| 468 |
if ( $is_admin ) { |
| 469 |
return true; |
| 470 |
} |
| 471 |
// If no user ID is provided, check if the attachment has no author |
| 472 |
if ( $attachment->post_author == $current_user_id || $attachment->post_author == 0 ) { |
| 473 |
return true; |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
return false; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Validates attachment ownership and updates the user meta and post author. |
| 484 |
* Used for IDOR-safe saving on both Upload and Avatar fields. |
| 485 |
* |
| 486 |
* @param string|int $attachment_id The attachment post ID. |
| 487 |
* @param array $field The field definition array (must contain 'meta-name'). |
| 488 |
* @param int $user_id The user ID to save for. |
| 489 |
*/ |
| 490 |
if ( !function_exists( 'wppb_save_attachment_id' ) ) { |
| 491 |
function wppb_save_attachment_id( $attachment_id, $field, $user_id ) { |
| 492 |
// Verify that the attachment either doesn't exist or already belongs to the user |
| 493 |
if ( wppb_verify_attachment_id( $attachment_id, $user_id ) ) { |
| 494 |
update_user_meta( $user_id, $field['meta-name'], absint( $attachment_id ) ); |
| 495 |
wp_update_post( array( |
| 496 |
'ID' => absint( trim( $attachment_id ) ), |
| 497 |
'post_author' => $user_id |
| 498 |
) ); |
| 499 |
} else { |
| 500 |
update_user_meta( $user_id, $field['meta-name'], '' ); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
function wppb_check_that_field_is_defined( $meta_name, $field_types = array() ){ |
| 507 |
|
| 508 |
if( empty( $meta_name ) ) |
| 509 |
return false; |
| 510 |
|
| 511 |
$defined_fields = apply_filters( 'wppb_form_fields', get_option( 'wppb_manage_fields' ), array( 'context' => 'upload_helper', 'upload_meta_name' => $meta_name ) ); |
| 512 |
|
| 513 |
if( empty( $defined_fields ) ) |
| 514 |
return false; |
| 515 |
else { |
| 516 |
|
| 517 |
if( empty( $field_types ) ){ |
| 518 |
foreach( $defined_fields as $field ){ |
| 519 |
|
| 520 |
if( $field['meta-name'] == $meta_name ) |
| 521 |
return true; |
| 522 |
|
| 523 |
} |
| 524 |
} else { |
| 525 |
foreach( $defined_fields as $field ){ |
| 526 |
|
| 527 |
if( in_array( $field['field'], $field_types ) && $field['meta-name'] == $meta_name ) |
| 528 |
return true; |
| 529 |
|
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
} |
| 534 |
|
| 535 |
return false; |
| 536 |
|
| 537 |
} |