/**
  * User directory shortcode
  *
  * @access public
  * @since  1.0.0
  * @return $output shortcode output
  */
 public function wpum_user_directory($atts, $content = null)
 {
     extract(shortcode_atts(array('id' => null), $atts));
     ob_start();
     $directory_id = intval($id);
     // Check if directory exists
     $check_directory = get_post_status($directory_id);
     // Display error if something is wrong.
     if (!$id || $check_directory !== 'publish') {
         $args = array('id' => 'wpum-no-user-directory-id', 'type' => 'error', 'text' => __('Something went wrong, you have not set a directory ID or the directory is not published.', 'wpum'));
         $warning = wpum_message($args, true);
         return;
     }
     // Prepare Pagination
     $number = wpum_directory_profiles_per_page($directory_id);
     $paged = get_query_var('paged') ? get_query_var('paged') : 1;
     if ($paged == 1) {
         $offset = 0;
     } else {
         $offset = ($paged - 1) * $number;
     }
     // Make the query
     $args = array('number' => $number, 'offset' => $offset, 'fields' => wpum_get_user_query_fields());
     $user_query = new WP_User_Query(apply_filters("wpum_user_directory_query", $args, $directory_id));
     // Detect which template we should be using.
     $template = "user-directory.php";
     $template_tag = wpum_directory_has_custom_template($directory_id);
     if ($template_tag) {
         $template = "user-directory-{$template_tag}.php";
     }
     // Build Pagination Count
     // Modify $number var if a custom amount is set from the frontend
     // This updates the pagination too.
     if (isset($_GET['amount']) && is_numeric($_GET['amount'])) {
         $number = $_GET['amount'];
     }
     $total_users = $user_query->total_users;
     $total_pages = ceil($total_users / $number);
     // Merge directory details in array
     $directory_args = array('user_data' => $user_query->get_results(), 'users_found' => $user_query->get_total(), 'total_users' => $total_users, 'total_pages' => $total_pages, 'directory_id' => $directory_id, 'paged' => $paged, 'search_form' => wpum_directory_has_search_form($directory_id));
     // Load the template
     get_wpum_template($template, array('directory_args' => $directory_args));
     $output = ob_get_clean();
     return $output;
 }
Example #2
0
/**
 * Modify the user query to search for users through the search form.
 *
 * @param array $args         query arguments.
 * @param string $directory_id directory id number.
 * @since 1.2.0
 */
function wpum_directory_pre_set_search($args, $directory_id)
{
    if (wpum_directory_has_search_form($directory_id) && isset($_POST['search_user'])) {
        $args['search'] = sanitize_text_field($_POST['search_user']);
    }
    return $args;
}