Make WordPress Core


Ignore:
Timestamp:
05/11/2010 07:52:50 PM (16 years ago)
Author:
nbachiyski
Message:

I18n for custom post type labels. Props demetris, dimadin. Fixes #12968

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r14561 r14571  
    1717function create_initial_post_types() {
    1818    register_post_type( 'post', array(
    19         'label' => __( 'Posts' ),
    20         'singular_label' => __( 'Post' ),
    2119        'public'  => true,
    2220        'show_ui' => false,
     
    3129
    3230    register_post_type( 'page', array(
    33         'label' => __( 'Pages' ),
    34         'singular_label' => __( 'Page' ),
    3531        'public' => true,
    3632        'show_ui' => false,
     
    770766 * Optional $args contents:
    771767 *
    772  * label - A (plural) descriptive name for the post type marked for translation. Defaults to $post_type.
    773  * singular_label - A (singular) descriptive name for the post type marked for translation. Defaults to $label.
    774  * description - A short descriptive summary of what the post type is. Defaults to blank.
    775  * public - Whether posts of this type should be shown in the admin UI. Defaults to false.
    776  * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
    777  * publicly_queryable - Whether post_type queries can be performed from the front page.  Defaults to whatever public is set as.
    778  * show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
    779  * menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
    780  * menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
    781  * inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
    782  * capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
    783  * edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post).
    784  * edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
    785  * edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
    786  * publish_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
    787  * read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post).
    788  * delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post).
    789  * hierarchical - Whether the post type is hierarchical. Defaults to false.
    790  * supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
    791  * register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form.  Do remove_meta_box() and add_meta_box() calls in the callback.
    792  * taxonomies - An array of taxonomy identifiers that will be registered for the post type.  Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
    793  *
    794  * @package WordPress
    795  * @subpackage Post
     768 * - description - A short descriptive summary of what the post type is. Defaults to blank.
     769 * - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
     770 * - exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
     771 * - publicly_queryable - Whether post_type queries can be performed from the front page.  Defaults to whatever public is set as.
     772 * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
     773 * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
     774 * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
     775 * - inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
     776 * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
     777 * - edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post).
     778 * - edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
     779 * - edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
     780 * - publish_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
     781 * - read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post).
     782 * - delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post).
     783 * - hierarchical - Whether the post type is hierarchical. Defaults to false.
     784 * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
     785 * - register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form.  Do remove_meta_box() and add_meta_box() calls in the callback.
     786 * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.  Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
     787 * - labels - An array of labels for this post type. You can see accepted values in {@link get_post_type_labels()}. By default post labels are used for non-hierarchical types and page labels for hierarchical ones.
     788 *
    796789 * @since 2.9.0
    797790 * @uses $wp_post_types Inserts new post type object into the list
     
    799792 * @param string $post_type Name of the post type.
    800793 * @param array|string $args See above description.
     794 * @return object the registered post type object
    801795 */
    802796function register_post_type($post_type, $args = array()) {
     
    807801
    808802    // Args prefixed with an underscore are reserved for internal use.
    809     $defaults = array('label' => false, 'singular_label' => false, 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null, 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null, 'permalink_epmask' => EP_PERMALINK, 'can_export' => true );
     803    $defaults = array(
     804        'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
     805        '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false,
     806        'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
     807        'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
     808        'permalink_epmask' => EP_PERMALINK, 'can_export' => true,
     809    );
    810810    $args = wp_parse_args($args, $defaults);
    811811    $args = (object) $args;
     
    825825    if ( null === $args->exclude_from_search )
    826826        $args->exclude_from_search = !$args->public;
    827 
    828     if ( false === $args->label )
    829         $args->label = $post_type;
    830 
    831     if ( false === $args->singular_label )
    832         $args->singular_label = $args->label;
    833827
    834828    if ( empty($args->capability_type) )
     
    881875        add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
    882876
     877    $args->labels = get_post_type_labels( $args );
     878   
     879    // we keep these two only for backwards compatibility
     880    // TODO: remove in 3.1 
     881    $args->label = $args->labels->name;
     882    $args->singular_label = $args->labels->singular_name;
     883
    883884    $wp_post_types[$post_type] = $args;
    884885
     
    890891
    891892    return $args;
     893}
     894
     895/**
     896 * Builds an object with all post type labels out of a post type object
     897 *
     898 * Accepted keys of the label array in the post type object:
     899 * - name - general name for the post type, usually plural. Default is Posts/Pages
     900 * - singular_name - name for one object of this post type. Default is Post/Page
     901 * - add_new - Default is Add New for both hierarchical and non-hierarchical types. When internationalizing this string, please use a {@link http://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} matching your post type. Example: <code>_x('Add New', 'product');</code>
     902 * - add_new_item - Default is Add New Post/Add New Page
     903 * - edit_item - Default is Edit Post/Edit Page
     904 * - edit - Default is Edit. When internationalizing this string, please use a {@link http://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} matching your post type. Example: <code>_x('Edit', 'product');</code>
     905 * - new_item - Default is New Post/New Page
     906 * - view_item - Default is View Post/View Page
     907 * - search_items - Default is Search Posts/Search Pages
     908 * - not_found - Default is No posts found/No pages found
     909 * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
     910 * - parent - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
     911 *
     912 * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages.)
     913 *
     914 * @since 3.0.0
     915 * @param object $post_type_object
     916 * @return object object with all the labels as member variables
     917 */
     918function get_post_type_labels( $post_type_object ) {
     919    $nohier_vs_hier_defaults = array(
     920        'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),
     921        'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),
     922        'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
     923        'add_new_item' => array( __('Add New Post'), __('Add New Page') ),
     924        'edit_item' => array( __('Edit Post'), __('Edit Page') ),
     925        'edit' => array( _x('Edit', 'post'), _x('Edit', 'page') ),
     926        'new_item' => array( __('New Post'), __('New Page') ),
     927        'view_item' => array( __('View Post'), __('View Page') ),
     928        'search_items' => array( __('Search Posts'), __('Search Pages') ),
     929        'not_found' => array( __('No posts found'), __('No pages found') ),
     930        'not_found_in_trash' => array( __('No posts found in Trash'), __('No pages found in Trash') ),
     931        'view' => array( __('View Post'), __('View Page') ),
     932        'parent' => array( null, __('Parent Page:') )
     933    );
     934   
     935    // try to get missing (singular_)?name from older style (singular_)?label member variables
     936    // we keep that for backwards compatibility
     937    // TODO: remove in 3.1
     938    if ( !isset( $post_type_object->labels['name'] ) && isset( $post_type_object->label ) ) {
     939        $post_type_object->labels['name'] = $post_type_object->label;
     940    }
     941    if ( !isset( $post_type_object->labels['singular_name'] ) && isset( $post_type_object->singular_label ) ) {
     942        $post_type_object->labels['singular_name'] = $post_type_object->singular_label;
     943    }
     944   
     945    $defaults = array_map( create_function( '$x', $post_type_object->hierarchical? 'return $x[1];' : 'return $x[0];' ), $nohier_vs_hier_defaults );
     946    $labels = array_merge( $defaults, $post_type_object->labels );
     947    return (object)$labels;
    892948}
    893949
Note: See TracChangeset for help on using the changeset viewer.