PluginProbe
Gutenberg / 9.8.0
Gutenberg v9.8.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / full-site-editing / templates-utils.php

templates-utils.php in Gutenberg 9.8.0, at lib/full-site-editing/templates-utils.php

114 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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['slug'] = __( 'Slug', 'gutenberg' );
16 $columns['description'] = __( 'Description', 'gutenberg' );
17 $columns['status'] = __( 'Status', 'gutenberg' );
18 $columns['theme'] = __( 'Theme', 'gutenberg' );
19 if ( isset( $columns['date'] ) ) {
20 unset( $columns['date'] );
21 }
22 return $columns;
23 }
24
25 /**
26 * Renders content for the templates and template parts admin list custom columns.
27 *
28 * @param string $column_name Column name to render.
29 * @param int $post_id Post ID.
30 */
31 function gutenberg_render_templates_lists_custom_column( $column_name, $post_id ) {
32 if ( 'slug' === $column_name ) {
33 $post = get_post( $post_id );
34 echo esc_html( $post->post_name );
35 return;
36 }
37
38 if ( 'description' === $column_name && has_excerpt( $post_id ) ) {
39 the_excerpt( $post_id );
40 return;
41 }
42
43 if ( 'status' === $column_name ) {
44 $post_status = get_post_status( $post_id );
45 // The auto-draft status doesn't have localized labels.
46 if ( 'auto-draft' === $post_status ) {
47 echo esc_html_x( 'Auto-Draft', 'Post status', 'gutenberg' );
48 return;
49 }
50 $post_status_object = get_post_status_object( $post_status );
51 echo esc_html( $post_status_object->label );
52 return;
53 }
54
55 if ( 'theme' === $column_name ) {
56 $terms = get_the_terms( $post_id, 'wp_theme' );
57 if ( empty( $terms ) || is_wp_error( $terms ) ) {
58 return;
59 }
60 $themes = array();
61 foreach ( $terms as $term ) {
62 $themes[] = esc_html( wp_get_theme( $term->slug ) );
63 }
64 echo implode( '<br />', $themes );
65 return;
66 }
67 }
68
69 /**
70 * Adds the auto-draft view to the templates and template parts admin lists.
71 *
72 * @param array $views The edit views to filter.
73 */
74 function gutenberg_filter_templates_edit_views( $views ) {
75 $post_type = get_current_screen()->post_type;
76 $url = add_query_arg(
77 array(
78 'post_type' => $post_type,
79 'post_status' => 'auto-draft',
80 ),
81 'edit.php'
82 );
83 $is_auto_draft_view = isset( $_REQUEST['post_status'] ) && 'auto-draft' === $_REQUEST['post_status'];
84 $class_html = $is_auto_draft_view ? ' class="current"' : '';
85 $aria_current = $is_auto_draft_view ? ' aria-current="page"' : '';
86 $post_count = wp_count_posts( $post_type, 'readable' );
87 $label = sprintf(
88 // The auto-draft status doesn't have localized labels.
89 translate_nooped_plural(
90 /* translators: %s: Number of auto-draft posts. */
91 _nx_noop(
92 'Auto-Draft <span class="count">(%s)</span>',
93 'Auto-Drafts <span class="count">(%s)</span>',
94 'Post status',
95 'gutenberg'
96 ),
97 $post_count->{'auto-draft'}
98 ),
99 number_format_i18n( $post_count->{'auto-draft'} )
100 );
101
102 $auto_draft_view = sprintf(
103 '<a href="%s"%s%s>%s</a>',
104 esc_url( $url ),
105 $class_html,
106 $aria_current,
107 $label
108 );
109
110 array_splice( $views, 1, 0, array( 'auto-draft' => $auto_draft_view ) );
111
112 return $views;
113 }
114