| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post Lock data filters. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Update post lock error and add user display nama. |
| 10 |
* |
| 11 |
* @param array $response The Heartbeat response. |
| 12 |
* @param array $data The $_POST data sent. |
| 13 |
* @return array The Heartbeat response. |
| 14 |
*/ |
| 15 |
function gutenberg_refresh_post_lock( $response, $data ) { |
| 16 |
if ( ! isset( $response['wp-refresh-post-lock']['lock_error'] ) ) { |
| 17 |
return $response; |
| 18 |
} |
| 19 |
|
| 20 |
if ( array_key_exists( 'wp-refresh-post-lock', $data ) ) { |
| 21 |
$received = $data['wp-refresh-post-lock']; |
| 22 |
|
| 23 |
$post_id = absint( $received['post_id'] ); |
| 24 |
if ( ! $post_id ) { |
| 25 |
return $response; |
| 26 |
} |
| 27 |
|
| 28 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 29 |
return $response; |
| 30 |
} |
| 31 |
|
| 32 |
$user_id = wp_check_post_lock( $post_id ); |
| 33 |
$user = get_userdata( $user_id ); |
| 34 |
if ( $user ) { |
| 35 |
$response['wp-refresh-post-lock']['lock_error']['name'] = $user->display_name; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return $response; |
| 40 |
} |
| 41 |
add_filter( 'heartbeat_received', 'gutenberg_refresh_post_lock', 20, 2 ); |
| 42 |
|
| 43 |
/** |
| 44 |
* Updates post editor settings and adds avatar to the `postLock` user details. |
| 45 |
* |
| 46 |
* @param array $settings Default editor settings. |
| 47 |
* @param WP_Block_Editor_Context $block_editor_context The current block editor context. |
| 48 |
* |
| 49 |
* @return array Filtered editor settings. |
| 50 |
*/ |
| 51 |
function gutenberg_update_post_lock_details( $settings, $block_editor_context ) { |
| 52 |
if ( empty( $block_editor_context->post ) ) { |
| 53 |
return $settings; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! isset( $settings['postLock']['user'] ) ) { |
| 57 |
return $settings; |
| 58 |
} |
| 59 |
|
| 60 |
$user_id = wp_check_post_lock( $block_editor_context->post->ID ); |
| 61 |
if ( $user_id ) { |
| 62 |
$settings['postLock']['user']['avatar'] = get_avatar_url( $user_id, array( 'size' => 128 ) ); |
| 63 |
} |
| 64 |
|
| 65 |
return $settings; |
| 66 |
} |
| 67 |
add_filter( 'block_editor_settings_all', 'gutenberg_update_post_lock_details', 10, 2 ); |
| 68 |
|