-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathheartbeat-api-pulse.php
132 lines (105 loc) · 3.35 KB
/
heartbeat-api-pulse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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