|
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
|
|
|
$config = getConfig(); |
|
13
|
|
|
if ($config && isset($config['toolbars'])) { |
|
14
|
|
|
$toolbars = $config['toolbars']; |
|
15
|
|
|
if (isset($toolbars['default']) && isset($toolbars['default'][0])) { |
|
16
|
|
|
return $toolbars['default'][0]; |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
return $buttons; |
|
20
|
|
|
}); |
|
21
|
|
|
|
|
22
|
|
|
// Second Bar |
|
23
|
|
|
add_filter('mce_buttons_2', function ($buttons) { |
|
|
|
|
|
|
24
|
|
|
return []; |
|
25
|
|
|
}); |
|
26
|
|
|
|
|
27
|
|
|
add_filter('tiny_mce_before_init', function ($init) { |
|
28
|
|
|
$config = getConfig(); |
|
29
|
|
|
if ($config) { |
|
30
|
|
|
if (isset($config['blockformats'])) { |
|
31
|
|
|
$init['block_formats'] = getBlockFormats($config['blockformats']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (isset($config['styleformats'])) { |
|
35
|
|
|
// Send it to style_formats as true js array |
|
36
|
|
|
$init['style_formats'] = json_encode($config['styleformats']); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $init; |
|
41
|
|
|
}); |
|
42
|
|
|
|
|
43
|
|
|
// TODO: refactor this |
|
44
|
|
|
add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) { |
|
45
|
|
|
// Load Toolbars and parse them into TinyMCE |
|
46
|
|
|
$config = getConfig(); |
|
47
|
|
|
if ($config && isset($config['toolbars'])) { |
|
48
|
|
|
$toolbarsFromFile = $config['toolbars']; |
|
49
|
|
|
if (!empty($toolbarsFromFile)) { |
|
50
|
|
|
$toolbars = []; |
|
51
|
|
|
foreach ($toolbars as $name => $toolbar) { |
|
52
|
|
|
array_unshift($toolbar, []); |
|
53
|
|
|
$toolbars[$name] = $toolbar; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $toolbars; |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
function getConfig() |
|
62
|
|
|
{ |
|
63
|
|
|
$filePath = Asset::requirePath('/Features/TinyMce/config.json'); |
|
64
|
|
|
if (file_exists($filePath)) { |
|
65
|
|
|
return json_decode(file_get_contents($filePath), true); |
|
66
|
|
|
} else { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function getBlockFormats($blockFormats) |
|
72
|
|
|
{ |
|
73
|
|
|
if (!empty($blockFormats)) { |
|
74
|
|
|
$blockFormatStrings = array_map(function ($tag, $label) { |
|
75
|
|
|
return "${label}=${tag}"; |
|
76
|
|
|
}, $blockFormats, array_keys($blockFormats)); |
|
77
|
|
|
return implode($blockFormatStrings, ';'); |
|
78
|
|
|
} |
|
79
|
|
|
return ''; |
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.