Completed
Pull Request — master (#158)
by Doğa
29:40 queued 25:45
created

functions.php ➔ getBlockFormats()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 6
nop 1
dl 0
loc 20
rs 8.8571
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
    $toolbarsFromFile = getToolbarsFromJson();
13
    return $toolbarsFromFile['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
    $options = Feature::getOptions('flynt-tiny-mce');
23
24
    $init['block_formats'] = getBlockFormats($options);
25
    $init['style_formats'] = getStyleFormats($options);
26
    
27
    return $init;
28
});
29
30
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) {
31
    // Load Toolbars and parse them into TinyMCE
32
    $toolbarsFromFile = getToolbarsFromJson();
33
    if ($toolbarsFromFile) {
34
        $toolbars = [];
35
        foreach ($toolbars as $name => $toolbar) {
36
            array_unshift($toolbar, []);
37
            $toolbars[$name] = $toolbar;
38
        }
39
    }
40
41
    return $toolbars;
42
});
43
44 View Code Duplication
function getToolbarsFromJson()
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
45
{
46
    $options = Feature::getOptions('flynt-tiny-mce');
47
    if (isset($options[0]) && isset($options[0]['toolbarsConfigPath'])) {
48
        $configPath = $options[0]['paths']['toolbarsConfigPath'];
49
    } else {
50
        $configPath = 'config/toolbars.json';
51
    }
52
53
    $filePath = Asset::requirePath('/Features/TinyMce/' . $configPath);
54
    if (file_exists($filePath)) {
55
        return json_decode(file_get_contents($filePath), true);
56
    } else {
57
        return false;
58
    }
59
}
60
61
function getBlockFormats($options)
62
{
63
    if (isset($options[0]) && isset($options[0]['blockformatsConfigPath'])) {
64
        $configPath = $options[0]['paths']['blockformatsConfigPath'];
65
    } else {
66
        $configPath = 'config/blockformats.json';
67
    }
68
69
    $filePath = Asset::requirePath('/Features/TinyMce/' . $configPath);
70
    if (file_exists($filePath)) {
71
        $blockstyles = json_decode(file_get_contents($filePath), true);
72
        $blockstylesQueries = [];
73
        foreach ($blockstyles as $label => $tag) {
74
            $blockstylesQueries[] = $label . '=' . $tag;
75
        }
76
        return implode(';', $blockstylesQueries);
77
    } else {
78
        return 'Paragraph=p;Heading 1=h1;Heading 2=h2;Heading 3=h3;Heading 4=h4;Heading 5=h5;Heading 6=h6';
79
    }
80
}
81
82 View Code Duplication
function getStyleFormats($options)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
83
{
84
    if (isset($options[0]) && isset($options[0]['styleformatsConfigPath'])) {
85
        $configPath = $options[0]['paths']['styleformatsConfigPath'];
86
    } else {
87
        $configPath = 'config/styleformats.json';
88
    }
89
90
    $filePath = Asset::requirePath('/Features/TinyMce/' . $configPath);
91
    if (file_exists($filePath)) {
92
        // Get contents as JSON string first and convert it to array for sending it to style_formats as true js array
93
        $loadedStyle = json_decode(file_get_contents($filePath), true);
94
        return json_encode($loadedStyle);
95
    } else {
96
        return false;
97
    }
98
}
99