<?php
/**
 * Plugin Load Hook
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Hooks
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Hooks;

use MOWCPS\Common\Migration\Migration;
use MOWCPS\Common\Interfaces\Hookable_Interface;

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

/**
 * Class Plugin_Load
 *
 * Registers plugin load hook
 */
class Plugin_Load implements Hookable_Interface {
	/**
	 * Initialize the plugin load hooks
	 */
	public function __construct() {
		$this->require_additional_files();
		$this->register_hooks();
	}

	/**
	 * Register hooks
	 *
	 * @return void
	 */
	public function register_hooks() {
		$migration = new Migration();
		add_action( 'plugins_loaded', array( $migration, 'check_for_migration' ) );
	}

	/**
	 * Require additional files
	 * Adding required files only if not included.
	 *
	 * @return void
	 */
	public function require_additional_files() {
		$load_files = array(
			ABSPATH . 'wp-admin/includes/media.php',
			ABSPATH . 'wp-admin/includes/file.php',
			ABSPATH . 'wp-admin/includes/image.php',
		);

		$included_files = array_flip( array_map( 'wp_normalize_path', get_included_files() ) );

		foreach ( $load_files as $file ) {
			$file = wp_normalize_path( $file );
			if ( file_exists( $file ) && ! isset( $included_files[ $file ] ) ) {
				require_once $file;
			}
		}
	}
}
