Completed
Pull Request — master (#158)
by Doğa
05:28 queued 02:33
created

functions.php ➔ getBlockFormats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
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
// 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
// NOTE: feel free to edit this if needed (see the first toolbar example)
24
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...
25
    return [];
26
});
27
28
add_filter('tiny_mce_before_init', function ($init) {
29
    $config = getConfig();
30
    if ($config) {
31
        if (isset($config['blockformats'])) {
32
            $init['block_formats'] = getBlockFormats($config['blockformats']);
33
        }
34
35
        if (isset($config['styleformats'])) {
36
            // Send it to style_formats as true js array
37
            $init['style_formats'] = json_encode($config['styleformats']);
38
        }
39
    }
40
    return $init;
41
});
42
43
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
44
    // Load Toolbars and parse them into TinyMCE
45
    $config = getConfig();
46
    if ($config && !empty($config['toolbars'])) {
47
        $toolbars = array_map(function ($toolbar) {
48
            array_unshift($toolbar, []);
49
            return $toolbar;
50
        }, $config['toolbars']);
51
    }
52
    return $toolbars;
53
});
54
55
function getConfig()
56
{
57
    $filePath = Asset::requirePath('/Features/TinyMce/config.json');
58
    if (file_exists($filePath)) {
59
        return json_decode(file_get_contents($filePath), true);
60
    } else {
61
        return false;
62
    }
63
}
64
65
function getBlockFormats($blockFormats)
66
{
67
    if (!empty($blockFormats)) {
68
        $blockFormatStrings = array_map(function ($tag, $label) {
69
            return "${label}=${tag}";
70
        }, $blockFormats, array_keys($blockFormats));
71
        return implode($blockFormatStrings, ';');
72
    }
73
    return '';
74
}
75