<?php

/**
 * Helper functions for file system.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Helpers;

defined('ABSPATH') || exit;
/**
 * Class File_System_Helper
 */
class File_System_Helper
{
    /**
     * Get the contents of a file.
     *
     * @param string $file_path The path to the file.
     *
     * @return string|false The contents of the file, or false on failure.
     */
    public static function get_contents(string $file_path): string|false
    {
        self::initialize_filesystem();
        global $wp_filesystem;
        return $wp_filesystem->get_contents($file_path);
    }
    /**
     * Put contents into a file.
     *
     * @param string $file_path The path to the file.
     * @param string $contents  The contents to write to the file.
     *
     * @return bool True on success, false on failure.
     *
     * @used Northmill Sync
     */
    public static function put_contents(string $file_path, string $contents): bool
    {
        self::initialize_filesystem();
        global $wp_filesystem;
        return $wp_filesystem->put_contents($file_path, $contents);
    }
    /**
     * Initialize the WP_Filesystem.
     *
     * @return void
     */
    private static function initialize_filesystem(): void
    {
        if (!function_exists('WP_Filesystem')) {
            require_once \ABSPATH . 'wp-admin/includes/file.php';
        }
        WP_Filesystem();
    }
}
