Completed
Pull Request — master (#158)
by
unknown
05:01 queued 02:33
created

functions.php ➔ getToolbarsFromJson()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 0
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\TinyMce;
4
5
use Flynt\Utils\Feature;
6
7
// Clean Up TinyMCE Buttons
8
9
// First Bar
10
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...
11
    $toolbarsFromFile = getToolbarsFromJson();
12
    return $toolbarsFromFile['Default'];
13
});
14
15
// Second Bar
16
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...
17
    return [];
18
});
19
20
add_filter('tiny_mce_before_init', function ($init) {
21
    // Add block format elements you want to show in dropdown
22
    $init['block_formats'] = 'Paragraph=p;Heading 1=h1;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6';
23
    
24
    // Load Styleformat Dropdown and parse it into TinyMCE
25
    $options = Feature::getOptions('flynt-tiny-mce');
26
    if (isset($options[0]) && isset($options[0]['styleformatsConfigPath'])) {
27
        $configPath = $options[0]['styleformatsConfigPath'];
28
    } else {
29
        $configPath = 'config/toolbars.json';
30
    }
31
32
    $filePath = get_template_directory() . '/Features/TinyMce/' . $configPath;
33
    if (file_exists($filePath)) {
34
        $loadedStyle = file_get_contents($filePath);
35
        $init['style_formats'] = $loadedStyle;
36
    }
37
    return $init;
38
});
39
40
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
41
    // Load Toolbars and parse them into TinyMCE
42
    $toolbarsFromFile = getToolbarsFromJson();
43
    if ($toolbarsFromFile) {
44
        $toolbars = [];
45
        foreach ($toolbars as $name => $toolbar) {
46
            array_unshift($toolbar, []);
47
            $toolbars[$name] = $toolbar;
48
        }
49
    }
50
51
    return $toolbars;
52
});
53
54
function getToolbarsFromJson()
55
{
56
    $options = Feature::getOptions('flynt-tiny-mce');
57
    if (isset($options[0]) && isset($options[0]['toolbarsConfigPath'])) {
58
        $configPath = $options[0]['toolbarsConfigPath'];
59
    } else {
60
        $configPath = 'config/toolbars.json';
61
    }
62
63
    $filePath = get_template_directory() . '/Features/TinyMce/' . $configPath;
64
    if (file_exists($filePath)) {
65
        return json_decode(file_get_contents($filePath), true);
66
    } else {
67
        return false;
68
    }
69
}
70