| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\WeDocs; |
| 4 |
|
| 5 |
class Widget extends \WP_Widget { |
| 6 |
|
| 7 |
/** |
| 8 |
* Sets up the widgets name etc. |
| 9 |
*/ |
| 10 |
public function __construct() { |
| 11 |
$widget_ops = [ |
| 12 |
'classname' => 'wedocs-search-widget', |
| 13 |
'description' => __( 'Document Search Widget', 'wedocs' ), |
| 14 |
]; |
| 15 |
|
| 16 |
parent::__construct( 'wedocs-search-widget', __( 'weDocs Document Search', 'wedocs' ), $widget_ops ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Outputs the content of the widget. |
| 21 |
* |
| 22 |
* @param array $args |
| 23 |
* @param array $instance |
| 24 |
*/ |
| 25 |
public function widget( $args, $instance ) { |
| 26 |
echo $args['before_widget']; |
| 27 |
|
| 28 |
if ( !empty( $instance['title'] ) ) { |
| 29 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
| 30 |
} |
| 31 |
|
| 32 |
$dropdown_args = [ |
| 33 |
'post_type' => 'docs', |
| 34 |
'echo' => 0, |
| 35 |
'depth' => 1, |
| 36 |
'show_option_none' => __( 'All Docs', 'wedocs' ), |
| 37 |
'option_none_value' => 'all', |
| 38 |
'name' => 'search_in_doc', |
| 39 |
]; |
| 40 |
|
| 41 |
if ( isset( $_GET['search_in_doc'] ) && 'all' != $_GET['search_in_doc'] ) { |
| 42 |
$dropdown_args['selected'] = (int) $_GET['search_in_doc']; |
| 43 |
} |
| 44 |
|
| 45 |
$form = '<form role="search" method="get" class="search-form wedocs-search-form" action="' . esc_url( home_url( '/' ) ) . '"> |
| 46 |
<div class="wedocs-search-input"> |
| 47 |
<span class="screen-reader-text">' . _x( 'Search for:', 'label', 'wedocs' ) . '</span> |
| 48 |
<input type="search" class="search-field" placeholder="' . esc_attr_x( 'Documentation Search …', 'placeholder', 'wedocs' ) . '" value="' . get_search_query() . '" name="s" title="' . esc_attr_x( 'Search for:', 'label', 'wedocs' ) . '" /> |
| 49 |
<input type="hidden" name="post_type" value="docs" /> |
| 50 |
</div> |
| 51 |
<div class="wedocs-search-in"> |
| 52 |
' . wp_dropdown_pages( $dropdown_args ) . ' |
| 53 |
</div> |
| 54 |
<input type="submit" class="search-submit" value="' . esc_attr_x( 'Search', 'submit button', 'wedocs' ) . '" /> |
| 55 |
</form>'; |
| 56 |
|
| 57 |
echo $form; |
| 58 |
|
| 59 |
echo $args['after_widget']; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Outputs the options form on admin. |
| 64 |
* |
| 65 |
* @param array $instance The widget options |
| 66 |
*/ |
| 67 |
public function form( $instance ) { |
| 68 |
$title = !empty( $instance['title'] ) ? $instance['title'] : __( 'Documentation Search', 'wedocs' ); ?> |
| 69 |
<p> |
| 70 |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
| 71 |
<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 ); ?>"> |
| 72 |
</p> |
| 73 |
<?php |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Processing widget options on save. |
| 78 |
* |
| 79 |
* @param array $new_instance The new options |
| 80 |
* @param array $old_instance The previous options |
| 81 |
*/ |
| 82 |
public function update( $new_instance, $old_instance ) { |
| 83 |
$instance = []; |
| 84 |
$instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 85 |
|
| 86 |
return $instance; |
| 87 |
} |
| 88 |
} |
| 89 |
|