| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 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 ); |
| 40 |
} |
| 41 |
|
| 42 |
add_action( 'restrict_manage_posts', '_imagify_attachments_filter_dropdown' ); |
| 43 |
/** |
| 44 |
* Adds a dropdown that allows filtering on the attachments Imagify status. |
| 45 |
* |
| 46 |
* @since 1.0 |
| 47 |
* @author Jonathan Buttigieg |
| 48 |
*/ |
| 49 |
function _imagify_attachments_filter_dropdown() { |
| 50 |
if ( ! Imagify_Views::get_instance()->is_wp_library_page() ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$optimized = imagify_count_optimized_attachments(); |
| 55 |
$unoptimized = imagify_count_unoptimized_attachments(); |
| 56 |
$errors = imagify_count_error_attachments(); |
| 57 |
$status = isset( $_GET['imagify-status'] ) ? wp_unslash( $_GET['imagify-status'] ) : 0; // WPCS: CSRF ok. |
| 58 |
$options = array( |
| 59 |
'optimized' => _x( 'Optimized', 'Media Files', 'imagify' ), |
| 60 |
'unoptimized' => _x( 'Unoptimized', 'Media Files', 'imagify' ), |
| 61 |
'errors' => _x( 'Errors', 'Media Files', 'imagify' ), |
| 62 |
); |
| 63 |
|
| 64 |
echo '<label class="screen-reader-text" for="filter-by-optimization-status">' . __( 'Filter by status', 'imagify' ) . '</label>'; |
| 65 |
echo '<select id="filter-by-optimization-status" name="imagify-status">'; |
| 66 |
echo '<option value="0" selected="selected">' . __( 'All Media Files', 'imagify' ) . '</option>'; |
| 67 |
|
| 68 |
foreach ( $options as $value => $label ) { |
| 69 |
echo '<option value="' . $value . '" ' . selected( $status, $value, false ) . '>' . $label . ' (' . ${$value} . ')</option>'; |
| 70 |
} |
| 71 |
echo '</select> '; |
| 72 |
} |
| 73 |
|
| 74 |
add_filter( 'request', '_imagify_sort_attachments_by_status' ); |
| 75 |
/** |
| 76 |
* Modify the query based on the imagify-status variable in $_GET. |
| 77 |
* |
| 78 |
* @since 1.0 |
| 79 |
* @author Jonathan Buttigieg |
| 80 |
* |
| 81 |
* @param array $vars The array of requested query variables. |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
function _imagify_sort_attachments_by_status( $vars ) { |
| 85 |
if ( empty( $_GET['imagify-status'] ) || ! Imagify_Views::get_instance()->is_wp_library_page() ) { // WPCS: CSRF ok. |
| 86 |
return $vars; |
| 87 |
} |
| 88 |
|
| 89 |
$status = wp_unslash( $_GET['imagify-status'] ); // WPCS: CSRF ok. |
| 90 |
$meta_key = '_imagify_status'; |
| 91 |
$meta_compare = '='; |
| 92 |
$relation = array(); |
| 93 |
|
| 94 |
switch ( $status ) { |
| 95 |
case 'unoptimized': |
| 96 |
$meta_key = '_imagify_data'; |
| 97 |
$meta_compare = 'NOT EXISTS'; |
| 98 |
break; |
| 99 |
case 'optimized': |
| 100 |
$status = 'success'; |
| 101 |
$relation = array( |
| 102 |
'key' => $meta_key, |
| 103 |
'value' => 'already_optimized', |
| 104 |
'compare' => $meta_compare, |
| 105 |
); |
| 106 |
break; |
| 107 |
case 'errors': |
| 108 |
$status = 'error'; |
| 109 |
break; |
| 110 |
default: |
| 111 |
return $vars; |
| 112 |
} |
| 113 |
|
| 114 |
$vars = array_merge( $vars, array( |
| 115 |
'meta_query' => array( |
| 116 |
'relation' => 'or', |
| 117 |
array( |
| 118 |
'key' => $meta_key, |
| 119 |
'value' => $status, |
| 120 |
'compare' => $meta_compare, |
| 121 |
), |
| 122 |
$relation, |
| 123 |
), |
| 124 |
) ); |
| 125 |
|
| 126 |
$vars['post_mime_type'] = imagify_get_mime_types(); |
| 127 |
|
| 128 |
return $vars; |
| 129 |
} |
| 130 |
|