<?php
/**
 * Code Manager import code
 *
 * @package Code_Manager
 */

namespace Code_Manager {

	/**
	 * Class Code_Manager_Import
	 *
	 * Add import feature to plugin (reached from list table).
	 *
	 * @author  Peter Schulz
	 * @since   1.0.0
	 */
	class Code_Manager_Import {

		/**
		 * Start import
		 *
		 * @return void
		 */
		public static function import() {
			if ( isset( $_FILES['filename'], $_FILES['filename']['error'], $_FILES['filename']['tmp_name'] ) &&
					UPLOAD_ERR_OK === $_FILES['filename']['error']
					&& is_uploaded_file( sanitize_text_field( wp_unslash( $_FILES['filename']['tmp_name'] ) ) )
			) {
				// Get file content.
				$import_file = new Code_Manager_Import_File(
					sanitize_text_field( wp_unslash( $_FILES['filename']['tmp_name'] ) )
				);
				$import_file->import();
			} else {
				// File upload failed.
				$msg = new Message_Box(
					array(
						'message_text'           => __( 'File upload failed', 'code-manager' ),
						'message_type'           => 'error',
						'message_is_dismissible' => false,
					)
				);
				$msg->box();
			}
		}

	}

}
