Passed
Pull Request — master (#258)
by Dominik
05:19
created

addSettings()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
rs 9.6111
cc 5
nc 4
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 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\Range\Control');
20
            $wp_customize->register_control_type('Flynt\Customizer\Typography\Control');
21
            addSettings($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'],
29
                null,
30
                true
31
            );
32
            wp_enqueue_style(
33
                'Flynt/customizerControls',
34
                Asset::requireUrl('assets/customizerControls.css')
35
            );
36
        });
37
38
        add_action('customize_preview_init', function () {
39
            wp_enqueue_script(
40
                'Flynt/customizerPreview',
41
                Asset::requireUrl('assets/customizerPreview.js'),
42
                ['jquery','customize-preview']
43
            );
44
            $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

44
            $fields = /** @scrutinizer ignore-call */ CSSVariables\getFields();
Loading history...
45
            wp_localize_script('Flynt/customizerPreview', 'FlyntCustomizerFields', $fields);
46
        });
47
    }
48
});
49
50
function addSettings($wp_customize)
51
{
52
    $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

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