Automattic\WooCommerce\Internal\Admin\Logging\FileV2

FileController::get_files()publicWC 1.0

Get an array of log files.

Метод класса: FileController{}

Хуков нет.

Возвращает

File[]|int|WP_Error.

Использование

$FileController = new FileController();
$FileController->get_files( $args, $count_only );
$args(массив)

Arguments to filter and sort the files that are returned.

По умолчанию: array()

  • date_end(int)
    The end of the date range to filter by, as a Unix timestamp.

  • date_filter(строка)
    Filter files by one of the date props. 'created' or 'modified'.

  • date_start(int)
    The beginning of the date range to filter by, as a Unix timestamp.

  • offset(int)
    Omit this number of files from the beginning of the list. Works with $per_page to do pagination.

  • order(строка)
    The sort direction. 'asc' or 'desc'.
    По умолчанию: 'desc'

  • orderby(строка)
    The property to sort the list by. 'created', 'modified', 'source', 'size'.
    По умолчанию: 'modified'

  • per_page(int)
    The number of files to include in the list. Works with $offset to do pagination.

  • source(строка)
    Only include files from this source.
$count_only(true|false)
True to return a total count of the files.
По умолчанию: false

Код FileController::get_files() WC 9.7.1

public function get_files( array $args = array(), bool $count_only = false ) {
	$args = wp_parse_args( $args, self::DEFAULTS_GET_FILES );

	$pattern = $args['source'] . '*.log';
	$paths   = glob( Settings::get_log_directory() . $pattern );

	if ( false === $paths ) {
		return new WP_Error(
			'wc_log_directory_error',
			__( 'Could not access the log file directory.', 'woocommerce' )
		);
	}

	$files = $this->convert_paths_to_objects( $paths );

	if ( $args['date_filter'] && $args['date_start'] && $args['date_end'] ) {
		switch ( $args['date_filter'] ) {
			case 'created':
				$files = array_filter(
					$files,
					fn( $file ) => $file->get_created_timestamp() >= $args['date_start']
						&& $file->get_created_timestamp() <= $args['date_end']
				);
				break;
			case 'modified':
				$files = array_filter(
					$files,
					fn( $file ) => $file->get_modified_timestamp() >= $args['date_start']
						&& $file->get_modified_timestamp() <= $args['date_end']
				);
				break;
		}
	}

	if ( true === $count_only ) {
		return count( $files );
	}

	$multi_sorter = function( $sort_sets, $order_sets ) {
		$comparison = 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];
			}

			if ( 0 !== $comparison ) {
				break;
			}
		}

		return $comparison;
	};

	switch ( $args['orderby'] ) {
		case 'created':
			$sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
				$sort_sets  = array(
					array( $a->get_created_timestamp(), $b->get_created_timestamp() ),
					array( $a->get_source(), $b->get_source() ),
					array( $a->get_rotation() || -1, $b->get_rotation() || -1 ),
				);
				$order_sets = array( $args['order'], 'asc', 'asc' );
				return $multi_sorter( $sort_sets, $order_sets );
			};
			break;
		case 'modified':
			$sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
				$sort_sets  = array(
					array( $a->get_modified_timestamp(), $b->get_modified_timestamp() ),
					array( $a->get_source(), $b->get_source() ),
					array( $a->get_rotation() || -1, $b->get_rotation() || -1 ),
				);
				$order_sets = array( $args['order'], 'asc', '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() ),
					array( $a->get_rotation() || -1, $b->get_rotation() || -1 ),
				);
				$order_sets = array( $args['order'], 'desc', 'asc' );
				return $multi_sorter( $sort_sets, $order_sets );
			};
			break;
		case 'size':
			$sort_callback = function( $a, $b ) use ( $args, $multi_sorter ) {
				$sort_sets  = array(
					array( $a->get_file_size(), $b->get_file_size() ),
					array( $a->get_source(), $b->get_source() ),
					array( $a->get_rotation() || -1, $b->get_rotation() || -1 ),
				);
				$order_sets = array( $args['order'], 'asc', 'asc' );
				return $multi_sorter( $sort_sets, $order_sets );
			};
			break;
	}

	usort( $files, $sort_callback );

	return array_slice( $files, $args['offset'], $args['per_page'] );
}