mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-06-05 15:09:37 +09:00
Add search widget
This commit is contained in:
@@ -765,3 +765,18 @@ small.um-max-filesize span{
|
||||
background-repeat: no-repeat;
|
||||
background-size: 24px 24px;
|
||||
}
|
||||
|
||||
.um-search-area {
|
||||
position: relative;
|
||||
max-width: 300px;
|
||||
}
|
||||
|
||||
.um-search-area .um-search-field {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.um-search-area .um-search-icon {
|
||||
position: absolute;
|
||||
top: 7px;
|
||||
right: 10px;
|
||||
}
|
||||
|
||||
@@ -288,4 +288,8 @@ jQuery(document).ready(function() {
|
||||
return false;
|
||||
});
|
||||
|
||||
jQuery(document).on('click', '#um-search-button', function() {
|
||||
jQuery(this).parents('form').submit();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -16,14 +16,14 @@
|
||||
extract( $args );
|
||||
|
||||
$query = $ultimatemember->permalinks->get_query_array();
|
||||
|
||||
|
||||
foreach( $ultimatemember->members->core_search_fields as $key ) {
|
||||
|
||||
|
||||
if ( isset( $query[$key] ) && ! empty( $query[$key] ) ) {
|
||||
$query_args['search'] = '*' . trim($query[$key]) . '*';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $query_args;
|
||||
}
|
||||
|
||||
@@ -66,6 +66,12 @@
|
||||
|
||||
$query = $ultimatemember->permalinks->get_query_array();
|
||||
|
||||
// if searching
|
||||
if( isset( $query['search'] ) ) {
|
||||
$query_args['search'] = '*' . um_filter_search( $query['search'] ) . '*';
|
||||
unset( $query['search'] );
|
||||
}
|
||||
|
||||
if ( $query && is_array( $query ) ) {
|
||||
foreach( $query as $field => $value ) {
|
||||
|
||||
@@ -147,7 +153,7 @@
|
||||
'value' => '',
|
||||
'compare' => '!='
|
||||
)
|
||||
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1524,3 +1524,46 @@ function um_fetch_user( $user_id ) {
|
||||
return $value;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the search query.
|
||||
*
|
||||
* @param string $search
|
||||
* @return string
|
||||
*/
|
||||
function um_filter_search($search) {
|
||||
$search = trim( strip_tags( $search ) );
|
||||
$search = preg_replace('/[^a-z \.\@\_\-]+/i', '', $search);
|
||||
|
||||
return $search;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user search query
|
||||
* @return string
|
||||
*/
|
||||
function um_get_search_query() {
|
||||
global $ultimatemember;
|
||||
|
||||
$query = $ultimatemember->permalinks->get_query_array();
|
||||
$search = isset( $query['search'] ) ? $query['search'] : '';
|
||||
|
||||
return um_filter_search($search);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ultimate member search form
|
||||
* @return string
|
||||
*/
|
||||
function um_get_search_form() {
|
||||
return do_shortcode( '[ultimatemember_searchform]' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the search form.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function um_search_form() {
|
||||
echo um_get_search_form();
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ class UM_Shortcodes {
|
||||
add_shortcode('um_loggedin', array(&$this, 'um_loggedin'));
|
||||
add_shortcode('um_loggedout', array(&$this, 'um_loggedout'));
|
||||
add_shortcode('um_show_content', array(&$this, 'um_shortcode_show_content_for_role') );
|
||||
add_shortcode('ultimatemember_searchform', array(&$this, 'ultimatemember_searchform') );
|
||||
|
||||
|
||||
add_filter('body_class', array(&$this, 'body_class'), 0);
|
||||
@@ -570,4 +571,20 @@ class UM_Shortcodes {
|
||||
return '';
|
||||
}
|
||||
|
||||
public function ultimatemember_searchform($args = array(), $content = "") {
|
||||
// turn off buffer
|
||||
ob_start();
|
||||
|
||||
// load template
|
||||
$this->load_template( 'searchform' );
|
||||
|
||||
// get the buffer
|
||||
$template = ob_get_contents();
|
||||
|
||||
// clear the buffer
|
||||
ob_end_clean();
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
class um_search_widget extends WP_Widget {
|
||||
|
||||
function __construct() {
|
||||
|
||||
parent::__construct(
|
||||
|
||||
// Base ID of your widget
|
||||
'um_search_widget',
|
||||
|
||||
// Widget name will appear in UI
|
||||
__('Ultimate Member - Search', 'ultimatemember'),
|
||||
|
||||
// Widget description
|
||||
array( 'description' => __( 'Shows users they follow in a widget.', 'ultimatemember' ), )
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
// Creating widget front-end
|
||||
public function widget( $args, $instance ) {
|
||||
$title = apply_filters( 'widget_title', $instance['title'] );
|
||||
|
||||
// before and after widget arguments are defined by themes
|
||||
echo $args['before_widget'];
|
||||
if ( ! empty( $title ) ) {
|
||||
echo $args['before_title'] . $title . $args['after_title'];
|
||||
}
|
||||
|
||||
// display the search form
|
||||
um_search_form();
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
// Widget Backend
|
||||
public function form( $instance ) {
|
||||
global $ultimatemember;
|
||||
|
||||
if ( isset( $instance[ 'title' ] ) ) {
|
||||
$title = $instance[ 'title' ];
|
||||
} else {
|
||||
$title = __( 'Search Users', 'ultimatemember' );
|
||||
}
|
||||
|
||||
if ( isset( $instance[ 'max' ] ) ) {
|
||||
$max = $instance[ 'max' ];
|
||||
} else {
|
||||
$max = 11;
|
||||
}
|
||||
|
||||
// Widget admin form
|
||||
?>
|
||||
|
||||
<p>
|
||||
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
|
||||
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
|
||||
</p>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
// Updating widget replacing old instances with new
|
||||
public function update( $new_instance, $old_instance ) {
|
||||
$instance = array();
|
||||
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
|
||||
return $instance;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<form role="search" method="get" class="search-form um-search-form" action="<?php echo esc_url( um_get_core_page( 'members' ) ); ?>">
|
||||
<input type="hidden" name="um_search" value="1">
|
||||
<div class="um-search-area">
|
||||
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ); ?></span>
|
||||
<input type="search" class="um-search-field search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ); ?>" value="<?php echo um_get_search_query(); ?>" name="search" title="<?php echo esc_attr_x( 'Search for:', 'label' ); ?>" />
|
||||
<a href="javascript: void(0);" id="um-search-button" class="um-search-icon um-faicon um-faicon-search"></a>
|
||||
</div>
|
||||
</form>
|
||||
+11
-1
@@ -58,6 +58,12 @@ class UM_API {
|
||||
__('This add-on enables you to link gravatar photos to user accounts with their email address.', 'ultimatemember'),
|
||||
);
|
||||
|
||||
// include widgets
|
||||
require_once um_path . 'core/widgets/um-search-widget.php';
|
||||
|
||||
// init widgets
|
||||
add_action( 'widgets_init', array(&$this, 'widgets_init' ) );
|
||||
|
||||
}
|
||||
|
||||
/***
|
||||
@@ -198,6 +204,10 @@ class UM_API {
|
||||
|
||||
}
|
||||
|
||||
function widgets_init() {
|
||||
register_widget( 'um_search_widget' );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$ultimatemember = new UM_API();
|
||||
$ultimatemember = new UM_API();
|
||||
|
||||
Reference in New Issue
Block a user