PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.1.0
Image Optimization – Compress Images and Convert to WebP or AVIF v1.1.0
1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 All 32 releases
image-optimization / modules / optimization / components / admin-filter.php

admin-filter.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.1.0, at modules/optimization/components/admin-filter.php

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