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

sanitizeNumberRange()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 2
nop 2
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 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...
12
use Flynt\CSSVariables;
0 ignored issues
show
Bug introduced by
The type Flynt\CSSVariables 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
17
    if ($options['enabled']) {
18
        add_action('customize_register', function ($wp_customize) {
19
            $wp_customize->register_control_type('Flynt\Customizer\RangeControl');
20
            addSettings($wp_customize);
21
        });
22
23
        add_action('customize_controls_enqueue_scripts', function () {
24
            wp_enqueue_script(
25
                'Flynt/customizerControls',
26
                Asset::requireUrl('assets/customizerControls.js'),
27
                ['jquery']
28
            );
29
            wp_enqueue_style(
30
                'Flynt/customizerControls',
31
                Asset::requireUrl('assets/customizerControls.css')
32
            );
33
        });
34
35
        add_action('customize_preview_init', function () {
36
            wp_enqueue_script(
37
                'Flynt/customizerPreview',
38
                Asset::requireUrl('assets/customizerPreview.js'),
39
                ['jquery','customize-preview']
40
            );
41
            $fields = CSSVariables\getFields();
0 ignored issues
show
Bug introduced by
The function getFields 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

41
            $fields = /** @scrutinizer ignore-call */ CSSVariables\getFields();
Loading history...
42
            wp_localize_script('Flynt/customizerPreview', 'FlyntCustomizerFields', $fields);
43
        });
44
    }
45
});
46
47
function addSettings($wp_customize)
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
51
    foreach ($settings as $setting) {
52
        if ($setting['type'] === 'panel') {
53
            $wp_customize->add_panel(
54
                "flynt_panel_{$setting['name']}",
55
                [
56
                    'title' => $setting['label'],
57
                    'priority' => $setting['priority'] ?? 160,
58
                ]
59
            );
60
61
            foreach ($setting['sections'] ?? [] as $section) {
62
                addSection($wp_customize, $section, $setting['name']);
63
            }
64
        } else if ($setting['type'] === 'section') {
65
            addSection($wp_customize, $setting);
66
        }
67
    }
68
}
69
70
function addSection($wp_customize, $section, $panel = '')
71
{
72
    $panelKey = $panel ? "{$panel}_" : '';
73
    $sectionKey = "flynt_section_{$panelKey}{$section['name']}";
74
    $wp_customize->add_section(
75
        $sectionKey,
76
        [
77
            'title' => $section['label'],
78
            'priority' => $section['priority'] ?? 160,
79
            'panel' => $panel ? "flynt_panel_{$panel}" : '',
80
        ]
81
    );
82
83
    foreach ($section['fields'] ?? [] as $setting) {
84
        addSetting($wp_customize, $setting, $sectionKey);
85
    }
86
}
87
88
function addSetting($wp_customize, $setting, $sectionKey)
89
{
90
    $settingId = $setting['name'];
91
    unset($setting['name']);
92
93
    $wp_customize->add_setting(
94
        $settingId,
95
        [
96
            'default' => $setting['default'],
97
            'transport' => 'postMessage',
98
            'sanitize_callback' => getSanitizeCallback($setting['type']),
99
        ]
100
    );
101
102
    $controllClass = getControllCass($setting['type']);
103
104
    $wp_customize->add_control(
105
        new $controllClass(
106
            $wp_customize,
107
            $settingId,
108
            array_merge(
109
                $setting,
110
                [
111
                    'section' => $sectionKey,
112
                ]
113
            )
114
        )
115
    );
116
}
117
118
function getControllCass($type)
119
{
120
    $controls = [
121
        'color' => 'WP_Customize_Color_Control',
122
        'range' => 'Flynt\Customizer\RangeControl',
123
    ];
124
125
    return $controls[$type] ?? 'WP_Customize_Control';
126
}
127
128
function getSanitizeCallback($type)
129
{
130
    $sanitize = [
131
        'color' => 'sanitize_hex_color',
132
        'range' => 'Flynt\Customizer\sanitizeNumberRange',
133
    ];
134
135
    return $sanitize[$type] ?? '';
136
}
137
138
function sanitizeNumberRange($number, $setting)
139
{
140
    $number = absint($number);
141
    $options = $setting->manager->get_control($setting->id)->options;
142
143
    $min = $options['min'] ?? $number;
144
    $max = $options['max'] ?? $number;
145
    $step = $options['step'] ?? 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $step is dead and can be removed.
Loading history...
146
147
    if ($min <= $number && $number <= $max) {
148
        return $number;
149
    }
150
151
    return $setting->default;
152
}
153
154
Options::addGlobal('Customizer', [
155
    [
156
        'label' => __('Status', 'flynt'),
157
        'name' => 'enabled',
158
        'type' => 'true_false',
159
        'ui' => 1,
160
        'ui_on_text' => 'Enabled',
161
        'ui_off_text' => 'Disabled',
162
        'default_value' => true,
163
    ],
164
]);
165