PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.35
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.35
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.35, at admin/class-admin.php

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