wp_check_locked_posts

函数
wp_check_locked_posts ( $response, $data, $screen_id )
参数
  • (array) $response The Heartbeat response.
    Required:
  • (array) $data The $_POST data sent.
    Required:
  • (string) $screen_id The screen ID.
    Required:
返回值
  • (array) The Heartbeat response.
定义位置
相关方法
wp_check_for_changed_slugswp_check_for_changed_dateswp_check_post_lockwp_count_postswp_remote_post
引入
3.6.0
弃用
-

wp_check_locked_posts: 这是一个检查一个文章是否被锁定编辑的函数。它可以用来防止两个用户同时编辑同一个文章。

检查显示在文章屏幕上的文章的锁定状态。

function wp_check_locked_posts( $response, $data, $screen_id ) {
	$checked = array();

	if ( array_key_exists( 'wp-check-locked-posts', $data ) && is_array( $data['wp-check-locked-posts'] ) ) {
		foreach ( $data['wp-check-locked-posts'] as $key ) {
			$post_id = absint( substr( $key, 5 ) );

			if ( ! $post_id ) {
				continue;
			}

			$user_id = wp_check_post_lock( $post_id );

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

				if ( $user && current_user_can( 'edit_post', $post_id ) ) {
					$send = array(
						'name' => $user->display_name,
						/* translators: %s: User's display name. */
						'text' => sprintf( __( '%s is currently editing' ), $user->display_name ),
					);

					if ( get_option( 'show_avatars' ) ) {
						$send['avatar_src']    = get_avatar_url( $user->ID, array( 'size' => 18 ) );
						$send['avatar_src_2x'] = get_avatar_url( $user->ID, array( 'size' => 36 ) );
					}

					$checked[ $key ] = $send;
				}
			}
		}
	}

	if ( ! empty( $checked ) ) {
		$response['wp-check-locked-posts'] = $checked;
	}

	return $response;
}

常见问题

FAQs
查看更多 >