| 1 |
<?php |
| 2 |
/** |
| 3 |
* Find all SCSS files and remove unsupported math.div module. |
| 4 |
* |
| 5 |
* We have to look at all scss files in our plugin and replace modules syntax, |
| 6 |
* because SCSSPHP does not support it yet. |
| 7 |
* |
| 8 |
* @package ghostkit |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* GhostKit_Scss_Replace_Modules |
| 13 |
*/ |
| 14 |
class GhostKit_Scss_Replace_Modules { |
| 15 |
/** |
| 16 |
* GhostKit_Scss_Replace_Modules constructor. |
| 17 |
* |
| 18 |
* @param string $plugin_path - plugin path. |
| 19 |
*/ |
| 20 |
public function __construct( $plugin_path ) { |
| 21 |
// Find all SCSS files and remove unsupported math.div module. |
| 22 |
$scss_files = array(); |
| 23 |
|
| 24 |
$di = new RecursiveDirectoryIterator( trailingslashit( $plugin_path ), RecursiveDirectoryIterator::SKIP_DOTS ); |
| 25 |
$it = new RecursiveIteratorIterator( $di ); |
| 26 |
|
| 27 |
foreach ( $it as $file ) { |
| 28 |
if ( pathinfo( $file, PATHINFO_EXTENSION ) === 'scss' ) { |
| 29 |
$scss_files[] = $file->getPathname(); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
foreach ( $scss_files as $scss_file_path ) { |
| 34 |
// phpcs:ignore |
| 35 |
$original_contents = file_get_contents( $scss_file_path ); |
| 36 |
$file_contents = $original_contents; |
| 37 |
|
| 38 |
if ( $file_contents ) { |
| 39 |
// find module include. |
| 40 |
$file_contents = str_replace( '@use "sass:math";', '', $file_contents ); |
| 41 |
$file_contents = str_replace( '@use "sass:map";', '', $file_contents ); |
| 42 |
$file_contents = str_replace( '@use "sass:list";', '', $file_contents ); |
| 43 |
$file_contents = str_replace( '@use "sass:string";', '', $file_contents ); |
| 44 |
|
| 45 |
// find math.div calls. |
| 46 |
preg_match_all( '/math\.div(?=\()(?:(?=.*?\((?!.*?\1)(.*\)(?!.*\2).*))(?=.*?\)(?!.*?\2)(.*)).)+?.*?(?=\1)[^(]*(?=\2$)/ms', $file_contents, $file_contents_calls ); |
| 47 |
if ( ! empty( $file_contents_calls[0] ) ) { |
| 48 |
foreach ( $file_contents_calls[0] as $content ) { |
| 49 |
$new_file_contents = $content; |
| 50 |
$new_file_contents = str_replace( 'math.div', '', $new_file_contents ); |
| 51 |
$new_file_contents = preg_replace( '/\,/', ' /', $new_file_contents, 1 ); |
| 52 |
|
| 53 |
$file_contents = str_replace( $content, $new_file_contents, $file_contents ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// find map calls. |
| 58 |
$file_contents = str_replace( 'map.get(', 'map-get(', $file_contents ); |
| 59 |
$file_contents = str_replace( 'map.values(', 'map-values(', $file_contents ); |
| 60 |
|
| 61 |
// find list calls. |
| 62 |
$file_contents = str_replace( 'list.index(', 'index(', $file_contents ); |
| 63 |
$file_contents = str_replace( 'list.nth(', 'nth(', $file_contents ); |
| 64 |
|
| 65 |
// find string calls. |
| 66 |
$file_contents = str_replace( 'string.index(', 'str-index(', $file_contents ); |
| 67 |
|
| 68 |
// Update file content. |
| 69 |
if ( $original_contents !== $file_contents ) { |
| 70 |
// phpcs:ignore |
| 71 |
file_put_contents( $scss_file_path, $file_contents ); |
| 72 |
} |
| 73 |
|
| 74 |
// Freeing memory. |
| 75 |
unset( $scss_file_path ); |
| 76 |
unset( $file_contents ); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|