Autosave and prototype. Props masquerade. fixes #3020 #3017

git-svn-id: http://svn.automattic.com/wordpress/trunk@4082 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2006-08-11 03:54:45 +00:00
parent 0ead59849c
commit b4601fc06c
9 changed files with 2181 additions and 12 deletions

View File

@ -216,6 +216,36 @@ case 'add-user' :
header('Content-type: text/xml');
die($r);
break;
case 'autosave' :
$_POST['post_content'] = $_POST['content'];
$_POST['post_excerpt'] = $_POST['excerpt'];
$_POST['post_status'] = 'draft';
$_POST['post_category'] = explode(",", $_POST['catslist']);
if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
unset($_POST['post_category']);
if($_POST['post_ID'] < 0) {
$_POST['temp_ID'] = $_POST['post_ID'];
$id = wp_write_post();
if(is_wp_error($id))
die($id->get_error_message());
else
die("$id");
} else {
$post_ID = (int) $_POST['post_ID'];
$_POST['ID'] = $post_ID;
$post = get_post($post_ID);
if ( 'page' == $post->post_type ) {
if ( !current_user_can('edit_page', $post_ID) )
die(__('You are not allowed to edit this page.'));
} else {
if ( !current_user_can('edit_post', $post_ID) )
die(__('You are not allowed to edit this post.'));
}
wp_update_post($_POST);
}
die('0');
break;
default :
do_action( 'wp_ajax_' . $_POST['action'] );
die('0');

View File

@ -1,15 +1,23 @@
<?php
// Creates a new post from the "Write Post" form using $_POST information.
function write_post() {
$result = wp_write_post();
if( is_wp_error($result) )
wp_die( $result->get_error_message() );
else
return $result;
}
// Creates a new post from the "Write Post" form using $_POST information.
function wp_write_post() {
global $user_ID;
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can('edit_pages') )
wp_die(__('You are not allowed to create pages on this blog.'));
return new WP_Error('edit_pages', __('You are not allowed to create pages on this blog.'));
} else {
if ( !current_user_can('edit_posts') )
wp_die(__('You are not allowed to create posts or drafts on this blog.'));
return new WP_Error('edit_posts', __('You are not allowed to create posts or drafts on this blog.'));
}
// Rename.
@ -32,10 +40,10 @@ function write_post() {
if ($_POST['post_author'] != $_POST['user_ID']) {
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can('edit_others_pages') )
wp_die(__('You cannot create pages as this user.'));
return new WP_Error('edit_others_pages', __('You cannot create pages as this user.'));
} else {
if ( !current_user_can('edit_others_posts') )
wp_die(__('You cannot post as this user.'));
return new WP_Error('edit_others_posts', __('You cannot post as this user.'));
}
}

View File

@ -3,6 +3,8 @@ require_once('admin.php');
$title = __('New Page');
$parent_file = 'post-new.php';
$editing = true;
wp_enqueue_script('prototype');
wp_enqueue_script('autosave');
require_once('admin-header.php');
?>

View File

@ -42,15 +42,17 @@ case 'post':
case 'edit':
$title = __('Edit');
$editing = true;
require_once('admin-header.php');
$page_ID = $post_ID = $p = (int) $_GET['post'];
$post = get_post_to_edit($page_ID);
if($post->post_status == 'draft') {
wp_enqueue_script('prototype');
wp_enqueue_script('autosave');
}
require_once('admin-header.php');
if ( !current_user_can('edit_page', $page_ID) )
die ( __('You are not allowed to edit this page.') );
$post = get_post_to_edit($page_ID);
include('edit-page-form.php');
?>
<div id='preview' class='wrap'>

View File

@ -3,6 +3,8 @@ require_once('admin.php');
$title = __('Create New Post');
$parent_file = 'post-new.php';
$editing = true;
wp_enqueue_script('prototype');
wp_enqueue_script('autosave');
require_once ('./admin-header.php');
if ( ! current_user_can('edit_posts') ) { ?>

View File

@ -45,11 +45,14 @@ case 'post':
case 'edit':
$title = __('Edit');
$editing = true;
$post_ID = $p = (int) $_GET['post'];
$post = get_post($post_ID);
if($post->post_status == 'draft') {
wp_enqueue_script('prototype');
wp_enqueue_script('autosave');
}
require_once('admin-header.php');
$post_ID = $p = (int) $_GET['post'];
$post = get_post($post_ID);
if ( !current_user_can('edit_post', $post_ID) )
die ( __('You are not allowed to edit this post.') );

View File

@ -0,0 +1,112 @@
<?php @require_once('../../wp-config.php');
$expiresOffset = 3600 * 24 * 10; // 10 days util client cache expires
header("Content-type: text/javascript; charset: UTF-8");
header("Vary: Accept-Encoding"); // Handle proxies
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
?>
function autosave_adddiv() {
var buttons = document.getElementsByClassName('submit')[0].innerHTML;
document.getElementsByClassName('submit')[0].innerHTML = '<span id="autosave"></span>' + buttons;
}
function autosave_timer() {
autosave();
setTimeout("autosave_timer()", <?php echo apply_filters('autosave_interval', '60000') ?>);
}
function autosave_start_timer() {
setTimeout("autosave_timer()", <?php echo apply_filters('autosave_start_delay', '60000') ?>);
}
addLoadEvent(autosave_start_timer)
addLoadEvent(autosave_adddiv);
function autosave_cur_time() {
var now = new Date();
return "" + ((now.getHours() >12) ? now.getHours() -12 : now.getHours()) +
((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes() +
((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();
}
function autosave_update_post_ID() {
var response = autosaveAjax.response;
var res = parseInt(response);
var message;
if(isNaN(res)) {
message = "<?php _e('Error: '); ?>" + response;
} else {
message = "<?php _e('Saved at '); ?>" + autosave_cur_time();
$('post_ID').name = "post_ID";
$('post_ID').value = res;
}
$('autosave').innerHTML = message;
}
function autosave_loading() {
$('autosave').innerHTML = "<?php _e('Saving Draft...'); ?>";
}
function autosave_saved() {
var response = autosaveAjax.response;
var res = parseInt(response);
var message;
if(isNaN(res)) {
message = "<?php _e('Error: '); ?>" + response;
} else {
message = "<?php _e('Saved at '); ?>" + autosave_cur_time() + ".";
}
$('autosave').innerHTML = message;
}
function autosave() {
autosaveAjax = new sack();
form = $('post');
/* Gotta do this up here so we can check the length when tinyMCE is in use */
if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 ) {
autosaveAjax.setVar("content", form.content.value);
} else {
tinyMCE.triggerSave();
autosaveAjax.setVar("content", form.content.value);
}
if(form.post_title.value.length==0 || form.content.value.length==0)
return;
cats = document.getElementsByName("post_category[]");
goodcats = ([]);
for(i=0;i<cats.length;i++) {
if(cats[i].checked)
goodcats.push(cats[i].value);
}
catslist = goodcats.join(",");
autosaveAjax.setVar("action", "autosave");
autosaveAjax.setVar("cookie", document.cookie);
autosaveAjax.setVar("catslist", catslist);
autosaveAjax.setVar("post_ID", $("post_ID").value);
autosaveAjax.setVar("post_title", form.post_title.value);
autosaveAjax.setVar("post_type", form.post_type.value);
if(form.excerpt)
autosaveAjax.setVar("excerpt", form.excerpt.value);
if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 ) {
autosaveAjax.setVar("content", form.content.value);
} else {
tinyMCE.triggerSave();
autosaveAjax.setVar("content", form.content.value);
}
autosaveAjax.requestFile = "<?php echo get_bloginfo('siteurl'); ?>/wp-admin/admin-ajax.php";
autosaveAjax.method = "POST";
autosaveAjax.element = null;
autosaveAjax.onLoading = autosave_loading;
autosaveAjax.onInteractive = autosave_loading;
if(parseInt($("post_ID").value) < 1)
autosaveAjax.onCompletion = autosave_update_post_ID;
else
autosaveAjax.onCompletion = autosave_saved;
autosaveAjax.runAJAX();
}

2008
wp-includes/js/prototype.js vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -17,6 +17,8 @@ class WP_Scripts {
$this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' );
$this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '04162006' );
$this->add( 'wp_tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('tiny_mce'), '04162006' );
$this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0');
$this->add( 'autosave', '/wp-includes/js/autosave.js.php', array('prototype', 'sack'), '4080');
if ( is_admin() ) {
$this->add( 'dbx-admin-key', '/wp-admin/dbx-admin-key-js.php', array('dbx'), '3651' );
$this->add( 'listman', '/wp-admin/list-manipulation-js.php', array('sack', 'fat'), '4042' ); // Make changeset # the correct one