Automattic\WooCommerce\Internal\Admin\Logging\FileV2

File::parse_path()public staticWC 1.0

Parse a path to a log file to determine if it uses the standard filename structure and various properties.

This makes assumptions about the structure of the log file's name. Using - to separate the name into segments, if there are at least 5 segments, it assumes that the last segment is the hash, and the three segments before that make up the date when the file was created in YYYY-MM-DD format. Any segments left after that are the "source" that generated the log entries. If the filename doesn't have enough segments, it falls back to the source and the hash both being the entire filename, and using the inode change time as the creation date.

Example:

my-custom-plugin.2-2025-01-01-a1b2c3d4e5f.log
	  |          |       |         |

'my-custom-plugin' | '2025-01-01' | (source) | (created) | '2' 'a1b2c3d4e5f' (rotation) (hash)

Method of the class: File{}

No Hooks.

Return

Array.

Usage

$result = File::parse_path( $path ): array;
$path(string) (required)
The full path of the log file.

File::parse_path() code WC 8.7.0

public static function parse_path( string $path ): array {
	$defaults = array(
		'dirname'   => '',
		'basename'  => '',
		'extension' => '',
		'filename'  => '',
		'source'    => '',
		'rotation'  => null,
		'created'   => 0,
		'hash'      => '',
		'file_id'   => '',
	);

	$parsed = array_merge( $defaults, pathinfo( $path ) );

	$segments  = explode( '-', $parsed['filename'] );
	$timestamp = strtotime( implode( '-', array_slice( $segments, -4, 3 ) ) );

	if ( count( $segments ) >= 5 && false !== $timestamp ) {
		$parsed['source']  = implode( '-', array_slice( $segments, 0, -4 ) );
		$parsed['created'] = $timestamp;
		$parsed['hash']    = array_slice( $segments, -1 )[0];
	} else {
		$parsed['source'] = implode( '-', $segments );
	}

	$rotation_marker = strrpos( $parsed['source'], '.', -1 );
	if ( false !== $rotation_marker ) {
		$rotation = substr( $parsed['source'], -1 );
		if ( is_numeric( $rotation ) ) {
			$parsed['rotation'] = intval( $rotation );
		}

		$parsed['source'] = substr( $parsed['source'], 0, $rotation_marker );
	}

	$parsed['file_id'] = static::generate_file_id(
		$parsed['source'],
		$parsed['rotation'],
		$parsed['created']
	);

	return $parsed;
}