PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.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 / compat / wordpress-5.9 / template-parts.php

template-parts.php in Gutenberg 12.6.0, at lib/compat/wordpress-5.9/template-parts.php

130 lines 4.6 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 * This is a temporary compatibility fix for WordPress 5.8.x, which is missing
6 * some features for template parts that are present in 5.9.
7 *
8 * Once 5.9 is the minimum supported WordPress version for the Gutenberg
9 * plugin, this shim can be removed.
10 *
11 * @package gutenberg
12 */
13
14 // Only run any of the code in this file if the version is less than 5.9.
15 // wp_list_users was introduced in 5.9.
16 if ( ! function_exists( 'wp_list_users' ) ) {
17 /**
18 * Registers block editor 'wp_template_part' post type.
19 */
20 function gutenberg_register_template_part_post_type() {
21 $labels = array(
22 'name' => __( 'Template Parts', 'gutenberg' ),
23 'singular_name' => __( 'Template Part', 'gutenberg' ),
24 'menu_name' => _x( 'Template Parts', 'Admin Menu text', 'gutenberg' ),
25 'add_new' => _x( 'Add New', 'Template Part', 'gutenberg' ),
26 'add_new_item' => __( 'Add New Template Part', 'gutenberg' ),
27 'new_item' => __( 'New Template Part', 'gutenberg' ),
28 'edit_item' => __( 'Edit Template Part', 'gutenberg' ),
29 'view_item' => __( 'View Template Part', 'gutenberg' ),
30 'view_items' => __( 'View Template Parts', 'gutenberg' ),
31 'all_items' => __( 'All Template Parts', 'gutenberg' ),
32 'search_items' => __( 'Search Template Parts', 'gutenberg' ),
33 'parent_item_colon' => __( 'Parent Template Part:', 'gutenberg' ),
34 'not_found' => __( 'No template parts found.', 'gutenberg' ),
35 'not_found_in_trash' => __( 'No template parts found in Trash.', 'gutenberg' ),
36 'archives' => __( 'Template part archives', 'gutenberg' ),
37 'insert_into_item' => __( 'Insert into template part', 'gutenberg' ),
38 'uploaded_to_this_item' => __( 'Uploaded to this template part', 'gutenberg' ),
39 'filter_items_list' => __( 'Filter template parts list', 'gutenberg' ),
40 'items_list_navigation' => __( 'Template parts list navigation', 'gutenberg' ),
41 'items_list' => __( 'Template parts list', 'gutenberg' ),
42 );
43
44 $args = array(
45 'labels' => $labels,
46 'description' => __( 'Template parts to include in your templates.', 'gutenberg' ),
47 'public' => false,
48 'has_archive' => false,
49 'show_ui' => true,
50 'show_in_menu' => false,
51 'show_in_admin_bar' => false,
52 'show_in_rest' => true,
53 'rest_base' => 'template-parts',
54 'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
55 'map_meta_cap' => true,
56 'supports' => array(
57 'title',
58 'slug',
59 'excerpt',
60 'editor',
61 'revisions',
62 'author',
63 ),
64 );
65
66 register_post_type( 'wp_template_part', $args );
67 }
68 add_action( 'init', 'gutenberg_register_template_part_post_type' );
69
70 /**
71 * Registers the 'wp_template_part_area' taxonomy.
72 */
73 function gutenberg_register_wp_template_part_area_taxonomy() {
74 register_taxonomy(
75 'wp_template_part_area',
76 array( 'wp_template_part' ),
77 array(
78 'public' => false,
79 'hierarchical' => false,
80 'labels' => array(
81 'name' => __( 'Template Part Areas', 'gutenberg' ),
82 'singular_name' => __( 'Template Part Area', 'gutenberg' ),
83 ),
84 'query_var' => false,
85 'rewrite' => false,
86 'show_ui' => false,
87 '_builtin' => true,
88 'show_in_nav_menus' => false,
89 'show_in_rest' => false,
90 )
91 );
92 }
93 add_action( 'init', 'gutenberg_register_wp_template_part_area_taxonomy' );
94
95 /**
96 * Sets a custom slug when creating auto-draft template parts.
97 * This is only needed for auto-drafts created by the regular WP editor.
98 * If this page is to be removed, this won't be necessary.
99 *
100 * @param int $post_id Post ID.
101 */
102 function gutenberg_set_unique_slug_on_create_template_part( $post_id ) {
103 // This is the core function with the same functionality.
104 if ( function_exists( 'wp_set_unique_slug_on_create_template_part' ) ) {
105 return;
106 }
107
108 $post = get_post( $post_id );
109 if ( 'auto-draft' !== $post->post_status ) {
110 return;
111 }
112
113 if ( ! $post->post_name ) {
114 wp_update_post(
115 array(
116 'ID' => $post_id,
117 'post_name' => 'custom_slug_' . uniqid(),
118 )
119 );
120 }
121
122 $terms = get_the_terms( $post_id, 'wp_theme' );
123 if ( ! $terms || ! count( $terms ) ) {
124 wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
125 }
126 }
127
128 add_action( 'save_post_wp_template_part', 'gutenberg_set_unique_slug_on_create_template_part' );
129 }
130