| 1 |
<?php |
| 2 |
/** |
| 3 |
* The form builder functionality of the plugin. |
| 4 |
* |
| 5 |
* @link http://wpgeodirectory.com |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package UsersWP |
| 9 |
* @subpackage UsersWP/Admin/Settings |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* The form builder functionality of the plugin. |
| 19 |
* |
| 20 |
* @package UsersWP |
| 21 |
* @subpackage UsersWP/Admin/Settings |
| 22 |
* @author GeoDirectory Team <info@wpgeodirectory.com> |
| 23 |
*/ |
| 24 |
class UsersWP_User_Types { |
| 25 |
|
| 26 |
public static function get_register_forms() { |
| 27 |
$register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() ); |
| 28 |
return $register_forms; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Output the user types list or edit form. |
| 33 |
* |
| 34 |
* @param string $tab Current tab. |
| 35 |
*/ |
| 36 |
public static function output( string $tab = '' ) { |
| 37 |
do_action( 'uwp_user_types_start' ); |
| 38 |
|
| 39 |
if ( isset( $_GET['form'] ) ) { |
| 40 |
self::output_edit_form( $_GET['form'] ); |
| 41 |
} else { |
| 42 |
self::display_table(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Output the edit form for a user type. |
| 48 |
*/ |
| 49 |
private static function output_edit_form( $form_type ) { |
| 50 |
if ( $form_type === 'add' ) { |
| 51 |
self::display_add_form(); |
| 52 |
} else { |
| 53 |
self::display_edit_form(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Display the 'add' user type form. |
| 59 |
*/ |
| 60 |
private static function display_add_form() { |
| 61 |
$all_pages = wp_list_pluck( get_pages(), 'post_title', 'ID' ); |
| 62 |
$user_roles = uwp_get_user_roles(); |
| 63 |
$registration_form_actions = uwp_get_registration_form_actions(); |
| 64 |
$current_role = get_option( 'default_role' ); |
| 65 |
|
| 66 |
// Remove admin role |
| 67 |
unset( $user_roles['administrator'] ); |
| 68 |
?> |
| 69 |
<div class="multiple-registration-form wrap"> |
| 70 |
<h1 class="wp-heading-inline"><?php esc_html_e( 'Add User Type', 'userswp' ); ?></h1> |
| 71 |
<form class="uwp_user_type_form" id="uwp_user_type_form" method="POST" style="max-width: 700px;"> |
| 72 |
<input type="hidden" name="action" value="add"> |
| 73 |
<table class="form-table bsui userswp" id="uwp-form-more-options"> |
| 74 |
<?php |
| 75 |
wp_nonce_field( 'uwp-create-register-form-nonce', 'uwp_create_register_form_nonce' ); |
| 76 |
self::render_text_field( |
| 77 |
'uwp_form_title', |
| 78 |
'form_title', |
| 79 |
__( 'Title:', 'userswp' ), |
| 80 |
__( 'Title of the form', 'userswp' ), |
| 81 |
'', |
| 82 |
array( |
| 83 |
'required' => 'required' |
| 84 |
) |
| 85 |
); |
| 86 |
|
| 87 |
if ( ! empty( $user_roles ) && is_array( $user_roles ) ) { |
| 88 |
self::render_select_field( |
| 89 |
'multiple_registration_user_role', |
| 90 |
'user_role', |
| 91 |
__( 'User Role to Assign:', 'userswp' ), |
| 92 |
__( 'Role to assign when user register via this form.', 'userswp' ), |
| 93 |
$user_roles, |
| 94 |
$current_role |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
self::render_select_field( |
| 99 |
'uwp_registration_action', |
| 100 |
'reg_action', |
| 101 |
__( 'Registration Action:', 'userswp' ), |
| 102 |
__( 'Select how registration should be handled.', 'userswp' ), |
| 103 |
$registration_form_actions, |
| 104 |
'' |
| 105 |
); |
| 106 |
|
| 107 |
self::render_select_field( |
| 108 |
'register_redirect_to', |
| 109 |
'redirect_to', |
| 110 |
__( 'Redirect Page:', 'userswp' ), |
| 111 |
__( 'Set the page to redirect the user to after signing up.', 'userswp' ), |
| 112 |
self::get_redirect_options(), |
| 113 |
'', |
| 114 |
false, |
| 115 |
array( 'style' => 'display:none;' ) |
| 116 |
); |
| 117 |
|
| 118 |
self::render_text_field( |
| 119 |
'register_redirect_custom_url', |
| 120 |
'custom_url', |
| 121 |
__( 'Custom Redirect URL:', 'userswp' ), |
| 122 |
__( 'Set the page to redirect the user to after signing up. If default redirect has been set then WordPress default will be used.', 'userswp' ), |
| 123 |
'', |
| 124 |
array( 'style' => 'display:none;' ) |
| 125 |
); |
| 126 |
|
| 127 |
self::render_select_field( |
| 128 |
'multiple_registration_gdpr_page', |
| 129 |
'gdpr_page', |
| 130 |
__( 'GDPR Policy Page:', 'userswp' ), |
| 131 |
__( 'Page to link when GDPR policy page custom field added to form. If not set then default setting will be used.', 'userswp' ), |
| 132 |
$all_pages, |
| 133 |
'', |
| 134 |
esc_attr__( 'Select a page…', 'userswp' ) |
| 135 |
); |
| 136 |
|
| 137 |
self::render_select_field( |
| 138 |
'multiple_registration_tos_page', |
| 139 |
'tos_page', |
| 140 |
__( 'TOS Page:', 'userswp' ), |
| 141 |
__( 'Page to link when Terms and Conditions custom field added to form. If not set then default setting will be used.', 'userswp' ), |
| 142 |
$all_pages, |
| 143 |
'', |
| 144 |
esc_attr__( 'Select a page…', 'userswp' ) |
| 145 |
); |
| 146 |
?> |
| 147 |
</table> |
| 148 |
|
| 149 |
<?php |
| 150 |
do_action( 'uwp_user_type_form_before_submit', array() ); |
| 151 |
?> |
| 152 |
|
| 153 |
<div class="bsui"> |
| 154 |
<div class="alert alert-success d-none"></div> |
| 155 |
<div class="alert alert-danger d-none"></div> |
| 156 |
</div> |
| 157 |
|
| 158 |
<div class="bsui"> |
| 159 |
<button class="btn btn-md btn-primary" id="form_update" type="submit" name="form_update"> |
| 160 |
<?php esc_html_e( 'Add User Type', 'userswp' ); ?> |
| 161 |
</button> |
| 162 |
</div> |
| 163 |
</form> |
| 164 |
</div> |
| 165 |
<?php |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Display the 'edit' user type form. |
| 170 |
*/ |
| 171 |
private static function display_edit_form() { |
| 172 |
$form_id = isset( $_GET['form'] ) ? (int) $_GET['form'] : 1; |
| 173 |
$register_forms = self::get_register_forms(); |
| 174 |
|
| 175 |
if ( ! empty( $_GET['form_type'] ) && $_GET['form_type'] === 'new' ) { |
| 176 |
$new_added = ! empty( $register_forms ) ? end( $register_forms ) : array(); |
| 177 |
$form_id = ! empty( $new_added['id'] ) ? $new_added['id'] : 1; |
| 178 |
} |
| 179 |
|
| 180 |
?> |
| 181 |
<div class="multiple-registration-form wrap"> |
| 182 |
<h1 class="wp-heading-inline"><?php esc_html_e( 'Edit User Type', 'userswp' ); ?></h1> |
| 183 |
<form class="uwp_user_type_form" id="uwp_user_type_form" method="POST" style="max-width: 700px;"> |
| 184 |
<input type="hidden" name="action" value="edit"> |
| 185 |
<input type="hidden" name="manage_field_form_id" class="manage_field_form_id" |
| 186 |
id="manage_field_form_id" value="<?php echo esc_attr( $form_id ); ?>"> |
| 187 |
<?php |
| 188 |
do_action( 'uwp_user_type_form_before', $form_id ); |
| 189 |
self::update_form( $form_id ); |
| 190 |
wp_nonce_field( 'uwp-update-register-form-nonce', 'uwp_update_register_form_nonce' ); |
| 191 |
do_action( 'uwp_user_type_form_after', $form_id ); |
| 192 |
?> |
| 193 |
</form> |
| 194 |
</div> |
| 195 |
<?php |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Output the user types list. |
| 200 |
*/ |
| 201 |
private static function display_table() { |
| 202 |
$nonce = wp_create_nonce( 'uwp-create-register-form-nonce' ); |
| 203 |
$table = new UsersWP_User_Types_Table(); |
| 204 |
$table->prepare_items(); |
| 205 |
?> |
| 206 |
<div class="wrap"> |
| 207 |
<h1 class="wp-heading-inline"><?php esc_html_e( 'User Types', 'userswp' ); ?></h1> |
| 208 |
<a href="<?php echo esc_url( admin_url( 'admin.php?page=uwp_user_types&form=add' ) ); ?>" class="page-title-action"> |
| 209 |
<?php esc_html_e( 'Add User Type', 'userswp' ); ?> |
| 210 |
</a> |
| 211 |
|
| 212 |
<p><?php esc_html_e( 'Create and manage different user types with custom roles and registration forms.', 'userswp' ); ?></p> |
| 213 |
|
| 214 |
<form method="POST"> |
| 215 |
<?php $table->display(); ?> |
| 216 |
</form> |
| 217 |
</div> |
| 218 |
<?php |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Update the user type form. |
| 223 |
* |
| 224 |
* @param int $form_id The form ID. |
| 225 |
*/ |
| 226 |
public static function update_form( int $form_id ) { |
| 227 |
$register_forms = self::get_register_forms(); |
| 228 |
$form_key = array_search( $form_id, wp_list_pluck( $register_forms, 'id' ) ); |
| 229 |
$current_form = $register_forms[ $form_key ]; |
| 230 |
$current_action = ! empty( $current_form['reg_action'] ) ? $current_form['reg_action'] : uwp_get_option( 'uwp_registration_action', false ); |
| 231 |
|
| 232 |
$user_roles = uwp_get_user_roles(); |
| 233 |
$all_pages = wp_list_pluck( get_pages(), 'post_title', 'ID' ); |
| 234 |
$registration_form_actions = uwp_get_registration_form_actions(); |
| 235 |
$current_role = get_option( 'default_role' ); |
| 236 |
|
| 237 |
// Remove admin role |
| 238 |
unset( $user_roles['administrator'] ); |
| 239 |
|
| 240 |
$user_role = ! empty( $current_form['user_role'] ) ? $current_form['user_role'] : ''; |
| 241 |
if ( ! empty( $user_role ) && in_array( $user_role, array_keys( $user_roles ), true ) ) { |
| 242 |
$current_role = $user_role; |
| 243 |
} |
| 244 |
?> |
| 245 |
<table class="form-table bsui userswp" id="uwp-form-more-options"> |
| 246 |
<?php |
| 247 |
self::render_text_field( |
| 248 |
'uwp_form_title', |
| 249 |
'form_title', |
| 250 |
__( 'Title:', 'userswp' ), |
| 251 |
__( 'Title of the form', 'userswp' ), |
| 252 |
! empty( $current_form['title'] ) ? $current_form['title'] : '' |
| 253 |
); |
| 254 |
|
| 255 |
if ( ! empty( $user_roles ) && is_array( $user_roles ) ) { |
| 256 |
self::render_select_field( |
| 257 |
'multiple_registration_user_role', |
| 258 |
'user_role', |
| 259 |
__( 'User Role to Assign:', 'userswp' ), |
| 260 |
__( 'Role to assign when user register via this form.', 'userswp' ), |
| 261 |
$user_roles, |
| 262 |
$current_role |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
self::render_select_field( |
| 267 |
'uwp_registration_action', |
| 268 |
'reg_action', |
| 269 |
__( 'Registration Action:', 'userswp' ), |
| 270 |
__( 'Select how registration should be handled.', 'userswp' ), |
| 271 |
$registration_form_actions, |
| 272 |
$current_action |
| 273 |
); |
| 274 |
|
| 275 |
self::render_select_field( |
| 276 |
'register_redirect_to', |
| 277 |
'redirect_to', |
| 278 |
__( 'Redirect Page:', 'userswp' ), |
| 279 |
__( 'Set the page to redirect the user to after signing up.', 'userswp' ), |
| 280 |
self::get_redirect_options(), |
| 281 |
! empty( $current_form['redirect_to'] ) ? $current_form['redirect_to'] : '', |
| 282 |
false, |
| 283 |
array( 'style' => 'display:none;' ) |
| 284 |
); |
| 285 |
|
| 286 |
self::render_text_field( |
| 287 |
'register_redirect_custom_url', |
| 288 |
'custom_url', |
| 289 |
__( 'Custom Redirect URL:', 'userswp' ), |
| 290 |
__( 'Set the page to redirect the user to after signing up. If default redirect has been set then WordPress default will be used.', 'userswp' ), |
| 291 |
! empty( $current_form['custom_url'] ) ? $current_form['custom_url'] : '', |
| 292 |
array( 'style' => 'display:none;' ) |
| 293 |
); |
| 294 |
|
| 295 |
self::render_select_field( |
| 296 |
'multiple_registration_gdpr_page', |
| 297 |
'gdpr_page', |
| 298 |
__( 'GDPR Policy Page:', 'userswp' ), |
| 299 |
__( 'Page to link when GDPR policy page custom field added to form. If not set then default setting will be used.', 'userswp' ), |
| 300 |
$all_pages, |
| 301 |
! empty( $current_form['gdpr_page'] ) ? (int) $current_form['gdpr_page'] : '', |
| 302 |
esc_attr__( 'Select a page…', 'userswp' ) |
| 303 |
); |
| 304 |
|
| 305 |
self::render_select_field( |
| 306 |
'multiple_registration_tos_page', |
| 307 |
'tos_page', |
| 308 |
__( 'TOS Page:', 'userswp' ), |
| 309 |
__( 'Page to link when Terms and Conditions custom field added to form. If not set then default setting will be used.', 'userswp' ), |
| 310 |
$all_pages, |
| 311 |
! empty( $current_form['tos_page'] ) ? (int) $current_form['tos_page'] : '', |
| 312 |
esc_attr__( 'Select a page…', 'userswp' ) |
| 313 |
); |
| 314 |
?> |
| 315 |
</table> |
| 316 |
|
| 317 |
<?php do_action( 'uwp_user_type_form_before_submit', $current_form ); ?> |
| 318 |
|
| 319 |
<div class="bsui"> |
| 320 |
<div class="alert alert-success d-none"></div> |
| 321 |
<div class="alert alert-danger d-none"></div> |
| 322 |
</div> |
| 323 |
|
| 324 |
<div class="bsui"> |
| 325 |
<button class="btn btn-sm btn-primary" id="form_update" type="submit" name="form_update"> |
| 326 |
<?php esc_html_e( 'Update', 'userswp' ); ?> |
| 327 |
</button> |
| 328 |
<a href="<?php echo esc_url( add_query_arg( 'form', $form_id, admin_url( 'admin.php?page=uwp_form_builder&tab=account' ) ) ); ?>" |
| 329 |
class="btn btn-sm btn-link" |
| 330 |
target="_blank"><?php esc_html_e( 'Edit registration form', 'userswp' ); ?></a> |
| 331 |
</div> |
| 332 |
<?php |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Render a text field. |
| 337 |
* |
| 338 |
* @param string $id Field id. |
| 339 |
* @param string $name Field name. |
| 340 |
* @param string $label Field label. |
| 341 |
* @param string $description Field description. |
| 342 |
* @param string $value Field value. |
| 343 |
* @param array $attributes Field attributes. |
| 344 |
*/ |
| 345 |
private static function render_text_field( string $id, string $name, string $label, string $description, string $value, array $attributes = array() ) { |
| 346 |
$attr_string = ''; |
| 347 |
foreach ( $attributes as $attr => $attr_value ) { |
| 348 |
$attr_string .= ' ' . $attr . '="' . esc_attr( $attr_value ) . '"'; |
| 349 |
} |
| 350 |
?> |
| 351 |
<tr<?php echo $attr_string; ?>> |
| 352 |
<th> |
| 353 |
<?php |
| 354 |
echo esc_html( $label ); |
| 355 |
echo wp_kses_post( uwp_help_tip( $description ) ); |
| 356 |
?> |
| 357 |
</th> |
| 358 |
<td> |
| 359 |
<input id="<?php echo esc_attr( $id ); ?>" type="text" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>" class="bsui form-control" /> |
| 360 |
<div class="invalid-feedback"></div> |
| 361 |
</td> |
| 362 |
</tr> |
| 363 |
<?php |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Render a select field. |
| 368 |
* |
| 369 |
* @param string $id Field id. |
| 370 |
* @param string $name Field name. |
| 371 |
* @param string $label Field label. |
| 372 |
* @param string $description Field description. |
| 373 |
* @param array $options Field options. |
| 374 |
* @param string $value Field value. |
| 375 |
* @param string $placeholder Field placeholder. |
| 376 |
* @param array $attributes Field attributes. |
| 377 |
*/ |
| 378 |
private static function render_select_field( string $id, string $name, string $label, string $description, array $options, string $value, $placeholder = null, array $attributes = array() ) { |
| 379 |
$placeholder = is_null( $placeholder ) ? sprintf( __( 'Select a %s…', 'userswp' ), strtolower( $label ) ) : $placeholder; |
| 380 |
$attr_string = ''; |
| 381 |
foreach ( $attributes as $attr => $attr_value ) { |
| 382 |
$attr_string .= ' ' . $attr . '="' . esc_attr( $attr_value ) . '"'; |
| 383 |
} |
| 384 |
?> |
| 385 |
<tr <?php echo $attr_string; ?>> |
| 386 |
<th> |
| 387 |
<?php |
| 388 |
echo esc_html( $label ); |
| 389 |
echo wp_kses_post( uwp_help_tip( $description ) ); |
| 390 |
?> |
| 391 |
</th> |
| 392 |
<td> |
| 393 |
<?php |
| 394 |
aui()->select( |
| 395 |
array( |
| 396 |
'id' => esc_attr( $id ), |
| 397 |
'name' => $name, |
| 398 |
'placeholder' => $placeholder, |
| 399 |
'options' => $options, |
| 400 |
'value' => $value, |
| 401 |
'no_wrap' => true, |
| 402 |
'select2' => true, |
| 403 |
'class' => 'w-100', |
| 404 |
), |
| 405 |
true |
| 406 |
); |
| 407 |
?> |
| 408 |
</td> |
| 409 |
</tr> |
| 410 |
<?php |
| 411 |
} |
| 412 |
|
| 413 |
private static function get_redirect_options() { |
| 414 |
$pages = get_pages(); |
| 415 |
$pages_options = array( |
| 416 |
'-1' => __( 'Last User Page', 'userswp' ), |
| 417 |
'0' => __( 'Default Redirect', 'userswp' ), |
| 418 |
'-2' => __( 'Custom Redirect', 'userswp' ), |
| 419 |
); |
| 420 |
if ( $pages ) { |
| 421 |
foreach ( $pages as $page ) { |
| 422 |
$pages_options[ $page->ID ] = $page->post_title; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
return $pages_options; |
| 427 |
} |
| 428 |
} |
| 429 |
|