PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.8.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.8.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 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 3.8.3, at features/functions.php

1,855 lines 72.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 /**
5 * Functions Load
6 *
7 */
8
9 // whitelist options, you can add more register_settings changing the second parameter
10 function wppb_register_settings() {
11 register_setting( 'wppb_option_group', 'wppb_default_settings' );
12 register_setting( 'wppb_general_settings', 'wppb_general_settings', 'wppb_general_settings_sanitize' );
13 register_setting( 'wppb_display_admin_settings', 'wppb_display_admin_settings' );
14 register_setting( 'wppb_profile_builder_pro_serial', 'wppb_profile_builder_pro_serial' );
15 register_setting( 'wppb_profile_builder_hobbyist_serial', 'wppb_profile_builder_hobbyist_serial' );
16 register_setting( 'wppb_module_settings', 'wppb_module_settings' );
17 register_setting( 'wppb_module_settings_description', 'wppb_module_settings_description' );
18 register_setting( 'customRedirectSettings', 'customRedirectSettings' );
19 register_setting( 'customUserListingSettings', 'customUserListingSettings' );
20 register_setting( 'reCaptchaSettings', 'reCaptchaSettings' );
21 register_setting( 'emailCustomizer', 'emailCustomizer' );
22 register_setting( 'wppb_content_restriction_settings', 'wppb_content_restriction_settings' );
23 register_setting( 'wppb_private_website_settings', 'wppb_private_website_settings' );
24 register_setting( 'wppb_two_factor_authentication_settings', 'wppb_two_factor_authentication_settings' );
25 }
26
27
28
29
30 // WPML support
31 function wppb_icl_t( $context, $name, $value, $kses = false ){
32
33 if( $kses === false ){
34 if( function_exists( 'icl_t' ) )
35 return icl_t( $context, $name, $value );
36 else
37 return $value;
38 } else {
39 if( function_exists( 'icl_t' ) )
40 return wp_kses_post( icl_t( $context, $name, $value ) );
41 else
42 return wp_kses_post( $value );
43 }
44
45 }
46
47 function wppb_icl_register_string( $context, $name, $value ) {
48 if( function_exists( 'icl_register_string' )) {
49 if ( $name === 'msf_previous_button_text_translation' || $name === 'msf_next_button_text_translation' )
50 $wpml_default_lang = 'en';
51 else
52 $wpml_default_lang = apply_filters('wpml_default_language', NULL );
53 $allow_empty_value = false;
54 icl_register_string($context, $name , $value , $allow_empty_value , $wpml_default_lang );
55 }
56 }
57
58
59 function wppb_add_plugin_stylesheet() {
60 $wppb_generalSettings = get_option( 'wppb_general_settings' );
61
62 if ( ( file_exists( WPPB_PLUGIN_DIR . '/assets/css/style-front-end.css' ) ) && ( isset( $wppb_generalSettings['extraFieldsLayout'] ) && ( $wppb_generalSettings['extraFieldsLayout'] == 'default' ) ) ){
63 wp_register_style( 'wppb_stylesheet', WPPB_PLUGIN_URL . 'assets/css/style-front-end.css', array(), PROFILE_BUILDER_VERSION );
64 wp_enqueue_style( 'wppb_stylesheet' );
65 }
66 if( is_rtl() ) {
67 if ( ( file_exists( WPPB_PLUGIN_DIR . '/assets/css/rtl.css' ) ) && ( isset( $wppb_generalSettings['extraFieldsLayout'] ) && ( $wppb_generalSettings['extraFieldsLayout'] == 'default' ) ) ){
68 wp_register_style( 'wppb_stylesheet_rtl', WPPB_PLUGIN_URL . 'assets/css/rtl.css', array(), PROFILE_BUILDER_VERSION );
69 wp_enqueue_style( 'wppb_stylesheet_rtl' );
70 }
71 }
72 }
73
74
75 function wppb_show_admin_bar($content){
76 global $current_user;
77
78 $adminSettingsPresent = get_option('wppb_display_admin_settings','not_found');
79 $show = null;
80
81 if ($adminSettingsPresent != 'not_found' && $current_user->ID)
82 foreach ($current_user->roles as $role_key) {
83 if (empty($GLOBALS['wp_roles']->roles[$role_key]))
84 continue;
85 $role = $GLOBALS['wp_roles']->roles[$role_key];
86 if (isset($adminSettingsPresent[$role['name']])) {
87 if ($adminSettingsPresent[$role['name']] == 'show')
88 $show = true;
89 if ($adminSettingsPresent[$role['name']] == 'hide' && $show === null)
90 $show = false;
91 }
92 }
93 return $show === null ? $content : $show;
94 }
95
96
97 if(!function_exists('wppb_curpageurl')){
98 function wppb_curpageurl(){
99 $req_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( $_SERVER['REQUEST_URI'] ) : '';
100
101 if( function_exists('wppb_get_abs_home') ) {
102 $home_path = trim(parse_url(wppb_get_abs_home(), PHP_URL_PATH), '/');
103 $home_path_regex = sprintf('|^%s|i', preg_quote($home_path, '|'));
104
105 // Trim path info from the end and the leading home path from the front.
106 $req_uri = ltrim($req_uri, '/');
107 $req_uri = preg_replace($home_path_regex, '', $req_uri);
108 $req_uri = trim(wppb_get_abs_home(), '/') . '/' . ltrim($req_uri, '/');
109 }
110
111 if ( function_exists('apply_filters') ) $req_uri = apply_filters('wppb_curpageurl', $req_uri);
112
113 return $req_uri;
114 }
115 }
116
117 /**
118 * Return absolute home url as stored in database, unfiltered.
119 *
120 * @return string
121 */
122 if(!function_exists('wppb_get_abs_home')) {
123 function wppb_get_abs_home(){
124 global $wpdb;
125 global $wppb_absolute_home;
126
127 if( isset($wppb_absolute_home) ) {
128 return $wppb_absolute_home;
129 }
130
131 // returns the unfiltered home_url by directly retrieving it from wp_options.
132 $wppb_absolute_home = (!is_multisite() && defined('WP_HOME')
133 ? WP_HOME
134 : (is_multisite() && !is_main_site()
135 ? (preg_match('/^(https)/', get_option('home')) === 1 ? 'https://'
136 : 'http://') . $wpdb->get_var(" SELECT CONCAT(b.domain, b.path)
137 FROM {$wpdb->blogs} b
138 WHERE blog_id = {$wpdb->blogid}
139 LIMIT 1")
140
141 : $wpdb->get_var(" SELECT option_value
142 FROM {$wpdb->options}
143 WHERE option_name = 'home'
144 LIMIT 1"))
145 );
146
147 if (empty($wppb_absolute_home)) {
148 $wppb_absolute_home = get_option("siteurl");
149 }
150
151 // always return absolute_home based on the http or https version of the current page request. This means no more redirects.
152 if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
153 $wppb_absolute_home = str_replace('http://', 'https://', $wppb_absolute_home);
154 } else {
155 $wppb_absolute_home = str_replace('https://', 'http://', $wppb_absolute_home);
156 }
157
158 return $wppb_absolute_home;
159 }
160 }
161
162
163 if ( is_admin() ){
164
165 // register the settings for the menu only display sidebar menu for a user with a certain capability, in this case only the "admin"
166 add_action( 'admin_init', 'wppb_register_settings' );
167
168 // display the same extra profile fields in the admin panel also
169 if ( file_exists ( WPPB_PLUGIN_DIR.'/front-end/default-fields/fields-functions.php' ) ){
170 require_once( WPPB_PLUGIN_DIR.'/front-end/default-fields/fields-functions.php' );
171
172 add_action( 'show_user_profile', 'wppb_display_fields_in_admin', 10 );
173 add_action( 'edit_user_profile', 'wppb_display_fields_in_admin', 10 );
174 global $pagenow;
175 if( $pagenow != 'user-new.php' )
176 add_action( 'user_profile_update_errors', 'wppb_validate_fields_in_admin', 10, 3 );
177 add_action( 'personal_options_update', 'wppb_save_fields_in_admin', 10 );
178 add_action( 'edit_user_profile_update', 'wppb_save_fields_in_admin', 10 );
179 }
180
181 // Since 3.8.1 fields are loaded in the back-end all the time: for conditional logic, simple uploads, display fields in admin functionalities
182 if (file_exists(WPPB_PLUGIN_DIR . '/front-end/default-fields/default-fields.php'))
183 require_once(WPPB_PLUGIN_DIR . '/front-end/default-fields/default-fields.php');
184
185 if ( defined( 'WPPB_PAID_PLUGIN_DIR' ) && file_exists( WPPB_PAID_PLUGIN_DIR . '/front-end/extra-fields/extra-fields.php'))
186 require_once(WPPB_PAID_PLUGIN_DIR . '/front-end/extra-fields/extra-fields.php');
187
188
189 }else if ( !is_admin() ){
190 // include the stylesheet
191 add_action( 'wp_print_styles', 'wppb_add_plugin_stylesheet' );
192
193 // include the menu file for the profile informations
194 include_once( WPPB_PLUGIN_DIR.'/front-end/edit-profile.php' );
195 include_once( WPPB_PLUGIN_DIR.'/front-end/class-formbuilder.php' );
196 add_shortcode( 'wppb-edit-profile', 'wppb_front_end_profile_info' );
197
198 // include the menu file for the login screen
199 include_once( WPPB_PLUGIN_DIR.'/front-end/login.php' );
200 add_shortcode( 'wppb-login', 'wppb_front_end_login' );
201
202 // include the menu file for the logout screen
203 include_once( WPPB_PLUGIN_DIR.'/front-end/logout.php' );
204 add_shortcode( 'wppb-logout', 'wppb_front_end_logout' );
205
206 // include the menu file for the register screen
207 include_once( WPPB_PLUGIN_DIR.'/front-end/register.php' );
208 add_shortcode( 'wppb-register', 'wppb_front_end_register_handler' );
209
210 // include the menu file for the recover password screen
211 include_once( WPPB_PLUGIN_DIR.'/front-end/recover.php' );
212 add_shortcode( 'wppb-recover-password', 'wppb_front_end_password_recovery' );
213
214 // set the front-end admin bar to show/hide
215 add_filter( 'show_admin_bar' , 'wppb_show_admin_bar');
216
217 // Shortcodes used for the widget area
218 add_filter( 'widget_text', 'do_shortcode', 11 );
219 }
220
221
222 /**
223 * Function that overwrites the default wp_mail function and sends out emails
224 *
225 * @since v.2.0
226 *
227 * @param string $to
228 * @param string $subject
229 * @param string $message
230 * @param string $message_from
231 *
232 */
233 function wppb_mail( $to, $subject, $message, $message_from = null, $context = null, $headers = '' ) {
234 $to = apply_filters( 'wppb_send_email_to', $to, $context );
235 $send_email = apply_filters( 'wppb_send_email', true, $to, $subject, $message, $context );
236
237 $message = apply_filters( 'wppb_email_message', $message, $context );
238
239 $message = wppb_maybe_add_propper_html_tags_to_email( $message );
240
241 do_action( 'wppb_before_sending_email', $to, $subject, $message, $send_email, $context );
242
243 if ( $send_email ) {
244 //we add this filter to enable html encoding
245 if( apply_filters( 'wppb_mail_enable_html', true, $context, $to, $subject, $message ) )
246 add_filter('wp_mail_content_type', 'wppb_html_content_type' );
247
248 $atts = apply_filters( 'wppb_mail', compact( 'to', 'subject', 'message', 'headers' ), $context );
249
250 $sent = wp_mail( $atts['to'] , html_entity_decode( htmlspecialchars_decode( $atts['subject'], ENT_QUOTES ), ENT_QUOTES ), $atts['message'], $atts['headers'] );
251
252 do_action( 'wppb_after_sending_email', $sent, $to, $subject, $message, $send_email, $context );
253
254 return $sent;
255 }
256
257 return '';
258 }
259
260 /* return text/html as email content type. used in wp_mail_content_type filter */
261 function wppb_html_content_type( $content_type ){
262 return 'text/html';
263 }
264
265 /*
266 * function that adds proper html tags to a html email message if they are missing
267 */
268 function wppb_maybe_add_propper_html_tags_to_email( $message ){
269
270 //check if we have html content
271 if( $message !== wp_strip_all_tags( $message ) ){
272 if( strpos( html_entity_decode( $message ), '<html' ) === false && strpos( html_entity_decode( $message ), '<body' ) === false ){
273 $message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>'. $message . '</body></html>';
274 }
275 }
276
277 return $message;
278 }
279
280 function wppb_activate_account_check(){
281 if ( ( isset( $_GET['activation_key'] ) ) && ( sanitize_text_field( $_GET['activation_key'] ) != '' ) ){
282 global $post;
283 $activation_key = sanitize_text_field( $_GET['activation_key'] );
284
285 $wppb_generalSettings = get_option( 'wppb_general_settings' );
286 $activation_landing_page_id = ( ( isset( $wppb_generalSettings['activationLandingPage'] ) && ( trim( $wppb_generalSettings['activationLandingPage'] ) != '' ) ) ? $wppb_generalSettings['activationLandingPage'] : 'not_set' );
287
288 if ( $activation_landing_page_id != 'not_set' ){
289 //an activation page was selected, but we still need to check if the current page doesn't already have the registration shortcode
290 if ( strpos( $post->post_content, '[wppb-register' ) === false )
291 add_filter( 'the_content', 'wppb_add_activation_message' );
292
293 }elseif ( strpos( $post->post_content, '[wppb-register' ) === false ){
294 //no activation page was selected, and the sent link pointed to the home url
295 nocache_headers();
296 wp_redirect( apply_filters( 'wppb_activatate_account_redirect_url', WPPB_PLUGIN_URL.'assets/misc/fallback-page.php?activation_key='.urlencode( $activation_key ).'&site_name='.urlencode( get_bloginfo( 'name' ) ).'&message='.urlencode( $activation_message = wppb_activate_signup( $activation_key ) ), $activation_key, $activation_message ) );
297 exit;
298 }
299 }
300 }
301 add_action( 'template_redirect', 'wppb_activate_account_check' );
302
303
304 function wppb_add_activation_message( $content ){
305 if( isset( $_GET['activation_key'] ) )
306 return wppb_activate_signup( sanitize_text_field( $_GET['activation_key'] ) ) . $content;
307 else
308 return $content;
309 }
310
311
312 // Create a new, top-level page
313 $args = array(
314 'page_title' => 'Profile Builder',
315 'menu_title' => 'Profile Builder',
316 'capability' => 'manage_options',
317 'menu_slug' => 'profile-builder',
318 'page_type' => 'menu_page',
319 'position' => '70.69',
320 'priority' => 1,
321 'icon_url' => WPPB_PLUGIN_URL . 'assets/images/pb-menu-icon.png'
322 );
323 new WCK_Page_Creator_PB( $args );
324
325 /**
326 * Remove the automatically created submenu page
327 *
328 * @since v.2.0
329 *
330 * @return void
331 */
332 function wppb_remove_main_menu_page(){
333 remove_submenu_page( 'profile-builder', 'profile-builder' );
334 }
335 add_action( 'admin_menu', 'wppb_remove_main_menu_page', 11 );
336
337 /**
338 * Add scripts to the back-end CPT's to remove the slug from the edit page
339 *
340 * @since v.2.0
341 *
342 * @return void
343 */
344 function wppb_print_cpt_script( $hook ){
345 wp_enqueue_script( 'jquery-ui-dialog' );
346 wp_enqueue_style( 'wp-jquery-ui-dialog' );
347
348 if ( $hook == 'profile-builder_page_manage-fields' ){
349 wp_enqueue_script( 'wppb-manage-fields-live-change', WPPB_PLUGIN_URL . 'assets/js/jquery-manage-fields-live-change.js', array(), PROFILE_BUILDER_VERSION, true );
350 wp_localize_script( 'wppb-manage-fields-live-change', 'wppb_fields_strings', array( 'gdpr_title' => __( 'GDPR Checkbox', 'profile-builder' ), 'gdpr_description' => __( 'I allow the website to collect and store the data I submit through this form.', 'profile-builder' ), 'honeypot_title' => __( 'Honeypot', 'profile-builder' ) ) );
351
352 wp_enqueue_script( 'wppb-select2', WPPB_PLUGIN_URL . 'assets/js/select2/select2.min.js', array(), PROFILE_BUILDER_VERSION, true );
353 wp_enqueue_style( 'wppb-select2-style', WPPB_PLUGIN_URL . 'assets/css/select2/select2.min.css', false, PROFILE_BUILDER_VERSION );
354 }
355
356 if ( $hook == 'admin_page_profile-builder-private-website' ){
357 wp_enqueue_script( 'wppb-select2', WPPB_PLUGIN_URL . 'assets/js/select2/select2.min.js', array(), PROFILE_BUILDER_VERSION, true );
358 wp_enqueue_script( 'wppb-select2-compat', WPPB_PLUGIN_URL . 'assets/js/select2-compat.js', array(), PROFILE_BUILDER_VERSION, true );
359 wp_enqueue_style( 'wppb-select2-style', WPPB_PLUGIN_URL . 'assets/css/select2/select2.min.css', false, PROFILE_BUILDER_VERSION );
360 }
361
362 if (( $hook == 'profile-builder_page_manage-fields' ) ||
363 ( $hook == 'profile-builder_page_profile-builder-basic-info' ) ||
364 ( $hook == 'profile-builder_page_profile-builder-add-ons' ) ||
365 ( $hook == 'profile-builder_page_profile-builder-general-settings' ) ||
366 ( $hook == 'profile-builder_page_profile-builder-admin-bar-settings' ) ||
367 ( $hook == 'profile-builder_page_profile-builder-register' ) ||
368 ( $hook == 'profile-builder_page_profile-builder-wppb_userListing' ) ||
369 ( $hook == 'profile-builder_page_custom-redirects' ) ||
370 ( $hook == 'profile-builder_page_profile-builder-wppb_emailCustomizer' ) ||//?what is this
371 ( $hook == 'profile-builder_page_profile-builder-wppb_emailCustomizerAdmin' ) ||//?what is this
372 ( $hook == 'profile-builder_page_profile-builder-add-ons' ) ||
373 ( $hook == 'profile-builder_page_profile-builder-woocommerce-sync' ) ||
374 ( $hook == 'profile-builder_page_profile-builder-bbpress') ||
375 ( $hook == 'profile-builder_page_admin-email-customizer') ||
376 ( $hook == 'profile-builder_page_user-email-customizer') ||
377 ( $hook == 'profile-builder_page_profile-builder-content_restriction' ) ||
378 ( strpos( $hook, 'profile-builder_page_' ) === 0 ) ||
379 ( $hook == 'edit.php' && ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'wppb-roles-editor' ) ) ||
380 ( $hook == 'admin_page_profile-builder-pms-promo') ||
381 ( $hook == 'toplevel_page_profile-builder-register') || //multisite register version page
382 ( $hook == 'admin_page_profile-builder-private-website') ) {
383 wp_enqueue_style( 'wppb-back-end-style', WPPB_PLUGIN_URL . 'assets/css/style-back-end.css', false, PROFILE_BUILDER_VERSION );
384 }
385
386 if ( $hook == 'profile-builder_page_profile-builder-general-settings' )
387 wp_enqueue_script( 'wppb-manage-fields-live-change', WPPB_PLUGIN_URL . 'assets/js/jquery-email-confirmation.js', array(), PROFILE_BUILDER_VERSION, true );
388
389 if( ($hook == 'profile-builder_page_profile-builder-add-ons' ) ||
390 ($hook == 'admin_page_profile-builder-pms-promo' ) ) {
391 wp_enqueue_script('wppb-add-ons', WPPB_PLUGIN_URL . 'assets/js/jquery-pb-add-ons.js', array(), PROFILE_BUILDER_VERSION, true);
392 wp_enqueue_style( 'thickbox' );
393 wp_enqueue_script( 'thickbox' );
394 }
395
396 if ( isset( $_GET['post_type'] ) || isset( $_GET['post'] ) ){
397 if ( isset( $_GET['post_type'] ) )
398 $post_type = sanitize_text_field( $_GET['post_type'] );
399
400 elseif ( isset( $_GET['post'] ) )
401 $post_type = get_post_type( absint( $_GET['post'] ) );
402
403 if ( ( 'wppb-epf-cpt' == $post_type ) || ( 'wppb-rf-cpt' == $post_type ) || ( 'wppb-ul-cpt' == $post_type ) ){
404 wp_enqueue_style( 'wppb-back-end-style', WPPB_PLUGIN_URL . 'assets/css/style-back-end.css', false, PROFILE_BUILDER_VERSION );
405 wp_enqueue_script( 'wppb-epf-rf', WPPB_PLUGIN_URL . 'assets/js/jquery-epf-rf.js', array(), PROFILE_BUILDER_VERSION, true );
406 }
407 else if( 'wppb-roles-editor' == $post_type ){
408 wp_enqueue_style( 'wppb-back-end-style', WPPB_PLUGIN_URL . 'assets/css/style-back-end.css', array(), PROFILE_BUILDER_VERSION );
409 }
410 }
411
412 wp_enqueue_script( 'wppb-sitewide', WPPB_PLUGIN_URL . 'assets/js/jquery-pb-sitewide.js', array(), PROFILE_BUILDER_VERSION, true );
413
414 wp_enqueue_style( 'wppb-serial-notice-css', WPPB_PLUGIN_URL . 'assets/css/serial-notice.css', false, PROFILE_BUILDER_VERSION );
415 }
416 add_action( 'admin_enqueue_scripts', 'wppb_print_cpt_script', 9 );
417
418 /**
419 * Highlight the settings page under Profile Builder in the admin menu for these pages
420 */
421 //add add_action( "admin_footer-$hook", "wppb_make_setting_menu_item_highlighted" ); for other pages that don't have a parent
422 add_action( "admin_footer-admin_page_profile-builder-private-website", "wppb_make_setting_menu_item_highlighted" );
423 add_action( "admin_footer-profile-builder_page_profile-builder-admin-bar-settings", "wppb_make_setting_menu_item_highlighted" );
424 add_action( "admin_footer-profile-builder_page_profile-builder-content_restriction", "wppb_make_setting_menu_item_highlighted" );
425 add_action( "admin_footer-profile-builder_page_profile-builder-content_restriction", "wppb_make_setting_menu_item_highlighted" );
426 add_action( "admin_footer-profile-builder_page_admin-email-customizer", "wppb_make_setting_menu_item_highlighted" );
427 add_action( "admin_footer-profile-builder_page_user-email-customizer", "wppb_make_setting_menu_item_highlighted" );
428 add_action( "admin_footer-profile-builder_page_profile-builder-toolbox-settings", "wppb_make_setting_menu_item_highlighted" );
429 add_action( "admin_footer-profile-builder_page_profile-builder-two-factor-authentication", "wppb_make_setting_menu_item_highlighted" );
430 function wppb_make_setting_menu_item_highlighted(){
431 echo'<script type="text/javascript">
432 jQuery(document).ready( function($) {
433 $("#toplevel_page_profile-builder").addClass("current wp-has-current-submenu wp-menu-open");
434 $("a[href=\'admin.php?page=profile-builder-general-settings\']").closest("li").addClass("current");
435 });
436 </script>';
437 }
438
439
440 //the function used to overwrite the avatar across the wp installation
441 function wppb_changeDefaultAvatar( $avatar, $id_or_email, $size, $default, $alt ){
442 /* Get user info. */
443 if(is_object($id_or_email)){
444 $my_user_id = $id_or_email->user_id;
445
446 if ($id_or_email instanceof WP_User){
447 $my_user_id = $id_or_email->ID;
448 }
449
450 }elseif(is_numeric($id_or_email)){
451 $my_user_id = $id_or_email;
452
453 }elseif(!is_integer($id_or_email)){
454 $user_info = get_user_by( 'email', $id_or_email );
455 $my_user_id = ( is_object( $user_info ) ? $user_info->ID : '' );
456 }else
457 $my_user_id = $id_or_email;
458
459 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
460 if ( $wppb_manage_fields != 'not_found' ){
461 foreach( $wppb_manage_fields as $value ){
462 if ( $value['field'] == 'Avatar'){
463 $avatar_field = $value;
464 }
465 }
466 }
467
468 /* for multisite if we don't have an avatar try to get it from the main blog */
469 if( is_multisite() && empty( $avatar_field ) ){
470 switch_to_blog(1);
471 $wppb_switched_blog = true;
472 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
473 if ( $wppb_manage_fields != 'not_found' ){
474 foreach( $wppb_manage_fields as $value ){
475 if ( $value['field'] == 'Avatar'){
476 $avatar_field = $value;
477 }
478 }
479 }
480 }
481
482 if ( !empty( $avatar_field ) ){
483
484 $customUserAvatar = get_user_meta( $my_user_id, Wordpress_Creation_Kit_PB::wck_generate_slug( $avatar_field['meta-name'] ), true );
485 if( !empty( $customUserAvatar ) ){
486 if( is_numeric( $customUserAvatar ) ){
487 $img_attr = wp_get_attachment_image_src( $customUserAvatar, 'wppb-avatar-size-'.$size );
488 if( $img_attr[3] === false ){
489 $img_attr = wp_get_attachment_image_src( $customUserAvatar, 'thumbnail' );
490 $avatar = "<img alt='{$alt}' src='{$img_attr[0]}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
491 }
492 else
493 $avatar = "<img alt='{$alt}' src='{$img_attr[0]}' class='avatar avatar-{$size} photo avatar-default' height='{$img_attr[2]}' width='{$img_attr[1]}' />";
494 }
495 else {
496 $customUserAvatar = get_user_meta($my_user_id, 'resized_avatar_' . $avatar_field['id'], true);
497 $customUserAvatarRelativePath = get_user_meta($my_user_id, 'resized_avatar_' . $avatar_field['id'] . '_relative_path', true);
498
499 if ((($customUserAvatar != '') || ($customUserAvatar != null)) && file_exists($customUserAvatarRelativePath)) {
500 $avatar = "<img alt='{$alt}' src='{$customUserAvatar}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
501 }
502 }
503 }
504
505 }
506
507 /* if we switched the blog restore it */
508 if( is_multisite() && !empty( $wppb_switched_blog ) && $wppb_switched_blog )
509 restore_current_blog();
510
511 return $avatar;
512 }
513 add_filter( 'get_avatar', 'wppb_changeDefaultAvatar', 21, 5 );
514
515
516 //the function used to overwrite the avatar across the wp installation
517 function wppb_changeDefaultAvatarUrl( $url, $id_or_email, $args ){
518 /* Get user info. */
519 if(is_object($id_or_email)){
520 $my_user_id = $id_or_email->user_id;
521
522 if ($id_or_email instanceof WP_User){
523 $my_user_id = $id_or_email->ID;
524 }
525
526 }elseif(is_numeric($id_or_email)){
527 $my_user_id = $id_or_email;
528
529 }elseif(!is_integer($id_or_email)){
530 $user_info = get_user_by( 'email', $id_or_email );
531 $my_user_id = ( is_object( $user_info ) ? $user_info->ID : '' );
532 }else
533 $my_user_id = $id_or_email;
534
535 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
536 if ( $wppb_manage_fields != 'not_found' ){
537 foreach( $wppb_manage_fields as $value ){
538 if ( $value['field'] == 'Avatar'){
539 $avatar_field = $value;
540 }
541 }
542 }
543
544 /* for multisite if we don't have an avatar try to get it from the main blog */
545 if( is_multisite() && empty( $avatar_field ) ){
546 switch_to_blog(1);
547 $wppb_switched_blog = true;
548 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
549 if ( $wppb_manage_fields != 'not_found' ){
550 foreach( $wppb_manage_fields as $value ){
551 if ( $value['field'] == 'Avatar'){
552 $avatar_field = $value;
553 }
554 }
555 }
556 }
557
558 $avatar_url = $url;
559
560 if ( !empty( $avatar_field ) ){
561
562 $customUserAvatar = get_user_meta( $my_user_id, Wordpress_Creation_Kit_PB::wck_generate_slug( $avatar_field['meta-name'] ), true );
563 if( !empty( $customUserAvatar ) ){
564 if( is_numeric( $customUserAvatar ) ){
565 if( $img_attr = wp_get_attachment_image_src( $customUserAvatar, 'wppb-avatar-size-'.$args['size'] ) ) {
566 if ( $img_attr[3] === false ) {
567 $img_attr = wp_get_attachment_image_src( $customUserAvatar, 'thumbnail' );
568 }
569 $avatar_url = $img_attr[0];
570 }
571 }
572 else {
573 $customUserAvatar = get_user_meta($my_user_id, 'resized_avatar_' . $avatar_field['id'], true);
574 $customUserAvatarRelativePath = get_user_meta($my_user_id, 'resized_avatar_' . $avatar_field['id'] . '_relative_path', true);
575
576 if ((($customUserAvatar != '') || ($customUserAvatar != null)) && file_exists($customUserAvatarRelativePath)) {
577 $avatar_url = $customUserAvatar;
578 }
579 }
580 }
581
582 }
583
584 /* if we switched the blog restore it */
585 if( is_multisite() && !empty( $wppb_switched_blog ) && $wppb_switched_blog )
586 restore_current_blog();
587
588 return $avatar_url;
589 }
590 add_filter( 'get_avatar_url', 'wppb_changeDefaultAvatarUrl', 21, 5 );
591
592
593 //the function used to resize the avatar image; the new function uses a user ID as parameter to make pages load faster
594 function wppb_resize_avatar( $userID, $userlisting_size = null, $userlisting_crop = null ){
595 // include the admin image API
596 require_once( ABSPATH . '/wp-admin/includes/image.php' );
597
598 // retrieve first a list of all the current custom fields
599 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
600 if ( $wppb_manage_fields != 'not_found' ){
601 foreach( $wppb_manage_fields as $value ){
602 if ( $value['field'] == 'Avatar'){
603 $avatar_field = $value;
604 }
605 }
606 }
607
608 /* for multisite if we don't have an avatar try to get it from the main blog */
609 if( is_multisite() && empty( $avatar_field ) ){
610 switch_to_blog(1);
611 $wppb_switched_blog = true;
612 $wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_found' );
613 if ( $wppb_manage_fields != 'not_found' ){
614 foreach( $wppb_manage_fields as $value ){
615 if ( $value['field'] == 'Avatar'){
616 $avatar_field = $value;
617 }
618 }
619 }
620 }
621
622
623 if ( !empty( $avatar_field ) ){
624
625 // retrieve width and height of the image
626 $width = $height = '';
627
628 //this checks if it only has 1 component
629 if ( is_numeric( $avatar_field['avatar-size'] ) ){
630 $width = $height = $avatar_field['avatar-size'];
631
632 }else{
633 //this checks if the entered value has 2 components
634 $sentValue = explode( ',', $avatar_field['avatar-size'] );
635 $width = $sentValue[0];
636 $height = $sentValue[1];
637 }
638
639 $width = ( !empty( $userlisting_size ) ? $userlisting_size : $width );
640 $height = ( !empty( $userlisting_size ) ? $userlisting_size : $height );
641
642 if( !strpos( get_user_meta( $userID, 'resized_avatar_'.$avatar_field['id'], true ), $width . 'x' . $height ) ) {
643 // retrieve the original image (in original size)
644 $avatar_directory_path = get_user_meta( $userID, 'avatar_directory_path_'.$avatar_field['id'], true );
645
646 $image = wp_get_image_editor( $avatar_directory_path );
647 if ( !is_wp_error( $image ) ) {
648 do_action( 'wppb_before_avatar_resizing', $image, $userID, Wordpress_Creation_Kit_PB::wck_generate_slug( $avatar_field['meta-name'] ), $avatar_field['avatar-size'] );
649
650 $crop = apply_filters( 'wppb_avatar_crop_resize', ( !empty( $userlisting_crop ) ? $userlisting_crop : false ) );
651
652 $resize = $image->resize( $width, $height, $crop );
653
654 if ($resize !== FALSE) {
655 do_action( 'wppb_avatar_resizing', $image, $resize );
656
657 $fileType = apply_filters( 'wppb_resized_file_extension', 'png' );
658
659 $wp_upload_array = wp_upload_dir(); // Array of key => value pairs
660
661 //create file(name); both with directory and url
662 $fileName_dir = $image->generate_filename( NULL, $wp_upload_array['basedir'].'/profile_builder/avatars/', $fileType );
663
664 if ( PHP_OS == "WIN32" || PHP_OS == "WINNT" )
665 $fileName_dir = str_replace( '\\', '/', $fileName_dir );
666
667 $fileName_url = str_replace( str_replace( '\\', '/', $wp_upload_array['basedir'] ), $wp_upload_array['baseurl'], $fileName_dir );
668
669 //save the newly created (resized) avatar on the disc
670 $saved_image = $image->save( $fileName_dir );
671
672 if ( !is_wp_error( $saved_image ) ) {
673 /* the image save sometimes doesn't save with the desired extension so we need to see with what extension it saved it with and
674 if it differs replace the extension in the path and url that we save as meta */
675 $validate_saved_image = wp_check_filetype_and_ext( $saved_image['path'], $saved_image['path'] );
676 $ext = substr( $fileName_dir,strrpos( $fileName_dir, '.', -1 ), strlen($fileName_dir) );
677 if( !empty( $validate_saved_image['ext'] ) && $validate_saved_image['ext'] != $ext ){
678 $fileName_url = str_replace( $ext, '.'.$validate_saved_image['ext'], $fileName_url );
679 $fileName_dir = str_replace( $ext, '.'.$validate_saved_image['ext'], $fileName_dir );
680 }
681
682 update_user_meta( $userID, 'resized_avatar_'.$avatar_field['id'], $fileName_url );
683 update_user_meta( $userID, 'resized_avatar_'.$avatar_field['id'].'_relative_path', $fileName_dir );
684
685 do_action( 'wppb_after_avatar_resizing', $image, $fileName_dir, $fileName_url );
686 }
687 }
688 }
689 }
690 }
691
692 /* if we switched the blog restore it */
693 if( is_multisite() && !empty( $wppb_switched_blog ) && $wppb_switched_blog )
694 restore_current_blog();
695
696 }
697
698
699 if ( is_admin() ){
700 // add a hook to delete the user from the _signups table if either the email confirmation is activated, or it is a wpmu installation
701 function wppb_delete_user_from_signups_table($user_id) {
702 global $wpdb;
703
704 $userLogin = $wpdb->get_var( $wpdb->prepare( "SELECT user_login, user_email FROM " . $wpdb->users . " WHERE ID = %d LIMIT 1", $user_id ) );
705 if ( is_multisite() )
706 $delete = $wpdb->delete( $wpdb->signups, array( 'user_login' => $userLogin ) );
707 else {
708 $table_name = $wpdb->prefix . 'signups';
709 $val = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) );
710 if ( $val ){
711 $delete = $wpdb->delete($wpdb->prefix . 'signups', array('user_login' => $userLogin));
712 }
713 }
714 }
715
716 $wppb_generalSettings = get_option( 'wppb_general_settings' );
717 if ( !empty( $wppb_generalSettings['emailConfirmation'] ) && ( $wppb_generalSettings['emailConfirmation'] == 'yes' ) ) {
718 if( is_multisite() )
719 add_action( 'wpmu_delete_user', 'wppb_delete_user_from_signups_table' );
720 else
721 add_action('delete_user', 'wppb_delete_user_from_signups_table');
722 }
723 }
724
725
726
727 // This function offers compatibility with the all in one event calendar plugin
728 function wppb_aioec_compatibility(){
729
730 wp_deregister_script( 'jquery.tools-form');
731 }
732 add_action('admin_print_styles-users_page_ProfileBuilderOptionsAndSettings', 'wppb_aioec_compatibility');
733
734
735 function wppb_user_meta_exists( $id, $meta_name ){
736 global $wpdb;
737
738 return apply_filters( 'wppb_user_meta_exists_meta_name', $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $id, $meta_name ) ), $id, $meta_name );
739 }
740
741
742 // function to check if there is a need to add the http:// prefix
743 function wppb_check_missing_http( $redirectLink ) {
744 return preg_match( '#^(?:[a-z\d]+(?:-+[a-z\d]+)*\.)+[a-z]+(?::\d+)?(?:/|$)#i', $redirectLink );
745 }
746
747 //function that adds missing http to a link
748 function wppb_add_missing_http( $link ){
749 $http = '';
750 if ( wppb_check_missing_http( $link ) ) { //if missing http(s)
751 $http = 'http';
752 if ((isset($_SERVER["HTTPS"])) && ($_SERVER["HTTPS"] == "on"))
753 $http .= "s";
754 $http .= "://";
755 }
756
757 return $http . $link;
758 }
759
760
761 //function to output the password strength checker on frontend forms
762 function wppb_password_strength_checker_html(){
763 $wppb_generalSettings = get_option( 'wppb_general_settings' );
764 if( !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
765 $password_strength = '<span id="pass-strength-result">'.__('Strength indicator', 'profile-builder' ).'</span>
766 <input type="hidden" value="" name="wppb_password_strength" id="wppb_password_strength"/>';
767 return $password_strength;
768 }
769 return '';
770 }
771
772 //function to check password length check
773 function wppb_check_password_length( $password ){
774 $wppb_generalSettings = get_option( 'wppb_general_settings' );
775 if( !empty( $wppb_generalSettings['minimum_password_length'] ) ){
776 if( strlen( $password ) < $wppb_generalSettings['minimum_password_length'] ){
777 return true;
778 }
779 else
780 return false;
781 }
782 return false;
783 }
784
785 //function to check password strength
786 function wppb_check_password_strength(){
787 $wppb_generalSettings = get_option( 'wppb_general_settings' );
788 if( isset( $_POST['wppb_password_strength'] ) && !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
789 $wppb_password_strength = sanitize_text_field( $_POST['wppb_password_strength'] );
790 $password_strength_array = array( 'short' => 0, 'bad' => 1, 'good' => 2, 'strong' => 3 );
791 $password_strength_text = array( 'short' => __( 'Very Weak', 'profile-builder' ), 'bad' => __( 'Weak', 'profile-builder' ), 'good' => __( 'Medium', 'profile-builder' ), 'strong' => __( 'Strong', 'profile-builder' ) );
792 if( $password_strength_array[$wppb_password_strength] < $password_strength_array[$wppb_generalSettings['minimum_password_strength']] ){
793 return $password_strength_text[$wppb_generalSettings['minimum_password_strength']];
794 }
795 else
796 return false;
797 }
798 return false;
799 }
800
801 /* function to output password length requirements text */
802 function wppb_password_length_text(){
803 $wppb_generalSettings = get_option( 'wppb_general_settings' );
804 if( !empty( $wppb_generalSettings['minimum_password_length'] ) ){
805 return sprintf(__('Minimum length of %d characters.', 'profile-builder'), $wppb_generalSettings['minimum_password_length']);
806 }
807 return '';
808 }
809
810 /* function to output password strength requirements text */
811 function wppb_password_strength_description() {
812 $wppb_generalSettings = get_option( 'wppb_general_settings' );
813
814 if( ! empty( $wppb_generalSettings['minimum_password_strength'] ) ) {
815 $password_strength_text = array( 'short' => __( 'Very Weak', 'profile-builder' ), 'bad' => __( 'Weak', 'profile-builder' ), 'good' => __( 'Medium', 'profile-builder' ), 'strong' => __( 'Strong', 'profile-builder' ) );
816 $password_strength_description = '<br>'. sprintf( __( 'The password must have a minimum strength of %s', 'profile-builder' ), $password_strength_text[$wppb_generalSettings['minimum_password_strength']] );
817
818 return $password_strength_description;
819 } else {
820 return '';
821 }
822 }
823
824 /**
825 * Include password strength check scripts on frontend where we have shortcodes present
826 */
827 add_action( 'wp_footer', 'wppb_enqueue_password_strength_check' );
828 function wppb_enqueue_password_strength_check() {
829 global $wppb_shortcode_on_front;
830 if( $wppb_shortcode_on_front ){
831 $wppb_generalSettings = get_option( 'wppb_general_settings' );
832 if( !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
833 wp_enqueue_script( 'password-strength-meter' );
834 }
835 }
836 }
837 add_action( 'wp_footer', 'wppb_password_strength_check', 102 );
838 function wppb_password_strength_check(){
839 global $wppb_shortcode_on_front;
840 if( $wppb_shortcode_on_front ){
841 $wppb_generalSettings = get_option( 'wppb_general_settings' );
842 if( !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
843 ?>
844 <script type="text/javascript">
845 function check_pass_strength() {
846 var pass1 = jQuery('#passw1').val(), pass2 = jQuery('#passw2').val(), strength;
847
848 jQuery('#pass-strength-result').removeClass('short bad good strong');
849 if ( ! pass1 ) {
850 jQuery('#pass-strength-result').html( pwsL10n.empty );
851 return;
852 }
853 <?php
854 global $wp_version;
855 if ( version_compare( $wp_version, "4.8", ">" ) ) {
856 ?>
857 strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputDisallowedList(), pass2 );
858 <?php
859 }
860 else {
861 ?>
862 strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputBlacklist(), pass2);
863 <?php
864 }
865 ?>
866 switch ( strength ) {
867 case 2:
868 jQuery('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
869 jQuery('#wppb_password_strength').val('bad');
870 break;
871 case 3:
872 jQuery('#pass-strength-result').addClass('good').html( pwsL10n.good );
873 jQuery('#wppb_password_strength').val('good');
874 break;
875 case 4:
876 jQuery('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
877 jQuery('#wppb_password_strength').val('strong');
878 break;
879 case 5:
880 jQuery('#pass-strength-result').addClass('short').html( pwsL10n.mismatch );
881 jQuery('#wppb_password_strength').val('short');
882 break;
883 default:
884 jQuery('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
885 jQuery('#wppb_password_strength').val('short');
886 }
887 }
888 jQuery( document ).ready( function() {
889 // Binding to trigger checkPasswordStrength
890 jQuery('#passw1').val('').on( 'keyup', check_pass_strength );
891 jQuery('#passw2').val('').on( 'keyup', check_pass_strength );
892 jQuery('#pass-strength-result').show();
893 });
894 </script>
895 <?php
896 }
897 }
898 }
899
900 /**
901 * Include toggle password visibility script on frontend where we have shortcodes present
902 */
903 function wppb_password_visibility_toggle_html(){
904 if( apply_filters( 'wppb_show_password_visibility_toggle', false ) ){
905 return '
906 <button type="button" class="wppb-toggle-pw wppb-show-pw hide-if-no-js" data-toggle="0" aria-label="Show password" tabindex="-1">
907 <img src="'.WPPB_PLUGIN_URL.'/assets/images/eye-outline.svg" width="20px" height="20px" />
908 </button>';
909 }
910 return '';
911 }
912
913 /**
914 * Include toggle password visibility script on frontend where we have shortcodes present
915 */
916 add_action( 'wp_footer', 'wppb_enqueue_password_visibility_toggle' );
917 function wppb_enqueue_password_visibility_toggle() {
918 global $wppb_shortcode_on_front;
919 if( $wppb_shortcode_on_front && apply_filters( 'wppb_show_password_visibility_toggle', false ) ){
920
921 //load jQuery if needed
922 if( !wp_script_is('jquery', 'done') ){
923 wp_print_scripts('jquery');
924 }
925
926 ?>
927 <script type="text/javascript">
928 jQuery( document ).ready( function() {
929
930 jQuery( "button.wppb-toggle-pw" ).on( "click", wppb_password_visibility_toggle );
931
932 jQuery( 'button.wppb-toggle-pw' ).each( function( index, toggle ){
933
934 var parent = jQuery( toggle ).parent()
935
936 if( parent.hasClass( 'wppb-form-field' ) && jQuery( '.wppb-description-delimiter', parent ) ){
937
938 jQuery( toggle ).css( 'top', parseInt( jQuery( toggle ).css('top') ) - ( jQuery( '.wppb-description-delimiter', parent ).outerHeight() / 2 ) )
939
940 }
941
942 })
943
944 });
945 function wppb_password_visibility_toggle() {
946 var target_form_id = "#" + jQuery(this).closest('form').attr("id") + " ";
947
948 var password_inputs = [ ".login-password input#user_pass", "input#passw1", "input#passw2" ]
949
950 for ( var password_input of password_inputs ){
951 var input = jQuery( target_form_id + password_input );
952 var button = jQuery( target_form_id + "button.wppb-toggle-pw" );
953 var icon = jQuery( target_form_id + "button.wppb-toggle-pw img" );
954
955 if ( input.length ) {
956 if ("password" === input.attr("type")) {
957 input.attr("type", "text");
958 button.toggleClass("wppb-show-pw").toggleClass("wppb-hide-pw");
959 icon.attr("src", "<?php echo esc_attr( WPPB_PLUGIN_URL ); ?>/assets/images/eye-off-outline.svg");
960 } else {
961 input.attr("type", "password");
962 button.toggleClass("wppb-show-pw").toggleClass("wppb-hide-pw");
963 icon.attr("src", "<?php echo esc_attr( WPPB_PLUGIN_URL ); ?>/assets/images/eye-outline.svg");
964 }
965 }
966 }
967 }
968 </script>
969 <?php
970 }
971 }
972
973 /**
974 * Create functions for repeating error messages in front-end forms
975 */
976 function wppb_required_field_error($field_title='') {
977 $required_error = apply_filters('wppb_required_error' , __('This field is required','profile-builder') , $field_title);
978
979 return $required_error;
980
981 }
982
983 /**
984 * Function that returns a certain field (from manage_fields) by a given id or meta_name
985 */
986 function wppb_get_field_by_id_or_meta( $id_or_meta ){
987
988 $id = 0;
989 $meta = '';
990
991 if ( is_numeric($id_or_meta) )
992 $id = $id_or_meta;
993 else
994 $meta = $id_or_meta;
995
996 $fields = get_option('wppb_manage_fields', 'not_found');
997
998 if ($fields != 'not_found') {
999
1000 foreach ($fields as $key => $field) {
1001 if ( (!empty($id)) && ($field['id'] == $id) )
1002 return $field;
1003 if ( (!empty($meta)) && ($field['meta-name'] == $meta) )
1004 return $field;
1005 }
1006
1007 }
1008
1009 return '';
1010 }
1011
1012
1013 /* Function for displaying reCAPTCHA error on Login and Recover Password forms */
1014 function wppb_recaptcha_field_error($field_title='') {
1015 $recaptcha_error = apply_filters('wppb_recaptcha_error' , __('Please enter a (valid) reCAPTCHA value','profile-builder') , $field_title);
1016
1017 return $recaptcha_error;
1018
1019 }
1020 /* Function for displaying phone field error */
1021 function wppb_phone_field_error( $field_title = '' ) {
1022 $phone_error = apply_filters( 'wppb_phone_error' , __( 'Incorrect phone number', 'profile-builder' ) , $field_title );
1023
1024 return $phone_error;
1025 }
1026
1027 /* Create a wrapper function for get_query_var */
1028 function wppb_get_query_var( $varname ){
1029 //if we want the userlisting on front page ( is_front_page() ) apparently the way we register query vars does not work so we will just use a simple GET var
1030 if($varname === 'username' && !get_query_var( $varname ) && isset($_GET['wppb_username'])){
1031 return apply_filters( 'wppb_get_query_var_'.$varname, sanitize_user( $_GET['wppb_username'] ) );
1032 }
1033
1034 return apply_filters( 'wppb_get_query_var_'.$varname, get_query_var( $varname ) );
1035 }
1036
1037 /** @param string|array $key Query key or keys to remove.
1038 */
1039 function wppb_remove_query_arg( $key, $url = false ){
1040 $striped_url = remove_query_arg( $key, $url);
1041
1042 //treat page key on frontpage case where it is transformed into a pretty permalink
1043 if( ( is_array($key) && in_array('wppb_page', $key) ) || ( !is_array($key) && 'wppb_page' === $key ) ){
1044 $striped_url = preg_replace( '/\/'.wppb_get_users_pagination_slug().'\/\d+\//', '/', $striped_url );
1045 $striped_url = preg_replace( '/\/'.wppb_get_users_pagination_slug().'\//', '/', $striped_url );
1046 }
1047
1048 return $striped_url;
1049 }
1050
1051 //function that generates tha pagination slug for userlisting
1052 function wppb_get_users_pagination_slug(){
1053 return apply_filters('wppb_users_pagination_slug', 'users-page');
1054 }
1055
1056 /* Filter the "Save Changes" button text, to make it translatable */
1057 function wppb_change_save_changes_button($value){
1058 $value = __('Save Changes','profile-builder');
1059 return $value;
1060 }
1061 add_filter( 'wck_save_changes_button', 'wppb_change_save_changes_button', 10, 2);
1062
1063 /* Filter the "Cancel" button text, to make it translatable */
1064 function wppb_change_cancel_button($value){
1065 $value = __('Cancel','profile-builder');
1066 return $value;
1067 }
1068 add_filter( 'wck_cancel_button', 'wppb_change_cancel_button', 10, 2);
1069
1070 /* ilter the "Delete" button text, to make it translatable */
1071 function wppb_change_delete_button($value){
1072 $value = __('Delete','profile-builder');
1073 return $value;
1074 }
1075 add_filter( 'wck_delete_button', 'wppb_change_delete_button', 10, 2);
1076
1077 /*Filter the "Edit" button text, to make it translatable*/
1078 function wppb_change_edit_button($value){
1079 $value = __('Edit','profile-builder');
1080 return $value;
1081 }
1082 add_filter( 'wck_edit_button', 'wppb_change_edit_button', 10, 2);
1083
1084 /*Filter the User Listing, Register Forms and Edit Profile forms metabox header content, to make it translatable*/
1085 function wppb_change_metabox_content_header(){
1086 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>';
1087 }
1088 add_filter('wck_metabox_content_header_wppb_ul_page_settings', 'wppb_change_metabox_content_header', 1);
1089 add_filter('wck_metabox_content_header_wppb_rf_page_settings', 'wppb_change_metabox_content_header', 1);
1090 add_filter('wck_metabox_content_header_wppb_epf_page_settings', 'wppb_change_metabox_content_header', 1);
1091
1092
1093 /*Filter default WordPress notices ("Post published. Post updated."), add post type name for User Listing, Registration Forms and Edit Profile Forms*/
1094 function wppb_change_default_post_updated_messages($messages){
1095 global $post;
1096 $post_type = get_post_type($post->ID);
1097 $object = get_post_type_object($post_type);
1098
1099 if ( ($post_type == 'wppb-rf-cpt')||($post_type == 'wppb-epf-cpt')||($post_type == 'wppb-ul-cpt') ){
1100 $messages['post'][1] = $object->labels->name . ' updated.';
1101 $messages['post'][6] = $object->labels->name . ' published.';
1102 }
1103 return $messages;
1104 }
1105 add_filter('post_updated_messages','wppb_change_default_post_updated_messages', 2);
1106
1107
1108 /* for meta-names with spaces in them PHP converts the space to underline in the $_POST */
1109 function wppb_handle_meta_name( $meta_name ){
1110 $meta_name = trim( $meta_name );
1111 $meta_name = str_replace( ' ', '_', $meta_name );
1112 $meta_name = str_replace( '.', '_', $meta_name );
1113 return $meta_name;
1114 }
1115
1116 /**
1117 * Function that checks if a field type exists in a form
1118 * @return bool
1119 */
1120 function wppb_field_exists_in_form( $field_type, $form_args ){
1121 if( !empty( $form_args ) && !empty( $form_args['form_fields'] ) ){
1122 foreach( $form_args['form_fields'] as $field ){
1123 if( $field['field'] === $field_type ){
1124 return true;
1125 }
1126 }
1127 }
1128
1129 return false;
1130 }
1131
1132 // change User Registered date and time according to timezone selected in WordPress settings
1133 function wppb_get_register_date() {
1134
1135 $time_format = "Y-m-d G:i:s";
1136 $wppb_get_date = date_i18n( $time_format, false, true );
1137
1138 if( apply_filters( 'wppb_return_local_time_for_register', false ) ){
1139 $wppb_get_date = date_i18n( $time_format );
1140 }
1141
1142 return $wppb_get_date;
1143 }
1144
1145 /**
1146 * Function that ads the gmt offset from the general settings to a unix timestamp
1147 * @param $timestamp
1148 * @return mixed
1149 */
1150 function wppb_add_gmt_offset( $timestamp ) {
1151 if( apply_filters( 'wppb_add_gmt_offset', true ) ){
1152 $timestamp = $timestamp + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
1153 }
1154 return $timestamp;
1155 }
1156
1157 /**
1158 * Add HTML tag 'required' to fields
1159 *
1160 * 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.
1161 * Field type 'Checkbox' is explicitly excluded because there is no HTML support to check if at least one option is selected.
1162 * Other fields excluded are Avatar, Upload, Heading, ReCaptcha, WYSIWYG, Map.
1163 *
1164 * @since
1165 *
1166 * @param string $extra_attributes Extra attributes attached to the field HTML tag.
1167 * @param array $field Field description.
1168 * @return string $extra_attributes
1169 */
1170 function wppb_add_html_tag_required_to_fields( $extra_attributes, $field, $form_location = '' ) {
1171 if ( $field['field'] != "Checkbox" && isset( $field['required'] ) && $field['required'] == 'Yes' ){
1172 if( !( ( $field['field'] == "Default - Password" || $field['field'] == "Default - Repeat Password" ) && $form_location == 'edit_profile' ) )
1173 $extra_attributes .= ' required ';
1174 }
1175 return $extra_attributes;
1176 }
1177 add_filter( 'wppb_extra_attribute', 'wppb_add_html_tag_required_to_fields', 10, 3 );
1178
1179 /**
1180 * Add HTML tag 'required' to WooCommerce fields
1181 *
1182 * 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.
1183 * Does not work on 'State / County' field, if it becomes required later depending on the Country Value
1184 *
1185 * @since
1186 *
1187 * @param string $extra_attributes Extra attributes attached to the field HTML tag.
1188 * @param array $field Field description.
1189 * @return string $extra_attributes
1190 */
1191 function wppb_add_html_tag_required_to_woo_fields( $extra_attributes, $field ) {
1192 if ( isset( $field['required'] ) && $field['required'] == 'Yes' ){
1193 $extra_attributes .= ' required ';
1194 }
1195 return $extra_attributes;
1196 }
1197 add_filter( 'wppb_woo_extra_attribute', 'wppb_add_html_tag_required_to_woo_fields', 10, 2 );
1198
1199
1200 /**
1201 * Add jQuery script to remove required attribute for hidden fields
1202 *
1203 * 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.
1204 * If a field is made visible again, add the required field back again.
1205 *
1206 * @since
1207 *
1208 * @param string $extra_attributes Extra attributes attached to the field HTML tag.
1209 * @param array $field Field description.
1210 * @return string $extra_attributes
1211 */
1212 function wppb_manage_required_attribute() {
1213 global $wppb_shortcode_on_front;
1214 if ($wppb_shortcode_on_front) {
1215 //check if jquery has been loaded yet because we need it at this point
1216 // we're checking if it's not admin because it brakes elementor otherwise.
1217 if( !wp_script_is('jquery', 'done') && !is_admin() ){
1218 wp_print_scripts('jquery');
1219 }
1220 ?>
1221 <script type="text/javascript">
1222 jQuery(document).on( "wppbAddRequiredAttributeEvent", wppbAddRequired );
1223 function wppbAddRequired(event) {
1224 var element = wppbEventTargetRequiredElement( event.target );
1225 if( jQuery( element ).attr( "wppb_cf_temprequired" ) ){
1226 jQuery( element ).removeAttr( "wppb_cf_temprequired" );
1227 jQuery( element ).attr( "required", "required" );
1228 }
1229 }
1230
1231 jQuery(document).on( "wppbRemoveRequiredAttributeEvent", wppbRemoveRequired );
1232 function wppbRemoveRequired(event) {
1233 var element = wppbEventTargetRequiredElement( event.target );
1234 if ( jQuery( element ).attr( "required" ) ) {
1235 jQuery( element ).removeAttr( "required" );
1236 jQuery( element ).attr( "wppb_cf_temprequired", "wppb_cf_temprequired" );
1237 }
1238 }
1239
1240 jQuery(document).on( "wppbToggleRequiredAttributeEvent", wppbToggleRequired );
1241 function wppbToggleRequired(event) {
1242 if ( jQuery( event.target ).attr( "required" ) ) {
1243 jQuery( event.target ).removeAttr( "required" );
1244 jQuery( event.target ).attr( "wppb_cf_temprequired", "wppb_cf_temprequired" );
1245 }else if( jQuery( event.target ).attr( "wppb_cf_temprequired" ) ){
1246 jQuery( event.target ).removeAttr( "wppb_cf_temprequired" );
1247 jQuery( event.target ).attr( "required", "required" );
1248 }
1249 }
1250
1251 function wppbEventTargetRequiredElement( htmlElement ){
1252 if ( htmlElement.nodeName == "OPTION" ){
1253 // <option> is the target element, so we need to get the parent <select>, in order to apply the required attribute
1254 return htmlElement.parentElement;
1255 }else{
1256 return htmlElement;
1257 }
1258 }
1259
1260 </script>
1261 <?php
1262 }
1263 }
1264 add_action( 'wp_footer', 'wppb_manage_required_attribute', 99 );
1265
1266 function wpbb_specify_blog_details_on_signup_email( $message, $user_email, $user, $activation_key, $registration_page_url, $meta, $from_name, $context ){
1267 $meta = unserialize($meta);
1268
1269 if ( is_multisite() && isset( $meta['wppb_create_new_site_checkbox'] ) && $meta['wppb_create_new_site_checkbox'] == 'yes' ) {
1270 $blog_details = wpmu_validate_blog_signup( $meta['wppb_blog_url'], $meta['wppb_blog_title'] );
1271
1272 if ( empty($blog_details['errors']->errors['blogname']) && empty($blog_details['errors']->errors['blog_title'])) {
1273 $blog_path = $blog_details['domain'] . $blog_details['path'];
1274 $message .= __( '<br><br>Also, you will be able to visit your site at ', 'profile-builder' ) . '<a href="' . $blog_path . '">' . $blog_path . '</a>.';
1275 }
1276 }
1277 return $message;
1278 }
1279 add_filter( 'wppb_signup_user_notification_email_content', 'wpbb_specify_blog_details_on_signup_email', 5, 8 );
1280
1281 function wpbb_specify_blog_details_on_registration_email( $user_message_content, $email, $password, $user_message_subject, $context ){
1282
1283 if ( is_multisite() ) {
1284 $user = get_user_by( 'email', $email );
1285 $blog_path = wppb_get_blog_url_of_user_id( $user->ID );
1286 if ( ! empty ( $blog_path ) ) {
1287 $user_message_content .= __( '<br><br>You can visit your site at ', 'profile-builder' ) . '<a href="' . $blog_path . '">' . $blog_path . '</a>.';
1288 }
1289 }
1290 return $user_message_content;
1291
1292 }
1293 add_filter( 'wppb_register_user_email_message_without_admin_approval', 'wpbb_specify_blog_details_on_registration_email', 5, 5 );
1294 add_filter( 'wppb_register_user_email_message_with_admin_approval', 'wpbb_specify_blog_details_on_registration_email', 5, 5 );
1295
1296
1297 function wppb_get_blog_url_of_user_id( $user_id, $ignore_privacy = true ){
1298 $blog_id = get_user_meta( $user_id, 'primary_blog', true );
1299 if ( is_multisite() && !empty( $blog_id ) ){
1300 $blog_details = get_blog_details( $blog_id );
1301 if ( $ignore_privacy || $blog_details->public ) {
1302 return $blog_details->domain . $blog_details->path;
1303 }
1304 }
1305 return '';
1306 }
1307
1308 function wppb_can_users_signup_blog(){
1309 if ( ! is_multisite() )
1310 return false;
1311 global $wpdb;
1312 $current_site = get_current_site();
1313 $sitemeta_options_query = $wpdb->prepare( "SELECT meta_value FROM {$wpdb->sitemeta} WHERE meta_key = 'registration' AND site_id = %d", $current_site->id );
1314 $network_options_meta = $wpdb->get_results( $sitemeta_options_query );
1315
1316 if ( $network_options_meta[0]->meta_value == 'all' ){
1317 return true;
1318 }
1319 return false;
1320 }
1321
1322 /**
1323 * Function that handle redirect URL
1324 *
1325 * @param string $redirect_priority - it can be normal or top priority
1326 * @param string $redirect_type - type of the redirect
1327 * @param null|string $redirect_url - redirect URL if already set
1328 * @param null|string|object $user - username, user email or user data
1329 * @param null|string $user_role - user role
1330 *
1331 * @return null|string $redirect_url
1332 */
1333 function wppb_get_redirect_url( $redirect_priority, $redirect_type, $redirect_url = NULL, $user = NULL, $user_role = NULL ) {
1334 if( empty($redirect_priority) ) {
1335 $redirect_priority = 'normal';
1336 }
1337
1338 $versions = array( 'Profile Builder Pro', 'Profile Builder Elite', 'Profile Builder Unlimited', 'Profile Builder Dev' );
1339
1340 if( in_array( PROFILE_BUILDER, $versions ) ) {
1341 $wppb_module_settings = get_option( 'wppb_module_settings' );
1342
1343 if( isset( $wppb_module_settings['wppb_customRedirect'] ) && $wppb_module_settings['wppb_customRedirect'] == 'show' && $redirect_priority != 'top' && function_exists( 'wppb_custom_redirect_url' ) ) {
1344 $redirect_url = wppb_custom_redirect_url( $redirect_type, $redirect_url, $user, $user_role );
1345 }
1346 }
1347
1348 if( ! empty( $redirect_url ) ) {
1349 $redirect_url = ( wppb_check_missing_http( $redirect_url ) ? 'http://'. $redirect_url : $redirect_url );
1350 }
1351
1352 return $redirect_url;
1353 }
1354
1355 /**
1356 * Function that builds the redirect
1357 *
1358 * @param string $redirect_url - redirect URL
1359 * @param int $redirect_delay - redirect delay in seconds
1360 * @param null|string $redirect_type - the type of the redirect
1361 * @param null|array $form_args - form args if set
1362 *
1363 * @return string $redirect_message
1364 */
1365 function wppb_build_redirect( $redirect_url, $redirect_delay, $redirect_type = NULL, $form_args = NULL ) {
1366 if( isset( $redirect_type ) ) {
1367 $redirect_url = apply_filters( 'wppb_'. $redirect_type .'_redirect', $redirect_url );
1368 }
1369
1370 $redirect_message = '';
1371
1372 if( ! empty( $redirect_url ) ) {
1373 $redirect_url = ( wppb_check_missing_http( $redirect_url ) ? 'http://'. $redirect_url : $redirect_url );
1374
1375 if( $redirect_delay == 0 ) {
1376 $redirect_message = '<meta http-equiv="Refresh" content="'. $redirect_delay .';url='. $redirect_url .'" />';
1377 } else {
1378 $redirect_url_href = apply_filters( 'wppb_redirect_url', '<a href="'. $redirect_url .'">'. __( 'here', 'profile-builder' ) .'</a>', $redirect_url, $redirect_type, $form_args );
1379 $redirect_message = apply_filters( 'wppb_redirect_message_before_returning', '<p class="redirect_message">'. sprintf( wp_slash( __( 'You will soon be redirected automatically. If you see this page for more than %1$d seconds, please click %2$s.%3$s', 'profile-builder' ) ), $redirect_delay, $redirect_url_href, '<meta http-equiv="Refresh" content="'. $redirect_delay .';url='. $redirect_url .'" />' ) .'</p>', $redirect_url, $redirect_delay, $redirect_url_href, $redirect_type, $form_args );
1380 }
1381 }
1382
1383 return $redirect_message;
1384 }
1385
1386 /**
1387 * Function that strips the script tags from an input
1388 * @param $string
1389 * @return mixed
1390 */
1391 function wppb_sanitize_value( $string ){
1392 return preg_replace( '/<script\b[^>]*>(.*?)<\/script>/is', '', $string );
1393 }
1394
1395 /**
1396 * Function that receives a user role and returns its label.
1397 * Returns the original role if not found.
1398 *
1399 * @since v.2.7.1
1400 *
1401 * @param string $role
1402 *
1403 * @return string
1404 */
1405 function wppb_get_role_name($role){
1406 global $wp_roles;
1407
1408 if ( array_key_exists( $role, $wp_roles->role_names ) )
1409 return $wp_roles->role_names[$role];
1410
1411 return $role;
1412 }
1413
1414 /**
1415 * Function that receives a user role label and returns its slug.
1416 * Returns the original role label if not found.
1417 *
1418 * @since v.3.5.6
1419 *
1420 * @param string $role
1421 *
1422 * @return string
1423 */
1424 function wppb_get_role_slug($role) {
1425 global $wp_roles;
1426
1427 foreach ( $wp_roles->role_names as $slug => $label ) {
1428 if ( $label === $role) {
1429 return $slug;
1430 }
1431 }
1432
1433 return $role;
1434 }
1435
1436 /**
1437 * Functionality for Private Website start
1438 */
1439 add_action( 'template_redirect', 'wppb_private_website_functionality' );
1440 add_action( 'login_init', 'wppb_private_website_functionality', 1 );
1441 function wppb_private_website_functionality(){
1442 $wppb_private_website_settings = get_option( 'wppb_private_website_settings', 'not_found' );
1443 if( $wppb_private_website_settings != 'not_found' ){
1444 if( $wppb_private_website_settings['private_website'] == 'yes' ){
1445 if( !is_user_logged_in() ){
1446
1447 //force wp-login.php if you accidentally get locked out
1448 global $pagenow;
1449 if( $pagenow === 'wp-login.php' && isset( $_GET['wppb_force_wp_login'] ) )
1450 return;
1451
1452 //go through paths first if they are set
1453 if( isset( $wppb_private_website_settings['allowed_paths'] ) && !empty( $wppb_private_website_settings['allowed_paths'] ) ){
1454 $allowed_paths = explode( "\r\n", $wppb_private_website_settings['allowed_paths'] );
1455 $parsed_url = wp_parse_url( wppb_curpageurl() );
1456 if( !empty( $parsed_url['path'] ) ) {
1457 $path = $parsed_url['path'];
1458
1459 foreach ($allowed_paths as $allowed_path) {
1460 if (strpos($allowed_path, '*') === false) {
1461 if (trim($path, "/") === trim($allowed_path, "/")) {
1462 return;
1463 }
1464 } else {
1465 if (strpos(ltrim($path, "/"), trailingslashit(trim(str_replace('*', '', $allowed_path), "/"))) === 0) {
1466 return;
1467 }
1468 }
1469 }
1470 }
1471 }
1472
1473 if( isset( $wppb_private_website_settings['allowed_pages'] ) )
1474 $allowed_pages = $wppb_private_website_settings['allowed_pages'];
1475 else{
1476 $allowed_pages = array();
1477 }
1478
1479 $redirect_to_id = $wppb_private_website_settings['redirect_to'];
1480 if( !empty( $redirect_to_id ) ) {
1481 $redirect_url = get_permalink($redirect_to_id);
1482 $allowed_pages[] = $redirect_to_id;
1483 }
1484 else {
1485 //don't redirect if we are already on the wp-login.php page
1486 if( $pagenow === 'wp-login.php' ){
1487 return;
1488 }
1489 else
1490 $redirect_url = wp_login_url(wppb_curpageurl());
1491 }
1492
1493 $redirect_url = apply_filters( 'wppb_private_website_redirect_url', $redirect_url );
1494 $allowed_pages = apply_filters( 'wppb_private_website_allowed_pages', $allowed_pages );
1495
1496 global $post;
1497 if( !isset( $post ) || ( isset($post) && $post->ID == 0 ) ) {//added || ( isset($post) && $post->ID == 0 ) for a compatibility issue with BuddyPress where the ID was 0 for a page
1498 if( function_exists('url_to_postid') ) {
1499 $post_id = url_to_postid(wppb_curpageurl());//try to get the id from the actual url
1500 }else {
1501 $post_id = 0;
1502 }
1503 }
1504 else
1505 $post_id = $post->ID;
1506
1507 if( ( !in_array( $post_id, $allowed_pages ) && $redirect_url !== strtok( wppb_curpageurl(), '?' ) ) || is_search() ){
1508 nocache_headers();
1509 if( apply_filters( 'wppb_private_website_redirect_add_query_args', true ) && current_filter() == 'template_redirect' ) {
1510 $redirect_url = add_query_arg( 'wppb_referer_url', urlencode( esc_url( wppb_curpageurl() ) ), $redirect_url );
1511 }
1512 wp_safe_redirect( $redirect_url );
1513 exit;
1514 }
1515
1516 }
1517 }
1518 }
1519 }
1520
1521 //add classes on the body to know if we have enabled private website options
1522 add_filter( 'body_class', 'wppb_private_website_body_classes' );
1523 function wppb_private_website_body_classes( $classes ){
1524 $wppb_private_website_settings = get_option( 'wppb_private_website_settings', 'not_found' );
1525 if( $wppb_private_website_settings != 'not_found' ) {
1526 if ($wppb_private_website_settings['private_website'] == 'yes') {
1527 if (!is_user_logged_in()) {
1528 $classes[] = 'wppb-private-website';
1529 if ($wppb_private_website_settings['hide_menus'] == 'yes') {
1530 $classes[] = 'wppb-private-website-hide-menus';
1531 }
1532 }
1533 }
1534 }
1535
1536 return $classes;
1537 }
1538
1539
1540 /**
1541 * Disable RSS
1542 */
1543 add_action('do_feed', 'wppb_disable_feed', 1);
1544 add_action('do_feed_rdf', 'wppb_disable_feed', 1);
1545 add_action('do_feed_rss', 'wppb_disable_feed', 1);
1546 add_action('do_feed_rss2', 'wppb_disable_feed', 1);
1547 add_action('do_feed_atom', 'wppb_disable_feed', 1);
1548 add_action('do_feed_rss2_comments', 'wppb_disable_feed', 1);
1549 add_action('do_feed_atom_comments', 'wppb_disable_feed', 1);
1550 function wppb_disable_feed() {
1551 $wppb_private_website_settings = get_option( 'wppb_private_website_settings', 'not_found' );
1552 if( $wppb_private_website_settings != 'not_found' ) {
1553 if ($wppb_private_website_settings['private_website'] == 'yes') {
1554 if (!is_user_logged_in()) {
1555 wp_die( wp_kses_post( sprintf( __('No feed available,please visit our <a href="%s">homepage</a>!', 'profile-builder' ), get_bloginfo('url') ) ) );
1556 }
1557 }
1558 }
1559 }
1560
1561
1562 /**
1563 * Disable REST
1564 */
1565 //add_filter('rest_enabled', 'wppb_disable_rest'); // this is depracated
1566 add_filter('rest_jsonp_enabled', 'wppb_disable_rest');
1567 function wppb_disable_rest( $bool ){
1568 $wppb_private_website_settings = get_option( 'wppb_private_website_settings', 'not_found' );
1569 if( $wppb_private_website_settings != 'not_found' ) {
1570 if ($wppb_private_website_settings['private_website'] == 'yes') {
1571 if ( isset( $wppb_private_website_settings[ 'disable_rest_api' ] ) && $wppb_private_website_settings[ 'disable_rest_api' ] == 'no' ) {
1572 return $bool;
1573 }
1574 if (!is_user_logged_in()) {
1575 return false;
1576 }
1577 }
1578 }
1579 return $bool;
1580 }
1581
1582 /* I should test this to not create any problems */
1583 add_filter('rest_authentication_errors', 'wppb_disable_rest_api_authentication', 10, 1 );
1584 function wppb_disable_rest_api_authentication($result) {
1585 if (!empty($result)) {
1586 return $result;
1587 }
1588
1589 $wppb_private_website_settings = get_option( 'wppb_private_website_settings', 'not_found' );
1590 if( $wppb_private_website_settings != 'not_found' ) {
1591 if ($wppb_private_website_settings['private_website'] == 'yes') {
1592 if ( isset( $wppb_private_website_settings[ 'disable_rest_api' ] ) && $wppb_private_website_settings[ 'disable_rest_api' ] == 'no' ) {
1593 return $result;
1594 }
1595 if (!is_user_logged_in() && isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] !== "/wp-json/jwt-auth/v1/token" && $_SERVER['REQUEST_URI'] !== "/wp-json/jwt-auth/v1/token/validate") {
1596 return new WP_Error('rest_not_logged_in', __( 'You are not currently logged in.', 'profile-builder' ), array('status' => 401));
1597 }
1598 }
1599 }
1600
1601 return $result;
1602 }
1603
1604 /**
1605 * We can hide all menu items
1606 */
1607 add_filter('wp_nav_menu', 'wppb_hide_menus');
1608 add_filter('wp_page_menu', 'wppb_hide_menus');
1609 function wppb_hide_menus( $menu ){
1610 $wppb_private_website_settings = get_option( 'wppb_private_website_settings', 'not_found' );
1611 if( $wppb_private_website_settings != 'not_found' ) {
1612 if ($wppb_private_website_settings['private_website'] == 'yes') {
1613 if ( !is_user_logged_in() && ( !empty($wppb_private_website_settings['hide_menus']) && $wppb_private_website_settings['hide_menus'] == 'yes' ) ) {
1614 return '';
1615 }
1616 }
1617 }
1618 return $menu;
1619 }
1620
1621 /**
1622 * Functionality for Private Website end
1623 */
1624
1625 /**
1626 * Functionality GDPR compliance start
1627 */
1628
1629 //hook into the wp export compatibility
1630 add_filter( 'wp_privacy_personal_data_exporters', 'wppb_register_profile_builder_wp_exporter', 10 );
1631 function wppb_register_profile_builder_wp_exporter( $exporters ) {
1632 $exporters['profile-builder'] = array(
1633 'exporter_friendly_name' => __( 'Profile Builder', 'profile-builder' ),
1634 'callback' => 'wppb_profile_builder_wp_exporter',
1635 );
1636 return $exporters;
1637 }
1638 /* function to add aour user meta to wp exporter */
1639 function wppb_profile_builder_wp_exporter( $email_address, $page = 1 ) {
1640
1641 $export_items = array();
1642
1643 $form_fields = get_option( 'wppb_manage_fields' );
1644
1645 if( !empty( $form_fields ) ) {
1646 $user = get_user_by( 'email', $email_address );
1647 if( $user ) {
1648
1649 $item_id = "user-meta-{$user->ID}";
1650 $group_id = 'user-meta';
1651 $group_label = __('User Meta' , 'profile-builder' );
1652 $data = array();
1653
1654 $all_meta_for_user = get_user_meta( $user->ID );
1655 if( !empty( $all_meta_for_user ) ) {
1656 foreach ($form_fields as $form_field) {
1657
1658 if (!empty($form_field['meta-name']) && strpos($form_field['field'], 'Default') === false) {
1659 $user_meta_value = $all_meta_for_user[$form_field['meta-name']][0];
1660 if( !empty( $user_meta_value ) ){
1661
1662
1663 $data[] = array(
1664 'name' => $form_field['field-title'],
1665 'value' => $user_meta_value
1666 );
1667 }
1668 }
1669 }
1670
1671 $export_items[] = array(
1672 'group_id' => $group_id,
1673 'group_label' => $group_label,
1674 'item_id' => $item_id,
1675 'data' => $data,
1676 );
1677
1678 }
1679 }
1680 }
1681
1682
1683 return array(
1684 'data' => $export_items,
1685 'done' => true,
1686 );
1687 }
1688
1689 /**
1690 * Hook to delete a user through the GDPR Delete button
1691 */
1692 add_action( 'template_redirect', 'wppb_gdpr_delete_user') ;
1693 function wppb_gdpr_delete_user() {
1694
1695 if( isset( $_GET['wppb_user'] ) && ! empty( $_GET['wppb_user'] ) ) {
1696
1697 $edited_user_id = get_current_user_id();
1698 if ( ( !is_multisite() && current_user_can('edit_users') ) || ( is_multisite() && current_user_can('manage_network') ) ) {
1699 $edited_user_id = absint($_GET['wppb_user']);
1700 }
1701
1702 if (isset($_REQUEST['wppb_action']) && $_REQUEST['wppb_action'] == 'wppb_delete_user' && isset( $_REQUEST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_REQUEST['wppb_nonce'] ), 'wppb-user-own-account-deletion') && isset($_REQUEST['wppb_user']) && $edited_user_id == $_REQUEST['wppb_user']) {
1703 require_once(ABSPATH . 'wp-admin/includes/user.php');
1704 $user = new WP_User( absint( $_REQUEST['wppb_user'] ) );
1705
1706 if (!empty($user->roles)) {
1707 if( !in_array( 'administrator', $user->roles ) ){
1708 wp_delete_user( absint( $_REQUEST['wppb_user'] ) );
1709
1710 do_action( 'wppb_gdpr_user_deleted', absint( $_REQUEST['wppb_user'] ) );
1711 }
1712 }
1713
1714 $args = array('wppb_user', 'wppb_action', 'wppb_nonce');
1715 nocache_headers();
1716 wp_redirect(remove_query_arg($args));
1717 }
1718 }
1719 }
1720
1721 /**
1722 * Function that removes user information from comments when the User Account is deleted using Edit Profile Form
1723 */
1724 function wppb_gdpr_remove_user_info_from_comments( $user_id ) {
1725 $user_comments = get_comments('user_id='.$user_id);
1726 foreach ( $user_comments as $comment ) {
1727 $comment_data = array();
1728 $comment_data['comment_ID'] = $comment->comment_ID;
1729 $comment_data['comment_author'] = '';
1730 $comment_data['comment_author_email'] = '';
1731 $comment_data['comment_author_url'] = '';
1732 wp_update_comment( $comment_data );
1733 }
1734 }
1735 add_action( 'wppb_gdpr_user_deleted', 'wppb_gdpr_remove_user_info_from_comments' );
1736
1737
1738 /**
1739 * Functionality GDPR compliance end
1740 */
1741
1742 /**
1743 * Function that checks if Admin Approval is enabled
1744 */
1745 function wppb_get_admin_approval_option_value(){
1746 $wppb_general_settings = get_option( 'wppb_general_settings', 'not_found' );
1747 if( defined( 'WPPB_PAID_PLUGIN_DIR' ) && file_exists( WPPB_PAID_PLUGIN_DIR . '/features/admin-approval/admin-approval.php' ) && $wppb_general_settings != 'not_found' && !empty( $wppb_general_settings['adminApproval'] ) && $wppb_general_settings['adminApproval'] === 'yes' )
1748 return 'yes';
1749 else
1750 return 'no';
1751 }
1752
1753 /**
1754 * Function that checks if conditional fields feature exists
1755 * @return bool
1756 */
1757 function wppb_conditional_fields_exists(){
1758 if ( defined( 'WPPB_PAID_PLUGIN_DIR' ) && file_exists( WPPB_PAID_PLUGIN_DIR . '/features/conditional-fields/conditional-fields.php' ) )
1759 return true;
1760 else
1761 return false;
1762 }
1763
1764 /**
1765 * Add support for [wppb-embed] shortcode inside userlisting as the default [embed] is weird and doesn't work outside the_content
1766 */
1767 add_shortcode('wppb-embed', 'wppb_embed');
1768 function wppb_embed($atts, $content){
1769 $atts = shortcode_atts( array(
1770 'width' => '',
1771 'height' => ''
1772 ), $atts, 'wppb-embed' );
1773
1774 global $wp_embed;
1775 if(empty($atts['width']) || empty($atts['height'])){
1776 $content = $wp_embed->run_shortcode('[embed]'.$content.'[/embed]');
1777 } else {
1778 $content = $wp_embed->run_shortcode('[embed width="'.$atts['width'].'" height="'.$atts['height'].'"]'.$content.'[/embed]');
1779 }
1780
1781 return $content;
1782 }
1783
1784 /**
1785 * Function to determine if an add-on is active
1786 */
1787
1788 function wppb_check_if_add_on_is_active( $slug ){
1789 //the old modules part
1790 if ( defined( 'WPPB_PAID_PLUGIN_DIR' ) && file_exists(WPPB_PAID_PLUGIN_DIR . '/add-ons/add-ons.php')) {
1791 $wppb_module_settings = get_option('wppb_module_settings', 'not_found');
1792 if ($wppb_module_settings != 'not_found') {
1793 foreach ($wppb_module_settings as $add_on_slug => $status) {
1794 if ($slug == $add_on_slug) {
1795 if ($status === 'hide')
1796 return false;
1797 elseif ($status === 'show')
1798 return true;
1799 }
1800 }
1801 }
1802 }
1803
1804 //the free addons part
1805 $wppb_free_add_ons_settings = get_option( 'wppb_free_add_ons_settings', array() );
1806 if ( !empty( $wppb_free_add_ons_settings ) ){
1807 foreach( $wppb_free_add_ons_settings as $add_on_slug => $status ){
1808 if( $slug == $add_on_slug ){
1809 return $status;
1810 }
1811 }
1812 }
1813
1814 //the advanced addons part
1815 $wppb_advanced_add_ons_settings = get_option( 'wppb_advanced_add_ons_settings', array() );
1816 if ( !empty( $wppb_advanced_add_ons_settings ) ){
1817 foreach( $wppb_advanced_add_ons_settings as $add_on_slug => $status ){
1818 if( $slug == $add_on_slug ){
1819 return $status;
1820 }
1821 }
1822 }
1823
1824 return false;
1825 }
1826
1827 /**
1828 * Function that checks if Two-Factor Authentication is active
1829 */
1830
1831 function wppb_is_2fa_active(){
1832 $wppb_two_factor_authentication_settings = get_option( 'wppb_two_factor_authentication_settings', 'not_found' );
1833 if( isset( $wppb_two_factor_authentication_settings['enabled'] ) && $wppb_two_factor_authentication_settings['enabled'] === 'yes' ) {
1834 return true;
1835 }
1836
1837 return false;
1838 }
1839
1840 /**
1841 * Function that returns an array containing the User Listing names
1842 */
1843
1844 function wppb_get_userlisting_names(){
1845 $ul_names = array();
1846 $userlisting_posts = get_posts( array( 'posts_per_page' => -1, 'post_status' =>'publish', 'post_type' => 'wppb-ul-cpt', 'orderby' => 'post_date', 'order' => 'ASC' ) );
1847 if( !empty( $userlisting_posts ) ){
1848 foreach ( $userlisting_posts as $post ){
1849 $ul_names[ $post->post_name ] = $post->post_title;
1850 }
1851 }
1852 reset($ul_names);
1853 return $ul_names;
1854 }
1855