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.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 / admin / admin-functions.php

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

362 lines 17.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 * Function which returns the same field-format over and over again.
5 *
6 * @since v.2.0
7 *
8 * @param string $field
9 * @param string $field_title
10 *
11 * @return string
12 */
13 function wppb_field_format ( $field_title, $field ){
14
15 return trim( $field_title ).' ( '.trim( $field ).' )';
16 }
17
18
19 /**
20 * Add a notification for either the Username or the Email field letting the user know that, even though it is there, it won't do anything
21 *
22 * @since v.2.0
23 *
24 * @param string $form
25 * @param integer $id
26 * @param string $value
27 *
28 * @return string $form
29 */
30
31 function wppb_manage_fields_display_field_title_slug( $form ){
32 // add a notice to fields
33 global $wppb_results_field;
34 switch ($wppb_results_field){
35 case 'Default - Username':
36 $wppb_generalSettings = get_option( 'wppb_general_settings', 'not_found' );
37 if ( $wppb_generalSettings != 'not_found' && $wppb_generalSettings['loginWith'] == 'email' ) {
38 $form .= '<div id="wppb-login-email-nag" class="wppb-backend-notice">' . sprintf(__('Login is set to be done using the Email. This field will NOT appear in the front-end! ( you can change these settings under the "%s" tab )', 'profile-builder'), '<a href="' . admin_url('admin.php?page=profile-builder-general-settings') . '" target="_blank">' . __('General Settings', 'profile-builder') . '</a>') . '</div>';
39 }
40 break;
41 case 'Default - Display name publicly as':
42 $form .= '<div id="wppb-display-name-nag" class="wppb-backend-notice">' . __( 'Display name publicly as - only appears on the Edit Profile page!', 'profile-builder' ) . '</div>';
43 break;
44 case 'Default - Blog Details':
45 $form .= '<div id="wppb-blog-details-nag" class="wppb-backend-notice">' . __( 'Blog Details - only appears on the Registration page!', 'profile-builder' ) . '</div>';
46 break;
47 }
48
49 return $form;
50 }
51
52 add_filter( 'wck_after_content_element', 'wppb_manage_fields_display_field_title_slug' );
53
54 /**
55 * Check if field type is 'Default - Display name publicly as' so we can add a notification for it
56 *
57 * @since v.2.2
58 *
59 * @param string $wck_element_class
60 * @param string $meta
61 * @param array $results
62 * @param integer $element_id
63 *
64 */
65 function wppb_manage_fields_display_name_notice( $wck_element_class, $meta, $results, $element_id ) {
66 global $wppb_results_field;
67
68 $wppb_results_field = $results[$element_id]['field'];
69 }
70 add_filter( 'wck_element_class_wppb_manage_fields', 'wppb_manage_fields_display_name_notice', 10, 4 );
71
72
73
74 /**
75 * Function that adds a custom class to the existing container
76 *
77 * @since v.2.0
78 *
79 * @param string $update_container_class - the new class name
80 * @param string $meta - the name of the meta
81 * @param array $results
82 * @param integer $element_id - the ID of the element
83 *
84 * @return string
85 */
86 function wppb_update_container_class( $update_container_class, $meta, $results, $element_id ) {
87 $wppb_element_type = Wordpress_Creation_Kit_PB::wck_generate_slug( $results[$element_id]["field"] );
88
89 return "class='wck_update_container update_container_$meta update_container_$wppb_element_type element_type_$wppb_element_type'";
90 }
91 add_filter( 'wck_update_container_class_wppb_manage_fields', 'wppb_update_container_class', 10, 4 );
92
93
94 /**
95 * Function that adds a custom class to the existing element
96 *
97 * @since v.2.0
98 *
99 * @param string $element_class - the new class name
100 * @param string $meta - the name of the meta
101 * @param array $results
102 * @param integer $element_id - the ID of the element
103 *
104 * @return string
105 */
106 function wppb_element_class( $element_class, $meta, $results, $element_id ){
107 $wppb_element_type = Wordpress_Creation_Kit_PB::wck_generate_slug( $results[$element_id]["field"] );
108
109 return "class='element_type_$wppb_element_type added_fields_list'";
110 }
111 add_filter( 'wck_element_class_wppb_manage_fields', 'wppb_element_class', 10, 4 );
112
113 /**
114 * Functions to check password length and strength
115 *
116 * @since v.2.0
117 */
118 /* on add user and update profile from WP admin area */
119 add_action( 'user_profile_update_errors', 'wppb_password_check_on_profile_update', 0, 3 );
120 function wppb_password_check_on_profile_update( $errors, $update, $user ){
121 wppb_password_check_extra_conditions( $errors, $user );
122 }
123
124 /* on reset password */
125 add_action( 'validate_password_reset', 'wppb_password_check_extra_conditions', 10, 2 );
126 function wppb_password_check_extra_conditions( $errors, $user ){
127 $password = ( isset( $_POST[ 'pass1' ] ) && trim( $_POST[ 'pass1' ] ) ) ? $_POST[ 'pass1' ] : false; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
128
129 if( $password && ( !isset( $_POST['pw_weak'] ) || $_POST['pw_weak'] != 'on' ) ){
130 $wppb_generalSettings = get_option( 'wppb_general_settings' );
131
132 if( !empty( $wppb_generalSettings['minimum_password_length'] ) ){
133 if( strlen( $password ) < $wppb_generalSettings['minimum_password_length'] )
134 $errors->add( 'pass', sprintf( __( '<strong>ERROR</strong>: The password must have the minimum length of %s characters', 'profile-builder' ), $wppb_generalSettings['minimum_password_length'] ) );
135 }
136
137 if( isset( $_POST['wppb_password_strength'] ) && !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
138 $password_strength_array = array( 'short' => 0, 'bad' => 1, 'good' => 2, 'strong' => 3 );
139 $password_strength_text = array( 'short' => __( 'Very weak', 'profile-builder' ), 'bad' => __( 'Weak', 'profile-builder' ), 'good' => __( 'Medium', 'profile-builder' ), 'strong' => __( 'Strong', 'profile-builder' ) );
140
141 foreach( $password_strength_text as $psr_key => $psr_text ){
142 if( $psr_text == sanitize_text_field( $_POST['wppb_password_strength'] ) ){
143 $password_strength_result_slug = $psr_key;
144 break;
145 }
146 }
147
148 if( !empty( $password_strength_result_slug ) ){
149 if( $password_strength_array[$password_strength_result_slug] < $password_strength_array[$wppb_generalSettings['minimum_password_strength']] )
150 $errors->add( 'pass', sprintf( __( '<strong>ERROR</strong>: The password must have a minimum strength of %s', 'profile-builder' ), $password_strength_text[$wppb_generalSettings['minimum_password_strength']] ) );
151 }
152 }
153 }
154
155 return $errors;
156 }
157
158 /* we need to create a hidden field that contains the results of the password strength from the js strength tester */
159 add_action( 'admin_footer', 'wppb_add_hidden_password_strength_on_backend' );
160 add_action( 'login_footer', 'wppb_add_hidden_password_strength_on_backend' );
161 function wppb_add_hidden_password_strength_on_backend(){
162 if( $GLOBALS['pagenow'] == 'profile.php' || $GLOBALS['pagenow'] == 'user-new.php' || ( $GLOBALS['pagenow'] == 'wp-login.php' && isset( $_GET['action'] ) && ( $_GET['action'] === 'rp' || $_GET['action'] === 'resetpass' ) ) ){
163 $wppb_generalSettings = get_option( 'wppb_general_settings' );
164 if( !empty( $wppb_generalSettings['minimum_password_strength'] ) ){
165 ?>
166 <script type="text/javascript">
167 jQuery( document ).ready( function() {
168 var passswordStrengthResult = jQuery( '#pass-strength-result' );
169 // Check for password strength meter
170 if ( passswordStrengthResult.length ) {
171 // Attach submit event to form
172 passswordStrengthResult.parents( 'form' ).on( 'submit', function() {
173 // Store check results in hidden field
174 jQuery( this ).append( '<input type="hidden" name="wppb_password_strength" value="' + passswordStrengthResult.text() + '">' );
175 });
176 }
177 });
178 </script>
179 <?php
180 }
181 }
182 }
183
184
185 /* Modify the Add Entry buttons for WCK metaboxes according to context */
186 add_filter( 'wck_add_entry_button', 'wppb_change_add_entry_button', 10, 2 );
187 function wppb_change_add_entry_button( $string, $meta ){
188 if( $meta == 'wppb_manage_fields' || $meta == 'wppb_epf_fields' || $meta == 'wppb_rf_fields' ){
189 return __( "Add Field", 'profile-builder' );
190 }elseif( $meta == 'wppb_epf_page_settings' || $meta == 'wppb_rf_page_settings' || $meta == 'wppb_ul_page_settings' ){
191 return __( "Save Settings", 'profile-builder' );
192 }
193
194 return $string;
195 }
196
197 /**
198 * add links on plugin page
199 */
200 add_filter( 'plugin_action_links', 'wppb_plugin_action_links', 10, 2 );
201 function wppb_plugin_action_links( $links, $file ) {
202 if ( $file != WPPB_PLUGIN_BASENAME ) {
203 return $links;
204 }
205
206 if ( ! current_user_can( 'manage_options' ) ) {
207 return $links;
208 }
209
210 $settings_link = sprintf( '<a href="%1$s">%2$s</a>',
211 menu_page_url( 'profile-builder-general-settings', false ),
212 esc_html( __( 'Settings', 'profile-builder' ) ) );
213
214 array_unshift( $links, $settings_link );
215
216 return $links;
217 }
218
219 /**
220 * add links on plugin page
221 */
222 add_filter( 'plugin_row_meta', 'wppb_plugin_row_meta', 10, 2 );
223 function wppb_plugin_row_meta( $links, $file ) {
224 if ( WPPB_PLUGIN_BASENAME == $file ) {
225 $row_meta = array(
226 'docs' => '<a href="' . esc_url( 'https://www.cozmoslabs.com/docs/profile-builder-2/' ) . '" target="_blank" aria-label="' . esc_attr__( 'View Profile Builder documentation', 'profile-builder' ) . '">' . esc_html__( 'Docs', 'profile-builder' ) . '</a>',
227 );
228
229 return array_merge( $links, $row_meta );
230 }
231
232 return (array) $links;
233 }
234
235
236
237 /* In plugin notifications */
238 add_action( 'admin_init', 'wppb_add_plugin_notifications' );
239 function wppb_add_plugin_notifications() {
240 /* initiate the plugin notifications class */
241 $notifications = WPPB_Plugin_Notifications::get_instance();
242 /* this must be unique */
243 $notification_id = 'wppb_migrated_free_add_ons';
244
245 $message = '<p style="margin-top: 16px; font-size: 15px;">' . __( 'All the free add-ons have been migrated to the main plugin. Their old individual plugins have been disabled and you can delete them from your site if you were using them: <ul><li>Profile Builder - Custom CSS Classes on fields</li><li>Profile Builder - Customization Toolbox Add-On</li><li>Profile Builder - Email Confirmation Field</li><li>Profile Builder - GDPR Communication Preferences</li><li>Profile Builder - Import and Export Add-On</li><li>Profile Builder - Labels Edit Add-On</li><li>Profile Builder - Maximum Character Length Add-On</li><li>Profile Builder - Multiple Admin E-mails Add-On</li><li>Profile Builder - Placeholder Labels Add-On</li></ul>', 'profile-builder' ) . '</p>';
246 // be careful to use wppb_dismiss_admin_notification as query arg
247 $message .= '<p><a href="https://www.cozmoslabs.com/277540-profile-builder-enhancements-free-addons-now-part-of-main-plugin/" target="_blank" class="button-primary">' . __( 'See details', 'profile-builder' ) . '</a></p>';
248 $message .= '<a href="' . add_query_arg( array( 'wppb_dismiss_admin_notification' => $notification_id ) ) . '" type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss this notice.', 'profile-builder' ) . '</span></a>';
249
250 $notifications->add_notification( $notification_id, $message, 'wppb-notice notice notice-info', false );
251
252 if ( did_action( 'elementor/loaded' ) ) {
253
254 $notification_id = 'wppb_elementor_styling_notice';
255
256 $message = '<img style="float: left; margin: 10px 12px 10px 0; max-width: 100px;" src="'.WPPB_PLUGIN_URL.'assets/images/elementor_logo.png" alt="Elementor Logo"/>';
257 $message .= '<p style="margin-top: 16px; font-size: 15px;">' . sprintf( __( 'You can now style %s forms from the %s interface. To get started, add a form widget to a page through %s and go to the <strong>Style</strong> tab.', 'profile-builder' ), '<strong>Profile Builder</strong>', '<strong>Elementor</strong>', '<strong>Elementor</strong>') . '</p>';
258 // be careful to use wppb_dismiss_admin_notification as query arg
259 $message .= '<p><a href="https://www.cozmoslabs.com/docs/profile-builder-2/integration-with-elementor/" target="_blank" class="button-primary">' . __( 'See details', 'profile-builder' ) . '</a></p>';
260 $message .= '<a href="' . add_query_arg( array( 'wppb_dismiss_admin_notification' => $notification_id ) ) . '" type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss this notice.', 'profile-builder' ) . '</span></a>';
261
262 $notifications->add_notification( $notification_id, $message, 'wppb-notice notice notice-info', false );
263
264 }
265 }
266
267
268 /* hook to create pages for out forms when a user press the create pages/setup button */
269 add_action( 'admin_init', 'wppb_create_form_pages' );
270 function wppb_create_form_pages(){
271 if( isset( $_GET['page'] ) && $_GET['page'] === 'profile-builder-basic-info' && isset( $_GET['wppb_create_pages'] ) && $_GET['wppb_create_pages'] === 'true' ){
272
273 $wppb_pages_created = get_option( 'wppb_pages_created' );
274
275 if( empty( $wppb_pages_created ) || ( isset( $_GET['wppb_force_create_pages'] ) && $_GET['wppb_force_create_pages'] === 'true' ) ) {
276 $register_page = array(
277 'post_title' => 'Register',
278 'post_content' => '[wppb-register]',
279 'post_status' => 'publish',
280 'post_type' => 'page'
281 );
282 $register_id = wp_insert_post($register_page);
283
284 $edit_page = array(
285 'post_title' => 'Edit Profile',
286 'post_content' => '[wppb-edit-profile]',
287 'post_status' => 'publish',
288 'post_type' => 'page'
289 );
290 $edit_id = wp_insert_post($edit_page);
291
292 $login_page = array(
293 'post_title' => 'Log In',
294 'post_content' => '[wppb-login]',
295 'post_status' => 'publish',
296 'post_type' => 'page'
297 );
298 $login_id = wp_insert_post($login_page);
299
300 update_option('wppb_pages_created', 'true' );
301
302 wp_safe_redirect( admin_url('edit.php?s=%5Bwppb-&post_status=all&post_type=page&action=-1&m=0&paged=1&action2=-1') );
303 }
304 }
305 }
306
307 /**
308 * Function that prepares labels to pass to the WCK api...we have a quirk in wck api that labels are wrapped
309 * in %label%...so if we want to have % inside the label for example 25% off...we will have a bad time
310 * @param string $label
311 * @return string|string[]
312 */
313 function wppb_prepare_wck_labels( $label ){
314 return trim( str_replace( '%', '&#37;', $label ) );
315 }
316
317 /**
318 * Function that returns the reserved meta name list
319 */
320 function wppb_get_reserved_meta_name_list( $all_fields, $posted_values ){
321
322 $unique_meta_name_list = array( 'first_name', 'last_name', 'nickname', 'description' );
323
324 // Default contact methods were removed in WP 3.6. A filter dictates contact methods.
325 if ( apply_filters( 'wppb_remove_default_contact_methods', get_site_option( 'initial_db_version' ) < 23588 ) ){
326 $unique_meta_name_list[] = 'aim';
327 $unique_meta_name_list[] = 'yim';
328 $unique_meta_name_list[] = 'jabber';
329 }
330
331 $add_reserved = true;
332
333 $reserved_meta_names = array( 'attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page',
334 'comments_popup', 'custom', 'customize_messenger_channel', 'customized', 'cpage', 'day', 'debug', 'embed', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'map', 'minute', 'monthnum', 'more',
335 'name', 'nav_menu', 'nonce', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type',
336 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'status', 'subpost',
337 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'terms', 'theme', 'title', 'type', 'w', 'withcomments', 'withoutcomments', 'year' );
338
339 $args = array(
340 'public' => true,
341 '_builtin' => true
342 );
343 $post_types = get_post_types( $args, 'names', 'or' );
344 $taxonomies = get_taxonomies( $args, 'names', 'or' );
345
346
347 /*reserved meta names were added in PB 3.1.2 so to avoid the situation where someone updates an already existing field
348 with a reserved name and gets an error check if it is an update or new field */
349 if( !empty( $all_fields ) && !empty($posted_values['id'] ) && !empty($posted_values['meta-name']) ){
350 foreach( $all_fields as $field ){
351 if( $field['id'] === $posted_values['id'] && $field['meta-name'] === $posted_values['meta-name'] ){//it is an update
352 $add_reserved = false;
353 }
354 }
355 }
356
357 if( $add_reserved )
358 $unique_meta_name_list = array_merge( $unique_meta_name_list, $reserved_meta_names, $post_types, $taxonomies );
359
360 return apply_filters ( 'wppb_unique_meta_name_list', $unique_meta_name_list );
361 }
362