/**
 Our profile menu
 */
 function user_menu()
 {
     global $user_ID, $s2nonce;
     if (isset($_GET['email'])) {
         global $wpdb;
         $user_ID = $wpdb->get_var("SELECT ID FROM {$wpdb->users} WHERE user_email = '" . $_GET['email'] . "'");
     } else {
         get_currentuserinfo();
     }
     // was anything POSTed?
     if (isset($_POST['s2_admin']) && 'user' == $_POST['s2_admin']) {
         check_admin_referer('subscribe2-user_subscribers' . $s2nonce);
         echo "<div id=\"message\" class=\"updated fade\"><p><strong>" . __('Subscription preferences updated.', 'subscribe2') . "</strong></p></div>\n";
         $format = 'text';
         $post = 'post';
         if ('html' == $_POST['s2_format']) {
             $format = 'html';
         }
         if ('excerpt' == $_POST['s2_excerpt']) {
             $post = 'excerpt';
         }
         update_usermeta($user_ID, 's2_excerpt', $post);
         update_usermeta($user_ID, 's2_format', $format);
         update_usermeta($user_ID, 's2_autosub', $_POST['new_category']);
         $cats = $_POST['category'];
         if (empty($cats) || $cats == '-1') {
             $oldcats = explode(',', get_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed')));
             if ($oldcats) {
                 foreach ($oldcats as $cat) {
                     delete_usermeta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat);
                 }
             }
             update_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), '');
         } elseif ($cats == 'digest') {
             $all_cats = get_categories(array('hide_empty' => false));
             foreach ($all_cats as $cat) {
                 '' == $catids ? $catids = "{$cat->term_id}" : ($catids .= ",{$cat->term_id}");
                 update_usermeta($user_ID, $this->get_usermeta_keyname('s2_cat') . $cat->term_id, $cat->term_id);
             }
             update_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), $catids);
         } else {
             if (!is_array($cats)) {
                 $cats = array($_POST['category']);
             }
             $old_cats = explode(',', get_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed')));
             $remove = array_diff($old_cats, $cats);
             $new = array_diff($cats, $old_cats);
             if (!empty($remove)) {
                 // remove subscription to these cat IDs
                 foreach ($remove as $id) {
                     delete_usermeta($user_ID, $this->get_usermeta_keyname('s2_cat') . $id);
                 }
             }
             if (!empty($new)) {
                 // add subscription to these cat IDs
                 foreach ($new as $id) {
                     update_usermeta($user_ID, $this->get_usermeta_keyname('s2_cat') . $id, $id);
                 }
             }
             update_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed'), implode(',', $cats));
         }
     }
     // show our form
     echo "<div class=\"wrap\">";
     echo "<h2>" . __('Notification Settings', 'subscribe2') . "</h2>\r\n";
     if (isset($_GET['email'])) {
         $user = get_userdata($user_ID);
         echo "<span style=\"color: red;line-height: 300%;\">" . __('Editing Subscribe2 preferences for user', 'subscribe2') . ": " . $user->display_name . "</span>";
     }
     echo "<form method=\"post\" action=\"\">";
     echo "<p>";
     if (function_exists('wp_nonce_field')) {
         wp_nonce_field('subscribe2-user_subscribers' . $s2nonce);
     }
     echo "<input type=\"hidden\" name=\"s2_admin\" value=\"user\" />";
     if ($this->subscribe2_options['email_freq'] == 'never') {
         echo __('Receive email as', 'subscribe2') . ": &nbsp;&nbsp;";
         echo "<label><input type=\"radio\" name=\"s2_format\" value=\"html\"";
         if ('html' == get_usermeta($user_ID, 's2_format')) {
             echo "checked=\"checked\" ";
         }
         echo "/> " . __('HTML', 'subscribe2') . "</label>&nbsp;&nbsp;";
         echo "<label><input type=\"radio\" name=\"s2_format\" value=\"text\" ";
         if ('text' == get_usermeta($user_ID, 's2_format')) {
             echo "checked=\"checked\" ";
         }
         echo "/> " . __('Plain Text', 'subscribe2') . "</label><br /><br />\r\n";
         echo __('Email contains', 'subscribe2') . ": &nbsp;&nbsp;";
         $amount = array('excerpt' => __('Excerpt Only', 'subscribe2'), 'post' => __('Full Post', 'subscribe2'));
         foreach ($amount as $key => $value) {
             echo "<label><input type=\"radio\" name=\"s2_excerpt\" value=\"" . $key . "\"";
             if ($key == get_usermeta($user_ID, 's2_excerpt')) {
                 echo " checked=\"checked\"";
             }
             echo " /> " . $value . "</label>&nbsp;&nbsp;";
         }
         echo "<br /><span style=\"color: red;line-height: 300%;\">" . __('Note: HTML format will always deliver the full post', 'subscribe2') . ".</span><br />\r\n";
         if ($this->subscribe2_options['show_autosub'] == 'yes') {
             echo __('Automatically subscribe me to newly created categories', 'subscribe2') . ': &nbsp;&nbsp;';
             echo "<label><input type=\"radio\" name=\"new_category\" value=\"yes\" ";
             if ('yes' == get_usermeta($user_ID, 's2_autosub')) {
                 echo "checked=\"checked\" ";
             }
             echo "/> " . __('Yes', 'subscribe2') . "</label>&nbsp;&nbsp;";
             echo "<label><input type=\"radio\" name=\"new_category\" value=\"no\" ";
             if ('no' == get_usermeta($user_ID, 's2_autosub')) {
                 echo "checked=\"checked\" ";
             }
             echo "/> " . __('No', 'subscribe2') . "</label><br /><br />";
             echo "</p>";
         }
         // subscribed categories
         if ($this->s2_mu) {
             global $blog_id;
             $subscribed = get_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed'));
             // if we are subscribed to the current blog display an "unsubscribe" link
             if (!empty($subscribed)) {
                 $unsubscribe_link = get_bloginfo('url') . "/wp-admin/?s2mu_unsubscribe=" . $blog_id;
                 echo "<p><a href=\"" . $unsubscribe_link . "\" class=\"button\">" . __('Unsubscribe me from this blog', 'subscribe2') . "</a></p>";
             } else {
                 // else we show a "subscribe" link
                 $subscribe_link = get_bloginfo('url') . "/wp-admin/?s2mu_subscribe=" . $blog_id;
                 echo "<p><a href=\"" . $subscribe_link . "\" class=\"button\">" . __('Subscribe to all categories', 'subscribe2') . "</a></p>";
             }
             echo "<h2>" . __('Subscribed Categories on', 'subscribe2') . " " . get_bloginfo('name') . " </h2>\r\n";
         } else {
             echo "<h2>" . __('Subscribed Categories', 'subscribe2') . "</h2>\r\n";
         }
         $this->display_category_form(explode(',', get_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed'))), $this->subscribe2_options['reg_override']);
     } else {
         // we're doing daily digests, so just show
         // subscribe / unnsubscribe
         echo __('Receive periodic summaries of new posts?', 'subscribe2') . ': &nbsp;&nbsp;';
         echo "<p><label>";
         echo "<input type=\"radio\" name=\"category\" value=\"digest\" ";
         if (get_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed'))) {
             echo "checked=\"yes\" ";
         }
         echo "/> " . __('Yes', 'subscribe2') . "</label> <label><input type=\"radio\" name=\"category\" value=\"-1\" ";
         if (!get_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed'))) {
             echo "checked=\"yes\" ";
         }
         echo "/> " . __('No', 'subscribe2');
         echo "</label></p>";
     }
     // submit
     echo "<p class=\"submit\"><input type=\"submit\" class=\"button-primary\" name=\"submit\" value=\"" . __("Update Preferences", 'subscribe2') . " &raquo;\" /></p>";
     echo "</form>\r\n";
     // list of subscribed blogs on wordpress mu
     if ($this->s2_mu) {
         global $blog_id;
         $blogs = get_blog_list(0, 'all');
         $blogs_subscribed = array();
         $blogs_notsubscribed = array();
         foreach ($blogs as $key => $blog) {
             // switch to blog
             switch_to_blog($blog['blog_id']);
             // check that the plugin is active on the current blog
             $current_plugins = get_option('active_plugins');
             if (!is_array($current_plugins)) {
                 $current_plugins = (array) $current_plugins;
             }
             if (!in_array('subscribe2/subscribe2.php', $current_plugins)) {
                 continue;
             }
             // check if we're subscribed to the blog
             $subscribed = get_usermeta($user_ID, $this->get_usermeta_keyname('s2_subscribed'));
             $blogname = get_bloginfo('name');
             if (strlen($blogname) > 30) {
                 $blog['blogname'] = wp_html_excerpt($blogname, 30) . "..";
             } else {
                 $blog['blogname'] = $blogname;
             }
             $blog['description'] = get_bloginfo('description');
             if (defined('AUTHOR_AVATARS_VERSION')) {
                 if (!class_exists('UserList')) {
                     include_once ABSPATH . 'wp-content/plugins/author-avatars/lib/UserList.class.php';
                 }
                 $userlist = new UserList();
                 $userlist->roles = array('Administrator', 'Editor', 'Subscriber');
                 $userlist->blogs = array($blog['blog_id']);
                 $userlist->avatar_size = 30;
                 $userlist->use_list_template();
                 $blog['users'] = $userlist->get_output();
             }
             $blog['blogurl'] = get_bloginfo('url');
             $blog['subscribe_page'] = get_bloginfo('url') . "/wp-admin/users.php?page=subscribe2/subscribe2.php";
             $key = strtolower($blog['blogname'] . "-" . $blog['blog_id']);
             if (!empty($subscribed)) {
                 $blogs_subscribed[$key] = $blog;
             } else {
                 $blogs_notsubscribed[$key] = $blog;
             }
             restore_current_blog();
         }
         if (!empty($blogs_subscribed)) {
             ksort($blogs_subscribed);
             echo '<h2>' . __('Subscribed Blogs', 'subscribe2') . '</h2>' . "\r\n";
             echo "<ul class=\"s2_blogs\">\r\n";
             foreach ($blogs_subscribed as $blog) {
                 echo "<li><span class=\"name\"><a href=\"" . $blog['blogurl'] . "\" title=\"" . $blog['description'] . "\">" . $blog['blogname'] . "</a></span>\r\n";
                 if ($blog_id == $blog['blog_id']) {
                     echo "<span class=\"buttons\">" . __('Viewing Settings Now', 'subscribe2') . "</span>\r\n";
                 } else {
                     echo "<span class=\"buttons\">";
                     if (is_blog_user($blog['blog_id'])) {
                         echo "<a href=\"" . $blog['subscribe_page'] . "\">" . __('View Settings', 'subscribe2') . "</a>\r\n";
                     }
                     echo "<a href=\"" . $blog['blogurl'] . "/wp-admin/?s2mu_unsubscribe=" . $blog['blog_id'] . "\">" . __('Unsubscribe', 'subscribe2') . "</a></span>\r\n";
                 }
                 echo "<div class=\"additional_info\"><span class=\"description\">" . $blog['description'] . "</span>" . $blog['users'] . "</div>\r\n";
                 echo "</li>";
             }
             echo "</ul>\r\n";
         }
         if (!empty($blogs_notsubscribed)) {
             ksort($blogs_notsubscribed);
             echo "<h2>" . __('Subscribe to new blogs', 'subscribe2') . "</h2>\r\n";
             echo "<ul class=\"s2_blogs\">";
             foreach ($blogs_notsubscribed as $blog) {
                 echo "<li><span class=\"name\"><a href=\"" . $blog['blogurl'] . "\" title=\"" . $blog['description'] . "\">" . $blog['blogname'] . "</a></span>\r\n";
                 if ($blog_id == $blog['blog_id']) {
                     echo "<span class=\"buttons\">" . __('Viewing Settings Now', 'subscribe2') . "</span>\r\n";
                 } else {
                     echo "<span class=\"buttons\">";
                     if (is_blog_user($blog['blog_id'])) {
                         echo "<a href=\"" . $blog['subscribe_page'] . "\">" . __('View Settings', 'subscribe2') . "</a>\r\n";
                     }
                     echo "<a href=\"" . $blog['blogurl'] . "/wp-admin/?s2mu_subscribe=" . $blog['blog_id'] . "\">" . __('Subscribe', 'subscribe2') . "</a></span>\r\n";
                 }
                 echo "<div class=\"additional_info\"><span class=\"description\">" . $blog['description'] . "</span>" . $blog['users'] . "</div>\r\n";
                 echo "</li>";
             }
             echo "</ul>\r\n";
         }
     }
     echo "</div>\r\n";
     include ABSPATH . 'wp-admin/admin-footer.php';
     // just to be sure
     die;
 }