Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Fix post lock data inconsistency #37914

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lib/compat/wordpress-6.0/post-lock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Post Lock data filters.
*
* @package gutenberg
*/

/**
* Update post lock error and add user display nama.
*
* @param array $response The Heartbeat response.
* @param array $data The $_POST data sent.
* @return array The Heartbeat response.
*/
function gutenberg_refresh_post_lock( $response, $data ) {
if ( ! isset( $response['wp-refresh-post-lock']['lock_error'] ) ) {
return $response;
}

if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) {
$received = $data['wp-refresh-post-lock'];

$post_id = absint( $received['post_id'] );
if ( ! $post_id ) {
return $response;
}

if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $response;
}

$user_id = wp_check_post_lock( $post_id );
$user = get_userdata( $user_id );
if ( $user ) {
$response['wp-refresh-post-lock']['lock_error']['name'] = $user->display_name;
}
}

return $response;
}
add_filter( 'heartbeat_received', 'gutenberg_refresh_post_lock', 20, 2 );

/**
* Updates post editor settings and adds avatar to the `postLock` user details.
*
* @param array $settings Default editor settings.
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
*
* @return array Filtered editor settings.
*/
function gutenberg_update_post_lock_details( $settings, $block_editor_context ) {
if ( empty( $block_editor_context->post ) ) {
return $settings;
}

if ( ! isset( $settings['postLock']['user'] ) ) {
return $settings;
}

$user_id = wp_check_post_lock( $block_editor_context->post->ID );
if ( $user_id ) {
$settings['postLock']['user']['avatar'] = get_avatar_url( $user_id, array( 'size' => 64 ) );
}

return $settings;
}
add_filter( 'block_editor_settings_all', 'gutenberg_update_post_lock_details', 10, 2 );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php';
require __DIR__ . '/compat/wordpress-5.9/rest-active-global-styles.php';
require __DIR__ . '/compat/wordpress-5.9/move-theme-editor-menu-item.php';
require __DIR__ . '/compat/wordpress-6.0/post-lock.php';
require __DIR__ . '/compat/experimental/blocks.php';

require __DIR__ . '/blocks.php';
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/post-locked-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export default function PostLockedModal() {
isLocked: true,
isTakeover: true,
user: {
name: received.lock_error.name,
avatar: received.lock_error.avatar_src,
},
} );
Expand Down