/**
	 * 
	 * @param string $file
	 * @param string $filename
	 * @return int
	 */
	protected function add_attachment($file, $filename)
	{
		$dest_file = trailingslashit(WC_Email_Att_Func::get_full_upload_path($this->options_new['upload_folder'])).$filename;
		
		if(file_exists($dest_file))
		{
			$unique = uniqid('_');
			$path_parts = pathinfo($filename);
			$filename = $path_parts['filename'].$unique.'.'.$path_parts['extension'];
			$dest_file = trailingslashit(WC_Email_Att_Func::get_full_upload_path($this->options_new['upload_folder'])).$filename;
		}
		
		if(!copy($file, $dest_file))
		{
			return 0;
		}
		
		$parent_post_id = WC_Email_Att::instance()->email_post->ID;

		// Check the type of tile. We'll use this as the 'post_mime_type'.
		$filetype = wp_check_filetype( basename( $dest_file ), null );
		
		// Get the path to the upload directory.
		$wp_upload_dir = wp_upload_dir();
		
		// Prepare an array of post data for the attachment.
		$attachment = array(
				'guid'           => $wp_upload_dir['baseurl'].trailingslashit($this->options_new['upload_folder']).$filename, 
				'post_mime_type' => $filetype['type'],
				'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $dest_file ) ),
				'post_content'   => '',
				'post_status'    => 'inherit'
			);
		
		// Insert the attachment.
		$attach_id = wp_insert_attachment( $attachment, $dest_file, $parent_post_id );

		// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
		require_once( ABSPATH . 'wp-admin/includes/image.php' );

		// Generate the metadata for the attachment, and update the database record.
		$attach_data = wp_generate_attachment_metadata( $attach_id, $dest_file );
		wp_update_attachment_metadata( $attach_id, $attach_data );
		
		return $attach_id;
	}
	/**
	 * 
	 * @param array $update_option
	 */
	protected function show_admin_update_needed( array &$update_option )
	{
		$new_folder = WC_Email_Att_Func::get_full_upload_path( WC_Email_Att::instance()->options['upload_folder'] );
		
		$tip_move = __( 'All options and settings are copied. All the existing files are moved to be shown as E-Mail attachment files in the WP media manager and the source files and folder is deleted.', WC_Email_Att::TEXT_DOMAIN );
		$tip_copy = __( 'All options and settings are copied. All the existing files are copied to be shown as E-Mail attachment files in the WP media manager and leave the source files and folder. You can delete this folder manually.', WC_Email_Att::TEXT_DOMAIN );
		$tip_general_move = __( 'Only General settings are copied - You have to set all the the E-Mail specific settings and the file attachments manually again. All the existing files are moved to be shown as E-Mail attachment files in the WP media manager and the source files and folder is deleted.', WC_Email_Att::TEXT_DOMAIN );
		$tip_general_delete = __( 'Only General settings are copied - You have to set all the the E-Mail specific settings and the file attachments manually again. The old source files and folder are deleted and no longer available.', WC_Email_Att::TEXT_DOMAIN );
		$tip_ignore = __( 'Ignore all old settings, delete all old files and folder and start with the default settings.', WC_Email_Att::TEXT_DOMAIN );
		$tip_upload = __( 'Change to a new upload folder first and update data afterwards.', WC_Email_Att::TEXT_DOMAIN );
		$tip_execute = __( 'Start executing the selected task.', WC_Email_Att::TEXT_DOMAIN );
		
		echo '<div id="message" class="updated woocommerce-message wc-connect">';
		echo		__( '<strong>WooCommerce Email Attachment Data Update Required:</strong><br/><br/> The new version needs a new data and directory structure. The upload path will be changed:', WC_Email_Att::TEXT_DOMAIN ).'<br/><br/>';
		echo		__( '<strong>From:</strong>', WC_Email_Att::TEXT_DOMAIN ) . '  '.$update_option['old_upload_folder'].'<br/>';
		echo		__( '<strong>To:</strong>', WC_Email_Att::TEXT_DOMAIN ) . '  '.$new_folder.'<br/><br/>';
		echo		__( 'It is recommended to make a backup of your database and WordPress installation before proceeding.', WC_Email_Att::TEXT_DOMAIN ) . '<br/>';
		echo		__( 'You have the following possibilities with the existing old data and files:', WC_Email_Att::TEXT_DOMAIN );
		echo	'<div class="submit wc_email_att_submit">';
		echo		'<div class="wc_email_att_select_div">';
		echo			'<select id="update_woocom_email_att_select" name="update_woocom_email_att_select" size="1" class="wc_email_att_select_upgrade">';
		echo				'<option selected="selected" value="'.$this->update_actions['move_210'] . '">' . __( 'Move all files and option settings', WC_Email_Att::TEXT_DOMAIN ) . '</option>';
		echo				'<option value="' . $this->update_actions['copy_210'].'">' . __( 'Copy all files and option settings', WC_Email_Att::TEXT_DOMAIN ) . '</option>';
		echo				'<option value="' . $this->update_actions['general_move_210'] . '">' . __( 'Copy General Settings and move files', WC_Email_Att::TEXT_DOMAIN ) . '</option>';
		echo				'<option value="' . $this->update_actions['general_delete_210'] . '">' . __( 'Copy General Settings and delete files', WC_Email_Att::TEXT_DOMAIN ) . '</option>';
		echo				'<option value="' . $this->update_actions['ignore_210'].'">' . __( 'Ignore old settings and delete files', WC_Email_Att::TEXT_DOMAIN ) . '</option>';
		echo				'<option value="' . $this->update_actions['upload_folder'].'">' . __( 'Set new upload folder', WC_Email_Att::TEXT_DOMAIN ) . '</option>';
		echo			'</select>';
		echo		'</div>';
		echo		'<div href="'.add_query_arg( $this->update_actions['do_update'], 'true', admin_url('admin.php?page=wc-settings')).'" class="wc_email_att_update_now button-secondary tips" data-tip="' . $tip_execute . '">'; 
		echo			__( 'Execute Selection', WC_Email_Att::TEXT_DOMAIN );
		echo		'</div>';
		echo		'<div class="wc_email_att_select_div_tip">';
		echo			'<div class="wc_email_att_select_tips" tip_src="' . $this->update_actions['move_210'] . '">' . $tip_move . '</div>';
		echo			'<div class="wc_email_att_select_tips" tip_src="' . $this->update_actions['copy_210'] . '">' . $tip_copy . '</div>';
		echo			'<div class="wc_email_att_select_tips" tip_src="' . $this->update_actions['general_move_210'] . '">' . $tip_general_move . '</div>';
		echo			'<div class="wc_email_att_select_tips" tip_src="' . $this->update_actions['general_delete_210'] . '">' . $tip_general_delete . '</div>';
		echo			'<div class="wc_email_att_select_tips" tip_src="' . $this->update_actions['ignore_210'] . '">' . $tip_ignore . '</div>';
		echo			'<div class="wc_email_att_select_tips" tip_src="' . $this->update_actions['upload_folder'] . '">' . $tip_upload . '</div>';
		echo		'</div>';
		echo	'</div>';
		echo '</div>';
		
		$message = 'var answer = confirm(\'';
		$message .= __( 'It is recommended that you backup your database and files before proceeding. Are you sure you wish to run the updater now? This may take some time depending on the amount of files to copy.', WC_Email_Att::TEXT_DOMAIN );
		$message .= '\');';
				
		echo '<script type="text/javascript">';
		echo '	jQuery(".wc_email_att_update_now").on("click", function(){';
		echo		$message;
		echo '		if(!answer) return;';
		echo '		var sel = jQuery("#update_woocom_email_att_select").val();';
		echo '		var href = jQuery(".wc_email_att_update_now").attr("href");';
		echo '		window.location.href = href+"&update_woocom_email_att_select="+sel;';
		echo '		return;';
		echo '	});';
		echo '</script>';
	}
	/**
	 * Saves the options in own option entry
	 */
	public function save_settings_page_options()
	{
		$this->options['del_on_deactivate'] = (WC_Email_Att::$show_activation) ? isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'del_on_deactivate' ] ) : false;
		$this->options['del_on_uninstall'] = (WC_Email_Att::$show_uninstall) ? isset( $_REQUEST[self::PREFIX_JS_NAMES . 'del_on_uninstall'] ) : true;
		
		$this->options['notification_headline'] = (isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_headline']) && ! empty( $_REQUEST[self::PREFIX_JS_NAMES . 'notification_headline' ] ) ) ? $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_headline' ] : '';
		$this->options['notification_headline'] =  trim( stripslashes( $this->options['notification_headline'] ) );
		
		$this->options['notification_text'] = (isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_text']) && ! empty( $_REQUEST[self::PREFIX_JS_NAMES . 'notification_text'])) ? $_REQUEST[self::PREFIX_JS_NAMES . 'notification_text' ] : '';
		$this->options['notification_text'] = trim( stripslashes( $this->options['notification_text'] ) );
	
		$this->options['show_notes_always'] = isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'show_notes_always' ] );
		
		$oldpath = $newpath = strtolower( $this->options['upload_folder'] );
		if( isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'upload_folder' ] ) )
		{
			$newpath = strtolower( trim( strtolower( stripslashes( $_REQUEST[ self::PREFIX_JS_NAMES . 'upload_folder'] ) ) ) );
			$newpath = str_replace( '\\', '/', $newpath );
			$newpath = (strlen( $newpath ) == 0 ) ? $oldpath : untrailingslashit( $newpath );
			$newpath = '/' . ltrim( $newpath, '/' );
			
			if( $oldpath != $newpath )
			{
				if( WC_Email_Att_Func::create_folder( $newpath ) )
				{
					$this->options['upload_folder'] = $newpath;
				}
				WC_Email_Att_Func::remove_empty_folder( $oldpath, WC_Email_Att::$skip_files );
			}
		}
		foreach ( $this->emailsubjects as $email_key => $emailclass ) 
		{
			$opt_key = $this->inputfields_param[ $email_key ]['id'];
			$this->options[ $opt_key ] = $this->get_opt_emailaddress( self::PREFIX_JS_NAMES . 'head_', $email_key );
			
			$opt_key = $this->inputfields_param[ $email_key ]['att_id'];
			$this->options[ $opt_key ] = $this->get_opt_attachment_files( self::PREFIX_JS_NAMES . 'att_', $email_key );
		}
		
		$this->options = apply_filters( 'wc_eatt_save_settings_options', $this->options, $this->emailsubjects, $this->inputfields_param );
		
		update_option( WC_Email_Att::OPTIONNAME, $this->options );
		
		WC_Email_Att::instance()->options = $this->options;
		
		WC_Email_Att::$admin_message = 'Options saved';
	}
<?php
if ( ! defined( 'ABSPATH' ) ) {  exit;  }    // Exit if accessed directly

global $wc_email_att_skip_files, $wc_email_att_htaccess, $wc_email_att_plugin_base_name, $wc_email_att_are_activation_hooks;

$wc_email_att_plugin_path = str_replace( basename( __FILE__ ), '', __FILE__ );

/**
 * load woocommerce_email_attachments main class, which loads needed classes with autoload when needed
 **/
require_once $wc_email_att_plugin_path . 'classes/class-wc-email-att.php';

	// create object
WC_Email_Att::instance();

WC_Email_Att::$show_activation = true;			//	true to show deactivation and uninstall checkbox
WC_Email_Att::$show_uninstall = true;
WC_Email_Att::$plugin_path = $wc_email_att_plugin_path;
WC_Email_Att::$plugin_url = trailingslashit( plugins_url( '', plugin_basename( __FILE__ ) ) );		//	also set in init hook to allow other plugins to change it in a filter hook
WC_Email_Att::$plugin_base_name = $wc_email_att_plugin_base_name;
WC_Email_Att::$skip_files = $wc_email_att_skip_files;

WC_Email_Att_Func::$htaccess = $wc_email_att_htaccess;

WC_Email_Att::instance()->init();
	

	//	allow plugins to load, which depend on this plugin being already loaded
do_action( 'wc_email_att_plugin_loaded' );

   /**
    * Checks, if an upgrade is necessary and returns the change array for upgrade display and doing the upgrade
    * 
    * Currently only upgrade from <= 2.1.0 is necessary
    * 
    * @return array
    */
   private function &check_for_update()
   {
		$change = array();
	   
		/**
		 * Optionname changed with 3.0.0.
		 */
		$option_210 = get_option( self::OPTIONNAME_210, array() );
		if( empty( $option_210 ) ) 
		{
			return $change;
		}
		
		/**
		 * Check for previous version -> Folder MUST NOT BE the same in case of upgrade to allow scan of files to make attachments
		 */
		if( isset( $option_210['upload_folder'] ) )
		{
			$new_path = str_replace( '\\', '/', WC_Email_Att_Func::get_full_upload_path( $this->options['upload_folder'] ) );
			$old_path = str_replace( '\\', '/', $option_210['upload_folder'] );

			if( strtolower( $new_path ) == strtolower( $old_path ) )
			{
				$this->options['upload_folder'] .= '_new';
				update_option( WC_Email_Att::OPTIONNAME, $this->options );
			}
			
			$change = array(
					'status' =>	'update_needed',
					'prev_version_found' => '210',
					'old_upload_folder' => $old_path
				);
		}
		return $change;
   }