Completed
Pull Request — componentlibrary (#307)
by
unknown
03:17 queued 01:43
created

functions.php ➔ getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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