WC_Admin_Log_Table_List

The WooCommerce WC Admin Log Table List class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-log-table-list.php  
  1. class WC_Admin_Log_Table_List extends WP_List_Table { 
  2.  
  3. /** 
  4. * Initialize the log table list. 
  5. */ 
  6. public function __construct() { 
  7. parent::__construct( array( 
  8. 'singular' => 'log',  
  9. 'plural' => 'logs',  
  10. 'ajax' => false,  
  11. ) ); 
  12.  
  13. /** 
  14. * Display level dropdown 
  15. * @global wpdb $wpdb 
  16. */ 
  17. public function level_dropdown() { 
  18.  
  19. $levels = array( 
  20. array( 'value' => WC_Log_Levels::EMERGENCY, 'label' => __( 'Emergency', 'woocommerce' ) ),  
  21. array( 'value' => WC_Log_Levels::ALERT, 'label' => __( 'Alert', 'woocommerce' ) ),  
  22. array( 'value' => WC_Log_Levels::CRITICAL, 'label' => __( 'Critical', 'woocommerce' ) ),  
  23. array( 'value' => WC_Log_Levels::ERROR, 'label' => __( 'Error', 'woocommerce' ) ),  
  24. array( 'value' => WC_Log_Levels::WARNING, 'label' => __( 'Warning', 'woocommerce' ) ),  
  25. array( 'value' => WC_Log_Levels::NOTICE, 'label' => __( 'Notice', 'woocommerce' ) ),  
  26. array( 'value' => WC_Log_Levels::INFO, 'label' => __( 'Info', 'woocommerce' ) ),  
  27. array( 'value' => WC_Log_Levels::DEBUG, 'label' => __( 'Debug', 'woocommerce' ) ),  
  28. ); 
  29.  
  30. $selected_level = isset( $_REQUEST['level'] ) ? $_REQUEST['level'] : ''; 
  31. ?> 
  32. <label for="filter-by-level" class="screen-reader-text"><?php _e( 'Filter by level', 'woocommerce' ); ?></label> 
  33. <select name="level" id="filter-by-level"> 
  34. <option<?php selected( $selected_level, '' ); ?> value=""><?php _e( 'All levels', 'woocommerce' ); ?></option> 
  35. <?php foreach ( $levels as $l ) { 
  36. printf( '<option%1$s value="%2$s">%3$s</option>',  
  37. selected( $selected_level, $l['value'], false ),  
  38. esc_attr( $l['value'] ),  
  39. esc_html( $l['label'] ) 
  40. ); 
  41. } ?> 
  42. </select> 
  43. <?php 
  44.  
  45. /** 
  46. * Get list columns. 
  47. * @return array 
  48. */ 
  49. public function get_columns() { 
  50. return array( 
  51. 'cb' => '<input type="checkbox" />',  
  52. 'timestamp' => __( 'Timestamp', 'woocommerce' ),  
  53. 'level' => __( 'Level', 'woocommerce' ),  
  54. 'message' => __( 'Message', 'woocommerce' ),  
  55. 'source' => __( 'Source', 'woocommerce' ),  
  56. ); 
  57.  
  58. /** 
  59. * Column cb. 
  60. * @param array $log 
  61. * @return string 
  62. */ 
  63. public function column_cb( $log ) { 
  64. return sprintf( '<input type="checkbox" name="log[]" value="%1$s" />', esc_attr( $log['log_id'] ) ); 
  65.  
  66. /** 
  67. * Timestamp column. 
  68. * @param array $log 
  69. * @return string 
  70. */ 
  71. public function column_timestamp( $log ) { 
  72. return esc_html( mysql2date( 
  73. get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),  
  74. $log['timestamp'] 
  75. ) ); 
  76.  
  77. /** 
  78. * Level column. 
  79. * @param array $log 
  80. * @return string 
  81. */ 
  82. public function column_level( $log ) { 
  83. $level_key = WC_Log_Levels::get_severity_level( $log['level'] ); 
  84. $levels = array( 
  85. 'emergency' => __( 'Emergency', 'woocommerce' ),  
  86. 'alert' => __( 'Alert', 'woocommerce' ),  
  87. 'critical' => __( 'Critical', 'woocommerce' ),  
  88. 'error' => __( 'Error', 'woocommerce' ),  
  89. 'warning' => __( 'Warning', 'woocommerce' ),  
  90. 'notice' => __( 'Notice', 'woocommerce' ),  
  91. 'info' => __( 'Info', 'woocommerce' ),  
  92. 'debug' => __( 'Debug', 'woocommerce' ),  
  93. ); 
  94.  
  95. if ( isset( $levels[ $level_key ] ) ) { 
  96. $level = $levels[ $level_key ]; 
  97. $level_class = sanitize_html_class( 'log-level--' . $level_key ); 
  98. return '<span class="log-level ' . $level_class . '">' . esc_html( $level ) . '</span>'; 
  99. } else { 
  100. return ''; 
  101.  
  102. /** 
  103. * Message column. 
  104. * @param array $log 
  105. * @return string 
  106. */ 
  107. public function column_message( $log ) { 
  108. return esc_html( $log['message'] ); 
  109.  
  110. /** 
  111. * Source column. 
  112. * @param array $log 
  113. * @return string 
  114. */ 
  115. public function column_source( $log ) { 
  116. return esc_html( $log['source'] ); 
  117.  
  118. /** 
  119. * Get bulk actions. 
  120. * @return array 
  121. */ 
  122. protected function get_bulk_actions() { 
  123. return array( 
  124. 'delete' => __( 'Delete', 'woocommerce' ),  
  125. ); 
  126.  
  127. /** 
  128. * Extra controls to be displayed between bulk actions and pagination. 
  129. * @param string $which 
  130. */ 
  131. protected function extra_tablenav( $which ) { 
  132. if ( 'top' === $which ) { 
  133. echo '<div class="alignleft actions">'; 
  134. $this->level_dropdown(); 
  135. $this->source_dropdown(); 
  136. submit_button( __( 'Filter', 'woocommerce' ), '', 'filter-action', false ); 
  137. echo '</div>'; 
  138.  
  139. /** 
  140. * Get a list of sortable columns. 
  141. * @return array 
  142. */ 
  143. protected function get_sortable_columns() { 
  144. return array( 
  145. 'timestamp' => array( 'timestamp', true ),  
  146. 'level' => array( 'level', true ),  
  147. 'source' => array( 'source', true ),  
  148. ); 
  149.  
  150. /** 
  151. * Display source dropdown 
  152. * @global wpdb $wpdb 
  153. */ 
  154. protected function source_dropdown() { 
  155. global $wpdb; 
  156.  
  157. $sources = $wpdb->get_col( " 
  158. SELECT DISTINCT source 
  159. FROM {$wpdb->prefix}woocommerce_log 
  160. WHERE source != '' 
  161. ORDER BY source ASC 
  162. " ); 
  163.  
  164. if ( ! empty( $sources ) ) { 
  165. $selected_source = isset( $_REQUEST['source'] ) ? $_REQUEST['source'] : ''; 
  166. ?> 
  167. <label for="filter-by-source" class="screen-reader-text"><?php _e( 'Filter by source', 'woocommerce' ); ?></label> 
  168. <select name="source" id="filter-by-source"> 
  169. <option<?php selected( $selected_source, '' ); ?> value=""><?php _e( 'All sources', 'woocommerce' ); ?></option> 
  170. <?php foreach ( $sources as $s ) { 
  171. printf( '<option%1$s value="%2$s">%3$s</option>',  
  172. selected( $selected_source, $s, false ),  
  173. esc_attr( $s ),  
  174. esc_html( $s ) 
  175. ); 
  176. } ?> 
  177. </select> 
  178. <?php 
  179.  
  180. /** 
  181. * Prepare table list items. 
  182. * @global wpdb $wpdb 
  183. */ 
  184. public function prepare_items() { 
  185. global $wpdb; 
  186.  
  187. $this->prepare_column_headers(); 
  188.  
  189. $per_page = $this->get_items_per_page( 'woocommerce_status_log_items_per_page', 10 ); 
  190.  
  191. $where = $this->get_items_query_where(); 
  192. $order = $this->get_items_query_order(); 
  193. $limit = $this->get_items_query_limit(); 
  194. $offset = $this->get_items_query_offset(); 
  195.  
  196. $query_items = " 
  197. SELECT log_id, timestamp, level, message, source 
  198. FROM {$wpdb->prefix}woocommerce_log 
  199. {$where} {$order} {$limit} {$offset} 
  200. "; 
  201.  
  202. $this->items = $wpdb->get_results( $query_items, ARRAY_A ); 
  203.  
  204. $query_count = "SELECT COUNT(log_id) FROM {$wpdb->prefix}woocommerce_log {$where}"; 
  205. $total_items = $wpdb->get_var( $query_count ); 
  206.  
  207. $this->set_pagination_args( array( 
  208. 'total_items' => $total_items,  
  209. 'per_page' => $per_page,  
  210. 'total_pages' => ceil( $total_items / $per_page ),  
  211. ) ); 
  212.  
  213. /** 
  214. * Get prepared LIMIT clause for items query 
  215. * @global wpdb $wpdb 
  216. * @return string Prepared LIMIT clause for items query. 
  217. */ 
  218. protected function get_items_query_limit() { 
  219. global $wpdb; 
  220.  
  221. $per_page = $this->get_items_per_page( 'woocommerce_status_log_items_per_page', 10 ); 
  222. return $wpdb->prepare( 'LIMIT %d', $per_page ); 
  223.  
  224. /** 
  225. * Get prepared OFFSET clause for items query 
  226. * @global wpdb $wpdb 
  227. * @return string Prepared OFFSET clause for items query. 
  228. */ 
  229. protected function get_items_query_offset() { 
  230. global $wpdb; 
  231.  
  232. $per_page = $this->get_items_per_page( 'woocommerce_status_log_items_per_page', 10 ); 
  233. $current_page = $this->get_pagenum(); 
  234. if ( 1 < $current_page ) { 
  235. $offset = $per_page * ( $current_page - 1 ); 
  236. } else { 
  237. $offset = 0; 
  238.  
  239. return $wpdb->prepare( 'OFFSET %d', $offset ); 
  240.  
  241. /** 
  242. * Get prepared ORDER BY clause for items query 
  243. * @return string Prepared ORDER BY clause for items query. 
  244. */ 
  245. protected function get_items_query_order() { 
  246. $valid_orders = array( 'level', 'source', 'timestamp' ); 
  247. if ( ! empty( $_REQUEST['orderby'] ) && in_array( $_REQUEST['orderby'], $valid_orders ) ) { 
  248. $by = wc_clean( $_REQUEST['orderby'] ); 
  249. } else { 
  250. $by = 'timestamp'; 
  251. $by = esc_sql( $by ); 
  252.  
  253. if ( ! empty( $_REQUEST['order'] ) && 'asc' === strtolower( $_REQUEST['order'] ) ) { 
  254. $order = 'ASC'; 
  255. } else { 
  256. $order = 'DESC'; 
  257.  
  258. return "ORDER BY {$by} {$order}"; 
  259.  
  260. /** 
  261. * Get prepared WHERE clause for items query 
  262. * @global wpdb $wpdb 
  263. * @return string Prepared WHERE clause for items query. 
  264. */ 
  265. protected function get_items_query_where() { 
  266. global $wpdb; 
  267.  
  268. $where_conditions = array(); 
  269. $where_values = array(); 
  270. if ( ! empty( $_REQUEST['level'] ) && WC_Log_Levels::is_valid_level( $_REQUEST['level'] ) ) { 
  271. $where_conditions[] = 'level >= %d'; 
  272. $where_values[] = WC_Log_Levels::get_level_severity( $_REQUEST['level'] ); 
  273. if ( ! empty( $_REQUEST['source'] ) ) { 
  274. $where_conditions[] = 'source = %s'; 
  275. $where_values[] = wc_clean( $_REQUEST['source'] ); 
  276.  
  277. if ( ! empty( $where_conditions ) ) { 
  278. return $wpdb->prepare( 'WHERE 1 = 1 AND ' . implode( ' AND ', $where_conditions ), $where_values ); 
  279. } else { 
  280. return ''; 
  281.  
  282. /** 
  283. * Set _column_headers property for table list 
  284. */ 
  285. protected function prepare_column_headers() { 
  286. $this->_column_headers = array( 
  287. $this->get_columns(),  
  288. array(),  
  289. $this->get_sortable_columns(),  
  290. );