function _cme_update_pp_usage()
{
    static $updated;
    if (!empty($updated)) {
        return true;
    }
    if (!current_user_can('pp_manage_settings')) {
        return false;
    }
    if (!empty($_REQUEST['update_filtered_types'])) {
        // update Press Permit "Filtered Post Types".  This determines whether type-specific capability definitions are forced
        $options = array('enabled_post_types', 'enabled_taxonomies');
        foreach ($options as $option_basename) {
            if (!isset($_POST["{$option_basename}-options"])) {
                continue;
            }
            $unselected = array();
            $value = array();
            foreach ($_POST["{$option_basename}-options"] as $key) {
                if (empty($_POST["{$option_basename}-{$key}"])) {
                    $unselected[$key] = true;
                } else {
                    $value[$key] = true;
                }
            }
            if ($current = pp_get_option($option_basename)) {
                if ($current = array_diff_key($current, $unselected)) {
                    $value = array_merge($current, $value);
                }
                // retain setting for any types which were previously enabled for filtering but are currently not registered
            }
            $value = stripslashes_deep($value);
            pp_update_option($option_basename, $value);
            $updated = true;
        }
        if (pp_wp_ver('3.5')) {
            pp_update_option('define_create_posts_cap', !empty($_REQUEST['pp_define_create_posts_cap']));
        }
    }
    if (!empty($_REQUEST['SaveRole'])) {
        if (!empty($_REQUEST['role'])) {
            $pp_only = (array) pp_get_option('supplemental_role_defs');
            if (empty($_REQUEST['pp_only_role'])) {
                $pp_only = array_diff($pp_only, array($_REQUEST['role']));
            } else {
                $pp_only[] = $_REQUEST['role'];
            }
            pp_update_option('supplemental_role_defs', array_unique($pp_only));
            _cme_pp_default_pattern_role($_REQUEST['role']);
        }
    }
    if ($updated) {
        pp_refresh_options();
    }
    return $updated;
}
Esempio n. 2
0
 function processAdminGeneral($post)
 {
     global $wp_roles;
     // Create a new role.
     if (!empty($post['CreateRole'])) {
         if ($newrole = $this->createRole($post['create-name'])) {
             ak_admin_notify(__('New role created.', $this->cm->ID));
             $this->cm->current = $newrole;
         } else {
             if (empty($post['create-name']) && (!defined('WPLANG') || !WPLANG)) {
                 ak_admin_error('Error: No role name specified.', $this->cm->ID);
             } else {
                 ak_admin_error(__('Error: Failed creating the new role.', $this->cm->ID));
             }
         }
         // Copy current role to a new one.
     } elseif (!empty($post['CopyRole'])) {
         $current = get_role($post['current']);
         if ($newrole = $this->createRole($post['copy-name'], $current->capabilities)) {
             ak_admin_notify(__('New role created.', $this->cm->ID));
             $this->cm->current = $newrole;
         } else {
             if (empty($post['copy-name']) && (!defined('WPLANG') || !WPLANG)) {
                 ak_admin_error('Error: No role name specified.', $this->cm->ID);
             } else {
                 ak_admin_error(__('Error: Failed creating the new role.', $this->cm->ID));
             }
         }
         // Save role changes. Already saved at start with self::saveRoleCapabilities().
     } elseif (!empty($post['SaveRole'])) {
         if (MULTISITE) {
             global $wp_roles;
             if (method_exists($wp_roles, 'reinit')) {
                 $wp_roles->reinit();
             }
         }
         $this->saveRoleCapabilities($post['current'], $post['caps'], $post['level']);
         if (defined('PP_ACTIVE')) {
             // log customized role caps for subsequent restoration
             // for bbPress < 2.2, need to log customization of roles following bbPress activation
             $plugins = function_exists('bbp_get_version') && version_compare(bbp_get_version(), '2.2', '<') ? array('bbpress.php') : array();
             // back compat
             if (!($customized_roles = get_option('pp_customized_roles'))) {
                 $customized_roles = array();
             }
             $customized_roles[$post['role']] = (object) array('caps' => array_map('boolval', $post['caps']), 'plugins' => $plugins);
             update_option('pp_customized_roles', $customized_roles);
             global $wpdb;
             $wpdb->query("UPDATE {$wpdb->options} SET autoload = 'no' WHERE option_name = 'pp_customized_roles'");
         }
         // Create New Capability and adds it to current role.
     } elseif (!empty($post['AddCap'])) {
         if (MULTISITE) {
             global $wp_roles;
             if (method_exists($wp_roles, 'reinit')) {
                 $wp_roles->reinit();
             }
         }
         $role = get_role($post['current']);
         $role->name = $post['current'];
         // bbPress workaround
         if ($newname = $this->createNewName($post['capability-name'])) {
             $role->add_cap($newname['name']);
             $this->cm->message = __('New capability added to role.');
             // for bbPress < 2.2, need to log customization of roles following bbPress activation
             $plugins = function_exists('bbp_get_version') && version_compare(bbp_get_version(), '2.2', '<') ? array('bbpress.php') : array();
             // back compat
             if (!($customized_roles = get_option('pp_customized_roles'))) {
                 $customized_roles = array();
             }
             $customized_roles[$post['role']] = (object) array('caps' => array_merge($role->capabilities, array($newname['name'] => 1)), 'plugins' => $plugins);
             update_option('pp_customized_roles', $customized_roles);
             global $wpdb;
             $wpdb->query("UPDATE {$wpdb->options} SET autoload = 'no' WHERE option_name = 'pp_customized_roles'");
         } else {
             $this->cm->message = __('Incorrect capability name.');
         }
     } elseif (!empty($post['update_filtered_types'])) {
         if (cme_update_pp_usage()) {
             ak_admin_notify(__('Capability settings saved.', $this->cm->ID));
         } else {
             ak_admin_error(__('Error saving capability settings.', $this->cm->ID));
         }
     } else {
         // TODO: Implement exceptions. This must be a fatal error.
         ak_admin_error(__('Bad form received.', $this->cm->ID));
     }
     if (!empty($newrole) && defined('PP_ACTIVE')) {
         if (!empty($post['CreateRole']) && !empty($_REQUEST['new_role_pp_only']) || !empty($post['CopyRole']) && !empty($_REQUEST['copy_role_pp_only'])) {
             $pp_only = (array) pp_get_option('supplemental_role_defs');
             $pp_only[] = $newrole;
             pp_update_option('supplemental_role_defs', $pp_only);
             _cme_pp_default_pattern_role($newrole);
             pp_refresh_options();
         }
     }
 }
Esempio n. 3
0
function _cme_new_blog($new_blog_id)
{
    if ($autocreate_roles = get_site_option('cme_autocreate_roles')) {
        global $wp_roles, $blog_id;
        $restore_blog_id = $blog_id;
        switch_to_blog(1);
        $wp_roles->reinit();
        $main_site_caps = array();
        $role_captions = array();
        $admin_role = $wp_roles->get_role('administrator');
        $main_admin_caps = $admin_role->capabilities;
        if (defined('PP_ACTIVE')) {
            $main_pp_only = (array) pp_get_option('supplemental_role_defs');
        }
        //$pp_only[]= $newrole;
        foreach ($autocreate_roles as $role_name) {
            if ($role = get_role($role_name)) {
                $main_site_caps[$role_name] = $role->capabilities;
                $role_captions[$role_name] = $wp_roles->role_names[$role_name];
            }
        }
        switch_to_blog($new_blog_id);
        $wp_roles->reinit();
        if (defined('PP_ACTIVE')) {
            pp_refresh_options();
            $blog_pp_only = (array) pp_get_option('supplemental_role_defs');
        }
        foreach ($main_site_caps as $role_name => $caps) {
            if ($blog_role = $wp_roles->get_role($role_name)) {
                $stored_role_caps = !empty($blog_role->capabilities) && is_array($blog_role->capabilities) ? array_intersect($blog_role->capabilities, array(true, 1)) : array();
                // Find caps to add and remove
                $add_caps = array_diff_key($caps, $stored_role_caps);
                $del_caps = array_intersect_key(array_diff_key($stored_role_caps, $caps), $main_admin_caps);
                // don't mess with caps that are totally unused on main site
                // Add new capabilities to role
                foreach ($add_caps as $cap => $grant) {
                    $blog_role->add_cap($cap);
                }
                // Remove capabilities from role
                foreach ($del_caps as $cap => $grant) {
                    $blog_role->remove_cap($cap);
                }
            } else {
                $wp_roles->add_role($role_name, $role_captions[$role_name], $caps);
            }
            if (defined('PP_ACTIVE')) {
                if (in_array($role_name, $main_pp_only)) {
                    _cme_pp_default_pattern_role($role_name);
                    $blog_pp_only[] = $role_name;
                } else {
                    array_diff($blog_pp_only, array($role_name));
                }
            }
        }
        if (defined('PP_ACTIVE')) {
            pp_update_option('supplemental_role_defs', $blog_pp_only);
        }
        switch_to_blog($restore_blog_id);
        $wp_roles->reinit();
        if (defined('PP_ACTIVE')) {
            pp_refresh_options();
        }
    }
}