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