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