/**
  *
  * Update Plugin
  */
 public static function updatePlugin()
 {
     try {
         //verify nonce:
         $nonce = UniteFunctionsUG::getPostVariable("nonce");
         $isVerified = wp_verify_nonce($nonce, "unitegallery_actions");
         if ($isVerified == false) {
             UniteFunctionsUG::throwError("Security error");
         }
         $linkBack = HelperUG::getGalleriesView();
         $htmlLinkBack = UniteFunctionsUG::getHtmlLink($linkBack, "Go Back");
         //check if zip exists
         $zip = new UniteZipUG();
         if (function_exists("unzip_file") == false) {
             if (UniteZipUG::isZipExists() == false) {
                 UniteFunctionsUG::throwError("The ZipArchive php extension not exists, can't extract the update file. Please turn it on in php ini.");
             }
         }
         dmp("Update in progress...");
         $arrFiles = UniteFunctionsUG::getVal($_FILES, "update_file");
         if (empty($arrFiles)) {
             UniteFunctionsUG::throwError("Update file don't found.");
         }
         $filename = UniteFunctionsUG::getVal($arrFiles, "name");
         if (empty($filename)) {
             UniteFunctionsIG::throwError("Update filename not found.");
         }
         $fileType = UniteFunctionsUG::getVal($arrFiles, "type");
         $fileType = strtolower($fileType);
         $arrMimeTypes = array();
         $arrMimeTypes[] = "application/zip";
         $arrMimeTypes[] = "application/x-zip";
         $arrMimeTypes[] = "application/x-zip-compressed";
         $arrMimeTypes[] = "application/octet-stream";
         $arrMimeTypes[] = "application/x-compress";
         $arrMimeTypes[] = "application/x-compressed";
         $arrMimeTypes[] = "multipart/x-zip";
         if (in_array($fileType, $arrMimeTypes) == false) {
             UniteFunctionsUG::throwError("The file uploaded is not zip.");
         }
         $filepathTemp = UniteFunctionsUG::getVal($arrFiles, "tmp_name");
         if (file_exists($filepathTemp) == false) {
             UniteFunctionsUG::throwError("Can't find the uploaded file.");
         }
         //crate temp folder
         $pathTemp = GlobalsUG::$pathPlugin . "temp/";
         UniteFunctionsUG::checkCreateDir($pathTemp);
         //create the update folder
         $pathUpdate = $pathTemp . "update_extract/";
         UniteFunctionsUG::checkCreateDir($pathUpdate);
         if (!is_dir($pathUpdate)) {
             UniteFunctionsUG::throwError("Could not create temp extract path");
         }
         //remove all files in the update folder
         $arrNotDeleted = UniteFunctionsUG::deleteDir($pathUpdate, false);
         if (!empty($arrNotDeleted)) {
             $strNotDeleted = print_r($arrNotDeleted, true);
             UniteFunctionsUG::throwError("Could not delete those files from the update folder: {$strNotDeleted}");
         }
         //copy the zip file.
         $filepathZip = $pathUpdate . $filename;
         $success = move_uploaded_file($filepathTemp, $filepathZip);
         if ($success == false) {
             UniteFunctionsUG::throwError("Can't move the uploaded file here: " . $filepathZip . ".");
         }
         //extract files:
         if (function_exists("unzip_file") == true) {
             WP_Filesystem();
             $response = unzip_file($filepathZip, $pathUpdate);
         } else {
             $zip->extract($filepathZip, $pathUpdate);
         }
         //check for internal zip in case that cocecanyon original zip was uploaded
         self::updatePlugin_checkUnpackInnerZip($pathUpdate, $filename);
         //get extracted folder
         $arrFolders = UniteFunctionsUG::getDirList($pathUpdate);
         if (empty($arrFolders)) {
             UniteFunctionsUG::throwError("The update folder is not extracted");
         }
         //get product folder
         $productFolder = null;
         if (count($arrFolders) == 1) {
             $productFolder = $arrFolders[0];
         } else {
             foreach ($arrFolders as $folder) {
                 if ($folder != "documentation") {
                     $productFolder = $folder;
                 }
             }
         }
         if (empty($productFolder)) {
             UniteFunctionsUG::throwError("Wrong product folder.");
         }
         $pathUpdateProduct = $pathUpdate . $productFolder . "/";
         //check some file in folder to validate it's the real one:
         $checkFilepath = $pathUpdateProduct . "unitegallery.php";
         if (file_exists($checkFilepath) == false) {
             UniteFunctionsUG::throwError("Wrong update extracted folder. The file: " . $checkFilepath . " not found.");
         }
         //copy the plugin without the captions file.
         $pathOriginalPlugin = GlobalsUG::$pathPlugin;
         $arrBlackList = array();
         UniteFunctionsUG::copyDir($pathUpdateProduct, $pathOriginalPlugin, "", $arrBlackList);
         //delete the update
         UniteFunctionsUG::deleteDir($pathUpdate);
         //change folder to original (if updated to full version)
         if ($productFolder == "unitegallery") {
             $pathRename = str_replace("unite-gallery-lite", "unitegallery", $pathOriginalPlugin);
             if ($pathRename != $pathOriginalPlugin) {
                 $success = @rename($pathOriginalPlugin, $pathRename);
                 if ($success == true) {
                     //activate plugin
                     $pluginFile = $pathRename . "unitegallery.php";
                     if (file_exists($pluginFile)) {
                         $activateSuccess = UniteFunctionsWPUG::activatePlugin($pluginFile);
                         if ($activateSuccess == false) {
                             $linkBack = admin_url("plugins.php");
                         }
                         //link to plugin activate
                     }
                 }
             }
         }
         dmp("Updated Successfully, redirecting...");
         echo "<script>location.href='{$linkBack}'</script>";
     } catch (Exception $e) {
         //remove all files in the update folder
         UniteFunctionsUG::deleteDir($pathUpdate);
         $message = $e->getMessage();
         $message .= " <br> Please update the plugin manually via the ftp";
         echo "<div style='color:#B80A0A;font-size:18px;'><b>Update Error: </b> {$message}</div><br>";
         echo $htmlLinkBack;
         exit;
     }
 }