/**
  * run client ajax actions
  */
 function onClientAjaxActions()
 {
     $action = UniteFunctionsUG::getPostGetVariable("action");
     if ($action != "unitegallery_ajax_action") {
         echo "nothing here";
         exit;
     }
     $clientAction = UniteFunctionsUG::getPostGetVariable("client_action");
     $objItems = new UniteGalleryItems();
     $galleryHtmlID = UniteFunctionsUG::getPostVariable("galleryID");
     $data = UniteFunctionsUG::getPostVariable("data");
     if (empty($data)) {
         $data = array();
     }
     $data["galleryID"] = HelperGalleryUG::getGalleryIDFromHtmlID($galleryHtmlID);
     try {
         switch ($clientAction) {
             case "front_get_cat_items":
                 $html = $objItems->getHtmlFrontFromData($data);
                 $output = array("html" => $html);
                 HelperUG::ajaxResponseData($output);
                 break;
             default:
                 HelperUG::ajaxResponseError("wrong ajax action: <b>{$action}</b> ");
                 break;
         }
     } catch (Exception $e) {
         $message = $e->getMessage();
         $errorMessage = $message;
         if (GlobalsUG::SHOW_TRACE == true) {
             $trace = $e->getTraceAsString();
             $errorMessage = $message . "<pre>" . $trace . "</pre>";
         }
         HelperUG::ajaxResponseError($errorMessage);
     }
     //it's an ajax action, so exit
     HelperUG::ajaxResponseError("No response output on <b> {$action} </b> action. please check with the developer.");
     exit;
 }
 /**
  * 
  * onAjax action handler
  */
 public static function onAjaxAction()
 {
     $actionType = UniteFunctionsUG::getPostGetVariable("action");
     if ($actionType != "unitegallery_ajax_action") {
         return false;
     }
     $gallery = new UniteGalleryGallery();
     $galleries = new UniteGalleryGalleries();
     $categories = new UniteGalleryCategories();
     $items = new UniteGalleryItems();
     $operations = new UGOperations();
     $action = UniteFunctionsUG::getPostGetVariable("client_action");
     $data = UniteFunctionsUG::getPostVariable("data");
     $data = UniteProviderFunctionsUG::normalizeAjaxInputData($data);
     $galleryType = UniteFunctionsUG::getPostVariable("gallery_type");
     $urlGalleriesView = HelperUG::getGalleriesView();
     try {
         switch ($action) {
             case "gallery_actions":
                 $galleryID = UniteFunctionsUG::getVal($data, "galleryID");
                 $galleryAction = UniteFunctionsUG::getVal($data, "gallery_action");
                 $galleryData = UniteFunctionsUG::getVal($data, "gallery_data", array());
                 self::onGalleryAjaxAction($galleryType, $galleryAction, $galleryData, $galleryID);
                 break;
             case "get_thumb_url":
                 $urlImage = UniteFunctionsUG::getVal($data, "urlImage");
                 $imageID = UniteFunctionsUG::getVal($data, "imageID");
                 $urlThumb = $operations->getThumbURLFromImageUrl($urlImage, $imageID);
                 $arrData = array("urlThumb" => $urlThumb);
                 HelperUG::ajaxResponseData($arrData);
                 break;
             case "add_category":
                 $catData = $categories->addFromData();
                 HelperUG::ajaxResponseData($catData);
                 break;
             case "remove_category":
                 $response = $categories->removeFromData($data);
                 HelperUG::ajaxResponseSuccess(__("The category deleted successfully.", UNITEGALLERY_TEXTDOMAIN), $response);
                 break;
             case "update_category":
                 $categories->updateFromData($data);
                 HelperUG::ajaxResponseSuccess(__("Category updated.", UNITEGALLERY_TEXTDOMAIN));
                 break;
             case "update_cat_order":
                 $categories->updateOrderFromData($data);
                 HelperUG::ajaxResponseSuccess(__("Order updated.", UNITEGALLERY_TEXTDOMAIN));
                 break;
             case "add_item":
                 $itemData = $items->addFromData($data);
                 HelperUG::ajaxResponseData($itemData);
                 break;
             case "get_item_data":
                 $response = $items->getItemData($data);
                 HelperUG::ajaxResponseData($response);
                 break;
             case "update_item_data":
                 $response = $items->updateItemData($data);
                 HelperUG::ajaxResponseSuccess(__("Item data updated!", UNITEGALLERY_TEXTDOMAIN), $response);
                 break;
             case "remove_items":
                 $response = $items->removeItemsFromData($data);
                 HelperUG::ajaxResponseSuccess(__("Items Removed", UNITEGALLERY_TEXTDOMAIN), $response);
                 break;
             case "get_cat_items":
                 $responeData = $items->getCatItemsHtmlFromData($data);
                 //update category param if inside gallery
                 $gallery->updateItemsCategoryFromData($data);
                 HelperUG::ajaxResponseData($responeData);
                 break;
             case "update_item_title":
                 $items->updateItemTitleFromData($data);
                 HelperUG::ajaxResponseSuccess(__("Item Title Updated", UNITEGALLERY_TEXTDOMAIN));
                 break;
             case "duplicate_items":
                 $response = $items->duplicateItemsFromData($data);
                 HelperUG::ajaxResponseSuccess(__("Items Duplicated", UNITEGALLERY_TEXTDOMAIN), $response);
                 break;
             case "update_items_order":
                 $items->saveOrderFromData($data);
                 HelperUG::ajaxResponseSuccess(__("Order Saved", UNITEGALLERY_TEXTDOMAIN));
                 break;
             case "copy_move_items":
                 $response = $items->copyMoveItemsFromData($data);
                 HelperUG::ajaxResponseSuccess(__("Done Operation", UNITEGALLERY_TEXTDOMAIN), $response);
                 break;
             case "create_gallery":
                 $galleryID = $galleries->addGaleryFromData($galleryType, $data);
                 $urlView = HelperUG::getGalleryView($galleryID);
                 HelperUG::ajaxResponseSuccessRedirect(__("Gallery Created", UNITEGALLERY_TEXTDOMAIN), $urlView);
                 break;
             case "delete_gallery":
                 $galleries->deleteGalleryFromData($data);
                 HelperUG::ajaxResponseSuccessRedirect(__("Gallery deleted", UNITEGALLERY_TEXTDOMAIN), $urlGalleriesView);
                 break;
             case "update_gallery":
                 $galleries->updateGalleryFromData($data);
                 HelperUG::ajaxResponseSuccess(__("Gallery Updated"));
                 break;
             case "duplicate_gallery":
                 $galleries->duplicateGalleryFromData($data);
                 HelperUG::ajaxResponseSuccessRedirect(__("Gallery duplicated", UNITEGALLERY_TEXTDOMAIN), $urlGalleriesView);
                 break;
             case "update_plugin":
                 if (method_exists("UniteProviderFunctionsUG", "updatePlugin")) {
                     UniteProviderFunctionsUG::updatePlugin();
                 } else {
                     echo "Functionality Don't Exists";
                 }
                 break;
             case "export_gallery_settings":
                 $galleryID = UniteFunctionsUG::getPostGetVariable("galleryid");
                 $galleries->exportGallerySettings($galleryID);
                 break;
             case "import_gallery_settings":
                 $galleryID = UniteFunctionsUG::getPostGetVariable("galleryid");
                 $galleries->importGallerySettingsFromUploadFile($galleryID);
                 break;
             default:
                 HelperUG::ajaxResponseError("wrong ajax action: <b>{$action}</b> ");
                 break;
         }
     } catch (Exception $e) {
         $message = $e->getMessage();
         $errorMessage = $message;
         if (GlobalsUG::SHOW_TRACE == true) {
             $trace = $e->getTraceAsString();
             $errorMessage = $message . "<pre>" . $trace . "</pre>";
         }
         HelperUG::ajaxResponseError($errorMessage);
     }
     //it's an ajax action, so exit
     HelperUG::ajaxResponseError("No response output on <b> {$action} </b> action. please check with the developer.");
     exit;
 }
 /**
  *
  * 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;
     }
 }
 /**
  * on ajax actions
  */
 public static function onAjaxAction()
 {
     $arrFrontActions = array("front_get_cat_items");
     $clientAction = UniteFunctionsUG::getPostVariable("client_action");
     $isFrontAction = in_array($clientAction, $arrFrontActions);
     if ($isFrontAction == true) {
         //front end action
         $operations = new UGOperations();
         $operations->onClientAjaxActions();
     } else {
         //backend actions
         //verify nonce
         $nonce = UniteFunctionsUG::getPostGetVariable("nonce");
         $verified = wp_verify_nonce($nonce, "unitegallery_actions");
         if ($verified == false) {
             _e("Unauthorised access!, if you updated the gallery recently, please do ctrl+f5 - refresh the page and try again.", UNITEGALLERY_TEXTDOMAIN);
             exit;
         }
         parent::onAjaxAction();
     }
 }