@@ 33-74 (lines=42) @@ | ||
30 | * |
|
31 | * @return array|WP_Error |
|
32 | */ |
|
33 | public function queue_pull( $queue_name, $number_of_items, $args ) { |
|
34 | $queue = new Queue( $queue_name ); |
|
35 | ||
36 | if ( 0 === $queue->size() ) { |
|
37 | return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); |
|
38 | } |
|
39 | ||
40 | $sender = Sender::get_instance(); |
|
41 | ||
42 | // try to give ourselves as much time as possible. |
|
43 | set_time_limit( 0 ); |
|
44 | ||
45 | if ( $args['pop'] ) { |
|
46 | $buffer = new Queue_Buffer( 'pop', $queue->pop( $number_of_items ) ); |
|
47 | } else { |
|
48 | // let's delete the checkin state. |
|
49 | if ( $args['force'] ) { |
|
50 | $queue->unlock(); |
|
51 | } |
|
52 | $buffer = $this->get_buffer( $queue, $number_of_items ); |
|
53 | } |
|
54 | // Check that the $buffer is not checkout out already. |
|
55 | if ( is_wp_error( $buffer ) ) { |
|
56 | return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 ); |
|
57 | } |
|
58 | ||
59 | if ( ! is_object( $buffer ) ) { |
|
60 | return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 ); |
|
61 | } |
|
62 | ||
63 | Settings::set_is_syncing( true ); |
|
64 | list( $items_to_send, $skipped_items_ids ) = $sender->get_items_to_send( $buffer, $args['encode'] ); |
|
65 | Settings::set_is_syncing( false ); |
|
66 | ||
67 | return array( |
|
68 | 'buffer_id' => $buffer->id, |
|
69 | 'items' => $items_to_send, |
|
70 | 'skipped_items' => $skipped_items_ids, |
|
71 | 'codec' => $args['encode'] ? $sender->get_codec()->name() : null, |
|
72 | 'sent_timestamp' => time(), |
|
73 | ); |
|
74 | } |
|
75 | ||
76 | /** |
|
77 | * Adds Sync items to local property. |
@@ 261-302 (lines=42) @@ | ||
258 | return $this->queue_pull( $queue_name, $number_of_items, $args ); |
|
259 | } |
|
260 | ||
261 | function queue_pull( $queue_name, $number_of_items, $args ){ |
|
262 | $queue = new Queue( $queue_name ); |
|
263 | ||
264 | if ( 0 === $queue->size() ) { |
|
265 | return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); |
|
266 | } |
|
267 | ||
268 | $sender = Sender::get_instance(); |
|
269 | ||
270 | // try to give ourselves as much time as possible. |
|
271 | set_time_limit( 0 ); |
|
272 | ||
273 | if ( $args['pop'] ) { |
|
274 | $buffer = new Queue_Buffer( 'pop', $queue->pop( $number_of_items ) ); |
|
275 | } else { |
|
276 | // let's delete the checkin state. |
|
277 | if ( $args['force'] ) { |
|
278 | $queue->unlock(); |
|
279 | } |
|
280 | $buffer = $this->get_buffer( $queue, $number_of_items ); |
|
281 | } |
|
282 | // Check that the $buffer is not checkout out already. |
|
283 | if ( is_wp_error( $buffer ) ) { |
|
284 | return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 ); |
|
285 | } |
|
286 | ||
287 | if ( ! is_object( $buffer ) ) { |
|
288 | return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 ); |
|
289 | } |
|
290 | ||
291 | Settings::set_is_syncing( true ); |
|
292 | list( $items_to_send, $skipped_items_ids ) = $sender->get_items_to_send( $buffer, $args['encode'] ); |
|
293 | Settings::set_is_syncing( false ); |
|
294 | ||
295 | return array( |
|
296 | 'buffer_id' => $buffer->id, |
|
297 | 'items' => $items_to_send, |
|
298 | 'skipped_items' => $skipped_items_ids, |
|
299 | 'codec' => $args['encode'] ? $sender->get_codec()->name() : null, |
|
300 | 'sent_timestamp' => time(), |
|
301 | ); |
|
302 | } |
|
303 | ||
304 | public $items = []; |
|
305 |