class-media-item-optimization-global-stats.php
3.68 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
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
<?php
namespace Smush\Core\Media;
use Smush\Core\Array_Utils;
class Media_Item_Optimization_Global_Stats extends Media_Item_Stats {
/**
* @var int How many media *items* are included in this instance.
*/
private $count = 0;
/**
* @var int[] Ids of the attachments included in this instance.
*/
private $attachment_ids = array();
private $array_utils;
public function __construct() {
$this->array_utils = new Array_Utils();
}
public function to_array() {
$array = parent::to_array();
$array['count'] = $this->get_count();
$array['attachment_ids'] = join( ',', $this->get_attachment_ids() );
return $array;
}
public function from_array( $array ) {
parent::from_array( $array );
$this->set_count( (int) $this->get_array_value( $array, 'count' ) );
$attachment_ids = $this->get_array_value( $array, 'attachment_ids' );
$attachment_ids = empty( $attachment_ids ) ? array() : explode( ',', $attachment_ids );
$this->set_attachment_ids( $attachment_ids );
}
/**
* @param $attachment_id int
* @param $item_stats Media_Item_Stats
*
* @return boolean
*/
public function add_item_stats( $attachment_id, $item_stats ) {
if ( $this->has_attachment_id( $attachment_id ) ) {
return false;
} else {
parent::add( $item_stats );
$this->set_count( $this->get_count() + 1 );
$this->add_attachment_id( $attachment_id );
return true;
}
}
/**
* @param $attachment_id int
* @param $item_stats Media_Item_Stats
*
* @return boolean
*/
public function subtract_item_stats( $attachment_id, $item_stats ) {
if ( $this->has_attachment_id( $attachment_id ) ) {
parent::subtract( $item_stats );
$this->set_count( $this->get_count() - 1 );
$this->remove_attachment_id( $attachment_id );
return true;
} else {
return false;
}
}
/**
* @param $addend Media_Item_Optimization_Global_Stats
*
* @return void
*/
public function add( $addend ) {
parent::add( $addend );
$this->set_count( $this->get_count() + $addend->get_count() );
$this->set_attachment_ids(
$this->array_utils->fast_array_unique( array_merge(
$this->get_attachment_ids(),
$addend->get_attachment_ids()
) )
);
}
/**
* @param $subtrahend Media_Item_Optimization_Global_Stats
*
* @return void
*/
public function subtract( $subtrahend ) {
parent::subtract( $subtrahend );
$this->set_count( max( $this->get_count() - $subtrahend->get_count(), 0 ) );
$this->set_attachment_ids(
array_diff(
$this->get_attachment_ids(),
$subtrahend->get_attachment_ids()
)
);
}
/**
* @return mixed
*/
public function get_count() {
return $this->count;
}
/**
* @param mixed $count
*
* @return Media_Item_Optimization_Global_Stats
*/
public function set_count( $count ) {
$this->count = $count;
return $this;
}
private function add_attachment_id( $attachment_id ) {
$this->attachment_ids[] = $attachment_id;
}
private function remove_attachment_id( $attachment_id ) {
$attachment_ids = $this->get_attachment_ids();
$index = array_search( $attachment_id, $attachment_ids );
if ( $index !== false ) {
unset( $attachment_ids[ $index ] );
$this->set_attachment_ids( $attachment_ids );
}
}
public function has_attachment_id( $attachment_id ) {
return in_array( $attachment_id, $this->get_attachment_ids() );
}
private function get_attachment_ids() {
$attachment_ids = $this->attachment_ids;
return empty( $attachment_ids ) || ! is_array( $attachment_ids )
? array()
: $attachment_ids;
}
private function set_attachment_ids( $attachment_ids ) {
$this->attachment_ids = empty( $attachment_ids ) || ! is_array( $attachment_ids )
? array()
: $attachment_ids;
}
}