| 1 |
<?php |
| 2 |
/** |
| 3 |
* Search comments widget |
| 4 |
* |
| 5 |
* @since 3.6.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Feature\Comments; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Search comment widget class |
| 17 |
*/ |
| 18 |
class Widget extends \WP_Widget { |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize the widget |
| 22 |
* |
| 23 |
* @since 3.6.0 |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
$options = array( |
| 27 |
'description' => esc_html__( 'A search form for comments.', 'elasticpress' ), |
| 28 |
'show_instance_in_rest' => true, |
| 29 |
); |
| 30 |
|
| 31 |
parent::__construct( 'ep-comments', esc_html__( 'ElasticPress - Comments', 'elasticpress' ), $options ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Display widget |
| 36 |
* |
| 37 |
* @param array $args Widget arguments |
| 38 |
* @param array $instance Widget instance variables |
| 39 |
* @since 3.6.0 |
| 40 |
*/ |
| 41 |
public function widget( $args, $instance ) { |
| 42 |
|
| 43 |
echo wp_kses_post( $args['before_widget'] ); |
| 44 |
|
| 45 |
ob_start(); |
| 46 |
|
| 47 |
if ( ! empty( $instance['title'] ) ) { |
| 48 |
echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'] ); |
| 49 |
} |
| 50 |
|
| 51 |
$wrapper_id = 'ep-search-comments-' . uniqid(); |
| 52 |
?> |
| 53 |
<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="ep-widget-search-comments"> |
| 54 |
<?php |
| 55 |
if ( ! empty( $instance['post_type'] ) ) { |
| 56 |
?> |
| 57 |
<input |
| 58 |
class="ep-widget-search-comments-post-type" |
| 59 |
type="hidden" |
| 60 |
id="<?php echo esc_attr( $wrapper_id ); ?>-post-type" |
| 61 |
value="<?php echo esc_attr( implode( ',', $instance['post_type'] ) ); ?>" |
| 62 |
/> |
| 63 |
<?php |
| 64 |
} |
| 65 |
?> |
| 66 |
</div> |
| 67 |
|
| 68 |
<?php |
| 69 |
$search_comments_html = ob_get_clean(); |
| 70 |
|
| 71 |
// Enqueue Script & Styles |
| 72 |
wp_enqueue_script( 'elasticpress-comments' ); |
| 73 |
wp_enqueue_style( 'elasticpress-comments' ); |
| 74 |
|
| 75 |
// phpcs:disable |
| 76 |
/** |
| 77 |
* Filter comment search widget HTML |
| 78 |
* |
| 79 |
* @hook ep_widget_search_comments |
| 80 |
* @since 3.6.0 |
| 81 |
* @param {string} $search_comments_html Widget HTML |
| 82 |
* @param {string} $title Widget title |
| 83 |
* @return {string} New HTML |
| 84 |
*/ |
| 85 |
echo apply_filters( 'ep_widget_search_comments', $search_comments_html, $instance['title'] ); |
| 86 |
// phpcs:enable |
| 87 |
|
| 88 |
echo wp_kses_post( $args['after_widget'] ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Display widget settings form |
| 93 |
* |
| 94 |
* @param array $instance Widget instance variables |
| 95 |
* @since 3.6.0 |
| 96 |
*/ |
| 97 |
public function form( $instance ) { |
| 98 |
$title = ( isset( $instance['title'] ) ) ? $instance['title'] : ''; |
| 99 |
$post_type = ( isset( $instance['post_type'] ) ) ? $instance['post_type'] : []; |
| 100 |
|
| 101 |
$post_types_options = Comments::get_searchable_post_types(); |
| 102 |
?> |
| 103 |
<p> |
| 104 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> |
| 105 |
<?php esc_html_e( 'Title:', 'elasticpress' ); ?> |
| 106 |
</label> |
| 107 |
|
| 108 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 109 |
</p> |
| 110 |
<p> |
| 111 |
<label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>"> |
| 112 |
<?php esc_html_e( 'Search for comments on:', 'elasticpress' ); ?> |
| 113 |
</label> |
| 114 |
|
| 115 |
<?php foreach ( $post_types_options as $indexable_post_type => $label ) : ?> |
| 116 |
<p> |
| 117 |
<input |
| 118 |
class="checkbox" |
| 119 |
type="checkbox" |
| 120 |
id="<?php echo esc_attr( $this->get_field_id( 'post_type' ) . '-' . $indexable_post_type ); ?>" |
| 121 |
name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>[]" |
| 122 |
value="<?php echo esc_attr( $indexable_post_type ); ?>" |
| 123 |
<?php checked( in_array( $indexable_post_type, $post_type, true ) ); ?> |
| 124 |
/> |
| 125 |
<label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) . '-' . $indexable_post_type ); ?>"> |
| 126 |
<?php echo esc_html( $label ); ?> |
| 127 |
</label> |
| 128 |
</p> |
| 129 |
<?php endforeach; ?> |
| 130 |
</p> |
| 131 |
<?php |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Update widget settings |
| 136 |
* |
| 137 |
* @param array $new_instance New instance settings |
| 138 |
* @param array $old_instance Old instance settings |
| 139 |
* @since 3.6.0 |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function update( $new_instance, $old_instance ) { |
| 143 |
$instance = []; |
| 144 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 145 |
|
| 146 |
if ( is_array( $new_instance['post_type'] ) ) { |
| 147 |
$instance['post_type'] = array_map( |
| 148 |
'sanitize_text_field', |
| 149 |
$new_instance['post_type'] |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
return $instance; |
| 154 |
} |
| 155 |
} |
| 156 |
|