PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.4.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.4.2
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / includes / class-wp-optimize-heartbeat.php

class-wp-optimize-heartbeat.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.4.2, at includes/class-wp-optimize-heartbeat.php

148 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WPO_VERSION')) die('No direct access allowed');
4
5 if (!class_exists('WP_Optimize_Heartbeat')) :
6
7 class WP_Optimize_Heartbeat {
8
9 /**
10 * Class constructor
11 *
12 * @return void
13 */
14 private function __construct() {
15 global $pagenow;
16
17 $pages_enabled = array('WP-Optimize', 'wpo_images', 'wpo_cache', 'wpo_minify', 'wpo_settings', 'wpo_support', 'wpo_mayalso', 'upload.php');
18
19 // Change heartbeat API frequency to 15 seconds to improve UI experience
20 // only for the pages that we enable in `$pages_enabled`
21 if (isset($_GET['page'])) {
22 $query_page = sanitize_text_field(wp_unslash($_GET['page'])); // phpcs:ignore WordPress.Security.NonceVerification -- not processing form data
23 } else {
24 $query_page = $pagenow;
25 }
26
27 if (in_array($query_page, $pages_enabled)) {
28 add_filter('heartbeat_settings', array($this, 'set_heartbeat_time_interval'), PHP_INT_MAX);
29 }
30
31 // Handle heartbeat events
32 add_filter('heartbeat_received', array($this, 'receive_heartbeat'), 10, 2);
33 }
34
35 /**
36 * Get WP_Optimize_Heartbeat instance.
37 *
38 * @return WP_Optimize_Heartbeat
39 */
40 public static function get_instance() {
41 $_instance = null;
42 if (null === $_instance) {
43 $_instance = new self();
44 }
45
46 return $_instance;
47 }
48
49 /**
50 * Set custom heartbeat API interval
51 *
52 * @param array $settings Current WP settings
53 * @return array
54 */
55 public function set_heartbeat_time_interval($settings) {
56 $settings['interval'] = 15;
57 return $settings;
58 }
59
60 /**
61 * Receive Heartbeat data and respond.
62 *
63 * Processes data received via a Heartbeat request, and returns additional data to pass back to the front end.
64 *
65 * @param array $response Heartbeat response data to pass back to front end.
66 * @param array $data Data received from the front end (unslashed).
67 *
68 * @return array
69 */
70 public function receive_heartbeat($response, $data) {
71 $commands = new Updraft_Smush_Manager_Commands(Updraft_Smush_Manager::instance());
72 $commands->heartbeat_command = true;
73
74 foreach ($data as $uid => $command) {
75 if (!$this->is_wpo_heartbeat($uid)) {
76 continue;
77 }
78
79 if (is_string($command)) {
80 if (apply_filters('wp_optimize_is_heartbeat_valid_ajax_command', $command)) {
81 $response['callbacks'][$uid] = apply_filters('wp_optimize_heartbeat_ajax', $command);
82 }
83 } else {
84 $command_name = key($command);
85 if ('updraft_smush_ajax' == $command_name) {
86 $command_data = current($command);
87
88 $command_data_param = null;
89 if (isset($command_data['data'])) {
90 $command_data_param = $command_data['data'];
91 }
92
93 $_REQUEST['subaction'] = $command_data['subaction'];
94 $_REQUEST['data'] = $command_data_param;
95
96 ob_start();
97
98 if (!is_callable(array($commands, $command_data['subaction']))) {
99 continue;
100 }
101
102 $method = new ReflectionMethod($commands, $command_data['subaction']);
103
104 $command_response = $method->invokeArgs($commands, array($command_data_param));
105
106 if (is_array($command_response)) {
107 $response['callbacks'][$uid] = $command_response;
108 ob_end_clean();
109 } else {
110 $response['callbacks'][$uid] = ob_get_clean();
111 }
112
113 if (is_wp_error($command_response)) {
114 $response['callbacks'][$uid] = array(
115 'status' => true,
116 'result' => false,
117 'error_code' => $command_response->get_error_code(),
118 'error_message' => $command_response->get_error_message(),
119 'error_data' => $command_response->get_error_data(),
120 );
121 }
122 }
123 }
124 }
125
126 if (true === $commands->background_command) {
127 $response['server_time'] = time();
128 $response['wp-auth-check'] = true;
129 $commands->final_response = $response;
130 die();
131 } else {
132 return $response;
133 }
134 }
135
136 /**
137 * Check if the received heartbeat action was triggered by WPO's heartbeat.js layer
138 *
139 * @param string $uid The task unique id
140 * @return bool
141 */
142 private function is_wpo_heartbeat($uid) {
143 return 0 === strpos($uid, 'wpo-heartbeat-');
144 }
145 }
146
147 endif;
148