<?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 	Admin\Installer\Plugin
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since 		6.1.2
 * @version		6.3.7
 */

namespace WeCodeArt\Admin\Installer;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Admin\Request;
use WeCodeArt\Admin\Installer;
use function WeCodeArt\Functions\get_prop;

/**
 * Plugin Installer
 */
class Plugin extends Installer {
	/**
	 * Sets remote.
	 *
	 * @since 	6.1.2
	 *
	 * @return 	void
	 */
	public function set_api_url(): bool {
		switch ( $this->source ) {
			case 'wordpress':
				$this->remote = 'https://api.wordpress.org/plugins/info/1.0/%s.json';
				break;
			default:
				parent::set_api_url();
			break;
		}

		return true;
	}

	/**
	 * Install the plugin.
	 *
	 * @since 	6.1.2
	 * @version	6.2.9
	 *
	 * @return 	mixed
	 */
	public function install() {
		if( ! $this->package ) {
			return $this->status = null;
		}

		// Include the necessary files
		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
        include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
        require_once ABSPATH . 'wp-admin/includes/file.php';

		// Create an instance of the theme upgrader
		$skin 		= new Skin();
		$upgrader 	= new \Plugin_Upgrader( $skin );
		$result 	= $upgrader->install( $this->package, [ 'overwrite_package' => true ] );

		// Plugin installed
		if( $result ) {
			return $this->status = true;
		}
		
		return $this->status = false;
	}

	/**
	 * Fix GitHub path.
	 *
	 * @since 	6.1.2
	 *
	 * @param 	boolean $true       always true
	 * @param 	mixed   $hook_extra not used
	 * @param 	array   $result     the result of the move
	 *
	 * @return 	array 	$result		the result of the move
	 */
	public function upgrader_post_install( $true, $hook_extra, $result ): array {
		if( $this->source !== 'github' ) {
			return $result;
		}

		global $wp_filesystem;

		$destination = WP_PLUGIN_DIR . '/' . $this->get_dir();

		$wp_filesystem->move( $result['destination'], $destination );
		$result['destination'] = $destination;

		return $result;
	}
}