函数文档

_admin_notice_post_locked()

💡 云策文档标注

概述

_admin_notice_post_locked() 函数用于生成一个 HTML 通知,告知当前用户有其他用户正在编辑或已接管编辑此文章。它检查文章锁定状态,并根据条件显示相应对话框和操作按钮。

关键要点

  • 函数通过 wp_check_post_lock() 检查文章是否被其他用户锁定,并获取锁定用户信息。
  • 使用 show_post_locked_dialog 过滤器控制是否显示锁定对话框,默认 true。
  • 根据锁定状态和引用页面,动态生成返回链接和文本,支持自定义文章类型。
  • 在锁定情况下,提供预览链接和覆盖锁定选项,后者可通过 override_post_lock 过滤器控制。
  • 输出包含用户头像、消息、操作按钮(如“接管编辑”或“返回”)的 HTML 结构。
  • 涉及多个 Hook,如 post_locked_dialog 和 post_lock_lost_dialog,用于在对话框显示前后触发动作。

代码示例

// 示例:调用 _admin_notice_post_locked() 函数
_admin_notice_post_locked();

注意事项

  • 函数依赖于 get_post() 获取当前文章对象,若无文章则直接返回。
  • 使用 esc_html() 和 esc_url() 确保输出安全,防止 XSS 攻击。
  • 非标准文章类型需通过 add_query_arg() 调整返回链接。
  • 覆盖锁定功能默认启用,但可通过 override_post_lock 过滤器禁用。

📄 原文内容

Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.

Source

function _admin_notice_post_locked() {
$post = get_post();

if ( ! $post ) {
return;
}

$user = null;
$user_id = wp_check_post_lock( $post->ID );

if ( $user_id ) {
$user = get_userdata( $user_id );
}

if ( $user ) {
/**
* Filters whether to show the post locked dialog.
*
* Returning false from the filter will prevent the dialog from being displayed.
*
* @since 3.6.0
*
* @param bool $display Whether to display the dialog. Default true.
* @param WP_Post $post Post object.
* @param WP_User $user The user with the lock for the post.
*/
if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) ) {
return;
}

$locked = true;
} else {
$locked = false;
}

$sendback = wp_get_referer();
$sendback_text = __( 'Go back' );

if ( ! $locked || ! $sendback || str_contains( $sendback, 'post.php' ) || str_contains( $sendback, 'post-new.php' ) ) {
$sendback = admin_url( 'edit.php' );

if ( 'post' !== $post->post_type ) {
$sendback = add_query_arg( 'post_type', $post->post_type, $sendback );
}

$post_type_object = get_post_type_object( $post->post_type );

if ( $post_type_object ) {
$sendback_text = $post_type_object->labels->all_items;
}
}

$hidden = $locked ? '' : ' hidden';

?>
<div id="post-lock-dialog" class="notification-dialog-wrap<?php echo $hidden; ?>">
<div class="notification-dialog-background"></div>
<div class="notification-dialog">
post_type )->public ) {
if ( 'publish' === $post->post_status || $user->ID !== (int) $post->post_author ) {
// Latest content is in autosave.
$nonce = wp_create_nonce( 'post_preview_' . $post->ID );
$query_args['preview_id'] = $post->ID;
$query_args['preview_nonce'] = $nonce;
}
}

$preview_link = get_preview_post_link( $post->ID, $query_args );

/**
* Filters whether to allow the post lock to be overridden.
*
* Returning false from the filter will disable the ability
* to override the post lock.
*
* @since 3.6.0
*
* @param bool $override Whether to allow the post lock to be overridden. Default true.
* @param WP_Post $post Post object.
* @param WP_User $user The user with the lock for the post.
*/
$override = apply_filters( 'override_post_lock', true, $post, $user );
$tab_last = $override ? '' : ' wp-tab-last';

?>
<div class="post-locked-message">
<div class="post-locked-avatar">ID, 64 ); ?></div>
<p class="currently-editing wp-tab-first" tabindex="0">
display_name ) );
} else {
/* translators: %s: User's display name. */
printf( __( '%s is currently editing this post.' ), esc_html( $user->display_name ) );
}
?>
</p>

<p>
<a class="button" href="<?php echo esc_url( $sendback ); ?>"></a>

<a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"></a>

<a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', wp_nonce_url( get_edit_post_link( $post->ID, 'url' ), 'lock-post_' . $post->ID ) ) ); ?>"></a>

</p>
</div>

<div class="post-taken-over">
<div class="post-locked-avatar"></div>
<p class="wp-tab-first" tabindex="0">
<span class="currently-editing"></span><br />
<span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" alt="" /> </span>
<span class="locked-saved hidden"></span>
</p>

<p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"></a></p>
</div>

</div>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/post.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/post.php#L1790">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/post.php#L1790-L1950">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/override_post_lock/"><span class="hook-func">apply_filters</span>( ‘override_post_lock’, <nobr><span class="arg-type">bool</span> <span class="arg-name">$override</span></nobr>, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr>, <nobr><span class="arg-type">WP_User</span> <span class="arg-name">$user</span></nobr> )</a></dt><dd><p>Filters whether to allow the post lock to be overridden.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/post_locked_dialog/"><span class="hook-func">do_action</span>( ‘post_locked_dialog’, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr>, <nobr><span class="arg-type">WP_User</span> <span class="arg-name">$user</span></nobr> )</a></dt><dd><p>Fires inside the post locked dialog before the buttons are displayed.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/post_lock_lost_dialog/"><span class="hook-func">do_action</span>( ‘post_lock_lost_dialog’, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Fires inside the dialog displayed when a user has lost the post lock.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/show_post_locked_dialog/"><span class="hook-func">apply_filters</span>( ‘show_post_locked_dialog’, <nobr><span class="arg-type">bool</span> <span class="arg-name">$display</span></nobr>, <nobr><span class="arg-type">WP_Post</span> <span class="arg-name">$post</span></nobr>, <nobr><span class="arg-type">WP_User</span> <span class="arg-name">$user</span></nobr> )</a></dt><dd><p>Filters whether to show the post locked dialog.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_preview_post_link/">get_preview_post_link()</a><code>wp-includes/link-template.php

Retrieves the URL used for the post preview.

wp_check_post_lock()wp-admin/includes/post.php

Determines whether the post is currently being edited by another user.

get_avatar()wp-includes/pluggable.php

Retrieves the avatar <img> tag for a user, email address, MD5 hash, comment, or post.

wp_get_referer()wp-includes/functions.php

Retrieves referer from ‘_wp_http_referer’ or HTTP referer.

get_edit_post_link()wp-includes/link-template.php

Retrieves the edit post link for post.

__()wp-includes/l10n.php

Retrieves the translation of $text.

_e()wp-includes/l10n.php

Displays translated text.

esc_html()wp-includes/formatting.php

Escaping for HTML blocks.

esc_url()wp-includes/formatting.php

Checks and cleans a URL.

wp_create_nonce()wp-includes/pluggable.php

Creates a cryptographic token tied to a specific action, user, user session, and window of time.

get_userdata()wp-includes/pluggable.php

Retrieves user info by user ID.

wp_nonce_url()wp-includes/functions.php

Retrieves URL with nonce added to URL query.

add_query_arg()wp-includes/functions.php

Retrieves a modified URL query string.

admin_url()wp-includes/link-template.php

Retrieves the URL to the admin area for the current site.

apply_filters()wp-includes/plugin.php

Calls the callback functions that have been added to a filter hook.

do_action()wp-includes/plugin.php

Calls the callback functions that have been added to an action hook.

get_post()wp-includes/post.php

Retrieves post data given a post ID or post object.

get_post_type_object()wp-includes/post.php

Retrieves a post type object by name.

Show 13 moreShow less

Changelog

Version Description
2.8.5 Introduced.