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

getThemesConfig()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 130
Code Lines 98

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 98
c 0
b 0
f 0
dl 0
loc 130
rs 8.0436
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Add theme options to customizer
5
 */
6
7
namespace Flynt\CustomizerColors;
8
9
use Flynt\Utils\Asset;
10
use Flynt\Utils\ColorHelpers;
11
use Flynt\Utils\Options;
12
use WP_Customize_Color_Control;
13
14
function getThemesConfig()
15
{
16
    $themes = [
17
        'default' => [
18
            'name' => __('Default', 'flynt'),
19
            'colors' => [
20
                'accent' => [
21
                    'label'       => __('Accent', 'flynt'),
22
                    'default'     => '#2b44df',
23
                    'description' => '',
24
                    'hsl'         => 1,
25
                ],
26
                'headline' => [
27
                    'label'       => __('Headline', 'flynt'),
28
                    'default'     => '#252525',
29
                    'description' => '',
30
                ],
31
                'text' => [
32
                    'label'       => __('Text', 'flynt'),
33
                    'default'     => '#353535',
34
                    'description' => '',
35
                ],
36
                'border' => [
37
                    'label'       => __('Border', 'flynt'),
38
                    'default'     => '#8791BA',
39
                    'description' => '',
40
                ],
41
                'background' => [
42
                    'label'       => __('Background', 'flynt'),
43
                    'default'     => '#ffffff',
44
                    'description' => '',
45
                ],
46
            ],
47
        ],
48
        'light' => [
49
            'name' => __('Theme Light', 'flynt'),
50
            'colors' => [
51
                'accent' => [
52
                    'label'       => __('Accent', 'flynt'),
53
                    'default'     => '#2b44df',
54
                    'description' => '',
55
                    'hsl'         => 1,
56
                ],
57
                'headline' => [
58
                    'label'       => __('Headline', 'flynt'),
59
                    'default'     => '#252525',
60
                    'description' => '',
61
                ],
62
                'text' => [
63
                    'label'       => __('Text', 'flynt'),
64
                    'default'     => '#353535',
65
                    'description' => '',
66
                ],
67
                'border' => [
68
                    'label'       => __('Border', 'flynt'),
69
                    'default'     => '#8791BA',
70
                    'description' => '',
71
                ],
72
                'background' => [
73
                    'label'       => __('Background', 'flynt'),
74
                    'default'     => '#F8F9FD',
75
                    'description' => '',
76
                ],
77
            ],
78
        ],
79
        'dark' => [
80
            'name' => __('Theme Dark', 'flynt'),
81
            'colors' => [
82
                'accent' => [
83
                    'label'       => __('Accent', 'flynt'),
84
                    'default'     => '#ffffff',
85
                    'description' => '',
86
                    'hsl'         => 1,
87
                ],
88
                'headline' => [
89
                    'label'       => __('Headline', 'flynt'),
90
                    'default'     => '#FBFBFB',
91
                    'description' => '',
92
                ],
93
                'text' => [
94
                    'label'       => __('Text', 'flynt'),
95
                    'default'     => '#E9E9EC',
96
                    'description' => '',
97
                ],
98
                'border' => [
99
                    'label'       => __('Border', 'flynt'),
100
                    'default'     => '#C3C4F7',
101
                    'description' => '',
102
                ],
103
                'background' => [
104
                    'label'       => __('Background', 'flynt'),
105
                    'default'     => '#10205A',
106
                    'description' => '',
107
                ],
108
            ],
109
        ],
110
        'hero' => [
111
            'name' => __('Theme Hero', 'flynt'),
112
            'colors' => [
113
                'accent' => [
114
                    'label'       => __('Accent', 'flynt'),
115
                    'default'     => '#ffffff',
116
                    'description' => '',
117
                    'hsl'         => 1,
118
                ],
119
                'headline' => [
120
                    'label'       => __('Headline', 'flynt'),
121
                    'default'     => '#FBFBFB',
122
                    'description' => '',
123
                ],
124
                'text' => [
125
                    'label'       => __('Text', 'flynt'),
126
                    'default'     => '#E9E9EC',
127
                    'description' => '',
128
                ],
129
                'border' => [
130
                    'label'       => __('Border', 'flynt'),
131
                    'default'     => '#CDE2FD',
132
                    'description' => '',
133
                ],
134
                'background' => [
135
                    'label'       => __('Background', 'flynt'),
136
                    'default'     => '#2B44DF',
137
                    'description' => '',
138
                ],
139
            ],
140
        ],
141
    ];
142
143
    return $themes;
144
}
145
146
add_action('acf/init', function () {
147
    $options = Options::getGlobal('CustomizerColors');
148
    if ($options['enabled']) {
149
        add_action('customize_register', function ($wp_customize) {
150
            $themes = getThemesConfig();
151
152
            $wp_customize->add_panel(
153
                'theme_colors_panel',
154
                [
155
                    'title' => __('Colors', 'flynt'),
156
                    'priority' => 160,
157
                ]
158
            );
159
160
            foreach ($themes as $key => $theme) {
161
                $wp_customize->add_section(
162
                    "theme_{$key}_colors",
163
                    [
164
                        'title' => $theme['name'],
165
                        'priority' => 20,
166
                        'panel' => 'theme_colors_panel',
167
                    ]
168
                );
169
            }
170
171
            foreach ($themes as $themeKey => $theme) {
172
                foreach ($theme['colors'] as $colorName => $colorConfig) {
173
                    $wp_customize->add_setting(
174
                        "theme_{$themeKey}_color_{$colorName}",
175
                        [
176
                            'default' => $colorConfig['default'],
177
                            'transport' => 'postMessage',
178
                        ]
179
                    );
180
181
                    $wp_customize->add_control(
182
                        new WP_Customize_Color_Control(
183
                            $wp_customize,
184
                            "theme_{$themeKey}_color_{$colorName}",
185
                            [
186
                                'section' => "theme_{$themeKey}_colors",
187
                                'label' => __($colorConfig['label']),
188
                                'description' => __($colorConfig['description']),
189
                            ]
190
                        )
191
                    );
192
                }
193
            }
194
        });
195
196
        add_action('customize_preview_init', function () {
197
            wp_enqueue_script(
198
                'customizer-colors',
199
                Asset::requireUrl('assets/customizer-colors.js'),
200
                ['jquery','customize-preview'],
201
            );
202
            $themes = getThemesConfig();
203
            $config = array_map(function ($theme) {
204
                return $theme['colors'];
205
            }, $themes);
206
            wp_localize_script('customizer-colors', 'FlyntCustomizerColorsData', $config);
207
        });
208
    }
209
});
210
211
add_action('wp_head', function () {
212
    $themes = getThemesConfig();
213
    ?>
214
    <style type="text/css">
215
        :root.html {
216
            <?php foreach ($themes as $themeKey => $theme) {
217
                foreach ($theme['colors'] as $colorName => $colorConfig) {
218
                    $colorValue = get_theme_mod("theme_{$themeKey}_color_{$colorName}", $colorConfig['default']);
219
                    $cssProperty = "--theme-{$themeKey}-color-{$colorName}";
220
                    echo "{$cssProperty}: {$colorValue};";
221
222
                    if ($colorConfig['hsl'] ?? false) {
223
                        $colorHsla = ColorHelpers::hexToHsla($colorValue);
224
                        echo "{$cssProperty}-h: {$colorHsla[0]};";
225
                        echo "{$cssProperty}-s: {$colorHsla[1]};";
226
                        echo "{$cssProperty}-l: {$colorHsla[2]};";
227
                    }
228
                }
229
            } ?>
230
        }
231
    </style>
232
    <?php
233
}, 5);
234
235
Options::addGlobal('CustomizerColors', [
236
    [
237
        'label' => __('Status', 'flynt'),
238
        'name' => 'enabled',
239
        'type' => 'true_false',
240
        'ui' => 1,
241
        'ui_on_text' => 'Enabled',
242
        'ui_off_text' => 'Disabled',
243
        'default_value' => true,
244
    ],
245
]);
246