Make WordPress Core


Ignore:
Timestamp:
10/03/2022 04:34:57 PM (4 years ago)
Author:
davidbaumwald
Message:

Editor: Make template names and descriptions dynamic, again.

In the lead up to 6.1 Beta 2, dynamic titles and descriptions for site editor templates was deemed "feature incomplete" and [54280] was reverted. After further consideration, this code is being re-merged in preparation for Beta 3, reverting the revert in [54333].

Follow-up to [54280] and [54333].

Props bernie, ntsekouras, jorgefilipecosta, jameskoster, cbravobernal.
See #56467.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r54333 r54370  
    532532
    533533/**
     534 * Builds the title and description of a post-specific template based on the underlying referenced post.
     535 *
     536 * Mutates the underlying template object.
     537 *
     538 * @since 6.1.0
     539 * @access private
     540 *
     541 * @param string            $post_type Post type, e.g. page, post, product.
     542 * @param string            $slug      Slug of the post, e.g. a-story-about-shoes.
     543 * @param WP_Block_Template $template  Template to mutate adding the description and title computed.
     544 * @return bool Returns true if the referenced post was found and false otherwise.
     545 */
     546function _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, WP_Block_Template $template ) {
     547        $post_type_object = get_post_type_object( $post_type );
     548
     549        $posts = get_posts(
     550                array(
     551                        'name'      => $slug,
     552                        'post_type' => $post_type,
     553                )
     554        );
     555
     556        if ( empty( $posts ) ) {
     557                $template->title = sprintf(
     558                        /* translators: Custom template title in the Site Editor referencing a post that was not found. 1: Post type singular name, 2: Post type slug. */
     559                        __( 'Not found: %1$s (%2$s)' ),
     560                        $post_type_object->labels->singular_name,
     561                        $slug
     562                );
     563
     564                return false;
     565        }
     566
     567        $post_title = $posts[0]->post_title;
     568
     569        $template->title = sprintf(
     570                /* translators: Custom template title in the Site Editor. 1: Post type singular name, 2: Post title. */
     571                __( '%1$s: %2$s' ),
     572                $post_type_object->labels->singular_name,
     573                $post_title
     574        );
     575
     576        $template->description = sprintf(
     577                /* translators: Custom template description in the Site Editor. %s: Post title. */
     578                __( 'Template for %s' ),
     579                $post_title
     580        );
     581
     582        $posts_with_same_title = get_posts(
     583                array(
     584                        'title'       => $post_title,
     585                        'post_type'   => $post_type,
     586                        'post_status' => 'publish',
     587                )
     588        );
     589
     590        if ( count( $posts_with_same_title ) > 1 ) {
     591                $template->title = sprintf(
     592                        /* translators: Custom template title in the Site Editor. 1: Template title, 2: Post type slug. */
     593                        __( '%1$s (%2$s)' ),
     594                        $template->title,
     595                        $slug
     596                );
     597        }
     598
     599        return true;
     600}
     601
     602/**
     603 * Builds the title and description of a taxonomy-specific template based on the underlying entity referenced.
     604 *
     605 * Mutates the underlying template object.
     606 *
     607 * @since 6.1.0
     608 * @access private
     609 *
     610 * @param string            $taxonomy Identifier of the taxonomy, e.g. category.
     611 * @param string            $slug     Slug of the term, e.g. shoes.
     612 * @param WP_Block_Template $template Template to mutate adding the description and title computed.
     613 * @return bool True if the term referenced was found and false otherwise.
     614 */
     615function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, WP_Block_Template $template ) {
     616        $taxonomy_object = get_taxonomy( $taxonomy );
     617
     618        $terms = get_terms(
     619                array(
     620                        'taxonomy'   => $taxonomy,
     621                        'hide_empty' => false,
     622                        'slug'       => $slug,
     623                )
     624        );
     625
     626        if ( empty( $terms ) ) {
     627                $template->title = sprintf(
     628                        /* translators: Custom template title in the Site Editor, referencing a taxonomy term that was not found. 1: Taxonomy singular name, 2: Term slug. */
     629                        __( 'Not found: %1$s (%2$s)' ),
     630                        $taxonomy_object->labels->singular_name,
     631                        $slug
     632                );
     633                return false;
     634        }
     635
     636        $term_title = $terms[0]->name;
     637
     638        $template->title = sprintf(
     639                /* translators: Custom template title in the Site Editor. 1: Taxonomy singular name, 2: Term title. */
     640                __( '%1$s: %2$s' ),
     641                $taxonomy_object->labels->singular_name,
     642                $term_title
     643        );
     644
     645        $template->description = sprintf(
     646                /* translators: Custom template description in the Site Editor. %s: Term title. */
     647                __( 'Template for %s' ),
     648                $term_title
     649        );
     650
     651        $terms_with_same_title = get_terms(
     652                array(
     653                        'taxonomy'   => $taxonomy,
     654                        'hide_empty' => false,
     655                        'name'       => $term_title,
     656                )
     657        );
     658
     659        if ( count( $terms_with_same_title ) > 1 ) {
     660                $template->title = sprintf(
     661                        /* translators: Custom template title in the Site Editor. 1: Template title, 2: Term slug. */
     662                        __( '%1$s (%2$s)' ),
     663                        $template->title,
     664                        $slug
     665                );
     666        }
     667
     668        return true;
     669}
     670
     671/**
    534672 * Builds a unified template object based a post Object.
    535673 *
     
    590728        }
    591729
     730        // Check for a block template without a description and title or with a title equal to the slug.
     731        if ( 'wp_template' === $post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
     732                $matches = array();
     733
     734                // Check for a block template for a single author, page, post, tag, category, custom post type, or custom taxonomy.
     735                if ( preg_match( '/(author|page|single|tag|category|taxonomy)-(.+)/', $template->slug, $matches ) ) {
     736                        $type           = $matches[1];
     737                        $slug_remaining = $matches[2];
     738
     739                        switch ( $type ) {
     740                                case 'author':
     741                                        $nice_name = $slug_remaining;
     742                                        $users     = get_users(
     743                                                array(
     744                                                        'capability'     => 'edit_posts',
     745                                                        'search'         => $nice_name,
     746                                                        'search_columns' => array( 'user_nicename' ),
     747                                                        'fields'         => 'display_name',
     748                                                )
     749                                        );
     750
     751                                        if ( empty( $users ) ) {
     752                                                $template->title = sprintf(
     753                                                        /* translators: Custom template title in the Site Editor, referencing a deleted author. %s: Author nicename. */
     754                                                        __( 'Deleted author: %s' ),
     755                                                        $nice_name
     756                                                );
     757                                        } else {
     758                                                $author_name = $users[0];
     759
     760                                                $template->title = sprintf(
     761                                                        /* translators: Custom template title in the Site Editor. %s: Author name. */
     762                                                        __( 'Author: %s' ),
     763                                                        $author_name
     764                                                );
     765
     766                                                $template->description = sprintf(
     767                                                        /* translators: Custom template description in the Site Editor. %s: Author name. */
     768                                                        __( 'Template for %s' ),
     769                                                        $author_name
     770                                                );
     771
     772                                                $users_with_same_name = get_users(
     773                                                        array(
     774                                                                'capability'     => 'edit_posts',
     775                                                                'search'         => $author_name,
     776                                                                'search_columns' => array( 'display_name' ),
     777                                                                'fields'         => 'display_name',
     778                                                        )
     779                                                );
     780
     781                                                if ( count( $users_with_same_name ) > 1 ) {
     782                                                        $template->title = sprintf(
     783                                                                /* translators: Custom template title in the Site Editor. 1: Template title of an author template, 2: Author nicename. */
     784                                                                __( '%1$s (%2$s)' ),
     785                                                                $template->title,
     786                                                                $nice_name
     787                                                        );
     788                                                }
     789                                        }
     790                                        break;
     791                                case 'page':
     792                                        _wp_build_title_and_description_for_single_post_type_block_template( 'page', $slug_remaining, $template );
     793                                        break;
     794                                case 'single':
     795                                        $post_types = get_post_types();
     796
     797                                        foreach ( $post_types as $post_type ) {
     798                                                $post_type_length = strlen( $post_type ) + 1;
     799
     800                                                // If $slug_remaining starts with $post_type followed by a hyphen.
     801                                                if ( 0 === strncmp( $slug_remaining, $post_type . '-', $post_type_length ) ) {
     802                                                        $slug  = substr( $slug_remaining, $post_type_length, strlen( $slug_remaining ) );
     803                                                        $found = _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, $template );
     804
     805                                                        if ( $found ) {
     806                                                                break;
     807                                                        }
     808                                                }
     809                                        }
     810                                        break;
     811                                case 'tag':
     812                                        _wp_build_title_and_description_for_taxonomy_block_template( 'post_tag', $slug_remaining, $template );
     813                                        break;
     814                                case 'category':
     815                                        _wp_build_title_and_description_for_taxonomy_block_template( 'category', $slug_remaining, $template );
     816                                        break;
     817                                case 'taxonomy':
     818                                        $taxonomies = get_taxonomies();
     819
     820                                        foreach ( $taxonomies as $taxonomy ) {
     821                                                $taxonomy_length = strlen( $taxonomy ) + 1;
     822
     823                                                // If $slug_remaining starts with $taxonomy followed by a hyphen.
     824                                                if ( 0 === strncmp( $slug_remaining, $taxonomy . '-', $taxonomy_length ) ) {
     825                                                        $slug  = substr( $slug_remaining, $taxonomy_length, strlen( $slug_remaining ) );
     826                                                        $found = _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, $template );
     827
     828                                                        if ( $found ) {
     829                                                                break;
     830                                                        }
     831                                                }
     832                                        }
     833                                        break;
     834                        }
     835                }
     836        }
     837
    592838        return $template;
    593839}
Note: See TracChangeset for help on using the changeset viewer.