PluginProbe
Media Cloud Sync / 1.2.3
Media Cloud Sync v1.2.3
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.3, at includes/base/item.php

470 lines 16.3 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(WPMCS_ITEM_TABLE, $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 = $wpdb->get_row("SELECT * FROM ".WPMCS_ITEM_TABLE." WHERE source_id = $source_id AND source_type='$source_type'", ARRAY_A);
109
110 if($wpdb->last_error || null === $item || !(isset($item) && !empty($item))) {
111 Cache::update_item_cache($source_id.'_item', '');
112 return false;
113 }
114
115 Utils::update_meta($source_id, 'item', $item);
116 }
117
118 Cache::update_item_cache($source_id.'_item', $item == false ? '' : $item);
119 }
120
121 return $item;
122 }
123
124 /**
125 * Function to delete item from data base
126 * @since 1.0.0
127 */
128 public function delete($source_id){
129 global $wpdb;
130 if (isset($source_id) && !empty($source_id)) {
131 $rows = $wpdb->delete(WPMCS_ITEM_TABLE, array('source_id' => $source_id));
132 if ($wpdb->last_error || false === $rows) {
133 return false;
134 }
135 Utils::delete_meta($source_id, 'item');
136 Cache::delete_item_cache($source_id.'_item');
137 Counter::remove( 'uploaded' );
138 return true;
139 }
140 return false;
141 }
142
143
144 /**
145 * Function to update item in data base
146 * @since 1.0.0
147 */
148 public function update($source_id, $data){
149 global $wpdb;
150 if (isset($source_id) && !empty($source_id)) {
151
152 $data['provider'] = $this->service;
153 $data['region'] = $this->region;
154 $data['storage'] = $this->bucket_name;
155
156 $rows = $wpdb->update(WPMCS_ITEM_TABLE, $data, array('source_id' => $source_id));
157 if ($wpdb->last_error || false === $rows) {
158 return false;
159 }
160
161 Utils::delete_meta($source_id, 'item');
162 Cache::delete_item_cache($source_id.'_item');
163
164 // Reset cache
165 $current_data = $this->get($source_id);
166 return true;
167 }
168 return false;
169 }
170
171 /**
172 * Get extra values of item from database
173 * @since 1.0.0
174 * @param
175 */
176 public function get_extras($source_id, $field = false){
177 $data = $this->get($source_id);
178 if ($data) {
179 if (isset($data['extra']) && !empty($data['extra'])) {
180
181 if(!Utils::is_empty($field)) {
182 $extras = unserialize($data['extra']);
183 return isset($extras[$field]) && !empty($extras[$field]) ? $extras[$field] : false;
184 }
185
186 return unserialize($data['extra']);
187 }
188 }
189 return false;
190 }
191
192
193 /**
194 * Get column of item from database
195 * @since 1.0.0
196 * @param
197 */
198 public function get_field($source_id, $field){
199 $data = $this->get($source_id);
200 if ($data) {
201 if (isset($data[$field]) && !empty($data[$field])) {
202 return $data[$field];
203 }
204 }
205 return false;
206 }
207
208
209 /**
210 * Get backup values of item from database
211 * @since 1.0.0
212 * @param
213 */
214 public function get_backup($source_id){
215 $data = $this->get_extras($source_id);
216 if ($data) {
217 if (isset($data['backup']) && !empty($data['backup'])) {
218 return unserialize($data['backup']);
219 }
220 }
221 return false;
222 }
223
224
225 /**
226 * Checking Item that is served by provider And Is rewrite URL is enabled
227 * @since 1.0.0
228 * @param
229 */
230 public function is_available_from_provider($attachment_id, $check_rewrite = true){
231 $item = $this->get($attachment_id);
232 if(Utils::is_empty($item)) {
233 return false;
234 }
235
236 if ($item['provider'] == $this->service) {
237 if(
238 ($check_rewrite && (isset($this->settings['rewrite_url']) && $this->settings['rewrite_url'])) ||
239 !$check_rewrite
240 ) {
241 return true;
242 }
243 }
244 return false;
245 }
246
247 /**
248 * Get items by paths
249 */
250 public function get_items_by_source_paths( $paths = [] ) {
251 global $wpdb;
252
253 $items = [];
254
255 if (!Utils::is_empty($paths)) {
256 // Ensure paths are properly sanitized and ready for the query
257 $like_conditions = array();
258 foreach ($paths as $path) {
259 // Prepare the SQL conditions for exact match in source_path and partial match in extras column
260 $like_conditions[] = $wpdb->prepare("(source_path = %s OR extra LIKE %s)", $path, '%' . $wpdb->esc_like($path) . '%');
261 }
262
263 // Combine the conditions with OR
264 $where_clause = implode(' OR ', $like_conditions);
265
266 // Execute the SQL query to fetch source_id instead of source_path
267 $sql = "SELECT DISTINCT source_id FROM " . WPMCS_ITEM_TABLE . " WHERE " . $where_clause;
268 $results = $wpdb->get_results($sql, ARRAY_A);
269
270 if ($wpdb->last_error || Utils::is_empty($results)) {
271 return [];
272 }
273
274 $source_ids = array();
275 foreach ($results as $row) {
276 $item = $this->get((int)$row['source_id']);
277 if(!Utils::is_empty($item)) {
278 $items[] = $item;
279 }
280 }
281 return $items;
282 }
283 return $items;
284 }
285
286
287 /**
288 * Get similar existing files like origin path
289 * @since 1.0.0
290 */
291 public function get_similar_files_by_path($path){
292 global $wpdb;
293 if (isset($path) && !empty($path)) {
294 $results = $wpdb->get_results("SELECT source_path FROM " . WPMCS_ITEM_TABLE . " WHERE source_path LIKE '$path%'", ARRAY_A);
295 if ($wpdb->last_error || null === $results || !(isset($results) && !empty($results))) {
296 return false;
297 }
298 $source_paths = array();
299 foreach ($results as $row) {
300 $source_paths[] = $row['source_path'];
301 }
302 return $source_paths;
303 }
304 return false;
305 }
306
307
308 /**
309 * Get service url of item from database
310 * @since 1.0.0
311 * @param
312 */
313 public function get_url($source_id, $size = 'full'){
314 if ($data = $this->get($source_id)) {
315 $extras = $this->get_extras($source_id);
316 $key = '';
317 $url = '';
318 switch($size) {
319 case 'full':
320 $key = $data['key'];
321 $url = $data['url'];
322 break;
323 case 'original':
324 if(
325 isset($extras) && !empty($extras) &&
326 isset($extras['original']) && !empty($extras['original'])
327 ) {
328 $key = $extras['original']['key'];
329 $url = $extras['original']['url'];
330 }
331 default:
332 if(
333 isset($extras) && !empty($extras) &&
334 isset($extras['sizes']) && !empty($extras['sizes']) &&
335 isset($extras['sizes'][$size]) && !empty($extras['sizes'][$size])
336 ) {
337 $key = $extras['sizes'][$size]['key'];
338 $url = $extras['sizes'][$size]['url'];
339 }
340 }
341
342 if(!empty($key)){
343 if (
344 isset($this->settings['enable_presigned']) && $this->settings['enable_presigned'] &&
345 isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire'])
346 ) {
347 $preSignedUrl = Utils::get_meta( $source_id, 'presigned_url_'.$size );
348 if ($preSignedUrl === false) {
349 $new_url = Service::instance()->get_presigned_url($key);
350
351 if (!Utils::is_empty($new_url)) {
352 $preSignedUrl = Cdn::may_generate_cdn_url($new_url, $key);
353 $expireMinutes = (int)(isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire']))
354 ? $this->settings['presigned_expire']
355 : 20;
356 $expireSeconds = $expireMinutes * 60;
357
358 Utils::update_meta($source_id, 'presigned_url_'.$size, $preSignedUrl, false, $expireSeconds);
359 }
360 }
361 return $preSignedUrl;
362 } else {
363 return Cdn::may_generate_cdn_url($url, $key);
364 }
365 }
366 }
367 return false;
368 }
369
370
371 /**
372 * Get service path of item from database
373 * @since 1.0.0
374 * @param
375 */
376 public function moveToServer($source_id, $size = 'full', $all = false){
377 $server_files = array();
378 $server_file = false;
379 $upload_dir = wp_get_upload_dir();
380 $source_id = (int)$source_id;
381
382 if ($data = $this->get($source_id)) {
383 $wpmcsService = Service::instance();
384 if($all) {
385 if (
386 isset($data['source_path']) && !empty($data['source_path']) &&
387 isset($data['key']) && !empty($data['key'])
388 ) {
389 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
390 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
391 $server_files['full'] = $file_path;
392 }
393 }
394 $extras = $this->get_extras($source_id, false) ?: [];
395 if (isset($extras['original']) && !empty($extras['original'])) {
396 $original = $extras['original'];
397 if(!empty($original)) {
398 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
399 if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) {
400 $server_files['original'] = $original_file_path;
401 }
402 }
403 }
404 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
405 $sizes = $extras['sizes'];
406 foreach($sizes as $sub_size => $sub_file) {
407 if(!empty($sub_file)) {
408 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sub_file['source_path'];
409 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sub_file['key'], $sub_file_path)) {
410 $server_files[$sub_size] = $sub_file_path;
411 }
412 }
413 }
414 }
415 return !empty($server_files) ? $server_files : false;
416 } else {
417 if($size === 'full') {
418 if (
419 isset($data['source_path']) && !empty($data['source_path']) &&
420 isset($data['key']) && !empty($data['key'])
421 ) {
422 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
423 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
424 $server_file = $file_path;
425 }
426 }
427 } else if($size === 'original') {
428 $extras = $this->get_extras($source_id) ? $this->get_extras($source_id) : [];
429 if (isset($extras['original']) && !empty($extras['original'])) {
430 $original = $extras['original'];
431 if(!empty($original)) {
432 $original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path'];
433 if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) {
434 $server_file = $original_file_path;
435 }
436 }
437 }
438 } else {
439 $extras = $this->get_extras($source_id) ? $this->get_extras($source_id) : [];
440 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
441 $sizes = $extras['sizes'];
442 if(isset($sizes[$size]) && !empty($sizes[$size])) {
443 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sizes[$size]['source_path'];
444 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sizes[$size]['key'], $sub_file_path)) {
445 $server_file = $sub_file_path;
446 }
447 }
448 }
449 }
450 return $server_file;
451 }
452 }
453 return false;
454 }
455
456
457 /**
458 * Ensures only one instance of Class is loaded or can be loaded.
459 *
460 * @return Item Class instance
461 * @since 1.0.0
462 * @static
463 */
464 public static function instance(){
465 if (is_null(self::$instance)) {
466 self::$instance = new self();
467 }
468 return self::$instance;
469 }
470 }