Jetpack_JSON_API_Sync_Checkout_Endpoint

The WordPress Core Jetpack JSON API Sync Checkout Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/jetpack/class.jetpack-json-api-sync-endpoint.php  
  1. class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { 
  2. protected function result() { 
  3. $args = $this->input(); 
  4. $queue_name = $this->validate_queue( $args['queue'] ); 
  5.  
  6. if ( is_wp_error( $queue_name ) ) { 
  7. return $queue_name; 
  8.  
  9. if ( $args[ 'number_of_items' ] < 1 || $args[ 'number_of_items' ] > 100 ) { 
  10. return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and less then 100', 400 ); 
  11.  
  12. require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php'; 
  13. $queue = new Jetpack_Sync_Queue( $queue_name ); 
  14.  
  15. if ( 0 === $queue->size() ) { 
  16. return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); 
  17.  
  18. require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; 
  19. $sender = Jetpack_Sync_Sender::get_instance(); 
  20.  
  21. // try to give ourselves as much time as possible 
  22. set_time_limit( 0 ); 
  23.  
  24. // let's delete the checkin state 
  25. if ( $args['force'] ) { 
  26. $queue->unlock(); 
  27.  
  28. $buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] ); 
  29.  
  30. // Check that the $buffer is not checkout out already 
  31. if ( is_wp_error( $buffer ) ) { 
  32. return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 ); 
  33.  
  34. if ( ! is_object( $buffer ) ) { 
  35. return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 ); 
  36.  
  37. Jetpack_Sync_Settings::set_is_syncing( true ); 
  38. list( $items_to_send, $skipped_items_ids, $items ) = $sender->get_items_to_send( $buffer, $args['encode'] ); 
  39. Jetpack_Sync_Settings::set_is_syncing( false ); 
  40.  
  41. return array( 
  42. 'buffer_id' => $buffer->id,  
  43. 'items' => $items_to_send,  
  44. 'skipped_items' => $skipped_items_ids,  
  45. 'codec' => $args['encode'] ? $sender->get_codec()->name() : null,  
  46. 'sent_timestamp' => time(),  
  47. ); 
  48.  
  49. protected function get_buffer( $queue, $number_of_items ) { 
  50. $start = time(); 
  51. $max_duration = 5; // this will try to get the buffer 
  52.  
  53. $buffer = $queue->checkout( $number_of_items ); 
  54. $duration = time() - $start; 
  55.  
  56. while( is_wp_error( $buffer ) && $duration < $max_duration ) { 
  57. sleep( 2 ); 
  58. $duration = time() - $start; 
  59. $buffer = $queue->checkout( $number_of_items ); 
  60.  
  61. if ( $buffer === false ) { 
  62. return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); 
  63.  
  64. return $buffer;