| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/* |
| 5 |
* Add "Imagify" column in upload.php |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
*/ |
| 9 |
add_filter( 'manage_media_columns', '_imagify_manage_media_columns' ); |
| 10 |
function _imagify_manage_media_columns( $columns ) { |
| 11 |
$columns['imagify_optimized_file'] = __( 'Imagify', 'imagify' ); |
| 12 |
return $columns; |
| 13 |
} |
| 14 |
|
| 15 |
add_filter( 'manage_media_custom_column', '_imagify_manage_media_custom_column', 10, 2 ); |
| 16 |
function _imagify_manage_media_custom_column( $column_name, $attachment_id ) { |
| 17 |
if ( 'imagify_optimized_file' == $column_name ) { |
| 18 |
echo get_imagify_media_column_content( $attachment_id ); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
/* |
| 23 |
* Adds a dropdown that allows filtering on the attachments Imagify status. |
| 24 |
* |
| 25 |
* @since 1.0 |
| 26 |
*/ |
| 27 |
add_action( 'restrict_manage_posts', '_imagify_attachments_filter_dropdown' ); |
| 28 |
function _imagify_attachments_filter_dropdown() { |
| 29 |
if ( 'upload.php' !== $GLOBALS['pagenow'] ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$optimized = imagify_count_optimized_attachments(); |
| 34 |
$unoptimized = imagify_count_unoptimized_attachments(); |
| 35 |
$errors = imagify_count_error_attachments(); |
| 36 |
$status = ( isset( $_GET['imagify-status'] ) ) ? $_GET['imagify-status'] : 0; |
| 37 |
$options = array( |
| 38 |
'optimized' => __( 'Optimized','imagify' ), |
| 39 |
'unoptimized' => __( 'Unoptimized','imagify' ), |
| 40 |
'errors' => __( 'Errors','imagify' ), |
| 41 |
); |
| 42 |
|
| 43 |
$output = '<label class="screen-reader-text" for="filter-by-optimization-status">' . __( 'Filter by status','imagify' ) . '</label>'; |
| 44 |
|
| 45 |
$output .= '<select id="filter-by-optimization-status" name="imagify-status">'; |
| 46 |
$output .= '<option value="0" selected="selected">' . __( 'All images','imagify' ) . '</option>'; |
| 47 |
|
| 48 |
foreach( $options as $value => $label ) { |
| 49 |
$output .= '<option value="' . $value . '" ' . selected( $status, $value, false ) . '>' . $label . ' (' . ${$value} . ')</option>'; |
| 50 |
} |
| 51 |
|
| 52 |
$output .= '</select> '; |
| 53 |
|
| 54 |
echo $output; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Modify the query based on the imagify-status variable in $_GET |
| 59 |
* |
| 60 |
* @since 1.0 |
| 61 |
*/ |
| 62 |
add_filter( 'request', '_imagify_sort_attachments_by_status' ); |
| 63 |
function _imagify_sort_attachments_by_status( $vars ) { |
| 64 |
if ( 'upload.php' !== $GLOBALS['pagenow'] || empty( $_GET['imagify-status'] ) ) { |
| 65 |
return $vars; |
| 66 |
} |
| 67 |
|
| 68 |
$status = $_GET['imagify-status']; |
| 69 |
$meta_key = '_imagify_status'; |
| 70 |
$meta_compare = '='; |
| 71 |
$relation = array(); |
| 72 |
|
| 73 |
switch( $status ) { |
| 74 |
case 'unoptimized': |
| 75 |
$meta_key = '_imagify_data'; |
| 76 |
$meta_compare = 'NOT EXISTS'; |
| 77 |
break; |
| 78 |
|
| 79 |
case 'optimized': |
| 80 |
$status = 'success'; |
| 81 |
$relation = array( |
| 82 |
'key' => $meta_key, |
| 83 |
'value' => 'already_optimized', |
| 84 |
'compare' => $meta_compare, |
| 85 |
); |
| 86 |
break; |
| 87 |
|
| 88 |
case 'errors': |
| 89 |
$status = 'error'; |
| 90 |
break; |
| 91 |
} |
| 92 |
|
| 93 |
$vars['post_mime_type'] = get_imagify_mime_type(); |
| 94 |
$vars = array_merge( |
| 95 |
$vars, |
| 96 |
array( |
| 97 |
'meta_query' => array( |
| 98 |
'relation' => 'or', |
| 99 |
array( |
| 100 |
'key' => $meta_key, |
| 101 |
'value' => $status, |
| 102 |
'compare' => $meta_compare, |
| 103 |
), |
| 104 |
$relation |
| 105 |
), |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
return $vars; |
| 110 |
} |