| 1 |
<?php |
| 2 |
/** |
| 3 |
* The UsersWP user types list table. |
| 4 |
* |
| 5 |
* @link http://wpgeodirectory.com |
| 6 |
* @since 1.2.3.11 |
| 7 |
* |
| 8 |
* @package UserWP |
| 9 |
* @subpackage UserWP/Admin/Tables |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 17 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Class UsersWP_User_Types_Table |
| 22 |
* |
| 23 |
* @since 1.2.3.11 |
| 24 |
*/ |
| 25 |
class UsersWP_User_Types_Table extends WP_List_Table { |
| 26 |
/** |
| 27 |
* Stores admin notices to be displayed |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
private $admin_notices = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor - Sets up the table properties and actions |
| 35 |
* |
| 36 |
*/ |
| 37 |
public function __construct() { |
| 38 |
parent::__construct( |
| 39 |
array( |
| 40 |
'singular' => __( 'User Type', 'userswp' ), |
| 41 |
'plural' => __( 'User Types', 'userswp' ), |
| 42 |
'ajax' => true, |
| 43 |
) |
| 44 |
); |
| 45 |
|
| 46 |
$this->process_bulk_action(); |
| 47 |
$this->display_admin_notices(); |
| 48 |
|
| 49 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Prepares table data for display |
| 54 |
* |
| 55 |
* Gets columns configuration, fetches table data, handles search filtering, |
| 56 |
* sorting, and pagination of items. |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function prepare_items() { |
| 61 |
$columns = $this->get_columns(); |
| 62 |
$hidden = $this->get_hidden_columns(); |
| 63 |
$sortable = $this->get_sortable_columns(); |
| 64 |
|
| 65 |
$data = $this->table_data(); |
| 66 |
|
| 67 |
$search = isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : ''; |
| 68 |
if ( $search ) { |
| 69 |
$data = $this->filter_data( $data, $search ); |
| 70 |
} |
| 71 |
|
| 72 |
// usort( $data, array( $this, 'sort_data' ) ); |
| 73 |
|
| 74 |
$per_page = $this->get_items_per_page( 'user_types_per_page', 10 ); |
| 75 |
$current_page = $this->get_pagenum(); |
| 76 |
$total_items = count( $data ); |
| 77 |
|
| 78 |
$this->set_pagination_args( |
| 79 |
array( |
| 80 |
'total_items' => $total_items, |
| 81 |
'per_page' => $per_page, |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page ); |
| 86 |
|
| 87 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 88 |
$this->items = $data; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Defines the table columns and their titles |
| 93 |
* |
| 94 |
* @return array Array of column names and their labels |
| 95 |
*/ |
| 96 |
public function get_columns() { |
| 97 |
$columns = array( |
| 98 |
'cb' => '<input type="checkbox" />', |
| 99 |
'title' => __( 'User Type', 'userswp' ), |
| 100 |
'user_role' => __( 'User Role', 'userswp' ), |
| 101 |
'reg_action' => __( 'Registration Action', 'userswp' ), |
| 102 |
'reg_link' => __( 'Register Link', 'userswp' ), |
| 103 |
'reg_lightbox' => __( 'Register Lightbox', 'userswp' ), |
| 104 |
// 'slug' => __( 'Slug', 'userswp' ), |
| 105 |
); |
| 106 |
|
| 107 |
$columns = apply_filters( 'uwp_user_types_table_columns', $columns ); |
| 108 |
$columns['reorder'] = __( 'Reorder', 'userswp' ); |
| 109 |
|
| 110 |
return $columns; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Specifies which columns should be hidden by default |
| 115 |
* |
| 116 |
* @return array Array of hidden column names |
| 117 |
*/ |
| 118 |
public function get_hidden_columns() { |
| 119 |
return array(); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Defines sortable columns and their sort direction |
| 124 |
* |
| 125 |
* @return array Array of sortable columns with their sort direction |
| 126 |
*/ |
| 127 |
public function get_sortable_columns() { |
| 128 |
return array( |
| 129 |
'title' => array( 'title', false ), |
| 130 |
'user_role' => array( 'user_role', false ), |
| 131 |
'reg_action' => array( 'reg_action', false ), |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Fetches and formats the table data |
| 137 |
* |
| 138 |
* @return array Formatted table data |
| 139 |
*/ |
| 140 |
private function table_data() { |
| 141 |
$data = array(); |
| 142 |
$register_forms = (array) uwp_get_option( 'multiple_registration_forms', array() ); |
| 143 |
|
| 144 |
$register_url = uwp_get_register_page_url(); |
| 145 |
$settings = uwp_get_settings(); |
| 146 |
$register_modal_form = ! empty( $settings['register_modal_form'] ) ? $settings['register_modal_form'] : array( 1 ); |
| 147 |
|
| 148 |
foreach ( $register_forms as $index => $register_form ) { |
| 149 |
|
| 150 |
$slug = $register_form['title'] ? sanitize_title_with_dashes( $register_form['title'] ) : ''; |
| 151 |
$reg_link_slug = $register_form['id'] > 1 ? add_query_arg( 'user_type', $slug, $register_url ) : $register_url; |
| 152 |
$reg_link_id = $register_form['id'] > 1 ? add_query_arg( 'user_type', absint( $register_form['id'] ), $register_url ) : $register_url; |
| 153 |
|
| 154 |
$copy_link_html = 'onclick="navigator.clipboard.writeText(this.href);aui_toast(\'uwp_user_reg_link_copied\', \'success\', \'' . esc_attr__( 'Link Copied!', 'userswp' ) . '\');return false;"';//; |
| 155 |
|
| 156 |
if ( isset( $register_form['id'], $register_form['title'] ) ) { |
| 157 |
$form_data = array( |
| 158 |
'id' => (int) $register_form['id'], |
| 159 |
'title' => $register_form['title'], |
| 160 |
// 'slug' => isset( $register_form['slug'] ) ? $register_form['slug'] : '', |
| 161 |
'user_role' => isset( $register_form['user_role'] ) ? $register_form['user_role'] : get_option( 'default_role' ), |
| 162 |
'reg_action' => isset( $register_form['reg_action'] ) ? $register_form['reg_action'] : 'auto_approve', |
| 163 |
'reg_link' => $register_form['id'] > 1 |
| 164 |
? '<a href="' . esc_url( $reg_link_slug ) . '" ' . $copy_link_html . ' >' . esc_html__( 'Slug', 'userswp' ) . '</a> | <a href="' . esc_url( $reg_link_id ) . '" ' . $copy_link_html . ' >' . esc_html__( 'ID', 'userswp' ) . '</a>' |
| 165 |
: '<a href="' . esc_url( $reg_link_id ) . '" ' . $copy_link_html . ' >' . esc_html__( 'Link', 'userswp' ) . '</a>', |
| 166 |
'reg_lightbox' => in_array( $register_form['id'], $register_modal_form ) ? __( 'Yes', 'userswp' ) : __( 'No', 'userswp' ) . ' (<a href="' . esc_url( admin_url( 'admin.php?page=userswp&tab=general§ion=register' ) ) . '">' . __( 'change', 'userswp' ) . '</a>)', |
| 167 |
'order' => $index, |
| 168 |
); |
| 169 |
|
| 170 |
$user_roles = uwp_get_user_roles(); |
| 171 |
$reg_actions = uwp_get_registration_form_actions(); |
| 172 |
|
| 173 |
$form_data['user_role'] = isset( $user_roles[ $form_data['user_role'] ] ) ? $user_roles[ $form_data['user_role'] ] : $form_data['user_role']; |
| 174 |
$form_data['reg_action'] = isset( $reg_actions[ $form_data['reg_action'] ] ) ? $reg_actions[ $form_data['reg_action'] ] : $form_data['reg_action']; |
| 175 |
|
| 176 |
$data[] = apply_filters( 'uwp_user_types_table_data', (array)$form_data, (array)$register_form ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return $data; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Handles default column value display |
| 185 |
* |
| 186 |
* @param array $item The current row item |
| 187 |
* @param string $column_name The current column name |
| 188 |
* @return mixed The column value or dash if empty |
| 189 |
*/ |
| 190 |
public function column_default( $item, $column_name ) { |
| 191 |
$value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : ''; |
| 192 |
$value = apply_filters( 'uwp_user_types_table_column_default', $value, $item, $column_name ); |
| 193 |
|
| 194 |
return $value === '' ? '—' : $value; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Customizes the display of the title column |
| 199 |
* |
| 200 |
* @param array $item The current row item |
| 201 |
* @return string Formatted title cell with row actions |
| 202 |
*/ |
| 203 |
public function column_title( $item ) { |
| 204 |
$edit_form_url = add_query_arg( 'form', (int) $item['id'], admin_url( 'admin.php?page=uwp_form_builder&tab=account' ) ); |
| 205 |
$edit_link = admin_url( sprintf( 'admin.php?page=uwp_user_types&form=%d', $item['id'] ) ); |
| 206 |
|
| 207 |
$title = sprintf( |
| 208 |
'<strong><a href="%s" class="row-title">%s</a></strong>', |
| 209 |
esc_url( $edit_link ), |
| 210 |
esc_html( $item['title'] ) |
| 211 |
); |
| 212 |
|
| 213 |
$actions = array( |
| 214 |
'id' => sprintf( '<span class="id">ID: %d</span>', $item['id'] ), |
| 215 |
'edit' => sprintf( '<a href="%s">%s</a>', esc_url( $edit_link ), esc_html__( 'Edit', 'userswp' ) ), |
| 216 |
'edit-form' => sprintf( '<a href="%s">%s</a>', esc_url( $edit_form_url ), esc_html__( 'Edit Fields', 'userswp' ) ), |
| 217 |
); |
| 218 |
|
| 219 |
if ( $item['id'] > 1 ) { |
| 220 |
$actions['delete'] = sprintf( |
| 221 |
'<a href="#" class="register-form-remove" data-id="%d">%s</a>', |
| 222 |
$item['id'], |
| 223 |
esc_html__( 'Delete', 'userswp' ) |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
$actions = apply_filters( 'uwp_user_types_table_actions', $actions, $item ); |
| 228 |
|
| 229 |
return $title . $this->row_actions( $actions ); |
| 230 |
} |
| 231 |
|
| 232 |
public function column_reorder( $item ) { |
| 233 |
return sprintf( |
| 234 |
'<span class="dashicons dashicons-move uwp-user-type-handle" style="cursor: move;" data-id="%s"></span>', |
| 235 |
$item['id'] |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Renders the checkbox column for bulk actions |
| 241 |
* |
| 242 |
* @param array $item The current row item |
| 243 |
* @return string Checkbox HTML |
| 244 |
*/ |
| 245 |
public function column_cb( $item ) { |
| 246 |
return sprintf( |
| 247 |
'<input type="checkbox" name="user_types[]" value="%s" />', |
| 248 |
$item['id'] |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Handles custom sorting of table data |
| 254 |
* |
| 255 |
* Sorts based on column values using GET parameters for order and direction. |
| 256 |
* Sanitizes input parameters for security. |
| 257 |
* |
| 258 |
* @param array $a First item to compare |
| 259 |
* @param array $b Second item to compare |
| 260 |
* @return int Comparison result (-1, 0, 1) |
| 261 |
*/ |
| 262 |
private function sort_data( $a, $b ) { |
| 263 |
// Set defaults |
| 264 |
$orderby = 'title'; |
| 265 |
$order = 'asc'; |
| 266 |
|
| 267 |
// If orderby is set, use this as the sort column |
| 268 |
if ( isset( $_GET['orderby'] ) && ! empty( $_GET['orderby'] ) ) { |
| 269 |
$orderby = sanitize_text_field( $_GET['orderby'] ); |
| 270 |
} |
| 271 |
|
| 272 |
// If order is set use this as the order |
| 273 |
if ( isset( $_GET['order'] ) && ! empty( $_GET['order'] ) ) { |
| 274 |
$order = sanitize_text_field( $_GET['order'] ); |
| 275 |
} |
| 276 |
|
| 277 |
$result = strcasecmp( $a[ $orderby ], $b[ $orderby ] ); |
| 278 |
|
| 279 |
return ( $order === 'asc' ) ? $result : -$result; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Filters table data based on search term |
| 284 |
* |
| 285 |
* Searches through all column values for matches with the search term. |
| 286 |
* |
| 287 |
* @param array $data Array of table data |
| 288 |
* @param string $search Search term to filter by |
| 289 |
* @return array Filtered data array |
| 290 |
*/ |
| 291 |
private function filter_data( $data, $search ) { |
| 292 |
$filtered_data = array_filter( |
| 293 |
$data, |
| 294 |
function ( $row ) use ( $search ) { |
| 295 |
foreach ( $row as $value ) { |
| 296 |
if ( stripos( $value, $search ) !== false ) { |
| 297 |
return true; |
| 298 |
} |
| 299 |
} |
| 300 |
return false; |
| 301 |
} |
| 302 |
); |
| 303 |
|
| 304 |
return $filtered_data; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Defines available bulk actions |
| 309 |
* |
| 310 |
* @return array Array of bulk actions |
| 311 |
*/ |
| 312 |
protected function get_bulk_actions() { |
| 313 |
$actions = array( |
| 314 |
'delete' => __( 'Delete', 'userswp' ), |
| 315 |
); |
| 316 |
return $actions; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Processes bulk actions |
| 321 |
* |
| 322 |
* Adds admin notice after successful deletion. |
| 323 |
*/ |
| 324 |
public function process_bulk_action() { |
| 325 |
|
| 326 |
if ( 'delete' === $this->current_action() ) { |
| 327 |
$user_types = isset( $_POST['user_types'] ) ? array_map( 'absint', $_POST['user_types'] ) : array(); |
| 328 |
|
| 329 |
if ( ! empty( $user_types ) ) { |
| 330 |
foreach ( $user_types as $user_type_id ) { |
| 331 |
UsersWP_Admin::remove_registration_form( (int) $user_type_id ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
$this->add_admin_notice( __( 'Selected user types have been deleted.', 'userswp' ) ); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Displays the table navigation |
| 341 |
* |
| 342 |
* @param string $which Location of the nav ('top' or 'bottom') |
| 343 |
*/ |
| 344 |
protected function display_tablenav( $which ) { |
| 345 |
?> |
| 346 |
<div class="tablenav <?php echo esc_attr( $which ); ?>"> |
| 347 |
<?php if ( $this->has_items() ) : ?> |
| 348 |
<div class="alignleft actions bulkactions"> |
| 349 |
<?php $this->bulk_actions( $which ); ?> |
| 350 |
</div> |
| 351 |
<?php |
| 352 |
endif; |
| 353 |
$this->pagination( $which ); |
| 354 |
?> |
| 355 |
<br class="clear" /> |
| 356 |
</div> |
| 357 |
<?php |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Adds an admin notice to be displayed |
| 362 |
* |
| 363 |
* @param string $message Notice message |
| 364 |
* @param string $type Notice type (success, error, warning, info) |
| 365 |
*/ |
| 366 |
public function add_admin_notice( $message, $type = 'success' ) { |
| 367 |
$this->admin_notices[] = array( |
| 368 |
'message' => $message, |
| 369 |
'type' => $type, |
| 370 |
); |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Displays queued admin notices |
| 375 |
*/ |
| 376 |
public function display_admin_notices() { |
| 377 |
foreach ( $this->admin_notices as $notice ) { |
| 378 |
?> |
| 379 |
<div class="notice notice-<?php echo esc_attr( $notice['type'] ); ?> is-dismissible"> |
| 380 |
<p><?php echo esc_html( $notice['message'] ); ?></p> |
| 381 |
</div> |
| 382 |
<?php |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|