Add source filtering
This commit is contained in:
parent
e17654226a
commit
8d21f645e7
|
@ -141,4 +141,22 @@ class FileController {
|
|||
|
||||
return array_slice( $files, $args['offset'], $args['per_page'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of sources for existing log files.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_file_sources() {
|
||||
$files = glob( $this->log_directory . '*.log' );
|
||||
$all_sources = array_map(
|
||||
function( $path ) {
|
||||
$file = new File( $path );
|
||||
return $file->get_source();
|
||||
},
|
||||
$files
|
||||
);
|
||||
|
||||
return array_unique( $all_sources );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,11 @@ class ListTable extends WP_List_Table {
|
|||
*/
|
||||
private $page_controller;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $file_args = array();
|
||||
|
||||
/**
|
||||
* ListTable class.
|
||||
*/
|
||||
|
@ -41,6 +46,19 @@ class ListTable extends WP_List_Table {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set file args for later use, since `prepare_items` can't take any parameters.
|
||||
*
|
||||
* @param array $args
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set_file_args( $args ) {
|
||||
if ( is_array( $args ) ) {
|
||||
$this->file_args = $args;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render message when there are no items.
|
||||
*
|
||||
|
@ -50,6 +68,47 @@ class ListTable extends WP_List_Table {
|
|||
esc_html_e( 'No log files found.', 'woocommerce' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the existing log sources for the filter dropdown.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_sources_list() {
|
||||
$sources = $this->file_controller->get_file_sources();
|
||||
sort( $sources );
|
||||
|
||||
return $sources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays extra controls between bulk actions and pagination.
|
||||
*
|
||||
* @param string $which
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function extra_tablenav( $which ) {
|
||||
$all_sources = $this->get_sources_list();
|
||||
$current_source = filter_input( INPUT_GET, 'source', FILTER_SANITIZE_STRING ) ?? '';
|
||||
|
||||
?>
|
||||
<div class="alignleft actions">
|
||||
<?php if ( 'top' === $which ) : ?>
|
||||
<label for="filter-by-source" class="screen-reader-text"><?php esc_html_e( 'Filter by log source', 'woocommerce' ); ?></label>
|
||||
<select name="source" id="filter-by-source">
|
||||
<option<?php selected( $current_source, '' ); ?> value=""><?php esc_html_e( 'All sources', 'woocommerce' ); ?></option>
|
||||
<?php foreach ( $all_sources as $source ) : ?>
|
||||
<option<?php selected( $current_source, $source ); ?> value="<?php echo esc_attr( $source ) ?>">
|
||||
<?php echo esc_html( $source ); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<?php submit_button( __( 'Filter', 'woocommerce' ), '', 'filter_action', false, array( 'id' => 'logs-filter-submit' ) ); ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the column header info.
|
||||
*
|
||||
|
@ -71,36 +130,12 @@ class ListTable extends WP_List_Table {
|
|||
*/
|
||||
public function prepare_items() {
|
||||
$per_page = $this->get_items_per_page( self::PER_PAGE_USER_OPTION_KEY );
|
||||
$offset = ( $this->get_pagenum() - 1 ) * $per_page;
|
||||
$orderby = filter_input(
|
||||
INPUT_GET,
|
||||
'orderby',
|
||||
FILTER_VALIDATE_REGEXP,
|
||||
array(
|
||||
'options' => array(
|
||||
'regexp' => '/^(created|modified|source|size)$/',
|
||||
'default' => 'modified'
|
||||
),
|
||||
)
|
||||
);
|
||||
$order = filter_input(
|
||||
INPUT_GET,
|
||||
'order',
|
||||
FILTER_VALIDATE_REGEXP,
|
||||
array(
|
||||
'options' => array(
|
||||
'regexp' => '/^(asc|desc)$/i',
|
||||
'default' => 'desc'
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$file_args = array(
|
||||
$defaults = array(
|
||||
'per_page' => $per_page,
|
||||
'offset' => $offset,
|
||||
'orderby' => $orderby,
|
||||
'order' => $order,
|
||||
'offset' => ( $this->get_pagenum() - 1 ) * $per_page,
|
||||
);
|
||||
$file_args = wp_parse_args( $this->file_args, $defaults );
|
||||
|
||||
$total_items = $this->file_controller->get_files( $file_args, true );
|
||||
$total_pages = ceil( $total_items / $per_page );
|
||||
|
|
|
@ -88,7 +88,7 @@ class PageController {
|
|||
|
||||
switch ( $handler ) {
|
||||
case LogHandlerFileV2::class:
|
||||
$args = $this->get_filev2_query_params();
|
||||
$args = $this->get_query_params();
|
||||
$this->render_filev2( $args );
|
||||
break;
|
||||
case 'WC_Log_Handler_DB':
|
||||
|
@ -113,8 +113,7 @@ class PageController {
|
|||
switch ( $view ) {
|
||||
case 'list_files':
|
||||
default:
|
||||
$this->get_list_table()->prepare_items();
|
||||
$this->get_list_table()->display();
|
||||
$this->render_file_list_page();
|
||||
break;
|
||||
case 'single_file':
|
||||
WC_Admin_Status::status_logs_file();
|
||||
|
@ -122,15 +121,52 @@ class PageController {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the file list view.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function render_file_list_page() {
|
||||
$defaults = $this->get_query_param_defaults();
|
||||
$params = $this->get_query_params();
|
||||
|
||||
$this->get_list_table()->set_file_args( $params );
|
||||
$this->get_list_table()->prepare_items();
|
||||
?>
|
||||
<form id="logs-filter" method="get">
|
||||
<input type="hidden" name="page" value="wc-status" />
|
||||
<input type="hidden" name="tab" value="logs" />
|
||||
<?php foreach ( $params as $key => $value ) : ?>
|
||||
<?php if ( $value !== $defaults[ $key ] ) : ?>
|
||||
<input type="hidden" name="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $value ); ?>" />
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
<?php $this->get_list_table()->display(); ?>
|
||||
</form>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default values for URL query params for FileV2 views.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
private function get_query_param_defaults() {
|
||||
return array(
|
||||
'view' => 'list_files',
|
||||
'orderby' => 'modified',
|
||||
'order' => 'desc',
|
||||
'source' => '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and validate URL query params for FileV2 views.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_filev2_query_params() {
|
||||
$defaults = array(
|
||||
'view' => 'list_files',
|
||||
);
|
||||
private function get_query_params() {
|
||||
$defaults = $this->get_query_param_defaults();
|
||||
$params = filter_input_array(
|
||||
INPUT_GET,
|
||||
array(
|
||||
|
@ -141,6 +177,21 @@ class PageController {
|
|||
'default' => $defaults['view'],
|
||||
),
|
||||
),
|
||||
'orderby' => array(
|
||||
'filter' => FILTER_VALIDATE_REGEXP,
|
||||
'options' => array(
|
||||
'regexp' => '/^(created|modified|source|size)$/',
|
||||
'default' => $defaults['orderby']
|
||||
),
|
||||
),
|
||||
'order' => array(
|
||||
'filter' => FILTER_VALIDATE_REGEXP,
|
||||
'options' => array(
|
||||
'regexp' => '/^(asc|desc)$/i',
|
||||
'default' => $defaults['order']
|
||||
),
|
||||
),
|
||||
'source' => FILTER_SANITIZE_STRING,
|
||||
),
|
||||
false
|
||||
);
|
||||
|
@ -170,7 +221,7 @@ class PageController {
|
|||
* @return void
|
||||
*/
|
||||
private function setup_screen_options() {
|
||||
$params = $this->get_filev2_query_params();
|
||||
$params = $this->get_query_params();
|
||||
|
||||
if ( 'list_files' === $params['view'] ) {
|
||||
// Ensure list table columns are initialized early enough to enable column hiding.
|
||||
|
|
Loading…
Reference in New Issue