Completed
Push — master ( faeebb...68bc9e )
by Doğa
02:46
created

functions.php ➔ getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
rs 9.6666
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
// Load TinyMCE Settings from config file
9
10
// First Toolbar
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 Toolbar
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
    return $init;
40
});
41
42
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
43
    // Load Toolbars and parse them into TinyMCE
44
    $config = getConfig();
45
    if ($config && !empty($config['toolbars'])) {
46
        $toolbars = array_map(function ($toolbar) {
47
            array_unshift($toolbar, []);
48
            return $toolbar;
49
        }, $config['toolbars']);
50
    }
51
    return $toolbars;
52
});
53
54
function getConfig()
55
{
56
    $filePath = Asset::requirePath('/Features/TinyMce/config.json');
57
    if (file_exists($filePath)) {
58
        return json_decode(file_get_contents($filePath), true);
59
    } else {
60
        return false;
61
    }
62
}
63
64
function getBlockFormats($blockFormats)
65
{
66
    if (!empty($blockFormats)) {
67
        $blockFormatStrings = array_map(function ($tag, $label) {
68
            return "${label}=${tag}";
69
        }, $blockFormats, array_keys($blockFormats));
70
        return implode($blockFormatStrings, ';');
71
    }
72
    return '';
73
}
74