functions.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2015 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderFunctions{
  9. public static function throwError($message,$code=null){
  10. if(!empty($code)){
  11. throw new Exception($message,$code);
  12. }else{
  13. throw new Exception($message);
  14. }
  15. }
  16. /**
  17. * set output for download
  18. */
  19. public static function downloadFile($str,$filename="output.txt"){
  20. //output for download
  21. header('Content-Description: File Transfer');
  22. header('Content-Type: text/html; charset=UTF-8');
  23. header("Content-Disposition: attachment; filename=".$filename.";");
  24. header("Content-Transfer-Encoding: binary");
  25. header("Content-Length: ".strlen($str));
  26. echo $str;
  27. exit();
  28. }
  29. /**
  30. * turn boolean to string
  31. */
  32. public static function boolToStr($bool){
  33. if(gettype($bool) == "string")
  34. return($bool);
  35. if($bool == true)
  36. return("true");
  37. else
  38. return("false");
  39. }
  40. /**
  41. * convert string to boolean
  42. */
  43. public static function strToBool($str){
  44. if(is_bool($str))
  45. return($str);
  46. if(empty($str))
  47. return(false);
  48. if(is_numeric($str))
  49. return($str != 0);
  50. $str = strtolower($str);
  51. if($str == "true")
  52. return(true);
  53. return(false);
  54. }
  55. /**
  56. * get value from array. if not - return alternative
  57. */
  58. public static function getVal($arr,$key,$altVal=""){
  59. if(is_array($arr)){
  60. if(isset($arr[$key])){
  61. return($arr[$key]);
  62. }
  63. }elseif(is_object($arr)){
  64. if(isset($arr->$key)){
  65. return($arr->$key);
  66. }
  67. }
  68. return($altVal);
  69. }
  70. //------------------------------------------------------------
  71. // get variable from post or from get. post wins.
  72. public static function getPostGetVariable($name,$initVar = ""){
  73. $var = $initVar;
  74. if(isset($_POST[$name])) $var = $_POST[$name];
  75. else if(isset($_GET[$name])) $var = $_GET[$name];
  76. return($var);
  77. }
  78. //------------------------------------------------------------
  79. public static function getPostVariable($name,$initVar = ""){
  80. $var = $initVar;
  81. if(isset($_POST[$name])) $var = $_POST[$name];
  82. return($var);
  83. }
  84. //------------------------------------------------------------
  85. public static function getGetVar($name,$initVar = ""){
  86. $var = $initVar;
  87. if(isset($_GET[$name])) $var = $_GET[$name];
  88. return($var);
  89. }
  90. public static function sortByOrder($a, $b) {
  91. return $a['order'] - $b['order'];
  92. }
  93. /**
  94. * validate that some file exists, if not - throw error
  95. */
  96. public static function validateFilepath($filepath,$errorPrefix=null){
  97. if(file_exists($filepath) == true)
  98. return(false);
  99. if($errorPrefix == null)
  100. $errorPrefix = "File";
  101. $message = $errorPrefix." ".esc_attr($filepath)." not exists!";
  102. self::throwError($message);
  103. }
  104. /**
  105. * validate that some value is numeric
  106. */
  107. public static function validateNumeric($val,$fieldName=""){
  108. self::validateNotEmpty($val,$fieldName);
  109. if(empty($fieldName))
  110. $fieldName = "Field";
  111. if(!is_numeric($val))
  112. self::throwError("$fieldName should be numeric ");
  113. }
  114. /**
  115. * validate that some variable not empty
  116. */
  117. public static function validateNotEmpty($val,$fieldName=""){
  118. if(empty($fieldName))
  119. $fieldName = "Field";
  120. if(empty($val) && is_numeric($val) == false)
  121. self::throwError("Field <b>$fieldName</b> should not be empty");
  122. }
  123. //------------------------------------------------------------
  124. //get path info of certain path with all needed fields
  125. public static function getPathInfo($filepath){
  126. $info = pathinfo($filepath);
  127. //fix the filename problem
  128. if(!isset($info["filename"])){
  129. $filename = $info["basename"];
  130. if(isset($info["extension"]))
  131. $filename = substr($info["basename"],0,(-strlen($info["extension"])-1));
  132. $info["filename"] = $filename;
  133. }
  134. return($info);
  135. }
  136. /**
  137. * Convert std class to array, with all sons
  138. * @param unknown_type $arr
  139. */
  140. public static function convertStdClassToArray($arr){
  141. $arr = (array)$arr;
  142. $arrNew = array();
  143. foreach($arr as $key=>$item){
  144. $item = (array)$item;
  145. $arrNew[$key] = $item;
  146. }
  147. return($arrNew);
  148. }
  149. public static function cleanStdClassToArray($arr){
  150. $arr = (array)$arr;
  151. $arrNew = array();
  152. foreach($arr as $key=>$item){
  153. $arrNew[$key] = $item;
  154. }
  155. return($arrNew);
  156. }
  157. /**
  158. * encode array into json for client side
  159. */
  160. public static function jsonEncodeForClientSide($arr){
  161. $json = "";
  162. if(!empty($arr)){
  163. $json = json_encode($arr);
  164. $json = addslashes($json);
  165. }
  166. if(empty($json)) $json = '{}';
  167. $json = "'".$json."'";
  168. return($json);
  169. }
  170. /**
  171. * decode json from the client side
  172. */
  173. public static function jsonDecodeFromClientSide($data){
  174. $data = stripslashes($data);
  175. $data = str_replace('&#092;"','\"',$data);
  176. $data = json_decode($data);
  177. $data = (array)$data;
  178. return($data);
  179. }
  180. /**
  181. * do "trim" operation on all array items.
  182. */
  183. public static function trimArrayItems($arr){
  184. if(gettype($arr) != "array")
  185. RevSliderFunctions::throwError("trimArrayItems error: The type must be array");
  186. foreach ($arr as $key=>$item){
  187. if(is_array($item)){
  188. foreach($item as $key => $value){
  189. $arr[$key][$key] = trim($value);
  190. }
  191. }else{
  192. $arr[$key] = trim($item);
  193. }
  194. }
  195. return($arr);
  196. }
  197. /**
  198. * get link html
  199. */
  200. public static function getHtmlLink($link,$text,$id="",$class=""){
  201. if(!empty($class))
  202. $class = " class='$class'";
  203. if(!empty($id))
  204. $id = " id='$id'";
  205. $html = "<a href=\"$link\"".$id.$class.">$text</a>";
  206. return($html);
  207. }
  208. /**
  209. * get select from array
  210. */
  211. public static function getHTMLSelect($arr,$default="",$htmlParams="",$assoc = false){
  212. $html = "<select $htmlParams>";
  213. foreach($arr as $key=>$item){
  214. $selected = "";
  215. if($assoc == false){
  216. if($item == $default) $selected = " selected ";
  217. }else{
  218. if(trim($key) == trim($default)) $selected = " selected ";
  219. }
  220. if($assoc == true)
  221. $html .= "<option $selected value='$key'>$item</option>";
  222. else
  223. $html .= "<option $selected value='$item'>$item</option>";
  224. }
  225. $html.= "</select>";
  226. return($html);
  227. }
  228. /**
  229. * convert assoc array to array
  230. */
  231. public static function assocToArray($assoc){
  232. $arr = array();
  233. foreach($assoc as $item)
  234. $arr[] = $item;
  235. return($arr);
  236. }
  237. /**
  238. *
  239. * strip slashes from textarea content after ajax request to server
  240. */
  241. public static function normalizeTextareaContent($content){
  242. if(empty($content))
  243. return($content);
  244. $content = stripslashes($content);
  245. $content = trim($content);
  246. return($content);
  247. }
  248. /**
  249. * get text intro, limit by number of words
  250. */
  251. public static function getTextIntro($text, $limit){
  252. $arrIntro = explode(' ', $text, $limit);
  253. if (count($arrIntro)>=$limit) {
  254. array_pop($arrIntro);
  255. $intro = implode(" ",$arrIntro);
  256. $intro = trim($intro);
  257. if(!empty($intro))
  258. $intro .= '...';
  259. } else {
  260. $intro = implode(" ",$arrIntro);
  261. }
  262. $intro = preg_replace('`\[[^\]]*\]`','',$intro);
  263. return($intro);
  264. }
  265. /**
  266. * add missing px/% to value, do also for object and array
  267. * @since: 5.0
  268. **/
  269. public static function add_missing_val($obj, $set_to = 'px'){
  270. if(is_array($obj)){
  271. foreach($obj as $key => $value){
  272. if(strpos($value, $set_to) === false){
  273. $obj[$key] = $value.$set_to;
  274. }
  275. }
  276. }elseif(is_object($obj)){
  277. foreach($obj as $key => $value){
  278. if(strpos($value, $set_to) === false){
  279. $obj->$key = $value.$set_to;
  280. }
  281. }
  282. }else{
  283. if(strpos($obj, $set_to) === false){
  284. $obj .= $set_to;
  285. }
  286. }
  287. return $obj;
  288. }
  289. /**
  290. * normalize object with device informations depending on what is enabled for the Slider
  291. * @since: 5.0
  292. **/
  293. public static function normalize_device_settings($obj, $enabled_devices, $return = 'obj', $set_to_if = array()){ //array -> from -> to
  294. /*desktop
  295. notebook
  296. tablet
  297. mobile*/
  298. if(!empty($set_to_if)){
  299. foreach($obj as $key => $value) {
  300. foreach($set_to_if as $from => $to){
  301. if(trim($value) == $from) $obj->$key = $to;
  302. }
  303. }
  304. }
  305. $inherit_size = self::get_biggest_device_setting($obj, $enabled_devices);
  306. if($enabled_devices['desktop'] == 'on'){
  307. if(!isset($obj->desktop) || $obj->desktop === ''){
  308. $obj->desktop = $inherit_size;
  309. }else{
  310. $inherit_size = $obj->desktop;
  311. }
  312. }else{
  313. $obj->desktop = $inherit_size;
  314. }
  315. if($enabled_devices['notebook'] == 'on'){
  316. if(!isset($obj->notebook) || $obj->notebook === ''){
  317. $obj->notebook = $inherit_size;
  318. }else{
  319. $inherit_size = $obj->notebook;
  320. }
  321. }else{
  322. $obj->notebook = $inherit_size;
  323. }
  324. if($enabled_devices['tablet'] == 'on'){
  325. if(!isset($obj->tablet) || $obj->tablet === ''){
  326. $obj->tablet = $inherit_size;
  327. }else{
  328. $inherit_size = $obj->tablet;
  329. }
  330. }else{
  331. $obj->tablet = $inherit_size;
  332. }
  333. if($enabled_devices['mobile'] == 'on'){
  334. if(!isset($obj->mobile) || $obj->mobile === ''){
  335. $obj->mobile = $inherit_size;
  336. }else{
  337. $inherit_size = $obj->mobile;
  338. }
  339. }else{
  340. $obj->mobile = $inherit_size;
  341. }
  342. switch($return){
  343. case 'obj':
  344. //order according to: desktop, notebook, tablet, mobile
  345. $new_obj = new stdClass();
  346. $new_obj->desktop = $obj->desktop;
  347. $new_obj->notebook = $obj->notebook;
  348. $new_obj->tablet = $obj->tablet;
  349. $new_obj->mobile = $obj->mobile;
  350. return $new_obj;
  351. break;
  352. case 'html-array':
  353. if($obj->desktop === $obj->notebook && $obj->desktop === $obj->mobile && $obj->desktop === $obj->tablet){
  354. return $obj->desktop;
  355. }else{
  356. return "['".@$obj->desktop."','".@$obj->notebook."','".@$obj->tablet."','".@$obj->mobile."']";
  357. }
  358. break;
  359. }
  360. return $obj;
  361. }
  362. /**
  363. * return biggest value of object depending on which devices are enabled
  364. * @since: 5.0
  365. **/
  366. public static function get_biggest_device_setting($obj, $enabled_devices){
  367. if($enabled_devices['desktop'] == 'on'){
  368. if(isset($obj->desktop) && $obj->desktop != ''){
  369. return $obj->desktop;
  370. }
  371. }
  372. if($enabled_devices['notebook'] == 'on'){
  373. if(isset($obj->notebook) && $obj->notebook != ''){
  374. return $obj->notebook;
  375. }
  376. }
  377. if($enabled_devices['tablet'] == 'on'){
  378. if(isset($obj->tablet) && $obj->tablet != ''){
  379. return $obj->tablet;
  380. }
  381. }
  382. if($enabled_devices['mobile'] == 'on'){
  383. if(isset($obj->mobile) && $obj->mobile != ''){
  384. return $obj->mobile;
  385. }
  386. }
  387. return '';
  388. }
  389. /**
  390. * change hex to rgba
  391. */
  392. public static function hex2rgba($hex, $transparency = false, $raw = false, $do_rgb = false) {
  393. if($transparency !== false){
  394. $transparency = ($transparency > 0) ? number_format( ( $transparency / 100 ), 2, ".", "" ) : 0;
  395. }else{
  396. $transparency = 1;
  397. }
  398. $hex = str_replace("#", "", $hex);
  399. if(strlen($hex) == 3) {
  400. $r = hexdec(substr($hex,0,1).substr($hex,0,1));
  401. $g = hexdec(substr($hex,1,1).substr($hex,1,1));
  402. $b = hexdec(substr($hex,2,1).substr($hex,2,1));
  403. } else if(self::isrgb($hex)){
  404. return $hex;
  405. } else {
  406. $r = hexdec(substr($hex,0,2));
  407. $g = hexdec(substr($hex,2,2));
  408. $b = hexdec(substr($hex,4,2));
  409. }
  410. if($do_rgb){
  411. $ret = $r.', '.$g.', '.$b;
  412. }else{
  413. $ret = $r.', '.$g.', '.$b.', '.$transparency;
  414. }
  415. if($raw){
  416. return $ret;
  417. }else{
  418. return 'rgba('.$ret.')';
  419. }
  420. }
  421. public static function isrgb($rgba){
  422. if(strpos($rgba, 'rgb') !== false) return true;
  423. return false;
  424. }
  425. /**
  426. * change rgba to hex
  427. * @since: 5.0
  428. */
  429. public static function rgba2hex($rgba){
  430. if(strtolower($rgba) == 'transparent') return $rgba;
  431. $temp = explode(',', $rgba);
  432. $rgb = array();
  433. if(count($temp) == 4) unset($temp[3]);
  434. foreach($temp as $val){
  435. $t = dechex(preg_replace('/[^\d.]/', '', $val));
  436. if(strlen($t) < 2) $t = '0'.$t;
  437. $rgb[] = $t;
  438. }
  439. return '#'.implode('', $rgb);
  440. }
  441. /**
  442. * get transparency from rgba
  443. * @since: 5.0
  444. */
  445. public static function get_trans_from_rgba($rgba, $in_percent = false){
  446. if(strtolower($rgba) == 'transparent') return 100;
  447. $temp = explode(',', $rgba);
  448. if(count($temp) == 4){
  449. return ($in_percent) ? preg_replace('/[^\d.]/', '', $temp[3]) : preg_replace('/[^\d.]/', "", $temp[3]) * 100;
  450. }
  451. return 100;
  452. }
  453. public static function get_responsive_size($slider){
  454. $operations = new RevSliderOperations();
  455. $arrValues = $operations->getGeneralSettingsValues();
  456. $enable_custom_size_notebook = $slider->slider->getParam('enable_custom_size_notebook','off');
  457. $enable_custom_size_tablet = $slider->slider->getParam('enable_custom_size_tablet','off');
  458. $enable_custom_size_iphone = $slider->slider->getParam('enable_custom_size_iphone','off');
  459. $adv_resp_sizes = ($enable_custom_size_notebook == 'on' || $enable_custom_size_tablet == 'on' || $enable_custom_size_iphone == 'on') ? true : false;
  460. if($adv_resp_sizes == true){
  461. $width = $slider->slider->getParam("width", 1240, RevSlider::FORCE_NUMERIC);
  462. $def = $width;
  463. $width .= ',';
  464. if($enable_custom_size_notebook == 'on'){
  465. $width.= $slider->slider->getParam("width_notebook", 1024, RevSlider::FORCE_NUMERIC);
  466. $def = $slider->slider->getParam("width_notebook", 1024, RevSlider::FORCE_NUMERIC);
  467. }else{
  468. $width.= $def;
  469. }
  470. $width.= ',';
  471. if($enable_custom_size_tablet == 'on'){
  472. $width.= $slider->slider->getParam("width_tablet", 778, RevSlider::FORCE_NUMERIC);
  473. $def = $slider->slider->getParam("width_tablet", 778, RevSlider::FORCE_NUMERIC);
  474. }else{
  475. $width.= $def;
  476. }
  477. $width.= ',';
  478. if($enable_custom_size_iphone == 'on'){
  479. $width.= $slider->slider->getParam("width_mobile", 480, RevSlider::FORCE_NUMERIC);
  480. $def = $slider->slider->getParam("width_mobile", 480, RevSlider::FORCE_NUMERIC);
  481. }else{
  482. $width.= $def;
  483. }
  484. $height = $slider->slider->getParam("height", 1240, RevSlider::FORCE_NUMERIC);
  485. $def = $height;
  486. $height .= ',';
  487. if($enable_custom_size_notebook == 'on'){
  488. $height.= $slider->slider->getParam("height_notebook", 1024, RevSlider::FORCE_NUMERIC);
  489. $def = $slider->slider->getParam("height_notebook", 1024, RevSlider::FORCE_NUMERIC);
  490. }else{
  491. $height.= $def;
  492. }
  493. $height.= ',';
  494. if($enable_custom_size_tablet == 'on'){
  495. $height.= $slider->slider->getParam("height_tablet", 778, RevSlider::FORCE_NUMERIC);
  496. $def = $slider->slider->getParam("height_tablet", 778, RevSlider::FORCE_NUMERIC);
  497. }else{
  498. $height.= $def;
  499. }
  500. $height.= ',';
  501. if($enable_custom_size_iphone == 'on'){
  502. $height.= $slider->slider->getParam("height_mobile", 480, RevSlider::FORCE_NUMERIC);
  503. $def = $slider->slider->getParam("height_mobile", 480, RevSlider::FORCE_NUMERIC);
  504. }else{
  505. $height.= $def;
  506. }
  507. $responsive = (isset($arrValues['width'])) ? $arrValues['width'] : '1240';
  508. $def = (isset($arrValues['width'])) ? $arrValues['width'] : '1240';
  509. $responsive.= ',';
  510. if($enable_custom_size_notebook == 'on'){
  511. $responsive.= (isset($arrValues['width_notebook'])) ? $arrValues['width_notebook'] : '1024';
  512. $def = (isset($arrValues['width_notebook'])) ? $arrValues['width_notebook'] : '1024';
  513. }else{
  514. $responsive.= $def;
  515. }
  516. $responsive.= ',';
  517. if($enable_custom_size_tablet == 'on'){
  518. $responsive.= (isset($arrValues['width_tablet'])) ? $arrValues['width_tablet'] : '778';
  519. $def = (isset($arrValues['width_tablet'])) ? $arrValues['width_tablet'] : '778';
  520. }else{
  521. $responsive.= $def;
  522. }
  523. $responsive.= ',';
  524. if($enable_custom_size_iphone == 'on'){
  525. $responsive.= (isset($arrValues['width_mobile'])) ? $arrValues['width_mobile'] : '480';
  526. $def = (isset($arrValues['width_mobile'])) ? $arrValues['width_mobile'] : '480';
  527. }else{
  528. $responsive.= $def;
  529. }
  530. return array(
  531. 'level' => $responsive,
  532. 'height' => $height,
  533. 'width' => $width
  534. );
  535. }else{
  536. $responsive = (isset($arrValues['width'])) ? $arrValues['width'] : '1240';
  537. $def = (isset($arrValues['width'])) ? $arrValues['width'] : '1240';
  538. $responsive.= ',';
  539. $responsive.= (isset($arrValues['width_notebook'])) ? $arrValues['width_notebook'] : '1024';
  540. $responsive.= ',';
  541. $responsive.= (isset($arrValues['width_tablet'])) ? $arrValues['width_tablet'] : '778';
  542. $responsive.= ',';
  543. $responsive.= (isset($arrValues['width_mobile'])) ? $arrValues['width_mobile'] : '480';
  544. return array(
  545. 'visibilitylevel' => $responsive,
  546. 'height' => $slider->slider->getParam("height", "868", RevSlider::FORCE_NUMERIC),
  547. 'width' => $slider->slider->getParam("width", "1240", RevSlider::FORCE_NUMERIC)
  548. );
  549. }
  550. }
  551. }
  552. if(!function_exists("dmp")){
  553. function dmp($str){
  554. echo "<div align='left'>";
  555. echo "<pre>";
  556. print_r($str);
  557. echo "</pre>";
  558. echo "</div>";
  559. }
  560. }
  561. /**
  562. * old classname extends new one (old classnames will be obsolete soon)
  563. * @since: 5.0
  564. **/
  565. class UniteFunctionsRev extends RevSliderFunctions {}
  566. ?>