| 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 |
$data = array( |
| 75 |
'provider' => $this->service, |
| 76 |
'region' => $this->region, |
| 77 |
'storage' => $this->bucket_name, |
| 78 |
'source_id' => $source_id, |
| 79 |
'source_path' => $source_path, |
| 80 |
'source_type' => $source_type, |
| 81 |
'url' => $url, |
| 82 |
'key' => $key, |
| 83 |
'is_private' => $is_private, |
| 84 |
'extra' => maybe_serialize($meta), |
| 85 |
); |
| 86 |
|
| 87 |
// Do some pre-update actions |
| 88 |
$this->pre_update_item($source_id, $data, $source_type); |
| 89 |
|
| 90 |
if ($wpdb->insert(Db::get_table_name(), $data)) { |
| 91 |
$item_id = $wpdb->insert_id; |
| 92 |
$data['id'] = $item_id; |
| 93 |
Integration::update_meta($source_id, 'item', $data, false, false, $source_type); |
| 94 |
Cache::update_item_cache($source_id.'_item_'.$source_type, $data); |
| 95 |
Counter::add( 'uploaded', $source_type ); |
| 96 |
} |
| 97 |
|
| 98 |
// Do some post-update actions |
| 99 |
$this->post_update_item($source_id, $data, $source_type); |
| 100 |
|
| 101 |
return $item_id; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Get Item From Db |
| 107 |
*/ |
| 108 |
public function get($source_id, $source_type = 'media_library') { |
| 109 |
global $wpdb; |
| 110 |
$item = Cache::get_item_cache($source_id.'_item_'.$source_type, false); |
| 111 |
if($item === false) { |
| 112 |
$item = Integration::get_meta($source_id, 'item', false, false, false, $source_type); |
| 113 |
if($item == false){ |
| 114 |
$item_table = Db::get_table_name(); |
| 115 |
$item = $wpdb->get_row("SELECT * FROM ".$item_table." WHERE source_id = $source_id AND source_type='$source_type'", ARRAY_A); |
| 116 |
|
| 117 |
if($wpdb->last_error || null === $item || !(isset($item) && !empty($item))) { |
| 118 |
Cache::update_item_cache($source_id.'_item_'.$source_type, ''); |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
Integration::update_meta($source_id, 'item', $item, false, false, $source_type); |
| 123 |
} |
| 124 |
// Update cache |
| 125 |
Cache::update_item_cache($source_id.'_item_'.$source_type, $item == false ? '' : $item); |
| 126 |
} |
| 127 |
|
| 128 |
return !empty($item) ? $item : false; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Function to delete item from data base |
| 133 |
* @since 1.0.0 |
| 134 |
*/ |
| 135 |
public function delete($source_id, $source_type = 'media_library'){ |
| 136 |
global $wpdb; |
| 137 |
if (isset($source_id) && !Utils::is_empty($source_id)) { |
| 138 |
$rows = $wpdb->delete(Db::get_table_name(), array('source_id' => $source_id, 'source_type' => $source_type)); |
| 139 |
if ($wpdb->last_error || false === $rows) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
Integration::delete_meta($source_id, 'item', false, $source_type); |
| 143 |
Cache::delete_item_cache($source_id.'_item_'.$source_type); |
| 144 |
Counter::remove( 'uploaded', $source_type ); |
| 145 |
return true; |
| 146 |
} |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* Function to update item in data base |
| 153 |
* @since 1.0.0 |
| 154 |
*/ |
| 155 |
public function update($source_id, $data, $source_type = 'media_library') { |
| 156 |
global $wpdb; |
| 157 |
if (isset($source_id) && !Utils::is_empty($source_id)) { |
| 158 |
|
| 159 |
$data['provider'] = $this->service; |
| 160 |
$data['region'] = $this->region; |
| 161 |
$data['storage'] = $this->bucket_name; |
| 162 |
|
| 163 |
// Do some pre-update actions |
| 164 |
$this->pre_update_item($source_id, $data, $source_type); |
| 165 |
|
| 166 |
$rows = $wpdb->update(Db::get_table_name(), $data, array('source_id' => $source_id, 'source_type' => $source_type)); |
| 167 |
if ($wpdb->last_error || false === $rows) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
Integration::delete_meta($source_id, 'item', false, $source_type); |
| 172 |
Cache::delete_item_cache($source_id.'_item_'.$source_type); |
| 173 |
|
| 174 |
// Reset cache |
| 175 |
$this->get($source_id, $source_type); |
| 176 |
// Do some post-update actions |
| 177 |
$this->post_update_item($source_id, $data, $source_type); |
| 178 |
|
| 179 |
return true; |
| 180 |
} |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get extra values of item from database |
| 186 |
* @since 1.0.0 |
| 187 |
* @param |
| 188 |
*/ |
| 189 |
public function get_extras($source_id, $field = false, $source_type = 'media_library'){ |
| 190 |
$data = $this->get($source_id, $source_type); |
| 191 |
if ($data) { |
| 192 |
if (isset($data['extra']) && !empty($data['extra'])) { |
| 193 |
|
| 194 |
if(!Utils::is_empty($field)) { |
| 195 |
$extras = Utils::maybe_unserialize($data['extra']); |
| 196 |
return isset($extras[$field]) && !empty($extras[$field]) ? $extras[$field] : false; |
| 197 |
} |
| 198 |
|
| 199 |
return Utils::maybe_unserialize($data['extra']); |
| 200 |
} |
| 201 |
} |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* Get column of item from database |
| 208 |
* @since 1.0.0 |
| 209 |
* @param |
| 210 |
*/ |
| 211 |
public function get_field($source_id, $field, $source_type = 'media_library'){ |
| 212 |
$data = $this->get($source_id, $source_type); |
| 213 |
if ($data) { |
| 214 |
if (isset($data[$field]) && !empty($data[$field])) { |
| 215 |
return $data[$field]; |
| 216 |
} |
| 217 |
} |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Get backup values of item from database |
| 224 |
* @since 1.0.0 |
| 225 |
* @param |
| 226 |
*/ |
| 227 |
public function get_backup($source_id, $source_type = 'media_library'){ |
| 228 |
$data = $this->get_extras($source_id, false, $source_type); |
| 229 |
if ($data) { |
| 230 |
if (isset($data['backup']) && !empty($data['backup'])) { |
| 231 |
return Utils::maybe_unserialize($data['backup']); |
| 232 |
} |
| 233 |
} |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* Checking Item that is served by provider And Is rewrite URL is enabled |
| 240 |
* @since 1.0.0 |
| 241 |
* @param |
| 242 |
*/ |
| 243 |
public function is_available_from_provider($attachment_id, $check_rewrite = true, $source_type = 'media_library') { |
| 244 |
$item = $this->get($attachment_id, $source_type); |
| 245 |
if(Utils::is_empty($item)) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
if ($item['provider'] == $this->service) { |
| 250 |
if( |
| 251 |
($check_rewrite && (isset($this->settings['rewrite_url']) && $this->settings['rewrite_url'])) || |
| 252 |
!$check_rewrite |
| 253 |
) { |
| 254 |
return true; |
| 255 |
} |
| 256 |
} |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get items by paths |
| 262 |
*/ |
| 263 |
public function get_items_by_source_paths( $paths = [] ) { |
| 264 |
global $wpdb; |
| 265 |
|
| 266 |
$items = []; |
| 267 |
|
| 268 |
if (!Utils::is_empty($paths)) { |
| 269 |
// Ensure paths are properly sanitized and ready for the query |
| 270 |
$like_conditions = array(); |
| 271 |
foreach ($paths as $path) { |
| 272 |
// Prepare the SQL conditions for exact match in source_path and partial match in extras column |
| 273 |
$like_conditions[] = $wpdb->prepare("(source_path = %s OR extra LIKE %s)", $path, '%' . $wpdb->esc_like($path) . '%'); |
| 274 |
} |
| 275 |
|
| 276 |
// Combine the conditions with OR |
| 277 |
$where_clause = implode(' OR ', $like_conditions); |
| 278 |
|
| 279 |
// Execute the SQL query to fetch source_id instead of source_path |
| 280 |
$item_table = Db::get_table_name(); |
| 281 |
$sql = "SELECT DISTINCT source_id, source_type FROM " . $item_table . " WHERE " . $where_clause; |
| 282 |
$results = $wpdb->get_results($sql, ARRAY_A); |
| 283 |
|
| 284 |
if ($wpdb->last_error || Utils::is_empty($results)) { |
| 285 |
return []; |
| 286 |
} |
| 287 |
|
| 288 |
$source_ids = array(); |
| 289 |
foreach ($results as $row) { |
| 290 |
$item = $this->get((int)$row['source_id'], $row['source_type']); |
| 291 |
if(!Utils::is_empty($item)) { |
| 292 |
$items[] = $item; |
| 293 |
} |
| 294 |
} |
| 295 |
return $items; |
| 296 |
} |
| 297 |
return $items; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
/** |
| 302 |
* Get similar existing files like origin path |
| 303 |
* @since 1.0.0 |
| 304 |
*/ |
| 305 |
public function get_similar_files_by_path($path){ |
| 306 |
global $wpdb; |
| 307 |
if (isset($path) && !empty($path)) { |
| 308 |
$item_table = Db::get_table_name(); |
| 309 |
$results = $wpdb->get_results("SELECT source_path FROM " . $item_table . " WHERE source_path LIKE '$path%'", ARRAY_A); |
| 310 |
if ($wpdb->last_error || null === $results || !(isset($results) && !empty($results))) { |
| 311 |
return false; |
| 312 |
} |
| 313 |
$source_paths = array(); |
| 314 |
foreach ($results as $row) { |
| 315 |
$source_paths[] = $row['source_path']; |
| 316 |
} |
| 317 |
return $source_paths; |
| 318 |
} |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
/** |
| 324 |
* Get service url of item from database |
| 325 |
* @since 1.0.0 |
| 326 |
* @param |
| 327 |
*/ |
| 328 |
public function get_url($source_id, $size = 'full', $source_type = 'media_library'){ |
| 329 |
if ($data = $this->get($source_id, $source_type)) { |
| 330 |
$extras = $this->get_extras($source_id, false, $source_type) ?: []; |
| 331 |
$key = ''; |
| 332 |
$url = ''; |
| 333 |
switch($size) { |
| 334 |
case 'full': |
| 335 |
$key = $data['key']; |
| 336 |
$url = $data['url']; |
| 337 |
break; |
| 338 |
case 'original': |
| 339 |
if( |
| 340 |
isset($extras) && !empty($extras) && |
| 341 |
isset($extras['original']) && !empty($extras['original']) |
| 342 |
) { |
| 343 |
$key = $extras['original']['key']; |
| 344 |
$url = $extras['original']['url']; |
| 345 |
} |
| 346 |
default: |
| 347 |
if( |
| 348 |
isset($extras) && !empty($extras) && |
| 349 |
isset($extras['sizes']) && !empty($extras['sizes']) && |
| 350 |
isset($extras['sizes'][$size]) && !empty($extras['sizes'][$size]) |
| 351 |
) { |
| 352 |
$key = $extras['sizes'][$size]['key']; |
| 353 |
$url = $extras['sizes'][$size]['url']; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
if(!empty($key)){ |
| 358 |
if ( |
| 359 |
isset($this->settings['enable_presigned']) && $this->settings['enable_presigned'] && |
| 360 |
isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire']) |
| 361 |
) { |
| 362 |
$preSignedUrl = Integration::get_meta( $source_id, 'presigned_url_'.$size, false, false, false, $source_type ); |
| 363 |
if ($preSignedUrl === false) { |
| 364 |
$new_url = Service::instance()->get_presigned_url($key); |
| 365 |
|
| 366 |
if (!Utils::is_empty($new_url)) { |
| 367 |
$preSignedUrl = Cdn::may_generate_cdn_url($new_url, $key); |
| 368 |
$expireMinutes = (int)(isset($this->settings['presigned_expire']) && !empty($this->settings['presigned_expire'])) |
| 369 |
? $this->settings['presigned_expire'] |
| 370 |
: 20; |
| 371 |
$expireSeconds = $expireMinutes * 60; |
| 372 |
|
| 373 |
Integration::update_meta($source_id, 'presigned_url_'.$size, $preSignedUrl, false, $expireSeconds, $source_type); |
| 374 |
} |
| 375 |
} |
| 376 |
return $preSignedUrl; |
| 377 |
} else { |
| 378 |
return Cdn::may_generate_cdn_url($url, $key); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
return false; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
/** |
| 387 |
* Get service path of item from database |
| 388 |
* @since 1.0.0 |
| 389 |
* @param |
| 390 |
*/ |
| 391 |
public function moveToServer($source_id, $size = 'full', $all = false, $source_type = 'media_library') { |
| 392 |
$server_files = array(); |
| 393 |
$server_file = false; |
| 394 |
$upload_dir = wp_get_upload_dir(); |
| 395 |
$source_id = (int)$source_id; |
| 396 |
|
| 397 |
if ($data = $this->get($source_id, $source_type)) { |
| 398 |
$wpmcsService = Service::instance(); |
| 399 |
if($all) { |
| 400 |
if ( |
| 401 |
isset($data['source_path']) && !empty($data['source_path']) && |
| 402 |
isset($data['key']) && !empty($data['key']) |
| 403 |
) { |
| 404 |
$file_path = trailingslashit($upload_dir['basedir']) . $data['source_path']; |
| 405 |
if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) { |
| 406 |
$server_files['full'] = $file_path; |
| 407 |
} |
| 408 |
} |
| 409 |
$extras = $this->get_extras($source_id, false, $source_type) ?: []; |
| 410 |
if (isset($extras['original']) && !empty($extras['original'])) { |
| 411 |
$original = $extras['original']; |
| 412 |
if(!empty($original)) { |
| 413 |
$original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path']; |
| 414 |
if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) { |
| 415 |
$server_files['original'] = $original_file_path; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
if (isset($extras['sizes']) && !empty($extras['sizes'])) { |
| 420 |
$sizes = $extras['sizes']; |
| 421 |
foreach($sizes as $sub_size => $sub_file) { |
| 422 |
if(!empty($sub_file)) { |
| 423 |
$sub_file_path = trailingslashit($upload_dir['basedir']) . $sub_file['source_path']; |
| 424 |
if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sub_file['key'], $sub_file_path)) { |
| 425 |
$server_files[$sub_size] = $sub_file_path; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
return !empty($server_files) ? $server_files : false; |
| 431 |
} else { |
| 432 |
if($size === 'full') { |
| 433 |
if ( |
| 434 |
isset($data['source_path']) && !empty($data['source_path']) && |
| 435 |
isset($data['key']) && !empty($data['key']) |
| 436 |
) { |
| 437 |
$file_path = trailingslashit($upload_dir['basedir']) . $data['source_path']; |
| 438 |
if(file_exists($file_path) || $wpmcsService->object_to_server($data['key'], $file_path)) { |
| 439 |
$server_file = $file_path; |
| 440 |
} |
| 441 |
} |
| 442 |
} else if($size === 'original') { |
| 443 |
$extras = $this->get_extras($source_id, false, $source_type) ?: []; |
| 444 |
if (isset($extras['original']) && !empty($extras['original'])) { |
| 445 |
$original = $extras['original']; |
| 446 |
if(!empty($original)) { |
| 447 |
$original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path']; |
| 448 |
if(file_exists($original_file_path) || $wpmcsService->object_to_server($original['key'], $original_file_path)) { |
| 449 |
$server_file = $original_file_path; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} else { |
| 454 |
$extras = $this->get_extras($source_id, false, $source_type) ?: []; |
| 455 |
if (isset($extras['sizes']) && !empty($extras['sizes'])) { |
| 456 |
$sizes = $extras['sizes']; |
| 457 |
if(isset($sizes[$size]) && !empty($sizes[$size])) { |
| 458 |
$sub_file_path = trailingslashit($upload_dir['basedir']) . $sizes[$size]['source_path']; |
| 459 |
if(file_exists($sub_file_path) || $wpmcsService->object_to_server($sizes[$size]['key'], $sub_file_path)) { |
| 460 |
$server_file = $sub_file_path; |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
return $server_file; |
| 466 |
} |
| 467 |
} |
| 468 |
return false; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
/** |
| 473 |
* Get service path of item from database by source url |
| 474 |
* @since 1.0.0 |
| 475 |
* @param |
| 476 |
*/ |
| 477 |
public function moveToServerBySourcePath($source_id, $file, $source_type = 'media_library'){ |
| 478 |
$server_files = array(); |
| 479 |
$server_file = false; |
| 480 |
$upload_dir = wp_get_upload_dir(); |
| 481 |
$source_id = (int)$source_id; |
| 482 |
|
| 483 |
if ($data = $this->get($source_id, $source_type)) { |
| 484 |
$source_path = Utils::get_attachment_source_path($file); |
| 485 |
|
| 486 |
if( isset($data['source_path']) && !empty($data['source_path']) && $data['source_path'] == $source_path ) { |
| 487 |
$file_path = trailingslashit($upload_dir['basedir']) . $data['source_path']; |
| 488 |
if(file_exists($file_path) || Service::instance()->object_to_server($data['key'], $file_path)) { |
| 489 |
return true; |
| 490 |
} |
| 491 |
} else { |
| 492 |
// Check in extras |
| 493 |
$extras = $this->get_extras($source_id, false, $source_type) ?: []; |
| 494 |
if (isset($extras['original']) && !empty($extras['original']) && $extras['original']['source_path'] == $source_path) { |
| 495 |
$original = $extras['original']; |
| 496 |
if(!empty($original)) { |
| 497 |
$original_file_path = trailingslashit($upload_dir['basedir']) . $original['source_path']; |
| 498 |
if(file_exists($original_file_path) || Service::instance()->object_to_server($original['key'], $original_file_path)) { |
| 499 |
return true; |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
// Check in sizes |
| 505 |
$sizes = isset($extras['sizes']) && !empty($extras['sizes']) ? $extras['sizes'] : []; |
| 506 |
if(isset($sizes) && !empty($sizes)) { |
| 507 |
foreach($sizes as $sub_size => $sub_file) { |
| 508 |
if(!empty($sub_file) && $sub_file['source_path'] == $source_path) { |
| 509 |
$sub_file_path = trailingslashit($upload_dir['basedir']) . $sub_file['source_path']; |
| 510 |
if(file_exists($sub_file_path) || Service::instance()->object_to_server($sub_file['key'], $sub_file_path)) { |
| 511 |
return true; |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
return false; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
/** |
| 523 |
* Move original file to server |
| 524 |
* |
| 525 |
* @param int $source_id |
| 526 |
* @param string $source_type |
| 527 |
* @return bool |
| 528 |
*/ |
| 529 |
public function moveOriginalToServer($source_id, $source_type = 'media_library') { |
| 530 |
$data = $this->get($source_id, $source_type); |
| 531 |
if ($data) { |
| 532 |
$size = 'full'; |
| 533 |
$original = $this->get_extras($source_id, 'original', $source_type); |
| 534 |
if ($original && isset($original['key']) && !empty($original['key'])) { |
| 535 |
$size = 'original'; |
| 536 |
} |
| 537 |
return $this->moveToServer($source_id, $size, false, $source_type); |
| 538 |
} |
| 539 |
return false; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
/** |
| 544 |
* Delete media item |
| 545 |
*/ |
| 546 |
public function delete_attachments_by_item($item, $delete_backup = true) { |
| 547 |
$upload_dir = wp_get_upload_dir(); |
| 548 |
|
| 549 |
if (isset($item['extra']) && !empty($item['extra'])) { |
| 550 |
$extras = Utils::maybe_unserialize($item['extra']); |
| 551 |
if ( |
| 552 |
isset($extras) && !empty($extras) && |
| 553 |
isset($extras['sizes']) && !empty($extras['sizes']) |
| 554 |
) { |
| 555 |
foreach ($extras['sizes'] as $sub_image) { |
| 556 |
if (isset($sub_image['key']) && !empty($sub_image['key'])) { |
| 557 |
Service::instance()->deleteSingle($sub_image['key']); |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
if ( |
| 563 |
isset($extras) && !empty($extras) && |
| 564 |
isset($extras['original']) && !empty($extras['original']) |
| 565 |
) { |
| 566 |
Service::instance()->deleteSingle($extras['original']['key']); |
| 567 |
} |
| 568 |
|
| 569 |
if ( |
| 570 |
isset($extras) && !empty($extras) && |
| 571 |
isset($extras['backup']) && !empty($extras['backup']) && |
| 572 |
$delete_backup |
| 573 |
) { |
| 574 |
$backup = Utils::maybe_unserialize($extras['backup']); |
| 575 |
if (isset($backup) && !empty($backup)) { |
| 576 |
$this->delete_attachments_by_item($backup, false); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
if (isset($item['key']) && !empty($item['key'])) { |
| 581 |
Service::instance()->deleteSingle($item['key']); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
/** |
| 587 |
* Pre-update item actions |
| 588 |
* @since 1.2.13 |
| 589 |
* @param int $source_id |
| 590 |
* @param array $data |
| 591 |
* @param string $source_type |
| 592 |
*/ |
| 593 |
public function pre_update_item($source_id, $data, $source_type = 'media_library') { |
| 594 |
// Hook for pre-update actions |
| 595 |
do_action('wpmcs_pre_update_item', $source_id, $data, $source_type); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
/** |
| 600 |
* Post-update item actions |
| 601 |
* @since 1.2.13 |
| 602 |
* @param int $source_id |
| 603 |
* @param array $data |
| 604 |
* @param string $source_type |
| 605 |
* This function is called after an item has been updated in the database. |
| 606 |
* It triggers a WordPress action hook 'wpmcs_post_update_item' to allow other functions to hook into this event. |
| 607 |
* After that, it calls may_be_delete_server_files_by_id to potentially delete server files associated with the item. |
| 608 |
* |
| 609 |
* @example |
| 610 |
* $item = Item::instance(); |
| 611 |
* $item->post_update_item(123, $data, 'media_library'); |
| 612 |
* |
| 613 |
* This example will trigger the post-update actions for the item with ID 123. |
| 614 |
* It will execute any functions hooked to 'wpmcs_post_update_item' and may delete server files if the settings allow it. |
| 615 |
*/ |
| 616 |
public function post_update_item($source_id, $data, $source_type = 'media_library') { |
| 617 |
// Hook for post-update actions |
| 618 |
do_action('wpmcs_post_update_item', $source_id, $data, $source_type); |
| 619 |
|
| 620 |
$this->may_be_delete_server_files_by_id($source_id, $source_type, true, true); |
| 621 |
} |
| 622 |
|
| 623 |
/** |
| 624 |
* Function to remove media from server by id |
| 625 |
* @param int $attachment_id |
| 626 |
* @param string $source_type |
| 627 |
* @param bool $delete_main_file |
| 628 |
* @param bool $delete_backup |
| 629 |
* |
| 630 |
* This function checks if the item exists and if the setting to remove from server is enabled. |
| 631 |
* If so, it deletes the main file and any backup files associated with the item. |
| 632 |
* It also checks if the item has any extra data, and if so, it attempts to delete the backup files if specified. |
| 633 |
* Finally, it deletes the main file associated with the item. |
| 634 |
* |
| 635 |
* @since 1.2.13 |
| 636 |
* @return bool Returns true if the deletion process was initiated, false otherwise. |
| 637 |
* |
| 638 |
* @throws \Exception If the item does not exist or if the removal from server setting is not enabled. |
| 639 |
* |
| 640 |
* @example |
| 641 |
* $item = Item::instance(); |
| 642 |
* $item->may_be_delete_server_files_by_id(123, 'media_library', true, true); |
| 643 |
* |
| 644 |
* This example will attempt to delete the server files for the attachment with ID 123, |
| 645 |
* including the main file and any backup files, if the settings allow it. |
| 646 |
* |
| 647 |
* @see Item::may_be_delete_server_files_by_item() for the function that actually performs the deletion. |
| 648 |
*/ |
| 649 |
public function may_be_delete_server_files_by_id($attachment_id, $source_type = 'media_library', $delete_main_file=false, $delete_backup = false) { |
| 650 |
$item = $this->get($attachment_id, $source_type); |
| 651 |
if(Utils::is_empty($item)) { |
| 652 |
return false; |
| 653 |
} |
| 654 |
|
| 655 |
if( !( isset($this->settings['remove_from_server']) && $this->settings['remove_from_server'] ) ) { |
| 656 |
return false; |
| 657 |
} |
| 658 |
|
| 659 |
if (isset($item['extra']) && !empty($item['extra'])) { |
| 660 |
$extras = Utils::maybe_unserialize($item['extra']); |
| 661 |
if ( |
| 662 |
isset($extras) && !empty($extras) && |
| 663 |
isset($extras['backup']) && !empty($extras['backup']) && |
| 664 |
$delete_backup |
| 665 |
) { |
| 666 |
$backup = Utils::maybe_unserialize($extras['backup']); |
| 667 |
if (isset($backup) && !empty($backup)) { |
| 668 |
$this->may_be_delete_server_files_by_item($backup, $delete_main_file); |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
$this->may_be_delete_server_files_by_item($item, $delete_main_file, $delete_backup); |
| 674 |
|
| 675 |
return true; |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Function to remove media from server by item |
| 680 |
*/ |
| 681 |
public function may_be_delete_server_files_by_item( $item, $delete_main_file=false ) { |
| 682 |
if(Utils::is_empty($item)) { |
| 683 |
return false; |
| 684 |
} |
| 685 |
|
| 686 |
if( !( isset($this->settings['remove_from_server']) && $this->settings['remove_from_server'] ) ) { |
| 687 |
return false; |
| 688 |
} |
| 689 |
|
| 690 |
$upload_dir = wp_get_upload_dir(); |
| 691 |
$has_original = false; |
| 692 |
$files_to_remove = array(); |
| 693 |
|
| 694 |
$file_path = trailingslashit($upload_dir['basedir']) . $item['source_path']; |
| 695 |
|
| 696 |
if (isset($item['extra']) && !empty($item['extra'])) { |
| 697 |
$extras = Utils::maybe_unserialize($item['extra']); |
| 698 |
if ( |
| 699 |
isset($extras) && !empty($extras) && |
| 700 |
isset($extras['sizes']) && !empty($extras['sizes']) |
| 701 |
) { |
| 702 |
foreach ($extras['sizes'] as $sub_image) { |
| 703 |
if (isset($sub_image['source_path']) && !empty($sub_image['source_path'])) { |
| 704 |
$file = trailingslashit($upload_dir['basedir']) . $sub_image['source_path']; |
| 705 |
if(file_exists($file)) { |
| 706 |
$files_to_remove[] = $file; |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
if ( |
| 713 |
isset($extras) && !empty($extras) && |
| 714 |
isset($extras['original']) && !empty($extras['original']) |
| 715 |
) { |
| 716 |
$has_original = true; |
| 717 |
$file = trailingslashit($upload_dir['basedir']).$extras['original']['source_path']; |
| 718 |
if(file_exists($file) && $delete_main_file) { |
| 719 |
$files_to_remove[] = $file; |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
if(file_exists($file_path)) { |
| 724 |
if ($has_original || (!$has_original && $delete_main_file)) { |
| 725 |
$files_to_remove[] = $file_path; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
$files_to_remove = apply_filters('wpmcs_files_to_remove_from_server', array_unique($files_to_remove), $item['source_id'], $item); |
| 731 |
|
| 732 |
if (!Utils::is_empty($files_to_remove)) { |
| 733 |
foreach ($files_to_remove as $file) { |
| 734 |
wp_delete_file($file, true); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
return true; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
/** |
| 743 |
* Ensures only one instance of Class is loaded or can be loaded. |
| 744 |
* |
| 745 |
* @return Item Class instance |
| 746 |
* @since 1.0.0 |
| 747 |
* @static |
| 748 |
*/ |
| 749 |
public static function instance(){ |
| 750 |
if (is_null(self::$instance)) { |
| 751 |
self::$instance = new self(); |
| 752 |
} |
| 753 |
return self::$instance; |
| 754 |
} |
| 755 |
} |