PluginProbe
WP Attachments / 6.0.3
WP Attachments v6.0.3
6.0.2 6.0.3 trunk 2.0 3 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.2 3.2.1 3.2.2 3.2.3 3.2.4 3.3 3.4 3.5 3.5.1 3.5.2 3.5.3 3.5.4 All 52 releases
wp-attachments / inc / counter.php

counter.php in WP Attachments 6.0.3, at inc/counter.php

79 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 if ( ! class_exists( 'WPA_Download_Counter' ) ) {
4 class WPA_Download_Counter {
5
6 public function __construct() {
7 add_filter( 'manage_media_columns', [ $this, 'add_download_column' ] );
8 add_action( 'manage_media_custom_column', [ $this, 'show_download_column' ], 10, 1 );
9 add_filter( 'manage_upload_sortable_columns', [ $this, 'register_sortable_column' ] );
10 add_filter( 'request', [ $this, 'handle_column_orderby' ] );
11 add_action( 'admin_head', [ $this, 'admin_column_css' ] );
12 }
13
14 /**
15 * Add Downloads column to Media Library.
16 */
17 public function add_download_column( $columns ) {
18 $columns['wpa-download'] = __( 'Downloads', 'wp-attachments' );
19 return $columns;
20 }
21
22 /**
23 * Output the Downloads column content.
24 */
25 public function show_download_column( $column_name ) {
26 global $post;
27 if ( $column_name === 'wpa-download' ) {
28 $downloads = (int) wpa_get_downloads( $post->ID );
29 printf(
30 '<span class="wpa-download-count" title="%s"><span class="dashicons dashicons-download"></span> %s</span>',
31 esc_attr__( 'Number of downloads', 'wp-attachments' ),
32 esc_html( number_format_i18n( $downloads ) )
33 );
34 }
35 }
36
37 /**
38 * Make the Downloads column sortable.
39 */
40 public function register_sortable_column( $columns ) {
41 $columns['wpa-download'] = 'wpa-download';
42 return $columns;
43 }
44
45 /**
46 * Handle sorting by Downloads column.
47 */
48 public function handle_column_orderby( $vars ) {
49 if ( isset( $vars['orderby'] ) && $vars['orderby'] === 'wpa-download' ) {
50 $vars = array_merge( $vars, [
51 'meta_key' => 'wpa-download',
52 'orderby' => 'meta_value_num',
53 ] );
54 }
55 return $vars;
56 }
57
58 /**
59 * Output custom CSS for the Downloads column in admin.
60 */
61 public function admin_column_css() {
62 $screen = function_exists('get_current_screen') ? get_current_screen() : null;
63 if (!$screen || $screen->base !== 'upload') {
64 return;
65 }
66
67 echo '<style>
68 .column-wpa-download { width: 110px; text-align: center; }
69 .wpa-download-count { display: inline-flex; align-items: center; gap: 4px; font-weight: 600; }
70 .wpa-download-count .dashicons { margin-right: 2px; }
71 </style>';
72 }
73 }
74
75 // Initialize the class.
76 new WPA_Download_Counter();
77 }
78
79 ?>