Passed
Push — master ( 478c3f...09294d )
by Dominik
03:57
created

getConfig()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 123
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 84
nc 1
nop 0
dl 0
loc 123
rs 8.349
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
 * Moves most relevant editor buttons to the first toolbar
5
 * and provides config for creating new toolbars, block formats, and style formats.
6
 * See the TinyMce documentation for more information: https://www.tiny.cloud/docs/
7
 *
8
 */
9
10
namespace Flynt\TinyMce;
11
12
// First Toolbar
13
add_filter('mce_buttons', function ($buttons) {
14
    $config = getConfig();
15
    if ($config && isset($config['toolbars'])) {
16
        $toolbars = $config['toolbars'];
17
        if (isset($toolbars['default']) && isset($toolbars['default'][0])) {
18
            return $toolbars['default'][0];
19
        }
20
    }
21
    return $buttons;
22
});
23
24
// Second Toolbar
25
add_filter('mce_buttons_2', function ($buttons) {
0 ignored issues
show
Unused Code introduced by
The parameter $buttons is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

25
add_filter('mce_buttons_2', function (/** @scrutinizer ignore-unused */ $buttons) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    return [];
27
});
28
29
add_filter('tiny_mce_before_init', function ($init) {
30
    $config = getConfig();
31
    if ($config) {
0 ignored issues
show
introduced by
$config is a non-empty array, thus is always true.
Loading history...
32
        if (isset($config['blockformats'])) {
33
            $init['block_formats'] = getBlockFormats($config['blockformats']);
34
        }
35
36
        if (isset($config['styleformats'])) {
37
            // Send it to style_formats as true js array
38
            $init['style_formats'] = json_encode($config['styleformats']);
39
        }
40
    }
41
    return $init;
42
});
43
44
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
45
    // Load Toolbars and parse them into TinyMCE
46
    $config = getConfig();
47
    if ($config && !empty($config['toolbars'])) {
48
        $toolbars = array_map(function ($toolbar) {
49
            array_unshift($toolbar, []);
50
            return $toolbar;
51
        }, $config['toolbars']);
52
    }
53
    return $toolbars;
54
});
55
56
function getBlockFormats($blockFormats)
57
{
58
    if (!empty($blockFormats)) {
59
        $blockFormatStrings = array_map(
60
            function ($tag, $label) {
61
                return "${label}=${tag}";
62
            },
63
            $blockFormats,
64
            array_keys($blockFormats)
65
        );
66
        return implode(';', $blockFormatStrings);
67
    }
68
    return '';
69
}
70
71
function getConfig()
72
{
73
    return [
74
        'blockformats' => [
75
            'Paragraph' => 'p',
76
            'Heading 1' => 'h1',
77
            'Heading 2' => 'h2',
78
            'Heading 3' => 'h3',
79
            'Heading 4' => 'h4',
80
            'Heading 5' => 'h5',
81
            'Heading 6' => 'h6'
82
        ],
83
        'styleformats' => [
84
            [
85
                'title' => 'Headings',
86
                'icon' => '',
87
                'items' => [
88
                    [
89
                        'title' => 'Heading 1',
90
                        'classes' => 'h1',
91
                        'selector' => '*'
92
                    ],
93
                    [
94
                        'title' => 'Heading 2',
95
                        'classes' => 'h2',
96
                        'selector' => '*'
97
                    ],
98
                    [
99
                        'title' => 'Heading 3',
100
                        'classes' => 'h3',
101
                        'selector' => '*'
102
                    ],
103
                    [
104
                        'title' => 'Heading 4',
105
                        'classes' => 'h4',
106
                        'selector' => '*'
107
                    ],
108
                    [
109
                        'title' => 'Heading 5',
110
                        'classes' => 'h5',
111
                        'selector' => '*'
112
                    ],
113
                    [
114
                        'title' => 'Heading 6',
115
                        'classes' => 'h6',
116
                        'selector' => '*'
117
                    ],
118
                ]
119
            ],
120
            [
121
                'title' => 'Buttons',
122
                'icon' => '',
123
                'items' => [
124
                    [
125
                        'title' => 'Button',
126
                        'classes' => 'button',
127
                        'selector' => 'a,button'
128
                    ],
129
                    [
130
                        'title' => 'Button Ghost',
131
                        'classes' => 'button--ghost',
132
                        'selector' => '.button'
133
                    ],
134
                    [
135
                        'title' => 'Button Small',
136
                        'classes' => 'button--small',
137
                        'selector' => '.button'
138
                    ],
139
                    [
140
                        'title' => 'Button Link',
141
                        'classes' => 'button--link',
142
                        'selector' => '.button'
143
                    ]
144
                ]
145
            ],
146
            [
147
                'title' => 'Icon Lists',
148
                'icon' => '',
149
                'items' => [
150
                    [
151
                        'title' => 'Check Circle',
152
                        'classes' => 'iconList iconList--checkCircle',
153
                        'selector' => 'ul,ol'
154
                    ]
155
                ]
156
            ]
157
        ],
158
        'toolbars' => [
159
            'default' => [
160
                [
161
                    'formatselect',
162
                    'styleselect',
163
                    'bold',
164
                    'italic',
165
                    'strikethrough',
166
                    'blockquote',
167
                    '|',
168
                    'bullist',
169
                    'numlist',
170
                    '|',
171
                    'link',
172
                    'unlink',
173
                    '|',
174
                    'pastetext',
175
                    'removeformat',
176
                    '|',
177
                    'undo',
178
                    'redo',
179
                    'fullscreen'
180
                ]
181
            ],
182
            'basic' => [
183
                [
184
                    'bold',
185
                    'italic',
186
                    'strikethrough',
187
                    '|',
188
                    'link',
189
                    'unlink',
190
                    '|',
191
                    'undo',
192
                    'redo',
193
                    'fullscreen'
194
                ]
195
            ]
196
        ]
197
    ];
198
}
199