1 | <?php |
---|
2 | /** |
---|
3 | * Sync package. |
---|
4 | * |
---|
5 | * @package automattic/jetpack-sync |
---|
6 | */ |
---|
7 | |
---|
8 | namespace Automattic\Jetpack\Sync; |
---|
9 | |
---|
10 | use WP_Error; |
---|
11 | |
---|
12 | /** |
---|
13 | * This class will handle checkout of Sync queues for REST Endpoints. |
---|
14 | * |
---|
15 | * @since 1.23.1 |
---|
16 | */ |
---|
17 | class REST_Sender { |
---|
18 | |
---|
19 | /** |
---|
20 | * Items pending send. |
---|
21 | * |
---|
22 | * @var array |
---|
23 | */ |
---|
24 | public $items = array(); |
---|
25 | |
---|
26 | /** |
---|
27 | * Checkout objects from the queue |
---|
28 | * |
---|
29 | * @param string $queue_name Name of Queue. |
---|
30 | * @param int $number_of_items Number of Items. |
---|
31 | * @param array $args arguments. |
---|
32 | * |
---|
33 | * @return array|WP_Error |
---|
34 | */ |
---|
35 | public function queue_pull( $queue_name, $number_of_items, $args ) { |
---|
36 | $queue = new Queue( $queue_name ); |
---|
37 | |
---|
38 | if ( 0 === $queue->size() ) { |
---|
39 | return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); |
---|
40 | } |
---|
41 | |
---|
42 | $sender = Sender::get_instance(); |
---|
43 | |
---|
44 | // try to give ourselves as much time as possible. |
---|
45 | set_time_limit( 0 ); |
---|
46 | |
---|
47 | if ( ! empty( $args['pop'] ) ) { |
---|
48 | $buffer = new Queue_Buffer( 'pop', $queue->pop( $number_of_items ) ); |
---|
49 | } else { |
---|
50 | // let's delete the checkin state. |
---|
51 | if ( $args['force'] ) { |
---|
52 | $queue->unlock(); |
---|
53 | } |
---|
54 | $buffer = $this->get_buffer( $queue, $number_of_items ); |
---|
55 | } |
---|
56 | // Check that the $buffer is not checkout out already. |
---|
57 | if ( is_wp_error( $buffer ) ) { |
---|
58 | return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 ); |
---|
59 | } |
---|
60 | |
---|
61 | if ( ! is_object( $buffer ) ) { |
---|
62 | return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 ); |
---|
63 | } |
---|
64 | |
---|
65 | $encode = isset( $args['encode'] ) ? $args['encode'] : true; |
---|
66 | |
---|
67 | Settings::set_is_syncing( true ); |
---|
68 | list( $items_to_send, $skipped_items_ids ) = $sender->get_items_to_send( $buffer, $encode ); |
---|
69 | Settings::set_is_syncing( false ); |
---|
70 | |
---|
71 | return array( |
---|
72 | 'buffer_id' => $buffer->id, |
---|
73 | 'items' => $items_to_send, |
---|
74 | 'skipped_items' => $skipped_items_ids, |
---|
75 | 'codec' => $encode ? $sender->get_codec()->name() : null, |
---|
76 | 'sent_timestamp' => time(), |
---|
77 | ); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Adds Sync items to local property. |
---|
82 | */ |
---|
83 | public function jetpack_sync_send_data_listener() { |
---|
84 | foreach ( func_get_args()[0] as $key => $item ) { |
---|
85 | $this->items[ $key ] = $item; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Check out a buffer of full sync actions. |
---|
91 | * |
---|
92 | * @return array Sync Actions to be returned to requestor |
---|
93 | */ |
---|
94 | public function immediate_full_sync_pull() { |
---|
95 | // try to give ourselves as much time as possible. |
---|
96 | set_time_limit( 0 ); |
---|
97 | |
---|
98 | $original_send_data_cb = array( 'Automattic\Jetpack\Sync\Actions', 'send_data' ); |
---|
99 | $temp_send_data_cb = array( $this, 'jetpack_sync_send_data_listener' ); |
---|
100 | |
---|
101 | Sender::get_instance()->set_enqueue_wait_time( 0 ); |
---|
102 | remove_filter( 'jetpack_sync_send_data', $original_send_data_cb ); |
---|
103 | add_filter( 'jetpack_sync_send_data', $temp_send_data_cb, 10, 6 ); |
---|
104 | Sender::get_instance()->do_full_sync(); |
---|
105 | remove_filter( 'jetpack_sync_send_data', $temp_send_data_cb ); |
---|
106 | add_filter( 'jetpack_sync_send_data', $original_send_data_cb, 10, 6 ); |
---|
107 | |
---|
108 | return array( |
---|
109 | 'items' => $this->items, |
---|
110 | 'codec' => Sender::get_instance()->get_codec()->name(), |
---|
111 | 'sent_timestamp' => time(), |
---|
112 | 'status' => Actions::get_sync_status(), |
---|
113 | ); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Checkout items out of the sync queue. |
---|
118 | * |
---|
119 | * @param Queue $queue Sync Queue. |
---|
120 | * @param int $number_of_items Number of items to checkout. |
---|
121 | * |
---|
122 | * @return WP_Error |
---|
123 | */ |
---|
124 | protected function get_buffer( $queue, $number_of_items ) { |
---|
125 | $start = time(); |
---|
126 | $max_duration = 5; // this will try to get the buffer. |
---|
127 | |
---|
128 | $buffer = $queue->checkout( $number_of_items ); |
---|
129 | $duration = time() - $start; |
---|
130 | |
---|
131 | while ( is_wp_error( $buffer ) && $duration < $max_duration ) { |
---|
132 | sleep( 2 ); |
---|
133 | $duration = time() - $start; |
---|
134 | $buffer = $queue->checkout( $number_of_items ); |
---|
135 | } |
---|
136 | |
---|
137 | if ( false === $buffer ) { |
---|
138 | return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); |
---|
139 | } |
---|
140 | |
---|
141 | return $buffer; |
---|
142 | } |
---|
143 | } |
---|