| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) 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 |
$query_page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : $pagenow; // phpcs:ignore WordPress.Security.NonceVerification -- not processing form data |
| 22 |
|
| 23 |
if (in_array($query_page, $pages_enabled)) { |
| 24 |
add_filter('heartbeat_settings', array($this, 'set_heartbeat_time_interval'), PHP_INT_MAX); |
| 25 |
} |
| 26 |
|
| 27 |
// Handle heartbeat events |
| 28 |
add_filter('heartbeat_received', array($this, 'receive_heartbeat'), 10, 2); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get WP_Optimize_Heartbeat instance. |
| 33 |
* |
| 34 |
* @return WP_Optimize_Heartbeat |
| 35 |
*/ |
| 36 |
public static function get_instance() { |
| 37 |
static $_instance = null; |
| 38 |
if (null === $_instance) { |
| 39 |
$_instance = new self(); |
| 40 |
} |
| 41 |
|
| 42 |
return $_instance; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set custom heartbeat API interval |
| 47 |
* |
| 48 |
* @param array $settings Current WP settings |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
public function set_heartbeat_time_interval($settings) { |
| 52 |
$settings['interval'] = 15; |
| 53 |
return $settings; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Receive Heartbeat data and respond. |
| 58 |
* |
| 59 |
* Processes data received via a Heartbeat request, and returns additional data to pass back to the front end. |
| 60 |
* |
| 61 |
* @param array $response Heartbeat response data to pass back to front end. |
| 62 |
* @param array $data Data received from the front end (unslashed). |
| 63 |
* |
| 64 |
* @return array|void |
| 65 |
*/ |
| 66 |
public function receive_heartbeat($response, $data) { |
| 67 |
$allowed_smush_commands = Updraft_Smush_Manager_Commands::get_allowed_ajax_commands(); |
| 68 |
$capability_required = current_user_can(WP_Optimize()->capability_required()); |
| 69 |
$commands = new Updraft_Smush_Manager_Commands(Updraft_Smush_Manager::instance()); |
| 70 |
$commands->heartbeat_command = true; |
| 71 |
|
| 72 |
foreach ($data as $uid => $command) { |
| 73 |
if (!$this->is_wpo_heartbeat($uid)) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
if (is_string($command)) { |
| 78 |
if (apply_filters('wp_optimize_is_heartbeat_valid_ajax_command', $command)) { |
| 79 |
$response['callbacks'][$uid] = apply_filters('wp_optimize_heartbeat_ajax', $command); |
| 80 |
} |
| 81 |
} else { |
| 82 |
$command_name = key($command); |
| 83 |
if ('updraft_smush_ajax' === $command_name) { |
| 84 |
|
| 85 |
if (!$capability_required) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
$command_data = current($command); |
| 90 |
|
| 91 |
if (empty($command_data['subaction'])) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
if (!in_array($command_data['subaction'], $allowed_smush_commands, true)) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
|
| 99 |
$command_data_param = null; |
| 100 |
if (isset($command_data['data'])) { |
| 101 |
$command_data_param = $command_data['data']; |
| 102 |
} |
| 103 |
|
| 104 |
$_REQUEST['subaction'] = $command_data['subaction']; |
| 105 |
$_REQUEST['data'] = $command_data_param; |
| 106 |
|
| 107 |
if (!is_callable(array($commands, $command_data['subaction']))) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
ob_start(); |
| 112 |
|
| 113 |
$method = new ReflectionMethod($commands, $command_data['subaction']); |
| 114 |
|
| 115 |
try { |
| 116 |
$command_response = $method->invokeArgs($commands, array($command_data_param)); |
| 117 |
} catch (Exception $e) { |
| 118 |
$command_response = new WP_Error($e->getCode() ? $e->getCode() : 1, $e->getMessage()); |
| 119 |
} |
| 120 |
|
| 121 |
if (is_array($command_response)) { |
| 122 |
$response['callbacks'][$uid] = $command_response; |
| 123 |
ob_end_clean(); |
| 124 |
} else { |
| 125 |
$response['callbacks'][$uid] = ob_get_clean(); |
| 126 |
} |
| 127 |
|
| 128 |
if (is_wp_error($command_response)) { |
| 129 |
$response['callbacks'][$uid] = array( |
| 130 |
'status' => true, |
| 131 |
'result' => false, |
| 132 |
'error_code' => $command_response->get_error_code(), |
| 133 |
'error_message' => $command_response->get_error_message(), |
| 134 |
'error_data' => $command_response->get_error_data(), |
| 135 |
'skip_notice' => !empty($command_data_param['skip_notice']), |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (true === $commands->background_command) { |
| 143 |
$response['server_time'] = time(); |
| 144 |
$response['wp-auth-check'] = true; |
| 145 |
$commands->final_response = $response; |
| 146 |
die(); |
| 147 |
} else { |
| 148 |
return $response; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Check if the received heartbeat action was triggered by WPO's heartbeat.js layer |
| 154 |
* |
| 155 |
* @param string $uid The task unique id |
| 156 |
* @return bool |
| 157 |
*/ |
| 158 |
private function is_wpo_heartbeat($uid) { |
| 159 |
return 0 === strpos($uid, 'wpo-heartbeat-'); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
endif; |
| 164 |
|