<?php
/**
 * Protected file write, used by snapshot restore for file-kind snapshots.
 *
 * This is EXCLUDED from the WordPress.org (Lite) build, which ships no file
 * tools and therefore never creates file-kind snapshots. The full
 * relay/marketplace build includes it so file snapshots can be restored.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class PCD_PC_File_Ops {

	/**
	 * Restore a captured file snapshot back to its original path.
	 *
	 * @param string $path    Absolute file path.
	 * @param string $payload File contents to write.
	 * @return array|WP_Error
	 */
	public static function restore_file( string $path, string $payload ) {
		global $wp_filesystem;
		if ( ! $wp_filesystem ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
			WP_Filesystem();
		}
		if ( ! $wp_filesystem || ! $wp_filesystem->put_contents( $path, $payload ) ) {
			return new WP_Error( 'write_failed', 'Could not write file.' );
		}
		return [ 'restored' => 'file', 'path' => $path ];
	}
}
