Make WordPress Core


Ignore:
Timestamp:
09/21/2022 03:43:39 PM (4 years ago)
Author:
jorgefilipecosta
Message:

Editor: Make template names and descriptions dynamic.

Backports PHP changes in WordPress/gutenberg#43862 to the core. Adds a mechanism to dynamically compute names and descriptions of the author, page, single, tag, category, and taxonomy templates.

Props mcsf, ntsekouras, antonvlasenko, jameskoster.
See #56467.

File:
1 edited

Legend:

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

    r54269 r54280  
    532532
    533533/**
     534 * Builds the title and description of a post specific template based on the underlying referenced post.
     535 * Mutates the underlying template object.
     536 *
     537 * @access private
     538 * @internal
     539 *
     540 * @param string            $post_type Post type e.g.: page, post, product.
     541 * @param string            $slug      Slug of the post e.g.: a-story-about-shoes.
     542 * @param WP_Block_Template $template  Template to mutate adding the description and title computed.
     543 *
     544 * @return boolean 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        if ( empty( $posts ) ) {
     556                $template->title = sprintf(
     557                        // translators: Represents the title of a user's custom template in the Site Editor referencing a post that was not found, where %1$s is the singular name of a post type and %2$s is the slug of the deleted post, e.g. "Not found: Page(hello)".
     558                        __( 'Not found: %1$s(%2$s)' ),
     559                        $post_type_object->labels->singular_name,
     560                        $slug
     561                );
     562                return false;
     563        }
     564
     565        $post_title = $posts[0]->post_title;
     566
     567        $template->title = sprintf(
     568                // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Page: Hello".
     569                __( '%1$s: %2$s' ),
     570                $post_type_object->labels->singular_name,
     571                $post_title
     572        );
     573        $template->description = sprintf(
     574                // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Page: Hello".
     575                __( 'Template for %1$s' ),
     576                $post_title
     577        );
     578
     579        $posts_with_same_title = get_posts(
     580                array(
     581                        'title'       => $post_title,
     582                        'post_type'   => $post_type,
     583                        'post_status' => 'publish',
     584                )
     585        );
     586        if ( count( $posts_with_same_title ) > 1 ) {
     587                $template->title = sprintf(
     588                        // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title and %2$s is the slug of the post type, e.g. "Project: Hello (project_type)".
     589                        __( '%1$s (%2$s)' ),
     590                        $template->title,
     591                        $slug
     592                );
     593        }
     594        return true;
     595}
     596
     597/**
     598 * Builds the title and description of a taxonomy specific template based on the underlying entity referenced.
     599 * Mutates the underlying template object.
     600 *
     601 * @access private
     602 * @internal
     603 *
     604 * @param string            $taxonomy Identifier of the taxonomy, e.g.: category.
     605 * @param string            $slug     Slug of the term, e.g.: shoes.
     606 * @param WP_Block_Template $template Template to mutate adding the description and title computed.
     607 *
     608 * @return boolean True if an term referenced was found and false otherwise.
     609 */
     610function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, WP_Block_Template $template ) {
     611        $taxonomy_object = get_taxonomy( $taxonomy );
     612
     613        $terms = get_terms(
     614                array(
     615                        'taxonomy'   => $taxonomy,
     616                        'hide_empty' => false,
     617                        'slug'       => $slug,
     618                )
     619        );
     620
     621        if ( empty( $terms ) ) {
     622                $template->title = sprintf(
     623                        // translators: Represents the title of a user's custom template in the Site Editor referencing a taxonomy term that was not found, where %1$s is the singular name of a taxonomy and %2$s is the slug of the deleted term, e.g. "Not found: Category(shoes)".
     624                        __( 'Not found: %1$s(%2$s)' ),
     625                        $taxonomy_object->labels->singular_name,
     626                        $slug
     627                );
     628                return false;
     629        }
     630
     631        $term_title = $terms[0]->name;
     632
     633        $template->title = sprintf(
     634                // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a taxonomy and %2$s is the name of the term, e.g. "Category: shoes".
     635                __( '%1$s: %2$s' ),
     636                $taxonomy_object->labels->singular_name,
     637                $term_title
     638        );
     639        $template->description = sprintf(
     640                // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Category: shoes".
     641                __( 'Template for %1$s' ),
     642                $term_title
     643        );
     644
     645        $terms_with_same_title = get_terms(
     646                array(
     647                        'taxonomy'   => $taxonomy,
     648                        'hide_empty' => false,
     649                        'name'       => $term_title,
     650                )
     651        );
     652        if ( count( $terms_with_same_title ) > 1 ) {
     653                $template->title = sprintf(
     654                        // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title and %2$s is the slug of the taxonomy, e.g. "Category: shoes (product_tag)".
     655                        __( '%1$s (%2$s)' ),
     656                        $template->title,
     657                        $slug
     658                );
     659        }
     660        return true;
     661}
     662
     663/**
    534664 * Builds a unified template object based a post Object.
    535665 *
     
    590720        }
    591721
     722        // If it is a block template without description and without title or with title equal to the slug.
     723        if ( 'wp_template' === $post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
     724                $matches = array();
     725                // If it is a block template for a single author, page, post, tag, category, custom post type or custom taxonomy.
     726                if ( preg_match( '/(author|page|single|tag|category|taxonomy)-(.+)/', $template->slug, $matches ) ) {
     727                        $type           = $matches[1];
     728                        $slug_remaining = $matches[2];
     729                        switch ( $type ) {
     730                                case 'author':
     731                                        $nice_name = $slug_remaining;
     732                                        $users     = get_users(
     733                                                array(
     734                                                        'capability'     => 'edit_posts',
     735                                                        'search'         => $nice_name,
     736                                                        'search_columns' => array( 'user_nicename' ),
     737                                                        'fields'         => 'display_name',
     738                                                )
     739                                        );
     740
     741                                        if ( empty( $users ) ) {
     742                                                $template->title = sprintf(
     743                                                        // translators: Represents the title of a user's custom template in the Site Editor referencing a deleted author, where %s is the author's nicename, e.g. "Deleted author: jane-doe".
     744                                                        __( 'Deleted author: %s' ),
     745                                                        $nice_name
     746                                                );
     747                                        } else {
     748                                                $author_name = $users[0];
     749
     750                                                $template->title = sprintf(
     751                                                        // translators: Represents the title of a user's custom template in the Site Editor, where %s is the author's name, e.g. "Author: Jane Doe".
     752                                                        __( 'Author: %s' ),
     753                                                        $author_name
     754                                                );
     755                                                $template->description = sprintf(
     756                                                        // translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Author: Jane Doe".
     757                                                        __( 'Template for %1$s' ),
     758                                                        $author_name
     759                                                );
     760
     761                                                $users_with_same_name = get_users(
     762                                                        array(
     763                                                                'capability'     => 'edit_posts',
     764                                                                'search'         => $author_name,
     765                                                                'search_columns' => array( 'display_name' ),
     766                                                                'fields'         => 'display_name',
     767                                                        )
     768                                                );
     769                                                if ( count( $users_with_same_name ) > 1 ) {
     770                                                        $template->title = sprintf(
     771                                                                // translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title of an author template and %2$s is the nicename of the author, e.g. "Author: Jorge (jorge-costa)".
     772                                                                __( '%1$s (%2$s)' ),
     773                                                                $template->title,
     774                                                                $nice_name
     775                                                        );
     776                                                }
     777                                        }
     778                                        break;
     779                                case 'page':
     780                                        _wp_build_title_and_description_for_single_post_type_block_template( 'page', $slug_remaining, $template );
     781                                        break;
     782                                case 'single':
     783                                        $post_types = get_post_types();
     784                                        foreach ( $post_types as $post_type ) {
     785                                                $post_type_length = strlen( $post_type ) + 1;
     786                                                // If $slug_remaining starts with $post_type followed by a hyphen.
     787                                                if ( 0 === strncmp( $slug_remaining, $post_type . '-', $post_type_length ) ) {
     788                                                        $slug  = substr( $slug_remaining, $post_type_length, strlen( $slug_remaining ) );
     789                                                        $found = _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, $template );
     790                                                        if ( $found ) {
     791                                                                break;
     792                                                        }
     793                                                }
     794                                        }
     795                                        break;
     796                                case 'tag':
     797                                        _wp_build_title_and_description_for_taxonomy_block_template( 'post_tag', $slug_remaining, $template );
     798                                        break;
     799                                case 'category':
     800                                        _wp_build_title_and_description_for_taxonomy_block_template( 'category', $slug_remaining, $template );
     801                                        break;
     802                                case 'taxonomy':
     803                                        $taxonomies = get_taxonomies();
     804                                        foreach ( $taxonomies as $taxonomy ) {
     805                                                $taxonomy_length = strlen( $taxonomy ) + 1;
     806                                                // If $slug_remaining starts with $taxonomy followed by a hyphen.
     807                                                if ( 0 === strncmp( $slug_remaining, $taxonomy . '-', $taxonomy_length ) ) {
     808                                                        $slug  = substr( $slug_remaining, $taxonomy_length, strlen( $slug_remaining ) );
     809                                                        $found = _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, $template );
     810                                                        if ( $found ) {
     811                                                                break;
     812                                                        }
     813                                                }
     814                                        }
     815                                        break;
     816                        }
     817                }
     818        }
    592819        return $template;
    593820}
Note: See TracChangeset for help on using the changeset viewer.