Skip to content

Commit

Permalink
Full Site Editing: Update the template parts admin list with new colu…
Browse files Browse the repository at this point in the history
…mns and views (#27156)

* Show the same data in both wp_template and wp_template_part admin lists

* Move the templates admin lists hooks in a separate utilities file
  • Loading branch information
Copons authored Nov 23, 2020
1 parent d44a082 commit c361b63
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 143 deletions.
118 changes: 118 additions & 0 deletions lib/full-site-editing/templates-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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['slug'] = __( 'Slug', 'gutenberg' );
$columns['description'] = __( 'Description', 'gutenberg' );
$columns['status'] = __( 'Status', '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 ( 'slug' === $column_name ) {
$post = get_post( $post_id );
echo esc_html( $post->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( '<br />', $themes );
if ( $is_file_based ) {
echo '<br />' . __( '(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 <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;
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
36 changes: 5 additions & 31 deletions lib/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ function gutenberg_register_template_part_post_type() {
'supports' => array(
'title',
'slug',
'excerpt',
'editor',
'revisions',
'custom-fields',
),
);

Expand Down Expand Up @@ -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.
Expand Down
115 changes: 3 additions & 112 deletions lib/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<br />', $themes );
if ( $is_file_based ) {
echo '<br />' . __( '(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 <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;
}
// 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' );

/**
Expand Down

0 comments on commit c361b63

Please sign in to comment.