-
Notifications
You must be signed in to change notification settings - Fork 6
/
Heartbeat.php
131 lines (113 loc) · 2.97 KB
/
Heartbeat.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
/**
* Contains the Heartbeat_Control\Heartbeat class.
*
* @package Heartbeat_Control
*/
namespace Heartbeat_Control;
/**
* Primary Hearbeat class.
*/
class Heartbeat {
/**
* The current screen being accessed.
*
* @var string
*/
public $current_screen;
/**
* The current query string being accessed.
*
* @var string
*/
public $current_query_string;
/**
* Stores heartbeat settings across class methods.
*
* @var array
*/
public $settings = array();
/**
* Constructor.
*/
public function __construct() {
$_query_string = filter_input( INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_URL );
$_request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL );
if ( $_query_string && $_request_uri ) {
$current_url = wp_unslash( $_query_string . '?' . $_request_uri );
} elseif ( $_query_string ) {
$current_url = wp_unslash( $_request_uri );
} else {
$current_url = admin_url();
}
$this->current_screen = wp_parse_url( $current_url );
if ( '/wp-admin/admin-ajax.php' === $this->current_screen ) {
return;
}
$settings = get_option( 'heartbeat_control_settings' );
if ( false === $settings ) {
return;
}
$this->settings = $settings;
add_action( 'admin_enqueue_scripts', array( $this, 'maybe_disable' ), 99 );
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_disable' ), 99 );
add_filter( 'heartbeat_settings', array( $this, 'maybe_modify' ), 99, 1 );
}
/**
* Checks if the current location has a rule.
*
* @param array $location Locations that have rules.
* @return bool
*/
public function check_location( $location ) {
$location_test = array(
'rules_dash' => function() {
return is_admin();
},
'rules_front' => function() {
return ! is_admin();
},
'rules_editor' => function() {
return ( '/wp-admin/post.php' === $this->current_screen['path'] );
},
);
if ( isset( $location_test[ $location ] ) ) {
return $location_test[ $location ]();
}
return false;
}
/**
* Disable the heartbeat, if needed.
*
* @return void
*/
public function maybe_disable() {
foreach ( $this->settings as $location => $r ) {
$rule = reset( $r );
if ( array_key_exists( 'heartbeat_control_behavior', $rule ) && 'disable' === $rule['heartbeat_control_behavior'] ) {
if ( $this->check_location( $location ) ) {
wp_deregister_script( 'heartbeat' );
return;
}
}
}
}
/**
* Modify the heartbeat, if needed.
*
* @param array $settings The settings.
* @return array $settings Maybe an updated settings.
*/
public function maybe_modify( $settings ) {
foreach ( $this->settings as $location => $r ) {
$rule = reset( $r );
if ( array_key_exists( 'heartbeat_control_behavior', $rule ) && 'modify' === $rule['heartbeat_control_behavior'] ) {
if ( $this->check_location( $location ) ) {
$settings['interval'] = intval( $rule['heartbeat_control_frequency'] );
return $settings;
}
}
}
return $settings;
}
}