/admin/less/sass.php

  1. <?php 
  2. // File Security Check 
  3. if (!defined('ABSPATH')) 
  4. exit; 
  5.  
  6. class WC_Dynamic_Gallery_Less 
  7. public $plugin_name = 'woo_dynamic_gallery'; 
  8. public $css_file_name = 'woo_dynamic_gallery'; 
  9. public $plugin_folder = WOO_DYNAMIC_GALLERY_FOLDER; 
  10. public $plugin_dir = WOO_DYNAMIC_GALLERY_DIR; 
  11.  
  12. /**-----------------------------------------------------------------------------------*/ 
  13. /** Constructor */ 
  14. /**-----------------------------------------------------------------------------------*/ 
  15. public function __construct() 
  16. add_action( $this->plugin_name.'_after_settings_save_reset', array ($this, 'plugin_build_sass') ); 
  17. //add_action( 'wp_enqueue_scripts', array ($this, 'apply_style_css_fontend') , 11 ); 
  18.  
  19. public function apply_style_css_fontend() 
  20. $_upload_dir = wp_upload_dir(); 
  21. if ( file_exists( $_upload_dir['basedir'] . '/sass/' . $this->css_file_name . '.min.css' ) ) { 
  22. wp_enqueue_style( 'a3' . $this->css_file_name, str_replace( array('http:', 'https:'), '', $_upload_dir['baseurl'] ) . '/sass/' . $this->css_file_name . '.min.css', array(), $this->get_css_file_version() ); 
  23.  
  24. public function plugin_build_sass() 
  25. $sass = $this->sass_content_data(); 
  26. $this->plugin_compile_less_mincss( $sass ); 
  27. $this->set_css_file_version(); 
  28. public function custom_filesystem_method( $method = '') { 
  29. return 'direct'; 
  30.  
  31. public function plugin_compile_less_mincss( $sass, $css_file_name = '' ) 
  32. // just filter when compile less file 
  33. add_filter( 'filesystem_method', array( $this, 'custom_filesystem_method' ) ); 
  34.  
  35. $form_url = wp_nonce_url( esc_url( add_query_arg( 'compile-sass', 'true' ) ), 'compile-sass' ); 
  36.  
  37. if ( ! function_exists( 'request_filesystem_credentials' ) ) { 
  38. require_once( ABSPATH . 'wp-admin/includes/file.php' ); 
  39.  
  40. if ( false === ( $creds = request_filesystem_credentials( $form_url, '', false, false, null ) ) ) { 
  41. return true; 
  42.  
  43. if ( ! WP_Filesystem( $creds ) ) { 
  44. // our credentials were no good, ask the user for them again 
  45. request_filesystem_credentials( $form_url, '', true ); 
  46. return true; 
  47.  
  48. global $wp_filesystem; 
  49.  
  50. @ini_set( 'display_errors', false ); 
  51. $_upload_dir = wp_upload_dir(); 
  52. $wp_filesystem->chmod($_upload_dir['basedir'], 0755); 
  53. if (! $wp_filesystem->is_dir($_upload_dir['basedir'] . '/sass')) { 
  54. $wp_filesystem->mkdir($_upload_dir['basedir'] . '/sass', 0755); 
  55. } else { 
  56. $wp_filesystem->chmod($_upload_dir['basedir'] . '/sass', 0755); 
  57.  
  58. if ( trim( $css_file_name ) == '' ) $css_file_name = $this->css_file_name; 
  59.  
  60. if ( $css_file_name == '' ) 
  61. return; 
  62.  
  63. if ( $this->plugin_folder == '' ) 
  64. return; 
  65.  
  66. $filename = $css_file_name; 
  67.  
  68. if (!file_exists($_upload_dir['basedir'] . '/sass/' . $filename . '.less')) { 
  69. $wp_filesystem->put_contents($_upload_dir['basedir'] . '/sass/' . $filename . '.less', '', 0644 ); 
  70. $wp_filesystem->put_contents($_upload_dir['basedir'] . '/sass/' . $filename . '.css', '', 0644); 
  71. $wp_filesystem->put_contents($_upload_dir['basedir'] . '/sass/' . $filename . '.min.css', '', 0644); 
  72.  
  73. $mixins = $this->css_file_name . '_mixins'; 
  74. if( !file_exists( $_upload_dir['basedir'].'/sass/'.$mixins.'.less' ) ) { 
  75. $mixinsless = $this->plugin_dir.'/admin/less/assets/css/mixins.less'; 
  76. $a3rev_mixins_less = $_upload_dir['basedir'].'/sass/'.$mixins.'.less'; 
  77. $wp_filesystem->copy($mixinsless, $a3rev_mixins_less, true ); 
  78.  
  79. $files = array_diff(scandir($_upload_dir['basedir'] . '/sass'), array( 
  80. '.',  
  81. '..' 
  82. )); 
  83. if ($files) { 
  84. foreach ($files as $file) { 
  85. $wp_filesystem->chmod($_upload_dir['basedir'] . '/sass/' . $file, 0644); 
  86.  
  87. $sass_data = ''; 
  88.  
  89. if ($sass != '') { 
  90.  
  91. $sass_data = '@import "'.$mixins.'.less";' . "\n"; 
  92.  
  93. $sass_data .= $sass; 
  94.  
  95. $sass_data = str_replace(':;', ': transparent;', $sass_data); 
  96. $sass_data = str_replace(': ;', ': transparent;', $sass_data); 
  97. $sass_data = str_replace(': !important', ': transparent !important', $sass_data); 
  98. $sass_data = str_replace(':px', ':0px', $sass_data); 
  99. $sass_data = str_replace(': px', ': 0px', $sass_data); 
  100.  
  101. $less_file = $_upload_dir['basedir'] . '/sass/' . $filename . '.less'; 
  102. if (is_writable($less_file)) { 
  103.  
  104. if (!class_exists('Compile_Less_Sass')) 
  105. include( dirname( __FILE__ ) . '/compile_less_sass_class.php'); 
  106. $wp_filesystem->put_contents($less_file, $sass_data, 0644); 
  107. $css_file = $_upload_dir['basedir'] . '/sass/' . $filename . '.css'; 
  108. $css_min_file = $_upload_dir['basedir'] . '/sass/' . $filename . '.min.css'; 
  109. $compile = new Compile_Less_Sass; 
  110. $compile->compileLessFile($less_file, $css_file, $css_min_file); 
  111.  
  112. public function sass_content_data() 
  113. do_action($this->plugin_name . '_get_all_settings'); 
  114.  
  115. ob_start(); 
  116. include( $this->plugin_dir. '/templates/customized_style.php' ); 
  117. $sass = ob_get_clean(); 
  118. $sass = str_replace( '<style>', '', str_replace( '</style>', '', $sass ) ); 
  119. $sass = str_replace( '<style type="text/css">', '', str_replace( '</style>', '', $sass ) ); 
  120.  
  121. // Start Less 
  122. $sass_ext = ''; 
  123.  
  124. $sass_ext = apply_filters( $this->plugin_name.'_build_sass', $sass_ext ); 
  125.  
  126. if ($sass_ext != '') 
  127. $sass .= "\n" . $sass_ext; 
  128.  
  129. return $sass; 
  130.  
  131. public function set_css_file_version( $css_file_name = '' ) { 
  132. if ( trim( $css_file_name ) == '' ) { 
  133. $css_file_name = $this->css_file_name; 
  134.  
  135. if ( $css_file_name == '' ) { 
  136. return false; 
  137.  
  138. update_option( $css_file_name . '_style_version', time() ); 
  139.  
  140. public function get_css_file_version( $css_file_name = '' ) { 
  141. if ( trim( $css_file_name ) == '' ) { 
  142. $css_file_name = $this->css_file_name; 
  143.  
  144. if ( $css_file_name == '' ) { 
  145. return false; 
  146.  
  147. $version_number = get_option( $css_file_name . '_style_version', time() ); 
  148.  
  149. return $version_number; 
  150. global $wc_wc_dynamic_gallery_less; 
  151. $wc_wc_dynamic_gallery_less = new WC_Dynamic_Gallery_Less(); 
  152. ?> 
.