'attachment', 'post_status' => 'inherit', 'meta_key' => self::ORIGINAL_META, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'posts_per_page' => $count, 'fields' => 'all', ]; if (!empty($mime_types)) { $args['post_mime_type'] = $mime_types; } $meta_query = []; if ($min_bytes > 0) { $meta_query[] = [ 'key' => self::ORIGINAL_META, 'value' => $min_bytes, 'type' => 'NUMERIC', 'compare' => '>=', ]; } if ($meta_query) { $args['meta_query'] = $meta_query; } return $args; } public function add_filesize_column($columns) { $columns['filesize'] = __('Original Size', '103graphics-media-inspector'); return $columns; } public function render_filesize_column($column_name, $post_id) { if ('filesize' !== $column_name) { return; } $orig_size = get_post_meta($post_id, self::ORIGINAL_META, true); if ($orig_size === '') { $file = get_attached_file($post_id); if ($file && is_file($file)) { $orig_size = filesize($file); if (false !== $orig_size) { update_post_meta($post_id, self::ORIGINAL_META, (int) $orig_size); } } else { $orig_size = false; } } if ($orig_size && is_numeric($orig_size)) { echo esc_html(size_format((int) $orig_size, 2)); } else { echo '—'; } } public function make_filesize_sortable($columns) { $columns['filesize'] = 'orig_filesize'; return $columns; } public function enable_filesize_sorting($query) { global $pagenow; if (!is_admin()) return; if ($pagenow !== 'upload.php') return; if (!$query->is_main_query()) return; if (isset($_GET['orderby']) && in_array($_GET['orderby'], ['orig_filesize', 'filesize'], true)) { $query->set('meta_key', self::ORIGINAL_META); $query->set('orderby', 'meta_value_num'); } } public function save_attachment_filesize($post_id) { if (!$post_id || get_post_type($post_id) !== 'attachment') { return; } $file = get_attached_file($post_id); if (!$file || !is_file($file)) { update_post_meta($post_id, self::ORIGINAL_META, ''); return; } $orig_size = filesize($file); update_post_meta($post_id, self::ORIGINAL_META, (int) $orig_size); $total = $this->get_total_filesize($post_id); if ($total !== false) { update_post_meta($post_id, self::TOTAL_META, (int) $total); } else { update_post_meta($post_id, self::TOTAL_META, (int) $orig_size); } } public function get_total_filesize($post_id) { $file = get_attached_file($post_id); if (!$file || !is_file($file)) { return false; } $total = (int) filesize($file); $meta = wp_get_attachment_metadata($post_id); if (!empty($meta['sizes']) && is_array($meta['sizes'])) { foreach ($meta['sizes'] as $variant) { if (empty($variant['file'])) { continue; } $variant_file = path_join(dirname($file), $variant['file']); if (is_file($variant_file)) { $total += (int) filesize($variant_file); } } } return $total; } public static function activation() { if (!wp_next_scheduled(self::CRON_HOOK)) { wp_schedule_single_event(time() + 30, self::CRON_HOOK, [0]); } } public static function deactivation() { wp_clear_scheduled_hook(self::CRON_HOOK); } public function batch_index_attachments($offset = 0) { $offset = (int) $offset; $per_page = self::BATCH_PER_PAGE; $attachments = get_posts([ 'post_type' => 'attachment', 'post_status' => 'inherit', 'posts_per_page' => $per_page, 'offset' => $offset, 'fields' => 'ids', ]); if (empty($attachments)) return; foreach ($attachments as $id) { $this->save_attachment_filesize($id); } if (count($attachments) === $per_page) { wp_schedule_single_event(time() + 5, self::CRON_HOOK, [$offset + $per_page]); } } public function cleanup_attachment_meta($post_id) { delete_post_meta($post_id, self::ORIGINAL_META); delete_post_meta($post_id, self::TOTAL_META); delete_post_meta($post_id, self::VARIANTS_META); } public function add_options_page() { add_media_page( __('103Graphics Media Inspector', '103graphics-media-inspector'), __('103Graphics Media Inspector', '103graphics-media-inspector'), 'manage_options', '103graphics-media-inspector', [$this, 'render_options_page'] ); } public function handle_csv_export() { if (empty($_GET['mfs_export_csv']) || empty($_GET['page']) || $_GET['page'] !== '103graphics-media-inspector') { return; } if (!current_user_can('manage_options')) { wp_die(esc_html__('Unauthorized', '103graphics-media-inspector'), '', ['response' => 403]); } if (empty($_REQUEST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'gmi_export_csv')) { wp_die(esc_html__('Security check failed', '103graphics-media-inspector'), '', ['response' => 403]); } while (ob_get_level()) { ob_end_clean(); } $count = isset($_GET['count']) && is_numeric($_GET['count']) && intval($_GET['count']) > 0 ? intval($_GET['count']) : 20; $min_kb = isset($_GET['min_kb']) && is_numeric($_GET['min_kb']) ? intval($_GET['min_kb']) : 0; $media_type = isset($_GET['media_type']) ? sanitize_text_field(wp_unslash($_GET['media_type'])) : 'all'; $show_variants = isset($_GET['show_variants']) && $_GET['show_variants'] === '1'; $min_bytes = $min_kb * 1024; $mime_types = $this->get_mime_types_for($media_type); $args = $this->build_media_query_args($count, '', $mime_types, [], $min_bytes); $filename = sanitize_file_name('media-inspector-export-' . date_i18n('Y-m-d') . '.csv'); header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename="' . esc_attr($filename) . '"'); $out = fopen('php://output', 'w'); if (!$out) { wp_die(esc_html__('Could not open output stream', '103graphics-media-inspector')); } $columns = ['ID', 'Title', 'Author', 'Date', 'File Name', 'Original Size', 'Total Size']; if ($show_variants) { $columns[] = 'Variant Details'; } fputcsv($out, $columns); $query = new WP_Query($args); if ($query->have_posts()) { foreach ($query->posts as $attachment) { $meta = wp_get_attachment_metadata($attachment->ID); $file = get_attached_file($attachment->ID); $orig_size = get_post_meta($attachment->ID, self::ORIGINAL_META, true); $total_size = get_post_meta($attachment->ID, self::TOTAL_META, true); $author = get_the_author_meta('display_name', $attachment->post_author); $row = [ $attachment->ID, $attachment->post_title, $author, get_the_date('Y-m-d', $attachment->ID), $file ? basename($file) : '', $orig_size ? size_format((int)$orig_size, 2) : '', $total_size ? size_format((int)$total_size, 2) : '', ]; if ($show_variants) { $variant_arr = []; if (!empty($meta['sizes']) && is_array($meta['sizes'])) { $variant_list = []; foreach ($meta['sizes'] as $size_name => $variant) { if (empty($variant['file'])) continue; $variant_file = $file ? path_join(dirname($file), $variant['file']) : ''; if ($variant_file && is_file($variant_file)) { $variant_list[] = [ 'size_name' => $size_name, 'file' => $variant['file'], 'size' => filesize($variant_file), ]; } } usort($variant_list, function($a, $b) { return $b['size'] - $a['size']; }); foreach ($variant_list as $v) { $variant_arr[] = "{$v['size_name']} ({$v['file']}): " . size_format((int)$v['size'], 2); } } $row[] = implode(' | ', $variant_arr); } fputcsv($out, $row); } } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- fclose() on php://output allowed fclose($out); exit; } public function render_options_page() { if (!current_user_can('manage_options')) { wp_die(esc_html__('Unauthorized', '103graphics-media-inspector'), '', ['response' => 403]); } $is_print = !empty($_GET['mfs_print_mode']); // STRICT NONCE: ONLY process filters if valid nonce present, else show default view $media_inspector_nonce = isset($_GET['media_inspector_nonce']) ? sanitize_text_field(wp_unslash($_GET['media_inspector_nonce'])) : ''; $filter_nonce_ok = $media_inspector_nonce && wp_verify_nonce($media_inspector_nonce, 'gmi_filter_search'); if ($filter_nonce_ok) { $count = isset($_GET['count']) && is_numeric($_GET['count']) && intval($_GET['count']) > 0 ? intval($_GET['count']) : 20; $min_kb = isset($_GET['min_kb']) && is_numeric($_GET['min_kb']) ? intval($_GET['min_kb']) : 0; $media_type = isset($_GET['media_type']) ? sanitize_text_field(wp_unslash($_GET['media_type'])) : 'all'; $show_variants = isset($_GET['show_variants']) && $_GET['show_variants'] === '1'; } else { $count = 20; $min_kb = 0; $media_type = 'all'; $show_variants = false; } $min_bytes = $min_kb * 1024; $mime_types = $this->get_mime_types_for($media_type); $args = $this->build_media_query_args($count, '', $mime_types, [], $min_bytes); $query = new WP_Query($args); $current_query = wp_unslash($_GET); if (isset($current_query['page'])) { $current_query['page'] = '103graphics-media-inspector'; } else { $current_query = array_merge($current_query, ['page' => '103graphics-media-inspector']); } $base_admin_url = add_query_arg($current_query, admin_url('upload.php')); $csv_url = add_query_arg('mfs_export_csv', '1', $base_admin_url); $csv_url = wp_nonce_url($csv_url, 'gmi_export_csv'); $print_url = add_query_arg('mfs_print_mode', '1', $base_admin_url); if ($show_variants) { $csv_url = add_query_arg('show_variants', '1', $csv_url); $print_url = add_query_arg('show_variants', '1', $print_url); } $reset_url = add_query_arg(['page' => '103graphics-media-inspector'], admin_url('upload.php')); ?>
| '; if ( $full_url ) { echo '' . ( $thumb_html ? wp_kses_post($thumb_html) : '' ) . ''; } else { echo $thumb_html ? wp_kses_post($thumb_html) : ''; } echo ' | '; echo '';
if ( $file && is_file( $file ) ) {
echo '' . esc_html( basename( $file ) ) . ': ' . esc_html( size_format( (int) filesize( $file ), 2 ) );
} else {
echo '' . esc_html( $attachment->post_title ) . '';
}
if ( $has_variants && $show_variants ) {
echo ' ' . ( $toggle_html ? wp_kses_post($toggle_html) : '' );
echo ' ' . ( $variants_output ? wp_kses_post($variants_output) : '' ) . ' ';
}
echo ' | ';
echo '' . ( $orig_size ? esc_html( size_format( (int) $orig_size, 2 ) ) : '—' ) . ' | '; echo '' . ( $total_size ? esc_html( size_format( (int) $total_size, 2 ) ) : '—' ) . ' | '; echo '