- Timestamp:
- 01/27/2024 12:05:24 AM (10 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php
r56819 r57366 727 727 } 728 728 729 if ( rest_is_field_included( 'author_text', $fields ) ) { 730 $data['author_text'] = self::get_wp_templates_author_text_field( $template ); 731 } 732 733 if ( rest_is_field_included( 'original_source', $fields ) ) { 734 $data['original_source'] = self::get_wp_templates_original_source_field( $template ); 735 } 736 729 737 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 730 738 $data = $this->add_additional_fields_to_object( $data, $request ); … … 747 755 748 756 return $response; 757 } 758 759 /** 760 * Returns the source from where the template originally comes from. 761 * 762 * @access private 763 * @internal 764 * 765 * @param WP_Block_Template $template_object Template instance. 766 * @return string Original source of the template one of theme, plugin, site, or user. 767 */ 768 private static function get_wp_templates_original_source_field( $template_object ) { 769 if ( 'wp_template' === $template_object->type || 'wp_template_part' === $template_object->type ) { 770 // Added by theme. 771 // Template originally provided by a theme, but customized by a user. 772 // Templates originally didn't have the 'origin' field so identify 773 // older customized templates by checking for no origin and a 'theme' 774 // or 'custom' source. 775 if ( $template_object->has_theme_file && 776 ( 'theme' === $template_object->origin || ( 777 empty( $template_object->origin ) && in_array( 778 $template_object->source, 779 array( 780 'theme', 781 'custom', 782 ), 783 true 784 ) ) 785 ) 786 ) { 787 return 'theme'; 788 } 789 790 // Added by plugin. 791 if ( $template_object->has_theme_file && 'plugin' === $template_object->origin ) { 792 return 'plugin'; 793 } 794 795 // Added by site. 796 // Template was created from scratch, but has no author. Author support 797 // was only added to templates in WordPress 5.9. Fallback to showing the 798 // site logo and title. 799 if ( empty( $template_object->has_theme_file ) && 'custom' === $template_object->source && empty( $template_object->author ) ) { 800 return 'site'; 801 } 802 } 803 804 // Added by user. 805 return 'user'; 806 } 807 808 /** 809 * Returns a human readable text for the author of the template. 810 * 811 * @access private 812 * @internal 813 * 814 * @param WP_Block_Template $template_object Template instance. 815 * @return string Human readable text for the author. 816 */ 817 private static function get_wp_templates_author_text_field( $template_object ) { 818 $original_source = self::get_wp_templates_original_source_field( $template_object ); 819 switch ( $original_source ) { 820 case 'theme': 821 $theme_name = wp_get_theme( $template_object->theme )->get( 'Name' ); 822 return empty( $theme_name ) ? $template_object->theme : $theme_name; 823 case 'plugin': 824 $plugins = get_plugins(); 825 $plugin = $plugins[ plugin_basename( sanitize_text_field( $template_object->theme . '.php' ) ) ]; 826 return empty( $plugin['Name'] ) ? $template_object->theme : $plugin['Name']; 827 case 'site': 828 return get_bloginfo( 'name' ); 829 case 'user': 830 $author = get_user_by( 'id', $template_object->author ); 831 if ( ! $author ) { 832 return __( 'Unknown author' ); 833 } 834 return $author->get( 'display_name' ); 835 } 749 836 } 750 837 … … 862 949 'type' => 'object', 863 950 'properties' => array( 864 'id' => array(951 'id' => array( 865 952 'description' => __( 'ID of template.' ), 866 953 'type' => 'string', … … 868 955 'readonly' => true, 869 956 ), 870 'slug' => array(957 'slug' => array( 871 958 'description' => __( 'Unique slug identifying the template.' ), 872 959 'type' => 'string', … … 876 963 'pattern' => '[a-zA-Z0-9_\%-]+', 877 964 ), 878 'theme' => array(965 'theme' => array( 879 966 'description' => __( 'Theme identifier for the template.' ), 880 967 'type' => 'string', 881 968 'context' => array( 'embed', 'view', 'edit' ), 882 969 ), 883 'type' => array(970 'type' => array( 884 971 'description' => __( 'Type of template.' ), 885 972 'type' => 'string', 886 973 'context' => array( 'embed', 'view', 'edit' ), 887 974 ), 888 'source' => array(975 'source' => array( 889 976 'description' => __( 'Source of template' ), 890 977 'type' => 'string', … … 892 979 'readonly' => true, 893 980 ), 894 'origin' => array(981 'origin' => array( 895 982 'description' => __( 'Source of a customized template' ), 896 983 'type' => 'string', … … 898 985 'readonly' => true, 899 986 ), 900 'content' => array(987 'content' => array( 901 988 'description' => __( 'Content of template.' ), 902 989 'type' => array( 'object', 'string' ), … … 917 1004 ), 918 1005 ), 919 'title' => array(1006 'title' => array( 920 1007 'description' => __( 'Title of template.' ), 921 1008 'type' => array( 'object', 'string' ), … … 936 1023 ), 937 1024 ), 938 'description' => array(1025 'description' => array( 939 1026 'description' => __( 'Description of template.' ), 940 1027 'type' => 'string', … … 942 1029 'context' => array( 'embed', 'view', 'edit' ), 943 1030 ), 944 'status' => array(1031 'status' => array( 945 1032 'description' => __( 'Status of template.' ), 946 1033 'type' => 'string', … … 949 1036 'context' => array( 'embed', 'view', 'edit' ), 950 1037 ), 951 'wp_id' => array(1038 'wp_id' => array( 952 1039 'description' => __( 'Post ID.' ), 953 1040 'type' => 'integer', … … 955 1042 'readonly' => true, 956 1043 ), 957 'has_theme_file' => array(1044 'has_theme_file' => array( 958 1045 'description' => __( 'Theme file exists.' ), 959 1046 'type' => 'bool', … … 961 1048 'readonly' => true, 962 1049 ), 963 'author' => array(1050 'author' => array( 964 1051 'description' => __( 'The ID for the author of the template.' ), 965 1052 'type' => 'integer', 966 1053 'context' => array( 'view', 'edit', 'embed' ), 967 1054 ), 968 'modified' => array(1055 'modified' => array( 969 1056 'description' => __( "The date the template was last modified, in the site's timezone." ), 970 1057 'type' => 'string', … … 972 1059 'context' => array( 'view', 'edit' ), 973 1060 'readonly' => true, 1061 ), 1062 'author_text' => array( 1063 'type' => 'string', 1064 'description' => __( 'Human readable text for the author.' ), 1065 'readonly' => true, 1066 'context' => array( 'view', 'edit', 'embed' ), 1067 ), 1068 'original_source' => array( 1069 'description' => __( 'Where the template originally comes from e.g. \'theme\'' ), 1070 'type' => 'string', 1071 'readonly' => true, 1072 'context' => array( 'view', 'edit', 'embed' ), 1073 'enum' => array( 1074 'theme', 1075 'plugin', 1076 'site', 1077 'user', 1078 ), 974 1079 ), 975 1080 ),
Note: See TracChangeset
for help on using the changeset viewer.