Add delete to db log viewer table

Clean up some nonce logic.
Use a single <form> containing log table.
This commit is contained in:
Jon Surrell 2016-12-15 22:03:12 +01:00
parent 9ba616b2c1
commit 5f24eca7e2
4 changed files with 63 additions and 13 deletions

View File

@ -5,7 +5,7 @@
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 2.0.0
* @version 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
@ -148,7 +148,9 @@ class WC_Admin_Log_Table_List extends WP_List_Table {
* @return array
*/
protected function get_bulk_actions() {
return array();
return array(
'delete' => __( 'Delete', 'woocommerce' ),
);
}
/**

View File

@ -111,9 +111,16 @@ class WC_Admin_Status {
*/
public static function status_logs_db() {
// Flush
if ( ! empty( $_REQUEST['flush-logs'] ) ) {
self::flush_db_logs();
}
// Bulk actions
if ( isset( $_GET['action'] ) && isset( $_GET['log'] ) ) {
self::log_table_bulk_actions();
}
$log_table_list = new WC_Admin_Log_Table_List();
$log_table_list->prepare_items();
@ -288,14 +295,33 @@ class WC_Admin_Status {
*
* @since 2.8
*/
public static function flush_db_logs() {
check_admin_referer( 'flush-logs' );
if ( ! empty( $_REQUEST['flush-logs'] ) ) {
WC_Log_Handler_DB::flush();
private static function flush_db_logs() {
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) {
wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
}
WC_Log_Handler_DB::flush();
wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
exit();
}
/**
* Bulk actions.
*
* @since 2.8
*/
private static function log_table_bulk_actions() {
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) {
wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
}
$log_ids = array_map( 'absint', (array) $_GET['log'] );
if ( 'delete' === $_GET['action'] ) {
WC_Log_Handler_DB::delete( $log_ids );
wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
exit();
}
}
}

View File

@ -16,12 +16,8 @@ if ( ! defined( 'ABSPATH' ) ) {
<input type="hidden" name="page" value="wc-status" />
<input type="hidden" name="tab" value="logs" />
</form>
<form method="post" action="">
<?php
wp_nonce_field( 'flush-logs' );
submit_button( __( 'Flush all logs', 'woocommerce' ), 'delete', 'flush-logs' );
?>
<?php submit_button( __( 'Flush all logs', 'woocommerce' ), 'delete', 'flush-logs' ); ?>
<?php wp_nonce_field( 'woocommerce-status-logs' ); ?>
</form>
<?php
wc_enqueue_js( "

View File

@ -88,4 +88,30 @@ class WC_Log_Handler_DB extends WC_Log_Handler {
return $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_log" );
}
/**
* Delete selected logs from DB.
*
* @param int|string|array Log ID or array of Log IDs to be deleted.
*
* @return bool
*/
public static function delete( $log_ids ) {
global $wpdb;
if ( ! is_array( $log_ids ) ) {
$log_ids = array( $log_ids );
}
$format = array_fill( 0, count( $log_ids ), '%d' );
$query_in = '(' . implode( ',', $format ) . ')';
$query = $wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_log WHERE log_id IN {$query_in}",
$log_ids
);
return $wpdb->query( $query );
}
}