PluginProbe
Media Cloud Sync / 1.2.10
Media Cloud Sync v1.2.10
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / base / item.php

item.php in Media Cloud Sync 1.2.10, at includes/base/item.php

473 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3
4 defined('ABSPATH') || exit;
5
6 class Item {
7 private static $instance = null;
8 private $assets_url;
9 private $version;
10 private $token;
11
12 protected $config;
13 protected $bucketConfig;
14 protected $settings;
15 protected $credentials;
16 protected $service;
17
18 protected $bucket_name;
19 protected $region = '';
20
21 /**
22 * Admin constructor.
23 * @since 1.0.0
24 */
25 public function __construct() {
26 $this->assets_url = WPMCS_ASSETS_URL;
27 $this->version = WPMCS_VERSION;
28 $this->token = WPMCS_TOKEN;
29
30 // Initialize setup
31 $this->init();
32 }
33
34 /**
35 * Initialize datas
36 */
37 public function init() {
38 $this->settings = Utils::get_settings();
39 $this->credentials = Utils::get_credentials();
40 $this->config = isset($this->credentials['config']) && !empty($this->credentials['config'])
41 ? $this->credentials['config']
42 : [];
43 $this->bucketConfig = isset($this->credentials['bucketConfig']) && !empty($this->credentials['bucketConfig'])
44 ? $this->credentials['bucketConfig']
45 : [];
46 $this->service = isset($this->credentials['service']) && !empty($this->credentials['service'])
47 ? $this->credentials['service']
48 : [];
49
50 if (isset($this->bucketConfig['bucket_name'])) {
51 $this->bucket_name = $this->bucketConfig['bucket_name'];
52 }
53 if (isset($this->config['region'])) {
54 $this->region = $this->config['region'];
55 }
56 }
57
58 /**
59 * Add Item In database
60 * @since 1.0.0
61 * @param
62 */
63 public function add(
64 $source_id,
65 $url,
66 $key,
67 $source_path,
68 $meta,
69 $source_type = 'media_library',
70 $is_private = 0
71 ) {
72 global $wpdb;
73 $item_id = false;
74
75 $data = array(
76 'provider' => $this->service,
77 'region' => $this->region,
78 'storage' => $this->bucket_name,
79 'source_id' => $source_id,
80 'source_path' => $source_path,
81 'source_type' => $source_type,
82 'url' => $url,
83 'key' => $key,
84 'is_private' => $is_private,
85 'extra' => maybe_serialize($meta),
86 );
87 if ($wpdb->insert(Db::get_table_name(), $data)) {
88 $item_id = $wpdb->insert_id;
89 $data['id'] = $item_id;
90 Utils::update_meta($source_id, 'item', $data);
91 Cache::update_item_cache($source_id.'_item', $data);
92 Counter::add( 'uploaded' );
93 }
94
95 return $item_id;
96 }
97
98
99 /**
100 * Get Item From Db
101 */
102 public function get($source_id, $source_type = 'media_library') {
103 global $wpdb;
104 $item = Cache::get_item_cache($source_id.'_item', false);
105 if($item === false) {
106 $item = Utils::get_meta($source_id, 'item', false);
107 if($item == false){
108 $item_table = Db::get_table_name();
109 $item = $wpdb->get_row("SELECT * FROM ".$item_table." WHERE source_id = $source_id AND source_type='$source_type'", ARRAY_A);
110
111 if($wpdb->last_error || null === $item || !(isset($item) && !empty($item))) {
112 Cache::update_item_cache($source_id.'_item', '');
113 return false;
114 }
115
116 Utils::update_meta($source_id, 'item', $item);
117 }
118
119 Cache::update_item_cache($source_id.'_item', $item == false ? '' : $item);
120 }
121
122 return $item;
123 }
124
125 /**
126 * Function to delete item from data base
127 * @since 1.0.0
128 */
129 public function delete($source_id){
130 global $wpdb;
131 if (isset($source_id) && !empty($source_id)) {
132 $rows = $wpdb->delete(Db::get_table_name(), array('source_id' => $source_id));
133 if ($wpdb->last_error || false === $rows) {
134 return false;
135 }
136 Utils::delete_meta($source_id, 'item');
137 Cache::delete_item_cache($source_id.'_item');
138 Counter::remove( 'uploaded' );
139 return true;
140 }
141 return false;
142 }
143
144
145 /**
146 * Function to update item in data base
147 * @since 1.0.0
148 */
149 public function update($source_id, $data){
150 global $wpdb;
151 if (isset($source_id) && !empty($source_id)) {
152
153 $data['provider'] = $this->service;
154 $data['region'] = $this->region;
155 $data['storage'] = $this->bucket_name;
156
157 $rows = $wpdb->update(Db::get_table_name(), $data, array('source_id' => $source_id));
158 if ($wpdb->last_error || false === $rows) {
159 return false;
160 }
161
162 Utils::delete_meta($source_id, 'item');
163 Cache::delete_item_cache($source_id.'_item');
164
165 // Reset cache
166 $current_data = $this->get($source_id);
167 return true;
168 }
169 return false;
170 }
171
172 /**
173 * Get extra values of item from database
174 * @since 1.0.0
175 * @param
176 */
177 public function get_extras($source_id, $field = false){
178 $data = $this->get($source_id);
179 if ($data) {
180 if (isset($data['extra']) && !empty($data['extra'])) {
181
182 if(!Utils::is_empty($field)) {
183 $extras = unserialize($data['extra']);
184 return isset($extras[$field]) && !empty($extras[$field]) ? $extras[$field] : false;
185 }
186
187 return unserialize($data['extra']);
188 }
189 }
190 return false;
191 }
192
193
194 /**
195 * Get column of item from database
196 * @since 1.0.0
197 * @param
198 */
199 public function get_field($source_id, $field){
200 $data = $this->get($source_id);
201 if ($data) {
202 if (isset($data[$field]) && !empty($data[$field])) {
203 return $data[$field];
204 }
205 }
206 return false;
207 }
208
209
210 /**
211 * Get backup values of item from database
212 * @since 1.0.0
213 * @param
214 */
215 public function get_backup($source_id){
216 $data = $this->get_extras($source_id);
217 if ($data) {
218 if (isset($data['backup']) && !empty($data['backup'])) {
219 return unserialize($data['backup']);
220 }
221 }
222 return false;
223 }
224
225
226 /**
227 * Checking Item that is served by provider And Is rewrite URL is enabled
228 * @since 1.0.0
229 * @param
230 */
231 public function is_available_from_provider($attachment_id, $check_rewrite = true){
232 $item = $this->get($attachment_id);
233 if(Utils::is_empty($item)) {
234 return false;
235 }
236
237 if ($item['provider'] == $this->service) {
238 if(
239 ($check_rewrite && (isset($this->settings['rewrite_url']) && $this->settings['rewrite_url'])) ||
240 !$check_rewrite
241 ) {
242 return true;
243 }
244 }
245 return false;
246 }
247
248 /**
249 * Get items by paths
250 */
251 public function get_items_by_source_paths( $paths = [] ) {
252 global $wpdb;
253
254 $items = [];
255
256 if (!Utils::is_empty($paths)) {
257 // Ensure paths are properly sanitized and ready for the query
258 $like_conditions = array();
259 foreach ($paths as $path) {
260 // Prepare the SQL conditions for exact match in source_path and partial match in extras column
261 $like_conditions[] = $wpdb->prepare("(source_path = %s OR extra LIKE %s)", $path, '%' . $wpdb->esc_like($path) . '%');
262 }
263
264 // Combine the conditions with OR
265 $where_clause = implode(' OR ', $like_conditions);
266
267 // Execute the SQL query to fetch source_id instead of source_path
268 $item_table = Db::get_table_name();
269 $sql = "SELECT DISTINCT source_id FROM " . $item_table . " WHERE " . $where_clause;
270 $results = $wpdb->get_results($sql, ARRAY_A);
271
272 if ($wpdb->last_error || Utils::is_empty($results)) {
273 return [];
274 }
275
276 $source_ids = array();
277 foreach ($results as $row) {
278 $item = $this->get((int)$row['source_id']);
279 if(!Utils::is_empty($item)) {
280 $items[] = $item;
281 }
282 }
283 return $items;
284 }
285 return $items;
286 }
287
288
289 /**
290 * Get similar existing files like origin path
291 * @since 1.0.0
292 */
293 public function get_similar_files_by_path($path){
294 global $wpdb;
295 if (isset($path) && !empty($path)) {
296 $item_table = Db::get_table_name();
297 $results = $wpdb->get_results("SELECT source_path FROM " . $item_table . " WHERE source_path LIKE '$path%'", ARRAY_A);
298 if ($wpdb->last_error || null === $results || !(isset($results) && !empty($results))) {
299 return false;
300 }
301 $source_paths = array();
302 foreach ($results as $row) {
303 $source_paths[] = $row['source_path'];
304 }
305 return $source_paths;
306 }
307 return false;
308 }
309
310
311 /**
312 * Get service url of item from database
313 * @since 1.0.0
314 * @param
315 */
316 public function get_url($source_id, $size = 'full'){
317 if ($data = $this->get($source_id)) {
318 $extras = $this->get_extras($source_id);
319 $key = '';
320 $url = '';
321 switch($size) {
322 case 'full':
323 $key = $data['key'];
324 $url = $data['url'];
325 break;
326 case 'original':
327 if(
328 isset($extras) && !empty($extras) &&
329 isset($extras['original']) && !empty($extras['original'])
330 ) {
331 $key = $extras['original']['key'];
332 $url = $extras['original']['url'];
333 }
334 default:
335 if(
336 isset($extras) && !empty($extras) &&
337 isset($extras['sizes']) && !empty($extras['sizes']) &&
338 isset($extras['sizes'][$size]) && !empty($extras['sizes'][$size])
339 ) {
340 $key = $extras['sizes'][$size]['key'];
341 $url = $extras['sizes'][$size]['url'];
342 }
343 }
344
345 if(!empty($key)){
346 if (
347 isset($this->settings['enable_presigned']) && $this->settings['enable_presigned'] &&
348 isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire'])
349 ) {
350 $preSignedUrl = Utils::get_meta( $source_id, 'presigned_url_'.$size );
351 if ($preSignedUrl === false) {
352 $new_url = Service::instance()->get_presigned_url($key);
353
354 if (!Utils::is_empty($new_url)) {
355 $preSignedUrl = Cdn::may_generate_cdn_url($new_url, $key);
356 $expireMinutes = (int)(isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire']))
357 ? $this->settings['presigned_expire']
358 : 20;
359 $expireSeconds = $expireMinutes * 60;
360
361 Utils::update_meta($source_id, 'presigned_url_'.$size, $preSignedUrl, false, $expireSeconds);
362 }
363 }
364 return $preSignedUrl;
365 } else {
366 return Cdn::may_generate_cdn_url($url, $key);
367 }
368 }
369 }
370 return false;
371 }
372
373
374 /**
375 * Get service path of item from database
376 * @since 1.0.0
377 * @param
378 */
379 public function moveToServer($source_id, $size = 'full', $all = false){
380 $server_files = array();
381 $server_file = false;
382 $upload_dir = wp_get_upload_dir();
383 $source_id = (int)$source_id;
384
385 if ($data = $this->get($source_id)) {
386 $wpmcsService = Service::instance();
387 if($all) {
388 if (
389 isset($data['source_path']) && !empty($data['source_path']) &&
390 isset($data['key']) && !empty($data['key'])
391 ) {
392 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
393 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
394 $server_files['full'] = $file_path;
395 }
396 }
397 $extras = $this->get_extras($source_id, false) ?: [];
398 if (isset($extras['original']) && !empty($extras['original'])) {
399 $original = $extras['original'];
400 if(!empty($original)) {
401 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
402 if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) {
403 $server_files['original'] = $original_file_path;
404 }
405 }
406 }
407 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
408 $sizes = $extras['sizes'];
409 foreach($sizes as $sub_size => $sub_file) {
410 if(!empty($sub_file)) {
411 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sub_file['source_path'];
412 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sub_file['key'], $sub_file_path)) {
413 $server_files[$sub_size] = $sub_file_path;
414 }
415 }
416 }
417 }
418 return !empty($server_files) ? $server_files : false;
419 } else {
420 if($size === 'full') {
421 if (
422 isset($data['source_path']) && !empty($data['source_path']) &&
423 isset($data['key']) && !empty($data['key'])
424 ) {
425 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
426 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
427 $server_file = $file_path;
428 }
429 }
430 } else if($size === 'original') {
431 $extras = $this->get_extras($source_id) ? $this->get_extras($source_id) : [];
432 if (isset($extras['original']) && !empty($extras['original'])) {
433 $original = $extras['original'];
434 if(!empty($original)) {
435 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
436 if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) {
437 $server_file = $original_file_path;
438 }
439 }
440 }
441 } else {
442 $extras = $this->get_extras($source_id) ? $this->get_extras($source_id) : [];
443 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
444 $sizes = $extras['sizes'];
445 if(isset($sizes[$size]) && !empty($sizes[$size])) {
446 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sizes[$size]['source_path'];
447 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sizes[$size]['key'], $sub_file_path)) {
448 $server_file = $sub_file_path;
449 }
450 }
451 }
452 }
453 return $server_file;
454 }
455 }
456 return false;
457 }
458
459
460 /**
461 * Ensures only one instance of Class is loaded or can be loaded.
462 *
463 * @return Item Class instance
464 * @since 1.0.0
465 * @static
466 */
467 public static function instance(){
468 if (is_null(self::$instance)) {
469 self::$instance = new self();
470 }
471 return self::$instance;
472 }
473 }