|
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) { |
|
|
|
|
|
|
12
|
|
|
$toolbars = getToolbars(); |
|
13
|
|
|
return $toolbars['default'][0]; |
|
14
|
|
|
}); |
|
15
|
|
|
|
|
16
|
|
|
// Second Bar |
|
17
|
|
|
add_filter('mce_buttons_2', function ($buttons) { |
|
|
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.