PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 / full-site-editing / template-parts.php

template-parts.php in Gutenberg 12.1.0, at lib/full-site-editing/template-parts.php

158 lines 5.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_supports_block_templates() ) {
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 'view_items' => __( 'View Template Parts', 'gutenberg' ),
26 'all_items' => __( 'All Template Parts', 'gutenberg' ),
27 'search_items' => __( 'Search Template Parts', 'gutenberg' ),
28 'parent_item_colon' => __( 'Parent Template Part:', 'gutenberg' ),
29 'not_found' => __( 'No template parts found.', 'gutenberg' ),
30 'not_found_in_trash' => __( 'No template parts found in Trash.', 'gutenberg' ),
31 'archives' => __( 'Template part archives', 'gutenberg' ),
32 'insert_into_item' => __( 'Insert into template part', 'gutenberg' ),
33 'uploaded_to_this_item' => __( 'Uploaded to this template part', 'gutenberg' ),
34 'filter_items_list' => __( 'Filter template parts list', 'gutenberg' ),
35 'items_list_navigation' => __( 'Template parts list navigation', 'gutenberg' ),
36 'items_list' => __( 'Template parts list', 'gutenberg' ),
37 );
38
39 $args = array(
40 'labels' => $labels,
41 'description' => __( 'Template parts to include in your templates.', 'gutenberg' ),
42 'public' => false,
43 'has_archive' => false,
44 'show_ui' => true,
45 'show_in_menu' => false,
46 'show_in_admin_bar' => false,
47 'show_in_rest' => true,
48 'rest_base' => 'template-parts',
49 'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
50 'map_meta_cap' => true,
51 'supports' => array(
52 'title',
53 'slug',
54 'excerpt',
55 'editor',
56 'revisions',
57 'author',
58 ),
59 );
60
61 register_post_type( 'wp_template_part', $args );
62 }
63 add_action( 'init', 'gutenberg_register_template_part_post_type' );
64
65 /**
66 * Registers the 'wp_template_part_area' taxonomy.
67 */
68 function gutenberg_register_wp_template_part_area_taxonomy() {
69 if ( ! gutenberg_supports_block_templates() ) {
70 return;
71 }
72
73 register_taxonomy(
74 'wp_template_part_area',
75 array( 'wp_template_part' ),
76 array(
77 'public' => false,
78 'hierarchical' => false,
79 'labels' => array(
80 'name' => __( 'Template Part Areas', 'gutenberg' ),
81 'singular_name' => __( 'Template Part Area', 'gutenberg' ),
82 ),
83 'query_var' => false,
84 'rewrite' => false,
85 'show_ui' => false,
86 '_builtin' => true,
87 'show_in_nav_menus' => false,
88 'show_in_rest' => false,
89 )
90 );
91 }
92 add_action( 'init', 'gutenberg_register_wp_template_part_area_taxonomy' );
93
94 /**
95 * Fixes the label of the 'wp_template_part' admin menu entry.
96 */
97 function gutenberg_fix_template_part_admin_menu_entry() {
98 if ( ! gutenberg_supports_block_templates() ) {
99 return;
100 }
101
102 global $submenu;
103 if ( ! isset( $submenu['themes.php'] ) ) {
104 return;
105 }
106 $post_type = get_post_type_object( 'wp_template_part' );
107 if ( ! $post_type ) {
108 return;
109 }
110 foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
111 if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
112 $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
113 break;
114 }
115 }
116 }
117 add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' );
118
119 // Customize the `wp_template` admin list.
120 add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_templates_lists_custom_columns' );
121 add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
122 add_filter( 'views_edit-wp_template_part', 'gutenberg_filter_templates_edit_views' );
123
124 /**
125 * Sets a custom slug when creating auto-draft template parts.
126 * This is only needed for auto-drafts created by the regular WP editor.
127 * If this page is to be removed, this won't be necessary.
128 *
129 * @param int $post_id Post ID.
130 */
131 function gutenberg_set_unique_slug_on_create_template_part( $post_id ) {
132 // This is the core function with the same functionality.
133 if ( function_exists( 'wp_set_unique_slug_on_create_template_part' ) ) {
134 return;
135 }
136
137 $post = get_post( $post_id );
138 if ( 'auto-draft' !== $post->post_status ) {
139 return;
140 }
141
142 if ( ! $post->post_name ) {
143 wp_update_post(
144 array(
145 'ID' => $post_id,
146 'post_name' => 'custom_slug_' . uniqid(),
147 )
148 );
149 }
150
151 $terms = get_the_terms( $post_id, 'wp_theme' );
152 if ( ! $terms || ! count( $terms ) ) {
153 wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
154 }
155 }
156
157 add_action( 'save_post_wp_template_part', 'gutenberg_set_unique_slug_on_create_template_part' );
158