class-wpml-media-usage.php
1.11 KB
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
<?php
class WPML_Media_Usage {
const FIELD_NAME = '_wpml_media_usage';
/**
* @var int
*/
private $attachment_id;
/**
* @var array
*/
private $usage;
/**
* @param int $attachment_id
*/
public function __construct( $attachment_id ) {
$this->attachment_id = $attachment_id;
$usage = get_post_meta( $this->attachment_id, self::FIELD_NAME, true );
$this->usage = empty( $usage ) ? array() : $usage;
}
/**
* @return array
*/
public function get_posts() {
return empty( $this->usage['posts'] ) ? array() : $this->usage['posts'];
}
/**
* @param int $post_id
*/
public function add_post( $post_id ) {
$posts = $this->get_posts();
$posts[] = $post_id;
$this->usage['posts'] = array_unique( $posts );
$this->update_usage();
}
/**
* @param int $post_id
*/
public function remove_post( $post_id ) {
$this->usage['posts'] = array_values( array_diff( (array) $this->usage['posts'], array( $post_id ) ) );
$this->update_usage();
}
private function update_usage() {
update_post_meta( $this->attachment_id, self::FIELD_NAME, $this->usage );
}
}