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 / block-templates.php

block-templates.php in Gutenberg 12.1.0, at lib/full-site-editing/block-templates.php

72 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Utilities used to fetch and create templates and template parts.
4 *
5 * @package Gutenberg
6 * @subpackage REST_API
7 */
8
9 /**
10 * Generates a unique slug for templates or template parts.
11 *
12 * @param string $override_slug The filtered value of the slug (starts as `null` from apply_filter).
13 * @param string $slug The original/un-filtered slug (post_name).
14 * @param int $post_ID Post ID.
15 * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
16 * @param string $post_type Post type.
17 * @return string The original, desired slug.
18 */
19 function gutenberg_filter_wp_template_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) {
20 if ( 'wp_template' !== $post_type && 'wp_template_part' !== $post_type ) {
21 return $override_slug;
22 }
23
24 if ( ! $override_slug ) {
25 $override_slug = $slug;
26 }
27
28 // Template slugs must be unique within the same theme.
29 // TODO - Figure out how to update this to work for a multi-theme
30 // environment. Unfortunately using `get_the_terms` for the 'wp-theme'
31 // term does not work in the case of new entities since is too early in
32 // the process to have been saved to the entity. So for now we use the
33 // currently activated theme for creation.
34 $theme = wp_get_theme()->get_stylesheet();
35 $terms = get_the_terms( $post_ID, 'wp_theme' );
36 if ( $terms && ! is_wp_error( $terms ) ) {
37 $theme = $terms[0]->name;
38 }
39
40 $check_query_args = array(
41 'post_name__in' => array( $override_slug ),
42 'post_type' => $post_type,
43 'posts_per_page' => 1,
44 'no_found_rows' => true,
45 'post__not_in' => array( $post_ID ),
46 'tax_query' => array(
47 array(
48 'taxonomy' => 'wp_theme',
49 'field' => 'name',
50 'terms' => $theme,
51 ),
52 ),
53 );
54 $check_query = new WP_Query( $check_query_args );
55 $posts = $check_query->posts;
56
57 if ( count( $posts ) > 0 ) {
58 $suffix = 2;
59 do {
60 $query_args = $check_query_args;
61 $alt_post_name = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
62 $query_args['post_name__in'] = array( $alt_post_name );
63 $query = new WP_Query( $query_args );
64 $suffix++;
65 } while ( count( $query->posts ) > 0 );
66 $override_slug = $alt_post_name;
67 }
68
69 return $override_slug;
70 }
71 add_filter( 'pre_wp_unique_post_slug', 'gutenberg_filter_wp_template_unique_post_slug', 10, 5 );
72