/** * Display or retrieve the HTML dropdown list of sorting options. * * @since 1.0.0 * @access public * @param string|array $args Optional. Override default arguments. * @return string HTML. */ function wpum_directory_sort_dropdown($args = '') { $defaults = array('exclude' => '', 'selected' => '', 'class' => 'wpum-dropdown-sort'); $args = wp_parse_args($args, $defaults); // Get css class $class = $args['class']; // Get options $sorting_methods = wpum_get_directory_sorting_methods(); // Exclude methods if any if (!empty($args['exclude'])) { // Check if it's only one value that we need to exclude if (is_string($args['exclude'])) { unset($sorting_methods[$args['exclude']]); // Check if there's more than one value to exclude } elseif (is_array($args['exclude'])) { foreach ($args['exclude'] as $method_to_exclude) { unset($sorting_methods[$method_to_exclude]); } } } $sorting_methods = apply_filters('wpum_sort_dropdown_methods', $sorting_methods, $args); $selected = isset($_GET['sort']) ? $selected = $_GET['sort'] : ($selected = $args['selected']); $output = "<select name='wpum-dropdown' id='wpum-dropdown' class='{$class}'>\n"; foreach ($sorting_methods as $value => $label) { $method_url = add_query_arg(array('sort' => $value), get_permalink()); if ($selected == $value) { $output .= "\t<option value='" . esc_url($method_url) . "' selected='selected' >{$label}</option>\n"; } else { $output .= "\t<option value='" . esc_url($method_url) . "'>{$label}</option>\n"; } } $output .= "</select>\n"; return $output; }
/** * Modify the WP_User_Query on the directory page. * Specify a custom sorting order. * * @since 1.0.0 * @param array $args WP_User_Query args. * @param string $directory_id id number of the directory. * @return array */ function wpum_directory_pre_set_order($args, $directory_id) { // Get selected sorting method $sorting_method = get_post_meta($directory_id, 'default_sorting_method', true); // Check whether a sorting method is set from frontend if (isset($_GET['sort']) && array_key_exists($_GET['sort'], wpum_get_directory_sorting_methods())) { $sorting_method = sanitize_key($_GET['sort']); } switch ($sorting_method) { case 'user_nicename': $args['orderby'] = 'user_nicename'; break; case 'newest': $args['orderby'] = 'registered'; $args['order'] = 'DESC'; break; case 'oldest': $args['orderby'] = 'registered'; break; case 'name': $args['meta_key'] = 'first_name'; $args['orderby'] = 'meta_value'; $args['order'] = 'ASC'; break; case 'last_name': $args['meta_key'] = 'last_name'; $args['orderby'] = 'meta_value'; $args['order'] = 'ASC'; break; } return $args; }
/** * Adds the directory post type. * This handles the creation of user directories. * * @access public * @since 1.0.0 * @return void */ public function meta_options() { $general_options = array('id' => 'wpum_directory_general_options', 'title' => __('General Settings', 'wpum'), 'pages' => array('wpum_directory'), 'fields' => array(array('id' => 'directory_roles', 'name' => __('User Roles', 'wpum'), 'sub' => __('Leave blank to display all user roles.', 'wpum'), 'desc' => __('Select the user roles you wish to display into this directory.', 'wpum'), 'type' => 'multiselect', 'options' => wpum_get_roles(true)), array('id' => 'display_search_form', 'name' => __('Display search form', 'wpum'), 'desc' => __('Enable this option to display the user search form', 'wpum'), 'type' => 'checkbox', 'std' => 1), array('id' => 'excluded_ids', 'name' => __('Exclude users', 'wpum'), 'sub' => __('Comma separated list of users id you wish to exclude.', 'wpum'), 'desc' => sprintf(__(' Example: type %s to exclude users with those id(s).', 'wpum'), '<code>1, 6, 89</code>'), 'type' => 'text'), array('id' => 'profiles_per_page', 'name' => __('Profiles per page', 'wpum'), 'sub' => __('Select how many profiles you wish to display per page.', 'wpum'), 'type' => 'number', 'std' => 10), array('id' => 'directory_template', 'name' => __('Directory Template', 'wpum'), 'desc' => __('Select a template from the list.', 'wpum'), 'type' => 'select', 'options' => wpum_get_directory_templates()))); // Create the new metabox $this->general_options = new Pretty_Metabox(apply_filters('wpum_directory_general_options', $general_options)); // Build the sorting metabox options $sorting_options = array('id' => 'wpum_directory_sorting_options', 'title' => __('Users sorting', 'wpum'), 'pages' => array('wpum_directory'), 'fields' => array(array('id' => 'display_sorter', 'name' => __('Display sorter', 'wpum'), 'desc' => __('Enable this setting to display a dropdown menu into the directory with the sorting options.', 'wpum'), 'type' => 'checkbox', 'std' => 1), array('id' => 'display_amount', 'name' => __('Display amount filter', 'wpum'), 'desc' => __('Enable this setting to display a dropdown menu into the directory with the results amount filter.', 'wpum'), 'type' => 'checkbox', 'std' => 1), array('id' => 'default_sorting_method', 'name' => __('Sorting method', 'wpum'), 'sub' => __('Select the sorting method for the directory', 'wpum'), 'desc' => __('If the sorter field is visible, this will be used as default option.', 'wpum'), 'type' => 'select', 'std' => 'newest', 'options' => wpum_get_directory_sorting_methods()))); // Create the new metabox $this->sorting_options = new Pretty_Metabox(apply_filters('wpum_directory_general_options', $sorting_options)); }