| 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_user_type', array( $this, 'process_remove_register_form' ) ); |
| 56 |
add_action( 'wp_ajax_uwp_ajax_reorder_user_types', array( $this, 'reorder_user_types' ) ); |
| 57 |
|
| 58 |
// Register with the deactivation survey class. |
| 59 |
if ( class_exists( 'AyeCode_Deactivation_Survey' ) ) { |
| 60 |
AyeCode_Deactivation_Survey::instance( |
| 61 |
array( |
| 62 |
'slug' => 'userswp', |
| 63 |
'version' => USERSWP_VERSION, |
| 64 |
'support_url' => 'https://userswp.io/support/', |
| 65 |
'documentation_url' => 'https://docs.userswp.io/', |
| 66 |
'activated' => get_option( 'uwp_installed_on', 0 ), |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Redirects to UsersWP info page after plugin activation. |
| 74 |
* |
| 75 |
* @return void |
| 76 |
* @package userswp |
| 77 |
* @since 1.0.0 |
| 78 |
*/ |
| 79 |
public function activation_redirect() { |
| 80 |
if ( get_option( 'uwp_activation_redirect', false ) ) { |
| 81 |
delete_option( 'uwp_activation_redirect' ); |
| 82 |
update_option( 'uwp_setup_wizard_notice', 1 ); |
| 83 |
wp_redirect( admin_url( 'index.php?page=uwp-setup' ) ); |
| 84 |
exit; |
| 85 |
} |
| 86 |
|
| 87 |
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' ) ) { |
| 88 |
$blog_id = get_current_blog_id(); |
| 89 |
do_action( 'wp_' . $blog_id . '_uwp_updater_cron' ); |
| 90 |
wp_safe_redirect( admin_url( 'admin.php?page=userswp' ) ); |
| 91 |
exit; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Maybe show the AyeCode Connect Notice. |
| 97 |
*/ |
| 98 |
public function init_ayecode_connect_helper() { |
| 99 |
// AyeCode Connect notice |
| 100 |
if ( is_admin() ) { |
| 101 |
// set the strings so they can be translated |
| 102 |
$strings = array( |
| 103 |
'connect_title' => __( 'UsersWP - an AyeCode product!', 'userswp' ), |
| 104 |
'connect_external' => __( 'Please confirm you wish to connect your site?', 'userswp' ), |
| 105 |
'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>' ), |
| 106 |
'connect_button' => __( 'Connect Site', 'userswp' ), |
| 107 |
'connecting_button' => __( 'Connecting...', 'userswp' ), |
| 108 |
'error_localhost' => __( 'This service will only work with a live domain, not a localhost.', 'userswp' ), |
| 109 |
'error' => __( 'Something went wrong, please refresh and try again.', 'userswp' ), |
| 110 |
); |
| 111 |
new AyeCode_Connect_Helper( $strings, array( 'uwp-addons' ) ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Restrict the wp-admin area from specific user roles if set to do so. |
| 117 |
*/ |
| 118 |
public function prevent_admin_access() { |
| 119 |
$restricted_roles = (array) uwp_get_option( 'admin_blocked_roles', array() ); |
| 120 |
|
| 121 |
// Checking action in request to allow ajax request go through |
| 122 |
if ( ! empty( $restricted_roles ) && is_user_logged_in() && ! wp_doing_ajax() && ! wp_doing_cron() ) { |
| 123 |
$roles = wp_get_current_user()->roles; |
| 124 |
|
| 125 |
$prevent = false; |
| 126 |
|
| 127 |
// Always allow administrator role. |
| 128 |
if ( ! ( ! empty( $roles ) && in_array( 'administrator', $roles ) ) ) { |
| 129 |
foreach ( $restricted_roles as $role ) { |
| 130 |
if ( in_array( $role, $roles ) ) { |
| 131 |
$prevent = true; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* Check and prevent admin access based on user role. |
| 139 |
* |
| 140 |
* @param bool $prevent True to prevent admin access. |
| 141 |
*/ |
| 142 |
$prevent = apply_filters( 'uwp_prevent_wp_admin_access', $prevent ); |
| 143 |
|
| 144 |
if ( $prevent ) { |
| 145 |
wp_safe_redirect( home_url() ); |
| 146 |
exit; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Register the stylesheets for the admin area. |
| 153 |
* |
| 154 |
* @param $hook_suffix |
| 155 |
* |
| 156 |
* @since 1.0.0 |
| 157 |
*/ |
| 158 |
public function enqueue_styles( $hook_suffix ) { |
| 159 |
$screen = get_current_screen(); |
| 160 |
$screen_id = $screen ? $screen->id : ''; |
| 161 |
|
| 162 |
if ( $hook_suffix == 'profile.php' || $hook_suffix == 'user-edit.php' ) { |
| 163 |
wp_register_style( 'jquery-ui', USERSWP_PLUGIN_URL . 'assets/css/jquery-ui.css' ); |
| 164 |
wp_enqueue_style( 'jquery-ui' ); |
| 165 |
wp_enqueue_style( 'jcrop' ); |
| 166 |
wp_enqueue_style( 'uwp-country-select', USERSWP_PLUGIN_URL . 'assets/css/countryselect.css', array(), USERSWP_VERSION, 'all' ); |
| 167 |
wp_enqueue_style( 'userswp', USERSWP_PLUGIN_URL . 'assets/css/users-wp.css', array(), USERSWP_VERSION, 'all' ); |
| 168 |
wp_enqueue_style( 'uwp_timepicker_css', USERSWP_PLUGIN_URL . 'assets/css/jquery.ui.timepicker.css', array(), USERSWP_VERSION, 'all' ); |
| 169 |
} |
| 170 |
|
| 171 |
if ( false !== strpos( $hook_suffix, 'page_uwp_tools' ) ) { |
| 172 |
wp_enqueue_style( 'userswp', USERSWP_PLUGIN_URL . 'assets/css/users-wp.css', array(), USERSWP_VERSION, 'all' ); |
| 173 |
} |
| 174 |
|
| 175 |
if ( false !== strpos( $hook_suffix, 'page_uwp_form_builder' ) ) { |
| 176 |
wp_enqueue_style( 'uwp_form_builder', USERSWP_PLUGIN_URL . 'admin/assets/css/uwp-form-builder.css', array(), USERSWP_VERSION, 'all' ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( $hook_suffix == 'toplevel_page_userswp' ) { |
| 180 |
wp_enqueue_style( 'wp-color-picker' ); |
| 181 |
} |
| 182 |
|
| 183 |
if ( in_array( $screen_id, uwp_get_screen_ids() ) ) { |
| 184 |
wp_enqueue_style( 'userswp_admin_css', USERSWP_PLUGIN_URL . 'admin/assets/css/users-wp-admin.css', array(), USERSWP_VERSION, 'all' ); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Register the JavaScript for the admin area. |
| 190 |
* |
| 191 |
* @param $hook_suffix |
| 192 |
* |
| 193 |
* @since 1.0.0 |
| 194 |
*/ |
| 195 |
public function enqueue_scripts( $hook_suffix ) { |
| 196 |
|
| 197 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 198 |
$screen = get_current_screen(); |
| 199 |
$screen_id = $screen ? $screen->id : ''; |
| 200 |
|
| 201 |
if ( $hook_suffix == 'profile.php' || $hook_suffix == 'user-edit.php' ) { |
| 202 |
|
| 203 |
wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) ); |
| 204 |
wp_enqueue_script( |
| 205 |
'uwp_timepicker', |
| 206 |
USERSWP_PLUGIN_URL . 'assets/js/jquery.ui.timepicker.min.js', |
| 207 |
array( |
| 208 |
'jquery', |
| 209 |
'jquery-ui-datepicker', |
| 210 |
'jquery-ui-core', |
| 211 |
), |
| 212 |
USERSWP_VERSION |
| 213 |
); |
| 214 |
wp_enqueue_script( 'userswp', USERSWP_PLUGIN_URL . 'assets/js/users-wp' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION, false ); |
| 215 |
$uwp_localize_data = uwp_get_localize_data(); |
| 216 |
wp_localize_script( 'userswp', 'uwp_localize_data', $uwp_localize_data ); |
| 217 |
wp_enqueue_script( 'jquery-ui-progressbar', array( 'jquery' ) ); |
| 218 |
wp_enqueue_script( 'jcrop', array( 'jquery' ) ); |
| 219 |
wp_enqueue_script( 'country-select', USERSWP_PLUGIN_URL . 'assets/js/countrySelect' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION ); |
| 220 |
|
| 221 |
$country_data = uwp_get_country_data(); |
| 222 |
wp_localize_script( USERSWP_NAME, 'uwp_country_data', $country_data ); |
| 223 |
} |
| 224 |
|
| 225 |
if ( false !== strpos( $hook_suffix, 'page_uwp_tools' ) ) { |
| 226 |
wp_enqueue_script( 'jquery-ui-progressbar', array( 'jquery' ) ); |
| 227 |
} |
| 228 |
|
| 229 |
if ( false !== strpos( $hook_suffix, 'page_uwp_status' ) ) { |
| 230 |
wp_enqueue_script( 'uwp_status', USERSWP_PLUGIN_URL . 'admin/assets/js/system-status.js', array( 'jquery' ), USERSWP_VERSION, true ); |
| 231 |
} |
| 232 |
|
| 233 |
if ( false !== strpos( $hook_suffix, 'page_uwp_form_builder' ) ) { |
| 234 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 235 |
wp_enqueue_script( 'uwp-nestable-script', USERSWP_PLUGIN_URL . 'admin/assets/js/jquery.nestable' . $suffix . '.js', array( 'jquery-ui-sortable' ), USERSWP_VERSION ); |
| 236 |
wp_enqueue_script( 'uwp_form_builder', USERSWP_PLUGIN_URL . 'admin/assets/js/uwp-form-builder' . $suffix . '.js', array(), USERSWP_VERSION, 'all' ); |
| 237 |
} |
| 238 |
|
| 239 |
if ( in_array( $screen_id, uwp_get_screen_ids() ) ) { |
| 240 |
wp_enqueue_script( 'userswp_admin', USERSWP_PLUGIN_URL . 'admin/assets/js/users-wp-admin' . $suffix . '.js', array( 'jquery' ), USERSWP_VERSION, false ); |
| 241 |
|
| 242 |
wp_enqueue_script( 'jquery-ui-tooltip' ); |
| 243 |
wp_enqueue_script( 'wp-color-picker' ); |
| 244 |
|
| 245 |
if ( 'userswp_page_uwp_user_types' === $screen_id ) { |
| 246 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 247 |
} |
| 248 |
|
| 249 |
$ajax_cons_data = array( |
| 250 |
'url' => admin_url( 'admin-ajax.php' ), |
| 251 |
'nonces' => array( |
| 252 |
'uwp_delete_user_type' => wp_create_nonce( 'uwp_delete_user_types' ), |
| 253 |
'uwp_reorder_user_types' => wp_create_nonce( 'uwp_reorder_user_types' ), |
| 254 |
), |
| 255 |
'custom_field_not_blank_var' => __( 'Field key must not be blank', 'userswp' ), |
| 256 |
'custom_field_options_not_blank_var' => __( 'Option Values must not be blank', 'userswp' ), |
| 257 |
'custom_field_not_special_char' => __( 'Please do not use special character and spaces in field key.', 'userswp' ), |
| 258 |
'custom_field_unique_name' => __( 'Field key should be a unique name.', 'userswp' ), |
| 259 |
'custom_field_delete' => __( 'Are you sure you wish to delete this field?', 'userswp' ), |
| 260 |
'custom_field_id_required' => __( 'This field is required.', 'userswp' ), |
| 261 |
'img_spacer' => admin_url( 'images/media-button-image.gif' ), |
| 262 |
'txt_choose_image' => __( 'Choose an image', 'userswp' ), |
| 263 |
'txt_use_image' => __( 'Use image', 'userswp' ), |
| 264 |
'txt_save' => __( 'Save', 'userswp' ), |
| 265 |
'txt_saved' => __( 'Saved', 'userswp' ), |
| 266 |
'txt_delete' => __( 'Delete', 'userswp' ), |
| 267 |
'txt_deleted' => __( 'Deleted', 'userswp' ), |
| 268 |
'txt_cancel' => __( 'Cancel', 'userswp' ), |
| 269 |
'txt_saving' => __( 'Saving...', 'userswp' ), |
| 270 |
'txt_saving_error' => __( 'An error occurred while saving. Please try again.', 'userswp' ), |
| 271 |
'delete_register_form' => __( 'Are you sure you wish to delete this form?', 'userswp' ), |
| 272 |
'ask_register_form_title' => __( 'Enter register form title', 'userswp' ), |
| 273 |
'form_updated_msg' => __( 'Updated! Reloading page...', 'userswp' ), |
| 274 |
); |
| 275 |
|
| 276 |
wp_localize_script( 'userswp_admin', 'uwp_admin_ajax', $ajax_cons_data ); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
public function get_screen_ids() { |
| 281 |
return uwp_get_screen_ids(); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Displays update messages |
| 286 |
*/ |
| 287 |
public function show_update_messages() { |
| 288 |
if ( ! isset( $_REQUEST['update'] ) ) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
$update = sanitize_text_field( $_REQUEST['update'] ); |
| 293 |
$messages = array(); |
| 294 |
|
| 295 |
switch ( $update ) { |
| 296 |
case 'uwp_resend': |
| 297 |
$messages['msg'] = __( 'Activation email has been sent!', 'userswp' ); |
| 298 |
break; |
| 299 |
case 'err_uwp_resend': |
| 300 |
$messages['err_msg'] = __( 'Error while sending activation email. Please try again.', 'userswp' ); |
| 301 |
break; |
| 302 |
case 'uwp_activate_user': |
| 303 |
$messages['msg'] = __( 'User(s) has been activated!', 'userswp' ); |
| 304 |
break; |
| 305 |
} |
| 306 |
|
| 307 |
if ( ! empty( $messages ) ) { |
| 308 |
if ( isset( $messages['err_msg'] ) ) { |
| 309 |
echo '<div class="notice notice-error"><p>' . esc_html( $messages['err_msg'] ) . '</p></div>'; |
| 310 |
} else { |
| 311 |
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $messages['msg'] ) . '</p></div>'; |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Adds UsersWP JS to admin area. |
| 318 |
* |
| 319 |
* @return void |
| 320 |
* @package userswp |
| 321 |
* |
| 322 |
* @since 1.0.0 |
| 323 |
*/ |
| 324 |
public function admin_only_script() { |
| 325 |
|
| 326 |
// check page is userswp or not. |
| 327 |
if ( ! empty( $_GET['page'] ) && 'userswp' == $_GET['page'] ) { |
| 328 |
|
| 329 |
// check tab is general or not. |
| 330 |
if ( ! empty( $_GET['tab'] ) && 'general' == $_GET['tab'] ) { |
| 331 |
|
| 332 |
// check for login section. |
| 333 |
if ( ! empty( $_GET['section'] ) && 'login' == $_GET['section'] ) { |
| 334 |
?> |
| 335 |
<script type="text/javascript"> |
| 336 |
jQuery(document).ready(function () { |
| 337 |
var login_redirect_to = jQuery('#login_redirect_to'); |
| 338 |
login_redirect_to.on('change', function () { |
| 339 |
var value = jQuery(this).val(); |
| 340 |
var login_redirect_custom_obj = jQuery('#login_redirect_custom_url'); |
| 341 |
|
| 342 |
if ('-2' === value) { |
| 343 |
login_redirect_custom_obj.parent().parent().show(); |
| 344 |
} else { |
| 345 |
login_redirect_custom_obj.parent().parent().hide(); |
| 346 |
} |
| 347 |
}).change(); |
| 348 |
}); |
| 349 |
</script> |
| 350 |
<?php |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
if ( ! empty( $_GET['page'] ) && 'uwp_user_types' == $_GET['page'] ) { |
| 356 |
?> |
| 357 |
<script type="text/javascript"> |
| 358 |
jQuery(document).ready(function () { |
| 359 |
var uwp_registration_action = jQuery('#uwp_registration_action'); |
| 360 |
uwp_registration_action.on('change', function () { |
| 361 |
var value = jQuery(this).val(); |
| 362 |
var register_redirect_obj = jQuery('#register_redirect_to'); |
| 363 |
|
| 364 |
if ('auto_approve_login' === value) { |
| 365 |
register_redirect_obj.parent().parent().show(); |
| 366 |
} else { |
| 367 |
register_redirect_obj.parent().parent().hide(); |
| 368 |
} |
| 369 |
}).change(); |
| 370 |
|
| 371 |
var register_redirect_to = jQuery('#register_redirect_to'); |
| 372 |
register_redirect_to.on('change', function () { |
| 373 |
var value = jQuery(this).val(); |
| 374 |
var register_redirect_custom_obj = jQuery('#register_redirect_custom_url'); |
| 375 |
|
| 376 |
if ('-2' === value) { |
| 377 |
register_redirect_custom_obj.parent().show(); |
| 378 |
register_redirect_custom_obj.parent().prev().show(); |
| 379 |
} else { |
| 380 |
register_redirect_custom_obj.parent().hide(); |
| 381 |
register_redirect_custom_obj.parent().prev().hide(); |
| 382 |
} |
| 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 bsui"> |
| 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 style="margin: 0; padding: 0"> |
| 586 |
<th class="uwp-profile-extra-key" style="margin: 0; padding: 0"><h3 |
| 587 |
style="margin: 10px 0;"><?php echo esc_html( $field->site_title ); ?></h3></th> |
| 588 |
<td></td> |
| 589 |
</tr> |
| 590 |
<?php |
| 591 |
} else { |
| 592 |
?> |
| 593 |
<tr> |
| 594 |
<th class="uwp-profile-extra-key"><?php echo esc_html( $field->site_title ); ?></th> |
| 595 |
<td class="uwp-profile-extra-value"> |
| 596 |
<?php |
| 597 |
$templates_obj = new UsersWP_Templates(); |
| 598 |
$templates_obj->template_fields_html( $field, 'account', $user->ID ); |
| 599 |
?> |
| 600 |
</td> |
| 601 |
</tr> |
| 602 |
<?php |
| 603 |
} |
| 604 |
} |
| 605 |
?> |
| 606 |
</table> |
| 607 |
</div> |
| 608 |
<?php |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
/** |
| 613 |
* Adds avatar and banner fields in admin side. |
| 614 |
* |
| 615 |
* @param object $user User object. |
| 616 |
* |
| 617 |
* @return void |
| 618 |
* @since 1.0.0 |
| 619 |
* @package userswp |
| 620 |
* |
| 621 |
*/ |
| 622 |
public function edit_profile_banner_fields( $user ) { |
| 623 |
global $wpdb; |
| 624 |
|
| 625 |
$file_obj = new UsersWP_Files(); |
| 626 |
|
| 627 |
$table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 628 |
$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 |
| 629 |
if ( $fields ) { |
| 630 |
?> |
| 631 |
<div class="uwp-profile-extra uwp_page"> |
| 632 |
<?php do_action( 'uwp_admin_profile_edit', $user ); ?> |
| 633 |
<h2><?php esc_html_e( 'UsersWP Account Details', 'userswp' ); ?></h2> |
| 634 |
<table class="uwp-profile-extra-table form-table"> |
| 635 |
<?php |
| 636 |
foreach ( $fields as $field ) { |
| 637 |
|
| 638 |
if ( $field->field_type == 'fieldset' ) { |
| 639 |
?> |
| 640 |
<tr style="margin: 0; padding: 0"> |
| 641 |
<th class="uwp-profile-extra-key" style="margin: 0; padding: 0"><h3 |
| 642 |
style="margin: 10px 0;"> |
| 643 |
<?php echo esc_html( $field->site_title ); ?></h3></th> |
| 644 |
<td></td> |
| 645 |
</tr> |
| 646 |
<?php |
| 647 |
} else { |
| 648 |
?> |
| 649 |
<tr> |
| 650 |
<th class="uwp-profile-extra-key"><?php echo esc_html( $field->site_title ); ?></th> |
| 651 |
<td class="uwp-profile-extra-value"> |
| 652 |
<?php |
| 653 |
if ( $field->htmlvar_name == 'avatar' ) { |
| 654 |
$value = uwp_get_usermeta( $user->ID, 'avatar_thumb', '' ); |
| 655 |
} elseif ( $field->htmlvar_name == 'banner' ) { |
| 656 |
$value = uwp_get_usermeta( $user->ID, 'banner_thumb', '' ); |
| 657 |
} else { |
| 658 |
$value = ''; |
| 659 |
} |
| 660 |
|
| 661 |
echo $file_obj->file_upload_preview( $field, $value ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 662 |
|
| 663 |
if ( $field->htmlvar_name == 'avatar' ) { |
| 664 |
if ( ! empty( $value ) ) { |
| 665 |
$label = __( 'Change Avatar', 'userswp' ); |
| 666 |
} else { |
| 667 |
$label = __( 'Upload Avatar', 'userswp' ); |
| 668 |
} |
| 669 |
?> |
| 670 |
<a onclick="uwp_profile_image_change('avatar');return false;" href="#" |
| 671 |
class="uwp-banner-change-icon-admin"> |
| 672 |
<?php echo esc_html( $label ); ?> |
| 673 |
</a> |
| 674 |
<?php |
| 675 |
} elseif ( $field->htmlvar_name == 'banner' ) { |
| 676 |
if ( ! empty( $value ) ) { |
| 677 |
$label = __( 'Change Banner', 'userswp' ); |
| 678 |
} else { |
| 679 |
$label = __( 'Upload Banner', 'userswp' ); |
| 680 |
} |
| 681 |
?> |
| 682 |
<a onclick="uwp_profile_image_change('banner');return false;" href="#" |
| 683 |
class="uwp-banner-change-icon-admin"> |
| 684 |
<?php echo esc_html( $label ); ?> |
| 685 |
</a> |
| 686 |
<?php |
| 687 |
} |
| 688 |
?> |
| 689 |
</td> |
| 690 |
</tr> |
| 691 |
<?php |
| 692 |
} |
| 693 |
} |
| 694 |
?> |
| 695 |
</table> |
| 696 |
</div> |
| 697 |
<?php |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* Returns user row actions |
| 703 |
* |
| 704 |
* @param array $actions Date string. |
| 705 |
* @param object $user_object The User ID. |
| 706 |
* |
| 707 |
* @return array Row actions. |
| 708 |
* @package userswp |
| 709 |
* |
| 710 |
*/ |
| 711 |
public function user_row_actions( $actions, $user_object ) { |
| 712 |
$user_id = $user_object->ID; |
| 713 |
$mod_value = get_user_meta( $user_id, 'uwp_mod', true ); |
| 714 |
$resend_link = add_query_arg( |
| 715 |
array( |
| 716 |
'user_id' => $user_id, |
| 717 |
'action' => 'uwp_resend', |
| 718 |
'_nonce' => wp_create_nonce( 'uwp_resend' ), |
| 719 |
'uwp_is_admin' => 1, |
| 720 |
), |
| 721 |
admin_url( 'users.php' ) |
| 722 |
); |
| 723 |
|
| 724 |
$activate_link = add_query_arg( |
| 725 |
array( |
| 726 |
'user_id' => $user_id, |
| 727 |
'action' => 'uwp_activate_user', |
| 728 |
'_nonce' => wp_create_nonce( 'uwp_activate_user' ), |
| 729 |
'uwp_is_admin' => 1, |
| 730 |
), |
| 731 |
admin_url( 'users.php' ) |
| 732 |
); |
| 733 |
|
| 734 |
if ( $mod_value == 'email_unconfirmed' ) { |
| 735 |
$actions['uwp_resend_activation'] = "<a class='' href='" . $resend_link . "'>" . __( 'Resend Activation', 'userswp' ) . '</a>'; |
| 736 |
$actions['uwp_auto_activate'] = "<a class='' href='" . $activate_link . "'>" . __( 'Activate User', 'userswp' ) . '</a>'; |
| 737 |
} |
| 738 |
|
| 739 |
return $actions; |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Returns users bulk actions |
| 744 |
* |
| 745 |
* @param array $bulk_actions Bulk actions. |
| 746 |
* |
| 747 |
* @return array Bulk actions. |
| 748 |
* @package userswp |
| 749 |
* |
| 750 |
*/ |
| 751 |
public function users_bulk_actions( $bulk_actions ) { |
| 752 |
$bulk_actions['uwp_resend'] = __( 'Resend Activation', 'userswp' ); |
| 753 |
$bulk_actions['uwp_activate_user'] = __( 'Activate Users', 'userswp' ); |
| 754 |
|
| 755 |
return $bulk_actions; |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* Handles users bulk actions |
| 760 |
* |
| 761 |
* @param string $redirect_to Bulk actions. |
| 762 |
* @param string $doaction Current action. |
| 763 |
* @param array $user_ids User IDs to process. |
| 764 |
* |
| 765 |
* @return string Redirect URL. |
| 766 |
* @package userswp |
| 767 |
* |
| 768 |
*/ |
| 769 |
public function handle_users_bulk_actions( $redirect_to, $doaction, $user_ids ) { |
| 770 |
if ( 'uwp_resend' == $doaction ) { |
| 771 |
foreach ( $user_ids as $user_id ) { |
| 772 |
uwp_resend_activation_mail( $user_id ); |
| 773 |
} |
| 774 |
|
| 775 |
$redirect_to = add_query_arg( 'update', 'uwp_resend', $redirect_to ); |
| 776 |
} elseif ( 'uwp_activate_user' == $doaction ) { |
| 777 |
foreach ( $user_ids as $user_id ) { |
| 778 |
$this->activate_user( $user_id ); |
| 779 |
} |
| 780 |
$redirect_to = add_query_arg( 'update', 'uwp_activate_user', $redirect_to ); |
| 781 |
} |
| 782 |
|
| 783 |
return $redirect_to; |
| 784 |
} |
| 785 |
|
| 786 |
/** |
| 787 |
* Activates user |
| 788 |
* |
| 789 |
* @param int $user_id User ID |
| 790 |
* |
| 791 |
* @return bool |
| 792 |
*/ |
| 793 |
public function activate_user( $user_id = 0 ) { |
| 794 |
if ( ! $user_id ) { |
| 795 |
return false; |
| 796 |
} |
| 797 |
|
| 798 |
$uwp_mode = get_user_meta( $user_id, 'uwp_mod', true ); |
| 799 |
if ( 'email_unconfirmed' == $uwp_mode ) { |
| 800 |
delete_user_meta( $user_id, 'uwp_mod' ); |
| 801 |
do_action( 'uwp_email_activation_success', $user_id ); |
| 802 |
} |
| 803 |
|
| 804 |
return true; |
| 805 |
} |
| 806 |
|
| 807 |
/** |
| 808 |
* Processes user action |
| 809 |
* |
| 810 |
* @return mixed |
| 811 |
* @package userswp |
| 812 |
* |
| 813 |
*/ |
| 814 |
public function process_user_actions() { |
| 815 |
$user_id = isset( $_REQUEST['user_id'] ) ? (int) $_REQUEST['user_id'] : 0; |
| 816 |
$action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : false; |
| 817 |
$nonce = isset( $_REQUEST['_nonce'] ) ? sanitize_text_field( $_REQUEST['_nonce'] ) : false; |
| 818 |
$is_admin = isset( $_REQUEST['uwp_is_admin'] ) ? sanitize_text_field( $_REQUEST['uwp_is_admin'] ) : false; |
| 819 |
|
| 820 |
if ( $user_id && 'uwp_resend' == $action && ! empty( $nonce ) && wp_verify_nonce( $nonce, 'uwp_resend' ) ) { |
| 821 |
uwp_resend_activation_mail( $user_id ); |
| 822 |
if ( isset( $is_admin ) && $is_admin ) { |
| 823 |
wp_redirect( add_query_arg( 'update', 'uwp_resend', admin_url( 'users.php' ) ) ); |
| 824 |
exit(); |
| 825 |
} else { |
| 826 |
global $uwp_notices; |
| 827 |
$message = __( 'Activation email has been sent!', 'userswp' ); |
| 828 |
$uwp_notices[] = aui()->alert( |
| 829 |
array( |
| 830 |
'type' => 'success', |
| 831 |
'content' => $message, |
| 832 |
) |
| 833 |
); |
| 834 |
} |
| 835 |
} elseif ( $user_id && 'uwp_activate_user' == $action && wp_verify_nonce( $nonce, 'uwp_activate_user' ) ) { |
| 836 |
if ( isset( $is_admin ) && $is_admin && current_user_can( 'edit_users' ) ) { |
| 837 |
$this->activate_user( $user_id ); |
| 838 |
wp_redirect( add_query_arg( 'update', 'uwp_activate_user', admin_url( 'users.php' ) ) ); |
| 839 |
} |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
/** |
| 844 |
* Show row meta on the plugin screen. |
| 845 |
* |
| 846 |
* @param mixed $links Plugin Row Meta |
| 847 |
* @param mixed $file Plugin Base file |
| 848 |
* @return array |
| 849 |
*/ |
| 850 |
public static function plugin_row_meta( $links, $file ) { |
| 851 |
if ( USERSWP_PLUGIN_BASENAME == $file ) { |
| 852 |
$row_meta = array( |
| 853 |
'docs' => '<a href="' . esc_url( 'https://docs.userswp.io/' ) . '" aria-label="' . esc_attr__( 'View UsersWP Documentation', 'userswp' ) . '">' . esc_html__( 'Docs', 'userswp' ) . '</a>', |
| 854 |
'support' => '<a href="' . esc_url( 'https://userswp.io/support/' ) . '" aria-label="' . esc_attr__( 'Visit UsersWP support', 'userswp' ) . '">' . esc_html__( 'Support', 'userswp' ) . '</a>', |
| 855 |
'translation' => '<a href="' . esc_url( 'https://userswp.io/translate/projects' ) . '" aria-label="' . esc_attr__( 'View translations', 'userswp' ) . '">' . esc_html__( 'Translations', 'userswp' ) . '</a>', |
| 856 |
); |
| 857 |
|
| 858 |
return array_merge( $links, $row_meta ); |
| 859 |
} |
| 860 |
|
| 861 |
return (array) $links; |
| 862 |
} |
| 863 |
|
| 864 |
/** |
| 865 |
* Process the creation of a new registration form. |
| 866 |
* |
| 867 |
* This method handles the AJAX request to create a new registration form, |
| 868 |
* including validation, sanitization, and database operations. |
| 869 |
*/ |
| 870 |
public function process_create_register_form() { |
| 871 |
check_ajax_referer( 'uwp-create-register-form-nonce', 'uwp_create_register_form_nonce' ); |
| 872 |
|
| 873 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 874 |
wp_die( -1 ); |
| 875 |
} |
| 876 |
|
| 877 |
$form_title = isset( $_POST['form_title'] ) ? sanitize_text_field( $_POST['form_title'] ) : __( 'Form', 'userswp' ); |
| 878 |
$user_role = isset( $_POST['user_role'] ) ? sanitize_text_field( $_POST['user_role'] ) : ''; |
| 879 |
$action = isset( $_POST['reg_action'] ) ? sanitize_text_field( $_POST['reg_action'] ) : uwp_get_option( 'uwp_registration_action', 'auto_approve' ); |
| 880 |
$redirect_to = isset( $_POST['redirect_to'] ) ? (int)$_POST['redirect_to'] : 0; |
| 881 |
$custom_url = isset( $_POST['custom_url'] ) ? sanitize_text_field( $_POST['custom_url'] ) : ''; |
| 882 |
$gdpr_page = isset( $_POST['gdpr_page'] ) ? (int)$_POST['gdpr_page'] : (int)uwp_get_option( 'register_gdpr_page', false ); |
| 883 |
$tos_page = isset( $_POST['tos_page'] ) ? (int)$_POST['tos_page'] : (int)uwp_get_option( 'register_terms_page', false ); |
| 884 |
|
| 885 |
if ( empty( $form_title ) ) { |
| 886 |
wp_send_json_error( |
| 887 |
array( |
| 888 |
'message' => esc_html__( 'Form title is required.', 'userswp' ), |
| 889 |
) |
| 890 |
); |
| 891 |
} |
| 892 |
|
| 893 |
$status = false; |
| 894 |
$redirect = ''; |
| 895 |
|
| 896 |
$new_form_id = uwp_get_next_register_form_id(); |
| 897 |
if ( $new_form_id ) { |
| 898 |
$register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() ); |
| 899 |
// Filter out invalid items. |
| 900 |
$register_forms = array_filter( |
| 901 |
$register_forms, |
| 902 |
function ( $form ) { |
| 903 |
return isset( $form['id'] ); |
| 904 |
} |
| 905 |
); |
| 906 |
|
| 907 |
$register_forms[] = array( |
| 908 |
'id' => $new_form_id, |
| 909 |
'title' => ! empty( $form_title ) ? $form_title : sprintf( __( 'Form %d', 'userswp' ), $new_form_id ), |
| 910 |
'slug' => ! empty( $form_title ) ? sanitize_title( $form_title ) : sprintf( 'form-%d', $new_form_id ), |
| 911 |
'user_role' => $user_role, |
| 912 |
'reg_action' => $action, |
| 913 |
'redirect_to' => $redirect_to, |
| 914 |
'custom_url' => $custom_url, |
| 915 |
'gdpr_page' => $gdpr_page, |
| 916 |
'tos_page' => $tos_page, |
| 917 |
); |
| 918 |
|
| 919 |
$register_forms = array_values( $register_forms ); |
| 920 |
$register_forms = apply_filters( 'uwp_multiple_registration_forms_update', $register_forms, $new_form_id ); |
| 921 |
|
| 922 |
uwp_update_option( 'multiple_registration_forms', $register_forms ); |
| 923 |
|
| 924 |
$fields = UsersWP_Activator::uwp_default_custom_fields_account(); |
| 925 |
$form_builder = new UsersWP_Form_Builder(); |
| 926 |
|
| 927 |
foreach ( $fields as $field ) { |
| 928 |
$field['form_id'] = $new_form_id; |
| 929 |
$form_builder->admin_form_field_save( $field ); |
| 930 |
} |
| 931 |
|
| 932 |
UsersWP_Activator::insert_form_extras( $new_form_id ); |
| 933 |
|
| 934 |
do_action( 'uwp_create_register_form', $new_form_id ); |
| 935 |
|
| 936 |
$status = true; |
| 937 |
$redirect = admin_url( sprintf( 'admin.php?page=uwp_user_types&form=%d', $new_form_id ) ); |
| 938 |
} |
| 939 |
|
| 940 |
wp_send_json_success( |
| 941 |
array( |
| 942 |
'status' => $status, |
| 943 |
'message' => esc_html__( 'User Type added successfully.', 'userswp' ), |
| 944 |
'redirect' => $redirect, |
| 945 |
) |
| 946 |
); |
| 947 |
} |
| 948 |
|
| 949 |
/** |
| 950 |
* Process the update of an existing registration form. |
| 951 |
* |
| 952 |
* This method handles the AJAX request to update an existing registration form, |
| 953 |
* including validation, sanitization, and database operations. |
| 954 |
*/ |
| 955 |
public function process_update_register_form() { |
| 956 |
check_ajax_referer( 'uwp-update-register-form-nonce', 'uwp_update_register_form_nonce' ); |
| 957 |
|
| 958 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 959 |
wp_die( -1 ); |
| 960 |
} |
| 961 |
|
| 962 |
$form_id = isset( $_POST['manage_field_form_id'] ) ? (int)$_POST['manage_field_form_id'] : 0; |
| 963 |
$form_title = isset( $_POST['form_title'] ) && ! empty( $_POST['form_title'] ) ? sanitize_text_field( $_POST['form_title'] ) : __( 'Form', 'userswp' ); |
| 964 |
$user_role = isset( $_POST['user_role'] ) ? sanitize_text_field( $_POST['user_role'] ) : ''; |
| 965 |
$action = isset( $_POST['reg_action'] ) ? sanitize_text_field( $_POST['reg_action'] ) : uwp_get_option( 'uwp_registration_action', 'auto_approve' ); |
| 966 |
$redirect_to = isset( $_POST['redirect_to'] ) ? (int)$_POST['redirect_to'] : 0; |
| 967 |
$custom_url = isset( $_POST['custom_url'] ) ? sanitize_text_field( $_POST['custom_url'] ) : ''; |
| 968 |
$gdpr_page = isset( $_POST['gdpr_page'] ) ? (int)$_POST['gdpr_page'] : (int)uwp_get_option( 'register_gdpr_page', false ); |
| 969 |
$tos_page = isset( $_POST['tos_page'] ) ? (int)$_POST['tos_page'] : (int)uwp_get_option( 'register_terms_page', false ); |
| 970 |
|
| 971 |
if ( ! $form_id ) { |
| 972 |
wp_send_json_error( |
| 973 |
array( |
| 974 |
'message' => esc_html__( 'Something went wrong. Please try again.', 'userswp' ), |
| 975 |
) |
| 976 |
); |
| 977 |
} |
| 978 |
|
| 979 |
$redirect = ''; |
| 980 |
$status = false; |
| 981 |
|
| 982 |
$register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() ); |
| 983 |
|
| 984 |
foreach ( $register_forms as &$register_form ) { |
| 985 |
if ( $register_form['id'] == $form_id ) { |
| 986 |
$register_form['title'] = $form_title; |
| 987 |
$register_form['user_role'] = $user_role; |
| 988 |
$register_form['reg_action'] = $action; |
| 989 |
$register_form['redirect_to'] = $redirect_to; |
| 990 |
$register_form['custom_url'] = $custom_url; |
| 991 |
$register_form['gdpr_page'] = $gdpr_page; |
| 992 |
$register_form['tos_page'] = $tos_page; |
| 993 |
$status = true; |
| 994 |
break; |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
if ( $status ) { |
| 999 |
$register_forms = array_values( $register_forms ); |
| 1000 |
$register_forms = apply_filters( 'uwp_multiple_registration_forms_update', $register_forms, $form_id ); |
| 1001 |
uwp_update_option( 'multiple_registration_forms', $register_forms ); |
| 1002 |
$redirect = add_query_arg( |
| 1003 |
array( |
| 1004 |
'page' => 'uwp_user_types', |
| 1005 |
'form' => $form_id, |
| 1006 |
), |
| 1007 |
admin_url( 'admin.php' ) |
| 1008 |
); |
| 1009 |
} |
| 1010 |
|
| 1011 |
wp_send_json_success( |
| 1012 |
array( |
| 1013 |
'status' => $status, |
| 1014 |
'message' => esc_html__( 'The user type has been updated successfully.', 'userswp' ), |
| 1015 |
) |
| 1016 |
); |
| 1017 |
} |
| 1018 |
|
| 1019 |
public function reorder_user_types() { |
| 1020 |
check_ajax_referer( 'uwp_reorder_user_types', 'nonce' ); |
| 1021 |
|
| 1022 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 1023 |
wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'userswp' ) ) ); |
| 1024 |
} |
| 1025 |
|
| 1026 |
$order = isset( $_POST['order'] ) ? array_map( 'absint', $_POST['order'] ) : array(); |
| 1027 |
|
| 1028 |
if ( empty( $order ) ) { |
| 1029 |
wp_send_json_error( array( 'message' => __( 'Invalid order data.', 'userswp' ) ) ); |
| 1030 |
} |
| 1031 |
|
| 1032 |
$register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() ); |
| 1033 |
$new_order = array(); |
| 1034 |
|
| 1035 |
foreach ( $order as $id ) { |
| 1036 |
foreach ( $register_forms as $form ) { |
| 1037 |
if ( (int) $form['id'] === (int) $id ) { |
| 1038 |
$new_order[] = $form; |
| 1039 |
break; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
uwp_update_option( 'multiple_registration_forms', $new_order ); |
| 1045 |
|
| 1046 |
wp_send_json_success( array( 'message' => __( 'User types order updated successfully.', 'userswp' ) ) ); |
| 1047 |
} |
| 1048 |
|
| 1049 |
public function process_remove_register_form() { |
| 1050 |
|
| 1051 |
check_ajax_referer( 'uwp_delete_user_types', 'nonce' ); |
| 1052 |
|
| 1053 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 1054 |
wp_die( -1 ); |
| 1055 |
} |
| 1056 |
|
| 1057 |
$type = ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : ''; |
| 1058 |
$form_id = ! empty( $_POST['form_id'] ) ? (int) $_POST['form_id'] : ''; |
| 1059 |
|
| 1060 |
$status = false; |
| 1061 |
$message = __( 'Security nonce failed. Please try again.', 'userswp' ); |
| 1062 |
$redirect = ''; |
| 1063 |
|
| 1064 |
if ( ! empty( $type ) && ! empty( $form_id ) && $type === 'remove' ) { |
| 1065 |
$status = self::remove_registration_form( (int) $form_id ); |
| 1066 |
$redirect = admin_url( 'admin.php?page=uwp_user_types' ); |
| 1067 |
} |
| 1068 |
|
| 1069 |
wp_send_json( |
| 1070 |
array( |
| 1071 |
'status' => $status, |
| 1072 |
'message' => $message, |
| 1073 |
'redirect' => $redirect, |
| 1074 |
) |
| 1075 |
); |
| 1076 |
} |
| 1077 |
|
| 1078 |
/** |
| 1079 |
* Removes the registration form and its associated fields. |
| 1080 |
* |
| 1081 |
* @global wpdb $wpdb WordPress database abstraction object. |
| 1082 |
* |
| 1083 |
* @param int $form_id The form ID to remove. |
| 1084 |
* @return bool True if the form was found and removed; false otherwise. |
| 1085 |
*/ |
| 1086 |
public static function remove_registration_form( int $form_id ) { |
| 1087 |
global $wpdb; |
| 1088 |
|
| 1089 |
$table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 1090 |
$status = false; |
| 1091 |
$register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() ); |
| 1092 |
$form_builder = new UsersWP_Form_Builder(); |
| 1093 |
|
| 1094 |
if ( empty( $register_forms ) || ! is_array( $register_forms ) ) { |
| 1095 |
return $status; |
| 1096 |
} |
| 1097 |
|
| 1098 |
foreach ( $register_forms as $key => $register_form ) { |
| 1099 |
if ( empty( $register_form['id'] ) || (int) $register_form['id'] !== $form_id ) { |
| 1100 |
continue; |
| 1101 |
} |
| 1102 |
|
| 1103 |
$status = true; |
| 1104 |
unset( $register_forms[ $key ] ); |
| 1105 |
|
| 1106 |
$fields = $wpdb->get_results( |
| 1107 |
$wpdb->prepare( |
| 1108 |
"SELECT id FROM {$table_name} WHERE form_type = %s AND form_id = %d ORDER BY sort_order ASC", |
| 1109 |
'account', |
| 1110 |
$form_id |
| 1111 |
) |
| 1112 |
); |
| 1113 |
|
| 1114 |
if ( ! empty( $fields ) ) { |
| 1115 |
foreach ( $fields as $field ) { |
| 1116 |
$form_builder->admin_form_field_delete( (int) $field->id, false, $form_id ); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
} |
| 1120 |
|
| 1121 |
$register_forms = array_values( $register_forms ); |
| 1122 |
uwp_update_option( 'multiple_registration_forms', $register_forms ); |
| 1123 |
|
| 1124 |
return $status; |
| 1125 |
} |
| 1126 |
|
| 1127 |
/** |
| 1128 |
* Tell AyeCode UI to load on certain admin pages. |
| 1129 |
* |
| 1130 |
* @since 1.2.3.22 |
| 1131 |
* |
| 1132 |
* @param array $screen_ids Screen IDs. |
| 1133 |
* @return array Screen IDs. |
| 1134 |
*/ |
| 1135 |
public function add_aui_screens( $screen_ids ) { |
| 1136 |
// Load on these pages if set |
| 1137 |
if ( is_admin() && ! wp_doing_ajax() ) { |
| 1138 |
$screen_ids = array_merge( $screen_ids, uwp_get_screen_ids() ); |
| 1139 |
} |
| 1140 |
|
| 1141 |
// AUI is also needed for setup wizard. |
| 1142 |
$screen_ids[] = 'uwp-setup'; |
| 1143 |
|
| 1144 |
return $screen_ids; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|