PluginProbe
Gutenberg / 7.4.0
Gutenberg v7.4.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 7.4.0 All 402 releases
gutenberg / lib / template-parts.php

template-parts.php in Gutenberg 7.4.0, at lib/template-parts.php

121 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block template part functions.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Registers block editor 'wp_template_part' post type.
10 */
11 function gutenberg_register_template_part_post_type() {
12 if ( ! gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ) ) {
13 return;
14 }
15
16 $labels = array(
17 'name' => __( 'Template Parts', 'gutenberg' ),
18 'singular_name' => __( 'Template Part', 'gutenberg' ),
19 'menu_name' => _x( 'Template Parts', 'Admin Menu text', 'gutenberg' ),
20 'add_new' => _x( 'Add New', 'Template Part', 'gutenberg' ),
21 'add_new_item' => __( 'Add New Template Part', 'gutenberg' ),
22 'new_item' => __( 'New Template Part', 'gutenberg' ),
23 'edit_item' => __( 'Edit Template Part', 'gutenberg' ),
24 'view_item' => __( 'View Template Part', 'gutenberg' ),
25 'all_items' => __( 'All Template Parts', 'gutenberg' ),
26 'search_items' => __( 'Search Template Parts', 'gutenberg' ),
27 'parent_item_colon' => __( 'Parent Template Part:', 'gutenberg' ),
28 'not_found' => __( 'No template parts found.', 'gutenberg' ),
29 'not_found_in_trash' => __( 'No template parts found in Trash.', 'gutenberg' ),
30 'archives' => __( 'Template part archives', 'gutenberg' ),
31 'insert_into_item' => __( 'Insert into template part', 'gutenberg' ),
32 'uploaded_to_this_item' => __( 'Uploaded to this template part', 'gutenberg' ),
33 'filter_items_list' => __( 'Filter template parts list', 'gutenberg' ),
34 'items_list_navigation' => __( 'Template parts list navigation', 'gutenberg' ),
35 'items_list' => __( 'Template parts list', 'gutenberg' ),
36 );
37
38 $args = array(
39 'labels' => $labels,
40 'description' => __( 'Template parts to include in your templates.', 'gutenberg' ),
41 'public' => false,
42 'has_archive' => false,
43 'show_ui' => true,
44 'show_in_menu' => 'themes.php',
45 'show_in_admin_bar' => false,
46 'show_in_rest' => true,
47 'rest_base' => 'template-parts',
48 'map_meta_cap' => true,
49 'supports' => array(
50 'title',
51 'slug',
52 'editor',
53 'revisions',
54 'custom-fields',
55 ),
56 );
57
58 $meta_args = array(
59 'object_subtype' => 'wp_template_part',
60 'type' => 'string',
61 'description' => 'The theme that provided the template part, if any.',
62 'single' => true,
63 'show_in_rest' => true,
64 );
65
66 register_post_type( 'wp_template_part', $args );
67 register_meta( 'post', 'theme', $meta_args );
68 }
69 add_action( 'init', 'gutenberg_register_template_part_post_type' );
70
71 /**
72 * Fixes the label of the 'wp_template_part' admin menu entry.
73 */
74 function gutenberg_fix_template_part_admin_menu_entry() {
75 global $submenu;
76 if ( ! isset( $submenu['themes.php'] ) ) {
77 return;
78 }
79 $post_type = get_post_type_object( 'wp_template_part' );
80 if ( ! $post_type ) {
81 return;
82 }
83 foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
84 if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
85 $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
86 break;
87 }
88 }
89 }
90 add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' );
91
92 /**
93 * Filters the 'wp_template_part' post type columns in the admin list table.
94 *
95 * @param array $columns Columns to display.
96 * @return array Filtered $columns.
97 */
98 function gutenberg_filter_template_part_list_table_columns( array $columns ) {
99 $columns['slug'] = __( 'Slug', 'gutenberg' );
100 if ( isset( $columns['date'] ) ) {
101 unset( $columns['date'] );
102 }
103 return $columns;
104 }
105 add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_filter_template_part_list_table_columns' );
106
107 /**
108 * Renders column content for the 'wp_template_part' post type list table.
109 *
110 * @param string $column_name Column name to render.
111 * @param int $post_id Post ID.
112 */
113 function gutenberg_render_template_part_list_table_column( $column_name, $post_id ) {
114 if ( 'slug' !== $column_name ) {
115 return;
116 }
117 $post = get_post( $post_id );
118 echo esc_html( $post->post_name );
119 }
120 add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_template_part_list_table_column', 10, 2 );
121