Ticket #20824: capabilities.php.patch
File capabilities.php.patch, 3.8 KB (added by , 11 years ago) |
---|
-
src/wp-includes/capabilities.php
819 819 } 820 820 821 821 /** 822 * Whether user has role. 823 * 824 * @since 3.7.0 825 * @access public 826 * 827 * @param string $role Role name to search. 828 * @return bool True, if user has role; false, if user does not have role. 829 */ 830 function has_role( $role ) { 831 if ( in_array($role, $this->roles) ) { 832 return true; 833 } 834 835 return false; 836 } 837 838 /** 822 839 * Choose the maximum level the user has. 823 840 * 824 841 * Will compare the level from the $item parameter against the $max … … 906 923 } 907 924 908 925 /** 909 * Whether user has capability or role name.926 * Whether user has capability. 910 927 * 911 * This is useful for looking up whether the user has a specific role 912 * assigned to the user. The second optional parameter can also be used to 928 * The second optional parameter can also be used to 913 929 * check for capabilities against a specific object, such as a post or user. 914 930 * 915 931 * @since 2.0.0 916 932 * @access public 917 933 * 918 * @param string|int $cap Capability or rolename to search.934 * @param string|int $cap Capability name to search. 919 935 * @return bool True, if user has capability; false, if user does not have capability. 920 936 */ 921 937 function has_cap( $cap ) { … … 1268 1284 } 1269 1285 1270 1286 /** 1271 * Whether current user has capability or role.1287 * Whether current user has capability. 1272 1288 * 1273 1289 * @since 2.0.0 1274 1290 * 1275 * @param string $capability Capability or rolename.1291 * @param string $capability Capability name. 1276 1292 * @return bool 1277 1293 */ 1278 1294 function current_user_can( $capability ) { … … 1288 1304 } 1289 1305 1290 1306 /** 1291 * Whether current user has a capability or rolefor a given blog.1307 * Whether current user has a capability for a given blog. 1292 1308 * 1293 1309 * @since 3.0.0 1294 1310 * 1295 1311 * @param int $blog_id Blog ID 1296 * @param string $capability Capability or role name.1312 * @param string $capability Capability. 1297 1313 * @return bool 1298 1314 */ 1299 1315 function current_user_can_for_blog( $blog_id, $capability ) { … … 1317 1333 } 1318 1334 1319 1335 /** 1320 * Whether author of supplied post has capability or role.1336 * Whether author of supplied post has capability. 1321 1337 * 1322 1338 * @since 2.9.0 1323 1339 * 1324 1340 * @param int|object $post Post ID or post object. 1325 * @param string $capability Capability or role name.1341 * @param string $capability Capability. 1326 1342 * @return bool 1327 1343 */ 1328 1344 function author_can( $post, $capability ) { … … 1341 1357 } 1342 1358 1343 1359 /** 1344 * Whether a particular user has capability or role.1360 * Whether a particular user has capability. 1345 1361 * 1346 1362 * @since 3.1.0 1347 1363 * 1348 1364 * @param int|object $user User ID or object. 1349 * @param string $capability Capability or role name.1365 * @param string $capability Capability. 1350 1366 * @return bool 1351 1367 */ 1352 1368 function user_can( $user, $capability ) { … … 1363 1379 } 1364 1380 1365 1381 /** 1382 * Whether current user has role. 1383 * 1384 * @since 3.7.0 1385 * 1386 * @param string $role role name. 1387 * @return bool 1388 */ 1389 function current_user_has_role( $role ) { 1390 $current_user = wp_get_current_user(); 1391 1392 if ( empty( $current_user ) ) 1393 return false; 1394 1395 return $current_user->has_role( $role ); 1396 } 1397 1398 /** 1366 1399 * Retrieve role object. 1367 1400 * 1368 1401 * @see WP_Roles::get_role() Uses method to retrieve role object. -
tests/phpunit/tests/user/capabilities.php
667 667 668 668 wp_set_current_user( $old_uid ); 669 669 } 670 671 function test_current_user_has_role() { 672 $user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); 673 wp_set_current_user( $user->ID ); 674 675 $this->assertTrue(current_user_has_role('administrator')); 676 $this->assertFalse(current_user_has_role('subscriber')); 677 } 670 678 }