| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || exit; |
| 3 |
|
| 4 |
add_filter( 'manage_media_columns', '_imagify_manage_media_columns' ); |
| 5 |
/** |
| 6 |
* Add "Imagify" column in upload.php. |
| 7 |
* |
| 8 |
* @since 1.0 |
| 9 |
* @author Jonathan Buttigieg |
| 10 |
* |
| 11 |
* @param array $columns An array of columns displayed in the Media list table. |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
function _imagify_manage_media_columns( $columns ) { |
| 15 |
if ( imagify_get_context( 'wp' )->current_user_can( 'optimize' ) ) { |
| 16 |
$columns['imagify_optimized_file'] = __( 'Imagify', 'imagify' ); |
| 17 |
} |
| 18 |
|
| 19 |
return $columns; |
| 20 |
} |
| 21 |
|
| 22 |
add_action( 'manage_media_custom_column', '_imagify_manage_media_custom_column', 10, 2 ); |
| 23 |
/** |
| 24 |
* Add content to the "Imagify" columns in upload.php. |
| 25 |
* |
| 26 |
* @since 1.0 |
| 27 |
* @author Jonathan Buttigieg |
| 28 |
* |
| 29 |
* @param string $column_name Name of the custom column. |
| 30 |
* @param int $attachment_id Attachment ID. |
| 31 |
*/ |
| 32 |
function _imagify_manage_media_custom_column( $column_name, $attachment_id ) { |
| 33 |
if ( 'imagify_optimized_file' !== $column_name ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$process = imagify_get_optimization_process( $attachment_id, 'wp' ); |
| 38 |
|
| 39 |
echo get_imagify_media_column_content( $process ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 40 |
} |
| 41 |
|
| 42 |
add_filter( 'request', '_imagify_sort_attachments_by_status' ); |
| 43 |
/** |
| 44 |
* Modify the query based on the imagify-status variable in $_GET. |
| 45 |
* |
| 46 |
* @since 1.0 |
| 47 |
* @author Jonathan Buttigieg |
| 48 |
* |
| 49 |
* @param array $vars The array of requested query variables. |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
function _imagify_sort_attachments_by_status( $vars ) { |
| 53 |
if ( empty( $_GET['imagify-status'] ) || ! Imagify_Views::get_instance()->is_wp_library_page() ) { |
| 54 |
return $vars; |
| 55 |
} |
| 56 |
|
| 57 |
$status = sanitize_text_field( wp_unslash( $_GET['imagify-status'] ) ); |
| 58 |
$meta_key = '_imagify_status'; |
| 59 |
$meta_compare = '='; |
| 60 |
$relation = []; |
| 61 |
|
| 62 |
switch ( $status ) { |
| 63 |
case 'unoptimized': |
| 64 |
$meta_key = '_imagify_data'; |
| 65 |
$meta_compare = 'NOT EXISTS'; |
| 66 |
break; |
| 67 |
case 'optimized': |
| 68 |
$status = 'success'; |
| 69 |
$relation = [ |
| 70 |
'key' => $meta_key, |
| 71 |
'value' => 'already_optimized', |
| 72 |
'compare' => $meta_compare, |
| 73 |
]; |
| 74 |
break; |
| 75 |
case 'errors': |
| 76 |
$status = 'error'; |
| 77 |
break; |
| 78 |
default: |
| 79 |
return $vars; |
| 80 |
} |
| 81 |
|
| 82 |
$vars = array_merge( |
| 83 |
$vars, |
| 84 |
[ |
| 85 |
'meta_query' => [ |
| 86 |
'relation' => 'or', |
| 87 |
[ |
| 88 |
'key' => $meta_key, |
| 89 |
'value' => $status, |
| 90 |
'compare' => $meta_compare, |
| 91 |
], |
| 92 |
$relation, |
| 93 |
], |
| 94 |
] |
| 95 |
); |
| 96 |
|
| 97 |
if ( ! key_exists( 'post_mime_type', $vars ) ) { |
| 98 |
$vars['post_mime_type'] = imagify_get_mime_types(); |
| 99 |
} |
| 100 |
|
| 101 |
return $vars; |
| 102 |
} |
| 103 |
|