| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\widgets; |
| 4 |
|
| 5 |
use WP_Widget; |
| 6 |
|
| 7 |
class Search extends WP_Widget { |
| 8 |
/** |
| 9 |
* @var array |
| 10 |
*/ |
| 11 |
private $default_instance; |
| 12 |
|
| 13 |
function __construct() { |
| 14 |
parent::__construct( 'wpforo_search', 'wpForo Search', [ 'description' => 'wpForo search form' ] ); |
| 15 |
$this->init_local_vars(); |
| 16 |
add_action( 'wp_ajax_wpforo_load_ajax_widget_Search', [ $this, 'load_ajax_widget' ] ); |
| 17 |
add_action( 'wp_ajax_nopriv_wpforo_load_ajax_widget_Search', [ $this, 'load_ajax_widget' ] ); |
| 18 |
} |
| 19 |
|
| 20 |
private function init_local_vars() { |
| 21 |
$this->default_instance = [ |
| 22 |
'title' => __( 'Forum Search', 'wpforo' ), |
| 23 |
'boardid' => 0, |
| 24 |
]; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_widget() { |
| 28 |
ob_start(); ?> |
| 29 |
|
| 30 |
<form action="<?php echo wpforo_home_url() ?>" method="GET" id="wpforo-search-form"> |
| 31 |
<?php wpforo_make_hidden_fields_from_url( wpforo_home_url() ) ?> |
| 32 |
<label class="wpf-search-widget-label"> |
| 33 |
<input type="text" placeholder="<?php wpforo_phrase( 'Search...' ) ?>" name="wpfs" class="wpfw-100" value="<?php echo isset( $_GET['wpfs'] ) ? esc_attr( sanitize_text_field( $_GET['wpfs'] ) ) : '' ?>"> |
| 34 |
<svg onclick="this.closest('form').submit();" viewBox="0 0 16 16" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="Guide"/><g id="Layer_2"><path d="M13.85,13.15l-2.69-2.69c0.74-0.9,1.2-2.03,1.2-3.28C12.37,4.33,10.04,2,7.18,2S2,4.33,2,7.18s2.33,5.18,5.18,5.18 c1.25,0,2.38-0.46,3.28-1.2l2.69,2.69c0.1,0.1,0.23,0.15,0.35,0.15s0.26-0.05,0.35-0.15C14.05,13.66,14.05,13.34,13.85,13.15z M3,7.18C3,4.88,4.88,3,7.18,3s4.18,1.88,4.18,4.18s-1.88,4.18-4.18,4.18S3,9.49,3,7.18z"/></g></svg> |
| 35 |
</label> |
| 36 |
</form> |
| 37 |
|
| 38 |
<?php |
| 39 |
|
| 40 |
return ob_get_clean(); |
| 41 |
} |
| 42 |
|
| 43 |
public function load_ajax_widget() { |
| 44 |
wp_send_json_success( [ 'html' => $this->get_widget() ] ); |
| 45 |
} |
| 46 |
|
| 47 |
public function widget( $args, $instance ) { |
| 48 |
wp_enqueue_script( 'wpforo-widgets-js' ); |
| 49 |
$instance = wpforo_parse_args( $instance, $this->default_instance ); |
| 50 |
$data = [ |
| 51 |
'boardid' => $instance['boardid'], |
| 52 |
'action' => 'wpforo_load_ajax_widget_Search', |
| 53 |
]; |
| 54 |
|
| 55 |
if( WPF()->board->get_current( 'boardid' ) === $instance['boardid'] ){ |
| 56 |
$html = $this->get_widget(); |
| 57 |
$onload = false; |
| 58 |
}else{ |
| 59 |
$html = '<div style="text-align: center; font-size: 20px;"><i class="fas fa-spinner fa-spin"></i></div>'; |
| 60 |
$onload = true; |
| 61 |
$data['referer'] = home_url(); |
| 62 |
} |
| 63 |
$json = wp_json_encode( $data ); |
| 64 |
|
| 65 |
echo $args['before_widget'] . '<div id="wpf-widget-search" class="wpforo-widget-wrap">'; |
| 66 |
if( $instance['title'] ) echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
| 67 |
echo '<div class="wpforo-widget-content wpforo-ajax-widget ' . ( ! $onload ? 'wpforo-ajax-widget-onload-false' : '' ) . '" data-json="' . esc_attr( $json ) . '">' . $html . '</div></div>' . $args['after_widget']; |
| 68 |
} |
| 69 |
|
| 70 |
public function form( $instance ) { |
| 71 |
$instance = wpforo_parse_args( $instance, $this->default_instance ); |
| 72 |
$boardid = (int) $instance['boardid']; |
| 73 |
$title = (string) $instance['title']; |
| 74 |
?> |
| 75 |
<p> |
| 76 |
<label for="<?php echo $this->get_field_id( 'title' ) ?>"><?php _e( 'Title', 'wpforo' ); ?>:</label> |
| 77 |
<input id="<?php echo $this->get_field_id( 'title' ) ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> |
| 78 |
</p> |
| 79 |
<p> |
| 80 |
<label for="<?php echo $this->get_field_id( 'boardid' ) ?>"></label> |
| 81 |
<select id="<?php echo $this->get_field_id( 'boardid' ) ?>" name="<?php echo esc_attr( $this->get_field_name( 'boardid' ) ); ?>"> |
| 82 |
<?php echo WPF()->board->dropdown( $boardid ) ?> |
| 83 |
</select> |
| 84 |
</p> |
| 85 |
<?php |
| 86 |
} |
| 87 |
|
| 88 |
public function update( $new_instance, $old_instance ) { |
| 89 |
$new_instance = wpforo_parse_args( $new_instance, $this->default_instance ); |
| 90 |
$instance = []; |
| 91 |
$instance['boardid'] = (int) $new_instance['boardid']; |
| 92 |
$instance['title'] = strip_tags( (string) $new_instance['title'] ); |
| 93 |
|
| 94 |
return $instance; |
| 95 |
} |
| 96 |
} |
| 97 |
|