function ameta_default_list_options()
{
    // default lists  $aopt
    /* setup some list defaults */
    ameta_cache_enable();
    //in case cache tables got deleted
    ameta_cachelogging_enable();
    if (amr_is_network_admin()) {
        $default = array('list' => array('1' => array('selected' => array('user_login' => 2, 'user_email' => 3, 'user_registered' => 5, 'blogcount_as_subscriber' => 10, 'blogcount_as_administrator' => 15, 'bloglist_as_subscriber' => 20, 'bloglist_as_administrator' => 25, 'bloglist' => 100), 'sortdir' => array('user_registered' => 'SORT_DESC'), 'sortby' => array('user_registered' => '1'), 'before' => array('last_name' => '<br />'), 'links' => array('user_email' => 'mailto', 'user_login' => 'edituser', 'bloglist' => 'wplist'))));
    } else {
        $sortdir = array('user_registered' => 'SORT_DESC');
        $default = array('list' => array('1' => array('selected' => array('avatar' => 10, 'user_login' => 20, 'user_email' => 30, 'display_name' => 40, 'user_registered' => 50, 'first_role' => 60), 'sortdir' => array('user_registered' => 'SORT_DESC'), 'sortby' => array('user_email' => '1'), 'links' => array('user_email' => 'mailto', 'user_login' => 'edituser', 'user_url' => 'url', 'avatar' => 'authorarchive'), 'excluded' => array('ID' => '1', 'first_role' => 'Administrator')), '2' => array('selected' => array('avatar' => 10, 'display_name' => 20, 'user_url' => 30, 'user_registered' => 40), 'excluded' => array('ID' => '1'), 'sortby' => array('user_registered' => '2'), 'links' => array('avatar' => 'url', 'display_name' => 'authorarchive', 'url' => 'url'))));
    }
    ausers_update_option('amr-users', $default);
    return $default;
}
function amr_meta_overview_page()
{
    /* the main setting spage  - num of lists and names of lists */
    global $amain;
    global $aopt;
    if (empty($amain)) {
        $amain = ausers_get_option('amr-users-main');
    }
    //amr_meta_main_admin_header('Overview of configured user lists'.' '.AUSERS_VERSION);
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check etc
    if (isset($_POST['import-list'])) {
        amr_meta_handle_import();
    } elseif (isset($_POST['action']) and $_POST['action'] == "save") {
        if (!empty($_POST['reset'])) {
            amr_meta_reset();
            return;
        } elseif (isset($_POST['export-list'])) {
            amr_meta_handle_export();
        } else {
            amrmeta_validate_overview();
        }
    } else {
        amr_handle_copy_delete();
    }
    if (!ameta_cache_enable() or !ameta_cachelogging_enable()) {
        echo '<h2>' . __('Problem creating DB tables', 'amr-users') . '</h2>';
    }
    if (!isset($amain['checkedpublic'])) {
        echo '<input type="hidden" name="checkedpublic" value="true"/>';
    }
    echo '<h2>' . __('Overview &amp; tools', 'amr-users') . '</h2>';
    echo PHP_EOL . '<div class="wrap"><!-- one wrap -->' . PHP_EOL;
    if (!isset($amain['names'])) {
        echo '<h2>' . __('There is a problem - Some overview list settings got lost somehow.  Try reset options.', 'amr-users') . '</h2>';
    } else {
        amr_meta_overview_onelist_headings();
        amr_meta_overview_onelist_headings_middle();
        foreach ($amain['names'] as $i => $name) {
            //for ($i = 1; $i <= $amain['no-lists']; $i++)	{
            amr_meta_overview_onelist_settings($i);
            echo '</tr>';
        }
        amr_meta_overview_onelist_headings_end();
    }
    echo '</div><!-- end of one wrap --> <br />' . PHP_EOL;
    //echo '<div style="clear: both; float:right; padding-right:100px;" class="submit">';
    echo ausers_submit();
    echo '<input class="button-primary" type="submit" name="addnew" value="' . __('Add new', 'amr-users') . '" />';
    amr_list_export_form();
    echo ausers_form_end();
    amr_list_import_form();
    // different form
}
function amrmeta_cache_settings_page()
{
    global $aopt;
    global $amr_nicenames;
    global $pluginpage;
    global $amain;
    if (empty($amain)) {
        $amain = ausers_get_option('amr-users-main');
    }
    $tabs['settings'] = __('Cache Settings', 'amr-users');
    $tabs['logs'] = __('Cache Logs', 'amr-users');
    $tabs['status'] = __('Cache Status', 'amr-users');
    if (isset($_GET['tab'])) {
        if ($_GET['tab'] == 'logs') {
            amr_users_do_tabs($tabs, 'logs');
            amrmeta_cache_logs_page();
            return;
        } elseif ($_GET['tab'] == 'status') {
            amr_users_do_tabs($tabs, 'status');
            amrmeta_cachestatus_page();
            return;
        }
    }
    amr_users_do_tabs($tabs, 'settings');
    //amr_meta_main_admin_header('Cache Settings');
    amr_meta_admin_headings($plugin_page = '');
    // does the nonce check etc
    if (!ameta_cache_enable() or !ameta_cachelogging_enable()) {
        echo '<h2>Problem creating DB tables</h2>';
    }
    if (isset($_POST['action']) and $_POST['action'] == "save") {
        amrmeta_validate_cache_settings();
    }
    if (isset($_REQUEST['rebuildback'])) {
        echo '<p>' . __('Background cache request received', 'amr-users') . '</p>';
        if (isset($_REQUEST['rebuildreal'])) {
            $ulist = (int) $_REQUEST['rebuildreal'];
            amr_request_cache_with_feedback($ulist);
        } else {
            amr_request_cache_with_feedback();
        }
        return;
    } elseif (isset($_REQUEST['rebuildreal'])) {
        /* can only do one list at a time in realtime */
        $ulist = (int) $_REQUEST['rebuildreal'];
        amr_rebuild_in_realtime_with_info($ulist);
        //echo amr_build_cache_for_one($_REQUEST['rebuildreal']);
        //echo '<h2>'.sprintf(__('Cache rebuilt for %s ','amr-users'),$_REQUEST['rebuildreal']).'</h2>'; /* check that allowed */
        //echo au_view_link(__('View Report','amr-users'), $_REQUEST['rebuildreal'], __('View the recently cached report','amr-users'));
        return;
    } else {
        /* validation will have been done */
        $freq = array('notauto' => __('No scheduled auto cacheing', 'amr-users'), 'hourly' => __('Hourly', 'amr-users'), 'twicedaily' => __('Twice daily', 'amr-users'), 'daily' => __('Daily', 'amr-users'));
        if (!isset($amain['cache_frequency'])) {
            $freqchosen = 'notauto';
        } else {
            $freqchosen = $amain['cache_frequency'];
        }
        echo '<h3>';
        _e('Activate regular cache rebuild ? ', 'amr-users');
        echo '</h3><span><em>';
        echo '<p>';
        _e('This cacheing grabs all the raw data it can find and does some preprocessing. ', 'amr-users');
        echo '<br />';
        _e('The data is stored in a flat db table for later formatting and reporting. ', 'amr-users');
        echo '</p>';
        echo '<p>';
        _e('The cache log will tell you the last few times that the cache was rebuilt and why. ', 'amr-users');
        echo '<a href="' . admin_url('admin.php?page=ameta-admin-cache-settings.php&tab=logs') . '">' . __('Go to cache log', 'amr-users') . '</a>';
        echo '<br />';
        _e('A cron plugin may also be useful.', 'amr-users');
        echo ' <a href="http://wpusersplugin.com/related-plugins/amr-cron-manager/">amr cron manager</a>';
        echo '</p>';
        echo '<p><a target="_blank" href="http://wpusersplugin.com/3458/cacheing-amr-users/">' . __('More information', 'amr-users') . '</a></p>';
        echo '</em>	</span>	<p>';
        /*		echo '<label for="notonuserupdate">
        			<input type="checkbox" size="2" id="notonuserupdate" 
        				name="notonuserupdate" ';
        		echo (empty($amain['notonuserupdate'])) ? '' :' checked="checked" '; 
        		echo '/>';
        		_e('Do NOT re-cache on user update', 'amr-users'); 
        		echo '</label>';
        */
        echo '<ul><li>';
        echo '<label for="notonuserupdate">
			<input type="radio" size="2" id="notonuserupdate" 
				name="notonuserupdate" value="true"';
        echo empty($amain['notonuserupdate']) ? '' : ' checked="checked" ';
        echo '/>';
        _e('Do NOT re-cache on user update', 'amr-users');
        echo '</label>';
        echo '</li><li>';
        //echo '<br />';
        echo '<label for="doonuserupdate">
			<input type="radio" size="2" id="doonuserupdate" 
				name="notonuserupdate" value="false"';
        echo $amain['notonuserupdate'] ? '' : ' checked="checked" ';
        echo '/>';
        _e('Do re-cache on user update', 'amr-users');
        echo '</label>';
        echo '</li></ul>';
        echo '</p><br />';
        echo '<br />';
        echo '<p><em><b>';
        _e('If you have very frequent user updates consider only cacheing at regular intervals', 'amr-users');
        echo '</b> ';
        _e('This will help prevent excessive database activity', 'amr-users');
        echo '<br />';
        _e('EG: Are you tracking every page ? every login.. you do not want it recaching all the time?!', 'amr-users');
        _e('Rather cache hourly only.  A refresh can be requested.', 'amr-users');
        echo '<br />';
        _e('Wordpress transients are also used to cache the html in public lists and front end', 'amr-users');
        echo '</em></p>';
        echo '<p><em><b>';
        _e('To switch off all auto cacheing, select "Do not.." above AND "No..." below.', 'amr-users');
        echo '</b><br />';
        _e('Lists will then be re-generated on manual refresh request only.', 'amr-users');
        echo '</em></p>';
        foreach ($freq as $i => $f) {
            echo '<label><input type="radio" name="cache_frequency" value="' . $i . '" ';
            if ($i == $freqchosen) {
                echo ' checked="checked" ';
            }
            echo '/>';
            echo $f;
            echo '</label><br />';
        }
        echo alist_update();
        echo alist_rebuild();
    }
    echo ausers_form_end();
}
function amr_check_for_upgrades()
{
    // NB must be in order of the oldest changes first // called from ausers_get_option
    // should already have values then - and will not be new ?
    global $amain, $aopt;
    if (empty($amain)) {
        $amain = ausers_get_option('amr-users-main');
    }
    //if (WP_DEBUG) echo '<div class="message">Debug mode: check doing upgrade check </div>';
    // must be in admin and be admin
    if (!is_admin() or !current_user_can('manage_options')) {
        return;
    }
    // handle a series of updates in order
    if (!isset($amain['version'])) {
        $amain['version'] = '0';
    }
    // really old?
    if (version_compare($amain['version'], AUSERS_VERSION, '=')) {
        return;
    }
    // if same version, don't repeat check
    $prev = $amain['version'];
    echo PHP_EOL . '<div class="updated"><p>';
    // closing div at end
    printf(__('Previous version was %s. ', 'amr-users'), $prev);
    _e('New version activated. ', 'amr-users');
    _e('We may need to process some updates.... checking now... ', 'amr-users');
    // do old changes first - user may not have updated for a while....
    if (!isset($amain['version']) or version_compare($amain['version'], '3.1', '<')) {
        // convert old options from before 3.1
        echo '<br />';
        printf(__('Prev version less than %s', 'amr-users'), '3.1.');
        if (!isset($amain['csv_text'])) {
            $amain['csv_text'] = '<img src="' . plugins_url('amr-users/images/file_export.png') . '" alt="' . __('Csv', 'amr-users') . '"/>';
        }
        if (!isset($amain['refresh_text'])) {
            $amain['refresh_text'] = '<img src="' . plugins_url('amr-users/images/rebuild.png') . '" alt="' . __('Refresh user list cache', 'amr-users') . '"/>';
        }
        ausers_update_option('amr-users-main', $amain);
        echo '<br />' . __('Image links updated.', 'amr-users');
        echo '</p>';
    }
    //
    if (!isset($amain['version']) or version_compare($amain['version'], '3.3.1', '<')) {
        // check for before 3.3.1
        echo '<br />';
        printf(__('Prev version less than %s', 'amr-users'), '3.3.1.');
        $c = new adb_cache();
        $c->deactivate();
        if (!ameta_cache_enable() or !ameta_cachelogging_enable()) {
            echo '<h2>' . __('Problem creating amr user DB tables', 'amr-users') . '</h2>';
        }
        echo '<br />';
        _e('Cacheing tables recreated.', 'amr-users');
    }
    //
    if (!isset($amain['version']) or version_compare($amain['version'], '3.3.6', '<')) {
        // check for before 3.3.6,
        echo '<br />';
        printf(__('Prev version less than %s', 'amr-users'), '3.3.6. ');
        echo '</p>' . __('Minor sub option name change for avatar size', 'amr-users') . '</p>';
        if (!empty($amain['avatar-size'])) {
            $amain['avatar_size'] = $amain['avatar-size'];
        } else {
            $amain['avatar_size'] = '16';
        }
        unset($amain['avatar-size']);
        ausers_update_option('amr-users-main', $amain);
    }
    // 3.4.4  July 2012
    if (!isset($amain['version']) or version_compare($amain['version'], '3.4.4', '<')) {
        // check for before 3.3.,
        echo '<br />';
        printf(__('Prev version less than %s', 'amr-users'), '3.4.4 ');
        echo '<p><b>' . __('New Pagination option default to yes for all lists.', 'amr-users') . '</b></p>';
        if (!isset($amain['show_pagination'])) {
            foreach ($amain['names'] as $i => $n) {
                $amain['show_pagination'][$i] = true;
            }
        }
    }
    $amain['version'] = AUSERS_VERSION;
    ausers_update_option('amr-users-main', $amain);
    // was 'amr-users-no-lists'
    echo '<p>' . __('Finished Update Checks', 'amr-users') . ' ';
    echo ' <a href="http://wordpress.org/extend/plugins/amr-users/changelog/">' . __('Please read the changelog', 'amr-users') . '</a>';
    echo '</p>' . PHP_EOL;
    echo '<br />' . __('As a precaution we will now rebuild the nice names.', 'amr-users');
    echo '<br />' . __('Relax .... you won\'t lose anything.', 'amr-users');
    ameta_rebuildnicenames();
    echo '</div><!-- end updated -->' . PHP_EOL;
}
function amr_meta_main_admin_header($title)
{
    echo PHP_EOL . '<div id="icon-users" class="icon32"><br/></div>' . PHP_EOL;
    echo PHP_EOL . '<h2>' . $title . '</h2>' . PHP_EOL;
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to update list settings.', 'amr-users'));
    }
    if (!ameta_cache_enable() or !ameta_cachelogging_enable()) {
        echo '<h2>Problem creating DB tables</h2>';
    }
}