Passed
Pull Request — master (#258)
by
unknown
07:36 queued 02:23
created

getControllCass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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

51
    $settings = /** @scrutinizer ignore-call */ Variables\getVariables();
Loading history...
52
53
    foreach ($settings as $setting) {
54
        if ($setting['type'] === 'panel') {
55
            $wp_customize->add_panel(
56
                "flynt_panel_{$setting['name']}",
57
                [
58
                    'title' => $setting['label'],
59
                    'priority' => $setting['priority'] ?? 160,
60
                ]
61
            );
62
63
            foreach ($setting['sections'] ?? [] as $section) {
64
                addSection($wp_customize, $section, $setting['name']);
65
            }
66
        } else if ($setting['type'] === 'section') {
67
            addSection($wp_customize, $setting);
68
        }
69
    }
70
}
71
72
function addSection($wp_customize, $section, $panel = '')
73
{
74
    $panelKey = $panel ? "{$panel}_" : '';
75
    $sectionKey = "flynt_section_{$panelKey}{$section['name']}";
76
    $wp_customize->add_section(
77
        $sectionKey,
78
        [
79
            'title' => $section['label'],
80
            'priority' => $section['priority'] ?? 160,
81
            'panel' => $panel ? "flynt_panel_{$panel}" : '',
82
        ]
83
    );
84
85
    foreach ($section['fields'] ?? [] as $setting) {
86
        addSetting($wp_customize, $setting, $sectionKey);
87
    }
88
}
89
90
function addSetting($wp_customize, $setting, $sectionKey)
91
{
92
    $settingId = $setting['name'];
93
    unset($setting['name']);
94
95
    $wp_customize->add_setting(
96
        $settingId,
97
        [
98
            'default' => $setting['default'],
99
            'transport' => 'postMessage',
100
            'sanitize_callback' => getSanitizeCallback($setting['type']),
101
        ]
102
    );
103
104
    $controllClass = getControllCass($setting['type']);
105
106
    $wp_customize->add_control(
107
        new $controllClass(
108
            $wp_customize,
109
            $settingId,
110
            array_merge(
111
                $setting,
112
                [
113
                    'section' => $sectionKey,
114
                ]
115
            )
116
        )
117
    );
118
}
119
120
function getControllCass($type)
121
{
122
    $controls = [
123
        'color' => 'WP_Customize_Color_Control',
124
        'flynt-range' => 'Flynt\Customizer\RangeControl',
125
    ];
126
127
    return $controls[$type] ?? 'WP_Customize_Control';
128
}
129
130
function getSanitizeCallback($type)
131
{
132
    $sanitize = [
133
        'color' => 'sanitize_hex_color',
134
        'flynt-range' => 'Flynt\Customizer\sanitizeNumberRange',
135
    ];
136
137
    return $sanitize[$type] ?? '';
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