-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version 1.0.0 of Pulse ready to rock!
- Loading branch information
Showing
5 changed files
with
232 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
/*-----------------------------------------------------------------------------------*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|