-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathclass-push-sync.php
More file actions
290 lines (254 loc) · 7.78 KB
/
class-push-sync.php
File metadata and controls
290 lines (254 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Push Sync to Cloudinary.
*
* @package Cloudinary
*/
namespace Cloudinary\Sync;
use Cloudinary\Sync;
use Cloudinary\Utils;
/**
* Class Push_Sync
*
* Push media library to Cloudinary.
*/
class Push_Sync {
/**
* Holds the plugin instance.
*
* @since 0.1
*
* @var \Cloudinary\Plugin Instance of the global plugin.
*/
public $plugin;
/**
* Holds the ID of the last attachment synced.
*
* @var int
*/
protected $post_id;
/**
* Holds the media component.
*
* @var \Cloudinary\Media
*/
protected $media;
/**
* Holds the sync component.
*
* @var \Cloudinary\Sync
*/
protected $sync;
/**
* Holds the connect component.
*
* @var \Cloudinary\Connect
*/
protected $connect;
/**
* Holds the Rest_API component.
*
* @var \Cloudinary\REST_API
*/
protected $api;
/**
* Holds the sync queue object.
*
* @var \Cloudinary\Sync\Sync_Queue
*/
public $queue;
/**
* Push_Sync constructor.
*
* @param \Cloudinary\Plugin $plugin Global instance of the main plugin.
*/
public function __construct( \Cloudinary\Plugin $plugin ) {
$this->plugin = $plugin;
$this->register_hooks();
}
/**
* Register any hooks that this component needs.
*/
private function register_hooks() {
add_filter( 'cloudinary_api_rest_endpoints', array( $this, 'rest_endpoints' ) );
}
/**
* Setup this component.
*/
public function setup() {
// Setup components.
$this->media = $this->plugin->components['media'];
$this->sync = $this->plugin->components['sync'];
$this->connect = $this->plugin->components['connect'];
$this->api = $this->plugin->components['api'];
$this->queue = $this->sync->managers['queue'];
add_action( 'cloudinary_run_queue', array( $this, 'process_queue' ) );
add_action( 'cloudinary_sync_items', array( $this, 'process_assets' ) );
}
/**
* Add endpoints to the \Cloudinary\REST_API::$endpoints array.
*
* @param array $endpoints Endpoints from the filter.
*
* @return array
*/
public function rest_endpoints( $endpoints ) {
$endpoints['attachments'] = array(
'method' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'rest_get_queue_status' ),
'args' => array(),
'permission_callback' => array( $this, 'rest_can_manage_assets' ),
);
$endpoints['sync'] = array(
'method' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'rest_start_sync' ),
'args' => array(),
'permission_callback' => array( $this, 'rest_can_manage_assets' ),
);
$endpoints['queue'] = array(
'method' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'process_queue' ),
'args' => array(),
'permission_callback' => array( 'Cloudinary\REST_API', 'validate_request' ),
);
$endpoints['stats'] = array(
'method' => \WP_REST_Server::READABLE,
'callback' => array( $this->queue, 'get_total_synced_media' ),
'args' => array(),
'permission_callback' => array( 'Cloudinary\REST_API', 'validate_request' ),
);
return $endpoints;
}
/**
* Admin permission callback.
*
* Explicitly defined to allow easier testability.
*
* @return bool
*/
public function rest_can_manage_assets() {
return Utils::user_can( 'manage_assets', 'manage_options', 'push_sync' );
}
/**
* Get status of the current queue via REST API.
*
* @return \WP_REST_Response
*/
public function rest_get_queue_status() {
return rest_ensure_response(
array(
'success' => true,
'data' => $this->queue->is_running(),
)
);
}
/**
* Starts a sync backbround process.
*
* @param \WP_REST_Request $request The request.
*
* @return \WP_REST_Response
*/
public function rest_start_sync( \WP_REST_Request $request ) {
$type = $request->get_param( 'type' );
$start = $this->queue->is_enabled();
$state = array(
'success' => false,
);
if ( empty( $start ) ) {
$this->queue->stop_queue();
} else {
$state['success'] = $this->queue->start_queue( $type );
}
return rest_ensure_response( $state );
}
/**
* Process asset sync.
*
* @param int|array $attachments An attachment ID or an array of ID's.
*
* @return array
*/
public function process_assets( $attachments = array() ) {
$stat = array();
// If a single specified ID, push and return response.
$ids = array_map( 'intval', (array) $attachments );
$thread = $this->plugin->settings->get_param( 'current_sync_thread' );
// Handle based on Sync Type.
foreach ( $ids as $attachment_id ) {
// Skip non uploadable media.
if ( ! $this->media->is_uploadable_media( $attachment_id ) ) {
continue;
}
// Skip unsyncable delivery types.
if ( ! $this->sync->is_syncable( $attachment_id ) ) {
continue;
}
// Flag attachment as being processed.
update_post_meta( $attachment_id, Sync::META_KEYS['syncing'], time() );
$stat[ $attachment_id ] = array();
while ( $type = $this->sync->get_sync_type( $attachment_id, false ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition
// translators: variable is sync type.
$action_message = sprintf( __( 'Sync type: %s', 'cloudinary' ), $type );
do_action( '_cloudinary_queue_action', $action_message, $thread );
if ( isset( $stat[ $attachment_id ][ $type ] ) ) {
// Loop prevention.
break;
}
$stat[ $attachment_id ][ $type ] = true;
$result = $this->sync->run_sync_method( $type, 'sync', $attachment_id );
if ( ! empty( $result ) ) {
$this->sync->log_sync_result( $attachment_id, $type, $result );
}
}
Utils::clean_up_sync_meta( $attachment_id );
}
return $stat;
}
/**
* Attempts to restart an auto-sync thread if needed.
*/
public function init_autosync_restart() {
$thread = $this->plugin->settings->get_param( 'current_sync_thread' );
if ( 1 !== $this->queue->get_thread_state( $thread ) && $this->queue->get_post( $thread ) ) {
do_action( '_cloudinary_queue_action', __( 'Starting new thread.', 'cloudinary' ) );
$this->plugin->components['api']->background_request( 'queue', array( 'thread' => $thread ) );
}
}
/**
* Resume the bulk sync.
*
* @param \WP_REST_Request $request The request.
*/
public function process_queue( \WP_REST_Request $request ) {
$thread = $request->get_param( 'thread' );
// A second thread would technically overwrite this, however, the manual queue is out in v3.
$this->plugin->settings->set_param( 'current_sync_thread', $thread );
$thread_type = $this->queue->get_thread_type( $thread );
if ( 'autosync' === $thread_type ) {
add_action( 'shutdown', array( $this, 'init_autosync_restart' ) );
}
$queue = $this->queue->get_thread_queue( $thread );
$runs = 0;
$last_id = 0;
if ( ! empty( $queue['next'] ) && $this->queue->is_running( $thread_type ) ) {
while ( ( $attachment_id = $this->queue->get_post( $thread ) ) && $runs < 10 ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition
if ( $last_id === $attachment_id ) {
update_post_meta( $attachment_id, Sync::META_KEYS['sync_error'], __( 'Asset in sync loop.', 'cloudinary' ) );
delete_post_meta( $attachment_id, $thread );
continue;
}
// translators: variable is thread name and asset ID.
$action_message = sprintf( __( '%1$s - cycle %3$s: Syncing asset %2$d', 'cloudinary' ), $thread, $attachment_id, $runs );
do_action( '_cloudinary_queue_action', $action_message, $thread );
$this->process_assets( $attachment_id );
++$runs;
$last_id = $attachment_id;
}
$this->queue->stop_maybe( $thread_type );
}
// translators: variable is thread name.
$action_message = sprintf( __( 'Ending thread %s', 'cloudinary' ), $thread );
do_action( '_cloudinary_queue_action', $action_message, $thread );
}
}