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

449 lines 15.0 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 return false;
112 }
113
114 Utils::update_meta($source_id, 'item', $item);
115 Cache::update_item_cache($source_id.'_item', $item);
116 } else {
117 Cache::update_item_cache($source_id.'_item', $item);
118 }
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 global $wpmcsService;
350 $new_url = $wpmcsService->get_presigned_url($key);
351
352 if (!Utils::is_empty($new_url)) {
353 $preSignedUrl = Cdn::may_generate_cdn_url($new_url, $key);
354 $expireMinutes = (int)(isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire']))
355 ? $this->settings['presigned_expire']
356 : 20;
357 $expireSeconds = $expireMinutes * 60;
358
359 Utils::update_meta($source_id, 'presigned_url_'.$size, $preSignedUrl, false, $expireSeconds);
360 }
361 }
362 return $preSignedUrl;
363 } else {
364 return Cdn::may_generate_cdn_url($url, $key);
365 }
366 }
367 }
368 return false;
369 }
370
371
372 /**
373 * Get service path of item from database
374 * @since 1.0.0
375 * @param
376 */
377 public function moveToServer($source_id, $size = 'full', $all = false){
378 $server_files = array();
379 $server_file = false;
380 $upload_dir = wp_get_upload_dir();
381 $source_id = (int)$source_id;
382
383 if ($data = $this->get($source_id)) {
384 global $wpmcsService;
385 if($all) {
386 if (
387 isset($data['source_path']) && !empty($data['source_path']) &&
388 isset($data['key']) && !empty($data['key'])
389 ) {
390 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
391 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
392 $server_files['full'] = $file_path;
393 }
394 }
395 $extras = $this->get_extras($source_id) ? $this->get_extras($source_id) : [];
396 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
397 $sizes = $extras['sizes'];
398 if(isset($sizes[$size]) && !empty($sizes[$size])) {
399 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sizes[$size]['source_path'];
400 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sizes[$size]['key'], $sub_file_path)) {
401 $server_files[$size] = $sub_file_path;
402 }
403 }
404 }
405 return !empty($server_files) ? $server_files : false;
406 } else {
407 if($size === 'full') {
408 if (
409 isset($data['source_path']) && !empty($data['source_path']) &&
410 isset($data['key']) && !empty($data['key'])
411 ) {
412 $file_path = trailingslashit($upload_dir['basedir']) . $data['source_path'];
413 if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) {
414 $server_file = $file_path;
415 }
416 }
417 } else {
418 $extras = $this->get_extras($source_id) ? $this->get_extras($source_id) : [];
419 if (isset($extras['sizes']) && !empty($extras['sizes'])) {
420 $sizes = $extras['sizes'];
421 if(isset($sizes[$size]) && !empty($sizes[$size])) {
422 $sub_file_path = trailingslashit($upload_dir['basedir']) . $sizes[$size]['source_path'];
423 if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sizes[$size]['key'], $sub_file_path)) {
424 $server_file = $sub_file_path;
425 }
426 }
427 }
428 }
429 return $server_file;
430 }
431 }
432 return false;
433 }
434
435
436 /**
437 * Ensures only one instance of Class is loaded or can be loaded.
438 *
439 * @return Item Class instance
440 * @since 1.0.0
441 * @static
442 */
443 public static function instance(){
444 if (is_null(self::$instance)) {
445 self::$instance = new self();
446 }
447 return self::$instance;
448 }
449 }