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

functions.php ➔ getToolbarsFromJson()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 5
Ratio 31.25 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 0
dl 5
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'][0];
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
    $options = Feature::getOptions('flynt-tiny-mce');
22
23
    if (isset($options[0]) && isset($options[0]['blockformatsConfigPath'])) {
24
        $configPath = $options[0]['blockformatsConfigPath'];
25
    } else {
26
        $configPath = 'config/blockformats.json';
27
    }
28
29
    $filePath = get_template_directory() . '/Features/TinyMce/' . $configPath;
30
    if (file_exists($filePath)) {
31
        $blockstyles = json_decode(file_get_contents($filePath), true);
32
        $blockstylesQueries = [];
33
        foreach ($blockstyles as $label => $tag) {
34
            $blockstylesQueries[] = $label . '=' . $tag;
35
        }
36
        $init['block_formats'] = implode(';', $blockstylesQueries);
37
    } else {
38
        $init['block_formats'] = 'Paragraph=p;Heading 1=h1;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6';
39
    }
40
    
41
    // Load Styleformat Dropdown and parse it into TinyMCE
42
    
43 View Code Duplication
    if (isset($options[0]) && isset($options[0]['styleformatsConfigPath'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
        $configPath = $options[0]['styleformatsConfigPath'];
45
    } else {
46
        $configPath = 'config/toolbars.json';
47
    }
48
49
    $filePath = get_template_directory() . '/Features/TinyMce/' . $configPath;
50
    if (file_exists($filePath)) {
51
        // Get contents as JSON string first and convert it to array for sending it to style_formats as true js array
52
        $loadedStyle = json_decode(file_get_contents($filePath), true);
53
        $init['style_formats'] = json_encode($loadedStyle);
54
    }
55
    return $init;
56
});
57
58
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
59
    // Load Toolbars and parse them into TinyMCE
60
    $toolbarsFromFile = getToolbarsFromJson();
61
    if ($toolbarsFromFile) {
62
        $toolbars = [];
63
        foreach ($toolbars as $name => $toolbar) {
64
            array_unshift($toolbar, []);
65
            $toolbars[$name] = $toolbar;
66
        }
67
    }
68
69
    return $toolbars;
70
});
71
72
function getToolbarsFromJson()
73
{
74
    $options = Feature::getOptions('flynt-tiny-mce');
75 View Code Duplication
    if (isset($options[0]) && isset($options[0]['toolbarsConfigPath'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
        $configPath = $options[0]['toolbarsConfigPath'];
77
    } else {
78
        $configPath = 'config/toolbars.json';
79
    }
80
81
    $filePath = get_template_directory() . '/Features/TinyMce/' . $configPath;
82
    if (file_exists($filePath)) {
83
        return json_decode(file_get_contents($filePath), true);
84
    } else {
85
        return false;
86
    }
87
}
88