PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/libero/rslib/customize/customize_style.php

https://gitlab.com/meixnertobias/thaimaidaiproductionwp
PHP | 521 lines | 479 code | 23 blank | 19 comment | 161 complexity | c845d5eb1cde6552aa0522a78b0c4c0a MD5 | raw file
  1. <?php
  2. function rst_customizer_css() {
  3. ?>
  4. <style type="text/css" id="customize-css">
  5. <?php
  6. foreach( rs::$customize as $tab ) {
  7. if( $tab['controls'] ) {
  8. foreach( $tab['controls'] as $key=>$control ) {
  9. if( isset($control['css']) && get_theme_mod( $control['name'] ) ) {
  10. echo str_replace( '$value', get_theme_mod( $control['name'] ), $control['css'] );
  11. }
  12. }
  13. }
  14. }
  15. ?>
  16. </style>
  17. <?php
  18. }
  19. add_action( 'wp_head', 'rst_customizer_css' );
  20. function rstCompressCSS( $minify )
  21. {
  22. /* remove comments */
  23. $minify = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $minify );
  24. /* remove tabs, spaces, newlines, etc. */
  25. $minify = str_replace( array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $minify );
  26. return $minify;
  27. }
  28. function rstGeneralCSS($values = array(), $write_file = false, $preview = false){
  29. global $array_google_fonts_as_key;
  30. global $rs_global_google_font_family;
  31. $webSafeFonts_array = array(
  32. 'Arial, Helvetica, sans-serif',
  33. '"Arial Black", Gadget, sans-serif',
  34. '"Comic Sans MS", cursive, sans-serif',
  35. '"Courier New", Courier, monospace',
  36. 'Georgia, serif',
  37. 'Impact, Charcoal, sans-serif',
  38. '"Lucida Console", Monaco, monospace',
  39. '"Lucida Sans Unicode", "Lucida Grande", sans-serif',
  40. '"Palatino Linotype", "Book Antiqua", Palatino, serif',
  41. 'Tahoma, Geneva, sans-serif',
  42. '"Times New Roman", Times, serif',
  43. '"Trebuchet MS", Helvetica, sans-serif',
  44. 'Verdana, Geneva, sans-serif',
  45. );
  46. $string = '';
  47. foreach($values as $key => $items){
  48. $string .= $key;
  49. $string .= '{';
  50. if(is_array($items)){
  51. if(isset($items['font-family']) && $items['font-family'] != NULL){
  52. $string .= 'font-family:'.$items['font-family'].';';
  53. }
  54. if(isset($items['font-size']) && $items['font-size'] != NULL){
  55. $string .= 'font-size:'.$items['font-size'].';';
  56. }
  57. if(isset($items['font-weight']) && $items['font-weight'] != NULL){
  58. $string .= 'font-weight:'.$items['font-weight'].';';
  59. }
  60. if(isset($items['font-style']) && $items['font-style'] != NULL){
  61. $string .= 'font-style:'.$items['font-style'].';';
  62. }
  63. if(isset($items['line-height']) && $items['line-height'] != NULL){
  64. $string .= 'line-height:'.$items['line-height'].';';
  65. }
  66. if(isset($items['letter-spacing']) && $items['letter-spacing'] != NULL){
  67. $string .= 'letter-spacing:'.$items['letter-spacing'].';';
  68. }
  69. if(isset($items['text-transform']) && $items['text-transform'] != NULL){
  70. $string .= 'text-transform:'.$items['text-transform'].';';
  71. }
  72. //Backgorund
  73. if(isset($items['background-color']) && $items['background-color'] != NULL){
  74. $string .= 'background-color:'.$items['background-color'].';';
  75. }
  76. if(isset($items['background-image']) && $items['background-image'] != NULL){
  77. $string .= 'background-image: url("'.$items['background-image'].'");';
  78. }
  79. if(isset($items['background-repeat']) && $items['background-repeat'] != NULL){
  80. $string .= 'background-repeat: '.$items['background-repeat'].';';
  81. }
  82. if(isset($items['background-size']) && $items['background-size'] != NULL){
  83. $string .= 'background-size: '.$items['background-size'].';';
  84. }
  85. if(isset($items['background-position-vertical']) || isset($items['background-position-horizontal'])){
  86. $position = ( isset($items['background-position-vertical']) && !empty($items['background-position-vertical']) ) ? $items['background-position-vertical'] : 'top';
  87. $position_after = ( isset($items['background-position-horizontal'])&& !empty($items['background-position-horizontal']) ) ? $items['background-position-horizontal'] : 'left';
  88. $position .= ' '.$position_after;
  89. $string .= 'background-position: '. $position .';';
  90. }
  91. if(isset($items['background-attachment']) && $items['background-attachment'] != NULL){
  92. $string .= 'background-attachment: '.$items['background-attachment'].';';
  93. }
  94. if($preview){
  95. //enqueue google font
  96. if ( isset($items['font-family']) && (!in_array($items['font-family'], $webSafeFonts_array))) {
  97. // Get the weight
  98. $variants = array();
  99. if( isset($items['font-weight']) ) {
  100. $variant_prev = $items['font-weight'];
  101. if ( $variant_prev == 'normal' ) {
  102. $variants[] = '400';
  103. $variants[] = '400italic';
  104. } else if ( $variant_prev == 'bold' ) {
  105. $variants[] = '500';
  106. $variants[] = '500italic';
  107. } else if ( $variant_prev == 'bolder' ) {
  108. $variants[] = '800';
  109. $variants[] = '800italic';
  110. } else if ( $variant_prev == 'lighter' ) {
  111. $variants[] = '100';
  112. $variants[] = '100italic';
  113. }
  114. }
  115. $variants[] = '400';
  116. $variants = array_unique( $variants );
  117. $rs_global_google_font_family[] = sprintf("%s:%s",str_replace( ' ', '+', $items['font-family'] ),implode( ',', $variants ));
  118. }
  119. }
  120. }
  121. $string .= '}';
  122. }
  123. $string_out = rstCompressCSS($string);
  124. if($preview){
  125. /**
  126. * Enqueue Google Fonts.
  127. */
  128. if(is_array($rs_global_google_font_family) && count($rs_global_google_font_family)){
  129. $rs_global_google_font_family_out = implode('|',$rs_global_google_font_family);
  130. $query_args = array(
  131. 'family' => $rs_global_google_font_family_out
  132. );
  133. wp_register_style( 'rst_custom_enqueue_google_font', add_query_arg( $query_args, "//fonts.googleapis.com/css" ), array(), null );
  134. wp_enqueue_style( 'rst_custom_enqueue_google_font' );
  135. }
  136. }
  137. else{
  138. if($write_file){
  139. rst_WriteCSS( $string_out, RS_LIB_PATH . "/customize/rst-writerCSS.min.css");
  140. }
  141. else{
  142. rst_WriteCSS( $string_out, RS_LIB_PATH . "/customize/rst-writerCSS-customize.min.css");
  143. }
  144. add_action( 'wp_enqueue_scripts', 'rst_enqueue_style_for_front_end_customize', 9999 );
  145. }
  146. }
  147. function rstCheckFileExistAndAvailable($cssFilename){
  148. require_once(ABSPATH . 'wp-admin/includes/file.php');
  149. WP_Filesystem();
  150. global $wp_filesystem;
  151. // Verify that we can create the file
  152. if ( $wp_filesystem->exists( $cssFilename ) ) {
  153. if ( ! $wp_filesystem->is_writable( $cssFilename ) ) {
  154. return false;
  155. }
  156. if ( ! $wp_filesystem->is_readable( $cssFilename ) ) {
  157. return false;
  158. }
  159. }
  160. // Verify directory
  161. if ( ! $wp_filesystem->is_dir( dirname( $cssFilename ) ) ) {
  162. return false;
  163. }
  164. if ( ! $wp_filesystem->is_writable( dirname( $cssFilename ) ) ) {
  165. return false;
  166. }
  167. return true;
  168. }
  169. function rst_enqueue_style_for_front_end_customize(){
  170. if(!file_exists(RS_LIB_PATH . "/customize/rst-writerCSS-customize.min.css")){
  171. rst_customizer_css_render_file(false);
  172. }
  173. wp_register_style( 'rst-font-for-frontend-customize', RS_LIB_URL . "/customize/rst-writerCSS-customize.min.css", array(), date("Ymdhis"));
  174. wp_enqueue_style( 'rst-font-for-frontend-customize' );
  175. }
  176. function rst_enqueue_style_for_front_end(){
  177. if(!file_exists(RS_LIB_PATH . "/customize/rst-writerCSS.min.css")){
  178. rst_customizer_css_render_file();
  179. }
  180. wp_register_style( 'rst-font-for-frontend', RS_LIB_URL . "/customize/rst-writerCSS.min.css", array(), date("Ymdhis"));
  181. wp_enqueue_style( 'rst-font-for-frontend' );
  182. }
  183. add_action( 'wp_enqueue_scripts', 'rst_enqueue_style_for_front_end',9999 );
  184. function rst_WriteCSS( $parsedCSS, $cssFilename ) {
  185. if(rstCheckFileExistAndAvailable($cssFilename)){
  186. // Write our CSS
  187. global $wp_filesystem;
  188. return $wp_filesystem->put_contents( $cssFilename, $parsedCSS, 0644 );
  189. }
  190. }
  191. function rst_customizer_css_render($write_file = false, $preview = false) {
  192. $controlCSS = array();
  193. foreach( rs::$customize as $tab ) {
  194. if( $tab['controls'] ) {
  195. foreach( $tab['controls'] as $key=>$control ) {
  196. if(
  197. ( $control['type'] == 'font' && isset( $control['css_selector'] ) ) ||
  198. ( $control['type'] == 'rsbackground' && isset( $control['css_selector'] ) )
  199. )
  200. {
  201. if( get_theme_mod( $control['name'] ) ) {
  202. $value = is_array(get_theme_mod( $control['name'] )) ? get_theme_mod( $control['name'] ) : maybe_unserialize(get_theme_mod( $control['name'] ));
  203. if( $value ) {
  204. if( isset( $controlCSS[$control['css_selector']] ) && is_array( array_merge($controlCSS[$control['css_selector']]) ) ) {
  205. $controlCSS[$control['css_selector']] = array_merge($controlCSS[$control['css_selector']], $value);
  206. }
  207. else {
  208. $controlCSS[$control['css_selector']] = $value;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. rstGeneralCSS($controlCSS, $write_file, $preview);
  217. }
  218. //render preview css
  219. function rst_customizer_css_render_call(){
  220. rst_customizer_css_render(false,false);
  221. }
  222. add_action( 'customize_preview_init', 'rst_customizer_css_render_call' );
  223. //render file
  224. function rst_customizer_css_render_file(){
  225. rst_customizer_css_render(true,false);
  226. }
  227. add_action( 'customize_save_after', 'rst_customizer_css_render_file' );
  228. //get google font
  229. function rst_customizer_css_render_google_fonts(){
  230. rst_customizer_css_render(false,true);
  231. }
  232. add_action( 'wp_enqueue_scripts', 'rst_customizer_css_render_google_fonts' );
  233. //Conditional logic
  234. function rstCusConvertRules($rules, $remove_null_rule = true){
  235. $data = array();
  236. foreach($rules as $key=>$value){
  237. if(is_string($key)){
  238. if($remove_null_rule && $value === null){
  239. continue;
  240. }
  241. $logic = array();
  242. if(strpos($key, ':not')){
  243. $key = trim(str_replace(':not', '', $key));
  244. $logic['not'] = ((string)$value);
  245. }
  246. else{
  247. $logic['equal'] = ((string)$value);
  248. }
  249. if(strpos($key, ':i')){
  250. $key = trim(str_replace(':i', '', $key));
  251. $logic['i'] = true;
  252. }
  253. $data[$key] = isset($data[$key]) ? array_merge($logic, $data[$key]) : $logic;
  254. }
  255. }
  256. return $data;
  257. }
  258. function rst_customizer_js() {
  259. ?>
  260. <script type="text/javascript" id="rst-customizer-js">
  261. jQuery(document).ready(function($){
  262. <?php
  263. $array_control_type = array();
  264. foreach( rs::$customize as $tab ) {
  265. if( $tab['controls'] ) {
  266. foreach( $tab['controls'] as $key=>$control ) {
  267. $array_control_type[$control['name']] = $control['type'];
  268. }
  269. }
  270. }
  271. foreach( rs::$customize as $tab ) {
  272. if( $tab['controls'] ) {
  273. foreach( $tab['controls'] as $key=>$control ) {
  274. if( isset($control['conditional_logic']) && ( $control['name'] ) ) {
  275. $content_out = array();
  276. foreach(rstCusConvertRules($control['conditional_logic']['items']) as $key_logic => $value_logic){
  277. if(is_array($value_logic)){
  278. $check_to_lower = false;
  279. $value_logics = current($value_logic);
  280. if(isset($value_logic["i"])){
  281. $value_logics = strtolower($value_logics);
  282. $check_to_lower = true;
  283. }
  284. if(isset($value_logic['not'])){
  285. $comparse = '!=';
  286. }
  287. else {
  288. $comparse = '==';
  289. }
  290. $value_logics_or_and = str_replace('&','|',$value_logics);
  291. $array_value_logics_or_and = explode('|',$value_logics_or_and);
  292. $value_logics_or = explode('|',$value_logics);
  293. $value_logics_or_out = array();
  294. foreach($value_logics_or as $value_out){
  295. if(in_array($value_out,$array_value_logics_or_and)){
  296. $value_logics_or_out[] = $value_out;
  297. }
  298. }
  299. $value_logics_and = explode('&',$value_logics);
  300. $value_logics_and_out = array();
  301. foreach($value_logics_and as $value_out_and){
  302. if(in_array($value_out_and,$array_value_logics_or_and)){
  303. $value_logics_and_out[] = $value_out_and;
  304. }
  305. }
  306. if(count($array_value_logics_or_and) == 1){
  307. $value_logics_or_out = $array_value_logics_or_and;
  308. $value_logics_and_out = array();
  309. }
  310. mb_internal_encoding('UTF-8');
  311. mb_http_output('UTF-8');
  312. mb_http_input('UTF-8');
  313. mb_language('uni');
  314. mb_regex_encoding('UTF-8');
  315. ob_start("mb_output_handler");
  316. if($array_control_type[$key_logic] == 'color'){
  317. ?>
  318. (
  319. <?php if(is_array($value_logics_or_out)) foreach($value_logics_or_out as $key_items_out => $value_logics_or_out_items ){ ?>
  320. <?php if($key_items_out != 0) {?>
  321. ||
  322. <?php } ?>
  323. (
  324. jQuery('#customize-control-settings-<?php echo esc_attr($control['name']); ?>').find('.color-picker-hex.wp-color-picker').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> '<?php printf("%s",$value_logics_or_out_items); ?>'
  325. )
  326. <?php } ?>
  327. <?php if(is_array($value_logics_and_out)) foreach($value_logics_and_out as $key_items_and_out => $value_logics_and_out_items ){ ?>
  328. <?php if($key_items_and_out != 0 || (count($value_logics_or_out) != 0 )) {?>
  329. &&
  330. <?php } ?>
  331. (
  332. jQuery('#customize-control-settings-<?php echo esc_attr($control['name']); ?>').find('.color-picker-hex.wp-color-picker').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> '<?php printf("%s",$value_logics_and_out_items); ?>'
  333. )
  334. <?php } ?>
  335. )
  336. <?php
  337. }
  338. elseif($array_control_type[$key_logic] == 'image'){
  339. ?>
  340. (
  341. <?php if(is_array($value_logics_or_out)) foreach($value_logics_or_out as $key_items_out => $value_logics_or_out_items ){ ?>
  342. <?php if($key_items_out != 0) {?>
  343. ||
  344. <?php } ?>
  345. (
  346. jQuery('#customize-control-settings-<?php echo esc_attr($control['name']); ?>.customize-control-image').find('img').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> '<?php printf("%s",$value_logics_or_out_items); ?>'
  347. )
  348. <?php } ?>
  349. <?php if(is_array($value_logics_and_out)) foreach($value_logics_and_out as $key_items_and_out => $value_logics_and_out_items ){ ?>
  350. <?php if($key_items_and_out != 0 || (count($value_logics_or_out) != 0 )) {?>
  351. &&
  352. <?php } ?>
  353. (
  354. jQuery('#customize-control-settings-<?php echo esc_attr($control['name']); ?>.customize-control-image').find('img').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> '<?php printf("%s",$value_logics_and_out_items); ?>'
  355. )
  356. <?php } ?>
  357. )
  358. <?php
  359. }
  360. elseif($array_control_type[$key_logic] == 'radio-image'){
  361. ?>
  362. (
  363. <?php if(is_array($value_logics_or_out)) foreach($value_logics_or_out as $key_items_out => $value_logics_or_out_items ){ ?>
  364. <?php if($key_items_out != 0) {?>
  365. ||
  366. <?php } ?>
  367. (
  368. jQuery('*[data-customize-setting-link="<?php echo esc_attr($key_logic); ?>"]:checked').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> '<?php printf("%s",$value_logics_or_out_items); ?>'
  369. )
  370. <?php } ?>
  371. <?php if(is_array($value_logics_and_out)) foreach($value_logics_and_out as $key_items_and_out => $value_logics_and_out_items ){ ?>
  372. <?php if($key_items_and_out != 0 || count($value_logics_or_out) != 0 ) {?>
  373. &&
  374. <?php
  375. } ?>
  376. (
  377. jQuery('*[data-customize-setting-link="<?php echo esc_attr($key_logic); ?>"]:checked').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> '<?php printf("%s",$value_logics_and_out_items); ?>'
  378. )
  379. <?php } ?>
  380. )
  381. <?php
  382. }
  383. // js for checkbok
  384. elseif($array_control_type[$key_logic] == 'checkbox'){
  385. $comparse = ($comparse == '==') ? '!=' : '==';
  386. ?>
  387. (
  388. <?php if(is_array($value_logics_or_out)) foreach($value_logics_or_out as $key_items_out => $value_logics_or_out_items ){ ?>
  389. <?php if($key_items_out != 0) {?>
  390. ||
  391. <?php } ?>
  392. (
  393. jQuery('*[data-customize-setting-link="<?php echo esc_attr($key_logic); ?>"]:checked').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> undefined
  394. )
  395. <?php } ?>
  396. <?php if(is_array($value_logics_and_out)) foreach($value_logics_and_out as $key_items_and_out => $value_logics_and_out_items ){ ?>
  397. <?php if($key_items_and_out != 0 || count($value_logics_or_out) != 0 ) {?>
  398. &&
  399. <?php
  400. } ?>
  401. (
  402. jQuery('*[data-customize-setting-link="<?php echo esc_attr($key_logic); ?>"]:checked').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> undefined
  403. )
  404. <?php } ?>
  405. )
  406. <?php
  407. }
  408. else{
  409. ?>
  410. (
  411. <?php if(is_array($value_logics_or_out)) foreach($value_logics_or_out as $key_items_out => $value_logics_or_out_items ){ ?>
  412. <?php if($key_items_out != 0) {?>
  413. ||
  414. <?php } ?>
  415. (
  416. jQuery('*[data-customize-setting-link="<?php echo esc_attr($key_logic); ?>"]').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> '<?php printf("%s",$value_logics_or_out_items); ?>'
  417. )
  418. <?php } ?>
  419. <?php if(is_array($value_logics_and_out)) foreach($value_logics_and_out as $key_items_and_out => $value_logics_and_out_items ){ ?>
  420. <?php if($key_items_and_out != 0 || count($value_logics_or_out) != 0 ) {?>
  421. &&
  422. <?php
  423. } ?>
  424. (
  425. jQuery('*[data-customize-setting-link="<?php echo esc_attr($key_logic); ?>"]').val()<?php if($check_to_lower){ ?>.toLowerCase() <?php } printf("%s",$comparse); ?> '<?php printf("%s",$value_logics_and_out_items); ?>'
  426. )
  427. <?php } ?>
  428. )
  429. <?php
  430. }
  431. }
  432. $content_out[$key_logic] = ob_get_contents();
  433. ob_clean();
  434. ob_end_flush();
  435. }
  436. $out_writer = '';
  437. // relation == and
  438. if(isset($control['conditional_logic']['relation']) || (isset($control['conditional_logic']['relation']) && $control['conditional_logic']['relation'] === 'and') ){
  439. $out_writer = implode(' && ',$content_out);
  440. $out_writer = str_replace('&quot;','"',$out_writer);
  441. $out_writer = str_replace('&#039;','"',$out_writer);
  442. }
  443. // relation == or
  444. elseif(!isset($control['conditional_logic']['relation']) || $control['conditional_logic']['relation'] === 'or'){
  445. $out_writer = implode(' || ',$content_out);
  446. $out_writer = str_replace('&quot;','"',$out_writer);
  447. $out_writer = str_replace('&#039;','"',$out_writer);
  448. }
  449. foreach(rstCusConvertRules($control['conditional_logic']['items']) as $key_logic => $value_logic){
  450. ?>
  451. if(<?php echo ($out_writer); ?>)
  452. {
  453. jQuery('*[data-customize-setting-link="<?php echo esc_attr($control['name']); ?>"]').show().removeClass('important-hide');
  454. jQuery('#customize-control-settings-<?php echo esc_attr($control['name']); ?>').show().removeClass('important-hide');
  455. }
  456. else{
  457. jQuery('*[data-customize-setting-link="<?php echo esc_attr($control['name']); ?>"]').hide().addClass('important-hide');
  458. jQuery('#customize-control-settings-<?php echo esc_attr($control['name']); ?>').hide().addClass('important-hide');
  459. }
  460. jQuery('*[data-customize-setting-link="<?php echo esc_attr($key_logic); ?>"]').live('change',function(){
  461. if(<?php echo ($out_writer); ?>)
  462. {
  463. jQuery('*[data-customize-setting-link="<?php echo esc_attr($control['name']); ?>"]').show().removeClass('important-hide');
  464. jQuery('#customize-control-settings-<?php echo esc_attr($control['name']); ?>').show().removeClass('important-hide');
  465. }
  466. else{
  467. jQuery('*[data-customize-setting-link="<?php echo esc_attr($control['name']); ?>"]').hide().addClass('important-hide');
  468. jQuery('#customize-control-settings-<?php echo esc_attr($control['name']); ?>').hide().addClass('important-hide');
  469. }
  470. });
  471. <?php
  472. }
  473. }
  474. }
  475. }
  476. }
  477. ?>
  478. jQuery('.customize-control-radio-image label').each(function(){
  479. if(jQuery(this).find('img').hasClass('item-checked')){
  480. jQuery(this).find('input').trigger('click');
  481. }
  482. });
  483. jQuery(document).on('click', '#accordion-panel-widgets .accordion-section-title', function(){
  484. rs.helpers.rebuildControls('#accordion-panel-widgets');
  485. });
  486. });
  487. </script>
  488. <style type="text/css">
  489. #customize-theme-controls .accordion-section-content {
  490. background: #fff;
  491. }
  492. .important-hide {
  493. display: none!important;
  494. }
  495. </style>
  496. <?php
  497. }
  498. add_action( 'customize_controls_print_footer_scripts', 'rst_customizer_js' );