Passed
Pull Request — master (#258)
by Dominik
05:19
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
        $value = get_theme_mod($field['name'], $field['default']);
32
        $variable = [
33
            $field['name'] => $value,
34
        ];
35
36
        $variables = array_merge(
37
            apply_filters("Flynt/cssVariable?type={$field['type']}", $variable, $value, $field),
38
            $variables
39
        );
40
    }
41
42
    return $variables;
43
}
44
45
function getFields()
46
{
47
    $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

47
    $settings = /** @scrutinizer ignore-call */ Variables\getVariables();
Loading history...
48
    $fields = [];
49
50
    foreach ($settings as $setting) {
51
        if ($setting['type'] === 'panel') {
52
            foreach ($setting['sections'] ?? [] as $section) {
53
                $fields = array_merge($section['fields'] ?? [], $fields);
54
            }
55
        } else if ($setting['type'] === 'section') {
56
            $fields = array_merge($setting['fields'] ?? [], $fields);
57
        }
58
    }
59
60
    return $fields;
61
}
62
63
add_filter('Flynt/cssVariable?type=color', function ($variable, $value, $field) {
64
    if (isset($field['hsl'])) {
65
        $colorHsla = ColorHelpers::hexToHsla($value);
66
        $variable["{$field['name']}-h"] = $colorHsla[0];
67
        $variable["{$field['name']}-s"] = $colorHsla[1];
68
        $variable["{$field['name']}-l"] = $colorHsla[2];
69
    }
70
71
    return $variable;
72
}, 10, 3);
73
74
add_filter('Flynt/cssVariable?type=flynt-range', function ($variable, $value, $field) {
75
    $unit = $field['unit'] ?? '';
76
    $variable[$field['name']] = $value . $unit;
77
78
    return $variable;
79
}, 10, 3);
80
81
add_filter('Flynt/cssVariable?type=flynt-typography', function ($variable, $value, $field) {
82
    $fontFamily = array_filter([
83
        $value['family'] ?? '',
84
        $field['fallback'] ?? '',
85
        $value['category'] ?? '',
86
    ]);
87
88
    unset($variable[$field['name']]);
89
    $variable["{$field['name']}-family"] = implode(', ', $fontFamily);
90
    $variable["{$field['name']}-weight"] = $value['variant'];
91
    return $variable;
92
}, 10, 3);
93