Saving/restoring the user interface state, see #7654

git-svn-id: http://svn.automattic.com/wordpress/trunk@8784 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2008-08-31 06:34:43 +00:00
parent ab69c83c26
commit e2b4009e66
10 changed files with 324 additions and 59 deletions

View File

@ -486,7 +486,7 @@ endif; ?>
<div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>" class="postarea"> <div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>" class="postarea">
<div id="add-media-button"><a href="<?php echo clean_url( admin_url( 'media-upload.php?post_id=' . ( $post_ID ? $post_ID : $temp_ID ) . '&amp;type=image&amp;TB_iframe=true' ) ); ?>" class="thickbox button"><?php _e( 'Add Media' ); ?></a></div> <div id="add-media-button"><a id="add-media-link" href="<?php echo clean_url( admin_url( 'media-upload.php?post_id=' . ( $post_ID ? $post_ID : $temp_ID ) . '&amp;type=image&amp;TB_iframe=true' ) ); ?>" class="thickbox button"><?php _e( 'Add Media' ); ?></a></div>
<h3><?php _e('Post') ?></h3> <h3><?php _e('Post') ?></h3>

View File

@ -358,7 +358,7 @@ endif; ?>
<div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>" class="postarea"> <div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>" class="postarea">
<div id="add-media-button"><a href="<?php echo clean_url( admin_url( 'media-upload.php?post_id=' . ( $post_ID ? $post_ID : $temp_ID ) . '&amp;type=image&amp;TB_iframe=true' ) ); ?>" class="thickbox button"><?php _e( 'Add Media' ); ?></a></div> <div id="add-media-button"><a id="add-media-link" href="<?php echo clean_url( admin_url( 'media-upload.php?post_id=' . ( $post_ID ? $post_ID : $temp_ID ) . '&amp;type=image&amp;TB_iframe=true' ) ); ?>" class="thickbox button"><?php _e( 'Add Media' ); ?></a></div>
<h3><?php _e('Page') ?></h3> <h3><?php _e('Page') ?></h3>
<?php the_editor($post->post_content); ?> <?php the_editor($post->post_content); ?>

View File

@ -1,24 +1,31 @@
wpEditorInit = function() {
var H;
// Activate tinyMCE if it's the user's default editor
if ( ( 'undefined' == typeof wpTinyMCEConfig ) || 'tinymce' == wpTinyMCEConfig.defaultEditor ) {
try { document.getElementById('editorcontainer').style.padding = '0px'; } catch(e){};
try { document.getElementById("quicktags").style.display = "none"; } catch(e){};
tinyMCE.execCommand("mceAddControl", false, "content");
} else {
if ( H = tinymce.util.Cookie.getHash("TinyMCE_content_size") )
try { document.getElementById('content').style.height = H.ch - 30 + 'px'; } catch(e){};
}
};
switchEditors = { switchEditors = {
I : function(e) {
return document.getElementById(e);
},
edInit : function() {
var h = tinymce.util.Cookie.getHash("TinyMCE_content_size"), H = this.I('edButtonHTML'), P = this.I('edButtonPreview');
// Activate TinyMCE if it's the user's default editor
if ( getUserSetting( 'editor', 'tinymce' ) == 'tinymce' ) {
try { P.onclick = ''; P.className = 'active'; } catch(e){};
try { this.I('editorcontainer').style.padding = '0px'; } catch(e){};
try { this.I("quicktags").style.display = "none"; } catch(e){};
tinyMCE.execCommand("mceAddControl", false, "content");
} else {
try { H.onclick = ''; H.className = 'active'; } catch(e){};
if ( h )
try { this.I('content').style.height = h.ch - 30 + 'px'; } catch(e){};
}
},
saveCallback : function(el, content, body) { saveCallback : function(el, content, body) {
document.getElementById(el).style.color = '#fff'; this.I(el).style.color = '#fff';
if ( tinyMCE.activeEditor.isHidden() ) if ( tinyMCE.activeEditor.isHidden() )
content = document.getElementById(el).value; content = this.I(el).value;
else else
content = this.pre_wpautop(content); content = this.pre_wpautop(content);
@ -84,11 +91,11 @@ switchEditors = {
go : function(id) { go : function(id) {
var ed = tinyMCE.get(id); var ed = tinyMCE.get(id);
var qt = document.getElementById('quicktags'); var qt = this.I('quicktags');
var H = document.getElementById('edButtonHTML'); var H = this.I('edButtonHTML');
var P = document.getElementById('edButtonPreview'); var P = this.I('edButtonPreview');
var ta = document.getElementById(id); var ta = this.I(id);
var ec = document.getElementById('editorcontainer'); var ec = (ta.parentNode && ta.parentNode.nodeName == 'DIV') ? ta.parentNode : '';
if ( ! ed || ed.isHidden() ) { if ( ! ed || ed.isHidden() ) {
ta.style.color = '#fff'; ta.style.color = '#fff';
@ -97,15 +104,16 @@ switchEditors = {
edCloseAllTags(); // :-( edCloseAllTags(); // :-(
qt.style.display = 'none'; qt.style.display = 'none';
ec.style.padding = '0px';
ta.style.padding = '0px'; ta.style.padding = '0px';
if ( ec )
ec.style.padding = '0px';
ta.value = this.wpautop(ta.value); ta.value = this.wpautop(ta.value);
if ( ed ) ed.show(); if ( ed ) ed.show();
else tinyMCE.execCommand("mceAddControl", false, id); else tinyMCE.execCommand("mceAddControl", false, id);
this.wpSetDefaultEditor('tinymce'); setUserSetting( 'editor', 'tinymce' );
} else { } else {
this.edToggle(H, P); this.edToggle(H, P);
ta.style.height = ed.getContentAreaContainer().offsetHeight + 6 + 'px'; ta.style.height = ed.getContentAreaContainer().offsetHeight + 6 + 'px';
@ -115,15 +123,17 @@ switchEditors = {
if ( tinymce.isIE6 ) { if ( tinymce.isIE6 ) {
ta.style.width = '98%'; ta.style.width = '98%';
if ( ec )
ec.style.padding = '0px'; ec.style.padding = '0px';
ta.style.padding = '6px'; ta.style.padding = '6px';
} else { } else {
ta.style.width = '100%'; ta.style.width = '100%';
if ( ec )
ec.style.padding = '6px'; ec.style.padding = '6px';
} }
ta.style.color = ''; ta.style.color = '';
this.wpSetDefaultEditor('html'); setUserSetting( 'editor', 'html' );
} }
}, },
@ -135,19 +145,6 @@ switchEditors = {
A.onclick = null; A.onclick = null;
}, },
wpSetDefaultEditor : function(editor) {
try {
editor = escape( editor.toString() );
} catch(err) {
editor = 'tinymce';
}
var userID = document.getElementById('user-id');
var date = new Date();
date.setTime(date.getTime()+(10*365*24*60*60*1000));
document.cookie = "wordpress_editor_" + userID.value + "=" + editor + "; expires=" + date.toGMTString();
},
wpautop : function(pee) { wpautop : function(pee) {
var blocklist = 'table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6]'; var blocklist = 'table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6]';

View File

@ -0,0 +1,129 @@
wpCookies = {
// The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
each : function(o, cb, s) {
var n, l;
if (!o)
return 0;
s = s || o;
if (typeof(o.length) != 'undefined') {
for (n=0, l = o.length; n<l; n++) {
if (cb.call(s, o[n], n, o) === false)
return 0;
}
} else {
for (n in o) {
if (o.hasOwnProperty(n)) {
if (cb.call(s, o[n], n, o) === false)
return 0;
}
}
}
return 1;
},
getHash : function(n) {
var v = this.get(n), h;
if (v) {
this.each(v.split('&'), function(v) {
v = v.split('=');
h = h || {};
h[v[0]] = v[1];
});
}
return h;
},
setHash : function(n, v, e, p, d, s) {
var o = '';
this.each(v, function(v, k) {
o += (!o ? '' : '&') + k + '=' + v;
});
this.set(n, o, e, p, d, s);
},
get : function(n) {
var c = document.cookie, e, p = n + "=", b;
if (!c)
return;
b = c.indexOf("; " + p);
if (b == -1) {
b = c.indexOf(p);
if (b != 0)
return null;
} else
b += 2;
e = c.indexOf(";", b);
if (e == -1)
e = c.length;
return decodeURIComponent(c.substring(b + p.length, e));
},
set : function(n, v, e, p, d, s) {
document.cookie = n + "=" + encodeURIComponent(v) +
((e) ? "; expires=" + e.toGMTString() : "") +
((p) ? "; path=" + p : "") +
((d) ? "; domain=" + d : "") +
((s) ? "; secure" : "");
},
remove : function(n, p) {
var d = new Date();
d.setTime(d.getTime() - 1000);
this.set(n, '', d, p, d);
}
};
// Returns the value as string. Second arg or empty string is returned when value is not set.
function getUserSetting( name, def ) {
var o = getAllUserSettings();
if ( o.hasOwnProperty(name) )
return o[name];
if ( typeof def != 'undefined' )
return def;
return '';
}
// Both name and value must be only ASCII letters, numbers or underscore
// and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
function setUserSetting( name, value, del ) {
var c = 'wp-settings-'+userSettings.uid, o = wpCookies.getHash(c) || {}, d = new Date();
var n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, '');
if ( del ) delete o[n];
else o[n] = v;
d.setTime( d.getTime() + 31536000000 );
p = userSettings.url;
wpCookies.setHash(c, o, d, p );
wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, d, p );
}
function deleteUserSetting( name ) {
setUserSetting( name, '', 1 );
}
// Returns all settings as js object.
function getAllUserSettings() {
return wpCookies.getHash('wp-settings-'+userSettings.uid) || {};
}

View File

@ -192,5 +192,6 @@ add_action('comment_form', 'wp_comment_form_unfiltered_html_nonce');
add_action('template_redirect', 'wp_old_slug_redirect'); add_action('template_redirect', 'wp_old_slug_redirect');
add_action('edit_post', 'wp_check_for_changed_slugs'); add_action('edit_post', 'wp_check_for_changed_slugs');
add_action('edit_form_advanced', 'wp_remember_old_slug'); add_action('edit_form_advanced', 'wp_remember_old_slug');
add_action('init', 'wp_user_settings', 9);
?> ?>

View File

@ -643,6 +643,134 @@ function delete_option( $name ) {
return true; return true;
} }
/**
* Saves and restores user interface settings stored in a cookie.
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*
* Checks if the current user-settings cookie is updated and stores it.
* When no cookie exists (different browser used), adds the last saved cookie restoring the settings.
*/
function wp_user_settings() {
if ( ! is_admin() )
return;
if ( ! $user = wp_get_current_user() )
return;
$settings = get_user_option( 'user-settings', $user->ID );
if ( isset($_COOKIE['wp-settings-'.$user->ID]) ) {
$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-'.$user->ID] );
if ( ! empty($cookie) && strpos($cookie, '=') ) {
if ( $cookie == $settings )
return;
$last_time = (int) get_user_option( 'user-settings-time', $user->ID );
$saved = isset($_COOKIE['wp-settings-time-'.$user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-'.$user->ID] ) : 0;
if ( $saved > $last_time ) {
update_user_option( $user->ID, 'user-settings', $cookie );
update_user_option( $user->ID, 'user-settings-time', time() - 5 );
return;
}
}
}
setcookie('wp-settings-'.$user->ID, $settings, time() + 31536000, SITECOOKIEPATH);
setcookie('wp-settings-time-'.$user->ID, time(), time() + 31536000, SITECOOKIEPATH);
}
/**
* Retrieve user interface setting value based on setting name.
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*
* @param string $name The name of the setting.
* @param string $default Optional default value to return when $name is not set.
* @return mixed the last saved user setting or the default value/false if it doesn't exist.
*/
function get_user_setting( $name, $default = false ) {
$arr = get_all_user_settings();
return isset($arr[$name]) ? $arr[$name] : $default;
}
/**
* Delete user interface settings.
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*
* Deleting settings would reset them to the defaults.
*
* @param mixed $names The name or array of names of the setting to be deleted.
*/
function delete_user_setting( $names ) {
global $current_user;
$arr = get_all_user_settings();
$names = (array) $names;
foreach ( $names as $name ) {
if ( isset($arr[$name]) ) {
unset($arr[$name]);
$settings = '';
}
}
if ( isset($settings) ) {
foreach ( $arr as $k => $v )
$settings .= $k . '=' . $v . '&';
$settings = rtrim($settings, '&');
update_user_option( $current_user->ID, 'user-settings', $settings );
setcookie('wp-settings-'.$current_user->ID, $settings, time() + 31536000, SITECOOKIEPATH);
}
}
/**
* Retrieve all user interface settings.
*
* @package WordPress
* @subpackage Option
* @since 2.7.0
*
* @return array the last saved user settings or empty array.
*/
function get_all_user_settings() {
if ( ! $user = wp_get_current_user() )
return array();
if ( isset($_COOKIE['wp-settings-'.$user->ID]) ) {
$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-'.$user->ID] );
if ( $cookie && strpos($cookie, '=') ) { // the '=' cannot be 1st char
parse_str($cookie, $arr);
return $arr;
}
}
return array();
}
function delete_all_user_settings() {
if ( ! $user = wp_get_current_user() )
return;
delete_usermeta( $user->ID, 'user-settings' );
setcookie('wp-settings-'.$user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
}
/** /**
* Serialize data, if needed. * Serialize data, if needed.
* *

View File

@ -942,8 +942,8 @@ function user_can_richedit() {
function wp_default_editor() { function wp_default_editor() {
$r = user_can_richedit() ? 'tinymce' : 'html'; // defaults $r = user_can_richedit() ? 'tinymce' : 'html'; // defaults
if ( $user = wp_get_current_user() ) { // look for cookie if ( $user = wp_get_current_user() ) { // look for cookie
if ( isset($_COOKIE['wordpress_editor_' . $user->ID]) && in_array($_COOKIE['wordpress_editor_' . $user->ID], array('tinymce', 'html', 'test') ) ) $ed = get_user_setting('editor', 'tinymce');
$r = $_COOKIE['wordpress_editor_' . $user->ID]; $r = ( in_array($ed, array('tinymce', 'html', 'test') ) ) ? $ed : $r;
} }
return apply_filters( 'wp_default_editor', $r ); // filter return apply_filters( 'wp_default_editor', $r ); // filter
} }

View File

@ -14,7 +14,7 @@
var moreHTML = '<img src="' + url + '/img/trans.gif" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />'; var moreHTML = '<img src="' + url + '/img/trans.gif" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
var nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />'; var nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
if ( tinymce.util.Cookie.get('kitchenSink') == '1' ) if ( getUserSetting('hidetb', '0') == '1' )
ed.settings.wordpress_adv_hidden = 0; ed.settings.wordpress_adv_hidden = 0;
// Hides the specified toolbar and resizes the iframe // Hides the specified toolbar and resizes the iframe
@ -44,23 +44,20 @@
}); });
ed.addCommand('WP_Adv', function() { ed.addCommand('WP_Adv', function() {
var id = ed.controlManager.get(tbId).id, cm = ed.controlManager, cook = tinymce.util.Cookie, date; var id = ed.controlManager.get(tbId).id, cm = ed.controlManager;
date = new Date();
date.setTime(date.getTime()+(10*365*24*60*60*1000));
if (DOM.isHidden(id)) { if (DOM.isHidden(id)) {
cm.setActive('wp_adv', 1); cm.setActive('wp_adv', 1);
DOM.show(id); DOM.show(id);
t._resizeIframe(ed, tbId, -28); t._resizeIframe(ed, tbId, -28);
ed.settings.wordpress_adv_hidden = 0; ed.settings.wordpress_adv_hidden = 0;
cook.set('kitchenSink', '1', date); setUserSetting('hidetb', '1');
} else { } else {
cm.setActive('wp_adv', 0); cm.setActive('wp_adv', 0);
DOM.hide(id); DOM.hide(id);
t._resizeIframe(ed, tbId, 28); t._resizeIframe(ed, tbId, 28);
ed.settings.wordpress_adv_hidden = 1; ed.settings.wordpress_adv_hidden = 1;
cook.set('kitchenSink', '0', date); setUserSetting('hidetb', '0');
} }
}); });
@ -94,11 +91,14 @@
title : 'wordpress.add_media', title : 'wordpress.add_media',
image : url + '/img/media.gif', image : url + '/img/media.gif',
onclick : function() { onclick : function() {
tb_show('', tinymce.DOM.get('add_media').href); var a = tinymce.DOM.get('add-media-link');
if ( a ) {
tb_show('', a.href);
tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' ); tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
} }
}
}); });
/*
ed.addButton('add_image', { ed.addButton('add_image', {
title : 'wordpress.add_image', title : 'wordpress.add_image',
image : url + '/img/image.gif', image : url + '/img/image.gif',
@ -125,12 +125,12 @@
tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' ); tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
} }
}); });
*/
// Add Media buttons to fullscreen // Add Media buttons to fullscreen
ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) { ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
if ( 'mceFullScreen' != cmd ) return; if ( 'mceFullScreen' != cmd ) return;
if ( 'mce_fullscreen' != ed.id ) if ( 'mce_fullscreen' != ed.id )
ed.settings.theme_advanced_buttons1 += ',|,add_image,add_video,add_audio,add_media'; ed.settings.theme_advanced_buttons1 += ',|,add_media';
}); });
// Add class "alignleft", "alignright" and "aligncenter" when selecting align for images. // Add class "alignleft", "alignright" and "aligncenter" when selecting align for images.

View File

@ -134,7 +134,7 @@ $no_captions = ( apply_filters( 'disable_captions', '' ) ) ? true : false;
// TinyMCE init settings // TinyMCE init settings
$initArray = array ( $initArray = array (
'mode' => 'none', 'mode' => 'none',
'onpageload' => 'wpEditorInit', 'onpageload' => 'switchEditors.edInit',
'width' => '100%', 'width' => '100%',
'theme' => 'advanced', 'theme' => 'advanced',
'skin' => 'wp_theme', 'skin' => 'wp_theme',
@ -214,7 +214,7 @@ if ( $compress && isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
// Setup cache info // Setup cache info
if ( $disk_cache ) { if ( $disk_cache ) {
$cacheKey = apply_filters('tiny_mce_version', '20080731'); $cacheKey = apply_filters('tiny_mce_version', '20080830');
foreach ( $initArray as $v ) foreach ( $initArray as $v )
$cacheKey .= $v; $cacheKey .= $v;

View File

@ -35,12 +35,16 @@ require( ABSPATH . WPINC . '/functions.wp-styles.php' );
* @param object $scripts WP_Scripts object. * @param object $scripts WP_Scripts object.
*/ */
function wp_default_scripts( &$scripts ) { function wp_default_scripts( &$scripts ) {
global $current_user;
if (!$guessurl = site_url()) if (!$guessurl = site_url())
$guessurl = wp_guess_url(); $guessurl = wp_guess_url();
$userid = isset($current_user) ? $current_user->ID : 0;
$scripts->base_url = $guessurl; $scripts->base_url = $guessurl;
$scripts->default_version = get_bloginfo( 'version' ); $scripts->default_version = get_bloginfo( 'version' );
$scripts->add( 'common', '/wp-admin/js/common.js', array('jquery'), '20080318' ); $scripts->add( 'common', '/wp-admin/js/common.js', array('jquery', 'user-settings'), '20080318' );
$scripts->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' ); $scripts->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
$scripts->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '20080823' ); $scripts->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '20080823' );
@ -65,7 +69,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'editor_functions', '/wp-admin/js/editor.js', false, '20080823' ); $scripts->add( 'editor_functions', '/wp-admin/js/editor.js', false, '20080823' );
// Modify this version when tinyMCE plugins are changed. // Modify this version when tinyMCE plugins are changed.
$mce_version = apply_filters('tiny_mce_version', '20080730'); $mce_version = apply_filters('tiny_mce_version', '20080830');
$scripts->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('editor_functions'), $mce_version ); $scripts->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('editor_functions'), $mce_version );
$scripts->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.6'); $scripts->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.6');
@ -242,6 +246,13 @@ function wp_default_scripts( &$scripts ) {
) ); ) );
$scripts->add( 'farbtastic', '/wp-admin/js/farbtastic.js', array('jquery'), '1.2' ); $scripts->add( 'farbtastic', '/wp-admin/js/farbtastic.js', array('jquery'), '1.2' );
$scripts->add( 'user-settings', '/wp-admin/js/user-settings.js', array(), '20080829' );
$scripts->localize( 'user-settings', 'userSettings', array(
'url' => SITECOOKIEPATH,
'uid' => $userid,
'time' => time()
) );
} }
} }
@ -334,7 +345,6 @@ function wp_prototype_before_jquery( $js_array ) {
* @since 2.5.0 * @since 2.5.0
*/ */
function wp_just_in_time_script_localization() { function wp_just_in_time_script_localization() {
wp_localize_script( 'tiny_mce', 'wpTinyMCEConfig', array( 'defaultEditor' => wp_default_editor() ) );
wp_localize_script( 'autosave', 'autosaveL10n', array( wp_localize_script( 'autosave', 'autosaveL10n', array(
'autosaveInterval' => AUTOSAVE_INTERVAL, 'autosaveInterval' => AUTOSAVE_INTERVAL,
'previewPageText' => __('Preview this Page'), 'previewPageText' => __('Preview this Page'),