PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.7.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.7.3
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 2.0.8 All 339 releases
profile-builder / features / functions.php

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

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