<!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>[23355] trunk: Heartbeat API: first run, see #23216</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/23355">23355</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2013-01-29 06:15:25 +0000 (Tue, 29 Jan 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Heartbeat API: first run, see <a href="http://core.trac.wordpress.org/ticket/23216">#23216</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminadminajaxphp">trunk/wp-admin/admin-ajax.php</a></li>
<li><a href="#trunkwpadminincludesajaxactionsphp">trunk/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunkwpincludesdefaultfiltersphp">trunk/wp-includes/default-filters.php</a></li>
<li><a href="#trunkwpincludesgeneraltemplatephp">trunk/wp-includes/general-template.php</a></li>
<li><a href="#trunkwpincludesscriptloaderphp">trunk/wp-includes/script-loader.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkwpincludesjsheartbeatjs">trunk/wp-includes/js/heartbeat.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminadminajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/admin-ajax.php (23354 => 23355)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/admin-ajax.php        2013-01-28 22:36:15 UTC (rev 23354)
+++ trunk/wp-admin/admin-ajax.php        2013-01-29 06:15:25 UTC (rev 23355)
</span><span class="lines">@@ -56,7 +56,7 @@
</span><span class="cx">         'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
</span><span class="cx">         'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
</span><span class="cx">         'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
</span><del>-        'send-attachment-to-editor', 'save-attachment-order',
</del><ins>+        'send-attachment-to-editor', 'save-attachment-order', 'heartbeat',
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> // Register core Ajax calls.
</span></span></pre></div>
<a id="trunkwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/ajax-actions.php (23354 => 23355)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/ajax-actions.php        2013-01-28 22:36:15 UTC (rev 23354)
+++ trunk/wp-admin/includes/ajax-actions.php        2013-01-29 06:15:25 UTC (rev 23355)
</span><span class="lines">@@ -2071,3 +2071,32 @@
</span><span class="cx"> 
</span><span class="cx">         wp_send_json_success( $html );
</span><span class="cx"> }
</span><ins>+
+function wp_ajax_heartbeat() {
+        check_ajax_referer( 'heartbeat-nonce', '_nonce' );
+        $response = array( 'pagenow' =&gt; '' );
+
+        if ( ! empty($_POST['pagenow']) )
+                $response['pagenow'] = sanitize_key($_POST['pagenow']);
+        
+        if ( ! empty($_POST['data']) ) {
+                $data = (array) $_POST['data'];
+                // todo: how much to sanitize and preset and what to leave to be accessed from $data or $_POST..?
+                $user = wp_get_current_user();
+                $data['user_id'] = $user-&gt;exists() ? $user-&gt;ID : 0;
+
+                // todo: separate filters: 'heartbeat_[action]' so we call different callbacks only when there is data for them,
+                // or all callbacks listen to one filter and run when there is something for them in $data?
+                $response = apply_filters( 'heartbeat_received', $response, $data );
+        }
+
+        $response = apply_filters( 'heartbeat_send', $response );
+
+        // Allow the transport to be replaced with long-polling easily
+        do_action( 'heartbeat_tick', $response );
+
+        // always send the current time acording to the server
+        $response['time'] = time();
+
+        wp_send_json($response);
+}
</ins></span></pre></div>
<a id="trunkwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/default-filters.php (23354 => 23355)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/default-filters.php        2013-01-28 22:36:15 UTC (rev 23354)
+++ trunk/wp-includes/default-filters.php        2013-01-29 06:15:25 UTC (rev 23355)
</span><span class="lines">@@ -290,4 +290,7 @@
</span><span class="cx"> // This option no longer exists; tell plugins we always support auto-embedding.
</span><span class="cx"> add_filter( 'default_option_embed_autourls', '__return_true' );
</span><span class="cx"> 
</span><ins>+// Default settings for heartbeat
+add_filter( 'heartbeat_settings', 'wp_heartbeat_settings' );
+
</ins><span class="cx"> unset($filter, $action);
</span></span></pre></div>
<a id="trunkwpincludesgeneraltemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/general-template.php (23354 => 23355)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/general-template.php        2013-01-28 22:36:15 UTC (rev 23354)
+++ trunk/wp-includes/general-template.php        2013-01-29 06:15:25 UTC (rev 23355)
</span><span class="lines">@@ -2284,3 +2284,18 @@
</span><span class="cx"> 
</span><span class="cx">         return $result;
</span><span class="cx"> }
</span><ins>+
+/**
+ * Default settings for heartbeat
+ *
+ * Outputs the nonce used in the heartbeat XHR
+ *
+ * @since 3.6.0
+ *
+ * @param array $settings
+ * @return array $settings
+ */
+function wp_heartbeat_settings( $settings ) {
+        $setting['nonce'] = wp_create_nonce( 'heartbeat-nonce' );
+        return $setting;
+}
</ins></span></pre></div>
<a id="trunkwpincludesjsheartbeatjs"></a>
<div class="addfile"><h4>Added: trunk/wp-includes/js/heartbeat.js (0 => 23355)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/heartbeat.js                                (rev 0)
+++ trunk/wp-includes/js/heartbeat.js        2013-01-29 06:15:25 UTC (rev 23355)
</span><span class="lines">@@ -0,0 +1,178 @@
</span><ins>+/**
+ * Heartbeat API
+ */
+
+ // Ensure the global `wp` object exists.
+window.wp = window.wp || {};
+
+(function($){
+        var Heartbeat = function() {
+                var self = this,
+                        running,
+                        timeout,
+                        nonce,
+                        screen = typeof pagenow != 'undefined' ? pagenow : '',
+                        settings,
+                        tick = 0,
+                        queue = {},
+                        interval,
+                        lastconnect = 0;
+
+                this.url = typeof ajaxurl != 'undefined' ? ajaxurl : 'wp-admin/admin-ajax.php';
+                this.autostart = true;
+
+                if ( typeof( window.heartbeatSettings != 'undefined' ) ) {
+                        settings = $.extend( {}, window.heartbeatSettings );
+                        delete window.heartbeatSettings;
+
+                        // Add private vars
+                        nonce = settings.nonce || '';
+                        delete settings.nonce;
+
+                        interval = settings.interval || 15000; // default interval
+                        delete settings.interval;
+                        
+                        // todo: needed?
+                        // 'pagenow' can be added from settings if not already defined
+                        screen = screen || settings.pagenow;
+                        delete settings.pagenow;
+
+                        // Add public vars
+                        $.extend( this, settings );
+                }
+
+                function time(s) {
+                        if ( s )
+                                return parseInt( (new Date()).getTime() / 1000 );
+
+                        return (new Date()).getTime();
+                }
+
+                // Set error state and fire an event if it persists for over 3 min
+                function errorstate() {
+                        var since;
+
+                        if ( lastconnect ) {
+                                since = time() - lastconnect;
+
+                                if ( since &gt; 180000 ) {
+                                        self.connectionLost = true;
+                                        $(document).trigger( 'heartbeat-connection-lost', parseInt(since / 1000) );
+                                } else if ( self.connectionLost ) {
+                                        self.connectionLost = false;
+                                        $(document).trigger( 'heartbeat-connection-restored' );
+                                }
+                        }
+                }
+
+                function connect() {
+                        var data = {};
+                        tick = time();
+
+                        data.data = $.extend( {}, queue );
+                        queue = {};
+
+                        data.interval = interval / 1000;
+                        data._nonce = nonce;
+                        data.action = 'heartbeat';
+                        data.pagenow = screen;
+
+                        self.xhr = $.post( self.url, data, function(r){
+                                lastconnect = time();
+                                // Clear error state
+                                if ( self.connectionLost )
+                                        errorstate();
+                                
+                                self.tick(r);
+                        }, 'json' ).always( function(){
+                                next();
+                        }).fail( function(r){
+                                errorstate();
+                                self.error(r);
+                        });
+                };
+
+                function next() {
+                        var delta = time() - tick;
+
+                        if ( !running )
+                                return;
+
+                        if ( delta &lt; interval ) {
+                                timeout = window.setTimeout(
+                                        function(){
+                                                if ( running )
+                                                        connect();
+                                        },
+                                        interval - delta
+                                );
+                        } else {
+                                window.clearTimeout(timeout); // this has already expired?
+                                connect();
+                        }
+                };
+
+                this.interval = function(seconds) {
+                        if ( seconds ) {
+                                // Limit
+                                if ( 5 &gt; seconds || seconds &gt; 60 )
+                                        return false;
+
+                                interval = seconds * 1000;
+                        } else if ( seconds === 0 ) {
+                                // Allow long polling to be turned on
+                                interval = 0;
+                        }
+                        return interval / 1000;
+                };
+
+                this.start = function() {
+                        // start only once
+                        if ( running )
+                                return false;
+
+                        running = true;
+                        connect();
+
+                        return true;
+                };
+
+                this.stop = function() {
+                        if ( !running )
+                                return false;
+
+                        if ( self.xhr )
+                                self.xhr.abort();
+
+                        running = false;
+                        return true;
+                }
+
+                this.send = function(action, data) {
+                        if ( action )
+                                queue[action] = data;
+                }
+
+                if ( this.autostart ) {
+                        $(document).ready( function(){
+                                // Start one tick (15 sec) after DOM ready
+                                running = true;
+                                tick = time();
+                                next();
+                        });
+                }
+                        
+        }
+
+        $.extend( Heartbeat.prototype, {
+                tick: function(r) {
+                        $(document).trigger( 'heartbeat-tick', r );
+                },
+                error: function(r) {
+                        $(document).trigger( 'heartbeat-error', r );
+                }
+        });
+
+        wp.heartbeat = new Heartbeat();
+
+}(jQuery));
</ins><span class="cx">Property changes on: trunk/wp-includes/js/heartbeat.js
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4>Added: svn:eol-style</h4></div>
<a id="trunkwpincludesscriptloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/script-loader.php (23354 => 23355)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/script-loader.php        2013-01-28 22:36:15 UTC (rev 23354)
+++ trunk/wp-includes/script-loader.php        2013-01-29 06:15:25 UTC (rev 23355)
</span><span class="lines">@@ -107,6 +107,11 @@
</span><span class="cx">         ) );
</span><span class="cx"> 
</span><span class="cx">         $scripts-&gt;add( 'autosave', &quot;/wp-includes/js/autosave$suffix.js&quot;, array('schedule', 'wp-ajax-response'), false, 1 );
</span><ins>+        
+        $scripts-&gt;add( 'heartbeat', &quot;/wp-includes/js/heartbeat$suffix.js&quot;, array('jquery'), false, 1 );
+        did_action( 'init' ) &amp;&amp; $scripts-&gt;localize( 'heartbeat', 'heartbeatSettings',
+                apply_filters( 'heartbeat_settings', array() )
+        );
</ins><span class="cx"> 
</span><span class="cx">         $scripts-&gt;add( 'wp-lists', &quot;/wp-includes/js/wp-lists$suffix.js&quot;, array( 'wp-ajax-response', 'jquery-color' ), false, 1 );
</span><span class="cx"> 
</span><span class="lines">@@ -371,7 +376,7 @@
</span><span class="cx"> 
</span><span class="cx">                 $scripts-&gt;add( 'postbox', &quot;/wp-admin/js/postbox$suffix.js&quot;, array('jquery-ui-sortable'), false, 1 );
</span><span class="cx"> 
</span><del>-                $scripts-&gt;add( 'post', &quot;/wp-admin/js/post$suffix.js&quot;, array('suggest', 'wp-lists', 'postbox'), false, 1 );
</del><ins>+                $scripts-&gt;add( 'post', &quot;/wp-admin/js/post$suffix.js&quot;, array('suggest', 'wp-lists', 'postbox', 'heartbeat'), false, 1 );
</ins><span class="cx">                 did_action( 'init' ) &amp;&amp; $scripts-&gt;localize( 'post', 'postL10n', array(
</span><span class="cx">                         'ok' =&gt; __('OK'),
</span><span class="cx">                         'cancel' =&gt; __('Cancel'),
</span></span></pre>
</div>
</div>

</body>
</html>