Completed
Push — testTinyMceRebase ( 36a97d )
by Doğa
03:12
created

functions.php ➔ getToolbars()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 7
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) {
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...
12
    $toolbars = getToolbars();
13
    return $toolbars['default'][0];
14
});
15
16
// Second Bar
17
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...
18
    return [];
19
});
20
21
add_filter('tiny_mce_before_init', function ($init) {
22
    $init['block_formats'] = getBlockFormats();
23
    $init['style_formats'] = getStyleFormats();
24
25
    return $init;
26
});
27
28
// TODO: refactor this
29
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
30
    // Load Toolbars and parse them into TinyMCE
31
    $toolbarsFromFile = getToolbars();
32
    if ($toolbarsFromFile) {
33
        $toolbars = [];
34
        foreach ($toolbars as $name => $toolbar) {
35
            array_unshift($toolbar, []);
36
            $toolbars[$name] = $toolbar;
37
        }
38
    }
39
40
    return $toolbars;
41
});
42
43
function getConfig()
44
{
45
    $filePath = Asset::requirePath('/Features/TinyMce/config.json');
46
    if (file_exists($filePath)) {
47
        return json_decode(file_get_contents($filePath), true);
48
    } else {
49
        return false;
50
    }
51
}
52
53
function getToolbars() {
54
    $config = getConfig();
55
    if ($config && isset($config['toolbars'])) {
56
        return $config['toolbars'];
57
    }
58
    return [];
59
}
60
61
// TODO: refactor this using php array functions
62
// TODO: consider removing default return
63
function getBlockFormats()
64
{
65
    $config = getConfig();
66
    if ($config && isset($config['blockstyles'])) {
67
        $blockstyles = $config['blockstyles'];
68
        $blockstylesQueries = [];
69
        foreach ($blockstyles as $label => $tag) {
70
            $blockstylesQueries[] = $label . '=' . $tag;
71
        }
72
        return implode(';', $blockstylesQueries);
73
    }
74
    return 'Paragraph=p;Heading 1=h1;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6';
75
}
76
77
function getStyleFormats()
78
{
79
    $config = getConfig();
80
    if ($config && isset($config['styleformats'])) {
81
        // Get contents as JSON string first and convert it to array for sending it to style_formats as true js array
82
        $loadedStyle = $config['styleformats'];
83
        return json_encode($loadedStyle);
84
    }
85
    return false;
86
}
87