-
Notifications
You must be signed in to change notification settings - Fork 225
/
MappingFormatter.php
47 lines (40 loc) · 1 KB
/
MappingFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace WP_Rocket\Engine\Media\Lazyload\CSS\Front;
class MappingFormatter {
/**
* Format data for the Mapping file.
*
* @param array $data Data to format.
* @return array
*/
public function format( array $data ): array {
$formatted_urls = [];
foreach ( $data as $datum ) {
$hash = $datum['hash'];
$selector = $datum['selector'];
$selector = $this->remove_pseudo_classes( $selector );
$url = $datum['url'];
$placeholder = "--wpr-bg-$hash";
$variable_placeholder = ':root{' . $placeholder . ': ' . $url . ';}';
$formatted_urls[] = [
'selector' => $selector,
'style' => $variable_placeholder,
];
}
return $formatted_urls;
}
/**
* Remove pseudo classes.
*
* @param string $selector Selector to clean.
*
* @return string
*/
protected function remove_pseudo_classes( string $selector ): string {
$result = preg_replace( '/::[\w\-]+/', '', $selector );
if ( ! $result ) {
return 'body';
}
return (string) $result;
}
}