PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.6
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.6
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 / upload / upload_helper_functions.php

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

381 lines 20.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /* Set up upload field for frontend */
3 /* overwrite the two functions for when an upload is made from the frontend so they don't check for a logged in user */
4 if( strpos( wp_get_referer(), 'wp-admin' ) === false && isset( $_REQUEST['action'] ) && 'upload-attachment' == $_REQUEST['action'] ){
5 if( !function_exists( 'check_ajax_referer' ) ){
6 function check_ajax_referer( ) {
7 return true;
8 }
9 }
10
11 if( !function_exists( 'auth_redirect' ) ){
12 function auth_redirect() {
13 return true;
14 }
15 }
16 }
17
18 /* 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 */
19 add_action( 'current_screen', 'wppb_create_fake_user_when_uploading_and_not_logged_in' );
20 if( !function_exists( 'wppb_create_fake_user_when_uploading_and_not_logged_in' ) ) {
21 function wppb_create_fake_user_when_uploading_and_not_logged_in()
22 {
23 if ( isset($_REQUEST['action']) && 'upload-attachment' == $_REQUEST['action'] && isset($_REQUEST['wppb_upload']) && 'true' == $_REQUEST['wppb_upload'] ) {
24 if (!is_user_logged_in() || !current_user_can('upload_files') || !current_user_can('edit_posts')) {
25 global $current_user;
26 $current_user = new WP_User(0, 'frontend_uploader');
27 $current_user->allcaps = array("upload_files" => true, "edit_posts" => true, "edit_others_posts" => true, "edit_pages" => true, "edit_others_pages" => true);
28 }
29 }
30 }
31 }
32
33 /* for a request of a upload from the frontend and no user is logged in don't query for attachments */
34 add_action( 'after_setup_theme', 'wppb_modify_query_attachements_when_not_logged_in' );
35 if( !function_exists( 'wppb_modify_query_attachements_when_not_logged_in' ) ) {
36 function wppb_modify_query_attachements_when_not_logged_in()
37 {
38 if (strpos(wp_get_referer(), 'wp-admin') === false && !is_user_logged_in()) {
39 add_action('wp_ajax_query-attachments', 'wppb_wp_ajax_not_loggedin_query_attachments', 0);
40 add_action('wp_ajax_nopriv_query-attachments', 'wppb_wp_ajax_not_loggedin_query_attachments', 0);
41 function wppb_wp_ajax_not_loggedin_query_attachments()
42 {
43 wp_send_json_success();
44 }
45 }
46 }
47 }
48
49 /* restrict file types of the upload field functionality */
50 add_filter('wp_handle_upload_prefilter', 'wppb_upload_file_type');
51 if( !function_exists( 'wppb_upload_file_type' ) ) {
52 function wppb_upload_file_type($file)
53 {
54 if( isset( $_POST['wppb_upload'] ) && $_POST['wppb_upload'] == 'true' ) {
55
56 // file size limits.
57 $size = $file['size'];
58 $limit = apply_filters('wppb_server_max_upload_size_byte_constant', wppb_return_bytes(ini_get('upload_max_filesize')));
59 if ( $size > $limit ) {
60 $limit = $limit / ( 1024 * 1024 ) ;
61 $file['error'] = __("Files must be smaller than ", "profile-builder") . $limit . 'MB';
62 }
63
64 if (isset($_POST['meta_name']) && !empty($_POST['meta_name'])) {
65 $meta_name = sanitize_text_field( $_POST['meta_name'] );
66 /*let's get the field details so we can see if we have any file restrictions */
67 $all_fields = apply_filters( 'wppb_form_fields', get_option('wppb_manage_fields'), array( 'context' => 'upload_helper', 'upload_meta_name' => $meta_name ) );
68 if (!empty($all_fields)) {
69 foreach ($all_fields as $field) {
70 if ($field['meta-name'] == $meta_name) {
71 $allowed_upload_extensions = '';
72 if ($field['field'] == 'Upload' && !empty($field['allowed-upload-extensions']))
73 $allowed_upload_extensions = $field['allowed-upload-extensions'];
74 if ($field['field'] == 'Avatar' && !empty($field['allowed-image-extensions'])) {
75 if (trim($field['allowed-image-extensions']) == '.*')
76 $allowed_upload_extensions = '.jpg,.jpeg,.gif,.png,.ico';
77 else
78 $allowed_upload_extensions = $field['allowed-image-extensions'];
79 }
80
81 $ext = strtolower( substr(strrchr($file['name'], '.'), 1) );
82
83 if (!empty($allowed_upload_extensions) && $allowed_upload_extensions != '.*') {
84 $allowed = str_replace('.', '', array_map('trim', explode(",", strtolower( $allowed_upload_extensions))));
85 //first check if the user uploaded the right type
86 if (!in_array($ext, (array)$allowed)) {
87 $file['error'] = __("Sorry, you cannot upload this file type for this field.", 'profile-builder');
88 return $file;
89 }
90 }
91
92 //check if the type is allowed at all by WordPress
93 foreach (get_allowed_mime_types() as $key => $value) {
94 if (strpos($key, $ext) !== false || $key == $ext)
95 return $file;
96 }
97 $file['error'] = __("Sorry, you cannot upload this file type for this field.", 'profile-builder');
98 }
99 }
100 }
101 }
102
103 if (empty($_POST['meta_name']))
104 $file['error'] = __("An error occurred, please try again later.", 'profile-builder');
105 }
106
107 return $file;
108 }
109 }
110
111 /**
112 * Function that performs validation for the simple upload field
113 *
114 * @param $field - simple upload field
115 * @param $upload - data to be uploaded
116 *
117 * @return bool
118 */
119 function wppb_valid_simple_upload( $field, $upload ){
120 $limit = apply_filters( 'wppb_server_max_upload_size_byte_constant', wppb_return_bytes( ini_get( 'upload_max_filesize' ) ) );
121 $allowed_mime_types = get_allowed_mime_types();
122 $all_fields = apply_filters( 'wppb_form_fields', get_option( 'wppb_manage_fields' ), array( 'context' => 'upload_helper', 'upload_meta_name' => $field[ 'meta-name' ] ) );
123 if ( !empty( $all_fields ) ) {
124 foreach ( $all_fields as $form_field ) {
125 if ($form_field[ 'meta-name' ] == $field[ 'meta-name' ] ) {
126 $allowed_upload_extensions = '';
127 if ( $form_field[ 'field' ] == 'Upload' && !empty( $form_field[ 'allowed-upload-extensions' ] ) ) {
128 $allowed_upload_extensions = $form_field[ 'allowed-upload-extensions' ];
129 }
130 if ( $form_field[ 'field' ] == 'Avatar' ) {
131 if ( trim( $field[ 'allowed-image-extensions' ] ) == '.*' || trim( $field[ 'allowed-image-extensions' ] ) == '' ) {
132 $allowed_upload_extensions = '.jpg,.jpeg,.gif,.png';
133 }
134 else {
135 $allowed_upload_extensions = $form_field[ 'allowed-image-extensions' ];
136 }
137 }
138 if ( !empty( $allowed_upload_extensions ) && $allowed_upload_extensions != '.*' ) {
139 $allowed_upload_extensions = str_replace( '.', '', array_map( 'trim', explode( ",", strtolower( $allowed_upload_extensions ) ) ) );
140 } else {
141 $allowed = true;
142 }
143 $allowed_by_wordpress = false;
144 foreach ( $allowed_mime_types as $key => $val ){
145 if ( $val == $upload[ 'type' ] ){
146 $possible_extensions = explode( '|', $key );
147 $allowed_by_wordpress = true;
148 }
149 }
150 if ( isset( $possible_extensions ) && $allowed_by_wordpress == true ){
151 if ( !isset( $allowed ) ){
152 $allowed = false;
153 foreach ( $allowed_upload_extensions as $extension ){
154 if ( in_array( $extension, $possible_extensions ) ){
155 $allowed = true;
156 }
157 }
158 }
159 if ( $upload[ 'size' ] > $limit ){
160 $allowed = false;
161 }
162 return $allowed;
163 }
164 else{
165 return false;
166 }
167 }
168 }
169 }
170 }
171
172 /**
173 * Function that registers intermediate avatar sizes
174 *
175 * @param $field - avatar field
176 *
177 */
178 function wppb_add_avatar_sizes( $field ){
179 if( !empty( $field['avatar-size'] ) )
180 add_image_size( 'wppb-avatar-size-'.$field['avatar-size'], $field['avatar-size'], $field['avatar-size'], true );
181 else
182 add_image_size( 'wppb-avatar-size-100', 100, 100, true );
183
184 add_image_size( 'wppb-avatar-size-64', 64, 64, true );
185 add_image_size( 'wppb-avatar-size-26', 26, 26, true );
186 }
187
188 //Function that registers avatar sizes for userlisting
189 function wppb_userlisting_avatar(){
190 $userlisting_posts = get_posts( array( 'posts_per_page' => -1, 'post_status' =>'publish', 'post_type' => 'wppb-ul-cpt', 'orderby' => 'post_date', 'order' => 'ASC' ) );
191 if( !empty( $userlisting_posts ) ){
192 foreach ( $userlisting_posts as $post ){
193 $this_form_settings = get_post_meta( $post->ID, 'wppb_ul_page_settings', true );
194 $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 ) );
195 $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 ) );
196
197 add_image_size( 'wppb-avatar-size-'.$all_userlisting_avatar_size, $all_userlisting_avatar_size, $all_userlisting_avatar_size, true );
198 add_image_size( 'wppb-avatar-size-'.$single_userlisting_avatar_size, $single_userlisting_avatar_size, $single_userlisting_avatar_size, true );
199 }
200 }
201 }
202
203 /**
204 * Function that checks if the simple upload field belongs to a repeater field with conditional logic enabled
205 *
206 * @param $field - simple upload field
207 *
208 * @return bool
209 */
210 function wppb_belongs_to_repeater_with_conditional_logic( $field ){
211 $all_fields = apply_filters( 'wppb_form_fields', get_option( 'wppb_manage_fields' ), array( 'context' => 'upload_helper', 'upload_meta_name' => $field[ 'meta-name' ] ) );
212 if ( !empty( $all_fields ) ) {
213 foreach ( $all_fields as $form_field ) {
214 if ( $form_field[ 'field' ] == 'Repeater' && isset( $form_field[ 'conditional-logic-enabled' ] ) && $form_field[ 'conditional-logic-enabled' ] == 'yes' ) {
215 $repeater_group = get_option( $form_field[ 'meta-name' ], 'not_set' );
216 if ( $repeater_group == 'not_set' ) {
217 continue;
218 }
219 else{
220 $repeater_count = count( $repeater_group );
221 for ( $i = 0; $i < $repeater_count; $i++ ){
222 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' ] ) ){
223 $groups = absint( $_REQUEST[ $form_field[ 'meta-name' ] . '_extra_groups_count' ] );
224 for ( $j = 0; $j <= $groups; $j++ ){
225 $name = $repeater_group[ $i ][ 'meta-name' ];
226 if ( $j != 0 ){
227 $name .= '_' . $j;
228 }
229 if ( $field[ 'meta-name' ] == $name ){
230 return true;
231 }
232 }
233 }
234 }
235 }
236 }
237 }
238 }
239 return false;
240 }
241
242 function wppb_make_upload_button( $field, $input_value, $extra_attr = '' ){
243 // change the upload limit. This is not functional.
244 // just for display in the upload window. see upload_helper_functions.php for the actual restriction.
245 add_filter('upload_size_limit', function($limit, $u, $p){
246 return apply_filters('wppb_server_max_upload_size_byte_constant', wppb_return_bytes(ini_get('upload_max_filesize')));
247 }, 10, 3);
248
249 $upload_button = '';
250 $upload_input_id = str_replace( '-', '_', Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'] ) );
251
252 /* container for the image preview (or file ico) and name and file type */
253 if( !empty( $input_value ) ){
254 /* it can hold multiple attachments separated by comma */
255 $values = explode( ',', $input_value );
256 foreach( $values as $value ) {
257 if( !empty( $value ) && is_numeric( $value ) ){
258 $thumbnail = wp_get_attachment_image($value, array(80, 80), true);
259 $file_name = get_the_title($value);
260 $file_type = get_post_mime_type($value);
261 $attachment_url = wp_get_attachment_url($value);
262 $upload_button .= '<div id="' . esc_attr($upload_input_id) . '_info_container" class="upload-field-details" data-attachment_id="' . $value . '">';
263 $upload_button .= '<div class="file-thumb">';
264 $upload_button .= "<a href='{$attachment_url}' target='_blank' class='wppb-attachment-link'>" . $thumbnail . "</a>";
265 $upload_button .= '</div>';
266 $upload_button .= '<p><span class="file-name">';
267 $upload_button .= $file_name;
268 $upload_button .= '</span><span class="file-type">';
269 $upload_button .= $file_type;
270 $upload_button .= '</span>';
271 $upload_button .= '<span class="wppb-remove-upload" tabindex="0">' . apply_filters( 'wppb_upload_button_remove_label', __( 'Remove', 'profile-builder' ) ) . '</span>';
272 $upload_button .= '</p></div>';
273 }
274 }
275 $hide_upload_button = ' style="display:none;"';
276 }
277 else{
278 $hide_upload_button = '';
279 }
280
281 if ( isset( $field[ 'simple-upload' ] ) && $field[ 'simple-upload' ] == 'yes' ){
282 //If selected accordingly in form fields, generate a simple upload button
283 $upload_button .= '<input type="file" id="upload_' . esc_attr(Wordpress_Creation_Kit_PB::wck_generate_slug($field['meta-name'], $field)) . '_button" name="simple_upload_'. esc_attr( Wordpress_Creation_Kit_PB::wck_generate_slug( $field['meta-name'], $field ) ) .'"';
284 $upload_button .= $hide_upload_button . '>';
285 $upload_button .= '<p id="p_simple_upload_'. esc_attr(Wordpress_Creation_Kit_PB::wck_generate_slug($field['meta-name'], $field)) .'"></p>';
286 $limit = apply_filters( 'wppb_server_max_upload_size_byte_constant', wppb_return_bytes( ini_get( 'upload_max_filesize' ) ) );
287 $all_fields = apply_filters( 'wppb_form_fields', get_option( 'wppb_manage_fields' ), array( 'context' => 'upload_helper', 'upload_meta_name' => $field[ 'meta-name' ] ) );
288 if ( !empty( $all_fields ) ) {
289 foreach ( $all_fields as $form_field ) {
290 if ($form_field[ 'meta-name' ] == $field[ 'meta-name' ] ) {
291 $allowed_upload_extensions = '';
292 if ( $form_field[ 'field' ] == 'Upload' && !empty( $form_field[ 'allowed-upload-extensions' ] ) ) {
293 $allowed_upload_extensions = $form_field[ 'allowed-upload-extensions' ];
294 }
295 if ( $form_field[ 'field' ] == 'Avatar' ) {
296 if ( trim( $field[ 'allowed-image-extensions' ] ) == '.*' || trim( $field[ 'allowed-image-extensions' ] ) == '' ) {
297 $allowed_upload_extensions = '.jpg,.jpeg,.gif,.png';
298 }
299 else {
300 $allowed_upload_extensions = $form_field[ 'allowed-image-extensions' ];
301 }
302 }
303 }
304 if ( !empty( $allowed_upload_extensions ) && $allowed_upload_extensions != '.*' ) {
305 $allowed_extensions = str_replace( '.', '', array_map( 'trim', explode( ",", strtolower( $allowed_upload_extensions ) ) ) );
306 $allowed_extensions = implode( ',', $allowed_extensions );
307 } else {
308 $allowed_extensions = '';
309 }
310 }
311 }
312 $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 .'"/>';
313 $allowed_mime_types = get_allowed_mime_types();
314 $allowed_types = '';
315 if ( !empty( $allowed_mime_types ) ) {
316 foreach ($allowed_mime_types as $key => $val){
317 $allowed_types .= $key . '=>' . $val . ',';
318 }
319 }
320 $error_messages = array(
321 'limit_error_message' => __( 'Files must be smaller than ', 'profile-builder' ),
322 'upload_type_error_message' => __( 'Sorry, you cannot upload this file type for this field.', 'profile-builder' ),
323 );
324 $size_limit = array(
325 'size_limit' => $limit
326 );
327 $allowed_wordpress_formats = array(
328 'allowed_wordpress_formats' => $allowed_mime_types
329 );
330 wp_localize_script( 'wppb-upload-script', 'wppb_error_messages', $error_messages );
331 wp_localize_script( 'wppb-upload-script', 'wppb_limit', $size_limit );
332 wp_localize_script( 'wppb-upload-script', 'wppb_allowed_wordpress_formats', $allowed_wordpress_formats );
333 }
334 else{
335 //Otherwise, generate the WordPress upload button
336 $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) . '"';
337
338 if (is_user_logged_in())
339 $upload_button .= ' data-uploader_logged_in="true"';
340 $upload_button .= ' data-multiple_upload="false"';
341
342 $upload_button .= '>' . apply_filters( 'wppb_upload_button_select_label', __( 'Upload ', 'profile-builder' ) ) . '</a>';
343 }
344
345 $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 .'"/>';
346 return $upload_button;
347 }
348
349 /**
350 * Function to save an attachment from the simple upload field
351 * @param $field_name
352 * @return string|WP_Error
353 */
354 function wppb_save_simple_upload_file ( $field_name ){
355 require_once(ABSPATH . 'wp-admin/includes/file.php');
356 $upload_overrides = array('test_form' => false);
357
358 if( isset( $_FILES[$field_name] ) )
359 $file = wp_handle_upload($_FILES[$field_name], $upload_overrides);
360
361 if (isset($file['error'])) {
362 return new WP_Error('upload_error', $file['error']);
363 }
364 $filename = isset( $_FILES[$field_name]['name'] ) ? sanitize_text_field( $_FILES[$field_name]['name'] ) : '';
365 $wp_filetype = wp_check_filetype($filename, null);
366 $attachment = array(
367 'post_mime_type' => $wp_filetype['type'],
368 'post_title' => $filename,
369 'post_content' => '',
370 'post_status' => 'inherit'
371 );
372 $attachment_id = wp_insert_attachment($attachment, $file['file']);
373 if (!is_wp_error($attachment_id) && is_numeric($attachment_id)) {
374 require_once(ABSPATH . 'wp-admin/includes/image.php');
375 $attachment_data = wp_generate_attachment_metadata($attachment_id, $file['file']);
376 wp_update_attachment_metadata($attachment_id, $attachment_data);
377 return trim($attachment_id);
378 } else {
379 return '';
380 }
381 }