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