Repository navigation
Expand file tree
/
Copy pathimg-optm-notify.trait.php
More file actions
285 lines (257 loc) · 8.9 KB
/
Copy pathimg-optm-notify.trait.php
File metadata and controls
285 lines (257 loc) · 8.9 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
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
<?php
/**
* Image optimization notification handling.
*
* @package LiteSpeed
* @since 7.9.1
*/
namespace LiteSpeed;
defined( 'WPINC' ) || exit();
/**
* Trait Img_Optm_Notify
*/
trait Img_Optm_Notify {
/**
* Normalize a positive working-row ID.
*
* @since 7.9.1
*
* @param mixed $row_id Working-row ID.
* @return int|false
*/
private function _normalize_notify_row_id( $row_id ) {
if ( ! is_int( $row_id ) && ! is_string( $row_id ) ) {
return false;
}
$value = (string) $row_id;
return preg_match( '/^[1-9]\d*$/D', $value ) && (string) (int) $row_id === $value ? (int) $row_id : false;
}
/**
* Normalize one successful image result.
*
* @since 7.9.1
*
* @param array $data Result data.
* @param string $default_server Default server origin.
* @return array|false
*/
private function _normalize_notify_result( $data, $default_server ) {
$server_info = $this->_normalize_server_info( $data, $default_server );
$src_size = is_array( $data ) && array_key_exists( 'src_size', $data ) && is_int( $data['src_size'] ) && 0 <= $data['src_size'] ? $data['src_size'] : false;
if ( ! $server_info || false === $src_size ) {
return false;
}
$result = [
'server_info' => $server_info,
'src_size' => $src_size,
];
foreach ( [ 'ori', 'webp', 'avif' ] as $type ) {
if ( empty( $server_info[ $type ] ) ) {
continue;
}
$key = $type . '_reduced';
$reduced = array_key_exists( $key, $data ) && is_int( $data[ $key ] ) && 0 <= $data[ $key ] ? $data[ $key ] : false;
if ( false === $reduced || $reduced > $src_size ) {
return false;
}
$result[ $key ] = $reduced;
}
return $result;
}
/**
* Normalize the callback data map or row-ID list.
*
* @since 7.9.1
*
* @param array $data Callback data.
* @param int $status Callback status.
* @param string $default_server Default server origin.
* @return array|false
*/
private function _normalize_notify_data( $data, $status, $default_server ) {
if ( empty( $data ) || ! is_array( $data ) || Cloud::SIGN_MAX_ITEMS < count( $data ) ) {
return false;
}
$normalized = [];
if ( self::STATUS_NOTIFIED === $status ) {
foreach ( $data as $row_id => $result ) {
$row_id = $this->_normalize_notify_row_id( $row_id );
$result = $this->_normalize_notify_result( $result, $default_server );
if ( false === $row_id || false === $result || isset( $normalized[ $row_id ] ) ) {
return false;
}
$normalized[ $row_id ] = $result;
}
return $normalized;
}
if ( array_keys( $data ) !== range( 0, count( $data ) - 1 ) ) {
return false;
}
foreach ( $data as $row_id ) {
$row_id = $this->_normalize_notify_row_id( $row_id );
if ( false === $row_id || isset( $normalized[ $row_id ] ) ) {
return false;
}
$normalized[ $row_id ] = true;
}
return $normalized;
}
/**
* Load the size summary for one attachment.
*
* @since 7.9.1
*
* @param int $post_id Attachment ID.
* @return array
*/
private function _load_notify_meta( $post_id ) {
$info = [
'ori_total' => 0,
'ori_saved' => 0,
'webp_total' => 0,
'webp_saved' => 0,
'avif_total' => 0,
'avif_saved' => 0,
];
$stored = get_post_meta( $post_id, self::DB_SIZE, true );
if ( is_array( $stored ) ) {
foreach ( $stored as $key => $value ) {
if ( is_numeric( $value ) ) {
$info[ $key ] = array_key_exists( $key, $info ) ? max( 0, (int) $value ) : (int) $value;
} elseif ( ! array_key_exists( $key, $info ) ) {
$info[ $key ] = $value;
}
}
}
return [
'info' => $info,
'reduced' => 0,
];
}
/**
* Persist successful results.
*
* @since 7.9.1
*
* @param array $results Normalized results keyed by working-row ID.
* @return array
*/
private function _persist_notify_results( $results ) {
global $wpdb;
$ids = array_keys( $results );
$q = "SELECT * FROM `$this->_table_img_optming` WHERE id IN ( " . implode( ',', array_fill( 0, count( $ids ), '%d' ) ) . ' )';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared
$list = $wpdb->get_results( $wpdb->prepare( $q, $ids ) );
if ( ! is_array( $list ) ) {
return Cloud::err( 'failed to load image rows' );
}
$rows = [];
foreach ( $list as $row ) {
if ( self::STATUS_RAW === (int) $row->optm_status ) {
continue;
}
$rows[ (int) $row->id ] = $row;
}
foreach ( $rows as $row ) {
if ( self::STATUS_REQUESTED !== (int) $row->optm_status && self::STATUS_NOTIFIED !== (int) $row->optm_status ) {
return Cloud::err( 'image row is not awaiting a result' );
}
}
$pending = [];
$total_reduced = 0;
$write_error = '';
foreach ( $rows as $row_id => $row ) {
$result = $results[ $row_id ];
$server_info = $result['server_info'];
$old_status = (int) $row->optm_status;
if ( self::STATUS_NOTIFIED === $old_status ) {
$q = "UPDATE `$this->_table_img_optming` SET server_info = %s WHERE id = %d AND optm_status = %d";
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared
if ( false === $wpdb->query( $wpdb->prepare( $q, [ wp_json_encode( $server_info ), $row_id, self::STATUS_NOTIFIED ] ) ) ) {
$write_error = 'failed to refresh image result';
}
continue;
}
$q = "UPDATE `$this->_table_img_optming` SET optm_status = %d, server_info = %s WHERE id = %d AND optm_status = %d";
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared
$claimed = $wpdb->query( $wpdb->prepare( $q, [ self::STATUS_NOTIFIED, wp_json_encode( $server_info ), $row_id, $old_status ] ) );
if ( 1 !== (int) $claimed ) {
$write_error = 'image row changed';
continue;
}
$post_id = (int) $row->post_id;
if ( ! isset( $pending[ $post_id ] ) ) {
$pending[ $post_id ] = $this->_load_notify_meta( $post_id );
}
foreach ( [ 'ori', 'webp', 'avif' ] as $type ) {
if ( empty( $server_info[ $type ] ) ) {
continue;
}
$pending[ $post_id ]['info'][ $type . '_total' ] += $result['src_size'];
$pending[ $post_id ]['info'][ $type . '_saved' ] += $result[ $type . '_reduced' ];
$pending[ $post_id ]['reduced'] += $result[ $type . '_reduced' ];
}
}
foreach ( $pending as $post_id => $meta ) {
update_post_meta( $post_id, self::DB_SIZE, $meta['info'] );
if ( get_post_meta( $post_id, self::DB_SIZE, true ) !== $meta['info'] ) {
$write_error = 'failed to update image summary';
continue;
}
$total_reduced += $meta['reduced'];
}
if ( $total_reduced ) {
self::reload_summary();
$this->_summary['reduced'] = ! empty( $this->_summary['reduced'] ) ? (int) $this->_summary['reduced'] + $total_reduced : $total_reduced;
self::save_summary();
}
if ( $rows ) {
self::update_option( self::DB_NEED_PULL, self::STATUS_NOTIFIED );
}
return $write_error ? Cloud::err( $write_error ) : Cloud::ok( [ 'count' => count( $rows ) ] );
}
/**
* Delete terminally failed working rows.
*
* @since 7.9.1
*
* @param array $ids Row IDs keyed by ID.
* @return array
*/
private function _delete_notify_rows( $ids ) {
global $wpdb;
$ids = array_keys( $ids );
$q = "DELETE FROM `$this->_table_img_optming` WHERE id IN ( " . implode( ',', array_fill( 0, count( $ids ), '%d' ) ) . ' ) AND optm_status = %d';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared
$deleted = $wpdb->query( $wpdb->prepare( $q, array_merge( $ids, [ self::STATUS_REQUESTED ] ) ) );
return false === $deleted ? Cloud::err( 'failed to delete working rows' ) : Cloud::ok( [ 'count' => count( $ids ) ] );
}
/**
* Handle a verified Cloud image notification.
*
* @since 7.9.1
*
* @param string|null $raw_body Verified raw JSON body.
* @return array
*/
public function notify_img( $raw_body = null ) {
$payload = is_string( $raw_body ) ? json_decode( $raw_body, true, 32 ) : false;
if ( ! is_array( $payload ) || ! isset( $payload['status'] ) || ! is_int( $payload['status'] ) ) {
return Cloud::err( 'invalid data' );
}
$terminal_statuses = [ self::STATUS_FAILED, self::STATUS_MISS, self::STATUS_ERR_FETCH, self::STATUS_ERR_404, self::STATUS_ERR_OPTM, self::STATUS_XMETA, self::STATUS_ERR ];
$status = $payload['status'];
if ( self::STATUS_NOTIFIED !== $status && ! in_array( $status, $terminal_statuses, true ) ) {
return Cloud::err( 'invalid status' );
}
$default_server = isset( $payload['server'] ) ? Img::normalize_cloud_url( $payload['server'], true ) : false;
$data = isset( $payload['data'] ) ? $this->_normalize_notify_data( $payload['data'], $status, $default_server ) : false;
if ( ! $default_server || false === $data ) {
return Cloud::err( 'invalid image result' );
}
if ( ! $this->__data->tb_exist( 'img_optming' ) ) {
return Cloud::ok( [ 'count' => 0 ] );
}
return self::STATUS_NOTIFIED === $status ? $this->_persist_notify_results( $data ) : $this->_delete_notify_rows( $data );
}
}