Guest User

Untitled

a guest
Jun 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Add display name / role for post locking dialogs.
  5. *
  6. * @param array $response The Heartbeat response.
  7. * @param array $data The $_POST data sent.
  8. * @param string $screen_id The screen id.
  9. *
  10. * @return array The Heartbeat response.
  11. */
  12. function post_lock_add_display_name_role( $response, $data, $screen_id ) {
  13.  
  14. if ( ! empty( $response['wp-refresh-post-lock']['lock_error'] ) ) {
  15. $post_id = absint( $data['wp-refresh-post-lock']['post_id'] );
  16.  
  17. $user_id = wp_check_post_lock( $post_id );
  18.  
  19. if ( ! $user_id ) {
  20. return $response;
  21. }
  22.  
  23. $display_name_role = post_lock_get_display_name_role( $user_id );
  24.  
  25. $response['wp-refresh-post-lock']['lock_error']['display_name'] = esc_html( $display_name_role['display_name'] );
  26. $response['wp-refresh-post-lock']['lock_error']['role'] = esc_html( $display_name_role['role'] );
  27. }
  28.  
  29. return $response;
  30.  
  31. }
  32.  
  33. add_filter( 'heartbeat_received', 'post_lock_add_display_name_role', 11, 3 );
  34.  
  35. /**
  36. * Get Display Name / Role text from user.
  37. *
  38. * @param int $user_id User ID.
  39. *
  40. * @return array|false Display name and role of user, false if user not found.
  41. */
  42. function post_lock_get_display_name_role( $user_id ) {
  43.  
  44. $user = get_userdata( $user_id );
  45.  
  46. if ( ! $user ) {
  47. return false;
  48. }
  49.  
  50. $roles = array();
  51.  
  52. // Loop through the roles and convert them to labels.
  53. foreach ( $user->roles as $role ) {
  54. $roles[] = ucwords( str_replace( array( '-', '_' ), ' ', $role ) );
  55. }
  56.  
  57. // Combine multiple roles into one string.
  58. $role_text = implode( ', ', $roles );
  59.  
  60. $display_name_role = array(
  61. 'display_name' => $user->display_name,
  62. 'role' => $role_text,
  63. );
  64.  
  65. return $display_name_role;
  66.  
  67. }
  68.  
  69. /**
  70. * Get notice text if a post is locked.
  71. *
  72. * @param int $post_id Post ID.
  73. *
  74. * @return string|false Notice text if post is locked, false if it is not locked.
  75. */
  76. function post_lock_get_display_name_role_from_post( $post_id ) {
  77.  
  78. /**
  79. * Check if the post is locked.
  80. *
  81. * @param int|string $post_id Post ID.
  82. *
  83. * @return string|false User ID who is currently editing or false if the post is not locked.
  84. */
  85. $user_id = wp_check_post_lock( $post_id );
  86.  
  87. if ( ! $user_id ) {
  88. return false;
  89. }
  90.  
  91. return post_lock_get_display_name_role( $user_id );
  92.  
  93. }
  94.  
  95. /**
  96. * Render the post locking frontend notice.
  97. *
  98. * @param int $post_id Post ID.
  99. *
  100. * @return bool Whether the post is currenty locked by another user.
  101. */
  102. function post_lock_frontend_notice( $post_id ) {
  103.  
  104. wp_enqueue_script( 'heartbeat', '', array(), false, true );
  105.  
  106. // Include necessary files.
  107. require_once ABSPATH . 'wp-admin/includes/post.php';
  108. require_once ABSPATH . 'wp-admin/includes/misc.php';
  109. require_once ABSPATH . 'wp-admin/includes/admin-filters.php';
  110.  
  111. // Get the display name / role for post lock (if there is one).
  112. $display_name_role = post_lock_get_display_name_role_from_post( $post_id );
  113.  
  114. $hidden = 'hidden';
  115.  
  116. $is_locked = false;
  117.  
  118. $active_post_lock = array();
  119.  
  120. if ( false !== $display_name_role ) {
  121. $hidden = '';
  122.  
  123. $is_locked = true;
  124. } else {
  125. $display_name_role = array(
  126. 'display_name' => '',
  127. 'role' => '',
  128. );
  129.  
  130. if ( is_user_logged_in() ) {
  131. $active_post_lock = wp_set_post_lock( $post_id );
  132. }
  133. }
  134. ?>
  135. <div id="post-lock-dialog" class="<?php echo esc_attr( $hidden ); ?>">
  136. <?php
  137. printf(
  138. '<span class="user-display-name">%1$s</span> (<span class="user-role">%2$s</span>) is editing.',
  139. esc_html( $display_name_role['display_name'] ),
  140. esc_html( $display_name_role['role'] )
  141. );
  142. ?>
  143. </div>
  144.  
  145. <input type="hidden" id="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'heartbeat-nonce' ) ); ?>" />
  146. <input type="hidden" id="post_ID" value="<?php echo esc_attr( $post_id ); ?>" />
  147. <input type="hidden" id="active_post_lock" value="<?php echo esc_attr( implode( ':', $active_post_lock ) ); ?>" />
  148. <?php
  149.  
  150. return $is_locked;
  151.  
  152. }
Add Comment
Please, Sign In to add comment