-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathheartbeat-control.php
186 lines (156 loc) · 5.47 KB
/
heartbeat-control.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Plugin Name: Heartbeat Control by WP Rocket
* Plugin URI: https://wordpress.org/plugins/heartbeat-control/
* Description: Completely controls the WordPress heartbeat.
* Version: 2.0.1
* Author: WP Rocket
* Author URI: https://wp-rocket.me
* License: GPL2
* Text Domain: heartbeat-control
*
* @package Heartbeat_Control
*/
namespace Heartbeat_Control;
define( 'HBC_VERSION', '2.0.1' );
define( 'HBC_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'HBC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
/**
* The primary Heartbeat Control class.
*/
class Heartbeat_Control {
/**
* The current version.
*
* @var string
*/
public $version = HBC_VERSION;
/**
* Constructor.
*/
public function __construct() {
$this->register_dependencies();
$this->maybe_upgrade();
new Heartbeat();
}
/**
* Register additional plugin dependencies.
*
* @return void
*/
public function register_dependencies() {
// Initialize CMB2 for the new settings page.
require_once dirname( __FILE__ ) . '/vendor/cmb2/cmb2/init.php';
add_action( 'cmb2_admin_init', array( new Settings(), 'init_metaboxes' ) );
}
/**
* Check the version and update as needed.
*
* @return void
*/
public function maybe_upgrade() {
$db_version = get_option( 'heartbeat_control_version', '1.0' );
if ( version_compare( $db_version, $this->version, '<' ) ) {
$this->upgrade_db( $db_version );
}
}
/**
* Upgrades the database from older versions.
*
* @param string $version The current DB version.
* @return void
*/
public function upgrade_db( $version ) {
if ( version_compare( $version, '1.1', '<' ) ) {
$updated_options = [];
$old_location = get_option( 'heartbeat_location', '' );
if ( 'disable-heartbeat-everywhere' === $old_location ) {
$updated_options['heartbeat_control_behavior'] = 'disable';
$updated_options['heartbeat_control_location'] = array( 'frontend', 'admin', '/wp-admin/post.php' );
} elseif ( 'disable-heartbeat-dashboard' === $old_location ) {
$updated_options['heartbeat_control_behavior'] = 'disable';
$updated_options['heartbeat_control_location'] = array( 'admin' );
} elseif ( 'allow-heartbeat-post-edit' === $old_location ) {
$updated_options['heartbeat_control_behavior'] = 'allow';
$updated_options['heartbeat_control_location'] = array( '/wp-admin/post.php' );
} else {
$old_frequency = get_option( 'heartbeat_frequency', '' );
$updated_options['heartbeat_control_behavior'] = 'modify';
$updated_options['heartbeat_control_location'] = [ 'frontend', 'admin', '/wp-admin/post.php' ];
$updated_options['heartbeat_control_frequency'] = $old_frequency;
}
update_option( 'heartbeat_control_settings', $updated_options );
}
$original_settings = get_option( 'heartbeat_control_settings', [] );
if ( version_compare( $version, '1.2', '<' ) && ! array_key_exists( 'rules', $original_settings ) ) {
update_option( 'heartbeat_control_settings', [ 'rules' => [ $original_settings ] ] );
}
/*
* In version 1.3.0 we remove the ordering and overwriting of rules,
* you can have only one behavior for each location now, it simpler and less misleading.
* So this code check for rules by location and take one for each based on there order.
*/
if ( version_compare( $version, '2.0', '<' ) ) {
$old_settings = get_option( 'heartbeat_control_settings', [] );
$new_mapping = [
[
'heartbeat_control_behavior' => 'allow',
'heartbeat_control_frequency' => 0,
],
];
$new_settings = [
'rules_dash' => $new_mapping,
'rules_front' => $new_mapping,
'rules_editor' => $new_mapping,
];
if ( ! isset( $old_settings['rules'] ) || empty( $old_settings['rules'] ) ) {
update_option( 'heartbeat_control_settings', $new_settings );
} else {
$value = [ false, false, false ];
foreach ( $old_settings['rules'] as $rules ) {
if ( ! isset( $rules['heartbeat_control_location'] ) ) {
continue;
}
foreach ( $rules['heartbeat_control_location'] as $location ) {
if ( 'frontend' === $location && false === $value[0] ) {
$new_settings['rules_front'] = [
[
'heartbeat_control_behavior' => $rules['heartbeat_control_behavior'],
'heartbeat_control_frequency' => $rules['heartbeat_control_frequency'],
],
];
$value[0] = true;
}
if ( 'admin' === $location && false === $value[1] ) {
$new_settings['rules_dash'] = [
[
'heartbeat_control_behavior' => $rules['heartbeat_control_behavior'],
'heartbeat_control_frequency' => $rules['heartbeat_control_frequency'],
],
];
$value[1] = true;
}
if ( '/wp-admin/post.php' === $location && false === $value[2] ) {
$new_settings['rules_editor'] = [
[
'heartbeat_control_behavior' => $rules['heartbeat_control_behavior'],
'heartbeat_control_frequency' => $rules['heartbeat_control_frequency'],
],
];
$value[2] = true;
}
if ( ! in_array( false, $value ) ) { // phpcs:ignore WordPress.PHP.StrictInArray
break 2;
}
}
}
}
update_option( 'heartbeat_control_settings', $new_settings );
}
update_option( 'heartbeat_control_version', $this->version );
$notices = Notices::get_instance();
$notices->append( 'success', __( 'Heartbeat Control data have been migrated successfully!', 'heartbeat-control' ) );
}
}
new Heartbeat_Control();