Passed
Pull Request — master (#258)
by
unknown
04:57
created

getFields()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
rs 9.6111
cc 5
nc 4
nop 0
1
<?php
2
3
/**
4
 * Output CSS variables in head before the main stylesheet.
5
 */
6
7
namespace Flynt\CSSVariables;
8
9
use Flynt\Variables;
0 ignored issues
show
Bug introduced by
The type Flynt\Variables was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Flynt\Utils\ColorHelpers;
11
12
add_action('wp_head', function () {
13
    $variables = getCssVariables();
14
    ?>
15
    <style type="text/css">
16
        :root.html {
17
            <?php foreach ($variables as $variable => $value) {
18
                echo "--{$variable}: {$value}; ";
19
            } ?>
20
        }
21
    </style>
22
    <?php
23
}, 5);
24
25
function getCssVariables()
26
{
27
    $fields = getFields();
28
    $variables = [];
29
30
    foreach ($fields as $field) {
31
        $variable = $field['name'];
32
        $value = get_theme_mod($field['name'], $field['default']);
33
        $unit = $field['unit'] ?? '';
34
        $variables[$variable] = $value . $unit;
35
36
        if (isset($field['hsl']) && $field['type'] === 'color') {
37
            $hslCssVariables = generateHslaCssVariables($field, $value);
38
            foreach ($hslCssVariables as $variable => $value) {
39
                $variables[$variable] = $value;
40
            }
41
        }
42
    }
43
44
    return $variables;
45
}
46
47
function getFields()
48
{
49
    $settings = Variables\getVariables();
0 ignored issues
show
Bug introduced by
The function getVariables was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
    $settings = /** @scrutinizer ignore-call */ Variables\getVariables();
Loading history...
50
    $fields = [];
51
52
    foreach ($settings as $setting) {
53
        if ($setting['type'] === 'panel') {
54
            foreach ($setting['sections'] ?? [] as $section) {
55
                $fields = array_merge($section['fields'] ?? [], $fields);
56
            }
57
        } else if ($setting['type'] === 'section') {
58
            $fields = array_merge($setting['fields'] ?? [], $fields);
59
        }
60
    }
61
62
    return $fields;
63
}
64
65
function generateHslaCssVariables($field, $value)
66
{
67
    $colorHsla = ColorHelpers::hexToHsla($value);
68
    return [
69
        "{$field['name']}-h" => $colorHsla[0],
70
        "{$field['name']}-s" => $colorHsla[1],
71
        "{$field['name']}-l" => $colorHsla[2],
72
    ];
73
}
74