| 1 |
<?php |
| 2 |
/** |
| 3 |
* UsersWP Tabs in form builder |
| 4 |
* |
| 5 |
* @author AyeCode |
| 6 |
* @category Admin |
| 7 |
* @package userswp/Admin |
| 8 |
* @version 1.0.24 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'UsersWP_Settings_User_Sorting', false ) ) { |
| 16 |
|
| 17 |
/** |
| 18 |
* UsersWP_Settings_User_Sorting. |
| 19 |
*/ |
| 20 |
class UsersWP_Settings_User_Sorting { |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
add_filter( 'uwp_form_builder_tabs_array', array( $this, 'form_builder_tab_items' ), 99 ); |
| 25 |
add_filter( 'uwp_form_builder_available_fields_head', array( $this, 'available_fields_head' ), 10, 2 ); |
| 26 |
add_filter( 'uwp_form_builder_available_fields_note', array( $this, 'available_fields_note' ), 10, 2 ); |
| 27 |
add_filter( 'uwp_form_builder_selected_fields_head', array( $this, 'selected_fields_head' ), 10, 2 ); |
| 28 |
add_filter( 'uwp_form_builder_selected_fields_note', array( $this, 'selected_fields_note' ), 10, 2 ); |
| 29 |
add_action( 'uwp_manage_available_fields', array( $this, 'manage_available_fields' ), 10, 1 ); |
| 30 |
add_action( 'uwp_manage_selected_fields', array( $this, 'manage_selected_fields' ), 10, 1 ); |
| 31 |
add_action( 'wp_ajax_uwp_ajax_user_sorting_action', array( $this, 'ajax_handler' ) ); |
| 32 |
add_action( 'uwp_add_custom_sort_options', array( $this, 'add_custom_sort_options' ) ); |
| 33 |
add_action( 'pre_user_query', array( $this, 'pre_user_query' ), 99 ); |
| 34 |
} |
| 35 |
|
| 36 |
public function pre_user_query( $vars ) { |
| 37 |
|
| 38 |
global $wpdb; |
| 39 |
|
| 40 |
if ( isset( $vars->query_vars['orderby'] ) && 'uwp_meta_value' == $vars->query_vars['orderby'] && is_uwp_users_page() ) { |
| 41 |
|
| 42 |
$sort_by = $meta_key = ''; |
| 43 |
$order = 'ASC'; |
| 44 |
if ( isset( $_GET['uwp_sort_by'] ) && $_GET['uwp_sort_by'] != '' ) { |
| 45 |
$sort_by = sanitize_key( strip_tags( esc_sql( $_GET['uwp_sort_by'] ) ) ); |
| 46 |
} |
| 47 |
|
| 48 |
if ( empty( $sort_by ) ) { |
| 49 |
$sort_by = uwp_get_default_sort(); |
| 50 |
} |
| 51 |
|
| 52 |
if ( $sort_by ) { |
| 53 |
|
| 54 |
if ( substr( strtolower( $sort_by ), - 5 ) == '_desc' ) { |
| 55 |
$order = 'DESC'; |
| 56 |
$meta_key = substr( $sort_by, 0, strlen( $sort_by ) - 5 ); |
| 57 |
$order_by = 'meta_value'; |
| 58 |
} elseif ( substr( strtolower( $sort_by ), - 4 ) == '_asc' ) { |
| 59 |
$order = 'ASC'; |
| 60 |
$meta_key = substr( $sort_by, 0, strlen( $sort_by ) - 4 ); |
| 61 |
$order_by = 'meta_value'; |
| 62 |
} |
| 63 |
|
| 64 |
// check meta key exists |
| 65 |
if ( $meta_key && 'user_registered' !== $meta_key ) { |
| 66 |
$table = new UsersWP_Tables(); |
| 67 |
if ( ! in_array( $meta_key, $table->get_db_usermeta_columns() ) ) { |
| 68 |
$meta_key = ''; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! empty( $order_by ) && $meta_key ) { |
| 73 |
$meta_table_name = uwp_get_table_prefix() . 'uwp_usermeta'; |
| 74 |
$table_name = uwp_get_table_prefix() . 'uwp_user_sorting'; |
| 75 |
|
| 76 |
if ( 'user_registered' == $meta_key ) { |
| 77 |
$orderby = $meta_key; |
| 78 |
} else { |
| 79 |
$orderby = $meta_table_name . '.' . sanitize_key( $meta_key ); |
| 80 |
} |
| 81 |
|
| 82 |
$vars->query_from .= ' INNER JOIN ' . $meta_table_name . ' ON (' . $meta_table_name . ".user_id = $wpdb->users.ID) "; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 83 |
$vars->query_orderby = 'ORDER BY ' . $orderby . ' ' . $order; |
| 84 |
|
| 85 |
$parent_id = $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM ' . $table_name . ' WHERE htmlvar_name = %s AND sort = %s AND tab_parent = 0', $meta_key, $order ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 86 |
|
| 87 |
if ( $parent_id ) { |
| 88 |
$children = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $table_name . ' WHERE tab_parent = %d ORDER BY sort_order ASC', $parent_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 89 |
|
| 90 |
if ( $children ) { |
| 91 |
|
| 92 |
foreach ( $children as $child ) { |
| 93 |
if ( ! in_array( $child->field_type, array( 'newer', 'older' ) ) ) { |
| 94 |
$vars->query_orderby .= ' , ' . $child->htmlvar_name . ' ' . $order; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ( is_uwp_users_page() && !empty($_GET['uwp_sort_by']) ) { |
| 104 |
if ($_GET['uwp_sort_by'] === 'random_asc' || $_GET['uwp_sort_by'] === 'random_desc') { |
| 105 |
// Random order |
| 106 |
$vars->query_orderby = 'ORDER BY RAND()'; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $vars; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Add a tab to form builder |
| 115 |
* |
| 116 |
* @since 1.2.2.38 |
| 117 |
* @package userswp |
| 118 |
* |
| 119 |
* @param array $tabs Tabs |
| 120 |
* |
| 121 |
* @return array $tabs |
| 122 |
*/ |
| 123 |
public function form_builder_tab_items( $tabs ) { |
| 124 |
$tabs['user-sorting'] = __( 'User Sorting', 'userswp' ); |
| 125 |
|
| 126 |
return $tabs; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Add a tab to form builder |
| 131 |
* |
| 132 |
* @since 1.2.2.38 |
| 133 |
* @package userswp |
| 134 |
* |
| 135 |
* @param string $heading Heading |
| 136 |
* @param string $form_type Form type |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public function available_fields_head( $heading, $form_type ) { |
| 141 |
switch ( $form_type ) { |
| 142 |
case 'user-sorting': |
| 143 |
$heading = __( 'Available sorting options for users listing and search results', 'userswp' ); |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
return $heading; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Add a note above available fields. |
| 152 |
* |
| 153 |
* @since 1.2.2.38 |
| 154 |
* @package userswp |
| 155 |
* |
| 156 |
* @param string $note Note to display |
| 157 |
* @param string $form_type Form type |
| 158 |
* |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
public function available_fields_note( $note, $form_type ) { |
| 162 |
switch ( $form_type ) { |
| 163 |
case 'user-sorting': |
| 164 |
$note = __( "Click on any box below to make it appear in the sorting option dropdown on users listing and search results. To make a field available here, go to account tab and expand any field from selected fields panel and tick the checkbox saying 'Include this field in sorting options'.", 'userswp' ); |
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
return $note; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Heading for the selected fields |
| 173 |
* |
| 174 |
* @since 1.2.2.38 |
| 175 |
* @package userswp |
| 176 |
* |
| 177 |
* @param string $heading Heading to display |
| 178 |
* @param string $form_type Form type |
| 179 |
* |
| 180 |
* @return string |
| 181 |
*/ |
| 182 |
public function selected_fields_head( $heading, $form_type ) { |
| 183 |
switch ( $form_type ) { |
| 184 |
case 'user-sorting': |
| 185 |
$heading = __( 'List of fields that will appear in users listing and search results sorting option dropdown box.', 'userswp' ); |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
return $heading; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Add a note above selected fields. |
| 194 |
* |
| 195 |
* @since 1.2.2.38 |
| 196 |
* @package userswp |
| 197 |
* |
| 198 |
* @param string $note Note to display |
| 199 |
* @param string $form_type Form type |
| 200 |
* |
| 201 |
* @return string |
| 202 |
*/ |
| 203 |
public function selected_fields_note( $note, $form_type ) { |
| 204 |
switch ( $form_type ) { |
| 205 |
case 'user-sorting': |
| 206 |
$note = __( 'Click to expand and view field related settings. You may drag and drop to arrange fields order in sorting option dropdown box on users listing and search results page.', 'userswp' ); |
| 207 |
break; |
| 208 |
} |
| 209 |
|
| 210 |
return $note; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Display available fields |
| 215 |
* |
| 216 |
* @since 1.2.2.38 |
| 217 |
* @package userswp |
| 218 |
* |
| 219 |
* @param string $form_type Form type |
| 220 |
* |
| 221 |
*/ |
| 222 |
public function manage_available_fields( $form_type ) { |
| 223 |
if ( 'user-sorting' == $form_type ) { |
| 224 |
$this->available_fields( $form_type ); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Display selected fields |
| 230 |
* |
| 231 |
* @since 1.2.2.38 |
| 232 |
* @package userswp |
| 233 |
* |
| 234 |
* @param string $form_type Form type |
| 235 |
* |
| 236 |
*/ |
| 237 |
public function manage_selected_fields( $form_type ) { |
| 238 |
if ( 'user-sorting' == $form_type ) { |
| 239 |
$this->selected_fields( $form_type ); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Display available sorting fields |
| 245 |
* |
| 246 |
* @since 1.2.2.38 |
| 247 |
* @package userswp |
| 248 |
* |
| 249 |
* @param string $form_type Form type |
| 250 |
* |
| 251 |
*/ |
| 252 |
public function available_fields( $form_type ) { |
| 253 |
global $wpdb; |
| 254 |
|
| 255 |
$fields = array(); |
| 256 |
|
| 257 |
$fields['newer'] = array( |
| 258 |
'data_type' => '', |
| 259 |
'field_type' => 'newer', |
| 260 |
'site_title' => __( 'Newer', 'userswp' ), |
| 261 |
'htmlvar_name' => 'newer', |
| 262 |
'field_icon' => 'fas fa-calendar', |
| 263 |
'sort' => 'asc', |
| 264 |
'description' => __( 'Sort by new user registration', 'userswp' ), |
| 265 |
); |
| 266 |
|
| 267 |
$fields['older'] = array( |
| 268 |
'data_type' => '', |
| 269 |
'field_type' => 'older', |
| 270 |
'site_title' => __( 'Older', 'userswp' ), |
| 271 |
'htmlvar_name' => 'older', |
| 272 |
'field_icon' => 'fas fa-calendar', |
| 273 |
'sort' => 'desc', |
| 274 |
'description' => __( 'Sort by new user registration in descending order', 'userswp' ), |
| 275 |
); |
| 276 |
|
| 277 |
$fields['display_name'] = array( |
| 278 |
'data_type' => '', |
| 279 |
'field_type' => 'text', |
| 280 |
'site_title' => __( 'Display Name', 'userswp' ), |
| 281 |
'htmlvar_name' => 'display_name', |
| 282 |
'field_icon' => 'fas fa-sort-alpha-up', |
| 283 |
'sort' => 'asc', |
| 284 |
'description' => __( 'Sort alphabetically by display name in ascending order', 'userswp' ), |
| 285 |
); |
| 286 |
|
| 287 |
$fields['first_name'] = array( |
| 288 |
'data_type' => '', |
| 289 |
'field_type' => 'text', |
| 290 |
'site_title' => __( 'First Name', 'userswp' ), |
| 291 |
'htmlvar_name' => 'first_name', |
| 292 |
'field_icon' => 'fas fa-sort-alpha-up', |
| 293 |
'sort' => 'asc', |
| 294 |
'description' => __( 'Sort alphabetically by first name in ascending order', 'userswp' ), |
| 295 |
); |
| 296 |
|
| 297 |
$fields['last_name'] = array( |
| 298 |
'data_type' => '', |
| 299 |
'field_type' => 'text', |
| 300 |
'site_title' => __( 'Last Name', 'userswp' ), |
| 301 |
'htmlvar_name' => 'last_name', |
| 302 |
'field_icon' => 'fas fa-sort-alpha-up', |
| 303 |
'sort' => 'asc', |
| 304 |
'description' => __( 'Sort alphabetically by last name in ascending order', 'userswp' ), |
| 305 |
); |
| 306 |
|
| 307 |
$fields['random'] = array( |
| 308 |
'data_type' => '', |
| 309 |
'field_type' => 'text', |
| 310 |
'site_title' => __( 'Random', 'userswp' ), |
| 311 |
'htmlvar_name' => 'random', |
| 312 |
'field_icon' => 'fa-solid fa-shuffle', |
| 313 |
'sort' => '', |
| 314 |
'description' => __( 'Sort users by random', 'userswp' ) |
| 315 |
); |
| 316 |
|
| 317 |
$fields = apply_filters( 'uwp_add_custom_sort_options', $fields ); |
| 318 |
|
| 319 |
?> |
| 320 |
<input type="hidden" name="form_type" id="form_type" value="<?php echo esc_attr( $form_type ); ?>"/> |
| 321 |
<input type="hidden" name="manage_field_type" class="manage_field_type" value="user_sorting"> |
| 322 |
<?php wp_nonce_field( 'uwp-admin-settings' ); ?> |
| 323 |
|
| 324 |
<ul class="row row-cols-2 px-2 mb-0"> |
| 325 |
<?php |
| 326 |
|
| 327 |
if ( ! empty( $fields ) ) { |
| 328 |
foreach ( $fields as $field ) { |
| 329 |
$field = stripslashes_deep( $field ); // strip slashes |
| 330 |
|
| 331 |
$icon = $field['field_icon']; |
| 332 |
if ( uwp_is_fa_icon( $icon ) ) { |
| 333 |
$field_icon = '<i class="' . esc_attr( $icon ) . '" aria-hidden="true"></i>'; |
| 334 |
} elseif ( uwp_is_icon_url( $icon ) ) { |
| 335 |
$field_icon = '<b style="background-image: url("' . esc_url( $icon ) . '")"></b>'; |
| 336 |
} else { |
| 337 |
$field_icon = '<i class="fas fa-sort" aria-hidden="true"></i>'; |
| 338 |
} |
| 339 |
?> |
| 340 |
<li class="col px-1"> |
| 341 |
<a id="uwp-<?php echo esc_attr( $field['htmlvar_name'] ); ?>" |
| 342 |
class="uwp-draggable-form-itemsx btn btn-sm d-block m-0 btn-outline-gray text-dark text-start" |
| 343 |
data-field_type="<?php echo isset( $field['field_type'] ) ? esc_attr( $field['field_type'] ) : ''; ?>" |
| 344 |
data-site_title="<?php echo isset( $field['site_title'] ) ? esc_attr( $field['site_title'] ) : ''; ?>" |
| 345 |
data-field_icon="<?php echo isset( $field['field_icon'] ) ? esc_attr( $field['field_icon'] ) : ''; ?>" |
| 346 |
data-id="<?php echo isset( $field['htmlvar_name'] ) ? esc_attr( $field['htmlvar_name'] ) : ''; ?>" |
| 347 |
data-data_type="<?php echo isset( $field['data_type'] ) ? esc_attr( $field['data_type'] ) : 'VARCHAR'; ?>" |
| 348 |
data-tab_parent="<?php echo isset( $field['tab_parent'] ) ? esc_attr( $field['tab_parent'] ) : ''; ?>" |
| 349 |
data-tab_level="<?php echo isset( $field['tab_content'] ) ? esc_attr( $field['tab_content'] ) : ''; ?>" |
| 350 |
data-sort="<?php echo isset( $field['sort'] ) ? esc_attr( $field['sort'] ) : 'asc'; ?>" |
| 351 |
href="javascript:void(0);"> |
| 352 |
<?php echo $field_icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 353 |
<?php echo esc_attr( $field['site_title'] ); ?> |
| 354 |
</a> |
| 355 |
</li> |
| 356 |
<?php |
| 357 |
} |
| 358 |
} |
| 359 |
?> |
| 360 |
</ul> |
| 361 |
<?php |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Display selected tabs fields |
| 366 |
* |
| 367 |
* @since 2.0.0 |
| 368 |
* @package userswp |
| 369 |
* |
| 370 |
* @param string $form_type Form type |
| 371 |
* |
| 372 |
*/ |
| 373 |
public function selected_fields( $form_type ) { |
| 374 |
global $wpdb; |
| 375 |
$table_name = uwp_get_table_prefix() . 'uwp_user_sorting'; |
| 376 |
?> |
| 377 |
<input type="hidden" name="form_type" id="form_type" value="<?php echo esc_attr( $form_type ); ?>"/> |
| 378 |
<input type="hidden" name="manage_field_type" class="manage_field_type" value="user_sorting"> |
| 379 |
<ul class="uwp-profile-tabs-selected core uwp_form_extras ps-0 list-group "> |
| 380 |
<?php |
| 381 |
$tabs = $wpdb->get_results( 'SELECT * FROM ' . $table_name . ' order by sort_order asc' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 382 |
if ( ! empty( $tabs ) ) { |
| 383 |
foreach ( $tabs as $key => $tab ) { |
| 384 |
$field_ins_upd = 'display'; |
| 385 |
|
| 386 |
if ( $tab->tab_level == '1' ) { |
| 387 |
continue; |
| 388 |
} |
| 389 |
|
| 390 |
ob_start(); |
| 391 |
$this->field_adminhtml( $tab->id, $field_ins_upd ); |
| 392 |
$tab_rendered = ob_get_clean(); |
| 393 |
|
| 394 |
$tab_rendered = str_replace( '</li>', '', $tab_rendered ); |
| 395 |
$child_tabs = ''; |
| 396 |
foreach ( $tabs as $child_tab ) { |
| 397 |
if ( $child_tab->tab_parent == $tab->id ) { |
| 398 |
ob_start(); |
| 399 |
$this->field_adminhtml( $child_tab->id, $field_ins_upd ); |
| 400 |
$child_tabs .= ob_get_clean(); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
if ( $child_tabs ) { |
| 405 |
$tab_rendered .= '<ul>'; |
| 406 |
$tab_rendered .= $child_tabs; |
| 407 |
$tab_rendered .= '</ul>'; |
| 408 |
} |
| 409 |
|
| 410 |
echo $tab_rendered; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 411 |
echo '</li>'; |
| 412 |
|
| 413 |
unset( $tabs[ $key ] ); |
| 414 |
} |
| 415 |
} |
| 416 |
?> |
| 417 |
</ul> |
| 418 |
<?php |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Displays sorting field HTML |
| 423 |
* |
| 424 |
* @since 2.0.0 |
| 425 |
* @package userswp |
| 426 |
* |
| 427 |
* @param int $field_id Field ID |
| 428 |
* @param string $field_ins_upd Field action |
| 429 |
* @param array $request Request data |
| 430 |
* |
| 431 |
*/ |
| 432 |
public function field_adminhtml( $field_id, $field_ins_upd = '', $request = array() ) { |
| 433 |
global $wpdb; |
| 434 |
|
| 435 |
$table_name = uwp_get_table_prefix() . 'uwp_user_sorting'; |
| 436 |
|
| 437 |
if ( ! is_object( $field_id ) && ( is_int( $field_id ) || ctype_digit( $field_id ) ) ) { |
| 438 |
$field = $wpdb->get_row( $wpdb->prepare( 'select * from ' . $table_name . ' where id= %d', array( $field_id ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 439 |
} elseif ( is_object( $field_id ) ) { |
| 440 |
$field_id = $field_id->id; |
| 441 |
$field = $wpdb->get_row( $wpdb->prepare( 'select * from ' . $table_name . ' where id= %d', array( (int) $field_id->id ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 442 |
} elseif ( isset( $field_id ) && ! empty( $field_id ) ) { |
| 443 |
$field = $wpdb->get_row( $wpdb->prepare( 'select * from ' . $table_name . ' where htmlvar_name= %s', array( $field_id ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 444 |
} else { |
| 445 |
$field = array(); |
| 446 |
} |
| 447 |
|
| 448 |
if ( isset( $request['site_title'] ) ) { |
| 449 |
$site_title = esc_attr( $request['site_title'] ); |
| 450 |
} elseif ( $field && isset( $field->site_title ) ) { |
| 451 |
$site_title = wp_unslash( $field->site_title ); |
| 452 |
} else { |
| 453 |
$site_title = ''; |
| 454 |
} |
| 455 |
|
| 456 |
if ( isset( $request['htmlvar_name'] ) ) { |
| 457 |
$htmlvar_name = esc_attr( $request['htmlvar_name'] ); |
| 458 |
} elseif ( $field && isset( $field->htmlvar_name ) ) { |
| 459 |
$htmlvar_name = $field->htmlvar_name; |
| 460 |
} else { |
| 461 |
$htmlvar_name = ''; |
| 462 |
} |
| 463 |
|
| 464 |
if ( isset( $request['field_type'] ) ) { |
| 465 |
$field_type = esc_attr( $request['field_type'] ); |
| 466 |
} elseif ( $field && isset( $field->field_type ) ) { |
| 467 |
$field_type = $field->field_type; |
| 468 |
} else { |
| 469 |
$field_type = ''; |
| 470 |
} |
| 471 |
|
| 472 |
if ( isset( $request['data_type'] ) ) { |
| 473 |
$data_type = esc_attr( $request['data_type'] ); |
| 474 |
} elseif ( $field && isset( $field->data_type ) ) { |
| 475 |
$data_type = $field->data_type; |
| 476 |
} else { |
| 477 |
$data_type = ''; |
| 478 |
} |
| 479 |
|
| 480 |
if ( isset( $request['field_icon'] ) && $request['field_icon'] != '' ) { |
| 481 |
$icon = esc_attr( $request['field_icon'] ); |
| 482 |
} elseif ( $field && isset( $field->field_icon ) ) { |
| 483 |
$icon = $field->field_icon; |
| 484 |
} else { |
| 485 |
$icon = 'fas fa-sort'; |
| 486 |
} |
| 487 |
|
| 488 |
if ( uwp_is_fa_icon( $icon ) ) { |
| 489 |
$field_icon = '<i class="' . esc_attr( $icon ) . '" aria-hidden="true"></i>'; |
| 490 |
} elseif ( uwp_is_icon_url( $icon ) ) { |
| 491 |
$field_icon = '<b style="background-image: url("' . esc_url( $icon ) . '")"></b>'; |
| 492 |
} elseif ( isset( $field->tab_type ) && $field->tab_type == 'fieldset' ) { |
| 493 |
$field_icon = '<i class="fas fa-arrows-h" aria-hidden="true"></i>'; |
| 494 |
} else { |
| 495 |
$field_icon = '<i class="fas fa-sort" aria-hidden="true"></i>'; |
| 496 |
} |
| 497 |
|
| 498 |
if ( isset( $request['tab_parent'] ) && $request['tab_parent'] != '' ) { |
| 499 |
$tab_parent = esc_attr( $request['tab_parent'] ); |
| 500 |
} elseif ( $field && isset( $field->tab_parent ) ) { |
| 501 |
$tab_parent = $field->tab_parent; |
| 502 |
} else { |
| 503 |
$tab_parent = 0; |
| 504 |
} |
| 505 |
|
| 506 |
if ( isset( $request['tab_level'] ) && $request['tab_level'] != '' ) { |
| 507 |
$tab_level = esc_attr( $request['tab_level'] ); |
| 508 |
} elseif ( $field && isset( $field->tab_level ) ) { |
| 509 |
$tab_level = $field->tab_level; |
| 510 |
} else { |
| 511 |
$tab_level = 0; |
| 512 |
} |
| 513 |
|
| 514 |
$nonce = wp_create_nonce( 'uwp_sort_extras_nonce_' . $field_id ); |
| 515 |
?> |
| 516 |
<li class="text li-settings" id="licontainer_<?php echo esc_attr( $field_id ); ?>"> |
| 517 |
<div class="title title<?php echo esc_attr( $field_id ); ?> uwp-fieldset hover-shadow dd-form d-flex justify-content-between rounded c-pointer list-group-item border rounded-smx text-start bg-light " onclick="uwp_tabs_item_settings(this);"> |
| 518 |
<?php |
| 519 |
?> |
| 520 |
<div class=" flex-fill font-weight-bold fw-bold"> |
| 521 |
<?php echo $field_icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 522 |
<b><?php echo esc_html( uwp_ucwords( ' ' . $site_title ) ); ?></b> |
| 523 |
<span class="field-type float-end text-end small"><?php echo ' (' . esc_html( uwp_ucwords( $field_type ) ) . ')'; ?></span> |
| 524 |
</div> |
| 525 |
<div class="dd-handle ui-sortable-handle"> |
| 526 |
<i class="far fa-trash-alt text-danger ml-2 ms-2" id="delete-16" |
| 527 |
onclick="delete_field('<?php echo esc_attr( $field_id ); ?>', '<?php echo esc_attr( wp_create_nonce( 'uwp_sort_delete_nonce_' . $field_id ) ); ?>', '<?php echo esc_attr( $htmlvar_name ); ?>', 'user_sorting');event.stopPropagation();return false;"></i> |
| 528 |
<i class="fas fa-grip-vertical text-muted ml-2 ms-2" style="cursor: move" aria-hidden="true"></i> |
| 529 |
</div> |
| 530 |
|
| 531 |
</div> |
| 532 |
|
| 533 |
<?php // store the form as a template. This saves a load of memory on page load. ?> |
| 534 |
<script type="text/template" class="dd-setting <?php echo 'dd-type-' . esc_attr( $field_type ); ?>"> |
| 535 |
<div id="field_frm<?php echo esc_attr( $field_id ); ?>" class="field_frm" style="<?php echo ( $field_ins_upd == 'submit' ) ? 'display:block;' : ''; ?>"> |
| 536 |
|
| 537 |
<input type="hidden" name="_wpnonce" id="uwp_sort_extras_nonce" value="<?php echo esc_attr( $nonce ); ?>"/> |
| 538 |
<input type="hidden" name="field_id" id="field_id" value="<?php echo esc_attr( $field_id ); ?>"/> |
| 539 |
<input type="hidden" name="form_type" id="form_type" value="user_sorting"/> |
| 540 |
<input type="hidden" name="field_icon" id="field_icon" value="<?php echo esc_attr( $icon ); ?>"/> |
| 541 |
<input type="hidden" name="field_type" id="field_type" value="<?php echo esc_attr( $field_type ); ?>"/> |
| 542 |
<input type="hidden" name="data_type" id="data_type" value="<?php echo esc_attr( $data_type ); ?>"/> |
| 543 |
<input type="hidden" name="tab_parent" value="<?php echo esc_attr( $tab_parent ); ?>"/> |
| 544 |
<input type="hidden" name="tab_level" value="<?php echo esc_attr( $tab_level ); ?>"/> |
| 545 |
<input type="hidden" name="htmlvar_name" value="<?php echo esc_attr( $htmlvar_name ); ?>"/> |
| 546 |
|
| 547 |
<?php |
| 548 |
// site_title |
| 549 |
echo aui()->input( |
| 550 |
array( |
| 551 |
'id' => 'site_title', |
| 552 |
'name' => 'site_title', |
| 553 |
'label_type' => 'top', |
| 554 |
'label' => esc_html__( 'Frontend title', 'userswp' ) . uwp_help_tip( __( 'This is the text used for the sort option.', 'userswp' ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 555 |
'type' => 'text', |
| 556 |
// 'wrap_class' => uwp_advanced_toggle_class(), |
| 557 |
'value' => $site_title, |
| 558 |
) |
| 559 |
); |
| 560 |
|
| 561 |
// sort |
| 562 |
$value = isset( $field->sort ) && $field->sort == 'desc' ? 'desc' : 'asc'; |
| 563 |
echo aui()->select( |
| 564 |
array( |
| 565 |
'id' => 'uwp-sort-' . ( isset( $field->id ) ? esc_attr( $field->id ) : '' ), |
| 566 |
'name' => 'sort', |
| 567 |
'label_type' => 'top', |
| 568 |
'multiple' => false, |
| 569 |
'class' => ' mw-100', |
| 570 |
'options' => array( |
| 571 |
'asc' => esc_html__( 'Ascending', 'userswp' ), |
| 572 |
'desc' => esc_html__( 'Descending', 'userswp' ), |
| 573 |
), |
| 574 |
'label' => __( 'Ascending or Descending', 'userswp' ) . uwp_help_tip( __( 'Select the sort direction: (A-Z or Z-A).', 'userswp' ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 575 |
'value' => $value, |
| 576 |
// 'wrap_class' => uwp_advanced_toggle_class(), |
| 577 |
) |
| 578 |
); |
| 579 |
|
| 580 |
echo aui()->input( |
| 581 |
array( |
| 582 |
'id' => 'is_default', |
| 583 |
'name' => 'is_default', |
| 584 |
'type' => 'checkbox', //'!$exclude_privacy_option ? 'hidden' : 'checkbox', |
| 585 |
'label_type' => 'horizontal', |
| 586 |
'label_col' => '4', |
| 587 |
'label' => __( 'Default sort', 'userswp' ), |
| 588 |
'checked' => isset( $field->is_default ) && $field->is_default == 1, |
| 589 |
'value' => '1', |
| 590 |
'switch' => 'md', |
| 591 |
'label_force_left' => true, |
| 592 |
'help_text' => uwp_help_tip( __( 'This sets the option as the overall default sort value, there can be only one.', 'userswp' ) ), |
| 593 |
) |
| 594 |
); |
| 595 |
|
| 596 |
echo aui()->input( |
| 597 |
array( |
| 598 |
'id' => 'is_active', |
| 599 |
'name' => 'is_active', |
| 600 |
'type' => 'checkbox', //'!$exclude_privacy_option ? 'hidden' : 'checkbox', |
| 601 |
'label_type' => 'horizontal', |
| 602 |
'label_col' => '4', |
| 603 |
'label' => __( 'Is active', 'userswp' ), |
| 604 |
'checked' => isset( $field->is_active ) && $field->is_active, |
| 605 |
'value' => '1', |
| 606 |
'switch' => 'md', |
| 607 |
'label_force_left' => true, |
| 608 |
'help_text' => uwp_help_tip( __( 'Set if this sort option is active or not, if not it will not be shown to users.', 'userswp' ) ), |
| 609 |
) |
| 610 |
); |
| 611 |
?> |
| 612 |
<input type="hidden" readonly="readonly" name="sort_order" id="sort_order" value="<?php echo (isset( $field->sort_order )) ? esc_attr( $field->sort_order ) : ''; ?>" size="50"/> |
| 613 |
<?php do_action( 'uwp_user_sorting_custom_fields', $field_id ); ?> |
| 614 |
|
| 615 |
<div class="uwp-input-wrap uwp-tab-actions" data-setting="save_button"> |
| 616 |
<a class=" btn btn-link text-muted" href="javascript:void(0);" onclick="uwp_tabs_close_settings(this); return false;"><?php _e( 'Close', 'userswp' ); ?></a> |
| 617 |
<a href='javascript:void(0);' type="button" class="btn btn-primary" id="save" |
| 618 |
onclick="save_field('<?php echo esc_attr( $field_id ); ?>', 'user_sorting');return false;"><?php echo esc_attr( __( 'Save', 'userswp' ) ); ?></a> |
| 619 |
</div> |
| 620 |
</div> |
| 621 |
</script> |
| 622 |
</li> |
| 623 |
<?php |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Handles tabs fields AJAX |
| 628 |
* |
| 629 |
* @since 2.0.0 |
| 630 |
* @package userswp |
| 631 |
* |
| 632 |
* @return mixed|void |
| 633 |
* |
| 634 |
*/ |
| 635 |
public function ajax_handler() { |
| 636 |
|
| 637 |
if ( isset( $_REQUEST['create_field'] ) ) { |
| 638 |
|
| 639 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 640 |
wp_die( - 1 ); |
| 641 |
} |
| 642 |
|
| 643 |
$field_id = isset( $_REQUEST['field_id'] ) ? trim( sanitize_text_field( $_REQUEST['field_id'] ), '_' ) : ''; |
| 644 |
$field_action = isset( $_REQUEST['field_ins_upd'] ) ? sanitize_text_field( $_REQUEST['field_ins_upd'] ) : ''; |
| 645 |
|
| 646 |
/* ------- check nonce field ------- */ |
| 647 |
if ( isset( $_REQUEST['update'] ) && $_REQUEST['update'] == 'update' ) { |
| 648 |
|
| 649 |
if ( ! empty( $_REQUEST['tabs'] ) && is_array( $_REQUEST['tabs'] ) ) { |
| 650 |
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'uwp-admin-settings' ) ) { |
| 651 |
return; |
| 652 |
} |
| 653 |
$tabs = $_REQUEST['tabs']; |
| 654 |
$this->update_field_order( $tabs ); |
| 655 |
|
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
/* ---- Show field form in admin ---- */ |
| 660 |
if ( $field_action == 'new' ) { |
| 661 |
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'uwp-admin-settings' ) ) { |
| 662 |
return; |
| 663 |
} |
| 664 |
$htmlvar_name = isset( $_REQUEST['htmlvar_name'] ) ? sanitize_text_field( $_REQUEST['htmlvar_name'] ) : sanitize_text_field( $_REQUEST['tab_key'] ); |
| 665 |
$this->field_adminhtml( $htmlvar_name, $field_action, $_REQUEST ); |
| 666 |
} |
| 667 |
|
| 668 |
/* ---- Delete field ---- */ |
| 669 |
if ( $field_id != '' && $field_action == 'delete' && isset( $_REQUEST['_wpnonce'] ) ) { |
| 670 |
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'uwp_sort_delete_nonce_' . $field_id ) ) { |
| 671 |
return; |
| 672 |
} |
| 673 |
|
| 674 |
$this->field_delete( $field_id ); |
| 675 |
} |
| 676 |
|
| 677 |
/* ---- Save field ---- */ |
| 678 |
if ( $field_action == 'submit' && isset( $_REQUEST['_wpnonce'] ) ) { |
| 679 |
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'uwp_sort_extras_nonce_' . $field_id ) ) { |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
foreach ( $_REQUEST as $pkey => $pval ) { |
| 684 |
$tags = is_array( $_REQUEST[ $pkey ] ) ? 'skip_field' : ''; |
| 685 |
|
| 686 |
if ( $tags != 'skip_field' ) { |
| 687 |
$_REQUEST[ $pkey ] = strip_tags( sanitize_text_field( $_REQUEST[ $pkey ] ), $tags ); |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
$lastid = $this->field_save( $_REQUEST ); |
| 692 |
|
| 693 |
if ( is_int( $lastid ) && $lastid > 0 ) { |
| 694 |
$this->field_adminhtml( $lastid, 'submit' ); |
| 695 |
} else { |
| 696 |
echo esc_html( $lastid ); |
| 697 |
} |
| 698 |
} |
| 699 |
} |
| 700 |
die(); |
| 701 |
} |
| 702 |
|
| 703 |
/** |
| 704 |
* Save tabs fields |
| 705 |
* |
| 706 |
* @since 2.0.0 |
| 707 |
* @package userswp |
| 708 |
* |
| 709 |
* @param array $request_field Request data |
| 710 |
* |
| 711 |
* @return string|int |
| 712 |
* |
| 713 |
*/ |
| 714 |
public function field_save( $request_field = array() ) { |
| 715 |
global $wpdb; |
| 716 |
$table_name = uwp_get_table_prefix() . 'uwp_user_sorting'; |
| 717 |
|
| 718 |
$id = isset( $request_field['field_id'] ) && $request_field['field_id'] ? absint( $request_field['field_id'] ) : ''; |
| 719 |
$data_type = isset( $request_field['data_type'] ) ? sanitize_text_field( $request_field['data_type'] ) : 'VARCHAR'; |
| 720 |
$field_type = isset( $request_field['field_type'] ) ? sanitize_text_field( $request_field['field_type'] ) : ''; |
| 721 |
$site_title = isset( $request_field['site_title'] ) ? sanitize_text_field( $request_field['site_title'] ) : ''; |
| 722 |
$htmlvar_name = ! empty( $request_field['htmlvar_name'] ) ? sanitize_text_field( $request_field['htmlvar_name'] ) : str_replace( |
| 723 |
array( |
| 724 |
'-', |
| 725 |
' ', |
| 726 |
'"', |
| 727 |
"'", |
| 728 |
), |
| 729 |
array( '_', '', '', '' ), |
| 730 |
sanitize_title_with_dashes( $request_field['site_title'] ) |
| 731 |
); |
| 732 |
$tab_parent = ! empty( $request_field['tab_parent'] ) ? sanitize_text_field( $request_field['tab_parent'] ) : 0; |
| 733 |
$tab_level = ! empty( $request_field['tab_level'] ) ? sanitize_text_field( $request_field['tab_level'] ) : 0; |
| 734 |
$field_icon = isset( $request_field['field_icon'] ) ? sanitize_text_field( $request_field['field_icon'] ) : ''; |
| 735 |
$is_active = isset( $request_field['is_active'] ) ? absint( $request_field['is_active'] ) : 0; |
| 736 |
$is_default = isset( $request_field['is_default'] ) ? absint( $request_field['is_default'] ) : 0; |
| 737 |
$sort = isset( $request_field['sort'] ) ? sanitize_text_field( $request_field['sort'] ) : 'asc'; |
| 738 |
|
| 739 |
$total_tabs = $wpdb->get_var( "SELECT COUNT(id) FROM {$table_name}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 740 |
|
| 741 |
if ( isset( $is_default ) && $is_default > 0 ) { |
| 742 |
$wpdb->query( 'update ' . $table_name . " set is_default='0' where is_default='1'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 743 |
} |
| 744 |
|
| 745 |
$data = array( |
| 746 |
'data_type' => $data_type, |
| 747 |
'field_type' => $field_type, |
| 748 |
'site_title' => $site_title, |
| 749 |
'htmlvar_name' => $htmlvar_name, |
| 750 |
'sort_order' => $total_tabs + 1, |
| 751 |
'tab_parent' => $tab_parent, |
| 752 |
'tab_level' => $tab_level, |
| 753 |
'field_icon' => $field_icon, |
| 754 |
'is_active' => $is_active, |
| 755 |
'is_default' => $is_default, |
| 756 |
'sort' => $sort, |
| 757 |
); |
| 758 |
|
| 759 |
$format = array_fill( 0, count( $data ), '%s' ); |
| 760 |
|
| 761 |
if ( $id ) { // update |
| 762 |
|
| 763 |
$wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 764 |
$table_name, |
| 765 |
$data, |
| 766 |
array( 'id' => $id ), |
| 767 |
$format |
| 768 |
); |
| 769 |
|
| 770 |
$lastid = $id; |
| 771 |
|
| 772 |
} else { // insert |
| 773 |
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 774 |
$table_name, |
| 775 |
$data, |
| 776 |
$format |
| 777 |
); |
| 778 |
|
| 779 |
$lastid = $wpdb->insert_id; |
| 780 |
} |
| 781 |
|
| 782 |
return (int) $lastid; |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* Save tabs fields |
| 787 |
* |
| 788 |
* @since 2.0.0 |
| 789 |
* @package userswp |
| 790 |
* |
| 791 |
* @param string $field_id Request data |
| 792 |
* |
| 793 |
* @return int |
| 794 |
* |
| 795 |
*/ |
| 796 |
public function field_delete( $field_id = '' ) { |
| 797 |
global $wpdb; |
| 798 |
$table_name = uwp_get_table_prefix() . 'uwp_user_sorting'; |
| 799 |
|
| 800 |
if ( $field_id != '' ) { |
| 801 |
$cf = trim( $field_id, '_' ); |
| 802 |
$wpdb->query( $wpdb->prepare( 'delete from ' . $table_name . ' where id= %d ', array( $cf ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 803 |
|
| 804 |
return $field_id; |
| 805 |
} else { |
| 806 |
return 0; |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* Updates user sorting sort order. |
| 812 |
* |
| 813 |
* @param array $tabs Tabs array. |
| 814 |
* |
| 815 |
* @return object|bool Sorted tabs. |
| 816 |
*/ |
| 817 |
public function update_field_order( $tabs = array() ) { |
| 818 |
global $wpdb; |
| 819 |
$table_name = uwp_get_table_prefix() . 'uwp_user_sorting'; |
| 820 |
|
| 821 |
$count = 0; |
| 822 |
if ( ! empty( $tabs ) ) { |
| 823 |
$result = false; |
| 824 |
foreach ( $tabs as $index => $tab ) { |
| 825 |
$result = $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 826 |
$table_name, |
| 827 |
array( |
| 828 |
'sort_order' => $index, |
| 829 |
'tab_level' => (int) $tab['tab_level'], |
| 830 |
'tab_parent' => (int) $tab['tab_parent'], |
| 831 |
), |
| 832 |
array( 'id' => absint( $tab['id'] ) ), |
| 833 |
array( '%d', '%d', '%d' ) |
| 834 |
); |
| 835 |
++$count; |
| 836 |
} |
| 837 |
if ( $result !== false ) { |
| 838 |
return true; |
| 839 |
} else { |
| 840 |
return new WP_Error( 'failed', __( 'Failed to sort tab items.', 'userswp' ) ); |
| 841 |
} |
| 842 |
} else { |
| 843 |
return new WP_Error( 'failed', __( 'Failed to sort tab items.', 'userswp' ) ); |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
/** |
| 848 |
* Show custom fields in the user sorting form builder. |
| 849 |
* |
| 850 |
* @param array $fields Fields array. |
| 851 |
* |
| 852 |
* @return array Merged fields array. |
| 853 |
*/ |
| 854 |
public function add_custom_sort_options( $fields ) { |
| 855 |
global $wpdb; |
| 856 |
$table_name = uwp_get_table_prefix() . 'uwp_form_fields'; |
| 857 |
|
| 858 |
$custom_fields = $wpdb->get_results( 'select data_type,field_type,site_title,htmlvar_name,field_icon from ' . $table_name . " where is_active='1' and user_sort='1' AND field_type != 'fieldset' order by sort_order asc", 'ARRAY_A' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 859 |
|
| 860 |
if ( ! empty( $custom_fields ) ) { |
| 861 |
|
| 862 |
foreach ( $custom_fields as $val ) { |
| 863 |
$fields[ $val['htmlvar_name'] ] = $val; |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
return $fields; |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
} |
| 872 |
|
| 873 |
new UsersWP_Settings_User_Sorting(); |
| 874 |
|