PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 2.25.0
Block Animations, Motion & Scroll Effects – Ghost Kit v2.25.0
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / classes / class-scss-compiler.php

class-scss-compiler.php in Block Animations, Motion & Scroll Effects – Ghost Kit 2.25.0, at classes/class-scss-compiler.php

198 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Scss Compiler
4 *
5 * @package ghostkit
6 */
7
8 /**
9 * GhostKit_Scss_Compiler
10 */
11 class GhostKit_Scss_Compiler {
12 /**
13 * Arguments for compile scss file
14 *
15 * @var array
16 */
17 private $args;
18
19 /**
20 * Information about folders and name input scss file
21 *
22 * @var array
23 */
24 private $input_file_info;
25
26 /**
27 * Information about folders and name output css file
28 *
29 * @var array
30 */
31 private $output_file_info;
32
33 /**
34 * GhostKit_Scss_Compiler constructor.
35 *
36 * @param array $args - Arguments for compile scss file.
37 */
38 public function __construct( $args = null ) {
39 $this->args = $args;
40
41 if ( $this->validation_of_arguments() ) {
42 $this->input_file_info = pathinfo( $this->args['input_file'] );
43 $this->output_file_info = pathinfo( $this->args['output_file'] );
44 $this->scss_in = $this->get_input_file_contents( $this->args['input_file'] );
45 $is_dir = $this->check_and_create_non_exist_output_folder();
46
47 if (
48 $this->scss_in &&
49 $is_dir &&
50 isset( $this->input_file_info['dirname'] ) &&
51 ! empty( $this->input_file_info['dirname'] )
52 ) {
53 $this->scss();
54 }
55 }
56 }
57
58 /**
59 * Compilation scss and save css output file.
60 *
61 * @return void
62 */
63 private function scss() {
64 require_once ghostkit()->plugin_path . 'vendor/autoload.php';
65
66 $scss = new ScssPhp\ScssPhp\Compiler();
67 $scss->setOutputStyle( ScssPhp\ScssPhp\OutputStyle::COMPRESSED );
68 $scss->addImportPath( $this->input_file_info['dirname'] );
69 $scss->setVariables( $this->args['variables'] );
70
71 // Find all SCSS files and remove unsupported math.div module.
72 $scss_files = array();
73 $di = new RecursiveDirectoryIterator( trailingslashit( $this->input_file_info['dirname'] ), RecursiveDirectoryIterator::SKIP_DOTS );
74 $it = new RecursiveIteratorIterator( $di );
75
76 foreach ( $it as $file ) {
77 if ( pathinfo( $file, PATHINFO_EXTENSION ) === 'scss' ) {
78 $scss_files[] = $file->getPathname();
79 }
80 }
81
82 foreach ( $scss_files as $scss_file_path ) {
83 // phpcs:ignore
84 $file_contents = file_get_contents( $scss_file_path );
85
86 if ( $file_contents ) {
87 // find module include.
88 $file_contents = str_replace( '@use "sass:math";', '', $file_contents );
89 $file_contents = str_replace( '@use "sass:map";', '', $file_contents );
90 $file_contents = str_replace( '@use "sass:list";', '', $file_contents );
91 $file_contents = str_replace( '@use "sass:string";', '', $file_contents );
92
93 // find math.div calls.
94 preg_match_all( '/math\.div(?=\()(?:(?=.*?\((?!.*?\1)(.*\)(?!.*\2).*))(?=.*?\)(?!.*?\2)(.*)).)+?.*?(?=\1)[^(]*(?=\2$)/ms', $file_contents, $file_contents_calls );
95 if ( ! empty( $file_contents_calls[0] ) ) {
96 foreach ( $file_contents_calls[0] as $content ) {
97 $new_file_contents = $content;
98 $new_file_contents = str_replace( 'math.div', '', $new_file_contents );
99 $new_file_contents = preg_replace( '/\,/', ' /', $new_file_contents, 1 );
100
101 $file_contents = str_replace( $content, $new_file_contents, $file_contents );
102 }
103 }
104
105 // find map calls.
106 $file_contents = str_replace( 'map.get(', 'map-get(', $file_contents );
107 $file_contents = str_replace( 'map.values(', 'map-values(', $file_contents );
108
109 // find list calls.
110 $file_contents = str_replace( 'list.index(', 'index(', $file_contents );
111 $file_contents = str_replace( 'list.nth(', 'nth(', $file_contents );
112
113 // find string calls.
114 $file_contents = str_replace( 'string.index(', 'str-index(', $file_contents );
115
116 // phpcs:ignore
117 file_put_contents( $scss_file_path, $file_contents );
118 }
119 }
120
121 // Compile scss.
122 $result = $scss->compile( $this->scss_in );
123
124 // Prepare RTL.
125 $parser = new Sabberworm\CSS\Parser( $result );
126 $tree = $parser->parse();
127 $rtlcss = new PrestaShop\RtlCss\RtlCss( $tree );
128 $rtlcss->flip();
129
130 // phpcs:ignore
131 file_put_contents( trailingslashit( $this->output_file_info['dirname'] ) . '/' . $this->output_file_info['basename'], $result );
132
133 // phpcs:ignore
134 file_put_contents(
135 trailingslashit( $this->output_file_info['dirname'] ) . '/' . $this->input_file_info['filename'] . '-rtl.min.' . $this->output_file_info['extension'],
136 $tree->render()
137 );
138 }
139
140 /**
141 * Validation of compile arguments.
142 *
143 * @return bool
144 */
145 private function validation_of_arguments() {
146 $validation = false;
147
148 if (
149 ! empty( $this->args ) &&
150 is_array( $this->args ) &&
151 isset( $this->args['input_file'] ) &&
152 isset( $this->args['output_file'] ) &&
153 isset( $this->args['variables'] )
154 ) {
155 $validation = true;
156 }
157
158 return $validation;
159 }
160
161 /**
162 * Get content of input scss file.
163 *
164 * @param string $file_name - Absolute path to file.
165 * @return string
166 */
167 private function get_input_file_contents( $file_name ) {
168 $contents = false;
169 if ( file_exists( $file_name ) ) {
170 // phpcs:ignore
171 $contents = file_get_contents( $file_name );
172 }
173
174 return $contents;
175 }
176
177 /**
178 * Check and Create non exist output folder.
179 *
180 * @return bool
181 */
182 private function check_and_create_non_exist_output_folder() {
183 $is_dir = false;
184
185 if (
186 isset( $this->output_file_info['dirname'] )
187 ) {
188 $dirname = $this->output_file_info['dirname'];
189 $is_dir = is_dir( $dirname );
190 if ( ! $is_dir ) {
191 $is_dir = mkdir( $dirname, 0777, true );
192 }
193 }
194
195 return $is_dir;
196 }
197 }
198