object-library.class.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2016 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderObjectLibrary {
  9. private $library_list = 'library.php';
  10. private $library_download = 'download.php';
  11. private $object_thumb_path = '/revslider/objects/thumbs/';
  12. private $object_orig_path = '/revslider/objects/';
  13. private $curl_check = null;
  14. const LIBRARY_VERSION = '1.0.0';
  15. /**
  16. * get list of objects
  17. * @since: 5.3.0
  18. */
  19. public function _get_list($force = false){
  20. global $wp_version, $rslb;
  21. $last_check = get_option('revslider-library-check');
  22. if($last_check == false){ //first time called
  23. $last_check = 1296001;
  24. update_option('revslider-library-check', time());
  25. }
  26. // Get latest object list
  27. if(time() - $last_check > 1296000 || $force == true){ //30 days
  28. update_option('revslider-library-check', time());
  29. $code = get_option('revslider-code', '');
  30. $library_version = self::LIBRARY_VERSION;
  31. $validated = get_option('revslider-valid', 'false');
  32. if($validated == 'false'){
  33. $code = '';
  34. }
  35. $rattr = array(
  36. 'code' => urlencode($code),
  37. 'library_version' => urlencode($library_version),
  38. 'version' => urlencode(RevSliderGlobals::SLIDER_REVISION),
  39. 'product' => urlencode(RS_PLUGIN_SLUG)
  40. );
  41. $done = false;
  42. $count = 0;
  43. do{
  44. $url = $rslb->get_url('library');
  45. $request = wp_remote_post($url.'/'.$this->library_list, array(
  46. 'user-agent' => 'WordPress/'.$wp_version.'; '.get_bloginfo('url'),
  47. 'body' => $rattr
  48. ));
  49. $response_code = wp_remote_retrieve_response_code( $request );
  50. if($response_code == 200){
  51. $done = true;
  52. }else{
  53. $rslb->move_server_list();
  54. }
  55. $count++;
  56. }while($done == false && $count < 5);
  57. if(!is_wp_error($request)) {
  58. if($response = maybe_unserialize($request['body'])) {
  59. $library = json_decode($response, true);
  60. if(is_array($library)) {
  61. update_option('rs-library', $library, false);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. public function _get_object_data($object_handle){
  68. $data = array('thumb' => $object_handle, 'orig' => $object_handle);
  69. $upload_dir = wp_upload_dir(); // Set upload folder
  70. $file = $upload_dir['basedir'] . $this->object_thumb_path . $object_handle;
  71. if(file_exists($file)){
  72. $url_file = $upload_dir['baseurl'] . $this->object_thumb_path . $object_handle;
  73. $data['thumb'] = $url_file;
  74. }
  75. $file = $upload_dir['basedir'] . $this->object_orig_path . $object_handle;
  76. if(file_exists($file)){
  77. $url_file = $upload_dir['baseurl'] . $this->object_orig_path . $object_handle;
  78. $data['orig'] = $url_file;
  79. //check
  80. }
  81. return $data;
  82. }
  83. /**
  84. * check if given URL is an object from object library
  85. * @since: 5.3.0
  86. */
  87. public function _is_object($url){
  88. $is_object = false;
  89. $upload_dir = wp_upload_dir(); // Set upload folder
  90. //$upload_directory = $upload_dir['basedir'] . $this->object_orig_path;
  91. $upload_url = $upload_dir['baseurl'] . $this->object_orig_path;
  92. $file_name = explode('/', $url);
  93. $file_name = $file_name[count($file_name) - 1];
  94. if(strpos($url, $upload_url) !== false){
  95. //check now if handle is inside of the array of objects
  96. $obj = $this->load_objects();
  97. $online = $obj['online']['objects'];
  98. foreach($online as $object){
  99. if($object['handle'] == $file_name){
  100. $is_object = true;
  101. break;
  102. }
  103. }
  104. }
  105. return $is_object;
  106. }
  107. /**
  108. * check if given URL is existing in the object library
  109. * @since: 5.3.0
  110. */
  111. public function _does_exist($url){
  112. $does_exist = false;
  113. $upload_dir = wp_upload_dir(); // Set upload folder
  114. $upload_directory = $upload_dir['basedir'] . $this->object_orig_path;
  115. $upload_url = $upload_dir['baseurl'] . $this->object_orig_path;
  116. $url = str_replace($upload_url, '', $url);
  117. if(file_exists($upload_directory.$url)){
  118. $does_exist = true;
  119. }
  120. return $does_exist;
  121. }
  122. /**
  123. * check if certain object needs to be redownloaded
  124. * @since: 5.3.0
  125. */
  126. public function _check_object_exist($object_url){
  127. //first check if it is an object
  128. $is_obj = $this->_is_object($object_url);
  129. //then check if it is existing
  130. if($is_obj){
  131. if($this->_does_exist($object_url)){
  132. //all cool
  133. }else{ //if not, redownload if allowed
  134. //need to redownload
  135. $file_name_with_ending = explode("/", $object_url);
  136. $file_name_with_ending = $file_name_with_ending[count($file_name_with_ending) - 1];
  137. $this->_get_object_thumb($file_name_with_ending, 'orig');
  138. }
  139. }
  140. }
  141. /**
  142. * get certain objects thumbnail, download if needed and if not, simply return path
  143. * @since: 5.3.0
  144. */
  145. public function _get_object_thumb($object_handle, $type){
  146. global $wp_version, $rslb;
  147. $error = '';
  148. $path = ($type == 'thumb') ? $this->object_thumb_path : $path = $this->object_orig_path;
  149. $download = false;
  150. $upload_dir = wp_upload_dir(); // Set upload folder
  151. $file = $upload_dir['basedir'] . $path . $object_handle;
  152. $url_file = $upload_dir['baseurl'] . $path . $object_handle;
  153. //check if object thumb is already downloaded
  154. $download = (!file_exists($file)) ? true : false;
  155. //check if new version of object thumb is available
  156. // Check folder permission and define file location
  157. if($download && wp_mkdir_p( $upload_dir['basedir'].$path ) ) {
  158. $curl = ($this->check_curl_connection()) ? new WP_Http_Curl() : false;
  159. $file = $upload_dir['basedir'] . $path . $object_handle;
  160. if(!file_exists($file) || isset($temp['push_image'])){
  161. $image_data = false;
  162. if($curl !== false){
  163. $validated = get_option('revslider-valid', 'false');
  164. if($validated == 'false' && $type != 'thumb'){
  165. $error = __('Plugin not activated', 'revslider');
  166. }else{
  167. $code = ($validated == 'false') ? '' : get_option('revslider-code', '');
  168. $done = false;
  169. $count = 0;
  170. do{
  171. $url = $rslb->get_url('library');
  172. $image_data = wp_remote_post($url.'/'.$this->library_download, array(
  173. 'user-agent' => 'WordPress/'.$wp_version.'; '.get_bloginfo('url'),
  174. 'body' => array(
  175. 'code' => urlencode($code),
  176. 'library_version' => urlencode(self::LIBRARY_VERSION),
  177. 'version' => urlencode(RevSliderGlobals::SLIDER_REVISION),
  178. 'handle' => urlencode($object_handle),
  179. 'download' => urlencode($type),
  180. 'product' => urlencode(RS_PLUGIN_SLUG)
  181. ),
  182. 'timeout' => 45
  183. ));
  184. $response_code = wp_remote_retrieve_response_code( $image_data );
  185. if($response_code == 200){
  186. $done = true;
  187. }else{
  188. $rslb->move_server_list();
  189. }
  190. $count++;
  191. }while($done == false && $count < 5);
  192. if(!is_wp_error($image_data) && isset($image_data['body']) && isset($image_data['response']) && isset($image_data['response']['code']) && $image_data['response']['code'] == '200'){
  193. $image_data = $image_data['body'];
  194. //check body for errors in here
  195. $check = json_decode($image_data, true);
  196. if(!empty($check)){
  197. if(isset($check['error'])){
  198. $image_data = false;
  199. $error = $check['error'];
  200. }
  201. }elseif(trim($image_data) == ''){
  202. $error = __('No data received', 'revslider');
  203. }
  204. }else{
  205. $image_data = false;
  206. $error = __('Error downloading object', 'revslider');
  207. }
  208. }
  209. }else{
  210. //cant download file
  211. }
  212. if($image_data !== false){
  213. @mkdir(dirname($file));
  214. @file_put_contents( $file, $image_data );
  215. $this->create_image_dimensions($object_handle);
  216. }else{//could not connect to server
  217. $error = __('Error downloading object', 'revslider');
  218. }
  219. }else{//use default image
  220. $error = __('Error downloading object', 'revslider');
  221. }
  222. }else{//use default images
  223. $error = __('Error downloading object', 'revslider');
  224. }
  225. if($error !== ''){
  226. return array('error' => true);
  227. }
  228. $width = false;
  229. $height = false;
  230. //get dimensions of image
  231. $imgsize = getimagesize( $file );
  232. if($imgsize !== false){
  233. $width = $imgsize['0'];
  234. $height = $imgsize['1'];
  235. }
  236. return array('error' => false, 'url' => $url_file, 'width' => $width, 'height' => $height);
  237. }
  238. /**
  239. * import object to media library
  240. * @since: 5.3.0
  241. */
  242. public function _import_object($file_path){
  243. $curl = ($this->check_curl_connection()) ? new WP_Http_Curl() : false;
  244. $upload_dir = wp_upload_dir(); // Set upload folder
  245. $path = $this->object_orig_path;
  246. $object_handle = basename($file_path);
  247. $file = $upload_dir['basedir'] . $path . $object_handle;
  248. $url_file = $upload_dir['baseurl'] . $path . $object_handle;
  249. $image_handle = @fopen($file_path, "r");
  250. if($image_handle != false){
  251. $image_data = stream_get_contents($image_handle);
  252. if($image_data !== false){
  253. @mkdir(dirname($file));
  254. @file_put_contents( $file, $image_data );
  255. $this->create_image_dimensions($object_handle);
  256. return array('path' => $url_file);
  257. }
  258. }
  259. return false;
  260. }
  261. public function write_markup(){
  262. ?>
  263. <!-- THE OBJECT LIBRARY DIALOG WINDOW -->
  264. <div id="dialog_addobj" class="dialog-addobj" title="<?php _e("Add Object Layer",'revslider'); ?>" style="display:none">
  265. <div class="addobj-dialog-inner">
  266. <div id="addobj-list-of-items">
  267. <div id="addobj-dialog-header">
  268. <div class="object_library_search_wrapper">
  269. <input type="text" id="obj_library_search" placeholder="<?php _e("Search for Objects...",'revslider'); ?>" /><span id="obj_library_search_trigger"><i class="eg-icon-search"></i></span>
  270. </div>
  271. <div id="object_library_type_list_new">
  272. <span id="obj_lib_main_cat_filt_all" data-value="all" class="obj_library_cats_filter"><?php _e("ALL",'revslider'); ?></span>
  273. <span id="obj_lib_main_cat_filt_allimages" data-value="allimages" class="obj_library_cats_filter all_img_cat"><?php _e("ALL IMAGES",'revslider'); ?></span>
  274. <span id="obj_lib_main_cat_filt_svg" data-value="svg" class="obj_library_cats_filter svg_cat"><?php _e("SVG",'revslider'); ?></span>
  275. <span id="obj_lib_main_cat_filt_icon" data-value="icon" class="obj_library_cats_filter fonticon_cat"><?php _e("ICON",'revslider'); ?></span>
  276. <span id="obj_lib_main_cat_filt_image" data-value="image" class="obj_library_cats_filter png_cat"><?php _e("PNG",'revslider'); ?></span>
  277. <span id="obj_lib_main_cat_filt_bgimage" data-value="bgimage" class="obj_library_cats_filter jpg_cat"><?php _e("JPG",'revslider'); ?></span>
  278. </div>
  279. <div id="up-lic-ob-lib">
  280. <div id="licence_obect_library"><i class="fa-icon-copyright"></i><?php _e("License Info",'revslider'); ?></div>
  281. <div id="update_obect_library"><i class="eg-icon-arrows-ccw"></i><?php _e("Update Object Library",'revslider'); ?></div>
  282. </div>
  283. <div id="object-tag-list" class="object-tag-list"><span id="obj_library_cats_favorite" class="obj_library_cats" data-tag="favorite" data-image="true" data-bgimages="true" data-svg="true"><i class="fa-icon-star-o" style="margin-right: 5px;"></i><?php _e("Favorite",'revslider'); ?></span><span id="obj_library_cats_allico" class="obj_library_cats" data-tag="allicon" data-icon="true" data-bgimages="false" data-svg="false"><i class="fa-icon-folder-o" style="margin-right: 5px;"></i><?php _e("All Icons",'revslider'); ?></span><span id="obj_library_cats_allpng" class="obj_library_cats" data-tag="allpng" data-image="true" data-bgimages="false" data-svg="false"><i class="fa-icon-folder-o" style="margin-right: 5px;"></i><?php _e("All PNG",'revslider'); ?></span><span id="obj_library_cats_alljpg" class="obj_library_cats" data-tag="alljpg" data-image="false" data-bgimages="true" data-svg="false"><i class="fa-icon-folder-o" style="margin-right: 5px;"></i><?php _e("All JPG",'revslider'); ?></span><span id="obj_library_cats_allsvg" class="obj_library_cats" data-tag="allsvg" data-image="false" data-bgimages="false" data-svg="true"><i class="fa-icon-folder-o" style="margin-right: 5px;"></i><?php _e("All SVG",'revslider'); ?></span></div>
  284. </div>
  285. <div id="object_library_results">
  286. <div id="object_library_results-inner">
  287. </div>
  288. </div>
  289. </div>
  290. </div>
  291. <div id="bg-vs-layer-wrapper">
  292. <span id="add_objimage_as_layer"><?php _e("As Layer",'revslider'); ?></span><span class="addthisasbg" id="obj-layer-bg-switcher"></span><span id="add_objimage_as_slidebg"><?php _e("As Slide BG",'revslider'); ?></span>
  293. </div>
  294. <?php
  295. $this->write_scripts();
  296. ?>
  297. </div>
  298. <?php
  299. }
  300. public function write_scripts(){
  301. ?>
  302. <script>
  303. var obj_libraries = [];
  304. jQuery('body').on('click','.object_library_itemfavorit',function() {
  305. var t = jQuery(this),
  306. item = t.parent(),
  307. d = item.data(),
  308. ic = t.find('i');
  309. if (ic.hasClass("fa-icon-star")) {
  310. ic.addClass("fa-icon-star-o");
  311. ic.removeClass("fa-icon-star");
  312. var newtags = item.data('tags');
  313. item.data('tags', newtags.replace(',favorite',''));
  314. favoriteObjectsList = jQuery.grep(favoriteObjectsList,function(value) { return value != d.src});
  315. } else {
  316. ic.removeClass("fa-icon-star-o");
  317. ic.addClass("fa-icon-star");
  318. var newtags = item.data('tags')+",favorite";
  319. item.data('tags', newtags);
  320. favoriteObjectsList.push(d.src);
  321. }
  322. return false;
  323. });
  324. // HOVER OVER SINGLE IMAGES
  325. jQuery('body').on('mouseenter','.obj_lib_container_img',function() {
  326. var _t = jQuery(this),
  327. typ = _t.data('type');
  328. if (typ==="2" || typ===2)
  329. jQuery('#bg-vs-layer-wrapper').appendTo(_t.find('.obj-item-size-selectors'));
  330. });
  331. // LOAD OBJECTS ON DEMAND WHEN THEY BECOME VISIBLE
  332. function loadObjNow(d) {
  333. d.data('loaded',true);
  334. var src = d.data('src'),
  335. ty = d.data('type');
  336. if (ty==="svg") {
  337. jQuery.get(src, function(data) {
  338. var div = document.createElement("div");
  339. div.innerHTML = new XMLSerializer().serializeToString(data.documentElement);
  340. d.find('.obj_item_media_holder').append(div);
  341. jQuery('#object_library_results').perfectScrollbar("update");
  342. });
  343. } else
  344. if (ty==="img" || ty==="1" || ty==="2" || ty===1 || ty===2) {
  345. if (src.indexOf("/") === -1) {
  346. getObjectUrl(src,d,'thumb');
  347. } else {
  348. d.find('.obj_item_media_holder').append('<div class="rs-obj-img-mediainner" style="background-image:url('+src+');"></div>');
  349. //jQuery('#object_library_results').perfectScrollbar("update");
  350. }
  351. } else
  352. if (ty==="icon") {
  353. d.find('.obj_item_media_holder').append('<i class="'+src+'"></i>');
  354. }
  355. }
  356. function ol_itemVisible(o,st) {
  357. var t = o.position().top,
  358. ch = jQuery('#object_library_results').height();
  359. if (t-st>-170 && t-st<ch && !o.data('loaded'))
  360. loadObjNow(o);
  361. }
  362. function ol_checkVisibilityOfItems() {
  363. var c = jQuery('#object_library_results-inner'),
  364. st = c.scrollTop();
  365. if (jQuery('#dialog_addobj').parent().css("display")==="block") {
  366. jQuery('#object_library_results-inner .obj_library_item').each(function() {
  367. var o = jQuery(this);
  368. if (o.hasClass("showit"))
  369. ol_itemVisible(o,st);
  370. });
  371. }
  372. }
  373. function createFontIcons() {
  374. jQuery('#waitaminute .waitaminute-message').append('<br>Loading Font Icons');
  375. var sheets = document.styleSheets,
  376. obj = new Object(),
  377. fi = 1;
  378. obj.handle = "FontIcons-"+fi;
  379. obj.list = new Array();
  380. if (sheets)
  381. jQuery.each(sheets,function(index,sheet) {
  382. var found = false,
  383. markup = "";
  384. if (sheet && sheet.href && sheet.href.indexOf("plugins/revslider/public/assets/fonts/")>=0) {
  385. try{
  386. if (sheet.cssRules !==null & sheet.cssRules!=undefined)
  387. jQuery.each(sheet.cssRules, function(index,rule) {
  388. if (rule && rule!==null && rule !=="null" && rule.selectorText!=undefined) {
  389. jQuery.each(rs_icon_sets,function(j,prefix){
  390. if (rule.selectorText.split(prefix).length>1 && rule.cssText.split("content").length>1) {
  391. var csname = rule.selectorText.split("::before")[0].split(":before")[0];
  392. if (csname!=undefined) {
  393. csname = csname.split(".")[1];
  394. if (csname!=undefined) {
  395. found = true;
  396. var iconobj = new Object();
  397. iconobj.name = csname;
  398. iconobj.group = "icon";
  399. iconobj.tags="fonticon"+fi+","+csname.replace(/\-/g,",");
  400. iconobj.type="icon";
  401. iconobj.src=csname;
  402. obj.list.push(iconobj);
  403. }
  404. }
  405. }
  406. })
  407. }
  408. });
  409. } catch(e) {
  410. }
  411. if (found) {
  412. found = false;
  413. if (obj.list.length>0) obj_libraries.push(obj);
  414. obj = new Object();
  415. jQuery('#object-tag-list').append('<span id="obj_library_cats_fonticon_'+fi+'" class="obj_library_cats" data-tag="fonticon'+fi+'" data-icon="true">Font Icons '+fi+'</span>')
  416. fi++;
  417. obj.handle="FontIcons"+fi;
  418. obj.list=new Array();
  419. }
  420. }
  421. });
  422. }
  423. // ADD THE ITEMS TO THE MAIN CONTAINER (ONLY EMPTY PLACEHOLDERS FIRST)
  424. function push_objects_to_library(){
  425. var c = jQuery('#object_library_results-inner'),
  426. svgi = 0,
  427. pngi = 0,
  428. jpgi = 0,
  429. iconi = 0,
  430. taggroups = {},
  431. counter = 0;
  432. if (favoriteObjectsList !==undefined && favoriteObjectsList.length>0) {
  433. svgi=5;
  434. pngi=5;
  435. jpgi=5;
  436. iconi=5;
  437. } else {
  438. favoriteObjectsList = new Array();
  439. }
  440. createFontIcons();
  441. //console.time("Create Markups");
  442. for (var i=0;i<obj_libraries.length;i++) {
  443. var library = obj_libraries[i];
  444. //console.time("Library Handle:"+library.handle);
  445. for (var j=0;j<library.list.length;j++) {
  446. var item = library.list[j],
  447. ref = 'obj-item-'+library.handle+'-'+i,
  448. titl = item.name!==undefined ? item.name : item.src,
  449. short = "SVG",
  450. color = "purple",
  451. classext = "",
  452. tt = '',
  453. favclass = "fa-icon-star-o";
  454. sizes = item.type==="svg" ? "" : '<div class="obj-item-size-selectors"><div class="sizetooltip"></div><div data-s="xs" class="obj-item-size-selector nfbg">XS</div><div data-s="s" class="obj-item-size-selector nfbg">S</div><div data-s="m" class="obj-item-size-selector">M</div><div data-s="l" class="obj-item-size-selector">L</div><div data-s="o" class="obj-item-size-selector">O</div></div>';
  455. switch (item.type) {
  456. case "svg":
  457. short = "SVG";
  458. color = "purple";
  459. classext = "svg";
  460. item.tags = item.tags+",allsvg";
  461. if (svgi<3) {
  462. item.tags = item.tags+",favorite";
  463. favclass = "fa-icon-star";
  464. svgi++;
  465. favoriteObjectsList.push(item.src);
  466. }
  467. break;
  468. case 1:
  469. case "1":
  470. short = "PNG";
  471. color = "green";
  472. classext = "img";
  473. item.tags = item.tags+",allpng";
  474. if (pngi<3) {
  475. item.tags = item.tags+",favorite";
  476. favclass = "fa-icon-star";
  477. pngi++;
  478. favoriteObjectsList.push(item.src);
  479. }
  480. break;
  481. case 2:
  482. case "2":
  483. short = "JPG";
  484. color = "blue";
  485. classext = "img";
  486. item.tags = item.tags+",alljpg";
  487. if (jpgi<3) {
  488. item.tags = item.tags+",favorite";
  489. favclass = "fa-icon-star";
  490. jpgi++;
  491. favoriteObjectsList.push(item.src);
  492. }
  493. break;
  494. case "icon":
  495. short = "ICON";
  496. color = "red";
  497. classext = "icon";
  498. item.tags = item.tags+",allicon";
  499. if (iconi<3) {
  500. item.tags = item.tags+",favorite";
  501. favclass = "fa-icon-star";
  502. iconi++;
  503. favoriteObjectsList.push(item.src);
  504. }
  505. break;
  506. }
  507. if (favoriteObjectsList !==undefined && favoriteObjectsList.length>0 && jQuery.inArray(item.src,favoriteObjectsList)>=0){
  508. item.tags = item.tags+",favorite";
  509. favclass = "fa-icon-star";
  510. }
  511. tt = '<div class="obj_library_item_type_'+color+' ">'+short+'</div>';
  512. if (titl.indexOf("/") !== -1) {
  513. titl = titl.split("/");
  514. titl = titl[titl.length-1].split(".")[0];
  515. } else {
  516. titl = titl.split(".")[0];
  517. }
  518. titl = titl.replace(/\_/g," ");
  519. titl = titl.replace(/\-/g," ");
  520. item.title = titl.toLowerCase().split(" ");
  521. item.tags_array = item.tags.toLowerCase().split(",");
  522. item.idref = ref+"_"+counter;
  523. counter++;
  524. var el = '<div id="'+item.idref+'" data-title="'+titl+'" data-origsrc="'+item.origsrc+'" data-type="'+item.type+'" data-group="'+item.group+'" data-src="'+item.src+'" data-mediawidth="'+item.width+'" data-mediaheight="'+item.height+'" data-tags="'+item.tags+'" class="obj_lib_container_'+classext+' obj_library_item objadd-single-item "><div class="object_library_itemfavorit"><i class="'+favclass+'"></i></div><div class="objadd-single-item_holder obj_item_media_holder"></div><div class="obj_lib_item_title">'+titl+'</div>'+tt+sizes+'</div>';
  525. c.append(el);
  526. item.ref = el;
  527. var otags = item.tags.split(",");
  528. for (var k=0;k<otags.length;k++) {
  529. if (taggroups[item.group]===undefined)
  530. taggroups[item.group] = new Object();
  531. taggroups[item.group][otags[k]] = true;
  532. }
  533. };
  534. //console.timeEnd("Library Handle:"+library.handle);
  535. };
  536. jQuery.each(taggroups, function(key,value) {
  537. jQuery.each(value,function(tag){
  538. jQuery('#obj_library_cats_'+tag).data(key,true);
  539. })
  540. });
  541. //console.timeEnd("Create Markups");
  542. }
  543. jQuery('body').on('mouseenter','.obj-item-size-selector',function() {
  544. var _t = jQuery(this),
  545. _i = _t.closest('.obj_library_item'),
  546. _ttip = _i.find('.sizetooltip'),
  547. size = 1;
  548. switch (_t.data('s')) {
  549. case "xs": size = 0.1;break;
  550. case "s": size = 0.25;break;
  551. case "m": size = 0.50;break;
  552. case "l": size = 0.75;break;
  553. case "o": size = 1;break;
  554. };
  555. _ttip.html(Math.round(_i.data('mediawidth')*size)+' x '+Math.round(_i.data('mediaheight')*size))
  556. });
  557. jQuery('body').on('mouseleave','.obj-item-size-selector',function() {
  558. var _t = jQuery(this),
  559. _i = _t.closest('.obj_library_item'),
  560. _ttip = _i.find('.sizetooltip');
  561. _ttip.html("");
  562. });
  563. jQuery('#add_objimage_as_layer').click(function() {
  564. jQuery('#obj-layer-bg-switcher').removeClass("addthisasbg");
  565. });
  566. jQuery('#add_objimage_as_slidebg').click(function() {
  567. jQuery('#obj-layer-bg-switcher').addClass("addthisasbg");
  568. });
  569. jQuery('#obj-layer-bg-switcher').click(function() {
  570. var _t = jQuery(this);
  571. if (_t.hasClass("addthisasbg"))
  572. _t.removeClass("addthisasbg");
  573. else
  574. _t.addClass("addthisasbg");
  575. });
  576. ol_checkVisibilityOfItems();
  577. // TAKE CARE ABOUT SCROLL OF THE LIBRARY CONTAINER
  578. jQuery('#object_library_results').perfectScrollbar({wheelPropagation:false,suppressScrollX:true});
  579. jQuery('#object_library_results').perfectScrollbar("update");
  580. document.addEventListener('ps-scroll-y', function (e) {
  581. if (jQuery(e.target).closest('#object_library_results').length>0) {
  582. ol_checkVisibilityOfItems();
  583. }
  584. });
  585. // CHANGING THE TAGS SHOULD CHANGE THE LIST OF ITEMS
  586. jQuery('body').on('click','.obj_library_cats',function() {
  587. jQuery('.obj_library_cats').removeClass("selected");
  588. jQuery('#obj_library_cats_searchresult').remove();
  589. jQuery(this).addClass("selected");
  590. jQuery('#obj_library_search').val("");
  591. searchForCatsAndGroups();
  592. });
  593. jQuery('body').on('click','.obj_library_cats_filter',function() {
  594. var op = jQuery(this),
  595. gr = op.data('value'),
  596. fv = 0,
  597. sv = 0;
  598. jQuery('.obj_library_cats_filter').removeClass("selected");
  599. op.addClass("selected");
  600. jQuery('.obj_library_cats').each(function() {
  601. var _t = jQuery(this);
  602. if (_t.data(gr) || gr==="all" || (gr==="allimages" && (_t.data("image") || _t.data("bgimage")))) {
  603. _t.show();
  604. if (fv===0)
  605. fv=_t;
  606. else
  607. if (sv===0)
  608. sv=_t;
  609. }
  610. else
  611. _t.hide();
  612. });
  613. if (gr==="all") {
  614. jQuery('#obj_library_cats_allico').hide();
  615. jQuery('#obj_library_cats_allpng').hide();
  616. jQuery('#obj_library_cats_alljpg').hide();
  617. jQuery('#obj_library_cats_allsvg').hide();
  618. }
  619. searchForCatsAndGroups();
  620. searchForObjects();
  621. if (jQuery('.obj_library_cats.selected').is(":visible")===false || (fv!==0 && jQuery('.obj_library_cats.selected').data('tag')==="favorite" && gr!=="all")) {
  622. jQuery('.obj_library_cats.selected').removeClass("selected");
  623. if(fv !== 0){
  624. if (gr==="all")
  625. fv.click();
  626. if (gr!=="all" && sv !== 0)
  627. sv.click();
  628. }
  629. }
  630. });
  631. // SHOW ONLY ELEMENTS WITH SELECTED TAGS
  632. function searchForCatsAndGroups() {
  633. jQuery('#object_library_results').scrollTop(0);
  634. jQuery('.obj_library_cats.selected').each(function() {
  635. var searched_lib = jQuery(this).data('tag'),
  636. group = jQuery('.obj_library_cats_filter.selected').data('value');
  637. jQuery('.obj_library_item').each(function() {
  638. var o = jQuery(this),
  639. otags = o.data('tags').split(","),
  640. gr = o.data('group'),
  641. ty = o.data('type'),
  642. afo = false;
  643. if (group==="all" || group===gr || (group=="allimages" && (ty===2 || ty===1))) {
  644. for (var ti =0;ti<otags.length;ti++) {
  645. if (otags[ti]===searched_lib || afo || searched_lib==="all") {
  646. o.addClass("showit");
  647. afo=true;
  648. }
  649. else {
  650. o.removeClass("showit");
  651. }
  652. }
  653. } else {
  654. o.removeClass("showit");
  655. }
  656. });
  657. });
  658. ol_checkVisibilityOfItems();
  659. }
  660. // SEARCH FOR OBJECTS
  661. function searchForObjects() {
  662. jQuery('#object_library_results').scrollTop(0);
  663. jQuery('#obj_library_cats_searchresult').remove();
  664. var searchfor = jQuery('#obj_library_search').val(),
  665. res = 0,
  666. group = jQuery('.obj_library_cats_filter.selected').data('value');
  667. if (searchfor.length<3) return;
  668. searchfor = searchfor.replace(/\ /g,",");
  669. searchfor = searchfor.split(",");
  670. jQuery.each(obj_libraries, function(i,library) {
  671. jQuery.each(library.list,function(i,item) {
  672. var found = false;
  673. if (group==="all" || item.group === group)
  674. jQuery.each(searchfor, function(i,sf){
  675. if (sf.length>2) {
  676. // CHECK TITLE
  677. if (jQuery.isArray(item.title)) {
  678. if (jQuery.inArray(sf.toLowerCase(),item.title)>=0) {
  679. jQuery('#'+item.idref).addClass("showit");
  680. res++;
  681. found = true;
  682. return true;
  683. } else {
  684. jQuery.each(item.title,function(i,tt){
  685. if (tt.indexOf(sf)>=0) {
  686. jQuery('#'+item.idref).addClass("showit");
  687. res++;
  688. found = true;
  689. return true;
  690. }
  691. });
  692. }
  693. } else {
  694. if (sf.toLowerCase() === item.title) {
  695. jQuery('#'+item.idref).addClass("showit");
  696. res++;
  697. found = true;
  698. return true;
  699. }
  700. }
  701. //CHECK ITME TAGS
  702. if (jQuery.isArray(item.tags_array)) {
  703. if (jQuery.inArray(sf.toLowerCase(),item.tags_array)>=0) {
  704. jQuery('#'+item.idref).addClass("showit");
  705. res++;
  706. found = true;
  707. return true;
  708. } else {
  709. jQuery.each(item.tags_array,function(i,tt){
  710. if (tt.indexOf(sf)>=0) {
  711. jQuery('#'+item.idref).addClass("showit");
  712. res++;
  713. found = true;
  714. return true;
  715. }
  716. });
  717. }
  718. } else {
  719. if (sf.toLowerCase() === item.tags_array) {
  720. jQuery('#'+item.idref).addClass("showit");
  721. res++;
  722. found = true;
  723. return true;
  724. }
  725. }
  726. item.tags_array
  727. }
  728. });
  729. if (!found) jQuery('#'+item.idref).removeClass("showit");
  730. });
  731. });
  732. jQuery('.obj_library_cats').removeClass("selected");
  733. jQuery('.object-tag-list').prepend('<span class="obj_library_cats selected" id="obj_library_cats_searchresult">Found ('+res+') elements</span>');
  734. ol_checkVisibilityOfItems();
  735. }
  736. var objl_keyuprefresh;
  737. jQuery("#obj_library_search").keyup(function(){
  738. clearTimeout(objl_keyuprefresh);
  739. var v = jQuery(this).val();
  740. objl_keyuprefresh = setTimeout(function() {
  741. if (v.length>2)
  742. searchForObjects();
  743. },150);
  744. });
  745. jQuery('#obj_library_search_trigger').click(searchForObjects);
  746. function getObjectUrl(handle, el, type){
  747. UniteAdminRev.ajaxRequest('load_library_object', {'handle': handle, 'type': type}, function(response){
  748. if(response.success){
  749. el.find('.obj_item_media_holder').append('<div class="rs-obj-img-mediainner" style="background-image:url('+response.url+');"></div>');
  750. jQuery('#object_library_results').perfectScrollbar("update");
  751. } else {
  752. }
  753. });
  754. }
  755. jQuery('#update_obect_library').click(function(){
  756. if(confirm(rev_lang.unsaved_data_will_be_lost_proceed)){
  757. jQuery('#dialog_addobj').dialog("close");
  758. showWaitAMinute({fadeIn:300,text:rev_lang.please_wait_a_moment});
  759. location.href = window.location.href+'&update_object_library';
  760. }
  761. });
  762. var favoriteObjectsList = [];
  763. <?php
  764. $obj_favs = $this->get_favorites();
  765. if(!empty($obj_favs)){
  766. foreach($obj_favs as $fav){
  767. ?>favoriteObjectsList.push("<?php echo $fav; ?>");
  768. <?php
  769. }
  770. }
  771. ?>
  772. </script>
  773. <?php
  774. }
  775. public function load_objects(){
  776. $obj = array();
  777. $svgs = RevSliderBase::get_svg_sets_full();
  778. $obj['svg'] = $svgs;
  779. $online = get_option('rs-library', array());
  780. if(!empty($online)){
  781. $obj['online'] = $online;
  782. }
  783. return $obj;
  784. }
  785. public function create_image_dimensions($handle, $force = false){
  786. $img_editor_test = wp_image_editor_supports(array('methods' => array('resize', 'save')));
  787. if($img_editor_test !== true){
  788. return false;
  789. }
  790. $upload_dir = wp_upload_dir(); // Set upload folder
  791. $upload_directory = $upload_dir['basedir'] . $this->object_orig_path;
  792. $upload_url = $upload_dir['baseurl'] . $this->object_orig_path;
  793. $image_path = $upload_directory.$handle;
  794. $file_name_with_ending = explode("/", $image_path);
  795. $file_name_with_ending = $file_name_with_ending[count($file_name_with_ending) - 1];
  796. $file_name_without_ending = explode(".", $file_name_with_ending);
  797. $file_ending = $file_name_without_ending[count($file_name_without_ending) - 1];
  798. $file_name_without_ending = $file_name_without_ending[count($file_name_without_ending) - 2];
  799. $sizes = array('75', '50', '25', '10');
  800. $image = wp_get_image_editor($image_path);
  801. $imgsize = getimagesize($image_path);
  802. if(!is_wp_error($image) && $imgsize !== false) {
  803. $orig_width = $imgsize['0'];
  804. $orig_height = $imgsize['1'];
  805. foreach($sizes as $size){
  806. $modified_file_name_without_ending = $file_name_without_ending . '-' . $size;
  807. if(!file_exists($upload_directory.$modified_file_name_without_ending.'.'.$file_ending) || $force) {
  808. $width = round($orig_width / 100 * $size, 0);
  809. $height = round($orig_height / 100 * $size, 0);
  810. $image->resize($width, $height);
  811. $image->save($upload_directory.$modified_file_name_without_ending.'.'.$file_ending);
  812. }
  813. }
  814. }else{ //cant create images
  815. return false;
  816. }
  817. return true;
  818. }
  819. /**
  820. * Check if Curl can be used
  821. */
  822. public function check_curl_connection(){
  823. if($this->curl_check !== null) return $this->curl_check;
  824. $curl = new WP_Http_Curl();
  825. $this->curl_check = $curl->test();
  826. return $this->curl_check;
  827. }
  828. /**
  829. * Returns an URL if it is an object library image, depending on the choosen width/height or the chosen image size
  830. */
  831. public function get_correct_size_url($image_path, $imgres, $library_size = array()){
  832. if(!is_array($imgres)){
  833. //wordpress full, medium ect
  834. //or check current device and change depending on device
  835. $img_sizes = get_intermediate_image_sizes();
  836. if(isset($img_sizes[$imgres]) && isset($img_sizes[$imgres]['width']) && isset($img_sizes[$imgres]['height'])){
  837. $imgres = array($img_sizes[$imgres]['width'], $img_sizes[$imgres]['height']);
  838. }
  839. }else{
  840. /**
  841. * check if we have a % and if yes, turn the image back to what was selected in the beginning instead of how it was scaled
  842. * as it is already an array, it can be the following:
  843. * px
  844. * %
  845. * empty, then this means auto
  846. * if %, then always get the image that was selected
  847. **/
  848. if(isset($library_size['width']) && isset($library_size['height'])){
  849. foreach($imgres as $res){
  850. if(strpos($res, '%') !== false || $res == 'SET'){
  851. $imgres = array($library_size['width'], $library_size['height']);
  852. break;
  853. }
  854. }
  855. }
  856. }
  857. if(is_array($imgres)){
  858. //check if file exists
  859. if(!file_exists($image_path)) return $image_path;
  860. $upload_dir = wp_upload_dir(); // Set upload folder
  861. $upload_directory = $upload_dir['basedir'] . $this->object_orig_path;
  862. $upload_url = $upload_dir['baseurl'] . $this->object_orig_path;
  863. //we got width and high, lets check which one to use
  864. $file_name_with_ending = explode("/", $image_path);
  865. $file_name_with_ending = $file_name_with_ending[count($file_name_with_ending) - 1];
  866. $file_name_without_ending = explode(".", $file_name_with_ending);
  867. $file_ending = $file_name_without_ending[count($file_name_without_ending) - 1];
  868. $file_name_without_ending = $file_name_without_ending[count($file_name_without_ending) - 2];
  869. $sizes = array('75', '50', '25', '10');
  870. $imgsize = getimagesize($image_path);
  871. if($imgsize !== false) {
  872. $orig_width = $imgsize['0'];
  873. $orig_height = $imgsize['1'];
  874. foreach($sizes as $size){
  875. $width = round($orig_width / 100 * $size, 0);
  876. $height = round($orig_height / 100 * $size, 0);
  877. if($width >= $imgres[0] && $height >= $imgres[1]){
  878. $modified_file_name_without_ending = $file_name_without_ending . '-' . $size;
  879. if(file_exists($upload_directory.$modified_file_name_without_ending.'.'.$file_ending)) {
  880. $image_path = $upload_url.$modified_file_name_without_ending.'.'.$file_ending;
  881. }
  882. }
  883. }
  884. }
  885. }
  886. return $image_path;
  887. }
  888. public function retrieve_all_object_data(){
  889. $obj = $this->load_objects();
  890. $data = array('html' => array(), 'list' => array());
  891. $svgs = $obj['svg'];
  892. if(!empty($svgs) && is_array($svgs)){
  893. foreach($svgs as $svghandle => $svgfiles){
  894. $data['html'][] = array('type' => 'tag', 'handle' => $svghandle, 'name' => $svghandle);
  895. $data['html'][] = array('type' => 'inner');
  896. $data['list'][$svghandle] = array();
  897. foreach($svgfiles as $svgfile => $svgpath){
  898. $data['list'][$svghandle][] = array(
  899. 'src' => $svgpath,
  900. 'origsrc' => '',
  901. 'type' => 'svg',
  902. 'group' => 'svg',
  903. 'tags' => $svghandle,
  904. );
  905. }
  906. }
  907. }
  908. if(isset($obj['online']) && isset($obj['online']['objects'])){
  909. $online = $obj['online']['objects'];
  910. if(!empty($online) && is_array($online)){
  911. if(isset($obj['online']['tags'])){
  912. foreach($obj['online']['tags'] as $t){
  913. $data['html'][] = array('type' => 'tag', 'handle' => $t['handle'], 'name' => $t['name']);
  914. }
  915. }
  916. $data['html'][] = array('type' => 'inner');
  917. $data['list']['png'] = array();
  918. foreach($online as $online_file){
  919. $my_data = $this->_get_object_data($online_file['handle']);
  920. $my_tags = array();
  921. $group = "image";
  922. if ($online_file['type']==='2') $group="bgimage";
  923. if(isset($online_file['tags']) && !empty($online_file['tags'])){
  924. foreach($online_file['tags'] as $t){
  925. if(is_array($t) && array_key_exists('handle', $t)){
  926. $my_tags[] = $t['handle'];
  927. }
  928. }
  929. }
  930. $data['list']['png'][] = array(
  931. 'src' => $my_data['thumb'],
  932. 'origsrc' => $my_data['orig'],
  933. 'type' => $online_file['type'],
  934. 'group' => $group,
  935. 'width' => $online_file['width'],
  936. 'height' => $online_file['height'],
  937. 'tags' => implode(',', $my_tags),
  938. 'name' => $online_file['name']
  939. );
  940. }
  941. }
  942. }
  943. return $data;
  944. }
  945. /**
  946. * get list of favorites
  947. * @since: 5.3.0
  948. */
  949. public function get_favorites(){
  950. return get_option('rs_obj_favorites', array());
  951. }
  952. /**
  953. * save list of favorites
  954. * @since: 5.3.0
  955. */
  956. public function save_favorites($favourites){
  957. update_option('rs_obj_favorites', $favourites);
  958. }
  959. }
  960. ?>