function frontier_post_set_defaults()
{
    if (!defined('FRONTIER_POST_SETTINGS_OPTION_NAME')) {
        define('FRONTIER_POST_SETTINGS_OPTION_NAME', "frontier_post_general_options");
    }
    if (!defined('FRONTIER_POST_CAPABILITY_OPTION_NAME')) {
        define('FRONTIER_POST_CAPABILITY_OPTION_NAME', "frontier_post_capabilities");
    }
    include FRONTIER_POST_DIR . '/include/frontier_post_defaults.php';
    $fp_last_upgrade = fp_get_option('fps_options_migrated_version', get_option("frontier_post_version", '0.0.0'));
    // Upgrade old versions, but dont run upgrade if fresh install
    if ($fp_last_upgrade != '0.0.0' && version_compare($fp_last_upgrade, '3.3.0') < 0) {
        include FRONTIER_POST_DIR . '/admin/frontier-post-convert-options.php';
        fps_cnv_general_options(true);
        $fp_upgrade_msg = 'Frontier Post - Settings upgraded from version: ' . $fp_last_upgrade . ' to version: ' . FRONTIER_POST_VERSION;
    } else {
        //******************************************************************************
        // add settings if not already there
        //******************************************************************************
        if (!fp_get_option_bool('fps_keep_options_uninstall', false)) {
            // Set default capabilities
            $saved_capabilities = frontier_post_get_capabilities();
            // administrators capabilities
            $tmp_administrator_cap = array('frontier_post_can_add' => 'true', 'frontier_post_can_edit' => 'true', 'frontier_post_can_delete' => 'true', 'frontier_post_can_publish' => 'true', 'frontier_post_can_draft' => 'true', 'frontier_post_can_pending' => 'true', 'frontier_post_can_private' => 'true', 'frontier_post_redir_edit' => 'true', 'frontier_post_show_admin_bar' => 'true', 'frontier_post_exerpt_edit' => 'true', 'frontier_post_tags_edit' => 'true', 'frontier_post_can_media' => 'true', 'frontier_post_can_page' => 'true', 'fps_role_editor_type' => 'full', 'fps_role_category_layout' => 'multi', 'fps_role_default_category' => get_option("default_category"), 'fps_role_allowed_categories' => '');
            // editor
            $tmp_editor_cap = $tmp_administrator_cap;
            // Author
            $tmp_author_cap = $tmp_editor_cap;
            $tmp_author_cap['frontier_post_can_private'] = 'false';
            $tmp_author_cap['frontier_post_show_admin_bar'] = 'false';
            $tmp_author_cap['frontier_post_can_page'] = 'false';
            // Contributor
            $tmp_contributor_cap = $tmp_author_cap;
            $tmp_contributor_cap['frontier_post_can_delete'] = 'false';
            $tmp_contributor_cap['frontier_post_can_publish'] = 'false';
            $tmp_contributor_cap['frontier_post_redir_edit'] = 'false';
            $tmp_contributor_cap['frontier_post_tags_edit'] = 'false';
            $tmp_contributor_cap['frontier_post_can_media'] = 'false';
            $tmp_contributor_cap['fps_role_editor_type'] = 'minimal-visual';
            // Subscriber
            $tmp_subscriber_cap = $tmp_contributor_cap;
            $tmp_subscriber_cap['frontier_post_can_add'] = 'false';
            $tmp_subscriber_cap['frontier_post_can_edit'] = 'false';
            $tmp_subscriber_cap['frontier_post_can_pending'] = 'false';
            $tmp_subscriber_cap['frontier_post_can_draft'] = 'false';
            $wp_roles = new WP_Roles();
            $roles = $wp_roles->get_names();
            $saved_capabilities = frontier_post_get_capabilities();
            foreach ($roles as $key => $item) {
                switch ($key) {
                    case 'administrator':
                        $tmp_cap_list = $tmp_administrator_cap;
                        break;
                    case 'editor':
                        $tmp_cap_list = $tmp_editor_cap;
                        break;
                    case 'author':
                        $tmp_cap_list = $tmp_author_cap;
                        break;
                    case 'frontier-author':
                        $tmp_cap_list = $tmp_author_cap;
                        break;
                    case 'contributor':
                        $tmp_cap_list = $tmp_contributor_cap;
                        break;
                    case 'subscriber':
                        $tmp_cap_list = $tmp_subscriber_cap;
                        break;
                    default:
                        $tmp_cap_list = $tmp_contributor_cap;
                        break;
                }
                $saved_capabilities[$key] = $tmp_cap_list;
            }
            // roles
            // Save options
            update_option(FRONTIER_POST_CAPABILITY_OPTION_NAME, $saved_capabilities);
        }
        // end update settings if not saved from during previous uninstall
    }
    //end Upgrade or not
    // update default settings
    fp_post_set_defaults();
    // Set Wordpress capabilities
    frontier_post_set_cap();
    global $wpdb;
    // Check if page containing [frontier-post] exists already, else create it
    $tmp_id = $wpdb->get_var("SELECT id \r\n\t\t  FROM {$wpdb->posts} \r\n\t\t  WHERE post_type='page' AND \r\n\t\t  post_status='publish' AND \r\n\t\t post_content LIKE '%[frontier-post]%'\r\n\t\t");
    if ((int) $tmp_id <= 0) {
        // Add new page
        $my_page = array('post_title' => __('My Posts', 'frontier-post'), 'post_content' => '[frontier-post]', 'post_status' => 'publish', 'comment_status' => 'closed', 'post_type' => 'page', 'ping_status' => 'closed');
        // Insert the page into the database
        $tmp_id = wp_insert_post($my_page);
        // save page id
        $fps_save_general_options = frontier_post_get_settings();
        $fps_save_general_options['fps_page_id'] = $tmp_id;
        update_option(FRONTIER_POST_SETTINGS_OPTION_NAME, $fps_save_general_options);
    } else {
        if (fp_get_option_int('fps_page_id', 0) === 0) {
            // save page id
            $fps_save_general_options = frontier_post_get_settings();
            $fps_save_general_options['fps_page_id'] = $tmp_id;
            update_option(FRONTIER_POST_SETTINGS_OPTION_NAME, $fps_save_general_options);
        }
    }
    //save to options that capabilities has been migrated
    $fps_general_options = frontier_post_get_settings();
    $fps_general_options['fps_options_migrated'] = "true";
    $fps_general_options['fps_options_migrated_version'] = FRONTIER_POST_VERSION;
    update_option(FRONTIER_POST_SETTINGS_OPTION_NAME, $fps_general_options);
    // Put an updated message on the screen - NO NO, Cant do that in activation script.
    //echo '<div class="updated"><p><strong>'.__("Frontier Post - Default settings and capabilities set - Please review settings and capabilities", 'frontier-post' ).'</strong></p></div>';
}
function frontier_post_admin_page_capabilities()
{
    //must check that the user has the required capability
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    include FRONTIER_POST_DIR . "/include/frontier_post_defaults.php";
    //include("../include/frontier_post_defaults.php");
    echo '<strong>Frontier Post version: ' . FRONTIER_POST_VERSION . '</strong>';
    // ****************************************************************************
    // Save settings
    //*******************************************************************************
    // See if the user has posted us some information
    // If they did, this hidden field will be set to 'Y'
    if (isset($_POST["frontier_isupdated_capabilities_hidden"]) && $_POST["frontier_isupdated_capabilities_hidden"] == 'Y') {
        // ****************************************************************************
        // Update option for capabilities per role
        //*******************************************************************************
        // Do not update capabilities if capabilities are managed externally
        if (!fp_get_option_bool("fps_external_cap")) {
            // Reinstate roles
            $wp_roles = new WP_Roles();
            $roles = $wp_roles->get_names();
            $tmp_cap_list = array_merge($fp_capability_list, $fp_role_option_list);
            $saved_capabilities = frontier_post_get_capabilities();
            foreach ($roles as $key => $item) {
                $xrole = get_role($key);
                $xrole_caps = $xrole->capabilities;
                foreach ($tmp_cap_list as $tmp_cap => $tmp_cap_name) {
                    $tmp_name = $key . '_' . $tmp_cap;
                    $def_value = "false";
                    if ($tmp_cap == 'fps_role_editor_type') {
                        $def_value = "minimal-visual";
                    }
                    if ($tmp_cap == 'fps_role_category_layout') {
                        $def_value = "multi";
                    }
                    if ($tmp_cap == 'fps_role_default_category') {
                        $def_value = get_option("default_category");
                    }
                    if ($tmp_cap == 'fps_role_allowed_categories') {
                        $def_value = '';
                    }
                    if (isset($_POST[$tmp_name])) {
                        $tmp_value = $_POST[$tmp_name] ? $_POST[$tmp_name] : $def_value;
                    } else {
                        $tmp_value = $def_value;
                    }
                    $saved_capabilities[$key][$tmp_cap] = $tmp_value;
                }
                //caps
            }
            // roles
            update_option(FRONTIER_POST_CAPABILITY_OPTION_NAME, $saved_capabilities);
            // Put an settings updated message on the screen
            echo '<div class="updated"><p><strong>' . __("Settings saved.", 'frontier-post') . '</strong></p></div>';
            // Set Wordpress capabilities
            frontier_post_set_cap();
            // Put an settings updated message on the screen
            echo '<div class="updated"><p><strong>' . __("Capabilities set.", 'frontier-post') . '</strong></p></div>';
        }
        // End external managed capabilities
    }
    // end update options
    echo '<div class="wrap">';
    echo '<div class="frontier-admin-menu">';
    echo '<h2>' . __("Frontier Post - Capabilities & Role based settings", "frontier-post") . '</h2>';
    echo '<hr>' . __("Documentation", "frontier_post") . ': <a href="http://wpfrontier.com/frontier-post-profiles-capabilities/" target="_blank">Profiles & Capabilities</a>';
    echo ' - <a href="http://wpfrontier.com/frontier-post-role-based-settings/" target="_blank">Role based settings</a><hr>';
    echo '<form name="frontier_post_settings" method="post" action="">';
    echo '<input type="hidden" name="frontier_isupdated_capabilities_hidden" value="Y">';
    //*****************************************************************************
    // Start capability listing
    //*****************************************************************************
    if (fp_get_option_bool("fps_external_cap")) {
        echo '<i><strong>' . __('Capabilities managed externally', 'frontier-post') . '</strong></i>';
    } else {
        echo '<table border="1" cellspacing="0" cellpadding="0">';
        echo "<tr>";
        echo '<th colspan="16"></center>' . __("Capabilities by user role", "frontier-post") . '</center></th>';
        echo "</tr><tr>";
        echo '<th width="6%">' . __("Role", "frontier-post") . '</th>';
        foreach ($fp_capability_list as $tmp_cap => $tmp_cap_name) {
            echo '<th width="6%">' . $tmp_cap_name . '</th>';
        }
        echo "</tr><tr>";
        global $wp_roles;
        if (!isset($wp_roles)) {
            $wp_roles = new WP_Roles();
        }
        $roles = $wp_roles->get_names();
        $saved_capabilities = frontier_post_get_capabilities();
        // loop through each role
        foreach ($roles as $key => $item) {
            echo '<tr><td>' . $item . '</td>';
            //If Role does not exists, create it in the frontier post array
            if (!array_key_exists($key, $saved_capabilities)) {
                $saved_capabilities[$key] = array();
            }
            $tmp_role_settings = $saved_capabilities[$key];
            if (!is_array($tmp_role_settings)) {
                $tmp_role_settings = array();
            }
            foreach ($fp_capability_list as $tmp_cap => $tmp_cap_name) {
                $tmp_name = $key . '_' . $tmp_cap;
                if (array_key_exists($tmp_cap, $tmp_role_settings)) {
                    $tmp_value = $saved_capabilities[$key][$tmp_cap] ? $saved_capabilities[$key][$tmp_cap] : "false";
                } else {
                    $tmp_value = "false";
                }
                if ($tmp_value == "true") {
                    $tmp_checked = " checked";
                } else {
                    $tmp_checked = " ";
                }
                echo '<td><center>';
                //echo $key."<br>".$tmp_cap."<br>";
                //Hide can_media for subscribers and contributors
                if ($tmp_cap == "frontier_post_can_media" && ($key == "subscriber" || $key == "contributor")) {
                    echo 'N/A';
                } else {
                    echo '<input value="true" type="checkbox" name="' . $tmp_name . '" id="' . $tmp_name . '" ' . $tmp_checked . ' />';
                }
                echo '</center></td>';
            }
            // end cap
            echo '</tr>';
        }
        // end roles
        echo '</table>';
        echo '* ' . __("Wordpress standard does not allow Contributors  and Subscribers to upload media", "frontier-post");
    }
    // endfps_external_cap
    //*****************************************************************************
    // Start Role Based settings
    //*****************************************************************************
    echo '<hr>';
    echo '<table border="1" cellspacing="0" cellpadding="0">';
    echo "<tr>";
    echo '<th colspan="5"></center>' . __("Role based settings", "frontier-post") . '</center></th>';
    echo "</tr><tr>";
    echo '<th width="6%">' . __("Role", "frontier-post") . '</th>';
    foreach ($fp_role_option_list as $tmp_role_option => $tmp_role_option_name) {
        echo '<th width="6%">' . $tmp_role_option_name . '</th>';
    }
    echo "</tr><tr>";
    global $wp_roles;
    if (!isset($wp_roles)) {
        $wp_roles = new WP_Roles();
    }
    $roles = $wp_roles->get_names();
    $saved_capabilities = frontier_post_get_capabilities();
    $tmp_role_option_list = array_keys($fp_role_option_list);
    // loop through each role
    foreach ($roles as $key => $item) {
        echo '<tr><td>' . $item . '</td>';
        if (!array_key_exists($key, $saved_capabilities)) {
            $saved_capabilities[$key] = array();
        }
        $tmp_role_options = $saved_capabilities[$key];
        if (!is_array($tmp_role_options)) {
            $tmp_role_options = array();
        }
        foreach ($fp_role_option_list as $tmp_role_option => $tmp_role_option_name) {
            $tmp_name = $key . '_' . $tmp_role_option;
            if (array_key_exists($tmp_role_option, $tmp_role_options)) {
                $tmp_value = $saved_capabilities[$key][$tmp_role_option] ? $saved_capabilities[$key][$tmp_role_option] : "false";
            } else {
                $tmp_value = "";
            }
            echo '<td>';
            switch ($tmp_role_option) {
                case 'fps_role_editor_type':
                    $optionlist = array_flip($editor_types);
                    ?>
	
							<select  id="<?php 
                    echo $tmp_name;
                    ?>
" name="<?php 
                    echo $tmp_name;
                    ?>
" >
							<?php 
                    foreach ($optionlist as $id => $desc) {
                        ?>
   
								<option value='<?php 
                        echo $id;
                        ?>
' <?php 
                        echo $id == $tmp_value ? "selected='selected'" : ' ';
                        ?>
>
									<?php 
                        echo $desc;
                        ?>
								</option>
							<?php 
                    }
                    ?>
							</select>
							<?php 
                    break;
                case 'fps_role_category_layout':
                    $optionlist = array_flip($category_types);
                    ?>
	
							<select  id="<?php 
                    echo $tmp_name;
                    ?>
" name="<?php 
                    echo $tmp_name;
                    ?>
" >
							<?php 
                    foreach ($optionlist as $id => $desc) {
                        ?>
   
								<option value='<?php 
                        echo $id;
                        ?>
' <?php 
                        echo $id == $tmp_value ? "selected='selected'" : ' ';
                        ?>
>
									<?php 
                        echo $desc;
                        ?>
								</option>
							<?php 
                    }
                    ?>
							</select>
							<?php 
                    break;
                case 'fps_role_default_category':
                    wp_dropdown_categories(array('id' => $tmp_name, 'hide_empty' => 0, 'name' => $tmp_name, 'orderby' => 'name', 'selected' => $tmp_value, 'hierarchical' => true));
                    break;
                case 'fps_role_allowed_categories':
                    if ($tmp_value == "false") {
                        $tmp_value = "";
                    }
                    echo '<input type="text" name="' . $tmp_name . '" value="' . $tmp_value . '">';
                    break;
            }
            echo '</td>';
        }
        // end option
        echo '</tr>';
    }
    // end roles
    echo '</table>';
    echo '<p class="submit"><input type="submit" name="Submit" class="button-primary" value="' . __('Save Changes') . '"></p>';
    echo '</form>';
    echo '<hr>';
    echo '</div>';
    //frontier-admin-menu
    echo '</div>';
    //wrap
}
function fps_cnv_general_options($suppress_output = false)
{
    // Move values from old single options to new array based options
    include FRONTIER_POST_DIR . "/include/frontier_post_defaults.php";
    $frontier_submit_buttons = get_option("frontier_post_submit_buttons", array('save' => 'true', 'savereturn' => 'true', 'preview' => 'true', 'cancel' => 'true'));
    $cnv_table = array('fps_edit_max_age' => get_option('frontier_post_edit_max_age', 10), 'fps_delete_max_age' => get_option('frontier_post_delete_max_age', 3), 'fps_ppp' => get_option('frontier_post_ppp', 25), 'fps_page_id' => get_option('frontier_post_page_id', 0), 'fps_del_w_comments' => get_option("frontier_post_del_w_comments", "false"), 'fps_edit_w_comments' => get_option("frontier_post_edit_w_comments", "false"), 'fps_author_role' => get_option("frontier_post_author_role", "false"), 'fps_mail_to_approve' => get_option("frontier_post_mail_to_approve", "false"), 'fps_mail_approved' => get_option("frontier_post_mail_approved", "false"), 'fps_mail_address' => get_option("frontier_post_mail_address", ""), 'fps_excl_cats' => get_option("frontier_post_excl_cats", ""), 'fps_show_feat_img' => get_option("frontier_post_show_feat_img", "false"), 'fps_show_login' => get_option("frontier_post_show_login", "false"), 'fps_change_status' => get_option("frontier_post_change_status", "true"), 'fps_catid_list' => get_option("frontier_post_catid_list", ""), 'fps_editor_lines' => get_option('frontier_post_editor_lines', 300), 'fps_default_status' => get_option("frontier_default_status", "publish"), 'fps_hide_status' => get_option("frontier_post_hide_status", "false"), 'fps_show_msg' => get_option("frontier_post_show_msg", "false"), 'fps_hide_title_ids' => get_option("frontier_post_hide_title_ids", ""), 'fps_default_editor' => get_option("frontier_default_editor", "full"), 'fps_default_cat_select' => get_option("frontier_default_cat_select", "multi"), 'fps_external_cap' => get_option("frontier_post_external_cap", "false"), 'fps_submit_save' => $frontier_submit_buttons['save'], 'fps_submit_savereturn' => $frontier_submit_buttons['savereturn'], 'fps_submit_preview' => $frontier_submit_buttons['preview'], 'fps_submit_cancel' => $frontier_submit_buttons['cancel']);
    $fps_save_general_options['fps_frontier_post_version'] = FRONTIER_POST_VERSION;
    update_option(FRONTIER_POST_SETTINGS_OPTION_NAME, $cnv_table);
    //Update default values for settings that doesnt exists.
    fp_post_set_defaults();
    // Rolebased settings
    $old_capabilities = get_option('frontier_post_options', array());
    $wp_roles = new WP_Roles();
    $roles = $wp_roles->get_names();
    $tmp_array = array_merge($fp_capability_list, $fp_role_option_list);
    $tmp_cap_list = array_keys($tmp_array);
    $saved_capabilities = frontier_post_get_capabilities();
    // Loop through the roles
    foreach ($roles as $key => $item) {
        $xrole = get_role($key);
        if (!array_key_exists($key, $saved_capabilities)) {
            $saved_capabilities[$key] = array();
        }
        if (!array_key_exists($key, $old_capabilities)) {
            $old_capabilities[$key] = array();
        }
        // set capabilities
        foreach ($tmp_cap_list as $tmp_cap) {
            $xrole_old_cap = $old_capabilities[$key];
            $xrole_cap = $saved_capabilities[$key];
            $old_cap_name = str_replace('frontier_post_', '', $tmp_cap);
            $def_value = "false";
            if ($tmp_cap == 'fps_role_editor_type') {
                $def_value = "minimal-visual";
                $old_cap_name = 'editor';
            }
            if ($tmp_cap == 'fps_role_category_layout') {
                $def_value = "multi";
                $old_cap_name = 'category';
            }
            if ($tmp_cap == 'fps_role_default_category') {
                $def_value = get_option("default_category");
                $old_cap_name = 'default_category';
            }
            if (array_key_exists($old_cap_name, $xrole_old_cap)) {
                $saved_capabilities[$key][$tmp_cap] = $xrole_old_cap[$old_cap_name];
            } else {
                if (!array_key_exists($tmp_cap, $xrole_cap)) {
                    $saved_capabilities[$key][$tmp_cap] = $def_value;
                }
            }
        }
        //caps
    }
    // roles
    // Save options
    update_option(FRONTIER_POST_CAPABILITY_OPTION_NAME, $saved_capabilities);
    // Set Wordpress capabilities
    frontier_post_set_cap();
    //save to options that capabilities has been migrated
    $fps_general_options = frontier_post_get_settings();
    $fps_general_options['fps_options_migrated'] = "true";
    $fps_general_options['fps_options_migrated_version'] = FRONTIER_POST_VERSION;
    update_option(FRONTIER_POST_SETTINGS_OPTION_NAME, $fps_general_options);
    $fp_last_upgrade = fp_get_option('fps_options_migrated_version', get_option("frontier_post_version", '0.0.0'));
    $fp_upgrade_msg = 'Frontier Post - Settings upgraded from version: ' . $fp_last_upgrade . ' to version: ' . FRONTIER_POST_VERSION;
    if (!$suppress_output) {
        echo '<div class="updated"><p><strong>' . $fp_upgrade_msg . '</strong></p></div>';
    }
    // Finally delete frontier_post_version
    delete_option("frontier_post_version");
}