/**
 * Removes pending users from member listings.
 *
 * @since 4.2.5
 *
 * @param array $r Query args for member list.
 *
 * @return array $r Amended query args.
 */
function bp_registration_hide_widget_members($r = array())
{
    $exclude_me = bp_registration_get_pending_users();
    $excluded = array();
    foreach ($exclude_me as $exclude) {
        $excluded[] = $exclude->user_id;
    }
    # Prevent overwriting of existing exclude values.
    if (empty($r['exclude'])) {
        $r['exclude'] = implode(',', $excluded);
    } else {
        $r['exclude'] .= ',' . implode(',', $excluded);
    }
    return $r;
}
/**
 * Options page for managing pending members.
 *
 * @since  unknown
 *
 * @return string  HTML page output
 */
function bp_registration_options_member_requests()
{
    /**/
    ?>

	<div class="wrap">
		<?php 
    bp_registration_options_tab_menu('requests');
    $member_requests = bp_registration_get_pending_user_count();
    if ($member_requests > 0) {
        ?>

			<form method="POST" name="bprwg">
			<?php 
        do_action('bpro_hook_before_pending_member_list');
        wp_nonce_field('bp_reg_options_check');
        ?>

			<p><?php 
        _e('Please approve or deny the following new members:', 'bp-registration-options');
        ?>
</p>

			<table class="widefat">
			<thead>
				<tr>
					<th id="cb" class="manage-column column-cb check-column" scope="col">
						<input type="checkbox" id="bp_checkall_top" name="checkall" />
					</th>
					<th><?php 
        _e('Photo', 'bp-registration-options');
        ?>
</th>
					<th><?php 
        _e('Name', 'bp-registration-options');
        ?>
</th>
					<th><?php 
        _e('Email', 'bp-registration-options');
        ?>
</th>
					<th><?php 
        _e('Created', 'bp-registration-options');
        ?>
</th>
					<th><?php 
        _e('Additional Data', 'bp-registration-options');
        ?>
</th>
				</tr>
			</thead>
			<?php 
        $odd = true;
        //Get paged value, determine total pages, and calculate start_from value for offset.
        $page = isset($_GET['p']) ? $_GET['p'] : 1;
        $total_pages = ceil($member_requests / 20);
        //TODO: Test pagination
        $start_from = ($page - 1) * 20;
        $pending_users = bp_registration_get_pending_users($start_from);
        foreach ($pending_users as $pending) {
            if (class_exists('BP_Core_User')) {
                $user = new BP_Core_User($pending->user_id);
            }
            $user_data = get_userdata($pending->user_id);
            $userip = trim(get_user_meta($pending->user_id, '_bprwg_ip_address', true));
            if ($odd) {
                ?>
					<tr class="alternate">
					<?php 
                $odd = false;
            } else {
                ?>
					<tr>
					<?php 
                $odd = true;
            }
            ?>
					<th class="check-column" scope="row">
						<input type="checkbox" class="bpro_checkbox" id="bp_member_check_<?php 
            echo $pending->user_id;
            ?>
" name="bp_member_check[]" value="<?php 
            echo $pending->user_id;
            ?>
"  />
					</th>
					<td>
						<?php 
            if (isset($user)) {
                ?>
							<a target="_blank" href="<?php 
                echo $user->user_url;
                ?>
">
								<?php 
                echo $user->avatar_mini;
                ?>
							</a>
						<?php 
            }
            ?>
					</td>
					<td>
						<?php 
            if (isset($user)) {
                ?>
							<strong><a target="_blank" href="<?php 
                echo $user->user_url;
                ?>
">
								<?php 
                echo $user->fullname;
                ?>
							</a></strong>
						<?php 
            } else {
                echo $user_data->user_login;
            }
            ?>
					</td>
					<td>
						<a href="mailto:<?php 
            echo $user_data->data->user_email;
            ?>
">
							<?php 
            echo $user_data->data->user_email;
            ?>
						</a>
					</td>
					<td>
						<?php 
            echo $user_data->data->user_registered;
            ?>
					</td>
					<td>
						<div class="alignleft">
							<img height="50" src="http://api.hostip.info/flag.php?ip=<?php 
            echo $userip;
            ?>
" / >
						</div>
						<div class="alignright">
							<?php 
            $response = wp_remote_get('http://api.hostip.info/get_html.php?ip=' . $userip);
            if (!is_wp_error($response)) {
                $data = $response['body'];
                $data = str_replace('City:', '<br>' . __('City:', 'bp-registration-options'), $data);
                $data = str_replace('IP:', '<br>' . __('IP:', 'bp-registration-options'), $data);
                echo $data;
            } else {
                echo $userip;
            }
            ?>
						</div>
					</td>
				</tr>
			<?php 
        }
        ?>
			<tfoot>
				<tr>
					<th class="manage-column column-cb check-column" scope="col"><input type="checkbox" id="bp_checkall_bottom" name="checkall" /></th>
					<th><?php 
        _e('Photo', 'bp-registration-options');
        ?>
</th>
					<th><?php 
        _e('Name', 'bp-registration-options');
        ?>
</th>
					<th><?php 
        _e('Email', 'bp-registration-options');
        ?>
</th>
					<th><?php 
        _e('Created', 'bp-registration-options');
        ?>
</th>
					<th><?php 
        _e('Additional Data', 'bp-registration-options');
        ?>
</th>
				</tr>
			</tfoot>
			</table>

			<p><input type="submit" class="button button-primary" name="moderate" value="<?php 
        esc_attr_e('Approve', 'bp-registration-options');
        ?>
" id="bpro_approve" />
			<input type="submit" class="button button-secondary" name="moderate" value="<?php 
        esc_attr_e('Deny', 'bp-registration-options');
        ?>
" id="bpro_deny" />
			<input type="submit" class="button button-secondary" name="moderate" disabled value="<?php 
        esc_attr_e('Ban', 'bp-registration-options');
        ?>
" id="bpro_ban" /></p>

			<?php 
        if ($total_pages > 1) {
            $current = !empty($_GET['p']) ? $_GET['p'] : 1;
            echo '<p>';
            for ($i = 1; $i <= $total_pages; $i++) {
                if ($i == $current) {
                    printf('<a class="bpro_current wp-ui-highlight" href="%s">%s</a>', esc_url(add_query_arg('p', $i)), $i);
                } else {
                    printf('<a href="%s">%s</a>', esc_url(add_query_arg('p', $i)), $i);
                }
            }
            echo '</p>';
        }
        do_action('bpro_hook_after_pending_member_list');
        ?>

			</form>

		<?php 
    } else {
        echo '<p><strong>' . __('No new members to approve.', 'bp-registration-options') . '</strong></p>';
    }
    ?>
	</div> <!--End Wrap-->

	<?php 
    bp_registration_options_admin_footer();
}