| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Log List Table. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Log_List_Table |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.11 |
| 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 |
if ( ! class_exists( 'Charitable_Log_List_Table' ) ) : |
| 21 |
|
| 22 |
/** |
| 23 |
* Charitable_Log_List_Table |
| 24 |
* |
| 25 |
* @since 1.8.11 |
| 26 |
*/ |
| 27 |
class Charitable_Log_List_Table extends WP_List_Table { |
| 28 |
|
| 29 |
/** |
| 30 |
* Total number of records. |
| 31 |
* |
| 32 |
* @since 1.8.11 |
| 33 |
* |
| 34 |
* @var int |
| 35 |
*/ |
| 36 |
private $total_items = 0; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor. |
| 40 |
* |
| 41 |
* @since 1.8.11 |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
parent::__construct( |
| 45 |
array( |
| 46 |
'plural' => esc_html__( 'Logs', 'charitable' ), |
| 47 |
'singular' => esc_html__( 'Log', 'charitable' ), |
| 48 |
'ajax' => false, |
| 49 |
) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get table columns. |
| 55 |
* |
| 56 |
* @since 1.8.11 |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function get_columns() { |
| 61 |
return array( |
| 62 |
'title' => __( 'Title', 'charitable' ), |
| 63 |
'message' => __( 'Message', 'charitable' ), |
| 64 |
'types' => __( 'Types', 'charitable' ), |
| 65 |
'level' => __( 'Level', 'charitable' ), |
| 66 |
'source' => __( 'Source', 'charitable' ), |
| 67 |
'date' => __( 'Date', 'charitable' ), |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get sortable columns. |
| 73 |
* |
| 74 |
* @since 1.8.11 |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function get_sortable_columns() { |
| 79 |
return array( |
| 80 |
'title' => array( 'title', false ), |
| 81 |
'level' => array( 'level', false ), |
| 82 |
'source' => array( 'source', false ), |
| 83 |
'date' => array( 'create_at', true ), |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Prepare items for display. |
| 89 |
* |
| 90 |
* @since 1.8.11 |
| 91 |
*/ |
| 92 |
public function prepare_items() { |
| 93 |
$per_page = 20; |
| 94 |
$page = $this->get_pagenum(); |
| 95 |
$offset = ( $page - 1 ) * $per_page; |
| 96 |
|
| 97 |
$args = array( |
| 98 |
'limit' => $per_page, |
| 99 |
'offset' => $offset, |
| 100 |
'search' => isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 101 |
'type' => isset( $_GET['log_type'] ) ? sanitize_key( $_GET['log_type'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 102 |
'level' => isset( $_GET['log_level'] ) ? sanitize_key( $_GET['log_level'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 103 |
'source' => isset( $_GET['log_source'] ) ? sanitize_key( $_GET['log_source'] ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 104 |
'orderby' => isset( $_GET['orderby'] ) ? sanitize_key( $_GET['orderby'] ) : 'id', // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 105 |
'order' => isset( $_GET['order'] ) ? sanitize_key( $_GET['order'] ) : 'DESC', // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 106 |
); |
| 107 |
|
| 108 |
$query = Charitable_Log::get_instance()->get_query(); |
| 109 |
$result = $query->get( $args ); |
| 110 |
|
| 111 |
$this->items = iterator_to_array( $result['records'] ); |
| 112 |
$this->total_items = $result['total']; |
| 113 |
|
| 114 |
// Explicitly set column headers — this list table renders on the |
| 115 |
// Tools page which doesn't have its own WP_Screen, so the |
| 116 |
// default manage_{screen}_columns filter returns nothing. |
| 117 |
$this->_column_headers = array( |
| 118 |
$this->get_columns(), |
| 119 |
array(), |
| 120 |
$this->get_sortable_columns(), |
| 121 |
$this->get_primary_column_name(), |
| 122 |
); |
| 123 |
|
| 124 |
$this->set_pagination_args( |
| 125 |
array( |
| 126 |
'total_items' => $this->total_items, |
| 127 |
'per_page' => $per_page, |
| 128 |
'total_pages' => ceil( $this->total_items / $per_page ), |
| 129 |
) |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Render the title column. |
| 135 |
* |
| 136 |
* @since 1.8.11 |
| 137 |
* |
| 138 |
* @param Charitable_Log_Record $item Current record. |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
public function column_title( $item ) { |
| 142 |
return sprintf( |
| 143 |
'<a href="#" class="js-charitable-log-link" data-record-id="%d">%s</a>', |
| 144 |
esc_attr( $item->id ), |
| 145 |
esc_html( $item->title ) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Return a clean label for the primary column's row header aria-label. |
| 151 |
* |
| 152 |
* WordPress 7.1 renders the primary column as the row header and reads this |
| 153 |
* method for the accessible row name. See #32892. |
| 154 |
* |
| 155 |
* Declaring this on older WordPress is harmless — nothing calls it there. |
| 156 |
* |
| 157 |
* @since 1.8.12 |
| 158 |
* |
| 159 |
* @param Charitable_Log_Record $item Current record. |
| 160 |
* @return string The log title, or an empty string to omit the attribute. |
| 161 |
*/ |
| 162 |
protected function get_primary_column_aria_label( $item ) { |
| 163 |
return isset( $item->title ) ? wp_strip_all_tags( $item->title ) : ''; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Render the message column (truncated). |
| 168 |
* |
| 169 |
* @since 1.8.11 |
| 170 |
* |
| 171 |
* @param Charitable_Log_Record $item Current record. |
| 172 |
* @return string |
| 173 |
*/ |
| 174 |
public function column_message( $item ) { |
| 175 |
$message = wp_strip_all_tags( $item->message ); |
| 176 |
|
| 177 |
if ( strlen( $message ) > 97 ) { |
| 178 |
$message = substr( $message, 0, 97 ) . '...'; |
| 179 |
} |
| 180 |
|
| 181 |
return esc_html( $message ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Render the types column. |
| 186 |
* |
| 187 |
* @since 1.8.11 |
| 188 |
* |
| 189 |
* @param Charitable_Log_Record $item Current record. |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function column_types( $item ) { |
| 193 |
$labels = $item->get_types( 'label' ); |
| 194 |
return esc_html( implode( ', ', $labels ) ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Render the level column with color badge. |
| 199 |
* |
| 200 |
* @since 1.8.11 |
| 201 |
* |
| 202 |
* @param Charitable_Log_Record $item Current record. |
| 203 |
* @return string |
| 204 |
*/ |
| 205 |
public function column_level( $item ) { |
| 206 |
$levels = Charitable_Log::get_log_levels(); |
| 207 |
$label = isset( $levels[ $item->level ] ) ? $levels[ $item->level ] : $item->level; |
| 208 |
|
| 209 |
return sprintf( |
| 210 |
'<span class="charitable-log-level charitable-log-level-%s">%s</span>', |
| 211 |
esc_attr( $item->level ), |
| 212 |
esc_html( $label ) |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Render the source column. |
| 218 |
* |
| 219 |
* @since 1.8.11 |
| 220 |
* |
| 221 |
* @param Charitable_Log_Record $item Current record. |
| 222 |
* @return string |
| 223 |
*/ |
| 224 |
public function column_source( $item ) { |
| 225 |
$sources = Charitable_Log::get_sources(); |
| 226 |
return esc_html( isset( $sources[ $item->source ] ) ? $sources[ $item->source ] : $item->source ); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Render the date column. |
| 231 |
* |
| 232 |
* @since 1.8.11 |
| 233 |
* |
| 234 |
* @param Charitable_Log_Record $item Current record. |
| 235 |
* @return string |
| 236 |
*/ |
| 237 |
public function column_date( $item ) { |
| 238 |
return esc_html( $item->get_date( 'short' ) ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Default column handler. |
| 243 |
* |
| 244 |
* @since 1.8.11 |
| 245 |
* |
| 246 |
* @param Charitable_Log_Record $item Current record. |
| 247 |
* @param string $column_name Column name. |
| 248 |
* @return string |
| 249 |
*/ |
| 250 |
public function column_default( $item, $column_name ) { |
| 251 |
return isset( $item->$column_name ) ? esc_html( $item->$column_name ) : ''; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Render extra controls above/below the table (filter dropdowns). |
| 256 |
* |
| 257 |
* @since 1.8.11 |
| 258 |
* |
| 259 |
* @param string $which 'top' or 'bottom'. |
| 260 |
*/ |
| 261 |
public function extra_tablenav( $which ) { |
| 262 |
if ( 'top' !== $which ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
$current_type = isset( $_GET['log_type'] ) ? sanitize_key( $_GET['log_type'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 267 |
$current_level = isset( $_GET['log_level'] ) ? sanitize_key( $_GET['log_level'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 268 |
$current_source = isset( $_GET['log_source'] ) ? sanitize_key( $_GET['log_source'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 269 |
?> |
| 270 |
<div class="alignleft actions charitable-log-filters"> |
| 271 |
<select name="log_type" id="charitable-log-type-filter"> |
| 272 |
<option value=""><?php esc_html_e( 'All Types', 'charitable' ); ?></option> |
| 273 |
<?php foreach ( Charitable_Log::get_log_types() as $key => $label ) : ?> |
| 274 |
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $current_type, $key ); ?>><?php echo esc_html( $label ); ?></option> |
| 275 |
<?php endforeach; ?> |
| 276 |
</select> |
| 277 |
|
| 278 |
<select name="log_level" id="charitable-log-level-filter"> |
| 279 |
<option value=""><?php esc_html_e( 'All Levels', 'charitable' ); ?></option> |
| 280 |
<?php foreach ( Charitable_Log::get_log_levels() as $key => $label ) : ?> |
| 281 |
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $current_level, $key ); ?>><?php echo esc_html( $label ); ?></option> |
| 282 |
<?php endforeach; ?> |
| 283 |
</select> |
| 284 |
|
| 285 |
<select name="log_source" id="charitable-log-source-filter"> |
| 286 |
<option value=""><?php esc_html_e( 'All Sources', 'charitable' ); ?></option> |
| 287 |
<?php foreach ( Charitable_Log::get_sources() as $key => $label ) : ?> |
| 288 |
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $current_source, $key ); ?>><?php echo esc_html( $label ); ?></option> |
| 289 |
<?php endforeach; ?> |
| 290 |
</select> |
| 291 |
|
| 292 |
<?php submit_button( __( 'Filter', 'charitable' ), '', 'filter_action', false ); ?> |
| 293 |
</div> |
| 294 |
<?php |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Message to display when there are no items. |
| 299 |
* |
| 300 |
* @since 1.8.11 |
| 301 |
*/ |
| 302 |
public function no_items() { |
| 303 |
esc_html_e( 'No log entries found.', 'charitable' ); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
endif; |
| 308 |
|