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

1,106 lines 40.2 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( 'wp_ajax_uwp_ajax_create_register', array( $this, 'process_create_register_form' ) );
54 add_action( 'wp_ajax_uwp_ajax_update_register', array( $this, 'process_update_register_form' ) );
55 add_action( 'wp_ajax_uwp_ajax_remove_register', array( $this, 'process_remove_register_form' ) );
56
57 // Register with the deactivation survey class.
58 if ( class_exists( 'AyeCode_Deactivation_Survey' ) ) {
59 AyeCode_Deactivation_Survey::instance(
60 array(
61 'slug' => 'userswp',
62 'version' => USERSWP_VERSION,
63 'support_url' => 'https://userswp.io/support/',
64 'documentation_url' => 'https://docs.userswp.io/',
65 'activated' => get_option( 'uwp_installed_on', 0 ),
66 )
67 );
68 }
69 }
70
71 /**
72 * Redirects to UsersWP info page after plugin activation.
73 *
74 * @return void
75 * @package userswp
76 * @since 1.0.0
77 */
78 public function activation_redirect() {
79 if ( get_option( 'uwp_activation_redirect', false ) ) {
80 delete_option( 'uwp_activation_redirect' );
81 update_option( 'uwp_setup_wizard_notice', 1 );
82 wp_redirect( admin_url( 'index.php?page=uwp-setup' ) );
83 exit;
84 }
85
86 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' ) ) {
87 $blog_id = get_current_blog_id();
88 do_action( 'wp_' . $blog_id . '_uwp_updater_cron' );
89 wp_safe_redirect( admin_url( 'admin.php?page=userswp' ) );
90 exit;
91 }
92 }
93
94 /**
95 * Maybe show the AyeCode Connect Notice.
96 */
97 public function init_ayecode_connect_helper() {
98 // AyeCode Connect notice
99 if ( is_admin() ) {
100 // set the strings so they can be translated
101 $strings = array(
102 'connect_title' => __( 'UsersWP - an AyeCode product!', 'userswp' ),
103 'connect_external' => __( 'Please confirm you wish to connect your site?', 'userswp' ),
104 '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>' ),
105 'connect_button' => __( 'Connect Site', 'userswp' ),
106 'connecting_button' => __( 'Connecting...', 'userswp' ),
107 'error_localhost' => __( 'This service will only work with a live domain, not a localhost.', 'userswp' ),
108 'error' => __( 'Something went wrong, please refresh and try again.', 'userswp' ),
109 );
110 new AyeCode_Connect_Helper( $strings, array( 'uwp-addons' ) );
111 }
112 }
113
114 /**
115 * Restrict the wp-admin area from specific user roles if set to do so.
116 */
117 public function prevent_admin_access() {
118 $restricted_roles = (array) uwp_get_option( 'admin_blocked_roles', array() );
119
120 // Checking action in request to allow ajax request go through
121 if ( ! empty( $restricted_roles ) && is_user_logged_in() && ! wp_doing_ajax() && ! wp_doing_cron() ) {
122 $roles = wp_get_current_user()->roles;
123
124 $prevent = false;
125
126 // Always allow administrator role.
127 if ( ! ( ! empty( $roles ) && in_array( 'administrator', $roles ) ) ) {
128 foreach ( $restricted_roles as $role ) {
129 if ( in_array( $role, $roles ) ) {
130 $prevent = true;
131 break;
132 }
133 }
134 }
135
136 /*
137 * Check and prevent admin access based on user role.
138 *
139 * @param bool $prevent True to prevent admin access.
140 */
141 $prevent = apply_filters( 'uwp_prevent_wp_admin_access', $prevent );
142
143 if ( $prevent ) {
144 wp_safe_redirect( home_url() );
145 exit;
146 }
147 }
148 }
149
150 /**
151 * Register the stylesheets for the admin area.
152 *
153 * @param $hook_suffix
154 *
155 * @since 1.0.0
156 */
157 public function enqueue_styles( $hook_suffix ) {
158 $screen = get_current_screen();
159 $screen_id = $screen ? $screen->id : '';
160
161 if ( $hook_suffix == 'profile.php' || $hook_suffix == 'user-edit.php' ) {
162 wp_register_style( 'jquery-ui', USERSWP_PLUGIN_URL . 'assets/css/jquery-ui.css' );
163 wp_enqueue_style( 'jquery-ui' );
164 wp_enqueue_style( 'jcrop' );
165 wp_enqueue_style( 'uwp-country-select', USERSWP_PLUGIN_URL . 'assets/css/countryselect.css', array(), USERSWP_VERSION, 'all' );
166 wp_enqueue_style( 'userswp', USERSWP_PLUGIN_URL . 'assets/css/users-wp.css', array(), USERSWP_VERSION, 'all' );
167 wp_enqueue_style( 'uwp_timepicker_css', USERSWP_PLUGIN_URL . 'assets/css/jquery.ui.timepicker.css', array(), USERSWP_VERSION, 'all' );
168 }
169
170 if ( false !== strpos( $hook_suffix, 'page_uwp_tools' ) ) {
171 wp_enqueue_style( 'userswp', USERSWP_PLUGIN_URL . 'assets/css/users-wp.css', array(), USERSWP_VERSION, 'all' );
172 }
173
174 if ( false !== strpos( $hook_suffix, 'page_uwp_form_builder' ) ) {
175 wp_enqueue_style( 'uwp_form_builder', USERSWP_PLUGIN_URL . 'admin/assets/css/uwp-form-builder.css', array(), USERSWP_VERSION, 'all' );
176 }
177
178 if ( $hook_suffix == 'toplevel_page_userswp' ) {
179 wp_enqueue_style( 'wp-color-picker' );
180 }
181
182 if ( in_array( $screen_id, uwp_get_screen_ids() ) ) {
183 wp_enqueue_style( 'userswp_admin_css', USERSWP_PLUGIN_URL . 'admin/assets/css/users-wp-admin.css', array(), USERSWP_VERSION, 'all' );
184 }
185 }
186
187 /**
188 * Register the JavaScript for the admin area.
189 *
190 * @param $hook_suffix
191 *
192 * @since 1.0.0
193 */
194 public function enqueue_scripts( $hook_suffix ) {
195
196 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
197 $screen = get_current_screen();
198 $screen_id = $screen ? $screen->id : '';
199
200 if ( $hook_suffix == 'profile.php' || $hook_suffix == 'user-edit.php' ) {
201
202 wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) );
203 wp_enqueue_script(
204 'uwp_timepicker',
205 USERSWP_PLUGIN_URL . 'assets/js/jquery.ui.timepicker.min.js',
206 array(
207 'jquery',
208 'jquery-ui-datepicker',
209 'jquery-ui-core',
210 ),
211 USERSWP_VERSION
212 );
213 wp_enqueue_script( 'userswp', USERSWP_PLUGIN_URL . 'assets/js/users-wp' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION, false );
214 $uwp_localize_data = uwp_get_localize_data();
215 wp_localize_script( 'userswp', 'uwp_localize_data', $uwp_localize_data );
216 wp_enqueue_script( 'jquery-ui-progressbar', array( 'jquery' ) );
217 wp_enqueue_script( 'jcrop', array( 'jquery' ) );
218 wp_enqueue_script( 'country-select', USERSWP_PLUGIN_URL . 'assets/js/countrySelect' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION );
219
220 $country_data = uwp_get_country_data();
221 wp_localize_script( USERSWP_NAME, 'uwp_country_data', $country_data );
222 }
223
224 if ( false !== strpos( $hook_suffix, 'page_uwp_tools' ) ) {
225 wp_enqueue_script( 'jquery-ui-progressbar', array( 'jquery' ) );
226 }
227
228 if ( false !== strpos( $hook_suffix, 'page_uwp_status' ) ) {
229 wp_enqueue_script( 'uwp_status', USERSWP_PLUGIN_URL . 'admin/assets/js/system-status.js', array( 'jquery' ), USERSWP_VERSION, true );
230 }
231
232 if ( false !== strpos( $hook_suffix, 'page_uwp_form_builder' ) ) {
233 wp_enqueue_script( 'jquery-ui-sortable' );
234 wp_enqueue_script( 'uwp-nestable-script', USERSWP_PLUGIN_URL . 'admin/assets/js/jquery.nestable' . $suffix . '.js', array( 'jquery-ui-sortable' ), USERSWP_VERSION );
235 wp_enqueue_script( 'uwp_form_builder', USERSWP_PLUGIN_URL . 'admin/assets/js/uwp-form-builder' . $suffix . '.js', array(), USERSWP_VERSION, 'all' );
236 }
237
238 if ( in_array( $screen_id, uwp_get_screen_ids() ) ) {
239 wp_enqueue_script( 'userswp_admin', USERSWP_PLUGIN_URL . 'admin/assets/js/users-wp-admin' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION, false );
240
241 wp_enqueue_script( 'jquery-ui-tooltip' );
242 wp_enqueue_script( 'wp-color-picker' );
243
244 $ajax_cons_data = array(
245 'url' => admin_url( 'admin-ajax.php' ),
246 'custom_field_not_blank_var' => __( 'Field key must not be blank', 'userswp' ),
247 'custom_field_options_not_blank_var' => __( 'Option Values must not be blank', 'userswp' ),
248 'custom_field_not_special_char' => __( 'Please do not use special character and spaces in field key.', 'userswp' ),
249 'custom_field_unique_name' => __( 'Field key should be a unique name.', 'userswp' ),
250 'custom_field_delete' => __( 'Are you sure you wish to delete this field?', 'userswp' ),
251 'custom_field_id_required' => __( 'This field is required.', 'userswp' ),
252 'img_spacer' => admin_url( 'images/media-button-image.gif' ),
253 'txt_choose_image' => __( 'Choose an image', 'userswp' ),
254 'txt_use_image' => __( 'Use image', 'userswp' ),
255 'txt_save' => __( 'Save', 'userswp' ),
256 'txt_saved' => __( 'Saved', 'userswp' ),
257 'txt_delete' => __( 'Delete', 'userswp' ),
258 'txt_deleted' => __( 'Deleted', 'userswp' ),
259 'txt_cancel' => __( 'Cancel', 'userswp' ),
260 'txt_saving' => __( 'Saving...', 'userswp' ),
261 'delete_register_form' => __( 'Are you sure you wish to delete this form?', 'userswp' ),
262 'ask_register_form_title' => __( 'Enter register form title', 'userswp' ),
263 'form_updated_msg' => __( 'Updated! Reloading page...', 'userswp' )
264 );
265 wp_localize_script( "userswp_admin", 'uwp_admin_ajax', $ajax_cons_data );
266 }
267 }
268
269 public function get_screen_ids() {
270 return uwp_get_screen_ids();
271 }
272
273 /**
274 * Displays update messages
275 */
276 public function show_update_messages() {
277 if ( ! isset( $_REQUEST['update'] ) ) {
278 return;
279 }
280
281 $update = sanitize_text_field( $_REQUEST['update'] );
282 $messages = array();
283
284 switch ( $update ) {
285 case 'uwp_resend':
286 $messages['msg'] = __( 'Activation email has been sent!', 'userswp' );
287 break;
288 case 'err_uwp_resend':
289 $messages['err_msg'] = __( 'Error while sending activation email. Please try again.', 'userswp' );
290 break;
291 case 'uwp_activate_user':
292 $messages['msg'] = __( 'User(s) has been activated!', 'userswp' );
293 break;
294 }
295
296 if ( ! empty( $messages ) ) {
297 if ( isset( $messages['err_msg'] ) ) {
298 echo '<div class="notice notice-error"><p>' . esc_html( $messages['err_msg'] ) . '</p></div>';
299 } else {
300 echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $messages['msg'] ) . '</p></div>';
301 }
302 }
303 }
304
305 /**
306 * Adds UsersWP JS to admin area.
307 *
308 * @return void
309 * @package userswp
310 *
311 * @since 1.0.0
312 */
313 public function admin_only_script() {
314
315 // check page is userswp or not.
316 if ( ! empty( $_GET['page'] ) && 'userswp' == $_GET['page'] ) {
317
318 // check tab is general or not.
319 if ( ! empty( $_GET['tab'] ) && 'general' == $_GET['tab'] ) {
320
321 // check for login section.
322 if ( ! empty( $_GET['section'] ) && 'login' == $_GET['section'] ) {
323 ?>
324 <script type="text/javascript">
325 jQuery(document).ready(function () {
326 var login_redirect_to = jQuery('#login_redirect_to');
327 login_redirect_to.on('change', function () {
328 var value = jQuery(this).val();
329 var login_redirect_custom_obj = jQuery('#login_redirect_custom_url');
330
331 if ('-2' === value) {
332 login_redirect_custom_obj.parent().parent().show();
333 } else {
334 login_redirect_custom_obj.parent().parent().hide();
335 }
336 }).change();
337 });
338 </script>
339 <?php
340 }
341 }
342 }
343
344 if ( ! empty( $_GET['page'] ) && 'uwp_user_types' == $_GET['page'] ) {
345 ?>
346 <script type="text/javascript">
347 jQuery(document).ready(function () {
348 var uwp_registration_action = jQuery('#uwp_registration_action');
349 uwp_registration_action.on('change', function () {
350 var value = jQuery(this).val();
351 var register_redirect_obj = jQuery('#register_redirect_to');
352
353 if ('auto_approve_login' === value) {
354 register_redirect_obj.parent().parent().show();
355 } else {
356 register_redirect_obj.parent().parent().hide();
357 }
358 }).change();
359
360 var register_redirect_to = jQuery('#register_redirect_to');
361 register_redirect_to.on('change', function () {
362 var value = jQuery(this).val();
363 var register_redirect_custom_obj = jQuery('#register_redirect_custom_url');
364
365 if ('-2' === value) {
366 register_redirect_custom_obj.parent().show();
367 register_redirect_custom_obj.parent().prev().show();
368 } else {
369 register_redirect_custom_obj.parent().hide();
370 register_redirect_custom_obj.parent().prev().hide();
371 }
372 }).change();
373 });
374 </script>
375 <?php
376 }
377 }
378
379 /**
380 * Filters the user profile picture description displayed under the Gravatar.
381 *
382 * @param string $description Profile picture description.
383 *
384 * @return string Modified description.
385 * @since 1.0.0
386 * @package userswp
387 *
388 */
389 public function user_profile_picture_description( $description ) {
390 if ( is_admin() && IS_PROFILE_PAGE ) {
391 $user_id = get_current_user_id();
392 $avatar = uwp_get_usermeta( $user_id, 'avatar_thumb', '' );
393
394 if ( ! empty( $avatar ) ) {
395 $description = sprintf(
396 __( 'You can change your profile picture on your <a href="%s">Profile Page</a>.', 'userswp' ),
397 uwp_build_profile_tab_url( $user_id )
398 );
399 }
400 }
401
402 return $description;
403 }
404
405 public function admin_body_class( $classes ) {
406 $screen = get_current_screen();
407 if ( 'profile' == $screen->base || 'user-edit' == $screen->base ) {
408 $classes .= ' uwp_page';
409 }
410
411 // Add original UsersWP page class when UsersWP screen is translated.
412 if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], array( 'uwp_form_builder', 'uwp_tools', 'uwp_status', 'uwp-addons' ) ) ) {
413 $uwp_screen_id = sanitize_title( __( 'UsersWP', 'userswp' ) );
414
415 if ( $uwp_screen_id != 'userswp' ) {
416 $classes .= ' userswp_page_' . esc_attr( sanitize_text_field( $_GET['page'] ) );
417 }
418 }
419
420 // Add body class for admin pages.
421 $screen = get_current_screen();
422 $screen_id = $screen ? $screen->id : '';
423
424 if ( $screen_id && in_array( $screen_id, uwp_get_screen_ids() ) ) {
425 $classes .= ' uwp-admin-page uwp-admin-page-' . sanitize_key( $screen_id );
426 }
427
428 return $classes;
429 }
430
431 /**
432 * Preview email template for UWP emails.
433 */
434 public function preview_emails() {
435 if ( isset( $_GET['uwp_preview_mail'] ) ) {
436 if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'uwp-preview-mail' ) ) {
437 die( 'Security check failed.' );
438 }
439
440 $email_name = 'preview_mail';
441 $email_vars = array();
442 $plain_text = UsersWP_Mails::get_email_type() != 'html' ? true : false;
443
444 // Get the preview email content.
445 ob_start();
446 include 'views/html-email-template-preview.php';
447 $message = ob_get_clean();
448
449 $message = UsersWP_Mails::email_wrap_message( $message, $email_name, $email_vars, '', $plain_text );
450 $message = UsersWP_Mails::style_body( $message, $email_name, $email_vars );
451 $message = apply_filters( 'uwp_mail_content', $message, $email_name, $email_vars );
452
453 // Print the preview email content.
454 if ( $plain_text ) {
455 echo '<div style="white-space:pre-wrap;font-family:sans-serif">';
456 }
457 echo $message; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
458 if ( $plain_text ) {
459 echo '</div>';
460 }
461 exit;
462 }
463 }
464
465 /**
466 * Returns user views filter
467 *
468 * @return array User views.
469 */
470 public function request_views_users( $views ) {
471
472 $current = '';
473
474 if ( isset( $_REQUEST['uwp_status'] ) && $_REQUEST['uwp_status'] == 'pending-email-activate' ) {
475 $views['all'] = str_replace( 'current', '', $views['all'] );
476 $current = 'current';
477 }
478
479 $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>';
480
481 return $views;
482 }
483
484 /**
485 * Returns pending email activation user counts
486 *
487 * @return int User count
488 */
489 public function uwp_pending_email_count() {
490
491 $args = array(
492 'fields' => 'ID',
493 'number' => 0,
494 'meta_query' => array(
495 array(
496 'key' => 'uwp_mod',
497 'value' => 'email_unconfirmed',
498 'compare' => '=',
499 ),
500 ),
501 );
502 $users = new WP_User_Query( $args );
503
504 return isset( $users->results ) ? (int) count( $users->results ) : 0;
505 }
506
507 /**
508 * Filter to modify the user query
509 *
510 * @return object User query.
511 */
512 public function request_users_filter( $query ) {
513
514 global $wpdb, $pagenow;
515
516 if ( is_admin() && $pagenow == 'users.php' && isset( $_GET['uwp_status'] ) && $_GET['uwp_status'] != '' ) {
517
518 do_action( 'uwp_users_show_pending_email_activation', $query );
519
520 $status = sanitize_text_field( urldecode( $_GET['uwp_status'] ) );
521
522 if ( $status == 'pending-email-activate' ) {
523 $query->query_where = str_replace(
524 'WHERE 1=1',
525 "WHERE 1=1 AND {$wpdb->users}.ID IN (
526 SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
527 WHERE {$wpdb->usermeta}.meta_key = 'uwp_mod'
528 AND {$wpdb->usermeta}.meta_value = 'email_unconfirmed')",
529 $query->query_where
530 );
531 }
532 }
533
534 return $query;
535 }
536
537 /**
538 * Gets UsersWP fields in WP-Admin Users Edit page.
539 *
540 * @param object $user User Object.
541 *
542 * @package userswp
543 * @since 1.0.0
544 */
545 public function get_profile_extra_edit( $user ) {
546 global $wpdb;
547 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
548 $form_id = uwp_get_register_form_id( $user->ID );
549 $excluded_fields = apply_filters( 'uwp_exclude_edit_profile_fields', array( 'bio', 'register_gdpr', 'register_tos', 'uwp_language', 'multisite_site_title', 'multisite_site_address' ) );
550 $query = $wpdb->prepare( 'SELECT * FROM ' . $table_name . " WHERE form_type = 'account' AND is_default = '0' AND form_id = %d", $form_id );
551 if ( is_array( $excluded_fields ) && count( $excluded_fields ) > 0 ) {
552 $query .= " AND htmlvar_name NOT IN ('" . implode( "','", $excluded_fields ) . "')";
553 }
554 $query .= ' ORDER BY sort_order ASC';
555 $fields = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
556
557 $this->edit_profile_banner_fields( $user ); // Displays avatar and banner fields
558
559 if ( $fields ) {
560 ?>
561 <div class="uwp-profile-extra">
562 <table class="uwp-profile-extra-table form-table bsui">
563 <?php
564 foreach ( $fields as $field ) {
565
566 if ( isset( $field->css_class ) && ! empty( $field->css_class ) ) {
567 $field->css_class = $field->css_class . ' w-50';
568 } else {
569 $field->css_class = 'w-50';
570 }
571
572 if ( $field->field_type == 'fieldset' ) {
573 ?>
574 <tr style="margin: 0; padding: 0">
575 <th class="uwp-profile-extra-key" style="margin: 0; padding: 0"><h3
576 style="margin: 10px 0;"><?php echo esc_html( $field->site_title ); ?></h3></th>
577 <td></td>
578 </tr>
579 <?php
580 } else {
581 ?>
582 <tr>
583 <th class="uwp-profile-extra-key"><?php echo esc_html( $field->site_title ); ?></th>
584 <td class="uwp-profile-extra-value">
585 <?php
586 $templates_obj = new UsersWP_Templates();
587 $templates_obj->template_fields_html( $field, 'account', $user->ID );
588 ?>
589 </td>
590 </tr>
591 <?php
592 }
593 }
594 ?>
595 </table>
596 </div>
597 <?php
598 }
599 }
600
601 /**
602 * Adds avatar and banner fields in admin side.
603 *
604 * @param object $user User object.
605 *
606 * @return void
607 * @since 1.0.0
608 * @package userswp
609 *
610 */
611 public function edit_profile_banner_fields( $user ) {
612 global $wpdb;
613
614 $file_obj = new UsersWP_Files();
615
616 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
617 $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
618 if ( $fields ) {
619 ?>
620 <div class="uwp-profile-extra uwp_page">
621 <?php do_action( 'uwp_admin_profile_edit', $user ); ?>
622 <h2><?php esc_html_e( 'UsersWP Account Details', 'userswp' ); ?></h2>
623 <table class="uwp-profile-extra-table form-table">
624 <?php
625 foreach ( $fields as $field ) {
626
627 if ( $field->field_type == 'fieldset' ) {
628 ?>
629 <tr style="margin: 0; padding: 0">
630 <th class="uwp-profile-extra-key" style="margin: 0; padding: 0"><h3
631 style="margin: 10px 0;">
632 <?php echo esc_html( $field->site_title ); ?></h3></th>
633 <td></td>
634 </tr>
635 <?php
636 } else {
637 ?>
638 <tr>
639 <th class="uwp-profile-extra-key"><?php echo esc_html( $field->site_title ); ?></th>
640 <td class="uwp-profile-extra-value">
641 <?php
642 if ( $field->htmlvar_name == 'avatar' ) {
643 $value = uwp_get_usermeta( $user->ID, 'avatar_thumb', '' );
644 } elseif ( $field->htmlvar_name == 'banner' ) {
645 $value = uwp_get_usermeta( $user->ID, 'banner_thumb', '' );
646 } else {
647 $value = '';
648 }
649
650 echo $file_obj->file_upload_preview( $field, $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
651
652 if ( $field->htmlvar_name == 'avatar' ) {
653 if ( ! empty( $value ) ) {
654 $label = __( 'Change Avatar', 'userswp' );
655 } else {
656 $label = __( 'Upload Avatar', 'userswp' );
657 }
658 ?>
659 <a onclick="uwp_profile_image_change('avatar');return false;" href="#"
660 class="uwp-banner-change-icon-admin">
661 <?php echo esc_html( $label ); ?>
662 </a>
663 <?php
664 } elseif ( $field->htmlvar_name == 'banner' ) {
665 if ( ! empty( $value ) ) {
666 $label = __( 'Change Banner', 'userswp' );
667 } else {
668 $label = __( 'Upload Banner', 'userswp' );
669 }
670 ?>
671 <a onclick="uwp_profile_image_change('banner');return false;" href="#"
672 class="uwp-banner-change-icon-admin">
673 <?php echo esc_html( $label ); ?>
674 </a>
675 <?php
676 }
677 ?>
678 </td>
679 </tr>
680 <?php
681 }
682 }
683 ?>
684 </table>
685 </div>
686 <?php
687 }
688 }
689
690 /**
691 * Returns user row actions
692 *
693 * @param array $actions Date string.
694 * @param object $user_object The User ID.
695 *
696 * @return array Row actions.
697 * @package userswp
698 *
699 */
700 public function user_row_actions( $actions, $user_object ) {
701 $user_id = $user_object->ID;
702 $mod_value = get_user_meta( $user_id, 'uwp_mod', true );
703 $resend_link = add_query_arg(
704 array(
705 'user_id' => $user_id,
706 'action' => 'uwp_resend',
707 '_nonce' => wp_create_nonce( 'uwp_resend' ),
708 'uwp_is_admin' => 1,
709 ),
710 admin_url( 'users.php' )
711 );
712
713 $activate_link = add_query_arg(
714 array(
715 'user_id' => $user_id,
716 'action' => 'uwp_activate_user',
717 '_nonce' => wp_create_nonce( 'uwp_activate_user' ),
718 'uwp_is_admin' => 1,
719 ),
720 admin_url( 'users.php' )
721 );
722
723 if ( $mod_value == 'email_unconfirmed' ) {
724 $actions['uwp_resend_activation'] = "<a class='' href='" . $resend_link . "'>" . __( 'Resend Activation', 'userswp' ) . '</a>';
725 $actions['uwp_auto_activate'] = "<a class='' href='" . $activate_link . "'>" . __( 'Activate User', 'userswp' ) . '</a>';
726 }
727
728 return $actions;
729 }
730
731 /**
732 * Returns users bulk actions
733 *
734 * @param array $bulk_actions Bulk actions.
735 *
736 * @return array Bulk actions.
737 * @package userswp
738 *
739 */
740 public function users_bulk_actions( $bulk_actions ) {
741 $bulk_actions['uwp_resend'] = __( 'Resend Activation', 'userswp' );
742 $bulk_actions['uwp_activate_user'] = __( 'Activate Users', 'userswp' );
743
744 return $bulk_actions;
745 }
746
747 /**
748 * Handles users bulk actions
749 *
750 * @param string $redirect_to Bulk actions.
751 * @param string $doaction Current action.
752 * @param array $user_ids User IDs to process.
753 *
754 * @return string Redirect URL.
755 * @package userswp
756 *
757 */
758 public function handle_users_bulk_actions( $redirect_to, $doaction, $user_ids ) {
759 if ( 'uwp_resend' == $doaction ) {
760 foreach ( $user_ids as $user_id ) {
761 uwp_resend_activation_mail( $user_id );
762 }
763
764 $redirect_to = add_query_arg( 'update', 'uwp_resend', $redirect_to );
765 } elseif ( 'uwp_activate_user' == $doaction ) {
766 foreach ( $user_ids as $user_id ) {
767 $this->activate_user( $user_id );
768 }
769 $redirect_to = add_query_arg( 'update', 'uwp_activate_user', $redirect_to );
770 }
771
772 return $redirect_to;
773 }
774
775 /**
776 * Activates user
777 *
778 * @param int $user_id User ID
779 *
780 * @return bool
781 */
782 public function activate_user( $user_id = 0 ) {
783 if ( ! $user_id ) {
784 return false;
785 }
786
787 $uwp_mode = get_user_meta( $user_id, 'uwp_mod', true );
788 if ( 'email_unconfirmed' == $uwp_mode ) {
789 delete_user_meta( $user_id, 'uwp_mod' );
790 do_action( 'uwp_email_activation_success', $user_id );
791 }
792
793 return true;
794 }
795
796 /**
797 * Processes user action
798 *
799 * @return mixed
800 * @package userswp
801 *
802 */
803 public function process_user_actions() {
804 $user_id = isset( $_REQUEST['user_id'] ) ? (int) $_REQUEST['user_id'] : 0;
805 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : false;
806 $nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( $_REQUEST['_nonce'] ) : false;
807 $is_admin = isset( $_REQUEST['uwp_is_admin'] ) ? sanitize_text_field( $_REQUEST['uwp_is_admin'] ) : false;
808
809 if ( $user_id && 'uwp_resend' == $action && ! empty( $nonce ) && wp_verify_nonce( $nonce, 'uwp_resend' ) ) {
810 uwp_resend_activation_mail( $user_id );
811 if ( isset( $is_admin ) && $is_admin ) {
812 wp_redirect( add_query_arg( 'update', 'uwp_resend', admin_url( 'users.php' ) ) );
813 exit();
814 } else {
815 global $uwp_notices;
816 $message = __( 'Activation email has been sent!', 'userswp' );
817 $uwp_notices[] = aui()->alert(
818 array(
819 'type' => 'success',
820 'content' => $message,
821 )
822 );
823 }
824 } elseif ( $user_id && 'uwp_activate_user' == $action && wp_verify_nonce( $nonce, 'uwp_activate_user' ) ) {
825 if ( isset( $is_admin ) && $is_admin && current_user_can( 'edit_users' ) ) {
826 $this->activate_user( $user_id );
827 wp_redirect( add_query_arg( 'update', 'uwp_activate_user', admin_url( 'users.php' ) ) );
828 }
829 }
830 }
831
832 /**
833 * Show row meta on the plugin screen.
834 *
835 * @param mixed $links Plugin Row Meta
836 * @param mixed $file Plugin Base file
837 * @return array
838 */
839 public static function plugin_row_meta( $links, $file ) {
840 if ( USERSWP_PLUGIN_BASENAME == $file ) {
841 $row_meta = array(
842 'docs' => '<a href="' . esc_url( 'https://docs.userswp.io/' ) . '" aria-label="' . esc_attr__( 'View UsersWP Documentation', 'userswp' ) . '">' . esc_html__( 'Docs', 'userswp' ) . '</a>',
843 'support' => '<a href="' . esc_url( 'https://userswp.io/support/' ) . '" aria-label="' . esc_attr__( 'Visit UsersWP support', 'userswp' ) . '">' . esc_html__( 'Support', 'userswp' ) . '</a>',
844 'translation' => '<a href="' . esc_url( 'https://userswp.io/translate/projects' ) . '" aria-label="' . esc_attr__( 'View translations', 'userswp' ) . '">' . esc_html__( 'Translations', 'userswp' ) . '</a>',
845 );
846
847 return array_merge( $links, $row_meta );
848 }
849
850 return (array) $links;
851 }
852
853 /**
854 * Process the creation of a new registration form.
855 *
856 * This method handles the AJAX request to create a new registration form,
857 * including validation, sanitization, and database operations.
858 */
859 public function process_create_register_form() {
860 check_ajax_referer( 'uwp-create-register-form-nonce', 'uwp_create_register_form_nonce' );
861
862 if ( ! current_user_can( 'manage_options' ) ) {
863 wp_die( -1 );
864 }
865
866 $form_title = isset( $_POST['form_title'] ) ? sanitize_text_field( $_POST['form_title'] ) : __( 'Form', 'userswp' );
867 $user_role = isset( $_POST['user_role'] ) ? sanitize_text_field( $_POST['user_role'] ) : '';
868 $action = isset( $_POST['reg_action'] ) ? sanitize_text_field( $_POST['reg_action'] ) : uwp_get_option( 'uwp_registration_action', 'auto_approve' );
869 $redirect_to = isset( $_POST['redirect_to'] ) ? (int)$_POST['redirect_to'] : 0;
870 $custom_url = isset( $_POST['custom_url'] ) ? sanitize_text_field( $_POST['custom_url'] ) : '';
871 $gdpr_page = isset( $_POST['gdpr_page'] ) ? (int)$_POST['gdpr_page'] : (int)uwp_get_option( 'register_gdpr_page', false );
872 $tos_page = isset( $_POST['tos_page'] ) ? (int)$_POST['tos_page'] : (int)uwp_get_option( 'register_terms_page', false );
873
874 if ( empty( $form_title ) ) {
875 wp_send_json_error(
876 array(
877 'message' => esc_html__( 'Form title is required.', 'userswp' ),
878 )
879 );
880 }
881
882 $status = false;
883 $redirect = '';
884
885 $new_form_id = uwp_get_next_register_form_id();
886 if ( $new_form_id ) {
887 $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
888 // Filter out invalid items.
889 $register_forms = array_filter(
890 $register_forms,
891 function ( $form ) {
892 return isset( $form['id'] );
893 }
894 );
895
896 $register_forms[] = array(
897 'id' => $new_form_id,
898 'title' => ! empty( $form_title ) ? $form_title : sprintf( __( 'Form %d', 'userswp' ), $new_form_id ),
899 'slug' => ! empty( $form_title ) ? sanitize_title( $form_title ) : sprintf( 'form-%d', $new_form_id ),
900 'user_role' => $user_role,
901 'reg_action' => $action,
902 'redirect_to' => $redirect_to,
903 'custom_url' => $custom_url,
904 'gdpr_page' => $gdpr_page,
905 'tos_page' => $tos_page,
906 );
907
908 $register_forms = array_values( $register_forms );
909 $register_forms = apply_filters( 'uwp_multiple_registration_forms_update', $register_forms, $new_form_id );
910
911 uwp_update_option( 'multiple_registration_forms', $register_forms );
912
913 $fields = UsersWP_Activator::uwp_default_custom_fields_account();
914 $form_builder = new UsersWP_Form_Builder();
915
916 foreach ( $fields as $field ) {
917 $field['form_id'] = $new_form_id;
918 $form_builder->admin_form_field_save( $field );
919 }
920
921 UsersWP_Activator::insert_form_extras( $new_form_id );
922
923 do_action( 'uwp_create_register_form', $new_form_id );
924
925 $status = true;
926 $redirect = admin_url( sprintf( 'admin.php?page=uwp_user_types&form=%d', $new_form_id ) );
927 }
928
929 wp_send_json_success(
930 array(
931 'status' => $status,
932 'message' => esc_html__( 'User Type added successfully.', 'userswp' ),
933 'redirect' => $redirect,
934 )
935 );
936 }
937
938 /**
939 * Process the update of an existing registration form.
940 *
941 * This method handles the AJAX request to update an existing registration form,
942 * including validation, sanitization, and database operations.
943 */
944 public function process_update_register_form() {
945 check_ajax_referer( 'uwp-update-register-form-nonce', 'uwp_update_register_form_nonce' );
946
947 if ( ! current_user_can( 'manage_options' ) ) {
948 wp_die( -1 );
949 }
950
951 $form_id = isset( $_POST['manage_field_form_id'] ) ? (int)$_POST['manage_field_form_id'] : 0;
952 $form_title = isset( $_POST['form_title'] ) && ! empty( $_POST['form_title'] ) ? sanitize_text_field( $_POST['form_title'] ) : __( 'Form', 'userswp' );
953 $user_role = isset( $_POST['user_role'] ) ? sanitize_text_field( $_POST['user_role'] ) : '';
954 $action = isset( $_POST['reg_action'] ) ? sanitize_text_field( $_POST['reg_action'] ) : uwp_get_option( 'uwp_registration_action', 'auto_approve' );
955 $redirect_to = isset( $_POST['redirect_to'] ) ? (int)$_POST['redirect_to'] : 0;
956 $custom_url = isset( $_POST['custom_url'] ) ? sanitize_text_field( $_POST['custom_url'] ) : '';
957 $gdpr_page = isset( $_POST['gdpr_page'] ) ? (int)$_POST['gdpr_page'] : (int)uwp_get_option( 'register_gdpr_page', false );
958 $tos_page = isset( $_POST['tos_page'] ) ? (int)$_POST['tos_page'] : (int)uwp_get_option( 'register_terms_page', false );
959
960 if ( ! $form_id ) {
961 wp_send_json_error(
962 array(
963 'message' => esc_html__( 'Something went wrong. Please try again.', 'userswp' ),
964 )
965 );
966 }
967
968 $redirect = '';
969 $status = false;
970
971 $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
972
973 foreach ( $register_forms as &$register_form ) {
974 if ( $register_form['id'] == $form_id ) {
975 $register_form['title'] = $form_title;
976 $register_form['user_role'] = $user_role;
977 $register_form['reg_action'] = $action;
978 $register_form['redirect_to'] = $redirect_to;
979 $register_form['custom_url'] = $custom_url;
980 $register_form['gdpr_page'] = $gdpr_page;
981 $register_form['tos_page'] = $tos_page;
982 $status = true;
983 break;
984 }
985 }
986
987 if ( $status ) {
988 $register_forms = array_values( $register_forms );
989 $register_forms = apply_filters( 'uwp_multiple_registration_forms_update', $register_forms, $form_id );
990 uwp_update_option( 'multiple_registration_forms', $register_forms );
991 $redirect = add_query_arg(
992 array(
993 'page' => 'uwp_user_types',
994 'form' => $form_id,
995 ),
996 admin_url( 'admin.php' )
997 );
998 }
999
1000 wp_send_json_success(
1001 array(
1002 'status' => $status,
1003 'message' => esc_html__( 'The user type has been updated successfully.', 'userswp' ),
1004 )
1005 );
1006 }
1007
1008 public function process_remove_register_form() {
1009
1010 check_ajax_referer( 'uwp-delete-register-form-nonce', 'nonce' );
1011
1012 if ( ! current_user_can( 'manage_options' ) ) {
1013 wp_die( -1 );
1014 }
1015
1016 $type = ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : '';
1017 $form_id = ! empty( $_POST['form_id'] ) ? (int) $_POST['form_id'] : '';
1018
1019 $status = false;
1020 $message = __( 'Security nonce failed. Please try again.', 'userswp' );
1021 $redirect = '';
1022
1023 if ( ! empty( $type ) && ! empty( $form_id ) && $type === 'remove' ) {
1024 $status = self::remove_registration_form( (int) $form_id );
1025 $redirect = admin_url( 'admin.php?page=uwp_user_types' );
1026 }
1027
1028 wp_send_json(
1029 array(
1030 'status' => $status,
1031 'message' => $message,
1032 'redirect' => $redirect,
1033 )
1034 );
1035 }
1036
1037 /**
1038 * Removes the registration form and its associated fields.
1039 *
1040 * @global wpdb $wpdb WordPress database abstraction object.
1041 *
1042 * @param int $form_id The form ID to remove.
1043 * @return bool True if the form was found and removed; false otherwise.
1044 */
1045 public static function remove_registration_form( int $form_id ) {
1046 global $wpdb;
1047
1048 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
1049 $status = false;
1050 $register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() );
1051 $form_builder = new UsersWP_Form_Builder();
1052
1053 if ( empty( $register_forms ) || ! is_array( $register_forms ) ) {
1054 return $status;
1055 }
1056
1057 foreach ( $register_forms as $key => $register_form ) {
1058 if ( empty( $register_form['id'] ) || (int) $register_form['id'] !== $form_id ) {
1059 continue;
1060 }
1061
1062 $status = true;
1063 unset( $register_forms[ $key ] );
1064
1065 $fields = $wpdb->get_results(
1066 $wpdb->prepare(
1067 "SELECT id FROM {$table_name} WHERE form_type = %s AND form_id = %d ORDER BY sort_order ASC",
1068 'account',
1069 $form_id
1070 )
1071 );
1072
1073 if ( ! empty( $fields ) ) {
1074 foreach ( $fields as $field ) {
1075 $form_builder->admin_form_field_delete( (int) $field->id, false, $form_id );
1076 }
1077 }
1078 }
1079
1080 $register_forms = array_values( $register_forms );
1081 uwp_update_option( 'multiple_registration_forms', $register_forms );
1082
1083 return $status;
1084 }
1085
1086 /**
1087 * Tell AyeCode UI to load on certain admin pages.
1088 *
1089 * @since 1.2.3.22
1090 *
1091 * @param array $screen_ids Screen IDs.
1092 * @return array Screen IDs.
1093 */
1094 public function add_aui_screens( $screen_ids ) {
1095 // Load on these pages if set
1096 if ( is_admin() && ! wp_doing_ajax() ) {
1097 $screen_ids = array_merge( $screen_ids, uwp_get_screen_ids() );
1098 }
1099
1100 // AUI is also needed for setup wizard.
1101 $screen_ids[] = 'uwp-setup';
1102
1103 return $screen_ids;
1104 }
1105 }
1106