521c0ee67b93f7163a14197a79df2d26cdc2e611
[autoinstalls/wordpress.git] / wp-includes / class-wp-editor.php
1 <?php
2 /**
3  * Facilitates adding of the WordPress editor as used on the Write and Edit screens.
4  *
5  * @package WordPress
6  * @since 3.3.0
7  *
8  * Private, not included by default. See wp_editor() in wp-includes/general-template.php.
9  */
10
11 final class _WP_Editors {
12         public static $mce_locale;
13
14         private static $mce_settings = array();
15         private static $qt_settings = array();
16         private static $plugins = array();
17         private static $qt_buttons = array();
18         private static $ext_plugins;
19         private static $baseurl;
20         private static $first_init;
21         private static $this_tinymce = false;
22         private static $this_quicktags = false;
23         private static $has_tinymce = false;
24         private static $has_quicktags = false;
25         private static $has_medialib = false;
26         private static $editor_buttons_css = true;
27         private static $drag_drop_upload = false;
28
29         private function __construct() {}
30
31         /**
32          * Parse default arguments for the editor instance.
33          *
34          * @param string $editor_id ID for the current editor instance.
35          * @param array  $settings {
36          *     Array of editor arguments.
37          *
38          *     @type bool       $wpautop           Whether to use wpautop(). Default true.
39          *     @type bool       $media_buttons     Whether to show the Add Media/other media buttons.
40          *     @type string     $default_editor    When both TinyMCE and Quicktags are used, set which
41          *                                         editor is shown on page load. Default empty.
42          *     @type bool       $drag_drop_upload  Whether to enable drag & drop on the editor uploading. Default false.
43          *                                         Requires the media modal.
44          *     @type string     $textarea_name     Give the textarea a unique name here. Square brackets
45          *                                         can be used here. Default $editor_id.
46          *     @type int        $textarea_rows     Number rows in the editor textarea. Default 20.
47          *     @type string|int $tabindex          Tabindex value to use. Default empty.
48          *     @type string     $tabfocus_elements The previous and next element ID to move the focus to
49          *                                         when pressing the Tab key in TinyMCE. Defualt ':prev,:next'.
50          *     @type string     $editor_css        Intended for extra styles for both Visual and Text editors.
51          *                                         Should include `<style>` tags, and can use "scoped". Default empty.
52          *     @type string     $editor_class      Extra classes to add to the editor textarea elemen. Default empty.
53          *     @type bool       $teeny             Whether to output the minimal editor config. Examples include
54          *                                         Press This and the Comment editor. Default false.
55          *     @type bool       $dfw               Whether to replace the default fullscreen with "Distraction Free
56          *                                         Writing". DFW requires specific DOM elements and css). Default false.
57          *     @type bool|array $tinymce           Whether to load TinyMCE. Can be used to pass settings directly to
58          *                                         TinyMCE using an array. Default true.
59          *     @type bool|array $quicktags         Whether to load Quicktags. Can be used to pass settings directly to
60          *                                         Quicktags using an array. Default true.
61          * }
62          * @return array Parsed arguments array.
63          */
64         public static function parse_settings( $editor_id, $settings ) {
65
66                 /**
67                  * Filter the wp_editor() settings.
68                  *
69                  * @since 4.0.0
70                  *
71                  * @see _WP_Editors()::parse_settings()
72                  *
73                  * @param array  $settings  Array of editor arguments.
74                  * @param string $editor_id ID for the current editor instance.
75                  */
76                 $settings = apply_filters( 'wp_editor_settings', $settings, $editor_id );
77
78                 $set = wp_parse_args( $settings, array(
79                         'wpautop'             => true,
80                         'media_buttons'       => true,
81                         'default_editor'      => '',
82                         'drag_drop_upload'    => false,
83                         'textarea_name'       => $editor_id,
84                         'textarea_rows'       => 20,
85                         'tabindex'            => '',
86                         'tabfocus_elements'   => ':prev,:next',
87                         'editor_css'          => '',
88                         'editor_class'        => '',
89                         'teeny'               => false,
90                         'dfw'                 => false,
91                         '_content_editor_dfw' => false,
92                         'tinymce'             => true,
93                         'quicktags'           => true
94                 ) );
95
96                 self::$this_tinymce = ( $set['tinymce'] && user_can_richedit() );
97
98                 if ( self::$this_tinymce ) {
99                         if ( false !== strpos( $editor_id, '[' ) ) {
100                                 self::$this_tinymce = false;
101                                 _deprecated_argument( 'wp_editor()', '3.9', 'TinyMCE editor IDs cannot have brackets.' );
102                         }
103                 }
104
105                 self::$this_quicktags = (bool) $set['quicktags'];
106
107                 if ( self::$this_tinymce )
108                         self::$has_tinymce = true;
109
110                 if ( self::$this_quicktags )
111                         self::$has_quicktags = true;
112
113                 if ( empty( $set['editor_height'] ) )
114                         return $set;
115
116                 if ( 'content' === $editor_id && empty( $set['tinymce']['wp_autoresize_on'] ) ) {
117                         // A cookie (set when a user resizes the editor) overrides the height.
118                         $cookie = (int) get_user_setting( 'ed_size' );
119
120                         // Upgrade an old TinyMCE cookie if it is still around, and the new one isn't.
121                         if ( ! $cookie && isset( $_COOKIE['TinyMCE_content_size'] ) ) {
122                                 parse_str( $_COOKIE['TinyMCE_content_size'], $cookie );
123                                 $cookie = $cookie['ch'];
124                         }
125
126                         if ( $cookie )
127                                 $set['editor_height'] = $cookie;
128                 }
129
130                 if ( $set['editor_height'] < 50 )
131                         $set['editor_height'] = 50;
132                 elseif ( $set['editor_height'] > 5000 )
133                         $set['editor_height'] = 5000;
134
135                 return $set;
136         }
137
138         /**
139          * Outputs the HTML for a single instance of the editor.
140          *
141          * @param string $content The initial content of the editor.
142          * @param string $editor_id ID for the textarea and TinyMCE and Quicktags instances (can contain only ASCII letters and numbers).
143          * @param array $settings See the _parse_settings() method for description.
144          */
145         public static function editor( $content, $editor_id, $settings = array() ) {
146
147                 $set = self::parse_settings( $editor_id, $settings );
148                 $editor_class = ' class="' . trim( $set['editor_class'] . ' wp-editor-area' ) . '"';
149                 $tabindex = $set['tabindex'] ? ' tabindex="' . (int) $set['tabindex'] . '"' : '';
150                 $switch_class = 'html-active';
151                 $toolbar = $buttons = $autocomplete = '';
152
153                 if ( $set['drag_drop_upload'] ) {
154                         self::$drag_drop_upload = true;
155                 }
156
157                 if ( ! empty( $set['editor_height'] ) )
158                         $height = ' style="height: ' . $set['editor_height'] . 'px"';
159                 else
160                         $height = ' rows="' . $set['textarea_rows'] . '"';
161
162                 if ( !current_user_can( 'upload_files' ) )
163                         $set['media_buttons'] = false;
164
165                 if ( ! self::$this_quicktags && self::$this_tinymce ) {
166                         $switch_class = 'tmce-active';
167                         $autocomplete = ' autocomplete="off"';
168                 } elseif ( self::$this_quicktags && self::$this_tinymce ) {
169                         $default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor();
170                         $autocomplete = ' autocomplete="off"';
171
172                         // 'html' is used for the "Text" editor tab.
173                         if ( 'html' === $default_editor ) {
174                                 add_filter('the_editor_content', 'wp_htmledit_pre');
175                                 $switch_class = 'html-active';
176                         } else {
177                                 add_filter('the_editor_content', 'wp_richedit_pre');
178                                 $switch_class = 'tmce-active';
179                         }
180
181                         $buttons .= '<button type="button" id="' . $editor_id . '-tmce" class="wp-switch-editor switch-tmce" onclick="switchEditors.switchto(this);">' . __('Visual') . "</button>\n";
182                         $buttons .= '<button type="button" id="' . $editor_id . '-html" class="wp-switch-editor switch-html" onclick="switchEditors.switchto(this);">' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "</button>\n";
183                 }
184
185                 $wrap_class = 'wp-core-ui wp-editor-wrap ' . $switch_class;
186
187                 if ( $set['_content_editor_dfw'] ) {
188                         $wrap_class .= ' has-dfw';
189                 }
190
191                 echo '<div id="wp-' . $editor_id . '-wrap" class="' . $wrap_class . '">';
192
193                 if ( self::$editor_buttons_css ) {
194                         wp_print_styles('editor-buttons');
195                         self::$editor_buttons_css = false;
196                 }
197
198                 if ( !empty($set['editor_css']) )
199                         echo $set['editor_css'] . "\n";
200
201                 if ( !empty($buttons) || $set['media_buttons'] ) {
202                         echo '<div id="wp-' . $editor_id . '-editor-tools" class="wp-editor-tools hide-if-no-js">';
203
204                         if ( $set['media_buttons'] ) {
205                                 self::$has_medialib = true;
206
207                                 if ( !function_exists('media_buttons') )
208                                         include(ABSPATH . 'wp-admin/includes/media.php');
209
210                                 echo '<div id="wp-' . $editor_id . '-media-buttons" class="wp-media-buttons">';
211
212                                 /**
213                                  * Fires after the default media button(s) are displayed.
214                                  *
215                                  * @since 2.5.0
216                                  *
217                                  * @param string $editor_id Unique editor identifier, e.g. 'content'.
218                                  */
219                                 do_action( 'media_buttons', $editor_id );
220                                 echo "</div>\n";
221                         }
222
223                         echo '<div class="wp-editor-tabs">' . $buttons . "</div>\n";
224                         echo "</div>\n";
225                 }
226
227                 /**
228                  * Filter the HTML markup output that displays the editor.
229                  *
230                  * @since 2.1.0
231                  *
232                  * @param string $output Editor's HTML markup.
233                  */
234                 $the_editor = apply_filters( 'the_editor', '<div id="wp-' . $editor_id . '-editor-container" class="wp-editor-container">' .
235                         '<textarea' . $editor_class . $height . $tabindex . $autocomplete . ' cols="40" name="' . $set['textarea_name'] . '" ' .
236                         'id="' . $editor_id . '">%s</textarea></div>' );
237
238                 /**
239                  * Filter the default editor content.
240                  *
241                  * @since 2.1.0
242                  *
243                  * @param string $content Default editor content.
244                  */
245                 $content = apply_filters( 'the_editor_content', $content );
246
247                 printf( $the_editor, $content );
248                 echo "\n</div>\n\n";
249
250                 self::editor_settings($editor_id, $set);
251         }
252
253         /**
254          * @param string $editor_id
255          * @param array  $set
256          */
257         public static function editor_settings($editor_id, $set) {
258                 $first_run = false;
259
260                 if ( empty(self::$first_init) ) {
261                         if ( is_admin() ) {
262                                 add_action( 'admin_print_footer_scripts', array( __CLASS__, 'editor_js' ), 50 );
263                                 add_action( 'admin_print_footer_scripts', array( __CLASS__, 'enqueue_scripts' ), 1 );
264                         } else {
265                                 add_action( 'wp_print_footer_scripts', array( __CLASS__, 'editor_js' ), 50 );
266                                 add_action( 'wp_print_footer_scripts', array( __CLASS__, 'enqueue_scripts' ), 1 );
267                         }
268                 }
269
270                 if ( self::$this_quicktags ) {
271
272                         $qtInit = array(
273                                 'id' => $editor_id,
274                                 'buttons' => ''
275                         );
276
277                         if ( is_array($set['quicktags']) )
278                                 $qtInit = array_merge($qtInit, $set['quicktags']);
279
280                         if ( empty($qtInit['buttons']) )
281                                 $qtInit['buttons'] = 'strong,em,link,block,del,ins,img,ul,ol,li,code,more,close';
282
283                         if ( $set['dfw'] )
284                                 $qtInit['buttons'] .= ',fullscreen';
285
286                         if ( $set['_content_editor_dfw'] ) {
287                                 $qtInit['buttons'] .= ',dfw';
288                         }
289
290                         /**
291                          * Filter the Quicktags settings.
292                          *
293                          * @since 3.3.0
294                          *
295                          * @param array  $qtInit    Quicktags settings.
296                          * @param string $editor_id The unique editor ID, e.g. 'content'.
297                          */
298                         $qtInit = apply_filters( 'quicktags_settings', $qtInit, $editor_id );
299
300                         self::$qt_settings[$editor_id] = $qtInit;
301
302                         self::$qt_buttons = array_merge( self::$qt_buttons, explode(',', $qtInit['buttons']) );
303                 }
304
305                 if ( self::$this_tinymce ) {
306
307                         if ( empty( self::$first_init ) ) {
308                                 self::$baseurl = includes_url( 'js/tinymce' );
309
310                                 $mce_locale = get_locale();
311                                 self::$mce_locale = $mce_locale = empty( $mce_locale ) ? 'en' : strtolower( substr( $mce_locale, 0, 2 ) ); // ISO 639-1
312
313                                 /** This filter is documented in wp-admin/includes/media.php */
314                                 $no_captions = (bool) apply_filters( 'disable_captions', '' );
315                                 $first_run = true;
316                                 $ext_plugins = '';
317
318                                 if ( $set['teeny'] ) {
319
320                                         /**
321                                          * Filter the list of teenyMCE plugins.
322                                          *
323                                          * @since 2.7.0
324                                          *
325                                          * @param array  $plugins   An array of teenyMCE plugins.
326                                          * @param string $editor_id Unique editor identifier, e.g. 'content'.
327                                          */
328                                         self::$plugins = $plugins = apply_filters( 'teeny_mce_plugins', array( 'colorpicker', 'lists', 'fullscreen', 'image', 'wordpress', 'wpeditimage', 'wplink' ), $editor_id );
329                                 } else {
330
331                                         /**
332                                          * Filter the list of TinyMCE external plugins.
333                                          *
334                                          * The filter takes an associative array of external plugins for
335                                          * TinyMCE in the form 'plugin_name' => 'url'.
336                                          *
337                                          * The url should be absolute, and should include the js filename
338                                          * to be loaded. For example:
339                                          * 'myplugin' => 'http://mysite.com/wp-content/plugins/myfolder/mce_plugin.js'.
340                                          *
341                                          * If the external plugin adds a button, it should be added with
342                                          * one of the 'mce_buttons' filters.
343                                          *
344                                          * @since 2.5.0
345                                          *
346                                          * @param array $external_plugins An array of external TinyMCE plugins.
347                                          */
348                                         $mce_external_plugins = apply_filters( 'mce_external_plugins', array() );
349
350                                         $plugins = array(
351                                                 'charmap',
352                                                 'colorpicker',
353                                                 'hr',
354                                                 'lists',
355                                                 'media',
356                                                 'paste',
357                                                 'tabfocus',
358                                                 'textcolor',
359                                                 'fullscreen',
360                                                 'wordpress',
361                                                 'wpautoresize',
362                                                 'wpeditimage',
363                                                 'wpgallery',
364                                                 'wplink',
365                                                 'wpdialogs',
366                                                 'wpview',
367                                         );
368
369                                         if ( ! self::$has_medialib ) {
370                                                 $plugins[] = 'image';
371                                         }
372
373                                         /**
374                                          * Filter the list of default TinyMCE plugins.
375                                          *
376                                          * The filter specifies which of the default plugins included
377                                          * in WordPress should be added to the TinyMCE instance.
378                                          *
379                                          * @since 3.3.0
380                                          *
381                                          * @param array $plugins An array of default TinyMCE plugins.
382                                          */
383                                         $plugins = array_unique( apply_filters( 'tiny_mce_plugins', $plugins ) );
384
385                                         if ( ( $key = array_search( 'spellchecker', $plugins ) ) !== false ) {
386                                                 // Remove 'spellchecker' from the internal plugins if added with 'tiny_mce_plugins' filter to prevent errors.
387                                                 // It can be added with 'mce_external_plugins'.
388                                                 unset( $plugins[$key] );
389                                         }
390
391                                         if ( ! empty( $mce_external_plugins ) ) {
392
393                                                 /**
394                                                  * Filter the translations loaded for external TinyMCE 3.x plugins.
395                                                  *
396                                                  * The filter takes an associative array ('plugin_name' => 'path')
397                                                  * where 'path' is the include path to the file.
398                                                  *
399                                                  * The language file should follow the same format as wp_mce_translation(),
400                                                  * and should define a variable ($strings) that holds all translated strings.
401                                                  *
402                                                  * @since 2.5.0
403                                                  *
404                                                  * @param array $translations Translations for external TinyMCE plugins.
405                                                  */
406                                                 $mce_external_languages = apply_filters( 'mce_external_languages', array() );
407
408                                                 $loaded_langs = array();
409                                                 $strings = '';
410
411                                                 if ( ! empty( $mce_external_languages ) ) {
412                                                         foreach ( $mce_external_languages as $name => $path ) {
413                                                                 if ( @is_file( $path ) && @is_readable( $path ) ) {
414                                                                         include_once( $path );
415                                                                         $ext_plugins .= $strings . "\n";
416                                                                         $loaded_langs[] = $name;
417                                                                 }
418                                                         }
419                                                 }
420
421                                                 foreach ( $mce_external_plugins as $name => $url ) {
422                                                         if ( in_array( $name, $plugins, true ) ) {
423                                                                 unset( $mce_external_plugins[ $name ] );
424                                                                 continue;
425                                                         }
426
427                                                         $url = set_url_scheme( $url );
428                                                         $mce_external_plugins[ $name ] = $url;
429                                                         $plugurl = dirname( $url );
430                                                         $strings = '';
431
432                                                         // Try to load langs/[locale].js and langs/[locale]_dlg.js
433                                                         if ( ! in_array( $name, $loaded_langs, true ) ) {
434                                                                 $path = str_replace( content_url(), '', $plugurl );
435                                                                 $path = WP_CONTENT_DIR . $path . '/langs/';
436
437                                                                 if ( function_exists('realpath') )
438                                                                         $path = trailingslashit( realpath($path) );
439
440                                                                 if ( @is_file( $path . $mce_locale . '.js' ) )
441                                                                         $strings .= @file_get_contents( $path . $mce_locale . '.js' ) . "\n";
442
443                                                                 if ( @is_file( $path . $mce_locale . '_dlg.js' ) )
444                                                                         $strings .= @file_get_contents( $path . $mce_locale . '_dlg.js' ) . "\n";
445
446                                                                 if ( 'en' != $mce_locale && empty( $strings ) ) {
447                                                                         if ( @is_file( $path . 'en.js' ) ) {
448                                                                                 $str1 = @file_get_contents( $path . 'en.js' );
449                                                                                 $strings .= preg_replace( '/([\'"])en\./', '$1' . $mce_locale . '.', $str1, 1 ) . "\n";
450                                                                         }
451
452                                                                         if ( @is_file( $path . 'en_dlg.js' ) ) {
453                                                                                 $str2 = @file_get_contents( $path . 'en_dlg.js' );
454                                                                                 $strings .= preg_replace( '/([\'"])en\./', '$1' . $mce_locale . '.', $str2, 1 ) . "\n";
455                                                                         }
456                                                                 }
457
458                                                                 if ( ! empty( $strings ) )
459                                                                         $ext_plugins .= "\n" . $strings . "\n";
460                                                         }
461
462                                                         $ext_plugins .= 'tinyMCEPreInit.load_ext("' . $plugurl . '", "' . $mce_locale . '");' . "\n";
463                                                         $ext_plugins .= 'tinymce.PluginManager.load("' . $name . '", "' . $url . '");' . "\n";
464                                                 }
465                                         }
466                                 }
467
468                                 if ( $set['dfw'] )
469                                         $plugins[] = 'wpfullscreen';
470
471                                 self::$plugins = $plugins;
472                                 self::$ext_plugins = $ext_plugins;
473
474                                 self::$first_init = array(
475                                         'theme' => 'modern',
476                                         'skin' => 'lightgray',
477                                         'language' => self::$mce_locale,
478                                         'formats' => "{
479                                                 alignleft: [
480                                                         {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'left'}},
481                                                         {selector: 'img,table,dl.wp-caption', classes: 'alignleft'}
482                                                 ],
483                                                 aligncenter: [
484                                                         {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'center'}},
485                                                         {selector: 'img,table,dl.wp-caption', classes: 'aligncenter'}
486                                                 ],
487                                                 alignright: [
488                                                         {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'right'}},
489                                                         {selector: 'img,table,dl.wp-caption', classes: 'alignright'}
490                                                 ],
491                                                 strikethrough: {inline: 'del'}
492                                         }",
493                                         'block_formats' =>
494                                                 'Paragraph=p;' .
495                                                 'Pre=pre;' .
496                                                 'Heading 1=h1;' .
497                                                 'Heading 2=h2;' .
498                                                 'Heading 3=h3;' .
499                                                 'Heading 4=h4;' .
500                                                 'Heading 5=h5;' .
501                                                 'Heading 6=h6',
502                                         'relative_urls' => false,
503                                         'remove_script_host' => false,
504                                         'convert_urls' => false,
505                                         'browser_spellcheck' => true,
506                                         'fix_list_elements' => true,
507                                         'entities' => '38,amp,60,lt,62,gt',
508                                         'entity_encoding' => 'raw',
509                                         'keep_styles' => false,
510                                         'cache_suffix' => 'wp-mce-' . $GLOBALS['tinymce_version'],
511
512                                         // Limit the preview styles in the menu/toolbar
513                                         'preview_styles' => 'font-family font-size font-weight font-style text-decoration text-transform',
514
515                                         'wpeditimage_disable_captions' => $no_captions,
516                                         'wpeditimage_html5_captions' => current_theme_supports( 'html5', 'caption' ),
517                                         'plugins' => implode( ',', $plugins ),
518                                 );
519
520                                 if ( ! empty( $mce_external_plugins ) ) {
521                                         self::$first_init['external_plugins'] = wp_json_encode( $mce_external_plugins );
522                                 }
523
524                                 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
525                                 $version = 'ver=' . $GLOBALS['wp_version'];
526                                 $dashicons = includes_url( "css/dashicons$suffix.css?$version" );
527
528                                 // WordPress default stylesheet and dashicons
529                                 $mce_css = array(
530                                         $dashicons,
531                                         self::$baseurl . '/skins/wordpress/wp-content.css?' . $version
532                                 );
533
534                                 $editor_styles = get_editor_stylesheets();
535                                 if ( ! empty( $editor_styles ) ) {
536                                         foreach ( $editor_styles as $style ) {
537                                                 $mce_css[] = $style;
538                                         }
539                                 }
540
541                                 /**
542                                  * Filter the comma-delimited list of stylesheets to load in TinyMCE.
543                                  *
544                                  * @since 2.1.0
545                                  *
546                                  * @param array $stylesheets Comma-delimited list of stylesheets.
547                                  */
548                                 $mce_css = trim( apply_filters( 'mce_css', implode( ',', $mce_css ) ), ' ,' );
549
550                                 if ( ! empty($mce_css) )
551                                         self::$first_init['content_css'] = $mce_css;
552                         }
553
554                         if ( $set['teeny'] ) {
555
556                                 /**
557                                  * Filter the list of teenyMCE buttons (Text tab).
558                                  *
559                                  * @since 2.7.0
560                                  *
561                                  * @param array  $buttons   An array of teenyMCE buttons.
562                                  * @param string $editor_id Unique editor identifier, e.g. 'content'.
563                                  */
564                                 $mce_buttons = apply_filters( 'teeny_mce_buttons', array('bold', 'italic', 'underline', 'blockquote', 'strikethrough', 'bullist', 'numlist', 'alignleft', 'aligncenter', 'alignright', 'undo', 'redo', 'link', 'unlink', 'fullscreen'), $editor_id );
565                                 $mce_buttons_2 = $mce_buttons_3 = $mce_buttons_4 = array();
566                         } else {
567                                 $mce_buttons = array( 'bold', 'italic', 'strikethrough', 'bullist', 'numlist', 'blockquote', 'hr', 'alignleft', 'aligncenter', 'alignright', 'link', 'unlink', 'wp_more', 'spellchecker' );
568
569                                 if ( $set['_content_editor_dfw'] ) {
570                                         $mce_buttons[] = 'dfw';
571                                 } else {
572                                         $mce_buttons[] = 'fullscreen';
573                                 }
574
575                                 $mce_buttons[] = 'wp_adv';
576
577                                 /**
578                                  * Filter the first-row list of TinyMCE buttons (Visual tab).
579                                  *
580                                  * @since 2.0.0
581                                  *
582                                  * @param array  $buttons   First-row list of buttons.
583                                  * @param string $editor_id Unique editor identifier, e.g. 'content'.
584                                  */
585                                 $mce_buttons = apply_filters( 'mce_buttons', $mce_buttons, $editor_id );
586
587                                 /**
588                                  * Filter the second-row list of TinyMCE buttons (Visual tab).
589                                  *
590                                  * @since 2.0.0
591                                  *
592                                  * @param array  $buttons   Second-row list of buttons.
593                                  * @param string $editor_id Unique editor identifier, e.g. 'content'.
594                                  */
595                                 $mce_buttons_2 = apply_filters( 'mce_buttons_2', array( 'formatselect', 'underline', 'alignjustify', 'forecolor', 'pastetext', 'removeformat', 'charmap', 'outdent', 'indent', 'undo', 'redo', 'wp_help' ), $editor_id );
596
597                                 /**
598                                  * Filter the third-row list of TinyMCE buttons (Visual tab).
599                                  *
600                                  * @since 2.0.0
601                                  *
602                                  * @param array  $buttons   Third-row list of buttons.
603                                  * @param string $editor_id Unique editor identifier, e.g. 'content'.
604                                  */
605                                 $mce_buttons_3 = apply_filters( 'mce_buttons_3', array(), $editor_id );
606
607                                 /**
608                                  * Filter the fourth-row list of TinyMCE buttons (Visual tab).
609                                  *
610                                  * @since 2.5.0
611                                  *
612                                  * @param array  $buttons   Fourth-row list of buttons.
613                                  * @param string $editor_id Unique editor identifier, e.g. 'content'.
614                                  */
615                                 $mce_buttons_4 = apply_filters( 'mce_buttons_4', array(), $editor_id );
616                         }
617
618                         $body_class = $editor_id;
619
620                         if ( $post = get_post() ) {
621                                 $body_class .= ' post-type-' . sanitize_html_class( $post->post_type ) . ' post-status-' . sanitize_html_class( $post->post_status );
622                                 if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
623                                         $post_format = get_post_format( $post );
624                                         if ( $post_format && ! is_wp_error( $post_format ) )
625                                                 $body_class .= ' post-format-' . sanitize_html_class( $post_format );
626                                         else
627                                                 $body_class .= ' post-format-standard';
628                                 }
629                         }
630
631                         $body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
632
633                         if ( !empty($set['tinymce']['body_class']) ) {
634                                 $body_class .= ' ' . $set['tinymce']['body_class'];
635                                 unset($set['tinymce']['body_class']);
636                         }
637
638                         if ( $set['dfw'] ) {
639                                 // replace the first 'fullscreen' with 'wp_fullscreen'
640                                 if ( ($key = array_search('fullscreen', $mce_buttons)) !== false )
641                                         $mce_buttons[$key] = 'wp_fullscreen';
642                                 elseif ( ($key = array_search('fullscreen', $mce_buttons_2)) !== false )
643                                         $mce_buttons_2[$key] = 'wp_fullscreen';
644                                 elseif ( ($key = array_search('fullscreen', $mce_buttons_3)) !== false )
645                                         $mce_buttons_3[$key] = 'wp_fullscreen';
646                                 elseif ( ($key = array_search('fullscreen', $mce_buttons_4)) !== false )
647                                         $mce_buttons_4[$key] = 'wp_fullscreen';
648                         }
649
650                         $mceInit = array (
651                                 'selector' => "#$editor_id",
652                                 'resize' => 'vertical',
653                                 'menubar' => false,
654                                 'wpautop' => (bool) $set['wpautop'],
655                                 'indent' => ! $set['wpautop'],
656                                 'toolbar1' => implode($mce_buttons, ','),
657                                 'toolbar2' => implode($mce_buttons_2, ','),
658                                 'toolbar3' => implode($mce_buttons_3, ','),
659                                 'toolbar4' => implode($mce_buttons_4, ','),
660                                 'tabfocus_elements' => $set['tabfocus_elements'],
661                                 'body_class' => $body_class
662                         );
663
664                         if ( $first_run )
665                                 $mceInit = array_merge( self::$first_init, $mceInit );
666
667                         if ( is_array( $set['tinymce'] ) )
668                                 $mceInit = array_merge( $mceInit, $set['tinymce'] );
669
670                         /*
671                          * For people who really REALLY know what they're doing with TinyMCE
672                          * You can modify $mceInit to add, remove, change elements of the config
673                          * before tinyMCE.init. Setting "valid_elements", "invalid_elements"
674                          * and "extended_valid_elements" can be done through this filter. Best
675                          * is to use the default cleanup by not specifying valid_elements,
676                          * as TinyMCE contains full set of XHTML 1.0.
677                          */
678                         if ( $set['teeny'] ) {
679
680                                 /**
681                                  * Filter the teenyMCE config before init.
682                                  *
683                                  * @since 2.7.0
684                                  *
685                                  * @param array  $mceInit   An array with teenyMCE config.
686                                  * @param string $editor_id Unique editor identifier, e.g. 'content'.
687                                  */
688                                 $mceInit = apply_filters( 'teeny_mce_before_init', $mceInit, $editor_id );
689                         } else {
690
691                                 /**
692                                  * Filter the TinyMCE config before init.
693                                  *
694                                  * @since 2.5.0
695                                  *
696                                  * @param array  $mceInit   An array with TinyMCE config.
697                                  * @param string $editor_id Unique editor identifier, e.g. 'content'.
698                                  */
699                                 $mceInit = apply_filters( 'tiny_mce_before_init', $mceInit, $editor_id );
700                         }
701
702                         if ( empty( $mceInit['toolbar3'] ) && ! empty( $mceInit['toolbar4'] ) ) {
703                                 $mceInit['toolbar3'] = $mceInit['toolbar4'];
704                                 $mceInit['toolbar4'] = '';
705                         }
706
707                         self::$mce_settings[$editor_id] = $mceInit;
708                 } // end if self::$this_tinymce
709         }
710
711         private static function _parse_init($init) {
712                 $options = '';
713
714                 foreach ( $init as $k => $v ) {
715                         if ( is_bool($v) ) {
716                                 $val = $v ? 'true' : 'false';
717                                 $options .= $k . ':' . $val . ',';
718                                 continue;
719                         } elseif ( !empty($v) && is_string($v) && ( ('{' == $v{0} && '}' == $v{strlen($v) - 1}) || ('[' == $v{0} && ']' == $v{strlen($v) - 1}) || preg_match('/^\(?function ?\(/', $v) ) ) {
720                                 $options .= $k . ':' . $v . ',';
721                                 continue;
722                         }
723                         $options .= $k . ':"' . $v . '",';
724                 }
725
726                 return '{' . trim( $options, ' ,' ) . '}';
727         }
728
729         public static function enqueue_scripts() {
730                 wp_enqueue_script('word-count');
731
732                 if ( self::$has_tinymce )
733                         wp_enqueue_script('editor');
734
735                 if ( self::$has_quicktags ) {
736                         wp_enqueue_script( 'quicktags' );
737                         wp_enqueue_style( 'buttons' );
738                 }
739
740                 if ( in_array('wplink', self::$plugins, true) || in_array('link', self::$qt_buttons, true) ) {
741                         wp_enqueue_script('wplink');
742                 }
743
744                 if ( in_array('wpfullscreen', self::$plugins, true) || in_array('fullscreen', self::$qt_buttons, true) )
745                         wp_enqueue_script('wp-fullscreen');
746
747                 if ( self::$has_medialib ) {
748                         add_thickbox();
749                         wp_enqueue_script('media-upload');
750                 }
751
752                 /**
753                  * Fires when scripts and styles are enqueued for the editor.
754                  *
755                  * @since 3.9.0
756                  *
757                  * @param array $to_load An array containing boolean values whether TinyMCE
758                  *                       and Quicktags are being loaded.
759                  */
760                 do_action( 'wp_enqueue_editor', array(
761                         'tinymce'   => self::$has_tinymce,
762                         'quicktags' => self::$has_quicktags,
763                 ) );
764         }
765
766         /**
767          * Translates the default TinyMCE strings and returns them as JSON encoded object ready to be loaded with tinymce.addI18n().
768          * Can be used directly (_WP_Editors::wp_mce_translation()) by passing the same locale as set in the TinyMCE init object.
769          *
770          * @param string $mce_locale The locale used for the editor.
771          * @param bool $json_only optional Whether to include the JavaScript calls to tinymce.addI18n() and tinymce.ScriptLoader.markDone().
772          * @return string Translation object, JSON encoded.
773          */
774         public static function wp_mce_translation( $mce_locale = '', $json_only = false ) {
775
776                 $mce_translation = array(
777                         // Default TinyMCE strings
778                         'New document' => __( 'New document' ),
779                         'Formats' => _x( 'Formats', 'TinyMCE' ),
780
781                         'Headings' => _x( 'Headings', 'TinyMCE' ),
782                         'Heading 1' => __( 'Heading 1' ),
783                         'Heading 2' => __( 'Heading 2' ),
784                         'Heading 3' => __( 'Heading 3' ),
785                         'Heading 4' => __( 'Heading 4' ),
786                         'Heading 5' => __( 'Heading 5' ),
787                         'Heading 6' => __( 'Heading 6' ),
788
789                         /* translators: block tags */
790                         'Blocks' => _x( 'Blocks', 'TinyMCE' ),
791                         'Paragraph' => __( 'Paragraph' ),
792                         'Blockquote' => __( 'Blockquote' ),
793                         'Div' => _x( 'Div', 'HTML tag' ),
794                         'Pre' => _x( 'Pre', 'HTML tag' ),
795                         'Address' => _x( 'Address', 'HTML tag' ),
796
797                         'Inline' => _x( 'Inline', 'HTML elements' ),
798                         'Underline' => __( 'Underline' ),
799                         'Strikethrough' => __( 'Strikethrough' ),
800                         'Subscript' => __( 'Subscript' ),
801                         'Superscript' => __( 'Superscript' ),
802                         'Clear formatting' => __( 'Clear formatting' ),
803                         'Bold' => __( 'Bold' ),
804                         'Italic' => __( 'Italic' ),
805                         'Code' => _x( 'Code', 'editor button' ),
806                         'Source code' => __( 'Source code' ),
807                         'Font Family' => __( 'Font Family' ),
808                         'Font Sizes' => __( 'Font Sizes' ),
809
810                         'Align center' => __( 'Align center' ),
811                         'Align right' => __( 'Align right' ),
812                         'Align left' => __( 'Align left' ),
813                         'Justify' => __( 'Justify' ),
814                         'Increase indent' => __( 'Increase indent' ),
815                         'Decrease indent' => __( 'Decrease indent' ),
816
817                         'Cut' => __( 'Cut' ),
818                         'Copy' => __( 'Copy' ),
819                         'Paste' => __( 'Paste' ),
820                         'Select all' => __( 'Select all' ),
821                         'Undo' => __( 'Undo' ),
822                         'Redo' => __( 'Redo' ),
823
824                         'Ok' => __( 'OK' ),
825                         'Cancel' => __( 'Cancel' ),
826                         'Close' => __( 'Close' ),
827                         'Visual aids' => __( 'Visual aids' ),
828
829                         'Bullet list' => __( 'Bulleted list' ),
830                         'Numbered list' => __( 'Numbered list' ),
831                         'Square' => _x( 'Square', 'list style' ),
832                         'Default' => _x( 'Default', 'list style' ),
833                         'Circle' => _x( 'Circle', 'list style' ),
834                         'Disc' => _x('Disc', 'list style' ),
835                         'Lower Greek' => _x( 'Lower Greek', 'list style' ),
836                         'Lower Alpha' => _x( 'Lower Alpha', 'list style' ),
837                         'Upper Alpha' => _x( 'Upper Alpha', 'list style' ),
838                         'Upper Roman' => _x( 'Upper Roman', 'list style' ),
839                         'Lower Roman' => _x( 'Lower Roman', 'list style' ),
840
841                         // Anchor plugin
842                         'Name' => _x( 'Name', 'Name of link anchor (TinyMCE)' ),
843                         'Anchor' => _x( 'Anchor', 'Link anchor (TinyMCE)' ),
844                         'Anchors' => _x( 'Anchors', 'Link anchors (TinyMCE)' ),
845
846                         // Fullpage plugin
847                         'Document properties' => __( 'Document properties' ),
848                         'Robots' => __( 'Robots' ),
849                         'Title' => __( 'Title' ),
850                         'Keywords' => __( 'Keywords' ),
851                         'Encoding' => __( 'Encoding' ),
852                         'Description' => __( 'Description' ),
853                         'Author' => __( 'Author' ),
854
855                         // Media, image plugins
856                         'Insert/edit image' => __( 'Insert/edit image' ),
857                         'General' => __( 'General' ),
858                         'Advanced' => __( 'Advanced' ),
859                         'Source' => __( 'Source' ),
860                         'Border' => __( 'Border' ),
861                         'Constrain proportions' => __( 'Constrain proportions' ),
862                         'Vertical space' => __( 'Vertical space' ),
863                         'Image description' => __( 'Image description' ),
864                         'Style' => __( 'Style' ),
865                         'Dimensions' => __( 'Dimensions' ),
866                         'Insert image' => __( 'Insert image' ),
867                         'Insert date/time' => __( 'Insert date/time' ),
868                         'Insert/edit video' => __( 'Insert/edit video' ),
869                         'Poster' => __( 'Poster' ),
870                         'Alternative source' => __( 'Alternative source' ),
871                         'Paste your embed code below:' => __( 'Paste your embed code below:' ),
872                         'Insert video' => __( 'Insert video' ),
873                         'Embed' => __( 'Embed' ),
874
875                         // Each of these have a corresponding plugin
876                         'Special character' => __( 'Special character' ),
877                         'Right to left' => _x( 'Right to left', 'editor button' ),
878                         'Left to right' => _x( 'Left to right', 'editor button' ),
879                         'Emoticons' => __( 'Emoticons' ),
880                         'Nonbreaking space' => __( 'Nonbreaking space' ),
881                         'Page break' => __( 'Page break' ),
882                         'Paste as text' => __( 'Paste as text' ),
883                         'Preview' => __( 'Preview' ),
884                         'Print' => __( 'Print' ),
885                         'Save' => __( 'Save' ),
886                         'Fullscreen' => __( 'Fullscreen' ),
887                         'Horizontal line' => __( 'Horizontal line' ),
888                         'Horizontal space' => __( 'Horizontal space' ),
889                         'Restore last draft' => __( 'Restore last draft' ),
890                         'Insert/edit link' => __( 'Insert/edit link' ),
891                         'Remove link' => __( 'Remove link' ),
892
893                         'Color' => __( 'Color' ),
894                         'Custom color' => __( 'Custom color' ),
895                         'Custom...' => _x( 'Custom...', 'label for custom color' ),
896                         'No color' => __( 'No color' ),
897
898                         // Spelling, search/replace plugins
899                         'Could not find the specified string.' => __( 'Could not find the specified string.' ),
900                         'Replace' => _x( 'Replace', 'find/replace' ),
901                         'Next' => _x( 'Next', 'find/replace' ),
902                         /* translators: previous */
903                         'Prev' => _x( 'Prev', 'find/replace' ),
904                         'Whole words' => _x( 'Whole words', 'find/replace' ),
905                         'Find and replace' => __( 'Find and replace' ),
906                         'Replace with' => _x('Replace with', 'find/replace' ),
907                         'Find' => _x( 'Find', 'find/replace' ),
908                         'Replace all' => _x( 'Replace all', 'find/replace' ),
909                         'Match case' => __( 'Match case' ),
910                         'Spellcheck' => __( 'Check Spelling' ),
911                         'Finish' => _x( 'Finish', 'spellcheck' ),
912                         'Ignore all' => _x( 'Ignore all', 'spellcheck' ),
913                         'Ignore' => _x( 'Ignore', 'spellcheck' ),
914                         'Add to Dictionary' => __( 'Add to Dictionary' ),
915
916                         // TinyMCE tables
917                         'Insert table' => __( 'Insert table' ),
918                         'Delete table' => __( 'Delete table' ),
919                         'Table properties' => __( 'Table properties' ),
920                         'Row properties' => __( 'Table row properties' ),
921                         'Cell properties' => __( 'Table cell properties' ),
922                         'Border color' => __( 'Border color' ),
923
924                         'Row' => __( 'Row' ),
925                         'Rows' => __( 'Rows' ),
926                         'Column' => _x( 'Column', 'table column' ),
927                         'Cols' => _x( 'Cols', 'table columns' ),
928                         'Cell' => _x( 'Cell', 'table cell' ),
929                         'Header cell' => __( 'Header cell' ),
930                         'Header' => _x( 'Header', 'table header' ),
931                         'Body' => _x( 'Body', 'table body' ),
932                         'Footer' => _x( 'Footer', 'table footer' ),
933
934                         'Insert row before' => __( 'Insert row before' ),
935                         'Insert row after' => __( 'Insert row after' ),
936                         'Insert column before' => __( 'Insert column before' ),
937                         'Insert column after' => __( 'Insert column after' ),
938                         'Paste row before' => __( 'Paste table row before' ),
939                         'Paste row after' => __( 'Paste table row after' ),
940                         'Delete row' => __( 'Delete row' ),
941                         'Delete column' => __( 'Delete column' ),
942                         'Cut row' => __( 'Cut table row' ),
943                         'Copy row' => __( 'Copy table row' ),
944                         'Merge cells' => __( 'Merge table cells' ),
945                         'Split cell' => __( 'Split table cell' ),
946
947                         'Height' => __( 'Height' ),
948                         'Width' => __( 'Width' ),
949                         'Caption' => __( 'Caption' ),
950                         'Alignment' => __( 'Alignment' ),
951                         'H Align' => _x( 'H Align', 'horizontal table cell alignment' ),
952                         'Left' => __( 'Left' ),
953                         'Center' => __( 'Center' ),
954                         'Right' => __( 'Right' ),
955                         'None' => _x( 'None', 'table cell alignment attribute' ),
956                         'V Align' => _x( 'V Align', 'vertical table cell alignment' ),
957                         'Top' => __( 'Top' ),
958                         'Middle' => __( 'Middle' ),
959                         'Bottom' => __( 'Bottom' ),
960
961                         'Row group' => __( 'Row group' ),
962                         'Column group' => __( 'Column group' ),
963                         'Row type' => __( 'Row type' ),
964                         'Cell type' => __( 'Cell type' ),
965                         'Cell padding' => __( 'Cell padding' ),
966                         'Cell spacing' => __( 'Cell spacing' ),
967                         'Scope' => _x( 'Scope', 'table cell scope attribute' ),
968
969                         'Insert template' => _x( 'Insert template', 'TinyMCE' ),
970                         'Templates' => _x( 'Templates', 'TinyMCE' ),
971
972                         'Background color' => __( 'Background color' ),
973                         'Text color' => __( 'Text color' ),
974                         'Show blocks' => _x( 'Show blocks', 'editor button' ),
975                         'Show invisible characters' => __( 'Show invisible characters' ),
976
977                         /* translators: word count */
978                         'Words: {0}' => sprintf( __( 'Words: %s' ), '{0}' ),
979                         'Paste is now in plain text mode. Contents will now be pasted as plain text until you toggle this option off.' => __( 'Paste is now in plain text mode. Contents will now be pasted as plain text until you toggle this option off.' ) . "\n\n" . __( 'If you&#8217;re looking to paste rich content from Microsoft Word, try turning this option off. The editor will clean up text pasted from Word automatically.' ),
980                         'Rich Text Area. Press ALT-F9 for menu. Press ALT-F10 for toolbar. Press ALT-0 for help' => __( 'Rich Text Area. Press Alt-Shift-H for help' ),
981                         'You have unsaved changes are you sure you want to navigate away?' => __( 'The changes you made will be lost if you navigate away from this page.' ),
982                         'Your browser doesn\'t support direct access to the clipboard. Please use the Ctrl+X/C/V keyboard shortcuts instead.' => __( 'Your browser does not support direct access to the clipboard. Please use keyboard shortcuts or your browser&#8217;s edit menu instead.' ),
983
984                         // TinyMCE menus
985                         'Insert' => _x( 'Insert', 'TinyMCE menu' ),
986                         'File' => _x( 'File', 'TinyMCE menu' ),
987                         'Edit' => _x( 'Edit', 'TinyMCE menu' ),
988                         'Tools' => _x( 'Tools', 'TinyMCE menu' ),
989                         'View' => _x( 'View', 'TinyMCE menu' ),
990                         'Table' => _x( 'Table', 'TinyMCE menu' ),
991                         'Format' => _x( 'Format', 'TinyMCE menu' ),
992
993                         // WordPress strings
994                         'Keyboard Shortcuts' => __( 'Keyboard Shortcuts' ),
995                         'Toolbar Toggle' => __( 'Toolbar Toggle' ),
996                         'Insert Read More tag' => __( 'Insert Read More tag' ),
997                         'Read more...' => __( 'Read more...' ), // Title on the placeholder inside the editor
998                         'Distraction-free writing mode' => __( 'Distraction-free writing mode' ),
999                         'No alignment' => __( 'No alignment' ), // Tooltip for the 'alignnone' button in the image toolbar
1000                         'Remove' => __( 'Remove' ), // Tooltip for the 'remove' button in the image toolbar
1001                         'Edit ' => __( 'Edit' ), // Tooltip for the 'edit' button in the image toolbar
1002                 );
1003
1004                 /**
1005                  * Link plugin (not included):
1006                  *      Insert link
1007                  *      Target
1008                  *      New window
1009                  *      Text to display
1010                  *      The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?
1011                  *      The URL you entered seems to be an external link. Do you want to add the required http:\/\/ prefix?
1012                  *      Url
1013                  */
1014
1015                 if ( ! $mce_locale ) {
1016                         $mce_locale = self::$mce_locale;
1017                 }
1018
1019                 /**
1020                  * Filter translated strings prepared for TinyMCE.
1021                  *
1022                  * @since 3.9.0
1023                  *
1024                  * @param array  $mce_translation Key/value pairs of strings.
1025                  * @param string $mce_locale      Locale.
1026                  */
1027                 $mce_translation = apply_filters( 'wp_mce_translation', $mce_translation, $mce_locale );
1028
1029                 foreach ( $mce_translation as $key => $value ) {
1030                         // Remove strings that are not translated.
1031                         if ( $key === $value ) {
1032                                 unset( $mce_translation[$key] );
1033                                 continue;
1034                         }
1035
1036                         if ( false !== strpos( $value, '&' ) ) {
1037                                 $mce_translation[$key] = html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
1038                         }
1039                 }
1040
1041                 // Set direction
1042                 if ( is_rtl() ) {
1043                         $mce_translation['_dir'] = 'rtl';
1044                 }
1045
1046                 if ( $json_only ) {
1047                         return wp_json_encode( $mce_translation );
1048                 }
1049
1050                 $baseurl = self::$baseurl ? self::$baseurl : includes_url( 'js/tinymce' );
1051
1052                 return "tinymce.addI18n( '$mce_locale', " . wp_json_encode( $mce_translation ) . ");\n" .
1053                         "tinymce.ScriptLoader.markDone( '$baseurl/langs/$mce_locale.js' );\n";
1054         }
1055
1056         public static function editor_js() {
1057                 global $tinymce_version, $concatenate_scripts, $compress_scripts;
1058
1059                 /**
1060                  * Filter "tiny_mce_version" is deprecated
1061                  *
1062                  * The tiny_mce_version filter is not needed since external plugins are loaded directly by TinyMCE.
1063                  * These plugins can be refreshed by appending query string to the URL passed to "mce_external_plugins" filter.
1064                  * If the plugin has a popup dialog, a query string can be added to the button action that opens it (in the plugin's code).
1065                  */
1066                 $version = 'ver=' . $tinymce_version;
1067                 $tmce_on = !empty(self::$mce_settings);
1068
1069                 if ( ! isset($concatenate_scripts) )
1070                         script_concat_settings();
1071
1072                 $compressed = $compress_scripts && $concatenate_scripts && isset($_SERVER['HTTP_ACCEPT_ENCODING'])
1073                         && false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
1074
1075                 $mceInit = $qtInit = '';
1076                 if ( $tmce_on ) {
1077                         foreach ( self::$mce_settings as $editor_id => $init ) {
1078                                 $options = self::_parse_init( $init );
1079                                 $mceInit .= "'$editor_id':{$options},";
1080                         }
1081                         $mceInit = '{' . trim($mceInit, ',') . '}';
1082                 } else {
1083                         $mceInit = '{}';
1084                 }
1085
1086                 if ( !empty(self::$qt_settings) ) {
1087                         foreach ( self::$qt_settings as $editor_id => $init ) {
1088                                 $options = self::_parse_init( $init );
1089                                 $qtInit .= "'$editor_id':{$options},";
1090                         }
1091                         $qtInit = '{' . trim($qtInit, ',') . '}';
1092                 } else {
1093                         $qtInit = '{}';
1094                 }
1095
1096                 $ref = array(
1097                         'plugins' => implode( ',', self::$plugins ),
1098                         'theme' => 'modern',
1099                         'language' => self::$mce_locale
1100                 );
1101
1102                 $suffix = ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ) ? '' : '.min';
1103
1104                 /**
1105                  * Fires immediately before the TinyMCE settings are printed.
1106                  *
1107                  * @since 3.2.0
1108                  *
1109                  * @param array $mce_settings TinyMCE settings array.
1110                  */
1111                 do_action( 'before_wp_tiny_mce', self::$mce_settings );
1112                 ?>
1113
1114                 <script type="text/javascript">
1115                 tinyMCEPreInit = {
1116                         baseURL: "<?php echo self::$baseurl; ?>",
1117                         suffix: "<?php echo $suffix; ?>",
1118                         <?php
1119
1120                         if ( self::$drag_drop_upload ) {
1121                                 echo 'dragDropUpload: true,';
1122                         }
1123
1124                         ?>
1125                         mceInit: <?php echo $mceInit; ?>,
1126                         qtInit: <?php echo $qtInit; ?>,
1127                         ref: <?php echo self::_parse_init( $ref ); ?>,
1128                         load_ext: function(url,lang){var sl=tinymce.ScriptLoader;sl.markDone(url+'/langs/'+lang+'.js');sl.markDone(url+'/langs/'+lang+'_dlg.js');}
1129                 };
1130                 </script>
1131                 <?php
1132
1133                 $baseurl = self::$baseurl;
1134                 // Load tinymce.js when running from /src, else load wp-tinymce.js.gz (production) or tinymce.min.js (SCRIPT_DEBUG)
1135                 $mce_suffix = false !== strpos( $GLOBALS['wp_version'], '-src' ) ? '' : '.min';
1136
1137                 if ( $tmce_on ) {
1138                         if ( $compressed ) {
1139                                 echo "<script type='text/javascript' src='{$baseurl}/wp-tinymce.php?c=1&amp;$version'></script>\n";
1140                         } else {
1141                                 echo "<script type='text/javascript' src='{$baseurl}/tinymce{$mce_suffix}.js?$version'></script>\n";
1142                                 echo "<script type='text/javascript' src='{$baseurl}/plugins/compat3x/plugin{$suffix}.js?$version'></script>\n";
1143                         }
1144
1145                         echo "<script type='text/javascript'>\n" . self::wp_mce_translation() . "</script>\n";
1146
1147                         if ( self::$ext_plugins ) {
1148                                 // Load the old-format English strings to prevent unsightly labels in old style popups
1149                                 echo "<script type='text/javascript' src='{$baseurl}/langs/wp-langs-en.js?$version'></script>\n";
1150                         }
1151                 }
1152
1153                 /**
1154                  * Fires after tinymce.js is loaded, but before any TinyMCE editor
1155                  * instances are created.
1156                  *
1157                  * @since 3.9.0
1158                  *
1159                  * @param array $mce_settings TinyMCE settings array.
1160                  */
1161                 do_action( 'wp_tiny_mce_init', self::$mce_settings );
1162
1163                 ?>
1164                 <script type="text/javascript">
1165                 <?php
1166
1167                 if ( self::$ext_plugins )
1168                         echo self::$ext_plugins . "\n";
1169
1170                 if ( ! is_admin() )
1171                         echo 'var ajaxurl = "' . admin_url( 'admin-ajax.php', 'relative' ) . '";';
1172
1173                 ?>
1174
1175                 ( function() {
1176                         var init, edId, qtId, firstInit, wrapper;
1177
1178                         if ( typeof tinymce !== 'undefined' ) {
1179                                 for ( edId in tinyMCEPreInit.mceInit ) {
1180                                         if ( firstInit ) {
1181                                                 init = tinyMCEPreInit.mceInit[edId] = tinymce.extend( {}, firstInit, tinyMCEPreInit.mceInit[edId] );
1182                                         } else {
1183                                                 init = firstInit = tinyMCEPreInit.mceInit[edId];
1184                                         }
1185
1186                                         wrapper = tinymce.DOM.select( '#wp-' + edId + '-wrap' )[0];
1187
1188                                         if ( ( tinymce.DOM.hasClass( wrapper, 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( edId ) ) &&
1189                                                 ! init.wp_skip_init ) {
1190
1191                                                 try {
1192                                                         tinymce.init( init );
1193
1194                                                         if ( ! window.wpActiveEditor ) {
1195                                                                 window.wpActiveEditor = edId;
1196                                                         }
1197                                                 } catch(e){}
1198                                         }
1199                                 }
1200                         }
1201
1202                         if ( typeof quicktags !== 'undefined' ) {
1203                                 for ( qtId in tinyMCEPreInit.qtInit ) {
1204                                         try {
1205                                                 quicktags( tinyMCEPreInit.qtInit[qtId] );
1206
1207                                                 if ( ! window.wpActiveEditor ) {
1208                                                         window.wpActiveEditor = qtId;
1209                                                 }
1210                                         } catch(e){};
1211                                 }
1212                         }
1213
1214                         if ( typeof jQuery !== 'undefined' ) {
1215                                 jQuery('.wp-editor-wrap').on( 'click.wp-editor', function() {
1216                                         if ( this.id ) {
1217                                                 window.wpActiveEditor = this.id.slice( 3, -5 );
1218                                         }
1219                                 });
1220                         } else {
1221                                 for ( qtId in tinyMCEPreInit.qtInit ) {
1222                                         document.getElementById( 'wp-' + qtId + '-wrap' ).onclick = function() {
1223                                                 window.wpActiveEditor = this.id.slice( 3, -5 );
1224                                         }
1225                                 }
1226                         }
1227                 }());
1228                 </script>
1229                 <?php
1230
1231                 if ( in_array( 'wplink', self::$plugins, true ) || in_array( 'link', self::$qt_buttons, true ) )
1232                         self::wp_link_dialog();
1233
1234                 if ( in_array( 'wpfullscreen', self::$plugins, true ) || in_array( 'fullscreen', self::$qt_buttons, true ) )
1235                         self::wp_fullscreen_html();
1236
1237                 /**
1238                  * Fires after any core TinyMCE editor instances are created.
1239                  *
1240                  * @since 3.2.0
1241                  *
1242                  * @param array $mce_settings TinyMCE settings array.
1243                  */
1244                 do_action( 'after_wp_tiny_mce', self::$mce_settings );
1245         }
1246
1247         public static function wp_fullscreen_html() {
1248                 global $content_width;
1249                 $post = get_post();
1250
1251                 $width = isset( $content_width ) && 800 > $content_width ? $content_width : 800;
1252                 $width = $width + 22; // compensate for the padding and border
1253                 $dfw_width = get_user_setting( 'dfw_width', $width );
1254                 $save = isset( $post->post_status ) && $post->post_status == 'publish' ? __('Update') : __('Save');
1255
1256                 ?>
1257                 <div id="wp-fullscreen-body" class="wp-core-ui<?php if ( is_rtl() ) echo ' rtl'; ?>" data-theme-width="<?php echo (int) $width; ?>" data-dfw-width="<?php echo (int) $dfw_width; ?>">
1258                 <div id="fullscreen-topbar">
1259                         <div id="wp-fullscreen-toolbar">
1260                         <div id="wp-fullscreen-close"><a href="#" onclick="wp.editor.fullscreen.off();return false;"><?php _e('Exit fullscreen'); ?></a></div>
1261                         <div id="wp-fullscreen-central-toolbar" style="width:<?php echo $width; ?>px;">
1262
1263                         <div id="wp-fullscreen-mode-bar">
1264                                 <div id="wp-fullscreen-modes" class="button-group">
1265                                         <a class="button wp-fullscreen-mode-tinymce" href="#" onclick="wp.editor.fullscreen.switchmode( 'tinymce' ); return false;"><?php _e( 'Visual' ); ?></a>
1266                                         <a class="button wp-fullscreen-mode-html" href="#" onclick="wp.editor.fullscreen.switchmode( 'html' ); return false;"><?php _ex( 'Text', 'Name for the Text editor tab (formerly HTML)' ); ?></a>
1267                                 </div>
1268                         </div>
1269
1270                         <div id="wp-fullscreen-button-bar"><div id="wp-fullscreen-buttons" class="mce-toolbar">
1271                 <?php
1272
1273                 $buttons = array(
1274                         // format: title, onclick, show in both editors
1275                         'bold' => array( 'title' => __('Bold (Ctrl + B)'), 'both' => false ),
1276                         'italic' => array( 'title' => __('Italic (Ctrl + I)'), 'both' => false ),
1277                         'bullist' => array( 'title' => __('Unordered list (Alt + Shift + U)'), 'both' => false ),
1278                         'numlist' => array( 'title' => __('Ordered list (Alt + Shift + O)'), 'both' => false ),
1279                         'blockquote' => array( 'title' => __('Blockquote (Alt + Shift + Q)'), 'both' => false ),
1280                         'wp-media-library' => array( 'title' => __('Media library (Alt + Shift + M)'), 'both' => true ),
1281                         'link' => array( 'title' => __('Insert/edit link (Alt + Shift + A)'), 'both' => true ),
1282                         'unlink' => array( 'title' => __('Unlink (Alt + Shift + S)'), 'both' => false ),
1283                         'help' => array( 'title' => __('Help (Alt + Shift + H)'), 'both' => false ),
1284                 );
1285
1286                 /**
1287                  * Filter the list of TinyMCE buttons for the fullscreen
1288                  * 'Distraction-Free Writing' editor.
1289                  *
1290                  * @since 3.2.0
1291                  *
1292                  * @param array $buttons An array of TinyMCE buttons for the DFW editor.
1293                  */
1294                 $buttons = apply_filters( 'wp_fullscreen_buttons', $buttons );
1295
1296                 foreach ( $buttons as $button => $args ) {
1297                         if ( 'separator' == $args ) {
1298                                 continue;
1299                         }
1300
1301                         $onclick = ! empty( $args['onclick'] ) ? ' onclick="' . $args['onclick'] . '"' : '';
1302                         $title = esc_attr( $args['title'] );
1303                         ?>
1304
1305                         <div class="mce-widget mce-btn<?php if ( $args['both'] ) { ?> wp-fullscreen-both<?php } ?>">
1306                         <button type="button" aria-label="<?php echo $title; ?>" title="<?php echo $title; ?>"<?php echo $onclick; ?> id="wp_fs_<?php echo $button; ?>">
1307                                 <i class="mce-ico mce-i-<?php echo $button; ?>"></i>
1308                         </button>
1309                         </div>
1310                         <?php
1311                 }
1312
1313                 ?>
1314
1315                 </div></div>
1316
1317                 <div id="wp-fullscreen-save">
1318                         <input type="button" class="button button-primary right" value="<?php echo $save; ?>" onclick="wp.editor.fullscreen.save();" />
1319                         <span class="wp-fullscreen-saved-message"><?php if ( $post->post_status == 'publish' ) _e('Updated.'); else _e('Saved.'); ?></span>
1320                         <span class="wp-fullscreen-error-message"><?php _e('Save failed.'); ?></span>
1321                         <span class="spinner"></span>
1322                 </div>
1323
1324                 </div>
1325                 </div>
1326         </div>
1327         <div id="wp-fullscreen-statusbar">
1328                 <div id="wp-fullscreen-status">
1329                         <div id="wp-fullscreen-count"><?php printf( __( 'Word count: %s' ), '<span class="word-count">0</span>' ); ?></div>
1330                         <div id="wp-fullscreen-tagline"><?php _e('Just write.'); ?></div>
1331                 </div>
1332         </div>
1333         </div>
1334
1335         <div class="fullscreen-overlay" id="fullscreen-overlay"></div>
1336         <div class="fullscreen-overlay fullscreen-fader fade-300" id="fullscreen-fader"></div>
1337         <?php
1338         }
1339
1340         /**
1341          * Performs post queries for internal linking.
1342          *
1343          * @since 3.1.0
1344          *
1345          * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
1346          * @return false|array Results.
1347          */
1348         public static function wp_link_query( $args = array() ) {
1349                 $pts = get_post_types( array( 'public' => true ), 'objects' );
1350                 $pt_names = array_keys( $pts );
1351
1352                 $query = array(
1353                         'post_type' => $pt_names,
1354                         'suppress_filters' => true,
1355                         'update_post_term_cache' => false,
1356                         'update_post_meta_cache' => false,
1357                         'post_status' => 'publish',
1358                         'posts_per_page' => 20,
1359                 );
1360
1361                 $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
1362
1363                 if ( isset( $args['s'] ) )
1364                         $query['s'] = $args['s'];
1365
1366                 $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
1367
1368                 /**
1369                  * Filter the link query arguments.
1370                  *
1371                  * Allows modification of the link query arguments before querying.
1372                  *
1373                  * @see WP_Query for a full list of arguments
1374                  *
1375                  * @since 3.7.0
1376                  *
1377                  * @param array $query An array of WP_Query arguments.
1378                  */
1379                 $query = apply_filters( 'wp_link_query_args', $query );
1380
1381                 // Do main query.
1382                 $get_posts = new WP_Query;
1383                 $posts = $get_posts->query( $query );
1384                 // Check if any posts were found.
1385                 if ( ! $get_posts->post_count )
1386                         return false;
1387
1388                 // Build results.
1389                 $results = array();
1390                 foreach ( $posts as $post ) {
1391                         if ( 'post' == $post->post_type )
1392                                 $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
1393                         else
1394                                 $info = $pts[ $post->post_type ]->labels->singular_name;
1395
1396                         $results[] = array(
1397                                 'ID' => $post->ID,
1398                                 'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
1399                                 'permalink' => get_permalink( $post->ID ),
1400                                 'info' => $info,
1401                         );
1402                 }
1403
1404                 /**
1405                  * Filter the link query results.
1406                  *
1407                  * Allows modification of the returned link query results.
1408                  *
1409                  * @since 3.7.0
1410                  *
1411                  * @see 'wp_link_query_args' filter
1412                  *
1413                  * @param array $results {
1414                  *     An associative array of query results.
1415                  *
1416                  *     @type array {
1417                  *         @type int    $ID        Post ID.
1418                  *         @type string $title     The trimmed, escaped post title.
1419                  *         @type string $permalink Post permalink.
1420                  *         @type string $info      A 'Y/m/d'-formatted date for 'post' post type,
1421                  *                                 the 'singular_name' post type label otherwise.
1422                  *     }
1423                  * }
1424                  * @param array $query  An array of WP_Query arguments.
1425                  */
1426                 return apply_filters( 'wp_link_query', $results, $query );
1427         }
1428
1429         /**
1430          * Dialog for internal linking.
1431          *
1432          * @since 3.1.0
1433          */
1434         public static function wp_link_dialog() {
1435                 $search_panel_visible = '1' == get_user_setting( 'wplink', '0' ) ? ' search-panel-visible' : '';
1436
1437                 // display: none is required here, see #WP27605
1438                 ?>
1439                 <div id="wp-link-backdrop" style="display: none"></div>
1440                 <div id="wp-link-wrap" class="wp-core-ui<?php echo $search_panel_visible; ?>" style="display: none">
1441                 <form id="wp-link" tabindex="-1">
1442                 <?php wp_nonce_field( 'internal-linking', '_ajax_linking_nonce', false ); ?>
1443                 <div id="link-modal-title">
1444                         <?php _e( 'Insert/edit link' ) ?>
1445                         <button type="button" id="wp-link-close"><span class="screen-reader-text"><?php _e( 'Close' ); ?></span></button>
1446                 </div>
1447                 <div id="link-selector">
1448                         <div id="link-options">
1449                                 <p class="howto"><?php _e( 'Enter the destination URL' ); ?></p>
1450                                 <div>
1451                                         <label><span><?php _e( 'URL' ); ?></span><input id="url-field" type="text" name="href" /></label>
1452                                 </div>
1453                                 <div>
1454                                         <label><span><?php _e( 'Title' ); ?></span><input id="link-title-field" type="text" name="linktitle" /></label>
1455                                 </div>
1456                                 <div class="link-target">
1457                                         <label><span>&nbsp;</span><input type="checkbox" id="link-target-checkbox" /> <?php _e( 'Open link in a new window/tab' ); ?></label>
1458                                 </div>
1459                         </div>
1460                         <p class="howto"><a href="#" id="wp-link-search-toggle"><?php _e( 'Or link to existing content' ); ?></a></p>
1461                         <div id="search-panel">
1462                                 <div class="link-search-wrapper">
1463                                         <label>
1464                                                 <span class="search-label"><?php _e( 'Search' ); ?></span>
1465                                                 <input type="search" id="search-field" class="link-search-field" autocomplete="off" />
1466                                                 <span class="spinner"></span>
1467                                         </label>
1468                                 </div>
1469                                 <div id="search-results" class="query-results" tabindex="0">
1470                                         <ul></ul>
1471                                         <div class="river-waiting">
1472                                                 <span class="spinner"></span>
1473                                         </div>
1474                                 </div>
1475                                 <div id="most-recent-results" class="query-results" tabindex="0">
1476                                         <div class="query-notice" id="query-notice-message">
1477                                                 <em class="query-notice-default"><?php _e( 'No search term specified. Showing recent items.' ); ?></em>
1478                                                 <em class="query-notice-hint screen-reader-text"><?php _e( 'Search or use up and down arrow keys to select an item.' ); ?></em>
1479                                         </div>
1480                                         <ul></ul>
1481                                         <div class="river-waiting">
1482                                                 <span class="spinner"></span>
1483                                         </div>
1484                                 </div>
1485                         </div>
1486                 </div>
1487                 <div class="submitbox">
1488                         <div id="wp-link-cancel">
1489                                 <a class="submitdelete deletion" href="#"><?php _e( 'Cancel' ); ?></a>
1490                         </div>
1491                         <div id="wp-link-update">
1492                                 <input type="submit" value="<?php esc_attr_e( 'Add Link' ); ?>" class="button button-primary" id="wp-link-submit" name="wp-link-submit">
1493                         </div>
1494                 </div>
1495                 </form>
1496                 </div>
1497                 <?php
1498         }
1499 }