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

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