Edit permalink in place. Fixes #5679. Hat tip: nbachiyski.

git-svn-id: http://svn.automattic.com/wordpress/trunk@6633 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
matt 2008-01-17 16:51:32 +00:00
parent 95bbfed206
commit 5fbca12c9e
7 changed files with 114 additions and 5 deletions

View File

@ -473,6 +473,11 @@ case 'closed-postboxes' :
$current_user = wp_get_current_user(); $current_user = wp_get_current_user();
update_usermeta($current_user->ID, 'closedpostboxes', $closed); update_usermeta($current_user->ID, 'closedpostboxes', $closed);
break; break;
case 'sample-permalink':
check_ajax_referer( $action );
$post_id = isset($_POST['post_id'])? intval($_POST['post_id']) : 0;
die(get_sample_permalink_html($post_id, $_POST['new_slug']));
break;
default : default :
do_action( 'wp_ajax_' . $_POST['action'] ); do_action( 'wp_ajax_' . $_POST['action'] );
die('0'); die('0');

View File

@ -70,7 +70,21 @@ addLoadEvent(focusit);
<div id="titlediv"> <div id="titlediv">
<h3><?php _e('Title') ?></h3> <h3><?php _e('Title') ?></h3>
<div class="inside"><input type="text" name="post_title" size="30" tabindex="1" value="<?php echo attribute_escape($post->post_title); ?>" id="title" /></div> <div class="inside">
<input type="text" name="post_title" size="30" tabindex="1" value="<?php echo attribute_escape($post->post_title); ?>" id="title" />
<?php
$sample_permalink_html = get_sample_permalink_html($post->ID);
if ($post->ID && $sample_permalink_html):
?>
<div id="edit-slug-box" style="display: <?php echo $post->ID? 'block' : 'none';?>">
<strong><?php _e('Permalink:'); ?></strong>
<span id="sample-permalink"><?php echo $sample_permalink_html; ?></span>
<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug" onclick="edit_permalink(<?php echo $post->ID; ?>);return false;"><?php _e('Edit');?></a></span>
</div>
<?php
endif;
?>
</div>
</div> </div>
<div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>" class="postarea"> <div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>" class="postarea">
@ -126,7 +140,7 @@ else
</div> </div>
<p class="submit"> <p class="submit">
<input type="submit" name="submit" value="<?php _e('Save'); ?>" style="font-weight: bold;" tabindex="4" /> <input type="submit" name="save" value="<?php _e('Save'); ?>" style="font-weight: bold;" tabindex="4" />
<?php <?php
if ( !in_array( $post->post_status, array('publish', 'future') ) || 0 == $post_ID ) { if ( !in_array( $post->post_status, array('publish', 'future') ) || 0 == $post_ID ) {
?> ?>

View File

@ -524,4 +524,40 @@ function postbox_classes( $id ) {
} }
} }
function get_sample_permalink($id, $name = null) {
$post = &get_post($id);
$original_status = $post->post_status;
$original_date = $post->post_date;
$original_name = $post->post_name;
if (in_array($post->post_status, array('draft', 'pending'))) {
$post->post_status = 'publish';
$post->post_date = date('Y-m-d H:i:s');
$post->post_name = sanitize_title($post->post_name? $post->post_name : $post->post_title, $post->ID);
}
if (!is_null($name)) {
$post->post_name = sanitize_title($name, $post->ID);
}
$permalink = get_permalink($post, true);
$permalink = array($permalink, $post->post_name);
$post->post_status = $original_status;
$post->post_date = $original_date;
$post->post_name = $original_name;
return $permalink;
}
function get_sample_permalink_html($id, $new_slug=null) {
$post = &get_post($id);
list($permalink, $post_name) = get_sample_permalink($post->ID, $new_slug);
if (false === strpos($permalink, '%postname%')) {
return '';
}
$title = __('You can edit this part of the permalink using the Edit button on the right');
if (strlen($post_name) > 30) {
$post_name = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
}
$post_name_html = '<span id="editable-post-name" title="'.$title.'">'.$post_name.'</span>';
$display_link = str_replace('%postname%', $post_name_html, $permalink);
return $display_link;
}
?> ?>

View File

@ -65,6 +65,42 @@ function save_postboxes_state() {
cookie: document.cookie}); cookie: document.cookie});
} }
function edit_permalink(post_id) {
var e = jQuery('#editable-post-name');
var revert_e = e.html();
var real_slug = jQuery('#post_name');
var b = jQuery('#edit-slug-buttons');
var revert_b = b.html();
var old_slug = e.children('span').html();
b.html('<a href="" class="save">'+postL10n.save+'</a> <a class="cancel" href="">'+postL10n.cancel+'</a>');
b.children('.save').click(function() {
var new_slug = e.children('input').attr('value');
jQuery.post(postL10n.requestFile, {
action: 'sample-permalink',
post_id: post_id,
new_slug: new_slug,
cookie: document.cookie}, function(data) {
jQuery('#sample-permalink').html(data);
b.html(revert_b);
real_slug.attr('value', new_slug);
});
return false;
});
jQuery('#edit-slug-buttons .cancel').click(function() {
e.html(revert_e);
b.html(revert_b);
real_slug.attr('value', revert_e);
return false;
});
e.html('<input type="text" id="new-post-slug" value="" />').children('input').keypress(function(e){
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
// on enter, just save the new slug, don't save the post
if (13 == key) {b.children('.save').click();return false;}
if (27 == key) {b.children('.cancel').click();return false;}
real_slug.attr('value', this.value)}).focus();
}
addLoadEvent( function() { addLoadEvent( function() {
jQuery('#tags-input').hide(); jQuery('#tags-input').hide();
tag_update_quickclicks(); tag_update_quickclicks();
@ -136,4 +172,6 @@ addLoadEvent( function() {
return false; return false;
} ); } );
jQuery('.categorychecklist :checkbox').change( syncChecks ).filter( ':checked' ).change(); jQuery('.categorychecklist :checkbox').change( syncChecks ).filter( ':checked' ).change();
jQuery('#editable-post-name').click(function() {jQuery('#edit-slug-buttons').children('.edit-slug').click()});
}); });

View File

@ -1178,6 +1178,19 @@ html, body {
padding: 4px 3px; padding: 4px 3px;
width: 98%; width: 98%;
} }
#edit-slug-box {
margin-top: 8px;
color: #999;
}
#edit-slug-box strong {color: #777;}
#editable-post-name {background-color: #FFFBCC;}
#editable-post-name input {width: 16em;}
#edit-slug-buttons a.save {
background-color: #ebebeb;
-moz-border-raduis: 5px;
padding: 6px;
}
#edit-slug-buttons a.cancel {font-size: 80%;}
#poststuff #editor-toolbar { #poststuff #editor-toolbar {
position: relative; position: relative;
@ -1331,3 +1344,4 @@ ul.categorychecklist li {
.ui-tabs-hide { display: none; } .ui-tabs-hide { display: none; }
.form-input-tip { color: #999; } .form-input-tip { color: #999; }

View File

@ -43,7 +43,7 @@ function permalink_anchor($mode = 'id') {
} }
function get_permalink($id = 0) { function get_permalink($id = 0, $leavename=false) {
$rewritecode = array( $rewritecode = array(
'%year%', '%year%',
'%monthnum%', '%monthnum%',
@ -51,11 +51,11 @@ function get_permalink($id = 0) {
'%hour%', '%hour%',
'%minute%', '%minute%',
'%second%', '%second%',
'%postname%', $leavename? '' : '%postname%',
'%post_id%', '%post_id%',
'%category%', '%category%',
'%author%', '%author%',
'%pagename%' $leavename? '' : '%pagename%',
); );
$post = &get_post($id); $post = &get_post($id);

View File

@ -114,6 +114,8 @@ class WP_Scripts {
'add' => attribute_escape(__('Add')), 'add' => attribute_escape(__('Add')),
'addTag' => attribute_escape(__('Add new tag')), 'addTag' => attribute_escape(__('Add new tag')),
'separate' => __('Separate tags with commas'), 'separate' => __('Separate tags with commas'),
'save' => __('Save'),
'cancel' => __('Cancel'),
'requestFile' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php', 'requestFile' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php',
) ); ) );
$this->add( 'media-upload', '/wp-admin/js/media-upload.js', false, '20080109' ); $this->add( 'media-upload', '/wp-admin/js/media-upload.js', false, '20080109' );