�PNG  IHDR22?�� pHYs  �� OiCCPPhotoshop ICC profilexڝSgTS�=���BK���KoR RB���&*! J�!��Q�EEȠ�����Q, � ��!���������{�kּ������>���� �H3Q5� �B�������.@� $p�d!s�#�~<<+"��x� �M��0���B�\���t�8K�@z�B�@F���&S�`�cb�P-`'������{[�!�� e�Dh;��V�EX0fK�9�-0IWfH���� � 0Q��){`�##x��F�W<�+��*x��<�$9E�[-qWW.(�I+6aa�@.�y�2�4���������x����6��_-��"bb���ϫp@�t~��,/��;�m��%�h^ �u��f�@����W�p�~<�5�j>{�-�]c�K'Xt����o��(�h���w��?�G�%�fI�q^D$.Tʳ?�D��*�A��,���� �`6�B$��BB d�r`)��B(�Ͱ*`/�@4�Qh��p.�U�=p�a��(�� A�a!ڈb�X#����!�H�$ ɈQ"K�5H1R�T UH�=r9�\F��;�2����G1���Q=� �C��7�F� �dt1�����r�=�6��Ыhڏ>C�0��3�l0.��B�8, �c˱"� ���V����cϱw�E� 6wB aAHXLXN�H� $4� 7 �Q�'"��K�&���b21�XH,#��/{�C�7$�C2'��I��T��F�nR#�,��4H#���dk�9�, +ȅ����3��!�[ �b@q��S�(R�jJ��4�e�2AU��Rݨ�T5�ZB���R�Q��4u�9̓IK�����hh�i��t�ݕN��W���G���w ��Ljg(�gw��L�Ӌ�T071���oUX*�*|�� �J�&�*/T����ު U�U�T��^S}�FU3S� Ԗ�U��P�SSg�;���g�oT?�~Y��Y�L�OC�Q��_�� c�x,!k ��u�5�&���|v*�����=���9C3J3W�R�f?�q��tN �(���~���)�)�4L�1e\k����X�H�Q�G�6������E�Y��A�J'\'Gg����S�Sݧ �M=:��.�k���Dw�n��^��Lo��y���}/�T�m���G X� $� �<�5qo</���QC]�@C�a�a�ᄑ��<��F�F�i�\�$�m�mƣ&&!&KM�M�RM��)�;L;L���͢�֙5�=1�2��כ߷`ZxZ,����eI��Z�Yn�Z9Y�XUZ]�F���%ֻ�����N�N���gð�ɶ�����ۮ�m�}agbg�Ů��}�}��= ���Z~s�r:V:ޚΜ�?}����/gX���3��)�i�S��Ggg�s�󈋉K��.�>.���Ƚ�Jt�q]�z���������ۯ�6�i�ܟ�4�)�Y3s���C�Q��? ��0k߬~OCO�g��#/c/�W�װ��w��a�>�>r��>�<7�2�Y_�7��ȷ�O�o�_��C#�d�z����%g��A�[��z|!��?:�e����A���AA�������!h�쐭!��Α�i�P~���a�a��~ '���W�?�p�X�1�5w��Cs�D�D�Dޛg1O9�-J5*>�.j<�7�4�?�.fY��X�XIlK9.*�6nl������� �{�/�]py�����.,:�@L�N8��A*��%�w%� y��g"/�6ш�C\*N�H*Mz�쑼5y$�3�,幄'���L Lݛ:��v m2=:�1����qB�!M��g�g�fvˬe����n��/��k���Y- �B��TZ(�*�geWf�͉�9���+��̳�ې7�����ᒶ��KW-X潬j9�������(�x��oʿ�ܔ���Ĺd�f�f���-�[����n �ڴ �V����E�/��(ۻ��C���<��e����;?T�T�T�T6��ݵa��n��{��4���[���>ɾ�UUM�f�e�I���?�������m]�Nmq����#�׹���=TR��+�G�����w- 6 U����#pDy��� �� :�v�{���vg/jB��F�S��[b[�O�>����z�G��499�?r����C�d�&����ˮ/~�����јѡ�򗓿m|������������x31^�V���w�w��O�| (�h���SЧ��������c3-� cHRMz%������u0�`:�o�_�F5IDATx��ytUս�?�;���fN$$��@B�!�k��� -*�gE���j�O�kQ�ʠ`U�B_ �AÐ0%�Ȕ�@r3ޛ;߳��$�@��v����^�s����������$��ߢ !�W��5�̱'-٘������4�>�Rƫ$�G��0U�H}�_ _� #��k�MJ/=�9Du *ڲ�k�PQ;0��j|�*�$�^컂脛y���>�z×� B��� ��(�:$�ʔ_�6��C!B����MVx��a��ζyY����j��ƋY2�9�F�{r��r�딟F��Q��hY6��� x��[l��RC��������nt@��;P��3��� F�w��7��:�P���ɋ'���mj���Bƪ�$�Q�L��y��߷g��\���z����v5�p�v$�v櫻�`6~K �Y�X����,�:��"�o�e0II"(��,������l_�C���/4�+f�E�!q��:����u����c��樼.UE�@� �]K�W���$-�h]�JG���Dz)�`��.�A/ ���mᏣc�3jK� �!�>���$�Q�!�_�? \�C2(E �M$Z I{�U���s�ǣ���=��@Բe ޻��W�����@ �w����}�9v,)��.}*|"�s��di���,���G�]W�AC�������|<����[��ߕ������K�*�G nߴ!��‹��J�!��6(L�a)��Am��������;adf��:�xm��nh�;֦I��(���o�C~ډa?�=��s����h��E������xp����)���_���t��W�����´;8���`dz=(��@@�+,�;�`�fhl��XȌ�3,uI�~z(J����� �ZX�A����H'�À�㜦�82|��a�n�z2��I���� ���EY�h�ε<�0�q�{��C��v�˅��?"@��χ�x�WS+��N�~�KQ�J�8ل�� _.W��� !TU{�����,��"�� �;�������HI S� ��))�o�1�H�D� ���� (� ;����"Ҕ��,���� �F=���O��&�Ph>�`���y��߲ &S��P�<��'��1s�1cĂ�N����2)����@�u��tv��<�+�-&�����h��[!FSG���' � z z0��<����8d&�����E0�,��<��"!P�b4��� �{S�����X����.�囤���T��hEF��X�q0"-<����h���]�Py*����F⩍��@v�˂�Y08�� .q��a��s����(!<̫1�3�L�H��7sYW�`��qtu�\7��?�@�Y�� %Ka�x�GZ�*W�P��y�����N0r9��Z�c�%���q,��k�����!!,��>���IB��� �x1�Xu�Y���v-���ֺ��N������JP��a�2`�e5��w��u;�6�i���̃��(K���ťa��(l51��JL��O'%���]���ٖ뿉!��{*�p+f ���i8��D�ȑ���ܩ�M��V��ӟz��x�7Z�1i}�^͜���6e����x��)u�d�ˏ�V�eG�\�X1�����=n�U'�~Y����L�\,�(d!x ,�:�TW8� M`r���@��r+v�Q0��>^;˴�VbF��('����� �*O��i��j�Ӆ����^�S3&� ���{������.ی�� N;��NU%d0�Ce\r"�C�b��&t7N�7��� �ȪD�[%l�۪�$��'m?�A9tI�9>��NVL ��� 6v~M�����ъ�B���0����9$&=ώm��W�0���L���&��E���f�f��'+�9��hv��7,���M��6�D�&�^� �����8 D֮�����o��k^z��O��hP�8q�5d*��YVq3�{��������#LX4�?��jP=3�~�؅c�ݰ�̱�ұ��8̲��2�G�����j��έ�9��w7���U�X�w�85C��?!I&���o&����L���I#�^�eV�,�^t5�k� �xs˛\��U�8��."���{���ǵ�l4��ݼ�;��r"���G�/~���gJV�N糦��ZZ^k+NN�D��I$�=OE�z����|*�*{��%k���G��>�`9��b;�s���8,�qs��c��.b��q�%�>r��n�R�Ƨ�%�,�A��le#�8���}; 21�0�b�&�QX�f���q�00����$>�h�N�'�a3��A���/�r ��L5�8�GԲ'�����w;��c#�q1���h$*��V^�k��QĽ�b:i������� ����H F#�_���,��]g����Ը����`RUU���� ��"�qD���>-��ϤI�~��3�̴�����NZ[[555�eeek7�m����YF#[��Đ��R��i��L�H�r ��@��J-�Pŝ��_|�-��6y��ks�V�U~��6���w.����(��0A� 4��f��� /���C�xŊ�ٳ����z=6�����rg���mKo��o�/����a�g���?'9%9��P��斓��~���L{�� �H��{AW��B��$����v�1b�&Mb�ر��ر#�0`�`xMBp! )Z�Nmo�mZ�g�}�eذaWTVV ���'99��C5��&�&+5�bT>|8��PUUETT����&���t��~TE%��'������b�tEGG;RSS�S�N�v��ѯ��t����u1@�ޚ"f��͈���r'L�Ph��m��ƌ�74o��`�x�q�P���NRSS�X,�����l&&&��MFFA!-��`��ҷ�n��Yuu�����x<I�LB%����KԒ50�!�3�bz�����ʸKL���;---�l6[��`��������Ȏ;�����egg4��!D�T��jWujyZ��������K����%����p�^���X,6��sV=�ܖ����6�3�X ���4ҿ��A2�&����_k�;d�� �VfOIEND�B`�
Warning: session_start(): Session cannot be started after headers have already been sent in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 124

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 130

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 131

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 132

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 133

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 134

Warning: Cannot modify header information - headers already sent by (output started at /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php:1) in /home/mciraet/www/wp-includes/php-ai-client/src/Providers/Enums/Documentsoliders.php on line 135
0x0x
PHP 8.2.31
Preview: FileController.php Size: 21.23 KB
/home/mciraet/www/wp-content/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/FileController.php
<?php
declare( strict_types = 1 );

namespace Automattic\WooCommerce\Internal\Admin\Logging\FileV2;

use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Internal\Admin\Logging\Settings;
use Automattic\WooCommerce\Internal\Utilities\FilesystemUtil;
use Exception;
use PclZip;
use WC_Cache_Helper;
use WP_Error;

/**
 * FileController class.
 */
class FileController {
	/**
	 * The maximum number of rotations for a file before they start getting overwritten.
	 *
	 * This number should not go above 10, or it will cause issues with the glob patterns.
	 *
	 * const int
	 */
	private const MAX_FILE_ROTATIONS = 10;

	/**
	 * Default values for arguments for the get_files method.
	 *
	 * @const array
	 */
	public const DEFAULTS_GET_FILES = array(
		'date_end'    => 0,
		'date_filter' => '',
		'date_start'  => 0,
		'offset'      => 0,
		'order'       => 'desc',
		'orderby'     => 'modified',
		'per_page'    => 20,
		'source'      => '',
	);

	/**
	 * Default values for arguments for the search_within_files method.
	 *
	 * @const array
	 */
	public const DEFAULTS_SEARCH_WITHIN_FILES = array(
		'offset'   => 0,
		'per_page' => 50,
	);

	/**
	 * The maximum number of files that can be searched at one time.
	 *
	 * @const int
	 */
	public const SEARCH_MAX_FILES = 100;

	/**
	 * The maximum number of search results that can be returned at one time.
	 *
	 * @const int
	 */
	public const SEARCH_MAX_RESULTS = 200;

	/**
	 * The cache group name to use for caching operations.
	 *
	 * @const string
	 */
	private const CACHE_GROUP = 'log-files';

	/**
	 * A cache key for storing and retrieving the results of the last logs search.
	 *
	 * @const string
	 */
	private const SEARCH_CACHE_KEY = 'logs_previous_search';

	/**
	 * Get the file size limit that determines when to rotate a file.
	 *
	 * @return int
	 */
	private function get_file_size_limit(): int {
		$default = 5 * MB_IN_BYTES;

		/**
		 * Filter the threshold size of a log file at which point it will get rotated.
		 *
		 * @since 3.4.0
		 *
		 * @param int $file_size_limit The file size limit in bytes.
		 */
		$file_size_limit = apply_filters( 'woocommerce_log_file_size_limit', $default );

		if ( ! is_int( $file_size_limit ) || $file_size_limit < 1 ) {
			return $default;
		}

		return $file_size_limit;
	}

	/**
	 * Write a log entry to the appropriate file, after rotating the file if necessary.
	 *
	 * @param string   $source The source property of the log entry, which determines which file to write to.
	 * @param string   $text   The contents of the log entry to add to a file.
	 * @param int|null $time   Optional. The time of the log entry as a Unix timestamp. Defaults to the current time.
	 *
	 * @return bool True if the contents were successfully written to the file.
	 */
	public function write_to_file( string $source, string $text, ?int $time = null ): bool {
		if ( is_null( $time ) ) {
			$time = time();
		}

		$path = Settings::get_log_directory() . $this->generate_filename( $source, $time );
		$file = new File( $path );

		$size = $file->get_file_size();
		if ( false !== $size && $size >= $this->get_file_size_limit() ) {
			if ( ! $this->rotate_file( $file ) ) {
				return false;
			}

			$file = new File( $path );
		}

		return $file->write( $text );
	}

	/**
	 * Generate the full name of a file based on source and date values.
	 *
	 * @param string $source The source property of a log entry, which determines the filename.
	 * @param int    $time   The time of the log entry as a Unix timestamp.
	 *
	 * @return string
	 */
	private function generate_filename( string $source, int $time ): string {
		$file_id = File::generate_file_id( $source, null, $time );
		$hash    = File::generate_hash( $file_id );

		return "$file_id-$hash.log";
	}

	/**
	 * Get all the rotations of a file and increment them, so that they overwrite the previous file with that rotation.
	 *
	 * @param File $file The un-rotated ("current") iteration of the file to rotate.
	 *
	 * @return bool True if the file and all its rotations were successfully rotated.
	 */
	private function rotate_file( File $file ): bool {
		$rotations = $this->get_rotation_siblings( $file );

		$max_rotation_marker = self::MAX_FILE_ROTATIONS - 1;

		// Don't rotate a file with the maximum rotation.
		unset( $rotations[ $max_rotation_marker ] );

		$results = array();
		// Rotate starting with oldest first and working backwards.
		for ( $i = $max_rotation_marker; $i >= 0; $i -- ) {
			if ( isset( $rotations[ $i ] ) ) {
				$results[] = $rotations[ $i ]->rotate();
			}
		}
		$results[] = $file->rotate();

		return ! in_array( false, $results, true );
	}

	/**
	 * Get an array of log files.
	 *
	 * @param array $args      {
	 *     Optional. Arguments to filter and sort the files that are returned.
	 *
	 *     @type int    $date_end    The end of the date range to filter by, as a Unix timestamp.
	 *     @type string $date_filter Filter files by one of the date props. 'created' or 'modified'.
	 *     @type int    $date_start  The beginning of the date range to filter by, as a Unix timestamp.
	 *     @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'. Defaults to 'desc'.
	 *     @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 string $source      Only include files from this source.
	 * }
	 * @param bool  $count_only Optional. True to return a total count of the files.
	 *
	 * @return File[]|int|WP_Error
	 */
	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'] );
	}

	/**
	 * Get one or more File instances from an array of file IDs.
	 *
	 * @param array $file_ids An array of file IDs (file basename without the hash).
	 *
	 * @return File[]
	 */
	public function get_files_by_id( array $file_ids ): array {
		$log_directory = Settings::get_log_directory();
		$paths         = array();

		foreach ( $file_ids as $file_id ) {
			// Look for the standard filename format first, which includes a hash.
			$glob = glob( $log_directory . $file_id . '-*.log' );

			if ( ! $glob ) {
				$glob = glob( $log_directory . $file_id . '.log' );
			}

			if ( is_array( $glob ) ) {
				$paths = array_merge( $paths, $glob );
			}
		}

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

		return $files;
	}

	/**
	 * Get a File instance from a file ID.
	 *
	 * @param string $file_id A file ID (file basename without the hash).
	 *
	 * @return File|WP_Error
	 */
	public function get_file_by_id( string $file_id ) {
		$result = $this->get_files_by_id( array( $file_id ) );

		if ( count( $result ) < 1 ) {
			return new WP_Error(
				'wc_log_file_error',
				esc_html__( 'This file does not exist.', 'woocommerce' )
			);
		}

		if ( count( $result ) > 1 ) {
			return new WP_Error(
				'wc_log_file_error',
				esc_html__( 'Multiple files match this ID.', 'woocommerce' )
			);
		}

		return reset( $result );
	}

	/**
	 * Get File instances for a given file ID and all of its related rotations.
	 *
	 * @param string $file_id A file ID (file basename without the hash).
	 *
	 * @return File[]|WP_Error An associative array where the rotation integer of the file is the key, and a "current"
	 *                         key for the iteration of the file that hasn't been rotated (if it exists).
	 */
	public function get_file_rotations( string $file_id ) {
		$file = $this->get_file_by_id( $file_id );

		if ( is_wp_error( $file ) ) {
			return $file;
		}

		$current = array();

		if ( is_null( $file->get_rotation() ) ) {
			$current['current'] = $file;
		} else {
			$current_file_id = File::generate_file_id( $file->get_source(), null, $this->get_filename_timestamp( $file ) );
			$result          = $this->get_file_by_id( $current_file_id );
			if ( ! is_wp_error( $result ) ) {
				$current['current'] = $result;
			}
		}

		return array_merge( $current, $this->get_rotation_siblings( $file ) );
	}

	/**
	 * Get the creation timestamp encoded in a file's name, or 0 if it doesn't use the standard format.
	 *
	 * @param File $file The file to get the timestamp from.
	 *
	 * @return int
	 */
	private function get_filename_timestamp( File $file ): int {
		return $file->has_standard_filename() ? $file->get_created_timestamp() : 0;
	}

	/**
	 * Get File instances for the existing rotations of a file.
	 *
	 * @param File $file Any iteration of a file, from which the source and creation date are taken.
	 *
	 * @return File[] An associative array where the rotation integer of the file is the key, sorted by rotation.
	 */
	private function get_rotation_siblings( File $file ): array {
		$source  = $file->get_source();
		$created = $this->get_filename_timestamp( $file );

		$rotations_pattern = sprintf(
			'.[%s]',
			implode(
				'',
				range( 0, self::MAX_FILE_ROTATIONS - 1 )
			)
		);

		$created_pattern = $created ? '-' . gmdate( 'Y-m-d', $created ) . '-' : '';

		$rotation_pattern = Settings::get_log_directory() . $source . $rotations_pattern . $created_pattern . '*.log';
		$rotation_paths   = glob( $rotation_pattern );
		$rotation_files   = $this->convert_paths_to_objects( $rotation_paths );

		$rotations = array();
		foreach ( $rotation_files as $rotation_file ) {
			if ( $rotation_file->is_readable() ) {
				$rotations[ $rotation_file->get_rotation() ] = $rotation_file;
			}
		}

		ksort( $rotations );

		return $rotations;
	}

	/**
	 * Helper method to get an array of File instances.
	 *
	 * @param array $paths An array of absolute file paths.
	 *
	 * @return File[]
	 */
	private function convert_paths_to_objects( array $paths ): array {
		$files = array_map(
			function( $path ) {
				$file = new File( $path );
				return $file->is_readable() ? $file : null;
			},
			$paths
		);

		return array_filter( $files );
	}

	/**
	 * Get a list of sources for existing log files.
	 *
	 * @return array|WP_Error
	 */
	public function get_file_sources() {
		$paths = glob( Settings::get_log_directory() . '*.log' );
		if ( false === $paths ) {
			return new WP_Error(
				'wc_log_directory_error',
				__( 'Could not access the log file directory.', 'woocommerce' )
			);
		}

		$all_sources = array_map(
			function( $path ) {
				$file = new File( $path );
				return $file->is_readable() ? $file->get_source() : null;
			},
			$paths
		);

		return array_unique( array_filter( $all_sources ) );
	}

	/**
	 * Delete one or more files from the filesystem.
	 *
	 * @param array $file_ids An array of file IDs (file basename without the hash).
	 *
	 * @return int The number of files that were deleted.
	 */
	public function delete_files( array $file_ids ): int {
		$deleted = 0;

		$files = $this->get_files_by_id( $file_ids );
		foreach ( $files as $file ) {
			$result = $file->delete();

			if ( true === $result ) {
				$deleted ++;
			}
		}

		if ( $deleted > 0 ) {
			$this->invalidate_cache();
		}

		return $deleted;
	}

	/**
	 * Delete files of a given source that were last modified before a given time.
	 *
	 * Files are enumerated lazily and are neither sorted nor turned into File instances, so this
	 * stays cheap on directories holding a large number of files, unlike get_files().
	 *
	 * @param string $source          Match files whose name begins with this source.
	 * @param int    $modified_before Only delete files modified before this Unix timestamp.
	 * @param int    $limit           The maximum number of files to delete.
	 *
	 * @return int The number of files that were deleted.
	 */
	public function delete_stale_files( string $source, int $modified_before, int $limit ): int {
		if ( '' === $source || $limit < 1 ) {
			return 0;
		}

		try {
			$filesystem = FilesystemUtil::get_wp_filesystem_direct();
			$iterator   = new \FilesystemIterator(
				Settings::get_log_directory(),
				\FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::KEY_AS_FILENAME
			);
		} catch ( Exception $exception ) {
			// Surface this so a persistent failure to reach the log directory doesn't stay silent.
			wc_get_logger()->warning(
				sprintf(
					'Could not enumerate the log directory to delete stale "%1$s" files: %2$s',
					$source,
					$exception->getMessage()
				),
				array( 'source' => 'wc-logs-cleanup' )
			);

			return 0;
		}

		$deleted = 0;

		foreach ( $iterator as $basename => $path ) {
			if ( ! str_starts_with( (string) $basename, $source ) || ! str_ends_with( (string) $basename, '.log' ) ) {
				continue;
			}

			$path = (string) $path;

			$modified = $filesystem->mtime( $path );

			if ( false === $modified || $modified >= $modified_before ) {
				continue;
			}

			if ( $filesystem->delete( $path, false, 'f' ) ) {
				++$deleted;
			}

			if ( $deleted >= $limit ) {
				break;
			}
		}

		if ( $deleted > 0 ) {
			$this->invalidate_cache();
		}

		return $deleted;
	}

	/**
	 * Stream a single file to the browser without zipping it first.
	 *
	 * @param string $file_id A file ID (file basename without the hash).
	 *
	 * @return WP_Error|void Only returns something if there is an error.
	 */
	public function export_single_file( $file_id ) {
		$file = $this->get_file_by_id( $file_id );

		if ( is_wp_error( $file ) ) {
			return $file;
		}

		$file_name = $file->get_file_id() . '.log';
		$exporter  = new FileExporter( $file->get_path(), $file_name );

		return $exporter->emit_file();
	}

	/**
	 * Create a zip archive of log files and stream it to the browser.
	 *
	 * @param array $file_ids An array of file IDs (file basename without the hash).
	 *
	 * @return WP_Error|void Only returns something if there is an error.
	 */
	public function export_multiple_files( array $file_ids ) {
		$files = $this->get_files_by_id( $file_ids );

		if ( count( $files ) < 1 ) {
			return new WP_Error(
				'wc_logs_invalid_file',
				__( 'Could not access the specified files.', 'woocommerce' )
			);
		}

		$temp_dir = get_temp_dir();

		if ( ! is_dir( $temp_dir ) || ! wp_is_writable( $temp_dir ) ) {
			return new WP_Error(
				'wc_logs_invalid_directory',
				__( 'Could not write to the temp directory. Try downloading files one at a time instead.', 'woocommerce' )
			);
		}

		require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';

		$path       = trailingslashit( $temp_dir ) . 'woocommerce_logs_' . gmdate( 'Y-m-d_H-i-s' ) . '.zip';
		$file_paths = array_map(
			fn( $file ) => $file->get_path(),
			$files
		);
		$archive    = new PclZip( $path );

		$archive->create( $file_paths, PCLZIP_OPT_REMOVE_ALL_PATH );

		$exporter = new FileExporter( $path );

		return $exporter->emit_file();
	}

	/**
	 * Search within a set of log files for a particular string.
	 *
	 * @param string $search     The string to search for.
	 * @param array  $args       Optional. Arguments for pagination of search results.
	 * @param array  $file_args  Optional. Arguments to filter and sort the files that are returned. See get_files().
	 * @param bool   $count_only Optional. True to return a total count of the matches.
	 *
	 * @return array|int|WP_Error When matches are found, each array item is an associative array that includes the
	 *                            file ID, line number, and the matched string with HTML markup around the matched parts.
	 */
	public function search_within_files( string $search, array $args = array(), array $file_args = array(), bool $count_only = false ) {
		if ( '' === $search ) {
			return $count_only ? 0 : array();
		}

		$search = esc_html( $search );

		$args = wp_parse_args( $args, self::DEFAULTS_SEARCH_WITHIN_FILES );

		$file_args = array_merge(
			$file_args,
			array(
				'offset'   => 0,
				'per_page' => self::SEARCH_MAX_FILES,
			)
		);

		$cache_key = WC_Cache_Helper::get_prefixed_key( self::SEARCH_CACHE_KEY, self::CACHE_GROUP );
		$query     = wp_json_encode( array( $search, $args, $file_args ) );
		$cache     = wp_cache_get( $cache_key );
		$is_cached = isset( $cache['query'], $cache['results'] ) && $query === $cache['query'];

		if ( true === $is_cached ) {
			$matched_lines = $cache['results'];
		} else {
			$files = $this->get_files( $file_args );
			if ( is_wp_error( $files ) ) {
				return $files;
			}

			// Max string size * SEARCH_MAX_RESULTS = ~1MB largest possible cache entry.
			$max_string_size = 5 * KB_IN_BYTES;

			$matched_lines = array();

			foreach ( $files as $file ) {
				$stream      = $file->get_stream();
				$line_number = 1;

				while ( ! feof( $stream ) ) {
					$line = fgets( $stream, $max_string_size );
					if ( ! is_string( $line ) ) {
						continue;
					}

					$sanitized_line = esc_html( trim( $line ) );
					if ( false !== stripos( $sanitized_line, $search ) ) {
						$matched_lines[] = array(
							'file_id'     => $file->get_file_id(),
							'line_number' => $line_number,
							'line'        => $sanitized_line,
						);
					}

					if ( count( $matched_lines ) >= self::SEARCH_MAX_RESULTS ) {
						$file->close_stream();
						break 2;
					}

					if ( false !== strstr( $line, PHP_EOL ) ) {
						$line_number ++;
					}
				}

				$file->close_stream();
			}

			$to_cache = array(
				'query'   => $query,
				'results' => $matched_lines,
			);
			wp_cache_set( $cache_key, $to_cache, self::CACHE_GROUP, DAY_IN_SECONDS );
		}

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

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

	/**
	 * Calculate the size, in bytes, of the log directory.
	 *
	 * @return int
	 */
	public function get_log_directory_size(): int {
		$bytes = 0;
		$path  = realpath( Settings::get_log_directory( false ) );

		if ( empty( $path ) ) {
			return 0;
		}

		if ( wp_is_writable( $path ) ) {
			$iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $path, \FilesystemIterator::SKIP_DOTS ), \RecursiveIteratorIterator::CATCH_GET_CHILD );

			foreach ( $iterator as $file ) {
				$bytes += $file->getSize();
			}
		}

		return $bytes;
	}

	/**
	 * Invalidate the cache group related to log file data.
	 *
	 * @return bool True on successfully invalidating the cache.
	 */
	public function invalidate_cache(): bool {
		return WC_Cache_Helper::invalidate_cache_group( self::CACHE_GROUP );
	}
}

Directory Contents

Dirs: 0 × Files: 5
Name Size Perms Modified Actions
13.48 KB lrw-r--r-- 2026-08-11 10:05:48
Edit Download
21.23 KB lrw-r--r-- 2026-08-11 10:05:48
Edit Download
3.81 KB lrw-r--r-- 2026-08-11 10:05:48
Edit Download
7.73 KB lrw-r--r-- 2026-08-11 10:05:48
Edit Download
5.62 KB lrw-r--r-- 2026-08-11 10:05:48
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).
© 2026 0xNothings — Secure File Manager. All rights reserved. Built with ❤️ & Tailwind x Dark UI