| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Optimization\Components; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Image\Image_Meta; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Admin_Filter { |
| 12 |
public function add_filter( string $post_type ) { |
| 13 |
if ( 'attachment' !== $post_type ) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
$options = [ |
| 18 |
'' => __( 'All Media Files', 'image-optimization' ), |
| 19 |
'optimized' => __( 'Optimized', 'image-optimization' ), |
| 20 |
'not-optimized' => __( 'Unoptimized', 'image-optimization' ), |
| 21 |
'in-progress' => __( 'In progress', 'image-optimization' ), |
| 22 |
'failed' => __( 'Errors', 'image-optimization' ), |
| 23 |
]; |
| 24 |
|
| 25 |
$current_value = $this->get_current_filter(); |
| 26 |
?> |
| 27 |
<label class="screen-reader-text" for="image-optimization-filter"> |
| 28 |
<?php esc_html_e( 'Filter by optimization status', 'image-optimization' ); ?> |
| 29 |
</label> |
| 30 |
|
| 31 |
<select class="image-optimization-filter" id="image-optimization-filter" name="image-optimization-filter"> |
| 32 |
<?php |
| 33 |
foreach ( $options as $value => $title ) { |
| 34 |
printf( |
| 35 |
'<option value="%s" %s>%s</option>', |
| 36 |
esc_attr( $value ), |
| 37 |
selected( $value, $current_value, false ), |
| 38 |
esc_html( $title ) |
| 39 |
); |
| 40 |
} |
| 41 |
?> |
| 42 |
</select> |
| 43 |
<?php |
| 44 |
} |
| 45 |
|
| 46 |
public function handle_filter( $query ) { |
| 47 |
global $pagenow; |
| 48 |
|
| 49 |
if ( 'upload.php' !== $pagenow ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$current_value = $this->get_current_filter(); |
| 54 |
|
| 55 |
if ( empty( $current_value ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$meta_query = empty( $query->get( 'meta_query' ) ) ? [] : $query->get( 'meta_query' ); |
| 60 |
|
| 61 |
$meta_query[] = [ |
| 62 |
[ |
| 63 |
'key' => '_wp_attachment_metadata', // Images without this field considered invalid |
| 64 |
'compare' => 'EXISTS', |
| 65 |
], |
| 66 |
]; |
| 67 |
|
| 68 |
switch ( $current_value ) { |
| 69 |
case 'not-optimized': |
| 70 |
$meta_query[] = [ |
| 71 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 72 |
'compare' => 'NOT EXISTS', |
| 73 |
]; |
| 74 |
|
| 75 |
break; |
| 76 |
|
| 77 |
case 'optimized': |
| 78 |
$meta_query[] = [ |
| 79 |
'compare' => 'LIKE', |
| 80 |
'value' => '"status";s:9:"optimized"', |
| 81 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 82 |
]; |
| 83 |
|
| 84 |
break; |
| 85 |
|
| 86 |
case 'in-progress': |
| 87 |
$meta_query[] = [ |
| 88 |
'compare' => 'LIKE', |
| 89 |
'value' => '-in-progress"', // Covers both optimization and restoring |
| 90 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 91 |
]; |
| 92 |
|
| 93 |
break; |
| 94 |
|
| 95 |
case 'failed': |
| 96 |
$meta_query[] = [ |
| 97 |
'compare' => 'LIKE', |
| 98 |
'value' => '-failed"', // Covers both optimization and restoring |
| 99 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 100 |
]; |
| 101 |
|
| 102 |
break; |
| 103 |
} |
| 104 |
|
| 105 |
$query->set( 'meta_query', $meta_query ); |
| 106 |
} |
| 107 |
|
| 108 |
private function get_current_filter(): string { |
| 109 |
return sanitize_text_field( wp_unslash( $_GET['image-optimization-filter'] ?? '' ) ); |
| 110 |
} |
| 111 |
|
| 112 |
public function __construct() { |
| 113 |
add_filter( 'restrict_manage_posts', [ $this, 'add_filter' ] ); |
| 114 |
add_filter( 'parse_query', [ $this, 'handle_filter' ] ); |
| 115 |
} |
| 116 |
} |
| 117 |
|