Completed
Push — Features/TinyMce/Improvements ( 6eca63...ad3806 )
by Doğa
02:46
created

functions.php ➔ getStyleFormats()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\TinyMce;
4
5
use Flynt\Utils\Feature;
6
use Flynt\Utils\Asset;
7
8
// Clean Up TinyMCE Buttons
9
10
// First Bar
11
add_filter('mce_buttons', function ($buttons) {
12
    $config = getConfig();
13
    if ($config && isset($config['toolbars'])) {
14
        $toolbars = $config['toolbars'];
15
        if (isset($toolbars['default']) && isset($toolbars['default'][0])) {
16
            return $toolbars['default'][0];
17
        }
18
    }
19
    return $buttons;
20
});
21
22
// Second Bar
23
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...
24
    return [];
25
});
26
27
add_filter('tiny_mce_before_init', function ($init) {
28
    $config = getConfig();
29
    if ($config) {
30
        if (isset($config['blockformats'])) {
31
            $init['block_formats'] = getBlockFormats($config['blockformats']);
32
        }
33
34
        if (isset($config['styleformats'])) {
35
            // Send it to style_formats as true js array
36
            $init['style_formats'] = json_encode($config['styleformats']);
37
        }
38
    }
39
40
    return $init;
41
});
42
43
// TODO: refactor this
44
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
45
    // Load Toolbars and parse them into TinyMCE
46
    $config = getConfig();
47
    if ($config && isset($config['toolbars'])) {
48
        $toolbarsFromFile = $config['toolbars'];
49
        if (!empty($toolbarsFromFile)) {
50
            $toolbars = [];
51
            foreach ($toolbars as $name => $toolbar) {
52
                array_unshift($toolbar, []);
53
                $toolbars[$name] = $toolbar;
54
            }
55
        }
56
    }
57
58
    return $toolbars;
59
});
60
61
function getConfig()
62
{
63
    $filePath = Asset::requirePath('/Features/TinyMce/config.json');
64
    if (file_exists($filePath)) {
65
        return json_decode(file_get_contents($filePath), true);
66
    } else {
67
        return false;
68
    }
69
}
70
71
function getBlockFormats($blockFormats)
72
{
73
    if (!empty($blockFormats)) {
74
        $blockFormatStrings = array_map(function ($tag, $label) {
75
            return "${label}=${tag}";
76
        }, $blockFormats, array_keys($blockFormats));
77
        return implode($blockFormatStrings, ';');
78
    }
79
    return '';
80
}
81