<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[23661] trunk/wp-admin: Check post locks with heartbeat and display modal notifications when a post is locked or a user takes over editing, props dh-shredder, see #23697</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23661">23661</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2013-03-12 03:22:30 +0000 (Tue, 12 Mar 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Check post locks with heartbeat and display modal notifications when a post is locked or a user takes over editing, props dh-shredder, see <a href="http://core.trac.wordpress.org/ticket/23697">#23697</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmincsswpadmincss">trunk/wp-admin/css/wp-admin.css</a></li>
<li><a href="#trunkwpadminincludesajaxactionsphp">trunk/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunkwpadminincludesmiscphp">trunk/wp-admin/includes/misc.php</a></li>
<li><a href="#trunkwpadminincludespostphp">trunk/wp-admin/includes/post.php</a></li>
<li><a href="#trunkwpadminjspostjs">trunk/wp-admin/js/post.js</a></li>
<li><a href="#trunkwpadminpostphp">trunk/wp-admin/post.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmincsswpadmincss"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/css/wp-admin.css (23660 => 23661)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/css/wp-admin.css        2013-03-12 01:50:46 UTC (rev 23660)
+++ trunk/wp-admin/css/wp-admin.css        2013-03-12 03:22:30 UTC (rev 23661)
</span><span class="lines">@@ -3407,7 +3407,47 @@
</span><span class="cx">         border-style: solid;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#notification-dialog {
+        position: fixed;
+        top: 30%;
+        left: 50%;
+        width: 450px;
+        margin-left: -225px;
+        background: #fff;
+        z-index: 1000005;
+}
</ins><span class="cx"> 
</span><ins>+#notification-dialog-background {
+        position: fixed;
+        top: 0;
+        left: 0;
+        right: 0;
+        bottom: 0;
+        background: #000;
+        opacity: 0.5;
+        filter: alpha(opacity=50);
+        z-index: 1000000;
+}
+
+#notification-dialog .post-locked-message,
+#notification-dialog .post-taken-over {
+        margin: 25px;
+}
+
+#notification-dialog .post-locked-message a.button-primary {
+        margin: 0 10px;
+}
+
+#notification-dialog .post-locked-avatar {
+        float: left;
+        margin-right: 20px;
+}
+
+#notification-dialog .currently-editing {
+        margin-bottom: 20px;
+}
+
+
</ins><span class="cx"> /*------------------------------------------------------------------------------
</span><span class="cx">   11.1 - Custom Fields
</span><span class="cx"> ------------------------------------------------------------------------------*/
</span></span></pre></div>
<a id="trunkwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/ajax-actions.php (23660 => 23661)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/ajax-actions.php        2013-03-12 01:50:46 UTC (rev 23660)
+++ trunk/wp-admin/includes/ajax-actions.php        2013-03-12 03:22:30 UTC (rev 23661)
</span><span class="lines">@@ -1057,6 +1057,7 @@
</span><span class="cx">                 $_POST['post_status'] = 'draft';
</span><span class="cx"> 
</span><span class="cx">         if ( $last = wp_check_post_lock( $post-&gt;ID ) ) {
</span><ins>+                // This will change after we have per-user autosaves
</ins><span class="cx">                 $do_autosave = $do_lock = false;
</span><span class="cx"> 
</span><span class="cx">                 $last_user = get_userdata( $last );
</span><span class="lines">@@ -1064,7 +1065,6 @@
</span><span class="cx">                 $data = __( 'Autosave disabled.' );
</span><span class="cx"> 
</span><span class="cx">                 $supplemental['disable_autosave'] = 'disable';
</span><del>-                $alert .= sprintf( __( '%s is currently editing this article. If you update it, you will overwrite the changes.' ), esc_html( $last_user_name ) );
</del><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( 'page' == $post-&gt;post_type ) {
</span><span class="lines">@@ -1094,11 +1094,6 @@
</span><span class="cx">                         $id = $post-&gt;ID;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if ( $do_lock &amp;&amp; empty( $_POST['auto_draft'] ) &amp;&amp; $id &amp;&amp; is_numeric( $id ) ) {
-                $lock_result = wp_set_post_lock( $id );
-                $supplemental['active-post-lock'] = implode( ':', $lock_result );
-        }
-
</del><span class="cx">         if ( $nonce_age == 2 ) {
</span><span class="cx">                 $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave');
</span><span class="cx">                 $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink');
</span><span class="lines">@@ -1777,7 +1772,7 @@
</span><span class="cx">         if ( $active_lock[1] != get_current_user_id() )
</span><span class="cx">                 wp_die( 0 );
</span><span class="cx"> 
</span><del>-        $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 ) + 5 ) . ':' . $active_lock[1];
</del><ins>+        $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', 120 ) + 5 ) . ':' . $active_lock[1];
</ins><span class="cx">         update_post_meta( $post_id, '_edit_lock', $new_lock, implode( ':', $active_lock ) );
</span><span class="cx">         wp_die( 1 );
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkwpadminincludesmiscphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/misc.php (23660 => 23661)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/misc.php        2013-03-12 01:50:46 UTC (rev 23660)
+++ trunk/wp-admin/includes/misc.php        2013-03-12 03:22:30 UTC (rev 23661)
</span><span class="lines">@@ -586,3 +586,44 @@
</span><span class="cx">         return $response;
</span><span class="cx"> }
</span><span class="cx"> add_filter( 'heartbeat_received', 'wp_check_locked_posts', 10, 2 );
</span><ins>+
+/**
+ * Check lock status on the New/Edit Post screen and refresh the lock
+ *
+ * @since 3.6
+ */
+function wp_refresh_post_lock( $response, $data, $screen_id ) {
+        if ( 'post' == $screen_id &amp;&amp; array_key_exists( 'wp-refresh-post-lock', $data ) ) {
+                $received = $data['wp-refresh-post-lock'];
+                $send = array();
+
+                if ( !$post_id = absint( $received['post_id'] ) )
+                        return $response;
+
+                if ( !current_user_can('edit_post', $post_id) )
+                        return $response;
+
+                if ( $user_id = wp_check_post_lock( $post_id ) ) {
+                        $user = get_userdata( $user_id );
+
+                        $error = array(
+                                'text' =&gt; sprintf( __( '%s has taken over and is currently editing.' ), $user-&gt;display_name )
+                        );
+                        
+                        if ( $avatar = get_avatar( $user-&gt;ID, 64 ) ) {
+                                if ( preg_match( &quot;|src='([^']+)'|&quot;, $avatar, $matches ) )
+                                        $error['avatar_src'] = $matches[1];
+                        }
+
+                        $send['lock_error'] = $error;
+                } else {
+                        if ( $new_lock = wp_set_post_lock( $post_id ) )
+                                $send['new_lock'] = implode( ':', $new_lock );
+                }
+
+                $response['wp-refresh-post-lock'] = $send;
+        }
+
+        return $response;
+}
+add_filter( 'heartbeat_received', 'wp_refresh_post_lock', 10, 3 );
</ins></span></pre></div>
<a id="trunkwpadminincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/post.php (23660 => 23661)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/post.php        2013-03-12 01:50:46 UTC (rev 23660)
+++ trunk/wp-admin/includes/post.php        2013-03-12 03:22:30 UTC (rev 23661)
</span><span class="lines">@@ -1162,7 +1162,7 @@
</span><span class="cx">         $time = $lock[0];
</span><span class="cx">         $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post-&gt;ID, '_edit_last', true );
</span><span class="cx"> 
</span><del>-        $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
</del><ins>+        $time_window = apply_filters( 'wp_check_post_lock_window', 120 );
</ins><span class="cx"> 
</span><span class="cx">         if ( $time &amp;&amp; $time &gt; time() - $time_window &amp;&amp; $user != get_current_user_id() )
</span><span class="cx">                 return $user;
</span><span class="lines">@@ -1192,31 +1192,61 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><del>- * Outputs the notice message to say that someone else is editing this post at the moment.
</del><ins>+ * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
</ins><span class="cx">  *
</span><span class="cx">  * @since 2.8.5
</span><span class="cx">  * @return none
</span><span class="cx">  */
</span><span class="cx"> function _admin_notice_post_locked() {
</span><del>-        $post = get_post();
-        $lock = explode( ':', get_post_meta( $post-&gt;ID, '_edit_lock', true ) );
-        $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post-&gt;ID, '_edit_last', true );
-        $last_user = get_userdata( $user );
-        $last_user_name = $last_user ? $last_user-&gt;display_name : __('Somebody');
</del><ins>+        global $post_ID;
</ins><span class="cx"> 
</span><del>-        switch ($post-&gt;post_type) {
-                case 'post':
-                        $message = __( 'Warning: %s is currently editing this post' );
-                        break;
-                case 'page':
-                        $message = __( 'Warning: %s is currently editing this page' );
-                        break;
-                default:
-                        $message = __( 'Warning: %s is currently editing this.' );
</del><ins>+        if ( !empty( $post_ID ) &amp;&amp; ( $user = wp_check_post_lock( $post_ID ) ) ) {
+                $user = get_userdata( $user );
+                $locked = apply_filters( 'show_post_locked_dialog', true, $post_ID, $user );
+        } else {
+                $locked = false;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        $message = sprintf( $message, esc_html( $last_user_name ) );
-        echo &quot;&lt;div class='error'&gt;&lt;p&gt;$message&lt;/p&gt;&lt;/div&gt;&quot;;
</del><ins>+        ?&gt;
+        &lt;div id=&quot;notification-dialog-wrap&quot;&lt;?php if ( ! $locked ) echo ' style=&quot;display:none&quot;'; ?&gt;&gt;
+        &lt;div id=&quot;notification-dialog-background&quot;&gt;&lt;/div&gt;
+        &lt;div id=&quot;notification-dialog&quot;&gt;
+        &lt;?php
+
+        if ( $locked ) {
+                ?&gt;
+                &lt;div class=&quot;post-locked-message&quot;&gt;
+                &lt;div class=&quot;post-locked-avatar&quot;&gt;&lt;?php echo get_avatar( $user-&gt;ID, 64 ); ?&gt;&lt;/div&gt;
+                &lt;p&gt;&lt;?php esc_html_e( sprintf( __( 'This content is currently locked. If you take over, %s will be blocked from continuing to edit.' ), $user-&gt;display_name ) ); ?&gt;&lt;/p&gt;
+                &lt;p&gt;
+                &lt;a class=&quot;button&quot; href=&quot;&lt;?php echo esc_url( wp_get_referer() ); ?&gt;&quot;&gt;&lt;?php _e('Go back'); ?&gt;&lt;/a&gt;
+                &lt;?php
+
+                // Allow plugins to prevent some users taking over
+                if ( apply_filters( 'post_lock_take_over', true, $post_ID, $user ) ) {
+                        ?&gt;
+                        &lt;a class=&quot;button button-primary&quot; href=&quot;&lt;?php echo esc_url( add_query_arg( 'get-post-lock', '1', get_edit_post_link( $post_ID, 'url' ) ) ); ?&gt;&quot;&gt;&lt;?php _e('Take over'); ?&gt;&lt;/a&gt;
+                        &lt;?php
+                }
+
+                ?&gt;
+                &lt;/p&gt;
+                &lt;/div&gt;
+                &lt;?php
+        } else {
+                ?&gt;
+                &lt;div class=&quot;post-taken-over&quot;&gt;
+                        &lt;div class=&quot;post-locked-avatar&quot;&gt;&lt;/div&gt;
+                        &lt;p class=&quot;currently-editing&quot;&gt;&lt;/p&gt;
+                        &lt;p&gt;&lt;a class=&quot;button button-primary&quot; href=&quot;&lt;?php echo esc_url( admin_url('edit.php') ); ?&gt;&quot;&gt;&lt;?php _e('Go to All Posts'); ?&gt;&lt;/a&gt;&lt;/p&gt;
+                &lt;/div&gt;
+                &lt;?php
+        }
+
+        ?&gt;
+        &lt;/div&gt;
+        &lt;/div&gt;
+        &lt;?php
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkwpadminjspostjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/post.js (23660 => 23661)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/post.js        2013-03-12 01:50:46 UTC (rev 23660)
+++ trunk/wp-admin/js/post.js        2013-03-12 03:22:30 UTC (rev 23661)
</span><span class="lines">@@ -251,8 +251,50 @@
</span><span class="cx">         );
</span><span class="cx"> };
</span><span class="cx"> 
</span><del>-})(jQuery);
</del><ins>+$(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
+        var lock = $('#active_post_lock').val(), post_id = $('#post_ID').val(), send = {};
</ins><span class="cx"> 
</span><ins>+        if ( !post_id )
+                return;
+
+        send['post_id'] = post_id;
+
+        if ( lock )
+                send['lock'] = lock;
+
+        data['wp-refresh-post-lock'] = send;
+});
+
+$(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
+        var received, wrap, avatar;
+
+        if ( data['wp-refresh-post-lock'] ) {
+                received = data['wp-refresh-post-lock'];
+
+                if ( received.lock_error ) {
+                        // show &quot;editing taken over&quot; message
+                        wrap = $('#notification-dialog-wrap');
+
+                        if ( ! wrap.is(':visible') ) {
+                                autosave();
+                                wrap.find('p.currently-editing').text( received.lock_error.text );
+                                
+                                if ( received.lock_error.avatar_src &amp;&amp; /^https?:\/\/[a-z0-9]+?\.gravatar\.com\/avatar/.test( received.lock_error.avatar_src ) ) {
+                                        avatar = $('&lt;img class=&quot;avatar avatar-64 photo&quot; width=&quot;64&quot; height=&quot;64&quot; /&gt;').attr( 'src', received.lock_error.avatar_src.replace(/&amp;amp;/g, '&amp;') );
+                                        wrap.find('div.post-locked-avatar').empty().append( avatar );
+                                }
+
+                                wrap.show();
+                        }
+                }
+
+                if ( received['new_lock'] )
+                        $('#active_post_lock').val( received['new_lock'].replace(/[^0-9:]+/, '') );
+        }
+});
+
+}(jQuery));
+
</ins><span class="cx"> jQuery(document).ready( function($) {
</span><span class="cx">         var stamp, visibility, sticky = '', last = 0, co = $('#content');
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpadminpostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/post.php (23660 => 23661)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/post.php        2013-03-12 01:50:46 UTC (rev 23660)
+++ trunk/wp-admin/post.php        2013-03-12 03:22:30 UTC (rev 23661)
</span><span class="lines">@@ -147,6 +147,12 @@
</span><span class="cx">         if ( 'trash' == $post-&gt;post_status )
</span><span class="cx">                 wp_die( __('You can&amp;#8217;t edit this item because it is in the Trash. Please restore it and try again.') );
</span><span class="cx"> 
</span><ins>+        if ( !empty( $_GET['get-post-lock'] ) ) {
+                wp_set_post_lock( $post_id );
+                wp_redirect( get_edit_post_link( $post_id, 'url' ) );
+                exit();
+        }
+
</ins><span class="cx">         $post_type = $post-&gt;post_type;
</span><span class="cx">         if ( 'post' == $post_type ) {
</span><span class="cx">                 $parent_file = &quot;edit.php&quot;;
</span><span class="lines">@@ -165,15 +171,15 @@
</span><span class="cx">                 $post_new_file = &quot;post-new.php?post_type=$post_type&quot;;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if ( $last = wp_check_post_lock( $post-&gt;ID ) ) {
-                add_action('admin_notices', '_admin_notice_post_locked' );
-        } else {
</del><ins>+        if ( ! wp_check_post_lock( $post-&gt;ID ) ) {
</ins><span class="cx">                 $active_post_lock = wp_set_post_lock( $post-&gt;ID );
</span><span class="cx"> 
</span><span class="cx">                 if ( 'attachment' !== $post_type )
</span><span class="cx">                         wp_enqueue_script('autosave');
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        add_action( 'admin_footer', '_admin_notice_post_locked' );
+
</ins><span class="cx">         $title = $post_type_object-&gt;labels-&gt;edit_item;
</span><span class="cx">         $post = get_post($post_id, OBJECT, 'edit');
</span><span class="cx"> 
</span><span class="lines">@@ -217,6 +223,11 @@
</span><span class="cx">         if ( !current_user_can($post_type_object-&gt;cap-&gt;delete_post, $post_id) )
</span><span class="cx">                 wp_die( __('You are not allowed to move this item to the Trash.') );
</span><span class="cx"> 
</span><ins>+        if ( $user_id = wp_check_post_lock( $post_id ) ) {
+                $user = get_userdata( $user_id );
+                wp_die( sprintf( __( 'You cannot move this item to the Trash. %s is currently editing.' ), $user-&gt;display_name ) );
+        }
+
</ins><span class="cx">         if ( ! wp_trash_post($post_id) )
</span><span class="cx">                 wp_die( __('Error in moving to Trash.') );
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>