Passed
Pull Request — master (#258)
by
unknown
05:18
created

addSizesSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 34
rs 9.584
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Add theme options to customizer
5
 */
6
7
namespace Flynt\Customizer;
8
9
use Flynt\Utils\Asset;
10
use Flynt\Utils\Options;
11
use WP_Customize_Color_Control;
12
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...
13
14
add_action('acf/init', function () {
15
    $options = Options::getGlobal('Customizer');
16
    if ($options['enabled']) {
17
        add_action('customize_register', function ($wp_customize) {
18
            $wp_customize->register_control_type('Flynt\Customizer\RangeControl');
19
20
            addSizesSettings($wp_customize);
21
            addColorsSettings($wp_customize);
22
        });
23
24
        add_action('customize_controls_enqueue_scripts', function () {
25
            wp_enqueue_script(
26
                'Flynt/customizerControls',
27
                Asset::requireUrl('assets/customizerControls.js'),
28
                ['jquery','customize-preview']
29
            );
30
            wp_enqueue_style(
31
                'Flynt/customizerControls',
32
                Asset::requireUrl('assets/customizerControls.css')
33
            );
34
        });
35
36
        add_action('customize_preview_init', function () {
37
            wp_enqueue_script(
38
                'Flynt/customizerPreview',
39
                Asset::requireUrl('assets/customizerPreview.js'),
40
                ['jquery','customize-preview']
41
            );
42
            $themes = Variables\getColors();
0 ignored issues
show
Bug introduced by
The function getColors 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

42
            $themes = /** @scrutinizer ignore-call */ Variables\getColors();
Loading history...
43
            $config = array_map(function ($theme) {
44
                return $theme['colors'];
45
            }, $themes);
46
            wp_localize_script('Flynt/customizerPreview', 'FlyntCustomizerData', $config);
47
        });
48
    }
49
});
50
51
function addSizesSettings($wp_customize)
52
{
53
    $sizes = Variables\getSizes();
0 ignored issues
show
Bug introduced by
The function getSizes 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

53
    $sizes = /** @scrutinizer ignore-call */ Variables\getSizes();
Loading history...
54
55
    $wp_customize->add_section(
56
        "sizes_section",
57
        [
58
            'title' => __('Sizes', 'flynt'),
59
            'priority' => 150,
60
        ]
61
    );
62
63
    foreach ($sizes as $sizeKey => $size) {
64
        $sizeKey = str_replace('-', '_', $sizeKey);
65
66
        $wp_customize->add_setting(
67
            "size_{$sizeKey}",
68
            [
69
                'default' => $size['default'],
70
                'transport' => 'postMessage',
71
                'sanitize_callback' => "Flynt\\Customizer\\sanitizeNumberRange",
72
            ]
73
        );
74
75
        $wp_customize->add_control(
76
            new RangeControl(
77
                $wp_customize,
78
                "size_{$sizeKey}",
79
                [
80
                    'section' => "sizes_section",
81
                    'label' => $size['label'],
82
                    'description' => $size['description'] ?? '',
83
                    'unit' => $size['unit'],
84
                    'options' => $size['options'],
85
                ]
86
            )
87
        );
88
    }
89
}
90
91
function addColorsSettings($wp_customize)
92
{
93
    $themes = Variables\getColors();
0 ignored issues
show
Bug introduced by
The function getColors 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

93
    $themes = /** @scrutinizer ignore-call */ Variables\getColors();
Loading history...
94
95
    $wp_customize->add_panel(
96
        'theme_colors_panel',
97
        [
98
            'title' => __('Colors', 'flynt'),
99
            'priority' => 160,
100
        ]
101
    );
102
103
    foreach ($themes as $key => $theme) {
104
        $wp_customize->add_section(
105
            "theme_{$key}_colors",
106
            [
107
                'title' => $theme['label'],
108
                'priority' => 20,
109
                'panel' => 'theme_colors_panel',
110
            ]
111
        );
112
    }
113
114
    foreach ($themes as $themeKey => $theme) {
115
        foreach ($theme['colors'] as $colorName => $colorConfig) {
116
            $wp_customize->add_setting(
117
                "theme_{$themeKey}_color_{$colorName}",
118
                [
119
                    'default' => $colorConfig['default'],
120
                    'transport' => 'postMessage',
121
                    'sanitize_callback' => 'sanitize_hex_color',
122
                ]
123
            );
124
125
            $wp_customize->add_control(
126
                new WP_Customize_Color_Control(
127
                    $wp_customize,
128
                    "theme_{$themeKey}_color_{$colorName}",
129
                    [
130
                        'section' => "theme_{$themeKey}_colors",
131
                        'label' => $colorConfig['label'],
132
                        'description' => $colorConfig['description'] ?? '',
133
                    ]
134
                )
135
            );
136
        }
137
    }
138
}
139
140
function sanitizeNumberRange($number, $setting)
141
{
142
    $number = absint($number);
143
    $options = $setting->manager->get_control($setting->id)->options;
144
145
    $min = $options['min'] ?? $number;
146
    $max = $options['max'] ?? $number;
147
    $step = $options['step'] ?? 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $step is dead and can be removed.
Loading history...
148
149
    if ($min <= $number && $number <= $max) {
150
        return $number;
151
    }
152
153
    return $setting->default;
154
}
155
156
Options::addGlobal('Customizer', [
157
    [
158
        'label' => __('Status', 'flynt'),
159
        'name' => 'enabled',
160
        'type' => 'true_false',
161
        'ui' => 1,
162
        'ui_on_text' => 'Enabled',
163
        'ui_off_text' => 'Disabled',
164
        'default_value' => true,
165
    ],
166
]);
167