Make WordPress Core

Ticket #2531: register_cap.diff

File register_cap.diff, 2.9 KB (added by markjaquith, 18 years ago)
  • wp-includes/capabilities.php

     
    393393        return $caps;
    394394}
    395395
     396// Third party registration of additional capabilities, without assignment to a particular role
     397function register_cap($cap) {
     398        $caplist = get_settings('caplist');
     399        if ( !in_array($cap, (array) $caplist) ) {
     400                $caplist[] = $cap;
     401                $caplist = array_unique($caplist);
     402                sort($caplist);
     403                update_option('caplist', $caplist);
     404        }
     405}
     406
     407// Third party unregistration of additional capabilities
     408// This will NOT remove the capability from any roles that have it
     409function unregister_cap($cap) {
     410        $caplist = get_settings('caplist');
     411        if ( in_array($cap, (array) $caplist) ) {
     412                $caplist = array_unique($caplist);
     413                sort($caplist);
     414                $caplist = array_diff((array) $caplist, array($cap));
     415                update_option('caplist', $caplist);
     416        }
     417}
     418
     419// Third party fetching of all possible capabilities (WP default + third party)
     420// Mostly taken from Owen Winkler's Role Manager plugin
     421function get_all_caps() {
     422        global $wp_roles;
     423
     424        // Get Role List
     425        foreach ( $wp_roles->role_objects as $key => $role ) {
     426                foreach ( $role->capabilities as $cap => $grant ) {
     427                        $capnames[$cap] = $cap;
     428                }
     429        }
     430
     431        // Get third party caps
     432        $other_caps = get_option('caplist');
     433
     434        // Get default WP caps
     435        $default_caps = array(
     436                'activate_plugins',
     437                'delete_others_pages',
     438                'delete_others_posts',
     439                'delete_pages',
     440                'delete_posts',
     441                'delete_published_pages',
     442                'delete_published_posts',
     443                'edit_files',
     444                'edit_others_pages',
     445                'edit_others_posts',
     446                'edit_pages',
     447                'edit_plugins',
     448                'edit_posts',
     449                'edit_published_pages',
     450                'edit_published_posts',
     451                'edit_themes',
     452                'edit_users',
     453                'import',
     454                'level_0',
     455                'level_1',
     456                'level_10',
     457                'level_2',
     458                'level_3',
     459                'level_4',
     460                'level_5',
     461                'level_6',
     462                'level_7',
     463                'level_8',
     464                'level_9',
     465                'manage_categories',
     466                'manage_links',
     467                'manage_options',
     468                'moderate_comments',
     469                'publish_pages',
     470                'publish_posts',
     471                'read',
     472                'switch_themes',
     473                'unfiltered_html',
     474                'upload_files'
     475        );
     476
     477        if ( !is_array($capnames) )
     478                $capnames = array();
     479        if ( !is_array($other_caps) )
     480                $other_caps = array();
     481
     482        // Cleanup
     483        $capnames = array_merge($capnames, $other_caps, $default_caps);
     484        $capnames = array_unique($capnames);
     485        sort($capnames);
     486
     487        // Remove roles, we just want capabilities
     488        foreach ( $wp_roles->get_names() as $role ) {
     489                $key = array_search($role, $capnames);
     490                if ( $key !== false && $key !== null ) //array_search() returns null if not found in PHP 4.1
     491                        unset($capnames[$key]);
     492        }
     493
     494        return apply_filters('get_all_caps', $capnames);
     495}
     496
    396497// Capability checking wrapper around the global $current_user object.
    397498function current_user_can($capability) {
    398499        $current_user = wp_get_current_user();