| 1 |
<?php |
| 2 |
/** |
| 3 |
* "Files" column for the post, page and custom post type list tables. |
| 4 |
* |
| 5 |
* Shows how many attachments belong to each row. The counts for the whole |
| 6 |
* screen are fetched with a single grouped query, not one per row. |
| 7 |
* |
| 8 |
* @package WP_Attachments |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Post types that get the column: the same ones that get the metabox. |
| 13 |
* |
| 14 |
* @return string[] |
| 15 |
*/ |
| 16 |
function wpatt_column_post_types() { |
| 17 |
$types = get_post_types( array( 'public' => true ), 'names' ); |
| 18 |
$out = array(); |
| 19 |
|
| 20 |
foreach ( $types as $type ) { |
| 21 |
if ( 'attachment' === $type ) { |
| 22 |
continue; |
| 23 |
} |
| 24 |
if ( get_option( 'wpatt_enable_metabox_' . $type, '1' ) !== '1' ) { |
| 25 |
continue; |
| 26 |
} |
| 27 |
$out[] = $type; |
| 28 |
} |
| 29 |
|
| 30 |
return $out; |
| 31 |
} |
| 32 |
|
| 33 |
foreach ( wpatt_column_post_types() as $wpatt_type ) { |
| 34 |
add_filter( "manage_{$wpatt_type}_posts_columns", 'wpatt_add_files_column' ); |
| 35 |
add_action( "manage_{$wpatt_type}_posts_custom_column", 'wpatt_render_files_column', 10, 2 ); |
| 36 |
} |
| 37 |
unset( $wpatt_type ); |
| 38 |
|
| 39 |
/** |
| 40 |
* Insert the column just before Date, the conventional slot for extra metadata. |
| 41 |
* |
| 42 |
* @param array $columns Existing columns. |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
function wpatt_add_files_column( $columns ) { |
| 46 |
$label = esc_html_x( 'Files', 'list table column heading', 'wp-attachments' ); |
| 47 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; |
| 48 |
$out = array(); |
| 49 |
|
| 50 |
// List switched off for the whole post type: say so once, here. |
| 51 |
if ( $screen && $screen->post_type && ! wpatt_is_frontend_enabled( $screen->post_type ) ) { |
| 52 |
$off = __( 'The list of files is not shown on the site for this content type.', 'wp-attachments' ); |
| 53 |
$label = '<span class="wpa-files-heading-off" title="' . esc_attr( $off ) . '">' . $label . wpatt_eye_off_svg() |
| 54 |
. '<span class="screen-reader-text">' . esc_html( $off ) . '</span></span>'; |
| 55 |
} |
| 56 |
|
| 57 |
foreach ( $columns as $key => $value ) { |
| 58 |
if ( 'date' === $key ) { |
| 59 |
$out['wpa_files'] = $label; |
| 60 |
} |
| 61 |
$out[ $key ] = $value; |
| 62 |
} |
| 63 |
|
| 64 |
if ( ! isset( $out['wpa_files'] ) ) { |
| 65 |
$out['wpa_files'] = $label; |
| 66 |
} |
| 67 |
|
| 68 |
return $out; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Attachment counts for every row currently on screen. |
| 73 |
* |
| 74 |
* Primed once from the main query and reused, so a 20 row screen costs one |
| 75 |
* extra query rather than twenty. |
| 76 |
* |
| 77 |
* @return array<int,int> Parent post ID => number of attachments. |
| 78 |
*/ |
| 79 |
function wpatt_get_attachment_counts() { |
| 80 |
static $counts = null; |
| 81 |
|
| 82 |
if ( null !== $counts ) { |
| 83 |
return $counts; |
| 84 |
} |
| 85 |
|
| 86 |
global $wp_query, $wpdb; |
| 87 |
|
| 88 |
$counts = array(); |
| 89 |
$ids = array(); |
| 90 |
|
| 91 |
if ( isset( $wp_query->posts ) && is_array( $wp_query->posts ) ) { |
| 92 |
foreach ( $wp_query->posts as $post ) { |
| 93 |
$ids[] = is_object( $post ) ? (int) $post->ID : (int) $post; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$ids = array_filter( array_unique( $ids ) ); |
| 98 |
|
| 99 |
if ( empty( $ids ) ) { |
| 100 |
return $counts; |
| 101 |
} |
| 102 |
|
| 103 |
$placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); |
| 104 |
|
| 105 |
// Trashed attachments are excluded, to match what the metabox lists. |
| 106 |
$rows = $wpdb->get_results( |
| 107 |
$wpdb->prepare( |
| 108 |
"SELECT post_parent, COUNT(*) AS total |
| 109 |
FROM {$wpdb->posts} |
| 110 |
WHERE post_type = 'attachment' |
| 111 |
AND post_status != 'trash' |
| 112 |
AND post_parent IN ($placeholders) |
| 113 |
GROUP BY post_parent", |
| 114 |
$ids |
| 115 |
) |
| 116 |
); |
| 117 |
|
| 118 |
foreach ( $rows as $row ) { |
| 119 |
$counts[ (int) $row->post_parent ] = (int) $row->total; |
| 120 |
} |
| 121 |
|
| 122 |
return $counts; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Render one cell. |
| 127 |
* |
| 128 |
* @param string $column Column key. |
| 129 |
* @param int $post_id Row post ID. |
| 130 |
*/ |
| 131 |
function wpatt_render_files_column( $column, $post_id ) { |
| 132 |
if ( 'wpa_files' !== $column ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$counts = wpatt_get_attachment_counts(); |
| 137 |
$count = isset( $counts[ $post_id ] ) ? $counts[ $post_id ] : 0; |
| 138 |
|
| 139 |
if ( ! $count ) { |
| 140 |
echo '<span class="wpa-files-count is-zero" aria-hidden="true">—</span>'; |
| 141 |
echo '<span class="screen-reader-text">' . esc_html__( 'No files attached', 'wp-attachments' ) . '</span>'; |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
// Media Library, filtered to this parent. WordPress passes post_parent |
| 146 |
// from the query string straight into the attachments query, so no extra |
| 147 |
// handling is needed at the other end. |
| 148 |
$url = add_query_arg( |
| 149 |
array( |
| 150 |
'mode' => 'list', |
| 151 |
'post_parent' => (int) $post_id, |
| 152 |
), |
| 153 |
admin_url( 'upload.php' ) |
| 154 |
); |
| 155 |
|
| 156 |
$label = sprintf( |
| 157 |
/* translators: %s: number of attached files. */ |
| 158 |
_n( 'View %s attached file', 'View %s attached files', $count, 'wp-attachments' ), |
| 159 |
number_format_i18n( $count ) |
| 160 |
); |
| 161 |
|
| 162 |
printf( |
| 163 |
'<a class="wpa-files-link" href="%1$s" title="%2$s">%3$s<span aria-hidden="true">%4$s</span><span class="screen-reader-text">%2$s</span></a>', |
| 164 |
esc_url( $url ), |
| 165 |
esc_attr( $label ), |
| 166 |
wpatt_paperclip_svg(), |
| 167 |
esc_html( number_format_i18n( $count ) ) |
| 168 |
); |
| 169 |
|
| 170 |
// Whether this post shows its list on the site, as set in its metabox. |
| 171 |
// When the whole post type is off, every row would carry the same mark, |
| 172 |
// so the column heading explains it once instead. |
| 173 |
if ( ! wpatt_is_frontend_enabled( get_post_type( $post_id ) ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$shown = wpatt_is_display_enabled( $post_id ); |
| 178 |
$state = $shown ? __( 'Shown on the site', 'wp-attachments' ) : __( 'Not shown on the site', 'wp-attachments' ); |
| 179 |
|
| 180 |
printf( |
| 181 |
'<span class="wpa-files-state %1$s" title="%2$s">%3$s<span class="screen-reader-text">%2$s</span></span>', |
| 182 |
$shown ? 'is-shown' : 'is-hidden', |
| 183 |
esc_attr( $state ), |
| 184 |
$shown ? wpatt_eye_svg() : wpatt_eye_off_svg() |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Eye: the list is shown on the frontend for this post. |
| 190 |
*/ |
| 191 |
function wpatt_eye_svg() { |
| 192 |
return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"' |
| 193 |
. ' stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">' |
| 194 |
. '<path d="M2.5 12S6 5.75 12 5.75 21.5 12 21.5 12 18 18.25 12 18.25 2.5 12 2.5 12Z"/>' |
| 195 |
. '<circle cx="12" cy="12" r="2.75"/>' |
| 196 |
. '</svg>'; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Crossed-out eye: the list is hidden on the frontend for this post. |
| 201 |
*/ |
| 202 |
function wpatt_eye_off_svg() { |
| 203 |
return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"' |
| 204 |
. ' stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">' |
| 205 |
. '<path d="M10.6 5.8A9.8 9.8 0 0 1 12 5.75c6 0 9.5 6.25 9.5 6.25a17 17 0 0 1-2.3 3.1M6.5 7.4C3.9 9.1 2.5 12 2.5 12s3.5 6.25 9.5 6.25a9.6 9.6 0 0 0 4.6-1.15"/>' |
| 206 |
. '<path d="M9.9 9.9a3 3 0 0 0 4.2 4.2"/>' |
| 207 |
. '<path d="M3 3l18 18"/>' |
| 208 |
. '</svg>'; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Paperclip mark. Without it the cell is a bare digit that could be a count of |
| 213 |
* anything; with it the column reads at a glance. |
| 214 |
*/ |
| 215 |
function wpatt_paperclip_svg() { |
| 216 |
return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"' |
| 217 |
. ' stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">' |
| 218 |
. '<path d="M21 11.5 12.5 20a5 5 0 0 1-7-7l8.5-8.5a3.5 3.5 0 0 1 5 5L10.5 18a2 2 0 0 1-3-3l8-8"/>' |
| 219 |
. '</svg>'; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Tell people why the Media Library is showing a subset. |
| 224 |
* |
| 225 |
* Landing on a filtered list with no explanation is disorienting: WordPress |
| 226 |
* gives no hint that post_parent is in play. |
| 227 |
*/ |
| 228 |
add_action( |
| 229 |
'admin_notices', |
| 230 |
function () { |
| 231 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; |
| 232 |
|
| 233 |
if ( ! $screen || 'upload' !== $screen->base || empty( $_GET['post_parent'] ) ) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
$parent = get_post( absint( wp_unslash( $_GET['post_parent'] ) ) ); |
| 238 |
|
| 239 |
if ( ! $parent ) { |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
printf( |
| 244 |
'<div class="notice notice-info"><p>%1$s <a href="%2$s">%3$s</a></p></div>', |
| 245 |
esc_html( |
| 246 |
sprintf( |
| 247 |
/* translators: %s: title of the post the files are attached to. */ |
| 248 |
__( 'Showing only the files attached to "%s".', 'wp-attachments' ), |
| 249 |
get_the_title( $parent ) |
| 250 |
) |
| 251 |
), |
| 252 |
esc_url( remove_query_arg( 'post_parent' ) ), |
| 253 |
esc_html__( 'Show all files', 'wp-attachments' ) |
| 254 |
); |
| 255 |
} |
| 256 |
); |
| 257 |
|
| 258 |
/** |
| 259 |
* Files filter above the list table, next to the date and category ones. |
| 260 |
* |
| 261 |
* A native select (it combines with the other filters and keeps working |
| 262 |
* with the core Filter button), followed by a legend with the same icons the |
| 263 |
* column uses -- options of a select cannot hold icons. |
| 264 |
*/ |
| 265 |
add_action( |
| 266 |
'restrict_manage_posts', |
| 267 |
function ( $post_type, $which = 'top' ) { |
| 268 |
if ( 'top' !== $which || ! in_array( $post_type, wpatt_column_post_types(), true ) ) { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
$frontend = wpatt_is_frontend_enabled( $post_type ); |
| 273 |
$current = wpatt_files_filter_mode(); |
| 274 |
|
| 275 |
$options = array( |
| 276 |
'' => __( 'All files', 'wp-attachments' ), |
| 277 |
'with' => __( 'With files', 'wp-attachments' ), |
| 278 |
'without' => __( 'Without files', 'wp-attachments' ), |
| 279 |
); |
| 280 |
if ( $frontend ) { |
| 281 |
$options['shown'] = __( 'Shown on the site', 'wp-attachments' ); |
| 282 |
$options['hidden'] = __( 'Not shown on the site', 'wp-attachments' ); |
| 283 |
} |
| 284 |
|
| 285 |
echo '<label for="wpa-files-filter" class="screen-reader-text">' . esc_html__( 'Filter by attached files', 'wp-attachments' ) . '</label>'; |
| 286 |
echo '<select name="wpa_files" id="wpa-files-filter">'; |
| 287 |
foreach ( $options as $value => $label ) { |
| 288 |
printf( '<option value="%1$s"%2$s>%3$s</option>', esc_attr( $value ), selected( $current, $value, false ), esc_html( $label ) ); |
| 289 |
} |
| 290 |
echo '</select>'; |
| 291 |
|
| 292 |
// Decorative: the cells carry the same information as text. |
| 293 |
echo '<span class="wpa-files-legend" aria-hidden="true">'; |
| 294 |
echo '<span>' . wpatt_paperclip_svg() . esc_html__( 'Files', 'wp-attachments' ) . '</span>'; |
| 295 |
if ( $frontend ) { |
| 296 |
echo '<span>' . wpatt_eye_svg() . esc_html__( 'Shown', 'wp-attachments' ) . '</span>'; |
| 297 |
echo '<span>' . wpatt_eye_off_svg() . esc_html__( 'Not shown', 'wp-attachments' ) . '</span>'; |
| 298 |
} |
| 299 |
echo '</span>'; |
| 300 |
}, |
| 301 |
10, |
| 302 |
2 |
| 303 |
); |
| 304 |
|
| 305 |
/** |
| 306 |
* Current value of the Files filter, or '' when it is not in use. |
| 307 |
* |
| 308 |
* @return string |
| 309 |
*/ |
| 310 |
function wpatt_files_filter_mode() { |
| 311 |
$mode = isset( $_GET['wpa_files'] ) ? sanitize_key( wp_unslash( $_GET['wpa_files'] ) ) : ''; |
| 312 |
|
| 313 |
return in_array( $mode, array( 'with', 'without', 'shown', 'hidden' ), true ) ? $mode : ''; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Apply the Files filter to the main list table query. |
| 318 |
*/ |
| 319 |
add_action( |
| 320 |
'pre_get_posts', |
| 321 |
function ( $query ) { |
| 322 |
global $pagenow, $typenow; |
| 323 |
|
| 324 |
if ( ! is_admin() || ! $query->is_main_query() || 'edit.php' !== $pagenow ) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
$post_type = $typenow ? $typenow : 'post'; |
| 329 |
$mode = wpatt_files_filter_mode(); |
| 330 |
|
| 331 |
if ( ! $mode || ! in_array( $post_type, wpatt_column_post_types(), true ) ) { |
| 332 |
return; |
| 333 |
} |
| 334 |
// Shown / not shown mean nothing while the whole post type is off. |
| 335 |
if ( in_array( $mode, array( 'shown', 'hidden' ), true ) && ! wpatt_is_frontend_enabled( $post_type ) ) { |
| 336 |
return; |
| 337 |
} |
| 338 |
|
| 339 |
$query->set( 'wpa_files_filter', $mode ); |
| 340 |
|
| 341 |
// Same rule as wpatt_is_display_enabled(): only '1' hides the list. |
| 342 |
if ( 'hidden' === $mode || 'shown' === $mode ) { |
| 343 |
$meta_query = (array) $query->get( 'meta_query' ); |
| 344 |
$meta_query[] = ( 'hidden' === $mode ) |
| 345 |
? array( 'key' => 'wpa_off', 'value' => '1' ) |
| 346 |
: array( |
| 347 |
'relation' => 'OR', |
| 348 |
array( 'key' => 'wpa_off', 'compare' => 'NOT EXISTS' ), |
| 349 |
array( 'key' => 'wpa_off', 'value' => '1', 'compare' => '!=' ), |
| 350 |
); |
| 351 |
$query->set( 'meta_query', $meta_query ); |
| 352 |
} |
| 353 |
} |
| 354 |
); |
| 355 |
|
| 356 |
/** |
| 357 |
* "Has attachments" condition for the Files filter. |
| 358 |
* |
| 359 |
* Same rule as the column count: attachments that are not in the trash. |
| 360 |
* Shown / not shown only cover posts with files, matching the eye icons. |
| 361 |
*/ |
| 362 |
add_filter( |
| 363 |
'posts_where', |
| 364 |
function ( $where, $query ) { |
| 365 |
$mode = $query->get( 'wpa_files_filter' ); |
| 366 |
if ( ! $mode ) { |
| 367 |
return $where; |
| 368 |
} |
| 369 |
|
| 370 |
global $wpdb; |
| 371 |
|
| 372 |
$exists = "EXISTS (SELECT 1 FROM {$wpdb->posts} AS wpa_att" |
| 373 |
. " WHERE wpa_att.post_parent = {$wpdb->posts}.ID" |
| 374 |
. " AND wpa_att.post_type = 'attachment'" |
| 375 |
. " AND wpa_att.post_status != 'trash')"; |
| 376 |
|
| 377 |
return $where . ' AND ' . ( 'without' === $mode ? 'NOT ' : '' ) . $exists; |
| 378 |
}, |
| 379 |
10, |
| 380 |
2 |
| 381 |
); |
| 382 |
|
| 383 |
/** |
| 384 |
* Column styling. Loaded only on the list tables that show it. |
| 385 |
*/ |
| 386 |
add_action( |
| 387 |
'admin_enqueue_scripts', |
| 388 |
function () { |
| 389 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; |
| 390 |
|
| 391 |
if ( ! $screen || 'edit' !== $screen->base ) { |
| 392 |
return; |
| 393 |
} |
| 394 |
if ( ! in_array( $screen->post_type, wpatt_column_post_types(), true ) ) { |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
wp_register_style( 'wpa-post-columns', false, array(), WPATT_VERSION ); |
| 399 |
wp_enqueue_style( 'wpa-post-columns' ); |
| 400 |
wp_add_inline_style( |
| 401 |
'wpa-post-columns', |
| 402 |
/* .widefat th wins over a bare .column-* selector, so the header |
| 403 |
needs the extra specificity or it stays left aligned. */ |
| 404 |
'.widefat th.column-wpa_files,.widefat td.column-wpa_files{width:6.5em;text-align:center;} |
| 405 |
.wpa-files-count{font-variant-numeric:tabular-nums;} |
| 406 |
.wpa-files-count.is-zero{color:#a7aaad;} |
| 407 |
.wpa-files-link{display:inline-flex;align-items:center;gap:4px;text-decoration:none;font-variant-numeric:tabular-nums;} |
| 408 |
.wpa-files-link svg{width:13px;height:13px;flex-shrink:0;opacity:.65;} |
| 409 |
.wpa-files-link:hover svg,.wpa-files-link:focus svg{opacity:1;} |
| 410 |
.wpa-files-state,.wpa-files-heading-off{display:inline-flex;align-items:center;gap:4px;vertical-align:middle;} |
| 411 |
.wpa-files-state{margin-left:6px;} |
| 412 |
.wpa-files-state.is-shown{color:#a7aaad;} |
| 413 |
.wpa-files-state.is-hidden{color:#646970;} |
| 414 |
.wpa-files-state svg,.wpa-files-heading-off svg{width:14px;height:14px;flex-shrink:0;} |
| 415 |
.tablenav .wpa-files-legend{display:inline-flex;align-items:center;gap:10px;height:30px;margin:0 10px 0 2px;color:#646970;font-size:12px;vertical-align:middle;} |
| 416 |
.wpa-files-legend>span{display:inline-flex;align-items:center;gap:3px;} |
| 417 |
.wpa-files-legend svg{width:13px;height:13px;flex-shrink:0;} |
| 418 |
@media screen and (max-width:782px){ |
| 419 |
.tablenav .wpa-files-legend{display:none;} |
| 420 |
.widefat th.column-wpa_files,.widefat td.column-wpa_files{width:auto;text-align:left;} |
| 421 |
.wpa-files-link{display:inline-flex;} |
| 422 |
}' |
| 423 |
); |
| 424 |
} |
| 425 |
); |
| 426 |
|