PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 2.5.4
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v2.5.4
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 2.0.7 All 340 releases
profile-builder / features / functions.php

functions.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 2.5.4, at features/functions.php

914 lines 37.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions Load
4 *
5 */
6
7 // whitelist options, you can add more register_settings changing the second parameter
8 function wppb_register_settings() {
9 register_setting( 'wppb_option_group', 'wppb_default_settings' );
10 register_setting( 'wppb_general_settings', 'wppb_general_settings', 'wppb_general_settings_sanitize' );
11 register_setting( 'wppb_display_admin_settings', 'wppb_display_admin_settings' );
12 register_setting( 'wppb_profile_builder_pro_serial', 'wppb_profile_builder_pro_serial' );
13 register_setting( 'wppb_profile_builder_hobbyist_serial', 'wppb_profile_builder_hobbyist_serial' );
14 register_setting( 'wppb_module_settings', 'wppb_module_settings' );
15 register_setting( 'wppb_module_settings_description', 'wppb_module_settings_description' );
16 register_setting( 'customRedirectSettings', 'customRedirectSettings' );
17 register_setting( 'customUserListingSettings', 'customUserListingSettings' );
18 register_setting( 'reCaptchaSettings', 'reCaptchaSettings' );
19 register_setting( 'emailCustomizer', 'emailCustomizer' );
20 }
21
22
23
24
25 // WPML support
26 function wppb_icl_t($context, $name, $value){
27 if( function_exists( 'icl_t' ) )
28 return icl_t( $context, $name, $value );
29
30 else
31 return $value;
32 }
33
34
35 function wppb_add_plugin_stylesheet() {
36 $wppb_generalSettings = get_option( 'wppb_general_settings' );
37
38 if ( ( file_exists( WPPB_PLUGIN_DIR . '/assets/css/style-front-end.css' ) ) && ( isset( $wppb_generalSettings['extraFieldsLayout'] ) && ( $wppb_generalSettings['extraFieldsLayout'] == 'default' ) ) ){
39 wp_register_style( 'wppb_stylesheet', WPPB_PLUGIN_URL . 'assets/css/style-front-end.css', array(), PROFILE_BUILDER_VERSION );
40 wp_enqueue_style( 'wppb_stylesheet' );
41 }
42 if( is_rtl() ) {
43 if ( ( file_exists( WPPB_PLUGIN_DIR . '/assets/css/rtl.css' ) ) && ( isset( $wppb_generalSettings['extraFieldsLayout'] ) && ( $wppb_generalSettings['extraFieldsLayout'] == 'default' ) ) ){
44 wp_register_style( 'wppb_stylesheet_rtl', WPPB_PLUGIN_URL . 'assets/css/rtl.css', array(), PROFILE_BUILDER_VERSION );
45 wp_enqueue_style( 'wppb_stylesheet_rtl' );
46 }
47 }
48 }
49
50
51 function wppb_show_admin_bar($content){
52 global $current_user;
53
54 $adminSettingsPresent = get_option('wppb_display_admin_settings','not_found');
55 $show = null;
56
57 if ($adminSettingsPresent != 'not_found' && $current_user->ID)
58 foreach ($current_user->roles as $role_key) {
59 if (empty($GLOBALS['wp_roles']->roles[$role_key]))
60 continue;
61 $role = $GLOBALS['wp_roles']->roles[$role_key];
62 if (isset($adminSettingsPresent[$role['name']])) {
63 if ($adminSettingsPresent[$role['name']] == 'show')
64 $show = true;
65 if ($adminSettingsPresent[$role['name']] == 'hide' && $show === null)
66 $show = false;
67 }
68 }
69 return $show === null ? $content : $show;
70 }
71
72
73 if(!function_exists('wppb_curpageurl')){
74 function wppb_curpageurl() {
75 $pageURL = 'http';
76
77 if ((isset($_SERVER["HTTPS"])) && ($_SERVER["HTTPS"] == "on"))
78 $pageURL .= "s";
79
80 $pageURL .= "://";
81
82 if( strpos( $_SERVER["HTTP_HOST"], $_SERVER["SERVER_NAME"] ) !== false ){
83 $pageURL .=$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
84 }
85 else {
86 if ($_SERVER["SERVER_PORT"] != "80")
87 $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
88 else
89 $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
90 }
91
92 if ( function_exists('apply_filters') ) $pageURL = apply_filters('wppb_curpageurl', $pageURL);
93
94 return $pageURL;
95 }
96 }
97
98
99 if ( is_admin() ){
100
101 // register the settings for the menu only display sidebar menu for a user with a certain capability, in this case only the "admin"
102 add_action( 'admin_init', 'wppb_register_settings' );
103
104 // display the same extra profile fields in the admin panel also
105 if ( file_exists ( WPPB_PLUGIN_DIR.'/front-end/extra-fields/extra-fields.php' ) ){
106 require_once( WPPB_PLUGIN_DIR.'/front-end/extra-fields/extra-fields.php' );
107
108 add_action( 'show_user_profile', 'display_profile_extra_fields_in_admin', 10 );
109 add_action( 'edit_user_profile', 'display_profile_extra_fields_in_admin', 10 );
110 global $pagenow;
111 if( $pagenow != 'user-new.php' )
112 add_action( 'user_profile_update_errors', 'wppb_validate_backend_fields', 10, 3 );
113 add_action( 'personal_options_update', 'save_profile_extra_fields_in_admin', 10 );
114 add_action( 'edit_user_profile_update', 'save_profile_extra_fields_in_admin', 10 );
115 }
116
117 }else if ( !is_admin() ){
118 // include the stylesheet
119 add_action( 'wp_print_styles', 'wppb_add_plugin_stylesheet' );
120
121 // include the menu file for the profile informations
122 include_once( WPPB_PLUGIN_DIR.'/front-end/edit-profile.php' );
123 include_once( WPPB_PLUGIN_DIR.'/front-end/class-formbuilder.php' );
124 add_shortcode( 'wppb-edit-profile', 'wppb_front_end_profile_info' );
125
126 // include the menu file for the login screen
127 include_once( WPPB_PLUGIN_DIR.'/front-end/login.php' );
128 add_shortcode( 'wppb-login', 'wppb_front_end_login' );
129
130 // include the menu file for the logout screen
131 include_once( WPPB_PLUGIN_DIR.'/front-end/logout.php' );
132 add_shortcode( 'wppb-logout', 'wppb_front_end_logout' );
133
134 // include the menu file for the register screen
135 include_once( WPPB_PLUGIN_DIR.'/front-end/register.php' );
136 add_shortcode( 'wppb-register', 'wppb_front_end_register_handler' );
137
138 // include the menu file for the recover password screen
139 include_once( WPPB_PLUGIN_DIR.'/front-end/recover.php' );
140 add_shortcode( 'wppb-recover-password', 'wppb_front_end_password_recovery' );
141
142 // set the front-end admin bar to show/hide
143 add_filter( 'show_admin_bar' , 'wppb_show_admin_bar');
144
145 // Shortcodes used for the widget area
146 add_filter( 'widget_text', 'do_shortcode', 11 );
147 }
148
149
150 /**
151 * Function that overwrites the default wp_mail function and sends out emails
152 *
153 * @since v.2.0
154 *
155 * @param string $to
156 * @param string $subject
157 * @param string $message
158 * @param string $message_from
159 *
160 */
161 function wppb_mail( $to, $subject, $message, $message_from, $context = null ) {
162 $to = apply_filters( 'wppb_send_email_to', $to );
163 $send_email = apply_filters( 'wppb_send_email', true, $to, $subject, $message, $context );
164
165 $message = apply_filters( 'wppb_email_message', $message, $context );
166
167 do_action( 'wppb_before_sending_email', $to, $subject, $message, $send_email, $context );
168
169 if ( $send_email ) {
170 //we add this filter to enable html encoding
171 add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html"; ' ) );
172
173 $sent = wp_mail( $to , $subject, $message );
174
175 do_action( 'wppb_after_sending_email', $sent, $to, $subject, $message, $send_email, $context );
176
177 return $sent;
178 }
179
180 return '';
181 }
182
183 function wppb_activate_account_check(){
184 if ( ( isset( $_GET['activation_key'] ) ) && ( trim( $_GET['activation_key'] ) != '' ) ){
185 global $post;
186
187 $wppb_generalSettings = get_option( 'wppb_general_settings' );
188 $activation_landing_page_id = ( ( isset( $wppb_generalSettings['activationLandingPage'] ) && ( trim( $wppb_generalSettings['activationLandingPage'] ) != '' ) ) ? $wppb_generalSettings['activationLandingPage'] : 'not_set' );
189
190 if ( $activation_landing_page_id != 'not_set' ){
191 //an activation page was selected, but we still need to check if the current page doesn't already have the registration shortcode
192 if ( strpos( $post->post_content, '[wppb-register' ) === false )
193 add_filter( 'the_content', 'wppb_add_activation_message' );
194
195 }elseif ( strpos( $post->post_content, '[wppb-register' ) === false ){
196 //no activation page was selected, and the sent link pointed to the home url
197 wp_redirect( apply_filters( 'wppb_activatate_account_redirect_url', WPPB_PLUGIN_URL.'assets/misc/fallback-page.php?activation_key='.urlencode( $_GET['activation_key'] ).'&site_name='.urlencode( get_bloginfo( 'name' ) ).'&site_url='.urlencode( get_bloginfo( 'url' ) ).'&message='.urlencode( $activation_message = wppb_activate_signup( $_GET['activation_key'] ) ), $_GET['activation_key'], $activation_message ) );
198 exit;
199 }
200 }
201 }
202 add_action( 'template_redirect', 'wppb_activate_account_check' );
203
204
205 function wppb_add_activation_message( $content ){
206
207 return wppb_activate_signup( $_GET['activation_key'] ) . $content;
208 }
209
210
211 // Create a new, top-level page
212 $args = array(
213 'page_title' => 'Profile Builder',
214 'menu_title' => 'Profile Builder',
215 'capability' => 'manage_options',
216 'menu_slug' => 'profile-builder',
217 'page_type' => 'menu_page',
218 'position' => '70,69',
219 'priority' => 1,
220 'icon_url' => WPPB_PLUGIN_URL . 'assets/images/pb-menu-icon.png'
221 );
222 new WCK_Page_Creator_PB( $args );
223
224 /**
225 * Remove the automatically created submenu page
226 *
227 * @since v.2.0
228 *
229 * @return void
230 */
231 function wppb_remove_main_menu_page(){
232 remove_submenu_page( 'profile-builder', 'profile-builder' );
233 }
234 add_action( 'admin_menu', 'wppb_remove_main_menu_page', 11 );
235
236 /**
237 * Add scripts to the back-end CPT's to remove the slug from the edit page
238 *
239 * @since v.2.0
240 *
241 * @return void
242 */
243 function wppb_print_cpt_script( $hook ){
244 wp_enqueue_script( 'jquery-ui-dialog' );
245 wp_enqueue_style( 'wp-jquery-ui-dialog' );
246
247 if ( $hook == 'profile-builder_page_manage-fields' ){
248 wp_enqueue_script( 'wppb-manage-fields-live-change', WPPB_PLUGIN_URL . 'assets/js/jquery-manage-fields-live-change.js', array(), PROFILE_BUILDER_VERSION, true );
249 }
250
251 if (( $hook == 'profile-builder_page_manage-fields' ) ||
252 ( $hook == 'profile-builder_page_profile-builder-basic-info' ) ||
253 ( $hook == 'profile-builder_page_profile-builder-modules' ) ||
254 ( $hook == 'profile-builder_page_profile-builder-general-settings' ) ||
255 ( $hook == 'profile-builder_page_profile-builder-admin-bar-settings' ) ||
256 ( $hook == 'profile-builder_page_profile-builder-register' ) ||
257 ( $hook == 'profile-builder_page_profile-builder-wppb_userListing' ) ||
258 ( $hook == 'profile-builder_page_custom-redirects' ) ||
259 ( $hook == 'profile-builder_page_profile-builder-wppb_emailCustomizer' ) ||
260 ( $hook == 'profile-builder_page_profile-builder-wppb_emailCustomizerAdmin' ) ||
261 ( $hook == 'profile-builder_page_profile-builder-add-ons' ) ||
262 ( $hook == 'profile-builder_page_profile-builder-woocommerce-sync' ) ||
263 ( $hook == 'profile-builder_page_profile-builder-bbpress') ||
264 ( $hook == 'admin_page_profile-builder-pms-promo') ) {
265 wp_enqueue_style( 'wppb-back-end-style', WPPB_PLUGIN_URL . 'assets/css/style-back-end.css', false, PROFILE_BUILDER_VERSION );
266 }
267
268 if ( $hook == 'profile-builder_page_profile-builder-general-settings' )
269 wp_enqueue_script( 'wppb-manage-fields-live-change', WPPB_PLUGIN_URL . 'assets/js/jquery-email-confirmation.js', array(), PROFILE_BUILDER_VERSION, true );
270
271 if( ($hook == 'profile-builder_page_profile-builder-add-ons' ) ||
272 ($hook == 'admin_page_profile-builder-pms-promo' ) ) {
273 wp_enqueue_script('wppb-add-ons', WPPB_PLUGIN_URL . 'assets/js/jquery-pb-add-ons.js', array(), PROFILE_BUILDER_VERSION, true);
274 wp_enqueue_style( 'thickbox' );
275 wp_enqueue_script( 'thickbox' );
276 }
277
278 if ( isset( $_GET['post_type'] ) || isset( $_GET['post'] ) ){
279 if ( isset( $_GET['post_type'] ) )
280 $post_type = $_GET['post_type'];
281
282 elseif ( isset( $_GET['post'] ) )
283 $post_type = get_post_type( $_GET['post'] );
284
285 if ( ( 'wppb-epf-cpt' == $post_type ) || ( 'wppb-rf-cpt' == $post_type ) || ( 'wppb-ul-cpt' == $post_type ) ){
286 wp_enqueue_style( 'wppb-back-end-style', WPPB_PLUGIN_URL . 'assets/css/style-back-end.css', false, PROFILE_BUILDER_VERSION );
287 wp_enqueue_script( 'wppb-epf-rf', WPPB_PLUGIN_URL . 'assets/js/jquery-epf-rf.js', array(), PROFILE_BUILDER_VERSION, true );
288 }
289 }
290 if ( file_exists ( WPPB_PLUGIN_DIR.'/update/update-checker.php' ) ) {
291 wp_enqueue_script( 'wppb-sitewide', WPPB_PLUGIN_URL . 'assets/js/jquery-pb-sitewide.js', array(), PROFILE_BUILDER_VERSION, true );
292 }
293 wp_enqueue_style( 'wppb-serial-notice-css', WPPB_PLUGIN_URL . 'assets/css/serial-notice.css', false, PROFILE_BUILDER_VERSION );
294 }
295 add_action( 'admin_enqueue_scripts', 'wppb_print_cpt_script' );
296
297
298 // Set up the AJAX hooks
299 function wppb_delete(){
300 if ( isset( $_POST['_ajax_nonce'] ) ){
301
302 if ( ( isset( $_POST['what'] ) ) && ( $_POST['what'] == 'avatar' ) ){
303 if ( !wp_verify_nonce( $_POST['_ajax_nonce'], 'user'.base64_decode( $_POST['currentUser'] ).'_nonce_avatar' ) ){
304 echo __( 'The user-validation has failed - the avatar was not deleted!', 'profile-builder' );
305 die();
306
307 }else{
308 update_user_meta( base64_decode( $_POST['currentUser'] ), base64_decode( $_POST['customFieldName'] ), '');
309 update_user_meta( base64_decode( $_POST['currentUser'] ), 'resized_avatar_'.base64_decode( $_POST['customFieldID'] ), '' );
310 echo 'done';
311 die();
312 }
313 }elseif ( ( isset( $_POST['what'] ) ) && ( $_POST['what'] == 'attachment' ) ){
314 if ( !wp_verify_nonce( $_POST['_ajax_nonce'], 'user'.base64_decode( $_POST['currentUser'] ).'_nonce_upload' ) ){
315 echo __( 'The user-validation has failed - the attachment was not deleted!', 'profile-builder' );
316 die();
317
318 }else{
319 update_user_meta( base64_decode( $_POST['currentUser'] ), base64_decode( $_POST['customFieldName'] ), '');
320 echo 'done';
321 die();
322 }
323 }
324 }
325 }
326 add_action( 'wp_ajax_hook_wppb_delete', 'wppb_delete' );
327
328
329 //the function used to overwrite the avatar across the wp installation
330 function wppb_changeDefaultAvatar( $avatar, $id_or_email, $size, $default, $alt ){
331 /* Get user info. */
332 if(is_object($id_or_email)){
333 $my_user_id = $id_or_email->user_id;
334
335 }elseif(is_numeric($id_or_email)){
336 $my_user_id = $id_or_email;
337
338 }elseif(!is_integer($id_or_email)){
339 $user_info = get_user_by( 'email', $id_or_email );
340 $my_user_id = ( is_object( $user_info ) ? $user_info->ID : '' );
341 }else
342 $my_user_id = $id_or_email;
343
344 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
345 if ( $wppb_manage_fields != 'not_found' ){
346 foreach( $wppb_manage_fields as $value ){
347 if ( $value['field'] == 'Avatar'){
348 $avatar_field = $value;
349 }
350 }
351 }
352
353 /* for multisite if we don't have an avatar try to get it from the main blog */
354 if( is_multisite() && empty( $avatar_field ) ){
355 switch_to_blog(1);
356 $wppb_switched_blog = true;
357 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
358 if ( $wppb_manage_fields != 'not_found' ){
359 foreach( $wppb_manage_fields as $value ){
360 if ( $value['field'] == 'Avatar'){
361 $avatar_field = $value;
362 }
363 }
364 }
365 }
366
367 if ( !empty( $avatar_field ) ){
368
369 $customUserAvatar = get_user_meta( $my_user_id, $avatar_field['meta-name'], true );
370 if( !empty( $customUserAvatar ) ){
371 if( is_numeric( $customUserAvatar ) ){
372 $img_attr = wp_get_attachment_image_src( $customUserAvatar, 'wppb-avatar-size-'.$size );
373 if( $img_attr[3] === false ){
374 $img_attr = wp_get_attachment_image_src( $customUserAvatar, 'thumbnail' );
375 $avatar = "<img alt='{$alt}' src='{$img_attr[0]}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
376 }
377 else
378 $avatar = "<img alt='{$alt}' src='{$img_attr[0]}' class='avatar avatar-{$size} photo avatar-default' height='{$img_attr[2]}' width='{$img_attr[1]}' />";
379 }
380 else {
381 $customUserAvatar = get_user_meta($my_user_id, 'resized_avatar_' . $avatar_field['id'], true);
382 $customUserAvatarRelativePath = get_user_meta($my_user_id, 'resized_avatar_' . $avatar_field['id'] . '_relative_path', true);
383
384 if ((($customUserAvatar != '') || ($customUserAvatar != null)) && file_exists($customUserAvatarRelativePath)) {
385 $avatar = "<img alt='{$alt}' src='{$customUserAvatar}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
386 }
387 }
388 }
389
390 }
391
392 /* if we switched the blog restore it */
393 if( is_multisite() && !empty( $wppb_switched_blog ) && $wppb_switched_blog )
394 restore_current_blog();
395
396 return $avatar;
397 }
398 add_filter( 'get_avatar', 'wppb_changeDefaultAvatar', 21, 5 );
399
400
401 //the function used to resize the avatar image; the new function uses a user ID as parameter to make pages load faster
402 function wppb_resize_avatar( $userID, $userlisting_size = null, $userlisting_crop = null ){
403 // include the admin image API
404 require_once( ABSPATH . '/wp-admin/includes/image.php' );
405
406 // retrieve first a list of all the current custom fields
407 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
408 if ( $wppb_manage_fields != 'not_found' ){
409 foreach( $wppb_manage_fields as $value ){
410 if ( $value['field'] == 'Avatar'){
411 $avatar_field = $value;
412 }
413 }
414 }
415
416 /* for multisite if we don't have an avatar try to get it from the main blog */
417 if( is_multisite() && empty( $avatar_field ) ){
418 switch_to_blog(1);
419 $wppb_switched_blog = true;
420 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
421 if ( $wppb_manage_fields != 'not_found' ){
422 foreach( $wppb_manage_fields as $value ){
423 if ( $value['field'] == 'Avatar'){
424 $avatar_field = $value;
425 }
426 }
427 }
428 }
429
430
431 if ( !empty( $avatar_field ) ){
432
433 // retrieve width and height of the image
434 $width = $height = '';
435
436 //this checks if it only has 1 component
437 if ( is_numeric( $avatar_field['avatar-size'] ) ){
438 $width = $height = $avatar_field['avatar-size'];
439
440 }else{
441 //this checks if the entered value has 2 components
442 $sentValue = explode( ',', $avatar_field['avatar-size'] );
443 $width = $sentValue[0];
444 $height = $sentValue[1];
445 }
446
447 $width = ( !empty( $userlisting_size ) ? $userlisting_size : $width );
448 $height = ( !empty( $userlisting_size ) ? $userlisting_size : $height );
449
450 if( !strpos( get_user_meta( $userID, 'resized_avatar_'.$avatar_field['id'], true ), $width . 'x' . $height ) ) {
451 // retrieve the original image (in original size)
452 $avatar_directory_path = get_user_meta( $userID, 'avatar_directory_path_'.$avatar_field['id'], true );
453
454 $image = wp_get_image_editor( $avatar_directory_path );
455 if ( !is_wp_error( $image ) ) {
456 do_action( 'wppb_before_avatar_resizing', $image, $userID, $avatar_field['meta-name'], $avatar_field['avatar-size'] );
457
458 $crop = apply_filters( 'wppb_avatar_crop_resize', ( !empty( $userlisting_crop ) ? $userlisting_crop : false ) );
459
460 $resize = $image->resize( $width, $height, $crop );
461
462 if ($resize !== FALSE) {
463 do_action( 'wppb_avatar_resizing', $image, $resize );
464
465 $fileType = apply_filters( 'wppb_resized_file_extension', 'png' );
466
467 $wp_upload_array = wp_upload_dir(); // Array of key => value pairs
468
469 //create file(name); both with directory and url
470 $fileName_dir = $image->generate_filename( NULL, $wp_upload_array['basedir'].'/profile_builder/avatars/', $fileType );
471
472 if ( PHP_OS == "WIN32" || PHP_OS == "WINNT" )
473 $fileName_dir = str_replace( '\\', '/', $fileName_dir );
474
475 $fileName_url = str_replace( str_replace( '\\', '/', $wp_upload_array['basedir'] ), $wp_upload_array['baseurl'], $fileName_dir );
476
477 //save the newly created (resized) avatar on the disc
478 $saved_image = $image->save( $fileName_dir );
479
480 /* the image save sometimes doesn't save with the desired extension so we need to see with what extension it saved it with and
481 if it differs replace the extension in the path and url that we save as meta */
482 $validate_saved_image = wp_check_filetype_and_ext( $saved_image['path'], $saved_image['path'] );
483 $ext = substr( $fileName_dir,strrpos( $fileName_dir, '.', -1 ), strlen($fileName_dir) );
484 if( !empty( $validate_saved_image['ext'] ) && $validate_saved_image['ext'] != $ext ){
485 $fileName_url = str_replace( $ext, '.'.$validate_saved_image['ext'], $fileName_url );
486 $fileName_dir = str_replace( $ext, '.'.$validate_saved_image['ext'], $fileName_dir );
487 }
488
489 update_user_meta( $userID, 'resized_avatar_'.$avatar_field['id'], $fileName_url );
490 update_user_meta( $userID, 'resized_avatar_'.$avatar_field['id'].'_relative_path', $fileName_dir );
491
492 do_action( 'wppb_after_avatar_resizing', $image, $fileName_dir, $fileName_url );
493 }
494 }
495 }
496 }
497
498 /* if we switched the blog restore it */
499 if( is_multisite() && !empty( $wppb_switched_blog ) && $wppb_switched_blog )
500 restore_current_blog();
501
502 }
503
504
505 if ( is_admin() ){
506 // add a hook to delete the user from the _signups table if either the email confirmation is activated, or it is a wpmu installation
507 function wppb_delete_user_from_signups_table($user_id) {
508 global $wpdb;
509
510 $userLogin = $wpdb->get_var( $wpdb->prepare( "SELECT user_login, user_email FROM " . $wpdb->users . " WHERE ID = %d LIMIT 1", $user_id ) );
511 if ( is_multisite() )
512 $delete = $wpdb->delete( $wpdb->signups, array( 'user_login' => $userLogin ) );
513 else
514 $delete = $wpdb->delete( $wpdb->prefix.'signups', array( 'user_login' => $userLogin ) );
515 }
516
517 $wppb_generalSettings = get_option( 'wppb_general_settings' );
518 if ( !empty( $wppb_generalSettings['emailConfirmation'] ) && ( $wppb_generalSettings['emailConfirmation'] == 'yes' ) ) {
519 if( is_multisite() )
520 add_action( 'wpmu_delete_user', 'wppb_delete_user_from_signups_table' );
521 else
522 add_action('delete_user', 'wppb_delete_user_from_signups_table');
523 }
524 }
525
526
527
528 // This function offers compatibility with the all in one event calendar plugin
529 function wppb_aioec_compatibility(){
530
531 wp_deregister_script( 'jquery.tools-form');
532 }
533 add_action('admin_print_styles-users_page_ProfileBuilderOptionsAndSettings', 'wppb_aioec_compatibility');
534
535
536 function wppb_user_meta_exists( $id, $meta_name ){
537 global $wpdb;
538
539 return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $id, $meta_name ) );
540 }
541
542
543 // function to check if there is a need to add the http:// prefix
544 function wppb_check_missing_http( $redirectLink ) {
545 return preg_match( '#^(?:[a-z\d]+(?:-+[a-z\d]+)*\.)+[a-z]+(?::\d+)?(?:/|$)#i', $redirectLink );
546 }
547
548
549
550 //function to output the password strength checker on frontend forms
551 function wppb_password_strength_checker_html(){
552 $wppb_generalSettings = get_option( 'wppb_general_settings' );
553 if( !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
554 $password_strength = '<span id="pass-strength-result">'.__('Strength indicator', 'profile-builder' ).'</span>
555 <input type="hidden" value="" name="wppb_password_strength" id="wppb_password_strength"/>';
556 return $password_strength;
557 }
558 return '';
559 }
560
561 //function to check password length check
562 function wppb_check_password_length( $password ){
563 $wppb_generalSettings = get_option( 'wppb_general_settings' );
564 if( !empty( $wppb_generalSettings['minimum_password_length'] ) ){
565 if( strlen( $password ) < $wppb_generalSettings['minimum_password_length'] ){
566 return true;
567 }
568 else
569 return false;
570 }
571 return false;
572 }
573
574 //function to check password strength
575 function wppb_check_password_strength(){
576 $wppb_generalSettings = get_option( 'wppb_general_settings' );
577 if( isset( $_POST['wppb_password_strength'] ) && !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
578 $password_strength_array = array( 'short' => 0, 'bad' => 1, 'good' => 2, 'strong' => 3 );
579 $password_strength_text = array( 'short' => __( 'Very Weak', 'profile-builder' ), 'bad' => __( 'Weak', 'profile-builder' ), 'good' => __( 'Medium', 'profile-builder' ), 'strong' => __( 'Strong', 'profile-builder' ) );
580 if( $password_strength_array[$_POST['wppb_password_strength']] < $password_strength_array[$wppb_generalSettings['minimum_password_strength']] ){
581 return $password_strength_text[$wppb_generalSettings['minimum_password_strength']];
582 }
583 else
584 return false;
585 }
586 return false;
587 }
588
589 /* function to output password length requirements text */
590 function wppb_password_length_text(){
591 $wppb_generalSettings = get_option( 'wppb_general_settings' );
592 if( !empty( $wppb_generalSettings['minimum_password_length'] ) ){
593 return sprintf(__('Minimum length of %d characters', 'profile-builder'), $wppb_generalSettings['minimum_password_length']);
594 }
595 return '';
596 }
597
598 /**
599 * Include password strength check scripts on frontend where we have shortoces present
600 */
601 add_action( 'wp_footer', 'wppb_enqueue_password_strength_check' );
602 function wppb_enqueue_password_strength_check() {
603 global $wppb_shortcode_on_front;
604 if( $wppb_shortcode_on_front ){
605 $wppb_generalSettings = get_option( 'wppb_general_settings' );
606 if( !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
607 wp_enqueue_script( 'password-strength-meter' );
608 }
609 }
610 }
611 add_action( 'wp_footer', 'wppb_password_strength_check', 102 );
612 function wppb_password_strength_check(){
613 global $wppb_shortcode_on_front;
614 if( $wppb_shortcode_on_front ){
615 $wppb_generalSettings = get_option( 'wppb_general_settings' );
616 if( !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
617 ?>
618 <script type="text/javascript">
619 function check_pass_strength() {
620 var pass1 = jQuery('#passw1').val(), pass2 = jQuery('#passw2').val(), strength;
621
622 jQuery('#pass-strength-result').removeClass('short bad good strong');
623 if ( ! pass1 ) {
624 jQuery('#pass-strength-result').html( pwsL10n.empty );
625 return;
626 }
627
628 strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputBlacklist(), pass2 );
629
630 switch ( strength ) {
631 case 2:
632 jQuery('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
633 jQuery('#wppb_password_strength').val('bad');
634 break;
635 case 3:
636 jQuery('#pass-strength-result').addClass('good').html( pwsL10n.good );
637 jQuery('#wppb_password_strength').val('good');
638 break;
639 case 4:
640 jQuery('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
641 jQuery('#wppb_password_strength').val('strong');
642 break;
643 case 5:
644 jQuery('#pass-strength-result').addClass('short').html( pwsL10n.mismatch );
645 jQuery('#wppb_password_strength').val('short');
646 break;
647 default:
648 jQuery('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
649 jQuery('#wppb_password_strength').val('short');
650 }
651 }
652 jQuery( document ).ready( function() {
653 // Binding to trigger checkPasswordStrength
654 jQuery('#passw1').val('').keyup( check_pass_strength );
655 jQuery('#passw2').val('').keyup( check_pass_strength );
656 jQuery('#pass-strength-result').show();
657 });
658 </script>
659 <?php
660 }
661 }
662 }
663 /**
664 * Create functions for repeating error messages in front-end forms
665 */
666 function wppb_required_field_error($field_title='') {
667 $required_error = apply_filters('wppb_required_error' , __('This field is required','profile-builder') , $field_title);
668
669 return $required_error;
670
671 }
672
673 /**
674 * Function that returns a certain field (from manage_fields) by a given id or meta_name
675 */
676 function wppb_get_field_by_id_or_meta( $id_or_meta ){
677
678 $id = 0;
679 $meta = '';
680
681 if ( is_numeric($id_or_meta) )
682 $id = $id_or_meta;
683 else
684 $meta = $id_or_meta;
685
686 $fields = get_option('wppb_manage_fields', 'not_found');
687
688 if ($fields != 'not_found') {
689
690 foreach ($fields as $key => $field) {
691 if ( (!empty($id)) && ($field['id'] == $id) )
692 return $field;
693 if ( (!empty($meta)) && ($field['meta-name'] == $meta) )
694 return $field;
695 }
696
697 }
698
699 return '';
700 }
701
702
703 /* Function for displaying reCAPTCHA error on Login and Recover Password forms */
704 function wppb_recaptcha_field_error($field_title='') {
705 $recaptcha_error = apply_filters('wppb_recaptcha_error' , __('Please enter a (valid) reCAPTCHA value','profile-builder') , $field_title);
706
707 return $recaptcha_error;
708
709 }
710 /* Function for displaying phone field error */
711 function wppb_phone_field_error( $field_title = '' ) {
712 $phone_error = apply_filters( 'wppb_phone_error' , __( 'Incorrect phone number', 'profile-builder' ) , $field_title );
713
714 return $phone_error;
715 }
716
717 /* Create a wrapper function for get_query_var */
718 function wppb_get_query_var( $varname ){
719 return apply_filters( 'wppb_get_query_var_'.$varname, get_query_var( $varname ) );
720 }
721
722 /* Filter the "Save Changes" button text, to make it translatable */
723 function wppb_change_save_changes_button($value){
724 $value = __('Save Changes','profile-builder');
725 return $value;
726 }
727 add_filter( 'wck_save_changes_button', 'wppb_change_save_changes_button', 10, 2);
728
729 /* Filter the "Cancel" button text, to make it translatable */
730 function wppb_change_cancel_button($value){
731 $value = __('Cancel','profile-builder');
732 return $value;
733 }
734 add_filter( 'wck_cancel_button', 'wppb_change_cancel_button', 10, 2);
735
736 /* ilter the "Delete" button text, to make it translatable */
737 function wppb_change_delete_button($value){
738 $value = __('Delete','profile-builder');
739 return $value;
740 }
741 add_filter( 'wck_delete_button', 'wppb_change_delete_button', 10, 2);
742
743 /*Filter the "Edit" button text, to make it translatable*/
744 function wppb_change_edit_button($value){
745 $value = __('Edit','profile-builder');
746 return $value;
747 }
748 add_filter( 'wck_edit_button', 'wppb_change_edit_button', 10, 2);
749
750 /*Filter the User Listing, Register Forms and Edit Profile forms metabox header content, to make it translatable*/
751 function wppb_change_metabox_content_header(){
752 return '<thead><tr><th class="wck-number">#</th><th class="wck-content">'. __( 'Content', 'profile-builder' ) .'</th><th class="wck-edit">'. __( 'Edit', 'profile-builder' ) .'</th><th class="wck-delete">'. __( 'Delete', 'profile-builder' ) .'</th></tr></thead>';
753 }
754 add_filter('wck_metabox_content_header_wppb_ul_page_settings', 'wppb_change_metabox_content_header', 1);
755 add_filter('wck_metabox_content_header_wppb_rf_page_settings', 'wppb_change_metabox_content_header', 1);
756 add_filter('wck_metabox_content_header_wppb_epf_page_settings', 'wppb_change_metabox_content_header', 1);
757
758
759 /* Add a notice if people are not able to register via Profile Builder; Membership -> "Anyone can register" checkbox is not checked under WordPress admin UI -> Settings -> General tab */
760 if ( (get_option('users_can_register') == false) && (!class_exists('PMS_Add_General_Notices')) ) {
761 if( is_multisite() ) {
762 new WPPB_Add_General_Notices('wppb_anyone_can_register',
763 sprintf(__('To allow users to register for your website via Profile Builder, you first must enable user registration. Go to %1$sNetwork Settings%2$s, and under Registration Settings make sure to check “User accounts may be registered”. %3$sDismiss%4$s', 'profile-builder'), "<a href='" . network_admin_url('settings.php') . "'>", "</a>", "<a href='" . esc_url( add_query_arg('wppb_anyone_can_register_dismiss_notification', '0') ) . "'>", "</a>"),
764 'update-nag');
765 }else{
766 new WPPB_Add_General_Notices('wppb_anyone_can_register',
767 sprintf(__('To allow users to register for your website via Profile Builder, you first must enable user registration. Go to %1$sSettings -> General%2$s tab, and under Membership make sure to check “Anyone can register”. %3$sDismiss%4$s', 'profile-builder'), "<a href='" . admin_url('options-general.php') . "'>", "</a>", "<a href='" . esc_url( add_query_arg('wppb_anyone_can_register_dismiss_notification', '0') ) . "'>", "</a>"),
768 'update-nag');
769 }
770 }
771
772 /*Filter default WordPress notices ("Post published. Post updated."), add post type name for User Listing, Registration Forms and Edit Profile Forms*/
773 function wppb_change_default_post_updated_messages($messages){
774 global $post;
775 $post_type = get_post_type($post->ID);
776 $object = get_post_type_object($post_type);
777
778 if ( ($post_type == 'wppb-rf-cpt')||($post_type == 'wppb-epf-cpt')||($post_type == 'wppb-ul-cpt') ){
779 $messages['post'][1] = $object->labels->name . ' updated.';
780 $messages['post'][6] = $object->labels->name . ' published.';
781 }
782 return $messages;
783 }
784 add_filter('post_updated_messages','wppb_change_default_post_updated_messages', 2);
785
786
787 /* for meta-names with spaces in them PHP converts the space to underline in the $_POST */
788 function wppb_handle_meta_name( $meta_name ){
789 $meta_name = str_replace( ' ', '_', $meta_name );
790 $meta_name = str_replace( '.', '_', $meta_name );
791 return $meta_name;
792 }
793
794
795 // change User Registered date and time according to timezone selected in WordPress settings
796 function wppb_get_date_by_timezone() {
797 $wppb_wp_timezone = get_option( 'timezone_string' );
798
799 if( ! empty( $wppb_wp_timezone ) ) {
800 date_default_timezone_set( $wppb_wp_timezone );
801 $wppb_get_date = date( "Y-m-d G:i:s" );
802 } else {
803 $wppb_wp_gmt_offset = get_option( 'gmt_offset' );
804 $wppb_gmt_offset = $wppb_wp_gmt_offset * 60 * 60;
805 $wppb_get_date = gmdate( "Y-m-d G:i:s", time() + $wppb_gmt_offset );
806 }
807
808 return $wppb_get_date;
809 }
810
811 /**
812 * Add HTML tag 'required' to fields
813 *
814 * Add HTML tag 'required' for each field if the field is required. For browsers that don't support this HTML tag, we will still have the fallback.
815 * Field type 'Checkbox' is explicitly excluded because there is no HTML support to check if at least one option is selected.
816 * Other fields excluded are Avatar, Upload, Heading, ReCaptcha, WYSIWYG, Map.
817 *
818 * @since
819 *
820 * @param string $extra_attributes Extra attributes attached to the field HTML tag.
821 * @param array $field Field description.
822 * @return string $extra_attributes
823 */
824 function wppb_add_html_tag_required_to_fields( $extra_attributes, $field, $form_location ) {
825 if ( $field['field'] != "Checkbox" && isset( $field['required'] ) && $field['required'] == 'Yes' ){
826 if( !( ( $field['field'] == "Default - Password" || $field['field'] == "Default - Repeat Password" ) && $form_location == 'edit_profile' ) )
827 $extra_attributes .= ' required ';
828 }
829 return $extra_attributes;
830 }
831 add_filter( 'wppb_extra_attribute', 'wppb_add_html_tag_required_to_fields', 10, 3 );
832
833 /**
834 * Add HTML tag 'required' to WooCommerce fields
835 *
836 * Add HTML tag 'required' for each WooCommerce field if the field is required. For browsers that don't support this HTML tag, we will still have the fallback.
837 * Does not work on 'State / County' field, if it becomes required later depending on the Country Value
838 *
839 * @since
840 *
841 * @param string $extra_attributes Extra attributes attached to the field HTML tag.
842 * @param array $field Field description.
843 * @return string $extra_attributes
844 */
845 function wppb_add_html_tag_required_to_woo_fields( $extra_attributes, $field ) {
846 if ( isset( $field['required'] ) && $field['required'] == 'Yes' ){
847 $extra_attributes .= ' required ';
848 }
849 return $extra_attributes;
850 }
851 add_filter( 'wppb_woo_extra_attribute', 'wppb_add_html_tag_required_to_woo_fields', 10, 2 );
852
853
854 /**
855 * Add jQuery script to remove required attribute for hidden fields
856 *
857 * If a field is hidden dynamically via conditional fields or WooSync 'Ship to a different address' checkbox, then the required field needs to be removed.
858 * If a field is made visible again, add the required field back again.
859 *
860 * @since
861 *
862 * @param string $extra_attributes Extra attributes attached to the field HTML tag.
863 * @param array $field Field description.
864 * @return string $extra_attributes
865 */
866 function wppb_manage_required_attribute() {
867 global $wppb_shortcode_on_front;
868 if ($wppb_shortcode_on_front) {
869 ?>
870 <script type="text/javascript">
871 jQuery(document).on( "wppbAddRequiredAttributeEvent", wppbAddRequired );
872 function wppbAddRequired(event) {
873 var element = wppbEventTargetRequiredElement( event.target );
874 if( jQuery( element ).attr( "wppb_cf_temprequired" ) ){
875 jQuery( element ).removeAttr( "wppb_cf_temprequired" );
876 jQuery( element ).attr( "required", "required" );
877 }
878 }
879
880 jQuery(document).on( "wppbRemoveRequiredAttributeEvent", wppbRemoveRequired );
881 function wppbRemoveRequired(event) {
882 var element = wppbEventTargetRequiredElement( event.target );
883 if ( jQuery( element ).attr( "required" ) ) {
884 jQuery( element ).removeAttr( "required" );
885 jQuery( element ).attr( "wppb_cf_temprequired", "wppb_cf_temprequired" );
886 }
887 }
888
889 jQuery(document).on( "wppbToggleRequiredAttributeEvent", wppbToggleRequired );
890 function wppbToggleRequired(event) {
891 if ( jQuery( event.target ).attr( "required" ) ) {
892 jQuery( event.target ).removeAttr( "required" );
893 jQuery( event.target ).attr( "wppb_cf_temprequired", "wppb_cf_temprequired" );
894 }else if( jQuery( event.target ).attr( "wppb_cf_temprequired" ) ){
895 jQuery( event.target ).removeAttr( "wppb_cf_temprequired" );
896 jQuery( event.target ).attr( "required", "required" );
897 }
898 }
899
900 function wppbEventTargetRequiredElement( htmlElement ){
901 if ( htmlElement.nodeName == "OPTION" ){
902 // <option> is the target element, so we need to get the parent <select>, in order to apply the required attribute
903 return htmlElement.parentElement;
904 }else{
905 return htmlElement;
906 }
907 }
908
909 </script>
910 <?php
911 }
912 }
913 add_action( 'wp_footer', 'wppb_manage_required_attribute' );
914