Skip to content

Commit

Permalink
Version 1.0.0 of Pulse ready to rock!
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffikus committed Oct 23, 2013
1 parent f3c8b1f commit 8cd76a4
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Pulse by [Jeffikus](http://jeffikus.com/ "Jeffikus") is an example plugin for th

The plugin basically does the following:

* Outputs console data for a logged in user - the object will state that the user is logged in
* Outputs console data for a non logged in user - the object will state that the user is not logged in
* Outputs console data for a logged in user - the object will state that the user is logged in - [Console Screenshot](http://d.pr/i/22H6 "Console Screenshot")
* Outputs console data for a non logged in user - the object will state that the user is not logged in - [Console Screenshot](http://d.pr/i/cCCs "Console Screenshot")

# Why on Github and not WordPress.org

Expand All @@ -31,8 +31,8 @@ Credit must go to the following sites and people for making resources available:

* Remi Corson - a colleague of mine at [WooThemes](http://woothemes.com/ "WooThemes") gave me my initial run through of Heartbeat at this years WordCamp Europe in between talks - www.remicorson.com
* Pippin wrote a great starter tutorial here - http://pippinsplugins.com/using-the-wordpress-heartbeat-api/
* WP Tuts also wrote a great 3 part series - http://wp.tutsplus.com/tutorials/creative-coding/the-heartbeat-api-getting-started/
* The WordPress codex of course - http://codex.wordpress.org/Function_Reference/wp_heartbeat_settings
* WP Tuts Plus also wrote a great 3 part series - http://wp.tutsplus.com/tutorials/creative-coding/the-heartbeat-api-getting-started/
* The WordPress Codex of course - http://codex.wordpress.org/Function_Reference/wp_heartbeat_settings
* wp-heartbeat-notify is a great example plugin - https://github.com/micc83/wp-heartbeat-notify
* Jason Coleman also wrote a great tutorial - http://www.strangerstudios.com/blog/2013/08/heartbeat-api-for-wordpress/

Expand Down
12 changes: 12 additions & 0 deletions assets/css/frontend.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Plugin Name: Pulse - Heartbeat API Example
Plugin URI: https://github.com/jeffikus/pulse
Description: Pulse by Jeffikus is an example plugin for the Heartbeat API!
Version: 1.0.0
Author: Jeffikus
Author URI: http://jeffikus.com/
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/*-----------------------------------------------------------------------------------*/
/* Frontend Stylesheet */
/*-----------------------------------------------------------------------------------*/
51 changes: 51 additions & 0 deletions assets/js/pulse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
jQuery(document).ready( function($) {

// Initial pulse data
var pulse = { debug: true };

// Change default beat tick period
wp.heartbeat.interval( 'fast' ); // slow (1 beat every 60 seconds), standard (1 beat every 15 seconds), fast (1 beat every 5 seconds)

// Initiate namespace with pulse data
wp.heartbeat.enqueue( 'pulse', pulse, false );

// Hook into the heartbeat-send
jQuery(document).on('heartbeat-send.pulse', function(e, data) {

// Send data to Heartbeat
if ( data.hasOwnProperty( 'pulse' ) ) {

if ( data.pulse.debug === 'true' ) {

console.log( 'Data Sent: ' );
console.log(data);
console.log( '------------------' );

} // End If Statement

} // End If Statement

});

// Listen for the custom event "heartbeat-tick" on $(document).
jQuery(document).on( 'heartbeat-tick.pulse', function(e, data) {

// Receive Data back from Heartbeat
if ( data.hasOwnProperty( 'pulse' ) ) {

if ( data.pulse.debug === 'true' ) {

console.log( 'Data Received: ' );
console.log(data);
console.log( '------------------' );

} // End If Statement

} // End If Statement

// Pass data back into namespace
wp.heartbeat.enqueue( 'pulse', data.pulse, false );

});

});
132 changes: 132 additions & 0 deletions heartbeat-api-pulse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
* Heartbeat API Pulse Class
*
* All functionality pertaining to the Pulse Heartbeat API.
*
* @package WordPress
* @subpackage Heartbeat API
* @category Frontend
* @author Jeffikus
* @since 1.0.0
*
* TABLE OF CONTENTS
*
* - __construct()
* - init()
* - enqueue_scripts()
* - enqueue_styles()
* - heartbeat_settings()
* - respond_to_browser_unauthenticated
* - respond_to_browser_authenticated
*/
class Heartbeat_API_Pulse {
public $token;
public $plugin_url;
public $version;

/**
* Constructor.
* @since 1.0.0
* @return void
*/
public function __construct ( $file ) {

// Class variables
$this->token = 'heartbeat-api-pulse';
$this->plugin_url = trailingslashit( plugins_url( '', $file ) );

// Actions & filters
add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_styles' ) );
add_action( 'wp_footer', array( &$this, 'enqueue_scripts' ) );
add_filter( 'heartbeat_settings', array( &$this, 'heartbeat_settings' ) );

} // End __construct()

/**
* Initialise the code.
* @since 1.0.0
* @return void
*/
public function init () {

// Heartbeat filters
add_filter( 'heartbeat_received', array( &$this, 'respond_to_browser_authenticated' ), 5, 2 );
add_filter( 'heartbeat_nopriv_received', array( &$this, 'respond_to_browser_unauthenticated' ), 5, 2 );

} // End init()

/**
* Enqueue frontend JavaScripts.
* @since 1.0.0
* @return void
*/
public function enqueue_scripts () {

// Load the pulse javascript
wp_enqueue_script( $this->token . '-pulse' , $this->plugin_url . 'assets/js/pulse.js', array( 'jquery', 'heartbeat' ), '1.0.0', true );

} // End enqueue_scripts()

/**
* Enqueue frontend CSS files.
* @since 1.0.0
* @return void
*/
public function enqueue_styles () {

// Load the pulse frontend CSS
wp_register_style( $this->token . '-frontend', $this->plugin_url . 'assets/css/frontend.css', '', '1.0.0', 'screen' );
wp_enqueue_style( $this->token . '-frontend' );

} // End enqueue_styles()

/**
* Sets heartbeat tick interval.
* @since 1.0.0
* @return void
*/
public function heartbeat_settings( $settings ) {

$settings['interval'] = 15; //Anything between 15-60
// $settings['autostart'] = false;
return $settings;

} // End heartbeat_settings

/**
* Handle send data and respond to the browser.
* @since 1.0.0
* @return $response data to the heartbeat tick function
*/
public function respond_to_browser_unauthenticated( $response, $data ) {

// Do custom code here
$data['user'] = 'User is not logged in.';
$data['php'] = 'Sent from PHP.';
$response = $data;

return $response;

} // End respond_to_browser_unauthenticated()

/**
* Handle authenticated user (logged in) send data and respond to the browser.
* @since 1.0.0
* @return $response data to the heartbeat tick function
*/
public function respond_to_browser_authenticated( $response, $data ) {

// Do custom code here
$data['user'] = 'User is logged in.';
$data['php'] = 'Sent from PHP.';
$response = $data;

return $response;

} // End respond_to_browser_authenticated()

} // End Heartbeat_API_Pulse

33 changes: 33 additions & 0 deletions heartbeat-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
Plugin Name: Pulse - Heartbeat API Example
Plugin URI: https://github.com/jeffikus/pulse
Description: Pulse by Jeffikus is an example plugin for the Heartbeat API!
Version: 1.0.0
Author: Jeffikus
Author URI: http://jeffikus.com/
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/* Copyright 2013 Jeffikus (email : jeffikus@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

require_once( 'heartbeat-api-pulse.php' );
$heartbeat_api_pulse = new Heartbeat_API_Pulse( __FILE__ );
$heartbeat_api_pulse->version = '1.0.0';
$heartbeat_api_pulse->init();

0 comments on commit 8cd76a4

Please sign in to comment.