File: /var/www/shoetique/wp-content/plugins/Ipis-product-Import/index.php
<?php
/*
Plugin Name:IPIS+ Product import for  Woocomerce
Descrpition: IPIS+ Product import for  Woocomerce
Author: Borealis d.o.o.
*/
add_action('admin_menu', 'ipis_plugin_settings');
function ipis_plugin_settings() {
    add_menu_page('IPIS+ Import Settings', 'IPIS+ Import Settings', 'administrator', 'ipis_import_settings', 'ipis_import_display_settings');
}
function ipis_import_display_settings() {
 	 $serverhost = (get_option('ipis_srver_host') != '') ? get_option('ipis_srver_host') : '';
     $username = (get_option('ipis_server_username') != '') ? get_option('ipis_server_username') : '';
     $password = (get_option('ipis_server_password') != '') ? get_option('ipis_server_password') : '';
    $html = '</pre>
<div class="wrap"><form action="options.php" method="post" name="options">
<h2>Uredite postavke servera za IPIS+ import</h2>
' . wp_nonce_field('update-options') . '
<table class="form-table" width="100%" cellpadding="10">
<tbody>
<tr>
<td scope="row" align="left">
 <label>Web adresa servera: </label><input type="text" name="ipis_srver_host" value="' . $serverhost . '" /></td>
</tr>
<td scope="row" align="left">
 <label>Korisničko ime servera: </label><input type="text" name="ipis_server_username" value="' . $username . '" /></td>
</tr>
<td scope="row" align="left">
 <label>lozinka servera: </label><input type="password" name="ipis_server_password" value="' . $password . '" /></td>
</tr>
</tbody>
</table>
 <input type="hidden" name="action" value="update" />
 <input type="hidden" name="page_options" value="ipis_srver_host,ipis_server_username,ipis_server_password" />
 <button id="button1" style="margin:10px;" > Update </button></form></div>
<a id="pullBtn" style="margin-top:25px; margin-left:10px; display:inline-block; font-weight:700;" href="#pull"> POVUCI</a>
<p class="ipis-info-msg"><p>
<pre>
<script>
jQuery(document).ready(function() { // wait for page to finish loading
   jQuery("#pullBtn").click(function () {
	jQuery(".ipis-info-msg").html("Dohvaćam...");
    jQuery.ajax({
        type: "POST",
        url: "'.  get_home_url() . '/wp-admin/admin-ajax.php",
        data: {
            action: "do_import",
        },
        success: function (output) {
			if(output.success){
				jQuery(".ipis-info-msg").html("Uspješno ažurirano.");
			} else {
				jQuery(".ipis-info-msg").html(output.error);
			}
        }
    });
  });
});
</script>
';
    echo $html;
}
add_action( 'wp_ajax_do_import', 'do_import' );
add_action( 'wp_ajax_nopriv_do_import', 'do_import' );
function get_product_by_sku( $sku ) {
  global $wpdb;
  $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
  if ( $product_id ) return wc_get_product( $product_id );
  return null;
}
 function do_import(){
 	include( plugin_dir_path( __FILE__ ) . 'sftp_class.php');
  	$serverhost = (get_option('ipis_srver_host') != '') ? get_option('ipis_srver_host') : '';
    $username = (get_option('ipis_server_username') != '') ? get_option('ipis_server_username') : '';
    $password = (get_option('ipis_server_password') != '') ? get_option('ipis_server_password') : '';
	try{
		/*** Connect ***/
		$sftpObj = new SFTPClient($serverhost);
		$sftpObj->auth_password($username, $password);
		/*** Get contents of latest "Cjenik" file ***/
		$filePath = $sftpObj->get_latest_file();
		$fileContents = file_get_contents($filePath);
		$items = json_decode($fileContents);
		/*** Update products with data from file ***/
		parseImportFile($items->cjenik);
		/*** Move file to "/imported" folder and delete from "/data" folder ***/
		$sftpObj->change_dir($filePath);
		$sftpObj->delete_file($filePath);
		echo wp_send_json(array(
			'success' => true,
			'items' => $items
		));
	} catch (Exception $e){
		echo wp_send_json(array(
			'success' => false,
			'error' => $e->getMessage()
		));
	}
	$sftp->disconnect();
	/*** Always die in functions echoing ajax content ***/
	wp_die();
}
function parseImportFile ($items){
	foreach ($items as $item) {
		$nameArray = explode(";", $item->nazmat);
		$product = get_product_by_sku($nameArray[2]);
		if($product && $product->is_type('variable')){
			$variations = $product->get_available_variations();
			foreach ($variations as $vari) {
				$variId = $vari['variation_id'];
				if(strtolower($nameArray[5]) == strtolower($vari['attributes']['attribute_pa_size'])){
					update_post_meta( $variId, '_manage_stock', "yes" );
					update_post_meta( $variId, '_stock', $item->kolkng );
					//update_post_meta( $variId, '_price', $item->cijmp );
					//update_post_meta( $variId, '_regular_price', $item->cijmp );
				}
			}
		}
	}
}
?>