所以我设法用一点javascript拼凑出一些东西。。。它很粗糙,但我觉得有点优雅。
add_action(\'admin_init\', function () {
if ( current_user_can("send_notifications") ) {
$post_types = ["post", "go_live"];
foreach ($post_types as $post_type) {
add_meta_box(
\'cabtv_notif_on_post\',
\'Notifications\',
function ($post) {
wp_nonce_field( \'cabtv_notif_metabox\', \'cabtv_notif_metabox\' );
$sent = (bool) get_post_meta( $post->ID, \'cabtv_notifications_sent\', true );
$checked = cabtv_should_post_send_notification($post->ID);
?>
<div id="cabtv_notification_sent" style=\'margin-bottom:10px; <?php if (!$sent) echo \'display: none\'; ?>\'><span style=\'color:red; font-weight:bold;\'>Notifications have been sent for this post.</span></div>
<label><input type="checkbox" id="cabtv_send_notification" name="cabtv_send_notification" value="1" <?php if ($checked) echo \'checked\'; ?>></input> <?php echo esc_attr(\'Send notification on \'.($post->post_status === \'publish\' ? \'update\' : \'publish\') ); ?></label>
<script>
$(document).ready(()=>{
function update_meta_box() {
var cb = document.querySelector("#cabtv_send_notification");
if (cb.checked) document.querySelector("#cabtv_notification_sent").style.display = null;
cb.checked = false;
}
var dispatch = wp.data.dispatch( \'core/edit-post\' );
var oldMetaBoxUpdatesSuccess = dispatch.metaBoxUpdatesSuccess;
dispatch.metaBoxUpdatesSuccess = function(...args) {
update_meta_box();
return oldMetaBoxUpdatesSuccess.apply(this, args);
}
});
</script>
<?php },
$post_type,
\'side\',
\'high\'
);
}
}
});
我浏览了wordpress\'
edit-post.js
并找到一个名为
metaBoxUpdatesSuccess
这似乎什么都不做,但它是在所有metabox标题中的微调器消失时调用的。因此,我用包装器替换了该函数,这也会触发我的metabox html更新。令人烦恼的是,这意味着必须在PHP中编写两次相同的内容&;基本上是JS,我可以想象这样一种场景,需要额外的AJAX来获取基本数据。
不管怎样,总比什么都没有好!