我的解决方案有两种工作方式:通过修改the list-revisions
Javascript file, 如果通过某种方式选择了两个相同的修订版本,它会提供一条真正的错误消息。
此屏幕截图为您提供了错误消息的快速概述,以及新Javascript的效果。如您所见,不再可能选择同一修订两次。如果你想看到效果,你要么禁用Javascript(注释掉add_action((\'wp_default_scripts\', ...
行)或手动更改URL。
它是作为插件编写的。对于多站点安装,您可以将其放入mu-plugins
, 并让初始化依赖于数据库配置选项。这样,您可以为不同的客户端启用或禁用它。
欢迎对我的插件编写风格发表评论,我尝试将代码分为三个逻辑块
主插件文件。此文件切换list-revisions
具有“更安全”版本的Javascript文件,可防止两次选择同一版本。它还检查admin_action_diff
如果两个修订版本相等,则返回带有错误消息的自有页面的操作和快捷方式。新页面已写入ui-identical.php
分离代码和布局。
<?php
/*
Plugin Name: Myxomatosis
Plugin URI: http://www.monkeyman.be/
Description: Kill the Easter Bunny and his eggs!
Version: 1.0
Author: Jan Fabry
*/
class Myxomatosis
{
function __construct()
{
add_action(\'admin_action_diff\', array(&$this, \'checkEqualDiff\'));
add_action(\'wp_default_scripts\', array(&$this, \'loadSafeListRevisions\'), 20);
}
function checkEqualDiff()
{
global $left, $right;
if (!array_key_exists(\'left\', $_REQUEST) || !array_key_exists(\'right\', $_REQUEST)) {
return;
}
$left = absint($_REQUEST[\'left\']);
$right = absint($_REQUEST[\'right\']);
if (!$left || !$right || $left != $right) {
// Nothing wrong, they are not equal
return;
}
// They are equal. Set up globals so UI can be included
// Which globals do I need here?
global $title, $hook_suffix, $pagenow, $is_iphone, $current_screen, $user_identity, $post, $wp_locale;
$post = get_post($left);
if ($post->post_parent) {
$post = get_post($post->post_parent);
}
require_once(plugin_dir_path(__FILE__) . \'ui-identical.php\');
exit();
}
function loadSafeListRevisions(&$scripts)
{
$scripts->remove(\'list-revisions\');
$scripts->add(\'list-revisions\', plugin_dir_url(__FILE__) . \'safe-list-revisions.dev.js\', null, \'20101026\' );
}
}
$myxomatosis_instance = new Myxomatosis();
文件
ui-identical.php
, 在插件目录中。这将创建一个页面,显示两个修订是否相同。将再次显示修订列表。
<?php
wp_enqueue_script(\'list-revisions\');
$post_title = \'<a href="\' . get_edit_post_link() . \'">\' . get_the_title() . \'</a>\';
$h2 = sprintf( __( \'Compare Revisions of “%1$s”\' ), $post_title );
$title = __( \'Revisions\' );
require_once(ABSPATH . \'/wp-admin/admin-header.php\');
?>
<div class="wrap">
<h2 class="long-header"><?php echo $h2; ?></h2>
<div class="error"><p><?php _e( \'You selected two identical revisions\' ); ?></p></div>
<h2><?php echo $title; ?></h2>
<?php
$args = array( \'format\' => \'form-table\', \'parent\' => true, \'right\' => $right, \'left\' => $left );
if ( ! WP_POST_REVISIONS || !post_type_supports($post->post_type, \'revisions\') )
$args[\'type\'] = \'autosave\';
wp_list_post_revisions( $post, $args );
?>
</div>
<?php
require_once(ABSPATH . \'/wp-admin/admin-footer.php\');
文件
safe-list-revisions.dev.js
, 在插件目录中。逻辑分为两个循环:一个用于读取当前选中的按钮,另一个用于隐藏和显示其他按钮。这可能比原始版本更简单,并且允许在中间进行一些调整。
(function(w) {
var init = function() {
var pr = document.getElementById(\'post-revisions\'),
var inputs = pr ? pr.getElementsByTagName(\'input\') : [];
if (inputs.length <= 2) {
// Sanity check: if there is only one revision, hide nothing
// Not that we should get here...
return;
}
// For simplicity, only one click handler is registered on the whole #post-revisions table
pr.onclick = function() {
var i, checkCount = 0, side;
var checkedIdx = {};
for (i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
side = inputs[i].getAttribute(\'name\');
checkedIdx[side] = i;
}
}
if (checkedIdx[\'left\'] + 1 == checkedIdx[\'right\']) {
// The same revisions are checked
// Move left down (or right up, if left is at the bottom)
// There should always be at least two revisions (see sanity check above)
if (checkedIdx[\'left\'] + 2 < inputs.length) {
checkedIdx[\'left\'] += 2;
inputs[checkedIdx[\'left\']].checked = true;
} else {
checkedIdx[\'right\'] -= 2;
inputs[checkedIdx[\'right\']].checked = true;
}
}
if (checkedIdx[\'left\'] < checkedIdx[\'right\']) {
// \'Old\' is newer than \'New\', so switch them
// We sometimes get this - don\'t know why
var origLeft = checkedIdx[\'left\'];
checkedIdx[\'left\'] = checkedIdx[\'right\'] - 1;
checkedIdx[\'right\'] = origLeft + 1;
inputs[checkedIdx[\'left\']].checked = true;
inputs[checkedIdx[\'right\']].checked = true;
}
// Loop again, hide what we should not see
// \'left\' buttons should be hidden until we see a checked \'right\' button
// \'right\' buttons should be visible until we see a checked \'left\' button
for (i = 0; i < inputs.length; i++) {
side = inputs[i].getAttribute(\'name\');
if (\'left\' == side) {
inputs[i].style.visibility = (i < checkedIdx[\'right\'] ? \'hidden\' : \'visible\');
} else if (\'right\' == side) {
inputs[i].style.visibility = (checkedIdx[\'left\'] < i ? \'hidden\' : \'visible\');
}
}
}
pr.onclick();
}
if ( w && w.addEventListener )
w.addEventListener(\'load\', init, false);
else if ( w && w.attachEvent )
w.attachEvent(\'onload\', init);
})(window);