From a7c296dd4707720890f9b1d66633e45f0c24d0dd Mon Sep 17 00:00:00 2001 From: nacin Date: Wed, 23 Nov 2011 19:06:52 +0000 Subject: [PATCH] Move WP_Editor to a _WP_Editors encapsulation. * WP_Editor will return in 3.4 as a one true API for editor instances. Stick to wp_editor() for now. * TinyMCE can now be forced on with tinymce = true. It defaults to the value for user_can_richedit(). * Restores wp_default_editor(), wp_link_query(), wp_link_dialog(), wp_fullscreen_html(). fixes #19320. git-svn-id: http://svn.automattic.com/wordpress/trunk@19420 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-ajax.php | 2 +- wp-admin/includes/deprecated.php | 25 +-- wp-includes/class-wp-editor.php | 212 ++++++++++------------ wp-includes/deprecated.php | 21 --- wp-includes/general-template.php | 34 +++- wp-includes/js/tinymce/langs/wp-langs.php | 23 +-- 6 files changed, 137 insertions(+), 180 deletions(-) diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index 718ed3e6b..c21c861e4 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -1127,7 +1127,7 @@ case 'wp-link-ajax': $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1; require(ABSPATH . WPINC . '/class-wp-editor.php'); - $results = WP_Editor::wp_link_query( $args ); + $results = _WP_Editors::wp_link_query( $args ); if ( ! isset( $results ) ) die( '0' ); diff --git a/wp-admin/includes/deprecated.php b/wp-admin/includes/deprecated.php index 9d4420413..efe155778 100644 --- a/wp-admin/includes/deprecated.php +++ b/wp-admin/includes/deprecated.php @@ -711,18 +711,12 @@ function wp_dashboard_quick_press_output() { function wp_tiny_mce( $teeny = false, $settings = false ) { _deprecated_function( __FUNCTION__, '3.3', 'wp_editor()' ); - global $wp_editor; static $num = 1; - if ( !is_a($wp_editor, 'WP_Editor') ) { - if ( !class_exists('WP_Editor') ) - require_once( ABSPATH . WPINC . '/class-wp-editor.php' ); + if ( ! class_exists('_WP_Editors' ) ) + require_once( ABSPATH . WPINC . '/class-wp-editor.php' ); - $wp_editor = new WP_Editor; - } - - $editor_id = 'content' . $num; - ++$num; + $editor_id = 'content' . $num++; $set = array( 'teeny' => $teeny, @@ -730,8 +724,8 @@ function wp_tiny_mce( $teeny = false, $settings = false ) { 'quicktags' => false ); - $set = $wp_editor->parse_settings($editor_id, $set); - $wp_editor->editor_settings($editor_id, $set); + $set = _WP_Editors::parse_settings($editor_id, $set); + _WP_Editors::editor_settings($editor_id, $set); } /** @@ -761,15 +755,6 @@ function wp_quicktags() { _deprecated_function( __FUNCTION__, '3.3', 'wp_editor()' ); } -/** - * @deprecated 3.3.0 - * @deprecated Use wp_editor() - * @see wp_editor() - */ -function wp_fullscreen_html() { - _deprecated_function( __FUNCTION__, '3.3', 'wp_editor()' ); -} - /** * Returns the screen layout options. * diff --git a/wp-includes/class-wp-editor.php b/wp-includes/class-wp-editor.php index cbde53daf..8ed9163ca 100644 --- a/wp-includes/class-wp-editor.php +++ b/wp-includes/class-wp-editor.php @@ -8,31 +8,26 @@ * Private, not included by default. See wp_editor() in wp-includes/general-template.php. */ -class WP_Editor { +final class _WP_Editors { + public static $mce_locale; - var $mce_settings = array(); - var $qt_settings = array(); - var $plugins = array(); - var $qt_buttons = array(); - var $mce_locale; - var $ext_plugins; - var $baseurl; - var $can_richedit; - var $default_editor; - var $first_init; - var $this_tinymce = false; - var $this_quicktags = false; - var $has_tinymce = false; - var $has_quicktags = false; - var $has_medialib = false; - var $editor_buttons_css = true; + private static $mce_settings = array(); + private static $qt_settings = array(); + private static $plugins = array(); + private static $qt_buttons = array(); + private static $ext_plugins; + private static $baseurl; + private static $first_init; + private static $this_tinymce = false; + private static $this_quicktags = false; + private static $has_tinymce = false; + private static $has_quicktags = false; + private static $has_medialib = false; + private static $editor_buttons_css = true; - function __construct() { - $this->can_richedit = user_can_richedit(); - $this->default_editor = $this->wp_default_editor(); - } + private function __construct() {} - function parse_settings($editor_id, $settings) { + public static function parse_settings($editor_id, $settings) { $set = wp_parse_args( $settings, array( 'wpautop' => true, // use wpautop? 'media_buttons' => true, // show insert/upload button(s) @@ -43,18 +38,22 @@ class WP_Editor { 'editor_class' => '', // add extra class(es) to the editor textarea 'teeny' => false, // output the minimal editor config used in Press This 'dfw' => false, // replace the default fullscreen with DFW (needs specific DOM elements and css) - 'tinymce' => true, // load TinyMCE, can be used to pass settings directly to TinyMCE using an array() + 'tinymce' => null, // load TinyMCE, can be used to pass settings directly to TinyMCE using an array() 'quicktags' => true // load Quicktags, can be used to pass settings directly to Quicktags using an array() ) ); - $this->this_tinymce = !empty($set['tinymce']) && $this->can_richedit; - $this->this_quicktags = !empty($set['quicktags']); + if ( null === $set['tinymce'] ) + self::$this_tinymce = user_can_richedit(); + else + self::$this_tinymce = (bool) $set['tinymce']; - if ( $this->this_tinymce ) - $this->has_tinymce = true; + self::$this_quicktags = (bool) $set['quicktags']; - if ( $this->this_quicktags ) - $this->has_quicktags = true; + if ( self::$this_tinymce ) + self::$has_tinymce = true; + + if ( self::$this_quicktags ) + self::$has_quicktags = true; return $set; } @@ -64,11 +63,11 @@ class WP_Editor { * * @param string $content The initial content of the editor. * @param string $editor_id ID for the textarea and TinyMCE and Quicktags instances (can contain only ASCII letters and numbers). - * @param array $settings See WP_Editor::_parse_settings for description. + * @param array $settings See the _parse_settings() method for description. */ - function editor( $content, $editor_id, $settings = array() ) { + public static function editor( $content, $editor_id, $settings = array() ) { - $set = $this->parse_settings($editor_id, $settings); + $set = self::parse_settings($editor_id, $settings); $editor_class = ' class="' . trim( $set['editor_class'] . ' wp-editor-area' ) . '"'; $tabindex = $set['tabindex'] ? ' tabindex="' . (int) $set['tabindex'] . '"' : ''; $rows = ' rows="' . (int) $set['textarea_rows'] . '"'; @@ -78,10 +77,10 @@ class WP_Editor { if ( !current_user_can( 'upload_files' ) ) $set['media_buttons'] = false; - if ( $this->this_quicktags && $this->this_tinymce ) { + if ( self::$this_quicktags && self::$this_tinymce ) { $switch_class = 'html-active'; - if ( 'html' == $this->default_editor ) { + if ( 'html' == wp_default_editor() ) { add_filter('the_editor_content', 'wp_htmledit_pre'); } else { add_filter('the_editor_content', 'wp_richedit_pre'); @@ -94,9 +93,9 @@ class WP_Editor { echo '
'; - if ( $this->editor_buttons_css ) { + if ( self::$editor_buttons_css ) { wp_print_styles('editor-buttons'); - $this->editor_buttons_css = false; + self::$editor_buttons_css = false; } if ( !empty($set['editor_css']) ) @@ -107,7 +106,7 @@ class WP_Editor { echo $buttons; if ( $set['media_buttons'] ) { - $this->has_medialib = true; + self::$has_medialib = true; if ( !function_exists('media_buttons') ) include(ABSPATH . 'wp-admin/includes/media.php'); @@ -125,24 +124,24 @@ class WP_Editor { printf($the_editor, $content); echo "\n
\n\n"; - $this->editor_settings($editor_id, $set); + self::editor_settings($editor_id, $set); } - function editor_settings($editor_id, $set) { + public static function editor_settings($editor_id, $set) { global $editor_styles; $first_run = false; - if ( empty($this->first_init) ) { + if ( empty(self::$first_init) ) { if ( is_admin() ) { - add_action( 'admin_print_footer_scripts', array($this, 'editor_js'), 50 ); - add_action( 'admin_footer', array($this, 'enqueue_scripts'), 1 ); + add_action( 'admin_print_footer_scripts', array( __CLASS__, 'editor_js'), 50 ); + add_action( 'admin_footer', array( __CLASS__, 'enqueue_scripts'), 1 ); } else { - add_action( 'wp_print_footer_scripts', array($this, 'editor_js'), 50 ); - add_action( 'wp_footer', array($this, 'enqueue_scripts'), 1 ); + add_action( 'wp_print_footer_scripts', array( __CLASS__, 'editor_js'), 50 ); + add_action( 'wp_footer', array( __CLASS__, 'enqueue_scripts'), 1 ); } } - if ( $this->this_quicktags ) { + if ( self::$this_quicktags ) { $qtInit = array( 'id' => $editor_id, @@ -159,22 +158,22 @@ class WP_Editor { $qtInit['buttons'] .= ',fullscreen'; $qtInit = apply_filters('quicktags_settings', $qtInit, $editor_id); - $this->qt_settings[$editor_id] = $qtInit; + self::$qt_settings[$editor_id] = $qtInit; - $this->qt_buttons = array_merge( $this->qt_buttons, explode(',', $qtInit['buttons']) ); + self::$qt_buttons = array_merge( self::$qt_buttons, explode(',', $qtInit['buttons']) ); } - if ( $this->this_tinymce ) { + if ( self::$this_tinymce ) { - if ( empty($this->first_init) ) { - $this->baseurl = includes_url('js/tinymce'); - $this->mce_locale = $mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1 + if ( empty(self::$first_init) ) { + self::$baseurl = includes_url('js/tinymce'); + self::$mce_locale = $mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1 $no_captions = (bool) apply_filters( 'disable_captions', '' ); $plugins = array( 'inlinepopups', 'spellchecker', 'tabfocus', 'paste', 'media', 'fullscreen', 'wordpress', 'wpeditimage', 'wpgallery', 'wplink', 'wpdialogs' ); $first_run = true; if ( $set['teeny'] ) { - $this->plugins = $plugins = apply_filters( 'teeny_mce_plugins', array('inlinepopups', 'fullscreen', 'wordpress', 'wplink', 'wpdialogs'), $editor_id ); + self::$plugins = $plugins = apply_filters( 'teeny_mce_plugins', array('inlinepopups', 'fullscreen', 'wordpress', 'wplink', 'wpdialogs'), $editor_id ); $ext_plugins = ''; } else { /* @@ -253,7 +252,7 @@ class WP_Editor { $ext_plugins .= 'tinyMCEPreInit.load_ext("' . $plugurl . '", "' . $mce_locale . '");' . "\n"; $ext_plugins .= 'tinymce.PluginManager.load("' . $name . '", "' . $url . '");' . "\n"; - $this->ext_plugins .= $ext_plugins; + self::$ext_plugins .= $ext_plugins; } } @@ -263,7 +262,7 @@ class WP_Editor { if ( $set['dfw'] ) $plugins[] = 'wpfullscreen'; - $this->plugins = $plugins; + self::$plugins = $plugins; /* The following filter allows localization scripts to change the languages displayed in the spellchecker's drop-down menu. @@ -273,12 +272,12 @@ class WP_Editor { */ $mce_spellchecker_languages = apply_filters('mce_spellchecker_languages', '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv'); - $this->first_init = array( + self::$first_init = array( 'mode' => 'exact', 'width' => '100%', 'theme' => 'advanced', 'skin' => 'wp_theme', - 'language' => $this->mce_locale, + 'language' => self::$mce_locale, 'spellchecker_languages' => $mce_spellchecker_languages, 'theme_advanced_toolbar_location' => 'top', 'theme_advanced_toolbar_align' => 'left', @@ -317,7 +316,7 @@ class WP_Editor { 'paste_text_use_dialog' => true, 'extended_valid_elements' => 'article[*],aside[*],audio[*],canvas[*],command[*],datalist[*],details[*],embed[*],figcaption[*],figure[*],footer[*],header[*],hgroup[*],keygen[*],mark[*],meter[*],nav[*],output[*],progress[*],section[*],source[*],summary,time[*],video[*],wbr', 'wpeditimage_disable_captions' => $no_captions, - 'wp_fullscreen_content_css' => "$this->baseurl/plugins/wpfullscreen/css/wp-fullscreen.css", + 'wp_fullscreen_content_css' => self::$baseurl . '/plugins/wpfullscreen/css/wp-fullscreen.css', 'plugins' => implode( ',', $plugins ) ); @@ -352,7 +351,7 @@ class WP_Editor { $mce_css = trim( apply_filters( 'mce_css', $mce_css ), ' ,' ); if ( ! empty($mce_css) ) - $this->first_init['content_css'] = $mce_css; + self::$first_init['content_css'] = $mce_css; } if ( $set['teeny'] ) { @@ -389,7 +388,7 @@ class WP_Editor { ); if ( $first_run ) - $mceInit = array_merge($this->first_init, $mceInit); + $mceInit = array_merge(self::$first_init, $mceInit); if ( is_array($set['tinymce']) ) $mceInit = array_merge($mceInit, $set['tinymce']); @@ -409,11 +408,11 @@ class WP_Editor { $mceInit['theme_advanced_buttons4'] = ''; } - $this->mce_settings[$editor_id] = $mceInit; - } // end if $this->this_tinymce + self::$mce_settings[$editor_id] = $mceInit; + } // end if self::$this_tinymce } - function _parse_init($init) { + private static function _parse_init($init) { $options = ''; foreach ( $init as $k => $v ) { @@ -431,50 +430,31 @@ class WP_Editor { return '{' . trim( $options, ' ,' ) . '}'; } - /** - * Find out which editor should be displayed by default. - * - * Works out which of the two editors to display as the current editor for a - * user. - * - * @since 2.5.0 - * - * @return string Either 'tinymce', or 'html', or 'test' - */ - function wp_default_editor() { - $r = user_can_richedit() ? 'tinymce' : 'html'; // defaults - if ( $user = wp_get_current_user() ) { // look for cookie - $ed = get_user_setting('editor', 'tinymce'); - $r = ( in_array($ed, array('tinymce', 'html', 'test') ) ) ? $ed : $r; - } - return apply_filters( 'wp_default_editor', $r ); // filter - } - - function enqueue_scripts() { + public static function enqueue_scripts() { wp_enqueue_script('word-count'); - if ( $this->has_tinymce ) + if ( self::$has_tinymce ) wp_enqueue_script('editor'); - if ( $this->has_quicktags ) + if ( self::$has_quicktags ) wp_enqueue_script('quicktags'); - if ( in_array('wplink', $this->plugins, true) || in_array('link', $this->qt_buttons, true) ) { + if ( in_array('wplink', self::$plugins, true) || in_array('link', self::$qt_buttons, true) ) { wp_enqueue_script('wplink'); wp_enqueue_script('wpdialogs-popup'); wp_enqueue_style('wp-jquery-ui-dialog'); } - if ( in_array('wpfullscreen', $this->plugins, true) || in_array('fullscreen', $this->qt_buttons, true) ) + if ( in_array('wpfullscreen', self::$plugins, true) || in_array('fullscreen', self::$qt_buttons, true) ) wp_enqueue_script('wp-fullscreen'); - if ( $this->has_medialib ) { + if ( self::$has_medialib ) { add_thickbox(); wp_enqueue_script('media-upload'); } } - function editor_js() { + public static function editor_js() { global $tinymce_version, $concatenate_scripts, $compress_scripts; /** @@ -485,7 +465,7 @@ class WP_Editor { * 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). */ $version = 'ver=' . $tinymce_version; - $tmce_on = !empty($this->mce_settings); + $tmce_on = !empty(self::$mce_settings); if ( ! isset($concatenate_scripts) ) script_concat_settings(); @@ -493,13 +473,13 @@ class WP_Editor { $compressed = $compress_scripts && $concatenate_scripts && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'); - if ( $tmce_on && 'en' != $this->mce_locale ) + if ( $tmce_on && 'en' != self::$mce_locale ) include_once(ABSPATH . WPINC . '/js/tinymce/langs/wp-langs.php'); $mceInit = $qtInit = ''; if ( $tmce_on ) { - foreach ( $this->mce_settings as $editor_id => $init ) { - $options = $this->_parse_init( $init ); + foreach ( self::$mce_settings as $editor_id => $init ) { + $options = self::_parse_init( $init ); $mceInit .= "'$editor_id':{$options},"; } $mceInit = '{' . trim($mceInit, ',') . '}'; @@ -507,9 +487,9 @@ class WP_Editor { $mceInit = '{}'; } - if ( !empty($this->qt_settings) ) { - foreach ( $this->qt_settings as $editor_id => $init ) { - $options = $this->_parse_init( $init ); + if ( !empty(self::$qt_settings) ) { + foreach ( self::$qt_settings as $editor_id => $init ) { + $options = self::_parse_init( $init ); $qtInit .= "'$editor_id':{$options},"; } $qtInit = '{' . trim($qtInit, ',') . '}'; @@ -518,43 +498,45 @@ class WP_Editor { } $ref = array( - 'plugins' => implode( ',', $this->plugins ), + 'plugins' => implode( ',', self::$plugins ), 'theme' => 'advanced', - 'language' => $this->mce_locale + 'language' => self::$mce_locale ); - do_action('before_wp_tiny_mce', $this->mce_settings); + do_action('before_wp_tiny_mce', self::$mce_settings); ?> baseurl/wp-tinymce.php?c=1&$version'>\n"; + echo "\n"; else - echo "\n"; + echo "\n"; - if ( 'en' != $this->mce_locale && isset($lang) ) + if ( 'en' != self::$mce_locale && isset($lang) ) echo "\n"; else - echo "\n"; + echo "\n"; } ?> plugins, true) || in_array('link', $this->qt_buttons, true) ) - $this->wp_link_dialog(); + if ( in_array('wplink', self::$plugins, true) || in_array('link', self::$qt_buttons, true) ) + self::wp_link_dialog(); - if ( in_array('wpfullscreen', $this->plugins, true) || in_array('fullscreen', $this->qt_buttons, true) ) - $this->wp_fullscreen_html(); + if ( in_array('wpfullscreen', self::$plugins, true) || in_array('fullscreen', self::$qt_buttons, true) ) + self::wp_fullscreen_html(); - do_action('after_wp_tiny_mce', $this->mce_settings); + do_action('after_wp_tiny_mce', self::$mce_settings); } - function wp_fullscreen_html() { + public static function wp_fullscreen_html() { global $content_width, $post; $width = isset($content_width) && 800 > $content_width ? $content_width : 800; @@ -705,7 +687,7 @@ class WP_Editor { * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments. * @return array Results. */ - function wp_link_query( $args = array() ) { + public static function wp_link_query( $args = array() ) { $pts = get_post_types( array( 'public' => true ), 'objects' ); $pt_names = array_keys( $pts ); @@ -758,7 +740,7 @@ class WP_Editor { * * @since 3.1.0 */ - function wp_link_dialog() { + public static function wp_link_dialog() { ?>