forked from wp-plugins/tinymce-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinymce-advanced.php
588 lines (466 loc) · 17.3 KB
/
tinymce-advanced.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
<?php
/*
Plugin Name: TinyMCE Advanced
Plugin URI: http://www.laptoptips.ca/projects/tinymce-advanced/
Description: Enables advanced features and plugins in TinyMCE, the visual editor in WordPress.
Version: 4.1.9
Author: Andrew Ozz
Author URI: http://www.laptoptips.ca/
Text Domain: tinymce-advanced
Domain Path: /langs/
Released under the GPL version 2.0, http://www.gnu.org/licenses/gpl-2.0.html
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License version 2.0 for more details.
*/
if ( ! class_exists('Tinymce_Advanced') ) :
class Tinymce_Advanced {
private $required_version = '4.2';
private $settings;
private $admin_settings;
private $admin_options;
private $plugins;
private $options;
private $toolbar_1;
private $toolbar_2;
private $toolbar_3;
private $toolbar_4;
private $used_buttons = array();
private $all_buttons = array();
private $buttons_filter = array();
private $all_plugins = array(
'advlist',
'anchor',
'code',
'contextmenu',
'emoticons',
'importcss',
'insertdatetime',
'nonbreaking',
'print',
'searchreplace',
'table',
'visualblocks',
'visualchars',
'link',
'textpattern',
);
private $default_settings = array(
'options' => 'menubar,advlist',
'toolbar_1' => 'bold,italic,blockquote,bullist,numlist,alignleft,aligncenter,alignright,link,unlink,table,fullscreen,undo,redo,wp_adv',
'toolbar_2' => 'formatselect,alignjustify,strikethrough,outdent,indent,pastetext,removeformat,charmap,wp_more,emoticons,forecolor,wp_help',
'toolbar_3' => '',
'toolbar_4' => '',
'plugins' => 'anchor,code,insertdatetime,nonbreaking,print,searchreplace,table,visualblocks,visualchars,emoticons,advlist',
);
private $default_admin_settings = array( 'options' => array() );
function __construct() {
// Don't run outside of WP
if ( ! defined('ABSPATH') ) {
return;
}
add_action( 'plugins_loaded', array( &$this, 'set_paths' ), 50 );
if ( is_admin() ) {
add_action( 'admin_menu', array( &$this, 'add_menu' ) );
add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
}
add_filter( 'mce_buttons', array( &$this, 'mce_buttons_1' ), 999, 2 );
add_filter( 'mce_buttons_2', array( &$this, 'mce_buttons_2' ), 999 );
add_filter( 'mce_buttons_3', array( &$this, 'mce_buttons_3' ), 999 );
add_filter( 'mce_buttons_4', array( &$this, 'mce_buttons_4' ), 999 );
add_filter( 'tiny_mce_before_init', array( &$this, 'mce_options' ) );
add_filter( 'htmledit_pre', array( &$this, 'htmledit' ), 999 );
add_filter( 'mce_external_plugins', array( &$this, 'mce_external_plugins' ), 999 );
add_filter( 'tiny_mce_plugins', array( &$this, 'tiny_mce_plugins' ), 999 );
add_action( 'after_wp_tiny_mce', array( &$this, 'after_wp_tiny_mce' ) );
add_action( 'before_wp_tiny_mce', array( &$this, 'show_version_warning' ) );
}
// When using a plugin that changes the paths dinamically, set these earlier than 'plugins_loaded' 50.
function set_paths() {
if ( ! defined( 'TADV_URL' ) )
define( 'TADV_URL', plugin_dir_url( __FILE__ ) );
if ( ! defined( 'TADV_PATH' ) )
define( 'TADV_PATH', plugin_dir_path( __FILE__ ) );
}
function enqueue_scripts( $page ) {
if ( 'settings_page_tinymce-advanced' == $page ) {
wp_enqueue_script( 'tadv-js', TADV_URL . 'js/tadv.js', array( 'jquery-ui-sortable' ), '4.0', true );
wp_enqueue_style( 'tadv-mce-skin', includes_url( 'js/tinymce/skins/lightgray/skin.min.css' ), array(), '4.0' );
wp_enqueue_style( 'tadv-css', TADV_URL . 'css/tadv-styles.css', array( 'editor-buttons' ), '4.0' );
add_action( 'admin_footer', array( &$this, 'load_mce_translation' ) );
}
}
function load_mce_translation() {
if ( ! class_exists( '_WP_Editors' ) ) {
require( ABSPATH . WPINC . '/class-wp-editor.php' );
}
?>
<script>var tadvTranslation = <?php echo _WP_Editors::wp_mce_translation( '', true ); ?>;</script>
<?php
}
function load_settings() {
if ( empty( $_POST ) ) {
$this->check_plugin_version();
}
if ( empty( $this->settings ) ) {
$this->admin_settings = get_option( 'tadv_admin_settings', false );
$this->settings = get_option( 'tadv_settings', false );
}
// load defaults if the options don't exist...
if ( $this->admin_settings === false )
$this->admin_settings = $this->default_admin_settings;
$this->admin_options = ! empty( $this->admin_settings['options'] ) ? explode( ',', $this->admin_settings['options'] ) : array();
if ( $this->settings === false )
$this->settings = $this->default_settings;
$this->options = ! empty( $this->settings['options'] ) ? explode( ',', $this->settings['options'] ) : array();
$this->plugins = ! empty( $this->settings['plugins'] ) ? explode( ',', $this->settings['plugins'] ) : array();
$this->toolbar_1 = ! empty( $this->settings['toolbar_1'] ) ? explode( ',', $this->settings['toolbar_1'] ) : array();
$this->toolbar_2 = ! empty( $this->settings['toolbar_2'] ) ? explode( ',', $this->settings['toolbar_2'] ) : array();
$this->toolbar_3 = ! empty( $this->settings['toolbar_3'] ) ? explode( ',', $this->settings['toolbar_3'] ) : array();
$this->toolbar_4 = ! empty( $this->settings['toolbar_4'] ) ? explode( ',', $this->settings['toolbar_4'] ) : array();
$this->used_buttons = array_merge( $this->toolbar_1, $this->toolbar_2, $this->toolbar_3, $this->toolbar_4 );
$this->get_all_buttons();
}
public function show_version_warning() {
if ( is_admin() && current_user_can( 'update_plugins' ) && get_current_screen()->base === 'post' ) {
$this->warn_if_unsupported();
}
}
public function warn_if_unsupported() {
if ( ! $this->check_minimum_supported_version() ) {
$wp_ver = ! empty( $GLOBALS['wp_version'] ) ? $GLOBALS['wp_version'] : '(undefined)';
?>
<div class="error"><p>
<?php
printf( __( 'TinyMCE Advanced requires WordPress version %1$s or newer. It appears that you are running %2$s. This can make the editor unstable.', 'tinymce-advanced' ),
$this->required_version,
esc_html( $wp_ver )
);
echo '<br>';
printf( __( 'Please upgrade your WordPress installation or download an <a href="%s">older version of the plugin</a>.', 'tinymce-advanced' ),
'https://wordpress.org/plugins/tinymce-advanced/download/'
);
?>
</p></div>
<?php
}
}
// Min version
private function check_minimum_supported_version() {
$wp_version = isset( $GLOBALS['wp_version'] ) ? $GLOBALS['wp_version'] : '0';
$wp_version = str_replace( '-src', '', $wp_version );
return ( version_compare( $wp_version, $this->required_version, '>=' ) );
}
private function check_plugin_version() {
$version = get_option( 'tadv_version', 0 );
if ( ! $version || $version < 4000 ) {
// First install or upgrade to TinyMCE 4.0
$this->settings = $this->default_settings;
$this->admin_settings = $this->default_admin_settings;
update_option( 'tadv_settings', $this->settings );
update_option( 'tadv_admin_settings', $this->admin_settings );
update_option( 'tadv_version', 4000 );
}
if ( $version < 4000 ) {
// Upgrade to TinyMCE 4.0, clean options
delete_option('tadv_options');
delete_option('tadv_toolbars');
delete_option('tadv_plugins');
delete_option('tadv_btns1');
delete_option('tadv_btns2');
delete_option('tadv_btns3');
delete_option('tadv_btns4');
delete_option('tadv_allbtns');
}
}
function get_all_buttons() {
if ( ! empty( $this->all_buttons ) )
return $this->all_buttons;
$buttons = array(
// Core
'bold' => 'Bold',
'italic' => 'Italic',
'underline' => 'Underline',
'strikethrough' => 'Strikethrough',
'alignleft' => 'Align left',
'aligncenter' => 'Align center',
'alignright' => 'Align right',
'alignjustify' => 'Justify',
'styleselect' => 'Formats',
'formatselect' => 'Paragraph',
'fontselect' => 'Font Family',
'fontsizeselect' => 'Font Sizes',
'cut' => 'Cut',
'copy' => 'Copy',
'paste' => 'Paste',
'bullist' => 'Bulleted list',
'numlist' => 'Numbered list',
'outdent' => 'Decrease indent',
'indent' => 'Increase indent',
'blockquote' => 'Blockquote',
'undo' => 'Undo',
'redo' => 'Redo',
'removeformat' => 'Clear formatting',
'subscript' => 'Subscript',
'superscript' => 'Superscript',
// From plugins
'hr' => 'Horizontal line',
'link' => 'Insert/edit link',
'unlink' => 'Remove link',
'image' => 'Insert/edit image',
'charmap' => 'Special character',
'pastetext' => 'Paste as text',
'print' => 'Print',
'anchor' => 'Anchor',
'searchreplace' => 'Find and replace',
'visualblocks' => 'Show blocks',
'visualchars' => 'Show invisible characters',
'code' => 'Source code',
'wp_code' => 'Code',
'fullscreen' => 'Fullscreen',
'insertdatetime' => 'Insert date/time',
'media' => 'Insert/edit video',
'nonbreaking' => 'Nonbreaking space',
'table' => 'Table',
'ltr' => 'Left to right',
'rtl' => 'Right to left',
'emoticons' => 'Emoticons',
'forecolor' => 'Text color',
'backcolor' => 'Background color',
// Layer plugin ?
// 'insertlayer' => 'Layer',
// WP
'wp_adv' => 'Toolbar Toggle',
'wp_help' => 'Keyboard Shortcuts',
'wp_more' => 'Read more...',
'wp_page' => 'Page break',
);
// add/remove allowed buttons
$buttons = apply_filters( 'tadv_allowed_buttons', $buttons );
$this->all_buttons = $buttons;
$this->buttons_filter = array_keys( $buttons );
return $buttons;
}
function get_plugins( $plugins = array() ) {
if ( ! is_array( $this->used_buttons ) ) {
$this->load_settings();
}
if ( in_array( 'anchor', $this->used_buttons, true ) )
$plugins[] = 'anchor';
if ( in_array( 'visualchars', $this->used_buttons, true ) )
$plugins[] = 'visualchars';
if ( in_array( 'visualblocks', $this->used_buttons, true ) )
$plugins[] = 'visualblocks';
if ( in_array( 'nonbreaking', $this->used_buttons, true ) )
$plugins[] = 'nonbreaking';
if ( in_array( 'emoticons', $this->used_buttons, true ) )
$plugins[] = 'emoticons';
if ( in_array( 'insertdatetime', $this->used_buttons, true ) )
$plugins[] = 'insertdatetime';
if ( in_array( 'table', $this->used_buttons, true ) )
$plugins[] = 'table';
if ( in_array( 'print', $this->used_buttons, true ) )
$plugins[] = 'print';
if ( in_array( 'searchreplace', $this->used_buttons, true ) )
$plugins[] = 'searchreplace';
if ( in_array( 'insertlayer', $this->used_buttons, true ) )
$plugins[] = 'layer';
// From options
if ( $this->check_setting( 'advlist' ) )
$plugins[] = 'advlist';
if ( $this->check_setting( 'advlink' ) )
$plugins[] = 'link';
if ( $this->check_admin_setting( 'importcss' ) )
$plugins[] = 'importcss';
if ( $this->check_setting( 'contextmenu' ) )
$plugins[] = 'contextmenu';
if ( $this->check_admin_setting( 'textpattern' ) )
$plugins[] = 'textpattern';
// add/remove used plugins
$plugins = apply_filters( 'tadv_used_plugins', $plugins, $this->used_buttons );
return array_unique( $plugins );
}
private function check_setting( $setting, $admin = false ) {
if ( ! is_array( $this->options ) ) {
$this->load_settings();
}
$array = $admin ? $this->admin_options : $this->options;
return in_array( $setting, $array, true );
}
private function check_admin_setting( $setting ) {
return $this->check_setting( $setting, true );
}
function mce_buttons_1( $original, $editor_id ) {
if ( ! is_array( $this->options ) ) {
$this->load_settings();
}
$buttons_1 = $this->toolbar_1;
if ( is_array( $original ) && ! empty( $original ) ) {
$original = array_diff( $original, $this->buttons_filter );
$buttons_1 = array_merge( $buttons_1, $original );
}
return $buttons_1;
}
function mce_buttons_2( $original ) {
if ( ! is_array( $this->options ) ) {
$this->load_settings();
}
$buttons_2 = $this->toolbar_2;
if ( is_array( $original ) && ! empty( $original ) ) {
$original = array_diff( $original, $this->buttons_filter );
$buttons_2 = array_merge( $buttons_2, $original );
}
return $buttons_2;
}
function mce_buttons_3( $original ) {
if ( ! is_array( $this->options ) ) {
$this->load_settings();
}
$buttons_3 = $this->toolbar_3;
if ( is_array( $original ) && ! empty( $original ) ) {
$original = array_diff( $original, $this->buttons_filter );
$buttons_3 = array_merge( $buttons_3, $original );
}
return $buttons_3;
}
function mce_buttons_4( $original ) {
if ( ! is_array( $this->options ) ) {
$this->load_settings();
}
$buttons_4 = $this->toolbar_4;
if ( is_array( $original ) && ! empty( $original ) ) {
$original = array_diff( $original, $this->buttons_filter );
$buttons_4 = array_merge( $buttons_4, $original );
}
return $buttons_4;
}
function mce_options( $init ) {
if ( $this->check_admin_setting( 'no_autop' ) ) {
$init['wpautop'] = false;
// $init['indent'] = true;
$init['tadv_noautop'] = true;
}
if ( $this->check_setting('menubar') ) {
$init['menubar'] = true;
}
if ( $this->check_setting('image') ) {
$init['image_advtab'] = true;
}
if ( $this->check_setting( 'advlink' ) ) {
$init['rel_list'] = '[{text: "None", value: ""}, {text: "Nofollow", value: "nofollow"}]';
}
if ( ! in_array( 'wp_adv', $this->toolbar_1, true ) ) {
$init['wordpress_adv_hidden'] = false;
}
if ( $this->check_admin_setting( 'importcss' ) ) {
// $init['importcss_selector_filter'] = 'function(sel){return /^\.[a-z0-9]+$/i.test(sel);}';
$init['importcss_file_filter'] = 'editor-style.css';
}
if ( $this->check_admin_setting( 'fontsize_formats' ) ) {
$init['fontsize_formats'] = '8px 10px 12px 14px 16px 20px 24px 28px 32px 36px';
}
if ( $this->check_setting( 'paste_images' ) ) {
$init['paste_data_images'] = true;
$init['paste_word_valid_elements'] = '-strong/b,-em/i,-span,-p,-ol,-ul,-li,-h1,-h2,-h3,-h4,-h5,-h6,-p/div,-a[href|name],' .
'-table[width],-tr,-td[colspan|rowspan|width],-th,-thead,-tfoot,-tbody,sub,sup,strike,br,del,ins,img[src|alt|title|height|width]';
}
return $init;
}
function after_wp_tiny_mce() {
?>
<style type="text/css">
.wp-fullscreen-wrap .mce-menubar { position: static !important; width: auto !important; }
#wp-content-wrap .mce-tinymce.mce-fullscreen .mce-wp-dfw { display: none; }
</style>
<?php
}
function htmledit( $c ) {
if ( $this->check_admin_setting( 'no_autop' ) ) {
$c = str_replace( array('&', '<', '>'), array('&', '<', '>'), $c );
$c = wpautop( $c );
$c = preg_replace( '/^<p>(https?:\/\/[^<> "]+?)<\/p>$/im', '$1', $c );
$c = htmlspecialchars( $c, ENT_NOQUOTES, get_option( 'blog_charset' ) );
}
return $c;
}
function mce_external_plugins( $mce_plugins ) {
// import user created editor-style.css
if ( $this->check_admin_setting( 'editorstyle' ) ) {
add_editor_style();
}
if ( ! is_array( $this->plugins ) ) {
$this->plugins = array();
}
if ( $this->check_admin_setting( 'no_autop' ) ) {
$this->plugins[] = 'wptadv';
}
$plugpath = TADV_URL . 'mce/';
$mce_plugins = (array) $mce_plugins;
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
foreach ( $this->plugins as $plugin ) {
$mce_plugins["$plugin"] = $plugpath . $plugin . "/plugin{$suffix}.js";
}
return $mce_plugins;
}
function tiny_mce_plugins( $plugins ) {
// This calls load_settings()
if ( $this->check_setting('image') && ! in_array( 'image', $plugins, true ) ) {
$plugins[] = 'image';
}
if ( ( in_array( 'rtl', $this->used_buttons, true ) || in_array( 'ltr', $this->used_buttons, true ) ) &&
! in_array( 'directionality', (array) $plugins, true ) ) {
$plugins[] = 'directionality';
}
return $plugins;
}
private function parse_buttons( $toolbar_id = false, $buttons = false ) {
if ( $toolbar_id && ! $buttons && ! empty( $_POST[$toolbar_id] ) )
$buttons = $_POST[$toolbar_id];
if ( is_array( $buttons ) ) {
$_buttons = array_map( array( @$this, 'filter_name' ), $buttons );
return implode( ',', array_filter( $_buttons ) );
}
return '';
}
private function filter_name( $str ) {
if ( empty( $str ) || ! is_string( $str ) )
return '';
// Button names
return preg_replace( '/[^a-z0-9_]/i', '', $str );
}
private function sanitize_settings( $settings ) {
$_settings = array();
if ( ! is_array( $settings ) ) {
return $_settings;
}
foreach( $settings as $name => $value ) {
$name = preg_replace( '/[^a-z0-9_]+/', '', $name );
if ( strpos( $name, 'toolbar_' ) === 0 ) {
$_settings[$name] = $this->parse_buttons( false, explode( ',', $value ) );
} else if ( 'options' === $name || 'plugins' === $name || 'disabled_plugins' === $name ) {
$_settings[$name] = preg_replace( '/[^a-z0-9_,]+/', '', $value );
}
}
return $_settings;
}
function settings_page() {
if ( ! defined( 'TADV_ADMIN_PAGE' ) ) {
define( 'TADV_ADMIN_PAGE', true );
}
include_once( TADV_PATH . 'tadv_admin.php' );
}
function add_menu() {
add_options_page( 'TinyMCE Advanced', 'TinyMCE Advanced', 'manage_options', 'tinymce-advanced', array( &$this, 'settings_page' ) );
}
}
new Tinymce_Advanced;
endif;
// Add settings link on plugin page
function mce_settings_link($links) {
$settings_link = '<a href="options-general.php?page=tinymce-advanced">' . __('Settings') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'mce_settings_link' );