diff --git a/lib/full-site-editing/templates-utils.php b/lib/full-site-editing/templates-utils.php new file mode 100644 index 00000000000000..62fb7f6da83bbc --- /dev/null +++ b/lib/full-site-editing/templates-utils.php @@ -0,0 +1,118 @@ +post_name ); + return; + } + + if ( 'description' === $column_name && has_excerpt( $post_id ) ) { + the_excerpt( $post_id ); + return; + } + + if ( 'status' === $column_name ) { + $post_status = get_post_status( $post_id ); + // The auto-draft status doesn't have localized labels. + if ( 'auto-draft' === $post_status ) { + echo esc_html_x( 'Auto-Draft', 'Post status', 'gutenberg' ); + return; + } + $post_status_object = get_post_status_object( $post_status ); + echo esc_html( $post_status_object->label ); + return; + } + + if ( 'theme' === $column_name ) { + $terms = get_the_terms( $post_id, 'wp_theme' ); + $themes = array(); + $is_file_based = false; + foreach ( $terms as $term ) { + if ( '_wp_file_based' === $term->slug ) { + $is_file_based = true; + } else { + $themes[] = esc_html( wp_get_theme( $term->slug ) ); + } + } + echo implode( '
', $themes ); + if ( $is_file_based ) { + echo '
' . __( '(Created from a template file)', 'gutenberg' ); + } + 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 (%s)', + 'Auto-Drafts (%s)', + 'Post status', + 'gutenberg' + ), + $post_count->{'auto-draft'} + ), + number_format_i18n( $post_count->{'auto-draft'} ) + ); + + $auto_draft_view = sprintf( + '%s', + esc_url( $url ), + $class_html, + $aria_current, + $label + ); + + array_splice( $views, 1, 0, array( 'auto-draft' => $auto_draft_view ) ); + + return $views; +} diff --git a/lib/load.php b/lib/load.php index d8b89f7ba0200c..9227918378a0fd 100644 --- a/lib/load.php +++ b/lib/load.php @@ -106,6 +106,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/full-site-editing.php'; require __DIR__ . '/full-site-editing/default-template-types.php'; +require __DIR__ . '/full-site-editing/templates-utils.php'; require __DIR__ . '/templates-sync.php'; require __DIR__ . '/templates.php'; require __DIR__ . '/template-parts.php'; diff --git a/lib/template-parts.php b/lib/template-parts.php index 11532570786a5b..464200f66d000a 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -49,9 +49,9 @@ function gutenberg_register_template_part_post_type() { 'supports' => array( 'title', 'slug', + 'excerpt', 'editor', 'revisions', - 'custom-fields', ), ); @@ -104,36 +104,10 @@ function gutenberg_fix_template_part_admin_menu_entry() { } add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' ); -/** - * Filters the 'wp_template_part' post type columns in the admin list table. - * - * @param array $columns Columns to display. - * @return array Filtered $columns. - */ -function gutenberg_filter_template_part_list_table_columns( array $columns ) { - $columns['slug'] = __( 'Slug', 'gutenberg' ); - if ( isset( $columns['date'] ) ) { - unset( $columns['date'] ); - } - return $columns; -} -add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_filter_template_part_list_table_columns' ); - -/** - * Renders column content for the 'wp_template_part' post type list table. - * - * @param string $column_name Column name to render. - * @param int $post_id Post ID. - */ -function gutenberg_render_template_part_list_table_column( $column_name, $post_id ) { - if ( 'slug' !== $column_name ) { - return; - } - $post = get_post( $post_id ); - echo esc_html( $post->post_name ); -} -add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_template_part_list_table_column', 10, 2 ); - +// Customize the `wp_template` admin list. +add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_templates_lists_custom_columns' ); +add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 ); +add_filter( 'views_edit-wp_template_part', 'gutenberg_filter_templates_edit_views' ); /** * Filter for adding and a `theme` parameter to `wp_template_part` queries. diff --git a/lib/templates.php b/lib/templates.php index d99d146e9eff42..aa085c0d54d125 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -192,118 +192,9 @@ function gutenberg_fix_template_admin_menu_entry() { } add_action( 'admin_menu', 'gutenberg_fix_template_admin_menu_entry' ); -/** - * Filters the 'wp_template' post type columns in the admin list table. - * - * @param array $columns Columns to display. - * @return array Filtered $columns. - */ -function gutenberg_filter_template_list_table_columns( array $columns ) { - $columns['slug'] = __( 'Slug', 'gutenberg' ); - $columns['description'] = __( 'Description', 'gutenberg' ); - $columns['status'] = __( 'Status', 'gutenberg' ); - $columns['theme'] = __( 'Theme', 'gutenberg' ); - if ( isset( $columns['date'] ) ) { - unset( $columns['date'] ); - } - return $columns; -} -add_filter( 'manage_wp_template_posts_columns', 'gutenberg_filter_template_list_table_columns' ); - -/** - * Renders column content for the 'wp_template' post type list table. - * - * @param string $column_name Column name to render. - * @param int $post_id Post ID. - */ -function gutenberg_render_template_list_table_column( $column_name, $post_id ) { - if ( 'slug' === $column_name ) { - $post = get_post( $post_id ); - echo esc_html( $post->post_name ); - return; - } - - if ( 'description' === $column_name ) { - the_excerpt( $post_id ); - return; - } - - if ( 'status' === $column_name ) { - $post_status = get_post_status( $post_id ); - // The auto-draft status doesn't have localized labels. - if ( 'auto-draft' === $post_status ) { - echo esc_html_x( 'Auto-Draft', 'Post status', 'gutenberg' ); - return; - } - $post_status_object = get_post_status_object( $post_status ); - echo esc_html( $post_status_object->label ); - return; - } - - if ( 'theme' === $column_name ) { - $terms = get_the_terms( $post_id, 'wp_theme' ); - $themes = array(); - $is_file_based = false; - foreach ( $terms as $term ) { - if ( '_wp_file_based' === $term->slug ) { - $is_file_based = true; - } else { - $themes[] = esc_html( wp_get_theme( $term->slug ) ); - } - } - echo implode( '
', $themes ); - if ( $is_file_based ) { - echo '
' . __( '(Created from a template file)', 'gutenberg' ); - } - return; - } -} -add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_template_list_table_column', 10, 2 ); - -/** - * Adds the auto-draft view to the 'wp_template' post type list. - * - * @param array $views The edit views to filter. - */ -function gutenberg_filter_templates_edit_views( $views ) { - $url = add_query_arg( - array( - 'post_type' => 'wp_template', - '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( 'wp_template', '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 (%s)', - 'Auto-Drafts (%s)', - 'Post status', - 'gutenberg' - ), - $post_count->{'auto-draft'} - ), - number_format_i18n( $post_count->{'auto-draft'} ) - ); - - $auto_draft_view = sprintf( - '%s', - esc_url( $url ), - $class_html, - $aria_current, - $label - ); - - array_splice( $views, 1, 0, array( 'auto-draft' => $auto_draft_view ) ); - - return $views; -} +// Customize the `wp_template` admin list. +add_filter( 'manage_wp_template_posts_columns', 'gutenberg_templates_lists_custom_columns' ); +add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 ); add_filter( 'views_edit-wp_template', 'gutenberg_filter_templates_edit_views' ); /**