PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.54
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.54
1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 All 172 releases
userswp / admin / class-admin.php

class-admin.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.2.54, at admin/class-admin.php

1,416 lines 50.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The admin-specific functionality of the plugin.
4 *
5 * @link http://wpgeodirectory.com
6 * @since 1.0.0
7 *
8 * @package userswp
9 * @subpackage userswp/admin
10 */
11
12 /**
13 * The admin-specific functionality of the plugin.
14 *
15 * Defines the plugin name, version, and two examples hooks for how to
16 * enqueue the admin-specific stylesheet and JavaScript.
17 *
18 * @package userswp
19 * @subpackage userswp/admin
20 * @author GeoDirectory Team <info@wpgeodirectory.com>
21 */
22 class UsersWP_Admin {
23
24 /**
25 * Register all of the hooks related to the admin area functionality
26 * of the plugin.
27 *
28 * @since 1.0.0
29 */
30 public function __construct() {
31 add_action( 'admin_init', array( $this, 'activation_redirect' ) );
32 add_action( 'admin_init', array( $this, 'init_ayecode_connect_helper' ) );
33 add_action( 'admin_init', array( $this, 'prevent_admin_access' ) );
34 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
35 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
36 add_action( 'admin_notices', array( $this, 'show_update_messages' ) );
37 add_action( 'admin_footer', array( $this, 'admin_only_script' ) );
38 add_action( 'user_profile_picture_description', array( $this, 'user_profile_picture_description' ) );
39 add_action( 'admin_body_class', array( $this, 'admin_body_class' ) );
40 add_action( 'admin_init', array( $this, 'preview_emails' ) );
41
42 add_filter( 'views_users', array( $this, 'request_views_users' ) );
43 add_filter( 'pre_user_query', array( $this, 'request_users_filter' ) );
44 add_action( 'edit_user_profile', array( $this, 'get_profile_extra_edit' ), 10, 1 );
45 add_action( 'show_user_profile', array( $this, 'get_profile_extra_edit' ), 10, 1 );
46 add_filter( 'user_row_actions', array( $this, 'user_row_actions' ), 10, 2 );
47 add_action( 'bulk_actions-users', array( $this, 'users_bulk_actions' ) );
48 add_action( 'handle_bulk_actions-users', array( $this, 'handle_users_bulk_actions' ), 10, 3 );
49 add_filter( 'init', array( $this, 'process_user_actions' ) );
50 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
51 add_filter( 'aui_screen_ids', array( $this, 'add_aui_screens' ), 20, 1 );
52
53 add_action( 'admin_notices', array( $this, 'add_user_type_updated_notice' ) );
54 add_action( 'manage_users_extra_tablenav', array( $this, 'add_bulk_user_type_dropdown' ) );
55 add_action( 'admin_init', array( $this, 'handle_bulk_user_type_change' ) );
56 add_action( 'admin_footer', array( $this, 'add_validation_script' ) );
57 add_filter( 'manage_users_columns', array( $this, 'add_user_type_column' ) );
58 add_action( 'manage_users_custom_column', array( $this, 'display_user_type_column' ), 10, 3 );
59 add_action( 'edit_user_profile', array( $this, 'display_admin_change_user_type' ), 10, 1 );
60 add_action( 'show_user_profile', array( $this, 'display_admin_change_user_type' ), 10, 1 );
61 add_action( 'personal_options_update', array( $this, 'update_user_type' ) );
62 add_action( 'edit_user_profile_update', array( $this, 'update_user_type' ) );
63
64 add_action( 'wp_ajax_uwp_ajax_create_register', array( $this, 'process_create_register_form' ) );
65 add_action( 'wp_ajax_uwp_ajax_update_register', array( $this, 'process_update_register_form' ) );
66 add_action( 'wp_ajax_uwp_ajax_remove_user_type', array( $this, 'process_remove_register_form' ) );
67 add_action( 'wp_ajax_uwp_ajax_reorder_user_types', array( $this, 'reorder_user_types' ) );
68
69 // Register with the deactivation survey class.
70 if ( class_exists( 'AyeCode_Deactivation_Survey' ) ) {
71 AyeCode_Deactivation_Survey::instance(
72 array(
73 'slug' => 'userswp',
74 'version' => USERSWP_VERSION,
75 'support_url' => 'https://userswp.io/support/',
76 'documentation_url' => 'https://userswp.io/documentation/',
77 'activated' => get_option( 'uwp_installed_on', 0 ),
78 )
79 );
80 }
81 }
82
83 /**
84 * Redirects to UsersWP info page after plugin activation.
85 *
86 * @return void
87 * @package userswp
88 * @since 1.0.0
89 */
90 public function activation_redirect() {
91 if ( get_option( 'uwp_activation_redirect', false ) ) {
92 delete_option( 'uwp_activation_redirect' );
93 update_option( 'uwp_setup_wizard_notice', 1 );
94 wp_redirect( admin_url( 'index.php?page=uwp-setup' ) );
95 exit;
96 }
97
98 if ( ! empty( $_GET['force_sync_data'] ) && ! empty( $_GET['_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_GET['_nonce'] ), 'force_sync_data' ) && current_user_can( 'manage_options' ) ) {
99 $blog_id = get_current_blog_id();
100 do_action( 'wp_' . $blog_id . '_uwp_updater_cron' );
101 wp_safe_redirect( admin_url( 'admin.php?page=userswp' ) );
102 exit;
103 }
104 }
105
106 /**
107 * Maybe show the AyeCode Connect Notice.
108 */
109 public function init_ayecode_connect_helper() {
110 // AyeCode Connect notice
111 if ( is_admin() ) {
112 // set the strings so they can be translated
113 $strings = array(
114 'connect_title' => __( 'UsersWP - an AyeCode product!', 'userswp' ),
115 'connect_external' => __( 'Please confirm you wish to connect your site?', 'userswp' ),
116 'connect' => sprintf( __( '<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %1$slearn more%2$s', 'userswp' ), "<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>", '</a>' ),
117 'connect_button' => __( 'Connect Site', 'userswp' ),
118 'connecting_button' => __( 'Connecting...', 'userswp' ),
119 'error_localhost' => __( 'This service will only work with a live domain, not a localhost.', 'userswp' ),
120 'error' => __( 'Something went wrong, please refresh and try again.', 'userswp' ),
121 );
122 new AyeCode_Connect_Helper( $strings, array( 'uwp-addons' ) );
123 }
124 }
125
126 /**
127 * Restrict the wp-admin area from specific user roles if set to do so.
128 */
129 public function prevent_admin_access() {
130 $restricted_roles = (array) uwp_get_option( 'admin_blocked_roles', array() );
131
132 // Checking action in request to allow ajax request go through
133 if ( ! empty( $restricted_roles ) && is_user_logged_in() && ! wp_doing_ajax() && ! wp_doing_cron() ) {
134 $action = ! empty( $_POST['action'] ) ? sanitize_text_field( wp_unslash( $_POST['action'] ) ) : '';
135
136 /*
137 * Prevent conflicts with MailPoet plugin.
138 */
139 if ( in_array( $action, array( 'mailpoet_subscription_update' ) ) ) {
140 return;
141 }
142
143 $roles = wp_get_current_user()->roles;
144
145 $prevent = false;
146
147 // Always allow administrator role.
148 if ( ! ( ! empty( $roles ) && in_array( 'administrator', $roles ) ) ) {
149 foreach ( $restricted_roles as $role ) {
150 if ( in_array( $role, $roles ) ) {
151 $prevent = true;
152 break;
153 }
154 }
155 }
156
157 /*
158 * Check and prevent admin access based on user role.
159 *
160 * @param bool $prevent True to prevent admin access.
161 */
162 $prevent = apply_filters( 'uwp_prevent_wp_admin_access', $prevent );
163
164 if ( $prevent ) {
165 wp_safe_redirect( home_url() );
166 exit;
167 }
168 }
169 }
170
171 /**
172 * Register the stylesheets for the admin area.
173 *
174 * @param $hook_suffix
175 *
176 * @since 1.0.0
177 */
178 public function enqueue_styles( $hook_suffix ) {
179 $screen = get_current_screen();
180 $screen_id = $screen ? $screen->id : '';
181
182 if ( $hook_suffix == 'profile.php' || $hook_suffix == 'user-edit.php' ) {
183 wp_register_style( 'jquery-ui', USERSWP_PLUGIN_URL . 'assets/css/jquery-ui.css' );
184 wp_enqueue_style( 'jquery-ui' );
185 wp_enqueue_style( 'jcrop' );
186 wp_enqueue_style( 'uwp-country-select', USERSWP_PLUGIN_URL . 'assets/css/countryselect.css', array(), USERSWP_VERSION, 'all' );
187 wp_enqueue_style( 'userswp', USERSWP_PLUGIN_URL . 'assets/css/users-wp.css', array(), USERSWP_VERSION, 'all' );
188 wp_enqueue_style( 'uwp_timepicker_css', USERSWP_PLUGIN_URL . 'assets/css/jquery.ui.timepicker.css', array(), USERSWP_VERSION, 'all' );
189 }
190
191 if ( false !== strpos( $hook_suffix, 'page_uwp_tools' ) ) {
192 wp_enqueue_style( 'userswp', USERSWP_PLUGIN_URL . 'assets/css/users-wp.css', array(), USERSWP_VERSION, 'all' );
193 }
194
195 if ( false !== strpos( $hook_suffix, 'page_uwp_form_builder' ) ) {
196 wp_enqueue_style( 'uwp_form_builder', USERSWP_PLUGIN_URL . 'admin/assets/css/uwp-form-builder.css', array(), USERSWP_VERSION, 'all' );
197 }
198
199 if ( $hook_suffix == 'toplevel_page_userswp' ) {
200 wp_enqueue_style( 'wp-color-picker' );
201 }
202
203 if ( in_array( $screen_id, uwp_get_screen_ids() ) ) {
204 wp_enqueue_style( 'userswp_admin_css', USERSWP_PLUGIN_URL . 'admin/assets/css/users-wp-admin.css', array(), USERSWP_VERSION, 'all' );
205 }
206 }
207
208 /**
209 * Register the JavaScript for the admin area.
210 *
211 * @param $hook_suffix
212 *
213 * @since 1.0.0
214 */
215 public function enqueue_scripts( $hook_suffix ) {
216
217 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
218 $screen = get_current_screen();
219 $screen_id = $screen ? $screen->id : '';
220
221 if ( $hook_suffix == 'profile.php' || $hook_suffix == 'user-edit.php' ) {
222
223 wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) );
224 wp_enqueue_script(
225 'uwp_timepicker',
226 USERSWP_PLUGIN_URL . 'assets/js/jquery.ui.timepicker.min.js',
227 array(
228 'jquery',
229 'jquery-ui-datepicker',
230 'jquery-ui-core',
231 ),
232 USERSWP_VERSION
233 );
234 wp_enqueue_script( 'userswp', USERSWP_PLUGIN_URL . 'assets/js/users-wp' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION, false );
235 $uwp_localize_data = uwp_get_localize_data();
236 wp_localize_script( 'userswp', 'uwp_localize_data', $uwp_localize_data );
237 wp_enqueue_script( 'jquery-ui-progressbar', array( 'jquery' ) );
238 wp_enqueue_script( 'jcrop', array( 'jquery' ) );
239 wp_enqueue_script( 'country-select', USERSWP_PLUGIN_URL . 'assets/js/countrySelect' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION );
240
241 $country_data = uwp_get_country_data();
242 wp_localize_script( USERSWP_NAME, 'uwp_country_data', $country_data );
243 }
244
245 if ( false !== strpos( $hook_suffix, 'page_uwp_tools' ) ) {
246 wp_enqueue_script( 'jquery-ui-progressbar', array( 'jquery' ) );
247 }
248
249 if ( false !== strpos( $hook_suffix, 'page_uwp_status' ) ) {
250 wp_enqueue_script( 'uwp_status', USERSWP_PLUGIN_URL . 'admin/assets/js/system-status.js', array( 'jquery' ), USERSWP_VERSION, true );
251 }
252
253 if ( false !== strpos( $hook_suffix, 'page_uwp_form_builder' ) ) {
254 wp_enqueue_script( 'jquery-ui-sortable' );
255 wp_enqueue_script( 'uwp-nestable-script', USERSWP_PLUGIN_URL . 'admin/assets/js/jquery.nestable' . $suffix . '.js', array( 'jquery-ui-sortable' ), USERSWP_VERSION );
256 wp_enqueue_script( 'uwp_form_builder', USERSWP_PLUGIN_URL . 'admin/assets/js/uwp-form-builder' . $suffix . '.js', array(), USERSWP_VERSION, 'all' );
257 }
258
259 if ( in_array( $screen_id, uwp_get_screen_ids() ) ) {
260 wp_enqueue_script( 'userswp_admin', USERSWP_PLUGIN_URL . 'admin/assets/js/users-wp-admin' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION, false );
261
262 wp_enqueue_script( 'jquery-ui-tooltip' );
263 wp_enqueue_script( 'wp-color-picker' );
264
265 if ( 'userswp_page_uwp_user_types' === $screen_id ) {
266 wp_enqueue_script( 'jquery-ui-sortable' );
267 }
268
269 $ajax_cons_data = array(
270 'url' => admin_url( 'admin-ajax.php' ),
271 'nonces' => array(
272 'uwp_delete_user_type' => wp_create_nonce( 'uwp_delete_user_types' ),
273 'uwp_reorder_user_types' => wp_create_nonce( 'uwp_reorder_user_types' ),
274 ),
275 'custom_field_not_blank_var' => __( 'Field key must not be blank', 'userswp' ),
276 'custom_field_options_not_blank_var' => __( 'Option Values must not be blank', 'userswp' ),
277 'custom_field_not_special_char' => __( 'Please do not use special character and spaces in field key.', 'userswp' ),
278 'custom_field_unique_name' => __( 'Field key should be a unique name.', 'userswp' ),
279 'custom_field_delete' => __( 'Are you sure you wish to delete this field?', 'userswp' ),
280 'custom_field_id_required' => __( 'This field is required.', 'userswp' ),
281 'img_spacer' => admin_url( 'images/media-button-image.gif' ),
282 'txt_choose_image' => __( 'Choose an image', 'userswp' ),
283 'txt_use_image' => __( 'Use image', 'userswp' ),
284 'txt_save' => __( 'Save', 'userswp' ),
285 'txt_saved' => __( 'Saved', 'userswp' ),
286 'txt_delete' => __( 'Delete', 'userswp' ),
287 'txt_deleted' => __( 'Deleted', 'userswp' ),
288 'txt_cancel' => __( 'Cancel', 'userswp' ),
289 'txt_saving' => __( 'Saving...', 'userswp' ),
290 'txt_saving_error' => __( 'An error occurred while saving. Please try again.', 'userswp' ),
291 'delete_register_form' => __( 'Are you sure you wish to delete this form?', 'userswp' ),
292 'ask_register_form_title' => __( 'Enter register form title', 'userswp' ),
293 'form_updated_msg' => __( 'Updated! Reloading page...', 'userswp' ),
294 );
295
296 wp_localize_script( 'userswp_admin', 'uwp_admin_ajax', $ajax_cons_data );
297 }
298 }
299
300 public function get_screen_ids() {
301 return uwp_get_screen_ids();
302 }
303
304 /**
305 * Displays update messages
306 */
307 public function show_update_messages() {
308 if ( ! isset( $_REQUEST['update'] ) ) {
309 return;
310 }
311
312 $update = sanitize_text_field( $_REQUEST['update'] );
313 $messages = array();
314
315 switch ( $update ) {
316 case 'uwp_resend':
317 $messages['msg'] = __( 'Activation email has been sent!', 'userswp' );
318 break;
319 case 'err_uwp_resend':
320 $messages['err_msg'] = __( 'Error while sending activation email. Please try again.', 'userswp' );
321 break;
322 case 'uwp_activate_user':
323 $messages['msg'] = __( 'User(s) has been activated!', 'userswp' );
324 break;
325 }
326
327 if ( ! empty( $messages ) ) {
328 if ( isset( $messages['err_msg'] ) ) {
329 echo '<div class="notice notice-error"><p>' . esc_html( $messages['err_msg'] ) . '</p></div>';
330 } else {
331 echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $messages['msg'] ) . '</p></div>';
332 }
333 }
334 }
335
336 /**
337 * Adds UsersWP JS to admin area.
338 *
339 * @return void
340 * @package userswp
341 *
342 * @since 1.0.0
343 */
344 public function admin_only_script() {
345
346 // check page is userswp or not.
347 if ( ! empty( $_GET['page'] ) && 'userswp' == $_GET['page'] ) {
348
349 // check tab is general or not.
350 if ( ! empty( $_GET['tab'] ) && 'general' == $_GET['tab'] ) {
351
352 // check for login section.
353 if ( ! empty( $_GET['section'] ) && 'login' == $_GET['section'] ) {
354 ?>
355 <script type="text/javascript">
356 jQuery(document).ready(function () {
357 var login_redirect_to = jQuery('#login_redirect_to');
358 login_redirect_to.on('change', function () {
359 var value = jQuery(this).val();
360 var login_redirect_custom_obj = jQuery('#login_redirect_custom_url');
361
362 if ('-2' === value) {
363 login_redirect_custom_obj.parent().parent().show();
364 } else {
365 login_redirect_custom_obj.parent().parent().hide();
366 }
367 }).change();
368 });
369 </script>
370 <?php
371 }
372 }
373 }
374
375 if ( ! empty( $_GET['page'] ) && 'uwp_user_types' == $_GET['page'] ) {
376 ?>
377 <script type="text/javascript">
378 jQuery(document).ready(function () {
379 var uwp_registration_action = jQuery('#uwp_registration_action');
380 uwp_registration_action.on('change', function () {
381 var value = jQuery(this).val();
382 var register_redirect_obj = jQuery('#register_redirect_to');
383
384 register_redirect_obj.parents('tr').toggle(value === 'auto_approve_login');
385 }).change();
386
387 var register_redirect_to = jQuery('#register_redirect_to');
388 register_redirect_to.on('change', function () {
389 var value = jQuery(this).val();
390 var register_redirect_custom_obj = jQuery('#register_redirect_custom_url');
391 register_redirect_custom_obj.parents('tr').toggle(value === '-2');
392 }).change();
393 });
394 </script>
395 <?php
396 }
397 }
398
399 /**
400 * Filters the user profile picture description displayed under the Gravatar.
401 *
402 * @param string $description Profile picture description.
403 *
404 * @return string Modified description.
405 * @since 1.0.0
406 * @package userswp
407 *
408 */
409 public function user_profile_picture_description( $description ) {
410 if ( is_admin() && IS_PROFILE_PAGE ) {
411 $user_id = get_current_user_id();
412 $avatar = uwp_get_usermeta( $user_id, 'avatar_thumb', '' );
413
414 if ( ! empty( $avatar ) ) {
415 $description = sprintf(
416 __( 'You can change your profile picture on your <a href="%s">Profile Page</a>.', 'userswp' ),
417 uwp_build_profile_tab_url( $user_id )
418 );
419 }
420 }
421
422 return $description;
423 }
424
425 public function admin_body_class( $classes ) {
426 $screen = get_current_screen();
427 if ( 'profile' == $screen->base || 'user-edit' == $screen->base ) {
428 $classes .= ' uwp_page';
429 }
430
431 // Add original UsersWP page class when UsersWP screen is translated.
432 if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], array( 'uwp_form_builder', 'uwp_tools', 'uwp_status', 'uwp-addons' ) ) ) {
433 $uwp_screen_id = sanitize_title( __( 'UsersWP', 'userswp' ) );
434
435 if ( $uwp_screen_id != 'userswp' ) {
436 $classes .= ' userswp_page_' . esc_attr( sanitize_text_field( $_GET['page'] ) );
437 }
438 }
439
440 // Add body class for admin pages.
441 $screen = get_current_screen();
442 $screen_id = $screen ? $screen->id : '';
443
444 if ( $screen_id && in_array( $screen_id, uwp_get_screen_ids() ) ) {
445 $classes .= ' uwp-admin-page uwp-admin-page-' . sanitize_key( $screen_id );
446 }
447
448 return $classes;
449 }
450
451 /**
452 * Preview email template for UWP emails.
453 */
454 public function preview_emails() {
455 if ( isset( $_GET['uwp_preview_mail'] ) ) {
456 if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'uwp-preview-mail' ) ) {
457 die( 'Security check failed.' );
458 }
459
460 $email_name = 'preview_mail';
461 $email_vars = array();
462 $plain_text = UsersWP_Mails::get_email_type() != 'html' ? true : false;
463
464 // Get the preview email content.
465 ob_start();
466 include 'views/html-email-template-preview.php';
467 $message = ob_get_clean();
468
469 $message = UsersWP_Mails::email_wrap_message( $message, $email_name, $email_vars, '', $plain_text );
470 $message = UsersWP_Mails::style_body( $message, $email_name, $email_vars );
471 $message = apply_filters( 'uwp_mail_content', $message, $email_name, $email_vars );
472
473 // Print the preview email content.
474 if ( $plain_text ) {
475 echo '<div style="white-space:pre-wrap;font-family:sans-serif">';
476 }
477 echo $message; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
478 if ( $plain_text ) {
479 echo '</div>';
480 }
481 exit;
482 }
483 }
484
485 /**
486 * Returns user views filter
487 *
488 * @return array User views.
489 */
490 public function request_views_users( $views ) {
491
492 $current = '';
493
494 if ( isset( $_REQUEST['uwp_status'] ) && $_REQUEST['uwp_status'] == 'pending-email-activate' ) {
495 $views['all'] = str_replace( 'current', '', $views['all'] );
496 $current = 'current';
497 }
498
499 $views['pending-email-activate'] = '<a href="' . admin_url( 'users.php' ) . '?uwp_status=pending-email-activate" class="' . $current . '">' . __( 'Pending Email Activation', 'userswp' ) . ' <span class="count">(' . $this->uwp_pending_email_count() . ')</span></a>';
500
501 return $views;
502 }
503
504 /**
505 * Returns pending email activation user counts
506 *
507 * @return int User count
508 */
509 public function uwp_pending_email_count() {
510
511 $args = array(
512 'fields' => 'ID',
513 'number' => 0,
514 'meta_query' => array(
515 array(
516 'key' => 'uwp_mod',
517 'value' => 'email_unconfirmed',
518 'compare' => '=',
519 ),
520 ),
521 );
522 $users = new WP_User_Query( $args );
523
524 return isset( $users->results ) ? (int) count( $users->results ) : 0;
525 }
526
527 /**
528 * Filter to modify the user query
529 *
530 * @return object User query.
531 */
532 public function request_users_filter( $query ) {
533
534 global $wpdb, $pagenow;
535
536 if ( is_admin() && $pagenow == 'users.php' && isset( $_GET['uwp_status'] ) && $_GET['uwp_status'] != '' ) {
537
538 do_action( 'uwp_users_show_pending_email_activation', $query );
539
540 $status = sanitize_text_field( urldecode( $_GET['uwp_status'] ) );
541
542 if ( $status == 'pending-email-activate' ) {
543 $query->query_where = str_replace(
544 'WHERE 1=1',
545 "WHERE 1=1 AND {$wpdb->users}.ID IN (
546 SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
547 WHERE {$wpdb->usermeta}.meta_key = 'uwp_mod'
548 AND {$wpdb->usermeta}.meta_value = 'email_unconfirmed')",
549 $query->query_where
550 );
551 }
552 }
553
554 return $query;
555 }
556
557 /**
558 * Gets UsersWP fields in WP-Admin Users Edit page.
559 *
560 * @param object $user User Object.
561 *
562 * @package userswp
563 * @since 1.0.0
564 */
565 public function get_profile_extra_edit( $user ) {
566 global $wpdb;
567 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
568 $form_id = uwp_get_register_form_id( $user->ID );
569 $excluded_fields = apply_filters( 'uwp_exclude_edit_profile_fields', array( 'bio', 'register_gdpr', 'register_tos', 'uwp_language', 'multisite_site_title', 'multisite_site_address' ) );
570 $query = $wpdb->prepare( 'SELECT * FROM ' . $table_name . " WHERE form_type = 'account' AND is_default = '0' AND form_id = %d", $form_id );
571 if ( is_array( $excluded_fields ) && count( $excluded_fields ) > 0 ) {
572 $query .= " AND htmlvar_name NOT IN ('" . implode( "','", $excluded_fields ) . "')";
573 }
574 $query .= ' ORDER BY sort_order ASC';
575 $fields = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
576
577 $this->edit_profile_banner_fields( $user ); // Displays avatar and banner fields
578
579 if ( $fields ) {
580 ?>
581 <div class="uwp-profile-extra">
582 <table class="uwp-profile-extra-table form-table">
583 <?php
584 foreach ( $fields as $field ) {
585
586 if ( isset( $field->css_class ) && ! empty( $field->css_class ) ) {
587 $field->css_class = $field->css_class . ' w-50';
588 } else {
589 $field->css_class = 'w-50';
590 }
591
592 if ( $field->field_type == 'fieldset' ) {
593 ?>
594 <tr>
595 <th class="uwp-profile-extra-key">
596 <h2><?php echo esc_html( $field->site_title ); ?></h2>
597 </th>
598 <td></td>
599 </tr>
600 <?php
601 } else {
602 ?>
603 <tr>
604 <th class="uwp-profile-extra-key"><?php echo esc_html( $field->site_title ); ?></th>
605 <td class="uwp-profile-extra-value bsui">
606 <?php
607 $templates_obj = new UsersWP_Templates();
608 $templates_obj->template_fields_html( $field, 'account', $user->ID );
609 ?>
610 </td>
611 </tr>
612 <?php
613 }
614 }
615 ?>
616 </table>
617 </div>
618 <?php
619 }
620 }
621
622 /**
623 * Adds avatar and banner fields in admin side.
624 *
625 * @param object $user User object.
626 *
627 * @return void
628 * @since 1.0.0
629 * @package userswp
630 *
631 */
632 public function edit_profile_banner_fields( $user ) {
633 global $wpdb;
634
635 $file_obj = new UsersWP_Files();
636
637 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
638 $fields = $wpdb->get_results( 'SELECT * FROM ' . $table_name . " WHERE (form_type = 'avatar' OR form_type = 'banner') ORDER BY sort_order ASC" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
639 if ( $fields ) {
640 ?>
641 <div class="uwp-profile-extra uwp_page">
642 <?php do_action( 'uwp_admin_profile_edit', $user ); ?>
643 <h2><?php esc_html_e( 'UsersWP Account Details', 'userswp' ); ?></h2>
644 <table class="uwp-profile-extra-table form-table">
645 <?php
646 foreach ( $fields as $field ) {
647
648 if ( $field->field_type == 'fieldset' ) {
649 ?>
650 <tr>
651 <th class="uwp-profile-extra-key">
652 <h2><?php echo esc_html( $field->site_title ); ?></h2>
653 </th>
654 <td></td>
655 </tr>
656 <?php
657 } else {
658 ?>
659 <tr>
660 <th class="uwp-profile-extra-key"><?php echo esc_html( $field->site_title ); ?></th>
661 <td class="uwp-profile-extra-value bsui">
662 <?php
663 if ( $field->htmlvar_name == 'avatar' ) {
664 $value = uwp_get_usermeta( $user->ID, 'avatar_thumb', '' );
665 } elseif ( $field->htmlvar_name == 'banner' ) {
666 $value = uwp_get_usermeta( $user->ID, 'banner_thumb', '' );
667 } else {
668 $value = '';
669 }
670
671 echo $file_obj->file_upload_preview( $field, $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
672
673 if ( $field->htmlvar_name == 'avatar' ) {
674 if ( ! empty( $value ) ) {
675 $label = __( 'Change Avatar', 'userswp' );
676 } else {
677 $label = __( 'Upload Avatar', 'userswp' );
678 }
679 ?>
680 <a onclick="uwp_profile_image_change('avatar');return false;" href="#"
681 class="uwp-banner-change-icon-admin">
682 <?php echo esc_html( $label ); ?>
683 </a>
684 <?php
685 } elseif ( $field->htmlvar_name == 'banner' ) {
686 if ( ! empty( $value ) ) {
687 $label = __( 'Change Banner', 'userswp' );
688 } else {
689 $label = __( 'Upload Banner', 'userswp' );
690 }
691 ?>
692 <a onclick="uwp_profile_image_change('banner');return false;" href="#"
693 class="uwp-banner-change-icon-admin">
694 <?php echo esc_html( $label ); ?>
695 </a>
696 <?php
697 }
698 ?>
699 </td>
700 </tr>
701 <?php
702 }
703 }
704 ?>
705 </table>
706 </div>
707 <?php
708 }
709 }
710
711 /**
712 * Returns user row actions
713 *
714 * @param array $actions Date string.
715 * @param object $user_object The User ID.
716 *
717 * @return array Row actions.
718 * @package userswp
719 *
720 */
721 public function user_row_actions( $actions, $user_object ) {
722 $user_id = $user_object->ID;
723 $mod_value = get_user_meta( $user_id, 'uwp_mod', true );
724 $resend_link = add_query_arg(
725 array(
726 'user_id' => $user_id,
727 'action' => 'uwp_resend',
728 '_nonce' => wp_create_nonce( 'uwp_resend' ),
729 'uwp_is_admin' => 1,
730 ),
731 admin_url( 'users.php' )
732 );
733
734 $activate_link = add_query_arg(
735 array(
736 'user_id' => $user_id,
737 'action' => 'uwp_activate_user',
738 '_nonce' => wp_create_nonce( 'uwp_activate_user' ),
739 'uwp_is_admin' => 1,
740 ),
741 admin_url( 'users.php' )
742 );
743
744 if ( $mod_value == 'email_unconfirmed' ) {
745 $actions['uwp_resend_activation'] = "<a class='' href='" . $resend_link . "'>" . __( 'Resend Activation', 'userswp' ) . '</a>';
746 $actions['uwp_auto_activate'] = "<a class='' href='" . $activate_link . "'>" . __( 'Activate User', 'userswp' ) . '</a>';
747 }
748
749 return $actions;
750 }
751
752 /**
753 * Returns users bulk actions
754 *
755 * @param array $bulk_actions Bulk actions.
756 *
757 * @return array Bulk actions.
758 * @package userswp
759 *
760 */
761 public function users_bulk_actions( $bulk_actions ) {
762 $bulk_actions['uwp_resend'] = __( 'Resend Activation', 'userswp' );
763 $bulk_actions['uwp_activate_user'] = __( 'Activate Users', 'userswp' );
764
765 return $bulk_actions;
766 }
767
768 /**
769 * Handles users bulk actions
770 *
771 * @param string $redirect_to Bulk actions.
772 * @param string $doaction Current action.
773 * @param array $user_ids User IDs to process.
774 *
775 * @return string Redirect URL.
776 * @package userswp
777 *
778 */
779 public function handle_users_bulk_actions( $redirect_to, $doaction, $user_ids ) {
780 if ( 'uwp_resend' == $doaction ) {
781 foreach ( $user_ids as $user_id ) {
782 uwp_resend_activation_mail( $user_id );
783 }
784
785 $redirect_to = add_query_arg( 'update', 'uwp_resend', $redirect_to );
786 } elseif ( 'uwp_activate_user' == $doaction ) {
787 foreach ( $user_ids as $user_id ) {
788 $this->activate_user( $user_id );
789 }
790 $redirect_to = add_query_arg( 'update', 'uwp_activate_user', $redirect_to );
791 }
792
793 return $redirect_to;
794 }
795
796 /**
797 * Activates user
798 *
799 * @param int $user_id User ID
800 *
801 * @return bool
802 */
803 public function activate_user( $user_id = 0 ) {
804 if ( ! $user_id ) {
805 return false;
806 }
807
808 $uwp_mode = get_user_meta( $user_id, 'uwp_mod', true );
809 if ( 'email_unconfirmed' == $uwp_mode ) {
810 delete_user_meta( $user_id, 'uwp_mod' );
811 do_action( 'uwp_email_activation_success', $user_id );
812 }
813
814 return true;
815 }
816
817 /**
818 * Processes user action
819 *
820 * @return mixed
821 * @package userswp
822 *
823 */
824 public function process_user_actions() {
825 $user_id = isset( $_REQUEST['user_id'] ) ? (int) $_REQUEST['user_id'] : 0;
826 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : false;
827 $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( $_REQUEST['_nonce'] ) : false;
828 $is_admin = isset( $_REQUEST['uwp_is_admin'] ) ? sanitize_text_field( $_REQUEST['uwp_is_admin'] ) : false;
829
830 if ( $user_id && 'uwp_resend' == $action && ! empty( $nonce ) && wp_verify_nonce( $nonce, 'uwp_resend' ) ) {
831 uwp_resend_activation_mail( $user_id );
832 if ( isset( $is_admin ) && $is_admin ) {
833 wp_redirect( add_query_arg( 'update', 'uwp_resend', admin_url( 'users.php' ) ) );
834 exit();
835 } else {
836 global $uwp_notices;
837 $message = __( 'Activation email has been sent!', 'userswp' );
838 $uwp_notices[] = aui()->alert(
839 array(
840 'type' => 'success',
841 'content' => $message,
842 )
843 );
844 }
845 } elseif ( $user_id && 'uwp_activate_user' == $action && wp_verify_nonce( $nonce, 'uwp_activate_user' ) ) {
846 if ( isset( $is_admin ) && $is_admin && current_user_can( 'edit_users' ) ) {
847 $this->activate_user( $user_id );
848 wp_redirect( add_query_arg( 'update', 'uwp_activate_user', admin_url( 'users.php' ) ) );
849 }
850 }
851 }
852
853 /**
854 * Show row meta on the plugin screen.
855 *
856 * @param mixed $links Plugin Row Meta
857 * @param mixed $file Plugin Base file
858 * @return array
859 */
860 public static function plugin_row_meta( $links, $file ) {
861 if ( USERSWP_PLUGIN_BASENAME == $file ) {
862 $row_meta = array(
863 'docs' => '<a href="' . esc_url( 'https://userswp.io/documentation/' ) . '" aria-label="' . esc_attr__( 'View UsersWP Documentation', 'userswp' ) . '">' . esc_html__( 'Docs', 'userswp' ) . '</a>',
864 'support' => '<a href="' . esc_url( 'https://userswp.io/support/' ) . '" aria-label="' . esc_attr__( 'Visit UsersWP support', 'userswp' ) . '">' . esc_html__( 'Support', 'userswp' ) . '</a>',
865 'translation' => '<a href="' . esc_url( 'https://userswp.io/translate/projects' ) . '" aria-label="' . esc_attr__( 'View translations', 'userswp' ) . '">' . esc_html__( 'Translations', 'userswp' ) . '</a>',
866 );
867
868 return array_merge( $links, $row_meta );
869 }
870
871 return (array) $links;
872 }
873
874 /**
875 * Process the creation of a new registration form.
876 *
877 * This method handles the AJAX request to create a new registration form,
878 * including validation, sanitization, and database operations.
879 */
880 public function process_create_register_form() {
881 check_ajax_referer( 'uwp-create-register-form-nonce', 'uwp_create_register_form_nonce' );
882
883 if ( ! current_user_can( 'manage_options' ) ) {
884 wp_die( -1 );
885 }
886
887 $form_title = isset( $_POST['form_title'] ) ? sanitize_text_field( $_POST['form_title'] ) : __( 'Form', 'userswp' );
888 $user_role = isset( $_POST['user_role'] ) ? sanitize_text_field( $_POST['user_role'] ) : '';
889 $action = isset( $_POST['reg_action'] ) ? sanitize_text_field( $_POST['reg_action'] ) : uwp_get_option( 'uwp_registration_action', 'auto_approve' );
890 $redirect_to = isset( $_POST['redirect_to'] ) ? (int)$_POST['redirect_to'] : 0;
891 $custom_url = isset( $_POST['custom_url'] ) ? sanitize_text_field( $_POST['custom_url'] ) : '';
892 $gdpr_page = isset( $_POST['gdpr_page'] ) ? (int)$_POST['gdpr_page'] : (int)uwp_get_option( 'register_gdpr_page', false );
893 $tos_page = isset( $_POST['tos_page'] ) ? (int)$_POST['tos_page'] : (int)uwp_get_option( 'register_terms_page', false );
894
895 if ( empty( $form_title ) ) {
896 wp_send_json_error(
897 array(
898 'message' => esc_html__( 'Form title is required.', 'userswp' ),
899 )
900 );
901 }
902
903 $status = false;
904 $redirect = '';
905
906 $new_form_id = uwp_get_next_register_form_id();
907 if ( $new_form_id ) {
908 $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
909 // Filter out invalid items.
910 $register_forms = array_filter(
911 $register_forms,
912 function ( $form ) {
913 return isset( $form['id'] );
914 }
915 );
916
917 $register_forms[] = array(
918 'id' => $new_form_id,
919 'title' => ! empty( $form_title ) ? $form_title : sprintf( __( 'Form %d', 'userswp' ), $new_form_id ),
920 'slug' => ! empty( $form_title ) ? sanitize_title( $form_title ) : sprintf( 'form-%d', $new_form_id ),
921 'user_role' => $user_role,
922 'reg_action' => $action,
923 'redirect_to' => $redirect_to,
924 'custom_url' => $custom_url,
925 'gdpr_page' => $gdpr_page,
926 'tos_page' => $tos_page,
927 );
928
929 $register_forms = array_values( $register_forms );
930 $register_forms = apply_filters( 'uwp_multiple_registration_forms_update', $register_forms, $new_form_id );
931
932 uwp_update_option( 'multiple_registration_forms', $register_forms );
933
934 $fields = UsersWP_Activator::uwp_default_custom_fields_account();
935 $form_builder = new UsersWP_Form_Builder();
936
937 foreach ( $fields as $field ) {
938 $field['form_id'] = $new_form_id;
939 $form_builder->admin_form_field_save( $field );
940 }
941
942 UsersWP_Activator::insert_form_extras( $new_form_id );
943
944 do_action( 'uwp_create_register_form', $new_form_id );
945
946 $status = true;
947 $redirect = admin_url( sprintf( 'admin.php?page=uwp_user_types&form=%d', $new_form_id ) );
948 }
949
950 wp_send_json_success(
951 array(
952 'status' => $status,
953 'message' => esc_html__( 'User Type added successfully.', 'userswp' ),
954 'redirect' => $redirect,
955 )
956 );
957 }
958
959 /**
960 * Process the update of an existing registration form.
961 *
962 * This method handles the AJAX request to update an existing registration form,
963 * including validation, sanitization, and database operations.
964 */
965 public function process_update_register_form() {
966 check_ajax_referer( 'uwp-update-register-form-nonce', 'uwp_update_register_form_nonce' );
967
968 if ( ! current_user_can( 'manage_options' ) ) {
969 wp_die( -1 );
970 }
971
972 $form_id = isset( $_POST['manage_field_form_id'] ) ? (int)$_POST['manage_field_form_id'] : 0;
973 $form_title = isset( $_POST['form_title'] ) && ! empty( $_POST['form_title'] ) ? sanitize_text_field( $_POST['form_title'] ) : __( 'Form', 'userswp' );
974 $user_role = isset( $_POST['user_role'] ) ? sanitize_text_field( $_POST['user_role'] ) : '';
975 $action = isset( $_POST['reg_action'] ) ? sanitize_text_field( $_POST['reg_action'] ) : uwp_get_option( 'uwp_registration_action', 'auto_approve' );
976 $redirect_to = isset( $_POST['redirect_to'] ) ? (int)$_POST['redirect_to'] : 0;
977 $custom_url = isset( $_POST['custom_url'] ) ? sanitize_text_field( $_POST['custom_url'] ) : '';
978 $gdpr_page = isset( $_POST['gdpr_page'] ) ? (int)$_POST['gdpr_page'] : (int)uwp_get_option( 'register_gdpr_page', false );
979 $tos_page = isset( $_POST['tos_page'] ) ? (int)$_POST['tos_page'] : (int)uwp_get_option( 'register_terms_page', false );
980
981 if ( ! $form_id ) {
982 wp_send_json_error(
983 array(
984 'message' => esc_html__( 'Something went wrong. Please try again.', 'userswp' ),
985 )
986 );
987 }
988
989 $redirect = '';
990 $status = false;
991
992 $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
993
994 foreach ( $register_forms as &$register_form ) {
995 if ( $register_form['id'] == $form_id ) {
996 $register_form['title'] = $form_title;
997 $register_form['user_role'] = $user_role;
998 $register_form['reg_action'] = $action;
999 $register_form['redirect_to'] = $redirect_to;
1000 $register_form['custom_url'] = $custom_url;
1001 $register_form['gdpr_page'] = $gdpr_page;
1002 $register_form['tos_page'] = $tos_page;
1003 $status = true;
1004 break;
1005 }
1006 }
1007
1008 if ( $status ) {
1009 $register_forms = array_values( $register_forms );
1010 $register_forms = apply_filters( 'uwp_multiple_registration_forms_update', $register_forms, $form_id );
1011 uwp_update_option( 'multiple_registration_forms', $register_forms );
1012 $redirect = add_query_arg(
1013 array(
1014 'page' => 'uwp_user_types',
1015 'form' => $form_id,
1016 ),
1017 admin_url( 'admin.php' )
1018 );
1019 }
1020
1021 wp_send_json_success(
1022 array(
1023 'status' => $status,
1024 'message' => esc_html__( 'The user type has been updated successfully.', 'userswp' ),
1025 )
1026 );
1027 }
1028
1029 public function reorder_user_types() {
1030 check_ajax_referer( 'uwp_reorder_user_types', 'nonce' );
1031
1032 if ( ! current_user_can( 'manage_options' ) ) {
1033 wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'userswp' ) ) );
1034 }
1035
1036 $order = isset( $_POST['order'] ) ? array_map( 'absint', $_POST['order'] ) : array();
1037
1038 if ( empty( $order ) ) {
1039 wp_send_json_error( array( 'message' => __( 'Invalid order data.', 'userswp' ) ) );
1040 }
1041
1042 $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
1043 $new_order = array();
1044
1045 foreach ( $order as $id ) {
1046 foreach ( $register_forms as $form ) {
1047 if ( (int) $form['id'] === (int) $id ) {
1048 $new_order[] = $form;
1049 break;
1050 }
1051 }
1052 }
1053
1054 uwp_update_option( 'multiple_registration_forms', $new_order );
1055
1056 wp_send_json_success( array( 'message' => __( 'User types order updated successfully.', 'userswp' ) ) );
1057 }
1058
1059 public function process_remove_register_form() {
1060
1061 check_ajax_referer( 'uwp_delete_user_types', 'nonce' );
1062
1063 if ( ! current_user_can( 'manage_options' ) ) {
1064 wp_die( -1 );
1065 }
1066
1067 $type = ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : '';
1068 $form_id = ! empty( $_POST['form_id'] ) ? (int) $_POST['form_id'] : '';
1069
1070 $status = false;
1071 $message = __( 'Security nonce failed. Please try again.', 'userswp' );
1072 $redirect = '';
1073
1074 if ( ! empty( $type ) && ! empty( $form_id ) && $type === 'remove' ) {
1075 $status = self::remove_registration_form( (int) $form_id );
1076 $redirect = admin_url( 'admin.php?page=uwp_user_types' );
1077 }
1078
1079 wp_send_json(
1080 array(
1081 'status' => $status,
1082 'message' => $message,
1083 'redirect' => $redirect,
1084 )
1085 );
1086 }
1087
1088 /**
1089 * Removes the registration form and its associated fields.
1090 *
1091 * @global wpdb $wpdb WordPress database abstraction object.
1092 *
1093 * @param int $form_id The form ID to remove.
1094 * @return bool True if the form was found and removed; false otherwise.
1095 */
1096 public static function remove_registration_form( int $form_id ) {
1097 global $wpdb;
1098
1099 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
1100 $status = false;
1101 $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
1102 $form_builder = new UsersWP_Form_Builder();
1103
1104 if ( empty( $register_forms ) || ! is_array( $register_forms ) ) {
1105 return $status;
1106 }
1107
1108 foreach ( $register_forms as $key => $register_form ) {
1109 if ( empty( $register_form['id'] ) || (int) $register_form['id'] !== $form_id ) {
1110 continue;
1111 }
1112
1113 $status = true;
1114 unset( $register_forms[ $key ] );
1115
1116 $fields = $wpdb->get_results(
1117 $wpdb->prepare(
1118 "SELECT id FROM {$table_name} WHERE form_type = %s AND form_id = %d ORDER BY sort_order ASC",
1119 'account',
1120 $form_id
1121 )
1122 );
1123
1124 if ( ! empty( $fields ) ) {
1125 foreach ( $fields as $field ) {
1126 $form_builder->admin_form_field_delete( (int) $field->id, false, $form_id );
1127 }
1128 }
1129 }
1130
1131 $register_forms = array_values( $register_forms );
1132 uwp_update_option( 'multiple_registration_forms', $register_forms );
1133
1134 return $status;
1135 }
1136
1137 /**
1138 * Tell AyeCode UI to load on certain admin pages.
1139 *
1140 * @since 1.2.3.22
1141 *
1142 * @param array $screen_ids Screen IDs.
1143 * @return array Screen IDs.
1144 */
1145 public function add_aui_screens( $screen_ids ) {
1146 // Load on these pages if set
1147 if ( is_admin() && ! wp_doing_ajax() ) {
1148 $screen_ids = array_merge( $screen_ids, uwp_get_screen_ids() );
1149 }
1150
1151 // AUI is also needed for setup wizard.
1152 $screen_ids[] = 'uwp-setup';
1153
1154 return $screen_ids;
1155 }
1156
1157 /**
1158 * Adds a new user type column to the users list table.
1159 *
1160 * @param array $columns Existing columns in the users list table.
1161 * @return array Modified columns array with the new user type column.
1162 */
1163 public function add_user_type_column( array $columns ) {
1164 $columns['uwp_user_type'] = __( 'User Type', 'userswp' );
1165 return $columns;
1166 }
1167
1168 /**
1169 * Displays user type in the user list column.
1170 *
1171 * @param string $value Current column value.
1172 * @param string $column_name Column name being displayed.
1173 * @param int $user_id Current user ID.
1174 * @return string user type title.
1175 */
1176 public function display_user_type_column( $value, $column_name, $user_id ) {
1177 if ( 'uwp_user_type' === $column_name ) {
1178 $form_id = uwp_get_register_form_id( $user_id );
1179 $uwp_user_type = uwp_get_user_register_form( $form_id );
1180
1181 if ( ! isset( $uwp_user_type['id'], $uwp_user_type['title'] ) ) {
1182 return '&mdash;';
1183 }
1184
1185 return $uwp_user_type['title'];
1186 }
1187
1188 return $value;
1189 }
1190
1191 /**
1192 * Display user type options in the admin user edit screen.
1193 *
1194 * @param WP_User $user The WP user object.
1195 * @return void
1196 */
1197 public function display_admin_change_user_type( $user ) {
1198 if ( ! current_user_can( 'manage_options' ) ) {
1199 return;
1200 }
1201
1202 $register_forms = UsersWP_User_Types::get_register_forms();
1203 $user_type_id = (int) uwp_get_register_form_id( $user->ID );
1204
1205 ob_start();
1206 ?>
1207 <table class="form-table">
1208 <tbody>
1209 <tr>
1210 <th>
1211 <label for="uwp_user_type_id" class="fw-semibold">
1212 <?php esc_html_e( 'User Type', 'userswp' ); ?>
1213 </label>
1214 </th>
1215 <td>
1216 <select name="uwp_user_type_id" id="uwp_user_type_id" class="regular-text">
1217 <?php foreach ( $register_forms as $form ) : ?>
1218 <?php
1219 $form_id = absint( $form['id'] );
1220 $is_current = ( $form_id === $user_type_id );
1221 ?>
1222 <option value="<?php echo esc_attr( $form_id ); ?>" <?php selected( $form_id, $user_type_id ); ?>>
1223 <?php echo esc_html( $form['title'] ); ?><?php echo $is_current ? ' (' . esc_html__( 'Active', 'userswp' ) . ')' : ''; ?>
1224 </option>
1225 <?php endforeach; ?>
1226 </select>
1227 <p class="description">
1228 <?php esc_html_e( "Select the user type for this account.", 'userswp' ); ?>
1229 </p>
1230 </td>
1231 </tr>
1232 </tbody>
1233 </table>
1234 <?php
1235
1236 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1237 echo apply_filters( 'uwp_admin_change_user_type_display', ob_get_clean(), $user );
1238 }
1239
1240 /**
1241 * Update user membership.
1242 *
1243 * @param int $user_id The ID of the user to update.
1244 * @return void
1245 */
1246 public function update_user_type( $user_id ) {
1247 if ( ! current_user_can( 'manage_options' ) ) {
1248 return;
1249 }
1250
1251 check_admin_referer( 'update-user_' . $user_id );
1252
1253 if ( isset( $_POST['uwp_user_type_id'] ) && ! empty( $_POST['uwp_user_type_id'] ) ) {
1254 $user_type_id = absint( $_POST['uwp_user_type_id'] );
1255
1256 // Validate new membership.
1257 $uwp_user_type = uwp_get_user_register_form( $user_type_id );
1258 if ( ! $uwp_user_type ) {
1259 return;
1260 }
1261
1262 update_user_meta( $user_id, '_uwp_register_form_id', $user_type_id );
1263 }
1264 }
1265
1266 /**
1267 * Adds a notice after updating user type.
1268 */
1269 public function add_user_type_updated_notice() {
1270 if ( isset( $_GET['user_type_updated'] ) && 'true' === $_GET['user_type_updated'] ) {
1271 ?>
1272 <div class="updated notice is-dismissible">
1273 <p><?php esc_html_e( 'User type updated successfully.', 'userswp' ); ?></p>
1274 </div>
1275 <?php
1276 }
1277 }
1278
1279 /**
1280 * Adds a bulk action dropdown for changing user types on the Users list table.
1281 *
1282 * @param string $which The position of the table (top or bottom).
1283 */
1284 public function add_bulk_user_type_dropdown( $which ) {
1285 if ( ! is_admin() || 'users' !== get_current_screen()->id || 'top' !== $which ) {
1286 return;
1287 }
1288
1289 $user_types = UsersWP_User_Types::get_register_forms();
1290
1291 if ( empty( $user_types ) ) {
1292 return;
1293 }
1294
1295 $options = array_map( function( $user_type ) {
1296 return sprintf(
1297 '<option value="%d">%s</option>',
1298 absint( $user_type['id'] ),
1299 esc_html( $user_type['title'] )
1300 );
1301 }, $user_types );
1302
1303 ob_start();
1304 ?>
1305 <div class="alignleft actions">
1306 <label class="screen-reader-text" for="uwp_new_user_type">
1307 <?php esc_html_e( 'Change user type to…', 'userswp' ); ?>
1308 </label>
1309 <select name="uwp_new_user_type" id="uwp_new_user_type">
1310 <option value=""><?php esc_html_e( 'Change user type to…', 'userswp' ); ?></option>
1311 <?php echo implode( '', $options ); ?>
1312 </select>
1313 <input type="submit" name="uwp_change_user_type" id="uwp_change_user_type" class="button" value="<?php esc_attr_e( 'Change', 'userswp' ); ?>">
1314 </div>
1315 <?php
1316
1317 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1318 echo apply_filters( 'uwp_bulk_change_user_type_display', ob_get_clean() );
1319 }
1320
1321 /**
1322 * Handles the bulk user type change.
1323 */
1324 public function handle_bulk_user_type_change() {
1325 if ( ! isset( $_GET['uwp_change_user_type'], $_GET['uwp_new_user_type'] ) ) {
1326 return;
1327 }
1328
1329 if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'bulk-users' ) ) {
1330 wp_die( __( 'Security check failed. Please try again.' ) );
1331 }
1332
1333 if ( ! current_user_can( 'edit_users' ) ) {
1334 wp_die( __( 'You do not have permission to perform this action.' ) );
1335 }
1336
1337 $new_user_type = absint( $_GET['uwp_new_user_type'] );
1338
1339 if ( ! $new_user_type ) {
1340 return;
1341 }
1342
1343 $users = isset( $_REQUEST['users'] ) && ! empty( $_REQUEST['users'] ) ? (array) $_REQUEST['users'] : array();
1344
1345 if ( ! empty( $users ) ) {
1346 array_map( function( $user_id ) use ( $new_user_type ) {
1347 update_user_meta( absint( $user_id ), '_uwp_register_form_id', (int) $new_user_type );
1348 }, $users );
1349 }
1350
1351 wp_safe_redirect( add_query_arg( 'user_type_updated', 'true', admin_url( 'users.php' ) ) );
1352 exit;
1353 }
1354
1355 /**
1356 * Adds JavaScript validation script.
1357 */
1358 public function add_validation_script() {
1359 $screen = get_current_screen();
1360
1361 if ( 'users' !== $screen->id ) {
1362 return;
1363 }
1364 ?>
1365 <script type="text/javascript">
1366 (function($) {
1367 $(document).ready(function() {
1368 var $userTypeErrorNotice = $(
1369 '<div id="no-user-type-selected" class="notice notice-error is-dismissible" style="display:none;">' +
1370 '<p><?php esc_html_e( "Please select a user type to perform this action.", "userswp" ); ?></p>' +
1371 '<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( "Dismiss this notice.", "userswp" ); ?></span></button>' +
1372 '</div>',
1373 );
1374
1375 var $itemSelectionErrorNotice = $(
1376 '<div id="no-item-selected" class="notice notice-error is-dismissible" style="display:none;">' +
1377 '<p><?php esc_html_e( "Please select at least one item to perform this action on.", "userswp" ); ?></p>' +
1378 '<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( "Dismiss this notice.", "userswp" ); ?></span></button>' +
1379 '</div>',
1380 );
1381
1382 $("#wpbody-content").prepend($userTypeErrorNotice, $itemSelectionErrorNotice)
1383
1384 $("#uwp_change_user_type").on("click", (e) => {
1385 if ($('input[name="users[]"]:checked').length === 0) {
1386 e.preventDefault();
1387 showErrorNotice($itemSelectionErrorNotice);
1388 hideErrorNotice($userTypeErrorNotice);
1389 } else if ($("#new_user_type").val() === "") {
1390 e.preventDefault();
1391 showErrorNotice($userTypeErrorNotice);
1392 hideErrorNotice($itemSelectionErrorNotice);
1393 } else {
1394 hideErrorNotice($userTypeErrorNotice);
1395 hideErrorNotice($itemSelectionErrorNotice);
1396 }
1397 });
1398
1399 $(".notice-dismiss").on("click", function () {
1400 hideErrorNotice($(this).closest(".notice"));
1401 })
1402
1403 function showErrorNotice($notice) {
1404 $notice.fadeIn(300);
1405 }
1406
1407 function hideErrorNotice($notice) {
1408 $notice.fadeOut(300);
1409 }
1410 });
1411 })(jQuery);
1412 </script>
1413 <?php
1414 }
1415 }
1416