-
Notifications
You must be signed in to change notification settings - Fork 61
/
class-db.php
930 lines (771 loc) · 25.5 KB
/
class-db.php
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
<?php
/**
* DB
*
* @since 4.0.0
*/
namespace wpCloud\StatelessMedia {
if (!class_exists('wpCloud\StatelessMedia\DB')) {
class DB {
use Singleton;
const DB_VERSION_KEY = 'sm_db_version';
const DB_VERSION = '1.0';
const FULL_SIZE = '__full';
/**
* @var \WPDB
*/
private $wpdb;
/**
* Bucket link
*/
private $bucket_link = '';
/**
* Files table name
*/
private $files = '';
/**
* File sizes table name
*/
private $file_sizes = '';
/**
* File meta table name
*/
private $file_meta = '';
/**
* Sync table name
*/
private $sm_sync = '';
/**
* Cache group name
*/
private $cache_group = 'stateless_media';
/**
* Files table fields mapping, used for backward compatibility with old postmeta
*/
private $file_mapping = [
'id' => 'id',
'post_id' => 'post_id',
'bucket' => 'bucket',
'name' => 'name',
'generation' => 'generation',
'cacheControl' => 'cache_control',
'contentType' => 'content_type',
'contentDisposition' => 'content_disposition',
'filesize' => 'file_size',
'width' => 'width',
'height' => 'height',
'stateless_version' => 'stateless_version',
'storageClass' => 'storage_class',
'fileLink' => 'file_link',
'selfLink' => 'self_link',
];
/**
* Files sizes table fields mapping, used for backward compatibility with old postmeta
*/
private $file_sizes_mapping = [
'id' => 'id',
'post_id' => 'post_id',
'size_name' => 'size_name',
'name' => 'name',
'generation' => 'generation',
'filesize' => 'file_size',
'width' => 'width',
'height' => 'height',
'fileLink' => 'file_link',
'selfLink' => 'self_link',
];
protected function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
$this->files = $this->wpdb->prefix . 'stateless_files';
$this->file_sizes = $this->wpdb->prefix . 'stateless_file_sizes';
$this->file_meta = $this->wpdb->prefix . 'stateless_file_meta';
$this->sm_sync = $this->wpdb->prefix . 'sm_sync';
$image_host = ud_get_stateless_media()->get_gs_host();
$this->bucket_link = apply_filters('wp_stateless_bucket_link', $image_host);
if ( is_multisite() ) {
$this->cache_group = implode('_', [
$this->cache_group,
get_current_blog_id(),
]);
}
$this->_init();
}
/**
* Init hooks
*/
private function _init() {
add_filter('wp_stateless_generate_cloud_meta', [$this, 'process_cloud_meta'], 10, 5);
add_action('deleted_post', [$this, 'delete_post'], 10, 2);
add_filter('wp_stateless_get_file', [$this, 'get_file'], 10, 3);
add_filter('wp_stateless_get_file_sizes', [$this, 'get_file_sizes'], 10, 2);
add_filter('wp_stateless_get_file_meta', [$this, 'get_file_meta'], 10, 2);
add_filter('wp_stateless_get_file_meta_value', [$this, 'get_file_meta_value'], 10, 4);
add_action('wp_stateless_set_file', [$this, 'set_file'], 10, 2);
add_action('wp_stateless_set_file_size', [$this, 'set_file_size'], 10, 3);
add_action('wp_stateless_set_file_meta', [$this, 'update_file_meta'], 10, 3);
}
/**
* Getters
*/
public function __get($property) {
if ( in_array($property, ['files', 'file_sizes', 'file_meta', 'sm_sync']) ) {
return $this->$property;
}
}
/**
* Creates or updates DB structure
*/
public function create_db() {
$version = get_option(self::DB_VERSION_KEY, '');
if ($version === self::DB_VERSION) {
return;
}
$charset_collate = $this->wpdb->get_charset_collate();
$sql = "CREATE TABLE $this->files (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NULL DEFAULT NULL,
`bucket` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`generation` bigint(16) NOT NULL,
`cache_control` varchar(255) NULL DEFAULT NULL,
`content_type` varchar(255) NULL DEFAULT NULL,
`content_disposition` varchar(100) NULL DEFAULT NULL,
`file_size` bigint(20) unsigned NULL DEFAULT NULL,
`width` int unsigned NULL DEFAULT NULL,
`height` int unsigned NULL DEFAULT NULL,
`stateless_version` varchar(20) NOT NULL,
`storage_class` varchar(50) NULL DEFAULT NULL,
`file_link` text NOT NULL,
`self_link` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY post_id (post_id)
) $charset_collate;
CREATE TABLE $this->file_sizes (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NULL DEFAULT NULL,
`size_name` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`generation` bigint(16) NOT NULL,
`file_size` bigint(20) unsigned NULL DEFAULT NULL,
`width` int unsigned NULL DEFAULT NULL,
`height` int unsigned NULL DEFAULT NULL,
`file_link` text NOT NULL,
`self_link` text NOT NULL,
PRIMARY KEY (`id`),
KEY post_id (post_id),
UNIQUE KEY post_id_size (post_id, size_name)
) $charset_collate;
CREATE TABLE $this->file_meta (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NULL DEFAULT NULL,
`meta_key` varchar(255) NOT NULL,
`meta_value` longtext NOT NULL,
PRIMARY KEY (`id`),
KEY post_id (post_id),
UNIQUE KEY post_id_key (post_id, meta_key)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option(self::DB_VERSION_KEY, self::DB_VERSION);
}
/**
* Remove custom DB table on plugin uninstall
*
* @param int $site_id
*/
public function clear_db($site_id) {
switch_to_blog($id);
$tables = array(
$this->wpdb->prefix . 'files',
$this->wpdb->prefix . 'files_sizes',
$this->wpdb->prefix . 'file_meta',
$this->wpdb->prefix . 'sm_sync',
);
$tables = implode(', ', $tables);
$sql = "DROP TABLE IF EXISTS $tables;";
$this->wpdb->query($sql);
restore_current_blog();
}
/**
* Get file link
*
* @param string $name
* @return string
*/
public function get_file_link($name) {
return trailingslashit($this->bucket_link) . $name;
}
/**
* Get file row ID by post ID
*
* @param int $post_id
* @return int | null
*/
public function get_file_id($post_id) {
$sql = "SELECT id FROM $this->files WHERE post_id = %d";
$id = $this->wpdb->get_var( $this->wpdb->prepare($sql, $post_id) );
return $id ? (int) $id : null;
}
/**
* Get file size row ID by post ID and size name
*
* @param int $post_id
* @param string $size
* @return int | null
*/
public function get_file_size_id($post_id, $size) {
$sql = "SELECT id FROM $this->file_sizes WHERE post_id = %d AND size_name = %s";
$id = $this->wpdb->get_var( $this->wpdb->prepare($sql, $post_id, $size) );
return $id ? (int) $id : null;
}
/**
* Get file meta row ID by post ID and meta key
*
* @param int $post_id
* @param string $key
* @return int | null
*/
public function get_file_meta_id($post_id, $key) {
$key = sanitize_key($key);
$sql = "SELECT id FROM $this->file_meta WHERE post_id = %d AND meta_key = %s";
$id = $this->wpdb->get_var( $this->wpdb->prepare($sql, $post_id, $key) );
return $id ? (int) $id : null;
}
/**
* Add file data to DB
*
* @param array $cloud_meta - generated WP postmeta
* @param array $media - GCS media object
* @param string $image_size - image size name
* @param array $img - image (or image size) data for upload
* @param string $bucketLink - bucket link
* @return mixed
*/
public function process_cloud_meta($cloud_meta, $media, $image_size, $img, $bucketLink) {
$this->update_data($media);
return $cloud_meta;
}
/**
* Determine which data to update and run updates
*
* @param array $media - GCS media object
*/
public function update_data($media) {
$error = false;
if ( !isset($media['name']) || !isset($media['metadata']) || !isset($media['metadata']['size']) ) {
$error = 'Metadata missing or incorrect for GCS media object';
} else {
$size = $media['metadata']['size'];
if ( $size == self::FULL_SIZE ) {
$attachment_id = isset($media['metadata']['object-id']) ? $media['metadata']['object-id'] : null;
if ( !$attachment_id ) {
$error = 'Unable to get attachment ID for GCS media object';
} else {
$this->_update_file(
$attachment_id,
$this->_get_file_from_media($media),
);
}
} else {
$attachment_id = isset($media['metadata']['child-of']) ? $media['metadata']['child-of'] : null;
if ( !$attachment_id ) {
$error = 'Unable to get parent attachment ID for GCS media object';
} else {
$this->_update_file_size(
$attachment_id,
$size,
$this->_get_file_size_from_media($media),
);
}
}
}
if ( $error ) {
Helper::log($error);
Helper::log($media, true);
}
}
/**
* Update file data, based on the mapping received from the media object
*
* @param int $attachment_id
* @param array $data
* @return int | null
*/
public function set_file($attachment_id, $data) {
$result = [];
foreach ($this->file_mapping as $key => $mapping) {
if ( isset($data[$key]) ) {
$result[$mapping] = $data[$key];
continue;
}
if ( isset($data[$mapping]) ) {
$result[$mapping] = $data[$mapping];
continue;
}
}
unset($result['id']);
return $this->_update_file($attachment_id, $result);
}
/**
* Update file size data
*
* @param int $attachment_id
* @param string $size_name
* @param array $data
* @return int | null
*/
public function set_file_size($attachment_id, $size_name, $data) {
$result = [];
foreach ($this->file_sizes_mapping as $key => $mapping) {
if ( isset($data[$key]) ) {
$result[$mapping] = $data[$key];
continue;
}
if ( isset($data[$mapping]) ) {
$result[$mapping] = $data[$mapping];
continue;
}
}
unset($result['id']);
return $this->_update_file_size($attachment_id, $size_name, $result);
}
/**
* Convert GSC media object into stateless file data
*
* @param array $media
* @return array
*/
private function _get_file_from_media($media) {
$name = $media['name'];
$data =[
'bucket' => $media['bucket'] ?? '',
'name' => $name,
'generation' => $media['generation'] ?? '',
'cache_control' => $media['cacheControl'] ?? null,
'content_type' => $media['contentType'] ?? null,
'content_disposition' => $media['contentDisposition'] ?? null,
'file_size' => $media['size'] ?? null,
'width' => $media['metadata']['width'] ?? null,
'height' => $media['metadata']['height'] ?? null,
'stateless_version' => get_option('wp_sm_version', false),
'storage_class' => $media['storageClass'] ?? null,
'file_link' => $this->get_file_link($name),
'self_link' => $media['selfLink'] ?? '',
];
return $data;
}
/**
* Update file data
*
* @param int $attachment_id
* @param array $data
* @return int | null
*/
private function _update_file($attachment_id, $data) {
$file_id = $this->get_file_id($attachment_id);
if ( $file_id ) {
$this->wpdb->update(
$this->files,
$data,
['id' => $file_id]
);
} else {
$data['post_id'] = $attachment_id;
$this->wpdb->insert( $this->files, $data );
$file_id = $this->wpdb->insert_id;
}
$this->_delete_file_cache($attachment_id);
return $file_id;
}
/**
* Convert GSC media object into stateless file size data
*
* @param array $media
* @return array
*/
private function _get_file_size_from_media($media) {
$name = $media['name'];
$data =[
'name' => $name,
'generation' => $media['generation'] ?? '',
'file_size' => $media['size'] ?? null,
'width' => $media['metadata']['width'] ?? null,
'height' => $media['metadata']['height'] ?? null,
'file_link' => $this->get_file_link($name),
'self_link' => $media['selfLink'] ?? '',
];
return $data;
}
/**
* Update file size data
*
* @param int $attachment_id
* @param string $size_name
* @param array $media
* @return int | null
*/
private function _update_file_size($attachment_id, $size_name, $data) {
$file_size_id = $this->get_file_size_id($attachment_id, $size_name);
if ( $file_size_id ) {
$this->wpdb->update(
$this->file_sizes,
$data,
['id' => $file_size_id]
);
} else {
$data['post_id'] = $attachment_id;
$data['size_name'] = $size_name;
$this->wpdb->insert( $this->file_sizes, $data );
$file_size_id = $this->wpdb->insert_id;
}
$this->_delete_file_sizes_cache($attachment_id);
return $file_size_id;
}
/**
* Delete file data when attachment is deleted
*
* @param int $post_id
* @param \WP_Post $post
*/
public function delete_post($post_id, $post) {
$file_id = $this->get_file_id($post_id);
if ( !$file_id ) {
return;
}
$this->wpdb->delete(
$this->files,
['post_id' => $post_id]
);
$this->wpdb->delete(
$this->file_sizes,
['post_id' => $post_id]
);
$this->wpdb->delete(
$this->file_meta,
['post_id' => $post_id]
);
$this->_delete_attachment_cache($post_id);
}
/**
* Map requested fields into database query< compatible with the old post meta structure
*
* @param array $fields
* @param array $fields_mapping
* @return string
*/
private function _map_fields($fields, $fields_mapping) {
$mapped = [];
$result = [];
if ( !is_array($fields) ) {
$fields = [$fields];
}
if (empty($fields)) {
$fields = array_keys($fields_mapping);
}
foreach ($fields as $key) {
if ( isset($fields_mapping[$key]) ) {
$mapped[$key] = $fields_mapping[$key];
}
}
foreach ($mapped as $key => $value) {
$result[] = "$value AS $key";
}
return implode(', ', $result);
}
/**
* Get cache key for file data
*
* @param int $post_id
* @return string
*/
private function _get_file_cache_key($post_id) {
return implode('_', ['file', $post_id]);
}
/**
* Get cache key for file sizes data
*
* @param int $post_id
* @return string
*/
private function _get_file_sizes_cache_key($post_id) {
return implode('_', ['file_sizes', $post_id]);
}
/**
* Get cache group for file meta data
*
* @param int $post_id
* @return string
*/
private function _get_file_meta_cache_group($post_id) {
return implode('_', [$this->cache_group, 'meta', $post_id]);
}
/**
* Get cache key for file meta data
*
* @param int $post_id
* @param string $key
* @return string
*/
private function _get_file_meta_cache_key($post_id, $key) {
return implode('_', [$key, $post_id]);
}
/**
* Get cache for file meta
*
* @param int $post_id
* @param string $key
* @param bool $found
* @return mixed
*/
private function _get_file_meta_cache($post_id, $key, &$found) {
return wp_cache_get(
$this->_get_file_meta_cache_key($post_id, $key),
$this->_get_file_meta_cache_group($post_id),
false,
$found
);
}
/**
* Set cache for file meta
*
* @param int $post_id
* @param string $key
* @param mixed $value
* @return bool
*/
private function _set_file_meta_cache($post_id, $key, $value) {
return wp_cache_set(
$this->_get_file_meta_cache_key($post_id, $key),
$value,
$this->_get_file_meta_cache_group($post_id)
);
}
/**
* Delete file cache
*
* @param int $attachment_id
*/
private function _delete_file_cache($attachment_id) {
wp_cache_delete( $this->_get_file_cache_key($attachment_id), $this->cache_group );
}
/**
* Delete file sizes cache
*
* @param int $attachment_id
*/
private function _delete_file_sizes_cache($attachment_id) {
wp_cache_delete( $this->_get_file_sizes_cache_key($attachment_id), $this->cache_group );
}
/**
* Delete file meta cache for single key
*
* @param int $attachment_id
* @param string $key
*/
private function _delete_file_meta_key_cache($attachment_id, $key) {
wp_cache_delete(
$this->_get_file_meta_cache_key($attachment_id, $key),
$this->_get_file_meta_cache_group($attachment_id)
);
}
/**
* Delete all file meta cache
*
* @param int $attachment_id
*/
private function _delete_file_meta_cache($attachment_id) {
wp_cache_flush_group( $this->_get_file_meta_cache_group($attachment_id) );
}
/**
* Delete all attachment cache
*
* @param int $attachment_id
*/
private function _delete_attachment_cache($attachment_id) {
$this->_delete_file_cache($attachment_id);
$this->_delete_file_sizes_cache($attachment_id);
$this->_delete_file_meta_cache($attachment_id);
}
/**
* Get the total files count known to WP-Stateless
*
* @return int
*/
public function get_total_files() {
global $wpdb;
try {
$query = "SELECT COUNT(id) FROM $this->files";
return $wpdb->get_var($query);
} catch (\Throwable $e) {
return 0;
}
}
/**
* Get the total file sizes count known to WP-Stateless
*
* @return int
*/
public function get_total_file_sizes() {
global $wpdb;
try {
$query = "SELECT COUNT(id) FROM $this->file_sizes";
return $wpdb->get_var($query);
} catch (\Throwable $e) {
return 0;
}
}
/**
* Get file data. If $with_sizes is set to true, all sizes will be included
*
* @param array $meta
* @param int $attachment_id
* @param bool $with_sizes
* @return array
*/
public function get_file($meta, $attachment_id, $with_sizes = false) {
if ( ud_get_stateless_media()->get('sm.use_postmeta') ) {
$meta = get_post_meta($attachment_id, 'sm_cloud', true);
if ( !empty($meta) ) {
return $meta;
}
}
// Get values from the cache
$cache_key = $this->_get_file_cache_key($attachment_id);
$meta = wp_cache_get($cache_key, $this->cache_group, false, $found);
if ( $found ) {
return $meta;
}
// Get values from the DB
$fields = $this->_map_fields([], $this->file_mapping);
$sql = "SELECT $fields FROM $this->files WHERE post_id = %d";
$meta = $this->wpdb->get_row( $this->wpdb->prepare($sql, $attachment_id), ARRAY_A );
if ( empty($meta) ) {
return get_post_meta($attachment_id, 'sm_cloud', true);
}
wp_cache_set($cache_key, $meta, $this->cache_group);
// Get file size meta data
if ( $with_sizes ) {
$meta['sizes'] = apply_filters('wp_stateless_get_file_sizes', [], $attachment_id);
}
return $meta;
}
/**
* Get file sizes data
*
* @param array $sizes
* @param int $attachment_id
* @return array
*/
public function get_file_sizes($sizes, $attachment_id) {
if ( ud_get_stateless_media()->get('sm.use_postmeta') ) {
$meta = get_post_meta($attachment_id, 'sm_cloud', true);
return isset($meta['sizes']) ? $meta['sizes'] : [];
}
// Get values from the cache
$cache_key = $this->_get_file_sizes_cache_key($attachment_id);
$sizes = wp_cache_get($cache_key, $this->cache_group, false, $found);
if ( $found ) {
return $sizes;
}
// Get values from the DB
$fields = $this->_map_fields([], $this->file_sizes_mapping);
$sql = "SELECT $fields FROM $this->file_sizes WHERE post_id = %d";
$sql = $this->wpdb->prepare($sql, $attachment_id);
$result = $this->wpdb->get_results( $this->wpdb->prepare($sql, $attachment_id), ARRAY_A );
$sizes = [];
if ( !empty($result) ) {
foreach ($result as $size) {
$size_name = $size['size_name'];
unset($size['size_name']);
$sizes[$size_name] = $size;
}
}
wp_cache_set($cache_key, $sizes, $this->cache_group);
return $sizes;
}
/**
* Get file meta
*
* @param int $post_id
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get_file_meta_value($value, $post_id, $key, $default = null) {
if ( ud_get_stateless_media()->get('sm.use_postmeta') ) {
$meta = get_post_meta($post_id, 'sm_cloud', []);
return isset($meta[$key]) ? $meta[$key] : $default;
}
// Get values from the cache
$key = sanitize_key($key);
$value = $this->_get_file_meta_cache($post_id, $key, $found);
if ( $found ) {
return $value;
}
// Get values from the DB
$sql = "SELECT meta_value FROM $this->file_meta WHERE post_id = %d AND meta_key = %s";
$data = $this->wpdb->get_var( $this->wpdb->prepare($sql, $post_id, $key) );
$value = null;
if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in.
$value = @unserialize( trim( $data ) );
} else {
$value = $data;
}
$this->_set_file_meta_cache($post_id, $key, $value);
return $value;
}
/**
* Update file meta
*
* @param int $post_id
* @param string $key
* @param mixed $value
* @return int
*/
public function update_file_meta($post_id, $key, $value) {
$key = sanitize_key($key);
// Update value in the DB
$value = maybe_serialize( $value );
$file_meta_id = $this->get_file_meta_id($post_id, $key);
if ( $file_meta_id ) {
$this->wpdb->update(
$this->file_meta,
['meta_value' => $value],
['id' => $file_meta_id],
);
} else {
$this->wpdb->insert( $this->file_meta, [
'meta_key' => $key,
'meta_value' => $value,
'post_id' => $post_id,
]);
$file_meta_id = $this->wpdb->insert_id;
}
$this->_delete_file_meta_key_cache($post_id, $key);
return $file_meta_id;
}
/**
* Get all the meta for the file
*
* @param mixed $meta
* @param int $post_id
* @return mixed
*/
public function get_file_meta($meta, $post_id) {
$sql = "SELECT meta_key, meta_value FROM $this->file_meta WHERE post_id = %d";
$result = $this->wpdb->get_results( $this->wpdb->prepare($sql, $post_id), ARRAY_A );
$data = [];
foreach ($result as $row) {
$key = $row['meta_key'];
$value = $row['meta_value'];
if ( is_serialized( $value ) ) { // Don't attempt to unserialize data that wasn't serialized going in.
$value = @unserialize( trim( $value ) );
}
$data[$key] = $value;
}
return $data;
}
}
}
}