-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws-media-check.php
385 lines (320 loc) · 14.6 KB
/
ws-media-check.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
<?php
/*
* Plugin Name: Media Library Check
* Plugin URI: http://www.wierstewart.com/
* Description: Checks to see each Media Library item is locally available.
* Version: 1.0.0
* Author: W/S
* License: GPLv2
* */
$extensions = array(
'gif',
'jpg',
'jpeg',
'png'
//pdf?
);
//include_once('parallel-curl.php');
class wsMediaCheck{
private $aws_options;
private $image_meta;
public function __construct(){
$this->add_actions();
$this->aws_options = get_option('tantan_wordpress_s3');
}
private function add_actions(){
add_filter( 'manage_upload_columns', array($this, 'add_media_library_columns') );
add_filter( 'manage_media_custom_column', array($this, 'add_media_library_content'), 10, 2);
add_filter( 'manage_upload_sortable_columns', array($this, 'add_media_library_columns_sortable') );
add_filter( 'wp_generate_attachment_metadata', array($this, 'generate_media_check_metadata'), 10, 2);
add_action( 'pre_get_posts', array($this, 'media_check_column_localsize_do_sort') );
add_action( 'pre_get_posts', array($this, 'media_check_column_awssize_do_sort') );
add_action( 'admin_print_styles-upload.php', array($this, 'media_check_column_size') );
if( ! is_network_admin() ) add_action( 'admin_menu', array($this, 'media_check_tools_menu') );
add_action('current_screen', function($current_screen){
//$current_screen->base == 'upload';
//$current_screen->post_type == 'attachment';
});
}
//////////////////////////////////////
//////////////////////////////////////
function add_media_library_columns( $columns ) {
$columns['filesize_local'] = __( 'Local File Exists', 'media-file-tools' );
$columns['filesize_aws'] = __( 'File on AWS', 'media-file-tools' );
return $columns;
}
/**
* Adjust File Size column on Media Library page in WP admin
*/
function media_check_column_size() {
echo
'<style>
.manage-column.column-title,
.wp-list-table.widefat td.sortable{
width:30%;
}
.wp-list-table .manage-column.column-smushit{
width:15%;
}
.wp-list-table .column-filesize_local,
.wp-list-table .column-filesize_aws {
width: 10%;
text-align: center;
}
.wp-list-table td.column-filesize_local a span,
.wp-list-table td.column-filesize_aws a span{
font-size: 30px;
}
.wp-list-table .column-filesize_local a span.dashicons-yes,
.wp-list-table .column-filesize_aws a span.dashicons-yes{
color: green;
}
.wp-list-table .column-filesize_local a span.dashicons-no,
.wp-list-table .column-filesize_aws a span.dashicons-no{
color: red;
}
</style>';
}
/**
* Core Content:
*
*/
function add_media_library_content( $column_name, $post_id ){
$image_data = null;
if( $column_name == 'filesize_local'){
$image_data = json_decode( get_post_meta( $post_id, '_filesize_local', true ), true); //size_format
if ( !$image_data ){
$image_data = $this->generate_media_check_metadata( $image_data, $post_id );
}
if( $image_data['file_size'] > 0 ) echo "<a target='_blank' href='".$image_data['url']."'><span class='dashicons dashicons-yes'></span></a>";
else echo "<a target='_blank' href='".$image_data['url']."'><span class='dashicons dashicons-no'></span></a>";
//TODO: add a URL if not found?
unset($image_data);
}//filesize-column
if( $column_name == 'filesize_aws'){
$aws_url = get_attached_file( $post_id ); //aws url will be returned if file not found
if(stripos($aws_url, '//')===false) $aws_url = $this->build_aws_url($post_id);
if( stripos($aws_url, '//')!==false ) echo "<a target='_blank' href='$aws_url'><span class='dashicons dashicons-yes'></span></a>";
else echo "<a target='_blank' href='$aws_url'><span class='dashicons dashicons-no'></span></a>";
}//filesize-column
}
function generate_media_check_metadata( $image_data, $att_id ){
// $file = get_attached_file( $att_id ); //aws url will be returned if file not found
// Local data:
$image_data['attachment'] = get_post_meta($att_id, "_wp_attachment_metadata"); //local filename
$file = '';
$url = '';
if( array_key_exists('file', $image_data['attachment'][0]) ){
if(stripos($image_data['attachment'][0]['file'], 'http') === false ){
$file = wp_upload_dir()['basedir'] .'/'. $image_data['attachment'][0]['file']; //this should be the true local file
$url = wp_upload_dir()['baseurl'] .'/'. $image_data['attachment'][0]['file']; //this should be the true local file
}
}
$image_data['file']=$file;
$image_data['url']=$url;
$image_data['file_size'] = 0;
if(file_exists($file)) $image_data['file_size'] = @filesize( $file ); //missing files = 4096 file size!
$key='_filesize_local';
if ( $image_data['file_size'] > 0 ){
$image_data['status'] = '';
}
if( $file == '' ){
$image_data['status'] = 'Could not determine local url';
}else{
$image_data['status'] = 'File Not Found';
}
unset($image_data['attachment']);
update_post_meta( $att_id, $key, json_encode($image_data) );
/*
// AWS data:
$aws_urlbase = $this->aws_options['domain'];
if( $aws_urlbase == 'cloudfront' ) $aws_urlbase = $this->aws_options['cloudfront'];
// $aws_urlbase .= $this->aws_options['object-prefix'];
$image_data['aws'] = get_post_meta($att_id, "amazonS3_info"); // See: ../amazon-s3-and-cloudfront/classes/as3cf-filter.php:8: const CACHE_KEY = 'amazonS3_cache';
$file = '';
if( array_key_exists(0, $image_data['aws']) ){
if( array_key_exists('key', $image_data['aws'][0]) ){
$file = 'https://'. $aws_urlbase .'/'. $image_data['aws'][0]['key']; //this should be the true aws url
}
}
$image_data['file_aws']=$file;
$file_size = 0;
$response = wp_remote_get( $file );
print_r($response);
if(!is_wp_error($response)) $file_size = wp_remote_retrieve_header( $response, 'content-length' );
unset($response);
if( $response ){
$file_size = filesize( $file ); //missing files = 4096 file size!
}else{
// file_exists($file);
}
$key='_filesize_aws';
$value='';
if ( $file !== '' ) $value = $file;
if( count($image_data['aws']) == 0 ) $value = 'Could not determine AWS url! ';
// else $value='File Not Found: '.$file;
update_post_meta( $att_id, $key, $value );
$image_data[$key]=$value;
*/
return $image_data;
}
function build_aws_url($att_id){
$aws_url='';
$aws_urlbase = $this->aws_options['domain'];
if( $aws_urlbase == 'cloudfront' ) $aws_urlbase = $this->aws_options['cloudfront'];
$image_data['aws'] = get_post_meta($att_id, "amazonS3_info"); // See: ../amazon-s3-and-cloudfront/classes/as3cf-filter.php:8: const CACHE_KEY = 'amazonS3_cache';
if( array_key_exists(0, $image_data['aws']) ){
if( array_key_exists('key', $image_data['aws'][0]) ){
$aws_url = 'https://'. $aws_urlbase .'/'. $image_data['aws'][0]['key']; //this should be the true aws url
}
}
return $aws_url;
}
function get_remote_sizes($urls){
/*
$max_requests = 10;
$curl_options = array(
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE
);
$parallel_curl = new ParallelCurl($max_requests, $curl_options);
// Start 3 parallel requests. All three will be started simultaneously.
foreach($urls as $i=>$url){
$parallel_curl->startRequest($url, 'on_request_done');
}
$parallel_curl->finishAllRequests();
*/
}
/**
* Add media-library page functions:
*
*/
function add_media_library_columns_sortable( $columns ){
$columns['filesize_local'] = '_filesize_local';
$columns['filesize_aws'] = '_filesize_aws';
return $columns;
}
function media_check_column_localsize_do_sort(&$query){
global $current_screen;
if( is_object($current_screen) ){
if( 'upload' != $current_screen->id ) return;
$is_filesize = (isset( $_GET['orderby'] ) && '_filesize_local' == $_GET['orderby']);
if( !$is_filesize ) return;
if ( '_filesize_local' == $_GET['orderby'] ){
$query->set('meta_key', '_filesize_local');
$query->set('orderby', 'meta_value_num');
}
}
}
function media_check_column_awssize_do_sort(&$query){
global $current_screen;
if( is_object($current_screen) ){
if( 'upload' !== $current_screen->id ) return;
$is_mimetype = (isset( $_GET['orderby'] ) && '_filesize_aws' == $_GET['orderby']);
if( !$is_mimetype ) return;
if ( '_filesize_aws' == $_GET['orderby'] ){
$query->set('meta_key', '_filesize_aws');
$query->set('orderby', 'meta_value');
}
}
}
/**
* Add an options-page:
*
*/
function media_check_tools_menu() {
$size_media = add_media_page( 'File Sizes', 'File Sizes', 'activate_plugins', 'mediacheck_filesize', array($this,'media_check_options_page'));
}
function media_check_options_page(){
global $wpdb;
if ( !current_user_can('level_10') )
die(__('Cheatin’ uh?', 'media-file-tools' ));
echo '<div class="wrap">';
echo '<h2>' . __( 'File Size Options', 'media-file-tools' ) . '</h2>';
$action = isset($_GET['action']) ? $_GET['action'] : 'default';
switch ( $action ) {
case "size":
/*
$attachments = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment'" ); ?>
<table class="widefat">
<thead>
<tr>
<th><?php _e( 'File', 'media-file-tools' ) ?></th>
<th><?php _e( 'Size', 'media-file-tools' ) ?></th>
<th><?php _e( 'MIME Type', 'media-file-tools' ) ?></th>
<th><?php _e( 'State', 'media-file-tools' ) ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th><?php _e( 'File', 'media-file-tools' ) ?></th>
<th><?php _e( 'Size', 'media-file-tools' ) ?></th>
<th><?php _e( 'MIME Type', 'media-file-tools' ) ?></th>
<th><?php _e( 'State', 'media-file-tools' ) ?></th>
</tr>
</tfoot>
<tbody><?php
foreach( $attachments as $att ){
$att_id = $att->ID;
$file = get_attached_file( $att_id );
$filename_only = basename( get_attached_file( $att_id ) );
$mimetype = get_post_mime_type( $att_id );
$file_size = false;
$file_size = filesize( $file );
$file_size_format = size_format( $file_size );
if ( ! empty( $file_size ) ) {
update_post_meta( $att_id, '_filesize', $file_size );
update_post_meta( $att_id, '_filesmimetype', $mimetype ); ?>
<tr>
<td><?php echo $filename_only; ?></td>
<td><?php echo $file_size_format; ?></td>
<td><?php echo $mimetype; ?></td>
<td>Done!</td>
</tr><?php
} else {
update_post_meta( $att_id, '_filesize', 'N/D' );
update_post_meta( $att_id, '_filesmimetype', $mimetype ); ?>
<tr>
<td><?php echo $filename_only; ?></td>
<td><?php echo 'Error'; ?></td>
<td><?php echo $mimetype; ?></td>
<td>Done!</td>
</tr><?php
}
} ?>
</tbody>
</table><?php
*/
break;
case 'single':
$image_data=array();
$image_id = isset($_GET['image_id']) ? $_GET['image_id'] : '-1';
if($image_id){ $image_data = media_files_tools_metadata_generate( $image_data, $image_id );
print_r($image_data);
}
break;
case 'reset':
$attachment_reset_result= $wpdb->get_results( "DELETE $wpdb->postmeta.* FROM $wpdb->postmeta WHERE meta_key = '_filesize' or meta_key = '_filesmimetype' or meta_key = '_filesize_local' or meta_key = '_filesize_aws' " );
// print_R($attachment_reset_result);
// $attachment_reset_result = ( $attachment_reset_result );
// break;
default:
$attachment_size_count = $wpdb->get_results( "SELECT count(*) as `total` FROM $wpdb->postmeta WHERE meta_key = '_filesize_local' " );
$sizecount = intval( $attachment_size_count[0]->total );
?>
<p><?php echo $sizecount; ?> Media Library items have sizes calculated.</p>
<p><?php _e( 'Reset all file sizes?', 'media-file-tools' ); ?></p>
<p><a class="button" href="admin.php?page=mediacheck_filesize&action=reset"><?php _e( 'Reset All Files Sizes', 'media-file-tools' ); ?></a></p>
<?php
break;
}
/*
?>
<p><?php _e( 'Update all files size can take a while.', 'media-file-tools' ); ?></p>
<p><a class="button" href="admin.php?page=mediacheck_filesize&action=size"><?php _e( 'Get Files Size', 'media-file-tools' ); ?></a></p><?php
*/
}
//////////////////////////////////////
//////////////////////////////////////
}//class
if( is_admin() ) $wsmc=new wsMediaCheck();