Version Description
- Fixed issue caused by previous version deployment.
- Added hbc_disable_notice hook to force dismissal of update notices.
- Additional documentation added.
- Minor standards adjustments.
Download this release
Release Info
Developer | JeffMatson |
Plugin | Heartbeat Control |
Version | 1.2.5 |
Comparing to | |
See all releases |
Code changes from version 1.2.4 to 1.2.5
- autoloader.php +23 -7
- heartbeat-control.php +49 -16
- heartbeat.php +59 -2
- readme.txt +7 -1
- settings.php +60 -28
autoloader.php
CHANGED
@@ -1,12 +1,28 @@
|
|
1 |
<?php
|
2 |
-
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
function heartbeat_control_autoload( $classname ) {
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
}
|
11 |
|
12 |
-
|
|
1 |
<?php
|
2 |
+
/**
|
3 |
+
* Contains the autoloader.
|
4 |
+
*
|
5 |
+
* @package Heartbeat_Control
|
6 |
+
*/
|
7 |
|
8 |
+
if ( ! defined( 'ABSPATH' ) ) {
|
9 |
+
exit;
|
10 |
+
}
|
11 |
+
|
12 |
+
/**
|
13 |
+
* Autoloads files based on class names.
|
14 |
+
*
|
15 |
+
* @param string $classname The class name.
|
16 |
+
*
|
17 |
+
* @return void
|
18 |
+
*/
|
19 |
function heartbeat_control_autoload( $classname ) {
|
20 |
+
$class = str_replace( '\\', DIRECTORY_SEPARATOR, str_replace( '_', '-', strtolower( $classname ) ) );
|
21 |
+
$file_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $class . '.php';
|
22 |
+
if ( file_exists( $file_path ) ) {
|
23 |
+
require_once $file_path;
|
24 |
+
}
|
25 |
}
|
26 |
|
27 |
+
// Register the autoloader.
|
28 |
+
spl_autoload_register( 'heartbeat_control_autoload' );
|
heartbeat-control.php
CHANGED
@@ -3,30 +3,32 @@
|
|
3 |
* Plugin Name: Heartbeat Control
|
4 |
* Plugin URI: https://jeffmatson.net/heartbeat-control
|
5 |
* Description: Completely controls the WordPress heartbeat.
|
6 |
-
* Version: 1.2.
|
7 |
* Author: Jeff Matson
|
8 |
* Author URI: http://jeffmatson.net
|
9 |
* License: GPL2
|
10 |
* Text Domain: heartbeat-control
|
11 |
* Domain Path: /languages
|
|
|
|
|
12 |
*/
|
13 |
|
14 |
namespace Heartbeat_Control;
|
15 |
|
16 |
/**
|
17 |
-
*
|
18 |
*/
|
19 |
class Heartbeat_Control {
|
20 |
|
21 |
/**
|
22 |
-
*
|
23 |
*
|
24 |
* @var string
|
25 |
*/
|
26 |
-
public $version = '1.2.
|
27 |
|
28 |
/**
|
29 |
-
*
|
30 |
*/
|
31 |
public function __construct() {
|
32 |
$this->maybe_upgrade();
|
@@ -37,6 +39,11 @@ class Heartbeat_Control {
|
|
37 |
new Heartbeat();
|
38 |
}
|
39 |
|
|
|
|
|
|
|
|
|
|
|
40 |
public function maybe_enqueue_scripts() {
|
41 |
if ( get_option( 'heartbeat_control_update_notice' ) ) {
|
42 |
wp_enqueue_script( 'heartbeat-control-notices', plugins_url( '/assets/js/bundle.js', __FILE__ ), array( 'jquery' ), '1.0.0', true );
|
@@ -44,14 +51,30 @@ class Heartbeat_Control {
|
|
44 |
}
|
45 |
}
|
46 |
|
|
|
|
|
|
|
|
|
|
|
47 |
public function register_dependencies() {
|
|
|
48 |
require_once dirname( __FILE__ ) . '/autoloader.php';
|
|
|
|
|
49 |
require_once dirname( __FILE__ ) . '/vendor/webdevstudios/cmb2/init.php';
|
50 |
add_action( 'cmb2_admin_init', array( new Settings(), 'init_metaboxes' ) );
|
51 |
}
|
52 |
|
|
|
|
|
|
|
|
|
|
|
53 |
public function maybe_upgrade() {
|
54 |
-
|
|
|
|
|
|
|
55 |
$db_version = get_option( 'heartbeat_control_version', '1.0' );
|
56 |
if ( version_compare( $db_version, $this->version, '<' ) ) {
|
57 |
$this->upgrade_db( $db_version );
|
@@ -59,9 +82,9 @@ class Heartbeat_Control {
|
|
59 |
}
|
60 |
|
61 |
/**
|
62 |
-
*
|
63 |
*
|
64 |
-
* @param
|
65 |
* @return void
|
66 |
*/
|
67 |
public function upgrade_db( $version ) {
|
@@ -80,9 +103,9 @@ class Heartbeat_Control {
|
|
80 |
$updated_options['heartbeat_control_behavior'] = 'allow';
|
81 |
$updated_options['heartbeat_control_location'] = array( '/wp-admin/post.php' );
|
82 |
} else {
|
83 |
-
if ( $old_frequency
|
84 |
-
$updated_options['heartbeat_control_behavior']
|
85 |
-
$updated_options['heartbeat_control_location']
|
86 |
$updated_options['heartbeat_control_frequency'] = $old_frequency;
|
87 |
}
|
88 |
}
|
@@ -100,21 +123,31 @@ class Heartbeat_Control {
|
|
100 |
update_option( 'heartbeat_control_update_notice', true );
|
101 |
}
|
102 |
|
|
|
|
|
|
|
|
|
|
|
103 |
public function heartbeat_control_updated() {
|
104 |
if ( get_option( 'heartbeat_control_update_notice' ) ) {
|
105 |
-
|
106 |
<div id="heartbeat_control_update_notice" class="notice notice-success is-dismissible">
|
107 |
-
<p><?php
|
108 |
-
<p><?php
|
109 |
</div>
|
110 |
-
|
111 |
}
|
112 |
}
|
113 |
|
|
|
|
|
|
|
|
|
|
|
114 |
public function dismiss_update_notice() {
|
115 |
delete_option( 'heartbeat_control_update_notice' );
|
116 |
}
|
117 |
|
118 |
}
|
119 |
|
120 |
-
new Heartbeat_Control;
|
3 |
* Plugin Name: Heartbeat Control
|
4 |
* Plugin URI: https://jeffmatson.net/heartbeat-control
|
5 |
* Description: Completely controls the WordPress heartbeat.
|
6 |
+
* Version: 1.2.5
|
7 |
* Author: Jeff Matson
|
8 |
* Author URI: http://jeffmatson.net
|
9 |
* License: GPL2
|
10 |
* Text Domain: heartbeat-control
|
11 |
* Domain Path: /languages
|
12 |
+
*
|
13 |
+
* @package Heartbeat_Control
|
14 |
*/
|
15 |
|
16 |
namespace Heartbeat_Control;
|
17 |
|
18 |
/**
|
19 |
+
* The primary Heartbeat Control class.
|
20 |
*/
|
21 |
class Heartbeat_Control {
|
22 |
|
23 |
/**
|
24 |
+
* The current version.
|
25 |
*
|
26 |
* @var string
|
27 |
*/
|
28 |
+
public $version = '1.2.5';
|
29 |
|
30 |
/**
|
31 |
+
* Heartbeat_Control Constructor.
|
32 |
*/
|
33 |
public function __construct() {
|
34 |
$this->maybe_upgrade();
|
39 |
new Heartbeat();
|
40 |
}
|
41 |
|
42 |
+
/**
|
43 |
+
* Enqueue additional scrips if needed.
|
44 |
+
*
|
45 |
+
* @return void
|
46 |
+
*/
|
47 |
public function maybe_enqueue_scripts() {
|
48 |
if ( get_option( 'heartbeat_control_update_notice' ) ) {
|
49 |
wp_enqueue_script( 'heartbeat-control-notices', plugins_url( '/assets/js/bundle.js', __FILE__ ), array( 'jquery' ), '1.0.0', true );
|
51 |
}
|
52 |
}
|
53 |
|
54 |
+
/**
|
55 |
+
* Register additional plugin dependencies.
|
56 |
+
*
|
57 |
+
* @return void
|
58 |
+
*/
|
59 |
public function register_dependencies() {
|
60 |
+
// Main plugin autoloader.
|
61 |
require_once dirname( __FILE__ ) . '/autoloader.php';
|
62 |
+
|
63 |
+
// Initialize CMB2 for settings pages.
|
64 |
require_once dirname( __FILE__ ) . '/vendor/webdevstudios/cmb2/init.php';
|
65 |
add_action( 'cmb2_admin_init', array( new Settings(), 'init_metaboxes' ) );
|
66 |
}
|
67 |
|
68 |
+
/**
|
69 |
+
* Check the version and update as needed.
|
70 |
+
*
|
71 |
+
* @return void
|
72 |
+
*/
|
73 |
public function maybe_upgrade() {
|
74 |
+
if ( ! apply_filters( 'hbc_disable_notice', false ) ) {
|
75 |
+
add_action( 'admin_notices', array( $this, 'heartbeat_control_updated' ) );
|
76 |
+
}
|
77 |
+
|
78 |
$db_version = get_option( 'heartbeat_control_version', '1.0' );
|
79 |
if ( version_compare( $db_version, $this->version, '<' ) ) {
|
80 |
$this->upgrade_db( $db_version );
|
82 |
}
|
83 |
|
84 |
/**
|
85 |
+
* Upgrades the database from older versions.
|
86 |
*
|
87 |
+
* @param string $version The current DB version.
|
88 |
* @return void
|
89 |
*/
|
90 |
public function upgrade_db( $version ) {
|
103 |
$updated_options['heartbeat_control_behavior'] = 'allow';
|
104 |
$updated_options['heartbeat_control_location'] = array( '/wp-admin/post.php' );
|
105 |
} else {
|
106 |
+
if ( $old_frequency === get_option( 'heartbeat_frequency' ) ) {
|
107 |
+
$updated_options['heartbeat_control_behavior'] = 'modify';
|
108 |
+
$updated_options['heartbeat_control_location'] = array( 'frontend', 'admin', '/wp-admin/post.php' );
|
109 |
$updated_options['heartbeat_control_frequency'] = $old_frequency;
|
110 |
}
|
111 |
}
|
123 |
update_option( 'heartbeat_control_update_notice', true );
|
124 |
}
|
125 |
|
126 |
+
/**
|
127 |
+
* Displays the update notice.
|
128 |
+
*
|
129 |
+
* @return void
|
130 |
+
*/
|
131 |
public function heartbeat_control_updated() {
|
132 |
if ( get_option( 'heartbeat_control_update_notice' ) ) {
|
133 |
+
?>
|
134 |
<div id="heartbeat_control_update_notice" class="notice notice-success is-dismissible">
|
135 |
+
<p><?php esc_html_e( 'Heartbeat Control has updated to a new version!', 'heartbeat-control' ); ?></p>
|
136 |
+
<p><?php esc_html_e( 'Love it? Does it save you money and valuable server resources? Consider <a href="https://paypal.me/JeffMatson">sending me a donation</a>. The plugin is entirely developed in my spare time and every little bit helps to motivate me to add more features and bug fixes.', 'heartbeat-control' ); ?></p>
|
137 |
</div>
|
138 |
+
<?php
|
139 |
}
|
140 |
}
|
141 |
|
142 |
+
/**
|
143 |
+
* Dismisses the update notice.
|
144 |
+
*
|
145 |
+
* @return void
|
146 |
+
*/
|
147 |
public function dismiss_update_notice() {
|
148 |
delete_option( 'heartbeat_control_update_notice' );
|
149 |
}
|
150 |
|
151 |
}
|
152 |
|
153 |
+
new Heartbeat_Control();
|
heartbeat.php
CHANGED
@@ -1,18 +1,46 @@
|
|
1 |
<?php
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
namespace Heartbeat_Control;
|
4 |
|
|
|
|
|
|
|
5 |
class Heartbeat {
|
6 |
|
|
|
|
|
|
|
|
|
|
|
7 |
public $current_screen;
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
public $current_query_string;
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
public $settings = array();
|
10 |
|
|
|
|
|
|
|
11 |
public function __construct() {
|
12 |
|
13 |
if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
|
14 |
$current_url = $_SERVER['REQUEST_URI'] . '?' . $_SERVER['QUERY_STRING'];
|
15 |
-
}
|
16 |
$current_url = $_SERVER['REQUEST_URI'];
|
17 |
}
|
18 |
|
@@ -36,6 +64,13 @@ class Heartbeat {
|
|
36 |
add_filter( 'heartbeat_settings', array( $this, 'maybe_modify' ), 99, 1 );
|
37 |
}
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
public function check_location( $locations ) {
|
40 |
if ( ! isset( $locations ) || ! is_array( $locations ) ) {
|
41 |
return false;
|
@@ -52,9 +87,19 @@ class Heartbeat {
|
|
52 |
return false;
|
53 |
}
|
54 |
|
|
|
|
|
|
|
|
|
|
|
55 |
public function maybe_disable() {
|
56 |
foreach ( $this->settings as $rule ) {
|
57 |
-
if ( array_key_exists( 'heartbeat_control_behavior', $rule ) && $rule['heartbeat_control_behavior']
|
|
|
|
|
|
|
|
|
|
|
58 |
if ( $this->check_location( $rule['heartbeat_control_location'] ) ) {
|
59 |
wp_deregister_script( 'heartbeat' );
|
60 |
return;
|
@@ -64,10 +109,22 @@ class Heartbeat {
|
|
64 |
|
65 |
}
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
public function maybe_modify( $settings ) {
|
68 |
|
69 |
foreach ( $this->settings as $rule ) {
|
70 |
if ( array_key_exists( 'heartbeat_control_behavior', $rule ) && $rule['heartbeat_control_behavior'] === 'modify' ) {
|
|
|
|
|
|
|
|
|
|
|
71 |
if ( $this->check_location( $rule['heartbeat_control_location'] ) ) {
|
72 |
$settings['interval'] = intval( $rule['heartbeat_control_frequency'] );
|
73 |
return $settings;
|
1 |
<?php
|
2 |
+
/**
|
3 |
+
* Contains the Heartbeat_Control\Heartbeat class.
|
4 |
+
*
|
5 |
+
* @package Heartbeat_Control
|
6 |
+
*/
|
7 |
|
8 |
namespace Heartbeat_Control;
|
9 |
|
10 |
+
/**
|
11 |
+
* Primary Hearbeat class.
|
12 |
+
*/
|
13 |
class Heartbeat {
|
14 |
|
15 |
+
/**
|
16 |
+
* The current screen being accessed.
|
17 |
+
*
|
18 |
+
* @var string
|
19 |
+
*/
|
20 |
public $current_screen;
|
21 |
+
|
22 |
+
/**
|
23 |
+
* The current query string being accessed.
|
24 |
+
*
|
25 |
+
* @var string
|
26 |
+
*/
|
27 |
public $current_query_string;
|
28 |
+
|
29 |
+
/**
|
30 |
+
* Stores heartbeat settings across class methods.
|
31 |
+
*
|
32 |
+
* @var array
|
33 |
+
*/
|
34 |
public $settings = array();
|
35 |
|
36 |
+
/**
|
37 |
+
* Just a regular ole constructor.
|
38 |
+
*/
|
39 |
public function __construct() {
|
40 |
|
41 |
if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
|
42 |
$current_url = $_SERVER['REQUEST_URI'] . '?' . $_SERVER['QUERY_STRING'];
|
43 |
+
} else {
|
44 |
$current_url = $_SERVER['REQUEST_URI'];
|
45 |
}
|
46 |
|
64 |
add_filter( 'heartbeat_settings', array( $this, 'maybe_modify' ), 99, 1 );
|
65 |
}
|
66 |
|
67 |
+
/**
|
68 |
+
* Checks if the current location has a rule.
|
69 |
+
*
|
70 |
+
* @param array $locations Locations that have rules.
|
71 |
+
*
|
72 |
+
* @return bool
|
73 |
+
*/
|
74 |
public function check_location( $locations ) {
|
75 |
if ( ! isset( $locations ) || ! is_array( $locations ) ) {
|
76 |
return false;
|
87 |
return false;
|
88 |
}
|
89 |
|
90 |
+
/**
|
91 |
+
* Disable the heartbeat, if needed.
|
92 |
+
*
|
93 |
+
* @return void
|
94 |
+
*/
|
95 |
public function maybe_disable() {
|
96 |
foreach ( $this->settings as $rule ) {
|
97 |
+
if ( array_key_exists( 'heartbeat_control_behavior', $rule ) && $rule['heartbeat_control_behavior'] === 'disable' ) {
|
98 |
+
|
99 |
+
if ( ! array_key_exists( 'heartbeat_control_location', $rule ) ) {
|
100 |
+
return;
|
101 |
+
}
|
102 |
+
|
103 |
if ( $this->check_location( $rule['heartbeat_control_location'] ) ) {
|
104 |
wp_deregister_script( 'heartbeat' );
|
105 |
return;
|
109 |
|
110 |
}
|
111 |
|
112 |
+
/**
|
113 |
+
* Modify the heartbeat, if needed.
|
114 |
+
*
|
115 |
+
* @param array $settings The settings.
|
116 |
+
*
|
117 |
+
* @return array
|
118 |
+
*/
|
119 |
public function maybe_modify( $settings ) {
|
120 |
|
121 |
foreach ( $this->settings as $rule ) {
|
122 |
if ( array_key_exists( 'heartbeat_control_behavior', $rule ) && $rule['heartbeat_control_behavior'] === 'modify' ) {
|
123 |
+
|
124 |
+
if ( ! array_key_exists( 'heartbeat_control_location', $rule ) ) {
|
125 |
+
return;
|
126 |
+
}
|
127 |
+
|
128 |
if ( $this->check_location( $rule['heartbeat_control_location'] ) ) {
|
129 |
$settings['interval'] = intval( $rule['heartbeat_control_frequency'] );
|
130 |
return $settings;
|
readme.txt
CHANGED
@@ -4,7 +4,7 @@ Donate link: https://paypal.me/JeffMatson
|
|
4 |
Tags: heartbeat, admin-ajax, server resources, heartbeat control, heartbeat api, performance, debugging, javascript
|
5 |
Requires at least: 3.6
|
6 |
Tested up to: 4.9.7
|
7 |
-
Stable tag: 1.2.
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
@@ -35,6 +35,12 @@ All options are located within Settings > Heartbeat Control.
|
|
35 |
If you commonly leave your WordPress admin up for long periods of time, especially while writing or editing a post, the repeated POST requests can cause high resource usage. To avoid this, the heartbeat can be modified or even disabled to lower your server resource usage.
|
36 |
|
37 |
== Changelog ==
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
= 1.2.4 =
|
39 |
* Updated CMB2 to 2.4.2.
|
40 |
* Bumpted "tested up to" version.
|
4 |
Tags: heartbeat, admin-ajax, server resources, heartbeat control, heartbeat api, performance, debugging, javascript
|
5 |
Requires at least: 3.6
|
6 |
Tested up to: 4.9.7
|
7 |
+
Stable tag: 1.2.5
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
35 |
If you commonly leave your WordPress admin up for long periods of time, especially while writing or editing a post, the repeated POST requests can cause high resource usage. To avoid this, the heartbeat can be modified or even disabled to lower your server resource usage.
|
36 |
|
37 |
== Changelog ==
|
38 |
+
= 1.2.5 =
|
39 |
+
* Fixed issue caused by previous version deployment.
|
40 |
+
* Added hbc_disable_notice hook to force dismissal of update notices.
|
41 |
+
* Additional documentation added.
|
42 |
+
* Minor standards adjustments.
|
43 |
+
|
44 |
= 1.2.4 =
|
45 |
* Updated CMB2 to 2.4.2.
|
46 |
* Bumpted "tested up to" version.
|
settings.php
CHANGED
@@ -1,36 +1,68 @@
|
|
1 |
<?php
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
namespace Heartbeat_Control;
|
4 |
|
|
|
|
|
|
|
5 |
class Settings {
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
public function render_slider_field( $field, $field_escaped_value, $field_object_id, $field_object_type, $field_type_object ) {
|
8 |
echo '<div class="slider-field"></div>';
|
|
|
9 |
echo $field_type_object->input( array(
|
10 |
'type' => 'hidden',
|
11 |
'class' => 'slider-field-value',
|
12 |
'readonly' => 'readonly',
|
13 |
'data-start' => absint( $field_escaped_value ),
|
14 |
-
'data-min' => $field->min(),
|
15 |
-
'data-max' => $field->max(),
|
16 |
-
'data-step' => $field->step(),
|
17 |
'desc' => '',
|
18 |
) );
|
19 |
-
echo '<span class="slider-field-value-display">'. $field->value_label() .' <span class="slider-field-value-text"></span></span>';
|
20 |
$field_type_object->_desc( true, true );
|
21 |
}
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
public function enqueue_scripts( $hook ) {
|
24 |
-
if ( $hook
|
25 |
return;
|
26 |
}
|
27 |
|
28 |
-
wp_enqueue_script('heartbeat-control-settings', plugins_url( '/assets/js/bundle.js'
|
29 |
wp_localize_script( 'heartbeat-control-settings', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
|
30 |
wp_register_style( 'slider_ui', '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.min.css', array(), '1.0' );
|
31 |
wp_enqueue_style( 'slider_ui' );
|
32 |
}
|
33 |
|
|
|
|
|
|
|
|
|
|
|
34 |
public function init_metaboxes() {
|
35 |
add_action( 'cmb2_render_slider', array( $this, 'render_slider_field' ), 10, 5 );
|
36 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
@@ -39,7 +71,7 @@ class Settings {
|
|
39 |
$cmb = new_cmb2_box( array(
|
40 |
'id' => 'heartbeat_control_settings',
|
41 |
'title' => __( 'Heartbeat Control Settings', 'heartbeat-control' ),
|
42 |
-
'object_types' => array( 'options-page'
|
43 |
'option_key' => 'heartbeat_control_settings',
|
44 |
'capability' => 'manage_options',
|
45 |
'parent_slug' => 'options-general.php',
|
@@ -58,36 +90,36 @@ class Settings {
|
|
58 |
) );
|
59 |
|
60 |
$cmb->add_group_field( $rule_group, array(
|
61 |
-
'name'
|
62 |
-
'id'
|
63 |
-
'type'
|
64 |
-
'default'
|
65 |
-
'classes'
|
66 |
-
'options'
|
67 |
-
'allow'
|
68 |
-
'disable'
|
69 |
-
'modify'
|
70 |
),
|
71 |
) );
|
72 |
|
73 |
$cmb->add_group_field( $rule_group, array(
|
74 |
-
'name'
|
75 |
-
'id'
|
76 |
-
'type'
|
77 |
-
'options'
|
78 |
-
'admin'
|
79 |
-
'frontend'
|
80 |
'/wp-admin/post.php' => __( 'Post Editor', 'heartbeat-control' ),
|
81 |
),
|
82 |
) );
|
83 |
|
84 |
$cmb->add_group_field( $rule_group, array(
|
85 |
-
'name'
|
86 |
-
'id'
|
87 |
-
'type'
|
88 |
-
'min'
|
89 |
-
'step'
|
90 |
-
'max'
|
91 |
'default' => '15',
|
92 |
'classes' => 'heartbeat_frequency',
|
93 |
) );
|
1 |
<?php
|
2 |
+
/**
|
3 |
+
* Contains the Heartbeat_Control\Settings class.
|
4 |
+
*
|
5 |
+
* @package Heartbeat_Control
|
6 |
+
*/
|
7 |
|
8 |
namespace Heartbeat_Control;
|
9 |
|
10 |
+
/**
|
11 |
+
* Primary settings page class.
|
12 |
+
*/
|
13 |
class Settings {
|
14 |
|
15 |
+
/**
|
16 |
+
* Renders the slider field.
|
17 |
+
*
|
18 |
+
* @param [type] $field
|
19 |
+
* @param [type] $field_escaped_value
|
20 |
+
* @param [type] $field_object_id
|
21 |
+
* @param [type] $field_object_type
|
22 |
+
* @param [type] $field_type_object
|
23 |
+
*
|
24 |
+
* @return void
|
25 |
+
*/
|
26 |
public function render_slider_field( $field, $field_escaped_value, $field_object_id, $field_object_type, $field_type_object ) {
|
27 |
echo '<div class="slider-field"></div>';
|
28 |
+
// phpcs:ignore
|
29 |
echo $field_type_object->input( array(
|
30 |
'type' => 'hidden',
|
31 |
'class' => 'slider-field-value',
|
32 |
'readonly' => 'readonly',
|
33 |
'data-start' => absint( $field_escaped_value ),
|
34 |
+
'data-min' => intval( $field->min() ),
|
35 |
+
'data-max' => intval( $field->max() ),
|
36 |
+
'data-step' => intval( $field->step() ),
|
37 |
'desc' => '',
|
38 |
) );
|
39 |
+
echo '<span class="slider-field-value-display">' . esc_html( $field->value_label() ) . ' <span class="slider-field-value-text"></span></span>';
|
40 |
$field_type_object->_desc( true, true );
|
41 |
}
|
42 |
|
43 |
+
/**
|
44 |
+
* Enqueue scripts on settings pages.
|
45 |
+
*
|
46 |
+
* @param string $hook The settings page slug.
|
47 |
+
*
|
48 |
+
* @return void
|
49 |
+
*/
|
50 |
public function enqueue_scripts( $hook ) {
|
51 |
+
if ( $hook !== 'settings_page_heartbeat_control_settings' ) {
|
52 |
return;
|
53 |
}
|
54 |
|
55 |
+
wp_enqueue_script( 'heartbeat-control-settings', plugins_url( '/assets/js/bundle.js', __FILE__ ), array( 'jquery', 'jquery-ui-slider' ), '1.0.0', true );
|
56 |
wp_localize_script( 'heartbeat-control-settings', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
|
57 |
wp_register_style( 'slider_ui', '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.min.css', array(), '1.0' );
|
58 |
wp_enqueue_style( 'slider_ui' );
|
59 |
}
|
60 |
|
61 |
+
/**
|
62 |
+
* Initialize the meta boxes in CMB2.
|
63 |
+
*
|
64 |
+
* @return void
|
65 |
+
*/
|
66 |
public function init_metaboxes() {
|
67 |
add_action( 'cmb2_render_slider', array( $this, 'render_slider_field' ), 10, 5 );
|
68 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
71 |
$cmb = new_cmb2_box( array(
|
72 |
'id' => 'heartbeat_control_settings',
|
73 |
'title' => __( 'Heartbeat Control Settings', 'heartbeat-control' ),
|
74 |
+
'object_types' => array( 'options-page' ),
|
75 |
'option_key' => 'heartbeat_control_settings',
|
76 |
'capability' => 'manage_options',
|
77 |
'parent_slug' => 'options-general.php',
|
90 |
) );
|
91 |
|
92 |
$cmb->add_group_field( $rule_group, array(
|
93 |
+
'name' => __( 'Heartbeat Behavior', 'heartbeat-control' ),
|
94 |
+
'id' => $prefix . 'behavior',
|
95 |
+
'type' => 'select',
|
96 |
+
'default' => 'allow',
|
97 |
+
'classes' => 'heartbeat_behavior',
|
98 |
+
'options' => array(
|
99 |
+
'allow' => __( 'Allow Heartbeat', 'heartbeat-control' ),
|
100 |
+
'disable' => __( 'Disable Heartbeat', 'heartbeat-control' ),
|
101 |
+
'modify' => __( 'Modify Heartbeat', 'heartbeat-control' ),
|
102 |
),
|
103 |
) );
|
104 |
|
105 |
$cmb->add_group_field( $rule_group, array(
|
106 |
+
'name' => __( 'Locations', 'heartbeat-control' ),
|
107 |
+
'id' => $prefix . 'location',
|
108 |
+
'type' => 'multicheck',
|
109 |
+
'options' => array(
|
110 |
+
'admin' => __( 'WordPress Dashboard', 'heartbeat-control' ),
|
111 |
+
'frontend' => __( 'Frontend', 'heartbeat-control' ),
|
112 |
'/wp-admin/post.php' => __( 'Post Editor', 'heartbeat-control' ),
|
113 |
),
|
114 |
) );
|
115 |
|
116 |
$cmb->add_group_field( $rule_group, array(
|
117 |
+
'name' => __( 'Frequency', 'heartbeat-control' ),
|
118 |
+
'id' => $prefix . 'frequency',
|
119 |
+
'type' => 'slider',
|
120 |
+
'min' => '15',
|
121 |
+
'step' => '1',
|
122 |
+
'max' => '300',
|
123 |
'default' => '15',
|
124 |
'classes' => 'heartbeat_frequency',
|
125 |
) );
|