1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sync package. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class will handle checkout of Sync queues for REST Endpoints. |
12
|
|
|
* |
13
|
|
|
* @since 9.9.0 |
14
|
|
|
*/ |
15
|
|
|
class REST_Sender { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Items pending send. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
public $items = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Checkout objects from the queue |
26
|
|
|
* |
27
|
|
|
* @param string $queue_name Name of Queue. |
28
|
|
|
* @param int $number_of_items Number of Items. |
29
|
|
|
* @param array $args arguments. |
30
|
|
|
* |
31
|
|
|
* @return array|WP_Error |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
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. |
78
|
|
|
*/ |
79
|
|
|
public function jetpack_sync_send_data_listener() { |
80
|
|
|
foreach ( func_get_args()[0] as $key => $item ) { |
81
|
|
|
$this->items[ $key ] = $item; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Check out a buffer of full sync actions. |
87
|
|
|
* |
88
|
|
|
* @return array Sync Actions to be returned to requestor |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function immediate_full_sync_pull() { |
91
|
|
|
// try to give ourselves as much time as possible. |
92
|
|
|
set_time_limit( 0 ); |
93
|
|
|
|
94
|
|
|
$original_send_data_cb = array( 'Automattic\Jetpack\Sync\Actions', 'send_data' ); |
95
|
|
|
$temp_send_data_cb = array( $this, 'jetpack_sync_send_data_listener' ); |
96
|
|
|
|
97
|
|
|
Sender::get_instance()->set_enqueue_wait_time( 0 ); |
98
|
|
|
remove_filter( 'jetpack_sync_send_data', $original_send_data_cb ); |
99
|
|
|
add_filter( 'jetpack_sync_send_data', $temp_send_data_cb, 10, 6 ); |
100
|
|
|
Sender::get_instance()->do_full_sync(); |
101
|
|
|
remove_filter( 'jetpack_sync_send_data', $temp_send_data_cb ); |
102
|
|
|
add_filter( 'jetpack_sync_send_data', $original_send_data_cb, 10, 6 ); |
103
|
|
|
|
104
|
|
|
return array( |
105
|
|
|
'items' => $this->items, |
106
|
|
|
'codec' => Sender::get_instance()->get_codec()->name(), |
107
|
|
|
'sent_timestamp' => time(), |
108
|
|
|
'status' => Actions::get_sync_status(), |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Checkout items out of the sync queue. |
114
|
|
|
* |
115
|
|
|
* @param Queue $queue Sync Queue. |
116
|
|
|
* @param int $number_of_items Number of items to checkout. |
117
|
|
|
* |
118
|
|
|
* @return WP_Error |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
protected function get_buffer( $queue, $number_of_items ) { |
121
|
|
|
$start = time(); |
122
|
|
|
$max_duration = 5; // this will try to get the buffer. |
123
|
|
|
|
124
|
|
|
$buffer = $queue->checkout( $number_of_items ); |
125
|
|
|
$duration = time() - $start; |
126
|
|
|
|
127
|
|
|
while ( is_wp_error( $buffer ) && $duration < $max_duration ) { |
128
|
|
|
sleep( 2 ); |
129
|
|
|
$duration = time() - $start; |
130
|
|
|
$buffer = $queue->checkout( $number_of_items ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ( false === $buffer ) { |
134
|
|
|
return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $buffer; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.