flyntwp /
flynt
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Moves most relevant editor buttons to the first toolbar |
||
| 5 | * and provides config for creating new toolbars, block formats, and style formats. |
||
| 6 | * See the TinyMce documentation for more information: https://www.tiny.cloud/docs/ |
||
| 7 | * |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace Flynt\TinyMce; |
||
| 11 | |||
| 12 | // First Toolbar |
||
| 13 | add_filter('mce_buttons', function ($buttons) { |
||
| 14 | $config = getConfig(); |
||
| 15 | if ($config && isset($config['toolbars'])) { |
||
| 16 | $toolbars = $config['toolbars']; |
||
| 17 | if (isset($toolbars['default']) && isset($toolbars['default'][0])) { |
||
| 18 | return $toolbars['default'][0]; |
||
| 19 | } |
||
| 20 | } |
||
| 21 | return $buttons; |
||
| 22 | }); |
||
| 23 | |||
| 24 | // Second Toolbar |
||
| 25 | add_filter('mce_buttons_2', function ($buttons) { |
||
| 26 | return []; |
||
| 27 | }); |
||
| 28 | |||
| 29 | add_filter('tiny_mce_before_init', function ($init) { |
||
| 30 | $config = getConfig(); |
||
| 31 | if ($config) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 32 | if (isset($config['blockformats'])) { |
||
| 33 | $init['block_formats'] = getBlockFormats($config['blockformats']); |
||
| 34 | } |
||
| 35 | |||
| 36 | if (isset($config['styleformats'])) { |
||
| 37 | // Send it to style_formats as true js array |
||
| 38 | $init['style_formats'] = json_encode($config['styleformats']); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | return $init; |
||
| 42 | }); |
||
| 43 | |||
| 44 | add_filter('acf/fields/wysiwyg/toolbars', function ($toolbars) { |
||
| 45 | // Load Toolbars and parse them into TinyMCE |
||
| 46 | $config = getConfig(); |
||
| 47 | if ($config && !empty($config['toolbars'])) { |
||
| 48 | $toolbars = array_map(function ($toolbar) { |
||
| 49 | array_unshift($toolbar, []); |
||
| 50 | return $toolbar; |
||
| 51 | }, $config['toolbars']); |
||
| 52 | } |
||
| 53 | return $toolbars; |
||
| 54 | }); |
||
| 55 | |||
| 56 | function getBlockFormats($blockFormats) |
||
| 57 | { |
||
| 58 | if (!empty($blockFormats)) { |
||
| 59 | $blockFormatStrings = array_map( |
||
| 60 | function ($tag, $label) { |
||
| 61 | return "${label}=${tag}"; |
||
| 62 | }, |
||
| 63 | $blockFormats, |
||
| 64 | array_keys($blockFormats) |
||
| 65 | ); |
||
| 66 | return implode(';', $blockFormatStrings); |
||
| 67 | } |
||
| 68 | return ''; |
||
| 69 | } |
||
| 70 | |||
| 71 | function getConfig() |
||
| 72 | { |
||
| 73 | return [ |
||
| 74 | 'blockformats' => [ |
||
| 75 | 'Paragraph' => 'p', |
||
| 76 | 'Heading 1' => 'h1', |
||
| 77 | 'Heading 2' => 'h2', |
||
| 78 | 'Heading 3' => 'h3', |
||
| 79 | 'Heading 4' => 'h4', |
||
| 80 | 'Heading 5' => 'h5', |
||
| 81 | 'Heading 6' => 'h6' |
||
| 82 | ], |
||
| 83 | 'styleformats' => [ |
||
| 84 | [ |
||
| 85 | 'title' => 'Headings', |
||
| 86 | 'icon' => '', |
||
| 87 | 'items' => [ |
||
| 88 | [ |
||
| 89 | 'title' => 'Heading 1', |
||
| 90 | 'classes' => 'h1', |
||
| 91 | 'selector' => '*' |
||
| 92 | ], |
||
| 93 | [ |
||
| 94 | 'title' => 'Heading 2', |
||
| 95 | 'classes' => 'h2', |
||
| 96 | 'selector' => '*' |
||
| 97 | ], |
||
| 98 | [ |
||
| 99 | 'title' => 'Heading 3', |
||
| 100 | 'classes' => 'h3', |
||
| 101 | 'selector' => '*' |
||
| 102 | ], |
||
| 103 | [ |
||
| 104 | 'title' => 'Heading 4', |
||
| 105 | 'classes' => 'h4', |
||
| 106 | 'selector' => '*' |
||
| 107 | ], |
||
| 108 | [ |
||
| 109 | 'title' => 'Heading 5', |
||
| 110 | 'classes' => 'h5', |
||
| 111 | 'selector' => '*' |
||
| 112 | ], |
||
| 113 | [ |
||
| 114 | 'title' => 'Heading 6', |
||
| 115 | 'classes' => 'h6', |
||
| 116 | 'selector' => '*' |
||
| 117 | ], |
||
| 118 | ] |
||
| 119 | ], |
||
| 120 | [ |
||
| 121 | 'title' => 'Buttons', |
||
| 122 | 'icon' => '', |
||
| 123 | 'items' => [ |
||
| 124 | [ |
||
| 125 | 'title' => 'Button', |
||
| 126 | 'classes' => 'button', |
||
| 127 | 'selector' => 'a,button' |
||
| 128 | ], |
||
| 129 | [ |
||
| 130 | 'title' => 'Button Ghost', |
||
| 131 | 'classes' => 'button--ghost', |
||
| 132 | 'selector' => '.button' |
||
| 133 | ], |
||
| 134 | [ |
||
| 135 | 'title' => 'Button Small', |
||
| 136 | 'classes' => 'button--small', |
||
| 137 | 'selector' => '.button' |
||
| 138 | ], |
||
| 139 | [ |
||
| 140 | 'title' => 'Button Link', |
||
| 141 | 'classes' => 'button--link', |
||
| 142 | 'selector' => '.button' |
||
| 143 | ] |
||
| 144 | ] |
||
| 145 | ], |
||
| 146 | [ |
||
| 147 | 'title' => 'Icon Lists', |
||
| 148 | 'icon' => '', |
||
| 149 | 'items' => [ |
||
| 150 | [ |
||
| 151 | 'title' => 'Check Circle', |
||
| 152 | 'classes' => 'iconList iconList--checkCircle', |
||
| 153 | 'selector' => 'ul,ol' |
||
| 154 | ] |
||
| 155 | ] |
||
| 156 | ] |
||
| 157 | ], |
||
| 158 | 'toolbars' => [ |
||
| 159 | 'default' => [ |
||
| 160 | [ |
||
| 161 | 'formatselect', |
||
| 162 | 'styleselect', |
||
| 163 | 'bold', |
||
| 164 | 'italic', |
||
| 165 | 'strikethrough', |
||
| 166 | 'blockquote', |
||
| 167 | '|', |
||
| 168 | 'bullist', |
||
| 169 | 'numlist', |
||
| 170 | '|', |
||
| 171 | 'link', |
||
| 172 | 'unlink', |
||
| 173 | '|', |
||
| 174 | 'pastetext', |
||
| 175 | 'removeformat', |
||
| 176 | '|', |
||
| 177 | 'undo', |
||
| 178 | 'redo', |
||
| 179 | 'fullscreen' |
||
| 180 | ] |
||
| 181 | ], |
||
| 182 | 'basic' => [ |
||
| 183 | [ |
||
| 184 | 'bold', |
||
| 185 | 'italic', |
||
| 186 | 'strikethrough', |
||
| 187 | '|', |
||
| 188 | 'link', |
||
| 189 | 'unlink', |
||
| 190 | '|', |
||
| 191 | 'undo', |
||
| 192 | 'redo', |
||
| 193 | 'fullscreen' |
||
| 194 | ] |
||
| 195 | ] |
||
| 196 | ] |
||
| 197 | ]; |
||
| 198 | } |
||
| 199 |