PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-log-table-list.php

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