Use REST API in heartbeat callback #130
This commit is contained in:
parent
fae9d42831
commit
3d8939e778
|
@ -55,6 +55,10 @@ class REST_Background_Processes_Controller extends REST_Controller {
|
|||
'paged' => [
|
||||
'type' => 'integer',
|
||||
'description' => __( 'Page to retrieve. Default 1', 'tainacan' ),
|
||||
],
|
||||
'recent' => [
|
||||
'type' => 'bool',
|
||||
'description' => __( 'Return only processes created or updated recently', 'tainacan' ),
|
||||
],
|
||||
],
|
||||
),
|
||||
|
@ -150,8 +154,13 @@ class REST_Background_Processes_Controller extends REST_Controller {
|
|||
$status_q = "AND done = 1";
|
||||
}
|
||||
}
|
||||
|
||||
$recent_q = '';
|
||||
if ( isset($request['recent']) && $request['recent'] !== false ) {
|
||||
$recent_q = "AND (processed_last >= NOW() - INTERVAL 10 MINUTE OR queued_on >= NOW() - INTERVAL 10 MINUTE)";
|
||||
}
|
||||
|
||||
$base_query = "FROM $this->table WHERE 1=1 $status_q $user_q ORDER BY priority DESC, queued_on DESC";
|
||||
$base_query = "FROM $this->table WHERE 1=1 $status_q $user_q $recent_q ORDER BY priority DESC, queued_on DESC";
|
||||
|
||||
$query = "SELECT * $base_query $limit_q";
|
||||
$count_query = "SELECT COUNT(ID) $base_query";
|
||||
|
|
|
@ -2,34 +2,33 @@
|
|||
namespace Tainacan;
|
||||
|
||||
class Background_Importer_Heartbeat {
|
||||
|
||||
public function __construct() {
|
||||
global $wpdb;
|
||||
$this->table = $wpdb->prefix . 'tnc_bg_process';
|
||||
add_filter( 'heartbeat_send', array( &$this, 'bg_process_feedback' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive Heartbeat data and response.
|
||||
*
|
||||
* Processes data received via a Heartbeat request, and returns additional data to pass back to the front end.
|
||||
*
|
||||
* @param array $response Heartbeat response data to pass back to front end.
|
||||
* @param array $data Data received from the front end (unslashed).
|
||||
*/
|
||||
public function bg_process_feedback( $response, $data ){
|
||||
global $wpdb;
|
||||
|
||||
$user_q = $wpdb->prepare("AND user_id = %d", get_current_user_id());
|
||||
$status_q = "";
|
||||
|
||||
$base_query = "FROM $this->table WHERE processed_last >= NOW() - INTERVAL 10 MINUTE";
|
||||
|
||||
$query = "SELECT * $base_query";
|
||||
$result = $wpdb->get_results($query);
|
||||
|
||||
$response['bg_process_feedback'] = $result;
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function __construct() {
|
||||
add_filter( 'heartbeat_send', array( &$this, 'bg_process_feedback' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive Heartbeat data and response.
|
||||
*
|
||||
* Processes data received via a Heartbeat request, and returns additional data to pass back to the front end.
|
||||
*
|
||||
* @param array $response Heartbeat response data to pass back to front end.
|
||||
* @param array $data Data received from the front end (unslashed).
|
||||
*/
|
||||
public function bg_process_feedback( $response, $data ){
|
||||
|
||||
|
||||
$request = new \WP_REST_Request( 'GET', '/tainacan/v2/bg-processes' );
|
||||
$request->set_query_params( [ 'recent' => 1 ] );
|
||||
$api_response = rest_do_request( $request );
|
||||
$server = rest_get_server();
|
||||
$data = $server->response_to_data( $api_response, false );
|
||||
|
||||
|
||||
$response['bg_process_feedback'] = $data;
|
||||
|
||||
return $response;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue