<?php
/**
 * WeCodeArt Framework.
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package 	WeCodeArt Framework
 * @subpackage 	Support\FileSystem
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since 		5.0.0
 * @version		6.3.7
 */

namespace WeCodeArt\Support;

defined( 'ABSPATH' ) || exit;

use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use WeCodeArt\Config\Interfaces\Integration;
use WeCodeArt\Config\Traits\{ Singleton, No_Conditionals };

/**
 * Manages the Theme Uploads.
 */
class Files implements Integration {

	use Singleton;
	use No_Conditionals;

	/**
	 * Current folder
	 *
	 * @since 	5.0.0
	 * @var 	string
	 */
	public	$folder = '';

	/**
	 * Send to contructor.
	 */
	public function init() {
		$this->set_folder();
	}

	/**
	 * Register hooks
	 */
	public function register_hooks() {}

	/**
	 * Function to set folder in WordPress Filesystem.
	 *
	 * @since   5.0.0
	 *
	 * @return  bool
	 */
	public function set_folder( $folder = '' ) {
		$theme_dir = wecodeart_config( 'paths' )['directory'];

		if ( ! is_writable( $theme_dir ) ) {
			return false;
		}

		$this->folder = implode( DIRECTORY_SEPARATOR, array_filter( [ $theme_dir, 'assets', $folder ] ) );

		wp_mkdir_p( wp_normalize_path( $this->folder ) );

		return true;
	}

	/**
	 * Function to delete folder from WordPress Filesystem.
	 *
	 * @since   5.0.6
	 * @version 6.1.2
	 *
	 * @return  boolean
	 */
	public function clean_folder( $path = '' ) {
		$folder = wp_normalize_path( implode( DIRECTORY_SEPARATOR, array_filter( [ $this->folder, $path ] ) ) );

		if ( is_dir( $folder ) ) {
            $objects = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator( $folder, RecursiveDirectoryIterator::SKIP_DOTS ),
                RecursiveIteratorIterator::CHILD_FIRST
            );

            foreach ( $objects as $object ) {
                if ( $object->isDir() ) rmdir( $object->getPathname() );
         		else unlink( $object->getPathname() );
            }

            return rmdir( $folder );
        }

        return true;
	}
	
	/**
	 * Function to create file from WordPress Filesystem.
	 *
	 * @since   5.0.0
	 *
	 * @return  bool
	 */
	public function create_file( $filename, $content ) {
		// Delete old file
		$this->delete_file( $filename );

		// Create new file
		return file_put_contents( $this->get_file_url( $filename ), $content );
	}

	/**
	 * Function to delete file from WordPress Filesystem.
	 *
	 * @since   5.0.0
	 *
	 * @return  bool
	 */
	public function delete_file( $filename ) {
		// If no specific file, bail
		if ( $this->has_file( $filename ) ) {
			return wp_delete_file( $this->get_file_url( $filename ) );
		}

		return null;
	}

	/**
	 * Check if we have a file
	 *
	 * @since   5.0.0
	 *
	 * @return  bool
	 */
	public function has_file( $file ): bool {
		return is_file( $this->get_file_url( $file ) );
	}

	/**
	 * Get File url.
	 *
	 * @return  string File url.
	 */
	public function get_file_url( $filename, $link = false ): string {
		$file = implode( DIRECTORY_SEPARATOR, array_filter( [ $this->folder, $filename ] ) );
		
		if( $link ) {
			$paths = wecodeart_config( 'paths' );

			$file = str_replace( $paths['directory'], $paths['uri'], $file );
		}

		return wp_normalize_path( $file );
	}
}