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