You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
git-svn-id: https://develop.svn.wordpress.org/trunk@54280 602fd350-edb4-49c9-b593-d223f7449a82
// 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
+
returnfalse;
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
+
returntrue;
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.
// 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
+
returnfalse;
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
+
returntrue;
661
+
}
662
+
533
663
/**
534
664
* Builds a unified template object based a post Object.
535
665
*
@@ -589,6 +719,103 @@ function _build_block_template_result_from_post( $post ) {
589
719
}
590
720
}
591
721
722
+
// If it is a block template without description and without title or with title equal to the slug.
// 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)".
0 commit comments