Passed
Pull Request — master (#258)
by
unknown
04:48
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
    // print_r($variables);
15
    ?>
16
    <style type="text/css">
17
        :root.html {
18
            <?php foreach ($variables as $variable => $value) {
19
                echo "--{$variable}: {$value}; ";
20
            } ?>
21
        }
22
    </style>
23
    <?php
24
}, 5);
25
26
function getCssVariables()
27
{
28
    $fields = getFields();
29
    $variables = [];
30
31
    foreach ($fields as $field) {
32
        $value = get_theme_mod($field['name'], $field['default']);
33
        $variable = [
34
            $field['name'] => $value,
35
        ];
36
37
        $variables = array_merge(
38
            apply_filters("Flynt/cssVariable?type={$field['type']}", $variable, $value, $field),
39
            $variables
40
        );
41
    }
42
43
    return $variables;
44
}
45
46
function getFields()
47
{
48
    $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

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