Passed
Pull Request — master (#258)
by
unknown
06:38
created

getThemesConfig()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 134
Code Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

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