Ticket #38218: 38218.diff
File 38218.diff, 14.4 KB (added by , 8 years ago) |
---|
-
new file src/wp-includes/class-wp-post-type-labels.php
diff --git src/wp-includes/class-wp-post-type-labels.php src/wp-includes/class-wp-post-type-labels.php new file mode 100644 index 0000000..898ca12
- + 1 <?php 2 /** 3 * Post API: WP_Post_Type_Labels class 4 * 5 * @package WordPress 6 * @subpackage Post 7 * @since 4.7.0 8 */ 9 10 /** 11 * Core class used for providing post type labels. 12 * 13 * @since 4.7.0 14 * 15 * @see register_post_type() 16 * @see get_post_type_labels() 17 * 18 * @property string name 19 * @property string singular_name 20 * @property string add_new 21 * @property string add_new_item 22 * @property string edit_item 23 * @property string new_item 24 * @property string view_item 25 * @property string view_items 26 * @property string search_items 27 * @property string not_found 28 * @property string not_found_in_trash 29 * @property string parent_item_colon 30 * @property string all_items 31 * @property string archives 32 * @property string insert_into_item 33 * @property string uploaded_to_this_item 34 * @property string featured_image 35 * @property string set_featured_image 36 * @property string remove_featured_image 37 * @property string use_featured_image 38 * @property string filter_items_list 39 * @property string items_list_navigation 40 * @property string items_list 41 * @property string menu_name 42 * @property string name_admin_bar 43 */ 44 final class WP_Post_Type_Labels { 45 /** 46 * Current post type. 47 * 48 * @since 4.7.0 49 * @access protected 50 * @var WP_Post_Type 51 */ 52 protected $post_type; 53 54 /** 55 * The default labels of the post type. 56 * 57 * @since 4.7.0 58 * @access protected 59 * @var array 60 */ 61 protected $defaults = array(); 62 63 /** 64 * Constructor. 65 * 66 * Will populate object properties from the provided arguments and assign other 67 * default properties based on that information. 68 * 69 * @since 4.7.0 70 * @access public 71 * 72 * @see register_post_type() 73 * 74 * @param WP_Post_Type $post_type Post type object. 75 */ 76 public function __construct( $post_type ) { 77 $this->post_type = $post_type; 78 79 $this->defaults = (array) $post_type->labels; 80 } 81 82 /** 83 * Returns the (non-)hierarchical default labels. 84 * 85 * @since 4.7.0 86 * @access protected 87 * 88 * @return array Hierarchical vs non-hierarchical default labels. 89 */ 90 protected function nohier_vs_hier_defaults() { 91 $nohier_vs_hier_defaults = array( 92 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ), 93 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ), 94 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ), 95 'add_new_item' => array( __('Add New Post'), __('Add New Page') ), 96 'edit_item' => array( __('Edit Post'), __('Edit Page') ), 97 'new_item' => array( __('New Post'), __('New Page') ), 98 'view_item' => array( __('View Post'), __('View Page') ), 99 'view_items' => array( __('View Posts'), __('View Pages') ), 100 'search_items' => array( __('Search Posts'), __('Search Pages') ), 101 'not_found' => array( __('No posts found.'), __('No pages found.') ), 102 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ), 103 'parent_item_colon' => array( null, __('Parent Page:') ), 104 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ), 105 'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ), 106 'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ), 107 'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ), 108 'featured_image' => array( __( 'Featured Image' ), __( 'Featured Image' ) ), 109 'set_featured_image' => array( __( 'Set featured image' ), __( 'Set featured image' ) ), 110 'remove_featured_image' => array( __( 'Remove featured image' ), __( 'Remove featured image' ) ), 111 'use_featured_image' => array( __( 'Use as featured image' ), __( 'Use as featured image' ) ), 112 'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ), 113 'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ), 114 'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ), 115 ); 116 117 return $nohier_vs_hier_defaults; 118 } 119 120 /** 121 * Magic method for checking the existence of a certain post type label. 122 * 123 * @since 4.7.0 124 * @access public 125 * 126 * @param string $key Label to check if set. 127 * @return bool Whether the given label is set. 128 */ 129 public function __isset( $key ) { 130 return null !== $this->{$key}; 131 } 132 133 /** 134 * Magic method for accessing post type labels. 135 * 136 * @since 4.7.0 137 * @access public 138 * 139 * @param string $key Label to retrieve. 140 * @return string THe post type label. 141 */ 142 public function __get( $key ) { 143 /** 144 * Filters the labels of a specific post type. 145 * 146 * The dynamic portion of the hook name, `$post_type`, refers to 147 * the post type slug. 148 * 149 * @since 3.5.0 150 * 151 * @see get_post_type_labels() for the full list of labels. 152 * 153 * @param object $labels Object with labels for the post type as member variables. 154 */ 155 $original = apply_filters( "post_type_labels_{$this->post_type->name}", $this->defaults ); 156 157 $defaults = array(); 158 159 foreach ( $this->nohier_vs_hier_defaults() as $default_key => $value ) { 160 $defaults[ $default_key ] = $this->post_type->hierarchical ? $value[1] : $value[0]; 161 } 162 163 $labels = array_merge( $defaults, $original ); 164 165 if ( isset( $labels[ $key ] ) ) { 166 return $labels[ $key ]; 167 } 168 169 if ( 'name' === $key && isset( $this->post_type->label ) ) { 170 return $this->post_type->label; 171 } 172 173 if ( 'singular_name' === $key ) { 174 $key = 'name'; 175 } 176 177 if ( 'name_admin_bar' === $key ) { 178 return isset( $this->singular_name ) ? $this->singular_name : $this->post_type->name; 179 } 180 181 if ( 'menu_name' === $key ) { 182 $key = 'name'; 183 } 184 185 if ( 'all_items' === $key ) { 186 $key = 'menu_name'; 187 } 188 189 if ( 'archives' === $key ) { 190 $key = 'all_items'; 191 } 192 193 return $labels[ $key ]; 194 } 195 } -
src/wp-includes/class-wp-post-type.php
diff --git src/wp-includes/class-wp-post-type.php src/wp-includes/class-wp-post-type.php index 0b1d3c0..0dd44d4 100644
final class WP_Post_Type { 43 43 * 44 44 * @since 4.6.0 45 45 * @access public 46 * @var object$labels46 * @var WP_Post_Type_Labels $labels 47 47 */ 48 48 public $labels; 49 49 -
src/wp-includes/post.php
diff --git src/wp-includes/post.php src/wp-includes/post.php index c02108e..01a7ec7 100644
function _post_type_meta_capabilities( $capabilities = null ) { 1273 1273 * @access private 1274 1274 * 1275 1275 * @param object|WP_Post_Type $post_type_object Post type object. 1276 * @return objectObject with all the labels as member variables.1276 * @return WP_Post_Type_Labels Object with all the labels as member variables. 1277 1277 */ 1278 1278 function get_post_type_labels( $post_type_object ) { 1279 $nohier_vs_hier_defaults = array( 1280 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ), 1281 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ), 1282 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ), 1283 'add_new_item' => array( __('Add New Post'), __('Add New Page') ), 1284 'edit_item' => array( __('Edit Post'), __('Edit Page') ), 1285 'new_item' => array( __('New Post'), __('New Page') ), 1286 'view_item' => array( __('View Post'), __('View Page') ), 1287 'view_items' => array( __('View Posts'), __('View Pages') ), 1288 'search_items' => array( __('Search Posts'), __('Search Pages') ), 1289 'not_found' => array( __('No posts found.'), __('No pages found.') ), 1290 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ), 1291 'parent_item_colon' => array( null, __('Parent Page:') ), 1292 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ), 1293 'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ), 1294 'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ), 1295 'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ), 1296 'featured_image' => array( __( 'Featured Image' ), __( 'Featured Image' ) ), 1297 'set_featured_image' => array( __( 'Set featured image' ), __( 'Set featured image' ) ), 1298 'remove_featured_image' => array( __( 'Remove featured image' ), __( 'Remove featured image' ) ), 1299 'use_featured_image' => array( __( 'Use as featured image' ), __( 'Use as featured image' ) ), 1300 'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ), 1301 'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ), 1302 'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ), 1303 ); 1304 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; 1305 1306 $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); 1307 1308 $post_type = $post_type_object->name; 1309 1310 $default_labels = clone $labels; 1311 1312 /** 1313 * Filters the labels of a specific post type. 1314 * 1315 * The dynamic portion of the hook name, `$post_type`, refers to 1316 * the post type slug. 1317 * 1318 * @since 3.5.0 1319 * 1320 * @see get_post_type_labels() for the full list of labels. 1321 * 1322 * @param object $labels Object with labels for the post type as member variables. 1323 */ 1324 $labels = apply_filters( "post_type_labels_{$post_type}", $labels ); 1325 1326 // Ensure that the filtered labels contain all required default values. 1327 $labels = (object) array_merge( (array) $default_labels, (array) $labels ); 1328 1329 return $labels; 1279 return new WP_Post_Type_Labels( $post_type_object ); 1330 1280 } 1331 1281 1332 1282 /** -
src/wp-settings.php
diff --git src/wp-settings.php src/wp-settings.php index 80f556c..cd4b37a 100644
require( ABSPATH . WPINC . '/post.php' ); 163 163 require( ABSPATH . WPINC . '/class-walker-page.php' ); 164 164 require( ABSPATH . WPINC . '/class-walker-page-dropdown.php' ); 165 165 require( ABSPATH . WPINC . '/class-wp-post-type.php' ); 166 require( ABSPATH . WPINC . '/class-wp-post-type-labels.php' ); 166 167 require( ABSPATH . WPINC . '/class-wp-post.php' ); 167 168 require( ABSPATH . WPINC . '/post-template.php' ); 168 169 require( ABSPATH . WPINC . '/revision.php' ); -
new file tests/phpunit/tests/post/getPostTypeLabels.php
diff --git tests/phpunit/tests/post/getPostTypeLabels.php tests/phpunit/tests/post/getPostTypeLabels.php new file mode 100644 index 0000000..59b9126
- + 1 <?php 2 3 /** 4 * @group post 5 */ 6 class Tests_Get_Post_Type_Labels extends WP_UnitTestCase { 7 public function test_returns_an_object() { 8 $this->assertInternalType( 'object', get_post_type_labels( (object) array( 9 'name' => 'foo', 10 'labels' => array(), 11 'hierarchical' => false, 12 ) ) ); 13 } 14 15 public function test_returns_hierachical_labels() { 16 $labels = get_post_type_labels( (object) array( 17 'name' => 'foo', 18 'labels' => array(), 19 'hierarchical' => true, 20 ) ); 21 22 $this->assertSame( 'Pages', $labels->name ); 23 } 24 25 public function test_existing_labels_are_not_overridden() { 26 $labels = get_post_type_labels( (object) array( 27 'name' => 'foo', 28 'labels' => array( 29 'singular_name' => 'Foo', 30 ), 31 'hierarchical' => false, 32 ) ); 33 34 $this->assertSame( 'Foo', $labels->singular_name ); 35 } 36 37 public function test_name_admin_bar_label_should_fall_back_to_singular_name() { 38 $labels = get_post_type_labels( (object) array( 39 'name' => 'foo', 40 'labels' => array( 41 'singular_name' => 'Foo', 42 ), 43 'hierarchical' => false, 44 ) ); 45 46 $this->assertSame( 'Foo', $labels->name_admin_bar ); 47 } 48 49 public function test_menu_name_should_fall_back_to_name() { 50 $labels = get_post_type_labels( (object) array( 51 'name' => 'foo', 52 'labels' => array( 53 'name' => 'Bar', 54 ), 55 'hierarchical' => false, 56 ) ); 57 58 $this->assertSame( 'Bar', $labels->menu_name ); 59 } 60 61 public function test_labels_should_be_added_when_registering_a_post_type() { 62 $post_type_object = register_post_type( 'foo', array( 63 'labels' => array( 64 'singular_name' => 'bar', 65 ), 66 ) ); 67 68 unregister_post_type( 'foo' ); 69 70 $this->assertObjectHasAttribute( 'labels', $post_type_object ); 71 $this->assertObjectHasAttribute( 'label', $post_type_object ); 72 $this->assertNotNull( $post_type_object->labels->not_found_in_trash ); 73 } 74 75 public function test_label_should_be_derived_from_labels_when_registering_a_post_type() { 76 $post_type_object = register_post_type( 'foo', array( 77 'labels' => array( 78 'name' => 'bar', 79 ), 80 ) ); 81 82 $this->assertSame( 'bar', $post_type_object->label ); 83 84 unregister_post_type( 'foo' ); 85 } 86 87 /** 88 * @ticket 33543 89 */ 90 public function test_should_fall_back_on_defaults_when_filtered_labels_do_not_contain_the_keys() { 91 add_filter( 'post_type_labels_foo', array( $this, 'filter_post_type_labels' ) ); 92 register_post_type( 'foo' ); 93 94 $this->assertNotNull( get_post_type_object( 'foo' )->labels->featured_image ); 95 $this->assertNotNull( get_post_type_object( 'foo' )->labels->set_featured_image ); 96 97 unregister_post_type( 'foo' ); 98 remove_filter( 'post_type_labels_foo', array( $this, 'filter_post_type_labels' ) ); 99 } 100 101 public function filter_post_type_labels( $labels ) { 102 unset( $labels->featured_image ); 103 unset( $labels->set_featured_image ); 104 105 return $labels; 106 } 107 } -
tests/phpunit/tests/post/types.php
diff --git tests/phpunit/tests/post/types.php tests/phpunit/tests/post/types.php index e7dcf84..31a51c2 100644
class Tests_Post_Types extends WP_UnitTestCase { 220 220 } 221 221 222 222 /** 223 * @ticket 33543224 */225 function test_get_post_type_labels_should_fall_back_on_defaults_when_filtered_labels_do_not_contain_the_keys() {226 add_filter( 'post_type_labels_foo', array( $this, 'filter_post_type_labels' ) );227 register_post_type( 'foo' );228 229 $this->assertObjectHasAttribute( 'featured_image', get_post_type_object( 'foo' )->labels );230 $this->assertObjectHasAttribute( 'set_featured_image', get_post_type_object( 'foo' )->labels );231 232 _unregister_post_type( 'foo' );233 remove_filter( 'post_type_labels_foo', array( $this, 'filter_post_type_labels' ) );234 }235 236 public function filter_post_type_labels( $labels ) {237 unset( $labels->featured_image );238 unset( $labels->set_featured_image );239 return $labels;240 }241 242 243 /**244 223 * @ticket 30013 245 224 */ 246 225 public function test_get_post_type_object_with_non_scalar_values() {