Completed
Push — componentlibrary_featureless ( f542c2...921b57 )
by Dominik
01:42
created

tinyMce.php ➔ getBlockFormats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Cleans up TinyMCE Buttons to show all relevant buttons on the first bar. Adds an easy to configure way to change these defaults.
5
 *
6
 *
7
 * ## Updating global TinyMce settings using the JSON config
8
 *
9
 * By updating the function `getConfig` you can easily add new **Block Formats**, **Style Formats** and **Toolbars** for all Wysiwyg editors in your project.
10
 *
11
 * ## Editor Toolbars
12
 *
13
 * The MCE Buttons that show up by default are specified by the `toolbars` section in the `getConfig` function.
14
 * You can modify the settings for all Wysiwyg toolbars (all over your project).
15
 */
16
namespace Flynt\TinyMce;
17
18
// First Toolbar
19
add_filter('mce_buttons', function ($buttons) {
20
    $config = getConfig();
21
    if ($config && isset($config['toolbars'])) {
22
        $toolbars = $config['toolbars'];
23
        if (isset($toolbars['default']) && isset($toolbars['default'][0])) {
24
            return $toolbars['default'][0];
25
        }
26
    }
27
    return $buttons;
28
});
29
30
// Second Toolbar
31
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.

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

Loading history...
32
    return [];
33
});
34
35
add_filter('tiny_mce_before_init', function ($init) {
36
    $config = getConfig();
37
    if ($config) {
38
        if (isset($config['blockformats'])) {
39
            $init['block_formats'] = getBlockFormats($config['blockformats']);
40
        }
41
42
        if (isset($config['styleformats'])) {
43
            // Send it to style_formats as true js array
44
            $init['style_formats'] = json_encode($config['styleformats']);
45
        }
46
    }
47
    return $init;
48
});
49
50
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
51
    // Load Toolbars and parse them into TinyMCE
52
    $config = getConfig();
53
    if ($config && !empty($config['toolbars'])) {
54
        $toolbars = array_map(function ($toolbar) {
55
            array_unshift($toolbar, []);
56
            return $toolbar;
57
        }, $config['toolbars']);
58
    }
59
    return $toolbars;
60
});
61
62
function getBlockFormats($blockFormats)
63
{
64
    if (!empty($blockFormats)) {
65
        $blockFormatStrings = array_map(function ($tag, $label) {
66
            return "${label}=${tag}";
67
        }, $blockFormats, array_keys($blockFormats));
68
        return implode(';', $blockFormatStrings);
69
    }
70
    return '';
71
}
72
73
function getConfig()
74
{
75
    return [
76
        'blockformats' => [
77
            'Paragraph' => 'p',
78
            'Heading 1' => 'h1',
79
            'Heading 2' => 'h2',
80
            'Heading 3' => 'h3',
81
            'Heading 4' => 'h4',
82
            'Heading 5' => 'h5',
83
            'Heading 6' => 'h6'
84
        ],
85
        'styleformats' => [
86
            [
87
                'title' => 'Buttons',
88
                'icon' => '',
89
                'items' => [
90
                    [
91
                        'title' => 'Button Primary',
92
                        'classes' => 'btn btn--primary',
93
                        'selector' => 'a'
94
                    ],
95
                    [
96
                        'title' => 'Button Primary Block',
97
                        'classes' => 'btn btn--primary btn--block',
98
                        'selector' => 'a'
99
                    ]
100
                ]
101
            ]
102
        ],
103
        'toolbars' => [
104
            'default' => [
105
                [
106
                    'formatselect',
107
                    'styleselect',
108
                    'bold',
109
                    'italic',
110
                    'underline',
111
                    'strikethrough',
112
                    '|',
113
                    'bullist',
114
                    'numlist',
115
                    '|',
116
                    'link',
117
                    'unlink',
118
                    '|',
119
                    'wp_more',
120
                    'pastetext',
121
                    'removeformat',
122
                    '|',
123
                    'undo',
124
                    'redo',
125
                    'fullscreen'
126
                ]
127
            ],
128
            'full' => [
129
                [
130
                    'formatselect',
131
                    'styleselect',
132
                    'bold',
133
                    'italic',
134
                    'underline',
135
                    'blockquote',
136
                    '|',
137
                    'bullist',
138
                    'numlist',
139
                    '|',
140
                    'link',
141
                    'unlink',
142
                    '|',
143
                    'pastetext',
144
                    'removeformat',
145
                    '|',
146
                    'undo',
147
                    'redo',
148
                    'fullscreen'
149
                ]
150
            ],
151
            'basic' => [
152
                [
153
                    'bold',
154
                    'italic',
155
                    'underline',
156
                    'blockquote',
157
                    'bullist',
158
                    'numlist',
159
                    'alignleft',
160
                    'aligncenter',
161
                    'alignright',
162
                    'undo',
163
                    'redo',
164
                    'link',
165
                    'unlink',
166
                    'fullscreen'
167
                ]
168
            ]
169
        ]
170
    ];
171
}
172