/**
 * Sanitize supplied field value(s) depending on it's data type
 *
 * @param $field - The data to santitize
 *
 * @return array|int|string
 *
 * @since .5
 */
function pmpromd_sanitize($field)
{
    if (!is_numeric($field)) {
        if (is_array($field)) {
            foreach ($field as $key => $val) {
                $field[$key] = pmpromd_sanitize($val);
            }
        }
        if (is_object($field)) {
            foreach ($field as $key => $val) {
                $field->{$key} = pmpromd_sanitize($val);
            }
        }
        if (!is_array($field) && ctype_alpha($field) || !is_array($field) && strtotime($field) || !is_array($field) && is_string($field)) {
            $field = sanitize_text_field($field);
        }
    } else {
        if (is_float($field + 1)) {
            $field = sanitize_text_field($field);
        }
        if (is_int($field + 1)) {
            $field = intval($field);
        }
    }
    return $field;
}
function pmpromd_shortcode($atts, $content = null, $code = "")
{
    // $atts    ::= array of attributes
    // $content ::= text within enclosing form of shortcode element
    // $code    ::= the shortcode found, when == callback name
    // examples: [pmpro_member_directory show_avatar="false" show_email="false" levels="1,2" search_rh_fields="yes" search_fields="" statuses="active,expired"]
    /*
     * Init variables (to avoid warnings/notices)
     */
    $avatar_size = '128';
    $fields = null;
    $layout = 'div';
    $level = null;
    $levels = null;
    $limit = null;
    $link = null;
    $order_by = 'u.display_name';
    $order = 'ASC';
    $show_avatar = null;
    $show_email = null;
    $show_level = null;
    $show_search = null;
    $show_startdate = null;
    $limit_to = null;
    $search_rh_fields = false;
    $search_fields = '';
    $statuses = 'active';
    extract(shortcode_atts(array('avatar_size' => '128', 'fields' => null, 'layout' => 'div', 'level' => null, 'levels' => null, 'limit' => null, 'link' => null, 'order_by' => 'u.display_name', 'order' => 'ASC', 'show_avatar' => null, 'show_email' => null, 'show_level' => null, 'show_search' => null, 'show_startdate' => null, 'limit_to' => null, 'search_rh_fields' => false, 'search_fields' => null, 'statuses' => 'active'), $atts));
    global $wpdb, $post, $pmpro_pages, $pmprorh_registration_fields;
    //some page vars
    if (!empty($pmpro_pages['directory'])) {
        $directory_url = get_permalink($pmpro_pages['directory']);
    }
    if (!empty($pmpro_pages['profile'])) {
        $profile_url = get_permalink($pmpro_pages['profile']);
    }
    //turn 0's into false
    if ($link === "0" || $link === "false" || $link === "no" || $link === __("no", "pmpromd")) {
        $link = false;
    } else {
        $link = true;
    }
    //did they use level instead of levels?
    if (empty($levels) && !empty($level)) {
        $levels = $level;
    }
    if ($show_avatar === "0" || $show_avatar === "false" || $show_avatar === "no" || $show_avatar === __("no", "pmpromd")) {
        $show_avatar = false;
    } else {
        $show_avatar = true;
    }
    if ($show_email === "0" || $show_email === "false" || $show_email === "no" || $show_email === __("no", "pmpromd")) {
        $show_email = false;
    } else {
        $show_email = true;
    }
    if ($show_level === "0" || $show_level === "false" || $show_level === "no" || $show_level === __("no", "pmpromd")) {
        $show_level = false;
    } else {
        $show_level = true;
    }
    if ($show_search === "0" || $show_search === "false" || $show_search === "no" || $show_search === __("no", "pmpromd")) {
        $show_search = false;
    } else {
        $show_search = true;
    }
    if ($show_startdate === "0" || $show_startdate === "false" || $show_startdate === "no" || $show_startdate === __("no", "pmpromd")) {
        $show_startdate = false;
    } else {
        $show_startdate = true;
    }
    if ($limit_to === "0" || $limit_to === "false" || $limit_to === "no" || $limit_to === __("no", "pmpromd")) {
        $limit_to = false;
    } else {
        $limit_to = true;
    }
    if ($search_rh_fields === "1" || $search_rh_fields === 'true' || $search_rh_fields === 'yes' || $search_rh_fields === __("yes", "pmpromd")) {
        $search_rh_fields = true;
    } else {
        $search_rh_fields = false;
    }
    ob_start();
    if (isset($_REQUEST['ps'])) {
        $s = pmpromd_sanitize($_REQUEST['ps']);
    } else {
        $s = "";
    }
    if (isset($_REQUEST['pn'])) {
        $pn = pmpromd_sanitize($_REQUEST['pn']);
    } else {
        $pn = 1;
    }
    if (isset($_REQUEST['limit'])) {
        $limit = pmpromd_sanitize($_REQUEST['limit']);
    } elseif (empty($limit)) {
        $limit = 15;
    }
    /*
     * Add support for user defined search fields & tables (array value = usermeta field name)
     * Can be array of field names (usermeta fields)
     */
    $rh_fields = array();
    if (true === $search_rh_fields && !empty($search_fields)) {
        $rh_fields = array_map('trim', explode(',', $search_fields));
    }
    $extra_search_fields = apply_filters('pmpromd_extra_search_fields', $rh_fields);
    if (!empty($extra_search_fields) && !is_array($extra_search_fields)) {
        $extra_search_fields = array($extra_search_fields);
    }
    // process list of extra search fields when filter is set but not included in shortcode
    if (!empty($extra_search_fields) && false === $search_rh_fields) {
        foreach ($extra_search_fields as $field_name) {
            if (isset($_REQUEST[$field_name])) {
                ${$field_name} = pmpromd_sanitize($_REQUEST[$field_name]);
            }
        }
        // search the field name (from shortcode attribute: search_fields="")
    } elseif (!empty($extra_search_fields) && true === $search_rh_fields) {
        foreach ($extra_search_fields as $field_name) {
            // still support using the query_var if present
            if (isset($_REQUEST[$field_name])) {
                ${$field_name} = pmpromd_sanitize($_REQUEST[$field_name]);
            } else {
                ${$field_name} = $s;
            }
        }
    }
    $end = $pn * $limit;
    $start = $end - $limit;
    // handle lists of statuses to include
    if ('active' !== $statuses) {
        $statuses_list = array_map('trim', explode(',', $statuses));
    } else {
        $statuses_list = array($statuses);
    }
    $statuses = apply_filters('pmpromd_membership_statuses', $statuses_list);
    $status_list = esc_sql(implode("', '", $statuses));
    if (!empty($s) || !empty($extra_search_fields)) {
        $sqlQuery = "\r\n\t\tSELECT SQL_CALC_FOUND_ROWS\r\n\t\t\tu.ID,\r\n\t\t\tu.user_login,\r\n\t\t\tu.user_email,\r\n\t\t\tu.user_nicename,\r\n\t\t\tu.display_name,\r\n\t\t\tUNIX_TIMESTAMP(u.user_registered) as joindate,\r\n\t\t\tmu.membership_id, mu.initial_payment,\r\n\t\t\tmu.billing_amount, mu.cycle_period,\r\n\t\t\tmu.cycle_number,\r\n\t\t\tmu.billing_limit,\r\n\t\t\tmu.trial_amount,\r\n\t\t\tmu.trial_limit,\r\n\t\t\tUNIX_TIMESTAMP(mu.startdate) as startdate,\r\n\t\t\tUNIX_TIMESTAMP(mu.enddate) as enddate,\r\n\t\t\tm.name as membership,\r\n\t\t\tumf.meta_value as first_name,\r\n\t\t\tuml.meta_value as last_name\r\n\t\tFROM {$wpdb->users} u\r\n\t\tLEFT JOIN {$wpdb->usermeta} umh ON umh.meta_key = 'pmpromd_hide_directory' AND u.ID = umh.user_id\r\n\t\tLEFT JOIN {$wpdb->usermeta} umf ON umf.meta_key = 'first_name' AND u.ID = umf.user_id\r\n\t\tLEFT JOIN {$wpdb->usermeta} uml ON uml.meta_key = 'last_name' AND u.ID = uml.user_id\r\n\t\tLEFT JOIN {$wpdb->usermeta} um ON u.ID = um.user_id\r\n\t\tLEFT JOIN {$wpdb->pmpro_memberships_users} mu ON u.ID = mu.user_id\r\n\t\tLEFT JOIN {$wpdb->pmpro_membership_levels} m ON mu.membership_id = m.id\r\n\t\t";
        if (!empty($extra_search_fields)) {
            $cnt = 1;
            foreach ($extra_search_fields as $f) {
                if (!empty(${$f})) {
                    $sqlQuery .= "LEFT JOIN {$wpdb->usermeta} umrh_{$cnt} ON umrh_{$cnt}.meta_key = '{$f}' AND u.ID = umrh_{$cnt}.user_id\r\n\t\t\t\t\t";
                }
                ++$cnt;
            }
        }
        $sqlQuery .= " WHERE mu.status IN ('{$status_list}')\r\n\t\t\tAND (umh.meta_value IS NULL\r\n\t\t\t\tOR umh.meta_value <> '1')\r\n\t\t\t\t";
        if (!empty($s)) {
            $sqlQuery .= " AND (u.user_login LIKE '%" . esc_sql($s) . "%'\r\n\t\t\t\tOR u.user_email LIKE '%" . esc_sql($s) . "%'\r\n\t\t\t\tOR u.display_name LIKE '%" . esc_sql($s) . "%'\r\n\t\t\t\tOR um.meta_value LIKE '%" . esc_sql($s) . "%') ";
        }
        // process any additional/extra/RH related search fields
        if (!empty($extra_search_fields)) {
            $cnt = 1;
            foreach ($extra_search_fields as $f) {
                if (is_array(${$f}) && !empty(${$f})) {
                    $sqlQuery .= " AND (";
                    $max_v = count(${$f}) - 1;
                    $i = 0;
                    foreach (${$f} as $v) {
                        $sqlQuery .= " umrh_{$cnt}.meta_value LIKE '%{$v}%' ";
                        if ($max_v > $i) {
                            $sqlQuery .= " OR ";
                            ++$i;
                        }
                    }
                    $sqlQuery .= ")\r\n\t\t\t\t\t";
                } elseif (!empty(${$f})) {
                    $sqlQuery .= " AND (";
                    $sqlQuery .= " umrh_{$cnt}.meta_value LIKE '%{${$f}}%' ";
                    $sqlQuery .= " )\r\n\t\t\t\t\t";
                }
                ++$cnt;
            }
        }
        // allow users to specify a status other than 'active'
        if (count($statuses) == 1 && in_array('active', $statuses)) {
            $sqlQuery .= " AND mu.membership_id > 0";
        } else {
            $sqlQuery .= " AND mu.membership_id >= 0";
        }
        if ($levels) {
            $sqlQuery .= " AND mu.membership_id IN(" . esc_sql($levels) . ") ";
        }
        $sqlQuery .= " GROUP BY u.ID ORDER BY " . esc_sql($order_by) . " " . $order;
    } else {
        $sqlQuery = "\r\n\t\tSELECT SQL_CALC_FOUND_ROWS\r\n\t\t\tDISTINCT u.ID,\r\n\t\t\tu.user_login,\r\n\t\t\tu.user_email,\r\n\t\t\tu.user_nicename,\r\n\t\t\tu.display_name,\r\n\t\t\tUNIX_TIMESTAMP(u.user_registered) as joindate,\r\n\t\t\tmu.membership_id,\r\n\t\t\tmu.initial_payment,\r\n\t\t\tmu.billing_amount,\r\n\t\t\tmu.cycle_period,\r\n\t\t\tmu.cycle_number,\r\n\t\t\tmu.billing_limit,\r\n\t\t\tmu.trial_amount,\r\n\t\t\tmu.trial_limit,\r\n\t\t\tUNIX_TIMESTAMP(mu.startdate) as startdate,\r\n\t\t\tUNIX_TIMESTAMP(mu.enddate) as enddate,\r\n\t\t\tm.name as membership,\r\n\t\t\tumf.meta_value as first_name,\r\n\t\t\tuml.meta_value as last_name\r\n\t\tFROM {$wpdb->users} u\r\n\t\tLEFT JOIN {$wpdb->usermeta} umh ON umh.meta_key = 'pmpromd_hide_directory' AND u.ID = umh.user_id\r\n\t\tLEFT JOIN {$wpdb->usermeta} umf ON umf.meta_key = 'first_name' AND u.ID = umf.user_id\r\n\t\tLEFT JOIN {$wpdb->usermeta} uml ON uml.meta_key = 'last_name' AND u.ID = uml.user_id\r\n\t\tLEFT JOIN {$wpdb->pmpro_memberships_users} mu ON u.ID = mu.user_id\r\n\t\tLEFT JOIN {$wpdb->pmpro_membership_levels} m ON mu.membership_id = m.id\r\n\t\tWHERE mu.status IN ('{$status_list}')\r\n\t\t\tAND (umh.meta_value IS NULL OR umh.meta_value <> '1')\r\n\t\t\t";
        if (count($statuses) == 1 && in_array('active', $statuses)) {
            $sqlQuery .= " AND mu.membership_id > 0";
        } else {
            $sqlQuery .= " AND mu.membership_id >= 0";
        }
        if ($levels) {
            $sqlQuery .= " AND mu.membership_id IN(" . esc_sql($levels) . ") ";
        }
        $sqlQuery .= " ORDER BY " . esc_sql($order_by) . " " . esc_sql($order);
    }
    $sqlQuery .= " LIMIT {$start}, {$limit}";
    $sqlQuery = apply_filters("pmpro_member_directory_sql", $sqlQuery, $levels, $s, $pn, $limit, $start, $end);
    if (WP_DEBUG) {
        error_log("Query for Directory search: " . $sqlQuery);
    }
    $theusers = $wpdb->get_results($sqlQuery);
    $totalrows = $wpdb->get_var("SELECT FOUND_ROWS() AS found_rows");
    if (WP_DEBUG) {
        error_log("Rows returned: " . $totalrows);
    }
    //update end to match totalrows if total rows is small
    if ($totalrows < $end) {
        $end = $totalrows;
    }
    $layout_cols = preg_replace('/[^0-9]/', '', $layout);
    if (!empty($layout_cols)) {
        $theusers_chunks = array_chunk($theusers, $layout_cols);
    } else {
        $theusers_chunks = array_chunk($theusers, 1);
    }
    ob_start();
    ?>
	<?php 
    if (!empty($show_search)) {
        ?>
		<form role="search" class="pmpro_member_directory_search search-form">
			<div class="pmpromd_main_search_field">
				<label>
					<span class="screen-reader-text"><?php 
        _e('Search for:', 'label');
        ?>
</span>
					<input type="search" class="search-field" placeholder="<?php 
        _e("Search Members", "pmpromd");
        ?>
"
					       name="ps"
					       value="<?php 
        if (!empty($_REQUEST['ps'])) {
            echo esc_attr($_REQUEST['ps']);
        }
        ?>
" title="<?php 
        _e("Search Members", "pmprmd");
        ?>
"/>
					<input type="hidden" name="limit" value="<?php 
        echo esc_attr($limit);
        ?>
"/>
				</label>
			</div>
			<?php 
        $field_array = apply_filters('pmpro_member_directory_extra_search_input', array());
        foreach ($field_array as $field) {
            echo $field;
        }
        ?>
			<div class="search-button clear">
				<input type="submit" class="search-submit" value="<?php 
        _e("Search Members", "pmpromd");
        ?>
">
			</div>
		</form>
	<?php 
    }
    ?>

	<h3 id="pmpro_member_directory_subheading">
		<?php 
    if (!empty($s)) {
        ?>
			<?php 
        printf(__('Profiles Within <em>%s</em>.', 'pmpromd'), ucwords(esc_html($s)));
        ?>
		<?php 
    } else {
        ?>
			<?php 
        _e('Viewing All Profiles.', 'pmpromd');
        ?>
		<?php 
    }
    ?>
		<?php 
    if ($totalrows > 0) {
        ?>
			<small class="muted">
				(<?php 
        if ($totalrows == 1) {
            printf(__('Showing 1 Result', 'pmpromd'), $start + 1, $end, $totalrows);
        } else {
            printf(__('Showing %s-%s of %s Results', 'pmpromd'), $start + 1, $end, $totalrows);
        }
        ?>
)
			</small>
		<?php 
    }
    ?>
	</h3>
	<?php 
    if (!empty($theusers)) {
        if (!empty($fields)) {
            $fields_array = explode(";", $fields);
            if (!empty($fields_array)) {
                for ($i = 0; $i < count($fields_array); $i++) {
                    $fields_array[$i] = explode(",", trim($fields_array[$i]));
                }
            }
        } else {
            $fields_array = false;
        }
        // Get Register Helper field options
        $rh_fields = array();
        if (!empty($pmprorh_registration_fields)) {
            foreach ($pmprorh_registration_fields as $location) {
                foreach ($location as $field) {
                    if (!empty($field->options)) {
                        $rh_fields[$field->name] = $field->options;
                    }
                }
            }
        }
        ?>
		<div class="pmpro_member_directory">
			<hr class="clear"/>
			<?php 
        if ($layout == "table") {
            ?>
				<table width="100%" cellpadding="0" cellspacing="0" border="0">
					<thead>
					<?php 
            if (!empty($show_avatar)) {
                ?>
						<th class="pmpro_member_directory_avatar">
							<?php 
                _e('Avatar', 'pmpro');
                ?>
						</th>
					<?php 
            }
            ?>
					<th class="pmpro_member_directory_display-name">
						<?php 
            _e('Member', 'pmpro');
            ?>
					</th>
					<?php 
            if (!empty($show_email)) {
                ?>
						<th class="pmpro_member_directory_email">
							<?php 
                _e('Email Address', 'pmpro');
                ?>
						</th>
					<?php 
            }
            ?>
					<?php 
            if (!empty($fields_array)) {
                ?>
						<th class="pmpro_member_directory_additional">
							<?php 
                _e('More Information', 'pmpro');
                ?>
						</th>
					<?php 
            }
            ?>
					<?php 
            if (!empty($show_level)) {
                ?>
						<th class="pmpro_member_directory_level">
							<?php 
                _e('Level', 'pmpro');
                ?>
						</th>
					<?php 
            }
            ?>
					<?php 
            if (!empty($show_startdate)) {
                ?>
						<th class="pmpro_member_directory_date">
							<?php 
                _e('Start Date', 'pmpro');
                ?>
						</th>
					<?php 
            }
            ?>
					<?php 
            if (!empty($link) && !empty($profile_url)) {
                ?>
						<th class="pmpro_member_directory_link">&nbsp;</th>
					<?php 
            }
            ?>
					</thead>
					<tbody>
					<?php 
            $count = 0;
            foreach ($theusers as $auser) {
                $auser = get_userdata($auser->ID);
                $auser->membership_level = pmpro_getMembershipLevelForUser($auser->ID);
                $count++;
                ?>
						<tr id="pmpro_member_directory_row-<?php 
                echo $auser->ID;
                ?>
"
						    class="pmpro_member_directory_row<?php 
                if (!empty($link) && !empty($profile_url)) {
                    echo " pmpro_member_directory_linked";
                }
                ?>
">
							<?php 
                if (!empty($show_avatar)) {
                    ?>
								<td class="pmpro_member_directory_avatar">
									<?php 
                    if (!empty($link) && !empty($profile_url)) {
                        ?>
										<a href="<?php 
                        echo add_query_arg('pu', $auser->user_nicename, $profile_url);
                        ?>
"><?php 
                        echo get_avatar($auser->ID, $avatar_size);
                        ?>
</a>
									<?php 
                    } else {
                        ?>
										<?php 
                        echo get_avatar($auser->ID, $avatar_size);
                        ?>
									<?php 
                    }
                    ?>
								</td>
							<?php 
                }
                ?>
							<td>
								<h3 class="pmpro_member_directory_display-name">
									<?php 
                if (!empty($link) && !empty($profile_url)) {
                    ?>
										<a href="<?php 
                    echo add_query_arg('pu', $auser->user_nicename, $profile_url);
                    ?>
"><?php 
                    echo $auser->display_name;
                    ?>
</a>
									<?php 
                } else {
                    ?>
										<?php 
                    echo $auser->display_name;
                    ?>
									<?php 
                }
                ?>
								</h3>
							</td>
							<?php 
                if (!empty($show_email)) {
                    ?>
								<td class="pmpro_member_directory_email">
									<?php 
                    echo $auser->user_email;
                    ?>
								</td>
							<?php 
                }
                ?>
							<?php 
                if (!empty($fields_array)) {
                    ?>
								<td class="pmpro_member_directory_additional">
									<?php 
                    foreach ($fields_array as $field) {
                        $meta_field = $auser->{$field}[1];
                        if (!empty($meta_field)) {
                            ?>
											<p class="pmpro_member_directory_<?php 
                            echo $field[1];
                            ?>
">
												<?php 
                            if (is_array($meta_field) && !empty($meta_field['filename'])) {
                                //this is a file field
                                ?>
													<strong><?php 
                                echo $field[0];
                                ?>
</strong>
													<?php 
                                echo pmpromd_display_file_field($meta_field);
                                ?>
													<?php 
                            } elseif (is_array($meta_field)) {
                                //this is a general array, check for Register Helper options first
                                if (!empty($rh_fields[$field[1]])) {
                                    foreach ($meta_field as $key => $value) {
                                        $meta_field[$key] = $rh_fields[$field[1]][$value];
                                    }
                                }
                                ?>
													<strong><?php 
                                echo $field[0];
                                ?>
</strong>
													<?php 
                                echo implode(", ", $meta_field);
                                ?>
													<?php 
                            } else {
                                if ($field[1] == 'user_url') {
                                    ?>
														<a href="<?php 
                                    echo esc_url($meta_field);
                                    ?>
"
														   target="_blank"><?php 
                                    echo $field[0];
                                    ?>
</a>
														<?php 
                                } else {
                                    ?>
														<strong><?php 
                                    echo $field[0];
                                    ?>
</strong>
														<?php 
                                    $meta_field_embed = wp_oembed_get($meta_field);
                                    if (!empty($meta_field_embed)) {
                                        echo $meta_field_embed;
                                    } else {
                                        echo make_clickable($meta_field);
                                    }
                                    ?>
														<?php 
                                }
                            }
                            ?>
											</p>
											<?php 
                        }
                    }
                    ?>
								</td>
								<?php 
                }
                ?>
							<?php 
                if (!empty($show_level)) {
                    ?>
								<td class="pmpro_member_directory_level">
									<?php 
                    echo $auser->membership_level->name;
                    ?>
								</td>
							<?php 
                }
                ?>
							<?php 
                if (!empty($show_startdate)) {
                    ?>
								<td class="pmpro_member_directory_date">
									<?php 
                    echo date(get_option("date_format"), $auser->membership_level->startdate);
                    ?>
								</td>
							<?php 
                }
                ?>
							<?php 
                if (!empty($link) && !empty($profile_url)) {
                    ?>
								<td class="pmpro_member_directory_link">
									<a href="<?php 
                    echo add_query_arg('pu', $auser->user_nicename, $profile_url);
                    ?>
"><?php 
                    _e('View Profile', 'pmpromd');
                    ?>
</a>
								</td>
							<?php 
                }
                ?>
						</tr>
						<?php 
            }
            ?>
					</tbody>
				</table>
				<?php 
        } else {
            $count = 0;
            foreach ($theusers_chunks as $row) {
                ?>
					<div class="row">
						<?php 
                foreach ($row as $auser) {
                    $count++;
                    $auser = get_userdata($auser->ID);
                    $auser->membership_level = pmpro_getMembershipLevelForUser($auser->ID);
                    ?>
							<div class="medium-<?php 
                    if ($layout == '2col') {
                        $avatar_align = "alignright";
                        echo '6 ';
                    } elseif ($layout == '3col') {
                        $avatar_align = "aligncenter";
                        echo '4 text-center ';
                    } elseif ($layout == '4col') {
                        $avatar_align = "aligncenter";
                        echo '3 text-center ';
                    } else {
                        $avatar_align = "alignright";
                        echo '12 ';
                    }
                    if ($count == $end) {
                        echo 'end ';
                    }
                    ?>
								columns">
								<div id="pmpro_member-<?php 
                    echo $auser->ID;
                    ?>
">
									<?php 
                    if (!empty($show_avatar)) {
                        ?>
										<div class="pmpro_member_directory_avatar">
											<?php 
                        if (!empty($link) && !empty($profile_url)) {
                            ?>
												<a class="<?php 
                            echo $avatar_align;
                            ?>
"
												   href="<?php 
                            echo add_query_arg('pu', $auser->user_nicename, $profile_url);
                            ?>
"><?php 
                            echo get_avatar($auser->ID, $avatar_size, null, $auser->display_name);
                            ?>
</a>
											<?php 
                        } else {
                            ?>
												<span
													class="<?php 
                            echo $avatar_align;
                            ?>
"><?php 
                            echo get_avatar($auser->ID, $avatar_size, null, $auser->display_name);
                            ?>
</span>
											<?php 
                        }
                        ?>
										</div>
									<?php 
                    }
                    ?>
									<h3 class="pmpro_member_directory_display-name">
										<?php 
                    if (!empty($link) && !empty($profile_url)) {
                        ?>
											<a href="<?php 
                        echo add_query_arg('pu', $auser->user_nicename, $profile_url);
                        ?>
"><?php 
                        echo $auser->display_name;
                        ?>
</a>
										<?php 
                    } else {
                        ?>
											<?php 
                        echo $auser->display_name;
                        ?>
										<?php 
                    }
                    ?>
									</h3>
									<?php 
                    if (!empty($show_email)) {
                        ?>
										<p class="pmpro_member_directory_email">
											<strong><?php 
                        _e('Email Address', 'pmpro');
                        ?>
</strong>
											<?php 
                        echo $auser->user_email;
                        ?>
										</p>
									<?php 
                    }
                    ?>
									<?php 
                    if (!empty($show_level)) {
                        ?>
										<p class="pmpro_member_directory_level">
											<strong><?php 
                        _e('Level', 'pmpro');
                        ?>
</strong>
											<?php 
                        echo $auser->membership_level->name;
                        ?>
										</p>
									<?php 
                    }
                    ?>
									<?php 
                    if (!empty($show_startdate)) {
                        ?>
										<p class="pmpro_member_directory_date">
											<strong><?php 
                        _e('Start Date', 'pmpro');
                        ?>
</strong>
											<?php 
                        echo date(get_option("date_format"), $auser->membership_level->startdate);
                        ?>
										</p>
									<?php 
                    }
                    ?>
									<?php 
                    if (!empty($fields_array)) {
                        foreach ($fields_array as $field) {
                            $meta_field = $auser->{$field}[1];
                            if (!empty($meta_field)) {
                                ?>
												<p class="pmpro_member_directory_<?php 
                                echo $field[1];
                                ?>
">
													<?php 
                                if (is_array($meta_field) && !empty($meta_field['filename'])) {
                                    //this is a file field
                                    ?>
														<strong><?php 
                                    echo $field[0];
                                    ?>
</strong>
														<?php 
                                    echo pmpromd_display_file_field($meta_field);
                                    ?>
														<?php 
                                } elseif (is_array($meta_field)) {
                                    //this is a general array, check for Register Helper options first
                                    if (!empty($rh_fields[$field[1]])) {
                                        foreach ($meta_field as $key => $value) {
                                            $meta_field[$key] = $rh_fields[$field[1]][$value];
                                        }
                                    }
                                    ?>
														<strong><?php 
                                    echo $field[0];
                                    ?>
</strong>
														<?php 
                                    echo implode(", ", $meta_field);
                                    ?>
														<?php 
                                } elseif ($field[1] == 'user_url') {
                                    ?>
														<a href="<?php 
                                    echo $auser->{$field}[1];
                                    ?>
"
														   target="_blank"><?php 
                                    echo $field[0];
                                    ?>
</a>
														<?php 
                                } else {
                                    ?>
														<strong><?php 
                                    echo $field[0];
                                    ?>
:</strong>
														<?php 
                                    echo make_clickable($auser->{$field}[1]);
                                    ?>
														<?php 
                                }
                                ?>
												</p>
												<?php 
                            }
                        }
                    }
                    ?>
									<?php 
                    if (!empty($link) && !empty($profile_url)) {
                        ?>
										<p class="pmpro_member_directory_link">
											<a class="more-link"
											   href="<?php 
                        echo add_query_arg('pu', $auser->user_nicename, $profile_url);
                        ?>
"><?php 
                        _e('View Profile', 'pmpromd');
                        ?>
</a>
										</p>
									<?php 
                    }
                    ?>
								</div> <!-- end pmpro_addon_package-->
							</div>
							<?php 
                }
                ?>
					</div> <!-- end row -->
					<hr/>
					<?php 
            }
        }
        ?>
		</div> <!-- end pmpro_member_directory -->
		<?php 
    } else {
        ?>
		<p class="pmpro_member_directory_message pmpro_message pmpro_error">
			<?php 
        _e('No matching profiles found', 'pmpromd');
        ?>
			<?php 
        if ($s) {
            printf(__('within <em>%s</em>.', 'pmpromd'), ucwords(esc_html($s)));
            if (!empty($directory_url)) {
                ?>
					<a class="more-link"
					   href="<?php 
                echo $directory_url;
                ?>
"><?php 
                _e('View All Members', 'pmpromd');
                ?>
</a>
					<?php 
            }
        } else {
            echo ".";
        }
        ?>
		</p>
		<?php 
    }
    //prev/next
    ?>
	<div class="pmpro_pagination">
		<?php 
    //prev
    if ($pn > 1) {
        ?>
			<span class="pmpro_prev"><a href="<?php 
        echo esc_url(add_query_arg(array("ps" => $s, "pn" => $pn - 1, "limit" => $limit), get_permalink($post->ID)));
        ?>
"><?php 
        printf(__("%s Previous", "pmpromd"), '&alquo;');
        ?>
</a></span>
			<?php 
    }
    //next
    if ($totalrows > $end) {
        ?>
			<span class="pmpro_next"><a href="<?php 
        echo esc_url(add_query_arg(array("ps" => $s, "pn" => $pn + 1, "limit" => $limit), get_permalink($post->ID)));
        ?>
"><?php 
        printf(__("Next %s", "pmpromd"), '&raquo;');
        ?>
</a></span>
			<?php 
    }
    ?>
	</div>
	<?php 
    ?>
	<?php 
    $temp_content = ob_get_contents();
    ob_end_clean();
    return $temp_content;
}
function pmpromd_profile_shortcode($atts, $content = null, $code = "")
{
    // $atts    ::= array of attributes
    // $content ::= text within enclosing form of shortcode element
    // $code    ::= the shortcode found, when == callback name
    // examples: [pmpro_member_profile avatar="false" email="false"]
    /**
     * Init variables
     */
    $avatar_size = '128';
    $fields = null;
    $show_avatar = null;
    $show_bio = null;
    $show_billing = null;
    $show_email = null;
    $show_level = null;
    $show_name = null;
    $show_phone = null;
    $show_search = null;
    $show_startdate = null;
    $user_id = null;
    // decode attributes from shortcode
    extract(shortcode_atts(array('avatar_size' => '128', 'fields' => null, 'show_avatar' => null, 'show_bio' => null, 'show_billing' => null, 'show_email' => null, 'show_level' => null, 'show_name' => null, 'show_phone' => null, 'show_search' => null, 'show_startdate' => null, 'user_id' => null), $atts));
    global $current_user, $display_name, $wpdb, $pmpro_pages, $pmprorh_registration_fields;
    //some page vars
    if (!empty($pmpro_pages['directory'])) {
        $directory_url = get_permalink($pmpro_pages['directory']);
    } else {
        $directory_url = "";
    }
    if (!empty($pmpro_pages['profile'])) {
        $profile_url = get_permalink($pmpro_pages['profile']);
    }
    //turn 0's into falses
    if ($show_avatar === "0" || $show_avatar === "false" || $show_avatar === "no" || $show_avatar === __('no', 'pmpromd')) {
        $show_avatar = false;
    } else {
        $show_avatar = true;
    }
    if ($show_billing === "0" || $show_billing === "false" || $show_billing === "no" || $show_billing === __('no', 'pmpromd')) {
        $show_billing = false;
    } else {
        $show_billing = true;
    }
    if ($show_bio === "0" || $show_bio === "false" || $show_bio === "no" || $show_bio === __('no', 'pmpromd')) {
        $show_bio = false;
    } else {
        $show_bio = true;
    }
    if ($show_email === "0" || $show_email === "false" || $show_email === "no" || $show_email === __('no', 'pmpromd')) {
        $show_email = false;
    } else {
        $show_email = true;
    }
    if ($show_level === "0" || $show_level === "false" || $show_level === "no" || $show_level === __('no', 'pmpromd')) {
        $show_level = false;
    } else {
        $show_level = true;
    }
    if ($show_name === "0" || $show_name === "false" || $show_name === "no" || $show_name === __('no', 'pmpromd')) {
        $show_name = false;
    } else {
        $show_name = true;
    }
    if ($show_phone === "0" || $show_phone === "false" || $show_phone === "no" || $show_phone === __('no', 'pmpromd')) {
        $show_phone = false;
    } else {
        $show_phone = true;
    }
    if ($show_search === "0" || $show_search === "false" || $show_search === "no" || $show_search === __('no', 'pmpromd')) {
        $show_search = false;
    } else {
        $show_search = true;
    }
    if ($show_startdate === "0" || $show_startdate === "false" || $show_startdate === "no" || $show_startdate === __('no', 'pmpromd')) {
        $show_startdate = false;
    } else {
        $show_startdate = true;
    }
    if (isset($_REQUEST['limit'])) {
        $limit = intval($_REQUEST['limit']);
    } elseif (empty($limit)) {
        $limit = 15;
    }
    if (empty($user_id) && !empty($_REQUEST['pu'])) {
        //Get the profile user
        $pu = pmpromd_sanitize($_REQUEST['pu']);
        if (is_numeric($pu)) {
            $pu = get_user_by('id', $pu);
        } else {
            $pu = get_user_by('slug', $pu);
        }
        $user_id = $pu->ID;
    }
    if (!empty($user_id)) {
        $pu = get_userdata($user_id);
    } elseif (empty($_REQUEST['pu'])) {
        $pu = get_userdata($current_user->ID);
    }
    if (!empty($pu)) {
        $pu->membership_level = pmpro_getMembershipLevelForUser($pu->ID);
    }
    ob_start();
    ?>
	<?php 
    if (!empty($show_search)) {
        ?>
		<form action="<?php 
        echo $directory_url;
        ?>
" method="post" role="search"
		      class="pmpro_member_directory_search search-form">
			<label>
				<span class="screen-reader-text"><?php 
        _e('Search for:', 'label');
        ?>
</span>
				<input type="search" class="search-field" placeholder="<?php 
        _e("Search Members", "pmpromd");
        ?>
"
				       name="ps"
				       value="<?php 
        if (!empty($_REQUEST['ps'])) {
            echo esc_attr(pmpro_sanitize($_REQUEST['ps']));
        }
        ?>
" title="<?php 
        _e("Search Members", "pmpromd");
        ?>
"/>
				<input type="hidden" name="limit" value="<?php 
        echo esc_attr($limit);
        ?>
"/>
			</label>
			<input type="submit" class="search-submit" value="<?php 
        _e("Search Members", "pmpromd");
        ?>
">
		</form>
	<?php 
    }
    ?>
	<?php 
    if (!empty($pu)) {
        if (!empty($fields)) {
            $fields_array = explode(";", $fields);
            if (!empty($fields_array)) {
                for ($i = 0; $i < count($fields_array); $i++) {
                    $fields_array[$i] = explode(",", $fields_array[$i]);
                }
            }
        } else {
            $fields_array = false;
        }
        // Get Register Helper field options
        $rh_fields = array();
        if (!empty($pmprorh_registration_fields)) {
            foreach ($pmprorh_registration_fields as $location) {
                foreach ($location as $field) {
                    if (!empty($field->options)) {
                        $rh_fields[$field->name] = $field->options;
                    }
                }
            }
        }
        ?>
		<div id="pmpro_member_profile-<?php 
        echo $pu->ID;
        ?>
" class="pmpro_member_profile">
			<?php 
        if (!empty($show_avatar)) {
            ?>
				<p class="pmpro_member_directory_avatar">
					<?php 
            echo get_avatar($pu->ID, $avatar_size, null, $pu->display_name, array("class" => "alignright"));
            ?>
				</p>
			<?php 
        }
        ?>
			<?php 
        if (!empty($show_name) && !empty($pu->display_name)) {
            ?>
				<h2 class="pmpro_member_directory_name">
					<?php 
            echo $pu->display_name;
            ?>
				</h2>
			<?php 
        }
        ?>
			<?php 
        if (!empty($show_bio) && !empty($pu->description)) {
            ?>
				<p class="pmpro_member_directory_bio">
					<strong><?php 
            _e('Biographical Info', 'wp');
            ?>
</strong>
					<?php 
            echo $pu->description;
            ?>
				</p>
			<?php 
        }
        ?>
			<?php 
        if (!empty($show_email)) {
            ?>
				<p class="pmpro_member_directory_email">
					<strong><?php 
            _e('Email Address', 'pmpro');
            ?>
</strong>
					<?php 
            echo $pu->user_email;
            ?>
				</p>
			<?php 
        }
        ?>
			<?php 
        if (!empty($show_level)) {
            ?>
				<p class="pmpro_member_directory_level">
					<strong><?php 
            _e('Level', 'pmpro');
            ?>
</strong>
					<?php 
            echo $pu->membership_level->name;
            ?>
				</p>
			<?php 
        }
        ?>
			<?php 
        if (!empty($show_startdate)) {
            ?>
				<p class="pmpro_member_directory_date">
					<strong><?php 
            _e('Start Date', 'pmpro');
            ?>
</strong>
					<?php 
            echo date(get_option("date_format"), $pu->membership_level->startdate);
            ?>
				</p>
			<?php 
        }
        ?>
			<?php 
        if (!empty($show_billing) && !empty($pu->pmpro_baddress1)) {
            ?>
				<p class="pmpro_member_directory_baddress">
					<strong><?php 
            _e('Address', 'pmpro');
            ?>
</strong>
					<?php 
            echo $pu->pmpro_baddress1;
            ?>
<br/>
					<?php 
            if (!empty($pu->pmpro_baddress2)) {
                echo $pu->pmpro_baddress2 . "<br />";
            }
            ?>
					<?php 
            if ($pu->pmpro_bcity && $pu->pmpro_bstate) {
                ?>
						<?php 
                echo $pu->pmpro_bcity;
                ?>
, <?php 
                echo $pu->pmpro_bstate;
                echo $pu->pmpro_bzipcode;
                ?>
						<br/>
						<?php 
                echo $pu->pmpro_bcountry;
                ?>
<br/>
					<?php 
            }
            ?>
				</p>
			<?php 
        }
        ?>
			<?php 
        if (!empty($show_phone) && !empty($pu->pmpro_bphone)) {
            ?>
				<p class="pmpro_member_directory_phone">
					<strong><?php 
            _e('Phone Number', 'pmpro');
            ?>
</strong>
					<?php 
            echo formatPhone($pu->pmpro_bphone);
            ?>
				</p>
			<?php 
        }
        ?>
			<?php 
        //filter the fields
        $fields_array = apply_filters('pmpro_member_profile_fields', $fields_array, $pu);
        if (!empty($fields_array)) {
            foreach ($fields_array as $field) {
                if (empty($field[0])) {
                    break;
                }
                $meta_field = $pu->{$field}[1];
                if (!empty($meta_field)) {
                    ?>
						<p class="pmpro_member_directory_<?php 
                    echo esc_attr($field[1]);
                    ?>
">
							<?php 
                    if (is_array($meta_field) && !empty($meta_field['filename'])) {
                        //this is a file field
                        ?>
								<strong><?php 
                        echo $field[0];
                        ?>
</strong>
								<?php 
                        echo pmpromd_display_file_field($meta_field);
                        ?>
								<?php 
                    } elseif (is_array($meta_field)) {
                        //this is a general array, check for Register Helper options first
                        if (!empty($rh_fields[$field[1]])) {
                            foreach ($meta_field as $key => $value) {
                                $meta_field[$key] = $rh_fields[$field[1]][$value];
                            }
                        }
                        ?>
								<strong><?php 
                        echo $field[0];
                        ?>
</strong>
								<?php 
                        echo implode(", ", $meta_field);
                        ?>
								<?php 
                    } else {
                        if ($field[1] == 'user_url') {
                            ?>
									<a href="<?php 
                            echo esc_url($meta_field);
                            ?>
"
									   target="_blank"><?php 
                            echo $field[0];
                            ?>
</a>
									<?php 
                        } else {
                            ?>
									<strong><?php 
                            echo $field[0];
                            ?>
</strong>
									<?php 
                            $meta_field_embed = wp_oembed_get($meta_field);
                            if (!empty($meta_field_embed)) {
                                echo $meta_field_embed;
                            } else {
                                echo make_clickable($meta_field);
                            }
                            ?>
									<?php 
                        }
                    }
                    ?>
						</p>
						<?php 
                }
            }
        }
        ?>
			<div class="pmpro_clear"></div>
		</div>
		<hr/>
		<?php 
        if (!empty($directory_url)) {
            ?>
			<div align="center"><a class="more-link"
			                       href="<?php 
            echo $directory_url;
            ?>
"><?php 
            _e("View All Members", "pmpromd");
            ?>
</a>
			</div>
		<?php 
        }
        ?>
		<?php 
    }
    ?>
	<?php 
    $temp_content = ob_get_contents();
    ob_end_clean();
    return $temp_content;
}