PluginProbe
Gutenberg / 10.1.0
Gutenberg v10.1.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 / template-parts.php

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

204 lines 6.8 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_fse_theme() ) {
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 'rest_controller_class' => 'WP_REST_Templates_Controller',
49 'map_meta_cap' => true,
50 'supports' => array(
51 'title',
52 'slug',
53 'excerpt',
54 'editor',
55 'revisions',
56 ),
57 );
58
59 register_post_type( 'wp_template_part', $args );
60 }
61 add_action( 'init', 'gutenberg_register_template_part_post_type' );
62
63 /**
64 * Registers the 'wp_template_part_area' taxonomy.
65 */
66 function gutenberg_register_wp_template_part_area_taxonomy() {
67 if ( ! gutenberg_is_fse_theme() ) {
68 return;
69 }
70
71 register_taxonomy(
72 'wp_template_part_area',
73 array( 'wp_template_part' ),
74 array(
75 'public' => false,
76 'hierarchical' => false,
77 'labels' => array(
78 'name' => __( 'Template Part Areas', 'gutenberg' ),
79 'singular_name' => __( 'Template Part Area', 'gutenberg' ),
80 ),
81 'query_var' => false,
82 'rewrite' => false,
83 'show_ui' => false,
84 '_builtin' => true,
85 'show_in_nav_menus' => false,
86 'show_in_rest' => false,
87 )
88 );
89 }
90 add_action( 'init', 'gutenberg_register_wp_template_part_area_taxonomy' );
91
92 // Define constants for supported wp_template_part_area taxonomy.
93 if ( ! defined( 'WP_TEMPLATE_PART_AREA_HEADER' ) ) {
94 define( 'WP_TEMPLATE_PART_AREA_HEADER', 'header' );
95 }
96 if ( ! defined( 'WP_TEMPLATE_PART_AREA_FOOTER' ) ) {
97 define( 'WP_TEMPLATE_PART_AREA_FOOTER', 'footer' );
98 }
99 if ( ! defined( 'WP_TEMPLATE_PART_AREA_SIDEBAR' ) ) {
100 define( 'WP_TEMPLATE_PART_AREA_SIDEBAR', 'sidebar' );
101 }
102 if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) {
103 define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' );
104 }
105
106 /**
107 * Fixes the label of the 'wp_template_part' admin menu entry.
108 */
109 function gutenberg_fix_template_part_admin_menu_entry() {
110 if ( ! gutenberg_is_fse_theme() ) {
111 return;
112 }
113
114 global $submenu;
115 if ( ! isset( $submenu['themes.php'] ) ) {
116 return;
117 }
118 $post_type = get_post_type_object( 'wp_template_part' );
119 if ( ! $post_type ) {
120 return;
121 }
122 foreach ( $submenu['themes.php'] as $key => $submenu_entry ) {
123 if ( $post_type->labels->all_items === $submenu['themes.php'][ $key ][0] ) {
124 $submenu['themes.php'][ $key ][0] = $post_type->labels->menu_name; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
125 break;
126 }
127 }
128 }
129 add_action( 'admin_menu', 'gutenberg_fix_template_part_admin_menu_entry' );
130
131 // Customize the `wp_template` admin list.
132 add_filter( 'manage_wp_template_part_posts_columns', 'gutenberg_templates_lists_custom_columns' );
133 add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_templates_lists_custom_column', 10, 2 );
134 add_filter( 'views_edit-wp_template_part', 'gutenberg_filter_templates_edit_views' );
135
136 /**
137 * Sets a custom slug when creating auto-draft template parts.
138 * This is only needed for auto-drafts created by the regular WP editor.
139 * If this page is to be removed, this won't be necessary.
140 *
141 * @param int $post_id Post ID.
142 */
143 function set_unique_slug_on_create_template_part( $post_id ) {
144 $post = get_post( $post_id );
145 if ( 'auto-draft' !== $post->post_status ) {
146 return;
147 }
148
149 if ( ! $post->post_name ) {
150 wp_update_post(
151 array(
152 'ID' => $post_id,
153 'post_name' => 'custom_slug_' . uniqid(),
154 )
155 );
156 }
157
158 $terms = get_the_terms( $post_id, 'wp_theme' );
159 if ( ! $terms || ! count( $terms ) ) {
160 wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
161 }
162 }
163 add_action( 'save_post_wp_template_part', 'set_unique_slug_on_create_template_part' );
164
165 /**
166 * Returns a filtered list of allowed area types for template parts.
167 *
168 * @return array The supported template part area types.
169 */
170 function gutenberg_get_allowed_template_part_area_types() {
171 $default_area_types = array(
172 WP_TEMPLATE_PART_AREA_HEADER,
173 WP_TEMPLATE_PART_AREA_FOOTER,
174 WP_TEMPLATE_PART_AREA_SIDEBAR,
175 WP_TEMPLATE_PART_AREA_UNCATEGORIZED,
176 );
177
178 /**
179 * Filters the list of allowed template part area types.
180 *
181 * @param array $default_area_types An array of supported area types.
182 */
183 return apply_filters( 'default_wp_template_part_area_types', $default_area_types );
184 }
185
186 /**
187 * Checks whether the input 'type' is a supported area type.
188 * Returns the input if supported, otherwise returns the 'other' type.
189 *
190 * @param string $type Template part area name.
191 *
192 * @return string Input if supported, else 'other'.
193 */
194 function gutenberg_filter_template_part_area_type( $type ) {
195 if ( in_array( $type, gutenberg_get_allowed_template_part_area_types(), true ) ) {
196 return $type;
197 }
198 $warning_message = '"' . $type . '"';
199 $warning_message .= __( ' is not a supported wp_template_part_area type and has been added as ', 'gutenberg' );
200 $warning_message .= '"' . WP_TEMPLATE_PART_AREA_UNCATEGORIZED . '".';
201 trigger_error( $warning_message, E_USER_NOTICE );
202 return WP_TEMPLATE_PART_AREA_UNCATEGORIZED;
203 }
204