helper
3 years ago
importers
3 years ago
list-tables
3 years ago
marketplace-suggestions
3 years ago
meta-boxes
3 years ago
notes
3 years ago
plugin-updates
5 years ago
reports
3 years ago
settings
3 years ago
views
3 years ago
class-wc-admin-addons.php
3 years ago
class-wc-admin-api-keys-table-list.php
6 years ago
class-wc-admin-api-keys.php
6 years ago
class-wc-admin-assets.php
3 years ago
class-wc-admin-attributes.php
3 years ago
class-wc-admin-customize.php
5 years ago
class-wc-admin-dashboard-setup.php
3 years ago
class-wc-admin-dashboard.php
3 years ago
class-wc-admin-duplicate-product.php
5 years ago
class-wc-admin-exporters.php
3 years ago
class-wc-admin-help.php
4 years ago
class-wc-admin-importers.php
3 years ago
class-wc-admin-log-table-list.php
5 years ago
class-wc-admin-menus.php
3 years ago
class-wc-admin-meta-boxes.php
3 years ago
class-wc-admin-notices.php
3 years ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
3 years ago
class-wc-admin-post-types.php
3 years ago
class-wc-admin-profile.php
4 years ago
class-wc-admin-reports.php
5 years ago
class-wc-admin-settings.php
3 years ago
class-wc-admin-setup-wizard.php
4 years ago
class-wc-admin-status.php
3 years ago
class-wc-admin-taxonomies.php
3 years ago
class-wc-admin-webhooks-table-list.php
4 years ago
class-wc-admin-webhooks.php
3 years ago
class-wc-admin.php
4 years ago
wc-admin-functions.php
3 years ago
wc-meta-box-functions.php
3 years ago
class-wc-admin-log-table-list.php
396 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Log Table List |
| 4 | * |
| 5 | * @author WooThemes |
| 6 | * @category Admin |
| 7 | * @package WooCommerce\Admin |
| 8 | * @version 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 16 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 17 | } |
| 18 | |
| 19 | class WC_Admin_Log_Table_List extends WP_List_Table { |
| 20 | |
| 21 | /** |
| 22 | * Initialize the log table list. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | parent::__construct( |
| 26 | array( |
| 27 | 'singular' => 'log', |
| 28 | 'plural' => 'logs', |
| 29 | 'ajax' => false, |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Display level dropdown |
| 36 | * |
| 37 | * @global wpdb $wpdb |
| 38 | */ |
| 39 | public function level_dropdown() { |
| 40 | |
| 41 | $levels = array( |
| 42 | array( |
| 43 | 'value' => WC_Log_Levels::EMERGENCY, |
| 44 | 'label' => __( 'Emergency', 'woocommerce' ), |
| 45 | ), |
| 46 | array( |
| 47 | 'value' => WC_Log_Levels::ALERT, |
| 48 | 'label' => __( 'Alert', 'woocommerce' ), |
| 49 | ), |
| 50 | array( |
| 51 | 'value' => WC_Log_Levels::CRITICAL, |
| 52 | 'label' => __( 'Critical', 'woocommerce' ), |
| 53 | ), |
| 54 | array( |
| 55 | 'value' => WC_Log_Levels::ERROR, |
| 56 | 'label' => __( 'Error', 'woocommerce' ), |
| 57 | ), |
| 58 | array( |
| 59 | 'value' => WC_Log_Levels::WARNING, |
| 60 | 'label' => __( 'Warning', 'woocommerce' ), |
| 61 | ), |
| 62 | array( |
| 63 | 'value' => WC_Log_Levels::NOTICE, |
| 64 | 'label' => __( 'Notice', 'woocommerce' ), |
| 65 | ), |
| 66 | array( |
| 67 | 'value' => WC_Log_Levels::INFO, |
| 68 | 'label' => __( 'Info', 'woocommerce' ), |
| 69 | ), |
| 70 | array( |
| 71 | 'value' => WC_Log_Levels::DEBUG, |
| 72 | 'label' => __( 'Debug', 'woocommerce' ), |
| 73 | ), |
| 74 | ); |
| 75 | |
| 76 | $selected_level = isset( $_REQUEST['level'] ) ? $_REQUEST['level'] : ''; |
| 77 | ?> |
| 78 | <label for="filter-by-level" class="screen-reader-text"><?php esc_html_e( 'Filter by level', 'woocommerce' ); ?></label> |
| 79 | <select name="level" id="filter-by-level"> |
| 80 | <option<?php selected( $selected_level, '' ); ?> value=""><?php esc_html_e( 'All levels', 'woocommerce' ); ?></option> |
| 81 | <?php |
| 82 | foreach ( $levels as $l ) { |
| 83 | printf( |
| 84 | '<option%1$s value="%2$s">%3$s</option>', |
| 85 | selected( $selected_level, $l['value'], false ), |
| 86 | esc_attr( $l['value'] ), |
| 87 | esc_html( $l['label'] ) |
| 88 | ); |
| 89 | } |
| 90 | ?> |
| 91 | </select> |
| 92 | <?php |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get list columns. |
| 97 | * |
| 98 | * @return array |
| 99 | */ |
| 100 | public function get_columns() { |
| 101 | return array( |
| 102 | 'cb' => '<input type="checkbox" />', |
| 103 | 'timestamp' => __( 'Timestamp', 'woocommerce' ), |
| 104 | 'level' => __( 'Level', 'woocommerce' ), |
| 105 | 'message' => __( 'Message', 'woocommerce' ), |
| 106 | 'source' => __( 'Source', 'woocommerce' ), |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Column cb. |
| 112 | * |
| 113 | * @param array $log |
| 114 | * @return string |
| 115 | */ |
| 116 | public function column_cb( $log ) { |
| 117 | return sprintf( '<input type="checkbox" name="log[]" value="%1$s" />', esc_attr( $log['log_id'] ) ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Timestamp column. |
| 122 | * |
| 123 | * @param array $log |
| 124 | * @return string |
| 125 | */ |
| 126 | public function column_timestamp( $log ) { |
| 127 | return esc_html( |
| 128 | mysql2date( |
| 129 | 'Y-m-d H:i:s', |
| 130 | $log['timestamp'] |
| 131 | ) |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Level column. |
| 137 | * |
| 138 | * @param array $log |
| 139 | * @return string |
| 140 | */ |
| 141 | public function column_level( $log ) { |
| 142 | $level_key = WC_Log_Levels::get_severity_level( $log['level'] ); |
| 143 | $levels = array( |
| 144 | 'emergency' => __( 'Emergency', 'woocommerce' ), |
| 145 | 'alert' => __( 'Alert', 'woocommerce' ), |
| 146 | 'critical' => __( 'Critical', 'woocommerce' ), |
| 147 | 'error' => __( 'Error', 'woocommerce' ), |
| 148 | 'warning' => __( 'Warning', 'woocommerce' ), |
| 149 | 'notice' => __( 'Notice', 'woocommerce' ), |
| 150 | 'info' => __( 'Info', 'woocommerce' ), |
| 151 | 'debug' => __( 'Debug', 'woocommerce' ), |
| 152 | ); |
| 153 | |
| 154 | if ( ! isset( $levels[ $level_key ] ) ) { |
| 155 | return ''; |
| 156 | } |
| 157 | |
| 158 | $level = $levels[ $level_key ]; |
| 159 | $level_class = sanitize_html_class( 'log-level--' . $level_key ); |
| 160 | return '<span class="log-level ' . $level_class . '">' . esc_html( $level ) . '</span>'; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Message column. |
| 165 | * |
| 166 | * @param array $log |
| 167 | * @return string |
| 168 | */ |
| 169 | public function column_message( $log ) { |
| 170 | return esc_html( $log['message'] ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Source column. |
| 175 | * |
| 176 | * @param array $log |
| 177 | * @return string |
| 178 | */ |
| 179 | public function column_source( $log ) { |
| 180 | return esc_html( $log['source'] ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get bulk actions. |
| 185 | * |
| 186 | * @return array |
| 187 | */ |
| 188 | protected function get_bulk_actions() { |
| 189 | return array( |
| 190 | 'delete' => __( 'Delete', 'woocommerce' ), |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Extra controls to be displayed between bulk actions and pagination. |
| 196 | * |
| 197 | * @param string $which |
| 198 | */ |
| 199 | protected function extra_tablenav( $which ) { |
| 200 | if ( 'top' === $which ) { |
| 201 | echo '<div class="alignleft actions">'; |
| 202 | $this->level_dropdown(); |
| 203 | $this->source_dropdown(); |
| 204 | submit_button( __( 'Filter', 'woocommerce' ), '', 'filter-action', false ); |
| 205 | echo '</div>'; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get a list of sortable columns. |
| 211 | * |
| 212 | * @return array |
| 213 | */ |
| 214 | protected function get_sortable_columns() { |
| 215 | return array( |
| 216 | 'timestamp' => array( 'timestamp', true ), |
| 217 | 'level' => array( 'level', true ), |
| 218 | 'source' => array( 'source', true ), |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Display source dropdown |
| 224 | * |
| 225 | * @global wpdb $wpdb |
| 226 | */ |
| 227 | protected function source_dropdown() { |
| 228 | global $wpdb; |
| 229 | |
| 230 | $sources = $wpdb->get_col( |
| 231 | "SELECT DISTINCT source |
| 232 | FROM {$wpdb->prefix}woocommerce_log |
| 233 | WHERE source != '' |
| 234 | ORDER BY source ASC" |
| 235 | ); |
| 236 | |
| 237 | if ( ! empty( $sources ) ) { |
| 238 | $selected_source = isset( $_REQUEST['source'] ) ? $_REQUEST['source'] : ''; |
| 239 | ?> |
| 240 | <label for="filter-by-source" class="screen-reader-text"><?php esc_html_e( 'Filter by source', 'woocommerce' ); ?></label> |
| 241 | <select name="source" id="filter-by-source"> |
| 242 | <option<?php selected( $selected_source, '' ); ?> value=""><?php esc_html_e( 'All sources', 'woocommerce' ); ?></option> |
| 243 | <?php |
| 244 | foreach ( $sources as $s ) { |
| 245 | printf( |
| 246 | '<option%1$s value="%2$s">%3$s</option>', |
| 247 | selected( $selected_source, $s, false ), |
| 248 | esc_attr( $s ), |
| 249 | esc_html( $s ) |
| 250 | ); |
| 251 | } |
| 252 | ?> |
| 253 | </select> |
| 254 | <?php |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Prepare table list items. |
| 260 | * |
| 261 | * @global wpdb $wpdb |
| 262 | */ |
| 263 | public function prepare_items() { |
| 264 | global $wpdb; |
| 265 | |
| 266 | $this->prepare_column_headers(); |
| 267 | |
| 268 | $per_page = $this->get_items_per_page( 'woocommerce_status_log_items_per_page', 10 ); |
| 269 | |
| 270 | $where = $this->get_items_query_where(); |
| 271 | $order = $this->get_items_query_order(); |
| 272 | $limit = $this->get_items_query_limit(); |
| 273 | $offset = $this->get_items_query_offset(); |
| 274 | |
| 275 | $query_items = " |
| 276 | SELECT log_id, timestamp, level, message, source |
| 277 | FROM {$wpdb->prefix}woocommerce_log |
| 278 | {$where} {$order} {$limit} {$offset} |
| 279 | "; |
| 280 | |
| 281 | $this->items = $wpdb->get_results( $query_items, ARRAY_A ); |
| 282 | |
| 283 | $query_count = "SELECT COUNT(log_id) FROM {$wpdb->prefix}woocommerce_log {$where}"; |
| 284 | $total_items = $wpdb->get_var( $query_count ); |
| 285 | |
| 286 | $this->set_pagination_args( |
| 287 | array( |
| 288 | 'total_items' => $total_items, |
| 289 | 'per_page' => $per_page, |
| 290 | 'total_pages' => ceil( $total_items / $per_page ), |
| 291 | ) |
| 292 | ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get prepared LIMIT clause for items query |
| 297 | * |
| 298 | * @global wpdb $wpdb |
| 299 | * |
| 300 | * @return string Prepared LIMIT clause for items query. |
| 301 | */ |
| 302 | protected function get_items_query_limit() { |
| 303 | global $wpdb; |
| 304 | |
| 305 | $per_page = $this->get_items_per_page( 'woocommerce_status_log_items_per_page', 10 ); |
| 306 | return $wpdb->prepare( 'LIMIT %d', $per_page ); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Get prepared OFFSET clause for items query |
| 311 | * |
| 312 | * @global wpdb $wpdb |
| 313 | * |
| 314 | * @return string Prepared OFFSET clause for items query. |
| 315 | */ |
| 316 | protected function get_items_query_offset() { |
| 317 | global $wpdb; |
| 318 | |
| 319 | $per_page = $this->get_items_per_page( 'woocommerce_status_log_items_per_page', 10 ); |
| 320 | $current_page = $this->get_pagenum(); |
| 321 | if ( 1 < $current_page ) { |
| 322 | $offset = $per_page * ( $current_page - 1 ); |
| 323 | } else { |
| 324 | $offset = 0; |
| 325 | } |
| 326 | |
| 327 | return $wpdb->prepare( 'OFFSET %d', $offset ); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Get prepared ORDER BY clause for items query |
| 332 | * |
| 333 | * @return string Prepared ORDER BY clause for items query. |
| 334 | */ |
| 335 | protected function get_items_query_order() { |
| 336 | $valid_orders = array( 'level', 'source', 'timestamp' ); |
| 337 | if ( ! empty( $_REQUEST['orderby'] ) && in_array( $_REQUEST['orderby'], $valid_orders ) ) { |
| 338 | $by = wc_clean( $_REQUEST['orderby'] ); |
| 339 | } else { |
| 340 | $by = 'timestamp'; |
| 341 | } |
| 342 | $by = esc_sql( $by ); |
| 343 | |
| 344 | if ( ! empty( $_REQUEST['order'] ) && 'asc' === strtolower( $_REQUEST['order'] ) ) { |
| 345 | $order = 'ASC'; |
| 346 | } else { |
| 347 | $order = 'DESC'; |
| 348 | } |
| 349 | |
| 350 | return "ORDER BY {$by} {$order}, log_id {$order}"; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Get prepared WHERE clause for items query |
| 355 | * |
| 356 | * @global wpdb $wpdb |
| 357 | * |
| 358 | * @return string Prepared WHERE clause for items query. |
| 359 | */ |
| 360 | protected function get_items_query_where() { |
| 361 | global $wpdb; |
| 362 | |
| 363 | $where_conditions = array(); |
| 364 | $where_values = array(); |
| 365 | if ( ! empty( $_REQUEST['level'] ) && WC_Log_Levels::is_valid_level( $_REQUEST['level'] ) ) { |
| 366 | $where_conditions[] = 'level >= %d'; |
| 367 | $where_values[] = WC_Log_Levels::get_level_severity( $_REQUEST['level'] ); |
| 368 | } |
| 369 | if ( ! empty( $_REQUEST['source'] ) ) { |
| 370 | $where_conditions[] = 'source = %s'; |
| 371 | $where_values[] = wc_clean( $_REQUEST['source'] ); |
| 372 | } |
| 373 | if ( ! empty( $_REQUEST['s'] ) ) { |
| 374 | $where_conditions[] = 'message like %s'; |
| 375 | $where_values[] = '%' . $wpdb->esc_like( wc_clean( wp_unslash( $_REQUEST['s'] ) ) ) . '%'; |
| 376 | } |
| 377 | |
| 378 | if ( empty( $where_conditions ) ) { |
| 379 | return ''; |
| 380 | } |
| 381 | |
| 382 | return $wpdb->prepare( 'WHERE 1 = 1 AND ' . implode( ' AND ', $where_conditions ), $where_values ); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Set _column_headers property for table list |
| 387 | */ |
| 388 | protected function prepare_column_headers() { |
| 389 | $this->_column_headers = array( |
| 390 | $this->get_columns(), |
| 391 | array(), |
| 392 | $this->get_sortable_columns(), |
| 393 | ); |
| 394 | } |
| 395 | } |
| 396 |