PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/revslider/includes/framework/cssparser.class.php

https://gitlab.com/webkod3r/tripolis
PHP | 587 lines | 527 code | 33 blank | 27 comment | 49 complexity | b6ff7813a3774ba6a66f5deb58eec5ac MD5 | raw file
  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 RevSliderCssParser{
  9. private $cssContent;
  10. public function __construct(){
  11. }
  12. /**
  13. *
  14. * init the parser, set css content
  15. */
  16. public function initContent($cssContent){
  17. $this->cssContent = $cssContent;
  18. }
  19. /**
  20. *
  21. * get array of slide classes, between two sections.
  22. */
  23. public function getArrClasses($startText = "",$endText="",$explodeonspace=false){
  24. $content = $this->cssContent;
  25. //trim from top
  26. if(!empty($startText)){
  27. $posStart = strpos($content, $startText);
  28. if($posStart !== false)
  29. $content = substr($content, $posStart,strlen($content)-$posStart);
  30. }
  31. //trim from bottom
  32. if(!empty($endText)){
  33. $posEnd = strpos($content, $endText);
  34. if($posEnd !== false)
  35. $content = substr($content,0,$posEnd);
  36. }
  37. //get styles
  38. $lines = explode("\n",$content);
  39. $arrClasses = array();
  40. foreach($lines as $key=>$line){
  41. $line = trim($line);
  42. if(strpos($line, "{") === false)
  43. continue;
  44. //skip unnessasary links
  45. if(strpos($line, ".caption a") !== false)
  46. continue;
  47. if(strpos($line, ".tp-caption a") !== false)
  48. continue;
  49. //get style out of the line
  50. $class = str_replace("{", "", $line);
  51. $class = trim($class);
  52. //skip captions like this: .tp-caption.imageclass img
  53. if(strpos($class," ") !== false){
  54. if(!$explodeonspace){
  55. continue;
  56. }else{
  57. $class = explode(',', $class);
  58. $class = $class[0];
  59. }
  60. }
  61. //skip captions like this: .tp-caption.imageclass:hover, :before, :after
  62. if(strpos($class,":") !== false)
  63. continue;
  64. $class = str_replace(".caption.", ".", $class);
  65. $class = str_replace(".tp-caption.", ".", $class);
  66. $class = str_replace(".", "", $class);
  67. $class = trim($class);
  68. $arrWords = explode(" ", $class);
  69. $class = $arrWords[count($arrWords)-1];
  70. $class = trim($class);
  71. $arrClasses[] = $class;
  72. }
  73. sort($arrClasses);
  74. return($arrClasses);
  75. }
  76. public static function parseCssToArray($css){
  77. while(strpos($css, '/*') !== false){
  78. if(strpos($css, '*/') === false) return false;
  79. $start = strpos($css, '/*');
  80. $end = strpos($css, '*/') + 2;
  81. $css = str_replace(substr($css, $start, $end - $start), '', $css);
  82. }
  83. //preg_match_all( '/(?ims)([a-z0-9\s\.\:#_\-@]+)\{([^\}]*)\}/', $css, $arr);
  84. preg_match_all( '/(?ims)([a-z0-9\,\s\.\:#_\-@]+)\{([^\}]*)\}/', $css, $arr);
  85. $result = array();
  86. foreach ($arr[0] as $i => $x){
  87. $selector = trim($arr[1][$i]);
  88. if(strpos($selector, '{') !== false || strpos($selector, '}') !== false) return false;
  89. $rules = explode(';', trim($arr[2][$i]));
  90. $result[$selector] = array();
  91. foreach ($rules as $strRule){
  92. if (!empty($strRule)){
  93. $rule = explode(":", $strRule);
  94. if(strpos($rule[0], '{') !== false || strpos($rule[0], '}') !== false || strpos($rule[1], '{') !== false || strpos($rule[1], '}') !== false) return false;
  95. //put back everything but not $rule[0];
  96. $key = trim($rule[0]);
  97. unset($rule[0]);
  98. $values = implode(':', $rule);
  99. $result[$selector][trim($key)] = trim(str_replace("'", '"', $values));
  100. }
  101. }
  102. }
  103. return($result);
  104. }
  105. public static function parseDbArrayToCss($cssArray, $nl = "\n\r"){
  106. $css = '';
  107. $deformations = self::get_deformation_css_tags();
  108. $transparency = array(
  109. 'color' => 'color-transparency',
  110. 'background-color' => 'background-transparency',
  111. 'border-color' => 'border-transparency'
  112. );
  113. $check_parameters = array(
  114. 'border-width' => 'px',
  115. 'border-radius' => 'px',
  116. 'padding' => 'px',
  117. 'font-size' => 'px',
  118. 'line-height' => 'px'
  119. );
  120. foreach($cssArray as $id => $attr){
  121. $stripped = '';
  122. if(strpos($attr['handle'], '.tp-caption') !== false){
  123. $stripped = trim(str_replace('.tp-caption', '', $attr['handle']));
  124. }
  125. $attr['advanced'] = json_decode($attr['advanced'], true);
  126. $styles = json_decode(str_replace("'", '"', $attr['params']), true);
  127. $styles_adv = $attr['advanced']['idle'];
  128. if($stripped == '.News-Title'){
  129. /*echo '<pre>';
  130. print_r($attr);
  131. echo '</pre>';
  132. exit;*/
  133. }
  134. $css.= $attr['handle'];
  135. if(!empty($stripped)) $css.= ', '.$stripped;
  136. $css.= " {".$nl;
  137. if(is_array($styles) || is_array($styles_adv)){
  138. if(is_array($styles)){
  139. foreach($styles as $name => $style){
  140. if(in_array($name, $deformations) && $name !== 'css_cursor') continue;
  141. if(!is_array($name) && isset($transparency[$name])){ //the style can have transparency!
  142. if(isset($styles[$transparency[$name]]) && $style !== 'transparent'){
  143. $style = RevSliderFunctions::hex2rgba($style, $styles[$transparency[$name]] * 100);
  144. }
  145. }
  146. if(!is_array($name) && isset($check_parameters[$name])){
  147. $style = RevSliderFunctions::add_missing_val($style, $check_parameters[$name]);
  148. }
  149. if(is_array($style)) $style = implode(' ', $style);
  150. $ret = self::check_for_modifications($name, $style);
  151. if($ret['name'] == 'cursor' && $ret['style'] == 'auto') continue;
  152. $css.= $ret['name'].':'.$ret['style'].";".$nl;
  153. }
  154. }
  155. if(is_array($styles_adv)){
  156. foreach($styles_adv as $name => $style){
  157. if(in_array($name, $deformations) && $name !== 'css_cursor') continue;
  158. if(is_array($style)) $style = implode(' ', $style);
  159. $ret = self::check_for_modifications($name, $style);
  160. if($ret['name'] == 'cursor' && $ret['style'] == 'auto') continue;
  161. $css.= $ret['name'].':'.$ret['style'].";".$nl;
  162. }
  163. }
  164. }
  165. $css.= "}".$nl.$nl;
  166. //add hover
  167. $setting = json_decode($attr['settings'], true);
  168. if(isset($setting['hover']) && $setting['hover'] == 'true'){
  169. $hover = json_decode(str_replace("'", '"', $attr['hover']), true);
  170. $hover_adv = $attr['advanced']['hover'];
  171. if(is_array($hover) || is_array($hover_adv)){
  172. $css.= $attr['handle'].":hover";
  173. if(!empty($stripped)) $css.= ', '.$stripped.':hover';
  174. $css.= " {".$nl;
  175. if(is_array($hover)){
  176. foreach($hover as $name => $style){
  177. if(in_array($name, $deformations) && $name !== 'css_cursor') continue;
  178. if(!is_array($name) && isset($transparency[$name])){ //the style can have transparency!
  179. if(isset($hover[$transparency[$name]]) && $style !== 'transparent'){
  180. $style = RevSliderFunctions::hex2rgba($style, $hover[$transparency[$name]] * 100);
  181. }
  182. }
  183. if(!is_array($name) && isset($check_parameters[$name])){
  184. $style = RevSliderFunctions::add_missing_val($style, $check_parameters[$name]);
  185. }
  186. if(is_array($style)) $style = implode(' ', $style);
  187. $ret = self::check_for_modifications($name, $style);
  188. if($ret['name'] == 'cursor' && $ret['style'] == 'auto') continue;
  189. $css.= $ret['name'].':'.$ret['style'].";".$nl;
  190. }
  191. }
  192. if(is_array($hover_adv)){
  193. foreach($hover_adv as $name => $style){
  194. if(in_array($name, $deformations) && $name !== 'css_cursor') continue;
  195. if(is_array($style)) $style = implode(' ', $style);
  196. $ret = self::check_for_modifications($name, $style);
  197. if($ret['name'] == 'cursor' && $ret['style'] == 'auto') continue;
  198. $css.= $ret['name'].':'.$ret['style'].";".$nl;
  199. }
  200. }
  201. $css.= "}".$nl.$nl;
  202. }
  203. }
  204. }
  205. return $css;
  206. }
  207. /**
  208. * Check for Modifications like with css_cursor
  209. * @since: 5.1.3
  210. **/
  211. public static function check_for_modifications($name, $style){
  212. if($name == 'css_cursor'){
  213. if($style == 'zoom-in') $style = 'zoom-in; -webkit-zoom-in; cursor: -moz-zoom-in';
  214. if($style == 'zoom-out') $style = 'zoom-out; -webkit-zoom-out; cursor: -moz-zoom-out';
  215. $name = 'cursor';
  216. }
  217. return array('name' => $name, 'style' => $style);
  218. }
  219. public static function parseArrayToCss($cssArray, $nl = "\n\r", $adv = false){
  220. $css = '';
  221. foreach($cssArray as $id => $attr){
  222. $setting = (array)$attr['settings'];
  223. $advanced = (array)$attr['advanced'];
  224. $stripped = '';
  225. if(strpos($attr['handle'], '.tp-caption') !== false){
  226. $stripped = trim(str_replace('.tp-caption', '', $attr['handle']));
  227. }
  228. $styles = (array)$attr['params'];
  229. $css.= $attr['handle'];
  230. if(!empty($stripped)) $css.= ', '.$stripped;
  231. $css.= " {".$nl;
  232. if($adv && isset($advanced['idle'])){
  233. $styles = array_merge($styles, (array)$advanced['idle']);
  234. if(isset($setting['type'])){
  235. $styles['type'] = $setting['type'];
  236. }
  237. }
  238. if(is_array($styles) && !empty($styles)){
  239. foreach($styles as $name => $style){
  240. if($name == 'background-color' && strpos($style, 'rgba') !== false){ //rgb && rgba
  241. $rgb = explode(',', str_replace('rgba', 'rgb', $style));
  242. unset($rgb[count($rgb)-1]);
  243. $rgb = implode(',', $rgb).')';
  244. $css.= $name.':'.$rgb.";".$nl;
  245. }
  246. $style = (is_array($style)) ? implode(' ', $style) : $style;
  247. $css.= $name.':'.$style.";".$nl;
  248. }
  249. }
  250. $css.= "}".$nl.$nl;
  251. //add hover
  252. if(isset($setting['hover']) && $setting['hover'] == 'true'){
  253. $hover = (array)$attr['hover'];
  254. if($adv && isset($advanced['hover'])){
  255. $styles = array_merge($styles, (array)$advanced['hover']);
  256. }
  257. if(is_array($hover)){
  258. $css.= $attr['handle'].":hover";
  259. if(!empty($stripped)) $css.= ', '.$stripped.":hover";
  260. $css.= " {".$nl;
  261. foreach($hover as $name => $style){
  262. if($name == 'background-color' && strpos($style, 'rgba') !== false){ //rgb && rgba
  263. $rgb = explode(',', str_replace('rgba', 'rgb', $style));
  264. unset($rgb[count($rgb)-1]);
  265. $rgb = implode(',', $rgb).')';
  266. $css.= $name.':'.$rgb.";".$nl;
  267. }
  268. $style = (is_array($style)) ? implode(' ', $style) : $style;
  269. $css.= $name.':'.$style.";".$nl;
  270. }
  271. $css.= "}".$nl.$nl;
  272. }
  273. }
  274. }
  275. return $css;
  276. }
  277. public static function parseStaticArrayToCss($cssArray, $nl = "\n"){
  278. $css = '';
  279. foreach($cssArray as $class => $styles){
  280. $css.= $class." {".$nl;
  281. if(is_array($styles) && !empty($styles)){
  282. foreach($styles as $name => $style){
  283. $style = (is_array($style)) ? implode(' ', $style) : $style;
  284. $css.= $name.':'.$style.";".$nl;
  285. }
  286. }
  287. $css.= "}".$nl.$nl;
  288. }
  289. return $css;
  290. }
  291. public static function parseDbArrayToArray($cssArray, $handle = false){
  292. if(!is_array($cssArray) || empty($cssArray)) return false;
  293. foreach($cssArray as $key => $css){
  294. if($handle != false){
  295. if($cssArray[$key]['handle'] == '.tp-caption.'.$handle){
  296. $cssArray[$key]['params'] = json_decode(str_replace("'", '"', $css['params']));
  297. $cssArray[$key]['hover'] = json_decode(str_replace("'", '"', $css['hover']));
  298. $cssArray[$key]['advanced'] = json_decode(str_replace("'", '"', $css['advanced']));
  299. $cssArray[$key]['settings'] = json_decode(str_replace("'", '"', $css['settings']));
  300. return $cssArray[$key];
  301. }else{
  302. unset($cssArray[$key]);
  303. }
  304. }else{
  305. $cssArray[$key]['params'] = json_decode(str_replace("'", '"', $css['params']));
  306. $cssArray[$key]['hover'] = json_decode(str_replace("'", '"', $css['hover']));
  307. $cssArray[$key]['advanced'] = json_decode(str_replace("'", '"', $css['advanced']));
  308. $cssArray[$key]['settings'] = json_decode(str_replace("'", '"', $css['settings']));
  309. }
  310. }
  311. return $cssArray;
  312. }
  313. public static function compress_css($buffer){
  314. /* remove comments */
  315. $buffer = preg_replace("!/\*[^*]*\*+([^/][^*]*\*+)*/!", "", $buffer) ;
  316. /* remove tabs, spaces, newlines, etc. */
  317. $arr = array("\r\n", "\r", "\n", "\t", " ", " ", " ");
  318. $rep = array("", "", "", "", " ", " ", " ");
  319. $buffer = str_replace($arr, $rep, $buffer);
  320. /* remove whitespaces around {}:, */
  321. $buffer = preg_replace("/\s*([\{\}:,])\s*/", "$1", $buffer);
  322. /* remove last ; */
  323. $buffer = str_replace(';}', "}", $buffer);
  324. return $buffer;
  325. }
  326. /**
  327. * Defines the default CSS Classes, can be given a version number to order them accordingly
  328. * @since: 5.0
  329. **/
  330. public static function default_css_classes(){
  331. $default = array(
  332. '.tp-caption.medium_grey' => '4',
  333. '.tp-caption.small_text' => '4',
  334. '.tp-caption.medium_text' => '4',
  335. '.tp-caption.large_text' => '4',
  336. '.tp-caption.very_large_text' => '4',
  337. '.tp-caption.very_big_white' => '4',
  338. '.tp-caption.very_big_black' => '4',
  339. '.tp-caption.modern_medium_fat' => '4',
  340. '.tp-caption.modern_medium_fat_white' => '4',
  341. '.tp-caption.modern_medium_light' => '4',
  342. '.tp-caption.modern_big_bluebg' => '4',
  343. '.tp-caption.modern_big_redbg' => '4',
  344. '.tp-caption.modern_small_text_dark' => '4',
  345. '.tp-caption.boxshadow' => '4',
  346. '.tp-caption.black' => '4',
  347. '.tp-caption.noshadow' => '4',
  348. '.tp-caption.thinheadline_dark' => '4',
  349. '.tp-caption.thintext_dark' => '4',
  350. '.tp-caption.largeblackbg' => '4',
  351. '.tp-caption.largepinkbg' => '4',
  352. '.tp-caption.largewhitebg' => '4',
  353. '.tp-caption.largegreenbg' => '4',
  354. '.tp-caption.excerpt' => '4',
  355. '.tp-caption.large_bold_grey' => '4',
  356. '.tp-caption.medium_thin_grey' => '4',
  357. '.tp-caption.small_thin_grey' => '4',
  358. '.tp-caption.lightgrey_divider' => '4',
  359. '.tp-caption.large_bold_darkblue' => '4',
  360. '.tp-caption.medium_bg_darkblue' => '4',
  361. '.tp-caption.medium_bold_red' => '4',
  362. '.tp-caption.medium_light_red' => '4',
  363. '.tp-caption.medium_bg_red' => '4',
  364. '.tp-caption.medium_bold_orange' => '4',
  365. '.tp-caption.medium_bg_orange' => '4',
  366. '.tp-caption.grassfloor' => '4',
  367. '.tp-caption.large_bold_white' => '4',
  368. '.tp-caption.medium_light_white' => '4',
  369. '.tp-caption.mediumlarge_light_white' => '4',
  370. '.tp-caption.mediumlarge_light_white_center' => '4',
  371. '.tp-caption.medium_bg_asbestos' => '4',
  372. '.tp-caption.medium_light_black' => '4',
  373. '.tp-caption.large_bold_black' => '4',
  374. '.tp-caption.mediumlarge_light_darkblue' => '4',
  375. '.tp-caption.small_light_white' => '4',
  376. '.tp-caption.roundedimage' => '4',
  377. '.tp-caption.large_bg_black' => '4',
  378. '.tp-caption.mediumwhitebg' => '4',
  379. '.tp-caption.MarkerDisplay' => '5.0',
  380. '.tp-caption.Restaurant-Display' => '5.0',
  381. '.tp-caption.Restaurant-Cursive' => '5.0',
  382. '.tp-caption.Restaurant-ScrollDownText' => '5.0',
  383. '.tp-caption.Restaurant-Description' => '5.0',
  384. '.tp-caption.Restaurant-Price' => '5.0',
  385. '.tp-caption.Restaurant-Menuitem' => '5.0',
  386. '.tp-caption.Furniture-LogoText' => '5.0',
  387. '.tp-caption.Furniture-Plus' => '5.0',
  388. '.tp-caption.Furniture-Title' => '5.0',
  389. '.tp-caption.Furniture-Subtitle' => '5.0',
  390. '.tp-caption.Gym-Display' => '5.0',
  391. '.tp-caption.Gym-Subline' => '5.0',
  392. '.tp-caption.Gym-SmallText' => '5.0',
  393. '.tp-caption.Fashion-SmallText' => '5.0',
  394. '.tp-caption.Fashion-BigDisplay' => '5.0',
  395. '.tp-caption.Fashion-TextBlock' => '5.0',
  396. '.tp-caption.Sports-Display' => '5.0',
  397. '.tp-caption.Sports-DisplayFat' => '5.0',
  398. '.tp-caption.Sports-Subline' => '5.0',
  399. '.tp-caption.Instagram-Caption' => '5.0',
  400. '.tp-caption.News-Title' => '5.0',
  401. '.tp-caption.News-Subtitle' => '5.0',
  402. '.tp-caption.Photography-Display' => '5.0',
  403. '.tp-caption.Photography-Subline' => '5.0',
  404. '.tp-caption.Photography-ImageHover' => '5.0',
  405. '.tp-caption.Photography-Menuitem' => '5.0',
  406. '.tp-caption.Photography-Textblock' => '5.0',
  407. '.tp-caption.Photography-Subline-2' => '5.0',
  408. '.tp-caption.Photography-ImageHover2' => '5.0',
  409. '.tp-caption.WebProduct-Title' => '5.0',
  410. '.tp-caption.WebProduct-SubTitle' => '5.0',
  411. '.tp-caption.WebProduct-Content' => '5.0',
  412. '.tp-caption.WebProduct-Menuitem' => '5.0',
  413. '.tp-caption.WebProduct-Title-Light' => '5.0',
  414. '.tp-caption.WebProduct-SubTitle-Light' => '5.0',
  415. '.tp-caption.WebProduct-Content-Light' => '5.0',
  416. '.tp-caption.FatRounded' => '5.0',
  417. '.tp-caption.NotGeneric-Title' => '5.0',
  418. '.tp-caption.NotGeneric-SubTitle' => '5.0',
  419. '.tp-caption.NotGeneric-CallToAction' => '5.0',
  420. '.tp-caption.NotGeneric-Icon' => '5.0',
  421. '.tp-caption.NotGeneric-Menuitem' => '5.0',
  422. '.tp-caption.MarkerStyle' => '5.0',
  423. '.tp-caption.Gym-Menuitem' => '5.0',
  424. '.tp-caption.Newspaper-Button' => '5.0',
  425. '.tp-caption.Newspaper-Subtitle' => '5.0',
  426. '.tp-caption.Newspaper-Title' => '5.0',
  427. '.tp-caption.Newspaper-Title-Centered' => '5.0',
  428. '.tp-caption.Hero-Button' => '5.0',
  429. '.tp-caption.Video-Title' => '5.0',
  430. '.tp-caption.Video-SubTitle' => '5.0',
  431. '.tp-caption.NotGeneric-Button' => '5.0',
  432. '.tp-caption.NotGeneric-BigButton' => '5.0',
  433. '.tp-caption.WebProduct-Button' => '5.0',
  434. '.tp-caption.Restaurant-Button' => '5.0',
  435. '.tp-caption.Gym-Button' => '5.0',
  436. '.tp-caption.Gym-Button-Light' => '5.0',
  437. '.tp-caption.Sports-Button-Light' => '5.0',
  438. '.tp-caption.Sports-Button-Red' => '5.0',
  439. '.tp-caption.Photography-Button' => '5.0',
  440. '.tp-caption.Newspaper-Button-2' => '5.0'
  441. );
  442. $default = apply_filters('revslider_mod_default_css_handles', $default);
  443. return $default;
  444. }
  445. /**
  446. * Defines the deformation CSS which is not directly usable as pure CSS
  447. * @since: 5.0
  448. **/
  449. public static function get_deformation_css_tags(){
  450. return array(
  451. 'x',
  452. 'y',
  453. 'z',
  454. 'skewx',
  455. 'skewy',
  456. 'scalex',
  457. 'scaley',
  458. 'opacity',
  459. 'xrotate',
  460. 'yrotate',
  461. '2d_rotation',
  462. 'layer_2d_origin_x',
  463. 'layer_2d_origin_y',
  464. '2d_origin_x',
  465. '2d_origin_y',
  466. 'pers',
  467. 'color-transparency',
  468. 'background-transparency',
  469. 'border-transparency',
  470. 'css_cursor',
  471. 'speed',
  472. 'easing',
  473. 'corner_left',
  474. 'corner_right',
  475. 'parallax',
  476. 'type'
  477. );
  478. }
  479. public static function get_captions_sorted(){
  480. $db = new RevSliderDB();
  481. $styles = $db->fetch(RevSliderGlobals::$table_css, '', 'handle ASC');
  482. $arr = array('5.0' => array(), 'Custom' => array(), '4' => array());
  483. foreach($styles as $style){
  484. $setting = json_decode($style['settings'], true);
  485. if(!isset($setting['type'])) $setting['type'] = 'text';
  486. if(array_key_exists('version', $setting) && isset($setting['version'])) $arr[ucfirst($setting['version'])][] = array('label' => trim(str_replace('.tp-caption.', '', $style['handle'])), 'type' => $setting['type']);
  487. }
  488. $sorted = array();
  489. foreach($arr as $version => $class){
  490. foreach($class as $name){
  491. $sorted[] = array('label' => $name['label'], 'version' => $version, 'type' => $name['type']);
  492. }
  493. }
  494. return $sorted;
  495. }
  496. }
  497. /**
  498. * old classname extends new one (old classnames will be obsolete soon)
  499. * @since: 5.0
  500. **/
  501. class UniteCssParserRev extends RevSliderCssParser {}
  502. ?>