Updates to FileController

This commit is contained in:
Corey McKrill 2023-10-09 19:47:56 -07:00
parent 899f27f62d
commit 390539048c
No known key found for this signature in database
GPG Key ID: 84BBFE669C4D97B8
1 changed files with 61 additions and 33 deletions

View File

@ -5,6 +5,9 @@ namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2;
use Automattic\Jetpack\Constants; use Automattic\Jetpack\Constants;
use WP_Error; use WP_Error;
/**
* FileController class.
*/
class FileController { class FileController {
/** /**
* @var string The absolute path to the log directory. * @var string The absolute path to the log directory.
@ -25,8 +28,8 @@ class FileController {
* Optional. Arguments to filter and sort the files that are returned. * Optional. Arguments to filter and sort the files that are returned.
* *
* @type int $offset Omit this number of files from the beginning of the list. Works with $per_page to do pagination. * @type int $offset Omit this number of files from the beginning of the list. Works with $per_page to do pagination.
* @type string $order The sort direction. 'asc' or 'desc'. * @type string $order The sort direction. 'asc' or 'desc'. Defaults to 'desc'.
* @type string $orderby The property to sort the list by. 'filename', 'modified', 'size'. Defaults to 'filename'. * @type string $orderby The property to sort the list by. 'created', 'modified', 'source', 'size'. Defaults to 'modified'.
* @type int $per_page The number of files to include in the list. Works with $offset to do pagination. * @type int $per_page The number of files to include in the list. Works with $offset to do pagination.
* @type string $source Only include files from this source. * @type string $source Only include files from this source.
* } * }
@ -37,8 +40,8 @@ class FileController {
public function get_files( array $args = array(), bool $count_only = false ) { public function get_files( array $args = array(), bool $count_only = false ) {
$defaults = array( $defaults = array(
'offset' => 0, 'offset' => 0,
'order' => 'asc', 'order' => 'desc',
'orderby' => 'filename', 'orderby' => 'modified',
'per_page' => 10, 'per_page' => 10,
'source' => '', 'source' => '',
); );
@ -70,47 +73,72 @@ class FileController {
); );
$files = array_filter( $files ); $files = array_filter( $files );
$sort_callback = function( $a, $b ) use ( $args ) { $multi_sorter = function( $sort_sets, $order_sets ) {
if ( $a === $b ) { $comparison = 0;
return 0;
while ( ! empty( $sort_sets ) ) {
$set = array_shift( $sort_sets );
$order = array_shift( $order_sets );
if ( 'desc' === $order ) {
$comparison = $set[1] <=> $set[0];
} else {
$comparison = $set[0] <=> $set[1];
} }
$compare = $a < $b; if ( $comparison !== 0 ) {
break;
if ( 'desc' === $args['order'] ) { }
return $compare ? 1 : -1;
} }
return $compare ? -1 : 1; return $comparison;
}; };
switch ( $args['orderby'] ) { switch ( $args['orderby'] ) {
case 'filename': case 'created':
usort( $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
$files, $sort_sets = array(
function( $a, $b ) use ( $sort_callback ) { array( $a->get_created_timestamp(), $b->get_created_timestamp() ),
return $sort_callback( $a->get_filename(), $b->get_filename() ); array( $a->get_source(), $b->get_source() ),
}
); );
$order_sets = array( $args['order'], 'asc' );
return $multi_sorter( $sort_sets, $order_sets );
};
break; break;
case 'modified': case 'modified':
usort( $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
$files, $sort_sets = array(
function( $a, $b ) use ( $sort_callback ) { array( $a->get_modified_timestamp(), $b->get_modified_timestamp() ),
return $sort_callback( $a->get_modified_timestamp(), $b->get_modified_timestamp() ); array( $a->get_source(), $b->get_source() ),
}
); );
$order_sets = array( $args['order'], 'asc' );
return $multi_sorter( $sort_sets, $order_sets );
};
break;
case 'source':
$sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
$sort_sets = array(
array( $a->get_source(), $b->get_source() ),
array( $a->get_created_timestamp(), $b->get_created_timestamp() ),
);
$order_sets = array( $args['order'], 'desc' );
return $multi_sorter( $sort_sets, $order_sets );
};
break; break;
case 'size': case 'size':
usort( $sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
$files, $sort_sets = array(
function( $a, $b ) use ( $sort_callback ) { array( $a->get_file_size(), $b->get_file_size() ),
return $sort_callback( $a->get_file_size(), $b->get_file_size() ); array( $a->get_source(), $b->get_source() ),
}
); );
$order_sets = array( $args['order'], 'asc' );
return $multi_sorter( $sort_sets, $order_sets );
};
break; break;
} }
usort( $files, $sort_callback );
return array_slice( $files, $args['offset'], $args['per_page'] ); return array_slice( $files, $args['offset'], $args['per_page'] );
} }
} }