PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/oldpaper/framework/redux-framework-master/ReduxCore/inc/class.redux_sass.php

https://gitlab.com/eita/agencia-consumo-responsavel
PHP | 255 lines | 184 code | 55 blank | 16 comment | 46 complexity | 18adec0f018da4daee9eac0b173f75d9 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. if (!class_exists('reduxSassCompiler')) {
  6. class reduxSassCompiler {
  7. public static $path = array();
  8. public static $import = array();
  9. public static $_do_compile = false;
  10. private static $matrix_file = '';
  11. private static $matrix_key = '';
  12. const SASS_NO_COMPILE = 0;
  13. const SASS_FILE_COMPILE = 1;
  14. const SASS_PAGE_OUTPUT = 2;
  15. private static function is_sass_dir($dir){
  16. if (!is_dir($dir)) {
  17. wp_mkdir_p($dir);
  18. if (is_dir($dir)) {
  19. return true;
  20. } else {
  21. return false;
  22. }
  23. } else {
  24. return true;
  25. }
  26. }
  27. public static function get_current_id_matrix($parent) {
  28. if ($parent->args['sass']['enabled'] && !$parent->args['sass']['page_output']) {
  29. $ids = '';
  30. foreach($parent->options as $id => $opts) {
  31. $ids .= $id . '|';
  32. }
  33. $ids = rtrim($ids,'|');
  34. return $ids;
  35. }
  36. }
  37. public static function get_id_matrix(){
  38. if (!file_exists(self::$matrix_file)) {
  39. $ids = get_option(self::$matrix_key);
  40. } else {
  41. $ids = file_get_contents(self::$matrix_file);
  42. }
  43. return $ids;
  44. }
  45. public static function set_id_matrix($ids) {
  46. $ret = @file_put_contents(self::$matrix_file, $ids);
  47. if ($ret == false) {
  48. return update_option(self::$matrix_key, $ids);
  49. }
  50. }
  51. public static function add_path ($path) {
  52. if (!in_array($path, self::$path)) {
  53. array_push(self::$path, $path);
  54. }
  55. }
  56. public static function add_import($import) {
  57. if (!in_array($import, self::$import)) {
  58. array_push (self::$import, $import);
  59. }
  60. }
  61. public static function is_scss_newer($dir, $filename){
  62. $css_time = filemtime($dir . '/' . $filename . '.css');
  63. $scss_time = filemtime($dir . '/' . $filename . '.scss');
  64. if ($scss_time > $css_time) {
  65. echo 'css: ' . $css_time . '<br>';
  66. echo 'scss: ' . $scss_time . '<br>';
  67. return true;
  68. }
  69. return false;
  70. }
  71. public static function compile_sass($parent) {
  72. if (!empty(self::$path)) {
  73. $do_compile = false;
  74. $as_output = false;
  75. if (!self::is_sass_dir ( ReduxFramework::$_upload_dir . 'sass' )){
  76. $as_output = true;
  77. }
  78. if ($parent->args['sass']['page_output']) {
  79. $as_output = true;
  80. }
  81. $mb = $parent->extensions['metaboxes'];
  82. if (!empty($mb->boxes)) {
  83. $as_output = true;
  84. }
  85. $opt_name = $parent->args['opt_name'];
  86. self::$matrix_file = ReduxFramework::$_upload_dir . 'sass/' . $opt_name . '-id-matrix';
  87. self::$matrix_key = 'redux-sass-' . $opt_name . '-id-matrix';
  88. if (!$as_output) {
  89. $current_ids = self::get_current_id_matrix($parent);
  90. $saved_ids = self::get_id_matrix();
  91. if ($saved_ids == '' || empty($saved_ids)) {
  92. $ret = self::set_id_matrix($current_ids);
  93. $do_compile = true;
  94. } else {
  95. if ($current_ids != $saved_ids) {
  96. logconsole('not the same');
  97. self::set_id_matrix($current_ids);
  98. $do_compile = true;
  99. } else {
  100. logconsole('the same');
  101. }
  102. }
  103. } else {
  104. $do_compile = true;
  105. }
  106. if ($do_compile || self::$_do_compile) {
  107. logconsole('compiler run');
  108. if ( !class_exists( 'scssc' ) && !isset( $GLOBALS['redux_scss_compiler'] ) ) {
  109. $GLOBALS['redux_scss_compiler'] = true;
  110. require( "scssphp/scss.inc.php" );
  111. }
  112. $scss = new scssc();
  113. $scss->setImportPaths( self::$path );
  114. if (!$parent->args['dev_mode']) {
  115. $scss->setFormatter ( "scss_formatter_compressed" );
  116. }
  117. $new_css = '';
  118. foreach (self::$import as $import) {
  119. $new_css .= $scss->compile( $import );
  120. }
  121. unset ($scss);
  122. if ($new_css != '') {
  123. if ($as_output) {
  124. self::css_to_page($opt_name, $new_css);
  125. return self::SASS_PAGE_OUTPUT;
  126. } else {
  127. $css_file = Redux_Helpers::cleanFilePath( ReduxFramework::$_upload_dir . $parent->args['opt_name'] . '-redux.css');
  128. $ret = @file_put_contents($css_file, $new_css);
  129. if ($ret == false) {
  130. self::css_to_page($opt_name, $new_css);
  131. return self::SASS_PAGE_OUTPUT;
  132. }
  133. return self::SASS_FILE_COMPILE;
  134. }
  135. }
  136. } // do_compile
  137. }
  138. return self::SASS_NO_COMPILE;
  139. }
  140. private static function css_to_page($opt_name, $css) {
  141. echo '<style type="text/css" id="redux-' . $opt_name . '">' . $css . '</style>';
  142. }
  143. public static function compile_single_field($parent, $scss_path, $filename) {
  144. echo 'single field compile: ' . $scss_path . ' ' . $filename;
  145. if ( !class_exists( 'scssc' ) && !isset( $GLOBALS['redux_scss_compiler'] ) ) {
  146. $GLOBALS['redux_scss_compiler'] = true;
  147. require( "scssphp/scss.inc.php" );
  148. }
  149. $scss = new scssc();
  150. $scss->setImportPaths( $scss_path );
  151. if (!$parent->args['dev_mode']) {
  152. $scss->setFormatter ( "scss_formatter_compressed" );
  153. }
  154. $new_css = $scss->compile( '@import "' . $filename . '.scss"' );
  155. unset ($scss);
  156. $ret = @file_put_contents($scss_path . '/' . $filename . '.css', $new_css);
  157. }
  158. }
  159. }
  160. if (!function_exists ( 'redux_enqueue_style')) {
  161. /**
  162. * Enqueues style for SASS comnpile or WP enqueue, depending on 'use_sass' arg.
  163. *
  164. * @since 3.3.9
  165. * @access public
  166. * @param string $handle Name of the stylesheet.
  167. * @param string $css_src Path to the stylesheet from the root directory of WordPress. Example: '/css/mystyle.css'.
  168. * @param string $scss_dir Directory path to SCSS file.
  169. * @param array $deps An array of registered style handles this stylesheet depends on. Default empty array.
  170. * @param string $ver String specifying the stylesheet version number, if it has one. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the stylesheet.
  171. * @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print', 'screen', 'tty', or 'tv'.
  172. * @return void
  173. */
  174. function redux_enqueue_style ($parent, $handle, $css_src, $scss_dir, $deps = array(), $ver = '', $media = false){
  175. if ($parent->args['sass']['enabled']) {
  176. //if ($parent->args['dev_mode'] || $parent->args['sass']['page_output']) {
  177. $path_parts = pathinfo($css_src);
  178. $filename = $path_parts['filename'];
  179. //echo $filename . '<br>';
  180. $scss_dir = Redux_Helpers::cleanFilePath($scss_dir);
  181. $scss_dir = untrailingslashit($scss_dir);
  182. $is_diff = reduxSassCompiler::is_scss_newer($scss_dir, $filename);
  183. if ($is_diff) {
  184. reduxSassCompiler::compile_single_field($parent, $scss_dir, $filename);
  185. reduxSassCompiler::$_do_compile = true;
  186. }
  187. reduxSassCompiler::add_path($scss_dir);
  188. reduxSassCompiler::add_import('@import "' . $filename . '.scss"');
  189. //}
  190. } else {
  191. wp_enqueue_style(
  192. $handle,
  193. $css_src,
  194. $deps,
  195. $ver,
  196. $media
  197. );
  198. }
  199. }
  200. }