| 1 |
<?php |
| 2 |
/** |
| 3 |
* Templates and template parts utilities. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Filters the admin list columns to add those relevant to templates and template parts. |
| 10 |
* |
| 11 |
* @param array $columns Columns to display. |
| 12 |
* @return array Filtered $columns. |
| 13 |
*/ |
| 14 |
function gutenberg_templates_lists_custom_columns( array $columns ) { |
| 15 |
$columns['description'] = __( 'Description', 'gutenberg' ); |
| 16 |
$columns['theme'] = __( 'Theme', 'gutenberg' ); |
| 17 |
if ( isset( $columns['date'] ) ) { |
| 18 |
unset( $columns['date'] ); |
| 19 |
} |
| 20 |
return $columns; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Renders content for the templates and template parts admin list custom columns. |
| 25 |
* |
| 26 |
* @param string $column_name Column name to render. |
| 27 |
* @param int $post_id Post ID. |
| 28 |
*/ |
| 29 |
function gutenberg_render_templates_lists_custom_column( $column_name, $post_id ) { |
| 30 |
if ( 'description' === $column_name && has_excerpt( $post_id ) ) { |
| 31 |
the_excerpt( $post_id ); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
if ( 'theme' === $column_name ) { |
| 36 |
$terms = get_the_terms( $post_id, 'wp_theme' ); |
| 37 |
if ( empty( $terms ) || is_wp_error( $terms ) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
$themes = array(); |
| 41 |
foreach ( $terms as $term ) { |
| 42 |
$themes[] = esc_html( wp_get_theme( $term->slug ) ); |
| 43 |
} |
| 44 |
echo implode( '<br />', $themes ); |
| 45 |
return; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Adds the auto-draft view to the templates and template parts admin lists. |
| 51 |
* |
| 52 |
* @param array $views The edit views to filter. |
| 53 |
*/ |
| 54 |
function gutenberg_filter_templates_edit_views( $views ) { |
| 55 |
$post_type = get_current_screen()->post_type; |
| 56 |
$url = add_query_arg( |
| 57 |
array( |
| 58 |
'post_type' => $post_type, |
| 59 |
'post_status' => 'auto-draft', |
| 60 |
), |
| 61 |
'edit.php' |
| 62 |
); |
| 63 |
$is_auto_draft_view = isset( $_REQUEST['post_status'] ) && 'auto-draft' === $_REQUEST['post_status']; |
| 64 |
$class_html = $is_auto_draft_view ? ' class="current"' : ''; |
| 65 |
$aria_current = $is_auto_draft_view ? ' aria-current="page"' : ''; |
| 66 |
$post_count = wp_count_posts( $post_type, 'readable' ); |
| 67 |
$label = sprintf( |
| 68 |
// The auto-draft status doesn't have localized labels. |
| 69 |
translate_nooped_plural( |
| 70 |
/* translators: %s: Number of auto-draft posts. */ |
| 71 |
_nx_noop( |
| 72 |
'Auto-Draft <span class="count">(%s)</span>', |
| 73 |
'Auto-Drafts <span class="count">(%s)</span>', |
| 74 |
'Post status', |
| 75 |
'gutenberg' |
| 76 |
), |
| 77 |
$post_count->{'auto-draft'} |
| 78 |
), |
| 79 |
number_format_i18n( $post_count->{'auto-draft'} ) |
| 80 |
); |
| 81 |
|
| 82 |
$auto_draft_view = sprintf( |
| 83 |
'<a href="%s"%s%s>%s</a>', |
| 84 |
esc_url( $url ), |
| 85 |
$class_html, |
| 86 |
$aria_current, |
| 87 |
$label |
| 88 |
); |
| 89 |
|
| 90 |
array_splice( $views, 1, 0, array( 'auto-draft' => $auto_draft_view ) ); |
| 91 |
|
| 92 |
return $views; |
| 93 |
} |
| 94 |
|